selftests/dma-buf: Minor improvements to udmabuf selftest
cover selftests/dma-buf: Minor improvements to udmabuf selftest
Hi there,
two minor improvements to the udmabuf selftest:
- converting GNU-specific off64_t to 64-bit off_t for portability
- instead of uncoditional failing if not enough huge pages are available, check beforehand and skip the affected tests
Best,
Chris
Chris Gellermann (2):
selftests/dma-buf: Convert off64_t to off_t for portability selftests/dma-buf: Skip some tests if not enough free huge pages
.../selftests/drivers/dma-buf/Makefile | 1 + .../selftests/drivers/dma-buf/udmabuf.c | 50 ++++++++++++++++--- 2 files changed, 44 insertions(+), 7 deletions(-)
--
2.47.3
PATCH 1/2 selftests/dma-buf: Convert off64_t to off_t for portability
Use the POSIX type off_t [1] instead of the GNU-specific 64-bit offset type off64_t. Ensure it is 64 bit wide by compiling with _FILE_OFFSET_BITS set to 64.
--- tools/testing/selftests/drivers/dma-buf/Makefile | 1 + tools/testing/selftests/drivers/dma-buf/udmabuf.c | 14 +++++++------- 2 files changed, 8 insertions(+), 7 deletions(-)
tools/testing/selftests/drivers/dma-buf/Makefile +1 -0
@@ -1,4 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only +CFLAGS += -D_FILE_OFFSET_BITS=64 CFLAGS += $(KHDR_INCLUDES) TEST_GEN_PROGS := udmabuf
tools/testing/selftests/drivers/dma-buf/udmabuf.c +7 -7
@@ -25,7 +25,7 @@static unsigned int page_size; -static int create_memfd_with_seals (off64_t size,bool hpage) +static int create_memfd_with_seals (off_t size,bool hpage) {int memfd, ret;unsigned int flags =MFD_ALLOW_SEALING ;
@@ -54,7 +54,7 @@ static int create_memfd_with_seals(off64_t size, bool hpage)return memfd; } -static int create_udmabuf_list (int devfd,int memfd,off64_t memfd_size) +static int create_udmabuf_list (int devfd,int memfd,off_t memfd_size) {struct udmabuf_create_list *list;int ubuf_fd, i;
@@ -84,7 +84,7 @@ static int create_udmabuf_list(int devfd, int memfd, off64_t memfd_size)return ubuf_fd; } -static void write_to_memfd (void *addr,off64_t size,char chr) +static void write_to_memfd (void *addr,off_t size,char chr) {int i;
@@ -93,7 +93,7 @@ static void write_to_memfd(void *addr, off64_t size, char chr) } } -static void *mmap_fd (int fd,off64_t size) +static void *mmap_fd (int fd,off_t size) {void *addr;
@@ -106,9 +106,9 @@ static void *mmap_fd(int fd, off64_t size)return addr; } -static int compare_chunks (void *addr1,void *addr2,off64_t memfd_size) +static int compare_chunks (void *addr1,void *addr2,off_t memfd_size) { -off64_t off; +off_t off;int i =0 , j, k =0 , ret =0 ;char char1, char2;
@@ -134,7 +134,7 @@ int main(int argc, char *argv[]) {struct udmabuf_create create;int devfd, memfd, buf, ret; -off64_t size; +off_t size;void *addr1, *addr2;ksft_print_header ();
Signature · 1 line
2.47.3
PATCH 2/2 selftests/dma-buf: Skip some tests if not enough free huge pages
In reply to Chris GellermannSubtests 6 and 7 of the udmabuf test require free huge pages. If there are not enough huge pages available, the entire test fails on an mmap:
ok 4 drivers/dma-buf/udmabuf: [PASS,test-4]
ok 5 drivers/dma-buf/udmabuf: [PASS,test-5] drivers/dma-buf/udmabuf: ubuf_fd mmap fail
Instead of failing, check if there are enough huge pages available beforehand and potentially skip the subtests 6 and 7.
--- .../selftests/drivers/dma-buf/udmabuf.c | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+)
tools/testing/selftests/drivers/dma-buf/udmabuf.c +36 -0
@@ -25,6 +25,34 @@static unsigned int page_size; +static bool enough_free_huge_pages (off_t size) +{ +unsigned long free_pages =0 , hpage_size_kb =0 ; +bool found_free = false, found_size = false; +char line[256 ]; +FILE *f; + + f =fopen ("/proc/meminfo" ,"r" ); +if (!f) +return false; + +while (fgets (line,sizeof (line), f)) { +if (sscanf (line,"HugePages_Free: %lu" , &free_pages) ==1 ) + found_free = true; +if (sscanf (line,"Hugepagesize: %lu kB" , &hpage_size_kb) ==1 ) + found_size = true; +if (found_free && found_size) +break ; + } +fclose (f); + +if (!found_free || !found_size) +return false; + +return ((unsigned long long )free_pages * hpage_size_kb *1024ULL ) >= + (unsigned long long )size; +} +static int create_memfd_with_seals (off_t size,bool hpage) {int memfd, ret;
@@ -236,6 +264,13 @@ int main(int argc, char *argv[])/* should work (migration of 2MB size huge pages)*/ page_size =getpagesize () *512 ;/* 2 MB */ size =MEMFD_SIZE * page_size; +if (!enough_free_huge_pages (size)) { +ksft_test_result_skip ("%s: [SKIP,test-6] not enough huge pages\n" , +TEST_PREFIX ); +ksft_test_result_skip ("%s: [SKIP,test-7] not enough huge pages\n" , +TEST_PREFIX ); + goto out; + } memfd =create_memfd_with_seals (size, true); addr1 =mmap_fd (memfd, size);write_to_memfd (addr1, size,'a' );
@@ -268,6 +303,7 @@ int main(int argc, char *argv[])close (buf);close (memfd); +out:close (devfd);ksft_print_msg ("%s: ok\n" ,TEST_PREFIX );
Signature · 1 line
2.47.3