Skip to contentSkip to conversation

mm/slab: reject unsupported kmalloc sizes

PATCH Superseded Zi Yan 5 participants 12 messages Aug 17 – Aug 30

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Zi Yan

author
#0 lore raw

PATCH 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. Systems with panic_on_warn=1 crash because of this WARN. Fix it by rejecting any kmalloc size bigger than KMALLOC_MAX_SIZE.

Fixes aadb4bc4a1f9 ("SLUB: direct pass through of page size or higher kmalloc requests")
Reported-by [email protected]

---
It fixes a page allocator warning (order > MAX_PAGE_ORDER) when gadgetfs requests excessively large memory from kmalloc. Instead of adding __GFP_NOWARN to suppress the warning, as was done for usbfs[1], change kmalloc to return NULL without a warning for this specific issue.

[1] commit 4f2629ea67e72 ("USB: usbfs: Don't WARN about excessively large memory allocations")

---
 mm/slub.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
mm/slub.c +6 -1
index 0337e60db5ace..a3071f4ef1945 100644
@@ -5263,7 +5263,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 (size > KMALLOC_MAX_SIZE)
+		return NULL;
+
+	order = get_order(size);
 
 	if (unlikely(flags & GFP_SLAB_BUG_MASK))
 		flags = kmalloc_fix_flags(flags);

---
base-commit: 8d3ae59288f1e7d58d76558a6ee96d533bc5019f
change-id: 20260817-limit_kmalloc_size-3a4a2c73beac

Best regards,

Signature · 1 line
Yan, Zi
#1 lore raw
Quoted 14 lines · 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. Systems with panic_on_warn=1 crash because of this WARN. Fix it by rejecting any kmalloc size bigger than KMALLOC_MAX_SIZE.

Fixes aadb4bc4a1f9 ("SLUB: direct pass through of page size or higher kmalloc requests")
Reported-by [email protected]
Quoted 7 lines

---
It fixes a page allocator warning (order > MAX_PAGE_ORDER) when gadgetfs requests excessively large memory from kmalloc. Instead of adding __GFP_NOWARN to suppress the warning, as was done for usbfs[1], change kmalloc to return NULL without a warning for this specific issue.

[1] commit 4f2629ea67e72 ("USB: usbfs: Don't WARN about excessively large memory allocations")

Thanks for doing this. If you like, I can write a follow-up patch to remove the __GFP_NOWARN added in 4f2629ea67e72, now that it isn't needed any more.

Alan Stern

author
#2 lore raw
Quoted 21 lines · Alan Stern
Quoted 20 lines · 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. Systems with panic_on_warn=1 crash because of this WARN. Fix it by rejecting any kmalloc size bigger than KMALLOC_MAX_SIZE.

Fixes aadb4bc4a1f9 ("SLUB: direct pass through of page size or higher kmalloc requests")
Reported-by [email protected]

---
It fixes a page allocator warning (order > MAX_PAGE_ORDER) when gadgetfs requests excessively large memory from kmalloc. Instead of adding __GFP_NOWARN to suppress the warning, as was done for usbfs[1], change kmalloc to return NULL without a warning for this specific issue.

Quoted 5 lines
Quoted 1 line

[1] commit 4f2629ea67e72 ("USB: usbfs: Don't WARN about excessively large memory allocations")

Thanks for doing this. If you like, I can write a follow-up patch to remove the __GFP_NOWARN added in 4f2629ea67e72, now that it isn't needed any more.

Sure. Once the patch gets an Ack or Rb from a maintainer and Andrew picks it up, feel free to send a cleanup patch. Thanks.

Signature · 2 lines
Best Regards,
Yan, Zi
#3 lore raw
In reply to Zi Yan
Quoted 14 lines · 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. Systems with panic_on_warn=1 crash because of this WARN. Fix it by rejecting any kmalloc size bigger than KMALLOC_MAX_SIZE.

Fixes aadb4bc4a1f9 ("SLUB: direct pass through of page size or higher kmalloc requests")
Reported-by [email protected]
Quoted 7 lines

---
It fixes a page allocator warning (order > MAX_PAGE_ORDER) when gadgetfs requests excessively large memory from kmalloc. Instead of adding __GFP_NOWARN to suppress the warning, as was done for usbfs[1], change kmalloc to return NULL without a warning for this specific issue.

[1] commit 4f2629ea67e72 ("USB: usbfs: Don't WARN about excessively large memory allocations")

So I checked and for kvmalloc() we have in __kvmalloc_node_noprof()

        /* Don't even allow crazy sizes */
        if (unlikely(size > INT_MAX)) {
                WARN_ON_ONCE(!(flags & __GFP_NOWARN));
                return NULL;
        }

This comes from Linus in commit 7661809d493b4. I'd do the same thing here then. Thus there would be a useful warning for e.g. development mistakes resulting in the size to be unexpectedly high.

Callers passing size that comes from userspace or similar untrusted source can either pass __GFP_NOWARN or sanitize the size to what they expect to be sane (which is context dependent and I assume actually way lower than kmalloc limits in practice). Note that passing even sizes within but close to/at the limit, trusting blindly some external source, can succeed the allocations but effectively DoS the system with heavy reclaim/compaction. So caller sanitization should still be preferred IMHO. Some of the recent arguments from Linus [1] would apply to this too, I think.

[1]
https://lore.kernel.org/all/CAHk-=wiSmgwwLKCqJwGS-dVHnSLU8W+7q1UQq-G9=TBGGZbuhQ@mail.gmail.com/

Quoted 31 lines
---
 mm/slub.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
mm/slub.c +6 -2
index 0337e60db5ace..a3071f4ef1945 100644
@@ -5263,7 +5263,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 (size > KMALLOC_MAX_SIZE)
+		return NULL;
+
+	order = get_order(size);
 
 	if (unlikely(flags & GFP_SLAB_BUG_MASK))
 		flags = kmalloc_fix_flags(flags);
 
---

base-commit: 8d3ae59288f1e7d58d76558a6ee96d533bc5019f
change-id: 20260817-limit_kmalloc_size-3a4a2c73beac

Best regards,

Signature · 1 line
Yan, Zi
#4 lore raw
Quoted 35 lines · On Wed, 26 Aug 2026 11:28:12 +0200 "Vlastimil Babka (SUSE)"…
Quoted 14 lines · 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. Systems with panic_on_warn=1 crash because of this WARN. Fix it by rejecting any kmalloc size bigger than KMALLOC_MAX_SIZE.

Fixes aadb4bc4a1f9 ("SLUB: direct pass through of page size or higher kmalloc requests")
Reported-by [email protected]
Quoted 7 lines

---
It fixes a page allocator warning (order > MAX_PAGE_ORDER) when gadgetfs requests excessively large memory from kmalloc. Instead of adding __GFP_NOWARN to suppress the warning, as was done for usbfs[1], change kmalloc to return NULL without a warning for this specific issue.

[1] commit 4f2629ea67e72 ("USB: usbfs: Don't WARN about excessively large memory allocations")

So I checked and for kvmalloc() we have in __kvmalloc_node_noprof()

        /* Don't even allow crazy sizes */
        if (unlikely(size > INT_MAX)) {
                WARN_ON_ONCE(!(flags & __GFP_NOWARN));
                return NULL;
        }

This comes from Linus in commit 7661809d493b4. I'd do the same thing here then. Thus there would be a useful warning for e.g. development mistakes resulting in the size to be unexpectedly high.

Quoted 7 lines

Callers passing size that comes from userspace or similar untrusted source can either pass __GFP_NOWARN or sanitize the size to what they expect to be sane (which is context dependent and I assume actually way lower than kmalloc limits in practice). Note that passing even sizes within but close to/at the limit, trusting blindly some external source, can succeed the allocations but effectively DoS the system with heavy reclaim/compaction. So caller sanitization should still be preferred IMHO.

Indeed, and sanitising the values early on saves all the size_add() and size_mult() operations (that are just saturating maths) but still let through the 'DoS the system' sizes.
Mostly the actual maximum size is actually small. I suspect limits like 64k, 1M or 16M would be appropriate.

David

Quoted 39 lines

Some of the recent
arguments from Linus [1] would apply to this too, I think.

[1]
https://lore.kernel.org/all/CAHk-=wiSmgwwLKCqJwGS-dVHnSLU8W+7q1UQq-G9=TBGGZbuhQ@mail.gmail.com/

Quoted 31 lines
---
 mm/slub.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
mm/slub.c +6 -2
index 0337e60db5ace..a3071f4ef1945 100644
@@ -5263,7 +5263,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 (size > KMALLOC_MAX_SIZE)
+		return NULL;
+
+	order = get_order(size);
 
 	if (unlikely(flags & GFP_SLAB_BUG_MASK))
 		flags = kmalloc_fix_flags(flags);
 
---

base-commit: 8d3ae59288f1e7d58d76558a6ee96d533bc5019f
change-id: 20260817-limit_kmalloc_size-3a4a2c73beac

Best regards,

Signature · 1 line
Yan, Zi
author
#5 lore raw
Quoted 34 lines · David Laight
Quoted 31 lines · On Wed, 26 Aug 2026 11:28:12 +0200 "Vlastimil Babka (SUSE)"…
Quoted 14 lines · 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. Systems with panic_on_warn=1 crash because of this WARN. Fix it by rejecting any kmalloc size bigger than KMALLOC_MAX_SIZE.

Fixes aadb4bc4a1f9 ("SLUB: direct pass through of page size or higher kmalloc requests")
Reported-by [email protected]
Quoted 7 lines

---
It fixes a page allocator warning (order > MAX_PAGE_ORDER) when gadgetfs requests excessively large memory from kmalloc. Instead of adding __GFP_NOWARN to suppress the warning, as was done for usbfs[1], change kmalloc to return NULL without a warning for this specific issue.

[1] commit 4f2629ea67e72 ("USB: usbfs: Don't WARN about excessively large memory allocations")

So I checked and for kvmalloc() we have in __kvmalloc_node_noprof()

        /* Don't even allow crazy sizes */
        if (unlikely(size > INT_MAX)) {
                WARN_ON_ONCE(!(flags & __GFP_NOWARN));
                return NULL;
        }
Quoted 3 lines
Quoted 3 lines

This comes from Linus in commit 7661809d493b4. I'd do the same thing here then. Thus there would be a useful warning for e.g. development mistakes resulting in the size to be unexpectedly high.

You mean kmalloc should also have this check?

Quoted 9 lines
Quoted 8 lines

Callers passing size that comes from userspace or similar untrusted source can either pass __GFP_NOWARN or sanitize the size to what they expect to be sane (which is context dependent and I assume actually way lower than kmalloc limits in practice). Note that passing even sizes within but close to/at the limit, trusting blindly some external source, can succeed the allocations but effectively DoS the system with heavy reclaim/compaction. So caller sanitization should still be preferred IMHO.

Quoted 3 lines

Indeed, and sanitising the values early on saves all the size_add() and size_mult() operations (that are just saturating maths) but still let through the 'DoS the system' sizes.

Yes, I am all for santizing inputs.

Quoted 2 lines

Mostly the actual maximum size is actually small. I suspect limits like 64k, 1M or 16M would be appropriate.

I agree that the limit you suggested is better. One thing to consider is that Mike is converting all __get_free_pages() to kmalloc[1], so the limit might end up to be 2^MAX_PAGE_ORDER, matching the page allocator limit.

[1] https://lore.kernel.org/all/[email protected]/

Quoted 42 lines

David

Quoted 39 lines

Some of the recent
arguments from Linus [1] would apply to this too, I think.

[1]
https://lore.kernel.org/all/CAHk-=wiSmgwwLKCqJwGS-dVHnSLU8W+7q1UQq-G9=TBGGZbuhQ@mail.gmail.com/

Quoted 31 lines
---
 mm/slub.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
mm/slub.c +6 -2
index 0337e60db5ace..a3071f4ef1945 100644
@@ -5263,7 +5263,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 (size > KMALLOC_MAX_SIZE)
+		return NULL;
+
+	order = get_order(size);
 
 	if (unlikely(flags & GFP_SLAB_BUG_MASK))
 		flags = kmalloc_fix_flags(flags);
 
---

base-commit: 8d3ae59288f1e7d58d76558a6ee96d533bc5019f
change-id: 20260817-limit_kmalloc_size-3a4a2c73beac

Best regards,

Signature · 1 line
Yan, Zi
Signature · 2 lines
Best Regards,
Yan, Zi
maintainer
#6 lore raw
In reply to Vlastimil Babka (SUSE)
Quoted 25 lines · Vlastimil Babka (SUSE)
Quoted 14 lines · 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. Systems with panic_on_warn=1 crash because of this WARN. Fix it by rejecting any kmalloc size bigger than KMALLOC_MAX_SIZE.

Fixes aadb4bc4a1f9 ("SLUB: direct pass through of page size or higher kmalloc requests")
Reported-by [email protected]
Quoted 7 lines

---
It fixes a page allocator warning (order > MAX_PAGE_ORDER) when gadgetfs requests excessively large memory from kmalloc. Instead of adding __GFP_NOWARN to suppress the warning, as was done for usbfs[1], change kmalloc to return NULL without a warning for this specific issue.

[1] commit 4f2629ea67e72 ("USB: usbfs: Don't WARN about excessively large memory allocations")

So I checked and for kvmalloc() we have in __kvmalloc_node_noprof()

Quoted 8 lines
        /* Don't even allow crazy sizes */
        if (unlikely(size > INT_MAX)) {
                WARN_ON_ONCE(!(flags & __GFP_NOWARN));
                return NULL;
        }

This comes from Linus in commit 7661809d493b4. I'd do the same thing here then.

But the purpose of this patch is to avoid the warning in the page allocator. Should we fix this in the caller (gadgetfs) then?

Quoted 14 lines

Thus there would be a useful warning for e.g. development mistakes resulting in the size to be unexpectedly high.

Callers passing size that comes from userspace or similar untrusted source can either pass __GFP_NOWARN or sanitize the size to what they expect to be sane (which is context dependent and I assume actually way lower than kmalloc limits in practice). Note that passing even sizes within but close to/at the limit, trusting blindly some external source, can succeed the allocations but effectively DoS the system with heavy reclaim/compaction. So caller sanitization should still be preferred IMHO. Some of the recent arguments from Linus [1] would apply to this too, I think.

[1]
https://lore.kernel.org/all/CAHk-=wiSmgwwLKCqJwGS-dVHnSLU8W+7q1UQq-G9=TBGGZbuhQ@mail.gmail.com/

Signature · 2 lines
Cheers,
Harry / Hyeonggon
author
#7 lore raw
Quoted 32 lines · Harry Yoo
Quoted 31 lines · Vlastimil Babka (SUSE)
Quoted 14 lines · 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. Systems with panic_on_warn=1 crash because of this WARN. Fix it by rejecting any kmalloc size bigger than KMALLOC_MAX_SIZE.

Fixes aadb4bc4a1f9 ("SLUB: direct pass through of page size or higher kmalloc requests")
Reported-by [email protected]
Quoted 7 lines

---
It fixes a page allocator warning (order > MAX_PAGE_ORDER) when gadgetfs requests excessively large memory from kmalloc. Instead of adding __GFP_NOWARN to suppress the warning, as was done for usbfs[1], change kmalloc to return NULL without a warning for this specific issue.

[1] commit 4f2629ea67e72 ("USB: usbfs: Don't WARN about excessively large memory allocations")

So I checked and for kvmalloc() we have in __kvmalloc_node_noprof()

        /* Don't even allow crazy sizes */
        if (unlikely(size > INT_MAX)) {
                WARN_ON_ONCE(!(flags & __GFP_NOWARN));
                return NULL;
        }
Quoted 5 lines
Quoted 2 lines

This comes from Linus in commit 7661809d493b4. I'd do the same thing here then.

But the purpose of this patch is to avoid the warning in the page allocator. Should we fix this in the caller (gadgetfs) then?

It is fixed by: https://lore.kernel.org/all/[email protected]/

Please disregard this patch, but we can keep the discussion going.

Quoted 15 lines
Quoted 15 lines

Thus there would be a useful warning for e.g. development mistakes resulting in the size to be unexpectedly high.

Callers passing size that comes from userspace or similar untrusted source can either pass __GFP_NOWARN or sanitize the size to what they expect to be sane (which is context dependent and I assume actually way lower than kmalloc limits in practice). Note that passing even sizes within but close to/at the limit, trusting blindly some external source, can succeed the allocations but effectively DoS the system with heavy reclaim/compaction. So caller sanitization should still be preferred IMHO. Some of the recent arguments from Linus [1] would apply to this too, I think.

[1]
https://lore.kernel.org/all/CAHk-=wiSmgwwLKCqJwGS-dVHnSLU8W+7q1UQq-G9=TBGGZbuhQ@mail.gmail.com/

Signature · 2 lines
Best Regards,
Yan, Zi
#8 lore raw
Quoted 36 lines · Zi Yan
Quoted 35 lines · Harry Yoo
Quoted 33 lines · Vlastimil Babka (SUSE)
Quoted 14 lines · 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. Systems with panic_on_warn=1 crash because of this WARN. Fix it by rejecting any kmalloc size bigger than KMALLOC_MAX_SIZE.

Fixes aadb4bc4a1f9 ("SLUB: direct pass through of page size or higher kmalloc requests")
Reported-by [email protected]
Quoted 7 lines

---
It fixes a page allocator warning (order > MAX_PAGE_ORDER) when gadgetfs requests excessively large memory from kmalloc. Instead of adding __GFP_NOWARN to suppress the warning, as was done for usbfs[1], change kmalloc to return NULL without a warning for this specific issue.

[1] commit 4f2629ea67e72 ("USB: usbfs: Don't WARN about excessively large memory allocations")

So I checked and for kvmalloc() we have in __kvmalloc_node_noprof()

        /* Don't even allow crazy sizes */
        if (unlikely(size > INT_MAX)) {
                WARN_ON_ONCE(!(flags & __GFP_NOWARN));
                return NULL;
        }

This comes from Linus in commit 7661809d493b4. I'd do the same thing here then.

Quoted 6 lines
Quoted 2 lines

But the purpose of this patch is to avoid the warning in the page allocator. Should we fix this in the caller (gadgetfs) then?

It is fixed by: https://lore.kernel.org/all/[email protected]/

Please disregard this patch, but we can keep the discussion going.

I still think this patch has some value if done as proposed above. Yes in practice it will just replace the page allocator's warning with a different warning, but IMHO it's "nicer" if kmalloc() sanitizes its own requests to the page allocator, using the KMALLOC_MAX_SIZE value.

Quoted 19 lines
Quoted 15 lines
Quoted 15 lines

Thus there would be a useful warning for e.g. development mistakes resulting in the size to be unexpectedly high.

Callers passing size that comes from userspace or similar untrusted source can either pass __GFP_NOWARN or sanitize the size to what they expect to be sane (which is context dependent and I assume actually way lower than kmalloc limits in practice). Note that passing even sizes within but close to/at the limit, trusting blindly some external source, can succeed the allocations but effectively DoS the system with heavy reclaim/compaction. So caller sanitization should still be preferred IMHO. Some of the recent arguments from Linus [1] would apply to this too, I think.

[1]
https://lore.kernel.org/all/CAHk-=wiSmgwwLKCqJwGS-dVHnSLU8W+7q1UQq-G9=TBGGZbuhQ@mail.gmail.com/

author
#9 lore raw
Quoted 40 lines · Vlastimil Babka (SUSE)
Quoted 39 lines · Zi Yan
Quoted 37 lines · Harry Yoo
Quoted 25 lines · Vlastimil Babka (SUSE)
Quoted 14 lines · 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. Systems with panic_on_warn=1 crash because of this WARN. Fix it by rejecting any kmalloc size bigger than KMALLOC_MAX_SIZE.

Fixes aadb4bc4a1f9 ("SLUB: direct pass through of page size or higher kmalloc requests")
Reported-by [email protected]
Quoted 7 lines

---
It fixes a page allocator warning (order > MAX_PAGE_ORDER) when gadgetfs requests excessively large memory from kmalloc. Instead of adding __GFP_NOWARN to suppress the warning, as was done for usbfs[1], change kmalloc to return NULL without a warning for this specific issue.

[1] commit 4f2629ea67e72 ("USB: usbfs: Don't WARN about excessively large memory allocations")

So I checked and for kvmalloc() we have in __kvmalloc_node_noprof()

Quoted 8 lines
        /* Don't even allow crazy sizes */
        if (unlikely(size > INT_MAX)) {
                WARN_ON_ONCE(!(flags & __GFP_NOWARN));
                return NULL;
        }

This comes from Linus in commit 7661809d493b4. I'd do the same thing here then.

But the purpose of this patch is to avoid the warning in the page allocator. Should we fix this in the caller (gadgetfs) then?

Quoted 7 lines
Quoted 3 lines

It is fixed by: https://lore.kernel.org/all/[email protected]/

Please disregard this patch, but we can keep the discussion going.

I still think this patch has some value if done as proposed above. Yes in practice it will just replace the page allocator's warning with a different warning, but IMHO it's "nicer" if kmalloc() sanitizes its own

And SLAB maintainers will be Cc'd. :)

Quoted 1 line

requests to the page allocator, using the KMALLOC_MAX_SIZE value.

Like this? Or the exact pattern as kvmalloc() is preferred?

mm/slub.c +6 -1
index 0337e60db5ace..b562f2a6fbbee 100644
@@ -5263,7 +5263,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);
Signature · 2 lines
Best Regards,
Yan, Zi
#10 lore raw
Quoted 43 lines · Zi Yan
Quoted 42 lines · Vlastimil Babka (SUSE)
Quoted 41 lines · Zi Yan
Quoted 32 lines · Harry Yoo
Quoted 31 lines · Vlastimil Babka (SUSE)
Quoted 14 lines · 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. Systems with panic_on_warn=1 crash because of this WARN. Fix it by rejecting any kmalloc size bigger than KMALLOC_MAX_SIZE.

Fixes aadb4bc4a1f9 ("SLUB: direct pass through of page size or higher kmalloc requests")
Reported-by [email protected]
Quoted 7 lines

---
It fixes a page allocator warning (order > MAX_PAGE_ORDER) when gadgetfs requests excessively large memory from kmalloc. Instead of adding __GFP_NOWARN to suppress the warning, as was done for usbfs[1], change kmalloc to return NULL without a warning for this specific issue.

[1] commit 4f2629ea67e72 ("USB: usbfs: Don't WARN about excessively large memory allocations")

So I checked and for kvmalloc() we have in __kvmalloc_node_noprof()

        /* Don't even allow crazy sizes */
        if (unlikely(size > INT_MAX)) {
                WARN_ON_ONCE(!(flags & __GFP_NOWARN));
                return NULL;
        }
Quoted 5 lines
Quoted 2 lines

This comes from Linus in commit 7661809d493b4. I'd do the same thing here then.

But the purpose of this patch is to avoid the warning in the page allocator. Should we fix this in the caller (gadgetfs) then?

It is fixed by: https://lore.kernel.org/all/[email protected]/

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Please disregard this patch, but we can keep the discussion going.

I still think this patch has some value if done as proposed above. Yes in practice it will just replace the page allocator's warning with a different warning, but IMHO it's "nicer" if kmalloc() sanitizes its own

And SLAB maintainers will be Cc'd. :)

For some people it doesn't matter if it's SLAB or PAGE ALLOCATOR :D

Quoted 3 lines
Quoted 1 line

requests to the page allocator, using the KMALLOC_MAX_SIZE value.

Like this? Or the exact pattern as kvmalloc() is preferred?

LGTM. Should return NULL even with __GFP_NOWARN or if the warning has fired and won't again, and AFAICS this does.

The kvmalloc() pattern predates WARN_ON_ONCE_GFP addition, I think.

Quoted 20 lines
mm/slub.c +6 -1
index 0337e60db5ace..b562f2a6fbbee 100644
@@ -5263,7 +5263,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);
maintainer
#11 lore raw
Quoted 52 lines · Vlastimil Babka (SUSE)
Quoted 43 lines · Zi Yan
Quoted 42 lines · Vlastimil Babka (SUSE)
Quoted 41 lines · Zi Yan
Quoted 32 lines · Harry Yoo
Quoted 31 lines · Vlastimil Babka (SUSE)
Quoted 14 lines · 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. Systems with panic_on_warn=1 crash because of this WARN. Fix it by rejecting any kmalloc size bigger than KMALLOC_MAX_SIZE.

Fixes aadb4bc4a1f9 ("SLUB: direct pass through of page size or higher kmalloc requests")
Reported-by [email protected]
Quoted 7 lines

---
It fixes a page allocator warning (order > MAX_PAGE_ORDER) when gadgetfs requests excessively large memory from kmalloc. Instead of adding __GFP_NOWARN to suppress the warning, as was done for usbfs[1], change kmalloc to return NULL without a warning for this specific issue.

[1] commit 4f2629ea67e72 ("USB: usbfs: Don't WARN about excessively large memory allocations")

So I checked and for kvmalloc() we have in __kvmalloc_node_noprof()

        /* Don't even allow crazy sizes */
        if (unlikely(size > INT_MAX)) {
                WARN_ON_ONCE(!(flags & __GFP_NOWARN));
                return NULL;
        }
Quoted 5 lines
Quoted 2 lines

This comes from Linus in commit 7661809d493b4. I'd do the same thing here then.

But the purpose of this patch is to avoid the warning in the page allocator. Should we fix this in the caller (gadgetfs) then?

It is fixed by: https://lore.kernel.org/all/[email protected]/

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Please disregard this patch, but we can keep the discussion going.

I still think this patch has some value if done as proposed above. Yes in practice it will just replace the page allocator's warning with a different warning, but IMHO it's "nicer" if kmalloc() sanitizes its own

And SLAB maintainers will be Cc'd. :)

Quoted 7 lines

For some people it doesn't matter if it's SLAB or PAGE ALLOCATOR :D

Quoted 3 lines
Quoted 1 line

requests to the page allocator, using the KMALLOC_MAX_SIZE value.

Like this? Or the exact pattern as kvmalloc() is preferred?

LGTM.

Looks good to me too.

Quoted 2 lines

Should return NULL even with __GFP_NOWARN or if the warning has fired and won't again, and AFAICS this does.

Right.

Quoted 22 lines

The kvmalloc() pattern predates WARN_ON_ONCE_GFP addition, I think.

Quoted 20 lines
mm/slub.c +6 -1
index 0337e60db5ace..b562f2a6fbbee 100644
@@ -5263,7 +5263,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);
Signature · 2 lines
Cheers,
Harry / Hyeonggon