Skip to contentSkip to conversation

tools/testing/shared: catch a failed posix_memalign()

PATCH Applied Liam R. Howlett (Google) 4 participants 4 messages Oct 20 – Oct 21 Memory management

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/testing/shared: catch a failed posix_memalign()

posix_memalign() returns zero or a positive error number and never sets errno, so the checks for a negative return in kmem_cache_alloc_lru() and kmem_cache_alloc_bulk() is not possible. A failed aligned allocation carries on with the pointer unset, into the constructor or the memset().

Test for any non-zero return.

---
 tools/testing/shared/linux.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/shared/linux.c +2 -2
index e0a0693df08f5..a37d2f86d3254 100644
@@ -83,7 +83,7 @@ void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru,
 	} else {
 		pthread_mutex_unlock(&cachep->lock);
 		if (cachep->align) {
-			if (posix_memalign(&p, cachep->align, cachep->size) < 0)
+			if (posix_memalign(&p, cachep->align, cachep->size))
 				return NULL;
 		} else {
 			p = malloc(cachep->size);
@@ -191,7 +191,7 @@ bool kmem_cache_alloc_bulk(struct kmem_cache *cachep, gfp_t gfp, size_t size,
 
 			if (cachep->align) {
 				if (posix_memalign(&p[i], cachep->align,
-					       cachep->size) < 0)
+					       cachep->size))
 					break;
 			} else {
 				p[i] = malloc(cachep->size);
Signature · 1 line
2.47.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Liam R. Howlett (Google)

posix_memalign() returns zero or a positive error number and never sets errno, so the checks for a negative return in kmem_cache_alloc_lru() and kmem_cache_alloc_bulk() is not possible. A failed aligned allocation carries on with the pointer unset, into the constructor or the memset().

Test for any non-zero return.

LGTM so:

Quoted 27 lines
---
 tools/testing/shared/linux.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/shared/linux.c +2 -3
index e0a0693df08f5..a37d2f86d3254 100644
@@ -83,7 +83,7 @@ void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru,
 	} else {
 		pthread_mutex_unlock(&cachep->lock);
 		if (cachep->align) {
-			if (posix_memalign(&p, cachep->align, cachep->size) < 0)
+			if (posix_memalign(&p, cachep->align, cachep->size))
 				return NULL;
 		} else {
 			p = malloc(cachep->size);
@@ -191,7 +191,7 @@ bool kmem_cache_alloc_bulk(struct kmem_cache *cachep, gfp_t gfp, size_t size,
 
 			if (cachep->align) {
 				if (posix_memalign(&p[i], cachep->align,
-					       cachep->size) < 0)
+					       cachep->size))
 					break;
 			} else {
 				p[i] = malloc(cachep->size);
--
Quoted 2 lines

2.47.3

--
Cheers, Lorenzo

Reviewed-by on v1 1/1
#2 lore raw
In reply to Liam R. Howlett (Google)
Quoted 8 lines · Liam R. Howlett (Google)

posix_memalign() returns zero or a positive error number and never sets errno, so the checks for a negative return in kmem_cache_alloc_lru() and kmem_cache_alloc_bulk() is not possible. A failed aligned allocation carries on with the pointer unset, into the constructor or the memset().

Test for any non-zero return.

It's not really a big deal, but seems this was introduced in commit 8801c35c3672c ("tools: fix -Wunused-result in linux.c"), so +CC the author Shuah Khan.

Quoted 28 lines
 tools/testing/shared/linux.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/shared/linux.c +2 -2
index e0a0693df08f5..a37d2f86d3254 100644
@@ -83,7 +83,7 @@ void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru,
 	} else {
 		pthread_mutex_unlock(&cachep->lock);
 		if (cachep->align) {
-			if (posix_memalign(&p, cachep->align, cachep->size) < 0)
+			if (posix_memalign(&p, cachep->align, cachep->size))
 				return NULL;
 		} else {
 			p = malloc(cachep->size);
@@ -191,7 +191,7 @@ bool kmem_cache_alloc_bulk(struct kmem_cache *cachep, gfp_t gfp, size_t size,
 
 			if (cachep->align) {
 				if (posix_memalign(&p[i], cachep->align,
-					       cachep->size) < 0)
+					       cachep->size))
 					break;
 			} else {
 				p[i] = malloc(cachep->size);
Signature · 1 line
2.47.3
Reviewed-by on v1 1/1
#3 lore raw
In reply to Liam R. Howlett (Google)
Quoted 6 lines · On Mon, 19 Oct 2026 23:31:28 -0400 "Liam R. Howlett (Google)"…

posix_memalign() returns zero or a positive error number and never sets errno, so the checks for a negative return in kmem_cache_alloc_lru() and kmem_cache_alloc_bulk() is not possible. A failed aligned allocation carries on with the pointer unset, into the constructor or the memset().

Test for any non-zero return.

Nice catch, makes sense to me.

Quoted 2 lines

Thanks,
SJ

[...]