blob: 37f7b6711514c7bc25fca683cd86f13fc6310b0f [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Andrew Morton16d69262008-07-25 19:44:36 -07002#include <linux/mm.h>
Matt Mackall30992c92006-01-08 01:01:43 -08003#include <linux/slab.h>
4#include <linux/string.h>
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -07005#include <linux/compiler.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -04006#include <linux/export.h>
Davi Arnaut96840aa2006-03-24 03:18:42 -08007#include <linux/err.h>
Adrian Bunk3b8f14b2008-07-26 15:22:28 -07008#include <linux/sched.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +01009#include <linux/sched/mm.h>
Daniel Jordan79eb5972019-07-16 16:30:54 -070010#include <linux/sched/signal.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010011#include <linux/sched/task_stack.h>
Al Viroeb36c582012-05-30 20:17:35 -040012#include <linux/security.h>
Shaohua Li98003392013-02-22 16:34:35 -080013#include <linux/swap.h>
Shaohua Li33806f02013-02-22 16:34:37 -080014#include <linux/swapops.h>
Jerome Marchand00619bc2013-11-12 15:08:31 -080015#include <linux/mman.h>
16#include <linux/hugetlb.h>
Al Viro39f1f782014-05-06 14:02:53 -040017#include <linux/vmalloc.h>
Mike Rapoport897ab3e2017-02-24 14:58:22 -080018#include <linux/userfaultfd_k.h>
Jerome Marchand00619bc2013-11-12 15:08:31 -080019
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080020#include <linux/uaccess.h>
Matt Mackall30992c92006-01-08 01:01:43 -080021
Namhyung Kim6038def2011-05-24 17:11:22 -070022#include "internal.h"
23
Andrzej Hajdaa4bb1e42015-02-13 14:36:24 -080024/**
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 */
30void kfree_const(const void *x)
31{
32 if (!is_kernel_rodata((unsigned long)x))
33 kfree(x);
34}
35EXPORT_SYMBOL(kfree_const);
36
Matt Mackall30992c92006-01-08 01:01:43 -080037/**
Matt Mackall30992c92006-01-08 01:01:43 -080038 * kstrdup - allocate space for and copy an existing string
Matt Mackall30992c92006-01-08 01:01:43 -080039 * @s: the string to duplicate
40 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
Mike Rapoporta862f682019-03-05 15:48:42 -080041 *
42 * Return: newly allocated copy of @s or %NULL in case of error
Matt Mackall30992c92006-01-08 01:01:43 -080043 */
44char *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 Hellwig1d2c8ee2006-10-04 02:15:25 -070053 buf = kmalloc_track_caller(len, gfp);
Matt Mackall30992c92006-01-08 01:01:43 -080054 if (buf)
55 memcpy(buf, s, len);
56 return buf;
57}
58EXPORT_SYMBOL(kstrdup);
Davi Arnaut96840aa2006-03-24 03:18:42 -080059
Alexey Dobriyan1a2f67b2006-09-30 23:27:20 -070060/**
Andrzej Hajdaa4bb1e42015-02-13 14:36:24 -080061 * 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 Rapoporta862f682019-03-05 15:48:42 -080065 * 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 Hajdaa4bb1e42015-02-13 14:36:24 -080069 */
70const 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}
77EXPORT_SYMBOL(kstrdup_const);
78
79/**
Jeremy Fitzhardinge1e66df32007-07-17 18:37:02 -070080 * 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 Howellsf3515742017-07-04 17:25:02 +010084 *
85 * Note: Use kmemdup_nul() instead if the size is known exactly.
Mike Rapoporta862f682019-03-05 15:48:42 -080086 *
87 * Return: newly allocated copy of @s or %NULL in case of error
Jeremy Fitzhardinge1e66df32007-07-17 18:37:02 -070088 */
89char *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}
105EXPORT_SYMBOL(kstrndup);
106
107/**
Alexey Dobriyan1a2f67b2006-09-30 23:27:20 -0700108 * kmemdup - duplicate region of memory
109 *
110 * @src: memory region to duplicate
111 * @len: memory region length
112 * @gfp: GFP mask to use
Mike Rapoporta862f682019-03-05 15:48:42 -0800113 *
114 * Return: newly allocated copy of @src or %NULL in case of error
Alexey Dobriyan1a2f67b2006-09-30 23:27:20 -0700115 */
116void *kmemdup(const void *src, size_t len, gfp_t gfp)
117{
118 void *p;
119
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -0700120 p = kmalloc_track_caller(len, gfp);
Alexey Dobriyan1a2f67b2006-09-30 23:27:20 -0700121 if (p)
122 memcpy(p, src, len);
123 return p;
124}
125EXPORT_SYMBOL(kmemdup);
126
Christoph Lameteref2ad802007-07-17 04:03:21 -0700127/**
David Howellsf3515742017-07-04 17:25:02 +0100128 * 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 Rapoporta862f682019-03-05 15:48:42 -0800132 *
133 * Return: newly allocated copy of @s with NUL-termination or %NULL in
134 * case of error
David Howellsf3515742017-07-04 17:25:02 +0100135 */
136char *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}
150EXPORT_SYMBOL(kmemdup_nul);
151
152/**
Li Zefan610a77e2009-03-31 15:23:16 -0700153 * 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 Rapoporta862f682019-03-05 15:48:42 -0800158 * Return: an ERR_PTR() on failure. Result is physically
Al Viro50fd2f22018-01-07 13:06:15 -0500159 * contiguous, to be freed by kfree().
Li Zefan610a77e2009-03-31 15:23:16 -0700160 */
161void *memdup_user(const void __user *src, size_t len)
162{
163 void *p;
164
Daniel Vetter6c8fcc02019-02-20 22:20:42 -0800165 p = kmalloc_track_caller(len, GFP_USER | __GFP_NOWARN);
Li Zefan610a77e2009-03-31 15:23:16 -0700166 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}
176EXPORT_SYMBOL(memdup_user);
177
Al Viro50fd2f22018-01-07 13:06:15 -0500178/**
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 Rapoporta862f682019-03-05 15:48:42 -0800184 * Return: an ERR_PTR() on failure. Result may be not
Al Viro50fd2f22018-01-07 13:06:15 -0500185 * physically contiguous. Use kvfree() to free.
186 */
187void *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}
202EXPORT_SYMBOL(vmemdup_user);
203
Mike Rapoportb86181f2018-08-23 17:00:59 -0700204/**
Davi Arnaut96840aa2006-03-24 03:18:42 -0800205 * strndup_user - duplicate an existing string from user space
Davi Arnaut96840aa2006-03-24 03:18:42 -0800206 * @s: The string to duplicate
207 * @n: Maximum number of bytes to copy, including the trailing NUL.
Mike Rapoporta862f682019-03-05 15:48:42 -0800208 *
Andrew Mortone9145522019-04-05 18:39:34 -0700209 * Return: newly allocated copy of @s or an ERR_PTR() in case of error
Davi Arnaut96840aa2006-03-24 03:18:42 -0800210 */
211char *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 Lawall90d74042010-08-09 17:18:26 -0700224 p = memdup_user(s, length);
Davi Arnaut96840aa2006-03-24 03:18:42 -0800225
Julia Lawall90d74042010-08-09 17:18:26 -0700226 if (IS_ERR(p))
227 return p;
Davi Arnaut96840aa2006-03-24 03:18:42 -0800228
229 p[length - 1] = '\0';
230
231 return p;
232}
233EXPORT_SYMBOL(strndup_user);
Andrew Morton16d69262008-07-25 19:44:36 -0700234
Al Viroe9d408e2015-12-24 00:06:05 -0500235/**
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 Rapoporta862f682019-03-05 15:48:42 -0800241 * Return: an ERR_PTR() on failure.
Al Viroe9d408e2015-12-24 00:06:05 -0500242 */
243void *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}
264EXPORT_SYMBOL(memdup_user_nul);
265
Namhyung Kim6038def2011-05-24 17:11:22 -0700266void __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 Poyarekarb7643752012-03-21 16:34:04 -0700288/* Check if the vma is being used as a stack by this task */
Andy Lutomirskid17af502016-09-30 10:58:58 -0700289int vma_is_stack_for_current(struct vm_area_struct *vma)
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700290{
Andy Lutomirskid17af502016-09-30 10:58:58 -0700291 struct task_struct * __maybe_unused t = current;
292
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700293 return (vma->vm_start <= KSTK_ESP(t) && vma->vm_end >= KSTK_ESP(t));
294}
295
David Howellsefc1a3b2010-01-15 17:01:35 -0800296#if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT)
Kees Cook8f2af152018-04-10 16:34:53 -0700297void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
Andrew Morton16d69262008-07-25 19:44:36 -0700298{
299 mm->mmap_base = TASK_UNMAPPED_BASE;
300 mm->get_unmapped_area = arch_get_unmapped_area;
Andrew Morton16d69262008-07-25 19:44:36 -0700301}
302#endif
Rusty Russell912985d2008-08-12 17:52:52 -0500303
Daniel Jordan79eb5972019-07-16 16:30:54 -0700304/**
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 */
319int __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}
348EXPORT_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 */
362int 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}
376EXPORT_SYMBOL_GPL(account_locked_vm);
377
Al Viroeb36c582012-05-30 20:17:35 -0400378unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr,
379 unsigned long len, unsigned long prot,
Michal Hocko9fbeb5a2016-05-23 16:25:30 -0700380 unsigned long flag, unsigned long pgoff)
Al Viroeb36c582012-05-30 20:17:35 -0400381{
382 unsigned long ret;
383 struct mm_struct *mm = current->mm;
Michel Lespinasse41badc12013-02-22 16:32:47 -0800384 unsigned long populate;
Mike Rapoport897ab3e2017-02-24 14:58:22 -0800385 LIST_HEAD(uf);
Al Viroeb36c582012-05-30 20:17:35 -0400386
387 ret = security_mmap_file(file, prot, flag);
388 if (!ret) {
Michal Hocko9fbeb5a2016-05-23 16:25:30 -0700389 if (down_write_killable(&mm->mmap_sem))
390 return -EINTR;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -0800391 ret = do_mmap_pgoff(file, addr, len, prot, flag, pgoff,
Mike Rapoport897ab3e2017-02-24 14:58:22 -0800392 &populate, &uf);
Al Viroeb36c582012-05-30 20:17:35 -0400393 up_write(&mm->mmap_sem);
Mike Rapoport897ab3e2017-02-24 14:58:22 -0800394 userfaultfd_unmap_complete(mm, &uf);
Michel Lespinasse41badc12013-02-22 16:32:47 -0800395 if (populate)
396 mm_populate(ret, populate);
Al Viroeb36c582012-05-30 20:17:35 -0400397 }
398 return ret;
399}
400
401unsigned 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 Kuleshovea53cde2015-11-05 18:46:46 -0800407 if (unlikely(offset_in_page(offset)))
Al Viroeb36c582012-05-30 20:17:35 -0400408 return -EINVAL;
409
Michal Hocko9fbeb5a2016-05-23 16:25:30 -0700410 return vm_mmap_pgoff(file, addr, len, prot, flag, offset >> PAGE_SHIFT);
Al Viroeb36c582012-05-30 20:17:35 -0400411}
412EXPORT_SYMBOL(vm_mmap);
413
Michal Hockoa7c3e902017-05-08 15:57:09 -0700414/**
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 Hockocc965a22017-07-12 14:36:52 -0700424 * 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 Hockoa7c3e902017-05-08 15:57:09 -0700427 *
Michal Hockoce91f6e2018-06-07 17:09:40 -0700428 * Please note that any use of gfp flags outside of GFP_KERNEL is careful to not
429 * fall back to vmalloc.
Mike Rapoporta862f682019-03-05 15:48:42 -0800430 *
431 * Return: pointer to the allocated memory of %NULL in case of failure
Michal Hockoa7c3e902017-05-08 15:57:09 -0700432 */
433void *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 Hockoce91f6e2018-06-07 17:09:40 -0700442 if ((flags & GFP_KERNEL) != GFP_KERNEL)
443 return kmalloc_node(size, flags, node);
Michal Hockoa7c3e902017-05-08 15:57:09 -0700444
445 /*
Michal Hocko4f4f2ba2017-06-02 14:46:19 -0700446 * 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 Hockoa7c3e902017-05-08 15:57:09 -0700451 */
Michal Hocko6c5ab652017-05-08 15:57:15 -0700452 if (size > PAGE_SIZE) {
453 kmalloc_flags |= __GFP_NOWARN;
454
Michal Hockocc965a22017-07-12 14:36:52 -0700455 if (!(kmalloc_flags & __GFP_RETRY_MAYFAIL))
Michal Hocko6c5ab652017-05-08 15:57:15 -0700456 kmalloc_flags |= __GFP_NORETRY;
457 }
Michal Hockoa7c3e902017-05-08 15:57:09 -0700458
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 Hocko8594a212017-05-12 15:46:41 -0700468 return __vmalloc_node_flags_caller(size, node, flags,
469 __builtin_return_address(0));
Michal Hockoa7c3e902017-05-08 15:57:09 -0700470}
471EXPORT_SYMBOL(kvmalloc_node);
472
Mike Rapoportff4dc772018-08-23 17:01:02 -0700473/**
Andrew Morton04b8e942018-09-04 15:45:55 -0700474 * kvfree() - Free memory.
475 * @addr: Pointer to allocated memory.
Mike Rapoportff4dc772018-08-23 17:01:02 -0700476 *
Andrew Morton04b8e942018-09-04 15:45:55 -0700477 * 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 Ryabinin52414d32018-10-26 15:07:00 -0700481 * Context: Either preemptible task context or not-NMI interrupt.
Mike Rapoportff4dc772018-08-23 17:01:02 -0700482 */
Al Viro39f1f782014-05-06 14:02:53 -0400483void kvfree(const void *addr)
484{
485 if (is_vmalloc_addr(addr))
486 vfree(addr);
487 else
488 kfree(addr);
489}
490EXPORT_SYMBOL(kvfree);
491
Kirill A. Shutemove39155e2015-04-15 16:14:53 -0700492static 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 */
503void *page_rmapping(struct page *page)
504{
505 page = compound_head(page);
506 return __page_rmapping(page);
507}
508
Andrew Morton1aa8aea2016-05-19 17:12:00 -0700509/*
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 */
513bool 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)d8c65462019-09-23 15:34:30 -0700524 for (i = 0; i < compound_nr(page); i++) {
Andrew Morton1aa8aea2016-05-19 17:12:00 -0700525 if (atomic_read(&page[i]._mapcount) >= 0)
526 return true;
527 }
528 return false;
529}
530EXPORT_SYMBOL(page_mapped);
531
Kirill A. Shutemove39155e2015-04-15 16:14:53 -0700532struct 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 Li98003392013-02-22 16:34:35 -0800543struct address_space *page_mapping(struct page *page)
544{
Kirill A. Shutemov1c290f62016-01-15 16:52:07 -0800545 struct address_space *mapping;
546
547 page = compound_head(page);
Shaohua Li98003392013-02-22 16:34:35 -0800548
Mikulas Patocka03e5ac22014-01-14 17:56:40 -0800549 /* This happens if someone calls flush_dcache_page on slab page */
550 if (unlikely(PageSlab(page)))
551 return NULL;
552
Shaohua Li33806f02013-02-22 16:34:37 -0800553 if (unlikely(PageSwapCache(page))) {
554 swp_entry_t entry;
555
556 entry.val = page_private(page);
Kirill A. Shutemove39155e2015-04-15 16:14:53 -0700557 return swap_address_space(entry);
558 }
559
Kirill A. Shutemov1c290f62016-01-15 16:52:07 -0800560 mapping = page->mapping;
Minchan Kimbda807d2016-07-26 15:23:05 -0700561 if ((unsigned long)mapping & PAGE_MAPPING_ANON)
Kirill A. Shutemove39155e2015-04-15 16:14:53 -0700562 return NULL;
Minchan Kimbda807d2016-07-26 15:23:05 -0700563
564 return (void *)((unsigned long)mapping & ~PAGE_MAPPING_FLAGS);
Shaohua Li98003392013-02-22 16:34:35 -0800565}
Minchan Kimbda807d2016-07-26 15:23:05 -0700566EXPORT_SYMBOL(page_mapping);
Shaohua Li98003392013-02-22 16:34:35 -0800567
Huang Yingcb9f7532018-04-05 16:24:39 -0700568/*
569 * For file cache pages, return the address_space, otherwise return NULL
570 */
571struct 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. Shutemovb20ce5e2016-01-15 16:54:37 -0800578/* Slow path of page_mapcount() for compound pages */
579int __page_mapcount(struct page *page)
580{
581 int ret;
582
583 ret = atomic_read(&page->_mapcount) + 1;
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -0700584 /*
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. Shutemovb20ce5e2016-01-15 16:54:37 -0800590 page = compound_head(page);
591 ret += atomic_read(compound_mapcount_ptr(page)) + 1;
592 if (PageDoubleMap(page))
593 ret--;
594 return ret;
595}
596EXPORT_SYMBOL_GPL(__page_mapcount);
597
Andrey Ryabinin39a1aa82016-03-17 14:18:50 -0700598int sysctl_overcommit_memory __read_mostly = OVERCOMMIT_GUESS;
599int sysctl_overcommit_ratio __read_mostly = 50;
600unsigned long sysctl_overcommit_kbytes __read_mostly;
601int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT;
602unsigned long sysctl_user_reserve_kbytes __read_mostly = 1UL << 17; /* 128MB */
603unsigned long sysctl_admin_reserve_kbytes __read_mostly = 1UL << 13; /* 8MB */
604
Jerome Marchand49f0ce52014-01-21 15:49:14 -0800605int 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
617int 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 Marchand00619bc2013-11-12 15:08:31 -0800629/*
630 * Committed memory limit enforced when OVERCOMMIT_NEVER policy is used
631 */
632unsigned long vm_commit_limit(void)
633{
Jerome Marchand49f0ce52014-01-21 15:49:14 -0800634 unsigned long allowed;
635
636 if (sysctl_overcommit_kbytes)
637 allowed = sysctl_overcommit_kbytes >> (PAGE_SHIFT - 10);
638 else
Arun KSca79b0c2018-12-28 00:34:29 -0800639 allowed = ((totalram_pages() - hugetlb_total_pages())
Jerome Marchand49f0ce52014-01-21 15:49:14 -0800640 * sysctl_overcommit_ratio / 100);
641 allowed += total_swap_pages;
642
643 return allowed;
Jerome Marchand00619bc2013-11-12 15:08:31 -0800644}
645
Andrey Ryabinin39a1aa82016-03-17 14:18:50 -0700646/*
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 */
650struct 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 */
660unsigned long vm_memory_committed(void)
661{
662 return percpu_counter_read_positive(&vm_committed_as);
663}
664EXPORT_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 Rapoportad56b732018-03-21 21:22:47 +0200672 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting.rst
Andrey Ryabinin39a1aa82016-03-17 14:18:50 -0700673 *
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 */
682int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
683{
Johannes Weiner8c7829b2019-05-13 17:21:50 -0700684 long allowed;
Andrey Ryabinin39a1aa82016-03-17 14:18:50 -0700685
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 Weiner8c7829b2019-05-13 17:21:50 -0700699 if (pages > totalram_pages() + total_swap_pages)
Andrey Ryabinin39a1aa82016-03-17 14:18:50 -0700700 goto error;
Johannes Weiner8c7829b2019-05-13 17:21:50 -0700701 return 0;
Andrey Ryabinin39a1aa82016-03-17 14:18:50 -0700702 }
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 Weiner8c7829b2019-05-13 17:21:50 -0700715 long reserve = sysctl_user_reserve_kbytes >> (PAGE_SHIFT - 10);
716
Andrey Ryabinin39a1aa82016-03-17 14:18:50 -0700717 allowed -= min_t(long, mm->total_vm / 32, reserve);
718 }
719
720 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
721 return 0;
722error:
723 vm_unacct_memory(pages);
724
725 return -ENOMEM;
726}
727
William Robertsa9090252014-02-11 10:11:59 -0800728/**
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 Rapoporta862f682019-03-05 15:48:42 -0800734 *
735 * Return: the size of the cmdline field copied. Note that the copy does
William Robertsa9090252014-02-11 10:11:59 -0800736 * not guarantee an ending NULL byte.
737 */
738int 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 Guzika3b609e2016-01-20 15:01:05 -0800743 unsigned long arg_start, arg_end, env_start, env_end;
William Robertsa9090252014-02-11 10:11:59 -0800744 if (!mm)
745 goto out;
746 if (!mm->arg_end)
747 goto out_mm; /* Shh! No looking before we're done */
748
Michal Koutnýbc814262019-05-31 22:30:19 -0700749 spin_lock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800750 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ýbc814262019-05-31 22:30:19 -0700754 spin_unlock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800755
756 len = arg_end - arg_start;
William Robertsa9090252014-02-11 10:11:59 -0800757
758 if (len > buflen)
759 len = buflen;
760
Lorenzo Stoakesf307ab62016-10-13 01:20:20 +0100761 res = access_process_vm(task, arg_start, buffer, len, FOLL_FORCE);
William Robertsa9090252014-02-11 10:11:59 -0800762
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 Guzika3b609e2016-01-20 15:01:05 -0800772 len = env_end - env_start;
William Robertsa9090252014-02-11 10:11:59 -0800773 if (len > buflen - res)
774 len = buflen - res;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800775 res += access_process_vm(task, env_start,
Lorenzo Stoakesf307ab62016-10-13 01:20:20 +0100776 buffer+res, len,
777 FOLL_FORCE);
William Robertsa9090252014-02-11 10:11:59 -0800778 res = strnlen(buffer, res);
779 }
780 }
781out_mm:
782 mmput(mm);
783out:
784 return res;
785}
Song Liu010c1642019-09-23 15:38:19 -0700786
787int 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}