[Devel] [PATCH RHEL COMMIT] mm: introduce kvmalloc_check() for safe calls to kvmalloc()

Konstantin Khorenko khorenko at virtuozzo.com
Thu Sep 30 16:04:03 MSK 2021


The commit is pushed to "branch-rh9-5.14.vz9.1.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after ark-5.14
------>
commit 3fd11b65ed7165ae25a0965854380c10f963e626
Author: Konstantin Khorenko <khorenko at virtuozzo.com>
Date:   Thu Sep 30 16:04:03 2021 +0300

    mm: introduce kvmalloc_check() for safe calls to kvmalloc()
    
    Sometimes a function which allocates high order pages is called with different
    flags while kvmalloc() should be called with at least GFP_KERNEL.
    
    kvmalloc_check() function uses kvmalloc() when possible depending on flags
    provided.
    
    https://jira.sw.ru/browse/PSBM-82593
    
    Signed-off-by: Konstantin Khorenko <khorenko at virtuozzo.com>
    
    Reviewed-by: Andrey Ryabinin <aryabinin at virtuozzo.com>
    
    [VvS RHEL77b rebase]
    
    (cherry picked from vz7 commit def125fece27 ("mm: introduce kvmalloc_check() for
    safe calls to kvmalloc()"))
    
    VZ 8 rebase part https://jira.sw.ru/browse/PSBM-127798
    
    Signed-off-by: Alexander Mikhalitsyn <alexander.mikhalitsyn at virtuozzo.com>
    
    (cherry picked from vz8 commit 0b60ec03b340b485aad331df6ada518259a0cc5e)
    Signed-off-by: Andrey Zhadchenko <andrey.zhadchenko at virtuozzo.com>
---
 include/linux/mm.h |  1 +
 mm/util.c          | 15 +++++++++++++++
 2 files changed, 16 insertions(+)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 41e81911cd9a..63380ba4ecaa 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -830,6 +830,7 @@ static inline void *kvcalloc(size_t n, size_t size, gfp_t flags)
 	return kvmalloc_array(n, size, flags | __GFP_ZERO);
 }
 
+extern void *kvmalloc_check(size_t size, gfp_t flags);
 extern void kvfree(const void *addr);
 extern void kvfree_sensitive(const void *addr, size_t len);
 
diff --git a/mm/util.c b/mm/util.c
index 9043d03750a7..90f35714ec8a 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -598,6 +598,20 @@ void *kvmalloc_node(size_t size, gfp_t flags, int node)
 }
 EXPORT_SYMBOL(kvmalloc_node);
 
+/*
+ * Sometimes a function which allocates high order pages is called with
+ * different flags while kvmalloc() should be called with at least GFP_KERNEL.
+ * This function uses kvmalloc() when possible depending on flags provided.
+ */
+void *kvmalloc_check(size_t size, gfp_t flags)
+{
+	if ((flags & GFP_KERNEL) == GFP_KERNEL)
+		return kvmalloc(size, flags);
+	else
+		return kmalloc(size, flags);
+}
+EXPORT_SYMBOL(kvmalloc_check);
+
 /**
  * kvfree() - Free memory.
  * @addr: Pointer to allocated memory.
@@ -608,6 +622,7 @@ EXPORT_SYMBOL(kvmalloc_node);
  *
  * Context: Either preemptible task context or not-NMI interrupt.
  */
+
 void kvfree(const void *addr)
 {
 	if (is_vmalloc_addr(addr))


More information about the Devel mailing list