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: 70531 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp212434wrt; Wed, 15 Mar 2023 17:57:23 -0700 (PDT) X-Google-Smtp-Source: AK7set/ttGBh74S7ohdCkh7A/wtC98zw5fuTyblNyHb0vWh1cbiC/ktv30LkBF2Qy3MXhu57c9Gc X-Received: by 2002:a17:90b:3881:b0:237:a500:eca6 with SMTP id mu1-20020a17090b388100b00237a500eca6mr1613945pjb.22.1678928243526; Wed, 15 Mar 2023 17:57:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678928243; cv=none; d=google.com; s=arc-20160816; b=hFtTwMJ9rmtGvQ3zv/AkowBtnFWUyeTIuFUoXhPHwL8kdorqM6RxSpR3PDdJYfC8Dr rIGVqj9ujNUp+FE9UzWsedLgbjftXSNRCl4WhUdhZ7ByrRJh303hUChhQhtXkbm1Sa3N 6FND+xjeOMBTFJqtk2lbVkGHt/lzcQPCH461uEsG8bNcBh7soDL/PofY+psfi+5GDv8m 3aAx9QM7uu7cFgLFfKYYGSdfdTkDRf75w48feI6WLQ8kmV07TznVOsgcbvOsY3l9IMP3 GQjtfuvZp6c7g6Kct5xr0Rv+49IB0El0KLbYvTzXxUa0Og9RSFxCGeGObYQOoWlm/lDI 7nkg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=xKXEz6/mGmWtcTzhu61L6SyW9ch3P6ySrkhZdBbEs24=; b=LljNBeMCZYTSKT+g9pIMzguplAM67j0dBIplUovIlouGt5SXUX/4ocEU68+RCtcW+G t3bEYhfL6RO9MgtBnSJ0iWeLQ5/k1FmYXZiMKBI2BDoWcCqJB/hPOht7dWnDlNfkMD3g py7gszLHG34FYvWdEkS8oZFUPQ3ygU7v4mpp+SJTY9lSEzOjrJC/cGDO43JYqp+TGvG1 Vv0o3N/QCykunmih5We9g7Wp76HDsoYbfNuatsxzhn+aoA0SQp3prJPxbPFy4zy7J4gV YN7X2yMA0ofNxGCMqTB/nbugdYwiGKulNFgDxqsixi9AHL2dJdDllnwQgnFo/Z4b++l+ fAGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=bOjlcixb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x18-20020a17090abc9200b0023b4138a6acsi2968590pjr.164.2023.03.15.17.57.10; Wed, 15 Mar 2023 17:57:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=bOjlcixb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233258AbjCPAdH (ORCPT + 99 others); Wed, 15 Mar 2023 20:33:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35646 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233340AbjCPAcF (ORCPT ); Wed, 15 Mar 2023 20:32:05 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B91BFA6491 for ; Wed, 15 Mar 2023 17:31:32 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id x3-20020a62fb03000000b00622df3f5d0cso175329pfm.10 for ; Wed, 15 Mar 2023 17:31:32 -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=OdPyoi35E4Y3hRb1ZWuLozCEj6pQ0c04kVkqXBydPBRbJ8xfibfxDtil16EnIsAGVc dIh7OegXjCxiIEj7kxLHPOYpdNcOVOq5qSvCaELk8eJJa0wd6ZijdMqqPzhNxqobmmOp 2OHOuUlv+8W17wZiVyTArUhvF69tPT9e5IYpKm29I8V3yYn/GYpWD4KC2QPAA4c3iDf0 /0nyPMdvQGOcSpPddBFTxVC/OqqusFMPjoaUvxCenz6yyenf/Gc28PsU7prbM+aN8pzS oSR63cN25+magpnIvEsuAN+F6q7tUdVeG1oARnhfHg+BwMcQVZlx3I8aaAFpGgbGFRzN OLoQ== X-Gm-Message-State: AO0yUKUsufdb3Ljqs/FJkaXDZyT8bhhKqkOxhniovFAw17Kn+6T/u3OT hrLMwyRfGoyndH+3mcBomMsQJ4L8ALwhF4K8Jw== 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-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760483861600854222?= X-GMAIL-MSGID: =?utf-8?q?1760483861600854222?= 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[])