mlock.c 23 KB

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