From patchwork Fri Dec 13 16:48:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13907469 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 4B42AE7717F for ; Fri, 13 Dec 2024 16:48:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 995606B00AB; Fri, 13 Dec 2024 11:48:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8F57C6B00AD; Fri, 13 Dec 2024 11:48:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 770916B00AE; Fri, 13 Dec 2024 11:48:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 5202B6B00AB for ; Fri, 13 Dec 2024 11:48:40 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 0D357A107E for ; Fri, 13 Dec 2024 16:48:40 +0000 (UTC) X-FDA: 82890519012.18.C7BDFAF Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf10.hostedemail.com (Postfix) with ESMTP id EC278C0019 for ; Fri, 13 Dec 2024 16:48:26 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=G6Kt36GF; spf=pass (imf10.hostedemail.com: domain of 3ZGVcZwUKCOEWDEEDJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3ZGVcZwUKCOEWDEEDJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734108494; a=rsa-sha256; cv=none; b=OEjupIT6S2YUgpqrm4MokKX2+d5Ms5OjMXAN1gRSrKX6rnx1SfLRm4UUcZx+sDrmmgYvuS xGanaJwgE9mjg5W0AtZR/WDDQ60oeTkSiJ14HR4BOJaXbT8dZeqcwPJUL0ASXrTyCebpKn e2A9CJlzl/EBvYwlytEm4e2locO3f0A= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=G6Kt36GF; spf=pass (imf10.hostedemail.com: domain of 3ZGVcZwUKCOEWDEEDJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3ZGVcZwUKCOEWDEEDJRRJOH.FRPOLQXa-PPNYDFN.RUJ@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=1734108494; 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=DhFiA/KCvNxVvN2dxXqgdhmMKMTNkZQ9H4IjGYFoLJU=; b=Qq+0ISzVXpG9aajAjJ7YYKUTId2CX3LF+QPeCAewHWLOL1YxQwxrt/JeNWk0su31mE2vUY 3GLxP6Ve61xEos2mo9ZIjszCMlJ2glupy4d7SlJ6iYUY39hmvBKrMxXpOWW18V7vlcAbGw gMhUXHmsXaCZm+7k3MWrtFEGajzP4Bc= Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-4362153dcd6so11693135e9.2 for ; Fri, 13 Dec 2024 08:48:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734108517; x=1734713317; 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=DhFiA/KCvNxVvN2dxXqgdhmMKMTNkZQ9H4IjGYFoLJU=; b=G6Kt36GFBUDQ9/H8ejypP1i1jNjVqo5DyLLMRv9VExIGn0zMatYRISS8uaQIncH4T2 bccVoy2qTitUKxRnE7WW9KB0//hFGWYfMXihYaryeLnkJq2OL64IN0tuGMS218dUXNVL bM/cZiNefLce3mlGWc5ExgvRNZLtzvZpRIw0t4KrSK34UnpqLiYI0lt1FlByOtwJ8FCn p6ntCfjQKxLkcDUH8zrlwQbz0r5PMCl9+LTUKJ6JGv9Oggymae26jCdaK6m+L4go37Wa z+aVxytKeastKAzuYDlDrXBepgJbLBL4U3Iqn0ZVn9un01u+nmhpUkonZd56PXXsQv0v oLyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734108517; x=1734713317; 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=DhFiA/KCvNxVvN2dxXqgdhmMKMTNkZQ9H4IjGYFoLJU=; b=YI1ecNc1GX9NuSAbEbUI8bia8BGNZecpb05AkBe9ry1LXu4hcBO3LwKYWfP8ZuR7jk gxDFFiNnJZbQ8nPpNpuhIGa6JtPgqs53wiusmJb29SrlrLVjyeIRIe2H8bkvVBAyoQ3u dnq67um4SI3ESBkulsTEOtUJc3PcV8HlLqOok3npw9EWIFqClrO9ItSSv2J/cI1fzYv7 +kBx7WDikFVQvXvQPFUDaWcmEqte9yy7vcfXpfgSzoV9IN+zR+msXItlwoHYKqS8qLkT qaweehw/nAFdB2i3BwJO/w2SgCoNaJCSjtLXdcILBsT8Y1vqG4XdSb3318wC5vA+AlEp SEEQ== X-Forwarded-Encrypted: i=1; AJvYcCXkAvUwNpwuhbKqBfoCc7sAmrlUgbujDfXYYQLJLQEyhCbW1uk0XVePLdA4syvhCYmTbnc4UIGtMg==@kvack.org X-Gm-Message-State: AOJu0YzroZi8FxX03AtkiRJAa7c8mLjeaRV0b2Wy1EqoSlexmHdTzXGM Nbwtf4m0Beh7ANvxdpSyNY+KXSKzvv7pm4Z2MC0qgeazWPRXNf6pxttGADHy0hHSI6o3987v1A= = X-Google-Smtp-Source: AGHT+IFC3wQu843hFQBCJ/CWlcnzA5CxWqK8aB90iNtnojUxP19hTT+b2JGWT0UhJ1/fNK7qaPLpg8+Hqw== X-Received: from wmmu14.prod.google.com ([2002:a05:600c:ce:b0:434:f0a3:7876]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a7b:cb89:0:b0:436:1af4:5e07 with SMTP id 5b1f17b1804b1-4362aa1561emr25562455e9.1.1734108516946; Fri, 13 Dec 2024 08:48:36 -0800 (PST) Date: Fri, 13 Dec 2024 16:48:07 +0000 In-Reply-To: <20241213164811.2006197-1-tabba@google.com> Mime-Version: 1.0 References: <20241213164811.2006197-1-tabba@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241213164811.2006197-12-tabba@google.com> Subject: [RFC PATCH v4 11/14] 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-Rspamd-Queue-Id: EC278C0019 X-Stat-Signature: 6rpwns17tnb8f8fwtyy7dsyfc6wzetie X-Rspam-User: X-Rspamd-Server: rspam09 X-HE-Tag: 1734108506-812348 X-HE-Meta: U2FsdGVkX1/+T/QVOxHA9xzhHXj1t8TpTGiaTNvy7X6CS2uAO1E9+fd+ioVwhrZCyMgK1reH6a6KzH+RlGbsN4bRrn2VXhd+bNwI9ue7ca2iajxLRyIrH4pnCxa2sQ4odhis14ZA+AlNuFlPdoZ0EJm/6Rnbsflbq/4SnqxJFeJfTrrbPpQd9RrfjAymV+eGaK5jDl1oNsfAKuLm0vh/hNPSX5/wqt0vbNlz5OF7P/I50LZtTBuLxubMSqrfWfxKRVxPMTWb6OGI5FKgJaoFbKE0fGB+xw2ThBBIliZp8LxJSI3S+N9jr57bZ4muu3Ja6wgGoa++VnXdphmRxNk1waw8E805c3qjNCI3rEIHYWldhB1ui++P2qeQXKTRYhcCvMnFG2j4Ojf4FWRpGYKk0OtYiFVkgVWmQbSIz0cLV5KAvweFEXYWcmTknlEQCiGyJ7gfstRUCYt1o6/QYuAIJ/WhKJkwHI3EiTg8rKya6LRgVU66NpUmQpapEtyBicY8ut2GerzgRbqBrF7WM9fYu41L1M4a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2iARsrcfKCIgxmug23xX4dvb90VMWeIhj2Ef6b+Ww== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000752, 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 | 57 +++++++++++++++++-- 2 files changed, 53 insertions(+), 5 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 04b4111b7190..12b5777c2eb5 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) @@ -175,13 +218,17 @@ static void test_create_guest_memfd_multiple(struct kvm_vm *vm) int main(int argc, char *argv[]) { - size_t page_size; + uint64_t flags = 0; + 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)) + flags |= GUEST_MEMFD_FLAG_INIT_MAPPABLE; + page_size = getpagesize(); total_size = page_size * 4; @@ -190,10 +237,10 @@ int main(int argc, char *argv[]) test_create_guest_memfd_invalid(vm); test_create_guest_memfd_multiple(vm); - fd = vm_create_guest_memfd(vm, total_size, 0); + fd = vm_create_guest_memfd(vm, total_size, flags); 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);