From patchwork Wed Jan 22 15:27:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13947436 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 68A21C02181 for ; Wed, 22 Jan 2025 15:28:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1B8186B009E; Wed, 22 Jan 2025 10:28:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 11AE3280001; Wed, 22 Jan 2025 10:28:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EAEA66B00A0; Wed, 22 Jan 2025 10:28:01 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id BDE4B6B009E for ; Wed, 22 Jan 2025 10:28:01 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 44E9BACCE5 for ; Wed, 22 Jan 2025 15:28:01 +0000 (UTC) X-FDA: 83035468362.18.6FAB217 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf27.hostedemail.com (Postfix) with ESMTP id 6C2C440008 for ; Wed, 22 Jan 2025 15:27:59 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=D7DaHWB0; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf27.hostedemail.com: domain of 3fg6RZwUKCCMS9AA9FNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3fg6RZwUKCCMS9AA9FNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--tabba.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737559679; a=rsa-sha256; cv=none; b=xqawdF7lYb7tBSng2DEGR1JCiHM3hcJO5D0ZYXe47z3egXXUCFIncT7bjZWVvhEhmcnPB0 Td27dZlz4mXd3nidzxFHkLNguIFBftkrCpi08uAtSaNOAmQPsllpuhrQ6msHLgWkCM5VVA /q9J7+0JcFHYvn925KfvSbaMecYzPFU= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=D7DaHWB0; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf27.hostedemail.com: domain of 3fg6RZwUKCCMS9AA9FNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3fg6RZwUKCCMS9AA9FNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--tabba.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737559679; 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=npQ4G6IRMYUbsOMeJWf56VvRGJDHzOqgY/NEKvIEjT0=; b=c8x1fyrWX6jb2toZxF9wxqgyEdeEpr92ZmTk3oSIQMnEQ9ppIUvjmaQ5LigIMkoRDG6QFz dc0BBJU5FCLCI9wM386RZBFXsjCKP0DTCN3VwAveJM8MfWxmlwxFtUiP99q417PFPfflzt Soi5PHMiH0nOUPfEpBYfarKvAHn5LJo= Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-43619b135bcso36416925e9.1 for ; Wed, 22 Jan 2025 07:27:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737559678; x=1738164478; 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=npQ4G6IRMYUbsOMeJWf56VvRGJDHzOqgY/NEKvIEjT0=; b=D7DaHWB04UuPaMoysk+224qznzWctNmVKzEbzrPwRi16wII/MAukQzhfeSRpJWkpPm 3QWpOnTv15y1WnzXiyPejkk56GS4uPUGCAdexdW33ik7QuCY9PrUNbP4sMDzLogeJJAJ HtywQ945Tb3XYsbdGqkTzRKMlxww4xlJTd9Ls0HJhKjRWxmH8NPn/vWskWAULJR/nVT0 IDz/TjtCwbeNAyXO3sf88soB+XN4UKr4jWW6eICBTm7KMOmjoLCB6eZ2BNNH1GZPcvn7 +8dCD0gd7qM9y0fiCOYe8M+3XDi1HCNFPKTgkKoyhY4qMNirZrPoRummW5aO79cI/JTG efjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737559678; x=1738164478; 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=npQ4G6IRMYUbsOMeJWf56VvRGJDHzOqgY/NEKvIEjT0=; b=Riv0i78RgGzgF3w0goI2ZNpkcuX9YGU/n7Qwv7X4RSJD5aTIIAEpP5os9OeWR5yOOB 52ra8CgQnDLZn62VRQiIAoaluwkdrK/GWfDg8Nq1RC32UK2bDNKqU8FBOU4PLnHrBHO0 PupPShLRvLid00+HTOXcH1tqShWYqX8uaR81/x5q0nBJABwau2feQP2V2Wfz6t4Zb0FE Vq3ONPIgWszsou9qFQym2MSpG2R+pmhgT3OlLcXgsEVUKfUq3Z9hdMhHEMYY6p4/ijXl F81n0gPJC8DElRklVxPSCvBs2LOrrv0A1xlWPDwFAhimNwY9GBMOhUtQ6XhxUaPtKflS yO2A== X-Forwarded-Encrypted: i=1; AJvYcCWJ5Ht4JAyC1bEjw0DuHj+zgT/XebFWBZ+unZdlzAlOUVnR1777HdlRgFpwMvj37+fvrJPBVtLZcg==@kvack.org X-Gm-Message-State: AOJu0Yw2vewFph3q9mSijbMmhK32vbHy8MDjxavy6S3a5UPdmVB24GTN wPUsqpJeE+YkLObZbOoCPxK1bH/jxa+CpI0Tl9khMSk7Hy3SpqRfmnOBp4ouVM+tnmhj1mQFpA= = X-Google-Smtp-Source: AGHT+IFMt51o/dm1bu2dWWRmK/754r3bTaAX9g4hoTpYJtAfuY1vj2C8A/Y3n5D7Vdmv0hIc0uGo5eq33Q== X-Received: from wmbjn3.prod.google.com ([2002:a05:600c:6b03:b0:434:a7ee:3c40]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4c24:b0:434:f3a1:b214 with SMTP id 5b1f17b1804b1-4389da1e6c7mr184498125e9.28.1737559678054; Wed, 22 Jan 2025 07:27:58 -0800 (PST) Date: Wed, 22 Jan 2025 15:27:37 +0000 In-Reply-To: <20250122152738.1173160-1-tabba@google.com> Mime-Version: 1.0 References: <20250122152738.1173160-1-tabba@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250122152738.1173160-9-tabba@google.com> Subject: [RFC PATCH v1 8/9] 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-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 6C2C440008 X-Stat-Signature: qax9855tjz3yiyuwy5i5zy5gc7yb4nba X-HE-Tag: 1737559679-652041 X-HE-Meta: U2FsdGVkX18MjsZU8aWKSdFPUS3l4jyXJ2Nge8YtzURqs1AshM2H2bamBTpb5wFTRpou+YgVquJrPzbD0k6kj7t/mxNGIUJdr7hIQdDqA+E/iuzZWZb5RM6fsoLV4RTnXgAUjCxK5CD4XZMGNmgmnlz66Ms6eJXs2VXpb+Yf1xRbfly7DUDcgjsMzgH7kasZrOXdYKpgKtafEld4wyvRIdccB3ADsasuWdcj8Bxl8cuQvOkzEhEbwALK9ykmn7q0gELZRspk0pQaOyJVC/FQUVP6lfIffnzoTlBGyEWFuPxqCqr/TlwFtDCHWWFF3eGYgvcnwAciHk7aKtHemK2c774dKMnMt1RJ9Wp/FFbd4kOhny7Y4P3UbzvEwCFWg/fkb0hqRHEkofZ1/QakOAP4UmBJZ0Ju8KJLhuUxB8mjjJa9q3nCzrP7XIO872tec1uQfmmr8eRIfUk92xdB+PcE+TvHciyCOZ7U4BIfSuhKQOStF6j/ctHdqmY17uqf42B6l+JB624w0q6c4CL0XdyKjtSKblZW9rwvanXgYhKLb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sbxeMJqy+huJI7fw== 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: Expand the guest_memfd selftests to include testing mapping guest memory if the capability is supported, and that still checks that memory is not mappable if the capability isn't supported. 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 | 60 +++++++++++++++++-- tools/testing/selftests/kvm/lib/kvm_util.c | 3 +- 3 files changed, 57 insertions(+), 7 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..6c501cb865f3 100644 --- a/tools/testing/selftests/kvm/guest_memfd_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -34,12 +34,55 @@ 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(); + 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, 0xaa, total_size); + for (i = 0; i < total_size; i++) + TEST_ASSERT_EQ(mem[i], 0xaa); + + 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], 0xaa); + + memset(mem, 0xaa, total_size); + for (i = 0; i < total_size; i++) + TEST_ASSERT_EQ(mem[i], 0xaa); + + 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_mmap(int fd, size_t total_size) +{ + if (kvm_has_cap(KVM_CAP_GUEST_MEMFD_MAPPABLE)) + test_mmap_allowed(fd, total_size); + else + test_mmap_denied(fd, total_size); } static void test_file_size(int fd, size_t page_size, size_t total_size) @@ -172,17 +215,24 @@ static void test_create_guest_memfd_multiple(struct kvm_vm *vm) int main(int argc, char *argv[]) { - size_t page_size; + unsigned long type = VM_TYPE_DEFAULT; + 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)); + if (kvm_has_cap(KVM_CAP_GUEST_MEMFD_MAPPABLE)) { +#ifdef __aarch64__ + type = KVM_VM_TYPE_ARM_SW_PROTECTED; +#endif + } + 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,7 +240,7 @@ 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); + test_mmap(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); 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);