mlock.c 16 KB

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