From patchwork Thu Mar 16 00:31:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 13176860 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 0FE9FC7619A for ; Thu, 16 Mar 2023 00:31:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 76EBC6B0093; Wed, 15 Mar 2023 20:31:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 71F056B0095; Wed, 15 Mar 2023 20:31:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4FB2A6B0096; Wed, 15 Mar 2023 20:31:33 -0400 (EDT) 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 38C1C6B0093 for ; Wed, 15 Mar 2023 20:31:33 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id EAAF912110E for ; Thu, 16 Mar 2023 00:31:32 +0000 (UTC) X-FDA: 80572882824.29.E81839B Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) by imf20.hostedemail.com (Postfix) with ESMTP id 2C7221C000D for ; Thu, 16 Mar 2023 00:31:30 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=bOjlcixb; spf=pass (imf20.hostedemail.com: domain of 3YmMSZAsKCJk35D7KE7RMG99HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--ackerleytng.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3YmMSZAsKCJk35D7KE7RMG99HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--ackerleytng.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=1678926691; 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=xKXEz6/mGmWtcTzhu61L6SyW9ch3P6ySrkhZdBbEs24=; b=gHnM4QeyZRNUKS7/gFZ13rLjeQvSTSXAeHkdAovJPN9OaBxa0vTNl6WodWZsqTN0+Qxh3u pF5cVBqh0nvw56kUsfj2nKnBk0O4ZKk2ke6HaQZc7/Rm4mtUJh7s7wg6VtU+QPbWT3fLrf kHafKj0QMI0ZS1mBIEsfEzAnXVRuy8Y= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=bOjlcixb; spf=pass (imf20.hostedemail.com: domain of 3YmMSZAsKCJk35D7KE7RMG99HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--ackerleytng.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3YmMSZAsKCJk35D7KE7RMG99HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--ackerleytng.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678926691; a=rsa-sha256; cv=none; b=vkdmdeAUdTng4peit/SFNf0SXc/36DKs1te/QSVMYuLeGXjt6COkHXpGPW1YK5g/J2n7jJ v38RTYuVNidSodXHUo2oznF788jsrg30KYkvKIg0MfiFKrHQfKO1HG5ehGXoQIKUtlOtIj sc2/ULLBc6Zlyg00dBeHO434PxVoor0= Received: by mail-pl1-f202.google.com with SMTP id u11-20020a170902e80b00b001a043e84bdfso6193040plg.23 for ; Wed, 15 Mar 2023 17:31:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678926690; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=xKXEz6/mGmWtcTzhu61L6SyW9ch3P6ySrkhZdBbEs24=; b=bOjlcixbSYLhJ8wlZFJqykn9M5Vc0cFpb5g7LhT4djxRWIFNy4JccJg4iGmlnPSQyG lJkRIlzLGQZ3CT8tV3X05ZRlfpMlsWsYm10BZk0r/Rv+yvfQdYk0fGtvbClQaHHaYv5l Oj+/kJI0SJy2+jeH1DiW21OupNOPFM9Wh7wNdPPay5eUxl09yuqaKdEkkyKrOhWCrZJX e3EV/eGevF9LE/ooEptOTOPMZBAmj7BKZBkDjiPROvuVe+4bwZbAe4m1RyZsPF9uX6pM WIBv4RjDLE054xRE18NVAkWPZAWP+NMIosMPzwAwGqSXU4pxgbXQRbLdQ2u4zKCbVIO2 DV9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678926690; 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=xKXEz6/mGmWtcTzhu61L6SyW9ch3P6ySrkhZdBbEs24=; b=HM9+eGXpAG4vKTz+60yXdNuLI0u0ozwRGdEcgFUNbILxRw+XkFOqPWPN2xm3ee48wq i8dOKRPDwFZojULOWCcR783kjxSfJIFfy/6pcaK1bd+9O656bv65sp4IkfZC3u1n18UZ 3FgW8w+h1iXAtP12C1rhzG9YyoJZqoNs2nufNF7qBB2QIxicsvssRUHABSydVIzdk+Tm x+2Ga62ItMPbSfCIPtJ4vxMTaumu2BGsQ+Y5IifB5MVxfdEJxeTeKvviOdhaadTxICbb 92V0+KgUV4RuQsy+pEOb8xrEbHjic6oaVviwOJDLsf97aYQNXNW5o33hlz7Sf391WENr 1qFA== X-Gm-Message-State: AO0yUKXMMnskTDIXj862WDmv/Tq/6f7U0upQy6PO944dVe2tjfFk02Z+ 52cVb2RDprLiJS6h+mOWMB5Tx4Wa9IwT0CV8Mg== X-Google-Smtp-Source: AK7set8gW1ruUe+jZfx4vo3AOKGV2qgKk/o0c2WgfJNyT6l/gKi5/qNPPNY0SZ250Rg6tfQ0OBKmkU3mHKKgzrPR/w== X-Received: from ackerleytng-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1f5f]) (user=ackerleytng job=sendgmr) by 2002:a65:6392:0:b0:50b:cc91:5534 with SMTP id h18-20020a656392000000b0050bcc915534mr320262pgv.2.1678926690322; Wed, 15 Mar 2023 17:31:30 -0700 (PDT) Date: Thu, 16 Mar 2023 00:31:02 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.40.0.rc2.332.ga46443480c-goog Message-ID: Subject: [RFC PATCH 09/10] KVM: selftests: Add tests around sharing a restrictedmem fd From: Ackerley Tng To: kvm@vger.kernel.org, linux-api@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, qemu-devel@nongnu.org Cc: aarcange@redhat.com, ak@linux.intel.com, akpm@linux-foundation.org, arnd@arndb.de, bfields@fieldses.org, bp@alien8.de, chao.p.peng@linux.intel.com, corbet@lwn.net, dave.hansen@intel.com, david@redhat.com, ddutile@redhat.com, dhildenb@redhat.com, hpa@zytor.com, hughd@google.com, jlayton@kernel.org, jmattson@google.com, joro@8bytes.org, jun.nakajima@intel.com, kirill.shutemov@linux.intel.com, linmiaohe@huawei.com, luto@kernel.org, mail@maciej.szmigiero.name, mhocko@suse.com, michael.roth@amd.com, mingo@redhat.com, naoya.horiguchi@nec.com, pbonzini@redhat.com, qperret@google.com, rppt@kernel.org, seanjc@google.com, shuah@kernel.org, steven.price@arm.com, tabba@google.com, tglx@linutronix.de, vannapurve@google.com, vbabka@suse.cz, vkuznets@redhat.com, wanpengli@tencent.com, wei.w.wang@intel.com, x86@kernel.org, yu.c.zhang@linux.intel.com, Ackerley Tng X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 2C7221C000D X-Stat-Signature: x3ohfuxy4h6mizokryyjkr45ojf9i6hb X-HE-Tag: 1678926690-357026 X-HE-Meta: U2FsdGVkX1+QeoMLO7xlBGLvr5KQ5ivXXgaUiBIf96P1at/Gkx4Ryuwg1kRvxfAoqQKJbVYe6B4j7h95VgD5RjYfRUv/qp3Ct1cFgDJNQmnMwyA3RIlc/bB01T9jry5Silgp/3Q34y8cjrBLg1oT6ybvWMrvOkc6i+67AYnx3F5F7aKLM0uBv/OZM98ZJRHXlINeXhe+7Cag9K53JeCjy4Po+AIFMaTYYtLJSK+kNbFkhcBj6BOQv989Nb/qA9yWSzMeAcFOVpfYq2a0ILSot97Ub8OmmClbt9UvtFvuTrHjPklJ1nuXqfdwkCqbfVBKZL/riqEFcgwU3h6gXmpLO1oU1KyZNNa/mh4whWoQn4Zil61R/JQE5fR7iNxN3Mwe+2Y+LUko7HVLrs7MlFP6sXsAtLKDp0GZLqtzvrGLNY8fI2yGHCe7Rwt/Uv4wvFLvREDhjSr+otBzjMfwA5VQTp54+EHvD+6fPjZYGHUProGXKsDnslcWUK+vxAJrv/NX0rUqvr5d/v0FvPv8TN8mupYHBMkvJczKPjr/T4SPvkYwDUkboC4ZVVRR0j3kH5xxvXNBbqwSm0n3LttedVyjqn++SiegMFwZRjTvMf9v+sz0eCH3EpWqk5HbA57tLv2V6oZhe42gJNiWxImjwuDxue6+rwc0DdqQTDPU/+dfUsBpebrr5/Jhk7BwoISqS+VSPkExAw3X8pfJoorpsSDF22wwPdSaWQT5/IOZHMuoy7kfDTsXqq7HIk20dhGXEoDfcqhyPFDgSxTxcJGMCYpWL9LY/TrgN3KmI0nMowvA6K3R0K86F7ML2y1/ujoJLGJuar+DerRRzBhPGqGDHy9YIOekHj/4EB6Sy9Rz53dH5O3USMWhwMj4UBes3PMV6FCFAbFi31pf5VGTC7/CWVnNKVLCEaOuGfehRdgrn761oshtpECiTC4OSDkE+Sy9GOt31jYJ0QJsRZ4Qeig82Wn Al50PjRw jAkgxa4xrLEbC2FG79QwH1YLyKjZ3epM6aR5Q95O14MYU6TDIV51TAcN+/+15PhAwQlkg7uRE9637lpjIsMo7gJ25tR+fPuTL3270u/DxcbYd5u5YL388cnmdRpqCzx1CpD/Y7ywJrjB5l5qUXANrrawKjkmCLzwFH0No5H5EBC8NO9BQYVMh0pWdW2A5EBpfQL8vhbNhnN+lFP7gjtQNZoKpDq1HIodFf7LgE/9BP1+FELm1SZADh2Datu/coEKNGqTd423CZnd8AiScq6RqpWKE5rg0x8qB7awoy+3aMpCczpaIc2OYLXE7Yc4rAIUsI7dEq+KA3ZMGF8ZT6/9H/LNx/VUMz83SQdyrzUQ6dlewwvPfLdCR9RnxneBjBRIKyZcmy20N0T1qFdS8Q3TPFY/9OSfXf4V0qqL/9cKIhR1YQrZJ4yzWfI2IBnJShz6TKm7Lheg7qsbQHNkXbBaFF9eICwKFsLm+fxolDPpnKlok63Phw8A2Z0Zqp2Wxda4Q7ortp5DZC/eWKCnHAYUKsyc/MWyiETgkPeZpwCW+Uqd3pmnEskYpJR+HAH0QrgrJysnEBvxTebTbKTBpHJ4O1R/ZND91Eg1fnKD12HbieN+KxEyIogcIPibVB6CYghc+OcmhSsSf+WkIy/0uRusFx6ZjbBa26r+u3YPK 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: Tests that + Different memslots in the same VM should be able to share a restrictedmem_fd + A second VM cannot share the same offsets in a restrictedmem_fd + Different VMs should be able to share the same restrictedmem_fd, as long as the offsets in the restrictedmem_fd are different Signed-off-by: Ackerley Tng --- .../selftests/kvm/set_memory_region_test.c | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/kvm/set_memory_region_test.c b/tools/testing/selftests/kvm/set_memory_region_test.c index cc727d11569e..789c413e2a67 100644 --- a/tools/testing/selftests/kvm/set_memory_region_test.c +++ b/tools/testing/selftests/kvm/set_memory_region_test.c @@ -401,7 +401,7 @@ static bool set_private_region_failed(struct kvm_vm *vm, void *hva, static void test_private_regions(void) { int ret; - struct kvm_vm *vm; + struct kvm_vm *vm, *vm2; void *mem; int fd; @@ -416,7 +416,7 @@ static void test_private_regions(void) vm = __vm_create(shape, 1, 0); - mem = mmap(NULL, MEM_REGION_SIZE * 2, PROT_READ | PROT_WRITE, + mem = mmap(NULL, MEM_REGION_SIZE * 3, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0); TEST_ASSERT(mem != MAP_FAILED, "Failed to mmap() host"); @@ -448,8 +448,31 @@ static void test_private_regions(void) TEST_ASSERT(ret == -1 && errno == EINVAL, "Set overlapping restrictedmem_offset should fail"); - munmap(mem, MEM_REGION_SIZE * 2); + ret = __vm_set_user_memory_region2(vm, MEM_REGION_SLOT + 1, + KVM_MEM_PRIVATE, + MEM_REGION_GPA + MEM_REGION_SIZE, + MEM_REGION_SIZE, + mem + MEM_REGION_SIZE, + fd, MEM_REGION_SIZE); + TEST_ASSERT(!ret, + "Different memslots should be able to share a restrictedmem_fd"); + + vm2 = __vm_create(shape, 1, 0); + TEST_ASSERT(set_private_region_failed(vm2, mem + 2 * MEM_REGION_SIZE, fd, 0), + "Pages (offsets) of a restrictedmem_fd should be exclusive to a VM"); + + ret = __vm_set_user_memory_region2(vm2, MEM_REGION_SLOT, + KVM_MEM_PRIVATE, + MEM_REGION_GPA + 2 * MEM_REGION_SIZE, + MEM_REGION_SIZE, + mem + 2 * MEM_REGION_SIZE, + fd, 2 * MEM_REGION_SIZE); + TEST_ASSERT(!ret, + "Different VMs should be able to share a restrictedmem_fd"); + + munmap(mem, MEM_REGION_SIZE * 3); kvm_vm_free(vm); + kvm_vm_free(vm2); } int main(int argc, char *argv[])