mlock.c 23 KB

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