Skip to contentSkip to conversation

mm, slab: warn when increasing refcount on large kmalloc page

RFC PATCH In review Vlastimil Babka 4 participants 5 messages Apr 17, 2025 – Apr 23, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 mm, slab: warn when increasing refcount on large kmalloc page

Since slab pages are now frozen, increasing refcount on a page containing a kmalloc() allocation is not possible anymore. Large kmalloc pages should ideally behave the same, because the decision for which allocation size to use them is the slab allocator's implementation detail, and sizes passed to kmalloc() might depend on e.g. user input.

Because of some unexpected fallout in the slab pages case (see commit b9c0e49abfca ("mm: decline to manipulate the refcount on a slab page"), let's take a more cautious approach and before making large kmalloc pages actually frozen, start warning about code that would try to increase refcount on them.

---
I'd like to expose this via slab-next and see if there are any reports. If not for few weeks, maybe proceed immediately to freezing refcount and handling it in get_page/put_page exactly like folio_test_slab. Thoughts?

 include/linux/mm.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/mm.h +1 -0
index b7f13f087954..664c67346484 100644
@@ -1532,6 +1532,7 @@ static inline void get_page(struct page *page)
 	struct folio *folio = page_folio(page);
 	if (WARN_ON_ONCE(folio_test_slab(folio)))
 		return;
+	WARN_ON_ONCE(folio_test_large_kmalloc(folio));
 	folio_get(folio);
 }
 
Signature · 1 line
2.49.0
#1 lore raw
Quoted 5 lines · Vlastimil Babka

Since slab pages are now frozen, increasing refcount on a page containing a kmalloc() allocation is not possible anymore. Large kmalloc pages should ideally behave the same, because the decision for which allocation size to use them is the slab allocator's implementation detail, and sizes passed to kmalloc() might depend on e.g. user input.

Agreed.

Quoted 11 lines

Because of some unexpected fallout in the slab pages case (see commit b9c0e49abfca ("mm: decline to manipulate the refcount on a slab page"), let's take a more cautious approach and before making large kmalloc pages actually frozen, start warning about code that would try to increase refcount on them.

---
I'd like to expose this via slab-next and see if there are any reports. If not for few weeks, maybe proceed immediately to freezing refcount and handling it in get_page/put_page exactly like folio_test_slab. Thoughts?

+1

Let's give testing a try.

...at least it does not hit anything on my box.

Signature · 2 lines
Cheers,
Harry / Hyeonggon
Quoted 17 lines
 include/linux/mm.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/mm.h +1 -0
index b7f13f087954..664c67346484 100644
@@ -1532,6 +1532,7 @@ static inline void get_page(struct page *page)
 	struct folio *folio = page_folio(page);
 	if (WARN_ON_ONCE(folio_test_slab(folio)))
 		return;
+	WARN_ON_ONCE(folio_test_large_kmalloc(folio));
 	folio_get(folio);
 }
 
Signature · 1 line
2.49.0
#2 lore raw
Quoted 20 lines · Harry Yoo
Quoted 5 lines

Since slab pages are now frozen, increasing refcount on a page containing a kmalloc() allocation is not possible anymore. Large kmalloc pages should ideally behave the same, because the decision for which allocation size to use them is the slab allocator's implementation detail, and sizes passed to kmalloc() might depend on e.g. user input.

Agreed.

Quoted 11 lines

Because of some unexpected fallout in the slab pages case (see commit b9c0e49abfca ("mm: decline to manipulate the refcount on a slab page"), let's take a more cautious approach and before making large kmalloc pages actually frozen, start warning about code that would try to increase refcount on them.

---
I'd like to expose this via slab-next and see if there are any reports. If not for few weeks, maybe proceed immediately to freezing refcount and handling it in get_page/put_page exactly like folio_test_slab. Thoughts?

Quoted 6 lines

+1

Let's give testing a try.

...at least it does not hit anything on my box.

Same, I ran this through a number of benchmarks on x86 and couldn't find any occurrences of the warning.

#3 lore raw
Quoted 22 lines · David Rientjes
Quoted 22 lines
Quoted 5 lines

Since slab pages are now frozen, increasing refcount on a page containing a kmalloc() allocation is not possible anymore. Large kmalloc pages should ideally behave the same, because the decision for which allocation size to use them is the slab allocator's implementation detail, and sizes passed to kmalloc() might depend on e.g. user input.

Agreed.

Quoted 11 lines

Because of some unexpected fallout in the slab pages case (see commit b9c0e49abfca ("mm: decline to manipulate the refcount on a slab page"), let's take a more cautious approach and before making large kmalloc pages actually frozen, start warning about code that would try to increase refcount on them.

---
I'd like to expose this via slab-next and see if there are any reports. If not for few weeks, maybe proceed immediately to freezing refcount and handling it in get_page/put_page exactly like folio_test_slab. Thoughts?

+1

Quoted 8 lines
Quoted 4 lines

Let's give testing a try.

...at least it does not hit anything on my box.

Same, I ran this through a number of benchmarks on x86 and couldn't find any occurrences of the warning.

Btw, the benchmarks that I normally run regularly on SLUB are kernbench, multichase, hackbench, specjbb, will-it-scale, redis, unixbench, netperf TCP_RR and UDP_STREAM, on both x86 and arm64. I haven't run on any other architectures recently, but perhaps others do.

Are there any other benchmarks that people normally run for slab allocator related changes?

#4 lore raw
In reply to Vlastimil Babka
Quoted 3 lines · Vlastimil Babka

I'd like to expose this via slab-next and see if there are any reports. If not for few weeks, maybe proceed immediately to freezing refcount and handling it in get_page/put_page exactly like folio_test_slab. Thoughts?

I think you're being too cautious ;-) I had a bit of a play around to see if there's a way of getting gcc to optimise the generated code for this, but didn't come up with anything too clever.