1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/slab.h>
4 #include <linux/string.h>
5 #include <linux/compiler.h>
6 #include <linux/export.h>
8 #include <linux/sched.h>
9 #include <linux/sched/mm.h>
10 #include <linux/sched/signal.h>
11 #include <linux/sched/task_stack.h>
12 #include <linux/security.h>
13 #include <linux/swap.h>
14 #include <linux/swapops.h>
15 #include <linux/mman.h>
16 #include <linux/hugetlb.h>
17 #include <linux/vmalloc.h>
18 #include <linux/userfaultfd_k.h>
19 #include <linux/elf.h>
20 #include <linux/elf-randomize.h>
21 #include <linux/personality.h>
22 #include <linux/random.h>
23 #include <linux/processor.h>
24 #include <linux/sizes.h>
25 #include <linux/compat.h>
27 #include <linux/uaccess.h>
33 * kfree_const - conditionally free memory
34 * @x: pointer to the memory
36 * Function calls kfree only if @x is not in .rodata section.
38 void kfree_const(const void *x)
40 if (!is_kernel_rodata((unsigned long)x))
43 EXPORT_SYMBOL(kfree_const);
46 * kstrdup - allocate space for and copy an existing string
47 * @s: the string to duplicate
48 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
50 * Return: newly allocated copy of @s or %NULL in case of error
52 char *kstrdup(const char *s, gfp_t gfp)
61 buf = kmalloc_track_caller(len, gfp);
66 EXPORT_SYMBOL(kstrdup);
69 * kstrdup_const - conditionally duplicate an existing const string
70 * @s: the string to duplicate
71 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
73 * Note: Strings allocated by kstrdup_const should be freed by kfree_const and
74 * must not be passed to krealloc().
76 * Return: source string if it is in .rodata section otherwise
77 * fallback to kstrdup.
79 const char *kstrdup_const(const char *s, gfp_t gfp)
81 if (is_kernel_rodata((unsigned long)s))
84 return kstrdup(s, gfp);
86 EXPORT_SYMBOL(kstrdup_const);
89 * kstrndup - allocate space for and copy an existing string
90 * @s: the string to duplicate
91 * @max: read at most @max chars from @s
92 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
94 * Note: Use kmemdup_nul() instead if the size is known exactly.
96 * Return: newly allocated copy of @s or %NULL in case of error
98 char *kstrndup(const char *s, size_t max, gfp_t gfp)
106 len = strnlen(s, max);
107 buf = kmalloc_track_caller(len+1, gfp);
114 EXPORT_SYMBOL(kstrndup);
117 * kmemdup - duplicate region of memory
119 * @src: memory region to duplicate
120 * @len: memory region length
121 * @gfp: GFP mask to use
123 * Return: newly allocated copy of @src or %NULL in case of error,
124 * result is physically contiguous. Use kfree() to free.
126 void *kmemdup(const void *src, size_t len, gfp_t gfp)
130 p = kmalloc_track_caller(len, gfp);
135 EXPORT_SYMBOL(kmemdup);
138 * kvmemdup - duplicate region of memory
140 * @src: memory region to duplicate
141 * @len: memory region length
142 * @gfp: GFP mask to use
144 * Return: newly allocated copy of @src or %NULL in case of error,
145 * result may be not physically contiguous. Use kvfree() to free.
147 void *kvmemdup(const void *src, size_t len, gfp_t gfp)
151 p = kvmalloc(len, gfp);
156 EXPORT_SYMBOL(kvmemdup);
159 * kmemdup_nul - Create a NUL-terminated string from unterminated data
160 * @s: The data to stringify
161 * @len: The size of the data
162 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
164 * Return: newly allocated copy of @s with NUL-termination or %NULL in
167 char *kmemdup_nul(const char *s, size_t len, gfp_t gfp)
174 buf = kmalloc_track_caller(len + 1, gfp);
181 EXPORT_SYMBOL(kmemdup_nul);
184 * memdup_user - duplicate memory region from user space
186 * @src: source address in user space
187 * @len: number of bytes to copy
189 * Return: an ERR_PTR() on failure. Result is physically
190 * contiguous, to be freed by kfree().
192 void *memdup_user(const void __user *src, size_t len)
196 p = kmalloc_track_caller(len, GFP_USER | __GFP_NOWARN);
198 return ERR_PTR(-ENOMEM);
200 if (copy_from_user(p, src, len)) {
202 return ERR_PTR(-EFAULT);
207 EXPORT_SYMBOL(memdup_user);
210 * vmemdup_user - duplicate memory region from user space
212 * @src: source address in user space
213 * @len: number of bytes to copy
215 * Return: an ERR_PTR() on failure. Result may be not
216 * physically contiguous. Use kvfree() to free.
218 void *vmemdup_user(const void __user *src, size_t len)
222 p = kvmalloc(len, GFP_USER);
224 return ERR_PTR(-ENOMEM);
226 if (copy_from_user(p, src, len)) {
228 return ERR_PTR(-EFAULT);
233 EXPORT_SYMBOL(vmemdup_user);
236 * strndup_user - duplicate an existing string from user space
237 * @s: The string to duplicate
238 * @n: Maximum number of bytes to copy, including the trailing NUL.
240 * Return: newly allocated copy of @s or an ERR_PTR() in case of error
242 char *strndup_user(const char __user *s, long n)
247 length = strnlen_user(s, n);
250 return ERR_PTR(-EFAULT);
253 return ERR_PTR(-EINVAL);
255 p = memdup_user(s, length);
260 p[length - 1] = '\0';
264 EXPORT_SYMBOL(strndup_user);
267 * memdup_user_nul - duplicate memory region from user space and NUL-terminate
269 * @src: source address in user space
270 * @len: number of bytes to copy
272 * Return: an ERR_PTR() on failure.
274 void *memdup_user_nul(const void __user *src, size_t len)
279 * Always use GFP_KERNEL, since copy_from_user() can sleep and
280 * cause pagefault, which makes it pointless to use GFP_NOFS
283 p = kmalloc_track_caller(len + 1, GFP_KERNEL);
285 return ERR_PTR(-ENOMEM);
287 if (copy_from_user(p, src, len)) {
289 return ERR_PTR(-EFAULT);
295 EXPORT_SYMBOL(memdup_user_nul);
297 /* Check if the vma is being used as a stack by this task */
298 int vma_is_stack_for_current(struct vm_area_struct *vma)
300 struct task_struct * __maybe_unused t = current;
302 return (vma->vm_start <= KSTK_ESP(t) && vma->vm_end >= KSTK_ESP(t));
306 * Change backing file, only valid to use during initial VMA setup.
308 void vma_set_file(struct vm_area_struct *vma, struct file *file)
310 /* Changing an anonymous vma with this is illegal */
312 swap(vma->vm_file, file);
315 EXPORT_SYMBOL(vma_set_file);
317 #ifndef STACK_RND_MASK
318 #define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) /* 8MB of VA */
321 unsigned long randomize_stack_top(unsigned long stack_top)
323 unsigned long random_variable = 0;
325 if (current->flags & PF_RANDOMIZE) {
326 random_variable = get_random_long();
327 random_variable &= STACK_RND_MASK;
328 random_variable <<= PAGE_SHIFT;
330 #ifdef CONFIG_STACK_GROWSUP
331 return PAGE_ALIGN(stack_top) + random_variable;
333 return PAGE_ALIGN(stack_top) - random_variable;
338 * randomize_page - Generate a random, page aligned address
339 * @start: The smallest acceptable address the caller will take.
340 * @range: The size of the area, starting at @start, within which the
341 * random address must fall.
343 * If @start + @range would overflow, @range is capped.
345 * NOTE: Historical use of randomize_range, which this replaces, presumed that
346 * @start was already page aligned. We now align it regardless.
348 * Return: A page aligned address within [start, start + range). On error,
349 * @start is returned.
351 unsigned long randomize_page(unsigned long start, unsigned long range)
353 if (!PAGE_ALIGNED(start)) {
354 range -= PAGE_ALIGN(start) - start;
355 start = PAGE_ALIGN(start);
358 if (start > ULONG_MAX - range)
359 range = ULONG_MAX - start;
361 range >>= PAGE_SHIFT;
366 return start + (get_random_long() % range << PAGE_SHIFT);
369 #ifdef CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT
370 unsigned long __weak arch_randomize_brk(struct mm_struct *mm)
372 /* Is the current task 32bit ? */
373 if (!IS_ENABLED(CONFIG_64BIT) || is_compat_task())
374 return randomize_page(mm->brk, SZ_32M);
376 return randomize_page(mm->brk, SZ_1G);
379 unsigned long arch_mmap_rnd(void)
383 #ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS
384 if (is_compat_task())
385 rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1);
387 #endif /* CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS */
388 rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1);
390 return rnd << PAGE_SHIFT;
393 static int mmap_is_legacy(struct rlimit *rlim_stack)
395 if (current->personality & ADDR_COMPAT_LAYOUT)
398 if (rlim_stack->rlim_cur == RLIM_INFINITY)
401 return sysctl_legacy_va_layout;
405 * Leave enough space between the mmap area and the stack to honour ulimit in
406 * the face of randomisation.
408 #define MIN_GAP (SZ_128M)
409 #define MAX_GAP (STACK_TOP / 6 * 5)
411 static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack)
413 unsigned long gap = rlim_stack->rlim_cur;
414 unsigned long pad = stack_guard_gap;
416 /* Account for stack randomization if necessary */
417 if (current->flags & PF_RANDOMIZE)
418 pad += (STACK_RND_MASK << PAGE_SHIFT);
420 /* Values close to RLIM_INFINITY can overflow. */
426 else if (gap > MAX_GAP)
429 return PAGE_ALIGN(STACK_TOP - gap - rnd);
432 void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
434 unsigned long random_factor = 0UL;
436 if (current->flags & PF_RANDOMIZE)
437 random_factor = arch_mmap_rnd();
439 if (mmap_is_legacy(rlim_stack)) {
440 mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
441 mm->get_unmapped_area = arch_get_unmapped_area;
443 mm->mmap_base = mmap_base(random_factor, rlim_stack);
444 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
447 #elif defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT)
448 void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
450 mm->mmap_base = TASK_UNMAPPED_BASE;
451 mm->get_unmapped_area = arch_get_unmapped_area;
456 * __account_locked_vm - account locked pages to an mm's locked_vm
457 * @mm: mm to account against
458 * @pages: number of pages to account
459 * @inc: %true if @pages should be considered positive, %false if not
460 * @task: task used to check RLIMIT_MEMLOCK
461 * @bypass_rlim: %true if checking RLIMIT_MEMLOCK should be skipped
463 * Assumes @task and @mm are valid (i.e. at least one reference on each), and
464 * that mmap_lock is held as writer.
468 * * -ENOMEM if RLIMIT_MEMLOCK would be exceeded.
470 int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc,
471 struct task_struct *task, bool bypass_rlim)
473 unsigned long locked_vm, limit;
476 mmap_assert_write_locked(mm);
478 locked_vm = mm->locked_vm;
481 limit = task_rlimit(task, RLIMIT_MEMLOCK) >> PAGE_SHIFT;
482 if (locked_vm + pages > limit)
486 mm->locked_vm = locked_vm + pages;
488 WARN_ON_ONCE(pages > locked_vm);
489 mm->locked_vm = locked_vm - pages;
492 pr_debug("%s: [%d] caller %ps %c%lu %lu/%lu%s\n", __func__, task->pid,
493 (void *)_RET_IP_, (inc) ? '+' : '-', pages << PAGE_SHIFT,
494 locked_vm << PAGE_SHIFT, task_rlimit(task, RLIMIT_MEMLOCK),
495 ret ? " - exceeded" : "");
499 EXPORT_SYMBOL_GPL(__account_locked_vm);
502 * account_locked_vm - account locked pages to an mm's locked_vm
503 * @mm: mm to account against, may be NULL
504 * @pages: number of pages to account
505 * @inc: %true if @pages should be considered positive, %false if not
507 * Assumes a non-NULL @mm is valid (i.e. at least one reference on it).
510 * * 0 on success, or if mm is NULL
511 * * -ENOMEM if RLIMIT_MEMLOCK would be exceeded.
513 int account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc)
517 if (pages == 0 || !mm)
521 ret = __account_locked_vm(mm, pages, inc, current,
522 capable(CAP_IPC_LOCK));
523 mmap_write_unlock(mm);
527 EXPORT_SYMBOL_GPL(account_locked_vm);
529 unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr,
530 unsigned long len, unsigned long prot,
531 unsigned long flag, unsigned long pgoff)
534 struct mm_struct *mm = current->mm;
535 unsigned long populate;
538 ret = security_mmap_file(file, prot, flag);
540 if (mmap_write_lock_killable(mm))
542 ret = do_mmap(file, addr, len, prot, flag, pgoff, &populate,
544 mmap_write_unlock(mm);
545 userfaultfd_unmap_complete(mm, &uf);
547 mm_populate(ret, populate);
552 unsigned long vm_mmap(struct file *file, unsigned long addr,
553 unsigned long len, unsigned long prot,
554 unsigned long flag, unsigned long offset)
556 if (unlikely(offset + PAGE_ALIGN(len) < offset))
558 if (unlikely(offset_in_page(offset)))
561 return vm_mmap_pgoff(file, addr, len, prot, flag, offset >> PAGE_SHIFT);
563 EXPORT_SYMBOL(vm_mmap);
566 * kvmalloc_node - attempt to allocate physically contiguous memory, but upon
567 * failure, fall back to non-contiguous (vmalloc) allocation.
568 * @size: size of the request.
569 * @flags: gfp mask for the allocation - must be compatible (superset) with GFP_KERNEL.
570 * @node: numa node to allocate from
572 * Uses kmalloc to get the memory but if the allocation fails then falls back
573 * to the vmalloc allocator. Use kvfree for freeing the memory.
575 * GFP_NOWAIT and GFP_ATOMIC are not supported, neither is the __GFP_NORETRY modifier.
576 * __GFP_RETRY_MAYFAIL is supported, and it should be used only if kmalloc is
577 * preferable to the vmalloc fallback, due to visible performance drawbacks.
579 * Return: pointer to the allocated memory of %NULL in case of failure
581 void *kvmalloc_node(size_t size, gfp_t flags, int node)
583 gfp_t kmalloc_flags = flags;
587 * We want to attempt a large physically contiguous block first because
588 * it is less likely to fragment multiple larger blocks and therefore
589 * contribute to a long term fragmentation less than vmalloc fallback.
590 * However make sure that larger requests are not too disruptive - no
591 * OOM killer and no allocation failure warnings as we have a fallback.
593 if (size > PAGE_SIZE) {
594 kmalloc_flags |= __GFP_NOWARN;
596 if (!(kmalloc_flags & __GFP_RETRY_MAYFAIL))
597 kmalloc_flags |= __GFP_NORETRY;
599 /* nofail semantic is implemented by the vmalloc fallback */
600 kmalloc_flags &= ~__GFP_NOFAIL;
603 ret = kmalloc_node(size, kmalloc_flags, node);
606 * It doesn't really make sense to fallback to vmalloc for sub page
609 if (ret || size <= PAGE_SIZE)
612 /* non-sleeping allocations are not supported by vmalloc */
613 if (!gfpflags_allow_blocking(flags))
616 /* Don't even allow crazy sizes */
617 if (unlikely(size > INT_MAX)) {
618 WARN_ON_ONCE(!(flags & __GFP_NOWARN));
623 * kvmalloc() can always use VM_ALLOW_HUGE_VMAP,
624 * since the callers already cannot assume anything
625 * about the resulting pointer, and cannot play
628 return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
629 flags, PAGE_KERNEL, VM_ALLOW_HUGE_VMAP,
630 node, __builtin_return_address(0));
632 EXPORT_SYMBOL(kvmalloc_node);
635 * kvfree() - Free memory.
636 * @addr: Pointer to allocated memory.
638 * kvfree frees memory allocated by any of vmalloc(), kmalloc() or kvmalloc().
639 * It is slightly more efficient to use kfree() or vfree() if you are certain
640 * that you know which one to use.
642 * Context: Either preemptible task context or not-NMI interrupt.
644 void kvfree(const void *addr)
646 if (is_vmalloc_addr(addr))
651 EXPORT_SYMBOL(kvfree);
654 * kvfree_sensitive - Free a data object containing sensitive information.
655 * @addr: address of the data object to be freed.
656 * @len: length of the data object.
658 * Use the special memzero_explicit() function to clear the content of a
659 * kvmalloc'ed object containing sensitive data to make sure that the
660 * compiler won't optimize out the data clearing.
662 void kvfree_sensitive(const void *addr, size_t len)
664 if (likely(!ZERO_OR_NULL_PTR(addr))) {
665 memzero_explicit((void *)addr, len);
669 EXPORT_SYMBOL(kvfree_sensitive);
671 void *kvrealloc(const void *p, size_t oldsize, size_t newsize, gfp_t flags)
675 if (oldsize >= newsize)
677 newp = kvmalloc(newsize, flags);
680 memcpy(newp, p, oldsize);
684 EXPORT_SYMBOL(kvrealloc);
687 * __vmalloc_array - allocate memory for a virtually contiguous array.
688 * @n: number of elements.
689 * @size: element size.
690 * @flags: the type of memory to allocate (see kmalloc).
692 void *__vmalloc_array(size_t n, size_t size, gfp_t flags)
696 if (unlikely(check_mul_overflow(n, size, &bytes)))
698 return __vmalloc(bytes, flags);
700 EXPORT_SYMBOL(__vmalloc_array);
703 * vmalloc_array - allocate memory for a virtually contiguous array.
704 * @n: number of elements.
705 * @size: element size.
707 void *vmalloc_array(size_t n, size_t size)
709 return __vmalloc_array(n, size, GFP_KERNEL);
711 EXPORT_SYMBOL(vmalloc_array);
714 * __vcalloc - allocate and zero memory for a virtually contiguous array.
715 * @n: number of elements.
716 * @size: element size.
717 * @flags: the type of memory to allocate (see kmalloc).
719 void *__vcalloc(size_t n, size_t size, gfp_t flags)
721 return __vmalloc_array(n, size, flags | __GFP_ZERO);
723 EXPORT_SYMBOL(__vcalloc);
726 * vcalloc - allocate and zero memory for a virtually contiguous array.
727 * @n: number of elements.
728 * @size: element size.
730 void *vcalloc(size_t n, size_t size)
732 return __vmalloc_array(n, size, GFP_KERNEL | __GFP_ZERO);
734 EXPORT_SYMBOL(vcalloc);
736 /* Neutral page->mapping pointer to address_space or anon_vma or other */
737 void *page_rmapping(struct page *page)
739 return folio_raw_mapping(page_folio(page));
742 struct anon_vma *folio_anon_vma(struct folio *folio)
744 unsigned long mapping = (unsigned long)folio->mapping;
746 if ((mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON)
748 return (void *)(mapping - PAGE_MAPPING_ANON);
752 * folio_mapping - Find the mapping where this folio is stored.
755 * For folios which are in the page cache, return the mapping that this
756 * page belongs to. Folios in the swap cache return the swap mapping
757 * this page is stored in (which is different from the mapping for the
758 * swap file or swap device where the data is stored).
760 * You can call this for folios which aren't in the swap cache or page
761 * cache and it will return NULL.
763 struct address_space *folio_mapping(struct folio *folio)
765 struct address_space *mapping;
767 /* This happens if someone calls flush_dcache_page on slab page */
768 if (unlikely(folio_test_slab(folio)))
771 if (unlikely(folio_test_swapcache(folio)))
772 return swap_address_space(folio_swap_entry(folio));
774 mapping = folio->mapping;
775 if ((unsigned long)mapping & PAGE_MAPPING_FLAGS)
780 EXPORT_SYMBOL(folio_mapping);
783 * folio_copy - Copy the contents of one folio to another.
784 * @dst: Folio to copy to.
785 * @src: Folio to copy from.
787 * The bytes in the folio represented by @src are copied to @dst.
788 * Assumes the caller has validated that @dst is at least as large as @src.
789 * Can be called in atomic context for order-0 folios, but if the folio is
790 * larger, it may sleep.
792 void folio_copy(struct folio *dst, struct folio *src)
795 long nr = folio_nr_pages(src);
798 copy_highpage(folio_page(dst, i), folio_page(src, i));
805 int sysctl_overcommit_memory __read_mostly = OVERCOMMIT_GUESS;
806 int sysctl_overcommit_ratio __read_mostly = 50;
807 unsigned long sysctl_overcommit_kbytes __read_mostly;
808 int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT;
809 unsigned long sysctl_user_reserve_kbytes __read_mostly = 1UL << 17; /* 128MB */
810 unsigned long sysctl_admin_reserve_kbytes __read_mostly = 1UL << 13; /* 8MB */
812 int overcommit_ratio_handler(struct ctl_table *table, int write, void *buffer,
813 size_t *lenp, loff_t *ppos)
817 ret = proc_dointvec(table, write, buffer, lenp, ppos);
818 if (ret == 0 && write)
819 sysctl_overcommit_kbytes = 0;
823 static void sync_overcommit_as(struct work_struct *dummy)
825 percpu_counter_sync(&vm_committed_as);
828 int overcommit_policy_handler(struct ctl_table *table, int write, void *buffer,
829 size_t *lenp, loff_t *ppos)
836 * The deviation of sync_overcommit_as could be big with loose policy
837 * like OVERCOMMIT_ALWAYS/OVERCOMMIT_GUESS. When changing policy to
838 * strict OVERCOMMIT_NEVER, we need to reduce the deviation to comply
839 * with the strict "NEVER", and to avoid possible race condition (even
840 * though user usually won't too frequently do the switching to policy
841 * OVERCOMMIT_NEVER), the switch is done in the following order:
842 * 1. changing the batch
843 * 2. sync percpu count on each CPU
844 * 3. switch the policy
848 t.data = &new_policy;
849 ret = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
850 if (ret || new_policy == -1)
853 mm_compute_batch(new_policy);
854 if (new_policy == OVERCOMMIT_NEVER)
855 schedule_on_each_cpu(sync_overcommit_as);
856 sysctl_overcommit_memory = new_policy;
858 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
864 int overcommit_kbytes_handler(struct ctl_table *table, int write, void *buffer,
865 size_t *lenp, loff_t *ppos)
869 ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
870 if (ret == 0 && write)
871 sysctl_overcommit_ratio = 0;
876 * Committed memory limit enforced when OVERCOMMIT_NEVER policy is used
878 unsigned long vm_commit_limit(void)
880 unsigned long allowed;
882 if (sysctl_overcommit_kbytes)
883 allowed = sysctl_overcommit_kbytes >> (PAGE_SHIFT - 10);
885 allowed = ((totalram_pages() - hugetlb_total_pages())
886 * sysctl_overcommit_ratio / 100);
887 allowed += total_swap_pages;
893 * Make sure vm_committed_as in one cacheline and not cacheline shared with
894 * other variables. It can be updated by several CPUs frequently.
896 struct percpu_counter vm_committed_as ____cacheline_aligned_in_smp;
899 * The global memory commitment made in the system can be a metric
900 * that can be used to drive ballooning decisions when Linux is hosted
901 * as a guest. On Hyper-V, the host implements a policy engine for dynamically
902 * balancing memory across competing virtual machines that are hosted.
903 * Several metrics drive this policy engine including the guest reported
906 * The time cost of this is very low for small platforms, and for big
907 * platform like a 2S/36C/72T Skylake server, in worst case where
908 * vm_committed_as's spinlock is under severe contention, the time cost
909 * could be about 30~40 microseconds.
911 unsigned long vm_memory_committed(void)
913 return percpu_counter_sum_positive(&vm_committed_as);
915 EXPORT_SYMBOL_GPL(vm_memory_committed);
918 * Check that a process has enough memory to allocate a new virtual
919 * mapping. 0 means there is enough memory for the allocation to
920 * succeed and -ENOMEM implies there is not.
922 * We currently support three overcommit policies, which are set via the
923 * vm.overcommit_memory sysctl. See Documentation/mm/overcommit-accounting.rst
925 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
926 * Additional code 2002 Jul 20 by Robert Love.
928 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
930 * Note this is a helper function intended to be used by LSMs which
931 * wish to use this logic.
933 int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
937 vm_acct_memory(pages);
940 * Sometimes we want to use more memory than we have
942 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
945 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
946 if (pages > totalram_pages() + total_swap_pages)
951 allowed = vm_commit_limit();
953 * Reserve some for root
956 allowed -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
959 * Don't let a single process grow so big a user can't recover
962 long reserve = sysctl_user_reserve_kbytes >> (PAGE_SHIFT - 10);
964 allowed -= min_t(long, mm->total_vm / 32, reserve);
967 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
970 pr_warn_ratelimited("%s: pid: %d, comm: %s, not enough memory for the allocation\n",
971 __func__, current->pid, current->comm);
972 vm_unacct_memory(pages);
978 * get_cmdline() - copy the cmdline value to a buffer.
979 * @task: the task whose cmdline value to copy.
980 * @buffer: the buffer to copy to.
981 * @buflen: the length of the buffer. Larger cmdline values are truncated
984 * Return: the size of the cmdline field copied. Note that the copy does
985 * not guarantee an ending NULL byte.
987 int get_cmdline(struct task_struct *task, char *buffer, int buflen)
991 struct mm_struct *mm = get_task_mm(task);
992 unsigned long arg_start, arg_end, env_start, env_end;
996 goto out_mm; /* Shh! No looking before we're done */
998 spin_lock(&mm->arg_lock);
999 arg_start = mm->arg_start;
1000 arg_end = mm->arg_end;
1001 env_start = mm->env_start;
1002 env_end = mm->env_end;
1003 spin_unlock(&mm->arg_lock);
1005 len = arg_end - arg_start;
1010 res = access_process_vm(task, arg_start, buffer, len, FOLL_FORCE);
1013 * If the nul at the end of args has been overwritten, then
1014 * assume application is using setproctitle(3).
1016 if (res > 0 && buffer[res-1] != '\0' && len < buflen) {
1017 len = strnlen(buffer, res);
1021 len = env_end - env_start;
1022 if (len > buflen - res)
1024 res += access_process_vm(task, env_start,
1027 res = strnlen(buffer, res);
1036 int __weak memcmp_pages(struct page *page1, struct page *page2)
1038 char *addr1, *addr2;
1041 addr1 = kmap_atomic(page1);
1042 addr2 = kmap_atomic(page2);
1043 ret = memcmp(addr1, addr2, PAGE_SIZE);
1044 kunmap_atomic(addr2);
1045 kunmap_atomic(addr1);
1049 #ifdef CONFIG_PRINTK
1051 * mem_dump_obj - Print available provenance information
1052 * @object: object for which to find provenance information.
1054 * This function uses pr_cont(), so that the caller is expected to have
1055 * printed out whatever preamble is appropriate. The provenance information
1056 * depends on the type of object and on how much debugging is enabled.
1057 * For example, for a slab-cache object, the slab name is printed, and,
1058 * if available, the return address and stack trace from the allocation
1059 * and last free path of that object.
1061 void mem_dump_obj(void *object)
1065 if (kmem_valid_obj(object)) {
1066 kmem_dump_obj(object);
1070 if (vmalloc_dump_obj(object))
1073 if (virt_addr_valid(object))
1074 type = "non-slab/vmalloc memory";
1075 else if (object == NULL)
1076 type = "NULL pointer";
1077 else if (object == ZERO_SIZE_PTR)
1078 type = "zero-size pointer";
1080 type = "non-paged memory";
1082 pr_cont(" %s\n", type);
1084 EXPORT_SYMBOL_GPL(mem_dump_obj);
1088 * A driver might set a page logically offline -- PageOffline() -- and
1089 * turn the page inaccessible in the hypervisor; after that, access to page
1090 * content can be fatal.
1092 * Some special PFN walkers -- i.e., /proc/kcore -- read content of random
1093 * pages after checking PageOffline(); however, these PFN walkers can race
1094 * with drivers that set PageOffline().
1096 * page_offline_freeze()/page_offline_thaw() allows for a subsystem to
1097 * synchronize with such drivers, achieving that a page cannot be set
1098 * PageOffline() while frozen.
1100 * page_offline_begin()/page_offline_end() is used by drivers that care about
1101 * such races when setting a page PageOffline().
1103 static DECLARE_RWSEM(page_offline_rwsem);
1105 void page_offline_freeze(void)
1107 down_read(&page_offline_rwsem);
1110 void page_offline_thaw(void)
1112 up_read(&page_offline_rwsem);
1115 void page_offline_begin(void)
1117 down_write(&page_offline_rwsem);
1119 EXPORT_SYMBOL(page_offline_begin);
1121 void page_offline_end(void)
1123 up_write(&page_offline_rwsem);
1125 EXPORT_SYMBOL(page_offline_end);
1127 #ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_FOLIO
1128 void flush_dcache_folio(struct folio *folio)
1130 long i, nr = folio_nr_pages(folio);
1132 for (i = 0; i < nr; i++)
1133 flush_dcache_page(folio_page(folio, i));
1135 EXPORT_SYMBOL(flush_dcache_folio);