From patchwork Thu Jun 13 21:42:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kasireddy, Vivek" X-Patchwork-Id: 13697528 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 76DFCC27C4F for ; Thu, 13 Jun 2024 22:14:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D6E3D6B0089; Thu, 13 Jun 2024 18:13:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CD29E6B00A0; Thu, 13 Jun 2024 18:13:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AD49D6B00A1; Thu, 13 Jun 2024 18:13:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 864866B0089 for ; Thu, 13 Jun 2024 18:13:47 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 42E14A0F08 for ; Thu, 13 Jun 2024 22:13:47 +0000 (UTC) X-FDA: 82227268494.30.5E52268 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) by imf09.hostedemail.com (Postfix) with ESMTP id 128BC14000A for ; Thu, 13 Jun 2024 22:13:44 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=IIW4kSCF; spf=pass (imf09.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.8 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1718316824; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=K6deuOdligGOWcs/cUpiN5v80J3yBU/FgM9oryyRYQU=; b=l3OHGJs5AWIS/uJLP6K5NghtjRqtih/3uvh/rFDT62c+KrWkm8Qfdut7IbJIm7DLeqslOC EthL0rUwfP8bnJ0kNeFw7SJzCHFVpCk77Wnl4owxVARJyGiqSq3ROeadtchppoWKjc8viI VjQChdY1goF75cHdLNKLkW+0WTE16NU= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=IIW4kSCF; spf=pass (imf09.hostedemail.com: domain of vivek.kasireddy@intel.com designates 192.198.163.8 as permitted sender) smtp.mailfrom=vivek.kasireddy@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718316824; a=rsa-sha256; cv=none; b=hoRDKxl+IBQ4xjWB78bvLcI1TF769np+xwF4HieNKLvXvbDb7a2/5ktcBVBmTd+cfaYADz 0xl2NdjlTC3stFksYlgEpqaEmeFscHuOZuIt/sKdXWnbjavYXcZS8m2WL0yTZLCh5COQV1 EWY1Zf0jj9qNFOBg/vlBes94cw5Q7s4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1718316825; x=1749852825; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mwX/Ut3liG+2eL4EiqMYYCOWpRlKdeioXbS3wyxFBs0=; b=IIW4kSCFeNTWoKXneYzIMZpIFJWmdTKuq5xNv12jSJkb8gDpENWFEMRf MUDn1wGhV8tW8lK2jYZywmJU3SJ9YtG3Ix2V3U9AGdjvfpLgWJlRuCX40 8Vgs2uZKcC2AAHah0triaKI48huQ4pWvwuzQavIMOCi0MNPHXS9t4szn+ EEBZ2ReNFsmVKcl6e2gg3b80GPKlWCtA6P6l1avJyzzHCxmJHntvJcuTc UOCSl1yVNU4XEnvBbdNCC/cezMiUCOqACUy4tdXinKJ2/6Rudq9pkRINn qUK+aWOXu1bQ5vFE6pCRbGx3PQql8DXM4AnSp9eNy3L3c7ntFbroAuw9O Q==; X-CSE-ConnectionGUID: VZFZ9cTYRAyjDvmtrHt2Wg== X-CSE-MsgGUID: 7G2dAORPThO5vg/l74Znlw== X-IronPort-AV: E=McAfee;i="6700,10204,11102"; a="32720580" X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="32720580" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jun 2024 15:13:38 -0700 X-CSE-ConnectionGUID: Tc0wLCpTSm23vyp5b7zatw== X-CSE-MsgGUID: rE40TlMOSMimxsMr6bf50g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,236,1712646000"; d="scan'208";a="45214117" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by ORVIESA003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jun 2024 15:13:38 -0700 From: Vivek Kasireddy To: dri-devel@lists.freedesktop.org, linux-mm@kvack.org Cc: Vivek Kasireddy , Shuah Khan , David Hildenbrand , Daniel Vetter , Hugh Dickins , Peter Xu , Jason Gunthorpe , Gerd Hoffmann , Dongwon Kim , Junxiao Chang , linux-kselftest@vger.kernel.org, Mike Kravetz , Dave Airlie Subject: [PATCH v15 9/9] selftests/udmabuf: Add tests to verify data after page migration Date: Thu, 13 Jun 2024 14:42:11 -0700 Message-ID: <20240613214741.1029446-10-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240613214741.1029446-1-vivek.kasireddy@intel.com> References: <20240613214741.1029446-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 128BC14000A X-Stat-Signature: xbd9gten59dopywt5ugjr7noeunfu6of X-HE-Tag: 1718316824-584786 X-HE-Meta: U2FsdGVkX19CsY7Zdy3GS+2WkiWk+PGdCxJvKdYOFRVP6oS38yHyXETfVN59CMRXeC1ldlCiNF8NeklFQpCqZHPKefVjFaV6W4QCq/02IdqDMv31pbAbA95f7rundfz9AbS6IN9POqsgOVT2lmh3MQlUN/676IdbjD2DO4rRlOLHKmKt2C8yoNDa3+1Ag0H7L/XDEqPkjj5jqlALcTYh3Pd1wrsniPGCj79LmyNkuXOBo16Y82PZcEYF0m7aVYgCu/zdiwBGHn5CgvzQrT19qbnnEYMEryPL2k+TryC03ZoaNDSAbyw20BXYE77MCcQ5cMap+soFS+F46NOK9Nf0eohJXmImlrdk9ucqqb174nqoObhva5Q5ia/MX7SXrAxpPdBH1u3ZZqUWKVg7lW94MlsNLVOcpfVMdEHU4Z0CkZR6fcUFJVTUv5FkToUsSXAahGQWmWqR2rjtT26TShYx/HSEaebHsjuzMsgWlNhViJ6CYI8ko/PyXZHMdhadbpzWAMJm8KhtPespnijThUcU8zgCp9rtGLNbRKtjZCr+NaVNchXPeT7s6F2/w9fqxrH6kL7KDqaJ4wfTZp1i0NpixblUfXXhzqYRRBK7SMrARHxKxMm//0vm1yPcACciMbAZqCjbr9H5YOT4o4mseVOpvRaZdEfR8NeOipK+a5XkWk7pud4c8R0eifZFa8CfFDk2GtoTWAplIQYbBr4lnoIuDTpJVqKeGRNI24KzeEGZZhbR/uV8GJY5dGSR9cZOXLPK6u1hfAjf5AGcRvosDLNhRtRUvP9xHO4oky/3tSYPUvgEbXQLvJ7v2gLCvWu7Trs4i8YF28okRtKQ6ZqMQ5RGeYqpP21gQh/WSr64YmOLx1kZ0SKHaCAenPAcokE/35RgrlC6+2VoC1/kg7I8n7nQ/65yT14cxh7DzUxcrqbVCkaLZlibRJfVy+KmKuiR4BsTk6BWnXvEO5ZsSxbCiRT kPE8mZDt V6c4D96EylEuTSE83IPoQVtcAHH56yZA2VQvnUzuxI6EnPysaNvGnf4WD9nV1j6/K9NLX5uO3gQSZ8VNBx7dwxGT2fYwcpEtAtCR5DarlgplFyM0PdwLzSNT5KrqElqzra3XsfY4kWo6D8xNBBwRf2W4Dfv3/ktNdfmpZIGqUQxG8mzk3zjhHiQNF9s4dZO9rdOoHOBSlIkIb1/3zVb8J0y+9McdHV55oKe4cd84S9B49Ot8BAeme8LC+W+moLfyOUYHiCIdG64RXii+vIJEQCkWfrqUs+Qvo8uxLLQR2sRqa6tKTwhm/SCVXUL+JBWcxXuTONKdHlzWrrEoHAqSu6LCCoFFy7CAj0QaFNYcuZluwjiEfZoRN70wSbhQwN5BmEInHLZa8AJL75HU= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Since the memfd pages associated with a udmabuf may be migrated as part of udmabuf create, we need to verify the data coherency after successful migration. The new tests added in this patch try to do just that using 4k sized pages and also 2 MB sized huge pages for the memfd. Successful completion of the tests would mean that there is no disconnect between the memfd pages and the ones associated with a udmabuf. And, these tests can also be augmented in the future to test newer udmabuf features (such as handling memfd hole punch). The idea for these tests comes from a patch by Mike Kravetz here: https://lists.freedesktop.org/archives/dri-devel/2023-June/410623.html v1->v2: (suggestions from Shuah) - Use ksft_* functions to print and capture results of tests - Use appropriate KSFT_* status codes for exit() - Add Mike Kravetz's suggested-by tag Cc: Shuah Khan Cc: David Hildenbrand Cc: Daniel Vetter Cc: Hugh Dickins Cc: Peter Xu Cc: Jason Gunthorpe Cc: Gerd Hoffmann Cc: Dongwon Kim Cc: Junxiao Chang Cc: linux-kselftest@vger.kernel.org Suggested-by: Mike Kravetz Acked-by: Dave Airlie Acked-by: Gerd Hoffmann Signed-off-by: Vivek Kasireddy --- .../selftests/drivers/dma-buf/udmabuf.c | 214 +++++++++++++++--- 1 file changed, 183 insertions(+), 31 deletions(-) diff --git a/tools/testing/selftests/drivers/dma-buf/udmabuf.c b/tools/testing/selftests/drivers/dma-buf/udmabuf.c index c812080e304e..6062723a172e 100644 --- a/tools/testing/selftests/drivers/dma-buf/udmabuf.c +++ b/tools/testing/selftests/drivers/dma-buf/udmabuf.c @@ -9,52 +9,162 @@ #include #include #include +#include #include #include +#include #include #include +#include "../../kselftest.h" #define TEST_PREFIX "drivers/dma-buf/udmabuf" #define NUM_PAGES 4 +#define NUM_ENTRIES 4 +#define MEMFD_SIZE 1024 /* in pages */ -static int memfd_create(const char *name, unsigned int flags) +static unsigned int page_size; + +static int create_memfd_with_seals(off64_t size, bool hpage) +{ + int memfd, ret; + unsigned int flags = MFD_ALLOW_SEALING; + + if (hpage) + flags |= MFD_HUGETLB; + + memfd = memfd_create("udmabuf-test", flags); + if (memfd < 0) { + ksft_print_msg("%s: [skip,no-memfd]\n", TEST_PREFIX); + exit(KSFT_SKIP); + } + + ret = fcntl(memfd, F_ADD_SEALS, F_SEAL_SHRINK); + if (ret < 0) { + ksft_print_msg("%s: [skip,fcntl-add-seals]\n", TEST_PREFIX); + exit(KSFT_SKIP); + } + + ret = ftruncate(memfd, size); + if (ret == -1) { + ksft_print_msg("%s: [FAIL,memfd-truncate]\n", TEST_PREFIX); + exit(KSFT_FAIL); + } + + return memfd; +} + +static int create_udmabuf_list(int devfd, int memfd, off64_t memfd_size) +{ + struct udmabuf_create_list *list; + int ubuf_fd, i; + + list = malloc(sizeof(struct udmabuf_create_list) + + sizeof(struct udmabuf_create_item) * NUM_ENTRIES); + if (!list) { + ksft_print_msg("%s: [FAIL, udmabuf-malloc]\n", TEST_PREFIX); + exit(KSFT_FAIL); + } + + for (i = 0; i < NUM_ENTRIES; i++) { + list->list[i].memfd = memfd; + list->list[i].offset = i * (memfd_size / NUM_ENTRIES); + list->list[i].size = getpagesize() * NUM_PAGES; + } + + list->count = NUM_ENTRIES; + list->flags = UDMABUF_FLAGS_CLOEXEC; + ubuf_fd = ioctl(devfd, UDMABUF_CREATE_LIST, list); + free(list); + if (ubuf_fd < 0) { + ksft_print_msg("%s: [FAIL, udmabuf-create]\n", TEST_PREFIX); + exit(KSFT_FAIL); + } + + return ubuf_fd; +} + +static void write_to_memfd(void *addr, off64_t size, char chr) +{ + int i; + + for (i = 0; i < size / page_size; i++) { + *((char *)addr + (i * page_size)) = chr; + } +} + +static void *mmap_fd(int fd, off64_t size) { - return syscall(__NR_memfd_create, name, flags); + void *addr; + + addr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + if (addr == MAP_FAILED) { + ksft_print_msg("%s: ubuf_fd mmap fail\n", TEST_PREFIX); + exit(KSFT_FAIL); + } + + return addr; +} + +static int compare_chunks(void *addr1, void *addr2, off64_t memfd_size) +{ + off64_t off; + int i = 0, j, k = 0, ret = 0; + char char1, char2; + + while (i < NUM_ENTRIES) { + off = i * (memfd_size / NUM_ENTRIES); + for (j = 0; j < NUM_PAGES; j++, k++) { + char1 = *((char *)addr1 + off + (j * getpagesize())); + char2 = *((char *)addr2 + (k * getpagesize())); + if (char1 != char2) { + ret = -1; + goto err; + } + } + i++; + } +err: + munmap(addr1, memfd_size); + munmap(addr2, NUM_ENTRIES * NUM_PAGES * getpagesize()); + return ret; } int main(int argc, char *argv[]) { struct udmabuf_create create; int devfd, memfd, buf, ret; - off_t size; - void *mem; + off64_t size; + void *addr1, *addr2; + + ksft_print_header(); + ksft_set_plan(6); devfd = open("/dev/udmabuf", O_RDWR); if (devfd < 0) { - printf("%s: [skip,no-udmabuf: Unable to access DMA buffer device file]\n", - TEST_PREFIX); - exit(77); + ksft_print_msg( + "%s: [skip,no-udmabuf: Unable to access DMA buffer device file]\n", + TEST_PREFIX); + exit(KSFT_SKIP); } memfd = memfd_create("udmabuf-test", MFD_ALLOW_SEALING); if (memfd < 0) { - printf("%s: [skip,no-memfd]\n", TEST_PREFIX); - exit(77); + ksft_print_msg("%s: [skip,no-memfd]\n", TEST_PREFIX); + exit(KSFT_SKIP); } ret = fcntl(memfd, F_ADD_SEALS, F_SEAL_SHRINK); if (ret < 0) { - printf("%s: [skip,fcntl-add-seals]\n", TEST_PREFIX); - exit(77); + ksft_print_msg("%s: [skip,fcntl-add-seals]\n", TEST_PREFIX); + exit(KSFT_SKIP); } - size = getpagesize() * NUM_PAGES; ret = ftruncate(memfd, size); if (ret == -1) { - printf("%s: [FAIL,memfd-truncate]\n", TEST_PREFIX); - exit(1); + ksft_print_msg("%s: [FAIL,memfd-truncate]\n", TEST_PREFIX); + exit(KSFT_FAIL); } memset(&create, 0, sizeof(create)); @@ -64,44 +174,86 @@ int main(int argc, char *argv[]) create.offset = getpagesize()/2; create.size = getpagesize(); buf = ioctl(devfd, UDMABUF_CREATE, &create); - if (buf >= 0) { - printf("%s: [FAIL,test-1]\n", TEST_PREFIX); - exit(1); - } + if (buf >= 0) + ksft_test_result_fail("%s: [FAIL,test-1]\n", TEST_PREFIX); + else + ksft_test_result_pass("%s: [PASS,test-1]\n", TEST_PREFIX); /* should fail (size not multiple of page) */ create.memfd = memfd; create.offset = 0; create.size = getpagesize()/2; buf = ioctl(devfd, UDMABUF_CREATE, &create); - if (buf >= 0) { - printf("%s: [FAIL,test-2]\n", TEST_PREFIX); - exit(1); - } + if (buf >= 0) + ksft_test_result_fail("%s: [FAIL,test-2]\n", TEST_PREFIX); + else + ksft_test_result_pass("%s: [PASS,test-2]\n", TEST_PREFIX); /* should fail (not memfd) */ create.memfd = 0; /* stdin */ create.offset = 0; create.size = size; buf = ioctl(devfd, UDMABUF_CREATE, &create); - if (buf >= 0) { - printf("%s: [FAIL,test-3]\n", TEST_PREFIX); - exit(1); - } + if (buf >= 0) + ksft_test_result_fail("%s: [FAIL,test-3]\n", TEST_PREFIX); + else + ksft_test_result_pass("%s: [PASS,test-3]\n", TEST_PREFIX); /* should work */ + page_size = getpagesize(); + addr1 = mmap_fd(memfd, size); + write_to_memfd(addr1, size, 'a'); create.memfd = memfd; create.offset = 0; create.size = size; buf = ioctl(devfd, UDMABUF_CREATE, &create); - if (buf < 0) { - printf("%s: [FAIL,test-4]\n", TEST_PREFIX); - exit(1); - } + if (buf < 0) + ksft_test_result_fail("%s: [FAIL,test-4]\n", TEST_PREFIX); + else + ksft_test_result_pass("%s: [PASS,test-4]\n", TEST_PREFIX); + + munmap(addr1, size); + close(buf); + close(memfd); + + /* should work (migration of 4k size pages)*/ + size = MEMFD_SIZE * page_size; + memfd = create_memfd_with_seals(size, false); + addr1 = mmap_fd(memfd, size); + write_to_memfd(addr1, size, 'a'); + buf = create_udmabuf_list(devfd, memfd, size); + addr2 = mmap_fd(buf, NUM_PAGES * NUM_ENTRIES * getpagesize()); + write_to_memfd(addr1, size, 'b'); + ret = compare_chunks(addr1, addr2, size); + if (ret < 0) + ksft_test_result_fail("%s: [FAIL,test-5]\n", TEST_PREFIX); + else + ksft_test_result_pass("%s: [PASS,test-5]\n", TEST_PREFIX); + + close(buf); + close(memfd); + + /* should work (migration of 2MB size huge pages)*/ + page_size = getpagesize() * 512; /* 2 MB */ + size = MEMFD_SIZE * page_size; + memfd = create_memfd_with_seals(size, true); + addr1 = mmap_fd(memfd, size); + write_to_memfd(addr1, size, 'a'); + buf = create_udmabuf_list(devfd, memfd, size); + addr2 = mmap_fd(buf, NUM_PAGES * NUM_ENTRIES * getpagesize()); + write_to_memfd(addr1, size, 'b'); + ret = compare_chunks(addr1, addr2, size); + if (ret < 0) + ksft_test_result_fail("%s: [FAIL,test-6]\n", TEST_PREFIX); + else + ksft_test_result_pass("%s: [PASS,test-6]\n", TEST_PREFIX); - fprintf(stderr, "%s: ok\n", TEST_PREFIX); close(buf); close(memfd); close(devfd); + + ksft_print_msg("%s: ok\n", TEST_PREFIX); + ksft_print_cnts(); + return 0; }