Skip to contentSkip to conversation

tools: fix -Wunused-result in linux.c

PATCH Applied Shuah Khan 4 participants 5 messages Oct 11, 2024 – Oct 21, 2024

lore
Share & tools
lore

Shuah Khan

author
#0 lore raw

PATCH 1/1 tools: fix -Wunused-result in linux.c

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);
      |                                ~~~~~~~~~~~~~
---
 tools/testing/shared/linux.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
tools/testing/shared/linux.c +9 -5
index 17263696b5d8..66dbb362385f 100644
@@ -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])
Signature · 1 line
2.40.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 13 lines · Shuah Khan

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);
      |                                ~~~~~~~~~~~~~
Quoted 1 line

Looks good to me!

Quoted 40 lines
---
 tools/testing/shared/linux.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
tools/testing/shared/linux.c +9 -6
index 17263696b5d8..66dbb362385f 100644
@@ -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

Reviewed-by on v1 1/1
#2 lore raw
In reply to Shuah Khan
Quoted 13 lines · Shuah Khan

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);
      |                                ~~~~~~~~~~~~~
Quoted 1 line
Quoted 40 lines
---
 tools/testing/shared/linux.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
tools/testing/shared/linux.c +9 -5
index 17263696b5d8..66dbb362385f 100644
@@ -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])
Signature · 1 line
2.40.1
#3 lore raw

From: Liam R. Howlett

Quoted 36 lines

Sent: 15 October 2024 02:11

Quoted 4 lines · Shuah Khan

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]

Quoted 4 lines
  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]

Quoted 6 lines
  198 |          posix_memalign(&p[i], cachep->align,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  199 |                                cachep->size);
      |                                ~~~~~~~~~~~~~
Quoted 14 lines
---
 tools/testing/shared/linux.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
tools/testing/shared/linux.c +0 -2
index 17263696b5d8..66dbb362385f 100644
@@ -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);
Quoted 8 lines

tools/testing/shared/linux.c void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru, · line 105

Quoted 8 lines
tools/testing/shared/linux.c +6 -1
-		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)

author
#4 lore raw
Quoted 57 lines · David Laight

From: Liam R. Howlett

Quoted 36 lines

Sent: 15 October 2024 02:11

Quoted 4 lines · Shuah Khan

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]

Quoted 4 lines
   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]

Quoted 6 lines
   198 |          posix_memalign(&p[i], cachep->align,
       |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   199 |                                cachep->size);
       |                                ~~~~~~~~~~~~~
Quoted 14 lines
---
  tools/testing/shared/linux.c | 14 +++++++++-----
  1 file changed, 9 insertions(+), 5 deletions(-)
tools/testing/shared/linux.c +0 -2
index 17263696b5d8..66dbb362385f 100644
@@ -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);
Quoted 8 lines

tools/testing/shared/linux.c void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru, · line 105

Quoted 8 lines
tools/testing/shared/linux.c +6 -1
-		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;
Quoted 1 line

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