mprotect.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * mm/mprotect.c
  4. *
  5. * (C) Copyright 1994 Linus Torvalds
  6. * (C) Copyright 2002 Christoph Hellwig
  7. *
  8. * Address space accounting code <alan@lxorguk.ukuu.org.uk>
  9. * (C) Copyright 2002 Red Hat Inc, All Rights Reserved
  10. */
  11. #include <linux/mm.h>
  12. #include <linux/hugetlb.h>
  13. #include <linux/shm.h>
  14. #include <linux/mman.h>
  15. #include <linux/fs.h>
  16. #include <linux/highmem.h>
  17. #include <linux/security.h>
  18. #include <linux/mempolicy.h>
  19. #include <linux/personality.h>
  20. #include <linux/syscalls.h>
  21. #include <linux/swap.h>
  22. #include <linux/swapops.h>
  23. #include <linux/mmu_notifier.h>
  24. #include <linux/migrate.h>
  25. #include <linux/perf_event.h>
  26. #include <linux/pkeys.h>
  27. #include <linux/ksm.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/mm_inline.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/cacheflush.h>
  32. #include <asm/mmu_context.h>
  33. #include <asm/tlbflush.h>
  34. #include "internal.h"
  35. static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
  36. unsigned long addr, unsigned long end, pgprot_t newprot,
  37. int dirty_accountable, int prot_numa)
  38. {
  39. struct mm_struct *mm = vma->vm_mm;
  40. pte_t *pte, oldpte;
  41. spinlock_t *ptl;
  42. unsigned long pages = 0;
  43. int target_node = NUMA_NO_NODE;
  44. /*
  45. * Can be called with only the mmap_sem for reading by
  46. * prot_numa so we must check the pmd isn't constantly
  47. * changing from under us from pmd_none to pmd_trans_huge
  48. * and/or the other way around.
  49. */
  50. if (pmd_trans_unstable(pmd))
  51. return 0;
  52. /*
  53. * The pmd points to a regular pte so the pmd can't change
  54. * from under us even if the mmap_sem is only hold for
  55. * reading.
  56. */
  57. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  58. /* Get target node for single threaded private VMAs */
  59. if (prot_numa && !(vma->vm_flags & VM_SHARED) &&
  60. atomic_read(&vma->vm_mm->mm_users) == 1)
  61. target_node = numa_node_id();
  62. flush_tlb_batched_pending(vma->vm_mm);
  63. arch_enter_lazy_mmu_mode();
  64. do {
  65. oldpte = *pte;
  66. if (pte_present(oldpte)) {
  67. pte_t ptent;
  68. bool preserve_write = prot_numa && pte_write(oldpte);
  69. /*
  70. * Avoid trapping faults against the zero or KSM
  71. * pages. See similar comment in change_huge_pmd.
  72. */
  73. if (prot_numa) {
  74. struct page *page;
  75. page = vm_normal_page(vma, addr, oldpte);
  76. if (!page || PageKsm(page))
  77. continue;
  78. /* Also skip shared copy-on-write pages */
  79. if (is_cow_mapping(vma->vm_flags) &&
  80. page_mapcount(page) != 1)
  81. continue;
  82. /*
  83. * While migration can move some dirty pages,
  84. * it cannot move them all from MIGRATE_ASYNC
  85. * context.
  86. */
  87. if (page_is_file_cache(page) && PageDirty(page))
  88. continue;
  89. /* Avoid TLB flush if possible */
  90. if (pte_protnone(oldpte))
  91. continue;
  92. /*
  93. * Don't mess with PTEs if page is already on the node
  94. * a single-threaded process is running on.
  95. */
  96. if (target_node == page_to_nid(page))
  97. continue;
  98. }
  99. ptent = ptep_modify_prot_start(mm, addr, pte);
  100. ptent = pte_modify(ptent, newprot);
  101. if (preserve_write)
  102. ptent = pte_mk_savedwrite(ptent);
  103. /* Avoid taking write faults for known dirty pages */
  104. if (dirty_accountable && pte_dirty(ptent) &&
  105. (pte_soft_dirty(ptent) ||
  106. !(vma->vm_flags & VM_SOFTDIRTY))) {
  107. ptent = pte_mkwrite(ptent);
  108. }
  109. ptep_modify_prot_commit(mm, addr, pte, ptent);
  110. pages++;
  111. } else if (IS_ENABLED(CONFIG_MIGRATION)) {
  112. swp_entry_t entry = pte_to_swp_entry(oldpte);
  113. if (is_write_migration_entry(entry)) {
  114. pte_t newpte;
  115. /*
  116. * A protection check is difficult so
  117. * just be safe and disable write
  118. */
  119. make_migration_entry_read(&entry);
  120. newpte = swp_entry_to_pte(entry);
  121. if (pte_swp_soft_dirty(oldpte))
  122. newpte = pte_swp_mksoft_dirty(newpte);
  123. set_pte_at(mm, addr, pte, newpte);
  124. pages++;
  125. }
  126. if (is_write_device_private_entry(entry)) {
  127. pte_t newpte;
  128. /*
  129. * We do not preserve soft-dirtiness. See
  130. * copy_one_pte() for explanation.
  131. */
  132. make_device_private_entry_read(&entry);
  133. newpte = swp_entry_to_pte(entry);
  134. set_pte_at(mm, addr, pte, newpte);
  135. pages++;
  136. }
  137. }
  138. } while (pte++, addr += PAGE_SIZE, addr != end);
  139. arch_leave_lazy_mmu_mode();
  140. pte_unmap_unlock(pte - 1, ptl);
  141. return pages;
  142. }
  143. /*
  144. * Used when setting automatic NUMA hinting protection where it is
  145. * critical that a numa hinting PMD is not confused with a bad PMD.
  146. */
  147. static inline int pmd_none_or_clear_bad_unless_trans_huge(pmd_t *pmd)
  148. {
  149. pmd_t pmdval = pmd_read_atomic(pmd);
  150. /* See pmd_none_or_trans_huge_or_clear_bad for info on barrier */
  151. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  152. barrier();
  153. #endif
  154. if (pmd_none(pmdval))
  155. return 1;
  156. if (pmd_trans_huge(pmdval))
  157. return 0;
  158. if (unlikely(pmd_bad(pmdval))) {
  159. pmd_clear_bad(pmd);
  160. return 1;
  161. }
  162. return 0;
  163. }
  164. static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
  165. pud_t *pud, unsigned long addr, unsigned long end,
  166. pgprot_t newprot, int dirty_accountable, int prot_numa)
  167. {
  168. pmd_t *pmd;
  169. struct mm_struct *mm = vma->vm_mm;
  170. unsigned long next;
  171. unsigned long pages = 0;
  172. unsigned long nr_huge_updates = 0;
  173. unsigned long mni_start = 0;
  174. pmd = pmd_offset(pud, addr);
  175. do {
  176. unsigned long this_pages;
  177. next = pmd_addr_end(addr, end);
  178. /*
  179. * Automatic NUMA balancing walks the tables with mmap_sem
  180. * held for read. It's possible a parallel update to occur
  181. * between pmd_trans_huge() and a pmd_none_or_clear_bad()
  182. * check leading to a false positive and clearing.
  183. * Hence, it's necessary to atomically read the PMD value
  184. * for all the checks.
  185. */
  186. if (!is_swap_pmd(*pmd) && !pmd_devmap(*pmd) &&
  187. pmd_none_or_clear_bad_unless_trans_huge(pmd))
  188. goto next;
  189. /* invoke the mmu notifier if the pmd is populated */
  190. if (!mni_start) {
  191. mni_start = addr;
  192. mmu_notifier_invalidate_range_start(mm, mni_start, end);
  193. }
  194. if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
  195. if (next - addr != HPAGE_PMD_SIZE) {
  196. __split_huge_pmd(vma, pmd, addr, false, NULL);
  197. } else {
  198. int nr_ptes = change_huge_pmd(vma, pmd, addr,
  199. newprot, prot_numa);
  200. if (nr_ptes) {
  201. if (nr_ptes == HPAGE_PMD_NR) {
  202. pages += HPAGE_PMD_NR;
  203. nr_huge_updates++;
  204. }
  205. /* huge pmd was handled */
  206. goto next;
  207. }
  208. }
  209. /* fall through, the trans huge pmd just split */
  210. }
  211. this_pages = change_pte_range(vma, pmd, addr, next, newprot,
  212. dirty_accountable, prot_numa);
  213. pages += this_pages;
  214. next:
  215. cond_resched();
  216. } while (pmd++, addr = next, addr != end);
  217. if (mni_start)
  218. mmu_notifier_invalidate_range_end(mm, mni_start, end);
  219. if (nr_huge_updates)
  220. count_vm_numa_events(NUMA_HUGE_PTE_UPDATES, nr_huge_updates);
  221. return pages;
  222. }
  223. static inline unsigned long change_pud_range(struct vm_area_struct *vma,
  224. p4d_t *p4d, unsigned long addr, unsigned long end,
  225. pgprot_t newprot, int dirty_accountable, int prot_numa)
  226. {
  227. pud_t *pud;
  228. unsigned long next;
  229. unsigned long pages = 0;
  230. pud = pud_offset(p4d, addr);
  231. do {
  232. next = pud_addr_end(addr, end);
  233. if (pud_none_or_clear_bad(pud))
  234. continue;
  235. pages += change_pmd_range(vma, pud, addr, next, newprot,
  236. dirty_accountable, prot_numa);
  237. } while (pud++, addr = next, addr != end);
  238. return pages;
  239. }
  240. static inline unsigned long change_p4d_range(struct vm_area_struct *vma,
  241. pgd_t *pgd, unsigned long addr, unsigned long end,
  242. pgprot_t newprot, int dirty_accountable, int prot_numa)
  243. {
  244. p4d_t *p4d;
  245. unsigned long next;
  246. unsigned long pages = 0;
  247. p4d = p4d_offset(pgd, addr);
  248. do {
  249. next = p4d_addr_end(addr, end);
  250. if (p4d_none_or_clear_bad(p4d))
  251. continue;
  252. pages += change_pud_range(vma, p4d, addr, next, newprot,
  253. dirty_accountable, prot_numa);
  254. } while (p4d++, addr = next, addr != end);
  255. return pages;
  256. }
  257. static unsigned long change_protection_range(struct vm_area_struct *vma,
  258. unsigned long addr, unsigned long end, pgprot_t newprot,
  259. int dirty_accountable, int prot_numa)
  260. {
  261. struct mm_struct *mm = vma->vm_mm;
  262. pgd_t *pgd;
  263. unsigned long next;
  264. unsigned long start = addr;
  265. unsigned long pages = 0;
  266. BUG_ON(addr >= end);
  267. pgd = pgd_offset(mm, addr);
  268. flush_cache_range(vma, addr, end);
  269. inc_tlb_flush_pending(mm);
  270. do {
  271. next = pgd_addr_end(addr, end);
  272. if (pgd_none_or_clear_bad(pgd))
  273. continue;
  274. pages += change_p4d_range(vma, pgd, addr, next, newprot,
  275. dirty_accountable, prot_numa);
  276. } while (pgd++, addr = next, addr != end);
  277. /* Only flush the TLB if we actually modified any entries: */
  278. if (pages)
  279. flush_tlb_range(vma, start, end);
  280. dec_tlb_flush_pending(mm);
  281. return pages;
  282. }
  283. unsigned long change_protection(struct vm_area_struct *vma, unsigned long start,
  284. unsigned long end, pgprot_t newprot,
  285. int dirty_accountable, int prot_numa)
  286. {
  287. unsigned long pages;
  288. if (is_vm_hugetlb_page(vma))
  289. pages = hugetlb_change_protection(vma, start, end, newprot);
  290. else
  291. pages = change_protection_range(vma, start, end, newprot, dirty_accountable, prot_numa);
  292. return pages;
  293. }
  294. static int prot_none_pte_entry(pte_t *pte, unsigned long addr,
  295. unsigned long next, struct mm_walk *walk)
  296. {
  297. return pfn_modify_allowed(pte_pfn(*pte), *(pgprot_t *)(walk->private)) ?
  298. 0 : -EACCES;
  299. }
  300. static int prot_none_hugetlb_entry(pte_t *pte, unsigned long hmask,
  301. unsigned long addr, unsigned long next,
  302. struct mm_walk *walk)
  303. {
  304. return pfn_modify_allowed(pte_pfn(*pte), *(pgprot_t *)(walk->private)) ?
  305. 0 : -EACCES;
  306. }
  307. static int prot_none_test(unsigned long addr, unsigned long next,
  308. struct mm_walk *walk)
  309. {
  310. return 0;
  311. }
  312. static int prot_none_walk(struct vm_area_struct *vma, unsigned long start,
  313. unsigned long end, unsigned long newflags)
  314. {
  315. pgprot_t new_pgprot = vm_get_page_prot(newflags);
  316. struct mm_walk prot_none_walk = {
  317. .pte_entry = prot_none_pte_entry,
  318. .hugetlb_entry = prot_none_hugetlb_entry,
  319. .test_walk = prot_none_test,
  320. .mm = current->mm,
  321. .private = &new_pgprot,
  322. };
  323. return walk_page_range(start, end, &prot_none_walk);
  324. }
  325. int
  326. mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
  327. unsigned long start, unsigned long end, unsigned long newflags)
  328. {
  329. struct mm_struct *mm = vma->vm_mm;
  330. unsigned long oldflags = vma->vm_flags;
  331. long nrpages = (end - start) >> PAGE_SHIFT;
  332. unsigned long charged = 0;
  333. pgoff_t pgoff;
  334. int error;
  335. int dirty_accountable = 0;
  336. if (newflags == oldflags) {
  337. *pprev = vma;
  338. return 0;
  339. }
  340. /*
  341. * Do PROT_NONE PFN permission checks here when we can still
  342. * bail out without undoing a lot of state. This is a rather
  343. * uncommon case, so doesn't need to be very optimized.
  344. */
  345. if (arch_has_pfn_modify_check() &&
  346. (vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) &&
  347. (newflags & (VM_READ|VM_WRITE|VM_EXEC)) == 0) {
  348. error = prot_none_walk(vma, start, end, newflags);
  349. if (error)
  350. return error;
  351. }
  352. /*
  353. * If we make a private mapping writable we increase our commit;
  354. * but (without finer accounting) cannot reduce our commit if we
  355. * make it unwritable again. hugetlb mapping were accounted for
  356. * even if read-only so there is no need to account for them here
  357. */
  358. if (newflags & VM_WRITE) {
  359. /* Check space limits when area turns into data. */
  360. if (!may_expand_vm(mm, newflags, nrpages) &&
  361. may_expand_vm(mm, oldflags, nrpages))
  362. return -ENOMEM;
  363. if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_HUGETLB|
  364. VM_SHARED|VM_NORESERVE))) {
  365. charged = nrpages;
  366. if (security_vm_enough_memory_mm(mm, charged))
  367. return -ENOMEM;
  368. newflags |= VM_ACCOUNT;
  369. }
  370. }
  371. /*
  372. * First try to merge with previous and/or next vma.
  373. */
  374. pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  375. *pprev = vma_merge(mm, *pprev, start, end, newflags,
  376. vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
  377. vma->vm_userfaultfd_ctx);
  378. if (*pprev) {
  379. vma = *pprev;
  380. VM_WARN_ON((vma->vm_flags ^ newflags) & ~VM_SOFTDIRTY);
  381. goto success;
  382. }
  383. *pprev = vma;
  384. if (start != vma->vm_start) {
  385. error = split_vma(mm, vma, start, 1);
  386. if (error)
  387. goto fail;
  388. }
  389. if (end != vma->vm_end) {
  390. error = split_vma(mm, vma, end, 0);
  391. if (error)
  392. goto fail;
  393. }
  394. success:
  395. /*
  396. * vm_flags and vm_page_prot are protected by the mmap_sem
  397. * held in write mode.
  398. */
  399. vma->vm_flags = newflags;
  400. dirty_accountable = vma_wants_writenotify(vma, vma->vm_page_prot);
  401. vma_set_page_prot(vma);
  402. change_protection(vma, start, end, vma->vm_page_prot,
  403. dirty_accountable, 0);
  404. /*
  405. * Private VM_LOCKED VMA becoming writable: trigger COW to avoid major
  406. * fault on access.
  407. */
  408. if ((oldflags & (VM_WRITE | VM_SHARED | VM_LOCKED)) == VM_LOCKED &&
  409. (newflags & VM_WRITE)) {
  410. populate_vma_page_range(vma, start, end, NULL);
  411. }
  412. vm_stat_account(mm, oldflags, -nrpages);
  413. vm_stat_account(mm, newflags, nrpages);
  414. perf_event_mmap(vma);
  415. return 0;
  416. fail:
  417. vm_unacct_memory(charged);
  418. return error;
  419. }
  420. /*
  421. * pkey==-1 when doing a legacy mprotect()
  422. */
  423. static int do_mprotect_pkey(unsigned long start, size_t len,
  424. unsigned long prot, int pkey)
  425. {
  426. unsigned long nstart, end, tmp, reqprot;
  427. struct vm_area_struct *vma, *prev;
  428. int error = -EINVAL;
  429. const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
  430. const bool rier = (current->personality & READ_IMPLIES_EXEC) &&
  431. (prot & PROT_READ);
  432. prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
  433. if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
  434. return -EINVAL;
  435. if (start & ~PAGE_MASK)
  436. return -EINVAL;
  437. if (!len)
  438. return 0;
  439. len = PAGE_ALIGN(len);
  440. end = start + len;
  441. if (end <= start)
  442. return -ENOMEM;
  443. if (!arch_validate_prot(prot, start))
  444. return -EINVAL;
  445. reqprot = prot;
  446. if (down_write_killable(&current->mm->mmap_sem))
  447. return -EINTR;
  448. /*
  449. * If userspace did not allocate the pkey, do not let
  450. * them use it here.
  451. */
  452. error = -EINVAL;
  453. if ((pkey != -1) && !mm_pkey_is_allocated(current->mm, pkey))
  454. goto out;
  455. vma = find_vma(current->mm, start);
  456. error = -ENOMEM;
  457. if (!vma)
  458. goto out;
  459. prev = vma->vm_prev;
  460. if (unlikely(grows & PROT_GROWSDOWN)) {
  461. if (vma->vm_start >= end)
  462. goto out;
  463. start = vma->vm_start;
  464. error = -EINVAL;
  465. if (!(vma->vm_flags & VM_GROWSDOWN))
  466. goto out;
  467. } else {
  468. if (vma->vm_start > start)
  469. goto out;
  470. if (unlikely(grows & PROT_GROWSUP)) {
  471. end = vma->vm_end;
  472. error = -EINVAL;
  473. if (!(vma->vm_flags & VM_GROWSUP))
  474. goto out;
  475. }
  476. }
  477. if (start > vma->vm_start)
  478. prev = vma;
  479. for (nstart = start ; ; ) {
  480. unsigned long mask_off_old_flags;
  481. unsigned long newflags;
  482. int new_vma_pkey;
  483. /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
  484. /* Does the application expect PROT_READ to imply PROT_EXEC */
  485. if (rier && (vma->vm_flags & VM_MAYEXEC))
  486. prot |= PROT_EXEC;
  487. /*
  488. * Each mprotect() call explicitly passes r/w/x permissions.
  489. * If a permission is not passed to mprotect(), it must be
  490. * cleared from the VMA.
  491. */
  492. mask_off_old_flags = VM_READ | VM_WRITE | VM_EXEC |
  493. VM_FLAGS_CLEAR;
  494. new_vma_pkey = arch_override_mprotect_pkey(vma, prot, pkey);
  495. newflags = calc_vm_prot_bits(prot, new_vma_pkey);
  496. newflags |= (vma->vm_flags & ~mask_off_old_flags);
  497. /* newflags >> 4 shift VM_MAY% in place of VM_% */
  498. if ((newflags & ~(newflags >> 4)) & (VM_READ | VM_WRITE | VM_EXEC)) {
  499. error = -EACCES;
  500. goto out;
  501. }
  502. error = security_file_mprotect(vma, reqprot, prot);
  503. if (error)
  504. goto out;
  505. tmp = vma->vm_end;
  506. if (tmp > end)
  507. tmp = end;
  508. error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
  509. if (error)
  510. goto out;
  511. nstart = tmp;
  512. if (nstart < prev->vm_end)
  513. nstart = prev->vm_end;
  514. if (nstart >= end)
  515. goto out;
  516. vma = prev->vm_next;
  517. if (!vma || vma->vm_start != nstart) {
  518. error = -ENOMEM;
  519. goto out;
  520. }
  521. prot = reqprot;
  522. }
  523. out:
  524. up_write(&current->mm->mmap_sem);
  525. return error;
  526. }
  527. SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
  528. unsigned long, prot)
  529. {
  530. return do_mprotect_pkey(start, len, prot, -1);
  531. }
  532. #ifdef CONFIG_ARCH_HAS_PKEYS
  533. SYSCALL_DEFINE4(pkey_mprotect, unsigned long, start, size_t, len,
  534. unsigned long, prot, int, pkey)
  535. {
  536. return do_mprotect_pkey(start, len, prot, pkey);
  537. }
  538. SYSCALL_DEFINE2(pkey_alloc, unsigned long, flags, unsigned long, init_val)
  539. {
  540. int pkey;
  541. int ret;
  542. /* No flags supported yet. */
  543. if (flags)
  544. return -EINVAL;
  545. /* check for unsupported init values */
  546. if (init_val & ~PKEY_ACCESS_MASK)
  547. return -EINVAL;
  548. down_write(&current->mm->mmap_sem);
  549. pkey = mm_pkey_alloc(current->mm);
  550. ret = -ENOSPC;
  551. if (pkey == -1)
  552. goto out;
  553. ret = arch_set_user_pkey_access(current, pkey, init_val);
  554. if (ret) {
  555. mm_pkey_free(current->mm, pkey);
  556. goto out;
  557. }
  558. ret = pkey;
  559. out:
  560. up_write(&current->mm->mmap_sem);
  561. return ret;
  562. }
  563. SYSCALL_DEFINE1(pkey_free, int, pkey)
  564. {
  565. int ret;
  566. down_write(&current->mm->mmap_sem);
  567. ret = mm_pkey_free(current->mm, pkey);
  568. up_write(&current->mm->mmap_sem);
  569. /*
  570. * We could provie warnings or errors if any VMA still
  571. * has the pkey set here.
  572. */
  573. return ret;
  574. }
  575. #endif /* CONFIG_ARCH_HAS_PKEYS */