blob: 6ed9861cb98c12413011fd420c24ff735279660b [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>
Alexandre Ghiti649775b2019-09-23 15:38:37 -070019#include <linux/elf.h>
Alexandre Ghiti67f39772019-09-23 15:38:47 -070020#include <linux/elf-randomize.h>
21#include <linux/personality.h>
Alexandre Ghiti649775b2019-09-23 15:38:37 -070022#include <linux/random.h>
Alexandre Ghiti67f39772019-09-23 15:38:47 -070023#include <linux/processor.h>
24#include <linux/sizes.h>
25#include <linux/compat.h>
Jerome Marchand00619bc2013-11-12 15:08:31 -080026
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080027#include <linux/uaccess.h>
Matt Mackall30992c92006-01-08 01:01:43 -080028
Namhyung Kim6038def2011-05-24 17:11:22 -070029#include "internal.h"
Kuan-Ying Leea5543c92021-06-18 13:18:59 +080030#ifndef __GENKSYMS__
31#include <trace/hooks/syscall_check.h>
Oven69a794a2023-06-16 20:49:05 +080032#include <trace/hooks/mm.h>
Kuan-Ying Leea5543c92021-06-18 13:18:59 +080033#endif
Namhyung Kim6038def2011-05-24 17:11:22 -070034
Andrzej Hajdaa4bb1e42015-02-13 14:36:24 -080035/**
36 * kfree_const - conditionally free memory
37 * @x: pointer to the memory
38 *
39 * Function calls kfree only if @x is not in .rodata section.
40 */
41void kfree_const(const void *x)
42{
43 if (!is_kernel_rodata((unsigned long)x))
44 kfree(x);
45}
46EXPORT_SYMBOL(kfree_const);
47
Matt Mackall30992c92006-01-08 01:01:43 -080048/**
Matt Mackall30992c92006-01-08 01:01:43 -080049 * kstrdup - allocate space for and copy an existing string
Matt Mackall30992c92006-01-08 01:01:43 -080050 * @s: the string to duplicate
51 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
Mike Rapoporta862f682019-03-05 15:48:42 -080052 *
53 * Return: newly allocated copy of @s or %NULL in case of error
Matt Mackall30992c92006-01-08 01:01:43 -080054 */
55char *kstrdup(const char *s, gfp_t gfp)
56{
57 size_t len;
58 char *buf;
59
60 if (!s)
61 return NULL;
62
63 len = strlen(s) + 1;
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -070064 buf = kmalloc_track_caller(len, gfp);
Matt Mackall30992c92006-01-08 01:01:43 -080065 if (buf)
66 memcpy(buf, s, len);
67 return buf;
68}
69EXPORT_SYMBOL(kstrdup);
Davi Arnaut96840aa2006-03-24 03:18:42 -080070
Alexey Dobriyan1a2f67b2006-09-30 23:27:20 -070071/**
Andrzej Hajdaa4bb1e42015-02-13 14:36:24 -080072 * kstrdup_const - conditionally duplicate an existing const string
73 * @s: the string to duplicate
74 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
75 *
Bartosz Golaszewski295a1732020-10-15 20:07:39 -070076 * Note: Strings allocated by kstrdup_const should be freed by kfree_const and
77 * must not be passed to krealloc().
Mike Rapoporta862f682019-03-05 15:48:42 -080078 *
79 * Return: source string if it is in .rodata section otherwise
80 * fallback to kstrdup.
Andrzej Hajdaa4bb1e42015-02-13 14:36:24 -080081 */
82const char *kstrdup_const(const char *s, gfp_t gfp)
83{
84 if (is_kernel_rodata((unsigned long)s))
85 return s;
86
87 return kstrdup(s, gfp);
88}
89EXPORT_SYMBOL(kstrdup_const);
90
91/**
Jeremy Fitzhardinge1e66df32007-07-17 18:37:02 -070092 * kstrndup - allocate space for and copy an existing string
93 * @s: the string to duplicate
94 * @max: read at most @max chars from @s
95 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
David Howellsf3515742017-07-04 17:25:02 +010096 *
97 * Note: Use kmemdup_nul() instead if the size is known exactly.
Mike Rapoporta862f682019-03-05 15:48:42 -080098 *
99 * Return: newly allocated copy of @s or %NULL in case of error
Jeremy Fitzhardinge1e66df32007-07-17 18:37:02 -0700100 */
101char *kstrndup(const char *s, size_t max, gfp_t gfp)
102{
103 size_t len;
104 char *buf;
105
106 if (!s)
107 return NULL;
108
109 len = strnlen(s, max);
110 buf = kmalloc_track_caller(len+1, gfp);
111 if (buf) {
112 memcpy(buf, s, len);
113 buf[len] = '\0';
114 }
115 return buf;
116}
117EXPORT_SYMBOL(kstrndup);
118
119/**
Alexey Dobriyan1a2f67b2006-09-30 23:27:20 -0700120 * kmemdup - duplicate region of memory
121 *
122 * @src: memory region to duplicate
123 * @len: memory region length
124 * @gfp: GFP mask to use
Mike Rapoporta862f682019-03-05 15:48:42 -0800125 *
126 * Return: newly allocated copy of @src or %NULL in case of error
Alexey Dobriyan1a2f67b2006-09-30 23:27:20 -0700127 */
128void *kmemdup(const void *src, size_t len, gfp_t gfp)
129{
130 void *p;
131
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -0700132 p = kmalloc_track_caller(len, gfp);
Alexey Dobriyan1a2f67b2006-09-30 23:27:20 -0700133 if (p)
134 memcpy(p, src, len);
135 return p;
136}
137EXPORT_SYMBOL(kmemdup);
138
Christoph Lameteref2ad802007-07-17 04:03:21 -0700139/**
David Howellsf3515742017-07-04 17:25:02 +0100140 * kmemdup_nul - Create a NUL-terminated string from unterminated data
141 * @s: The data to stringify
142 * @len: The size of the data
143 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
Mike Rapoporta862f682019-03-05 15:48:42 -0800144 *
145 * Return: newly allocated copy of @s with NUL-termination or %NULL in
146 * case of error
David Howellsf3515742017-07-04 17:25:02 +0100147 */
148char *kmemdup_nul(const char *s, size_t len, gfp_t gfp)
149{
150 char *buf;
151
152 if (!s)
153 return NULL;
154
155 buf = kmalloc_track_caller(len + 1, gfp);
156 if (buf) {
157 memcpy(buf, s, len);
158 buf[len] = '\0';
159 }
160 return buf;
161}
162EXPORT_SYMBOL(kmemdup_nul);
163
164/**
Li Zefan610a77e2009-03-31 15:23:16 -0700165 * memdup_user - duplicate memory region from user space
166 *
167 * @src: source address in user space
168 * @len: number of bytes to copy
169 *
Mike Rapoporta862f682019-03-05 15:48:42 -0800170 * Return: an ERR_PTR() on failure. Result is physically
Al Viro50fd2f22018-01-07 13:06:15 -0500171 * contiguous, to be freed by kfree().
Li Zefan610a77e2009-03-31 15:23:16 -0700172 */
173void *memdup_user(const void __user *src, size_t len)
174{
175 void *p;
176
Daniel Vetter6c8fcc02019-02-20 22:20:42 -0800177 p = kmalloc_track_caller(len, GFP_USER | __GFP_NOWARN);
Li Zefan610a77e2009-03-31 15:23:16 -0700178 if (!p)
179 return ERR_PTR(-ENOMEM);
180
181 if (copy_from_user(p, src, len)) {
182 kfree(p);
183 return ERR_PTR(-EFAULT);
184 }
185
186 return p;
187}
188EXPORT_SYMBOL(memdup_user);
189
Al Viro50fd2f22018-01-07 13:06:15 -0500190/**
191 * vmemdup_user - duplicate memory region from user space
192 *
193 * @src: source address in user space
194 * @len: number of bytes to copy
195 *
Mike Rapoporta862f682019-03-05 15:48:42 -0800196 * Return: an ERR_PTR() on failure. Result may be not
Al Viro50fd2f22018-01-07 13:06:15 -0500197 * physically contiguous. Use kvfree() to free.
198 */
199void *vmemdup_user(const void __user *src, size_t len)
200{
201 void *p;
202
203 p = kvmalloc(len, GFP_USER);
204 if (!p)
205 return ERR_PTR(-ENOMEM);
206
207 if (copy_from_user(p, src, len)) {
208 kvfree(p);
209 return ERR_PTR(-EFAULT);
210 }
211
212 return p;
213}
214EXPORT_SYMBOL(vmemdup_user);
215
Mike Rapoportb86181f2018-08-23 17:00:59 -0700216/**
Davi Arnaut96840aa2006-03-24 03:18:42 -0800217 * strndup_user - duplicate an existing string from user space
Davi Arnaut96840aa2006-03-24 03:18:42 -0800218 * @s: The string to duplicate
219 * @n: Maximum number of bytes to copy, including the trailing NUL.
Mike Rapoporta862f682019-03-05 15:48:42 -0800220 *
Andrew Mortone9145522019-04-05 18:39:34 -0700221 * Return: newly allocated copy of @s or an ERR_PTR() in case of error
Davi Arnaut96840aa2006-03-24 03:18:42 -0800222 */
223char *strndup_user(const char __user *s, long n)
224{
225 char *p;
226 long length;
227
228 length = strnlen_user(s, n);
229
230 if (!length)
231 return ERR_PTR(-EFAULT);
232
233 if (length > n)
234 return ERR_PTR(-EINVAL);
235
Julia Lawall90d74042010-08-09 17:18:26 -0700236 p = memdup_user(s, length);
Davi Arnaut96840aa2006-03-24 03:18:42 -0800237
Julia Lawall90d74042010-08-09 17:18:26 -0700238 if (IS_ERR(p))
239 return p;
Davi Arnaut96840aa2006-03-24 03:18:42 -0800240
241 p[length - 1] = '\0';
242
243 return p;
244}
245EXPORT_SYMBOL(strndup_user);
Andrew Morton16d69262008-07-25 19:44:36 -0700246
Al Viroe9d408e2015-12-24 00:06:05 -0500247/**
248 * memdup_user_nul - duplicate memory region from user space and NUL-terminate
249 *
250 * @src: source address in user space
251 * @len: number of bytes to copy
252 *
Mike Rapoporta862f682019-03-05 15:48:42 -0800253 * Return: an ERR_PTR() on failure.
Al Viroe9d408e2015-12-24 00:06:05 -0500254 */
255void *memdup_user_nul(const void __user *src, size_t len)
256{
257 char *p;
258
259 /*
260 * Always use GFP_KERNEL, since copy_from_user() can sleep and
261 * cause pagefault, which makes it pointless to use GFP_NOFS
262 * or GFP_ATOMIC.
263 */
264 p = kmalloc_track_caller(len + 1, GFP_KERNEL);
265 if (!p)
266 return ERR_PTR(-ENOMEM);
267
268 if (copy_from_user(p, src, len)) {
269 kfree(p);
270 return ERR_PTR(-EFAULT);
271 }
272 p[len] = '\0';
273
274 return p;
275}
276EXPORT_SYMBOL(memdup_user_nul);
277
Namhyung Kim6038def2011-05-24 17:11:22 -0700278void __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
Wei Yangaba6dfb2019-11-30 17:50:53 -0800279 struct vm_area_struct *prev)
Namhyung Kim6038def2011-05-24 17:11:22 -0700280{
281 struct vm_area_struct *next;
282
283 vma->vm_prev = prev;
284 if (prev) {
285 next = prev->vm_next;
286 prev->vm_next = vma;
287 } else {
Wei Yangaba6dfb2019-11-30 17:50:53 -0800288 next = mm->mmap;
Namhyung Kim6038def2011-05-24 17:11:22 -0700289 mm->mmap = vma;
Namhyung Kim6038def2011-05-24 17:11:22 -0700290 }
291 vma->vm_next = next;
292 if (next)
293 next->vm_prev = vma;
294}
295
Wei Yang1b9fc5b22019-11-30 17:50:49 -0800296void __vma_unlink_list(struct mm_struct *mm, struct vm_area_struct *vma)
297{
298 struct vm_area_struct *prev, *next;
299
300 next = vma->vm_next;
301 prev = vma->vm_prev;
302 if (prev)
303 prev->vm_next = next;
304 else
305 mm->mmap = next;
306 if (next)
307 next->vm_prev = prev;
308}
309
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700310/* Check if the vma is being used as a stack by this task */
Andy Lutomirskid17af502016-09-30 10:58:58 -0700311int vma_is_stack_for_current(struct vm_area_struct *vma)
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700312{
Andy Lutomirskid17af502016-09-30 10:58:58 -0700313 struct task_struct * __maybe_unused t = current;
314
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -0700315 return (vma->vm_start <= KSTK_ESP(t) && vma->vm_end >= KSTK_ESP(t));
316}
317
Alexandre Ghiti649775b2019-09-23 15:38:37 -0700318#ifndef STACK_RND_MASK
319#define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) /* 8MB of VA */
320#endif
321
322unsigned long randomize_stack_top(unsigned long stack_top)
323{
324 unsigned long random_variable = 0;
325
326 if (current->flags & PF_RANDOMIZE) {
327 random_variable = get_random_long();
328 random_variable &= STACK_RND_MASK;
329 random_variable <<= PAGE_SHIFT;
330 }
331#ifdef CONFIG_STACK_GROWSUP
332 return PAGE_ALIGN(stack_top) + random_variable;
333#else
334 return PAGE_ALIGN(stack_top) - random_variable;
335#endif
336}
337
Jason A. Donenfeld552ae8e2022-05-14 13:59:30 +0200338/**
339 * randomize_page - Generate a random, page aligned address
340 * @start: The smallest acceptable address the caller will take.
341 * @range: The size of the area, starting at @start, within which the
342 * random address must fall.
343 *
344 * If @start + @range would overflow, @range is capped.
345 *
346 * NOTE: Historical use of randomize_range, which this replaces, presumed that
347 * @start was already page aligned. We now align it regardless.
348 *
349 * Return: A page aligned address within [start, start + range). On error,
350 * @start is returned.
351 */
352unsigned long randomize_page(unsigned long start, unsigned long range)
353{
354 if (!PAGE_ALIGNED(start)) {
355 range -= PAGE_ALIGN(start) - start;
356 start = PAGE_ALIGN(start);
357 }
358
359 if (start > ULONG_MAX - range)
360 range = ULONG_MAX - start;
361
362 range >>= PAGE_SHIFT;
363
364 if (range == 0)
365 return start;
366
367 return start + (get_random_long() % range << PAGE_SHIFT);
368}
369
Alexandre Ghiti67f39772019-09-23 15:38:47 -0700370#ifdef CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT
Alexandre Ghitie7142bf2019-09-23 15:38:50 -0700371unsigned long arch_randomize_brk(struct mm_struct *mm)
372{
373 /* Is the current task 32bit ? */
374 if (!IS_ENABLED(CONFIG_64BIT) || is_compat_task())
375 return randomize_page(mm->brk, SZ_32M);
376
377 return randomize_page(mm->brk, SZ_1G);
378}
379
Alexandre Ghiti67f39772019-09-23 15:38:47 -0700380unsigned long arch_mmap_rnd(void)
381{
382 unsigned long rnd;
383
384#ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS
385 if (is_compat_task())
386 rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1);
387 else
388#endif /* CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS */
389 rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1);
390
391 return rnd << PAGE_SHIFT;
392}
xieliujied80c70d2021-07-03 16:59:16 +0800393EXPORT_SYMBOL_GPL(arch_mmap_rnd);
Alexandre Ghiti67f39772019-09-23 15:38:47 -0700394
395static int mmap_is_legacy(struct rlimit *rlim_stack)
396{
397 if (current->personality & ADDR_COMPAT_LAYOUT)
398 return 1;
399
400 if (rlim_stack->rlim_cur == RLIM_INFINITY)
401 return 1;
402
403 return sysctl_legacy_va_layout;
404}
405
406/*
407 * Leave enough space between the mmap area and the stack to honour ulimit in
408 * the face of randomisation.
409 */
410#define MIN_GAP (SZ_128M)
411#define MAX_GAP (STACK_TOP / 6 * 5)
412
413static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack)
414{
415 unsigned long gap = rlim_stack->rlim_cur;
416 unsigned long pad = stack_guard_gap;
417
418 /* Account for stack randomization if necessary */
419 if (current->flags & PF_RANDOMIZE)
420 pad += (STACK_RND_MASK << PAGE_SHIFT);
421
422 /* Values close to RLIM_INFINITY can overflow. */
423 if (gap + pad > gap)
424 gap += pad;
425
426 if (gap < MIN_GAP)
427 gap = MIN_GAP;
428 else if (gap > MAX_GAP)
429 gap = MAX_GAP;
430
431 return PAGE_ALIGN(STACK_TOP - gap - rnd);
432}
433
434void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
435{
436 unsigned long random_factor = 0UL;
437
438 if (current->flags & PF_RANDOMIZE)
439 random_factor = arch_mmap_rnd();
440
441 if (mmap_is_legacy(rlim_stack)) {
442 mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
443 mm->get_unmapped_area = arch_get_unmapped_area;
444 } else {
445 mm->mmap_base = mmap_base(random_factor, rlim_stack);
446 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
447 }
448}
449#elif defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT)
Kees Cook8f2af152018-04-10 16:34:53 -0700450void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
Andrew Morton16d69262008-07-25 19:44:36 -0700451{
452 mm->mmap_base = TASK_UNMAPPED_BASE;
453 mm->get_unmapped_area = arch_get_unmapped_area;
Andrew Morton16d69262008-07-25 19:44:36 -0700454}
455#endif
Rusty Russell912985d2008-08-12 17:52:52 -0500456
Daniel Jordan79eb5972019-07-16 16:30:54 -0700457/**
458 * __account_locked_vm - account locked pages to an mm's locked_vm
459 * @mm: mm to account against
460 * @pages: number of pages to account
461 * @inc: %true if @pages should be considered positive, %false if not
462 * @task: task used to check RLIMIT_MEMLOCK
463 * @bypass_rlim: %true if checking RLIMIT_MEMLOCK should be skipped
464 *
465 * Assumes @task and @mm are valid (i.e. at least one reference on each), and
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700466 * that mmap_lock is held as writer.
Daniel Jordan79eb5972019-07-16 16:30:54 -0700467 *
468 * Return:
469 * * 0 on success
470 * * -ENOMEM if RLIMIT_MEMLOCK would be exceeded.
471 */
472int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc,
473 struct task_struct *task, bool bypass_rlim)
474{
475 unsigned long locked_vm, limit;
476 int ret = 0;
477
Michel Lespinasse42fc5412020-06-08 21:33:44 -0700478 mmap_assert_write_locked(mm);
Daniel Jordan79eb5972019-07-16 16:30:54 -0700479
480 locked_vm = mm->locked_vm;
481 if (inc) {
482 if (!bypass_rlim) {
483 limit = task_rlimit(task, RLIMIT_MEMLOCK) >> PAGE_SHIFT;
484 if (locked_vm + pages > limit)
485 ret = -ENOMEM;
486 }
487 if (!ret)
488 mm->locked_vm = locked_vm + pages;
489 } else {
490 WARN_ON_ONCE(pages > locked_vm);
491 mm->locked_vm = locked_vm - pages;
492 }
493
494 pr_debug("%s: [%d] caller %ps %c%lu %lu/%lu%s\n", __func__, task->pid,
495 (void *)_RET_IP_, (inc) ? '+' : '-', pages << PAGE_SHIFT,
496 locked_vm << PAGE_SHIFT, task_rlimit(task, RLIMIT_MEMLOCK),
497 ret ? " - exceeded" : "");
498
499 return ret;
500}
501EXPORT_SYMBOL_GPL(__account_locked_vm);
502
503/**
504 * account_locked_vm - account locked pages to an mm's locked_vm
505 * @mm: mm to account against, may be NULL
506 * @pages: number of pages to account
507 * @inc: %true if @pages should be considered positive, %false if not
508 *
509 * Assumes a non-NULL @mm is valid (i.e. at least one reference on it).
510 *
511 * Return:
512 * * 0 on success, or if mm is NULL
513 * * -ENOMEM if RLIMIT_MEMLOCK would be exceeded.
514 */
515int account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc)
516{
517 int ret;
518
519 if (pages == 0 || !mm)
520 return 0;
521
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700522 mmap_write_lock(mm);
Daniel Jordan79eb5972019-07-16 16:30:54 -0700523 ret = __account_locked_vm(mm, pages, inc, current,
524 capable(CAP_IPC_LOCK));
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700525 mmap_write_unlock(mm);
Daniel Jordan79eb5972019-07-16 16:30:54 -0700526
527 return ret;
528}
529EXPORT_SYMBOL_GPL(account_locked_vm);
530
Al Viroeb36c582012-05-30 20:17:35 -0400531unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr,
532 unsigned long len, unsigned long prot,
Michal Hocko9fbeb5a2016-05-23 16:25:30 -0700533 unsigned long flag, unsigned long pgoff)
Al Viroeb36c582012-05-30 20:17:35 -0400534{
535 unsigned long ret;
536 struct mm_struct *mm = current->mm;
Michel Lespinasse41badc12013-02-22 16:32:47 -0800537 unsigned long populate;
Mike Rapoport897ab3e2017-02-24 14:58:22 -0800538 LIST_HEAD(uf);
Al Viroeb36c582012-05-30 20:17:35 -0400539
540 ret = security_mmap_file(file, prot, flag);
541 if (!ret) {
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700542 if (mmap_write_lock_killable(mm))
Michal Hocko9fbeb5a2016-05-23 16:25:30 -0700543 return -EINTR;
Peter Collingbourne45e55302020-08-06 23:23:37 -0700544 ret = do_mmap(file, addr, len, prot, flag, pgoff, &populate,
545 &uf);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700546 mmap_write_unlock(mm);
Mike Rapoport897ab3e2017-02-24 14:58:22 -0800547 userfaultfd_unmap_complete(mm, &uf);
Michel Lespinasse41badc12013-02-22 16:32:47 -0800548 if (populate)
549 mm_populate(ret, populate);
Al Viroeb36c582012-05-30 20:17:35 -0400550 }
Kuan-Ying Leea5543c92021-06-18 13:18:59 +0800551 trace_android_vh_check_mmap_file(file, prot, flag, ret);
Al Viroeb36c582012-05-30 20:17:35 -0400552 return ret;
553}
554
555unsigned long vm_mmap(struct file *file, unsigned long addr,
556 unsigned long len, unsigned long prot,
557 unsigned long flag, unsigned long offset)
558{
559 if (unlikely(offset + PAGE_ALIGN(len) < offset))
560 return -EINVAL;
Alexander Kuleshovea53cde2015-11-05 18:46:46 -0800561 if (unlikely(offset_in_page(offset)))
Al Viroeb36c582012-05-30 20:17:35 -0400562 return -EINVAL;
563
Michal Hocko9fbeb5a2016-05-23 16:25:30 -0700564 return vm_mmap_pgoff(file, addr, len, prot, flag, offset >> PAGE_SHIFT);
Al Viroeb36c582012-05-30 20:17:35 -0400565}
566EXPORT_SYMBOL(vm_mmap);
567
Michal Hockoa7c3e902017-05-08 15:57:09 -0700568/**
569 * kvmalloc_node - attempt to allocate physically contiguous memory, but upon
570 * failure, fall back to non-contiguous (vmalloc) allocation.
571 * @size: size of the request.
572 * @flags: gfp mask for the allocation - must be compatible (superset) with GFP_KERNEL.
573 * @node: numa node to allocate from
574 *
575 * Uses kmalloc to get the memory but if the allocation fails then falls back
576 * to the vmalloc allocator. Use kvfree for freeing the memory.
577 *
Michal Hockocc965a22017-07-12 14:36:52 -0700578 * Reclaim modifiers - __GFP_NORETRY and __GFP_NOFAIL are not supported.
579 * __GFP_RETRY_MAYFAIL is supported, and it should be used only if kmalloc is
580 * preferable to the vmalloc fallback, due to visible performance drawbacks.
Michal Hockoa7c3e902017-05-08 15:57:09 -0700581 *
Michal Hockoce91f6e2018-06-07 17:09:40 -0700582 * Please note that any use of gfp flags outside of GFP_KERNEL is careful to not
583 * fall back to vmalloc.
Mike Rapoporta862f682019-03-05 15:48:42 -0800584 *
585 * Return: pointer to the allocated memory of %NULL in case of failure
Michal Hockoa7c3e902017-05-08 15:57:09 -0700586 */
587void *kvmalloc_node(size_t size, gfp_t flags, int node)
588{
589 gfp_t kmalloc_flags = flags;
590 void *ret;
Oven69a794a2023-06-16 20:49:05 +0800591 bool use_vmalloc = false;
Michal Hockoa7c3e902017-05-08 15:57:09 -0700592
593 /*
594 * vmalloc uses GFP_KERNEL for some internal allocations (e.g page tables)
595 * so the given set of flags has to be compatible.
596 */
Michal Hockoce91f6e2018-06-07 17:09:40 -0700597 if ((flags & GFP_KERNEL) != GFP_KERNEL)
598 return kmalloc_node(size, flags, node);
Michal Hockoa7c3e902017-05-08 15:57:09 -0700599
Oven69a794a2023-06-16 20:49:05 +0800600 trace_android_vh_kvmalloc_node_use_vmalloc(size, &kmalloc_flags, &use_vmalloc);
601 if (use_vmalloc)
602 goto use_vmalloc_node;
603
Michal Hockoa7c3e902017-05-08 15:57:09 -0700604 /*
Michal Hocko4f4f2ba2017-06-02 14:46:19 -0700605 * We want to attempt a large physically contiguous block first because
606 * it is less likely to fragment multiple larger blocks and therefore
607 * contribute to a long term fragmentation less than vmalloc fallback.
608 * However make sure that larger requests are not too disruptive - no
609 * OOM killer and no allocation failure warnings as we have a fallback.
Michal Hockoa7c3e902017-05-08 15:57:09 -0700610 */
Michal Hocko6c5ab652017-05-08 15:57:15 -0700611 if (size > PAGE_SIZE) {
612 kmalloc_flags |= __GFP_NOWARN;
613
Michal Hockocc965a22017-07-12 14:36:52 -0700614 if (!(kmalloc_flags & __GFP_RETRY_MAYFAIL))
Michal Hocko6c5ab652017-05-08 15:57:15 -0700615 kmalloc_flags |= __GFP_NORETRY;
616 }
Michal Hockoa7c3e902017-05-08 15:57:09 -0700617
618 ret = kmalloc_node(size, kmalloc_flags, node);
619
620 /*
621 * It doesn't really make sense to fallback to vmalloc for sub page
622 * requests
623 */
624 if (ret || size <= PAGE_SIZE)
625 return ret;
626
Linus Torvalds57a269a2021-07-14 09:45:49 -0700627 /* Don't even allow crazy sizes */
Daniel Borkmanne93f2be2022-03-04 15:26:32 +0100628 if (unlikely(size > INT_MAX)) {
629 WARN_ON_ONCE(!(flags & __GFP_NOWARN));
Linus Torvalds57a269a2021-07-14 09:45:49 -0700630 return NULL;
Daniel Borkmanne93f2be2022-03-04 15:26:32 +0100631 }
Linus Torvalds57a269a2021-07-14 09:45:49 -0700632
Oven69a794a2023-06-16 20:49:05 +0800633use_vmalloc_node:
Christoph Hellwig2b905942020-06-01 21:51:53 -0700634 return __vmalloc_node(size, 1, flags, node,
Michal Hocko8594a212017-05-12 15:46:41 -0700635 __builtin_return_address(0));
Michal Hockoa7c3e902017-05-08 15:57:09 -0700636}
637EXPORT_SYMBOL(kvmalloc_node);
638
Mike Rapoportff4dc772018-08-23 17:01:02 -0700639/**
Andrew Morton04b8e942018-09-04 15:45:55 -0700640 * kvfree() - Free memory.
641 * @addr: Pointer to allocated memory.
Mike Rapoportff4dc772018-08-23 17:01:02 -0700642 *
Andrew Morton04b8e942018-09-04 15:45:55 -0700643 * kvfree frees memory allocated by any of vmalloc(), kmalloc() or kvmalloc().
644 * It is slightly more efficient to use kfree() or vfree() if you are certain
645 * that you know which one to use.
646 *
Andrey Ryabinin52414d32018-10-26 15:07:00 -0700647 * Context: Either preemptible task context or not-NMI interrupt.
Mike Rapoportff4dc772018-08-23 17:01:02 -0700648 */
Al Viro39f1f782014-05-06 14:02:53 -0400649void kvfree(const void *addr)
650{
651 if (is_vmalloc_addr(addr))
652 vfree(addr);
653 else
654 kfree(addr);
655}
656EXPORT_SYMBOL(kvfree);
657
Waiman Longd4eaa282020-06-04 16:48:21 -0700658/**
659 * kvfree_sensitive - Free a data object containing sensitive information.
660 * @addr: address of the data object to be freed.
661 * @len: length of the data object.
662 *
663 * Use the special memzero_explicit() function to clear the content of a
664 * kvmalloc'ed object containing sensitive data to make sure that the
665 * compiler won't optimize out the data clearing.
666 */
667void kvfree_sensitive(const void *addr, size_t len)
668{
669 if (likely(!ZERO_OR_NULL_PTR(addr))) {
670 memzero_explicit((void *)addr, len);
671 kvfree(addr);
672 }
673}
674EXPORT_SYMBOL(kvfree_sensitive);
675
Dave Chinnerf5f3e542022-08-10 16:15:50 +0200676void *kvrealloc(const void *p, size_t oldsize, size_t newsize, gfp_t flags)
677{
678 void *newp;
679
680 if (oldsize >= newsize)
681 return (void *)p;
682 newp = kvmalloc(newsize, flags);
683 if (!newp)
684 return NULL;
685 memcpy(newp, p, oldsize);
686 kvfree(p);
687 return newp;
688}
689EXPORT_SYMBOL(kvrealloc);
690
Kirill A. Shutemove39155e2015-04-15 16:14:53 -0700691static inline void *__page_rmapping(struct page *page)
692{
693 unsigned long mapping;
694
695 mapping = (unsigned long)page->mapping;
696 mapping &= ~PAGE_MAPPING_FLAGS;
697
698 return (void *)mapping;
699}
700
701/* Neutral page->mapping pointer to address_space or anon_vma or other */
702void *page_rmapping(struct page *page)
703{
704 page = compound_head(page);
705 return __page_rmapping(page);
706}
707
Andrew Morton1aa8aea2016-05-19 17:12:00 -0700708/*
709 * Return true if this page is mapped into pagetables.
710 * For compound page it returns true if any subpage of compound page is mapped.
711 */
712bool page_mapped(struct page *page)
713{
714 int i;
715
716 if (likely(!PageCompound(page)))
717 return atomic_read(&page->_mapcount) >= 0;
718 page = compound_head(page);
719 if (atomic_read(compound_mapcount_ptr(page)) >= 0)
720 return true;
721 if (PageHuge(page))
722 return false;
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -0700723 for (i = 0; i < compound_nr(page); i++) {
Andrew Morton1aa8aea2016-05-19 17:12:00 -0700724 if (atomic_read(&page[i]._mapcount) >= 0)
725 return true;
726 }
727 return false;
728}
729EXPORT_SYMBOL(page_mapped);
730
Kirill A. Shutemove39155e2015-04-15 16:14:53 -0700731struct anon_vma *page_anon_vma(struct page *page)
732{
733 unsigned long mapping;
734
735 page = compound_head(page);
736 mapping = (unsigned long)page->mapping;
737 if ((mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON)
738 return NULL;
739 return __page_rmapping(page);
740}
741
Shaohua Li98003392013-02-22 16:34:35 -0800742struct address_space *page_mapping(struct page *page)
743{
Kirill A. Shutemov1c290f62016-01-15 16:52:07 -0800744 struct address_space *mapping;
745
746 page = compound_head(page);
Shaohua Li98003392013-02-22 16:34:35 -0800747
Mikulas Patocka03e5ac22014-01-14 17:56:40 -0800748 /* This happens if someone calls flush_dcache_page on slab page */
749 if (unlikely(PageSlab(page)))
750 return NULL;
751
Shaohua Li33806f02013-02-22 16:34:37 -0800752 if (unlikely(PageSwapCache(page))) {
753 swp_entry_t entry;
754
755 entry.val = page_private(page);
Kirill A. Shutemove39155e2015-04-15 16:14:53 -0700756 return swap_address_space(entry);
757 }
758
Kirill A. Shutemov1c290f62016-01-15 16:52:07 -0800759 mapping = page->mapping;
Minchan Kimbda807d2016-07-26 15:23:05 -0700760 if ((unsigned long)mapping & PAGE_MAPPING_ANON)
Kirill A. Shutemove39155e2015-04-15 16:14:53 -0700761 return NULL;
Minchan Kimbda807d2016-07-26 15:23:05 -0700762
763 return (void *)((unsigned long)mapping & ~PAGE_MAPPING_FLAGS);
Shaohua Li98003392013-02-22 16:34:35 -0800764}
Minchan Kimbda807d2016-07-26 15:23:05 -0700765EXPORT_SYMBOL(page_mapping);
Shaohua Li98003392013-02-22 16:34:35 -0800766
Huang Yingcb9f7532018-04-05 16:24:39 -0700767/*
768 * For file cache pages, return the address_space, otherwise return NULL
769 */
770struct address_space *page_mapping_file(struct page *page)
771{
772 if (unlikely(PageSwapCache(page)))
773 return NULL;
774 return page_mapping(page);
775}
776
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -0800777/* Slow path of page_mapcount() for compound pages */
778int __page_mapcount(struct page *page)
779{
780 int ret;
781
782 ret = atomic_read(&page->_mapcount) + 1;
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -0700783 /*
784 * For file THP page->_mapcount contains total number of mapping
785 * of the page: no need to look into compound_mapcount.
786 */
787 if (!PageAnon(page) && !PageHuge(page))
788 return ret;
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -0800789 page = compound_head(page);
790 ret += atomic_read(compound_mapcount_ptr(page)) + 1;
791 if (PageDoubleMap(page))
792 ret--;
793 return ret;
794}
795EXPORT_SYMBOL_GPL(__page_mapcount);
796
Andrey Ryabinin39a1aa82016-03-17 14:18:50 -0700797int sysctl_overcommit_memory __read_mostly = OVERCOMMIT_GUESS;
798int sysctl_overcommit_ratio __read_mostly = 50;
799unsigned long sysctl_overcommit_kbytes __read_mostly;
800int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT;
801unsigned long sysctl_user_reserve_kbytes __read_mostly = 1UL << 17; /* 128MB */
802unsigned long sysctl_admin_reserve_kbytes __read_mostly = 1UL << 13; /* 8MB */
803
Christoph Hellwig32927392020-04-24 08:43:38 +0200804int overcommit_ratio_handler(struct ctl_table *table, int write, void *buffer,
805 size_t *lenp, loff_t *ppos)
Jerome Marchand49f0ce52014-01-21 15:49:14 -0800806{
807 int ret;
808
809 ret = proc_dointvec(table, write, buffer, lenp, ppos);
810 if (ret == 0 && write)
811 sysctl_overcommit_kbytes = 0;
812 return ret;
813}
814
Feng Tang56f35472020-08-06 23:23:15 -0700815static void sync_overcommit_as(struct work_struct *dummy)
816{
817 percpu_counter_sync(&vm_committed_as);
818}
819
820int overcommit_policy_handler(struct ctl_table *table, int write, void *buffer,
821 size_t *lenp, loff_t *ppos)
822{
823 struct ctl_table t;
Chen Jun83829722021-09-24 15:44:06 -0700824 int new_policy = -1;
Feng Tang56f35472020-08-06 23:23:15 -0700825 int ret;
826
827 /*
828 * The deviation of sync_overcommit_as could be big with loose policy
829 * like OVERCOMMIT_ALWAYS/OVERCOMMIT_GUESS. When changing policy to
830 * strict OVERCOMMIT_NEVER, we need to reduce the deviation to comply
831 * with the strict "NEVER", and to avoid possible race condtion (even
832 * though user usually won't too frequently do the switching to policy
833 * OVERCOMMIT_NEVER), the switch is done in the following order:
834 * 1. changing the batch
835 * 2. sync percpu count on each CPU
836 * 3. switch the policy
837 */
838 if (write) {
839 t = *table;
840 t.data = &new_policy;
841 ret = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
Chen Jun83829722021-09-24 15:44:06 -0700842 if (ret || new_policy == -1)
Feng Tang56f35472020-08-06 23:23:15 -0700843 return ret;
844
845 mm_compute_batch(new_policy);
846 if (new_policy == OVERCOMMIT_NEVER)
847 schedule_on_each_cpu(sync_overcommit_as);
848 sysctl_overcommit_memory = new_policy;
849 } else {
850 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
851 }
852
853 return ret;
854}
855
Christoph Hellwig32927392020-04-24 08:43:38 +0200856int overcommit_kbytes_handler(struct ctl_table *table, int write, void *buffer,
857 size_t *lenp, loff_t *ppos)
Jerome Marchand49f0ce52014-01-21 15:49:14 -0800858{
859 int ret;
860
861 ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
862 if (ret == 0 && write)
863 sysctl_overcommit_ratio = 0;
864 return ret;
865}
866
Jerome Marchand00619bc2013-11-12 15:08:31 -0800867/*
868 * Committed memory limit enforced when OVERCOMMIT_NEVER policy is used
869 */
870unsigned long vm_commit_limit(void)
871{
Jerome Marchand49f0ce52014-01-21 15:49:14 -0800872 unsigned long allowed;
873
874 if (sysctl_overcommit_kbytes)
875 allowed = sysctl_overcommit_kbytes >> (PAGE_SHIFT - 10);
876 else
Arun KSca79b0c2018-12-28 00:34:29 -0800877 allowed = ((totalram_pages() - hugetlb_total_pages())
Jerome Marchand49f0ce52014-01-21 15:49:14 -0800878 * sysctl_overcommit_ratio / 100);
879 allowed += total_swap_pages;
880
881 return allowed;
Jerome Marchand00619bc2013-11-12 15:08:31 -0800882}
883
Andrey Ryabinin39a1aa82016-03-17 14:18:50 -0700884/*
885 * Make sure vm_committed_as in one cacheline and not cacheline shared with
886 * other variables. It can be updated by several CPUs frequently.
887 */
888struct percpu_counter vm_committed_as ____cacheline_aligned_in_smp;
889
890/*
891 * The global memory commitment made in the system can be a metric
892 * that can be used to drive ballooning decisions when Linux is hosted
893 * as a guest. On Hyper-V, the host implements a policy engine for dynamically
894 * balancing memory across competing virtual machines that are hosted.
895 * Several metrics drive this policy engine including the guest reported
896 * memory commitment.
Feng Tang4e2ee512020-08-06 23:23:07 -0700897 *
898 * The time cost of this is very low for small platforms, and for big
899 * platform like a 2S/36C/72T Skylake server, in worst case where
900 * vm_committed_as's spinlock is under severe contention, the time cost
901 * could be about 30~40 microseconds.
Andrey Ryabinin39a1aa82016-03-17 14:18:50 -0700902 */
903unsigned long vm_memory_committed(void)
904{
Feng Tang4e2ee512020-08-06 23:23:07 -0700905 return percpu_counter_sum_positive(&vm_committed_as);
Andrey Ryabinin39a1aa82016-03-17 14:18:50 -0700906}
907EXPORT_SYMBOL_GPL(vm_memory_committed);
908
909/*
910 * Check that a process has enough memory to allocate a new virtual
911 * mapping. 0 means there is enough memory for the allocation to
912 * succeed and -ENOMEM implies there is not.
913 *
914 * We currently support three overcommit policies, which are set via the
Mike Rapoportad56b732018-03-21 21:22:47 +0200915 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting.rst
Andrey Ryabinin39a1aa82016-03-17 14:18:50 -0700916 *
917 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
918 * Additional code 2002 Jul 20 by Robert Love.
919 *
920 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
921 *
922 * Note this is a helper function intended to be used by LSMs which
923 * wish to use this logic.
924 */
925int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
926{
Johannes Weiner8c7829b2019-05-13 17:21:50 -0700927 long allowed;
Andrey Ryabinin39a1aa82016-03-17 14:18:50 -0700928
Andrey Ryabinin39a1aa82016-03-17 14:18:50 -0700929 vm_acct_memory(pages);
930
931 /*
932 * Sometimes we want to use more memory than we have
933 */
934 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
935 return 0;
936
937 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
Johannes Weiner8c7829b2019-05-13 17:21:50 -0700938 if (pages > totalram_pages() + total_swap_pages)
Andrey Ryabinin39a1aa82016-03-17 14:18:50 -0700939 goto error;
Johannes Weiner8c7829b2019-05-13 17:21:50 -0700940 return 0;
Andrey Ryabinin39a1aa82016-03-17 14:18:50 -0700941 }
942
943 allowed = vm_commit_limit();
944 /*
945 * Reserve some for root
946 */
947 if (!cap_sys_admin)
948 allowed -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
949
950 /*
951 * Don't let a single process grow so big a user can't recover
952 */
953 if (mm) {
Johannes Weiner8c7829b2019-05-13 17:21:50 -0700954 long reserve = sysctl_user_reserve_kbytes >> (PAGE_SHIFT - 10);
955
Andrey Ryabinin39a1aa82016-03-17 14:18:50 -0700956 allowed -= min_t(long, mm->total_vm / 32, reserve);
957 }
958
959 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
960 return 0;
961error:
962 vm_unacct_memory(pages);
963
964 return -ENOMEM;
965}
966
William Robertsa9090252014-02-11 10:11:59 -0800967/**
968 * get_cmdline() - copy the cmdline value to a buffer.
969 * @task: the task whose cmdline value to copy.
970 * @buffer: the buffer to copy to.
971 * @buflen: the length of the buffer. Larger cmdline values are truncated
972 * to this length.
Mike Rapoporta862f682019-03-05 15:48:42 -0800973 *
974 * Return: the size of the cmdline field copied. Note that the copy does
William Robertsa9090252014-02-11 10:11:59 -0800975 * not guarantee an ending NULL byte.
976 */
977int get_cmdline(struct task_struct *task, char *buffer, int buflen)
978{
979 int res = 0;
980 unsigned int len;
981 struct mm_struct *mm = get_task_mm(task);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800982 unsigned long arg_start, arg_end, env_start, env_end;
William Robertsa9090252014-02-11 10:11:59 -0800983 if (!mm)
984 goto out;
985 if (!mm->arg_end)
986 goto out_mm; /* Shh! No looking before we're done */
987
Michal Koutnýbc814262019-05-31 22:30:19 -0700988 spin_lock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800989 arg_start = mm->arg_start;
990 arg_end = mm->arg_end;
991 env_start = mm->env_start;
992 env_end = mm->env_end;
Michal Koutnýbc814262019-05-31 22:30:19 -0700993 spin_unlock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800994
995 len = arg_end - arg_start;
William Robertsa9090252014-02-11 10:11:59 -0800996
997 if (len > buflen)
998 len = buflen;
999
Lorenzo Stoakesf307ab62016-10-13 01:20:20 +01001000 res = access_process_vm(task, arg_start, buffer, len, FOLL_FORCE);
William Robertsa9090252014-02-11 10:11:59 -08001001
1002 /*
1003 * If the nul at the end of args has been overwritten, then
1004 * assume application is using setproctitle(3).
1005 */
1006 if (res > 0 && buffer[res-1] != '\0' && len < buflen) {
1007 len = strnlen(buffer, res);
1008 if (len < res) {
1009 res = len;
1010 } else {
Mateusz Guzika3b609e2016-01-20 15:01:05 -08001011 len = env_end - env_start;
William Robertsa9090252014-02-11 10:11:59 -08001012 if (len > buflen - res)
1013 len = buflen - res;
Mateusz Guzika3b609e2016-01-20 15:01:05 -08001014 res += access_process_vm(task, env_start,
Lorenzo Stoakesf307ab62016-10-13 01:20:20 +01001015 buffer+res, len,
1016 FOLL_FORCE);
William Robertsa9090252014-02-11 10:11:59 -08001017 res = strnlen(buffer, res);
1018 }
1019 }
1020out_mm:
1021 mmput(mm);
1022out:
1023 return res;
1024}
Song Liu010c1642019-09-23 15:38:19 -07001025
Catalin Marinas4d1a8a22019-11-27 09:53:44 +00001026int __weak memcmp_pages(struct page *page1, struct page *page2)
Song Liu010c1642019-09-23 15:38:19 -07001027{
1028 char *addr1, *addr2;
1029 int ret;
1030
1031 addr1 = kmap_atomic(page1);
1032 addr2 = kmap_atomic(page2);
1033 ret = memcmp(addr1, addr2, PAGE_SIZE);
1034 kunmap_atomic(addr2);
1035 kunmap_atomic(addr1);
1036 return ret;
1037}