From patchwork Fri Jan 17 16:29:57 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13943558 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 6AD23C02183 for ; Fri, 17 Jan 2025 16:30:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8DF926B00A2; Fri, 17 Jan 2025 11:30:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8B81C6B00A3; Fri, 17 Jan 2025 11:30:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7319E6B00A4; Fri, 17 Jan 2025 11:30:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 502616B00A2 for ; Fri, 17 Jan 2025 11:30:31 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id A555445592 for ; Fri, 17 Jan 2025 16:30:30 +0000 (UTC) X-FDA: 83017481820.02.D7CD65A Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) by imf25.hostedemail.com (Postfix) with ESMTP id AEAFAA0018 for ; Fri, 17 Jan 2025 16:30:28 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=mu8t9VFV; spf=pass (imf25.hostedemail.com: domain of 3o4WKZwUKCBwL23328GG8D6.4GEDAFMP-EECN24C.GJ8@flex--tabba.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=3o4WKZwUKCBwL23328GG8D6.4GEDAFMP-EECN24C.GJ8@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=1737131428; 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=L3JE5H2O+HhGpe+U3oFgZgOkWGJvxazt/6lMsPLsK3g=; b=Sr0Pe5ZjgyvdRW2q+tAk84S8T41I6DeqD0nRky5W8PE19F5tI4q6F4T3d3b8DMU3Vap+Qo 8/ybyKfeZqZRKc4TPDTusZgXli7uFUs5rOn10ZssvuGRJD5GNhYWZW+GY/cVokgS+1C9+I 181ObxiLOQzZZwutjwf7R3+/mjqaLuw= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=mu8t9VFV; spf=pass (imf25.hostedemail.com: domain of 3o4WKZwUKCBwL23328GG8D6.4GEDAFMP-EECN24C.GJ8@flex--tabba.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=3o4WKZwUKCBwL23328GG8D6.4GEDAFMP-EECN24C.GJ8@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737131428; a=rsa-sha256; cv=none; b=tIUNIfr5a2VObNbbSwGxvjJmXkZChMbSo52Q1xe2Pyl2y31Ndi21aoh5MEIRMDym0PUr1k FPTgdgwe+C6l9k+wsnSXBsJY+nsBmFc7xuMM1OZYNbZ9sX2spYDSF2ne9+mSv5cyQIaomi 0K2NLnwvru+FO2pYcsM+QWUdNisjQEg= Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-38a2140a400so1867191f8f.0 for ; Fri, 17 Jan 2025 08:30:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737131427; x=1737736227; 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=L3JE5H2O+HhGpe+U3oFgZgOkWGJvxazt/6lMsPLsK3g=; b=mu8t9VFVQ/B5Q8RvmirSp5Xg6gF58f5EokeX81t+DyEpn23SH18vU72MPJXaITdNmt fQIBxEW8qvsNXjxK6vdAHWc/dw0ZrB0u8cjbtAoPB6t+G+btvmYaQMuyELUx1PRjq7vh D3Up61Sk441xnWy5TmOHAIK3rlHrOMVEMJyHwkxEMHyNs3SVGQGc0VUFPN/0sQyV1OVe MW24EFqXO/gSVQS3z6OxSGjleAZi4rWJcSL5XA7dU4ztZYGeUxO/WQiQjJ5e00nHTo/+ ZSD2zqMuFWdKqe04KfKAtEypWRuKP3CdI6Rfj6rEX6NqRp6gpDWXcT+gCZe1LLhnqgJG Rkhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737131427; x=1737736227; 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=L3JE5H2O+HhGpe+U3oFgZgOkWGJvxazt/6lMsPLsK3g=; b=EiT0io8CkhndJjKGVrkdZpAjol7wsp3IS+nh6KLfjtwcjAmE8wkn4jpgK9vzPKx3+f h/ED+Ii8bfSRiPdmOr92IXNtrbNymVLu4tW8lt2QHu+diwafqVbKP2xuGZaMM4IfK1pj dXzVQPpg/G+0fMg7M4WZEooozwyZt8orYG6ov89keoJeHWX4ucxjA0G1GrYLFLfJEmp/ CA8HvGN4/P0r3JOOReOfQQs4SnscqiCt0/huQ7TLnJ1Mpt7IPAVk3xmUiJuYZ0mzwIJe wxoS5MgenKphLVuQ8E8xhBeeqQ24IW3ltpX9+EcD44lgFcuOjO40raCS2lAYKVNSu4P9 r1gQ== X-Forwarded-Encrypted: i=1; AJvYcCX/FzDNnqXeOQo2JbAv7KcA34f0wexihIOymZvVJNYlZ/ugvRQUrgTnrP6gLXAqqYYu2Ay54twC1w==@kvack.org X-Gm-Message-State: AOJu0YwyC2CCGNvHxyaCFuxGgpVGevhmsOkkenkmo9/NrglE8vZXbNT9 4LwD50s7joBhe82IMLr1dwRZnux7fKNiRXP1cqNzAqwavR/H4qXs4arVOEwn3uXMFUeNkuSKjQ= = X-Google-Smtp-Source: AGHT+IGRpQ4nyI6BTUrkjbZ8ozpuqJEpQ2vY5BcNInNhu8r9xyJzkDeNESuZ6QV5cyzLhvncD1YckbOmdA== X-Received: from wmsn40.prod.google.com ([2002:a05:600c:3ba8:b0:434:a1af:5d39]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:402a:b0:38a:a047:6c0b with SMTP id ffacd0b85a97d-38bf57a97e0mr4283682f8f.35.1737131427228; Fri, 17 Jan 2025 08:30:27 -0800 (PST) Date: Fri, 17 Jan 2025 16:29:57 +0000 In-Reply-To: <20250117163001.2326672-1-tabba@google.com> Mime-Version: 1.0 References: <20250117163001.2326672-1-tabba@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250117163001.2326672-12-tabba@google.com> Subject: [RFC PATCH v5 11/15] 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-Server: rspam07 X-Rspamd-Queue-Id: AEAFAA0018 X-Stat-Signature: 75reicpzpz9hzotze9zmos3ewqy3tcq1 X-Rspam-User: X-HE-Tag: 1737131428-763644 X-HE-Meta: U2FsdGVkX1/aYJ5WYF8DT1qD3yEE/DRGzBuKcCnhjmJEvjg/h8h/Q9k+PZ89kLQ0ZUAX+V/9vC3mNDUwvKh8ODg0yI9uZ7B0xNTFoOk29lePqTiC5ceG+WgVUzWqRfbRX0Deq9L0DRwCelqTAk23QIzh6L6dDtMPKKJ7EsSqMl+MB/J6irLE1hesGvNlUlPbH32sbRioWY9PSN3K9aoeh7zRJxJm6syA/pmkRg/7yrzMA7b7Gl0aj3mdI/usAkOIpFtrdtkdFmRjojO6QJM2ZflOwxjgcMDz0n9ie+OnXHh/mkDUi31qvttY7xZ+DYkdHqJBFyGIuI158RciMTYhhnwtBlDlR85dT+NxsYrbrpmEzzDS0CpOVXokv9omGs6Dz/dHuHeYU2ITgbh/HvQ+BzJyCw0qhplkY48WhYGGgjbUjgOQxdW/LnzzgfgFX9LPKBW74B9p+e6k6NJD/LUzUR8pIRy01fB73AeMrv51uyT7hq2/ohFxJFzOtFERndgr4+6frbIQNZIHB6qCfxKxB3DkvNAAgCuiU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JFAO7aFvGR3S9KVnwc+4HNzM= 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 | 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);