Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/mm/mlock.c |
| 3 | * |
| 4 | * (C) Copyright 1995 Linus Torvalds |
| 5 | * (C) Copyright 2002 Christoph Hellwig |
| 6 | */ |
| 7 | |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 8 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/mman.h> |
| 10 | #include <linux/mm.h> |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 11 | #include <linux/swap.h> |
| 12 | #include <linux/swapops.h> |
| 13 | #include <linux/pagemap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/mempolicy.h> |
| 15 | #include <linux/syscalls.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 16 | #include <linux/sched.h> |
Paul Gortmaker | b95f1b31 | 2011-10-16 02:01:52 -0400 | [diff] [blame] | 17 | #include <linux/export.h> |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 18 | #include <linux/rmap.h> |
| 19 | #include <linux/mmzone.h> |
| 20 | #include <linux/hugetlb.h> |
| 21 | |
| 22 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 24 | int can_do_mlock(void) |
| 25 | { |
| 26 | if (capable(CAP_IPC_LOCK)) |
| 27 | return 1; |
Jiri Slaby | 59e99e5 | 2010-03-05 13:41:44 -0800 | [diff] [blame] | 28 | if (rlimit(RLIMIT_MEMLOCK) != 0) |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 29 | return 1; |
| 30 | return 0; |
| 31 | } |
| 32 | EXPORT_SYMBOL(can_do_mlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 34 | /* |
| 35 | * Mlocked pages are marked with PageMlocked() flag for efficient testing |
| 36 | * in vmscan and, possibly, the fault path; and to support semi-accurate |
| 37 | * statistics. |
| 38 | * |
| 39 | * An mlocked page [PageMlocked(page)] is unevictable. As such, it will |
| 40 | * be placed on the LRU "unevictable" list, rather than the [in]active lists. |
| 41 | * The unevictable list is an LRU sibling list to the [in]active lists. |
| 42 | * PageUnevictable is set to indicate the unevictable state. |
| 43 | * |
| 44 | * When lazy mlocking via vmscan, it is important to ensure that the |
| 45 | * vma's VM_LOCKED status is not concurrently being modified, otherwise we |
| 46 | * may have mlocked a page that is being munlocked. So lazy mlock must take |
| 47 | * the mmap_sem for read, and verify that the vma really is locked |
| 48 | * (see mm/rmap.c). |
| 49 | */ |
| 50 | |
| 51 | /* |
| 52 | * LRU accounting for clear_page_mlock() |
| 53 | */ |
Hugh Dickins | e6c509f | 2012-10-08 16:33:19 -0700 | [diff] [blame] | 54 | void clear_page_mlock(struct page *page) |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 55 | { |
Hugh Dickins | e6c509f | 2012-10-08 16:33:19 -0700 | [diff] [blame] | 56 | if (!TestClearPageMlocked(page)) |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 57 | return; |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 58 | |
David Rientjes | 8449d21 | 2012-10-08 16:34:06 -0700 | [diff] [blame] | 59 | mod_zone_page_state(page_zone(page), NR_MLOCK, |
| 60 | -hpage_nr_pages(page)); |
Nick Piggin | 5344b7e | 2008-10-18 20:26:51 -0700 | [diff] [blame] | 61 | count_vm_event(UNEVICTABLE_PGCLEARED); |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 62 | if (!isolate_lru_page(page)) { |
| 63 | putback_lru_page(page); |
| 64 | } else { |
| 65 | /* |
KOSAKI Motohiro | 8891d6d | 2008-11-12 13:26:53 -0800 | [diff] [blame] | 66 | * We lost the race. the page already moved to evictable list. |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 67 | */ |
KOSAKI Motohiro | 8891d6d | 2008-11-12 13:26:53 -0800 | [diff] [blame] | 68 | if (PageUnevictable(page)) |
Nick Piggin | 5344b7e | 2008-10-18 20:26:51 -0700 | [diff] [blame] | 69 | count_vm_event(UNEVICTABLE_PGSTRANDED); |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Mark page as mlocked if not already. |
| 75 | * If page on LRU, isolate and putback to move to unevictable list. |
| 76 | */ |
| 77 | void mlock_vma_page(struct page *page) |
| 78 | { |
| 79 | BUG_ON(!PageLocked(page)); |
| 80 | |
Nick Piggin | 5344b7e | 2008-10-18 20:26:51 -0700 | [diff] [blame] | 81 | if (!TestSetPageMlocked(page)) { |
David Rientjes | 8449d21 | 2012-10-08 16:34:06 -0700 | [diff] [blame] | 82 | mod_zone_page_state(page_zone(page), NR_MLOCK, |
| 83 | hpage_nr_pages(page)); |
Nick Piggin | 5344b7e | 2008-10-18 20:26:51 -0700 | [diff] [blame] | 84 | count_vm_event(UNEVICTABLE_PGMLOCKED); |
| 85 | if (!isolate_lru_page(page)) |
| 86 | putback_lru_page(page); |
| 87 | } |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Lee Schermerhorn | 6927c1d | 2009-12-14 17:59:55 -0800 | [diff] [blame] | 90 | /** |
| 91 | * munlock_vma_page - munlock a vma page |
| 92 | * @page - page to be unlocked |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 93 | * |
Lee Schermerhorn | 6927c1d | 2009-12-14 17:59:55 -0800 | [diff] [blame] | 94 | * called from munlock()/munmap() path with page supposedly on the LRU. |
| 95 | * When we munlock a page, because the vma where we found the page is being |
| 96 | * munlock()ed or munmap()ed, we want to check whether other vmas hold the |
| 97 | * page locked so that we can leave it on the unevictable lru list and not |
| 98 | * bother vmscan with it. However, to walk the page's rmap list in |
| 99 | * try_to_munlock() we must isolate the page from the LRU. If some other |
| 100 | * task has removed the page from the LRU, we won't be able to do that. |
| 101 | * So we clear the PageMlocked as we might not get another chance. If we |
| 102 | * can't isolate the page, we leave it for putback_lru_page() and vmscan |
| 103 | * [page_referenced()/try_to_unmap()] to deal with. |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 104 | */ |
Michel Lespinasse | ff6a6da | 2013-02-27 17:02:44 -0800 | [diff] [blame] | 105 | unsigned int munlock_vma_page(struct page *page) |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 106 | { |
Michel Lespinasse | ff6a6da | 2013-02-27 17:02:44 -0800 | [diff] [blame] | 107 | unsigned int page_mask = 0; |
| 108 | |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 109 | BUG_ON(!PageLocked(page)); |
| 110 | |
Nick Piggin | 5344b7e | 2008-10-18 20:26:51 -0700 | [diff] [blame] | 111 | if (TestClearPageMlocked(page)) { |
Michel Lespinasse | ff6a6da | 2013-02-27 17:02:44 -0800 | [diff] [blame] | 112 | unsigned int nr_pages = hpage_nr_pages(page); |
| 113 | mod_zone_page_state(page_zone(page), NR_MLOCK, -nr_pages); |
| 114 | page_mask = nr_pages - 1; |
Nick Piggin | 5344b7e | 2008-10-18 20:26:51 -0700 | [diff] [blame] | 115 | if (!isolate_lru_page(page)) { |
Hugh Dickins | 3d470fc | 2011-10-31 17:09:43 -0700 | [diff] [blame] | 116 | int ret = SWAP_AGAIN; |
| 117 | |
| 118 | /* |
| 119 | * Optimization: if the page was mapped just once, |
| 120 | * that's our mapping and we don't need to check all the |
| 121 | * other vmas. |
| 122 | */ |
| 123 | if (page_mapcount(page) > 1) |
| 124 | ret = try_to_munlock(page); |
Nick Piggin | 5344b7e | 2008-10-18 20:26:51 -0700 | [diff] [blame] | 125 | /* |
| 126 | * did try_to_unlock() succeed or punt? |
| 127 | */ |
Hugh Dickins | 53f79ac | 2009-12-14 17:58:58 -0800 | [diff] [blame] | 128 | if (ret != SWAP_MLOCK) |
Nick Piggin | 5344b7e | 2008-10-18 20:26:51 -0700 | [diff] [blame] | 129 | count_vm_event(UNEVICTABLE_PGMUNLOCKED); |
| 130 | |
| 131 | putback_lru_page(page); |
| 132 | } else { |
| 133 | /* |
Lee Schermerhorn | 6927c1d | 2009-12-14 17:59:55 -0800 | [diff] [blame] | 134 | * Some other task has removed the page from the LRU. |
| 135 | * putback_lru_page() will take care of removing the |
| 136 | * page from the unevictable list, if necessary. |
| 137 | * vmscan [page_referenced()] will move the page back |
| 138 | * to the unevictable list if some other vma has it |
| 139 | * mlocked. |
Nick Piggin | 5344b7e | 2008-10-18 20:26:51 -0700 | [diff] [blame] | 140 | */ |
| 141 | if (PageUnevictable(page)) |
| 142 | count_vm_event(UNEVICTABLE_PGSTRANDED); |
| 143 | else |
| 144 | count_vm_event(UNEVICTABLE_PGMUNLOCKED); |
| 145 | } |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 146 | } |
Michel Lespinasse | ff6a6da | 2013-02-27 17:02:44 -0800 | [diff] [blame] | 147 | |
| 148 | return page_mask; |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Rik van Riel | ba470de | 2008-10-18 20:26:50 -0700 | [diff] [blame] | 151 | /** |
Hugh Dickins | 408e82b | 2009-09-21 17:03:23 -0700 | [diff] [blame] | 152 | * __mlock_vma_pages_range() - mlock a range of pages in the vma. |
Rik van Riel | ba470de | 2008-10-18 20:26:50 -0700 | [diff] [blame] | 153 | * @vma: target vma |
| 154 | * @start: start address |
| 155 | * @end: end address |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 156 | * |
Hugh Dickins | 408e82b | 2009-09-21 17:03:23 -0700 | [diff] [blame] | 157 | * This takes care of making the pages present too. |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 158 | * |
Rik van Riel | ba470de | 2008-10-18 20:26:50 -0700 | [diff] [blame] | 159 | * return 0 on success, negative error code on error. |
| 160 | * |
| 161 | * vma->vm_mm->mmap_sem must be held for at least read. |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 162 | */ |
Michel Lespinasse | cea10a1 | 2013-02-22 16:32:44 -0800 | [diff] [blame] | 163 | long __mlock_vma_pages_range(struct vm_area_struct *vma, |
| 164 | unsigned long start, unsigned long end, int *nonblocking) |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 165 | { |
| 166 | struct mm_struct *mm = vma->vm_mm; |
Michel Lespinasse | 28a3571 | 2013-02-22 16:35:55 -0800 | [diff] [blame] | 167 | unsigned long nr_pages = (end - start) / PAGE_SIZE; |
Hugh Dickins | 408e82b | 2009-09-21 17:03:23 -0700 | [diff] [blame] | 168 | int gup_flags; |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 169 | |
Rik van Riel | ba470de | 2008-10-18 20:26:50 -0700 | [diff] [blame] | 170 | VM_BUG_ON(start & ~PAGE_MASK); |
| 171 | VM_BUG_ON(end & ~PAGE_MASK); |
| 172 | VM_BUG_ON(start < vma->vm_start); |
| 173 | VM_BUG_ON(end > vma->vm_end); |
Hugh Dickins | 408e82b | 2009-09-21 17:03:23 -0700 | [diff] [blame] | 174 | VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem)); |
Rik van Riel | ba470de | 2008-10-18 20:26:50 -0700 | [diff] [blame] | 175 | |
Linus Torvalds | a1fde08 | 2011-05-04 21:30:28 -0700 | [diff] [blame] | 176 | gup_flags = FOLL_TOUCH | FOLL_MLOCK; |
Michel Lespinasse | 5ecfda0 | 2011-01-13 15:46:09 -0800 | [diff] [blame] | 177 | /* |
| 178 | * We want to touch writable mappings with a write fault in order |
| 179 | * to break COW, except for shared mappings because these don't COW |
| 180 | * and we would not want to dirty them for nothing. |
| 181 | */ |
| 182 | if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE) |
Hugh Dickins | 58fa879 | 2009-09-21 17:03:31 -0700 | [diff] [blame] | 183 | gup_flags |= FOLL_WRITE; |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 184 | |
Michel Lespinasse | fdf4c58 | 2011-01-31 17:03:41 -0800 | [diff] [blame] | 185 | /* |
| 186 | * We want mlock to succeed for regions that have any permissions |
| 187 | * other than PROT_NONE. |
| 188 | */ |
| 189 | if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)) |
| 190 | gup_flags |= FOLL_FORCE; |
| 191 | |
Johannes Weiner | 4805b02 | 2013-02-22 16:35:20 -0800 | [diff] [blame] | 192 | /* |
| 193 | * We made sure addr is within a VMA, so the following will |
| 194 | * not result in a stack expansion that recurses back here. |
| 195 | */ |
Michel Lespinasse | ff6a6da | 2013-02-27 17:02:44 -0800 | [diff] [blame] | 196 | return __get_user_pages(current, mm, start, nr_pages, gup_flags, |
Michel Lespinasse | 53a7706 | 2011-01-13 15:46:14 -0800 | [diff] [blame] | 197 | NULL, NULL, nonblocking); |
Lee Schermerhorn | 9978ad5 | 2008-10-18 20:26:56 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* |
| 201 | * convert get_user_pages() return value to posix mlock() error |
| 202 | */ |
| 203 | static int __mlock_posix_error_return(long retval) |
| 204 | { |
| 205 | if (retval == -EFAULT) |
| 206 | retval = -ENOMEM; |
| 207 | else if (retval == -ENOMEM) |
| 208 | retval = -EAGAIN; |
| 209 | return retval; |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 212 | /* |
Rik van Riel | ba470de | 2008-10-18 20:26:50 -0700 | [diff] [blame] | 213 | * munlock_vma_pages_range() - munlock all pages in the vma range.' |
| 214 | * @vma - vma containing range to be munlock()ed. |
| 215 | * @start - start address in @vma of the range |
| 216 | * @end - end of range in @vma. |
| 217 | * |
| 218 | * For mremap(), munmap() and exit(). |
| 219 | * |
| 220 | * Called with @vma VM_LOCKED. |
| 221 | * |
| 222 | * Returns with VM_LOCKED cleared. Callers must be prepared to |
| 223 | * deal with this. |
| 224 | * |
| 225 | * We don't save and restore VM_LOCKED here because pages are |
| 226 | * still on lru. In unmap path, pages might be scanned by reclaim |
| 227 | * and re-mlocked by try_to_{munlock|unmap} before we unmap and |
| 228 | * free them. This will result in freeing mlocked pages. |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 229 | */ |
Rik van Riel | ba470de | 2008-10-18 20:26:50 -0700 | [diff] [blame] | 230 | void munlock_vma_pages_range(struct vm_area_struct *vma, |
Hugh Dickins | 408e82b | 2009-09-21 17:03:23 -0700 | [diff] [blame] | 231 | unsigned long start, unsigned long end) |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 232 | { |
| 233 | vma->vm_flags &= ~VM_LOCKED; |
Hugh Dickins | 408e82b | 2009-09-21 17:03:23 -0700 | [diff] [blame] | 234 | |
Michel Lespinasse | ff6a6da | 2013-02-27 17:02:44 -0800 | [diff] [blame] | 235 | while (start < end) { |
Hugh Dickins | 6e91971 | 2009-09-21 17:03:32 -0700 | [diff] [blame] | 236 | struct page *page; |
Michel Lespinasse | ff6a6da | 2013-02-27 17:02:44 -0800 | [diff] [blame] | 237 | unsigned int page_mask, page_increm; |
| 238 | |
Hugh Dickins | 6e91971 | 2009-09-21 17:03:32 -0700 | [diff] [blame] | 239 | /* |
| 240 | * Although FOLL_DUMP is intended for get_dump_page(), |
| 241 | * it just so happens that its special treatment of the |
| 242 | * ZERO_PAGE (returning an error instead of doing get_page) |
| 243 | * suits munlock very well (and if somehow an abnormal page |
| 244 | * has sneaked into the range, we won't oops here: great). |
| 245 | */ |
Michel Lespinasse | ff6a6da | 2013-02-27 17:02:44 -0800 | [diff] [blame] | 246 | page = follow_page_mask(vma, start, FOLL_GET | FOLL_DUMP, |
| 247 | &page_mask); |
Hugh Dickins | 6e91971 | 2009-09-21 17:03:32 -0700 | [diff] [blame] | 248 | if (page && !IS_ERR(page)) { |
Hugh Dickins | 408e82b | 2009-09-21 17:03:23 -0700 | [diff] [blame] | 249 | lock_page(page); |
Michel Lespinasse | ff6a6da | 2013-02-27 17:02:44 -0800 | [diff] [blame] | 250 | lru_add_drain(); |
| 251 | /* |
| 252 | * Any THP page found by follow_page_mask() may have |
| 253 | * gotten split before reaching munlock_vma_page(), |
| 254 | * so we need to recompute the page_mask here. |
| 255 | */ |
| 256 | page_mask = munlock_vma_page(page); |
Hugh Dickins | 408e82b | 2009-09-21 17:03:23 -0700 | [diff] [blame] | 257 | unlock_page(page); |
| 258 | put_page(page); |
| 259 | } |
Michel Lespinasse | ff6a6da | 2013-02-27 17:02:44 -0800 | [diff] [blame] | 260 | page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask); |
| 261 | start += page_increm * PAGE_SIZE; |
Hugh Dickins | 408e82b | 2009-09-21 17:03:23 -0700 | [diff] [blame] | 262 | cond_resched(); |
| 263 | } |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /* |
| 267 | * mlock_fixup - handle mlock[all]/munlock[all] requests. |
| 268 | * |
| 269 | * Filters out "special" vmas -- VM_LOCKED never gets set for these, and |
| 270 | * munlock is a no-op. However, for some special vmas, we go ahead and |
Michel Lespinasse | cea10a1 | 2013-02-22 16:32:44 -0800 | [diff] [blame] | 271 | * populate the ptes. |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 272 | * |
| 273 | * For vmas that pass the filters, merge/split as appropriate. |
| 274 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev, |
KOSAKI Motohiro | ca16d14 | 2011-05-26 19:16:19 +0900 | [diff] [blame] | 276 | unsigned long start, unsigned long end, vm_flags_t newflags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | { |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 278 | struct mm_struct *mm = vma->vm_mm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | pgoff_t pgoff; |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 280 | int nr_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | int ret = 0; |
KOSAKI Motohiro | ca16d14 | 2011-05-26 19:16:19 +0900 | [diff] [blame] | 282 | int lock = !!(newflags & VM_LOCKED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
Michel Lespinasse | fed067d | 2011-01-13 15:46:10 -0800 | [diff] [blame] | 284 | if (newflags == vma->vm_flags || (vma->vm_flags & VM_SPECIAL) || |
Stephen Wilson | 31db58b | 2011-03-13 15:49:15 -0400 | [diff] [blame] | 285 | is_vm_hugetlb_page(vma) || vma == get_gate_vma(current->mm)) |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 286 | goto out; /* don't set VM_LOCKED, don't count */ |
| 287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT); |
| 289 | *prev = vma_merge(mm, *prev, start, end, newflags, vma->anon_vma, |
| 290 | vma->vm_file, pgoff, vma_policy(vma)); |
| 291 | if (*prev) { |
| 292 | vma = *prev; |
| 293 | goto success; |
| 294 | } |
| 295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | if (start != vma->vm_start) { |
| 297 | ret = split_vma(mm, vma, start, 1); |
| 298 | if (ret) |
| 299 | goto out; |
| 300 | } |
| 301 | |
| 302 | if (end != vma->vm_end) { |
| 303 | ret = split_vma(mm, vma, end, 0); |
| 304 | if (ret) |
| 305 | goto out; |
| 306 | } |
| 307 | |
| 308 | success: |
| 309 | /* |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 310 | * Keep track of amount of locked VM. |
| 311 | */ |
| 312 | nr_pages = (end - start) >> PAGE_SHIFT; |
| 313 | if (!lock) |
| 314 | nr_pages = -nr_pages; |
| 315 | mm->locked_vm += nr_pages; |
| 316 | |
| 317 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | * vm_flags is protected by the mmap_sem held in write mode. |
| 319 | * It's okay if try_to_unmap_one unmaps a page just after we |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 320 | * set VM_LOCKED, __mlock_vma_pages_range will bring it back. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
Michel Lespinasse | fed067d | 2011-01-13 15:46:10 -0800 | [diff] [blame] | 323 | if (lock) |
Hugh Dickins | 408e82b | 2009-09-21 17:03:23 -0700 | [diff] [blame] | 324 | vma->vm_flags = newflags; |
Michel Lespinasse | fed067d | 2011-01-13 15:46:10 -0800 | [diff] [blame] | 325 | else |
Hugh Dickins | 408e82b | 2009-09-21 17:03:23 -0700 | [diff] [blame] | 326 | munlock_vma_pages_range(vma, start, end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | out: |
Nick Piggin | b291f00 | 2008-10-18 20:26:44 -0700 | [diff] [blame] | 329 | *prev = vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | return ret; |
| 331 | } |
| 332 | |
| 333 | static int do_mlock(unsigned long start, size_t len, int on) |
| 334 | { |
| 335 | unsigned long nstart, end, tmp; |
| 336 | struct vm_area_struct * vma, * prev; |
| 337 | int error; |
| 338 | |
Michel Lespinasse | fed067d | 2011-01-13 15:46:10 -0800 | [diff] [blame] | 339 | VM_BUG_ON(start & ~PAGE_MASK); |
| 340 | VM_BUG_ON(len != PAGE_ALIGN(len)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | end = start + len; |
| 342 | if (end < start) |
| 343 | return -EINVAL; |
| 344 | if (end == start) |
| 345 | return 0; |
Linus Torvalds | 097d591 | 2012-03-06 18:23:36 -0800 | [diff] [blame] | 346 | vma = find_vma(current->mm, start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | if (!vma || vma->vm_start > start) |
| 348 | return -ENOMEM; |
| 349 | |
Linus Torvalds | 097d591 | 2012-03-06 18:23:36 -0800 | [diff] [blame] | 350 | prev = vma->vm_prev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | if (start > vma->vm_start) |
| 352 | prev = vma; |
| 353 | |
| 354 | for (nstart = start ; ; ) { |
KOSAKI Motohiro | ca16d14 | 2011-05-26 19:16:19 +0900 | [diff] [blame] | 355 | vm_flags_t newflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
| 357 | /* Here we know that vma->vm_start <= nstart < vma->vm_end. */ |
| 358 | |
Michel Lespinasse | 1869305 | 2013-02-22 16:32:46 -0800 | [diff] [blame] | 359 | newflags = vma->vm_flags & ~VM_LOCKED; |
| 360 | if (on) |
| 361 | newflags |= VM_LOCKED | VM_POPULATE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
| 363 | tmp = vma->vm_end; |
| 364 | if (tmp > end) |
| 365 | tmp = end; |
| 366 | error = mlock_fixup(vma, &prev, nstart, tmp, newflags); |
| 367 | if (error) |
| 368 | break; |
| 369 | nstart = tmp; |
| 370 | if (nstart < prev->vm_end) |
| 371 | nstart = prev->vm_end; |
| 372 | if (nstart >= end) |
| 373 | break; |
| 374 | |
| 375 | vma = prev->vm_next; |
| 376 | if (!vma || vma->vm_start != nstart) { |
| 377 | error = -ENOMEM; |
| 378 | break; |
| 379 | } |
| 380 | } |
| 381 | return error; |
| 382 | } |
| 383 | |
Michel Lespinasse | bebeb3d | 2013-02-22 16:32:37 -0800 | [diff] [blame] | 384 | /* |
| 385 | * __mm_populate - populate and/or mlock pages within a range of address space. |
| 386 | * |
| 387 | * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap |
| 388 | * flags. VMAs must be already marked with the desired vm_flags, and |
| 389 | * mmap_sem must not be held. |
| 390 | */ |
| 391 | int __mm_populate(unsigned long start, unsigned long len, int ignore_errors) |
Michel Lespinasse | fed067d | 2011-01-13 15:46:10 -0800 | [diff] [blame] | 392 | { |
| 393 | struct mm_struct *mm = current->mm; |
| 394 | unsigned long end, nstart, nend; |
| 395 | struct vm_area_struct *vma = NULL; |
Michel Lespinasse | 53a7706 | 2011-01-13 15:46:14 -0800 | [diff] [blame] | 396 | int locked = 0; |
Michel Lespinasse | 28a3571 | 2013-02-22 16:35:55 -0800 | [diff] [blame] | 397 | long ret = 0; |
Michel Lespinasse | fed067d | 2011-01-13 15:46:10 -0800 | [diff] [blame] | 398 | |
| 399 | VM_BUG_ON(start & ~PAGE_MASK); |
| 400 | VM_BUG_ON(len != PAGE_ALIGN(len)); |
| 401 | end = start + len; |
| 402 | |
Michel Lespinasse | fed067d | 2011-01-13 15:46:10 -0800 | [diff] [blame] | 403 | for (nstart = start; nstart < end; nstart = nend) { |
| 404 | /* |
| 405 | * We want to fault in pages for [nstart; end) address range. |
| 406 | * Find first corresponding VMA. |
| 407 | */ |
Michel Lespinasse | 53a7706 | 2011-01-13 15:46:14 -0800 | [diff] [blame] | 408 | if (!locked) { |
| 409 | locked = 1; |
| 410 | down_read(&mm->mmap_sem); |
Michel Lespinasse | fed067d | 2011-01-13 15:46:10 -0800 | [diff] [blame] | 411 | vma = find_vma(mm, nstart); |
Michel Lespinasse | 53a7706 | 2011-01-13 15:46:14 -0800 | [diff] [blame] | 412 | } else if (nstart >= vma->vm_end) |
Michel Lespinasse | fed067d | 2011-01-13 15:46:10 -0800 | [diff] [blame] | 413 | vma = vma->vm_next; |
| 414 | if (!vma || vma->vm_start >= end) |
| 415 | break; |
| 416 | /* |
| 417 | * Set [nstart; nend) to intersection of desired address |
| 418 | * range with the first VMA. Also, skip undesirable VMA types. |
| 419 | */ |
| 420 | nend = min(end, vma->vm_end); |
Michel Lespinasse | 1869305 | 2013-02-22 16:32:46 -0800 | [diff] [blame] | 421 | if ((vma->vm_flags & (VM_IO | VM_PFNMAP | VM_POPULATE)) != |
| 422 | VM_POPULATE) |
Michel Lespinasse | fed067d | 2011-01-13 15:46:10 -0800 | [diff] [blame] | 423 | continue; |
| 424 | if (nstart < vma->vm_start) |
| 425 | nstart = vma->vm_start; |
| 426 | /* |
Michel Lespinasse | 53a7706 | 2011-01-13 15:46:14 -0800 | [diff] [blame] | 427 | * Now fault in a range of pages. __mlock_vma_pages_range() |
| 428 | * double checks the vma flags, so that it won't mlock pages |
| 429 | * if the vma was already munlocked. |
Michel Lespinasse | fed067d | 2011-01-13 15:46:10 -0800 | [diff] [blame] | 430 | */ |
Michel Lespinasse | 53a7706 | 2011-01-13 15:46:14 -0800 | [diff] [blame] | 431 | ret = __mlock_vma_pages_range(vma, nstart, nend, &locked); |
| 432 | if (ret < 0) { |
| 433 | if (ignore_errors) { |
| 434 | ret = 0; |
| 435 | continue; /* continue at next VMA */ |
| 436 | } |
Michel Lespinasse | 5fdb200 | 2011-01-13 15:46:12 -0800 | [diff] [blame] | 437 | ret = __mlock_posix_error_return(ret); |
| 438 | break; |
| 439 | } |
Michel Lespinasse | 53a7706 | 2011-01-13 15:46:14 -0800 | [diff] [blame] | 440 | nend = nstart + ret * PAGE_SIZE; |
| 441 | ret = 0; |
Michel Lespinasse | fed067d | 2011-01-13 15:46:10 -0800 | [diff] [blame] | 442 | } |
Michel Lespinasse | 53a7706 | 2011-01-13 15:46:14 -0800 | [diff] [blame] | 443 | if (locked) |
| 444 | up_read(&mm->mmap_sem); |
Michel Lespinasse | fed067d | 2011-01-13 15:46:10 -0800 | [diff] [blame] | 445 | return ret; /* 0 or negative error code */ |
| 446 | } |
| 447 | |
Heiko Carstens | 6a6160a | 2009-01-14 14:14:15 +0100 | [diff] [blame] | 448 | SYSCALL_DEFINE2(mlock, unsigned long, start, size_t, len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | { |
| 450 | unsigned long locked; |
| 451 | unsigned long lock_limit; |
| 452 | int error = -ENOMEM; |
| 453 | |
| 454 | if (!can_do_mlock()) |
| 455 | return -EPERM; |
| 456 | |
KOSAKI Motohiro | 8891d6d | 2008-11-12 13:26:53 -0800 | [diff] [blame] | 457 | lru_add_drain_all(); /* flush pagevec */ |
| 458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | down_write(¤t->mm->mmap_sem); |
| 460 | len = PAGE_ALIGN(len + (start & ~PAGE_MASK)); |
| 461 | start &= PAGE_MASK; |
| 462 | |
| 463 | locked = len >> PAGE_SHIFT; |
| 464 | locked += current->mm->locked_vm; |
| 465 | |
Jiri Slaby | 59e99e5 | 2010-03-05 13:41:44 -0800 | [diff] [blame] | 466 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | lock_limit >>= PAGE_SHIFT; |
| 468 | |
| 469 | /* check against resource limits */ |
| 470 | if ((locked <= lock_limit) || capable(CAP_IPC_LOCK)) |
| 471 | error = do_mlock(start, len, 1); |
| 472 | up_write(¤t->mm->mmap_sem); |
Michel Lespinasse | fed067d | 2011-01-13 15:46:10 -0800 | [diff] [blame] | 473 | if (!error) |
Michel Lespinasse | bebeb3d | 2013-02-22 16:32:37 -0800 | [diff] [blame] | 474 | error = __mm_populate(start, len, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | return error; |
| 476 | } |
| 477 | |
Heiko Carstens | 6a6160a | 2009-01-14 14:14:15 +0100 | [diff] [blame] | 478 | SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | { |
| 480 | int ret; |
| 481 | |
| 482 | down_write(¤t->mm->mmap_sem); |
| 483 | len = PAGE_ALIGN(len + (start & ~PAGE_MASK)); |
| 484 | start &= PAGE_MASK; |
| 485 | ret = do_mlock(start, len, 0); |
| 486 | up_write(¤t->mm->mmap_sem); |
| 487 | return ret; |
| 488 | } |
| 489 | |
| 490 | static int do_mlockall(int flags) |
| 491 | { |
| 492 | struct vm_area_struct * vma, * prev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
| 494 | if (flags & MCL_FUTURE) |
Michel Lespinasse | 1869305 | 2013-02-22 16:32:46 -0800 | [diff] [blame] | 495 | current->mm->def_flags |= VM_LOCKED | VM_POPULATE; |
Gerald Schaefer | 9977f0f | 2013-02-12 13:46:20 -0800 | [diff] [blame] | 496 | else |
Michel Lespinasse | 1869305 | 2013-02-22 16:32:46 -0800 | [diff] [blame] | 497 | current->mm->def_flags &= ~(VM_LOCKED | VM_POPULATE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | if (flags == MCL_FUTURE) |
| 499 | goto out; |
| 500 | |
| 501 | for (vma = current->mm->mmap; vma ; vma = prev->vm_next) { |
KOSAKI Motohiro | ca16d14 | 2011-05-26 19:16:19 +0900 | [diff] [blame] | 502 | vm_flags_t newflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
Michel Lespinasse | 1869305 | 2013-02-22 16:32:46 -0800 | [diff] [blame] | 504 | newflags = vma->vm_flags & ~VM_LOCKED; |
| 505 | if (flags & MCL_CURRENT) |
| 506 | newflags |= VM_LOCKED | VM_POPULATE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
| 508 | /* Ignore errors */ |
| 509 | mlock_fixup(vma, &prev, vma->vm_start, vma->vm_end, newflags); |
| 510 | } |
| 511 | out: |
| 512 | return 0; |
| 513 | } |
| 514 | |
Heiko Carstens | 3480b25 | 2009-01-14 14:14:16 +0100 | [diff] [blame] | 515 | SYSCALL_DEFINE1(mlockall, int, flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | { |
| 517 | unsigned long lock_limit; |
| 518 | int ret = -EINVAL; |
| 519 | |
| 520 | if (!flags || (flags & ~(MCL_CURRENT | MCL_FUTURE))) |
| 521 | goto out; |
| 522 | |
| 523 | ret = -EPERM; |
| 524 | if (!can_do_mlock()) |
| 525 | goto out; |
| 526 | |
Christoph Lameter | df9d698 | 2011-10-31 17:09:35 -0700 | [diff] [blame] | 527 | if (flags & MCL_CURRENT) |
| 528 | lru_add_drain_all(); /* flush pagevec */ |
KOSAKI Motohiro | 8891d6d | 2008-11-12 13:26:53 -0800 | [diff] [blame] | 529 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | down_write(¤t->mm->mmap_sem); |
| 531 | |
Jiri Slaby | 59e99e5 | 2010-03-05 13:41:44 -0800 | [diff] [blame] | 532 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | lock_limit >>= PAGE_SHIFT; |
| 534 | |
| 535 | ret = -ENOMEM; |
| 536 | if (!(flags & MCL_CURRENT) || (current->mm->total_vm <= lock_limit) || |
| 537 | capable(CAP_IPC_LOCK)) |
| 538 | ret = do_mlockall(flags); |
| 539 | up_write(¤t->mm->mmap_sem); |
Michel Lespinasse | bebeb3d | 2013-02-22 16:32:37 -0800 | [diff] [blame] | 540 | if (!ret && (flags & MCL_CURRENT)) |
| 541 | mm_populate(0, TASK_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | out: |
| 543 | return ret; |
| 544 | } |
| 545 | |
Heiko Carstens | 3480b25 | 2009-01-14 14:14:16 +0100 | [diff] [blame] | 546 | SYSCALL_DEFINE0(munlockall) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | { |
| 548 | int ret; |
| 549 | |
| 550 | down_write(¤t->mm->mmap_sem); |
| 551 | ret = do_mlockall(0); |
| 552 | up_write(¤t->mm->mmap_sem); |
| 553 | return ret; |
| 554 | } |
| 555 | |
| 556 | /* |
| 557 | * Objects with different lifetime than processes (SHM_LOCK and SHM_HUGETLB |
| 558 | * shm segments) get accounted against the user_struct instead. |
| 559 | */ |
| 560 | static DEFINE_SPINLOCK(shmlock_user_lock); |
| 561 | |
| 562 | int user_shm_lock(size_t size, struct user_struct *user) |
| 563 | { |
| 564 | unsigned long lock_limit, locked; |
| 565 | int allowed = 0; |
| 566 | |
| 567 | locked = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Jiri Slaby | 59e99e5 | 2010-03-05 13:41:44 -0800 | [diff] [blame] | 568 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
Herbert van den Bergh | 5ed44a4 | 2007-07-15 23:38:25 -0700 | [diff] [blame] | 569 | if (lock_limit == RLIM_INFINITY) |
| 570 | allowed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | lock_limit >>= PAGE_SHIFT; |
| 572 | spin_lock(&shmlock_user_lock); |
Herbert van den Bergh | 5ed44a4 | 2007-07-15 23:38:25 -0700 | [diff] [blame] | 573 | if (!allowed && |
| 574 | locked + user->locked_shm > lock_limit && !capable(CAP_IPC_LOCK)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | goto out; |
| 576 | get_uid(user); |
| 577 | user->locked_shm += locked; |
| 578 | allowed = 1; |
| 579 | out: |
| 580 | spin_unlock(&shmlock_user_lock); |
| 581 | return allowed; |
| 582 | } |
| 583 | |
| 584 | void user_shm_unlock(size_t size, struct user_struct *user) |
| 585 | { |
| 586 | spin_lock(&shmlock_user_lock); |
| 587 | user->locked_shm -= (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 588 | spin_unlock(&shmlock_user_lock); |
| 589 | free_uid(user); |
| 590 | } |