mprotect.c 17 KB

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