mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] tools/testing/shared: catch a failed posix_memalign()
@ 2026-10-20  3:31 Liam R. Howlett (Google)
  2026-10-20  9:05 ` Lorenzo Stoakes (ARM)
  0 siblings, 1 reply; 2+ messages in thread
From: Liam R. Howlett (Google) @ 2026-10-20  3:31 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Lorenzo Stoakes, Matthew Wilcox, Vlastimil Babka, linux-mm,
	linux-kernel, 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.

Signed-off-by: Liam R. Howlett (Google) <liam@infradead.org>
---
 tools/testing/shared/linux.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/testing/shared/linux.c b/tools/testing/shared/linux.c
index e0a0693df08f5..a37d2f86d3254 100644
--- a/tools/testing/shared/linux.c
+++ b/tools/testing/shared/linux.c
@@ -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);
-- 
2.47.3


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] tools/testing/shared: catch a failed posix_memalign()
  2026-10-20  3:31 [PATCH] tools/testing/shared: catch a failed posix_memalign() Liam R. Howlett (Google)
@ 2026-10-20  9:05 ` Lorenzo Stoakes (ARM)
  0 siblings, 0 replies; 2+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-10-20  9:05 UTC (permalink / raw)
  To: Liam R. Howlett (Google)
  Cc: Andrew Morton, Matthew Wilcox, Vlastimil Babka, linux-mm, linux-kernel

On Mon, Oct 19, 2026 at 11:31:28PM -0400, Liam R. Howlett (Google) wrote:
> 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.
>
> Signed-off-by: Liam R. Howlett (Google) <liam@infradead.org>

LGTM so:

Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>

> ---
>  tools/testing/shared/linux.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/tools/testing/shared/linux.c b/tools/testing/shared/linux.c
> index e0a0693df08f5..a37d2f86d3254 100644
> --- a/tools/testing/shared/linux.c
> +++ b/tools/testing/shared/linux.c
> @@ -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);
> --
> 2.47.3
>

--
Cheers, Lorenzo

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-10-20  9:05 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-20  3:31 [PATCH] tools/testing/shared: catch a failed posix_memalign() Liam R. Howlett (Google)
2026-10-20  9:05 ` Lorenzo Stoakes (ARM)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®