mlock.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. * linux/mm/mlock.c
  3. *
  4. * (C) Copyright 1995 Linus Torvalds
  5. * (C) Copyright 2002 Christoph Hellwig
  6. */
  7. #include <linux/capability.h>
  8. #include <linux/mman.h>
  9. #include <linux/mm.h>
  10. #include <linux/swap.h>
  11. #include <linux/swapops.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/mempolicy.h>
  14. #include <linux/syscalls.h>
  15. #include <linux/sched.h>
  16. #include <linux/module.h>
  17. #include <linux/rmap.h>
  18. #include <linux/mmzone.h>
  19. #include <linux/hugetlb.h>
  20. #include "internal.h"
  21. int can_do_mlock(void)
  22. {
  23. if (capable(CAP_IPC_LOCK))
  24. return 1;
  25. if (rlimit(RLIMIT_MEMLOCK) != 0)
  26. return 1;
  27. return 0;
  28. }
  29. EXPORT_SYMBOL(can_do_mlock);
  30. /*
  31. * Mlocked pages are marked with PageMlocked() flag for efficient testing
  32. * in vmscan and, possibly, the fault path; and to support semi-accurate
  33. * statistics.
  34. *
  35. * An mlocked page [PageMlocked(page)] is unevictable. As such, it will
  36. * be placed on the LRU "unevictable" list, rather than the [in]active lists.
  37. * The unevictable list is an LRU sibling list to the [in]active lists.
  38. * PageUnevictable is set to indicate the unevictable state.
  39. *
  40. * When lazy mlocking via vmscan, it is important to ensure that the
  41. * vma's VM_LOCKED status is not concurrently being modified, otherwise we
  42. * may have mlocked a page that is being munlocked. So lazy mlock must take
  43. * the mmap_sem for read, and verify that the vma really is locked
  44. * (see mm/rmap.c).
  45. */
  46. /*
  47. * LRU accounting for clear_page_mlock()
  48. */
  49. void __clear_page_mlock(struct page *page)
  50. {
  51. VM_BUG_ON(!PageLocked(page));
  52. if (!page->mapping) { /* truncated ? */
  53. return;
  54. }
  55. dec_zone_page_state(page, NR_MLOCK);
  56. count_vm_event(UNEVICTABLE_PGCLEARED);
  57. if (!isolate_lru_page(page)) {
  58. putback_lru_page(page);
  59. } else {
  60. /*
  61. * We lost the race. the page already moved to evictable list.
  62. */
  63. if (PageUnevictable(page))
  64. count_vm_event(UNEVICTABLE_PGSTRANDED);
  65. }
  66. }
  67. /*
  68. * Mark page as mlocked if not already.
  69. * If page on LRU, isolate and putback to move to unevictable list.
  70. */
  71. void mlock_vma_page(struct page *page)
  72. {
  73. BUG_ON(!PageLocked(page));
  74. if (!TestSetPageMlocked(page)) {
  75. inc_zone_page_state(page, NR_MLOCK);
  76. count_vm_event(UNEVICTABLE_PGMLOCKED);
  77. if (!isolate_lru_page(page))
  78. putback_lru_page(page);
  79. }
  80. }
  81. /**
  82. * munlock_vma_page - munlock a vma page
  83. * @page - page to be unlocked
  84. *
  85. * called from munlock()/munmap() path with page supposedly on the LRU.
  86. * When we munlock a page, because the vma where we found the page is being
  87. * munlock()ed or munmap()ed, we want to check whether other vmas hold the
  88. * page locked so that we can leave it on the unevictable lru list and not
  89. * bother vmscan with it. However, to walk the page's rmap list in
  90. * try_to_munlock() we must isolate the page from the LRU. If some other
  91. * task has removed the page from the LRU, we won't be able to do that.
  92. * So we clear the PageMlocked as we might not get another chance. If we
  93. * can't isolate the page, we leave it for putback_lru_page() and vmscan
  94. * [page_referenced()/try_to_unmap()] to deal with.
  95. */
  96. void munlock_vma_page(struct page *page)
  97. {
  98. BUG_ON(!PageLocked(page));
  99. if (TestClearPageMlocked(page)) {
  100. dec_zone_page_state(page, NR_MLOCK);
  101. if (!isolate_lru_page(page)) {
  102. int ret = try_to_munlock(page);
  103. /*
  104. * did try_to_unlock() succeed or punt?
  105. */
  106. if (ret != SWAP_MLOCK)
  107. count_vm_event(UNEVICTABLE_PGMUNLOCKED);
  108. putback_lru_page(page);
  109. } else {
  110. /*
  111. * Some other task has removed the page from the LRU.
  112. * putback_lru_page() will take care of removing the
  113. * page from the unevictable list, if necessary.
  114. * vmscan [page_referenced()] will move the page back
  115. * to the unevictable list if some other vma has it
  116. * mlocked.
  117. */
  118. if (PageUnevictable(page))
  119. count_vm_event(UNEVICTABLE_PGSTRANDED);
  120. else
  121. count_vm_event(UNEVICTABLE_PGMUNLOCKED);
  122. }
  123. }
  124. }
  125. /* Is the vma a continuation of the stack vma above it? */
  126. static inline int vma_stack_continue(struct vm_area_struct *vma, unsigned long addr)
  127. {
  128. return vma && (vma->vm_end == addr) && (vma->vm_flags & VM_GROWSDOWN);
  129. }
  130. static inline int stack_guard_page(struct vm_area_struct *vma, unsigned long addr)
  131. {
  132. return (vma->vm_flags & VM_GROWSDOWN) &&
  133. (vma->vm_start == addr) &&
  134. !vma_stack_continue(vma->vm_prev, addr);
  135. }
  136. /**
  137. * __mlock_vma_pages_range() - mlock a range of pages in the vma.
  138. * @vma: target vma
  139. * @start: start address
  140. * @end: end address
  141. *
  142. * This takes care of making the pages present too.
  143. *
  144. * return 0 on success, negative error code on error.
  145. *
  146. * vma->vm_mm->mmap_sem must be held for at least read.
  147. */
  148. static long __mlock_vma_pages_range(struct vm_area_struct *vma,
  149. unsigned long start, unsigned long end)
  150. {
  151. struct mm_struct *mm = vma->vm_mm;
  152. unsigned long addr = start;
  153. struct page *pages[16]; /* 16 gives a reasonable batch */
  154. int nr_pages = (end - start) / PAGE_SIZE;
  155. int ret = 0;
  156. int gup_flags;
  157. VM_BUG_ON(start & ~PAGE_MASK);
  158. VM_BUG_ON(end & ~PAGE_MASK);
  159. VM_BUG_ON(start < vma->vm_start);
  160. VM_BUG_ON(end > vma->vm_end);
  161. VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
  162. gup_flags = FOLL_TOUCH | FOLL_GET;
  163. if (vma->vm_flags & VM_WRITE)
  164. gup_flags |= FOLL_WRITE;
  165. /* We don't try to access the guard page of a stack vma */
  166. if (stack_guard_page(vma, start)) {
  167. addr += PAGE_SIZE;
  168. nr_pages--;
  169. }
  170. while (nr_pages > 0) {
  171. int i;
  172. cond_resched();
  173. /*
  174. * get_user_pages makes pages present if we are
  175. * setting mlock. and this extra reference count will
  176. * disable migration of this page. However, page may
  177. * still be truncated out from under us.
  178. */
  179. ret = __get_user_pages(current, mm, addr,
  180. min_t(int, nr_pages, ARRAY_SIZE(pages)),
  181. gup_flags, pages, NULL);
  182. /*
  183. * This can happen for, e.g., VM_NONLINEAR regions before
  184. * a page has been allocated and mapped at a given offset,
  185. * or for addresses that map beyond end of a file.
  186. * We'll mlock the pages if/when they get faulted in.
  187. */
  188. if (ret < 0)
  189. break;
  190. lru_add_drain(); /* push cached pages to LRU */
  191. for (i = 0; i < ret; i++) {
  192. struct page *page = pages[i];
  193. if (page->mapping) {
  194. /*
  195. * That preliminary check is mainly to avoid
  196. * the pointless overhead of lock_page on the
  197. * ZERO_PAGE: which might bounce very badly if
  198. * there is contention. However, we're still
  199. * dirtying its cacheline with get/put_page:
  200. * we'll add another __get_user_pages flag to
  201. * avoid it if that case turns out to matter.
  202. */
  203. lock_page(page);
  204. /*
  205. * Because we lock page here and migration is
  206. * blocked by the elevated reference, we need
  207. * only check for file-cache page truncation.
  208. */
  209. if (page->mapping)
  210. mlock_vma_page(page);
  211. unlock_page(page);
  212. }
  213. put_page(page); /* ref from get_user_pages() */
  214. }
  215. addr += ret * PAGE_SIZE;
  216. nr_pages -= ret;
  217. ret = 0;
  218. }
  219. return ret; /* 0 or negative error code */
  220. }
  221. /*
  222. * convert get_user_pages() return value to posix mlock() error
  223. */
  224. static int __mlock_posix_error_return(long retval)
  225. {
  226. if (retval == -EFAULT)
  227. retval = -ENOMEM;
  228. else if (retval == -ENOMEM)
  229. retval = -EAGAIN;
  230. return retval;
  231. }
  232. /**
  233. * mlock_vma_pages_range() - mlock pages in specified vma range.
  234. * @vma - the vma containing the specfied address range
  235. * @start - starting address in @vma to mlock
  236. * @end - end address [+1] in @vma to mlock
  237. *
  238. * For mmap()/mremap()/expansion of mlocked vma.
  239. *
  240. * return 0 on success for "normal" vmas.
  241. *
  242. * return number of pages [> 0] to be removed from locked_vm on success
  243. * of "special" vmas.
  244. */
  245. long mlock_vma_pages_range(struct vm_area_struct *vma,
  246. unsigned long start, unsigned long end)
  247. {
  248. int nr_pages = (end - start) / PAGE_SIZE;
  249. BUG_ON(!(vma->vm_flags & VM_LOCKED));
  250. /*
  251. * filter unlockable vmas
  252. */
  253. if (vma->vm_flags & (VM_IO | VM_PFNMAP))
  254. goto no_mlock;
  255. if (!((vma->vm_flags & (VM_DONTEXPAND | VM_RESERVED)) ||
  256. is_vm_hugetlb_page(vma) ||
  257. vma == get_gate_vma(current))) {
  258. __mlock_vma_pages_range(vma, start, end);
  259. /* Hide errors from mmap() and other callers */
  260. return 0;
  261. }
  262. /*
  263. * User mapped kernel pages or huge pages:
  264. * make these pages present to populate the ptes, but
  265. * fall thru' to reset VM_LOCKED--no need to unlock, and
  266. * return nr_pages so these don't get counted against task's
  267. * locked limit. huge pages are already counted against
  268. * locked vm limit.
  269. */
  270. make_pages_present(start, end);
  271. no_mlock:
  272. vma->vm_flags &= ~VM_LOCKED; /* and don't come back! */
  273. return nr_pages; /* error or pages NOT mlocked */
  274. }
  275. /*
  276. * munlock_vma_pages_range() - munlock all pages in the vma range.'
  277. * @vma - vma containing range to be munlock()ed.
  278. * @start - start address in @vma of the range
  279. * @end - end of range in @vma.
  280. *
  281. * For mremap(), munmap() and exit().
  282. *
  283. * Called with @vma VM_LOCKED.
  284. *
  285. * Returns with VM_LOCKED cleared. Callers must be prepared to
  286. * deal with this.
  287. *
  288. * We don't save and restore VM_LOCKED here because pages are
  289. * still on lru. In unmap path, pages might be scanned by reclaim
  290. * and re-mlocked by try_to_{munlock|unmap} before we unmap and
  291. * free them. This will result in freeing mlocked pages.
  292. */
  293. void munlock_vma_pages_range(struct vm_area_struct *vma,
  294. unsigned long start, unsigned long end)
  295. {
  296. unsigned long addr;
  297. lru_add_drain();
  298. vma->vm_flags &= ~VM_LOCKED;
  299. for (addr = start; addr < end; addr += PAGE_SIZE) {
  300. struct page *page;
  301. /*
  302. * Although FOLL_DUMP is intended for get_dump_page(),
  303. * it just so happens that its special treatment of the
  304. * ZERO_PAGE (returning an error instead of doing get_page)
  305. * suits munlock very well (and if somehow an abnormal page
  306. * has sneaked into the range, we won't oops here: great).
  307. */
  308. page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP);
  309. if (page && !IS_ERR(page)) {
  310. lock_page(page);
  311. /*
  312. * Like in __mlock_vma_pages_range(),
  313. * because we lock page here and migration is
  314. * blocked by the elevated reference, we need
  315. * only check for file-cache page truncation.
  316. */
  317. if (page->mapping)
  318. munlock_vma_page(page);
  319. unlock_page(page);
  320. put_page(page);
  321. }
  322. cond_resched();
  323. }
  324. }
  325. /*
  326. * mlock_fixup - handle mlock[all]/munlock[all] requests.
  327. *
  328. * Filters out "special" vmas -- VM_LOCKED never gets set for these, and
  329. * munlock is a no-op. However, for some special vmas, we go ahead and
  330. * populate the ptes via make_pages_present().
  331. *
  332. * For vmas that pass the filters, merge/split as appropriate.
  333. */
  334. static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev,
  335. unsigned long start, unsigned long end, unsigned int newflags)
  336. {
  337. struct mm_struct *mm = vma->vm_mm;
  338. pgoff_t pgoff;
  339. int nr_pages;
  340. int ret = 0;
  341. int lock = newflags & VM_LOCKED;
  342. if (newflags == vma->vm_flags ||
  343. (vma->vm_flags & (VM_IO | VM_PFNMAP)))
  344. goto out; /* don't set VM_LOCKED, don't count */
  345. if ((vma->vm_flags & (VM_DONTEXPAND | VM_RESERVED)) ||
  346. is_vm_hugetlb_page(vma) ||
  347. vma == get_gate_vma(current)) {
  348. if (lock)
  349. make_pages_present(start, end);
  350. goto out; /* don't set VM_LOCKED, don't count */
  351. }
  352. pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  353. *prev = vma_merge(mm, *prev, start, end, newflags, vma->anon_vma,
  354. vma->vm_file, pgoff, vma_policy(vma));
  355. if (*prev) {
  356. vma = *prev;
  357. goto success;
  358. }
  359. if (start != vma->vm_start) {
  360. ret = split_vma(mm, vma, start, 1);
  361. if (ret)
  362. goto out;
  363. }
  364. if (end != vma->vm_end) {
  365. ret = split_vma(mm, vma, end, 0);
  366. if (ret)
  367. goto out;
  368. }
  369. success:
  370. /*
  371. * Keep track of amount of locked VM.
  372. */
  373. nr_pages = (end - start) >> PAGE_SHIFT;
  374. if (!lock)
  375. nr_pages = -nr_pages;
  376. mm->locked_vm += nr_pages;
  377. /*
  378. * vm_flags is protected by the mmap_sem held in write mode.
  379. * It's okay if try_to_unmap_one unmaps a page just after we
  380. * set VM_LOCKED, __mlock_vma_pages_range will bring it back.
  381. */
  382. if (lock) {
  383. vma->vm_flags = newflags;
  384. ret = __mlock_vma_pages_range(vma, start, end);
  385. if (ret < 0)
  386. ret = __mlock_posix_error_return(ret);
  387. } else {
  388. munlock_vma_pages_range(vma, start, end);
  389. }
  390. out:
  391. *prev = vma;
  392. return ret;
  393. }
  394. static int do_mlock(unsigned long start, size_t len, int on)
  395. {
  396. unsigned long nstart, end, tmp;
  397. struct vm_area_struct * vma, * prev;
  398. int error;
  399. len = PAGE_ALIGN(len);
  400. end = start + len;
  401. if (end < start)
  402. return -EINVAL;
  403. if (end == start)
  404. return 0;
  405. vma = find_vma_prev(current->mm, start, &prev);
  406. if (!vma || vma->vm_start > start)
  407. return -ENOMEM;
  408. if (start > vma->vm_start)
  409. prev = vma;
  410. for (nstart = start ; ; ) {
  411. unsigned int newflags;
  412. /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
  413. newflags = vma->vm_flags | VM_LOCKED;
  414. if (!on)
  415. newflags &= ~VM_LOCKED;
  416. tmp = vma->vm_end;
  417. if (tmp > end)
  418. tmp = end;
  419. error = mlock_fixup(vma, &prev, nstart, tmp, newflags);
  420. if (error)
  421. break;
  422. nstart = tmp;
  423. if (nstart < prev->vm_end)
  424. nstart = prev->vm_end;
  425. if (nstart >= end)
  426. break;
  427. vma = prev->vm_next;
  428. if (!vma || vma->vm_start != nstart) {
  429. error = -ENOMEM;
  430. break;
  431. }
  432. }
  433. return error;
  434. }
  435. SYSCALL_DEFINE2(mlock, unsigned long, start, size_t, len)
  436. {
  437. unsigned long locked;
  438. unsigned long lock_limit;
  439. int error = -ENOMEM;
  440. if (!can_do_mlock())
  441. return -EPERM;
  442. lru_add_drain_all(); /* flush pagevec */
  443. down_write(&current->mm->mmap_sem);
  444. len = PAGE_ALIGN(len + (start & ~PAGE_MASK));
  445. start &= PAGE_MASK;
  446. locked = len >> PAGE_SHIFT;
  447. locked += current->mm->locked_vm;
  448. lock_limit = rlimit(RLIMIT_MEMLOCK);
  449. lock_limit >>= PAGE_SHIFT;
  450. /* check against resource limits */
  451. if ((locked <= lock_limit) || capable(CAP_IPC_LOCK))
  452. error = do_mlock(start, len, 1);
  453. up_write(&current->mm->mmap_sem);
  454. return error;
  455. }
  456. SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len)
  457. {
  458. int ret;
  459. down_write(&current->mm->mmap_sem);
  460. len = PAGE_ALIGN(len + (start & ~PAGE_MASK));
  461. start &= PAGE_MASK;
  462. ret = do_mlock(start, len, 0);
  463. up_write(&current->mm->mmap_sem);
  464. return ret;
  465. }
  466. static int do_mlockall(int flags)
  467. {
  468. struct vm_area_struct * vma, * prev = NULL;
  469. unsigned int def_flags = 0;
  470. if (flags & MCL_FUTURE)
  471. def_flags = VM_LOCKED;
  472. current->mm->def_flags = def_flags;
  473. if (flags == MCL_FUTURE)
  474. goto out;
  475. for (vma = current->mm->mmap; vma ; vma = prev->vm_next) {
  476. unsigned int newflags;
  477. newflags = vma->vm_flags | VM_LOCKED;
  478. if (!(flags & MCL_CURRENT))
  479. newflags &= ~VM_LOCKED;
  480. /* Ignore errors */
  481. mlock_fixup(vma, &prev, vma->vm_start, vma->vm_end, newflags);
  482. }
  483. out:
  484. return 0;
  485. }
  486. SYSCALL_DEFINE1(mlockall, int, flags)
  487. {
  488. unsigned long lock_limit;
  489. int ret = -EINVAL;
  490. if (!flags || (flags & ~(MCL_CURRENT | MCL_FUTURE)))
  491. goto out;
  492. ret = -EPERM;
  493. if (!can_do_mlock())
  494. goto out;
  495. lru_add_drain_all(); /* flush pagevec */
  496. down_write(&current->mm->mmap_sem);
  497. lock_limit = rlimit(RLIMIT_MEMLOCK);
  498. lock_limit >>= PAGE_SHIFT;
  499. ret = -ENOMEM;
  500. if (!(flags & MCL_CURRENT) || (current->mm->total_vm <= lock_limit) ||
  501. capable(CAP_IPC_LOCK))
  502. ret = do_mlockall(flags);
  503. up_write(&current->mm->mmap_sem);
  504. out:
  505. return ret;
  506. }
  507. SYSCALL_DEFINE0(munlockall)
  508. {
  509. int ret;
  510. down_write(&current->mm->mmap_sem);
  511. ret = do_mlockall(0);
  512. up_write(&current->mm->mmap_sem);
  513. return ret;
  514. }
  515. /*
  516. * Objects with different lifetime than processes (SHM_LOCK and SHM_HUGETLB
  517. * shm segments) get accounted against the user_struct instead.
  518. */
  519. static DEFINE_SPINLOCK(shmlock_user_lock);
  520. int user_shm_lock(size_t size, struct user_struct *user)
  521. {
  522. unsigned long lock_limit, locked;
  523. int allowed = 0;
  524. locked = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  525. lock_limit = rlimit(RLIMIT_MEMLOCK);
  526. if (lock_limit == RLIM_INFINITY)
  527. allowed = 1;
  528. lock_limit >>= PAGE_SHIFT;
  529. spin_lock(&shmlock_user_lock);
  530. if (!allowed &&
  531. locked + user->locked_shm > lock_limit && !capable(CAP_IPC_LOCK))
  532. goto out;
  533. get_uid(user);
  534. user->locked_shm += locked;
  535. allowed = 1;
  536. out:
  537. spin_unlock(&shmlock_user_lock);
  538. return allowed;
  539. }
  540. void user_shm_unlock(size_t size, struct user_struct *user)
  541. {
  542. spin_lock(&shmlock_user_lock);
  543. user->locked_shm -= (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  544. spin_unlock(&shmlock_user_lock);
  545. free_uid(user);
  546. }