From patchwork Wed Apr 2 16:07:21 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikita Kalyazin X-Patchwork-Id: 14036212 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 63306C28B20 for ; Wed, 2 Apr 2025 16:08:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CB55528000B; Wed, 2 Apr 2025 12:08:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C3E34280001; Wed, 2 Apr 2025 12:08:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A93CF28000B; Wed, 2 Apr 2025 12:08:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 85ACA280001 for ; Wed, 2 Apr 2025 12:08:36 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 5EAD6121C6A for ; Wed, 2 Apr 2025 16:08:36 +0000 (UTC) X-FDA: 83289586632.02.919C79E Received: from smtp-fw-80006.amazon.com (smtp-fw-80006.amazon.com [99.78.197.217]) by imf20.hostedemail.com (Postfix) with ESMTP id 2FEC61C000B for ; Wed, 2 Apr 2025 16:08:33 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=YEbD3va5; spf=pass (imf20.hostedemail.com: domain of "prvs=1800b2f01=kalyazin@amazon.co.uk" designates 99.78.197.217 as permitted sender) smtp.mailfrom="prvs=1800b2f01=kalyazin@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1743610114; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=wTofU1xt/dJK2QsInBbhwR8+NZ3Sv3EgYY1jxlgxsY4=; b=b3by0VvqHbekaIwKXclggBCmdrE5kc6ggyolv+VbsV7olHXm1lR6yhlOGjmEFTQzgmt4bu 4l9b3oMO6sO20EyzAcJEcgtPr3ixDJ0BiZCBzWOlOBHnFuaGcMnNZZMM7gKVuH9amqLNce cGJyIF9OB+IY+w4ixqtQqmvNC/poTxg= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=YEbD3va5; spf=pass (imf20.hostedemail.com: domain of "prvs=1800b2f01=kalyazin@amazon.co.uk" designates 99.78.197.217 as permitted sender) smtp.mailfrom="prvs=1800b2f01=kalyazin@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1743610114; a=rsa-sha256; cv=none; b=G0sbeeHPgNcAjXcs9kdTYJLEeiVN6bimjIC6x1qkTTgllVVL1fSOK+JsExKOpC7pwf00Rt 3eMgFmp2cz8qB4Gq+Ni4DKb/CzvhAM8N/cvm4//oxu5quSCmQC8A6fBuD4Lop79tawr/7q b8r7Iy5EjJTtZfKyn5eppEilbsUb6dQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1743610114; x=1775146114; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=wTofU1xt/dJK2QsInBbhwR8+NZ3Sv3EgYY1jxlgxsY4=; b=YEbD3va5c0UREH7Zvs1YjpdApdZ/S3adVmfdUgmHFNTX4CoKS7/Zu2+V JGvUSxfUtxo2/ecwPg7xs3ykfQbkha+5X9uP0FgUePst8stEJ64DsN02C LmZokj33bBOUHIvYByAE/mhJWSbV8kjsfP4k+hmszwro2QNEKpRakfR9K A=; X-IronPort-AV: E=Sophos;i="6.15,182,1739836800"; d="scan'208";a="37468501" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.214]) by smtp-border-fw-80006.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Apr 2025 16:08:30 +0000 Received: from EX19MTAUWA001.ant.amazon.com [10.0.21.151:61509] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.3.46:2525] with esmtp (Farcaster) id bacfcabb-94e1-4d19-a36d-27c598e119db; Wed, 2 Apr 2025 16:08:30 +0000 (UTC) X-Farcaster-Flow-ID: bacfcabb-94e1-4d19-a36d-27c598e119db Received: from EX19D020UWA003.ant.amazon.com (10.13.138.254) by EX19MTAUWA001.ant.amazon.com (10.250.64.217) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 2 Apr 2025 16:08:29 +0000 Received: from EX19MTAUWC002.ant.amazon.com (10.250.64.143) by EX19D020UWA003.ant.amazon.com (10.13.138.254) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Wed, 2 Apr 2025 16:08:29 +0000 Received: from email-imr-corp-prod-pdx-all-2b-f5cd2367.us-west-2.amazon.com (10.25.36.210) by mail-relay.amazon.com (10.250.64.149) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14 via Frontend Transport; Wed, 2 Apr 2025 16:08:29 +0000 Received: from dev-dsk-kalyazin-1a-a12e27e2.eu-west-1.amazon.com (dev-dsk-kalyazin-1a-a12e27e2.eu-west-1.amazon.com [172.19.103.116]) by email-imr-corp-prod-pdx-all-2b-f5cd2367.us-west-2.amazon.com (Postfix) with ESMTPS id 06432C061B; Wed, 2 Apr 2025 16:08:26 +0000 (UTC) From: Nikita Kalyazin To: , , CC: , , , , , , , , , , , , , , , , Subject: [PATCH v2 5/5] KVM: selftests: test userfaultfd minor for guest_memfd Date: Wed, 2 Apr 2025 16:07:21 +0000 Message-ID: <20250402160721.97596-6-kalyazin@amazon.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250402160721.97596-1-kalyazin@amazon.com> References: <20250402160721.97596-1-kalyazin@amazon.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 2FEC61C000B X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: eqmoriocubt1r5tdapqfzi337ez4dnua X-HE-Tag: 1743610113-621461 X-HE-Meta: U2FsdGVkX18BTJs8otfd2Af9DIkK/2Dsq4ieViljPzNXGI4j1QOv8J3BNnaQwh+7HlOdp9vp9YL7OyH9d3pwp6xSzIizKreKbTHpSQVc88qcJmLI2I1dE9aBKAdimjonf+59QGc483PZ3YKqWWMnwkQfSXhaV/ahB99HQF3rvQy48zGzuipLjVTBCniJkan3hZHDqyJhT3NUT/Rpc4zBhr8rWPCURFiaWXO6/aX4Kd0maeAnk9xBV4Imp0CaVhcWskusjRKNQcgbw9LO5laLji+HoyTf4Ohi+qp73ZH01qeNRe7VCLoiCVZaUCXwRMm23+JdSqlzyNQtlKrz6Ql7UAxqEppsROuAmMILyWJ+gT/dfssiCs0S34wgNrKyB9Lpfyf5DwYc0m2F5IEHsQxdtIF/HULx7o/DNXz/gZsdxPGsSaDZkek2Dz8S+KNM0F5FBvQdWIoyjn24SFcD8nC04JXUi2VNtVGvUT3A52+XrjZZaPPN9ApjXwQdVArh5+Q/eqWUJulWqg3E6IIDou0kW0VKF/JY4Lhpb74U1rHM28R4bls2SRbpgh8VQE4+qEXdWrNyZCfr9fvjH4FczcrnE3atEBT7cN6l5kzjtV4thEIZeTLGIFMpVgxUdvd5CUb/Qi3EUXdsmpZKn6VCLzZLHaBBw/l+as7KOOobNY6noCSeLHZWGF3LCLwv10zwN7ffFMThV+JC8dXssYmpO+lv9QD9tEOnOpm4Ya7emMr1EcCqTc9tmXA1h9W8lXQUJubejqIvrhOlvucoYt0ezff3qvfqQOGbmnd/lrAJQRngQNE6IyvVucYPCwPZ8bhzRrPXtRv2Fo6ZsPciAuox+NGu7vp3Jvk6PiyqMmVwoPLOYK/CB4I8bb6CVFDQkGTSMjZ7FMQjmTMPgsUG8irw89vh6a6tPUIS2zBmAGuMGXTRhTz/yc9BLdtWIfyhqx1lb+mpra6EeAfgIdfLzvpia5g QrX0oVtu VzZSHzXAKqsrhnxWl1lnwA8F1o7jORRe6SUcpsfQ9dTvjckpwSh+3q7PFZeLWb4N6Dk2tEyrhUJoZbZWBG/FLafWx4Dt0de97nB5yPDY1jNDCuN9g/t1IKrGJw4pYndaV5mnJSeRmwYLPRkXZjjjDGj88Qom4x6lD7gYmh0HBztngpU5/OxleiEQoGYD8i+VY3k9vblefWfAu+C+TWim3Xv9Pn17NJWkSkIB2T8XJRYNgJI4xW0I6vIeD0JXezc1AetV18mn/sMEZ1kAcwF+Byg+zEwcJjYJqCE4N5FBDiwQ3Lf/WJiwQssU9edzXTOq8ebVWFG1PRYgPNMGXUHuGeWBR1VLjqUgi4Bqs8Wt+dV8Y0s58EjFN5AWfED+LSxzjC5OYsJgHPrTQLYqFE5KMBXrXCcXdC7JR71CtLeBxP0ygrrH7Gc3B+IfpFtF3Rpi5od9rYZ2oGAJdW3N3NIkn0RQSwMT52zdpSLxOo/1xIr1pXkYuTxmQ7qaUe/Yesgu1bwcDHYBdIXQaKXQ8LelhETGxlRRb0CnvhWYuIKeG3DAX1dU= 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: The test demonstrates that a minor userfaultfd event in guest_memfd can be resolved via a memcpy followed by a UFFDIO_CONTINUE ioctl. Signed-off-by: Nikita Kalyazin --- .../testing/selftests/kvm/guest_memfd_test.c | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c index 38c501e49e0e..9b47b796f3aa 100644 --- a/tools/testing/selftests/kvm/guest_memfd_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -10,12 +10,16 @@ #include #include #include +#include #include #include +#include #include #include #include +#include +#include #include "kvm_util.h" #include "test_util.h" @@ -206,6 +210,93 @@ static void test_create_guest_memfd_multiple(struct kvm_vm *vm) close(fd1); } +struct fault_args { + char *addr; + volatile char value; +}; + +static void *fault_thread_fn(void *arg) +{ + struct fault_args *args = arg; + + /* Trigger page fault */ + args->value = *args->addr; + return NULL; +} + +static void test_uffd_missing(int fd, size_t page_size, size_t total_size) +{ + struct uffdio_register uffd_reg; + struct uffdio_continue uffd_cont; + struct uffd_msg msg; + struct fault_args args; + pthread_t fault_thread; + void *mem, *mem_nofault, *buf = NULL; + int uffd, ret; + off_t offset = page_size; + void *fault_addr; + + ret = posix_memalign(&buf, page_size, total_size); + TEST_ASSERT_EQ(ret, 0); + + uffd = syscall(__NR_userfaultfd, O_CLOEXEC); + TEST_ASSERT(uffd != -1, "userfaultfd creation should succeed"); + + struct uffdio_api uffdio_api = { + .api = UFFD_API, + .features = UFFD_FEATURE_MISSING_SHMEM, + }; + ret = ioctl(uffd, UFFDIO_API, &uffdio_api); + TEST_ASSERT(ret != -1, "ioctl(UFFDIO_API) should succeed"); + + mem = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + TEST_ASSERT(mem != MAP_FAILED, "mmap should succeed"); + + mem_nofault = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + TEST_ASSERT(mem_nofault != MAP_FAILED, "mmap should succeed"); + + uffd_reg.range.start = (unsigned long)mem; + uffd_reg.range.len = total_size; + uffd_reg.mode = UFFDIO_REGISTER_MODE_MINOR; + ret = ioctl(uffd, UFFDIO_REGISTER, &uffd_reg); + TEST_ASSERT(ret != -1, "ioctl(UFFDIO_REGISTER) should succeed"); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, + offset, page_size); + TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) should succeed"); + + fault_addr = mem + offset; + args.addr = fault_addr; + + ret = pthread_create(&fault_thread, NULL, fault_thread_fn, &args); + TEST_ASSERT(ret == 0, "pthread_create should succeed"); + + ret = read(uffd, &msg, sizeof(msg)); + TEST_ASSERT(ret != -1, "read from userfaultfd should succeed"); + TEST_ASSERT(msg.event == UFFD_EVENT_PAGEFAULT, "event type should be pagefault"); + TEST_ASSERT((void *)(msg.arg.pagefault.address & ~(page_size - 1)) == fault_addr, + "pagefault should occur at expected address"); + + memcpy(mem_nofault + offset, buf + offset, page_size); + + uffd_cont.range.start = (unsigned long)fault_addr; + uffd_cont.range.len = page_size; + uffd_cont.mode = 0; + ret = ioctl(uffd, UFFDIO_CONTINUE, &uffd_cont); + TEST_ASSERT(ret != -1, "ioctl(UFFDIO_CONTINUE) should succeed"); + + ret = pthread_join(fault_thread, NULL); + TEST_ASSERT(ret == 0, "pthread_join should succeed"); + + ret = munmap(mem_nofault, total_size); + TEST_ASSERT(!ret, "munmap should succeed"); + + ret = munmap(mem, total_size); + TEST_ASSERT(!ret, "munmap should succeed"); + free(buf); + close(uffd); +} + unsigned long get_shared_type(void) { #ifdef __x86_64__ @@ -244,6 +335,9 @@ void test_vm_type(unsigned long type, bool is_shared) test_fallocate(fd, page_size, total_size); test_invalid_punch_hole(fd, page_size, total_size); + if (is_shared) + test_uffd_missing(fd, page_size, total_size); + close(fd); kvm_vm_release(vm); }