mlock.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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 (current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur != 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. * called from munlock()/munmap() path with page supposedly on the LRU.
  83. *
  84. * Note: unlike mlock_vma_page(), we can't just clear the PageMlocked
  85. * [in try_to_munlock()] and then attempt to isolate the page. We must
  86. * isolate the page to keep others from messing with its unevictable
  87. * and mlocked state while trying to munlock. However, we pre-clear the
  88. * mlocked state anyway as we might lose the isolation race and we might
  89. * not get another chance to clear PageMlocked. If we successfully
  90. * isolate the page and try_to_munlock() detects other VM_LOCKED vmas
  91. * mapping the page, it will restore the PageMlocked state, unless the page
  92. * is mapped in a non-linear vma. So, we go ahead and SetPageMlocked(),
  93. * perhaps redundantly.
  94. * If we lose the isolation race, and the page is mapped by other VM_LOCKED
  95. * vmas, we'll detect this in vmscan--via try_to_munlock() or try_to_unmap()
  96. * either of which will restore the PageMlocked state by calling
  97. * mlock_vma_page() above, if it can grab the vma's mmap sem.
  98. */
  99. static void munlock_vma_page(struct page *page)
  100. {
  101. BUG_ON(!PageLocked(page));
  102. if (TestClearPageMlocked(page)) {
  103. dec_zone_page_state(page, NR_MLOCK);
  104. if (!isolate_lru_page(page)) {
  105. int ret = try_to_munlock(page);
  106. /*
  107. * did try_to_unlock() succeed or punt?
  108. */
  109. if (ret == SWAP_SUCCESS || ret == SWAP_AGAIN)
  110. count_vm_event(UNEVICTABLE_PGMUNLOCKED);
  111. putback_lru_page(page);
  112. } else {
  113. /*
  114. * We lost the race. let try_to_unmap() deal
  115. * with it. At least we get the page state and
  116. * mlock stats right. However, page is still on
  117. * the noreclaim list. We'll fix that up when
  118. * the page is eventually freed or we scan the
  119. * noreclaim list.
  120. */
  121. if (PageUnevictable(page))
  122. count_vm_event(UNEVICTABLE_PGSTRANDED);
  123. else
  124. count_vm_event(UNEVICTABLE_PGMUNLOCKED);
  125. }
  126. }
  127. }
  128. /**
  129. * __mlock_vma_pages_range() - mlock/munlock a range of pages in the vma.
  130. * @vma: target vma
  131. * @start: start address
  132. * @end: end address
  133. * @mlock: 0 indicate munlock, otherwise mlock.
  134. *
  135. * If @mlock == 0, unlock an mlocked range;
  136. * else mlock the range of pages. This takes care of making the pages present ,
  137. * too.
  138. *
  139. * return 0 on success, negative error code on error.
  140. *
  141. * vma->vm_mm->mmap_sem must be held for at least read.
  142. */
  143. static long __mlock_vma_pages_range(struct vm_area_struct *vma,
  144. unsigned long start, unsigned long end,
  145. int mlock)
  146. {
  147. struct mm_struct *mm = vma->vm_mm;
  148. unsigned long addr = start;
  149. struct page *pages[16]; /* 16 gives a reasonable batch */
  150. int nr_pages = (end - start) / PAGE_SIZE;
  151. int ret = 0;
  152. int gup_flags = 0;
  153. VM_BUG_ON(start & ~PAGE_MASK);
  154. VM_BUG_ON(end & ~PAGE_MASK);
  155. VM_BUG_ON(start < vma->vm_start);
  156. VM_BUG_ON(end > vma->vm_end);
  157. VM_BUG_ON((!rwsem_is_locked(&mm->mmap_sem)) &&
  158. (atomic_read(&mm->mm_users) != 0));
  159. /*
  160. * mlock: don't page populate if vma has PROT_NONE permission.
  161. * munlock: always do munlock although the vma has PROT_NONE
  162. * permission, or SIGKILL is pending.
  163. */
  164. if (!mlock)
  165. gup_flags |= GUP_FLAGS_IGNORE_VMA_PERMISSIONS |
  166. GUP_FLAGS_IGNORE_SIGKILL;
  167. if (vma->vm_flags & VM_WRITE)
  168. gup_flags |= GUP_FLAGS_WRITE;
  169. while (nr_pages > 0) {
  170. int i;
  171. cond_resched();
  172. /*
  173. * get_user_pages makes pages present if we are
  174. * setting mlock. and this extra reference count will
  175. * disable migration of this page. However, page may
  176. * still be truncated out from under us.
  177. */
  178. ret = __get_user_pages(current, mm, addr,
  179. min_t(int, nr_pages, ARRAY_SIZE(pages)),
  180. gup_flags, pages, NULL);
  181. /*
  182. * This can happen for, e.g., VM_NONLINEAR regions before
  183. * a page has been allocated and mapped at a given offset,
  184. * or for addresses that map beyond end of a file.
  185. * We'll mlock the the pages if/when they get faulted in.
  186. */
  187. if (ret < 0)
  188. break;
  189. if (ret == 0) {
  190. /*
  191. * We know the vma is there, so the only time
  192. * we cannot get a single page should be an
  193. * error (ret < 0) case.
  194. */
  195. WARN_ON(1);
  196. break;
  197. }
  198. lru_add_drain(); /* push cached pages to LRU */
  199. for (i = 0; i < ret; i++) {
  200. struct page *page = pages[i];
  201. lock_page(page);
  202. /*
  203. * Because we lock page here and migration is blocked
  204. * by the elevated reference, we need only check for
  205. * page truncation (file-cache only).
  206. */
  207. if (page->mapping) {
  208. if (mlock)
  209. mlock_vma_page(page);
  210. else
  211. munlock_vma_page(page);
  212. }
  213. unlock_page(page);
  214. put_page(page); /* ref from get_user_pages() */
  215. /*
  216. * here we assume that get_user_pages() has given us
  217. * a list of virtually contiguous pages.
  218. */
  219. addr += PAGE_SIZE; /* for next get_user_pages() */
  220. nr_pages--;
  221. }
  222. ret = 0;
  223. }
  224. return ret; /* count entire vma as locked_vm */
  225. }
  226. /*
  227. * convert get_user_pages() return value to posix mlock() error
  228. */
  229. static int __mlock_posix_error_return(long retval)
  230. {
  231. if (retval == -EFAULT)
  232. retval = -ENOMEM;
  233. else if (retval == -ENOMEM)
  234. retval = -EAGAIN;
  235. return retval;
  236. }
  237. /**
  238. * mlock_vma_pages_range() - mlock pages in specified vma range.
  239. * @vma - the vma containing the specfied address range
  240. * @start - starting address in @vma to mlock
  241. * @end - end address [+1] in @vma to mlock
  242. *
  243. * For mmap()/mremap()/expansion of mlocked vma.
  244. *
  245. * return 0 on success for "normal" vmas.
  246. *
  247. * return number of pages [> 0] to be removed from locked_vm on success
  248. * of "special" vmas.
  249. */
  250. long mlock_vma_pages_range(struct vm_area_struct *vma,
  251. unsigned long start, unsigned long end)
  252. {
  253. int nr_pages = (end - start) / PAGE_SIZE;
  254. BUG_ON(!(vma->vm_flags & VM_LOCKED));
  255. /*
  256. * filter unlockable vmas
  257. */
  258. if (vma->vm_flags & (VM_IO | VM_PFNMAP))
  259. goto no_mlock;
  260. if (!((vma->vm_flags & (VM_DONTEXPAND | VM_RESERVED)) ||
  261. is_vm_hugetlb_page(vma) ||
  262. vma == get_gate_vma(current))) {
  263. __mlock_vma_pages_range(vma, start, end, 1);
  264. /* Hide errors from mmap() and other callers */
  265. return 0;
  266. }
  267. /*
  268. * User mapped kernel pages or huge pages:
  269. * make these pages present to populate the ptes, but
  270. * fall thru' to reset VM_LOCKED--no need to unlock, and
  271. * return nr_pages so these don't get counted against task's
  272. * locked limit. huge pages are already counted against
  273. * locked vm limit.
  274. */
  275. make_pages_present(start, end);
  276. no_mlock:
  277. vma->vm_flags &= ~VM_LOCKED; /* and don't come back! */
  278. return nr_pages; /* error or pages NOT mlocked */
  279. }
  280. /*
  281. * munlock_vma_pages_range() - munlock all pages in the vma range.'
  282. * @vma - vma containing range to be munlock()ed.
  283. * @start - start address in @vma of the range
  284. * @end - end of range in @vma.
  285. *
  286. * For mremap(), munmap() and exit().
  287. *
  288. * Called with @vma VM_LOCKED.
  289. *
  290. * Returns with VM_LOCKED cleared. Callers must be prepared to
  291. * deal with this.
  292. *
  293. * We don't save and restore VM_LOCKED here because pages are
  294. * still on lru. In unmap path, pages might be scanned by reclaim
  295. * and re-mlocked by try_to_{munlock|unmap} before we unmap and
  296. * free them. This will result in freeing mlocked pages.
  297. */
  298. void munlock_vma_pages_range(struct vm_area_struct *vma,
  299. unsigned long start, unsigned long end)
  300. {
  301. vma->vm_flags &= ~VM_LOCKED;
  302. __mlock_vma_pages_range(vma, start, end, 0);
  303. }
  304. /*
  305. * mlock_fixup - handle mlock[all]/munlock[all] requests.
  306. *
  307. * Filters out "special" vmas -- VM_LOCKED never gets set for these, and
  308. * munlock is a no-op. However, for some special vmas, we go ahead and
  309. * populate the ptes via make_pages_present().
  310. *
  311. * For vmas that pass the filters, merge/split as appropriate.
  312. */
  313. static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev,
  314. unsigned long start, unsigned long end, unsigned int newflags)
  315. {
  316. struct mm_struct *mm = vma->vm_mm;
  317. pgoff_t pgoff;
  318. int nr_pages;
  319. int ret = 0;
  320. int lock = newflags & VM_LOCKED;
  321. if (newflags == vma->vm_flags ||
  322. (vma->vm_flags & (VM_IO | VM_PFNMAP)))
  323. goto out; /* don't set VM_LOCKED, don't count */
  324. if ((vma->vm_flags & (VM_DONTEXPAND | VM_RESERVED)) ||
  325. is_vm_hugetlb_page(vma) ||
  326. vma == get_gate_vma(current)) {
  327. if (lock)
  328. make_pages_present(start, end);
  329. goto out; /* don't set VM_LOCKED, don't count */
  330. }
  331. pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  332. *prev = vma_merge(mm, *prev, start, end, newflags, vma->anon_vma,
  333. vma->vm_file, pgoff, vma_policy(vma));
  334. if (*prev) {
  335. vma = *prev;
  336. goto success;
  337. }
  338. if (start != vma->vm_start) {
  339. ret = split_vma(mm, vma, start, 1);
  340. if (ret)
  341. goto out;
  342. }
  343. if (end != vma->vm_end) {
  344. ret = split_vma(mm, vma, end, 0);
  345. if (ret)
  346. goto out;
  347. }
  348. success:
  349. /*
  350. * Keep track of amount of locked VM.
  351. */
  352. nr_pages = (end - start) >> PAGE_SHIFT;
  353. if (!lock)
  354. nr_pages = -nr_pages;
  355. mm->locked_vm += nr_pages;
  356. /*
  357. * vm_flags is protected by the mmap_sem held in write mode.
  358. * It's okay if try_to_unmap_one unmaps a page just after we
  359. * set VM_LOCKED, __mlock_vma_pages_range will bring it back.
  360. */
  361. vma->vm_flags = newflags;
  362. if (lock) {
  363. ret = __mlock_vma_pages_range(vma, start, end, 1);
  364. if (ret > 0) {
  365. mm->locked_vm -= ret;
  366. ret = 0;
  367. } else
  368. ret = __mlock_posix_error_return(ret); /* translate if needed */
  369. } else {
  370. __mlock_vma_pages_range(vma, start, end, 0);
  371. }
  372. out:
  373. *prev = vma;
  374. return ret;
  375. }
  376. static int do_mlock(unsigned long start, size_t len, int on)
  377. {
  378. unsigned long nstart, end, tmp;
  379. struct vm_area_struct * vma, * prev;
  380. int error;
  381. len = PAGE_ALIGN(len);
  382. end = start + len;
  383. if (end < start)
  384. return -EINVAL;
  385. if (end == start)
  386. return 0;
  387. vma = find_vma_prev(current->mm, start, &prev);
  388. if (!vma || vma->vm_start > start)
  389. return -ENOMEM;
  390. if (start > vma->vm_start)
  391. prev = vma;
  392. for (nstart = start ; ; ) {
  393. unsigned int newflags;
  394. /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
  395. newflags = vma->vm_flags | VM_LOCKED;
  396. if (!on)
  397. newflags &= ~VM_LOCKED;
  398. tmp = vma->vm_end;
  399. if (tmp > end)
  400. tmp = end;
  401. error = mlock_fixup(vma, &prev, nstart, tmp, newflags);
  402. if (error)
  403. break;
  404. nstart = tmp;
  405. if (nstart < prev->vm_end)
  406. nstart = prev->vm_end;
  407. if (nstart >= end)
  408. break;
  409. vma = prev->vm_next;
  410. if (!vma || vma->vm_start != nstart) {
  411. error = -ENOMEM;
  412. break;
  413. }
  414. }
  415. return error;
  416. }
  417. SYSCALL_DEFINE2(mlock, unsigned long, start, size_t, len)
  418. {
  419. unsigned long locked;
  420. unsigned long lock_limit;
  421. int error = -ENOMEM;
  422. if (!can_do_mlock())
  423. return -EPERM;
  424. lru_add_drain_all(); /* flush pagevec */
  425. down_write(&current->mm->mmap_sem);
  426. len = PAGE_ALIGN(len + (start & ~PAGE_MASK));
  427. start &= PAGE_MASK;
  428. locked = len >> PAGE_SHIFT;
  429. locked += current->mm->locked_vm;
  430. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
  431. lock_limit >>= PAGE_SHIFT;
  432. /* check against resource limits */
  433. if ((locked <= lock_limit) || capable(CAP_IPC_LOCK))
  434. error = do_mlock(start, len, 1);
  435. up_write(&current->mm->mmap_sem);
  436. return error;
  437. }
  438. SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len)
  439. {
  440. int ret;
  441. down_write(&current->mm->mmap_sem);
  442. len = PAGE_ALIGN(len + (start & ~PAGE_MASK));
  443. start &= PAGE_MASK;
  444. ret = do_mlock(start, len, 0);
  445. up_write(&current->mm->mmap_sem);
  446. return ret;
  447. }
  448. static int do_mlockall(int flags)
  449. {
  450. struct vm_area_struct * vma, * prev = NULL;
  451. unsigned int def_flags = 0;
  452. if (flags & MCL_FUTURE)
  453. def_flags = VM_LOCKED;
  454. current->mm->def_flags = def_flags;
  455. if (flags == MCL_FUTURE)
  456. goto out;
  457. for (vma = current->mm->mmap; vma ; vma = prev->vm_next) {
  458. unsigned int newflags;
  459. newflags = vma->vm_flags | VM_LOCKED;
  460. if (!(flags & MCL_CURRENT))
  461. newflags &= ~VM_LOCKED;
  462. /* Ignore errors */
  463. mlock_fixup(vma, &prev, vma->vm_start, vma->vm_end, newflags);
  464. }
  465. out:
  466. return 0;
  467. }
  468. SYSCALL_DEFINE1(mlockall, int, flags)
  469. {
  470. unsigned long lock_limit;
  471. int ret = -EINVAL;
  472. if (!flags || (flags & ~(MCL_CURRENT | MCL_FUTURE)))
  473. goto out;
  474. ret = -EPERM;
  475. if (!can_do_mlock())
  476. goto out;
  477. lru_add_drain_all(); /* flush pagevec */
  478. down_write(&current->mm->mmap_sem);
  479. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
  480. lock_limit >>= PAGE_SHIFT;
  481. ret = -ENOMEM;
  482. if (!(flags & MCL_CURRENT) || (current->mm->total_vm <= lock_limit) ||
  483. capable(CAP_IPC_LOCK))
  484. ret = do_mlockall(flags);
  485. up_write(&current->mm->mmap_sem);
  486. out:
  487. return ret;
  488. }
  489. SYSCALL_DEFINE0(munlockall)
  490. {
  491. int ret;
  492. down_write(&current->mm->mmap_sem);
  493. ret = do_mlockall(0);
  494. up_write(&current->mm->mmap_sem);
  495. return ret;
  496. }
  497. /*
  498. * Objects with different lifetime than processes (SHM_LOCK and SHM_HUGETLB
  499. * shm segments) get accounted against the user_struct instead.
  500. */
  501. static DEFINE_SPINLOCK(shmlock_user_lock);
  502. int user_shm_lock(size_t size, struct user_struct *user)
  503. {
  504. unsigned long lock_limit, locked;
  505. int allowed = 0;
  506. locked = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  507. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
  508. if (lock_limit == RLIM_INFINITY)
  509. allowed = 1;
  510. lock_limit >>= PAGE_SHIFT;
  511. spin_lock(&shmlock_user_lock);
  512. if (!allowed &&
  513. locked + user->locked_shm > lock_limit && !capable(CAP_IPC_LOCK))
  514. goto out;
  515. get_uid(user);
  516. user->locked_shm += locked;
  517. allowed = 1;
  518. out:
  519. spin_unlock(&shmlock_user_lock);
  520. return allowed;
  521. }
  522. void user_shm_unlock(size_t size, struct user_struct *user)
  523. {
  524. spin_lock(&shmlock_user_lock);
  525. user->locked_shm -= (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  526. spin_unlock(&shmlock_user_lock);
  527. free_uid(user);
  528. }
  529. int account_locked_memory(struct mm_struct *mm, struct rlimit *rlim,
  530. size_t size)
  531. {
  532. unsigned long lim, vm, pgsz;
  533. int error = -ENOMEM;
  534. pgsz = PAGE_ALIGN(size) >> PAGE_SHIFT;
  535. down_write(&mm->mmap_sem);
  536. lim = rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
  537. vm = mm->total_vm + pgsz;
  538. if (lim < vm)
  539. goto out;
  540. lim = rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
  541. vm = mm->locked_vm + pgsz;
  542. if (lim < vm)
  543. goto out;
  544. mm->total_vm += pgsz;
  545. mm->locked_vm += pgsz;
  546. error = 0;
  547. out:
  548. up_write(&mm->mmap_sem);
  549. return error;
  550. }
  551. void refund_locked_memory(struct mm_struct *mm, size_t size)
  552. {
  553. unsigned long pgsz = PAGE_ALIGN(size) >> PAGE_SHIFT;
  554. down_write(&mm->mmap_sem);
  555. mm->total_vm -= pgsz;
  556. mm->locked_vm -= pgsz;
  557. up_write(&mm->mmap_sem);
  558. }