* [PATCH v2] selftests:mm: Fix mmap() error paths to check for MAP_FAILED
@ 2024-08-22 12:00 Yang Ruibin
2024-08-22 12:29 ` Dev Jain
0 siblings, 1 reply; 2+ messages in thread
From: Yang Ruibin @ 2024-08-22 12:00 UTC (permalink / raw)
To: Andy Whitcroft, Joe Perches, Dwaipayan Ray, Lukas Bulwahn,
Andrew Morton, Shuah Khan, linux-kernel, linux-mm,
linux-kselftest
Cc: opensource.kernel, Yang Ruibin
To correctly detect whether mmap is successful, must use if
(map_ptr == MAP_FAILED)to avoid incorrectly handling a valid
mapping.Fix mmap() error paths to check for MAP_FAILED.
Signed-off-by: Yang Ruibin <11162571@vivo.com>
---
tools/testing/selftests/mm/ksm_tests.c | 2 +-
tools/testing/selftests/mm/madv_populate.c | 2 +-
tools/testing/selftests/mm/soft-dirty.c | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/tools/testing/selftests/mm/ksm_tests.c b/tools/testing/selftests/mm/ksm_tests.c
index b748c4890..dad54840f 100644
--- a/tools/testing/selftests/mm/ksm_tests.c
+++ b/tools/testing/selftests/mm/ksm_tests.c
@@ -201,7 +201,7 @@ static void *allocate_memory(void *ptr, int prot, int mapping, char data, size_
{
void *map_ptr = mmap(ptr, map_size, PROT_WRITE, mapping, -1, 0);
- if (!map_ptr) {
+ if (map_ptr == MAP_FAILED) {
perror("mmap");
return NULL;
}
diff --git a/tools/testing/selftests/mm/madv_populate.c b/tools/testing/selftests/mm/madv_populate.c
index ef7d911da..b89cb83ca 100644
--- a/tools/testing/selftests/mm/madv_populate.c
+++ b/tools/testing/selftests/mm/madv_populate.c
@@ -34,7 +34,7 @@ static void sense_support(void)
addr = mmap(0, pagesize, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
- if (!addr)
+ if (addr == MAP_FAILED)
ksft_exit_fail_msg("mmap failed\n");
ret = madvise(addr, pagesize, MADV_POPULATE_READ);
diff --git a/tools/testing/selftests/mm/soft-dirty.c b/tools/testing/selftests/mm/soft-dirty.c
index bdfa5d085..4ccbc053b 100644
--- a/tools/testing/selftests/mm/soft-dirty.c
+++ b/tools/testing/selftests/mm/soft-dirty.c
@@ -134,7 +134,7 @@ static void test_mprotect(int pagemap_fd, int pagesize, bool anon)
if (anon) {
map = mmap(NULL, pagesize, PROT_READ|PROT_WRITE,
MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
- if (!map)
+ if (map == MAP_FAILED)
ksft_exit_fail_msg("anon mmap failed\n");
} else {
test_fd = open(fname, O_RDWR | O_CREAT, 0664);
--
2.34.1
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH v2] selftests:mm: Fix mmap() error paths to check for MAP_FAILED
2024-08-22 12:00 [PATCH v2] selftests:mm: Fix mmap() error paths to check for MAP_FAILED Yang Ruibin
@ 2024-08-22 12:29 ` Dev Jain
0 siblings, 0 replies; 2+ messages in thread
From: Dev Jain @ 2024-08-22 12:29 UTC (permalink / raw)
To: Yang Ruibin, Andy Whitcroft, Joe Perches, Dwaipayan Ray,
Lukas Bulwahn, Andrew Morton, Shuah Khan, linux-kernel, linux-mm,
linux-kselftest
Cc: opensource.kernel
On 8/22/24 17:30, Yang Ruibin wrote:
> To correctly detect whether mmap is successful, must use if
> (map_ptr == MAP_FAILED)to avoid incorrectly handling a valid
> mapping.Fix mmap() error paths to check for MAP_FAILED.
Shouldn't you have written "avoid processing the return value
from a failed mmap" instead of "avoid incorrectly handling a
valid mapping", because !addr won't be a valid mapping? Also,
I guess it would be better to use ksft_exit_fail_perror(), but
I won't insist.
>
> Signed-off-by: Yang Ruibin <11162571@vivo.com>
> ---
> tools/testing/selftests/mm/ksm_tests.c | 2 +-
> tools/testing/selftests/mm/madv_populate.c | 2 +-
> tools/testing/selftests/mm/soft-dirty.c | 2 +-
> 3 files changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/tools/testing/selftests/mm/ksm_tests.c b/tools/testing/selftests/mm/ksm_tests.c
> index b748c4890..dad54840f 100644
> --- a/tools/testing/selftests/mm/ksm_tests.c
> +++ b/tools/testing/selftests/mm/ksm_tests.c
> @@ -201,7 +201,7 @@ static void *allocate_memory(void *ptr, int prot, int mapping, char data, size_
> {
> void *map_ptr = mmap(ptr, map_size, PROT_WRITE, mapping, -1, 0);
>
> - if (!map_ptr) {
> + if (map_ptr == MAP_FAILED) {
> perror("mmap");
> return NULL;
> }
> diff --git a/tools/testing/selftests/mm/madv_populate.c b/tools/testing/selftests/mm/madv_populate.c
> index ef7d911da..b89cb83ca 100644
> --- a/tools/testing/selftests/mm/madv_populate.c
> +++ b/tools/testing/selftests/mm/madv_populate.c
> @@ -34,7 +34,7 @@ static void sense_support(void)
>
> addr = mmap(0, pagesize, PROT_READ | PROT_WRITE,
> MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
> - if (!addr)
> + if (addr == MAP_FAILED)
> ksft_exit_fail_msg("mmap failed\n");
>
> ret = madvise(addr, pagesize, MADV_POPULATE_READ);
> diff --git a/tools/testing/selftests/mm/soft-dirty.c b/tools/testing/selftests/mm/soft-dirty.c
> index bdfa5d085..4ccbc053b 100644
> --- a/tools/testing/selftests/mm/soft-dirty.c
> +++ b/tools/testing/selftests/mm/soft-dirty.c
> @@ -134,7 +134,7 @@ static void test_mprotect(int pagemap_fd, int pagesize, bool anon)
> if (anon) {
> map = mmap(NULL, pagesize, PROT_READ|PROT_WRITE,
> MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
> - if (!map)
> + if (map == MAP_FAILED)
> ksft_exit_fail_msg("anon mmap failed\n");
> } else {
> test_fd = open(fname, O_RDWR | O_CREAT, 0664);
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-08-22 12:29 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-08-22 12:00 [PATCH v2] selftests:mm: Fix mmap() error paths to check for MAP_FAILED Yang Ruibin
2024-08-22 12:29 ` Dev Jain
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox