[PATCH 5/6] slab: validate slab before using it in alloc_single_from_partial()

Vlastimil Babka posted 6 patches 3 weeks ago
There is a newer version of this series
[PATCH 5/6] slab: validate slab before using it in alloc_single_from_partial()
Posted by Vlastimil Babka 3 weeks ago
We touch slab->freelist and slab->inuse before checking the slab pointer
is actually sane. Do that validation first, which will be safer. We can
thus also remove the check from alloc_debug_processing().

This adds a new "s->flags & SLAB_CONSISTENCY_CHECKS" test but
alloc_single_from_partial() is only called for caches with debugging
enabled so it's acceptable.

In alloc_single_from_new_slab() we just created the struct slab and call
alloc_debug_processing() to mainly set up redzones, tracking etc, while
not really expecting the consistency checks to fail. Thus don't validate
it there.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 909c71372a2f542b6e0d67c12ea683133b246b66..93df6e82af37c798c3fa5574c9d825f0f4a83013 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -1651,11 +1651,6 @@ static noinline bool alloc_debug_processing(struct kmem_cache *s,
 			struct slab *slab, void *object, int orig_size)
 {
 	if (s->flags & SLAB_CONSISTENCY_CHECKS) {
-		if (!validate_slab_ptr(slab)) {
-			slab_err(s, slab, "Not a valid slab page");
-			return false;
-		}
-
 		if (!alloc_consistency_checks(s, slab, object))
 			goto bad;
 	}
@@ -2825,15 +2820,19 @@ static void *alloc_single_from_partial(struct kmem_cache *s,
 
 	lockdep_assert_held(&n->list_lock);
 
+	if (s->flags & SLAB_CONSISTENCY_CHECKS) {
+		if (!validate_slab_ptr(slab)) {
+			slab_err(s, slab, "Not a valid slab page");
+			return NULL;
+		}
+	}
+
 	object = slab->freelist;
 	slab->freelist = get_freepointer(s, object);
 	slab->inuse++;
 
-	if (!alloc_debug_processing(s, slab, object, orig_size)) {
-		if (validate_slab_ptr(slab))
-			remove_partial(n, slab);
+	if (!alloc_debug_processing(s, slab, object, orig_size))
 		return NULL;
-	}
 
 	if (slab->inuse == slab->objects) {
 		remove_partial(n, slab);

-- 
2.51.0
Re: [PATCH 5/6] slab: validate slab before using it in alloc_single_from_partial()
Posted by Harry Yoo 2 weeks, 6 days ago
On Thu, Sep 11, 2025 at 07:02:38PM +0200, Vlastimil Babka wrote:
> We touch slab->freelist and slab->inuse before checking the slab pointer
> is actually sane. Do that validation first, which will be safer. We can
> thus also remove the check from alloc_debug_processing().
> 
> This adds a new "s->flags & SLAB_CONSISTENCY_CHECKS" test but
> alloc_single_from_partial() is only called for caches with debugging
> enabled so it's acceptable.
> 
> In alloc_single_from_new_slab() we just created the struct slab and call
> alloc_debug_processing() to mainly set up redzones, tracking etc, while
> not really expecting the consistency checks to fail. Thus don't validate
> it there.
> 
> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
> ---
>  mm/slub.c | 17 ++++++++---------
>  1 file changed, 8 insertions(+), 9 deletions(-)
> 
> diff --git a/mm/slub.c b/mm/slub.c
> index 909c71372a2f542b6e0d67c12ea683133b246b66..93df6e82af37c798c3fa5574c9d825f0f4a83013 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -1651,11 +1651,6 @@ static noinline bool alloc_debug_processing(struct kmem_cache *s,
>  			struct slab *slab, void *object, int orig_size)
>  {
>  	if (s->flags & SLAB_CONSISTENCY_CHECKS) {
> -		if (!validate_slab_ptr(slab)) {
> -			slab_err(s, slab, "Not a valid slab page");
> -			return false;
> -		}
> -
>  		if (!alloc_consistency_checks(s, slab, object))
>  			goto bad;
>  	}
> @@ -2825,15 +2820,19 @@ static void *alloc_single_from_partial(struct kmem_cache *s,
>  
>  	lockdep_assert_held(&n->list_lock);
>  
> +	if (s->flags & SLAB_CONSISTENCY_CHECKS) {
> +		if (!validate_slab_ptr(slab)) {
> +			slab_err(s, slab, "Not a valid slab page");
> +			return NULL;
> +		}
> +	}
> +
>  	object = slab->freelist;
>  	slab->freelist = get_freepointer(s, object);
>  	slab->inuse++;
>  
> -	if (!alloc_debug_processing(s, slab, object, orig_size)) {
> -		if (validate_slab_ptr(slab))
> -			remove_partial(n, slab);
> +	if (!alloc_debug_processing(s, slab, object, orig_size))
>  		return NULL;

Is it intentional to not remove the slab from the partial list
when alloc_debug_processing() returns false?

> -	}
>  
>  	if (slab->inuse == slab->objects) {
>  		remove_partial(n, slab);
> 
> -- 
> 2.51.0

-- 
Cheers,
Harry / Hyeonggon
Re: [PATCH 5/6] slab: validate slab before using it in alloc_single_from_partial()
Posted by Vlastimil Babka 2 weeks, 6 days ago
On 9/12/25 12:48, Harry Yoo wrote:
> On Thu, Sep 11, 2025 at 07:02:38PM +0200, Vlastimil Babka wrote:
>> We touch slab->freelist and slab->inuse before checking the slab pointer
>> is actually sane. Do that validation first, which will be safer. We can
>> thus also remove the check from alloc_debug_processing().
>> 
>> This adds a new "s->flags & SLAB_CONSISTENCY_CHECKS" test but
>> alloc_single_from_partial() is only called for caches with debugging
>> enabled so it's acceptable.
>> 
>> In alloc_single_from_new_slab() we just created the struct slab and call
>> alloc_debug_processing() to mainly set up redzones, tracking etc, while
>> not really expecting the consistency checks to fail. Thus don't validate
>> it there.
>> 
>> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
>> ---
>>  mm/slub.c | 17 ++++++++---------
>>  1 file changed, 8 insertions(+), 9 deletions(-)
>> 
>> diff --git a/mm/slub.c b/mm/slub.c
>> index 909c71372a2f542b6e0d67c12ea683133b246b66..93df6e82af37c798c3fa5574c9d825f0f4a83013 100644
>> --- a/mm/slub.c
>> +++ b/mm/slub.c
>> @@ -1651,11 +1651,6 @@ static noinline bool alloc_debug_processing(struct kmem_cache *s,
>>  			struct slab *slab, void *object, int orig_size)
>>  {
>>  	if (s->flags & SLAB_CONSISTENCY_CHECKS) {
>> -		if (!validate_slab_ptr(slab)) {
>> -			slab_err(s, slab, "Not a valid slab page");
>> -			return false;
>> -		}
>> -
>>  		if (!alloc_consistency_checks(s, slab, object))
>>  			goto bad;
>>  	}
>> @@ -2825,15 +2820,19 @@ static void *alloc_single_from_partial(struct kmem_cache *s,
>>  
>>  	lockdep_assert_held(&n->list_lock);
>>  
>> +	if (s->flags & SLAB_CONSISTENCY_CHECKS) {
>> +		if (!validate_slab_ptr(slab)) {
>> +			slab_err(s, slab, "Not a valid slab page");
>> +			return NULL;
>> +		}
>> +	}
>> +
>>  	object = slab->freelist;
>>  	slab->freelist = get_freepointer(s, object);
>>  	slab->inuse++;
>>  
>> -	if (!alloc_debug_processing(s, slab, object, orig_size)) {
>> -		if (validate_slab_ptr(slab))
>> -			remove_partial(n, slab);
>> +	if (!alloc_debug_processing(s, slab, object, orig_size))
>>  		return NULL;
> 
> Is it intentional to not remove the slab from the partial list
> when alloc_debug_processing() returns false?

No, good catch, will fix. Thanks!

>> -	}
>>  
>>  	if (slab->inuse == slab->objects) {
>>  		remove_partial(n, slab);
>> 
>> -- 
>> 2.51.0
>