From patchwork Wed Jan 29 17:23:20 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13953975 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 45424C02192 for ; Wed, 29 Jan 2025 17:23:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DF637280262; Wed, 29 Jan 2025 12:23:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DA2EF280067; Wed, 29 Jan 2025 12:23:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BF49F280262; Wed, 29 Jan 2025 12:23:49 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 9C787280067 for ; Wed, 29 Jan 2025 12:23:49 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 44F8580F65 for ; Wed, 29 Jan 2025 17:23:49 +0000 (UTC) X-FDA: 83061161778.21.4AA7D51 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf21.hostedemail.com (Postfix) with ESMTP id 75B8B1C0004 for ; Wed, 29 Jan 2025 17:23:47 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=1jxitcoW; spf=pass (imf21.hostedemail.com: domain of 3ImSaZwUKCJcK12217FF7C5.3FDC9ELO-DDBM13B.FI7@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3ImSaZwUKCJcK12217FF7C5.3FDC9ELO-DDBM13B.FI7@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738171427; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3zrvzGaN8zaP+D06MaP8yPSKdvx6oMhYyqVOrUtzvXY=; b=dETHmCWQeijfINY2wGTM7YxmLtvfKmW1R/SY4ZWPznLt5+xOQTkxCQOtY9gf7rGMSG/T21 na94rDolhc5zjQaFmriOe5iwip2mepqkVMPL+wXF3GiO+Xh95u8DmI30FYsQdzoJYMlm69 YvjsEPIZJRiSgZAgPFh9r721fI10NJI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738171427; a=rsa-sha256; cv=none; b=ivP23Prx28xQNjnIG5E4P1ke6T3JjNcHtls9umy+jwZXk7ypcX7/WBOfsC6uUl8FVamqkA xT5lo5eiqFJ//15GKkhahk0RdrMWYQPkNGsB2Wn7jgtZbbOiLG7bqMBfFdaLq7FiW8ZwJx fjAaYCfLZvNq3sAb2sOdh4jldjypq3A= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=1jxitcoW; spf=pass (imf21.hostedemail.com: domain of 3ImSaZwUKCJcK12217FF7C5.3FDC9ELO-DDBM13B.FI7@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3ImSaZwUKCJcK12217FF7C5.3FDC9ELO-DDBM13B.FI7@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4361b090d23so36495285e9.0 for ; Wed, 29 Jan 2025 09:23:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738171426; x=1738776226; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=3zrvzGaN8zaP+D06MaP8yPSKdvx6oMhYyqVOrUtzvXY=; b=1jxitcoW9Kndkqu3NgWbrW7ufy92IFDvekLuxGMk7qNewLUgUw+hjqlEET/y+mSqQK rlCwMiep3hMmdwetVsrCIq1A/9SFnYq3j90mtvFzZG3duB3XIZCKZCKY5/ZbxG+qm+Mr ugneRmq6RC+yKopm2bDB/68YkwmrVIs9PBHFQLcf2w4GTQRCcnIzrRzV2ajL0eZyrOc6 CMdKupZEYuJ4z94YkPIfBgB6YMjnfOKbxdHFYVkFIrPYdirO5pjy5OGgK2ksKcarZtoQ z40c3wbWApA9aenUb4/DPVXmY00fcagi5y3s9TwReW8RnXTxamSPJEt6kc+IGeLdtZ3h xsTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738171426; x=1738776226; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=3zrvzGaN8zaP+D06MaP8yPSKdvx6oMhYyqVOrUtzvXY=; b=bdRaeyvysxOn8C4FrMrc9OP3T6qUZoiw2aXUrYF0w3k5P+sXSdhnF0811gGFg5vTh5 Ol2iMIeMAhcp4K+laAwqITpSXvjVT1NPfJcyPyGbz1kTYW54qiUA3fZc4SDZg3JZG8vB C89kAhM/wT3P86l2ITyHA3lAFNeoL4CA/PD0mYKg6gJ9BtW80szn2LunB1B0w+Yrgwih u7j5sUcX8bJUKz4q9cs6QS3plvWb1uwzn9MmVfw+ujnx6RtmbFEeerHw58fVR+zecW8t 8HzavDItOvteS1UbfRvAkwIuOpzRZVo+bZmVPLXNab70FXIJclegNQvyTPOCCAB+hOHW IFwg== X-Forwarded-Encrypted: i=1; AJvYcCXuaTavVh/HkIT17ll8XSk7R6M1PJ5blYRElKM3SNuHJd7RNhx+EoqtXp53ty767HChrcu92+t5ew==@kvack.org X-Gm-Message-State: AOJu0YyNomWuUyDmTfmQXIgmKAcDGFp30gWtcgj0Qh57hlJqMJy+g7w7 1nxazMpNhgC3HCHdvGbGkccu51tu4nnssJar/JW5ohIErIuklgQF7KBsjOLBm+3rK3n7wqUChQ= = X-Google-Smtp-Source: AGHT+IFR5xFOkTLqeb6QODDX2zzdjcB9oqFqKPN0nLKNTAI4B+FSdQD2EzgL2GfPKUeAFSCR2jBuoM5BRA== X-Received: from wmqe7.prod.google.com ([2002:a05:600c:4e47:b0:434:fbcf:594e]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4e45:b0:436:6160:5b81 with SMTP id 5b1f17b1804b1-438dc3c7c5cmr38793015e9.14.1738171426208; Wed, 29 Jan 2025 09:23:46 -0800 (PST) Date: Wed, 29 Jan 2025 17:23:20 +0000 In-Reply-To: <20250129172320.950523-1-tabba@google.com> Mime-Version: 1.0 References: <20250129172320.950523-1-tabba@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129172320.950523-12-tabba@google.com> Subject: [RFC PATCH v2 11/11] KVM: guest_memfd: selftests: guest_memfd mmap() test when mapping is allowed From: Fuad Tabba To: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org Cc: pbonzini@redhat.com, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, seanjc@google.com, viro@zeniv.linux.org.uk, brauner@kernel.org, willy@infradead.org, akpm@linux-foundation.org, xiaoyao.li@intel.com, yilun.xu@intel.com, chao.p.peng@linux.intel.com, jarkko@kernel.org, amoorthy@google.com, dmatlack@google.com, yu.c.zhang@linux.intel.com, isaku.yamahata@intel.com, mic@digikod.net, vbabka@suse.cz, vannapurve@google.com, ackerleytng@google.com, mail@maciej.szmigiero.name, david@redhat.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com, suzuki.poulose@arm.com, steven.price@arm.com, quic_eberman@quicinc.com, quic_mnalajal@quicinc.com, quic_tsoni@quicinc.com, quic_svaddagi@quicinc.com, quic_cvanscha@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, catalin.marinas@arm.com, james.morse@arm.com, yuzenghui@huawei.com, oliver.upton@linux.dev, maz@kernel.org, will@kernel.org, qperret@google.com, keirf@google.com, roypat@amazon.co.uk, shuah@kernel.org, hch@infradead.org, jgg@nvidia.com, rientjes@google.com, jhubbard@nvidia.com, fvdl@google.com, hughd@google.com, jthoughton@google.com, tabba@google.com X-Stat-Signature: 1j64nmjxf87gp5i7i31msztsurnyhqya X-Rspamd-Queue-Id: 75B8B1C0004 X-Rspam-User: X-Rspamd-Server: rspam06 X-HE-Tag: 1738171427-875717 X-HE-Meta: U2FsdGVkX1+PRjsT3pQDJkWAvES1E+t6KmGrg6/0/f8pwpuhD6skEPWa4KgHnNnYgHJarFPyy1T6oX+lAQTWQBCHMA1HY2Ec+cFnQVXF6iMsBzKVEMhWnut2yfPcqAjNl3MQbjXt8dSn8jC/WdB1OHGkwfQ3s5Z7Se2bbQB9oJPipMUzw9571obYWaYsJTYlde51ubtDlBfbkiIdsiD1r97Xva469XBZwLoqVkdsKflLOk86kAYBiAkWjJOIj4sHnyc9u6eiBff4EcEz9N8XBNh8LWU3Gis5u5VRq1vmH/gQ+Ue+sWqtW4SycTP+lu4hD7KtdB1PBTg3yo9+iobv5IjfSihVlMjgQ03F+CqkkEv7dFqugadgKADAip/qHeUvUaj3q5KoJzs+0MMl4Q0MrYH98Zz873JX/Gc8PXqeDRt4acQkDhFwBDs9xhUi1+FrvRic6gPTF1d85k49uehADRfhfiycFtvldterepZjf31VZRVNvCT8yV5JNu9Rnzpl5v6wbDdmoSO0t54VwLw0HU4cGyW4HpJ6PjP8IxVoPNCVcLKAG71pW+nK/tTIfsNi8tgzzdolnGuNTjD+0rH2fcK/5Zd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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: Expand the guest_memfd selftests to include testing mapping guest memory for VM types that support it. Also, build the guest_memfd selftest for aarch64. Signed-off-by: Fuad Tabba --- tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/guest_memfd_test.c | 75 +++++++++++++++++-- tools/testing/selftests/kvm/lib/kvm_util.c | 3 +- 3 files changed, 71 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 41593d2e7de9..c998eb3c3b77 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -174,6 +174,7 @@ TEST_GEN_PROGS_aarch64 += coalesced_io_test TEST_GEN_PROGS_aarch64 += demand_paging_test TEST_GEN_PROGS_aarch64 += dirty_log_test TEST_GEN_PROGS_aarch64 += dirty_log_perf_test +TEST_GEN_PROGS_aarch64 += guest_memfd_test TEST_GEN_PROGS_aarch64 += guest_print_test TEST_GEN_PROGS_aarch64 += get-reg-list TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c index ce687f8d248f..f1e89f72b89f 100644 --- a/tools/testing/selftests/kvm/guest_memfd_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -34,12 +34,48 @@ static void test_file_read_write(int fd) "pwrite on a guest_mem fd should fail"); } -static void test_mmap(int fd, size_t page_size) +static void test_mmap_allowed(int fd, size_t total_size) { + size_t page_size = getpagesize(); + const char val = 0xaa; + char *mem; + int ret; + int i; + + mem = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + TEST_ASSERT(mem != MAP_FAILED, "mmaping() guest memory should pass."); + + memset(mem, val, total_size); + for (i = 0; i < total_size; i++) + TEST_ASSERT_EQ(mem[i], val); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, 0, + page_size); + TEST_ASSERT(!ret, "fallocate the first page should succeed"); + + for (i = 0; i < page_size; i++) + TEST_ASSERT_EQ(mem[i], 0x00); + for (; i < total_size; i++) + TEST_ASSERT_EQ(mem[i], val); + + memset(mem, val, total_size); + for (i = 0; i < total_size; i++) + TEST_ASSERT_EQ(mem[i], val); + + ret = munmap(mem, total_size); + TEST_ASSERT(!ret, "munmap should succeed"); +} + +static void test_mmap_denied(int fd, size_t total_size) +{ + size_t page_size = getpagesize(); char *mem; mem = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); TEST_ASSERT_EQ(mem, MAP_FAILED); + + mem = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + TEST_ASSERT_EQ(mem, MAP_FAILED); } static void test_file_size(int fd, size_t page_size, size_t total_size) @@ -170,19 +206,30 @@ static void test_create_guest_memfd_multiple(struct kvm_vm *vm) close(fd1); } -int main(int argc, char *argv[]) +unsigned long get_shared_type(void) { - size_t page_size; +#ifdef __x86_64__ + return KVM_X86_SW_PROTECTED_VM; +#endif +#ifdef __aarch64__ + return KVM_VM_TYPE_ARM_SW_PROTECTED; +#endif + return 0; +} + +void test_vm_type(unsigned long type, bool is_shared) +{ + struct kvm_vm *vm; size_t total_size; + size_t page_size; int fd; - struct kvm_vm *vm; TEST_REQUIRE(kvm_has_cap(KVM_CAP_GUEST_MEMFD)); page_size = getpagesize(); total_size = page_size * 4; - vm = vm_create_barebones(); + vm = vm_create_barebones_type(type); test_create_guest_memfd_invalid(vm); test_create_guest_memfd_multiple(vm); @@ -190,10 +237,26 @@ int main(int argc, char *argv[]) fd = vm_create_guest_memfd(vm, total_size, 0); test_file_read_write(fd); - test_mmap(fd, page_size); + + if (is_shared) + test_mmap_allowed(fd, total_size); + else + test_mmap_denied(fd, total_size); + test_file_size(fd, page_size, total_size); test_fallocate(fd, page_size, total_size); test_invalid_punch_hole(fd, page_size, total_size); close(fd); + kvm_vm_release(vm); +} + +int main(int argc, char *argv[]) +{ + test_vm_type(VM_TYPE_DEFAULT, false); + + if (kvm_has_cap(KVM_CAP_GMEM_SHARED_MEM)) + test_vm_type(get_shared_type(), true); + + return 0; } diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 480e3a40d197..098ea04ec099 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -347,9 +347,8 @@ struct kvm_vm *____vm_create(struct vm_shape shape) } #ifdef __aarch64__ - TEST_ASSERT(!vm->type, "ARM doesn't support test-provided types"); if (vm->pa_bits != 40) - vm->type = KVM_VM_TYPE_ARM_IPA_SIZE(vm->pa_bits); + vm->type |= KVM_VM_TYPE_ARM_IPA_SIZE(vm->pa_bits); #endif vm_open(vm);