From: Dmitry Vyukov <dvyu...@google.com>

[ Upstream commit 61448479a9f2c954cde0cfe778cb6bec5d0a748d ]

Slub does not call kmalloc_slab() for sizes > KMALLOC_MAX_CACHE_SIZE,
instead it falls back to kmalloc_large().

For slab KMALLOC_MAX_CACHE_SIZE == KMALLOC_MAX_SIZE and it calls
kmalloc_slab() for all allocations relying on NULL return value for
over-sized allocations.

This inconsistency leads to unwanted warnings from kmalloc_slab() for
over-sized allocations for slab.  Returning NULL for failed allocations is
the expected behavior.

Make slub and slab code consistent by checking size >
KMALLOC_MAX_CACHE_SIZE in slab before calling kmalloc_slab().

While we are here also fix the check in kmalloc_slab().  We should check
against KMALLOC_MAX_CACHE_SIZE rather than KMALLOC_MAX_SIZE.  It all kinda
worked because for slab the constants are the same, and slub always checks
the size against KMALLOC_MAX_CACHE_SIZE before kmalloc_slab().  But if we
get there with size > KMALLOC_MAX_CACHE_SIZE anyhow bad things will
happen.  For example, in case of a newly introduced bug in slub code.

Also move the check in kmalloc_slab() from function entry to the size >
192 case.  This partially compensates for the additional check in slab
code and makes slub code a bit faster (at least theoretically).

Also drop __GFP_NOWARN in the warning check.  This warning means a bug in
slab code itself, user-passed flags have nothing to do with it.

Nothing of this affects slob.

Link: http://lkml.kernel.org/r/20180927171502.226522-1-dvyu...@gmail.com
Signed-off-by: Dmitry Vyukov <dvyu...@google.com>
Reported-by: syzbot+87829a10073277282...@syzkaller.appspotmail.com
Reported-by: syzbot+ef4e8fc3a06e9019b...@syzkaller.appspotmail.com
Reported-by: syzbot+6e438f4036df52cbb...@syzkaller.appspotmail.com
Reported-by: syzbot+8574471d8734457d9...@syzkaller.appspotmail.com
Reported-by: syzbot+af1504df0807a083d...@syzkaller.appspotmail.com
Acked-by: Christoph Lameter <c...@linux.com>
Acked-by: Vlastimil Babka <vba...@suse.cz>
Cc: Pekka Enberg <penb...@kernel.org>
Cc: David Rientjes <rient...@google.com>
Cc: Joonsoo Kim <iamjoonsoo....@lge.com>
Signed-off-by: Andrew Morton <a...@linux-foundation.org>
Signed-off-by: Linus Torvalds <torva...@linux-foundation.org>
Signed-off-by: Sasha Levin <sas...@kernel.org>
---
 mm/slab.c        |  4 ++++
 mm/slab_common.c | 12 ++++++------
 2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/mm/slab.c b/mm/slab.c
index 198c1e2c5358..68ab88e2920e 100644
--- a/mm/slab.c
+++ b/mm/slab.c
@@ -3670,6 +3670,8 @@ __do_kmalloc_node(size_t size, gfp_t flags, int node, 
unsigned long caller)
        struct kmem_cache *cachep;
        void *ret;
 
+       if (unlikely(size > KMALLOC_MAX_CACHE_SIZE))
+               return NULL;
        cachep = kmalloc_slab(size, flags);
        if (unlikely(ZERO_OR_NULL_PTR(cachep)))
                return cachep;
@@ -3705,6 +3707,8 @@ static __always_inline void *__do_kmalloc(size_t size, 
gfp_t flags,
        struct kmem_cache *cachep;
        void *ret;
 
+       if (unlikely(size > KMALLOC_MAX_CACHE_SIZE))
+               return NULL;
        cachep = kmalloc_slab(size, flags);
        if (unlikely(ZERO_OR_NULL_PTR(cachep)))
                return cachep;
diff --git a/mm/slab_common.c b/mm/slab_common.c
index 91d271b90600..f6764cf162b8 100644
--- a/mm/slab_common.c
+++ b/mm/slab_common.c
@@ -971,18 +971,18 @@ struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags)
 {
        int index;
 
-       if (unlikely(size > KMALLOC_MAX_SIZE)) {
-               WARN_ON_ONCE(!(flags & __GFP_NOWARN));
-               return NULL;
-       }
-
        if (size <= 192) {
                if (!size)
                        return ZERO_SIZE_PTR;
 
                index = size_index[size_index_elem(size)];
-       } else
+       } else {
+               if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) {
+                       WARN_ON(1);
+                       return NULL;
+               }
                index = fls(size - 1);
+       }
 
 #ifdef CONFIG_ZONE_DMA
        if (unlikely((flags & GFP_DMA)))
-- 
2.17.1

Reply via email to