mlock.c 23 KB

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