From: Ackerley Tng Date: Tue, 28 Apr 2026 16:25:39 -0700 Subject: [PATCH RFC v5 44/53] KVM: selftests: Test that not specifying a conversion flag scrambles memory contents MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260428-gmem-inplace-conversion-v5-44-d8608ccfca22@google.com> References: <20260428-gmem-inplace-conversion-v5-0-d8608ccfca22@google.com> In-Reply-To: <20260428-gmem-inplace-conversion-v5-0-d8608ccfca22@google.com> To: aik@amd.com, andrew.jones@linux.dev, binbin.wu@linux.intel.com, brauner@kernel.org, chao.p.peng@linux.intel.com, david@kernel.org, ira.weiny@intel.com, jmattson@google.com, jthoughton@google.com, michael.roth@amd.com, oupton@kernel.org, pankaj.gupta@amd.com, qperret@google.com, rick.p.edgecombe@intel.com, rientjes@google.com, shivankg@amd.com, steven.price@arm.com, tabba@google.com, willy@infradead.org, wyihan@google.com, yan.y.zhao@intel.com, forkloop@google.com, pratyush@kernel.org, suzuki.poulose@arm.com, aneesh.kumar@kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Jonathan Corbet , Shuah Khan , Shuah Khan , Vishal Annapurve , Andrew Morton , Chris Li , Kairui Song , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Youngjun Park , Qi Zheng , Shakeel Butt , Kiryl Shutsemau , Jason Gunthorpe , Vlastimil Babka Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-coco@lists.linux.dev, Ackerley Tng X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1777418714; l=2091; i=ackerleytng@google.com; s=20260225; h=from:subject:message-id; bh=Avy1yjuj5TLXjW+gFYQvsB+PRbonfW+tVbhN8u3NZik=; b=fb9rZViAfwmrwJD9SGoXVoob6TT9WaSRJ4Wrr/TWhXnHc57g7FFxpcP7viuqXx3r+qwjvVnmr KXKhGbWj4CZCYiavrYR0ULbUfm3YqEoUTcfGzy/jE7G4hkJglBe7CP0 X-Developer-Key: i=ackerleytng@google.com; a=ed25519; pk=sAZDYXdm6Iz8FHitpHeFlCMXwabodTm7p8/3/8xUxuU= X-Endpoint-Received: by B4 Relay for ackerleytng@google.com/20260225 with auth_id=649 List-Id: B4 Relay Submissions When using KVM_SET_MEMORY_ATTRIBUTES2, not specifying flags for the ioctl implies no guarantees on memory contents. For KVM_X86_SW_PROTECTED_VM, this mode is implemented by scrambling contents of converted memory ranges. Add a test to check that the unspecified conversion mode was handled in KVM by checking the expected behavior, that existing memory contents are scrambled. Signed-off-by: Ackerley Tng --- .../kvm/x86/guest_memfd_conversions_test.c | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tools/testing/selftests/kvm/x86/guest_memfd_conversions_test.c b/tools/testing/selftests/kvm/x86/guest_memfd_conversions_test.c index 57adb6d84a053..f4705dc700879 100644 --- a/tools/testing/selftests/kvm/x86/guest_memfd_conversions_test.c +++ b/tools/testing/selftests/kvm/x86/guest_memfd_conversions_test.c @@ -512,6 +512,34 @@ GMEM_CONVERSION_TEST_INIT_SHARED(convert_to_private_does_not_support_zero) TEST_ASSERT_EQ(error_offset, start_offset); } +GMEM_CONVERSION_TEST_INIT_SHARED(convert_mode_unspecified_scrambles) +{ + loff_t error_offset = 0; + int ret; + + test_shared(t, 0, 0, 'A', 'B'); + ret = __gmem_set_private(t->gmem_fd, 0, nr_pages * page_size, + &error_offset, 0); + TEST_ASSERT_EQ(ret, 0); + TEST_ASSERT_EQ(error_offset, 0); + + /* + * Since the content mode 0 scrambles data in memory, there is + * a small chance that this test will falsely fail when the + * scrambled value matches the initial value. + */ + run_guest_do_rmw(t->vcpu, 0, 'B', 'C', true); + + ret = __gmem_set_shared(t->gmem_fd, 0, nr_pages * page_size, + &error_offset, 0); + TEST_ASSERT_EQ(ret, 0); + TEST_ASSERT_EQ(error_offset, 0); + + /* Same small chance of falsely failing test applies here. */ + TEST_ASSERT(READ_ONCE(t->mem[0]) != 'C', + "Conversion without specifying mode should scramble memory."); +} + int main(int argc, char *argv[]) { TEST_REQUIRE(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM)); -- 2.54.0.545.g6539524ca2-goog