linux-user: return ENOMEM from madvise on unmapped ranges
PATCH 1/2 linux-user: return ENOMEM from madvise on unmapped ranges
In reply to Roi KlevanskyThe kernel fails with ENOMEM when madvise() is called with a partially or fully unmapped address range. This fix allows target_madvise() to emulate this behavior by calling page_check_range() upfront while still allowing advice specific errors to take precedence.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4382
--- linux-user/mmap.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
linux-user/mmap.c +6 -1
@@ -1305,8 +1305,14 @@ abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice)* success, which is broken but some userspace programs fail to work * otherwise. Completely implementing such emulation is quite complicated * though. +* +* When some (or all) of the pages in the range are not mapped, madvise +* should fail with -ENOMEM. If another error occurs, its value is +* returned instead. */ mmap_lock (); + ret =page_check_range (start, len,PAGE_VALID ) ?0 : -TARGET_ENOMEM ; +switch (advice) {case MADV_NORMAL :case MADV_RANDOM :
@@ -1316,7 +1322,6 @@ abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice)case MADV_FREE :case MADV_COLD :case MADV_PAGEOUT : - ret =0 ;/* OK */ break ;case MADV_REMOVE : ret = -EOPNOTSUPP ;
Signature · 1 line
2.55.0
PATCH 2/2 tests/tcg: add linux-user test for madvise returning ENOMEM
In reply to Roi KlevanskyAdd tests for madvise() under linux-user with partially and fully unmapped address ranges, making sure it returns ENOMEM, just like the kernel does.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4382
--- tests/tcg/multiarch/linux/linux-madvise.c | 43 +++++++++++++++++++++++ 1 file changed, 43 insertions(+)
tests/tcg/multiarch/linux/linux-madvise.c +43 -0
@@ -1,4 +1,5 @@#include <assert.h> +#include <errno.h> #include <stdlib.h> #include <sys/mman.h> #include <unistd.h>
@@ -63,10 +64,52 @@ static void test_file(void)assert (ret ==0 ); } +static void test_mapped_and_unmapped (void ) +{ +int pagesize =getpagesize (); +void *page; +int ret; + + page =mmap (NULL , pagesize,PROT_READ ,MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 ); +assert (page !=MAP_FAILED ); + + ret =madvise (page, pagesize,MADV_NORMAL ); +assert (ret ==0 ); + + ret =munmap (page, pagesize); +assert (ret ==0 ); + + ret =madvise (page, pagesize,MADV_NORMAL ); +assert (ret ==-1 ); +assert (errno ==ENOMEM ); +} + +static void test_partially_unmapped_enomem (void ) +{ +int pagesize =getpagesize (); +void *page; +int ret; + + page =mmap (NULL ,3 * pagesize,PROT_READ , +MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 ); +assert (page !=MAP_FAILED ); + ret =munmap ((char *)page + pagesize, pagesize); +assert (ret ==0 ); + + ret =madvise (page,3 * pagesize,MADV_NORMAL ); +assert (ret ==-1 ); +assert (errno ==ENOMEM ); + + ret =munmap (page,3 * pagesize); +assert (ret ==0 ); +} +int main (void ) {test_anonymous ();test_file (); +test_mapped_and_unmapped (); +test_partially_unmapped_enomem ();return EXIT_SUCCESS ; }
Signature · 1 line
2.55.0
cover linux-user: return ENOMEM from madvise on unmapped ranges
The kernel fails madvise() with ENOMEM when it is given a partially or a fully unmapped address range. qemu-user differed from this and returned 0 instead.
Patch 1 makes target_madvise() check the range with page_check_range() and potentially fail with ENOMEM while still giving precedence to advice specific errors.
Patch 2 adds test cases, calling madvise() with partially and fully unmapped address ranges.
Roi Klevansky (2):
linux-user: return ENOMEM from madvise on unmapped ranges tests/tcg: add linux-user test for madvise returning ENOMEM
linux-user/mmap.c | 7 +++- tests/tcg/multiarch/linux/linux-madvise.c | 43 +++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-)
Signature · 1 line
2.55.0