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