mlock.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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/mempolicy.h>
  14. #include <linux/syscalls.h>
  15. #include <linux/sched.h>
  16. #include <linux/export.h>
  17. #include <linux/rmap.h>
  18. #include <linux/mmzone.h>
  19. #include <linux/hugetlb.h>
  20. #include "internal.h"
  21. int can_do_mlock(void)
  22. {
  23. if (capable(CAP_IPC_LOCK))
  24. return 1;
  25. if (rlimit(RLIMIT_MEMLOCK) != 0)
  26. return 1;
  27. return 0;
  28. }
  29. EXPORT_SYMBOL(can_do_mlock);
  30. /*
  31. * Mlocked pages are marked with PageMlocked() flag for efficient testing
  32. * in vmscan and, possibly, the fault path; and to support semi-accurate
  33. * statistics.
  34. *
  35. * An mlocked page [PageMlocked(page)] is unevictable. As such, it will
  36. * be placed on the LRU "unevictable" list, rather than the [in]active lists.
  37. * The unevictable list is an LRU sibling list to the [in]active lists.
  38. * PageUnevictable is set to indicate the unevictable state.
  39. *
  40. * When lazy mlocking via vmscan, it is important to ensure that the
  41. * vma's VM_LOCKED status is not concurrently being modified, otherwise we
  42. * may have mlocked a page that is being munlocked. So lazy mlock must take
  43. * the mmap_sem for read, and verify that the vma really is locked
  44. * (see mm/rmap.c).
  45. */
  46. /*
  47. * LRU accounting for clear_page_mlock()
  48. */
  49. void clear_page_mlock(struct page *page)
  50. {
  51. if (!TestClearPageMlocked(page))
  52. return;
  53. dec_zone_page_state(page, NR_MLOCK);
  54. count_vm_event(UNEVICTABLE_PGCLEARED);
  55. if (!isolate_lru_page(page)) {
  56. putback_lru_page(page);
  57. } else {
  58. /*
  59. * We lost the race. the page already moved to evictable list.
  60. */
  61. if (PageUnevictable(page))
  62. count_vm_event(UNEVICTABLE_PGSTRANDED);
  63. }
  64. }
  65. /*
  66. * Mark page as mlocked if not already.
  67. * If page on LRU, isolate and putback to move to unevictable list.
  68. */
  69. void mlock_vma_page(struct page *page)
  70. {
  71. BUG_ON(!PageLocked(page));
  72. if (!TestSetPageMlocked(page)) {
  73. inc_zone_page_state(page, NR_MLOCK);
  74. count_vm_event(UNEVICTABLE_PGMLOCKED);
  75. if (!isolate_lru_page(page))
  76. putback_lru_page(page);
  77. }
  78. }
  79. /**
  80. * munlock_vma_page - munlock a vma page
  81. * @page - page to be unlocked
  82. *
  83. * called from munlock()/munmap() path with page supposedly on the LRU.
  84. * When we munlock a page, because the vma where we found the page is being
  85. * munlock()ed or munmap()ed, we want to check whether other vmas hold the
  86. * page locked so that we can leave it on the unevictable lru list and not
  87. * bother vmscan with it. However, to walk the page's rmap list in
  88. * try_to_munlock() we must isolate the page from the LRU. If some other
  89. * task has removed the page from the LRU, we won't be able to do that.
  90. * So we clear the PageMlocked as we might not get another chance. If we
  91. * can't isolate the page, we leave it for putback_lru_page() and vmscan
  92. * [page_referenced()/try_to_unmap()] to deal with.
  93. */
  94. void munlock_vma_page(struct page *page)
  95. {
  96. BUG_ON(!PageLocked(page));
  97. if (TestClearPageMlocked(page)) {
  98. dec_zone_page_state(page, NR_MLOCK);
  99. if (!isolate_lru_page(page)) {
  100. int ret = SWAP_AGAIN;
  101. /*
  102. * Optimization: if the page was mapped just once,
  103. * that's our mapping and we don't need to check all the
  104. * other vmas.
  105. */
  106. if (page_mapcount(page) > 1)
  107. ret = try_to_munlock(page);
  108. /*
  109. * did try_to_unlock() succeed or punt?
  110. */
  111. if (ret != SWAP_MLOCK)
  112. count_vm_event(UNEVICTABLE_PGMUNLOCKED);
  113. putback_lru_page(page);
  114. } else {
  115. /*
  116. * Some other task has removed the page from the LRU.
  117. * putback_lru_page() will take care of removing the
  118. * page from the unevictable list, if necessary.
  119. * vmscan [page_referenced()] will move the page back
  120. * to the unevictable list if some other vma has it
  121. * mlocked.
  122. */
  123. if (PageUnevictable(page))
  124. count_vm_event(UNEVICTABLE_PGSTRANDED);
  125. else
  126. count_vm_event(UNEVICTABLE_PGMUNLOCKED);
  127. }
  128. }
  129. }
  130. /**
  131. * __mlock_vma_pages_range() - mlock a range of pages in the vma.
  132. * @vma: target vma
  133. * @start: start address
  134. * @end: end address
  135. *
  136. * This takes care of making the pages present too.
  137. *
  138. * return 0 on success, negative error code on error.
  139. *
  140. * vma->vm_mm->mmap_sem must be held for at least read.
  141. */
  142. static long __mlock_vma_pages_range(struct vm_area_struct *vma,
  143. unsigned long start, unsigned long end,
  144. int *nonblocking)
  145. {
  146. struct mm_struct *mm = vma->vm_mm;
  147. unsigned long addr = start;
  148. int nr_pages = (end - start) / PAGE_SIZE;
  149. int gup_flags;
  150. VM_BUG_ON(start & ~PAGE_MASK);
  151. VM_BUG_ON(end & ~PAGE_MASK);
  152. VM_BUG_ON(start < vma->vm_start);
  153. VM_BUG_ON(end > vma->vm_end);
  154. VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
  155. gup_flags = FOLL_TOUCH | FOLL_MLOCK;
  156. /*
  157. * We want to touch writable mappings with a write fault in order
  158. * to break COW, except for shared mappings because these don't COW
  159. * and we would not want to dirty them for nothing.
  160. */
  161. if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
  162. gup_flags |= FOLL_WRITE;
  163. /*
  164. * We want mlock to succeed for regions that have any permissions
  165. * other than PROT_NONE.
  166. */
  167. if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC))
  168. gup_flags |= FOLL_FORCE;
  169. return __get_user_pages(current, mm, addr, nr_pages, gup_flags,
  170. NULL, NULL, nonblocking);
  171. }
  172. /*
  173. * convert get_user_pages() return value to posix mlock() error
  174. */
  175. static int __mlock_posix_error_return(long retval)
  176. {
  177. if (retval == -EFAULT)
  178. retval = -ENOMEM;
  179. else if (retval == -ENOMEM)
  180. retval = -EAGAIN;
  181. return retval;
  182. }
  183. /**
  184. * mlock_vma_pages_range() - mlock pages in specified vma range.
  185. * @vma - the vma containing the specfied address range
  186. * @start - starting address in @vma to mlock
  187. * @end - end address [+1] in @vma to mlock
  188. *
  189. * For mmap()/mremap()/expansion of mlocked vma.
  190. *
  191. * return 0 on success for "normal" vmas.
  192. *
  193. * return number of pages [> 0] to be removed from locked_vm on success
  194. * of "special" vmas.
  195. */
  196. long mlock_vma_pages_range(struct vm_area_struct *vma,
  197. unsigned long start, unsigned long end)
  198. {
  199. int nr_pages = (end - start) / PAGE_SIZE;
  200. BUG_ON(!(vma->vm_flags & VM_LOCKED));
  201. /*
  202. * filter unlockable vmas
  203. */
  204. if (vma->vm_flags & (VM_IO | VM_PFNMAP))
  205. goto no_mlock;
  206. if (!((vma->vm_flags & VM_DONTEXPAND) ||
  207. is_vm_hugetlb_page(vma) ||
  208. vma == get_gate_vma(current->mm))) {
  209. __mlock_vma_pages_range(vma, start, end, NULL);
  210. /* Hide errors from mmap() and other callers */
  211. return 0;
  212. }
  213. /*
  214. * User mapped kernel pages or huge pages:
  215. * make these pages present to populate the ptes, but
  216. * fall thru' to reset VM_LOCKED--no need to unlock, and
  217. * return nr_pages so these don't get counted against task's
  218. * locked limit. huge pages are already counted against
  219. * locked vm limit.
  220. */
  221. make_pages_present(start, end);
  222. no_mlock:
  223. vma->vm_flags &= ~VM_LOCKED; /* and don't come back! */
  224. return nr_pages; /* error or pages NOT mlocked */
  225. }
  226. /*
  227. * munlock_vma_pages_range() - munlock all pages in the vma range.'
  228. * @vma - vma containing range to be munlock()ed.
  229. * @start - start address in @vma of the range
  230. * @end - end of range in @vma.
  231. *
  232. * For mremap(), munmap() and exit().
  233. *
  234. * Called with @vma VM_LOCKED.
  235. *
  236. * Returns with VM_LOCKED cleared. Callers must be prepared to
  237. * deal with this.
  238. *
  239. * We don't save and restore VM_LOCKED here because pages are
  240. * still on lru. In unmap path, pages might be scanned by reclaim
  241. * and re-mlocked by try_to_{munlock|unmap} before we unmap and
  242. * free them. This will result in freeing mlocked pages.
  243. */
  244. void munlock_vma_pages_range(struct vm_area_struct *vma,
  245. unsigned long start, unsigned long end)
  246. {
  247. unsigned long addr;
  248. lru_add_drain();
  249. vma->vm_flags &= ~VM_LOCKED;
  250. for (addr = start; addr < end; addr += PAGE_SIZE) {
  251. struct page *page;
  252. /*
  253. * Although FOLL_DUMP is intended for get_dump_page(),
  254. * it just so happens that its special treatment of the
  255. * ZERO_PAGE (returning an error instead of doing get_page)
  256. * suits munlock very well (and if somehow an abnormal page
  257. * has sneaked into the range, we won't oops here: great).
  258. */
  259. page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP);
  260. if (page && !IS_ERR(page)) {
  261. lock_page(page);
  262. munlock_vma_page(page);
  263. unlock_page(page);
  264. put_page(page);
  265. }
  266. cond_resched();
  267. }
  268. }
  269. /*
  270. * mlock_fixup - handle mlock[all]/munlock[all] requests.
  271. *
  272. * Filters out "special" vmas -- VM_LOCKED never gets set for these, and
  273. * munlock is a no-op. However, for some special vmas, we go ahead and
  274. * populate the ptes via make_pages_present().
  275. *
  276. * For vmas that pass the filters, merge/split as appropriate.
  277. */
  278. static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev,
  279. unsigned long start, unsigned long end, vm_flags_t newflags)
  280. {
  281. struct mm_struct *mm = vma->vm_mm;
  282. pgoff_t pgoff;
  283. int nr_pages;
  284. int ret = 0;
  285. int lock = !!(newflags & VM_LOCKED);
  286. if (newflags == vma->vm_flags || (vma->vm_flags & VM_SPECIAL) ||
  287. is_vm_hugetlb_page(vma) || vma == get_gate_vma(current->mm))
  288. goto out; /* don't set VM_LOCKED, don't count */
  289. pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  290. *prev = vma_merge(mm, *prev, start, end, newflags, vma->anon_vma,
  291. vma->vm_file, pgoff, vma_policy(vma));
  292. if (*prev) {
  293. vma = *prev;
  294. goto success;
  295. }
  296. if (start != vma->vm_start) {
  297. ret = split_vma(mm, vma, start, 1);
  298. if (ret)
  299. goto out;
  300. }
  301. if (end != vma->vm_end) {
  302. ret = split_vma(mm, vma, end, 0);
  303. if (ret)
  304. goto out;
  305. }
  306. success:
  307. /*
  308. * Keep track of amount of locked VM.
  309. */
  310. nr_pages = (end - start) >> PAGE_SHIFT;
  311. if (!lock)
  312. nr_pages = -nr_pages;
  313. mm->locked_vm += nr_pages;
  314. /*
  315. * vm_flags is protected by the mmap_sem held in write mode.
  316. * It's okay if try_to_unmap_one unmaps a page just after we
  317. * set VM_LOCKED, __mlock_vma_pages_range will bring it back.
  318. */
  319. if (lock)
  320. vma->vm_flags = newflags;
  321. else
  322. munlock_vma_pages_range(vma, start, end);
  323. out:
  324. *prev = vma;
  325. return ret;
  326. }
  327. static int do_mlock(unsigned long start, size_t len, int on)
  328. {
  329. unsigned long nstart, end, tmp;
  330. struct vm_area_struct * vma, * prev;
  331. int error;
  332. VM_BUG_ON(start & ~PAGE_MASK);
  333. VM_BUG_ON(len != PAGE_ALIGN(len));
  334. end = start + len;
  335. if (end < start)
  336. return -EINVAL;
  337. if (end == start)
  338. return 0;
  339. vma = find_vma(current->mm, start);
  340. if (!vma || vma->vm_start > start)
  341. return -ENOMEM;
  342. prev = vma->vm_prev;
  343. if (start > vma->vm_start)
  344. prev = vma;
  345. for (nstart = start ; ; ) {
  346. vm_flags_t newflags;
  347. /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
  348. newflags = vma->vm_flags | VM_LOCKED;
  349. if (!on)
  350. newflags &= ~VM_LOCKED;
  351. tmp = vma->vm_end;
  352. if (tmp > end)
  353. tmp = end;
  354. error = mlock_fixup(vma, &prev, nstart, tmp, newflags);
  355. if (error)
  356. break;
  357. nstart = tmp;
  358. if (nstart < prev->vm_end)
  359. nstart = prev->vm_end;
  360. if (nstart >= end)
  361. break;
  362. vma = prev->vm_next;
  363. if (!vma || vma->vm_start != nstart) {
  364. error = -ENOMEM;
  365. break;
  366. }
  367. }
  368. return error;
  369. }
  370. static int do_mlock_pages(unsigned long start, size_t len, int ignore_errors)
  371. {
  372. struct mm_struct *mm = current->mm;
  373. unsigned long end, nstart, nend;
  374. struct vm_area_struct *vma = NULL;
  375. int locked = 0;
  376. int ret = 0;
  377. VM_BUG_ON(start & ~PAGE_MASK);
  378. VM_BUG_ON(len != PAGE_ALIGN(len));
  379. end = start + len;
  380. for (nstart = start; nstart < end; nstart = nend) {
  381. /*
  382. * We want to fault in pages for [nstart; end) address range.
  383. * Find first corresponding VMA.
  384. */
  385. if (!locked) {
  386. locked = 1;
  387. down_read(&mm->mmap_sem);
  388. vma = find_vma(mm, nstart);
  389. } else if (nstart >= vma->vm_end)
  390. vma = vma->vm_next;
  391. if (!vma || vma->vm_start >= end)
  392. break;
  393. /*
  394. * Set [nstart; nend) to intersection of desired address
  395. * range with the first VMA. Also, skip undesirable VMA types.
  396. */
  397. nend = min(end, vma->vm_end);
  398. if (vma->vm_flags & (VM_IO | VM_PFNMAP))
  399. continue;
  400. if (nstart < vma->vm_start)
  401. nstart = vma->vm_start;
  402. /*
  403. * Now fault in a range of pages. __mlock_vma_pages_range()
  404. * double checks the vma flags, so that it won't mlock pages
  405. * if the vma was already munlocked.
  406. */
  407. ret = __mlock_vma_pages_range(vma, nstart, nend, &locked);
  408. if (ret < 0) {
  409. if (ignore_errors) {
  410. ret = 0;
  411. continue; /* continue at next VMA */
  412. }
  413. ret = __mlock_posix_error_return(ret);
  414. break;
  415. }
  416. nend = nstart + ret * PAGE_SIZE;
  417. ret = 0;
  418. }
  419. if (locked)
  420. up_read(&mm->mmap_sem);
  421. return ret; /* 0 or negative error code */
  422. }
  423. SYSCALL_DEFINE2(mlock, unsigned long, start, size_t, len)
  424. {
  425. unsigned long locked;
  426. unsigned long lock_limit;
  427. int error = -ENOMEM;
  428. if (!can_do_mlock())
  429. return -EPERM;
  430. lru_add_drain_all(); /* flush pagevec */
  431. down_write(&current->mm->mmap_sem);
  432. len = PAGE_ALIGN(len + (start & ~PAGE_MASK));
  433. start &= PAGE_MASK;
  434. locked = len >> PAGE_SHIFT;
  435. locked += current->mm->locked_vm;
  436. lock_limit = rlimit(RLIMIT_MEMLOCK);
  437. lock_limit >>= PAGE_SHIFT;
  438. /* check against resource limits */
  439. if ((locked <= lock_limit) || capable(CAP_IPC_LOCK))
  440. error = do_mlock(start, len, 1);
  441. up_write(&current->mm->mmap_sem);
  442. if (!error)
  443. error = do_mlock_pages(start, len, 0);
  444. return error;
  445. }
  446. SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len)
  447. {
  448. int ret;
  449. down_write(&current->mm->mmap_sem);
  450. len = PAGE_ALIGN(len + (start & ~PAGE_MASK));
  451. start &= PAGE_MASK;
  452. ret = do_mlock(start, len, 0);
  453. up_write(&current->mm->mmap_sem);
  454. return ret;
  455. }
  456. static int do_mlockall(int flags)
  457. {
  458. struct vm_area_struct * vma, * prev = NULL;
  459. unsigned int def_flags = 0;
  460. if (flags & MCL_FUTURE)
  461. def_flags = VM_LOCKED;
  462. current->mm->def_flags = def_flags;
  463. if (flags == MCL_FUTURE)
  464. goto out;
  465. for (vma = current->mm->mmap; vma ; vma = prev->vm_next) {
  466. vm_flags_t newflags;
  467. newflags = vma->vm_flags | VM_LOCKED;
  468. if (!(flags & MCL_CURRENT))
  469. newflags &= ~VM_LOCKED;
  470. /* Ignore errors */
  471. mlock_fixup(vma, &prev, vma->vm_start, vma->vm_end, newflags);
  472. }
  473. out:
  474. return 0;
  475. }
  476. SYSCALL_DEFINE1(mlockall, int, flags)
  477. {
  478. unsigned long lock_limit;
  479. int ret = -EINVAL;
  480. if (!flags || (flags & ~(MCL_CURRENT | MCL_FUTURE)))
  481. goto out;
  482. ret = -EPERM;
  483. if (!can_do_mlock())
  484. goto out;
  485. if (flags & MCL_CURRENT)
  486. lru_add_drain_all(); /* flush pagevec */
  487. down_write(&current->mm->mmap_sem);
  488. lock_limit = rlimit(RLIMIT_MEMLOCK);
  489. lock_limit >>= PAGE_SHIFT;
  490. ret = -ENOMEM;
  491. if (!(flags & MCL_CURRENT) || (current->mm->total_vm <= lock_limit) ||
  492. capable(CAP_IPC_LOCK))
  493. ret = do_mlockall(flags);
  494. up_write(&current->mm->mmap_sem);
  495. if (!ret && (flags & MCL_CURRENT)) {
  496. /* Ignore errors */
  497. do_mlock_pages(0, TASK_SIZE, 1);
  498. }
  499. out:
  500. return ret;
  501. }
  502. SYSCALL_DEFINE0(munlockall)
  503. {
  504. int ret;
  505. down_write(&current->mm->mmap_sem);
  506. ret = do_mlockall(0);
  507. up_write(&current->mm->mmap_sem);
  508. return ret;
  509. }
  510. /*
  511. * Objects with different lifetime than processes (SHM_LOCK and SHM_HUGETLB
  512. * shm segments) get accounted against the user_struct instead.
  513. */
  514. static DEFINE_SPINLOCK(shmlock_user_lock);
  515. int user_shm_lock(size_t size, struct user_struct *user)
  516. {
  517. unsigned long lock_limit, locked;
  518. int allowed = 0;
  519. locked = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  520. lock_limit = rlimit(RLIMIT_MEMLOCK);
  521. if (lock_limit == RLIM_INFINITY)
  522. allowed = 1;
  523. lock_limit >>= PAGE_SHIFT;
  524. spin_lock(&shmlock_user_lock);
  525. if (!allowed &&
  526. locked + user->locked_shm > lock_limit && !capable(CAP_IPC_LOCK))
  527. goto out;
  528. get_uid(user);
  529. user->locked_shm += locked;
  530. allowed = 1;
  531. out:
  532. spin_unlock(&shmlock_user_lock);
  533. return allowed;
  534. }
  535. void user_shm_unlock(size_t size, struct user_struct *user)
  536. {
  537. spin_lock(&shmlock_user_lock);
  538. user->locked_shm -= (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  539. spin_unlock(&shmlock_user_lock);
  540. free_uid(user);
  541. }