mlock.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/mm/mlock.c
  4. *
  5. * (C) Copyright 1995 Linus Torvalds
  6. * (C) Copyright 2002 Christoph Hellwig
  7. */
  8. #include <linux/capability.h>
  9. #include <linux/mman.h>
  10. #include <linux/mm.h>
  11. #include <linux/sched/user.h>
  12. #include <linux/swap.h>
  13. #include <linux/swapops.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/pagevec.h>
  16. #include <linux/mempolicy.h>
  17. #include <linux/syscalls.h>
  18. #include <linux/sched.h>
  19. #include <linux/export.h>
  20. #include <linux/rmap.h>
  21. #include <linux/mmzone.h>
  22. #include <linux/hugetlb.h>
  23. #include <linux/memcontrol.h>
  24. #include <linux/mm_inline.h>
  25. #include "internal.h"
  26. bool can_do_mlock(void)
  27. {
  28. if (rlimit(RLIMIT_MEMLOCK) != 0)
  29. return true;
  30. if (capable(CAP_IPC_LOCK))
  31. return true;
  32. return false;
  33. }
  34. EXPORT_SYMBOL(can_do_mlock);
  35. /*
  36. * Mlocked pages are marked with PageMlocked() flag for efficient testing
  37. * in vmscan and, possibly, the fault path; and to support semi-accurate
  38. * statistics.
  39. *
  40. * An mlocked page [PageMlocked(page)] is unevictable. As such, it will
  41. * be placed on the LRU "unevictable" list, rather than the [in]active lists.
  42. * The unevictable list is an LRU sibling list to the [in]active lists.
  43. * PageUnevictable is set to indicate the unevictable state.
  44. *
  45. * When lazy mlocking via vmscan, it is important to ensure that the
  46. * vma's VM_LOCKED status is not concurrently being modified, otherwise we
  47. * may have mlocked a page that is being munlocked. So lazy mlock must take
  48. * the mmap_sem for read, and verify that the vma really is locked
  49. * (see mm/rmap.c).
  50. */
  51. /*
  52. * LRU accounting for clear_page_mlock()
  53. */
  54. void clear_page_mlock(struct page *page)
  55. {
  56. if (!TestClearPageMlocked(page))
  57. return;
  58. mod_zone_page_state(page_zone(page), NR_MLOCK,
  59. -hpage_nr_pages(page));
  60. count_vm_event(UNEVICTABLE_PGCLEARED);
  61. /*
  62. * The previous TestClearPageMlocked() corresponds to the smp_mb()
  63. * in __pagevec_lru_add_fn().
  64. *
  65. * See __pagevec_lru_add_fn for more explanation.
  66. */
  67. if (!isolate_lru_page(page)) {
  68. putback_lru_page(page);
  69. } else {
  70. /*
  71. * We lost the race. the page already moved to evictable list.
  72. */
  73. if (PageUnevictable(page))
  74. count_vm_event(UNEVICTABLE_PGSTRANDED);
  75. }
  76. }
  77. /*
  78. * Mark page as mlocked if not already.
  79. * If page on LRU, isolate and putback to move to unevictable list.
  80. */
  81. void mlock_vma_page(struct page *page)
  82. {
  83. /* Serialize with page migration */
  84. BUG_ON(!PageLocked(page));
  85. VM_BUG_ON_PAGE(PageTail(page), page);
  86. VM_BUG_ON_PAGE(PageCompound(page) && PageDoubleMap(page), page);
  87. if (!TestSetPageMlocked(page)) {
  88. mod_zone_page_state(page_zone(page), NR_MLOCK,
  89. hpage_nr_pages(page));
  90. count_vm_event(UNEVICTABLE_PGMLOCKED);
  91. if (!isolate_lru_page(page))
  92. putback_lru_page(page);
  93. }
  94. }
  95. /*
  96. * Isolate a page from LRU with optional get_page() pin.
  97. * Assumes lru_lock already held and page already pinned.
  98. */
  99. static bool __munlock_isolate_lru_page(struct page *page, bool getpage)
  100. {
  101. if (PageLRU(page)) {
  102. struct lruvec *lruvec;
  103. lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page));
  104. if (getpage)
  105. get_page(page);
  106. ClearPageLRU(page);
  107. del_page_from_lru_list(page, lruvec, page_lru(page));
  108. return true;
  109. }
  110. return false;
  111. }
  112. /*
  113. * Finish munlock after successful page isolation
  114. *
  115. * Page must be locked. This is a wrapper for try_to_munlock()
  116. * and putback_lru_page() with munlock accounting.
  117. */
  118. static void __munlock_isolated_page(struct page *page)
  119. {
  120. /*
  121. * Optimization: if the page was mapped just once, that's our mapping
  122. * and we don't need to check all the other vmas.
  123. */
  124. if (page_mapcount(page) > 1)
  125. try_to_munlock(page);
  126. /* Did try_to_unlock() succeed or punt? */
  127. if (!PageMlocked(page))
  128. count_vm_event(UNEVICTABLE_PGMUNLOCKED);
  129. putback_lru_page(page);
  130. }
  131. /*
  132. * Accounting for page isolation fail during munlock
  133. *
  134. * Performs accounting when page isolation fails in munlock. There is nothing
  135. * else to do because it means some other task has already removed the page
  136. * from the LRU. putback_lru_page() will take care of removing the page from
  137. * the unevictable list, if necessary. vmscan [page_referenced()] will move
  138. * the page back to the unevictable list if some other vma has it mlocked.
  139. */
  140. static void __munlock_isolation_failed(struct page *page)
  141. {
  142. if (PageUnevictable(page))
  143. __count_vm_event(UNEVICTABLE_PGSTRANDED);
  144. else
  145. __count_vm_event(UNEVICTABLE_PGMUNLOCKED);
  146. }
  147. /**
  148. * munlock_vma_page - munlock a vma page
  149. * @page: page to be unlocked, either a normal page or THP page head
  150. *
  151. * returns the size of the page as a page mask (0 for normal page,
  152. * HPAGE_PMD_NR - 1 for THP head page)
  153. *
  154. * called from munlock()/munmap() path with page supposedly on the LRU.
  155. * When we munlock a page, because the vma where we found the page is being
  156. * munlock()ed or munmap()ed, we want to check whether other vmas hold the
  157. * page locked so that we can leave it on the unevictable lru list and not
  158. * bother vmscan with it. However, to walk the page's rmap list in
  159. * try_to_munlock() we must isolate the page from the LRU. If some other
  160. * task has removed the page from the LRU, we won't be able to do that.
  161. * So we clear the PageMlocked as we might not get another chance. If we
  162. * can't isolate the page, we leave it for putback_lru_page() and vmscan
  163. * [page_referenced()/try_to_unmap()] to deal with.
  164. */
  165. unsigned int munlock_vma_page(struct page *page)
  166. {
  167. int nr_pages;
  168. struct zone *zone = page_zone(page);
  169. /* For try_to_munlock() and to serialize with page migration */
  170. BUG_ON(!PageLocked(page));
  171. VM_BUG_ON_PAGE(PageTail(page), page);
  172. /*
  173. * Serialize with any parallel __split_huge_page_refcount() which
  174. * might otherwise copy PageMlocked to part of the tail pages before
  175. * we clear it in the head page. It also stabilizes hpage_nr_pages().
  176. */
  177. spin_lock_irq(zone_lru_lock(zone));
  178. if (!TestClearPageMlocked(page)) {
  179. /* Potentially, PTE-mapped THP: do not skip the rest PTEs */
  180. nr_pages = 1;
  181. goto unlock_out;
  182. }
  183. nr_pages = hpage_nr_pages(page);
  184. __mod_zone_page_state(zone, NR_MLOCK, -nr_pages);
  185. if (__munlock_isolate_lru_page(page, true)) {
  186. spin_unlock_irq(zone_lru_lock(zone));
  187. __munlock_isolated_page(page);
  188. goto out;
  189. }
  190. __munlock_isolation_failed(page);
  191. unlock_out:
  192. spin_unlock_irq(zone_lru_lock(zone));
  193. out:
  194. return nr_pages - 1;
  195. }
  196. /*
  197. * convert get_user_pages() return value to posix mlock() error
  198. */
  199. static int __mlock_posix_error_return(long retval)
  200. {
  201. if (retval == -EFAULT)
  202. retval = -ENOMEM;
  203. else if (retval == -ENOMEM)
  204. retval = -EAGAIN;
  205. return retval;
  206. }
  207. /*
  208. * Prepare page for fast batched LRU putback via putback_lru_evictable_pagevec()
  209. *
  210. * The fast path is available only for evictable pages with single mapping.
  211. * Then we can bypass the per-cpu pvec and get better performance.
  212. * when mapcount > 1 we need try_to_munlock() which can fail.
  213. * when !page_evictable(), we need the full redo logic of putback_lru_page to
  214. * avoid leaving evictable page in unevictable list.
  215. *
  216. * In case of success, @page is added to @pvec and @pgrescued is incremented
  217. * in case that the page was previously unevictable. @page is also unlocked.
  218. */
  219. static bool __putback_lru_fast_prepare(struct page *page, struct pagevec *pvec,
  220. int *pgrescued)
  221. {
  222. VM_BUG_ON_PAGE(PageLRU(page), page);
  223. VM_BUG_ON_PAGE(!PageLocked(page), page);
  224. if (page_mapcount(page) <= 1 && page_evictable(page)) {
  225. pagevec_add(pvec, page);
  226. if (TestClearPageUnevictable(page))
  227. (*pgrescued)++;
  228. unlock_page(page);
  229. return true;
  230. }
  231. return false;
  232. }
  233. /*
  234. * Putback multiple evictable pages to the LRU
  235. *
  236. * Batched putback of evictable pages that bypasses the per-cpu pvec. Some of
  237. * the pages might have meanwhile become unevictable but that is OK.
  238. */
  239. static void __putback_lru_fast(struct pagevec *pvec, int pgrescued)
  240. {
  241. count_vm_events(UNEVICTABLE_PGMUNLOCKED, pagevec_count(pvec));
  242. /*
  243. *__pagevec_lru_add() calls release_pages() so we don't call
  244. * put_page() explicitly
  245. */
  246. __pagevec_lru_add(pvec);
  247. count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued);
  248. }
  249. /*
  250. * Munlock a batch of pages from the same zone
  251. *
  252. * The work is split to two main phases. First phase clears the Mlocked flag
  253. * and attempts to isolate the pages, all under a single zone lru lock.
  254. * The second phase finishes the munlock only for pages where isolation
  255. * succeeded.
  256. *
  257. * Note that the pagevec may be modified during the process.
  258. */
  259. static void __munlock_pagevec(struct pagevec *pvec, struct zone *zone)
  260. {
  261. int i;
  262. int nr = pagevec_count(pvec);
  263. int delta_munlocked = -nr;
  264. struct pagevec pvec_putback;
  265. int pgrescued = 0;
  266. pagevec_init(&pvec_putback);
  267. /* Phase 1: page isolation */
  268. spin_lock_irq(zone_lru_lock(zone));
  269. for (i = 0; i < nr; i++) {
  270. struct page *page = pvec->pages[i];
  271. if (TestClearPageMlocked(page)) {
  272. /*
  273. * We already have pin from follow_page_mask()
  274. * so we can spare the get_page() here.
  275. */
  276. if (__munlock_isolate_lru_page(page, false))
  277. continue;
  278. else
  279. __munlock_isolation_failed(page);
  280. } else {
  281. delta_munlocked++;
  282. }
  283. /*
  284. * We won't be munlocking this page in the next phase
  285. * but we still need to release the follow_page_mask()
  286. * pin. We cannot do it under lru_lock however. If it's
  287. * the last pin, __page_cache_release() would deadlock.
  288. */
  289. pagevec_add(&pvec_putback, pvec->pages[i]);
  290. pvec->pages[i] = NULL;
  291. }
  292. __mod_zone_page_state(zone, NR_MLOCK, delta_munlocked);
  293. spin_unlock_irq(zone_lru_lock(zone));
  294. /* Now we can release pins of pages that we are not munlocking */
  295. pagevec_release(&pvec_putback);
  296. /* Phase 2: page munlock */
  297. for (i = 0; i < nr; i++) {
  298. struct page *page = pvec->pages[i];
  299. if (page) {
  300. lock_page(page);
  301. if (!__putback_lru_fast_prepare(page, &pvec_putback,
  302. &pgrescued)) {
  303. /*
  304. * Slow path. We don't want to lose the last
  305. * pin before unlock_page()
  306. */
  307. get_page(page); /* for putback_lru_page() */
  308. __munlock_isolated_page(page);
  309. unlock_page(page);
  310. put_page(page); /* from follow_page_mask() */
  311. }
  312. }
  313. }
  314. /*
  315. * Phase 3: page putback for pages that qualified for the fast path
  316. * This will also call put_page() to return pin from follow_page_mask()
  317. */
  318. if (pagevec_count(&pvec_putback))
  319. __putback_lru_fast(&pvec_putback, pgrescued);
  320. }
  321. /*
  322. * Fill up pagevec for __munlock_pagevec using pte walk
  323. *
  324. * The function expects that the struct page corresponding to @start address is
  325. * a non-TPH page already pinned and in the @pvec, and that it belongs to @zone.
  326. *
  327. * The rest of @pvec is filled by subsequent pages within the same pmd and same
  328. * zone, as long as the pte's are present and vm_normal_page() succeeds. These
  329. * pages also get pinned.
  330. *
  331. * Returns the address of the next page that should be scanned. This equals
  332. * @start + PAGE_SIZE when no page could be added by the pte walk.
  333. */
  334. static unsigned long __munlock_pagevec_fill(struct pagevec *pvec,
  335. struct vm_area_struct *vma, struct zone *zone,
  336. unsigned long start, unsigned long end)
  337. {
  338. pte_t *pte;
  339. spinlock_t *ptl;
  340. /*
  341. * Initialize pte walk starting at the already pinned page where we
  342. * are sure that there is a pte, as it was pinned under the same
  343. * mmap_sem write op.
  344. */
  345. pte = get_locked_pte(vma->vm_mm, start, &ptl);
  346. /* Make sure we do not cross the page table boundary */
  347. end = pgd_addr_end(start, end);
  348. end = p4d_addr_end(start, end);
  349. end = pud_addr_end(start, end);
  350. end = pmd_addr_end(start, end);
  351. /* The page next to the pinned page is the first we will try to get */
  352. start += PAGE_SIZE;
  353. while (start < end) {
  354. struct page *page = NULL;
  355. pte++;
  356. if (pte_present(*pte))
  357. page = vm_normal_page(vma, start, *pte);
  358. /*
  359. * Break if page could not be obtained or the page's node+zone does not
  360. * match
  361. */
  362. if (!page || page_zone(page) != zone)
  363. break;
  364. /*
  365. * Do not use pagevec for PTE-mapped THP,
  366. * munlock_vma_pages_range() will handle them.
  367. */
  368. if (PageTransCompound(page))
  369. break;
  370. get_page(page);
  371. /*
  372. * Increase the address that will be returned *before* the
  373. * eventual break due to pvec becoming full by adding the page
  374. */
  375. start += PAGE_SIZE;
  376. if (pagevec_add(pvec, page) == 0)
  377. break;
  378. }
  379. pte_unmap_unlock(pte, ptl);
  380. return start;
  381. }
  382. /*
  383. * munlock_vma_pages_range() - munlock all pages in the vma range.'
  384. * @vma - vma containing range to be munlock()ed.
  385. * @start - start address in @vma of the range
  386. * @end - end of range in @vma.
  387. *
  388. * For mremap(), munmap() and exit().
  389. *
  390. * Called with @vma VM_LOCKED.
  391. *
  392. * Returns with VM_LOCKED cleared. Callers must be prepared to
  393. * deal with this.
  394. *
  395. * We don't save and restore VM_LOCKED here because pages are
  396. * still on lru. In unmap path, pages might be scanned by reclaim
  397. * and re-mlocked by try_to_{munlock|unmap} before we unmap and
  398. * free them. This will result in freeing mlocked pages.
  399. */
  400. void munlock_vma_pages_range(struct vm_area_struct *vma,
  401. unsigned long start, unsigned long end)
  402. {
  403. vma->vm_flags &= VM_LOCKED_CLEAR_MASK;
  404. while (start < end) {
  405. struct page *page;
  406. unsigned int page_mask = 0;
  407. unsigned long page_increm;
  408. struct pagevec pvec;
  409. struct zone *zone;
  410. pagevec_init(&pvec);
  411. /*
  412. * Although FOLL_DUMP is intended for get_dump_page(),
  413. * it just so happens that its special treatment of the
  414. * ZERO_PAGE (returning an error instead of doing get_page)
  415. * suits munlock very well (and if somehow an abnormal page
  416. * has sneaked into the range, we won't oops here: great).
  417. */
  418. page = follow_page(vma, start, FOLL_GET | FOLL_DUMP);
  419. if (page && !IS_ERR(page)) {
  420. if (PageTransTail(page)) {
  421. VM_BUG_ON_PAGE(PageMlocked(page), page);
  422. put_page(page); /* follow_page_mask() */
  423. } else if (PageTransHuge(page)) {
  424. lock_page(page);
  425. /*
  426. * Any THP page found by follow_page_mask() may
  427. * have gotten split before reaching
  428. * munlock_vma_page(), so we need to compute
  429. * the page_mask here instead.
  430. */
  431. page_mask = munlock_vma_page(page);
  432. unlock_page(page);
  433. put_page(page); /* follow_page_mask() */
  434. } else {
  435. /*
  436. * Non-huge pages are handled in batches via
  437. * pagevec. The pin from follow_page_mask()
  438. * prevents them from collapsing by THP.
  439. */
  440. pagevec_add(&pvec, page);
  441. zone = page_zone(page);
  442. /*
  443. * Try to fill the rest of pagevec using fast
  444. * pte walk. This will also update start to
  445. * the next page to process. Then munlock the
  446. * pagevec.
  447. */
  448. start = __munlock_pagevec_fill(&pvec, vma,
  449. zone, start, end);
  450. __munlock_pagevec(&pvec, zone);
  451. goto next;
  452. }
  453. }
  454. page_increm = 1 + page_mask;
  455. start += page_increm * PAGE_SIZE;
  456. next:
  457. cond_resched();
  458. }
  459. }
  460. /*
  461. * mlock_fixup - handle mlock[all]/munlock[all] requests.
  462. *
  463. * Filters out "special" vmas -- VM_LOCKED never gets set for these, and
  464. * munlock is a no-op. However, for some special vmas, we go ahead and
  465. * populate the ptes.
  466. *
  467. * For vmas that pass the filters, merge/split as appropriate.
  468. */
  469. static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev,
  470. unsigned long start, unsigned long end, vm_flags_t newflags)
  471. {
  472. struct mm_struct *mm = vma->vm_mm;
  473. pgoff_t pgoff;
  474. int nr_pages;
  475. int ret = 0;
  476. int lock = !!(newflags & VM_LOCKED);
  477. vm_flags_t old_flags = vma->vm_flags;
  478. if (newflags == vma->vm_flags || (vma->vm_flags & VM_SPECIAL) ||
  479. is_vm_hugetlb_page(vma) || vma == get_gate_vma(current->mm) ||
  480. vma_is_dax(vma))
  481. /* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */
  482. goto out;
  483. pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  484. *prev = vma_merge(mm, *prev, start, end, newflags, vma->anon_vma,
  485. vma->vm_file, pgoff, vma_policy(vma),
  486. vma->vm_userfaultfd_ctx);
  487. if (*prev) {
  488. vma = *prev;
  489. goto success;
  490. }
  491. if (start != vma->vm_start) {
  492. ret = split_vma(mm, vma, start, 1);
  493. if (ret)
  494. goto out;
  495. }
  496. if (end != vma->vm_end) {
  497. ret = split_vma(mm, vma, end, 0);
  498. if (ret)
  499. goto out;
  500. }
  501. success:
  502. /*
  503. * Keep track of amount of locked VM.
  504. */
  505. nr_pages = (end - start) >> PAGE_SHIFT;
  506. if (!lock)
  507. nr_pages = -nr_pages;
  508. else if (old_flags & VM_LOCKED)
  509. nr_pages = 0;
  510. mm->locked_vm += nr_pages;
  511. /*
  512. * vm_flags is protected by the mmap_sem held in write mode.
  513. * It's okay if try_to_unmap_one unmaps a page just after we
  514. * set VM_LOCKED, populate_vma_page_range will bring it back.
  515. */
  516. if (lock)
  517. vma->vm_flags = newflags;
  518. else
  519. munlock_vma_pages_range(vma, start, end);
  520. out:
  521. *prev = vma;
  522. return ret;
  523. }
  524. static int apply_vma_lock_flags(unsigned long start, size_t len,
  525. vm_flags_t flags)
  526. {
  527. unsigned long nstart, end, tmp;
  528. struct vm_area_struct * vma, * prev;
  529. int error;
  530. VM_BUG_ON(offset_in_page(start));
  531. VM_BUG_ON(len != PAGE_ALIGN(len));
  532. end = start + len;
  533. if (end < start)
  534. return -EINVAL;
  535. if (end == start)
  536. return 0;
  537. vma = find_vma(current->mm, start);
  538. if (!vma || vma->vm_start > start)
  539. return -ENOMEM;
  540. prev = vma->vm_prev;
  541. if (start > vma->vm_start)
  542. prev = vma;
  543. for (nstart = start ; ; ) {
  544. vm_flags_t newflags = vma->vm_flags & VM_LOCKED_CLEAR_MASK;
  545. newflags |= flags;
  546. /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
  547. tmp = vma->vm_end;
  548. if (tmp > end)
  549. tmp = end;
  550. error = mlock_fixup(vma, &prev, nstart, tmp, newflags);
  551. if (error)
  552. break;
  553. nstart = tmp;
  554. if (nstart < prev->vm_end)
  555. nstart = prev->vm_end;
  556. if (nstart >= end)
  557. break;
  558. vma = prev->vm_next;
  559. if (!vma || vma->vm_start != nstart) {
  560. error = -ENOMEM;
  561. break;
  562. }
  563. }
  564. return error;
  565. }
  566. /*
  567. * Go through vma areas and sum size of mlocked
  568. * vma pages, as return value.
  569. * Note deferred memory locking case(mlock2(,,MLOCK_ONFAULT)
  570. * is also counted.
  571. * Return value: previously mlocked page counts
  572. */
  573. static unsigned long count_mm_mlocked_page_nr(struct mm_struct *mm,
  574. unsigned long start, size_t len)
  575. {
  576. struct vm_area_struct *vma;
  577. unsigned long count = 0;
  578. if (mm == NULL)
  579. mm = current->mm;
  580. vma = find_vma(mm, start);
  581. if (vma == NULL)
  582. vma = mm->mmap;
  583. for (; vma ; vma = vma->vm_next) {
  584. if (start >= vma->vm_end)
  585. continue;
  586. if (start + len <= vma->vm_start)
  587. break;
  588. if (vma->vm_flags & VM_LOCKED) {
  589. if (start > vma->vm_start)
  590. count -= (start - vma->vm_start);
  591. if (start + len < vma->vm_end) {
  592. count += start + len - vma->vm_start;
  593. break;
  594. }
  595. count += vma->vm_end - vma->vm_start;
  596. }
  597. }
  598. return count >> PAGE_SHIFT;
  599. }
  600. static __must_check int do_mlock(unsigned long start, size_t len, vm_flags_t flags)
  601. {
  602. unsigned long locked;
  603. unsigned long lock_limit;
  604. int error = -ENOMEM;
  605. if (!can_do_mlock())
  606. return -EPERM;
  607. len = PAGE_ALIGN(len + (offset_in_page(start)));
  608. start &= PAGE_MASK;
  609. lock_limit = rlimit(RLIMIT_MEMLOCK);
  610. lock_limit >>= PAGE_SHIFT;
  611. locked = len >> PAGE_SHIFT;
  612. if (down_write_killable(&current->mm->mmap_sem))
  613. return -EINTR;
  614. locked += current->mm->locked_vm;
  615. if ((locked > lock_limit) && (!capable(CAP_IPC_LOCK))) {
  616. /*
  617. * It is possible that the regions requested intersect with
  618. * previously mlocked areas, that part area in "mm->locked_vm"
  619. * should not be counted to new mlock increment count. So check
  620. * and adjust locked count if necessary.
  621. */
  622. locked -= count_mm_mlocked_page_nr(current->mm,
  623. start, len);
  624. }
  625. /* check against resource limits */
  626. if ((locked <= lock_limit) || capable(CAP_IPC_LOCK))
  627. error = apply_vma_lock_flags(start, len, flags);
  628. up_write(&current->mm->mmap_sem);
  629. if (error)
  630. return error;
  631. error = __mm_populate(start, len, 0);
  632. if (error)
  633. return __mlock_posix_error_return(error);
  634. return 0;
  635. }
  636. SYSCALL_DEFINE2(mlock, unsigned long, start, size_t, len)
  637. {
  638. return do_mlock(start, len, VM_LOCKED);
  639. }
  640. SYSCALL_DEFINE3(mlock2, unsigned long, start, size_t, len, int, flags)
  641. {
  642. vm_flags_t vm_flags = VM_LOCKED;
  643. if (flags & ~MLOCK_ONFAULT)
  644. return -EINVAL;
  645. if (flags & MLOCK_ONFAULT)
  646. vm_flags |= VM_LOCKONFAULT;
  647. return do_mlock(start, len, vm_flags);
  648. }
  649. SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len)
  650. {
  651. int ret;
  652. len = PAGE_ALIGN(len + (offset_in_page(start)));
  653. start &= PAGE_MASK;
  654. if (down_write_killable(&current->mm->mmap_sem))
  655. return -EINTR;
  656. ret = apply_vma_lock_flags(start, len, 0);
  657. up_write(&current->mm->mmap_sem);
  658. return ret;
  659. }
  660. /*
  661. * Take the MCL_* flags passed into mlockall (or 0 if called from munlockall)
  662. * and translate into the appropriate modifications to mm->def_flags and/or the
  663. * flags for all current VMAs.
  664. *
  665. * There are a couple of subtleties with this. If mlockall() is called multiple
  666. * times with different flags, the values do not necessarily stack. If mlockall
  667. * is called once including the MCL_FUTURE flag and then a second time without
  668. * it, VM_LOCKED and VM_LOCKONFAULT will be cleared from mm->def_flags.
  669. */
  670. static int apply_mlockall_flags(int flags)
  671. {
  672. struct vm_area_struct * vma, * prev = NULL;
  673. vm_flags_t to_add = 0;
  674. current->mm->def_flags &= VM_LOCKED_CLEAR_MASK;
  675. if (flags & MCL_FUTURE) {
  676. current->mm->def_flags |= VM_LOCKED;
  677. if (flags & MCL_ONFAULT)
  678. current->mm->def_flags |= VM_LOCKONFAULT;
  679. if (!(flags & MCL_CURRENT))
  680. goto out;
  681. }
  682. if (flags & MCL_CURRENT) {
  683. to_add |= VM_LOCKED;
  684. if (flags & MCL_ONFAULT)
  685. to_add |= VM_LOCKONFAULT;
  686. }
  687. for (vma = current->mm->mmap; vma ; vma = prev->vm_next) {
  688. vm_flags_t newflags;
  689. newflags = vma->vm_flags & VM_LOCKED_CLEAR_MASK;
  690. newflags |= to_add;
  691. /* Ignore errors */
  692. mlock_fixup(vma, &prev, vma->vm_start, vma->vm_end, newflags);
  693. cond_resched();
  694. }
  695. out:
  696. return 0;
  697. }
  698. SYSCALL_DEFINE1(mlockall, int, flags)
  699. {
  700. unsigned long lock_limit;
  701. int ret;
  702. if (!flags || (flags & ~(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT)))
  703. return -EINVAL;
  704. if (!can_do_mlock())
  705. return -EPERM;
  706. lock_limit = rlimit(RLIMIT_MEMLOCK);
  707. lock_limit >>= PAGE_SHIFT;
  708. if (down_write_killable(&current->mm->mmap_sem))
  709. return -EINTR;
  710. ret = -ENOMEM;
  711. if (!(flags & MCL_CURRENT) || (current->mm->total_vm <= lock_limit) ||
  712. capable(CAP_IPC_LOCK))
  713. ret = apply_mlockall_flags(flags);
  714. up_write(&current->mm->mmap_sem);
  715. if (!ret && (flags & MCL_CURRENT))
  716. mm_populate(0, TASK_SIZE);
  717. return ret;
  718. }
  719. SYSCALL_DEFINE0(munlockall)
  720. {
  721. int ret;
  722. if (down_write_killable(&current->mm->mmap_sem))
  723. return -EINTR;
  724. ret = apply_mlockall_flags(0);
  725. up_write(&current->mm->mmap_sem);
  726. return ret;
  727. }
  728. /*
  729. * Objects with different lifetime than processes (SHM_LOCK and SHM_HUGETLB
  730. * shm segments) get accounted against the user_struct instead.
  731. */
  732. static DEFINE_SPINLOCK(shmlock_user_lock);
  733. int user_shm_lock(size_t size, struct user_struct *user)
  734. {
  735. unsigned long lock_limit, locked;
  736. int allowed = 0;
  737. locked = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  738. lock_limit = rlimit(RLIMIT_MEMLOCK);
  739. if (lock_limit == RLIM_INFINITY)
  740. allowed = 1;
  741. lock_limit >>= PAGE_SHIFT;
  742. spin_lock(&shmlock_user_lock);
  743. if (!allowed &&
  744. locked + user->locked_shm > lock_limit && !capable(CAP_IPC_LOCK))
  745. goto out;
  746. get_uid(user);
  747. user->locked_shm += locked;
  748. allowed = 1;
  749. out:
  750. spin_unlock(&shmlock_user_lock);
  751. return allowed;
  752. }
  753. void user_shm_unlock(size_t size, struct user_struct *user)
  754. {
  755. spin_lock(&shmlock_user_lock);
  756. user->locked_shm -= (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  757. spin_unlock(&shmlock_user_lock);
  758. free_uid(user);
  759. }