[RHEL7,COMMIT] mm: introduce kvmalloc_check() for safe calls to kvmalloc()

Submitted by Konstantin Khorenko on March 22, 2018, 10:28 a.m.

Details

Message ID 201803221028.w2MASJmG028481@finist_ce7.work
State New
Series "Series without cover letter"
Headers show

Commit Message

Konstantin Khorenko March 22, 2018, 10:28 a.m.
The commit is pushed to "branch-rh7-3.10.0-693.17.1.vz7.45.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh7-3.10.0-693.17.1.vz7.45.8
------>
commit 45485462f491779cc8e084766761ff9fc3347f72
Author: Konstantin Khorenko <khorenko@virtuozzo.com>
Date:   Thu Mar 22 13:28:19 2018 +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@virtuozzo.com>
    Reviewed-by: Andrey Ryabinin <aryabinin@virtuozzo.com>
---
 include/linux/mm.h |  1 +
 mm/util.c          | 14 ++++++++++++++
 2 files changed, 15 insertions(+)

Patch hide | download patch | download mbox

diff --git a/include/linux/mm.h b/include/linux/mm.h
index ad1e6c6953c2..6ddc426d13b6 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -413,6 +413,7 @@  static inline void *kvzalloc(size_t size, gfp_t flags)
 	return kvmalloc(size, flags | __GFP_ZERO);
 }
 
+extern void *kvmalloc_check(size_t size, gfp_t flags);
 extern void kvfree(const void *addr);
 
 static inline void compound_lock(struct page *page)
diff --git a/mm/util.c b/mm/util.c
index ee71b7bdc3d6..e24671847a2e 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -375,6 +375,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);
+
 void kvfree(const void *addr)
 {
 	if (is_vmalloc_addr(addr))