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