mm/slab: reject unsupported kmalloc() sizes
Subsystems (2)
PATCH v2 1/1 mm/slab: reject unsupported kmalloc() sizes
kmalloc() is used to allocate physically contiguous memory for kernel allocations. For requests larger than KMALLOC_MAX_CACHE_SIZE, kmalloc() uses the page allocator and can only support up to KMALLOC_MAX_SIZE. For request sizes bigger than KMALLOC_MAX_SIZE, the page allocator can emit a WARN because kmalloc() allocates an order greater than MAX_PAGE_ORDER. To make the KMALLOC_MAX_SIZE limit more explicit instead of relying on the page allocator check, add a KMALLOC_MAX_SIZE check inside kmalloc() to emit a WARN and return NULL for requested sizes bigger than KMALLOC_MAX_SIZE. Like the WARN in the page allocator and kvmalloc(), allow __GFP_NOWARN to suppress the WARN.
---
Instead of passing requests greater than KMALLOC_MAX_SIZE to the page
allocator, reject them early inside kmalloc().
---
Changes in v2:
1. Rejected kmalloc() request sizes bigger than KMALLOC_MAX_SIZE by
emitting a WARN and returning NULL inside kmalloc(). The WARN can be suppressed by __GFP_NOWARN. So the page allocator will no longer see excessively large requests from kmalloc().
- Link to v1: https://patch.msgid.link/[email protected]
--- mm/slub.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
mm/slub.c +6 -1
@@ -5342,7 +5342,12 @@ static void *___kmalloc_large_node(size_t size, gfp_t flags, int node) {struct page *page;void *ptr =NULL ; -unsigned int order =get_order (size); +unsigned int order; + +if (WARN_ON_ONCE_GFP (size >KMALLOC_MAX_SIZE , flags)) +return NULL ; + + order =get_order (size);if (unlikely (flags &GFP_SLAB_BUG_MASK )) flags =kmalloc_fix_flags (flags);
---
base-commit: cee9395acd8043be0644b25c34bfa86623f2b935
change-id: 20260817-limit_kmalloc_size-3a4a2c73beac
Best regards,
Signature · 1 line
Yan, Zi
Vlastimil Babka (SUSE)
maintainerQuoted 11 lines · Zi Yan
kmalloc()is used to allocate physically contiguous memory for kernel allocations. For requests larger thanKMALLOC_MAX_CACHE_SIZE,kmalloc()uses the page allocator and can only support up toKMALLOC_MAX_SIZE. For request sizes bigger thanKMALLOC_MAX_SIZE, the page allocator can emit a WARN becausekmalloc()allocates an order greater thanMAX_PAGE_ORDER. To make theKMALLOC_MAX_SIZElimit more explicit instead of relying on the page allocator check, add aKMALLOC_MAX_SIZEcheck insidekmalloc()to emit a WARN and return NULL for requested sizes bigger thanKMALLOC_MAX_SIZE. Like the WARN in the page allocator andkvmalloc(), allow__GFP_NOWARNto suppress the WARN.
Quoted 2 lines
Suggested-by "Vlastimil Babka (SUSE)" <[email protected]>Signed-off-by Zi Yan <[email protected]>
Thanks, added to mm/slab.git slab/for-next
Quoted 41 lines
---
Instead of passing requests greater thanKMALLOC_MAX_SIZEto the page
allocator, reject them early insidekmalloc().
---
Changes in v2:
1. Rejectedkmalloc()request sizes bigger thanKMALLOC_MAX_SIZEby
emitting a WARN and returning NULL insidekmalloc(). The WARN can be suppressed by__GFP_NOWARN. So the page allocator will no longer see excessively large requests fromkmalloc().
- Link to v1: https://patch.msgid.link/[email protected]--- mm/slub.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)mm/slub.c +6
-2@@ -5342,7 +5342,12 @@ static void *___kmalloc_large_node(size_t size, gfp_t flags, int node) {struct page *page;void *ptr =NULL ; -unsigned int order =get_order (size); +unsigned int order; + +if (WARN_ON_ONCE_GFP (size >KMALLOC_MAX_SIZE , flags)) +return NULL ; + + order =get_order (size);if (unlikely (flags &GFP_SLAB_BUG_MASK )) flags =kmalloc_fix_flags (flags); ---base-commit:
cee9395acd8043be0644b25c34bfa86623f2b935
change-id: 20260817-limit_kmalloc_size-3a4a2c73beacBest regards,
Signature · 1 line
Yan, Zi
Quoted 11 lines · Zi Yan
kmalloc()is used to allocate physically contiguous memory for kernel allocations. For requests larger thanKMALLOC_MAX_CACHE_SIZE,kmalloc()uses the page allocator and can only support up toKMALLOC_MAX_SIZE. For request sizes bigger thanKMALLOC_MAX_SIZE, the page allocator can emit a WARN becausekmalloc()allocates an order greater thanMAX_PAGE_ORDER. To make theKMALLOC_MAX_SIZElimit more explicit instead of relying on the page allocator check, add aKMALLOC_MAX_SIZEcheck insidekmalloc()to emit a WARN and return NULL for requested sizes bigger thanKMALLOC_MAX_SIZE. Like the WARN in the page allocator andkvmalloc(), allow__GFP_NOWARNto suppress the WARN.
Quoted 3 lines
Suggested-by "Vlastimil Babka (SUSE)" <[email protected]>Signed-off-by Zi Yan <[email protected]>---
Looks good to me,
Signature · 2 lines
Cheers, Harry / Hyeonggon