4 * Copyright (C) 1994-1999 Linus Torvalds
8 * The msync() system call.
10 #include <linux/slab.h>
11 #include <linux/pagemap.h>
13 #include <linux/mman.h>
14 #include <linux/hugetlb.h>
15 #include <linux/syscalls.h>
17 #include <asm/pgtable.h>
18 #include <asm/tlbflush.h>
20 static void msync_pte_range(struct vm_area_struct
*vma
, pmd_t
*pmd
,
21 unsigned long addr
, unsigned long end
)
28 pte
= pte_offset_map_lock(vma
->vm_mm
, pmd
, addr
, &ptl
);
35 if (need_resched() || need_lockbreak(ptl
))
39 if (!pte_present(*pte
))
41 if (!pte_maybe_dirty(*pte
))
44 if (unlikely(!pfn_valid(pfn
))) {
45 print_bad_pte(vma
, *pte
, addr
);
48 page
= pfn_to_page(pfn
);
50 if (ptep_clear_flush_dirty(vma
, addr
, pte
) ||
51 page_test_and_clear_dirty(page
))
54 } while (pte
++, addr
+= PAGE_SIZE
, addr
!= end
);
55 pte_unmap_unlock(pte
- 1, ptl
);
61 static inline void msync_pmd_range(struct vm_area_struct
*vma
, pud_t
*pud
,
62 unsigned long addr
, unsigned long end
)
67 pmd
= pmd_offset(pud
, addr
);
69 next
= pmd_addr_end(addr
, end
);
70 if (pmd_none_or_clear_bad(pmd
))
72 msync_pte_range(vma
, pmd
, addr
, next
);
73 } while (pmd
++, addr
= next
, addr
!= end
);
76 static inline void msync_pud_range(struct vm_area_struct
*vma
, pgd_t
*pgd
,
77 unsigned long addr
, unsigned long end
)
82 pud
= pud_offset(pgd
, addr
);
84 next
= pud_addr_end(addr
, end
);
85 if (pud_none_or_clear_bad(pud
))
87 msync_pmd_range(vma
, pud
, addr
, next
);
88 } while (pud
++, addr
= next
, addr
!= end
);
91 static void msync_page_range(struct vm_area_struct
*vma
,
92 unsigned long addr
, unsigned long end
)
97 /* For hugepages we can't go walking the page table normally,
98 * but that's ok, hugetlbfs is memory based, so we don't need
99 * to do anything more on an msync().
100 * Can't do anything with VM_RESERVED regions either.
102 if (vma
->vm_flags
& (VM_HUGETLB
|VM_RESERVED
))
106 pgd
= pgd_offset(vma
->vm_mm
, addr
);
107 flush_cache_range(vma
, addr
, end
);
109 next
= pgd_addr_end(addr
, end
);
110 if (pgd_none_or_clear_bad(pgd
))
112 msync_pud_range(vma
, pgd
, addr
, next
);
113 } while (pgd
++, addr
= next
, addr
!= end
);
117 * MS_SYNC syncs the entire file - including mappings.
119 * MS_ASYNC does not start I/O (it used to, up to 2.5.67). Instead, it just
120 * marks the relevant pages dirty. The application may now run fsync() to
121 * write out the dirty pages and wait on the writeout and check the result.
122 * Or the application may run fadvise(FADV_DONTNEED) against the fd to start
123 * async writeout immediately.
124 * So my _not_ starting I/O in MS_ASYNC we provide complete flexibility to
127 static int msync_interval(struct vm_area_struct
*vma
,
128 unsigned long addr
, unsigned long end
, int flags
)
131 struct file
*file
= vma
->vm_file
;
133 if ((flags
& MS_INVALIDATE
) && (vma
->vm_flags
& VM_LOCKED
))
136 if (file
&& (vma
->vm_flags
& VM_SHARED
)) {
137 msync_page_range(vma
, addr
, end
);
139 if (flags
& MS_SYNC
) {
140 struct address_space
*mapping
= file
->f_mapping
;
143 ret
= filemap_fdatawrite(mapping
);
144 if (file
->f_op
&& file
->f_op
->fsync
) {
146 * We don't take i_sem here because mmap_sem
149 err
= file
->f_op
->fsync(file
,file
->f_dentry
,1);
153 err
= filemap_fdatawait(mapping
);
161 asmlinkage
long sys_msync(unsigned long start
, size_t len
, int flags
)
164 struct vm_area_struct
*vma
;
165 int unmapped_error
, error
= -EINVAL
;
168 current
->flags
|= PF_SYNCWRITE
;
170 down_read(¤t
->mm
->mmap_sem
);
171 if (flags
& ~(MS_ASYNC
| MS_INVALIDATE
| MS_SYNC
))
173 if (start
& ~PAGE_MASK
)
175 if ((flags
& MS_ASYNC
) && (flags
& MS_SYNC
))
178 len
= (len
+ ~PAGE_MASK
) & PAGE_MASK
;
186 * If the interval [start,end) covers some unmapped address ranges,
187 * just ignore them, but return -ENOMEM at the end.
189 vma
= find_vma(current
->mm
, start
);
192 /* Still start < end. */
196 /* Here start < vma->vm_end. */
197 if (start
< vma
->vm_start
) {
198 unmapped_error
= -ENOMEM
;
199 start
= vma
->vm_start
;
201 /* Here vma->vm_start <= start < vma->vm_end. */
202 if (end
<= vma
->vm_end
) {
204 error
= msync_interval(vma
, start
, end
, flags
);
208 error
= unmapped_error
;
211 /* Here vma->vm_start <= start < vma->vm_end < end. */
212 error
= msync_interval(vma
, start
, vma
->vm_end
, flags
);
219 up_read(¤t
->mm
->mmap_sem
);
220 current
->flags
&= ~PF_SYNCWRITE
;