* [PATCH v2] mm/slab: reject unsupported kmalloc() sizes
@ 2026-08-31 2:23 Zi Yan
2026-08-31 9:46 ` Vlastimil Babka (SUSE)
2026-09-02 11:12 ` Harry Yoo
0 siblings, 2 replies; 3+ messages in thread
From: Zi Yan @ 2026-08-31 2:23 UTC (permalink / raw)
To: Vlastimil Babka, Harry Yoo, Andrew Morton, Hao Li,
Christoph Lameter, David Rientjes, Roman Gushchin, Alan Stern,
Greg Kroah-Hartman
Cc: linux-mm, linux-kernel, Zi Yan
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.
Suggested-by: "Vlastimil Babka (SUSE)" <vbabka@kernel.org>
Signed-off-by: Zi Yan <ziy@nvidia.com>
---
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/20260817-limit_kmalloc_size-v1-1-5bef487701cc@nvidia.com
---
mm/slub.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/mm/slub.c b/mm/slub.c
index f9b56cb439e70..23e57a8fe55b4 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -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,
--
Yan, Zi
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v2] mm/slab: reject unsupported kmalloc() sizes
2026-08-31 2:23 [PATCH v2] mm/slab: reject unsupported kmalloc() sizes Zi Yan
@ 2026-08-31 9:46 ` Vlastimil Babka (SUSE)
2026-09-02 11:12 ` Harry Yoo
1 sibling, 0 replies; 3+ messages in thread
From: Vlastimil Babka (SUSE) @ 2026-08-31 9:46 UTC (permalink / raw)
To: Zi Yan, Harry Yoo, Andrew Morton, Hao Li, Christoph Lameter,
David Rientjes, Roman Gushchin, Alan Stern, Greg Kroah-Hartman
Cc: linux-mm, linux-kernel
On 8/31/26 04:23, Zi Yan wrote:
> 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.
>
> Suggested-by: "Vlastimil Babka (SUSE)" <vbabka@kernel.org>
> Signed-off-by: Zi Yan <ziy@nvidia.com>
Thanks, added to mm/slab.git slab/for-next
> ---
> 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/20260817-limit_kmalloc_size-v1-1-5bef487701cc@nvidia.com
> ---
> mm/slub.c | 7 ++++++-
> 1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/mm/slub.c b/mm/slub.c
> index f9b56cb439e70..23e57a8fe55b4 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -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,
> --
> Yan, Zi
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] mm/slab: reject unsupported kmalloc() sizes
2026-08-31 2:23 [PATCH v2] mm/slab: reject unsupported kmalloc() sizes Zi Yan
2026-08-31 9:46 ` Vlastimil Babka (SUSE)
@ 2026-09-02 11:12 ` Harry Yoo
1 sibling, 0 replies; 3+ messages in thread
From: Harry Yoo @ 2026-09-02 11:12 UTC (permalink / raw)
To: Zi Yan
Cc: Vlastimil Babka, Andrew Morton, Hao Li, Christoph Lameter,
David Rientjes, Roman Gushchin, Alan Stern, Greg Kroah-Hartman,
linux-mm, linux-kernel
On Sun, Aug 30, 2026 at 10:23:23PM -0400, Zi Yan wrote:
> 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.
>
> Suggested-by: "Vlastimil Babka (SUSE)" <vbabka@kernel.org>
> Signed-off-by: Zi Yan <ziy@nvidia.com>
> ---
Looks good to me,
Reviewed-by: Harry Yoo (Meta) <harry@kernel.org>
--
Cheers,
Harry / Hyeonggon
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-09-02 11:12 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-31 2:23 [PATCH v2] mm/slab: reject unsupported kmalloc() sizes Zi Yan
2026-08-31 9:46 ` Vlastimil Babka (SUSE)
2026-09-02 11:12 ` Harry Yoo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox