From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CDC86748F for ; Wed, 4 Jun 2025 10:05:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749031543; cv=none; b=L+P7QxJO7yJs/VFiFpe5W8xqPbReUPnu2kTF3bw/8haamkheC8kGRRKB/K7dmbLRsUYsqoM0UCdBVm3/e5MVbpP+2T4hE7VYvZ5oBUytl8q9tOEwpq/0u/GA5qguLNFFsedUbx5wKBntE4xowhDf1E5tCl2+O7IPZ36lf3fUu5c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749031543; c=relaxed/simple; bh=u+9kEUjH0KvHKezoG6uGQ2DEjCoQn+WqoPn2BqkiHxs=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=grf4O7iMpzA4aIxZFYYLSw6XOg7SSkL3o8okciaHSaLqdyamHU8k1efQXAbuqJnifv+XiJIQ8fE++hFbmXEAKpv1jGyO3BcvAs1muxZFdb0BgZ7zpH6rdSW+Vg1qlOwRSnRhtXMgIkBxzC8gEgbzEg/8RXQhtS3Big8dj8pQLgo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ilNFpA6w; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ilNFpA6w" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1749031539; h=from:from: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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=VMLhcnC9+JU6hatTCJVRZoN8Wtj4l/l+ETn1f3lqm+I=; b=ilNFpA6wc3AkxzbMA+zU3ctHzULiIHOzS4SEN0c2CcbiINKenY8ucGCSugaTWNxyuP3113 8J7b0WWzlUvb8kH8D4zfAEwsINXROec5opq5PfJl7Ea60pJxSH/NdJrzwsDd8Us2Lzqwdx 81+0AxE8B33eotId6hwCy3ZX0VBxXSc= Received: from mail-pf1-f200.google.com (mail-pf1-f200.google.com [209.85.210.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-679-yVHdtKWxNGO2GEDpIypmSA-1; Wed, 04 Jun 2025 06:05:38 -0400 X-MC-Unique: yVHdtKWxNGO2GEDpIypmSA-1 X-Mimecast-MFC-AGG-ID: yVHdtKWxNGO2GEDpIypmSA_1749031538 Received: by mail-pf1-f200.google.com with SMTP id d2e1a72fcca58-7395d07a3dcso5017593b3a.3 for ; Wed, 04 Jun 2025 03:05:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749031538; x=1749636338; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=VMLhcnC9+JU6hatTCJVRZoN8Wtj4l/l+ETn1f3lqm+I=; b=BzTxOQ3HSMsSYBZjfjONae/ysYJcjfLD2sXEC9+huadHUE98EKgMC7tEHFyqBtwDcE 2jsNvdRqC6jqSOUEHB/JqJSba6bqE7bovUmZkONSLQA13xaXjtJSk9mpH0P8taJS7IAy cUvShC8MAilMREgoU+KdUc/TNGIXXPyA43Tk/2Zpb5aD2V3duuMyzRJ3jnJZnuH8WaH9 c6WT636V5p2vFxknKywgJM98++ZtxsFYSdDXX8BkRdswjZJHqZtExIS3batjV8d0siUL Sx32Sz22WungBi0ierpMy/PHztipwJpuhCS0WJL8j+tjagdn2LuHeMZurLIOv3DF4T0p 7zyQ== X-Forwarded-Encrypted: i=1; AJvYcCXrxqMIJcVXGK8SSmExGPoIccezJ4tkQlXyxNRhK/sbVyvazj2C9ULq8FfMWkrLAw5o2I3Ol9w8nSJdEH8y@vger.kernel.org X-Gm-Message-State: AOJu0YzMb8+76LF2XnF4YZJVGjjOFo13isfnRfYXa3ZL2kjZ4Hig6sQy eHB4wlsDovLDUxjjI73VfhSDS9+HSaDujrHnwPqDhK8dFhwEhYoZG7/ZumIcx1nubHlFA8dR1nL BY7CFEVZlzYP7xuyJp1iqUTRFknmUBSUYIO41ltWiFAGj+nRbbbL3A8+o1duxddkS7sU= X-Gm-Gg: ASbGnctp4QEn2LaWUOycv1AlVCY/0juxyR58E2Xh/17rJTd7MiZvG+9jF4OgemVG8dw cCyQBOiSHNexZQr5dqnFW7hnL70RRBAj9fRlutEG4bLvHlqGqhwxhmptmYeYudJsIAWIExthAek uqSP5hGANxAYyVkjWfIdC41KTkOEqLUaTr3Q5Js/7Bw+Ore8Pl4Dk68sl379i0Yi1OIwamSilVm VCq8R/iyD3U+9F+xj5uhA2O+2QZECXZZ9GhlkFwzi6i++uppGp+lSxp3iWY8eMGkrM/Yn6upw4N 8I+fvyvBPjx6dplrhwcTzv6s4gdOuJBFFuolzLpJiUeraPEdcpA= X-Received: by 2002:a05:6a00:99c:b0:73e:23be:11fc with SMTP id d2e1a72fcca58-7480b48a226mr3509443b3a.22.1749031537495; Wed, 04 Jun 2025 03:05:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHZgFIeDTmpajCrDTaiqLjqZLgt6AKssR/SSnosipe7f4k0EhNhKGhtxyA9HSXTQicG/JvBEg== X-Received: by 2002:a05:6a00:99c:b0:73e:23be:11fc with SMTP id d2e1a72fcca58-7480b48a226mr3509343b3a.22.1749031536953; Wed, 04 Jun 2025 03:05:36 -0700 (PDT) Received: from [192.168.68.51] (n175-34-62-5.mrk21.qld.optusnet.com.au. [175.34.62.5]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-747affd55adsm10761776b3a.156.2025.06.04.03.05.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 04 Jun 2025 03:05:36 -0700 (PDT) Message-ID: Date: Wed, 4 Jun 2025 20:05:15 +1000 Precedence: bulk X-Mailing-List: linux-arm-msm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v10 16/16] KVM: selftests: guest_memfd mmap() test when mapping is allowed To: Fuad Tabba Cc: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org, 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, 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, peterx@redhat.com, pankaj.gupta@amd.com, ira.weiny@intel.com References: <20250527180245.1413463-1-tabba@google.com> <20250527180245.1413463-17-tabba@google.com> <025ae4ea-822b-44a1-8f78-38afc0e4b07e@redhat.com> Content-Language: en-US From: Gavin Shan In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Hi Fuad, On 6/4/25 7:48 PM, Fuad Tabba wrote: > On Wed, 4 Jun 2025 at 10:20, Gavin Shan wrote: >> >> On 5/28/25 4:02 AM, Fuad Tabba wrote: >>> Expand the guest_memfd selftests to include testing mapping guest >>> memory for VM types that support it. >>> >>> Also, build the guest_memfd selftest for arm64. >>> >>> Co-developed-by: Ackerley Tng >>> Signed-off-by: Ackerley Tng >>> Signed-off-by: Fuad Tabba >>> --- >>> tools/testing/selftests/kvm/Makefile.kvm | 1 + >>> .../testing/selftests/kvm/guest_memfd_test.c | 162 +++++++++++++++--- >>> 2 files changed, 142 insertions(+), 21 deletions(-) >>> >> >> The test case fails on 64KB host, and the file size in test_create_guest_memfd_multiple() >> would be page_size and (2 * page_size). The fixed size 4096 and 8192 aren't aligned to 64KB. > > Yes, however, this patch didn't introduce or modify this test. I think > it's better to fix it in a separate patch independent of this series. > Yeah, it can be separate patch or a preparatory patch before PATCH[16/16] of this series because x86 hasn't 64KB page size. The currently fixed sizes (4096 and 8192) are aligned to page size on x86. and 'guest-memfd-test' is enabled on arm64 by this series. >> # ./guest_memfd_test >> Random seed: 0x6b8b4567 >> ==== Test Assertion Failure ==== >> guest_memfd_test.c:178: fd1 != -1 >> pid=7565 tid=7565 errno=22 - Invalid argument >> 1 0x000000000040252f: test_create_guest_memfd_multiple at guest_memfd_test.c:178 >> 2 (inlined by) test_with_type at guest_memfd_test.c:231 >> 3 0x00000000004020c7: main at guest_memfd_test.c:306 >> 4 0x0000ffff8cec733f: ?? ??:0 >> 5 0x0000ffff8cec7417: ?? ??:0 >> 6 0x00000000004021ef: _start at ??:? >> memfd creation should succeed >> >>> diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm >>> index f62b0a5aba35..ccf95ed037c3 100644 >>> --- a/tools/testing/selftests/kvm/Makefile.kvm >>> +++ b/tools/testing/selftests/kvm/Makefile.kvm >>> @@ -163,6 +163,7 @@ TEST_GEN_PROGS_arm64 += access_tracking_perf_test >>> TEST_GEN_PROGS_arm64 += arch_timer >>> TEST_GEN_PROGS_arm64 += coalesced_io_test >>> TEST_GEN_PROGS_arm64 += dirty_log_perf_test >>> +TEST_GEN_PROGS_arm64 += guest_memfd_test >>> TEST_GEN_PROGS_arm64 += get-reg-list >>> TEST_GEN_PROGS_arm64 += memslot_modification_stress_test >>> TEST_GEN_PROGS_arm64 += memslot_perf_test >>> diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c >>> index ce687f8d248f..3d6765bc1f28 100644 >>> --- a/tools/testing/selftests/kvm/guest_memfd_test.c >>> +++ b/tools/testing/selftests/kvm/guest_memfd_test.c >>> @@ -34,12 +34,46 @@ 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 page_size, size_t total_size) >>> +{ >>> + const char val = 0xaa; >>> + char *mem; >>> + size_t i; >>> + int ret; >>> + >>> + mem = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); >>> + TEST_ASSERT(mem != MAP_FAILED, "mmaping() guest memory should pass."); >>> + >> >> If you agree, I think it would be nice to ensure guest-memfd doesn't support >> copy-on-write, more details are provided below. > > Good idea. I think we can do this without adding much more code. I'll > add a check in test_mmap_allowed(), since the idea is, even if mmap() > is supported, we still can't COW. I'll rename the functions to make > this a bit clearer (i.e., supported instead of allowed). > > Thank you for this and thank you for the reviews! > Sounds good to me :) > >>> + 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, page_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 page_size, size_t total_size) >>> { >>> 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); >>> } >> >> Add one more argument to test_mmap_denied as the flags passed to mmap(). >> >> static void test_mmap_denied(int fd, size_t page_size, size_t total_size, int mmap_flags) >> { >> mem = mmap(NULL, total_size, PROT_READ | PROT_WRITE, mmap_flags, fd, 0); >> } >> >>> >>> static void test_file_size(int fd, size_t page_size, size_t total_size) >>> @@ -120,26 +154,19 @@ static void test_invalid_punch_hole(int fd, size_t page_size, size_t total_size) >>> } >>> } >>> >>> -static void test_create_guest_memfd_invalid(struct kvm_vm *vm) >>> +static void test_create_guest_memfd_invalid_sizes(struct kvm_vm *vm, >>> + uint64_t guest_memfd_flags, >>> + size_t page_size) >>> { >>> - size_t page_size = getpagesize(); >>> - uint64_t flag; >>> size_t size; >>> int fd; >>> >>> for (size = 1; size < page_size; size++) { >>> - fd = __vm_create_guest_memfd(vm, size, 0); >>> - TEST_ASSERT(fd == -1 && errno == EINVAL, >>> + fd = __vm_create_guest_memfd(vm, size, guest_memfd_flags); >>> + TEST_ASSERT(fd < 0 && errno == EINVAL, >>> "guest_memfd() with non-page-aligned page size '0x%lx' should fail with EINVAL", >>> size); >>> } >>> - >>> - for (flag = BIT(0); flag; flag <<= 1) { >>> - fd = __vm_create_guest_memfd(vm, page_size, flag); >>> - TEST_ASSERT(fd == -1 && errno == EINVAL, >>> - "guest_memfd() with flag '0x%lx' should fail with EINVAL", >>> - flag); >>> - } >>> } >>> >>> static void test_create_guest_memfd_multiple(struct kvm_vm *vm) >>> @@ -170,30 +197,123 @@ static void test_create_guest_memfd_multiple(struct kvm_vm *vm) >>> close(fd1); >>> } >>> >>> -int main(int argc, char *argv[]) >>> +#define GUEST_MEMFD_TEST_SLOT 10 >>> +#define GUEST_MEMFD_TEST_GPA 0x100000000 >>> + >>> +static bool check_vm_type(unsigned long vm_type) >>> { >>> - size_t page_size; >>> + /* >>> + * Not all architectures support KVM_CAP_VM_TYPES. However, those that >>> + * support guest_memfd have that support for the default VM type. >>> + */ >>> + if (vm_type == VM_TYPE_DEFAULT) >>> + return true; >>> + >>> + return kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(vm_type); >>> +} >>> + >>> +static void test_with_type(unsigned long vm_type, uint64_t guest_memfd_flags, >>> + bool expect_mmap_allowed) >>> +{ >>> + 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 (!check_vm_type(vm_type)) >>> + return; >>> >>> page_size = getpagesize(); >>> total_size = page_size * 4; >>> >>> - vm = vm_create_barebones(); >>> + vm = vm_create_barebones_type(vm_type); >>> >>> - test_create_guest_memfd_invalid(vm); >>> test_create_guest_memfd_multiple(vm); >>> + test_create_guest_memfd_invalid_sizes(vm, guest_memfd_flags, page_size); >>> >>> - fd = vm_create_guest_memfd(vm, total_size, 0); >>> + fd = vm_create_guest_memfd(vm, total_size, guest_memfd_flags); >>> >>> test_file_read_write(fd); >>> - test_mmap(fd, page_size); >>> + >>> + if (expect_mmap_allowed) >>> + test_mmap_allowed(fd, page_size, total_size); >>> + else >>> + test_mmap_denied(fd, page_size, total_size); >>> + >> >> if (expect_mmap_allowed) { >> test_mmap_denied(fd, page_size, total_size, MAP_PRIVATE); >> test_mmap_allowed(fd, page_size, total_size); >> } else { >> test_mmap_denied(fd, page_size, total_size, MAP_SHARED); >> } >> >>> 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); >>> +} >>> + >>> +static void test_vm_type_gmem_flag_validity(unsigned long vm_type, >>> + uint64_t expected_valid_flags) >>> +{ >>> + size_t page_size = getpagesize(); >>> + struct kvm_vm *vm; >>> + uint64_t flag = 0; >>> + int fd; >>> + >>> + if (!check_vm_type(vm_type)) >>> + return; >>> + >>> + vm = vm_create_barebones_type(vm_type); >>> + >>> + for (flag = BIT(0); flag; flag <<= 1) { >>> + fd = __vm_create_guest_memfd(vm, page_size, flag); >>> + >>> + if (flag & expected_valid_flags) { >>> + TEST_ASSERT(fd >= 0, >>> + "guest_memfd() with flag '0x%lx' should be valid", >>> + flag); >>> + close(fd); >>> + } else { >>> + TEST_ASSERT(fd < 0 && errno == EINVAL, >>> + "guest_memfd() with flag '0x%lx' should fail with EINVAL", >>> + flag); >>> + } >>> + } >>> + >>> + kvm_vm_release(vm); >>> +} >>> + >>> +static void test_gmem_flag_validity(void) >>> +{ >>> + uint64_t non_coco_vm_valid_flags = 0; >>> + >>> + if (kvm_has_cap(KVM_CAP_GMEM_SHARED_MEM)) >>> + non_coco_vm_valid_flags = GUEST_MEMFD_FLAG_SUPPORT_SHARED; >>> + >>> + test_vm_type_gmem_flag_validity(VM_TYPE_DEFAULT, non_coco_vm_valid_flags); >>> + >>> +#ifdef __x86_64__ >>> + test_vm_type_gmem_flag_validity(KVM_X86_SW_PROTECTED_VM, non_coco_vm_valid_flags); >>> + test_vm_type_gmem_flag_validity(KVM_X86_SEV_VM, 0); >>> + test_vm_type_gmem_flag_validity(KVM_X86_SEV_ES_VM, 0); >>> + test_vm_type_gmem_flag_validity(KVM_X86_SNP_VM, 0); >>> + test_vm_type_gmem_flag_validity(KVM_X86_TDX_VM, 0); >>> +#endif >>> +} >>> + >>> +int main(int argc, char *argv[]) >>> +{ >>> + TEST_REQUIRE(kvm_has_cap(KVM_CAP_GUEST_MEMFD)); >>> + >>> + test_gmem_flag_validity(); >>> + >>> + test_with_type(VM_TYPE_DEFAULT, 0, false); >>> + if (kvm_has_cap(KVM_CAP_GMEM_SHARED_MEM)) { >>> + test_with_type(VM_TYPE_DEFAULT, GUEST_MEMFD_FLAG_SUPPORT_SHARED, >>> + true); >>> + } >>> + >>> +#ifdef __x86_64__ >>> + test_with_type(KVM_X86_SW_PROTECTED_VM, 0, false); >>> + if (kvm_has_cap(KVM_CAP_GMEM_SHARED_MEM)) { >>> + test_with_type(KVM_X86_SW_PROTECTED_VM, >>> + GUEST_MEMFD_FLAG_SUPPORT_SHARED, true); >>> + } >>> +#endif >>> } >> Thanks, Gavin