Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

objpool: fix to make percpu slot allocation more robust

Since gfp & GFP_ATOMIC == GFP_ATOMIC is true for GFP_KERNEL | GFP_HIGH, it
will use kmalloc if user specifies that combination. Here the reason why
combining the __vmalloc_node() and kmalloc_node() is that the vmalloc does
not support all GFP flag, especially GFP_ATOMIC. So we should check if
gfp & (GFP_ATOMIC | GFP_KERNEL) != GFP_ATOMIC for vmalloc first. This
ensures caller can sleep. And for the robustness, even if vmalloc fails,
it should retry with kmalloc to allocate it.

Link: https://lkml.kernel.org/r/173008598713.1262174.2959179484209897252.stgit@mhiramat.roam.corp.google.com
Fixes: aff1871bfc81 ("objpool: fix choosing allocation for percpu slots")
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Reported-by: Linus Torvalds <torvalds@linux-foundation.org>
Closes: https://lore.kernel.org/all/CAHk-=whO+vSH+XVRio8byJU8idAWES0SPGVZ7KAVdc4qrV0VUA@mail.gmail.com/
Cc: Leo Yan <leo.yan@arm.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Matt Wu <wuqiang.matt@bytedance.com>
Cc: Mikel Rychliski <mikel@mikelr.com>
Cc: Steven Rostedt (Google) <rostedt@goodmis.org>
Cc: Viktor Malik <vmalik@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Masami Hiramatsu (Google) and committed by
Andrew Morton
cb6fcef8 c928807f

+12 -6
+12 -6
lib/objpool.c
··· 74 74 * warm caches and TLB hits. in default vmalloc is used to 75 75 * reduce the pressure of kernel slab system. as we know, 76 76 * mimimal size of vmalloc is one page since vmalloc would 77 - * always align the requested size to page size 77 + * always align the requested size to page size. 78 + * but if vmalloc fails or it is not available (e.g. GFP_ATOMIC) 79 + * allocate percpu slot with kmalloc. 78 80 */ 79 - if ((pool->gfp & GFP_ATOMIC) == GFP_ATOMIC) 80 - slot = kmalloc_node(size, pool->gfp, cpu_to_node(i)); 81 - else 81 + slot = NULL; 82 + 83 + if ((pool->gfp & (GFP_ATOMIC | GFP_KERNEL)) != GFP_ATOMIC) 82 84 slot = __vmalloc_node(size, sizeof(void *), pool->gfp, 83 85 cpu_to_node(i), __builtin_return_address(0)); 84 - if (!slot) 85 - return -ENOMEM; 86 + 87 + if (!slot) { 88 + slot = kmalloc_node(size, pool->gfp, cpu_to_node(i)); 89 + if (!slot) 90 + return -ENOMEM; 91 + } 86 92 memset(slot, 0, size); 87 93 pool->cpu_slots[i] = slot; 88 94