Skip to contentSkip to conversation

selftests/net: use MAP_FAILED instead of (void *)-1 in tcp_mmap

PATCH Applied longlong yan 4 participants 4 messages Jul 22 – Jul 23

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/net: use MAP_FAILED instead of (void *)-1 in tcp_mmap

mmap() is documented to return MAP_FAILED on error, but tcp_mmap.c compares the return value against (void *)-1 and (unsigned char *)-1.

Replace these with the standard MAP_FAILED macro for better readability and type safety.

---
 tools/testing/selftests/net/tcp_mmap.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
tools/testing/selftests/net/tcp_mmap.c +5 -5
index 2544ae35d07a..487ae659a1f1 100644
@@ -141,12 +141,12 @@ static void *mmap_large_buffer(size_t need, size_t *allocated)
 	buffer = mmap(NULL, sz, PROT_READ | PROT_WRITE,
 		      MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
 
-	if (buffer == (void *)-1) {
+	if (buffer == MAP_FAILED) {
 		sz = need;
 		buffer = mmap(NULL, sz, PROT_READ | PROT_WRITE,
 			      MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE,
 			      -1, 0);
-		if (buffer != (void *)-1)
+		if (buffer != MAP_FAILED)
 			fprintf(stderr, "MAP_HUGETLB attempt failed, look at /sys/kernel/mm/hugepages for optimal performance\n");
 	}
 	*allocated = sz;
@@ -189,13 +189,13 @@ void *child_thread(void *arg)
 
 	fcntl(fd, F_SETFL, O_NDELAY);
 	buffer = mmap_large_buffer(chunk_size, &buffer_sz);
-	if (buffer == (void *)-1) {
+	if (buffer == MAP_FAILED) {
 		perror("mmap");
 		goto error;
 	}
 	if (zflg) {
 		raddr = mmap(NULL, chunk_size + map_align, PROT_READ, flags, fd, 0);
-		if (raddr == (void *)-1) {
+		if (raddr == MAP_FAILED) {
 			perror("mmap");
 			zflg = 0;
 		} else {
@@ -547,7 +547,7 @@ int main(int argc, char *argv[])
 	}
 
 	buffer = mmap_large_buffer(chunk_size, &buffer_sz);
-	if (buffer == (unsigned char *)-1) {
+	if (buffer == MAP_FAILED) {
 		perror("mmap");
 		exit(1);
 	}
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · longlong yan

mmap() is documented to return MAP_FAILED on error, but tcp_mmap.c compares the return value against (void *)-1 and (unsigned char *)-1.

Replace these with the standard MAP_FAILED macro for better readability and type safety.

---
 tools/testing/selftests/net/tcp_mmap.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

Looks right to me. Not sure if it needs to be set with "net-next" in the subject line to make which tree this targets more explicit?

Either way:

Reviewed-by
#2 lore raw
Quoted 14 lines · Joe Damato
Quoted 11 lines · longlong yan

mmap() is documented to return MAP_FAILED on error, but tcp_mmap.c compares the return value against (void *)-1 and (unsigned char *)-1.

Replace these with the standard MAP_FAILED macro for better readability and type safety.

---
 tools/testing/selftests/net/tcp_mmap.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
Quoted 6 lines

Looks right to me. Not sure if it needs to be set with "net-next" in the subject line to make which tree this targets more explicit?

Either way:

Yeah, I doubt linux will ever use a different value than (void *)-1 for MAP_FAILED.

#3 lore raw
In reply to longlong yan
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 9 lines · On Wed, 22 Jul 2026 09:51:29 +0800 you wrote:

mmap() is documented to return MAP_FAILED on error, but tcp_mmap.c compares the return value against (void *)-1 and (unsigned char *)-1.

Replace these with the standard MAP_FAILED macro for better readability and type safety.

[...]

Here is the summary with links:
- selftests/net: use MAP_FAILED instead of (void *)-1 in tcp_mmap

    https://git.kernel.org/netdev/net-next/c/94cdc6a2c837

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html