Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrew Morton | 16d6926 | 2008-07-25 19:44:36 -0700 | [diff] [blame] | 2 | #include <linux/mm.h> |
Matt Mackall | 30992c9 | 2006-01-08 01:01:43 -0800 | [diff] [blame] | 3 | #include <linux/slab.h> |
| 4 | #include <linux/string.h> |
Gideon Israel Dsouza | 3b32123 | 2014-04-07 15:37:26 -0700 | [diff] [blame] | 5 | #include <linux/compiler.h> |
Paul Gortmaker | b95f1b31 | 2011-10-16 02:01:52 -0400 | [diff] [blame] | 6 | #include <linux/export.h> |
Davi Arnaut | 96840aa | 2006-03-24 03:18:42 -0800 | [diff] [blame] | 7 | #include <linux/err.h> |
Adrian Bunk | 3b8f14b | 2008-07-26 15:22:28 -0700 | [diff] [blame] | 8 | #include <linux/sched.h> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 9 | #include <linux/sched/mm.h> |
Daniel Jordan | 79eb597 | 2019-07-16 16:30:54 -0700 | [diff] [blame] | 10 | #include <linux/sched/signal.h> |
Ingo Molnar | 68db0cf | 2017-02-08 18:51:37 +0100 | [diff] [blame] | 11 | #include <linux/sched/task_stack.h> |
Al Viro | eb36c58 | 2012-05-30 20:17:35 -0400 | [diff] [blame] | 12 | #include <linux/security.h> |
Shaohua Li | 9800339 | 2013-02-22 16:34:35 -0800 | [diff] [blame] | 13 | #include <linux/swap.h> |
Shaohua Li | 33806f0 | 2013-02-22 16:34:37 -0800 | [diff] [blame] | 14 | #include <linux/swapops.h> |
Jerome Marchand | 00619bc | 2013-11-12 15:08:31 -0800 | [diff] [blame] | 15 | #include <linux/mman.h> |
| 16 | #include <linux/hugetlb.h> |
Al Viro | 39f1f78 | 2014-05-06 14:02:53 -0400 | [diff] [blame] | 17 | #include <linux/vmalloc.h> |
Mike Rapoport | 897ab3e | 2017-02-24 14:58:22 -0800 | [diff] [blame] | 18 | #include <linux/userfaultfd_k.h> |
Jerome Marchand | 00619bc | 2013-11-12 15:08:31 -0800 | [diff] [blame] | 19 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 20 | #include <linux/uaccess.h> |
Matt Mackall | 30992c9 | 2006-01-08 01:01:43 -0800 | [diff] [blame] | 21 | |
Namhyung Kim | 6038def | 2011-05-24 17:11:22 -0700 | [diff] [blame] | 22 | #include "internal.h" |
| 23 | |
Andrzej Hajda | a4bb1e4 | 2015-02-13 14:36:24 -0800 | [diff] [blame] | 24 | /** |
| 25 | * kfree_const - conditionally free memory |
| 26 | * @x: pointer to the memory |
| 27 | * |
| 28 | * Function calls kfree only if @x is not in .rodata section. |
| 29 | */ |
| 30 | void kfree_const(const void *x) |
| 31 | { |
| 32 | if (!is_kernel_rodata((unsigned long)x)) |
| 33 | kfree(x); |
| 34 | } |
| 35 | EXPORT_SYMBOL(kfree_const); |
| 36 | |
Matt Mackall | 30992c9 | 2006-01-08 01:01:43 -0800 | [diff] [blame] | 37 | /** |
Matt Mackall | 30992c9 | 2006-01-08 01:01:43 -0800 | [diff] [blame] | 38 | * kstrdup - allocate space for and copy an existing string |
Matt Mackall | 30992c9 | 2006-01-08 01:01:43 -0800 | [diff] [blame] | 39 | * @s: the string to duplicate |
| 40 | * @gfp: the GFP mask used in the kmalloc() call when allocating memory |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 41 | * |
| 42 | * Return: newly allocated copy of @s or %NULL in case of error |
Matt Mackall | 30992c9 | 2006-01-08 01:01:43 -0800 | [diff] [blame] | 43 | */ |
| 44 | char *kstrdup(const char *s, gfp_t gfp) |
| 45 | { |
| 46 | size_t len; |
| 47 | char *buf; |
| 48 | |
| 49 | if (!s) |
| 50 | return NULL; |
| 51 | |
| 52 | len = strlen(s) + 1; |
Christoph Hellwig | 1d2c8ee | 2006-10-04 02:15:25 -0700 | [diff] [blame] | 53 | buf = kmalloc_track_caller(len, gfp); |
Matt Mackall | 30992c9 | 2006-01-08 01:01:43 -0800 | [diff] [blame] | 54 | if (buf) |
| 55 | memcpy(buf, s, len); |
| 56 | return buf; |
| 57 | } |
| 58 | EXPORT_SYMBOL(kstrdup); |
Davi Arnaut | 96840aa | 2006-03-24 03:18:42 -0800 | [diff] [blame] | 59 | |
Alexey Dobriyan | 1a2f67b | 2006-09-30 23:27:20 -0700 | [diff] [blame] | 60 | /** |
Andrzej Hajda | a4bb1e4 | 2015-02-13 14:36:24 -0800 | [diff] [blame] | 61 | * kstrdup_const - conditionally duplicate an existing const string |
| 62 | * @s: the string to duplicate |
| 63 | * @gfp: the GFP mask used in the kmalloc() call when allocating memory |
| 64 | * |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 65 | * Note: Strings allocated by kstrdup_const should be freed by kfree_const. |
| 66 | * |
| 67 | * Return: source string if it is in .rodata section otherwise |
| 68 | * fallback to kstrdup. |
Andrzej Hajda | a4bb1e4 | 2015-02-13 14:36:24 -0800 | [diff] [blame] | 69 | */ |
| 70 | const char *kstrdup_const(const char *s, gfp_t gfp) |
| 71 | { |
| 72 | if (is_kernel_rodata((unsigned long)s)) |
| 73 | return s; |
| 74 | |
| 75 | return kstrdup(s, gfp); |
| 76 | } |
| 77 | EXPORT_SYMBOL(kstrdup_const); |
| 78 | |
| 79 | /** |
Jeremy Fitzhardinge | 1e66df3 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 80 | * kstrndup - allocate space for and copy an existing string |
| 81 | * @s: the string to duplicate |
| 82 | * @max: read at most @max chars from @s |
| 83 | * @gfp: the GFP mask used in the kmalloc() call when allocating memory |
David Howells | f351574 | 2017-07-04 17:25:02 +0100 | [diff] [blame] | 84 | * |
| 85 | * Note: Use kmemdup_nul() instead if the size is known exactly. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 86 | * |
| 87 | * Return: newly allocated copy of @s or %NULL in case of error |
Jeremy Fitzhardinge | 1e66df3 | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 88 | */ |
| 89 | char *kstrndup(const char *s, size_t max, gfp_t gfp) |
| 90 | { |
| 91 | size_t len; |
| 92 | char *buf; |
| 93 | |
| 94 | if (!s) |
| 95 | return NULL; |
| 96 | |
| 97 | len = strnlen(s, max); |
| 98 | buf = kmalloc_track_caller(len+1, gfp); |
| 99 | if (buf) { |
| 100 | memcpy(buf, s, len); |
| 101 | buf[len] = '\0'; |
| 102 | } |
| 103 | return buf; |
| 104 | } |
| 105 | EXPORT_SYMBOL(kstrndup); |
| 106 | |
| 107 | /** |
Alexey Dobriyan | 1a2f67b | 2006-09-30 23:27:20 -0700 | [diff] [blame] | 108 | * kmemdup - duplicate region of memory |
| 109 | * |
| 110 | * @src: memory region to duplicate |
| 111 | * @len: memory region length |
| 112 | * @gfp: GFP mask to use |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 113 | * |
| 114 | * Return: newly allocated copy of @src or %NULL in case of error |
Alexey Dobriyan | 1a2f67b | 2006-09-30 23:27:20 -0700 | [diff] [blame] | 115 | */ |
| 116 | void *kmemdup(const void *src, size_t len, gfp_t gfp) |
| 117 | { |
| 118 | void *p; |
| 119 | |
Christoph Hellwig | 1d2c8ee | 2006-10-04 02:15:25 -0700 | [diff] [blame] | 120 | p = kmalloc_track_caller(len, gfp); |
Alexey Dobriyan | 1a2f67b | 2006-09-30 23:27:20 -0700 | [diff] [blame] | 121 | if (p) |
| 122 | memcpy(p, src, len); |
| 123 | return p; |
| 124 | } |
| 125 | EXPORT_SYMBOL(kmemdup); |
| 126 | |
Christoph Lameter | ef2ad80 | 2007-07-17 04:03:21 -0700 | [diff] [blame] | 127 | /** |
David Howells | f351574 | 2017-07-04 17:25:02 +0100 | [diff] [blame] | 128 | * kmemdup_nul - Create a NUL-terminated string from unterminated data |
| 129 | * @s: The data to stringify |
| 130 | * @len: The size of the data |
| 131 | * @gfp: the GFP mask used in the kmalloc() call when allocating memory |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 132 | * |
| 133 | * Return: newly allocated copy of @s with NUL-termination or %NULL in |
| 134 | * case of error |
David Howells | f351574 | 2017-07-04 17:25:02 +0100 | [diff] [blame] | 135 | */ |
| 136 | char *kmemdup_nul(const char *s, size_t len, gfp_t gfp) |
| 137 | { |
| 138 | char *buf; |
| 139 | |
| 140 | if (!s) |
| 141 | return NULL; |
| 142 | |
| 143 | buf = kmalloc_track_caller(len + 1, gfp); |
| 144 | if (buf) { |
| 145 | memcpy(buf, s, len); |
| 146 | buf[len] = '\0'; |
| 147 | } |
| 148 | return buf; |
| 149 | } |
| 150 | EXPORT_SYMBOL(kmemdup_nul); |
| 151 | |
| 152 | /** |
Li Zefan | 610a77e | 2009-03-31 15:23:16 -0700 | [diff] [blame] | 153 | * memdup_user - duplicate memory region from user space |
| 154 | * |
| 155 | * @src: source address in user space |
| 156 | * @len: number of bytes to copy |
| 157 | * |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 158 | * Return: an ERR_PTR() on failure. Result is physically |
Al Viro | 50fd2f2 | 2018-01-07 13:06:15 -0500 | [diff] [blame] | 159 | * contiguous, to be freed by kfree(). |
Li Zefan | 610a77e | 2009-03-31 15:23:16 -0700 | [diff] [blame] | 160 | */ |
| 161 | void *memdup_user(const void __user *src, size_t len) |
| 162 | { |
| 163 | void *p; |
| 164 | |
Daniel Vetter | 6c8fcc0 | 2019-02-20 22:20:42 -0800 | [diff] [blame] | 165 | p = kmalloc_track_caller(len, GFP_USER | __GFP_NOWARN); |
Li Zefan | 610a77e | 2009-03-31 15:23:16 -0700 | [diff] [blame] | 166 | if (!p) |
| 167 | return ERR_PTR(-ENOMEM); |
| 168 | |
| 169 | if (copy_from_user(p, src, len)) { |
| 170 | kfree(p); |
| 171 | return ERR_PTR(-EFAULT); |
| 172 | } |
| 173 | |
| 174 | return p; |
| 175 | } |
| 176 | EXPORT_SYMBOL(memdup_user); |
| 177 | |
Al Viro | 50fd2f2 | 2018-01-07 13:06:15 -0500 | [diff] [blame] | 178 | /** |
| 179 | * vmemdup_user - duplicate memory region from user space |
| 180 | * |
| 181 | * @src: source address in user space |
| 182 | * @len: number of bytes to copy |
| 183 | * |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 184 | * Return: an ERR_PTR() on failure. Result may be not |
Al Viro | 50fd2f2 | 2018-01-07 13:06:15 -0500 | [diff] [blame] | 185 | * physically contiguous. Use kvfree() to free. |
| 186 | */ |
| 187 | void *vmemdup_user(const void __user *src, size_t len) |
| 188 | { |
| 189 | void *p; |
| 190 | |
| 191 | p = kvmalloc(len, GFP_USER); |
| 192 | if (!p) |
| 193 | return ERR_PTR(-ENOMEM); |
| 194 | |
| 195 | if (copy_from_user(p, src, len)) { |
| 196 | kvfree(p); |
| 197 | return ERR_PTR(-EFAULT); |
| 198 | } |
| 199 | |
| 200 | return p; |
| 201 | } |
| 202 | EXPORT_SYMBOL(vmemdup_user); |
| 203 | |
Mike Rapoport | b86181f | 2018-08-23 17:00:59 -0700 | [diff] [blame] | 204 | /** |
Davi Arnaut | 96840aa | 2006-03-24 03:18:42 -0800 | [diff] [blame] | 205 | * strndup_user - duplicate an existing string from user space |
Davi Arnaut | 96840aa | 2006-03-24 03:18:42 -0800 | [diff] [blame] | 206 | * @s: The string to duplicate |
| 207 | * @n: Maximum number of bytes to copy, including the trailing NUL. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 208 | * |
Andrew Morton | e914552 | 2019-04-05 18:39:34 -0700 | [diff] [blame] | 209 | * Return: newly allocated copy of @s or an ERR_PTR() in case of error |
Davi Arnaut | 96840aa | 2006-03-24 03:18:42 -0800 | [diff] [blame] | 210 | */ |
| 211 | char *strndup_user(const char __user *s, long n) |
| 212 | { |
| 213 | char *p; |
| 214 | long length; |
| 215 | |
| 216 | length = strnlen_user(s, n); |
| 217 | |
| 218 | if (!length) |
| 219 | return ERR_PTR(-EFAULT); |
| 220 | |
| 221 | if (length > n) |
| 222 | return ERR_PTR(-EINVAL); |
| 223 | |
Julia Lawall | 90d7404 | 2010-08-09 17:18:26 -0700 | [diff] [blame] | 224 | p = memdup_user(s, length); |
Davi Arnaut | 96840aa | 2006-03-24 03:18:42 -0800 | [diff] [blame] | 225 | |
Julia Lawall | 90d7404 | 2010-08-09 17:18:26 -0700 | [diff] [blame] | 226 | if (IS_ERR(p)) |
| 227 | return p; |
Davi Arnaut | 96840aa | 2006-03-24 03:18:42 -0800 | [diff] [blame] | 228 | |
| 229 | p[length - 1] = '\0'; |
| 230 | |
| 231 | return p; |
| 232 | } |
| 233 | EXPORT_SYMBOL(strndup_user); |
Andrew Morton | 16d6926 | 2008-07-25 19:44:36 -0700 | [diff] [blame] | 234 | |
Al Viro | e9d408e | 2015-12-24 00:06:05 -0500 | [diff] [blame] | 235 | /** |
| 236 | * memdup_user_nul - duplicate memory region from user space and NUL-terminate |
| 237 | * |
| 238 | * @src: source address in user space |
| 239 | * @len: number of bytes to copy |
| 240 | * |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 241 | * Return: an ERR_PTR() on failure. |
Al Viro | e9d408e | 2015-12-24 00:06:05 -0500 | [diff] [blame] | 242 | */ |
| 243 | void *memdup_user_nul(const void __user *src, size_t len) |
| 244 | { |
| 245 | char *p; |
| 246 | |
| 247 | /* |
| 248 | * Always use GFP_KERNEL, since copy_from_user() can sleep and |
| 249 | * cause pagefault, which makes it pointless to use GFP_NOFS |
| 250 | * or GFP_ATOMIC. |
| 251 | */ |
| 252 | p = kmalloc_track_caller(len + 1, GFP_KERNEL); |
| 253 | if (!p) |
| 254 | return ERR_PTR(-ENOMEM); |
| 255 | |
| 256 | if (copy_from_user(p, src, len)) { |
| 257 | kfree(p); |
| 258 | return ERR_PTR(-EFAULT); |
| 259 | } |
| 260 | p[len] = '\0'; |
| 261 | |
| 262 | return p; |
| 263 | } |
| 264 | EXPORT_SYMBOL(memdup_user_nul); |
| 265 | |
Namhyung Kim | 6038def | 2011-05-24 17:11:22 -0700 | [diff] [blame] | 266 | void __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma, |
| 267 | struct vm_area_struct *prev, struct rb_node *rb_parent) |
| 268 | { |
| 269 | struct vm_area_struct *next; |
| 270 | |
| 271 | vma->vm_prev = prev; |
| 272 | if (prev) { |
| 273 | next = prev->vm_next; |
| 274 | prev->vm_next = vma; |
| 275 | } else { |
| 276 | mm->mmap = vma; |
| 277 | if (rb_parent) |
| 278 | next = rb_entry(rb_parent, |
| 279 | struct vm_area_struct, vm_rb); |
| 280 | else |
| 281 | next = NULL; |
| 282 | } |
| 283 | vma->vm_next = next; |
| 284 | if (next) |
| 285 | next->vm_prev = vma; |
| 286 | } |
| 287 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 288 | /* Check if the vma is being used as a stack by this task */ |
Andy Lutomirski | d17af50 | 2016-09-30 10:58:58 -0700 | [diff] [blame] | 289 | int vma_is_stack_for_current(struct vm_area_struct *vma) |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 290 | { |
Andy Lutomirski | d17af50 | 2016-09-30 10:58:58 -0700 | [diff] [blame] | 291 | struct task_struct * __maybe_unused t = current; |
| 292 | |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 293 | return (vma->vm_start <= KSTK_ESP(t) && vma->vm_end >= KSTK_ESP(t)); |
| 294 | } |
| 295 | |
David Howells | efc1a3b | 2010-01-15 17:01:35 -0800 | [diff] [blame] | 296 | #if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT) |
Kees Cook | 8f2af15 | 2018-04-10 16:34:53 -0700 | [diff] [blame] | 297 | void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) |
Andrew Morton | 16d6926 | 2008-07-25 19:44:36 -0700 | [diff] [blame] | 298 | { |
| 299 | mm->mmap_base = TASK_UNMAPPED_BASE; |
| 300 | mm->get_unmapped_area = arch_get_unmapped_area; |
Andrew Morton | 16d6926 | 2008-07-25 19:44:36 -0700 | [diff] [blame] | 301 | } |
| 302 | #endif |
Rusty Russell | 912985d | 2008-08-12 17:52:52 -0500 | [diff] [blame] | 303 | |
Daniel Jordan | 79eb597 | 2019-07-16 16:30:54 -0700 | [diff] [blame] | 304 | /** |
| 305 | * __account_locked_vm - account locked pages to an mm's locked_vm |
| 306 | * @mm: mm to account against |
| 307 | * @pages: number of pages to account |
| 308 | * @inc: %true if @pages should be considered positive, %false if not |
| 309 | * @task: task used to check RLIMIT_MEMLOCK |
| 310 | * @bypass_rlim: %true if checking RLIMIT_MEMLOCK should be skipped |
| 311 | * |
| 312 | * Assumes @task and @mm are valid (i.e. at least one reference on each), and |
| 313 | * that mmap_sem is held as writer. |
| 314 | * |
| 315 | * Return: |
| 316 | * * 0 on success |
| 317 | * * -ENOMEM if RLIMIT_MEMLOCK would be exceeded. |
| 318 | */ |
| 319 | int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc, |
| 320 | struct task_struct *task, bool bypass_rlim) |
| 321 | { |
| 322 | unsigned long locked_vm, limit; |
| 323 | int ret = 0; |
| 324 | |
| 325 | lockdep_assert_held_write(&mm->mmap_sem); |
| 326 | |
| 327 | locked_vm = mm->locked_vm; |
| 328 | if (inc) { |
| 329 | if (!bypass_rlim) { |
| 330 | limit = task_rlimit(task, RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 331 | if (locked_vm + pages > limit) |
| 332 | ret = -ENOMEM; |
| 333 | } |
| 334 | if (!ret) |
| 335 | mm->locked_vm = locked_vm + pages; |
| 336 | } else { |
| 337 | WARN_ON_ONCE(pages > locked_vm); |
| 338 | mm->locked_vm = locked_vm - pages; |
| 339 | } |
| 340 | |
| 341 | pr_debug("%s: [%d] caller %ps %c%lu %lu/%lu%s\n", __func__, task->pid, |
| 342 | (void *)_RET_IP_, (inc) ? '+' : '-', pages << PAGE_SHIFT, |
| 343 | locked_vm << PAGE_SHIFT, task_rlimit(task, RLIMIT_MEMLOCK), |
| 344 | ret ? " - exceeded" : ""); |
| 345 | |
| 346 | return ret; |
| 347 | } |
| 348 | EXPORT_SYMBOL_GPL(__account_locked_vm); |
| 349 | |
| 350 | /** |
| 351 | * account_locked_vm - account locked pages to an mm's locked_vm |
| 352 | * @mm: mm to account against, may be NULL |
| 353 | * @pages: number of pages to account |
| 354 | * @inc: %true if @pages should be considered positive, %false if not |
| 355 | * |
| 356 | * Assumes a non-NULL @mm is valid (i.e. at least one reference on it). |
| 357 | * |
| 358 | * Return: |
| 359 | * * 0 on success, or if mm is NULL |
| 360 | * * -ENOMEM if RLIMIT_MEMLOCK would be exceeded. |
| 361 | */ |
| 362 | int account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc) |
| 363 | { |
| 364 | int ret; |
| 365 | |
| 366 | if (pages == 0 || !mm) |
| 367 | return 0; |
| 368 | |
| 369 | down_write(&mm->mmap_sem); |
| 370 | ret = __account_locked_vm(mm, pages, inc, current, |
| 371 | capable(CAP_IPC_LOCK)); |
| 372 | up_write(&mm->mmap_sem); |
| 373 | |
| 374 | return ret; |
| 375 | } |
| 376 | EXPORT_SYMBOL_GPL(account_locked_vm); |
| 377 | |
Al Viro | eb36c58 | 2012-05-30 20:17:35 -0400 | [diff] [blame] | 378 | unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr, |
| 379 | unsigned long len, unsigned long prot, |
Michal Hocko | 9fbeb5a | 2016-05-23 16:25:30 -0700 | [diff] [blame] | 380 | unsigned long flag, unsigned long pgoff) |
Al Viro | eb36c58 | 2012-05-30 20:17:35 -0400 | [diff] [blame] | 381 | { |
| 382 | unsigned long ret; |
| 383 | struct mm_struct *mm = current->mm; |
Michel Lespinasse | 41badc1 | 2013-02-22 16:32:47 -0800 | [diff] [blame] | 384 | unsigned long populate; |
Mike Rapoport | 897ab3e | 2017-02-24 14:58:22 -0800 | [diff] [blame] | 385 | LIST_HEAD(uf); |
Al Viro | eb36c58 | 2012-05-30 20:17:35 -0400 | [diff] [blame] | 386 | |
| 387 | ret = security_mmap_file(file, prot, flag); |
| 388 | if (!ret) { |
Michal Hocko | 9fbeb5a | 2016-05-23 16:25:30 -0700 | [diff] [blame] | 389 | if (down_write_killable(&mm->mmap_sem)) |
| 390 | return -EINTR; |
Michel Lespinasse | bebeb3d | 2013-02-22 16:32:37 -0800 | [diff] [blame] | 391 | ret = do_mmap_pgoff(file, addr, len, prot, flag, pgoff, |
Mike Rapoport | 897ab3e | 2017-02-24 14:58:22 -0800 | [diff] [blame] | 392 | &populate, &uf); |
Al Viro | eb36c58 | 2012-05-30 20:17:35 -0400 | [diff] [blame] | 393 | up_write(&mm->mmap_sem); |
Mike Rapoport | 897ab3e | 2017-02-24 14:58:22 -0800 | [diff] [blame] | 394 | userfaultfd_unmap_complete(mm, &uf); |
Michel Lespinasse | 41badc1 | 2013-02-22 16:32:47 -0800 | [diff] [blame] | 395 | if (populate) |
| 396 | mm_populate(ret, populate); |
Al Viro | eb36c58 | 2012-05-30 20:17:35 -0400 | [diff] [blame] | 397 | } |
| 398 | return ret; |
| 399 | } |
| 400 | |
| 401 | unsigned long vm_mmap(struct file *file, unsigned long addr, |
| 402 | unsigned long len, unsigned long prot, |
| 403 | unsigned long flag, unsigned long offset) |
| 404 | { |
| 405 | if (unlikely(offset + PAGE_ALIGN(len) < offset)) |
| 406 | return -EINVAL; |
Alexander Kuleshov | ea53cde | 2015-11-05 18:46:46 -0800 | [diff] [blame] | 407 | if (unlikely(offset_in_page(offset))) |
Al Viro | eb36c58 | 2012-05-30 20:17:35 -0400 | [diff] [blame] | 408 | return -EINVAL; |
| 409 | |
Michal Hocko | 9fbeb5a | 2016-05-23 16:25:30 -0700 | [diff] [blame] | 410 | return vm_mmap_pgoff(file, addr, len, prot, flag, offset >> PAGE_SHIFT); |
Al Viro | eb36c58 | 2012-05-30 20:17:35 -0400 | [diff] [blame] | 411 | } |
| 412 | EXPORT_SYMBOL(vm_mmap); |
| 413 | |
Michal Hocko | a7c3e90 | 2017-05-08 15:57:09 -0700 | [diff] [blame] | 414 | /** |
| 415 | * kvmalloc_node - attempt to allocate physically contiguous memory, but upon |
| 416 | * failure, fall back to non-contiguous (vmalloc) allocation. |
| 417 | * @size: size of the request. |
| 418 | * @flags: gfp mask for the allocation - must be compatible (superset) with GFP_KERNEL. |
| 419 | * @node: numa node to allocate from |
| 420 | * |
| 421 | * Uses kmalloc to get the memory but if the allocation fails then falls back |
| 422 | * to the vmalloc allocator. Use kvfree for freeing the memory. |
| 423 | * |
Michal Hocko | cc965a2 | 2017-07-12 14:36:52 -0700 | [diff] [blame] | 424 | * Reclaim modifiers - __GFP_NORETRY and __GFP_NOFAIL are not supported. |
| 425 | * __GFP_RETRY_MAYFAIL is supported, and it should be used only if kmalloc is |
| 426 | * preferable to the vmalloc fallback, due to visible performance drawbacks. |
Michal Hocko | a7c3e90 | 2017-05-08 15:57:09 -0700 | [diff] [blame] | 427 | * |
Michal Hocko | ce91f6e | 2018-06-07 17:09:40 -0700 | [diff] [blame] | 428 | * Please note that any use of gfp flags outside of GFP_KERNEL is careful to not |
| 429 | * fall back to vmalloc. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 430 | * |
| 431 | * Return: pointer to the allocated memory of %NULL in case of failure |
Michal Hocko | a7c3e90 | 2017-05-08 15:57:09 -0700 | [diff] [blame] | 432 | */ |
| 433 | void *kvmalloc_node(size_t size, gfp_t flags, int node) |
| 434 | { |
| 435 | gfp_t kmalloc_flags = flags; |
| 436 | void *ret; |
| 437 | |
| 438 | /* |
| 439 | * vmalloc uses GFP_KERNEL for some internal allocations (e.g page tables) |
| 440 | * so the given set of flags has to be compatible. |
| 441 | */ |
Michal Hocko | ce91f6e | 2018-06-07 17:09:40 -0700 | [diff] [blame] | 442 | if ((flags & GFP_KERNEL) != GFP_KERNEL) |
| 443 | return kmalloc_node(size, flags, node); |
Michal Hocko | a7c3e90 | 2017-05-08 15:57:09 -0700 | [diff] [blame] | 444 | |
| 445 | /* |
Michal Hocko | 4f4f2ba | 2017-06-02 14:46:19 -0700 | [diff] [blame] | 446 | * We want to attempt a large physically contiguous block first because |
| 447 | * it is less likely to fragment multiple larger blocks and therefore |
| 448 | * contribute to a long term fragmentation less than vmalloc fallback. |
| 449 | * However make sure that larger requests are not too disruptive - no |
| 450 | * OOM killer and no allocation failure warnings as we have a fallback. |
Michal Hocko | a7c3e90 | 2017-05-08 15:57:09 -0700 | [diff] [blame] | 451 | */ |
Michal Hocko | 6c5ab65 | 2017-05-08 15:57:15 -0700 | [diff] [blame] | 452 | if (size > PAGE_SIZE) { |
| 453 | kmalloc_flags |= __GFP_NOWARN; |
| 454 | |
Michal Hocko | cc965a2 | 2017-07-12 14:36:52 -0700 | [diff] [blame] | 455 | if (!(kmalloc_flags & __GFP_RETRY_MAYFAIL)) |
Michal Hocko | 6c5ab65 | 2017-05-08 15:57:15 -0700 | [diff] [blame] | 456 | kmalloc_flags |= __GFP_NORETRY; |
| 457 | } |
Michal Hocko | a7c3e90 | 2017-05-08 15:57:09 -0700 | [diff] [blame] | 458 | |
| 459 | ret = kmalloc_node(size, kmalloc_flags, node); |
| 460 | |
| 461 | /* |
| 462 | * It doesn't really make sense to fallback to vmalloc for sub page |
| 463 | * requests |
| 464 | */ |
| 465 | if (ret || size <= PAGE_SIZE) |
| 466 | return ret; |
| 467 | |
Michal Hocko | 8594a21 | 2017-05-12 15:46:41 -0700 | [diff] [blame] | 468 | return __vmalloc_node_flags_caller(size, node, flags, |
| 469 | __builtin_return_address(0)); |
Michal Hocko | a7c3e90 | 2017-05-08 15:57:09 -0700 | [diff] [blame] | 470 | } |
| 471 | EXPORT_SYMBOL(kvmalloc_node); |
| 472 | |
Mike Rapoport | ff4dc77 | 2018-08-23 17:01:02 -0700 | [diff] [blame] | 473 | /** |
Andrew Morton | 04b8e94 | 2018-09-04 15:45:55 -0700 | [diff] [blame] | 474 | * kvfree() - Free memory. |
| 475 | * @addr: Pointer to allocated memory. |
Mike Rapoport | ff4dc77 | 2018-08-23 17:01:02 -0700 | [diff] [blame] | 476 | * |
Andrew Morton | 04b8e94 | 2018-09-04 15:45:55 -0700 | [diff] [blame] | 477 | * kvfree frees memory allocated by any of vmalloc(), kmalloc() or kvmalloc(). |
| 478 | * It is slightly more efficient to use kfree() or vfree() if you are certain |
| 479 | * that you know which one to use. |
| 480 | * |
Andrey Ryabinin | 52414d3 | 2018-10-26 15:07:00 -0700 | [diff] [blame] | 481 | * Context: Either preemptible task context or not-NMI interrupt. |
Mike Rapoport | ff4dc77 | 2018-08-23 17:01:02 -0700 | [diff] [blame] | 482 | */ |
Al Viro | 39f1f78 | 2014-05-06 14:02:53 -0400 | [diff] [blame] | 483 | void kvfree(const void *addr) |
| 484 | { |
| 485 | if (is_vmalloc_addr(addr)) |
| 486 | vfree(addr); |
| 487 | else |
| 488 | kfree(addr); |
| 489 | } |
| 490 | EXPORT_SYMBOL(kvfree); |
| 491 | |
Kirill A. Shutemov | e39155e | 2015-04-15 16:14:53 -0700 | [diff] [blame] | 492 | static inline void *__page_rmapping(struct page *page) |
| 493 | { |
| 494 | unsigned long mapping; |
| 495 | |
| 496 | mapping = (unsigned long)page->mapping; |
| 497 | mapping &= ~PAGE_MAPPING_FLAGS; |
| 498 | |
| 499 | return (void *)mapping; |
| 500 | } |
| 501 | |
| 502 | /* Neutral page->mapping pointer to address_space or anon_vma or other */ |
| 503 | void *page_rmapping(struct page *page) |
| 504 | { |
| 505 | page = compound_head(page); |
| 506 | return __page_rmapping(page); |
| 507 | } |
| 508 | |
Andrew Morton | 1aa8aea | 2016-05-19 17:12:00 -0700 | [diff] [blame] | 509 | /* |
| 510 | * Return true if this page is mapped into pagetables. |
| 511 | * For compound page it returns true if any subpage of compound page is mapped. |
| 512 | */ |
| 513 | bool page_mapped(struct page *page) |
| 514 | { |
| 515 | int i; |
| 516 | |
| 517 | if (likely(!PageCompound(page))) |
| 518 | return atomic_read(&page->_mapcount) >= 0; |
| 519 | page = compound_head(page); |
| 520 | if (atomic_read(compound_mapcount_ptr(page)) >= 0) |
| 521 | return true; |
| 522 | if (PageHuge(page)) |
| 523 | return false; |
Matthew Wilcox (Oracle) | d8c6546 | 2019-09-23 15:34:30 -0700 | [diff] [blame] | 524 | for (i = 0; i < compound_nr(page); i++) { |
Andrew Morton | 1aa8aea | 2016-05-19 17:12:00 -0700 | [diff] [blame] | 525 | if (atomic_read(&page[i]._mapcount) >= 0) |
| 526 | return true; |
| 527 | } |
| 528 | return false; |
| 529 | } |
| 530 | EXPORT_SYMBOL(page_mapped); |
| 531 | |
Kirill A. Shutemov | e39155e | 2015-04-15 16:14:53 -0700 | [diff] [blame] | 532 | struct anon_vma *page_anon_vma(struct page *page) |
| 533 | { |
| 534 | unsigned long mapping; |
| 535 | |
| 536 | page = compound_head(page); |
| 537 | mapping = (unsigned long)page->mapping; |
| 538 | if ((mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON) |
| 539 | return NULL; |
| 540 | return __page_rmapping(page); |
| 541 | } |
| 542 | |
Shaohua Li | 9800339 | 2013-02-22 16:34:35 -0800 | [diff] [blame] | 543 | struct address_space *page_mapping(struct page *page) |
| 544 | { |
Kirill A. Shutemov | 1c290f6 | 2016-01-15 16:52:07 -0800 | [diff] [blame] | 545 | struct address_space *mapping; |
| 546 | |
| 547 | page = compound_head(page); |
Shaohua Li | 9800339 | 2013-02-22 16:34:35 -0800 | [diff] [blame] | 548 | |
Mikulas Patocka | 03e5ac2 | 2014-01-14 17:56:40 -0800 | [diff] [blame] | 549 | /* This happens if someone calls flush_dcache_page on slab page */ |
| 550 | if (unlikely(PageSlab(page))) |
| 551 | return NULL; |
| 552 | |
Shaohua Li | 33806f0 | 2013-02-22 16:34:37 -0800 | [diff] [blame] | 553 | if (unlikely(PageSwapCache(page))) { |
| 554 | swp_entry_t entry; |
| 555 | |
| 556 | entry.val = page_private(page); |
Kirill A. Shutemov | e39155e | 2015-04-15 16:14:53 -0700 | [diff] [blame] | 557 | return swap_address_space(entry); |
| 558 | } |
| 559 | |
Kirill A. Shutemov | 1c290f6 | 2016-01-15 16:52:07 -0800 | [diff] [blame] | 560 | mapping = page->mapping; |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 561 | if ((unsigned long)mapping & PAGE_MAPPING_ANON) |
Kirill A. Shutemov | e39155e | 2015-04-15 16:14:53 -0700 | [diff] [blame] | 562 | return NULL; |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 563 | |
| 564 | return (void *)((unsigned long)mapping & ~PAGE_MAPPING_FLAGS); |
Shaohua Li | 9800339 | 2013-02-22 16:34:35 -0800 | [diff] [blame] | 565 | } |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 566 | EXPORT_SYMBOL(page_mapping); |
Shaohua Li | 9800339 | 2013-02-22 16:34:35 -0800 | [diff] [blame] | 567 | |
Huang Ying | cb9f753 | 2018-04-05 16:24:39 -0700 | [diff] [blame] | 568 | /* |
| 569 | * For file cache pages, return the address_space, otherwise return NULL |
| 570 | */ |
| 571 | struct address_space *page_mapping_file(struct page *page) |
| 572 | { |
| 573 | if (unlikely(PageSwapCache(page))) |
| 574 | return NULL; |
| 575 | return page_mapping(page); |
| 576 | } |
| 577 | |
Kirill A. Shutemov | b20ce5e | 2016-01-15 16:54:37 -0800 | [diff] [blame] | 578 | /* Slow path of page_mapcount() for compound pages */ |
| 579 | int __page_mapcount(struct page *page) |
| 580 | { |
| 581 | int ret; |
| 582 | |
| 583 | ret = atomic_read(&page->_mapcount) + 1; |
Kirill A. Shutemov | dd78fed | 2016-07-26 15:25:26 -0700 | [diff] [blame] | 584 | /* |
| 585 | * For file THP page->_mapcount contains total number of mapping |
| 586 | * of the page: no need to look into compound_mapcount. |
| 587 | */ |
| 588 | if (!PageAnon(page) && !PageHuge(page)) |
| 589 | return ret; |
Kirill A. Shutemov | b20ce5e | 2016-01-15 16:54:37 -0800 | [diff] [blame] | 590 | page = compound_head(page); |
| 591 | ret += atomic_read(compound_mapcount_ptr(page)) + 1; |
| 592 | if (PageDoubleMap(page)) |
| 593 | ret--; |
| 594 | return ret; |
| 595 | } |
| 596 | EXPORT_SYMBOL_GPL(__page_mapcount); |
| 597 | |
Andrey Ryabinin | 39a1aa8 | 2016-03-17 14:18:50 -0700 | [diff] [blame] | 598 | int sysctl_overcommit_memory __read_mostly = OVERCOMMIT_GUESS; |
| 599 | int sysctl_overcommit_ratio __read_mostly = 50; |
| 600 | unsigned long sysctl_overcommit_kbytes __read_mostly; |
| 601 | int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT; |
| 602 | unsigned long sysctl_user_reserve_kbytes __read_mostly = 1UL << 17; /* 128MB */ |
| 603 | unsigned long sysctl_admin_reserve_kbytes __read_mostly = 1UL << 13; /* 8MB */ |
| 604 | |
Jerome Marchand | 49f0ce5 | 2014-01-21 15:49:14 -0800 | [diff] [blame] | 605 | int overcommit_ratio_handler(struct ctl_table *table, int write, |
| 606 | void __user *buffer, size_t *lenp, |
| 607 | loff_t *ppos) |
| 608 | { |
| 609 | int ret; |
| 610 | |
| 611 | ret = proc_dointvec(table, write, buffer, lenp, ppos); |
| 612 | if (ret == 0 && write) |
| 613 | sysctl_overcommit_kbytes = 0; |
| 614 | return ret; |
| 615 | } |
| 616 | |
| 617 | int overcommit_kbytes_handler(struct ctl_table *table, int write, |
| 618 | void __user *buffer, size_t *lenp, |
| 619 | loff_t *ppos) |
| 620 | { |
| 621 | int ret; |
| 622 | |
| 623 | ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos); |
| 624 | if (ret == 0 && write) |
| 625 | sysctl_overcommit_ratio = 0; |
| 626 | return ret; |
| 627 | } |
| 628 | |
Jerome Marchand | 00619bc | 2013-11-12 15:08:31 -0800 | [diff] [blame] | 629 | /* |
| 630 | * Committed memory limit enforced when OVERCOMMIT_NEVER policy is used |
| 631 | */ |
| 632 | unsigned long vm_commit_limit(void) |
| 633 | { |
Jerome Marchand | 49f0ce5 | 2014-01-21 15:49:14 -0800 | [diff] [blame] | 634 | unsigned long allowed; |
| 635 | |
| 636 | if (sysctl_overcommit_kbytes) |
| 637 | allowed = sysctl_overcommit_kbytes >> (PAGE_SHIFT - 10); |
| 638 | else |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame] | 639 | allowed = ((totalram_pages() - hugetlb_total_pages()) |
Jerome Marchand | 49f0ce5 | 2014-01-21 15:49:14 -0800 | [diff] [blame] | 640 | * sysctl_overcommit_ratio / 100); |
| 641 | allowed += total_swap_pages; |
| 642 | |
| 643 | return allowed; |
Jerome Marchand | 00619bc | 2013-11-12 15:08:31 -0800 | [diff] [blame] | 644 | } |
| 645 | |
Andrey Ryabinin | 39a1aa8 | 2016-03-17 14:18:50 -0700 | [diff] [blame] | 646 | /* |
| 647 | * Make sure vm_committed_as in one cacheline and not cacheline shared with |
| 648 | * other variables. It can be updated by several CPUs frequently. |
| 649 | */ |
| 650 | struct percpu_counter vm_committed_as ____cacheline_aligned_in_smp; |
| 651 | |
| 652 | /* |
| 653 | * The global memory commitment made in the system can be a metric |
| 654 | * that can be used to drive ballooning decisions when Linux is hosted |
| 655 | * as a guest. On Hyper-V, the host implements a policy engine for dynamically |
| 656 | * balancing memory across competing virtual machines that are hosted. |
| 657 | * Several metrics drive this policy engine including the guest reported |
| 658 | * memory commitment. |
| 659 | */ |
| 660 | unsigned long vm_memory_committed(void) |
| 661 | { |
| 662 | return percpu_counter_read_positive(&vm_committed_as); |
| 663 | } |
| 664 | EXPORT_SYMBOL_GPL(vm_memory_committed); |
| 665 | |
| 666 | /* |
| 667 | * Check that a process has enough memory to allocate a new virtual |
| 668 | * mapping. 0 means there is enough memory for the allocation to |
| 669 | * succeed and -ENOMEM implies there is not. |
| 670 | * |
| 671 | * We currently support three overcommit policies, which are set via the |
Mike Rapoport | ad56b73 | 2018-03-21 21:22:47 +0200 | [diff] [blame] | 672 | * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting.rst |
Andrey Ryabinin | 39a1aa8 | 2016-03-17 14:18:50 -0700 | [diff] [blame] | 673 | * |
| 674 | * Strict overcommit modes added 2002 Feb 26 by Alan Cox. |
| 675 | * Additional code 2002 Jul 20 by Robert Love. |
| 676 | * |
| 677 | * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise. |
| 678 | * |
| 679 | * Note this is a helper function intended to be used by LSMs which |
| 680 | * wish to use this logic. |
| 681 | */ |
| 682 | int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin) |
| 683 | { |
Johannes Weiner | 8c7829b | 2019-05-13 17:21:50 -0700 | [diff] [blame] | 684 | long allowed; |
Andrey Ryabinin | 39a1aa8 | 2016-03-17 14:18:50 -0700 | [diff] [blame] | 685 | |
| 686 | VM_WARN_ONCE(percpu_counter_read(&vm_committed_as) < |
| 687 | -(s64)vm_committed_as_batch * num_online_cpus(), |
| 688 | "memory commitment underflow"); |
| 689 | |
| 690 | vm_acct_memory(pages); |
| 691 | |
| 692 | /* |
| 693 | * Sometimes we want to use more memory than we have |
| 694 | */ |
| 695 | if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS) |
| 696 | return 0; |
| 697 | |
| 698 | if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) { |
Johannes Weiner | 8c7829b | 2019-05-13 17:21:50 -0700 | [diff] [blame] | 699 | if (pages > totalram_pages() + total_swap_pages) |
Andrey Ryabinin | 39a1aa8 | 2016-03-17 14:18:50 -0700 | [diff] [blame] | 700 | goto error; |
Johannes Weiner | 8c7829b | 2019-05-13 17:21:50 -0700 | [diff] [blame] | 701 | return 0; |
Andrey Ryabinin | 39a1aa8 | 2016-03-17 14:18:50 -0700 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | allowed = vm_commit_limit(); |
| 705 | /* |
| 706 | * Reserve some for root |
| 707 | */ |
| 708 | if (!cap_sys_admin) |
| 709 | allowed -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10); |
| 710 | |
| 711 | /* |
| 712 | * Don't let a single process grow so big a user can't recover |
| 713 | */ |
| 714 | if (mm) { |
Johannes Weiner | 8c7829b | 2019-05-13 17:21:50 -0700 | [diff] [blame] | 715 | long reserve = sysctl_user_reserve_kbytes >> (PAGE_SHIFT - 10); |
| 716 | |
Andrey Ryabinin | 39a1aa8 | 2016-03-17 14:18:50 -0700 | [diff] [blame] | 717 | allowed -= min_t(long, mm->total_vm / 32, reserve); |
| 718 | } |
| 719 | |
| 720 | if (percpu_counter_read_positive(&vm_committed_as) < allowed) |
| 721 | return 0; |
| 722 | error: |
| 723 | vm_unacct_memory(pages); |
| 724 | |
| 725 | return -ENOMEM; |
| 726 | } |
| 727 | |
William Roberts | a909025 | 2014-02-11 10:11:59 -0800 | [diff] [blame] | 728 | /** |
| 729 | * get_cmdline() - copy the cmdline value to a buffer. |
| 730 | * @task: the task whose cmdline value to copy. |
| 731 | * @buffer: the buffer to copy to. |
| 732 | * @buflen: the length of the buffer. Larger cmdline values are truncated |
| 733 | * to this length. |
Mike Rapoport | a862f68 | 2019-03-05 15:48:42 -0800 | [diff] [blame] | 734 | * |
| 735 | * Return: the size of the cmdline field copied. Note that the copy does |
William Roberts | a909025 | 2014-02-11 10:11:59 -0800 | [diff] [blame] | 736 | * not guarantee an ending NULL byte. |
| 737 | */ |
| 738 | int get_cmdline(struct task_struct *task, char *buffer, int buflen) |
| 739 | { |
| 740 | int res = 0; |
| 741 | unsigned int len; |
| 742 | struct mm_struct *mm = get_task_mm(task); |
Mateusz Guzik | a3b609e | 2016-01-20 15:01:05 -0800 | [diff] [blame] | 743 | unsigned long arg_start, arg_end, env_start, env_end; |
William Roberts | a909025 | 2014-02-11 10:11:59 -0800 | [diff] [blame] | 744 | if (!mm) |
| 745 | goto out; |
| 746 | if (!mm->arg_end) |
| 747 | goto out_mm; /* Shh! No looking before we're done */ |
| 748 | |
Michal Koutný | bc81426 | 2019-05-31 22:30:19 -0700 | [diff] [blame] | 749 | spin_lock(&mm->arg_lock); |
Mateusz Guzik | a3b609e | 2016-01-20 15:01:05 -0800 | [diff] [blame] | 750 | arg_start = mm->arg_start; |
| 751 | arg_end = mm->arg_end; |
| 752 | env_start = mm->env_start; |
| 753 | env_end = mm->env_end; |
Michal Koutný | bc81426 | 2019-05-31 22:30:19 -0700 | [diff] [blame] | 754 | spin_unlock(&mm->arg_lock); |
Mateusz Guzik | a3b609e | 2016-01-20 15:01:05 -0800 | [diff] [blame] | 755 | |
| 756 | len = arg_end - arg_start; |
William Roberts | a909025 | 2014-02-11 10:11:59 -0800 | [diff] [blame] | 757 | |
| 758 | if (len > buflen) |
| 759 | len = buflen; |
| 760 | |
Lorenzo Stoakes | f307ab6 | 2016-10-13 01:20:20 +0100 | [diff] [blame] | 761 | res = access_process_vm(task, arg_start, buffer, len, FOLL_FORCE); |
William Roberts | a909025 | 2014-02-11 10:11:59 -0800 | [diff] [blame] | 762 | |
| 763 | /* |
| 764 | * If the nul at the end of args has been overwritten, then |
| 765 | * assume application is using setproctitle(3). |
| 766 | */ |
| 767 | if (res > 0 && buffer[res-1] != '\0' && len < buflen) { |
| 768 | len = strnlen(buffer, res); |
| 769 | if (len < res) { |
| 770 | res = len; |
| 771 | } else { |
Mateusz Guzik | a3b609e | 2016-01-20 15:01:05 -0800 | [diff] [blame] | 772 | len = env_end - env_start; |
William Roberts | a909025 | 2014-02-11 10:11:59 -0800 | [diff] [blame] | 773 | if (len > buflen - res) |
| 774 | len = buflen - res; |
Mateusz Guzik | a3b609e | 2016-01-20 15:01:05 -0800 | [diff] [blame] | 775 | res += access_process_vm(task, env_start, |
Lorenzo Stoakes | f307ab6 | 2016-10-13 01:20:20 +0100 | [diff] [blame] | 776 | buffer+res, len, |
| 777 | FOLL_FORCE); |
William Roberts | a909025 | 2014-02-11 10:11:59 -0800 | [diff] [blame] | 778 | res = strnlen(buffer, res); |
| 779 | } |
| 780 | } |
| 781 | out_mm: |
| 782 | mmput(mm); |
| 783 | out: |
| 784 | return res; |
| 785 | } |
Song Liu | 010c164 | 2019-09-23 15:38:19 -0700 | [diff] [blame^] | 786 | |
| 787 | int memcmp_pages(struct page *page1, struct page *page2) |
| 788 | { |
| 789 | char *addr1, *addr2; |
| 790 | int ret; |
| 791 | |
| 792 | addr1 = kmap_atomic(page1); |
| 793 | addr2 = kmap_atomic(page2); |
| 794 | ret = memcmp(addr1, addr2, PAGE_SIZE); |
| 795 | kunmap_atomic(addr2); |
| 796 | kunmap_atomic(addr1); |
| 797 | return ret; |
| 798 | } |