mlock.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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/export.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 = SWAP_AGAIN;
  103. /*
  104. * Optimization: if the page was mapped just once,
  105. * that's our mapping and we don't need to check all the
  106. * other vmas.
  107. */
  108. if (page_mapcount(page) > 1)
  109. ret = try_to_munlock(page);
  110. /*
  111. * did try_to_unlock() succeed or punt?
  112. */
  113. if (ret != SWAP_MLOCK)
  114. count_vm_event(UNEVICTABLE_PGMUNLOCKED);
  115. putback_lru_page(page);
  116. } else {
  117. /*
  118. * Some other task has removed the page from the LRU.
  119. * putback_lru_page() will take care of removing the
  120. * page from the unevictable list, if necessary.
  121. * vmscan [page_referenced()] will move the page back
  122. * to the unevictable list if some other vma has it
  123. * mlocked.
  124. */
  125. if (PageUnevictable(page))
  126. count_vm_event(UNEVICTABLE_PGSTRANDED);
  127. else
  128. count_vm_event(UNEVICTABLE_PGMUNLOCKED);
  129. }
  130. }
  131. }
  132. /**
  133. * __mlock_vma_pages_range() - mlock a range of pages in the vma.
  134. * @vma: target vma
  135. * @start: start address
  136. * @end: end address
  137. *
  138. * This takes care of making the pages present too.
  139. *
  140. * return 0 on success, negative error code on error.
  141. *
  142. * vma->vm_mm->mmap_sem must be held for at least read.
  143. */
  144. static long __mlock_vma_pages_range(struct vm_area_struct *vma,
  145. unsigned long start, unsigned long end,
  146. int *nonblocking)
  147. {
  148. struct mm_struct *mm = vma->vm_mm;
  149. unsigned long addr = start;
  150. int nr_pages = (end - start) / PAGE_SIZE;
  151. int gup_flags;
  152. VM_BUG_ON(start & ~PAGE_MASK);
  153. VM_BUG_ON(end & ~PAGE_MASK);
  154. VM_BUG_ON(start < vma->vm_start);
  155. VM_BUG_ON(end > vma->vm_end);
  156. VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
  157. gup_flags = FOLL_TOUCH | FOLL_MLOCK;
  158. /*
  159. * We want to touch writable mappings with a write fault in order
  160. * to break COW, except for shared mappings because these don't COW
  161. * and we would not want to dirty them for nothing.
  162. */
  163. if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
  164. gup_flags |= FOLL_WRITE;
  165. /*
  166. * We want mlock to succeed for regions that have any permissions
  167. * other than PROT_NONE.
  168. */
  169. if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC))
  170. gup_flags |= FOLL_FORCE;
  171. return __get_user_pages(current, mm, addr, nr_pages, gup_flags,
  172. NULL, NULL, nonblocking);
  173. }
  174. /*
  175. * convert get_user_pages() return value to posix mlock() error
  176. */
  177. static int __mlock_posix_error_return(long retval)
  178. {
  179. if (retval == -EFAULT)
  180. retval = -ENOMEM;
  181. else if (retval == -ENOMEM)
  182. retval = -EAGAIN;
  183. return retval;
  184. }
  185. /**
  186. * mlock_vma_pages_range() - mlock pages in specified vma range.
  187. * @vma - the vma containing the specfied address range
  188. * @start - starting address in @vma to mlock
  189. * @end - end address [+1] in @vma to mlock
  190. *
  191. * For mmap()/mremap()/expansion of mlocked vma.
  192. *
  193. * return 0 on success for "normal" vmas.
  194. *
  195. * return number of pages [> 0] to be removed from locked_vm on success
  196. * of "special" vmas.
  197. */
  198. long mlock_vma_pages_range(struct vm_area_struct *vma,
  199. unsigned long start, unsigned long end)
  200. {
  201. int nr_pages = (end - start) / PAGE_SIZE;
  202. BUG_ON(!(vma->vm_flags & VM_LOCKED));
  203. /*
  204. * filter unlockable vmas
  205. */
  206. if (vma->vm_flags & (VM_IO | VM_PFNMAP))
  207. goto no_mlock;
  208. if (!((vma->vm_flags & (VM_DONTEXPAND | VM_RESERVED)) ||
  209. is_vm_hugetlb_page(vma) ||
  210. vma == get_gate_vma(current->mm))) {
  211. __mlock_vma_pages_range(vma, start, end, NULL);
  212. /* Hide errors from mmap() and other callers */
  213. return 0;
  214. }
  215. /*
  216. * User mapped kernel pages or huge pages:
  217. * make these pages present to populate the ptes, but
  218. * fall thru' to reset VM_LOCKED--no need to unlock, and
  219. * return nr_pages so these don't get counted against task's
  220. * locked limit. huge pages are already counted against
  221. * locked vm limit.
  222. */
  223. make_pages_present(start, end);
  224. no_mlock:
  225. vma->vm_flags &= ~VM_LOCKED; /* and don't come back! */
  226. return nr_pages; /* error or pages NOT mlocked */
  227. }
  228. /*
  229. * munlock_vma_pages_range() - munlock all pages in the vma range.'
  230. * @vma - vma containing range to be munlock()ed.
  231. * @start - start address in @vma of the range
  232. * @end - end of range in @vma.
  233. *
  234. * For mremap(), munmap() and exit().
  235. *
  236. * Called with @vma VM_LOCKED.
  237. *
  238. * Returns with VM_LOCKED cleared. Callers must be prepared to
  239. * deal with this.
  240. *
  241. * We don't save and restore VM_LOCKED here because pages are
  242. * still on lru. In unmap path, pages might be scanned by reclaim
  243. * and re-mlocked by try_to_{munlock|unmap} before we unmap and
  244. * free them. This will result in freeing mlocked pages.
  245. */
  246. void munlock_vma_pages_range(struct vm_area_struct *vma,
  247. unsigned long start, unsigned long end)
  248. {
  249. unsigned long addr;
  250. lru_add_drain();
  251. vma->vm_flags &= ~VM_LOCKED;
  252. for (addr = start; addr < end; addr += PAGE_SIZE) {
  253. struct page *page;
  254. /*
  255. * Although FOLL_DUMP is intended for get_dump_page(),
  256. * it just so happens that its special treatment of the
  257. * ZERO_PAGE (returning an error instead of doing get_page)
  258. * suits munlock very well (and if somehow an abnormal page
  259. * has sneaked into the range, we won't oops here: great).
  260. */
  261. page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP);
  262. if (page && !IS_ERR(page)) {
  263. lock_page(page);
  264. /*
  265. * Like in __mlock_vma_pages_range(),
  266. * because we lock page here and migration is
  267. * blocked by the elevated reference, we need
  268. * only check for file-cache page truncation.
  269. */
  270. if (page->mapping)
  271. munlock_vma_page(page);
  272. unlock_page(page);
  273. put_page(page);
  274. }
  275. cond_resched();
  276. }
  277. }
  278. /*
  279. * mlock_fixup - handle mlock[all]/munlock[all] requests.
  280. *
  281. * Filters out "special" vmas -- VM_LOCKED never gets set for these, and
  282. * munlock is a no-op. However, for some special vmas, we go ahead and
  283. * populate the ptes via make_pages_present().
  284. *
  285. * For vmas that pass the filters, merge/split as appropriate.
  286. */
  287. static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev,
  288. unsigned long start, unsigned long end, vm_flags_t newflags)
  289. {
  290. struct mm_struct *mm = vma->vm_mm;
  291. pgoff_t pgoff;
  292. int nr_pages;
  293. int ret = 0;
  294. int lock = !!(newflags & VM_LOCKED);
  295. if (newflags == vma->vm_flags || (vma->vm_flags & VM_SPECIAL) ||
  296. is_vm_hugetlb_page(vma) || vma == get_gate_vma(current->mm))
  297. goto out; /* don't set VM_LOCKED, don't count */
  298. pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  299. *prev = vma_merge(mm, *prev, start, end, newflags, vma->anon_vma,
  300. vma->vm_file, pgoff, vma_policy(vma));
  301. if (*prev) {
  302. vma = *prev;
  303. goto success;
  304. }
  305. if (start != vma->vm_start) {
  306. ret = split_vma(mm, vma, start, 1);
  307. if (ret)
  308. goto out;
  309. }
  310. if (end != vma->vm_end) {
  311. ret = split_vma(mm, vma, end, 0);
  312. if (ret)
  313. goto out;
  314. }
  315. success:
  316. /*
  317. * Keep track of amount of locked VM.
  318. */
  319. nr_pages = (end - start) >> PAGE_SHIFT;
  320. if (!lock)
  321. nr_pages = -nr_pages;
  322. mm->locked_vm += nr_pages;
  323. /*
  324. * vm_flags is protected by the mmap_sem held in write mode.
  325. * It's okay if try_to_unmap_one unmaps a page just after we
  326. * set VM_LOCKED, __mlock_vma_pages_range will bring it back.
  327. */
  328. if (lock)
  329. vma->vm_flags = newflags;
  330. else
  331. munlock_vma_pages_range(vma, start, end);
  332. out:
  333. *prev = vma;
  334. return ret;
  335. }
  336. static int do_mlock(unsigned long start, size_t len, int on)
  337. {
  338. unsigned long nstart, end, tmp;
  339. struct vm_area_struct * vma, * prev;
  340. int error;
  341. VM_BUG_ON(start & ~PAGE_MASK);
  342. VM_BUG_ON(len != PAGE_ALIGN(len));
  343. end = start + len;
  344. if (end < start)
  345. return -EINVAL;
  346. if (end == start)
  347. return 0;
  348. vma = find_vma(current->mm, start);
  349. if (!vma || vma->vm_start > start)
  350. return -ENOMEM;
  351. prev = vma->vm_prev;
  352. if (start > vma->vm_start)
  353. prev = vma;
  354. for (nstart = start ; ; ) {
  355. vm_flags_t newflags;
  356. /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
  357. newflags = vma->vm_flags | VM_LOCKED;
  358. if (!on)
  359. newflags &= ~VM_LOCKED;
  360. tmp = vma->vm_end;
  361. if (tmp > end)
  362. tmp = end;
  363. error = mlock_fixup(vma, &prev, nstart, tmp, newflags);
  364. if (error)
  365. break;
  366. nstart = tmp;
  367. if (nstart < prev->vm_end)
  368. nstart = prev->vm_end;
  369. if (nstart >= end)
  370. break;
  371. vma = prev->vm_next;
  372. if (!vma || vma->vm_start != nstart) {
  373. error = -ENOMEM;
  374. break;
  375. }
  376. }
  377. return error;
  378. }
  379. static int do_mlock_pages(unsigned long start, size_t len, int ignore_errors)
  380. {
  381. struct mm_struct *mm = current->mm;
  382. unsigned long end, nstart, nend;
  383. struct vm_area_struct *vma = NULL;
  384. int locked = 0;
  385. int ret = 0;
  386. VM_BUG_ON(start & ~PAGE_MASK);
  387. VM_BUG_ON(len != PAGE_ALIGN(len));
  388. end = start + len;
  389. for (nstart = start; nstart < end; nstart = nend) {
  390. /*
  391. * We want to fault in pages for [nstart; end) address range.
  392. * Find first corresponding VMA.
  393. */
  394. if (!locked) {
  395. locked = 1;
  396. down_read(&mm->mmap_sem);
  397. vma = find_vma(mm, nstart);
  398. } else if (nstart >= vma->vm_end)
  399. vma = vma->vm_next;
  400. if (!vma || vma->vm_start >= end)
  401. break;
  402. /*
  403. * Set [nstart; nend) to intersection of desired address
  404. * range with the first VMA. Also, skip undesirable VMA types.
  405. */
  406. nend = min(end, vma->vm_end);
  407. if (vma->vm_flags & (VM_IO | VM_PFNMAP))
  408. continue;
  409. if (nstart < vma->vm_start)
  410. nstart = vma->vm_start;
  411. /*
  412. * Now fault in a range of pages. __mlock_vma_pages_range()
  413. * double checks the vma flags, so that it won't mlock pages
  414. * if the vma was already munlocked.
  415. */
  416. ret = __mlock_vma_pages_range(vma, nstart, nend, &locked);
  417. if (ret < 0) {
  418. if (ignore_errors) {
  419. ret = 0;
  420. continue; /* continue at next VMA */
  421. }
  422. ret = __mlock_posix_error_return(ret);
  423. break;
  424. }
  425. nend = nstart + ret * PAGE_SIZE;
  426. ret = 0;
  427. }
  428. if (locked)
  429. up_read(&mm->mmap_sem);
  430. return ret; /* 0 or negative error code */
  431. }
  432. SYSCALL_DEFINE2(mlock, unsigned long, start, size_t, len)
  433. {
  434. unsigned long locked;
  435. unsigned long lock_limit;
  436. int error = -ENOMEM;
  437. if (!can_do_mlock())
  438. return -EPERM;
  439. lru_add_drain_all(); /* flush pagevec */
  440. down_write(&current->mm->mmap_sem);
  441. len = PAGE_ALIGN(len + (start & ~PAGE_MASK));
  442. start &= PAGE_MASK;
  443. locked = len >> PAGE_SHIFT;
  444. locked += current->mm->locked_vm;
  445. lock_limit = rlimit(RLIMIT_MEMLOCK);
  446. lock_limit >>= PAGE_SHIFT;
  447. /* check against resource limits */
  448. if ((locked <= lock_limit) || capable(CAP_IPC_LOCK))
  449. error = do_mlock(start, len, 1);
  450. up_write(&current->mm->mmap_sem);
  451. if (!error)
  452. error = do_mlock_pages(start, len, 0);
  453. return error;
  454. }
  455. SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len)
  456. {
  457. int ret;
  458. down_write(&current->mm->mmap_sem);
  459. len = PAGE_ALIGN(len + (start & ~PAGE_MASK));
  460. start &= PAGE_MASK;
  461. ret = do_mlock(start, len, 0);
  462. up_write(&current->mm->mmap_sem);
  463. return ret;
  464. }
  465. static int do_mlockall(int flags)
  466. {
  467. struct vm_area_struct * vma, * prev = NULL;
  468. unsigned int def_flags = 0;
  469. if (flags & MCL_FUTURE)
  470. def_flags = VM_LOCKED;
  471. current->mm->def_flags = def_flags;
  472. if (flags == MCL_FUTURE)
  473. goto out;
  474. for (vma = current->mm->mmap; vma ; vma = prev->vm_next) {
  475. vm_flags_t newflags;
  476. newflags = vma->vm_flags | VM_LOCKED;
  477. if (!(flags & MCL_CURRENT))
  478. newflags &= ~VM_LOCKED;
  479. /* Ignore errors */
  480. mlock_fixup(vma, &prev, vma->vm_start, vma->vm_end, newflags);
  481. }
  482. out:
  483. return 0;
  484. }
  485. SYSCALL_DEFINE1(mlockall, int, flags)
  486. {
  487. unsigned long lock_limit;
  488. int ret = -EINVAL;
  489. if (!flags || (flags & ~(MCL_CURRENT | MCL_FUTURE)))
  490. goto out;
  491. ret = -EPERM;
  492. if (!can_do_mlock())
  493. goto out;
  494. if (flags & MCL_CURRENT)
  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. if (!ret && (flags & MCL_CURRENT)) {
  505. /* Ignore errors */
  506. do_mlock_pages(0, TASK_SIZE, 1);
  507. }
  508. out:
  509. return ret;
  510. }
  511. SYSCALL_DEFINE0(munlockall)
  512. {
  513. int ret;
  514. down_write(&current->mm->mmap_sem);
  515. ret = do_mlockall(0);
  516. up_write(&current->mm->mmap_sem);
  517. return ret;
  518. }
  519. /*
  520. * Objects with different lifetime than processes (SHM_LOCK and SHM_HUGETLB
  521. * shm segments) get accounted against the user_struct instead.
  522. */
  523. static DEFINE_SPINLOCK(shmlock_user_lock);
  524. int user_shm_lock(size_t size, struct user_struct *user)
  525. {
  526. unsigned long lock_limit, locked;
  527. int allowed = 0;
  528. locked = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  529. lock_limit = rlimit(RLIMIT_MEMLOCK);
  530. if (lock_limit == RLIM_INFINITY)
  531. allowed = 1;
  532. lock_limit >>= PAGE_SHIFT;
  533. spin_lock(&shmlock_user_lock);
  534. if (!allowed &&
  535. locked + user->locked_shm > lock_limit && !capable(CAP_IPC_LOCK))
  536. goto out;
  537. get_uid(user);
  538. user->locked_shm += locked;
  539. allowed = 1;
  540. out:
  541. spin_unlock(&shmlock_user_lock);
  542. return allowed;
  543. }
  544. void user_shm_unlock(size_t size, struct user_struct *user)
  545. {
  546. spin_lock(&shmlock_user_lock);
  547. user->locked_shm -= (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  548. spin_unlock(&shmlock_user_lock);
  549. free_uid(user);
  550. }