Skip to contentSkip to conversation

slab: fix clearing freelist in free_deferred_objects()

PATCH In review Vlastimil Babka 2 participants 3 messages Oct 14, 2025

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

PATCH 1/1 slab: fix clearing freelist in free_deferred_objects()

defer_free() links pending objects using the slab's freelist offset which is fine as they are not free yet. free_deferred_objects() then clears this pointer to avoid confusing the debugging consistency checks that may be enabled for the cache.

However, with CONFIG_SLAB_FREELIST_HARDENED, even the NULL pointer needs to be encoded appropriately using set_freepointer(), otherwise it's decoded as something else and triggers the consistency checks, as found by the kernel test robot.

Use set_freepointer() to prevent the issue.

Fixes af92793e52c3 ("slab: Introduce kmalloc_nolock() and kfree_nolock().")

---
will be added to slab/for-next-fixes and sent later this week

---
 mm/slub.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
mm/slub.c +4 -3
index b1f15598fbfd..64c17afc375b 100644
@@ -6443,15 +6443,16 @@ static void free_deferred_objects(struct irq_work *work)
 		slab = virt_to_slab(x);
 		s = slab->slab_cache;
 
+
+		/* Point 'x' back to the beginning of allocated object */
+		x -= s->offset;
 		/*
 		 * We used freepointer in 'x' to link 'x' into df->objects.
 		 * Clear it to NULL to avoid false positive detection
 		 * of "Freepointer corruption".
 		 */
-		*(void **)x = NULL;
+		set_freepointer(s, x, NULL);
 
-		/* Point 'x' back to the beginning of allocated object */
-		x -= s->offset;
 		__slab_free(s, slab, x, x, 1, _THIS_IP_);
 	}
 

---
base-commit: 3a8660878839faadb4f1a6dd72c3179c1df56787
change-id: 20251014-fix-freelist-29915edcc2f4

Best regards,

Signature · 1 line
Vlastimil Babka <[email protected]>
#1 lore raw
Quoted 28 lines · Vlastimil Babka

defer_free() links pending objects using the slab's freelist offset which is fine as they are not free yet. free_deferred_objects() then clears this pointer to avoid confusing the debugging consistency checks that may be enabled for the cache.

However, with CONFIG_SLAB_FREELIST_HARDENED, even the NULL pointer needs to be encoded appropriately using set_freepointer(), otherwise it's decoded as something else and triggers the consistency checks, as found by the kernel test robot.

Use set_freepointer() to prevent the issue.

Fixes af92793e52c3 ("slab: Introduce kmalloc_nolock() and kfree_nolock().")

---
will be added to slab/for-next-fixes and sent later this week

---
 mm/slub.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
mm/slub.c +0 -0
index b1f15598fbfd..64c17afc375b 100644

mm/slub.c static void free_deferred_objects(struct irq_work *work) · line 6446

Quoted 4 lines
mm/slub.c +1 -0
@@ -6443,15 +6443,16 @@ static void free_deferred_objects(struct irq_work *work)
 		slab = virt_to_slab(x);
 		s = slab->slab_cache;
 
+

extra newline moved...

mm/slub.c static void free_deferred_objects(struct irq_work *work) · line 6448

Quoted 2 lines
mm/slub.c +2 -0
+		/* Point 'x' back to the beginning of allocated object */
+		x -= s->offset;

... here. noticed and fixed up locally after sending...

Quoted 19 lines
mm/slub.c +1 -4
 		/*
 		 * We used freepointer in 'x' to link 'x' into df->objects.
 		 * Clear it to NULL to avoid false positive detection
 		 * of "Freepointer corruption".
 		 */
-		*(void **)x = NULL;
+		set_freepointer(s, x, NULL);
 
-		/* Point 'x' back to the beginning of allocated object */
-		x -= s->offset;
 		__slab_free(s, slab, x, x, 1, _THIS_IP_);
 	}
 
 
---

base-commit: 3a8660878839faadb4f1a6dd72c3179c1df56787
change-id: 20251014-fix-freelist-29915edcc2f4

Best regards,

Reviewed-by
#2 lore raw
Quoted 32 lines · Vlastimil Babka
Quoted 31 lines · Vlastimil Babka

defer_free() links pending objects using the slab's freelist offset which is fine as they are not free yet. free_deferred_objects() then clears this pointer to avoid confusing the debugging consistency checks that may be enabled for the cache.

However, with CONFIG_SLAB_FREELIST_HARDENED, even the NULL pointer needs to be encoded appropriately using set_freepointer(), otherwise it's decoded as something else and triggers the consistency checks, as found by the kernel test robot.

Use set_freepointer() to prevent the issue.

Fixes af92793e52c3 ("slab: Introduce kmalloc_nolock() and kfree_nolock().")

---
will be added to slab/for-next-fixes and sent later this week

---
 mm/slub.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
mm/slub.c +0 -0
index b1f15598fbfd..64c17afc375b 100644
@@ -6443,15 +6443,16 @@ static void free_deferred_objects(struct irq_work *work)
 		slab = virt_to_slab(x);
 		s = slab->slab_cache;
 
Quoted 8 lines

mm/slub.c static void free_deferred_objects(struct irq_work *work) · line 6446

Quoted 1 line
mm/slub.c +1 -0
+

extra newline moved...

mm/slub.c static void free_deferred_objects(struct irq_work *work) · line 6448

Quoted 2 lines
mm/slub.c +2 -0
+		/* Point 'x' back to the beginning of allocated object */
+		x -= s->offset;

... here. noticed and fixed up locally after sending...

LGTM,

Thanks for fixing it!

Quoted 12 lines
Quoted 12 lines
mm/slub.c +1 -3
 		/*
 		 * We used freepointer in 'x' to link 'x' into df->objects.
 		 * Clear it to NULL to avoid false positive detection
 		 * of "Freepointer corruption".
 		 */
-		*(void **)x = NULL;
+		set_freepointer(s, x, NULL);
 
-		/* Point 'x' back to the beginning of allocated object */
-		x -= s->offset;
 		__slab_free(s, slab, x, x, 1, _THIS_IP_);
 	}
Signature · 2 lines
Cheers,
Harry / Hyeonggon