From patchwork Thu Aug 1 09:01:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13749996 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 0D958C3DA64 for ; Thu, 1 Aug 2024 09:01:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4BAC66B008C; Thu, 1 Aug 2024 05:01:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3CC236B009A; Thu, 1 Aug 2024 05:01:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 294216B009B; Thu, 1 Aug 2024 05:01:36 -0400 (EDT) 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 076246B008C for ; Thu, 1 Aug 2024 05:01:36 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 7F4DF1A092C for ; Thu, 1 Aug 2024 09:01:35 +0000 (UTC) X-FDA: 82403083350.29.B8C8134 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf27.hostedemail.com (Postfix) with ESMTP id B2B0A40027 for ; Thu, 1 Aug 2024 09:01:33 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=XyyX0Xve; spf=pass (imf27.hostedemail.com: domain of 37E6rZgUKCHMkRSSRXffXcV.TfdcZelo-ddbmRTb.fiX@flex--tabba.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=37E6rZgUKCHMkRSSRXffXcV.TfdcZelo-ddbmRTb.fiX@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=1722502836; 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=FenZNVmro9JWmRf8yJhfaxf1ruU6PeQcDA6Tq5ucplA=; b=YncM5uu8CJz3nxo7aqEDChD9iV54ricAVtCFwWZD/bNu2FqDRE7Bubaf8omx+5VgvhQKbZ s38c1qNDqUBSd0IT1F4yogoTBm6bdXZASGyMNod7rnm2VcwtjmhwqH0DLD5wvpWI6HY6iZ 6pE1VwFCBfhemuLd4jpF9PRaJqrCLYU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722502836; a=rsa-sha256; cv=none; b=Z/YC9pA+8y8z/6IGVU+uBHGxzHLF0ltesxbhgrMpPbhUm7dniZSBhKJhenmnsTAnwSE4bo DhgJTAEhv7+XqzyB3kTzPIiMVbNSyiaQpIFVrZHAW17q6avqkoMSKA2b07uAS0wUKla+f6 LruBpas5MPfabqwtBkS2AztYsOyxzlg= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=XyyX0Xve; spf=pass (imf27.hostedemail.com: domain of 37E6rZgUKCHMkRSSRXffXcV.TfdcZelo-ddbmRTb.fiX@flex--tabba.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=37E6rZgUKCHMkRSSRXffXcV.TfdcZelo-ddbmRTb.fiX@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-665a6dd38c8so130277657b3.1 for ; Thu, 01 Aug 2024 02:01:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722502893; x=1723107693; 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=FenZNVmro9JWmRf8yJhfaxf1ruU6PeQcDA6Tq5ucplA=; b=XyyX0XveyXQg7256dR5hY1bKkvN7QtNME6bpJNV5q8+QWb7FE7/g6eikSxPKmDKShS T8fwjlXmzqz21vDJt2v0NVXaTdWidtm6QdPO4kYRPeoGrlclS+kl2lRosm/dTmgvKu8+ LfUpF2nS2t9FXYrkSsZMRgzFh+lqCFH1N+dQ1RNrDSvPE64BS8CJbP3cpol4ByUE4Nbe 7Lt3TqYoS52hOqk28sKOPhchZSbab3a22PL6umwtRESe/4BlyXnS/Ww3dekREqjeVPBX 53mziA4XDzXzzpYTugu+qMB4wLoYj4krJp4T/8TSuVzVL8eK7WG618SKtINVC5Kz4p+c JSsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722502893; x=1723107693; 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=FenZNVmro9JWmRf8yJhfaxf1ruU6PeQcDA6Tq5ucplA=; b=OoISZTiIUVTtJeMlB81P7Cgo/8dgTayXCsm09R17U3/Hc0i2Yl8ngbVEeQiRMIc+Rl 7CVqMnds4VFtfulrcrXY6t+zalZ33o3riwxs28E/V/Hl5xq4z2r4KldXQqAxZ9lpD4IZ 5pK8ZCJVnbeS2daFoq2V+a3ELd6QwGvQhrvrWyyEzG4gucds9Aa3xNNnyUpypNdMxycA o6QSt/84NHn2eOGieG8nnM9GTH5try15svyUZyiOP2CulVbIx3g5ayC8fuXOTE7QB/Gh KiBktIyg4gs5iewrFsmVOZ9qnuqka6YUTPio+J7lxIXiWFY2GenBRMQYHPibV9IvnIMK M8Yw== X-Forwarded-Encrypted: i=1; AJvYcCWTTIfSLrK+p9A8752nxlBFKMUm0njOLrjgrWBfKQluoos60tJ3MyD40bL8g4xt5e/Ymorc+DjeN83QeSbI2Z4dods= X-Gm-Message-State: AOJu0Yxnsebsk5BLQzbJIimfKu2UBICrU+KTYgGZek+TqdfSQV7Yo1W6 EE9e/peTe3yyj4sY+PTwzP9vtGH/IDd5fKo+DpkYh+RB+0Ete1WbWaXNR1bDuv11jtEiTqawlA= = X-Google-Smtp-Source: AGHT+IGm9QckLnDbVxc2uajSqqnHpYUpgzy1Qib76hzzsjZLg4j1Inen+nQBahz4WB8vSdsSSS9dwIaeEA== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:690c:dcf:b0:665:a4a4:57c1 with SMTP id 00721157ae682-6874a9ec608mr269487b3.2.1722502892571; Thu, 01 Aug 2024 02:01:32 -0700 (PDT) Date: Thu, 1 Aug 2024 10:01:12 +0100 In-Reply-To: <20240801090117.3841080-1-tabba@google.com> Mime-Version: 1.0 References: <20240801090117.3841080-1-tabba@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240801090117.3841080-6-tabba@google.com> Subject: [RFC PATCH v2 05/10] KVM: 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, tabba@google.com X-Stat-Signature: 3zw9bwrhs4a385gkbp1p57ubw8idbpof X-Rspamd-Queue-Id: B2B0A40027 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1722502893-822629 X-HE-Meta: U2FsdGVkX18WbvRK9ZDsTyeUhkRyLNyRkVaIugTRGx4NC+nPuPeeaHDRs62U4GjKoMNU/R2ak6gPEb7vCi5Y6qFFm0fHA4h40w4AnKckU+n8B/RRsN6L0WlBkXyUZZ3J7vg/vrSgzSjDre4YUtizZiZ/DZ0jZzffJ6rd8ghoA9sb17Nk301uHofoVQ9LW0fFR0W8x+HGHxc3O848sPeQ5aNx2NTvViXL1BTnVhI9snWup1HVvLgLFrHaj1QEqAmOhRYKGy6dkAufA1FBBAHeE98ynybRxJA/oYA+E+NlKJiIWK01EprPICfwLj2oTJza5XaCClNFmymDm119+WH0Hucb0/lpWjlTcglhOg6WDxSNt3eoJEmrhSQTSvhe/5Sw4eOKR/SbEFxXuHn+OlkmBseN/TNReKZ/Pe4EixwoF+nvHnXveLyNcFJCi2c7af7vt/P82ah+I4ltK7HqbG9nJQFVZrw7nfe67ZZCeriXAnNCChRnN6CS6i8QbGccZ02k5HRXaHm+ACehoH/dgnSVmA98M/4hWp3L0rlkva3/MFo4Aa35io74xQSAlH2+xw7rnthge7JWl5nYE4yzer9cdxHLUCQpIxOBf++gEjYjNetnc7PGLoE0EiAEijj3tmYgDWRMRM0G/wVaXWOjUjrviAiiwAR41y1hXlkw9yyQA42/FWExP2AiRRKpI6Z3Huj2XoXCfOukxcQhyIuosvemKzYdTcF8v/VBQRhWag2hromEvUzAq3W/+NygLmtNWOugJ3RHlYeayFLyuhzH7nd3CF1TUHkGxW/HnMoR+4D8aZP3JkUNq4uEG4PuE2W/WvVZYxiRr1ijvaZz7dwoiqcIeQrDb3uq3PZCxKHaNOx6ETvaVxYn5pmZCtFM4fpYvAXxXgxpPcFbmwISkziS3DRSf1xdbHFfXZEqTq0CqJIEcX9BBZrGczcTp7BjQ53KHo0cdvySvlvpcEvqqB38x+4 MKuIwiFk SDV67GmGyRalHtcOqaoPePIwbQI5NzhHMLHUL0nziDyBYlBQ6u1fxAfycD2agQZnXv979LoexIaM6xm5Jczkb5r0NUZJxWX2PKWTKIbMTZYtzbghmp+K+IzIGgZWISe3SpUnMFC9f5rfLFgYZdJTjiI9jn238ftWk86ruQgT8SgD+5QXOvLIco2UsY2uiE3REyDhyTYd6AphnltLfIUJKCFYj99+PNr+9K9wCCAcySnNqyiDBc/NMPTIonnSK8MSKPf6CUUc+7ABBp396xAmmP5gjeH36f8TJwnIVt+uKy/rwvgdVlT3vCFZc01+PGzbvqiPRaGk4UT14HFlRM7VvkPTCETvd03gOqTzmd3Nk/IFZsrvlxiN7Yjjj2dAZnOFnoRztLw3+4HWHPMYDv3/TIVb+PXWUiuaILYO4Ds89euRi0DRH7QG2woSnacGfURZLruwb10QbKotE/js52tUj9PTBzJFA+OJ/1N3lfU9MZgSD/d9P4BCQwFyXidTkzaVUGJPP+pgQbJmV+ylcEBBJ8xIHH0wMoo9lgez2pTys2/f5Whq5BWBvef//nsWXD3PXFk5/Z9XpVLu6nN/3bBpEs+x6mDpkO7sQ+HQXbdzPZien/TRhjLZW8nPgqg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000010, 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 | 47 ++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index ac280dcba996..fb63f7e956d4 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -166,6 +166,7 @@ TEST_GEN_PROGS_aarch64 += arch_timer 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 ba0c8e996035..c6bb2be5b6e2 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) @@ -190,7 +233,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);