xref: /linux/mm/msync.c (revision 707c21c848deeb0200ba3f07e4ba90e6dc419c2f)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	linux/mm/msync.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 1994-1999  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  * The msync() system call.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds #include <linux/slab.h>
111da177e4SLinus Torvalds #include <linux/pagemap.h>
121da177e4SLinus Torvalds #include <linux/mm.h>
131da177e4SLinus Torvalds #include <linux/mman.h>
141da177e4SLinus Torvalds #include <linux/hugetlb.h>
159c50823eSAndrew Morton #include <linux/writeback.h>
169c50823eSAndrew Morton #include <linux/file.h>
171da177e4SLinus Torvalds #include <linux/syscalls.h>
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <asm/pgtable.h>
201da177e4SLinus Torvalds #include <asm/tlbflush.h>
211da177e4SLinus Torvalds 
229c50823eSAndrew Morton static unsigned long msync_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
231da177e4SLinus Torvalds 				unsigned long addr, unsigned long end)
241da177e4SLinus Torvalds {
251da177e4SLinus Torvalds 	pte_t *pte;
26705e87c0SHugh Dickins 	spinlock_t *ptl;
270c942a45SHugh Dickins 	int progress = 0;
289c50823eSAndrew Morton 	unsigned long ret = 0;
291da177e4SLinus Torvalds 
300c942a45SHugh Dickins again:
31705e87c0SHugh Dickins 	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
321da177e4SLinus Torvalds 	do {
331da177e4SLinus Torvalds 		struct page *page;
341da177e4SLinus Torvalds 
350c942a45SHugh Dickins 		if (progress >= 64) {
360c942a45SHugh Dickins 			progress = 0;
37705e87c0SHugh Dickins 			if (need_resched() || need_lockbreak(ptl))
380c942a45SHugh Dickins 				break;
390c942a45SHugh Dickins 		}
400c942a45SHugh Dickins 		progress++;
411da177e4SLinus Torvalds 		if (!pte_present(*pte))
421da177e4SLinus Torvalds 			continue;
43b4955ce3SAbhijit Karmarkar 		if (!pte_maybe_dirty(*pte))
44b4955ce3SAbhijit Karmarkar 			continue;
456aab341eSLinus Torvalds 		page = vm_normal_page(vma, addr, *pte);
466aab341eSLinus Torvalds 		if (!page)
471da177e4SLinus Torvalds 			continue;
481da177e4SLinus Torvalds 		if (ptep_clear_flush_dirty(vma, addr, pte) ||
491da177e4SLinus Torvalds 				page_test_and_clear_dirty(page))
509c50823eSAndrew Morton 			ret += set_page_dirty(page);
510c942a45SHugh Dickins 		progress += 3;
521da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
53705e87c0SHugh Dickins 	pte_unmap_unlock(pte - 1, ptl);
54705e87c0SHugh Dickins 	cond_resched();
550c942a45SHugh Dickins 	if (addr != end)
560c942a45SHugh Dickins 		goto again;
579c50823eSAndrew Morton 	return ret;
581da177e4SLinus Torvalds }
591da177e4SLinus Torvalds 
609c50823eSAndrew Morton static inline unsigned long msync_pmd_range(struct vm_area_struct *vma,
619c50823eSAndrew Morton 			pud_t *pud, unsigned long addr, unsigned long end)
621da177e4SLinus Torvalds {
631da177e4SLinus Torvalds 	pmd_t *pmd;
641da177e4SLinus Torvalds 	unsigned long next;
659c50823eSAndrew Morton 	unsigned long ret = 0;
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
681da177e4SLinus Torvalds 	do {
691da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
701da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
711da177e4SLinus Torvalds 			continue;
729c50823eSAndrew Morton 		ret += msync_pte_range(vma, pmd, addr, next);
731da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
749c50823eSAndrew Morton 	return ret;
751da177e4SLinus Torvalds }
761da177e4SLinus Torvalds 
779c50823eSAndrew Morton static inline unsigned long msync_pud_range(struct vm_area_struct *vma,
789c50823eSAndrew Morton 			pgd_t *pgd, unsigned long addr, unsigned long end)
791da177e4SLinus Torvalds {
801da177e4SLinus Torvalds 	pud_t *pud;
811da177e4SLinus Torvalds 	unsigned long next;
829c50823eSAndrew Morton 	unsigned long ret = 0;
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
851da177e4SLinus Torvalds 	do {
861da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
871da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
881da177e4SLinus Torvalds 			continue;
899c50823eSAndrew Morton 		ret += msync_pmd_range(vma, pud, addr, next);
901da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
919c50823eSAndrew Morton 	return ret;
921da177e4SLinus Torvalds }
931da177e4SLinus Torvalds 
949c50823eSAndrew Morton static unsigned long msync_page_range(struct vm_area_struct *vma,
951da177e4SLinus Torvalds 				unsigned long addr, unsigned long end)
961da177e4SLinus Torvalds {
971da177e4SLinus Torvalds 	pgd_t *pgd;
981da177e4SLinus Torvalds 	unsigned long next;
999c50823eSAndrew Morton 	unsigned long ret = 0;
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds 	/* For hugepages we can't go walking the page table normally,
1021da177e4SLinus Torvalds 	 * but that's ok, hugetlbfs is memory based, so we don't need
103b5810039SNick Piggin 	 * to do anything more on an msync().
104b5810039SNick Piggin 	 */
1056aab341eSLinus Torvalds 	if (vma->vm_flags & VM_HUGETLB)
1069c50823eSAndrew Morton 		return 0;
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 	BUG_ON(addr >= end);
109705e87c0SHugh Dickins 	pgd = pgd_offset(vma->vm_mm, addr);
1101da177e4SLinus Torvalds 	flush_cache_range(vma, addr, end);
1111da177e4SLinus Torvalds 	do {
1121da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
1131da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
1141da177e4SLinus Torvalds 			continue;
1159c50823eSAndrew Morton 		ret += msync_pud_range(vma, pgd, addr, next);
1161da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
1179c50823eSAndrew Morton 	return ret;
1181da177e4SLinus Torvalds }
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds /*
1211da177e4SLinus Torvalds  * MS_SYNC syncs the entire file - including mappings.
1221da177e4SLinus Torvalds  *
1231da177e4SLinus Torvalds  * MS_ASYNC does not start I/O (it used to, up to 2.5.67).  Instead, it just
1241da177e4SLinus Torvalds  * marks the relevant pages dirty.  The application may now run fsync() to
1251da177e4SLinus Torvalds  * write out the dirty pages and wait on the writeout and check the result.
1261da177e4SLinus Torvalds  * Or the application may run fadvise(FADV_DONTNEED) against the fd to start
1271da177e4SLinus Torvalds  * async writeout immediately.
1281da177e4SLinus Torvalds  * So my _not_ starting I/O in MS_ASYNC we provide complete flexibility to
1291da177e4SLinus Torvalds  * applications.
1301da177e4SLinus Torvalds  */
1319c50823eSAndrew Morton static int msync_interval(struct vm_area_struct *vma, unsigned long addr,
1329c50823eSAndrew Morton 			unsigned long end, int flags,
1339c50823eSAndrew Morton 			unsigned long *nr_pages_dirtied)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	struct file *file = vma->vm_file;
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	if ((flags & MS_INVALIDATE) && (vma->vm_flags & VM_LOCKED))
1381da177e4SLinus Torvalds 		return -EBUSY;
1391da177e4SLinus Torvalds 
140*707c21c8SAndrew Morton 	if (file && (vma->vm_flags & VM_SHARED))
1419c50823eSAndrew Morton 		*nr_pages_dirtied = msync_page_range(vma, addr, end);
142*707c21c8SAndrew Morton 	return 0;
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds asmlinkage long sys_msync(unsigned long start, size_t len, int flags)
1461da177e4SLinus Torvalds {
1471da177e4SLinus Torvalds 	unsigned long end;
1481da177e4SLinus Torvalds 	struct vm_area_struct *vma;
1491da177e4SLinus Torvalds 	int unmapped_error, error = -EINVAL;
1509c50823eSAndrew Morton 	int done = 0;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
1531da177e4SLinus Torvalds 		goto out;
1541da177e4SLinus Torvalds 	if (start & ~PAGE_MASK)
1551da177e4SLinus Torvalds 		goto out;
1561da177e4SLinus Torvalds 	if ((flags & MS_ASYNC) && (flags & MS_SYNC))
1571da177e4SLinus Torvalds 		goto out;
1581da177e4SLinus Torvalds 	error = -ENOMEM;
1591da177e4SLinus Torvalds 	len = (len + ~PAGE_MASK) & PAGE_MASK;
1601da177e4SLinus Torvalds 	end = start + len;
1611da177e4SLinus Torvalds 	if (end < start)
1621da177e4SLinus Torvalds 		goto out;
1631da177e4SLinus Torvalds 	error = 0;
1641da177e4SLinus Torvalds 	if (end == start)
1651da177e4SLinus Torvalds 		goto out;
1661da177e4SLinus Torvalds 	/*
1671da177e4SLinus Torvalds 	 * If the interval [start,end) covers some unmapped address ranges,
1681da177e4SLinus Torvalds 	 * just ignore them, but return -ENOMEM at the end.
1691da177e4SLinus Torvalds 	 */
1709c50823eSAndrew Morton 	down_read(&current->mm->mmap_sem);
1719c50823eSAndrew Morton 	if (flags & MS_SYNC)
1729c50823eSAndrew Morton 		current->flags |= PF_SYNCWRITE;
1731da177e4SLinus Torvalds 	vma = find_vma(current->mm, start);
1741da177e4SLinus Torvalds 	unmapped_error = 0;
1759c50823eSAndrew Morton 	do {
1769c50823eSAndrew Morton 		unsigned long nr_pages_dirtied = 0;
1779c50823eSAndrew Morton 		struct file *file;
1789c50823eSAndrew Morton 
1791da177e4SLinus Torvalds 		/* Still start < end. */
1801da177e4SLinus Torvalds 		error = -ENOMEM;
1811da177e4SLinus Torvalds 		if (!vma)
1829c50823eSAndrew Morton 			goto out_unlock;
1831da177e4SLinus Torvalds 		/* Here start < vma->vm_end. */
1841da177e4SLinus Torvalds 		if (start < vma->vm_start) {
1851da177e4SLinus Torvalds 			unmapped_error = -ENOMEM;
1861da177e4SLinus Torvalds 			start = vma->vm_start;
1871da177e4SLinus Torvalds 		}
1881da177e4SLinus Torvalds 		/* Here vma->vm_start <= start < vma->vm_end. */
1891da177e4SLinus Torvalds 		if (end <= vma->vm_end) {
1901da177e4SLinus Torvalds 			if (start < end) {
1919c50823eSAndrew Morton 				error = msync_interval(vma, start, end, flags,
1929c50823eSAndrew Morton 							&nr_pages_dirtied);
1931da177e4SLinus Torvalds 				if (error)
1949c50823eSAndrew Morton 					goto out_unlock;
1951da177e4SLinus Torvalds 			}
1961da177e4SLinus Torvalds 			error = unmapped_error;
1979c50823eSAndrew Morton 			done = 1;
1989c50823eSAndrew Morton 		} else {
1991da177e4SLinus Torvalds 			/* Here vma->vm_start <= start < vma->vm_end < end. */
2009c50823eSAndrew Morton 			error = msync_interval(vma, start, vma->vm_end, flags,
2019c50823eSAndrew Morton 						&nr_pages_dirtied);
2021da177e4SLinus Torvalds 			if (error)
2039c50823eSAndrew Morton 				goto out_unlock;
2049c50823eSAndrew Morton 		}
2059c50823eSAndrew Morton 		file = vma->vm_file;
2061da177e4SLinus Torvalds 		start = vma->vm_end;
2079c50823eSAndrew Morton 		if ((flags & MS_ASYNC) && file && nr_pages_dirtied) {
2089c50823eSAndrew Morton 			get_file(file);
2099c50823eSAndrew Morton 			up_read(&current->mm->mmap_sem);
2109c50823eSAndrew Morton 			balance_dirty_pages_ratelimited_nr(file->f_mapping,
2119c50823eSAndrew Morton 							nr_pages_dirtied);
2129c50823eSAndrew Morton 			fput(file);
2139c50823eSAndrew Morton 			down_read(&current->mm->mmap_sem);
2149c50823eSAndrew Morton 			vma = find_vma(current->mm, start);
215*707c21c8SAndrew Morton 		} else if ((flags & MS_SYNC) && file &&
216*707c21c8SAndrew Morton 				(vma->vm_flags & VM_SHARED)) {
217*707c21c8SAndrew Morton 			struct address_space *mapping;
218*707c21c8SAndrew Morton 			int err;
219*707c21c8SAndrew Morton 
220*707c21c8SAndrew Morton 			get_file(file);
221*707c21c8SAndrew Morton 			up_read(&current->mm->mmap_sem);
222*707c21c8SAndrew Morton 			mapping = file->f_mapping;
223*707c21c8SAndrew Morton 			error = filemap_fdatawrite(mapping);
224*707c21c8SAndrew Morton 			if (file->f_op && file->f_op->fsync) {
225*707c21c8SAndrew Morton 				mutex_lock(&mapping->host->i_mutex);
226*707c21c8SAndrew Morton 				err = file->f_op->fsync(file,file->f_dentry,1);
227*707c21c8SAndrew Morton 				mutex_unlock(&mapping->host->i_mutex);
228*707c21c8SAndrew Morton 				if (err && !error)
229*707c21c8SAndrew Morton 					error = err;
230*707c21c8SAndrew Morton 			}
231*707c21c8SAndrew Morton 			err = filemap_fdatawait(mapping);
232*707c21c8SAndrew Morton 			if (err && !error)
233*707c21c8SAndrew Morton 				error = err;
234*707c21c8SAndrew Morton 			fput(file);
235*707c21c8SAndrew Morton 			down_read(&current->mm->mmap_sem);
236*707c21c8SAndrew Morton 			if (error)
237*707c21c8SAndrew Morton 				goto out_unlock;
238*707c21c8SAndrew Morton 			vma = find_vma(current->mm, start);
2399c50823eSAndrew Morton 		} else {
2401da177e4SLinus Torvalds 			vma = vma->vm_next;
2411da177e4SLinus Torvalds 		}
2429c50823eSAndrew Morton 	} while (!done);
2439c50823eSAndrew Morton out_unlock:
2441da177e4SLinus Torvalds 	current->flags &= ~PF_SYNCWRITE;
2459c50823eSAndrew Morton 	up_read(&current->mm->mmap_sem);
2469c50823eSAndrew Morton out:
2471da177e4SLinus Torvalds 	return error;
2481da177e4SLinus Torvalds }
249