mlock.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. if (!TestClearPageMlocked(page))
  52. return;
  53. mod_zone_page_state(page_zone(page), NR_MLOCK,
  54. -hpage_nr_pages(page));
  55. count_vm_event(UNEVICTABLE_PGCLEARED);
  56. if (!isolate_lru_page(page)) {
  57. putback_lru_page(page);
  58. } else {
  59. /*
  60. * We lost the race. the page already moved to evictable list.
  61. */
  62. if (PageUnevictable(page))
  63. count_vm_event(UNEVICTABLE_PGSTRANDED);
  64. }
  65. }
  66. /*
  67. * Mark page as mlocked if not already.
  68. * If page on LRU, isolate and putback to move to unevictable list.
  69. */
  70. void mlock_vma_page(struct page *page)
  71. {
  72. BUG_ON(!PageLocked(page));
  73. if (!TestSetPageMlocked(page)) {
  74. mod_zone_page_state(page_zone(page), NR_MLOCK,
  75. hpage_nr_pages(page));
  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. unsigned int munlock_vma_page(struct page *page)
  97. {
  98. unsigned int page_mask = 0;
  99. BUG_ON(!PageLocked(page));
  100. if (TestClearPageMlocked(page)) {
  101. unsigned int nr_pages = hpage_nr_pages(page);
  102. mod_zone_page_state(page_zone(page), NR_MLOCK, -nr_pages);
  103. page_mask = nr_pages - 1;
  104. if (!isolate_lru_page(page)) {
  105. int ret = SWAP_AGAIN;
  106. /*
  107. * Optimization: if the page was mapped just once,
  108. * that's our mapping and we don't need to check all the
  109. * other vmas.
  110. */
  111. if (page_mapcount(page) > 1)
  112. ret = try_to_munlock(page);
  113. /*
  114. * did try_to_unlock() succeed or punt?
  115. */
  116. if (ret != SWAP_MLOCK)
  117. count_vm_event(UNEVICTABLE_PGMUNLOCKED);
  118. putback_lru_page(page);
  119. } else {
  120. /*
  121. * Some other task has removed the page from the LRU.
  122. * putback_lru_page() will take care of removing the
  123. * page from the unevictable list, if necessary.
  124. * vmscan [page_referenced()] will move the page back
  125. * to the unevictable list if some other vma has it
  126. * mlocked.
  127. */
  128. if (PageUnevictable(page))
  129. count_vm_event(UNEVICTABLE_PGSTRANDED);
  130. else
  131. count_vm_event(UNEVICTABLE_PGMUNLOCKED);
  132. }
  133. }
  134. return page_mask;
  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. long __mlock_vma_pages_range(struct vm_area_struct *vma,
  149. unsigned long start, unsigned long end, int *nonblocking)
  150. {
  151. struct mm_struct *mm = vma->vm_mm;
  152. unsigned long 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. /*
  174. * We made sure addr is within a VMA, so the following will
  175. * not result in a stack expansion that recurses back here.
  176. */
  177. return __get_user_pages(current, mm, start, nr_pages, gup_flags,
  178. NULL, NULL, nonblocking);
  179. }
  180. /*
  181. * convert get_user_pages() return value to posix mlock() error
  182. */
  183. static int __mlock_posix_error_return(long retval)
  184. {
  185. if (retval == -EFAULT)
  186. retval = -ENOMEM;
  187. else if (retval == -ENOMEM)
  188. retval = -EAGAIN;
  189. return retval;
  190. }
  191. /*
  192. * munlock_vma_pages_range() - munlock all pages in the vma range.'
  193. * @vma - vma containing range to be munlock()ed.
  194. * @start - start address in @vma of the range
  195. * @end - end of range in @vma.
  196. *
  197. * For mremap(), munmap() and exit().
  198. *
  199. * Called with @vma VM_LOCKED.
  200. *
  201. * Returns with VM_LOCKED cleared. Callers must be prepared to
  202. * deal with this.
  203. *
  204. * We don't save and restore VM_LOCKED here because pages are
  205. * still on lru. In unmap path, pages might be scanned by reclaim
  206. * and re-mlocked by try_to_{munlock|unmap} before we unmap and
  207. * free them. This will result in freeing mlocked pages.
  208. */
  209. void munlock_vma_pages_range(struct vm_area_struct *vma,
  210. unsigned long start, unsigned long end)
  211. {
  212. vma->vm_flags &= ~VM_LOCKED;
  213. while (start < end) {
  214. struct page *page;
  215. unsigned int page_mask, page_increm;
  216. /*
  217. * Although FOLL_DUMP is intended for get_dump_page(),
  218. * it just so happens that its special treatment of the
  219. * ZERO_PAGE (returning an error instead of doing get_page)
  220. * suits munlock very well (and if somehow an abnormal page
  221. * has sneaked into the range, we won't oops here: great).
  222. */
  223. page = follow_page_mask(vma, start, FOLL_GET | FOLL_DUMP,
  224. &page_mask);
  225. if (page && !IS_ERR(page)) {
  226. lock_page(page);
  227. lru_add_drain();
  228. /*
  229. * Any THP page found by follow_page_mask() may have
  230. * gotten split before reaching munlock_vma_page(),
  231. * so we need to recompute the page_mask here.
  232. */
  233. page_mask = munlock_vma_page(page);
  234. unlock_page(page);
  235. put_page(page);
  236. }
  237. page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask);
  238. start += page_increm * PAGE_SIZE;
  239. cond_resched();
  240. }
  241. }
  242. /*
  243. * mlock_fixup - handle mlock[all]/munlock[all] requests.
  244. *
  245. * Filters out "special" vmas -- VM_LOCKED never gets set for these, and
  246. * munlock is a no-op. However, for some special vmas, we go ahead and
  247. * populate the ptes.
  248. *
  249. * For vmas that pass the filters, merge/split as appropriate.
  250. */
  251. static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev,
  252. unsigned long start, unsigned long end, vm_flags_t newflags)
  253. {
  254. struct mm_struct *mm = vma->vm_mm;
  255. pgoff_t pgoff;
  256. int nr_pages;
  257. int ret = 0;
  258. int lock = !!(newflags & VM_LOCKED);
  259. if (newflags == vma->vm_flags || (vma->vm_flags & VM_SPECIAL) ||
  260. is_vm_hugetlb_page(vma) || vma == get_gate_vma(current->mm))
  261. goto out; /* don't set VM_LOCKED, don't count */
  262. pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  263. *prev = vma_merge(mm, *prev, start, end, newflags, vma->anon_vma,
  264. vma->vm_file, pgoff, vma_policy(vma));
  265. if (*prev) {
  266. vma = *prev;
  267. goto success;
  268. }
  269. if (start != vma->vm_start) {
  270. ret = split_vma(mm, vma, start, 1);
  271. if (ret)
  272. goto out;
  273. }
  274. if (end != vma->vm_end) {
  275. ret = split_vma(mm, vma, end, 0);
  276. if (ret)
  277. goto out;
  278. }
  279. success:
  280. /*
  281. * Keep track of amount of locked VM.
  282. */
  283. nr_pages = (end - start) >> PAGE_SHIFT;
  284. if (!lock)
  285. nr_pages = -nr_pages;
  286. mm->locked_vm += nr_pages;
  287. /*
  288. * vm_flags is protected by the mmap_sem held in write mode.
  289. * It's okay if try_to_unmap_one unmaps a page just after we
  290. * set VM_LOCKED, __mlock_vma_pages_range will bring it back.
  291. */
  292. if (lock)
  293. vma->vm_flags = newflags;
  294. else
  295. munlock_vma_pages_range(vma, start, end);
  296. out:
  297. *prev = vma;
  298. return ret;
  299. }
  300. static int do_mlock(unsigned long start, size_t len, int on)
  301. {
  302. unsigned long nstart, end, tmp;
  303. struct vm_area_struct * vma, * prev;
  304. int error;
  305. VM_BUG_ON(start & ~PAGE_MASK);
  306. VM_BUG_ON(len != PAGE_ALIGN(len));
  307. end = start + len;
  308. if (end < start)
  309. return -EINVAL;
  310. if (end == start)
  311. return 0;
  312. vma = find_vma(current->mm, start);
  313. if (!vma || vma->vm_start > start)
  314. return -ENOMEM;
  315. prev = vma->vm_prev;
  316. if (start > vma->vm_start)
  317. prev = vma;
  318. for (nstart = start ; ; ) {
  319. vm_flags_t newflags;
  320. /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
  321. newflags = vma->vm_flags & ~VM_LOCKED;
  322. if (on)
  323. newflags |= VM_LOCKED;
  324. tmp = vma->vm_end;
  325. if (tmp > end)
  326. tmp = end;
  327. error = mlock_fixup(vma, &prev, nstart, tmp, newflags);
  328. if (error)
  329. break;
  330. nstart = tmp;
  331. if (nstart < prev->vm_end)
  332. nstart = prev->vm_end;
  333. if (nstart >= end)
  334. break;
  335. vma = prev->vm_next;
  336. if (!vma || vma->vm_start != nstart) {
  337. error = -ENOMEM;
  338. break;
  339. }
  340. }
  341. return error;
  342. }
  343. /*
  344. * __mm_populate - populate and/or mlock pages within a range of address space.
  345. *
  346. * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
  347. * flags. VMAs must be already marked with the desired vm_flags, and
  348. * mmap_sem must not be held.
  349. */
  350. int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
  351. {
  352. struct mm_struct *mm = current->mm;
  353. unsigned long end, nstart, nend;
  354. struct vm_area_struct *vma = NULL;
  355. int locked = 0;
  356. long ret = 0;
  357. VM_BUG_ON(start & ~PAGE_MASK);
  358. VM_BUG_ON(len != PAGE_ALIGN(len));
  359. end = start + len;
  360. for (nstart = start; nstart < end; nstart = nend) {
  361. /*
  362. * We want to fault in pages for [nstart; end) address range.
  363. * Find first corresponding VMA.
  364. */
  365. if (!locked) {
  366. locked = 1;
  367. down_read(&mm->mmap_sem);
  368. vma = find_vma(mm, nstart);
  369. } else if (nstart >= vma->vm_end)
  370. vma = vma->vm_next;
  371. if (!vma || vma->vm_start >= end)
  372. break;
  373. /*
  374. * Set [nstart; nend) to intersection of desired address
  375. * range with the first VMA. Also, skip undesirable VMA types.
  376. */
  377. nend = min(end, vma->vm_end);
  378. if (vma->vm_flags & (VM_IO | VM_PFNMAP))
  379. continue;
  380. if (nstart < vma->vm_start)
  381. nstart = vma->vm_start;
  382. /*
  383. * Now fault in a range of pages. __mlock_vma_pages_range()
  384. * double checks the vma flags, so that it won't mlock pages
  385. * if the vma was already munlocked.
  386. */
  387. ret = __mlock_vma_pages_range(vma, nstart, nend, &locked);
  388. if (ret < 0) {
  389. if (ignore_errors) {
  390. ret = 0;
  391. continue; /* continue at next VMA */
  392. }
  393. ret = __mlock_posix_error_return(ret);
  394. break;
  395. }
  396. nend = nstart + ret * PAGE_SIZE;
  397. ret = 0;
  398. }
  399. if (locked)
  400. up_read(&mm->mmap_sem);
  401. return ret; /* 0 or negative error code */
  402. }
  403. SYSCALL_DEFINE2(mlock, unsigned long, start, size_t, len)
  404. {
  405. unsigned long locked;
  406. unsigned long lock_limit;
  407. int error = -ENOMEM;
  408. if (!can_do_mlock())
  409. return -EPERM;
  410. lru_add_drain_all(); /* flush pagevec */
  411. down_write(&current->mm->mmap_sem);
  412. len = PAGE_ALIGN(len + (start & ~PAGE_MASK));
  413. start &= PAGE_MASK;
  414. locked = len >> PAGE_SHIFT;
  415. locked += current->mm->locked_vm;
  416. lock_limit = rlimit(RLIMIT_MEMLOCK);
  417. lock_limit >>= PAGE_SHIFT;
  418. /* check against resource limits */
  419. if ((locked <= lock_limit) || capable(CAP_IPC_LOCK))
  420. error = do_mlock(start, len, 1);
  421. up_write(&current->mm->mmap_sem);
  422. if (!error)
  423. error = __mm_populate(start, len, 0);
  424. return error;
  425. }
  426. SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len)
  427. {
  428. int ret;
  429. down_write(&current->mm->mmap_sem);
  430. len = PAGE_ALIGN(len + (start & ~PAGE_MASK));
  431. start &= PAGE_MASK;
  432. ret = do_mlock(start, len, 0);
  433. up_write(&current->mm->mmap_sem);
  434. return ret;
  435. }
  436. static int do_mlockall(int flags)
  437. {
  438. struct vm_area_struct * vma, * prev = NULL;
  439. if (flags & MCL_FUTURE)
  440. current->mm->def_flags |= VM_LOCKED;
  441. else
  442. current->mm->def_flags &= ~VM_LOCKED;
  443. if (flags == MCL_FUTURE)
  444. goto out;
  445. for (vma = current->mm->mmap; vma ; vma = prev->vm_next) {
  446. vm_flags_t newflags;
  447. newflags = vma->vm_flags & ~VM_LOCKED;
  448. if (flags & MCL_CURRENT)
  449. newflags |= VM_LOCKED;
  450. /* Ignore errors */
  451. mlock_fixup(vma, &prev, vma->vm_start, vma->vm_end, newflags);
  452. }
  453. out:
  454. return 0;
  455. }
  456. SYSCALL_DEFINE1(mlockall, int, flags)
  457. {
  458. unsigned long lock_limit;
  459. int ret = -EINVAL;
  460. if (!flags || (flags & ~(MCL_CURRENT | MCL_FUTURE)))
  461. goto out;
  462. ret = -EPERM;
  463. if (!can_do_mlock())
  464. goto out;
  465. if (flags & MCL_CURRENT)
  466. lru_add_drain_all(); /* flush pagevec */
  467. down_write(&current->mm->mmap_sem);
  468. lock_limit = rlimit(RLIMIT_MEMLOCK);
  469. lock_limit >>= PAGE_SHIFT;
  470. ret = -ENOMEM;
  471. if (!(flags & MCL_CURRENT) || (current->mm->total_vm <= lock_limit) ||
  472. capable(CAP_IPC_LOCK))
  473. ret = do_mlockall(flags);
  474. up_write(&current->mm->mmap_sem);
  475. if (!ret && (flags & MCL_CURRENT))
  476. mm_populate(0, TASK_SIZE);
  477. out:
  478. return ret;
  479. }
  480. SYSCALL_DEFINE0(munlockall)
  481. {
  482. int ret;
  483. down_write(&current->mm->mmap_sem);
  484. ret = do_mlockall(0);
  485. up_write(&current->mm->mmap_sem);
  486. return ret;
  487. }
  488. /*
  489. * Objects with different lifetime than processes (SHM_LOCK and SHM_HUGETLB
  490. * shm segments) get accounted against the user_struct instead.
  491. */
  492. static DEFINE_SPINLOCK(shmlock_user_lock);
  493. int user_shm_lock(size_t size, struct user_struct *user)
  494. {
  495. unsigned long lock_limit, locked;
  496. int allowed = 0;
  497. locked = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  498. lock_limit = rlimit(RLIMIT_MEMLOCK);
  499. if (lock_limit == RLIM_INFINITY)
  500. allowed = 1;
  501. lock_limit >>= PAGE_SHIFT;
  502. spin_lock(&shmlock_user_lock);
  503. if (!allowed &&
  504. locked + user->locked_shm > lock_limit && !capable(CAP_IPC_LOCK))
  505. goto out;
  506. get_uid(user);
  507. user->locked_shm += locked;
  508. allowed = 1;
  509. out:
  510. spin_unlock(&shmlock_user_lock);
  511. return allowed;
  512. }
  513. void user_shm_unlock(size_t size, struct user_struct *user)
  514. {
  515. spin_lock(&shmlock_user_lock);
  516. user->locked_shm -= (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  517. spin_unlock(&shmlock_user_lock);
  518. free_uid(user);
  519. }