mlock.c 21 KB

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