[PATCH] tools: fix -Wunused-result in linux.c

Shuah Khan posted 1 patch 1 month, 2 weeks ago
tools/testing/shared/linux.c | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)
[PATCH] tools: fix -Wunused-result in linux.c
Posted by Shuah Khan 1 month, 2 weeks ago
Fix the following -Wunused-result warnings on posix_memalign()
return values and add error handling.

./shared/linux.c:100:25: warning: ignoring return value of ‘posix_memalign’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  100 |          posix_memalign(&p, cachep->align, cachep->size);
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../shared/linux.c: In function ‘kmem_cache_alloc_bulk’:
../shared/linux.c:198:33: warning: ignoring return value of ‘posix_memalign’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  198 |          posix_memalign(&p[i], cachep->align,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  199 |                                cachep->size);
      |                                ~~~~~~~~~~~~~

Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
---
 tools/testing/shared/linux.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/tools/testing/shared/linux.c b/tools/testing/shared/linux.c
index 17263696b5d8..66dbb362385f 100644
--- a/tools/testing/shared/linux.c
+++ b/tools/testing/shared/linux.c
@@ -96,10 +96,13 @@ void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru,
 		p = node;
 	} else {
 		pthread_mutex_unlock(&cachep->lock);
-		if (cachep->align)
-			posix_memalign(&p, cachep->align, cachep->size);
-		else
+		if (cachep->align) {
+			if (posix_memalign(&p, cachep->align, cachep->size) < 0)
+				return NULL;
+		} else {
 			p = malloc(cachep->size);
+		}
+
 		if (cachep->ctor)
 			cachep->ctor(p);
 		else if (gfp & __GFP_ZERO)
@@ -195,8 +198,9 @@ int kmem_cache_alloc_bulk(struct kmem_cache *cachep, gfp_t gfp, size_t size,
 			}
 
 			if (cachep->align) {
-				posix_memalign(&p[i], cachep->align,
-					       cachep->size);
+				if (posix_memalign(&p[i], cachep->align,
+					       cachep->size) < 0)
+					break;
 			} else {
 				p[i] = malloc(cachep->size);
 				if (!p[i])
-- 
2.40.1

Re: [PATCH] tools: fix -Wunused-result in linux.c
Posted by Liam R. Howlett 1 month, 1 week ago
* Shuah Khan <skhan@linuxfoundation.org> [241011 18:52]:
> Fix the following -Wunused-result warnings on posix_memalign()
> return values and add error handling.
> 
> ./shared/linux.c:100:25: warning: ignoring return value of ‘posix_memalign’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
>   100 |          posix_memalign(&p, cachep->align, cachep->size);
>       |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> ../shared/linux.c: In function ‘kmem_cache_alloc_bulk’:
> ../shared/linux.c:198:33: warning: ignoring return value of ‘posix_memalign’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
>   198 |          posix_memalign(&p[i], cachep->align,
>       |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>   199 |                                cachep->size);
>       |                                ~~~~~~~~~~~~~
> 
> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

Reviewed-by: Liam R. Howlett <Liam.Howlett@Oracle.com>

> ---
>  tools/testing/shared/linux.c | 14 +++++++++-----
>  1 file changed, 9 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/testing/shared/linux.c b/tools/testing/shared/linux.c
> index 17263696b5d8..66dbb362385f 100644
> --- a/tools/testing/shared/linux.c
> +++ b/tools/testing/shared/linux.c
> @@ -96,10 +96,13 @@ void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru,
>  		p = node;
>  	} else {
>  		pthread_mutex_unlock(&cachep->lock);
> -		if (cachep->align)
> -			posix_memalign(&p, cachep->align, cachep->size);
> -		else
> +		if (cachep->align) {
> +			if (posix_memalign(&p, cachep->align, cachep->size) < 0)
> +				return NULL;
> +		} else {
>  			p = malloc(cachep->size);
> +		}
> +
>  		if (cachep->ctor)
>  			cachep->ctor(p);
>  		else if (gfp & __GFP_ZERO)
> @@ -195,8 +198,9 @@ int kmem_cache_alloc_bulk(struct kmem_cache *cachep, gfp_t gfp, size_t size,
>  			}
>  
>  			if (cachep->align) {
> -				posix_memalign(&p[i], cachep->align,
> -					       cachep->size);
> +				if (posix_memalign(&p[i], cachep->align,
> +					       cachep->size) < 0)
> +					break;
>  			} else {
>  				p[i] = malloc(cachep->size);
>  				if (!p[i])
> -- 
> 2.40.1
> 
RE: [PATCH] tools: fix -Wunused-result in linux.c
Posted by David Laight 1 month ago
From: Liam R. Howlett
> Sent: 15 October 2024 02:11
> 
> * Shuah Khan <skhan@linuxfoundation.org> [241011 18:52]:
> > Fix the following -Wunused-result warnings on posix_memalign()
> > return values and add error handling.
> >
> > ./shared/linux.c:100:25: warning: ignoring return value of ‘posix_memalign’ declared with attribute
> ‘warn_unused_result’ [-Wunused-result]
> >   100 |          posix_memalign(&p, cachep->align, cachep->size);
> >       |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > ../shared/linux.c: In function ‘kmem_cache_alloc_bulk’:
> > ../shared/linux.c:198:33: warning: ignoring return value of ‘posix_memalign’ declared with attribute
> ‘warn_unused_result’ [-Wunused-result]
> >   198 |          posix_memalign(&p[i], cachep->align,
> >       |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >   199 |                                cachep->size);
> >       |                                ~~~~~~~~~~~~~
> >
> > Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
> 
> Reviewed-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
> 
> > ---
> >  tools/testing/shared/linux.c | 14 +++++++++-----
> >  1 file changed, 9 insertions(+), 5 deletions(-)
> >
> > diff --git a/tools/testing/shared/linux.c b/tools/testing/shared/linux.c
> > index 17263696b5d8..66dbb362385f 100644
> > --- a/tools/testing/shared/linux.c
> > +++ b/tools/testing/shared/linux.c
> > @@ -96,10 +96,13 @@ void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru,
> >  		p = node;
> >  	} else {
> >  		pthread_mutex_unlock(&cachep->lock);
> > -		if (cachep->align)
> > -			posix_memalign(&p, cachep->align, cachep->size);
> > -		else
> > +		if (cachep->align) {
> > +			if (posix_memalign(&p, cachep->align, cachep->size) < 0)
> > +				return NULL;
> > +		} else {
> >  			p = malloc(cachep->size);
> > +		}
> > +

You really ought to be checking malloc() as well.
Perhaps:
		if (...) {
			if (posix_memalign(...) < 0)
				p = NULL;
		} else {
			p = malloc(...);
		}
		if (!p)
			return NULL;

Or just use a hack to get the compiler to STFU :-)

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
Re: [PATCH] tools: fix -Wunused-result in linux.c
Posted by Shuah Khan 1 month ago
On 10/21/24 10:10, David Laight wrote:
> From: Liam R. Howlett
>> Sent: 15 October 2024 02:11
>>
>> * Shuah Khan <skhan@linuxfoundation.org> [241011 18:52]:
>>> Fix the following -Wunused-result warnings on posix_memalign()
>>> return values and add error handling.
>>>
>>> ./shared/linux.c:100:25: warning: ignoring return value of ‘posix_memalign’ declared with attribute
>> ‘warn_unused_result’ [-Wunused-result]
>>>    100 |          posix_memalign(&p, cachep->align, cachep->size);
>>>        |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>> ../shared/linux.c: In function ‘kmem_cache_alloc_bulk’:
>>> ../shared/linux.c:198:33: warning: ignoring return value of ‘posix_memalign’ declared with attribute
>> ‘warn_unused_result’ [-Wunused-result]
>>>    198 |          posix_memalign(&p[i], cachep->align,
>>>        |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>>    199 |                                cachep->size);
>>>        |                                ~~~~~~~~~~~~~
>>>
>>> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
>>
>> Reviewed-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
>>
>>> ---
>>>   tools/testing/shared/linux.c | 14 +++++++++-----
>>>   1 file changed, 9 insertions(+), 5 deletions(-)
>>>
>>> diff --git a/tools/testing/shared/linux.c b/tools/testing/shared/linux.c
>>> index 17263696b5d8..66dbb362385f 100644
>>> --- a/tools/testing/shared/linux.c
>>> +++ b/tools/testing/shared/linux.c
>>> @@ -96,10 +96,13 @@ void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru,
>>>   		p = node;
>>>   	} else {
>>>   		pthread_mutex_unlock(&cachep->lock);
>>> -		if (cachep->align)
>>> -			posix_memalign(&p, cachep->align, cachep->size);
>>> -		else
>>> +		if (cachep->align) {
>>> +			if (posix_memalign(&p, cachep->align, cachep->size) < 0)
>>> +				return NULL;
>>> +		} else {
>>>   			p = malloc(cachep->size);
>>> +		}
>>> +
> 
> You really ought to be checking malloc() as well.
> Perhaps:
> 		if (...) {
> 			if (posix_memalign(...) < 0)
> 				p = NULL;
> 		} else {
> 			p = malloc(...);
> 		}
> 		if (!p)
> 			return NULL;
> 
> Or just use a hack to get the compiler to STFU :-)

Yes you are right. I will spin another version to cover
the malloc and I just noticed another posix_memalign()
with -Wunused-result that compiler didn't flag in
kmem_cache_alloc_bulk() to fix as well in the next version.

thanks,
-- Shuah
Re: [PATCH] tools: fix -Wunused-result in linux.c
Posted by Lorenzo Stoakes 1 month, 1 week ago
On Fri, Oct 11, 2024 at 04:51:55PM -0600, Shuah Khan wrote:
> Fix the following -Wunused-result warnings on posix_memalign()
> return values and add error handling.
>
> ./shared/linux.c:100:25: warning: ignoring return value of ‘posix_memalign’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
>   100 |          posix_memalign(&p, cachep->align, cachep->size);
>       |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> ../shared/linux.c: In function ‘kmem_cache_alloc_bulk’:
> ../shared/linux.c:198:33: warning: ignoring return value of ‘posix_memalign’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
>   198 |          posix_memalign(&p[i], cachep->align,
>       |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>   199 |                                cachep->size);
>       |                                ~~~~~~~~~~~~~
>
> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

Looks good to me!

Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>

> ---
>  tools/testing/shared/linux.c | 14 +++++++++-----
>  1 file changed, 9 insertions(+), 5 deletions(-)
>
> diff --git a/tools/testing/shared/linux.c b/tools/testing/shared/linux.c
> index 17263696b5d8..66dbb362385f 100644
> --- a/tools/testing/shared/linux.c
> +++ b/tools/testing/shared/linux.c
> @@ -96,10 +96,13 @@ void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru,
>  		p = node;
>  	} else {
>  		pthread_mutex_unlock(&cachep->lock);
> -		if (cachep->align)
> -			posix_memalign(&p, cachep->align, cachep->size);
> -		else
> +		if (cachep->align) {
> +			if (posix_memalign(&p, cachep->align, cachep->size) < 0)
> +				return NULL;
> +		} else {
>  			p = malloc(cachep->size);
> +		}
> +
>  		if (cachep->ctor)
>  			cachep->ctor(p);
>  		else if (gfp & __GFP_ZERO)
> @@ -195,8 +198,9 @@ int kmem_cache_alloc_bulk(struct kmem_cache *cachep, gfp_t gfp, size_t size,
>  			}
>
>  			if (cachep->align) {
> -				posix_memalign(&p[i], cachep->align,
> -					       cachep->size);
> +				if (posix_memalign(&p[i], cachep->align,
> +					       cachep->size) < 0)
> +					break;
>  			} else {
>  				p[i] = malloc(cachep->size);
>  				if (!p[i])
> --
> 2.40.1
>