Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/mm/msync.c |
| 3 | * |
| 4 | * Copyright (C) 1994-1999 Linus Torvalds |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * The msync() system call. |
| 9 | */ |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/pagemap.h> |
Andrew Morton | 8f2e9f1 | 2006-03-24 03:18:15 -0800 | [diff] [blame] | 12 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/mm.h> |
| 14 | #include <linux/mman.h> |
| 15 | #include <linux/hugetlb.h> |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 16 | #include <linux/writeback.h> |
| 17 | #include <linux/file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/syscalls.h> |
| 19 | |
| 20 | #include <asm/pgtable.h> |
| 21 | #include <asm/tlbflush.h> |
| 22 | |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 23 | static unsigned long msync_pte_range(struct vm_area_struct *vma, pmd_t *pmd, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | unsigned long addr, unsigned long end) |
| 25 | { |
| 26 | pte_t *pte; |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 27 | spinlock_t *ptl; |
Hugh Dickins | 0c942a4 | 2005-10-29 18:15:53 -0700 | [diff] [blame] | 28 | int progress = 0; |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 29 | unsigned long ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Hugh Dickins | 0c942a4 | 2005-10-29 18:15:53 -0700 | [diff] [blame] | 31 | again: |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 32 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | struct page *page; |
| 35 | |
Hugh Dickins | 0c942a4 | 2005-10-29 18:15:53 -0700 | [diff] [blame] | 36 | if (progress >= 64) { |
| 37 | progress = 0; |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 38 | if (need_resched() || need_lockbreak(ptl)) |
Hugh Dickins | 0c942a4 | 2005-10-29 18:15:53 -0700 | [diff] [blame] | 39 | break; |
| 40 | } |
| 41 | progress++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | if (!pte_present(*pte)) |
| 43 | continue; |
Abhijit Karmarkar | b4955ce | 2005-06-21 17:15:13 -0700 | [diff] [blame] | 44 | if (!pte_maybe_dirty(*pte)) |
| 45 | continue; |
Linus Torvalds | 6aab341 | 2005-11-28 14:34:23 -0800 | [diff] [blame] | 46 | page = vm_normal_page(vma, addr, *pte); |
| 47 | if (!page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | if (ptep_clear_flush_dirty(vma, addr, pte) || |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 50 | page_test_and_clear_dirty(page)) |
| 51 | ret += set_page_dirty(page); |
Hugh Dickins | 0c942a4 | 2005-10-29 18:15:53 -0700 | [diff] [blame] | 52 | progress += 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } while (pte++, addr += PAGE_SIZE, addr != end); |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 54 | pte_unmap_unlock(pte - 1, ptl); |
| 55 | cond_resched(); |
Hugh Dickins | 0c942a4 | 2005-10-29 18:15:53 -0700 | [diff] [blame] | 56 | if (addr != end) |
| 57 | goto again; |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 58 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 61 | static inline unsigned long msync_pmd_range(struct vm_area_struct *vma, |
| 62 | pud_t *pud, unsigned long addr, unsigned long end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
| 64 | pmd_t *pmd; |
| 65 | unsigned long next; |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 66 | unsigned long ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | pmd = pmd_offset(pud, addr); |
| 69 | do { |
| 70 | next = pmd_addr_end(addr, end); |
| 71 | if (pmd_none_or_clear_bad(pmd)) |
| 72 | continue; |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 73 | ret += msync_pte_range(vma, pmd, addr, next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } while (pmd++, addr = next, addr != end); |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 75 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 78 | static inline unsigned long msync_pud_range(struct vm_area_struct *vma, |
| 79 | pgd_t *pgd, unsigned long addr, unsigned long end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
| 81 | pud_t *pud; |
| 82 | unsigned long next; |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 83 | unsigned long ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
| 85 | pud = pud_offset(pgd, addr); |
| 86 | do { |
| 87 | next = pud_addr_end(addr, end); |
| 88 | if (pud_none_or_clear_bad(pud)) |
| 89 | continue; |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 90 | ret += msync_pmd_range(vma, pud, addr, next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } while (pud++, addr = next, addr != end); |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 92 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 95 | static unsigned long msync_page_range(struct vm_area_struct *vma, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | unsigned long addr, unsigned long end) |
| 97 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | pgd_t *pgd; |
| 99 | unsigned long next; |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 100 | unsigned long ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | /* For hugepages we can't go walking the page table normally, |
| 103 | * but that's ok, hugetlbfs is memory based, so we don't need |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 104 | * to do anything more on an msync(). |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 105 | */ |
Linus Torvalds | 6aab341 | 2005-11-28 14:34:23 -0800 | [diff] [blame] | 106 | if (vma->vm_flags & VM_HUGETLB) |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 107 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | BUG_ON(addr >= end); |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 110 | pgd = pgd_offset(vma->vm_mm, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | flush_cache_range(vma, addr, end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | do { |
| 113 | next = pgd_addr_end(addr, end); |
| 114 | if (pgd_none_or_clear_bad(pgd)) |
| 115 | continue; |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 116 | ret += msync_pud_range(vma, pgd, addr, next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } while (pgd++, addr = next, addr != end); |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 118 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | /* |
| 122 | * MS_SYNC syncs the entire file - including mappings. |
| 123 | * |
| 124 | * MS_ASYNC does not start I/O (it used to, up to 2.5.67). Instead, it just |
| 125 | * marks the relevant pages dirty. The application may now run fsync() to |
| 126 | * write out the dirty pages and wait on the writeout and check the result. |
| 127 | * Or the application may run fadvise(FADV_DONTNEED) against the fd to start |
| 128 | * async writeout immediately. |
Amos Waterland | 16538c4 | 2006-03-24 18:30:53 +0100 | [diff] [blame] | 129 | * So by _not_ starting I/O in MS_ASYNC we provide complete flexibility to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | * applications. |
| 131 | */ |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 132 | static int msync_interval(struct vm_area_struct *vma, unsigned long addr, |
| 133 | unsigned long end, int flags, |
| 134 | unsigned long *nr_pages_dirtied) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | struct file *file = vma->vm_file; |
| 137 | |
| 138 | if ((flags & MS_INVALIDATE) && (vma->vm_flags & VM_LOCKED)) |
| 139 | return -EBUSY; |
| 140 | |
Andrew Morton | 707c21c | 2006-03-24 03:18:13 -0800 | [diff] [blame] | 141 | if (file && (vma->vm_flags & VM_SHARED)) |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 142 | *nr_pages_dirtied = msync_page_range(vma, addr, end); |
Andrew Morton | 707c21c | 2006-03-24 03:18:13 -0800 | [diff] [blame] | 143 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | asmlinkage long sys_msync(unsigned long start, size_t len, int flags) |
| 147 | { |
| 148 | unsigned long end; |
| 149 | struct vm_area_struct *vma; |
Andrew Morton | 676758b | 2006-03-24 03:18:14 -0800 | [diff] [blame] | 150 | int unmapped_error = 0; |
| 151 | int error = -EINVAL; |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 152 | int done = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC)) |
| 155 | goto out; |
| 156 | if (start & ~PAGE_MASK) |
| 157 | goto out; |
| 158 | if ((flags & MS_ASYNC) && (flags & MS_SYNC)) |
| 159 | goto out; |
| 160 | error = -ENOMEM; |
| 161 | len = (len + ~PAGE_MASK) & PAGE_MASK; |
| 162 | end = start + len; |
| 163 | if (end < start) |
| 164 | goto out; |
| 165 | error = 0; |
| 166 | if (end == start) |
| 167 | goto out; |
| 168 | /* |
| 169 | * If the interval [start,end) covers some unmapped address ranges, |
| 170 | * just ignore them, but return -ENOMEM at the end. |
| 171 | */ |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 172 | down_read(¤t->mm->mmap_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | vma = find_vma(current->mm, start); |
Andrew Morton | 676758b | 2006-03-24 03:18:14 -0800 | [diff] [blame] | 174 | if (!vma) { |
| 175 | error = -ENOMEM; |
| 176 | goto out_unlock; |
| 177 | } |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 178 | do { |
| 179 | unsigned long nr_pages_dirtied = 0; |
| 180 | struct file *file; |
| 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | /* Here start < vma->vm_end. */ |
| 183 | if (start < vma->vm_start) { |
| 184 | unmapped_error = -ENOMEM; |
| 185 | start = vma->vm_start; |
| 186 | } |
| 187 | /* Here vma->vm_start <= start < vma->vm_end. */ |
| 188 | if (end <= vma->vm_end) { |
| 189 | if (start < end) { |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 190 | error = msync_interval(vma, start, end, flags, |
| 191 | &nr_pages_dirtied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | if (error) |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 193 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | error = unmapped_error; |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 196 | done = 1; |
| 197 | } else { |
| 198 | /* Here vma->vm_start <= start < vma->vm_end < end. */ |
| 199 | error = msync_interval(vma, start, vma->vm_end, flags, |
| 200 | &nr_pages_dirtied); |
| 201 | if (error) |
| 202 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | } |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 204 | file = vma->vm_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | start = vma->vm_end; |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 206 | if ((flags & MS_ASYNC) && file && nr_pages_dirtied) { |
| 207 | get_file(file); |
| 208 | up_read(¤t->mm->mmap_sem); |
| 209 | balance_dirty_pages_ratelimited_nr(file->f_mapping, |
| 210 | nr_pages_dirtied); |
| 211 | fput(file); |
| 212 | down_read(¤t->mm->mmap_sem); |
| 213 | vma = find_vma(current->mm, start); |
Andrew Morton | 707c21c | 2006-03-24 03:18:13 -0800 | [diff] [blame] | 214 | } else if ((flags & MS_SYNC) && file && |
| 215 | (vma->vm_flags & VM_SHARED)) { |
Andrew Morton | 707c21c | 2006-03-24 03:18:13 -0800 | [diff] [blame] | 216 | get_file(file); |
| 217 | up_read(¤t->mm->mmap_sem); |
Andrew Morton | 8f2e9f1 | 2006-03-24 03:18:15 -0800 | [diff] [blame] | 218 | error = do_fsync(file, 0); |
Andrew Morton | 707c21c | 2006-03-24 03:18:13 -0800 | [diff] [blame] | 219 | fput(file); |
| 220 | down_read(¤t->mm->mmap_sem); |
| 221 | if (error) |
| 222 | goto out_unlock; |
| 223 | vma = find_vma(current->mm, start); |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 224 | } else { |
| 225 | vma = vma->vm_next; |
| 226 | } |
Andrew Morton | 676758b | 2006-03-24 03:18:14 -0800 | [diff] [blame] | 227 | } while (vma && !done); |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 228 | out_unlock: |
Andrew Morton | 9c50823e | 2006-03-24 03:18:12 -0800 | [diff] [blame] | 229 | up_read(¤t->mm->mmap_sem); |
| 230 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | return error; |
| 232 | } |