mlock.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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/module.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 (current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur != 0)
  26. return 1;
  27. return 0;
  28. }
  29. EXPORT_SYMBOL(can_do_mlock);
  30. #ifdef CONFIG_UNEVICTABLE_LRU
  31. /*
  32. * Mlocked pages are marked with PageMlocked() flag for efficient testing
  33. * in vmscan and, possibly, the fault path; and to support semi-accurate
  34. * statistics.
  35. *
  36. * An mlocked page [PageMlocked(page)] is unevictable. As such, it will
  37. * be placed on the LRU "unevictable" list, rather than the [in]active lists.
  38. * The unevictable list is an LRU sibling list to the [in]active lists.
  39. * PageUnevictable is set to indicate the unevictable state.
  40. *
  41. * When lazy mlocking via vmscan, it is important to ensure that the
  42. * vma's VM_LOCKED status is not concurrently being modified, otherwise we
  43. * may have mlocked a page that is being munlocked. So lazy mlock must take
  44. * the mmap_sem for read, and verify that the vma really is locked
  45. * (see mm/rmap.c).
  46. */
  47. /*
  48. * LRU accounting for clear_page_mlock()
  49. */
  50. void __clear_page_mlock(struct page *page)
  51. {
  52. VM_BUG_ON(!PageLocked(page));
  53. if (!page->mapping) { /* truncated ? */
  54. return;
  55. }
  56. dec_zone_page_state(page, NR_MLOCK);
  57. count_vm_event(UNEVICTABLE_PGCLEARED);
  58. if (!isolate_lru_page(page)) {
  59. putback_lru_page(page);
  60. } else {
  61. /*
  62. * Page not on the LRU yet. Flush all pagevecs and retry.
  63. */
  64. lru_add_drain_all();
  65. if (!isolate_lru_page(page))
  66. putback_lru_page(page);
  67. else if (PageUnevictable(page))
  68. count_vm_event(UNEVICTABLE_PGSTRANDED);
  69. }
  70. }
  71. /*
  72. * Mark page as mlocked if not already.
  73. * If page on LRU, isolate and putback to move to unevictable list.
  74. */
  75. void mlock_vma_page(struct page *page)
  76. {
  77. BUG_ON(!PageLocked(page));
  78. if (!TestSetPageMlocked(page)) {
  79. inc_zone_page_state(page, NR_MLOCK);
  80. count_vm_event(UNEVICTABLE_PGMLOCKED);
  81. if (!isolate_lru_page(page))
  82. putback_lru_page(page);
  83. }
  84. }
  85. /*
  86. * called from munlock()/munmap() path with page supposedly on the LRU.
  87. *
  88. * Note: unlike mlock_vma_page(), we can't just clear the PageMlocked
  89. * [in try_to_munlock()] and then attempt to isolate the page. We must
  90. * isolate the page to keep others from messing with its unevictable
  91. * and mlocked state while trying to munlock. However, we pre-clear the
  92. * mlocked state anyway as we might lose the isolation race and we might
  93. * not get another chance to clear PageMlocked. If we successfully
  94. * isolate the page and try_to_munlock() detects other VM_LOCKED vmas
  95. * mapping the page, it will restore the PageMlocked state, unless the page
  96. * is mapped in a non-linear vma. So, we go ahead and SetPageMlocked(),
  97. * perhaps redundantly.
  98. * If we lose the isolation race, and the page is mapped by other VM_LOCKED
  99. * vmas, we'll detect this in vmscan--via try_to_munlock() or try_to_unmap()
  100. * either of which will restore the PageMlocked state by calling
  101. * mlock_vma_page() above, if it can grab the vma's mmap sem.
  102. */
  103. static void munlock_vma_page(struct page *page)
  104. {
  105. BUG_ON(!PageLocked(page));
  106. if (TestClearPageMlocked(page)) {
  107. dec_zone_page_state(page, NR_MLOCK);
  108. if (!isolate_lru_page(page)) {
  109. int ret = try_to_munlock(page);
  110. /*
  111. * did try_to_unlock() succeed or punt?
  112. */
  113. if (ret == SWAP_SUCCESS || ret == SWAP_AGAIN)
  114. count_vm_event(UNEVICTABLE_PGMUNLOCKED);
  115. putback_lru_page(page);
  116. } else {
  117. /*
  118. * We lost the race. let try_to_unmap() deal
  119. * with it. At least we get the page state and
  120. * mlock stats right. However, page is still on
  121. * the noreclaim list. We'll fix that up when
  122. * the page is eventually freed or we scan the
  123. * noreclaim list.
  124. */
  125. if (PageUnevictable(page))
  126. count_vm_event(UNEVICTABLE_PGSTRANDED);
  127. else
  128. count_vm_event(UNEVICTABLE_PGMUNLOCKED);
  129. }
  130. }
  131. }
  132. /**
  133. * __mlock_vma_pages_range() - mlock/munlock a range of pages in the vma.
  134. * @vma: target vma
  135. * @start: start address
  136. * @end: end address
  137. * @mlock: 0 indicate munlock, otherwise mlock.
  138. *
  139. * If @mlock == 0, unlock an mlocked range;
  140. * else mlock the range of pages. This takes care of making the pages present ,
  141. * too.
  142. *
  143. * return 0 on success, negative error code on error.
  144. *
  145. * vma->vm_mm->mmap_sem must be held for at least read.
  146. */
  147. static long __mlock_vma_pages_range(struct vm_area_struct *vma,
  148. unsigned long start, unsigned long end,
  149. int mlock)
  150. {
  151. struct mm_struct *mm = vma->vm_mm;
  152. unsigned long addr = start;
  153. struct page *pages[16]; /* 16 gives a reasonable batch */
  154. int nr_pages = (end - start) / PAGE_SIZE;
  155. int ret;
  156. int gup_flags = 0;
  157. VM_BUG_ON(start & ~PAGE_MASK);
  158. VM_BUG_ON(end & ~PAGE_MASK);
  159. VM_BUG_ON(start < vma->vm_start);
  160. VM_BUG_ON(end > vma->vm_end);
  161. VM_BUG_ON((!rwsem_is_locked(&mm->mmap_sem)) &&
  162. (atomic_read(&mm->mm_users) != 0));
  163. /*
  164. * mlock: don't page populate if page has PROT_NONE permission.
  165. * munlock: the pages always do munlock althrough
  166. * its has PROT_NONE permission.
  167. */
  168. if (!mlock)
  169. gup_flags |= GUP_FLAGS_IGNORE_VMA_PERMISSIONS;
  170. if (vma->vm_flags & VM_WRITE)
  171. gup_flags |= GUP_FLAGS_WRITE;
  172. lru_add_drain_all(); /* push cached pages to LRU */
  173. while (nr_pages > 0) {
  174. int i;
  175. cond_resched();
  176. /*
  177. * get_user_pages makes pages present if we are
  178. * setting mlock. and this extra reference count will
  179. * disable migration of this page. However, page may
  180. * still be truncated out from under us.
  181. */
  182. ret = __get_user_pages(current, mm, addr,
  183. min_t(int, nr_pages, ARRAY_SIZE(pages)),
  184. gup_flags, pages, NULL);
  185. /*
  186. * This can happen for, e.g., VM_NONLINEAR regions before
  187. * a page has been allocated and mapped at a given offset,
  188. * or for addresses that map beyond end of a file.
  189. * We'll mlock the the pages if/when they get faulted in.
  190. */
  191. if (ret < 0)
  192. break;
  193. if (ret == 0) {
  194. /*
  195. * We know the vma is there, so the only time
  196. * we cannot get a single page should be an
  197. * error (ret < 0) case.
  198. */
  199. WARN_ON(1);
  200. break;
  201. }
  202. lru_add_drain(); /* push cached pages to LRU */
  203. for (i = 0; i < ret; i++) {
  204. struct page *page = pages[i];
  205. lock_page(page);
  206. /*
  207. * Because we lock page here and migration is blocked
  208. * by the elevated reference, we need only check for
  209. * page truncation (file-cache only).
  210. */
  211. if (page->mapping) {
  212. if (mlock)
  213. mlock_vma_page(page);
  214. else
  215. munlock_vma_page(page);
  216. }
  217. unlock_page(page);
  218. put_page(page); /* ref from get_user_pages() */
  219. /*
  220. * here we assume that get_user_pages() has given us
  221. * a list of virtually contiguous pages.
  222. */
  223. addr += PAGE_SIZE; /* for next get_user_pages() */
  224. nr_pages--;
  225. }
  226. ret = 0;
  227. }
  228. lru_add_drain_all(); /* to update stats */
  229. return ret; /* count entire vma as locked_vm */
  230. }
  231. /*
  232. * convert get_user_pages() return value to posix mlock() error
  233. */
  234. static int __mlock_posix_error_return(long retval)
  235. {
  236. if (retval == -EFAULT)
  237. retval = -ENOMEM;
  238. else if (retval == -ENOMEM)
  239. retval = -EAGAIN;
  240. return retval;
  241. }
  242. #else /* CONFIG_UNEVICTABLE_LRU */
  243. /*
  244. * Just make pages present if VM_LOCKED. No-op if unlocking.
  245. */
  246. static long __mlock_vma_pages_range(struct vm_area_struct *vma,
  247. unsigned long start, unsigned long end,
  248. int mlock)
  249. {
  250. if (mlock && (vma->vm_flags & VM_LOCKED))
  251. return make_pages_present(start, end);
  252. return 0;
  253. }
  254. static inline int __mlock_posix_error_return(long retval)
  255. {
  256. return 0;
  257. }
  258. #endif /* CONFIG_UNEVICTABLE_LRU */
  259. /**
  260. * mlock_vma_pages_range() - mlock pages in specified vma range.
  261. * @vma - the vma containing the specfied address range
  262. * @start - starting address in @vma to mlock
  263. * @end - end address [+1] in @vma to mlock
  264. *
  265. * For mmap()/mremap()/expansion of mlocked vma.
  266. *
  267. * return 0 on success for "normal" vmas.
  268. *
  269. * return number of pages [> 0] to be removed from locked_vm on success
  270. * of "special" vmas.
  271. *
  272. * return negative error if vma spanning @start-@range disappears while
  273. * mmap semaphore is dropped. Unlikely?
  274. */
  275. long mlock_vma_pages_range(struct vm_area_struct *vma,
  276. unsigned long start, unsigned long end)
  277. {
  278. struct mm_struct *mm = vma->vm_mm;
  279. int nr_pages = (end - start) / PAGE_SIZE;
  280. BUG_ON(!(vma->vm_flags & VM_LOCKED));
  281. /*
  282. * filter unlockable vmas
  283. */
  284. if (vma->vm_flags & (VM_IO | VM_PFNMAP))
  285. goto no_mlock;
  286. if (!((vma->vm_flags & (VM_DONTEXPAND | VM_RESERVED)) ||
  287. is_vm_hugetlb_page(vma) ||
  288. vma == get_gate_vma(current))) {
  289. long error;
  290. downgrade_write(&mm->mmap_sem);
  291. error = __mlock_vma_pages_range(vma, start, end, 1);
  292. up_read(&mm->mmap_sem);
  293. /* vma can change or disappear */
  294. down_write(&mm->mmap_sem);
  295. vma = find_vma(mm, start);
  296. /* non-NULL vma must contain @start, but need to check @end */
  297. if (!vma || end > vma->vm_end)
  298. return -ENOMEM;
  299. return 0; /* hide other errors from mmap(), et al */
  300. }
  301. /*
  302. * User mapped kernel pages or huge pages:
  303. * make these pages present to populate the ptes, but
  304. * fall thru' to reset VM_LOCKED--no need to unlock, and
  305. * return nr_pages so these don't get counted against task's
  306. * locked limit. huge pages are already counted against
  307. * locked vm limit.
  308. */
  309. make_pages_present(start, end);
  310. no_mlock:
  311. vma->vm_flags &= ~VM_LOCKED; /* and don't come back! */
  312. return nr_pages; /* error or pages NOT mlocked */
  313. }
  314. /*
  315. * munlock_vma_pages_range() - munlock all pages in the vma range.'
  316. * @vma - vma containing range to be munlock()ed.
  317. * @start - start address in @vma of the range
  318. * @end - end of range in @vma.
  319. *
  320. * For mremap(), munmap() and exit().
  321. *
  322. * Called with @vma VM_LOCKED.
  323. *
  324. * Returns with VM_LOCKED cleared. Callers must be prepared to
  325. * deal with this.
  326. *
  327. * We don't save and restore VM_LOCKED here because pages are
  328. * still on lru. In unmap path, pages might be scanned by reclaim
  329. * and re-mlocked by try_to_{munlock|unmap} before we unmap and
  330. * free them. This will result in freeing mlocked pages.
  331. */
  332. void munlock_vma_pages_range(struct vm_area_struct *vma,
  333. unsigned long start, unsigned long end)
  334. {
  335. vma->vm_flags &= ~VM_LOCKED;
  336. __mlock_vma_pages_range(vma, start, end, 0);
  337. }
  338. /*
  339. * mlock_fixup - handle mlock[all]/munlock[all] requests.
  340. *
  341. * Filters out "special" vmas -- VM_LOCKED never gets set for these, and
  342. * munlock is a no-op. However, for some special vmas, we go ahead and
  343. * populate the ptes via make_pages_present().
  344. *
  345. * For vmas that pass the filters, merge/split as appropriate.
  346. */
  347. static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev,
  348. unsigned long start, unsigned long end, unsigned int newflags)
  349. {
  350. struct mm_struct *mm = vma->vm_mm;
  351. pgoff_t pgoff;
  352. int nr_pages;
  353. int ret = 0;
  354. int lock = newflags & VM_LOCKED;
  355. if (newflags == vma->vm_flags ||
  356. (vma->vm_flags & (VM_IO | VM_PFNMAP)))
  357. goto out; /* don't set VM_LOCKED, don't count */
  358. if ((vma->vm_flags & (VM_DONTEXPAND | VM_RESERVED)) ||
  359. is_vm_hugetlb_page(vma) ||
  360. vma == get_gate_vma(current)) {
  361. if (lock)
  362. make_pages_present(start, end);
  363. goto out; /* don't set VM_LOCKED, don't count */
  364. }
  365. pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  366. *prev = vma_merge(mm, *prev, start, end, newflags, vma->anon_vma,
  367. vma->vm_file, pgoff, vma_policy(vma));
  368. if (*prev) {
  369. vma = *prev;
  370. goto success;
  371. }
  372. if (start != vma->vm_start) {
  373. ret = split_vma(mm, vma, start, 1);
  374. if (ret)
  375. goto out;
  376. }
  377. if (end != vma->vm_end) {
  378. ret = split_vma(mm, vma, end, 0);
  379. if (ret)
  380. goto out;
  381. }
  382. success:
  383. /*
  384. * Keep track of amount of locked VM.
  385. */
  386. nr_pages = (end - start) >> PAGE_SHIFT;
  387. if (!lock)
  388. nr_pages = -nr_pages;
  389. mm->locked_vm += nr_pages;
  390. /*
  391. * vm_flags is protected by the mmap_sem held in write mode.
  392. * It's okay if try_to_unmap_one unmaps a page just after we
  393. * set VM_LOCKED, __mlock_vma_pages_range will bring it back.
  394. */
  395. vma->vm_flags = newflags;
  396. if (lock) {
  397. /*
  398. * mmap_sem is currently held for write. Downgrade the write
  399. * lock to a read lock so that other faults, mmap scans, ...
  400. * while we fault in all pages.
  401. */
  402. downgrade_write(&mm->mmap_sem);
  403. ret = __mlock_vma_pages_range(vma, start, end, 1);
  404. /*
  405. * Need to reacquire mmap sem in write mode, as our callers
  406. * expect this. We have no support for atomically upgrading
  407. * a sem to write, so we need to check for ranges while sem
  408. * is unlocked.
  409. */
  410. up_read(&mm->mmap_sem);
  411. /* vma can change or disappear */
  412. down_write(&mm->mmap_sem);
  413. *prev = find_vma(mm, start);
  414. /* non-NULL *prev must contain @start, but need to check @end */
  415. if (!(*prev) || end > (*prev)->vm_end)
  416. ret = -ENOMEM;
  417. else if (ret > 0) {
  418. mm->locked_vm -= ret;
  419. ret = 0;
  420. } else
  421. ret = __mlock_posix_error_return(ret); /* translate if needed */
  422. } else {
  423. /*
  424. * TODO: for unlocking, pages will already be resident, so
  425. * we don't need to wait for allocations/reclaim/pagein, ...
  426. * However, unlocking a very large region can still take a
  427. * while. Should we downgrade the semaphore for both lock
  428. * AND unlock ?
  429. */
  430. __mlock_vma_pages_range(vma, start, end, 0);
  431. }
  432. out:
  433. *prev = vma;
  434. return ret;
  435. }
  436. static int do_mlock(unsigned long start, size_t len, int on)
  437. {
  438. unsigned long nstart, end, tmp;
  439. struct vm_area_struct * vma, * prev;
  440. int error;
  441. len = PAGE_ALIGN(len);
  442. end = start + len;
  443. if (end < start)
  444. return -EINVAL;
  445. if (end == start)
  446. return 0;
  447. vma = find_vma_prev(current->mm, start, &prev);
  448. if (!vma || vma->vm_start > start)
  449. return -ENOMEM;
  450. if (start > vma->vm_start)
  451. prev = vma;
  452. for (nstart = start ; ; ) {
  453. unsigned int newflags;
  454. /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
  455. newflags = vma->vm_flags | VM_LOCKED;
  456. if (!on)
  457. newflags &= ~VM_LOCKED;
  458. tmp = vma->vm_end;
  459. if (tmp > end)
  460. tmp = end;
  461. error = mlock_fixup(vma, &prev, nstart, tmp, newflags);
  462. if (error)
  463. break;
  464. nstart = tmp;
  465. if (nstart < prev->vm_end)
  466. nstart = prev->vm_end;
  467. if (nstart >= end)
  468. break;
  469. vma = prev->vm_next;
  470. if (!vma || vma->vm_start != nstart) {
  471. error = -ENOMEM;
  472. break;
  473. }
  474. }
  475. return error;
  476. }
  477. asmlinkage long sys_mlock(unsigned long start, size_t len)
  478. {
  479. unsigned long locked;
  480. unsigned long lock_limit;
  481. int error = -ENOMEM;
  482. if (!can_do_mlock())
  483. return -EPERM;
  484. down_write(&current->mm->mmap_sem);
  485. len = PAGE_ALIGN(len + (start & ~PAGE_MASK));
  486. start &= PAGE_MASK;
  487. locked = len >> PAGE_SHIFT;
  488. locked += current->mm->locked_vm;
  489. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
  490. lock_limit >>= PAGE_SHIFT;
  491. /* check against resource limits */
  492. if ((locked <= lock_limit) || capable(CAP_IPC_LOCK))
  493. error = do_mlock(start, len, 1);
  494. up_write(&current->mm->mmap_sem);
  495. return error;
  496. }
  497. asmlinkage long sys_munlock(unsigned long start, size_t len)
  498. {
  499. int ret;
  500. down_write(&current->mm->mmap_sem);
  501. len = PAGE_ALIGN(len + (start & ~PAGE_MASK));
  502. start &= PAGE_MASK;
  503. ret = do_mlock(start, len, 0);
  504. up_write(&current->mm->mmap_sem);
  505. return ret;
  506. }
  507. static int do_mlockall(int flags)
  508. {
  509. struct vm_area_struct * vma, * prev = NULL;
  510. unsigned int def_flags = 0;
  511. if (flags & MCL_FUTURE)
  512. def_flags = VM_LOCKED;
  513. current->mm->def_flags = def_flags;
  514. if (flags == MCL_FUTURE)
  515. goto out;
  516. for (vma = current->mm->mmap; vma ; vma = prev->vm_next) {
  517. unsigned int newflags;
  518. newflags = vma->vm_flags | VM_LOCKED;
  519. if (!(flags & MCL_CURRENT))
  520. newflags &= ~VM_LOCKED;
  521. /* Ignore errors */
  522. mlock_fixup(vma, &prev, vma->vm_start, vma->vm_end, newflags);
  523. }
  524. out:
  525. return 0;
  526. }
  527. asmlinkage long sys_mlockall(int flags)
  528. {
  529. unsigned long lock_limit;
  530. int ret = -EINVAL;
  531. if (!flags || (flags & ~(MCL_CURRENT | MCL_FUTURE)))
  532. goto out;
  533. ret = -EPERM;
  534. if (!can_do_mlock())
  535. goto out;
  536. down_write(&current->mm->mmap_sem);
  537. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
  538. lock_limit >>= PAGE_SHIFT;
  539. ret = -ENOMEM;
  540. if (!(flags & MCL_CURRENT) || (current->mm->total_vm <= lock_limit) ||
  541. capable(CAP_IPC_LOCK))
  542. ret = do_mlockall(flags);
  543. up_write(&current->mm->mmap_sem);
  544. out:
  545. return ret;
  546. }
  547. asmlinkage long sys_munlockall(void)
  548. {
  549. int ret;
  550. down_write(&current->mm->mmap_sem);
  551. ret = do_mlockall(0);
  552. up_write(&current->mm->mmap_sem);
  553. return ret;
  554. }
  555. /*
  556. * Objects with different lifetime than processes (SHM_LOCK and SHM_HUGETLB
  557. * shm segments) get accounted against the user_struct instead.
  558. */
  559. static DEFINE_SPINLOCK(shmlock_user_lock);
  560. int user_shm_lock(size_t size, struct user_struct *user)
  561. {
  562. unsigned long lock_limit, locked;
  563. int allowed = 0;
  564. locked = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  565. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
  566. if (lock_limit == RLIM_INFINITY)
  567. allowed = 1;
  568. lock_limit >>= PAGE_SHIFT;
  569. spin_lock(&shmlock_user_lock);
  570. if (!allowed &&
  571. locked + user->locked_shm > lock_limit && !capable(CAP_IPC_LOCK))
  572. goto out;
  573. get_uid(user);
  574. user->locked_shm += locked;
  575. allowed = 1;
  576. out:
  577. spin_unlock(&shmlock_user_lock);
  578. return allowed;
  579. }
  580. void user_shm_unlock(size_t size, struct user_struct *user)
  581. {
  582. spin_lock(&shmlock_user_lock);
  583. user->locked_shm -= (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  584. spin_unlock(&shmlock_user_lock);
  585. free_uid(user);
  586. }