mlock.c 15 KB

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