1 /* 2 * linux/mm/mlock.c 3 * 4 * (C) Copyright 1995 Linus Torvalds 5 * (C) Copyright 2002 Christoph Hellwig 6 */ 7 8 #include <linux/capability.h> 9 #include <linux/mman.h> 10 #include <linux/mm.h> 11 #include <linux/swap.h> 12 #include <linux/swapops.h> 13 #include <linux/pagemap.h> 14 #include <linux/mempolicy.h> 15 #include <linux/syscalls.h> 16 #include <linux/sched.h> 17 #include <linux/export.h> 18 #include <linux/rmap.h> 19 #include <linux/mmzone.h> 20 #include <linux/hugetlb.h> 21 22 #include "internal.h" 23 24 int can_do_mlock(void) 25 { 26 if (capable(CAP_IPC_LOCK)) 27 return 1; 28 if (rlimit(RLIMIT_MEMLOCK) != 0) 29 return 1; 30 return 0; 31 } 32 EXPORT_SYMBOL(can_do_mlock); 33 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 */ 54 void clear_page_mlock(struct page *page) 55 { 56 if (!TestClearPageMlocked(page)) 57 return; 58 59 mod_zone_page_state(page_zone(page), NR_MLOCK, 60 -hpage_nr_pages(page)); 61 count_vm_event(UNEVICTABLE_PGCLEARED); 62 if (!isolate_lru_page(page)) { 63 putback_lru_page(page); 64 } else { 65 /* 66 * We lost the race. the page already moved to evictable list. 67 */ 68 if (PageUnevictable(page)) 69 count_vm_event(UNEVICTABLE_PGSTRANDED); 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 81 if (!TestSetPageMlocked(page)) { 82 mod_zone_page_state(page_zone(page), NR_MLOCK, 83 hpage_nr_pages(page)); 84 count_vm_event(UNEVICTABLE_PGMLOCKED); 85 if (!isolate_lru_page(page)) 86 putback_lru_page(page); 87 } 88 } 89 90 /** 91 * munlock_vma_page - munlock a vma page 92 * @page - page to be unlocked 93 * 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. 104 */ 105 unsigned int munlock_vma_page(struct page *page) 106 { 107 unsigned int page_mask = 0; 108 109 BUG_ON(!PageLocked(page)); 110 111 if (TestClearPageMlocked(page)) { 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; 115 if (!isolate_lru_page(page)) { 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); 125 /* 126 * did try_to_unlock() succeed or punt? 127 */ 128 if (ret != SWAP_MLOCK) 129 count_vm_event(UNEVICTABLE_PGMUNLOCKED); 130 131 putback_lru_page(page); 132 } else { 133 /* 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. 140 */ 141 if (PageUnevictable(page)) 142 count_vm_event(UNEVICTABLE_PGSTRANDED); 143 else 144 count_vm_event(UNEVICTABLE_PGMUNLOCKED); 145 } 146 } 147 148 return page_mask; 149 } 150 151 /** 152 * __mlock_vma_pages_range() - mlock a range of pages in the vma. 153 * @vma: target vma 154 * @start: start address 155 * @end: end address 156 * 157 * This takes care of making the pages present too. 158 * 159 * return 0 on success, negative error code on error. 160 * 161 * vma->vm_mm->mmap_sem must be held for at least read. 162 */ 163 long __mlock_vma_pages_range(struct vm_area_struct *vma, 164 unsigned long start, unsigned long end, int *nonblocking) 165 { 166 struct mm_struct *mm = vma->vm_mm; 167 unsigned long nr_pages = (end - start) / PAGE_SIZE; 168 int gup_flags; 169 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); 174 VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem)); 175 176 gup_flags = FOLL_TOUCH | FOLL_MLOCK; 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) 183 gup_flags |= FOLL_WRITE; 184 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 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 */ 196 return __get_user_pages(current, mm, start, nr_pages, gup_flags, 197 NULL, NULL, nonblocking); 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; 210 } 211 212 /* 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. 229 */ 230 void munlock_vma_pages_range(struct vm_area_struct *vma, 231 unsigned long start, unsigned long end) 232 { 233 vma->vm_flags &= ~VM_LOCKED; 234 235 while (start < end) { 236 struct page *page; 237 unsigned int page_mask, page_increm; 238 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 */ 246 page = follow_page_mask(vma, start, FOLL_GET | FOLL_DUMP, 247 &page_mask); 248 if (page && !IS_ERR(page)) { 249 lock_page(page); 250 /* 251 * Any THP page found by follow_page_mask() may have 252 * gotten split before reaching munlock_vma_page(), 253 * so we need to recompute the page_mask here. 254 */ 255 page_mask = munlock_vma_page(page); 256 unlock_page(page); 257 put_page(page); 258 } 259 page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask); 260 start += page_increm * PAGE_SIZE; 261 cond_resched(); 262 } 263 } 264 265 /* 266 * mlock_fixup - handle mlock[all]/munlock[all] requests. 267 * 268 * Filters out "special" vmas -- VM_LOCKED never gets set for these, and 269 * munlock is a no-op. However, for some special vmas, we go ahead and 270 * populate the ptes. 271 * 272 * For vmas that pass the filters, merge/split as appropriate. 273 */ 274 static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev, 275 unsigned long start, unsigned long end, vm_flags_t newflags) 276 { 277 struct mm_struct *mm = vma->vm_mm; 278 pgoff_t pgoff; 279 int nr_pages; 280 int ret = 0; 281 int lock = !!(newflags & VM_LOCKED); 282 283 if (newflags == vma->vm_flags || (vma->vm_flags & VM_SPECIAL) || 284 is_vm_hugetlb_page(vma) || vma == get_gate_vma(current->mm)) 285 goto out; /* don't set VM_LOCKED, don't count */ 286 287 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT); 288 *prev = vma_merge(mm, *prev, start, end, newflags, vma->anon_vma, 289 vma->vm_file, pgoff, vma_policy(vma)); 290 if (*prev) { 291 vma = *prev; 292 goto success; 293 } 294 295 if (start != vma->vm_start) { 296 ret = split_vma(mm, vma, start, 1); 297 if (ret) 298 goto out; 299 } 300 301 if (end != vma->vm_end) { 302 ret = split_vma(mm, vma, end, 0); 303 if (ret) 304 goto out; 305 } 306 307 success: 308 /* 309 * Keep track of amount of locked VM. 310 */ 311 nr_pages = (end - start) >> PAGE_SHIFT; 312 if (!lock) 313 nr_pages = -nr_pages; 314 mm->locked_vm += nr_pages; 315 316 /* 317 * vm_flags is protected by the mmap_sem held in write mode. 318 * It's okay if try_to_unmap_one unmaps a page just after we 319 * set VM_LOCKED, __mlock_vma_pages_range will bring it back. 320 */ 321 322 if (lock) 323 vma->vm_flags = newflags; 324 else 325 munlock_vma_pages_range(vma, start, end); 326 327 out: 328 *prev = vma; 329 return ret; 330 } 331 332 static int do_mlock(unsigned long start, size_t len, int on) 333 { 334 unsigned long nstart, end, tmp; 335 struct vm_area_struct * vma, * prev; 336 int error; 337 338 VM_BUG_ON(start & ~PAGE_MASK); 339 VM_BUG_ON(len != PAGE_ALIGN(len)); 340 end = start + len; 341 if (end < start) 342 return -EINVAL; 343 if (end == start) 344 return 0; 345 vma = find_vma(current->mm, start); 346 if (!vma || vma->vm_start > start) 347 return -ENOMEM; 348 349 prev = vma->vm_prev; 350 if (start > vma->vm_start) 351 prev = vma; 352 353 for (nstart = start ; ; ) { 354 vm_flags_t newflags; 355 356 /* Here we know that vma->vm_start <= nstart < vma->vm_end. */ 357 358 newflags = vma->vm_flags & ~VM_LOCKED; 359 if (on) 360 newflags |= VM_LOCKED; 361 362 tmp = vma->vm_end; 363 if (tmp > end) 364 tmp = end; 365 error = mlock_fixup(vma, &prev, nstart, tmp, newflags); 366 if (error) 367 break; 368 nstart = tmp; 369 if (nstart < prev->vm_end) 370 nstart = prev->vm_end; 371 if (nstart >= end) 372 break; 373 374 vma = prev->vm_next; 375 if (!vma || vma->vm_start != nstart) { 376 error = -ENOMEM; 377 break; 378 } 379 } 380 return error; 381 } 382 383 /* 384 * __mm_populate - populate and/or mlock pages within a range of address space. 385 * 386 * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap 387 * flags. VMAs must be already marked with the desired vm_flags, and 388 * mmap_sem must not be held. 389 */ 390 int __mm_populate(unsigned long start, unsigned long len, int ignore_errors) 391 { 392 struct mm_struct *mm = current->mm; 393 unsigned long end, nstart, nend; 394 struct vm_area_struct *vma = NULL; 395 int locked = 0; 396 long ret = 0; 397 398 VM_BUG_ON(start & ~PAGE_MASK); 399 VM_BUG_ON(len != PAGE_ALIGN(len)); 400 end = start + len; 401 402 for (nstart = start; nstart < end; nstart = nend) { 403 /* 404 * We want to fault in pages for [nstart; end) address range. 405 * Find first corresponding VMA. 406 */ 407 if (!locked) { 408 locked = 1; 409 down_read(&mm->mmap_sem); 410 vma = find_vma(mm, nstart); 411 } else if (nstart >= vma->vm_end) 412 vma = vma->vm_next; 413 if (!vma || vma->vm_start >= end) 414 break; 415 /* 416 * Set [nstart; nend) to intersection of desired address 417 * range with the first VMA. Also, skip undesirable VMA types. 418 */ 419 nend = min(end, vma->vm_end); 420 if (vma->vm_flags & (VM_IO | VM_PFNMAP)) 421 continue; 422 if (nstart < vma->vm_start) 423 nstart = vma->vm_start; 424 /* 425 * Now fault in a range of pages. __mlock_vma_pages_range() 426 * double checks the vma flags, so that it won't mlock pages 427 * if the vma was already munlocked. 428 */ 429 ret = __mlock_vma_pages_range(vma, nstart, nend, &locked); 430 if (ret < 0) { 431 if (ignore_errors) { 432 ret = 0; 433 continue; /* continue at next VMA */ 434 } 435 ret = __mlock_posix_error_return(ret); 436 break; 437 } 438 nend = nstart + ret * PAGE_SIZE; 439 ret = 0; 440 } 441 if (locked) 442 up_read(&mm->mmap_sem); 443 return ret; /* 0 or negative error code */ 444 } 445 446 SYSCALL_DEFINE2(mlock, unsigned long, start, size_t, len) 447 { 448 unsigned long locked; 449 unsigned long lock_limit; 450 int error = -ENOMEM; 451 452 if (!can_do_mlock()) 453 return -EPERM; 454 455 lru_add_drain_all(); /* flush pagevec */ 456 457 down_write(¤t->mm->mmap_sem); 458 len = PAGE_ALIGN(len + (start & ~PAGE_MASK)); 459 start &= PAGE_MASK; 460 461 locked = len >> PAGE_SHIFT; 462 locked += current->mm->locked_vm; 463 464 lock_limit = rlimit(RLIMIT_MEMLOCK); 465 lock_limit >>= PAGE_SHIFT; 466 467 /* check against resource limits */ 468 if ((locked <= lock_limit) || capable(CAP_IPC_LOCK)) 469 error = do_mlock(start, len, 1); 470 up_write(¤t->mm->mmap_sem); 471 if (!error) 472 error = __mm_populate(start, len, 0); 473 return error; 474 } 475 476 SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len) 477 { 478 int ret; 479 480 down_write(¤t->mm->mmap_sem); 481 len = PAGE_ALIGN(len + (start & ~PAGE_MASK)); 482 start &= PAGE_MASK; 483 ret = do_mlock(start, len, 0); 484 up_write(¤t->mm->mmap_sem); 485 return ret; 486 } 487 488 static int do_mlockall(int flags) 489 { 490 struct vm_area_struct * vma, * prev = NULL; 491 492 if (flags & MCL_FUTURE) 493 current->mm->def_flags |= VM_LOCKED; 494 else 495 current->mm->def_flags &= ~VM_LOCKED; 496 if (flags == MCL_FUTURE) 497 goto out; 498 499 for (vma = current->mm->mmap; vma ; vma = prev->vm_next) { 500 vm_flags_t newflags; 501 502 newflags = vma->vm_flags & ~VM_LOCKED; 503 if (flags & MCL_CURRENT) 504 newflags |= VM_LOCKED; 505 506 /* Ignore errors */ 507 mlock_fixup(vma, &prev, vma->vm_start, vma->vm_end, newflags); 508 } 509 out: 510 return 0; 511 } 512 513 SYSCALL_DEFINE1(mlockall, int, flags) 514 { 515 unsigned long lock_limit; 516 int ret = -EINVAL; 517 518 if (!flags || (flags & ~(MCL_CURRENT | MCL_FUTURE))) 519 goto out; 520 521 ret = -EPERM; 522 if (!can_do_mlock()) 523 goto out; 524 525 if (flags & MCL_CURRENT) 526 lru_add_drain_all(); /* flush pagevec */ 527 528 down_write(¤t->mm->mmap_sem); 529 530 lock_limit = rlimit(RLIMIT_MEMLOCK); 531 lock_limit >>= PAGE_SHIFT; 532 533 ret = -ENOMEM; 534 if (!(flags & MCL_CURRENT) || (current->mm->total_vm <= lock_limit) || 535 capable(CAP_IPC_LOCK)) 536 ret = do_mlockall(flags); 537 up_write(¤t->mm->mmap_sem); 538 if (!ret && (flags & MCL_CURRENT)) 539 mm_populate(0, TASK_SIZE); 540 out: 541 return ret; 542 } 543 544 SYSCALL_DEFINE0(munlockall) 545 { 546 int ret; 547 548 down_write(¤t->mm->mmap_sem); 549 ret = do_mlockall(0); 550 up_write(¤t->mm->mmap_sem); 551 return ret; 552 } 553 554 /* 555 * Objects with different lifetime than processes (SHM_LOCK and SHM_HUGETLB 556 * shm segments) get accounted against the user_struct instead. 557 */ 558 static DEFINE_SPINLOCK(shmlock_user_lock); 559 560 int user_shm_lock(size_t size, struct user_struct *user) 561 { 562 unsigned long lock_limit, locked; 563 int allowed = 0; 564 565 locked = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; 566 lock_limit = rlimit(RLIMIT_MEMLOCK); 567 if (lock_limit == RLIM_INFINITY) 568 allowed = 1; 569 lock_limit >>= PAGE_SHIFT; 570 spin_lock(&shmlock_user_lock); 571 if (!allowed && 572 locked + user->locked_shm > lock_limit && !capable(CAP_IPC_LOCK)) 573 goto out; 574 get_uid(user); 575 user->locked_shm += locked; 576 allowed = 1; 577 out: 578 spin_unlock(&shmlock_user_lock); 579 return allowed; 580 } 581 582 void user_shm_unlock(size_t size, struct user_struct *user) 583 { 584 spin_lock(&shmlock_user_lock); 585 user->locked_shm -= (size + PAGE_SIZE - 1) >> PAGE_SHIFT; 586 spin_unlock(&shmlock_user_lock); 587 free_uid(user); 588 } 589