Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Andrea Arcangeli | 932b18e | 2015-09-04 15:46:10 -0700 | [diff] [blame] | 2 | /* |
| 3 | * include/linux/userfaultfd_k.h |
| 4 | * |
| 5 | * Copyright (C) 2015 Red Hat, Inc. |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #ifndef _LINUX_USERFAULTFD_K_H |
| 10 | #define _LINUX_USERFAULTFD_K_H |
| 11 | |
| 12 | #ifdef CONFIG_USERFAULTFD |
| 13 | |
| 14 | #include <linux/userfaultfd.h> /* linux/include/uapi/linux/userfaultfd.h */ |
| 15 | |
| 16 | #include <linux/fcntl.h> |
Andrea Arcangeli | 55adf4d | 2020-04-06 20:05:37 -0700 | [diff] [blame] | 17 | #include <linux/mm.h> |
| 18 | #include <asm-generic/pgtable_uffd.h> |
Andrea Arcangeli | 932b18e | 2015-09-04 15:46:10 -0700 | [diff] [blame] | 19 | |
| 20 | /* |
| 21 | * CAREFUL: Check include/uapi/asm-generic/fcntl.h when defining |
| 22 | * new flags, since they might collide with O_* ones. We want |
| 23 | * to re-use O_* flags that couldn't possibly have a meaning |
| 24 | * from userfaultfd, in order to leave a free define-space for |
| 25 | * shared O_* flags. |
| 26 | */ |
| 27 | #define UFFD_CLOEXEC O_CLOEXEC |
| 28 | #define UFFD_NONBLOCK O_NONBLOCK |
| 29 | |
| 30 | #define UFFD_SHARED_FCNTL_FLAGS (O_CLOEXEC | O_NONBLOCK) |
| 31 | #define UFFD_FLAGS_SET (EFD_SHARED_FCNTL_FLAGS) |
| 32 | |
Peter Xu | cefdca0 | 2019-05-13 17:16:41 -0700 | [diff] [blame] | 33 | extern int sysctl_unprivileged_userfaultfd; |
| 34 | |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 35 | extern vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason); |
Andrea Arcangeli | 932b18e | 2015-09-04 15:46:10 -0700 | [diff] [blame] | 36 | |
Andrea Arcangeli | c1a4de9 | 2015-09-04 15:47:04 -0700 | [diff] [blame] | 37 | extern ssize_t mcopy_atomic(struct mm_struct *dst_mm, unsigned long dst_start, |
Mike Rapoport | df2cc96 | 2018-06-07 17:09:25 -0700 | [diff] [blame] | 38 | unsigned long src_start, unsigned long len, |
Andrea Arcangeli | 72981e0 | 2020-04-06 20:05:41 -0700 | [diff] [blame] | 39 | bool *mmap_changing, __u64 mode); |
Andrea Arcangeli | c1a4de9 | 2015-09-04 15:47:04 -0700 | [diff] [blame] | 40 | extern ssize_t mfill_zeropage(struct mm_struct *dst_mm, |
| 41 | unsigned long dst_start, |
Mike Rapoport | df2cc96 | 2018-06-07 17:09:25 -0700 | [diff] [blame] | 42 | unsigned long len, |
| 43 | bool *mmap_changing); |
Shaohua Li | ffd0579 | 2020-04-06 20:06:09 -0700 | [diff] [blame] | 44 | extern int mwriteprotect_range(struct mm_struct *dst_mm, |
| 45 | unsigned long start, unsigned long len, |
| 46 | bool enable_wp, bool *mmap_changing); |
Andrea Arcangeli | c1a4de9 | 2015-09-04 15:47:04 -0700 | [diff] [blame] | 47 | |
Andrea Arcangeli | 932b18e | 2015-09-04 15:46:10 -0700 | [diff] [blame] | 48 | /* mm helpers */ |
| 49 | static inline bool is_mergeable_vm_userfaultfd_ctx(struct vm_area_struct *vma, |
| 50 | struct vm_userfaultfd_ctx vm_ctx) |
| 51 | { |
| 52 | return vma->vm_userfaultfd_ctx.ctx == vm_ctx.ctx; |
| 53 | } |
| 54 | |
| 55 | static inline bool userfaultfd_missing(struct vm_area_struct *vma) |
| 56 | { |
| 57 | return vma->vm_flags & VM_UFFD_MISSING; |
| 58 | } |
| 59 | |
Shaohua Li | 1df319e | 2020-04-06 20:05:25 -0700 | [diff] [blame] | 60 | static inline bool userfaultfd_wp(struct vm_area_struct *vma) |
| 61 | { |
| 62 | return vma->vm_flags & VM_UFFD_WP; |
| 63 | } |
| 64 | |
Andrea Arcangeli | 55adf4d | 2020-04-06 20:05:37 -0700 | [diff] [blame] | 65 | static inline bool userfaultfd_pte_wp(struct vm_area_struct *vma, |
| 66 | pte_t pte) |
| 67 | { |
| 68 | return userfaultfd_wp(vma) && pte_uffd_wp(pte); |
| 69 | } |
| 70 | |
| 71 | static inline bool userfaultfd_huge_pmd_wp(struct vm_area_struct *vma, |
| 72 | pmd_t pmd) |
| 73 | { |
| 74 | return userfaultfd_wp(vma) && pmd_uffd_wp(pmd); |
| 75 | } |
| 76 | |
Andrea Arcangeli | 932b18e | 2015-09-04 15:46:10 -0700 | [diff] [blame] | 77 | static inline bool userfaultfd_armed(struct vm_area_struct *vma) |
| 78 | { |
| 79 | return vma->vm_flags & (VM_UFFD_MISSING | VM_UFFD_WP); |
| 80 | } |
| 81 | |
Pavel Emelyanov | 893e26e | 2017-02-22 15:42:27 -0800 | [diff] [blame] | 82 | extern int dup_userfaultfd(struct vm_area_struct *, struct list_head *); |
| 83 | extern void dup_userfaultfd_complete(struct list_head *); |
| 84 | |
Pavel Emelyanov | 72f8765 | 2017-02-22 15:42:34 -0800 | [diff] [blame] | 85 | extern void mremap_userfaultfd_prep(struct vm_area_struct *, |
| 86 | struct vm_userfaultfd_ctx *); |
Andrea Arcangeli | 90794bf | 2017-02-22 15:42:37 -0800 | [diff] [blame] | 87 | extern void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *, |
Pavel Emelyanov | 72f8765 | 2017-02-22 15:42:34 -0800 | [diff] [blame] | 88 | unsigned long from, unsigned long to, |
| 89 | unsigned long len); |
| 90 | |
Andrea Arcangeli | 70ccb92 | 2017-03-09 16:17:11 -0800 | [diff] [blame] | 91 | extern bool userfaultfd_remove(struct vm_area_struct *vma, |
Mike Rapoport | d811914 | 2017-02-24 14:56:02 -0800 | [diff] [blame] | 92 | unsigned long start, |
| 93 | unsigned long end); |
Pavel Emelyanov | 05ce772 | 2017-02-22 15:42:40 -0800 | [diff] [blame] | 94 | |
Mike Rapoport | 897ab3e | 2017-02-24 14:58:22 -0800 | [diff] [blame] | 95 | extern int userfaultfd_unmap_prep(struct vm_area_struct *vma, |
| 96 | unsigned long start, unsigned long end, |
| 97 | struct list_head *uf); |
| 98 | extern void userfaultfd_unmap_complete(struct mm_struct *mm, |
| 99 | struct list_head *uf); |
| 100 | |
Andrea Arcangeli | 932b18e | 2015-09-04 15:46:10 -0700 | [diff] [blame] | 101 | #else /* CONFIG_USERFAULTFD */ |
| 102 | |
| 103 | /* mm helpers */ |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 104 | static inline vm_fault_t handle_userfault(struct vm_fault *vmf, |
| 105 | unsigned long reason) |
Andrea Arcangeli | 932b18e | 2015-09-04 15:46:10 -0700 | [diff] [blame] | 106 | { |
| 107 | return VM_FAULT_SIGBUS; |
| 108 | } |
| 109 | |
| 110 | static inline bool is_mergeable_vm_userfaultfd_ctx(struct vm_area_struct *vma, |
| 111 | struct vm_userfaultfd_ctx vm_ctx) |
| 112 | { |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | static inline bool userfaultfd_missing(struct vm_area_struct *vma) |
| 117 | { |
| 118 | return false; |
| 119 | } |
| 120 | |
Shaohua Li | 1df319e | 2020-04-06 20:05:25 -0700 | [diff] [blame] | 121 | static inline bool userfaultfd_wp(struct vm_area_struct *vma) |
| 122 | { |
| 123 | return false; |
| 124 | } |
| 125 | |
Andrea Arcangeli | 55adf4d | 2020-04-06 20:05:37 -0700 | [diff] [blame] | 126 | static inline bool userfaultfd_pte_wp(struct vm_area_struct *vma, |
| 127 | pte_t pte) |
| 128 | { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | static inline bool userfaultfd_huge_pmd_wp(struct vm_area_struct *vma, |
| 133 | pmd_t pmd) |
| 134 | { |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | |
Andrea Arcangeli | 932b18e | 2015-09-04 15:46:10 -0700 | [diff] [blame] | 139 | static inline bool userfaultfd_armed(struct vm_area_struct *vma) |
| 140 | { |
| 141 | return false; |
| 142 | } |
| 143 | |
Pavel Emelyanov | 893e26e | 2017-02-22 15:42:27 -0800 | [diff] [blame] | 144 | static inline int dup_userfaultfd(struct vm_area_struct *vma, |
| 145 | struct list_head *l) |
| 146 | { |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static inline void dup_userfaultfd_complete(struct list_head *l) |
| 151 | { |
| 152 | } |
| 153 | |
Pavel Emelyanov | 72f8765 | 2017-02-22 15:42:34 -0800 | [diff] [blame] | 154 | static inline void mremap_userfaultfd_prep(struct vm_area_struct *vma, |
| 155 | struct vm_userfaultfd_ctx *ctx) |
| 156 | { |
| 157 | } |
| 158 | |
Andrea Arcangeli | 90794bf | 2017-02-22 15:42:37 -0800 | [diff] [blame] | 159 | static inline void mremap_userfaultfd_complete(struct vm_userfaultfd_ctx *ctx, |
Pavel Emelyanov | 72f8765 | 2017-02-22 15:42:34 -0800 | [diff] [blame] | 160 | unsigned long from, |
| 161 | unsigned long to, |
| 162 | unsigned long len) |
| 163 | { |
| 164 | } |
Pavel Emelyanov | 05ce772 | 2017-02-22 15:42:40 -0800 | [diff] [blame] | 165 | |
Andrea Arcangeli | 70ccb92 | 2017-03-09 16:17:11 -0800 | [diff] [blame] | 166 | static inline bool userfaultfd_remove(struct vm_area_struct *vma, |
Mike Rapoport | d811914 | 2017-02-24 14:56:02 -0800 | [diff] [blame] | 167 | unsigned long start, |
| 168 | unsigned long end) |
Pavel Emelyanov | 05ce772 | 2017-02-22 15:42:40 -0800 | [diff] [blame] | 169 | { |
Andrea Arcangeli | 70ccb92 | 2017-03-09 16:17:11 -0800 | [diff] [blame] | 170 | return true; |
Pavel Emelyanov | 05ce772 | 2017-02-22 15:42:40 -0800 | [diff] [blame] | 171 | } |
Mike Rapoport | 897ab3e | 2017-02-24 14:58:22 -0800 | [diff] [blame] | 172 | |
| 173 | static inline int userfaultfd_unmap_prep(struct vm_area_struct *vma, |
| 174 | unsigned long start, unsigned long end, |
| 175 | struct list_head *uf) |
| 176 | { |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static inline void userfaultfd_unmap_complete(struct mm_struct *mm, |
| 181 | struct list_head *uf) |
| 182 | { |
| 183 | } |
Mike Rapoport | ca49ca7 | 2017-02-24 14:58:25 -0800 | [diff] [blame] | 184 | |
Andrea Arcangeli | 932b18e | 2015-09-04 15:46:10 -0700 | [diff] [blame] | 185 | #endif /* CONFIG_USERFAULTFD */ |
| 186 | |
| 187 | #endif /* _LINUX_USERFAULTFD_K_H */ |