From patchwork Thu Mar 16 00:30:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 70529 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp207588wrt; Wed, 15 Mar 2023 17:43:11 -0700 (PDT) X-Google-Smtp-Source: AK7set/L9N7Lc/iIFKkO1A3VFs4wbhFtlOYXeQG/AT2xQIv+GxeUrDZ0ODk0T2TG7PPfqCk+3GX/ X-Received: by 2002:a62:5286:0:b0:624:8819:64c7 with SMTP id g128-20020a625286000000b00624881964c7mr1424866pfb.11.1678927390710; Wed, 15 Mar 2023 17:43:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678927390; cv=none; d=google.com; s=arc-20160816; b=uRwR11Fx4Q3su5yC8EZexywkd1VfNA5M8txzscRtdHzJQmrjrUb7QDnHXtHdxOvEBK Zf1/GCULOrqNvGcuDDA2awzubxAKQ7dm2oFVvtGubsT6eaTz5ETGN+IhsnET9FuCqaxO wnbQJpi/Gr9IR+c9jaNlw0Pcp7RG3Y6fpUoNd0u4BBI5JsCTQFN3DXJAe//lMJkUM+Gx s4AzexPuVg264nJ9Cy+UTQOcGIQhIYPwvOZSBnXkHi+G/Tv/8fgYIvN/OQJFIpLc6i+Q On/erWZDoMZjXxD1Eg2WsThPp+kCoh5abtXLBs9hSw1V9Uhya7++0yDvMcbTGUiqZBGo tW8Q== 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=rreqZKmQJtCTOHmDtxKTKEfGS9dImrAo0RCl95Sk/EE=; b=GAAHpQufus9nl0q0FcyNF05MlWwttv8Kc/0jOUaR4e0szv8XMmjzsJZRP5vbgHCKDY 2z0KWTf3AWK1/Qx8pvXntMn6oOk4lp8MRqQHk3anmsTm5JgIIKv9/QT/YYZIlZiFREqe AFZ1qXY+V4vJFgSeqaWA4ROnPFauGZwrUlO1zRIGK5biENmavCntYCLMJNKhjU4foejW qp1EUQcm8dk9EWfCXPh2M6g/IFSiNnStMvfVz4OVLcOh/r5ivmhp/mwcuYTXYZR49cf1 fawu9cP5v9xq0RcnQFzCuk2QUahSr3atkyqbJN0CZ/R6gUvq9Q9ig3hBlWUClDoF5m7k VS2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=JJueoK7s; 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 r20-20020a6560d4000000b00508caaf69besi6124428pgv.862.2023.03.15.17.42.58; Wed, 15 Mar 2023 17:43:10 -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=JJueoK7s; 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 S233185AbjCPAbe (ORCPT + 99 others); Wed, 15 Mar 2023 20:31:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34480 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233082AbjCPAb2 (ORCPT ); Wed, 15 Mar 2023 20:31:28 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E17EEA0F13 for ; Wed, 15 Mar 2023 17:31:18 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id e129-20020a251e87000000b00b56598237f5so94478ybe.16 for ; Wed, 15 Mar 2023 17:31:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678926677; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=rreqZKmQJtCTOHmDtxKTKEfGS9dImrAo0RCl95Sk/EE=; b=JJueoK7sdFHQHbdAl5nsqaNs4KCHm7sHzvLB42MnoY5lkfM6ZyYxqRBfJSJa8Yj0AO a2uaVznX+a7AGF4HtGGnVlQphXnV9jEi1nR4tPRumBxVDCE+WOnyJKaxZt8wQIJXoXkp loUdl05RrEtBxZJ1mxGOVb+Igf7OXAXS6g5lENKD5GhCbaaHIbFWr/+X13ZJvgEAeOrT ejL8r4WfIiMkFavlKyXDdwKqsnArh0+uyWf18MM3tbJLCmk8NSuRhA6hTr60P2bZx80K jj0hiuo3QlpYfzngCqcd5OQKvr0RQN1Z1ORNuOh1qFkI0/mHIDpESFq+n7v7NaT1lIXl wVMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678926677; 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=rreqZKmQJtCTOHmDtxKTKEfGS9dImrAo0RCl95Sk/EE=; b=FBWME9uujxNniHIccvDOXC8T3JN94E0r3121WUYtC3Eud1oqkG9bmQcxc9Tc96MsNP rQO1RXHYzxkrvZuaITsDTVVMZg661AE18jwjJz5Ih8keu69pGETxx9lYQ5fdbxD7Upb3 Xn5b5XBVoqJ0GNAWnttCEtQtg4zkR0S0D0SNyoD8rUYBdhqgkwZj5KTgkltvFM2GdPD8 FoCSte1qA1LVE5ghjQE/XbQ2RnhM9xqpWhknkqwNHQ9zeXPqtwvDQUje0e93EXR9d9nb vgmWkW+qSSJAfHpYvo9KEy06JCf88yj3In9GoIykxSaCg4YzMBeRWft3ydJJ3eiGJ4cb xauQ== X-Gm-Message-State: AO0yUKWJowVibXitNYWnwN95Tm8VfEYfZNcFfHYfe2Ugr10YWwmfzYOP KAzcgWnSwfbkWjZxJe4b4RBqG/fcNCbSfD+oLg== X-Received: from ackerleytng-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1f5f]) (user=ackerleytng job=sendgmr) by 2002:a25:e201:0:b0:b2e:f387:b428 with SMTP id h1-20020a25e201000000b00b2ef387b428mr12671962ybe.5.1678926677076; Wed, 15 Mar 2023 17:31:17 -0700 (PDT) Date: Thu, 16 Mar 2023 00:30:54 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.40.0.rc2.332.ga46443480c-goog Message-ID: <017a3f68ef7007d72f167f937fabd6d64efb9edc.1678926164.git.ackerleytng@google.com> Subject: [RFC PATCH 01/10] KVM: selftests: Test error message fixes for memfd_restricted selftests 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?1760482967305647113?= X-GMAIL-MSGID: =?utf-8?q?1760482967305647113?= Signed-off-by: Ackerley Tng --- tools/testing/selftests/vm/memfd_restricted.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/vm/memfd_restricted.c b/tools/testing/selftests/vm/memfd_restricted.c index 3a556b570129..43a512f273f7 100644 --- a/tools/testing/selftests/vm/memfd_restricted.c +++ b/tools/testing/selftests/vm/memfd_restricted.c @@ -49,12 +49,12 @@ static void test_file_size(int fd) } if (sb.st_size != page_size) { - fail("unexpected file size after ftruncate"); + fail("unexpected file size after ftruncate\n"); return; } if (!ftruncate(fd, page_size * 2)) { - fail("unexpected ftruncate\n"); + fail("size of file cannot be changed once set\n"); return; } From patchwork Thu Mar 16 00:30:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 70525 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp204731wrt; Wed, 15 Mar 2023 17:34:57 -0700 (PDT) X-Google-Smtp-Source: AK7set8oUutV8ZxFXRMwl2VsTG+I7/u1UyqqM482vvSaSb6nbP6REwqiRm4uIQQZns/bT5me+rXr X-Received: by 2002:a17:903:1107:b0:19e:b2f3:e8c4 with SMTP id n7-20020a170903110700b0019eb2f3e8c4mr1566968plh.25.1678926897427; Wed, 15 Mar 2023 17:34:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678926897; cv=none; d=google.com; s=arc-20160816; b=I0hsYbgoqkzONUimFQz4mkOasXint+MJzFvkIVFAeNgbj2NU0zvseem06Ae353c5jd P6qz4G+DdVtkBq7QCWwseiq4I/E0htFRUUIcUrbkWxkTdbkoxOIuazEkKNlpiy43qwGq ZgMWTxFG2phR0tLDNqzDrpFmaSVCH+mNqRLS02kcIpO/aB9GjaRXGCxX2mX47ggqLOEg IfGZO4cNN7Ewh8u5G/Ua+HOXUp+8UZ1Cij9VOA3hTGSD0ANZpQ+9c/vZvIx0ThrgS+yn ZLVxIeqYrrgGNEmYRm60yw3hvbbQWr+6+12keRRRZmfo2f+KsFodFIzyFDnLRk1px2qK 372Q== 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=vx2+K6b7VucsfYU5uvwShdem3MP0VCFQdKNh537WPsg=; b=mQufiKQjHtdAi0+/1/4hjyc11y9yxo24keLsN+S9wSEfSSvagJwq8Rwk0kpIG/XW7s uvwwZavXTtQcGFSX+yQUlH19+97YPdPKe/nH38rbgbKnMmIouEyHr6BuwxJSKPqE+jsG jsCILSra5gkh3sWzBDslSYsz3+QdM+8gDIkK0bCXqPi7No5OqWZII3NIVLk/vjcbtpoi /O8z7bJDwFWwA67sDIR8xOKRHdHyemV5fRXTqlRF8VfVZt+5A0OwKeVwMekTavD2NePO 9NJsyAMdpP6piGtFoEN3wMjjN+l11H2ZZSjK22zSVru8SEbTKt3PONhJIqNEbQjFIC7A PR4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=PAaXwSJq; 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 jl17-20020a170903135100b0019edb3d20f1si6007753plb.154.2023.03.15.17.34.42; Wed, 15 Mar 2023 17:34:57 -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=PAaXwSJq; 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 S233083AbjCPAbi (ORCPT + 99 others); Wed, 15 Mar 2023 20:31:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34492 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233126AbjCPAb3 (ORCPT ); Wed, 15 Mar 2023 20:31:29 -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 E7D399F20F for ; Wed, 15 Mar 2023 17:31:19 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id y31-20020a056a001c9f00b00623268e040dso176661pfw.7 for ; Wed, 15 Mar 2023 17:31:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678926678; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=vx2+K6b7VucsfYU5uvwShdem3MP0VCFQdKNh537WPsg=; b=PAaXwSJqPjUc6R/biIaCW2VNIfg9P921yoYdsBglXW7n/3lysnZkeEeswRZxCHBdd2 vae46IBUMuPrhA/m6sIKIzqdGOnQo5Ax7q3XOoZhIMO7H4xC0VbFQ0N3vE+qhGQUMHzu kPA+f6sp7rqp+TQqvoN2RVgjN8fnA/s8vq6Qc0ND26uA1J32QTIzKCh5QE6EZ2HKVyvQ j5NA1EYtTCtaS4iuVqchmK8CE1Gi+C6RcXJmBHwvadsPdAFJPwZbs86of6yw4m2yRstH qut3sHXQ3PqToSUKXfBRc1s9T+WqYiB5itYUbgh+khi4BVYb6F2EdusOJNoVl2aI0MUq SNww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678926678; 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=vx2+K6b7VucsfYU5uvwShdem3MP0VCFQdKNh537WPsg=; b=fAJ6ne+jH20fODIvU+35ztN4716iUytmTmnYc7/q3dBfFyqTQgiK/j3kzuvztj7unb XiAnQvWHR9Bb2DTPP54Lh00jXlrb94qhSRc+rYFz6KIgf+yRV/ugmEFD+g5DEnENZJ/9 TemSvXUiklC53xbgYFjAfcb+RHwuLY/jG+/YChboluiS+/X+vE8iZBZvcKeKns1s5/yl 06AorRDsaSkCvjIDH+sPMIJkvum4u9vE9wlDJXZpXdR9PEbZg9b0wRHWwRu9sK4S9x14 GEQr1LlQjH5O7ZYlRgK8CeJ0rupJyNH1feguixFlkiu88B+er501OHy8kwndBSYZxjnG YT0w== X-Gm-Message-State: AO0yUKVQiafOHAw6V5dGYKMvF+m51pwClf3BS4jsIiZ/NqhS+IPipcJo X7rs1rkNDSNK0iIYG4KX5BCiv0q6oId/XpuzFg== X-Received: from ackerleytng-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1f5f]) (user=ackerleytng job=sendgmr) by 2002:a05:6a00:23d6:b0:625:cf6d:b272 with SMTP id g22-20020a056a0023d600b00625cf6db272mr580538pfc.6.1678926678685; Wed, 15 Mar 2023 17:31:18 -0700 (PDT) Date: Thu, 16 Mar 2023 00:30:55 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.40.0.rc2.332.ga46443480c-goog Message-ID: <75eba82a2666b0caa96ed0484a713037045ed114.1678926164.git.ackerleytng@google.com> Subject: [RFC PATCH 02/10] KVM: selftests: Test that ftruncate to non-page-aligned size on a restrictedmem fd should fail 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?1760482450443113789?= X-GMAIL-MSGID: =?utf-8?q?1760482450443113789?= Signed-off-by: Ackerley Tng --- tools/testing/selftests/vm/memfd_restricted.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tools/testing/selftests/vm/memfd_restricted.c b/tools/testing/selftests/vm/memfd_restricted.c index 43a512f273f7..9c4e6a0becbc 100644 --- a/tools/testing/selftests/vm/memfd_restricted.c +++ b/tools/testing/selftests/vm/memfd_restricted.c @@ -38,6 +38,11 @@ static void test_file_size(int fd) { struct stat sb; + if (!ftruncate(fd, page_size + 1)) { + fail("ftruncate to non page-aligned sizes should fail\n"); + return; + } + if (ftruncate(fd, page_size)) { fail("ftruncate failed\n"); return; From patchwork Thu Mar 16 00:30:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 70522 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp204339wrt; Wed, 15 Mar 2023 17:33:47 -0700 (PDT) X-Google-Smtp-Source: AK7set9Mv1oBp+1L+aP5otGVJd6X/a5z0U5oEj/mLSGoQzHeuHyaIJUYSiNVQYhsj9wdLvXJcpbx X-Received: by 2002:a9d:19ca:0:b0:69b:4f8a:7bcd with SMTP id k68-20020a9d19ca000000b0069b4f8a7bcdmr1507766otk.35.1678926827361; Wed, 15 Mar 2023 17:33:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678926827; cv=none; d=google.com; s=arc-20160816; b=A/7c6Fjb1Hxp5bJtZmaVtsI4fZEBTulUq5+UUWceZv6vaDR/m3xb9KV0gWPUVDW338 LBoPScoaVLod6P2MFkPsEbcGGzXLzef/765uO+k9MIt8CJr/xQqml7nVwAn3M7kmJiW/ vTvrkKpLL8PKA+ZxqOPWdChC47YNE5eBIltJ67zwhJ5plpFoYbf3QPQrzSke7ujx6y8V g3mxbTlameEBpX7KiSbL7KW8tjV6efv5BdParaKKrEYaMGNjk9juUb2NtPqnsTR9PiGp n4rh4dubR59kvbZjEkS4R4Tye1FQB3Ln+V9dLtcOyR5l07CXeHfOhFLsMNCVVDpJguHe IkBA== 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=JjmIOJvjUEG2+4aovgmSBlRBGRP0JRLKbOkhR3o7e/I=; b=aHTdywZesfd1LZTODjA7WpIg/xb9gkw/jsKl40OVJx1uOcgEicw4ujomwwTMD0Ygim 5kd9b4BfY3WrtbRlE4pXFOx6LaqdoXS7q7mj1+GoHfMzQW32IkTO32pXu7Ho+hJkQM2A UFNp6KTP/1af/tzYNAmGjRD6Y94MeuO5yHdHMFhezqMIKoDT7dLKpJP6kpVPhrgxbjwh aeWzwr/9gG/k9RP57OsuUh8kiIw7NbEh+cdoIF2qWmiFp1rEyyk1JseHJCfOYc9xVfo1 A5wV7lBHHmZP8BVMzisWaLUFF6jZ0eeLUx6TKu2xxmYKWSR5ydtHn4ZMnLldu65cXhsT Qm0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=XWGmJ9JA; 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 g15-20020a056830308f00b0068bcdc1b6fasi5956350ots.40.2023.03.15.17.33.33; Wed, 15 Mar 2023 17:33:47 -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=XWGmJ9JA; 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 S233252AbjCPAby (ORCPT + 99 others); Wed, 15 Mar 2023 20:31:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231984AbjCPAbg (ORCPT ); Wed, 15 Mar 2023 20:31:36 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E0D78A3354 for ; Wed, 15 Mar 2023 17:31:21 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id k1-20020a632401000000b0050beb8972bfso19049pgk.7 for ; Wed, 15 Mar 2023 17:31:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678926680; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=JjmIOJvjUEG2+4aovgmSBlRBGRP0JRLKbOkhR3o7e/I=; b=XWGmJ9JAs9QizM49LznB/3WeGWT9Pc+PU89wCd8j/FUtVE5mMn/ax/bL088f1qtOz1 RFmCWBaiH29nU9Rm1KEJYwwbJN+AkVaHOxnJG89ZrDJuTFkTNXO5WvuZG2ISNDllw0uo fSjaUkPVuesviWcTzk98UxYatwmpd417x6c+02nHUCPhFRDc3jObfLW3h3zTrxlqgUMt LSd6LeHzPnNB0IV8pPZgCTjW0bYxKCuKc0IBsjiwto4vhAFiBAjpHsDO9Qt092Ms2tAr DgVpYe+MAJleDmnTkKW5CLiYIwwbNbMGX4Gf9ND5AcxA8F5WN4Z2xDh5NS+5PrMXSWp9 XSmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678926680; 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=JjmIOJvjUEG2+4aovgmSBlRBGRP0JRLKbOkhR3o7e/I=; b=p6gBgNpaRhAF0SEvTMwgD5iJsjwn9dBqFsjHHlVvMYifkJyVxC7njhEZ6OM1UrauRJ kp1O04ieBLd7JhkvKDzQRP1RjTymG2mOWKeQYtlXEeif3w0mgxBg5zCnB78r3ikbCysu +dZhXG7nTYoZ/nuVYqFOwPYyfrqD/kW1Z2z/AQkrzc0H6034+ElCOkZDK1zuetE2Pm4D wdc7NZD/py8hk5n2glDafKvMQsU8lc7I1NfBu/l6gSXUfkJps6KnBfARaVeg4ccK0s6p jqRfSZD+25hrgTk5Pi3fZB1B1J91Ttesnl1ARMDimLmuyElMiV1uocb1onEX2kZwPSAA CE6A== X-Gm-Message-State: AO0yUKWs5E2johGk2BIkpg8Aj9lJsdbiU0GFmLqBV3ivOenTae6emQZc 4TpWzg+waZiSArLObGcanZo34qa4WfZIm/xhLg== X-Received: from ackerleytng-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1f5f]) (user=ackerleytng job=sendgmr) by 2002:a17:902:e5cb:b0:1a0:4346:d43e with SMTP id u11-20020a170902e5cb00b001a04346d43emr595748plf.11.1678926680301; Wed, 15 Mar 2023 17:31:20 -0700 (PDT) Date: Thu, 16 Mar 2023 00:30:56 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.40.0.rc2.332.ga46443480c-goog Message-ID: <48490641ce981c31ea58c11ad478ff85cd0dd156.1678926164.git.ackerleytng@google.com> Subject: [RFC PATCH 03/10] KVM: selftests: Test that VM private memory should not be readable from host 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?1760482376870805789?= X-GMAIL-MSGID: =?utf-8?q?1760482376870805789?= After VM memory is remapped as private memory and guest has written to private memory, request the host to read the corresponding hva for that private memory. The host should not be able to read the value in private memory. This selftest shows that private memory contents of the guest are not accessible to host userspace via the HVA. Signed-off-by: Ackerley Tng --- .../kvm/x86_64/private_mem_conversions_test.c | 54 ++++++++++++++++--- 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c b/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c index ef9894340a2b..f2c1e4450b0e 100644 --- a/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c +++ b/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c @@ -47,6 +47,16 @@ static void memcmp_h(uint8_t *mem, uint8_t pattern, size_t size) pattern, i, mem[i]); } +static void memcmp_ne_h(uint8_t *mem, uint8_t pattern, size_t size) +{ + size_t i; + + for (i = 0; i < size; i++) + TEST_ASSERT(mem[i] != pattern, + "Expected not to find 0x%x at offset %lu but got 0x%x", + pattern, i, mem[i]); +} + /* * Run memory conversion tests with explicit conversion: * Execute KVM hypercall to map/unmap gpa range which will cause userspace exit @@ -64,8 +74,14 @@ static void memcmp_h(uint8_t *mem, uint8_t pattern, size_t size) #define GUEST_STAGE(o, s) { .offset = o, .size = s } -#define GUEST_SYNC4(gpa, size, current_pattern, new_pattern) \ - ucall(UCALL_SYNC, 4, gpa, size, current_pattern, new_pattern) +#define UCALL_RW_SHARED (0xca11 - 0) +#define UCALL_R_PRIVATE (0xca11 - 1) + +#define REQUEST_HOST_RW_SHARED(gpa, size, current_pattern, new_pattern) \ + ucall(UCALL_RW_SHARED, 4, gpa, size, current_pattern, new_pattern) + +#define REQUEST_HOST_R_PRIVATE(gpa, size, expected_pattern) \ + ucall(UCALL_R_PRIVATE, 3, gpa, size, expected_pattern) static void guest_code(void) { @@ -86,7 +102,7 @@ static void guest_code(void) /* Memory should be shared by default. */ memset((void *)DATA_GPA, ~init_p, DATA_SIZE); - GUEST_SYNC4(DATA_GPA, DATA_SIZE, ~init_p, init_p); + REQUEST_HOST_RW_SHARED(DATA_GPA, DATA_SIZE, ~init_p, init_p); memcmp_g(DATA_GPA, init_p, DATA_SIZE); for (i = 0; i < ARRAY_SIZE(stages); i++) { @@ -113,6 +129,12 @@ static void guest_code(void) kvm_hypercall_map_private(gpa, size); memset((void *)gpa, p2, size); + /* + * Host should not be able to read the values written to private + * memory + */ + REQUEST_HOST_R_PRIVATE(gpa, size, p2); + /* * Verify that the private memory was set to pattern two, and * that shared memory still holds the initial pattern. @@ -133,11 +155,20 @@ static void guest_code(void) continue; kvm_hypercall_map_shared(gpa + j, PAGE_SIZE); - GUEST_SYNC4(gpa + j, PAGE_SIZE, p1, p3); + REQUEST_HOST_RW_SHARED(gpa + j, PAGE_SIZE, p1, p3); memcmp_g(gpa + j, p3, PAGE_SIZE); } + /* + * Even-number pages are still mapped as private, host should + * not be able to read those values. + */ + for (j = 0; j < size; j += PAGE_SIZE) { + if (!((j >> PAGE_SHIFT) & 1)) + REQUEST_HOST_R_PRIVATE(gpa + j, PAGE_SIZE, p2); + } + /* * Convert the entire region back to shared, explicitly write * pattern three to fill in the even-number frames before @@ -145,7 +176,7 @@ static void guest_code(void) */ kvm_hypercall_map_shared(gpa, size); memset((void *)gpa, p3, size); - GUEST_SYNC4(gpa, size, p3, p4); + REQUEST_HOST_RW_SHARED(gpa, size, p3, p4); memcmp_g(gpa, p4, size); /* Reset the shared memory back to the initial pattern. */ @@ -209,7 +240,18 @@ static void test_mem_conversions(enum vm_mem_backing_src_type src_type) switch (get_ucall(vcpu, &uc)) { case UCALL_ABORT: REPORT_GUEST_ASSERT_4(uc, "%lx %lx %lx %lx"); - case UCALL_SYNC: { + case UCALL_R_PRIVATE: { + uint8_t *hva = addr_gpa2hva(vm, uc.args[0]); + uint64_t size = uc.args[1]; + + /* + * Try to read hva for private gpa from host, should not + * be able to read private data + */ + memcmp_ne_h(hva, uc.args[2], size); + break; + } + case UCALL_RW_SHARED: { uint8_t *hva = addr_gpa2hva(vm, uc.args[0]); uint64_t size = uc.args[1]; From patchwork Thu Mar 16 00:30:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 70523 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp204442wrt; Wed, 15 Mar 2023 17:34:04 -0700 (PDT) X-Google-Smtp-Source: AK7set92nj6fl0KinwFSLFwmfSTZaGvQNbn39ZWjrRBxm0ca4BYRYu/VZQplmLfnIKI35fYBlNRq X-Received: by 2002:a17:903:1206:b0:19e:e39b:6da2 with SMTP id l6-20020a170903120600b0019ee39b6da2mr1644062plh.29.1678926844587; Wed, 15 Mar 2023 17:34:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678926844; cv=none; d=google.com; s=arc-20160816; b=UikcfL75KEnSf4iQ4DdjnsZlrN6GiLa7dXoR+6Nx8LMwvHpM7y41ilYBhnfHtq4kLV jW9eorvoq6iNExRzD3R6mzsUzWkQOloFd1ZJsezxI8o6mUm5JAhesEtfdjnYlaI/ZgHx aClxcgJmi/SQc+7LK5t+6b94A+/6GSr3/1ZXjPXNzm+TXr5M6Tt9DJ0Jtq0v1haPqn7Z 8uIqig/Dj2upUIstEIlDew9eLcqiLslx0qX3ZFHiaIYRTtoARCCLf9fF8/uKOZg8AGoF aeSUzvIZBwS55wwDsAJ+1Ca6YGL93x/i1sGJJLfOV19gIjIoJit2XqV97HS8akRHzJmH fE8g== 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=/WhAZz5TZYbz5eACM2xmX0O35zR+4WA+K6fPnqmTdp0=; b=vtFDzoFPj20OgwNvOASydLvd36ZNl6aHxZoqxLOOYQbQ5Uee9zoAmUrG3BNgRxgWlV 3wJiZMPl6IWqY9EBw67tdabgRw+LKsVvqPPu96XB4q3PmuQrnrjaBcryAGuo5vyEZ5Y+ i/ImVm/7orZK0VGfyPALwSC/mjGLLuww/vRuLXy73ih7s4rip2+E+3Tkny7QmzKA6f4b c+esj1oaTmsjuTNK0UYJEskGI0mdECRsjkhwUm3JD7LPoAlHc3bYv7B1kvQvUvIlA61Q ogo6cDZNxCzZdp1NSfNxsGHd0fp+wicS61xut4a2XhiIaj++csE2+3OqtXqsDBCzvraL leow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=DFkZM4Wx; 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 k22-20020a170902761600b001a055252e3csi6240214pll.314.2023.03.15.17.33.49; Wed, 15 Mar 2023 17:34:04 -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=DFkZM4Wx; 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 S233355AbjCPAcI (ORCPT + 99 others); Wed, 15 Mar 2023 20:32:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35018 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233221AbjCPAbq (ORCPT ); Wed, 15 Mar 2023 20:31:46 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90D26A42DF for ; Wed, 15 Mar 2023 17:31:23 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id e129-20020a251e87000000b00b56598237f5so94626ybe.16 for ; Wed, 15 Mar 2023 17:31:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678926682; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=/WhAZz5TZYbz5eACM2xmX0O35zR+4WA+K6fPnqmTdp0=; b=DFkZM4WxAkVVNIJBuBzB0nwyIoWMqXfdjlRk3WXV6Jxw5x54jqdNlx617tRGFPhNbf FSFEhLgx9YrnXMD2N9F3ND0dGS2wEQLQ6W2DinnGY36gb1WqdxpuV00Tng2C80Dv85bd GGCXZZnPgvalLdYHdJ5gJafblCxeiqDbyy5BB57SEGPtzpj8zUdhi4l6LoIgVVGg05nZ 6Mo31eLAizgeZ7625k2PZoVDucHfhEkPNcn7OmHS1vFJrTg1pI8T6QFyePo8eh9L2ANf ioVmITbi3yeESUanrzgTRmSVNxGVFuNzX/w7x5gA4sylAMhIN1UM2xnxuIntZGawA78b lh/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678926682; 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=/WhAZz5TZYbz5eACM2xmX0O35zR+4WA+K6fPnqmTdp0=; b=sEfBFHDJBHcTtW2L8rmDtAfYeff9kUzAALNAjMCfMnbAnkHF/Db+jjR07pdjRUQiBH 4WPUyNZIB/RXUDgEAisJ0s5/nZRGctpujidjpKArE1hlGWZt2TxmOtuBwi8dOQW9tXku jmG4IyM4JUpFfKQwPHKRNQTpY5ulNosVX/JgYNVxUV8vMkdyXwEG0oPZvC8Ankb2uFLH mokyGWEIXvefYoni4khsGA/jvlwrZtZNdwm6HQMPyvgoTjUNfQ70+Rk2El2O4wqxdSNe QgLWfi6SZX4CpXhdGiU8cL9/Iw70z1lmJYl/ZMWNllWNLaDpjy8YgjEOEfVpkMGH/Eda zGZw== X-Gm-Message-State: AO0yUKUl4RkAXguOXhP2qTLD/eLR/VM1iErYZbZ1jCeLqm7v1EH6s1oO gXK2CSd42HcziKkMGyYe8B2c2Jgc4due41J3aQ== X-Received: from ackerleytng-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1f5f]) (user=ackerleytng job=sendgmr) by 2002:a25:8b08:0:b0:b14:91e:4d19 with SMTP id i8-20020a258b08000000b00b14091e4d19mr20688255ybl.7.1678926681818; Wed, 15 Mar 2023 17:31:21 -0700 (PDT) Date: Thu, 16 Mar 2023 00:30:57 +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 04/10] KVM: selftests: Exercise restrictedmem allocation and truncation code after KVM invalidation code has been unbound 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?1760482395159796282?= X-GMAIL-MSGID: =?utf-8?q?1760482395159796282?= The kernel interfaces restrictedmem_bind and restrictedmem_unbind are used by KVM to bind/unbind kvm functions to restrictedmem's invalidate_start and invalidate_end callbacks. After the KVM VM is freed, the KVM functions should have been unbound from the restrictedmem_fd's callbacks. In this test, we exercise fallocate to back and unback memory using the restrictedmem fd, and we expect no problems (crashes) after the KVM functions have been unbound. Signed-off-by: Ackerley Tng --- .../kvm/x86_64/private_mem_conversions_test.c | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c b/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c index f2c1e4450b0e..7741916818db 100644 --- a/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c +++ b/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c @@ -203,6 +203,30 @@ static void handle_exit_hypercall(struct kvm_vcpu *vcpu) run->hypercall.ret = 0; } +static void test_invalidation_code_unbound(struct kvm_vm *vm) +{ + uint32_t fd; + uint64_t offset; + struct userspace_mem_region *region; + + region = memslot2region(vm, DATA_SLOT); + fd = region->region.restrictedmem_fd; + offset = region->region.restrictedmem_offset; + + kvm_vm_free(vm); + + /* + * At this point the KVM invalidation code should have been unbound from + * the vm. We do allocation and truncation to exercise the restrictedmem + * code. There should be no issues after the unbinding happens. + */ + if (fallocate(fd, 0, offset, DATA_SIZE)) + TEST_FAIL("Unexpected error in fallocate"); + if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, + offset, DATA_SIZE)) + TEST_FAIL("Unexpected error in fallocate"); +} + static void test_mem_conversions(enum vm_mem_backing_src_type src_type) { struct kvm_vcpu *vcpu; @@ -270,7 +294,7 @@ static void test_mem_conversions(enum vm_mem_backing_src_type src_type) } done: - kvm_vm_free(vm); + test_invalidation_code_unbound(vm); } int main(int argc, char *argv[]) From patchwork Thu Mar 16 00:30:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 70530 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp208501wrt; Wed, 15 Mar 2023 17:45:51 -0700 (PDT) X-Google-Smtp-Source: AK7set8ZEas+dA1+GE9S0xQx98SNie7ambk8ziuhCMeEO4ai4YL780CvADm+eR/L6sPJk66AY7OO X-Received: by 2002:a17:90b:1e4e:b0:237:aed6:fff3 with SMTP id pi14-20020a17090b1e4e00b00237aed6fff3mr1665222pjb.44.1678927551588; Wed, 15 Mar 2023 17:45:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678927551; cv=none; d=google.com; s=arc-20160816; b=izANr28dO62c79G1MnWXdE7mukfJRF/KPwHJMBSutJFv+wwdFxqufh74dG6zmAAVlI VXp2/dZdYA8JXepJtSWy6L+/iNt8BanHbhwvNTRyLjof+NM+yAoD60ILu4xkJmBZxpD4 HM8tSxPl91VQY4/tXOmYiyra5nOmcd9pcSDU/BqfePB4iebKNz46v0Cwg9vz7RppqusS U+d0R8SnabzfWXkpWlvz/ldLwH6x3GCGQimisVFLxB9tj9M4CGFkDSslPEXdnliDfCRb UnXVRqJCkRQ62PyZyAejgyVaQgJJj9iM3zmq+1DlWWDcblGxVCgzOuVk8NiudlNbOD9u x4jQ== 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=QTeAQsqCeHYohT1+z6QROKS290DmjN4VP9f8cpdpaL8=; b=p56MJr2xk1BbauKu7PHPsoNRsgHsT5lUO4qrFQhCnKQgl8IFS8BlcMjXSHt21FRhx1 uOvA4BvGSNBb0i8xl9F+iKlOKPk4AtVwx4m8IVit7YJzwB4IBUz0hn62roxipgD+kRVB efS1GCUx3G3X73/S61gRzy02u5yc931NVOs8obXdcdwtRrSZQDox253BM513ICo/CgLH oYxXmuFP6ZayPPRSpWTZq8W7KJrU8tMacgkMjr2bAODMkBc/qLNwegsdEKVhxYNc4Sdn 0zgjO/jO5a+yD5OaN11VCDKl7rcRPYifTUPcjVE0cArBO4cST6wb8siqZqYBw1M3Zo07 v4GQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=auUqsxuf; 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.45.38; Wed, 15 Mar 2023 17:45:51 -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=auUqsxuf; 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 S232754AbjCPAcL (ORCPT + 99 others); Wed, 15 Mar 2023 20:32:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34470 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233191AbjCPAbq (ORCPT ); Wed, 15 Mar 2023 20:31:46 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E6545A0290 for ; Wed, 15 Mar 2023 17:31:23 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id m6-20020a056902118600b00aeb1e3dbd1bso110809ybu.9 for ; Wed, 15 Mar 2023 17:31:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678926683; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=QTeAQsqCeHYohT1+z6QROKS290DmjN4VP9f8cpdpaL8=; b=auUqsxufnhZazK80BuGYrUuT5xQy0nKByTHsDVa5nQ8ggWedTTVRL+DG5jMn5NcxdQ 9zKUsKBxbo+88C8lp499Nf5DzFL8iXzFzdAJ86nKvZqnnh/lTpcH6nmuB0r+XxVUCw1B tWxQxyfQ1qbw8+rrtFLmZAs8p0Ck8ha4gQgZVZNu5KU3Thh3AQojPLOynQtyjzv1FzSr Kq4i+5eQv1x3JKSEQwwxH3yKT9WUMAu3uj+orG9AOM7aiFj0W9PrTlTTkoUWU15/M43G MNpDDQxOGa8+IAVafp0LeD+kSvzDoyTC20PrCP7Ia85PDwUpeCiqEyENM1ZDwu+6T7Px 76MQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678926683; 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=QTeAQsqCeHYohT1+z6QROKS290DmjN4VP9f8cpdpaL8=; b=uH8RF25Ty/IK4quTXZoX+MEadS+B8RKSDAk8MeuKDXEmcC+wDehf2AwRwlMHRlz376 DlfZrLCzXaxUX1VYhnvPOfj4OtV8O+qCZ9wWFThRSQkhPnqXHJewts0fA1kQvoEWVS8A HBO/GW+x83Go2KtnQrBCSwBNjWW+C9FatEwkClEA9JngSvIyo6W9ei4WH3x+e2Gz61bO SsWk0J2iwu0iF2xFBLXpJdgqrUE7ozDsTNJqeFmmR0RkW1Htbb7ahmDGnjrIzFYjpNTW 30mPbbNtfO+OAqYflSjQVSaxPav8f/gz8Ch3VrxzEd4fStpXU7qYUc0MuLffOzsr4A1h A+Wg== X-Gm-Message-State: AO0yUKUlH+gIdH0pAxkumIJMS1YikN39ySLplfvS0fSKLOYu/dv6ZbyT d1I8rEcmHWto5G97yrjPa8W92c5tMeKvqQGrbA== X-Received: from ackerleytng-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1f5f]) (user=ackerleytng job=sendgmr) by 2002:a81:e803:0:b0:541:7f49:adac with SMTP id a3-20020a81e803000000b005417f49adacmr1106841ywm.8.1678926683452; Wed, 15 Mar 2023 17:31:23 -0700 (PDT) Date: Thu, 16 Mar 2023 00:30:58 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.40.0.rc2.332.ga46443480c-goog Message-ID: <53c62631b481f5811340ef4fcbef511abd2171d7.1678926164.git.ackerleytng@google.com> Subject: [RFC PATCH 05/10] KVM: selftests: Generalize private_mem_conversions_test for parallel execution 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=ham 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?1760483136297699237?= X-GMAIL-MSGID: =?utf-8?q?1760483136297699237?= By running the private/shared memory conversion tests on multiple vCPUs in parallel, we stress-test the restrictedmem subsystem to test conversion of non-overlapping GPA ranges in multiple memslots. Signed-off-by: Ackerley Tng --- .../kvm/x86_64/private_mem_conversions_test.c | 203 +++++++++++++----- 1 file changed, 150 insertions(+), 53 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c b/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c index 7741916818db..14aa90e9a89b 100644 --- a/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c +++ b/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c @@ -5,6 +5,7 @@ #define _GNU_SOURCE /* for program_invocation_short_name */ #include #include +#include #include #include #include @@ -22,9 +23,10 @@ #include #include -#define DATA_SLOT 10 -#define DATA_GPA ((uint64_t)(1ull << 32)) -#define DATA_SIZE ((uint64_t)(SZ_2M + PAGE_SIZE)) +#define DATA_SLOT_BASE 10 +#define DATA_GPA_BASE ((uint64_t)(1ull << 32)) +#define DATA_SIZE ((uint64_t)(SZ_2M + PAGE_SIZE)) +#define DATA_GPA_SPACING DATA_SIZE /* Horrific macro so that the line info is captured accurately :-( */ #define memcmp_g(gpa, pattern, size) \ @@ -83,7 +85,9 @@ static void memcmp_ne_h(uint8_t *mem, uint8_t pattern, size_t size) #define REQUEST_HOST_R_PRIVATE(gpa, size, expected_pattern) \ ucall(UCALL_R_PRIVATE, 3, gpa, size, expected_pattern) -static void guest_code(void) +const uint8_t init_p = 0xcc; + +static void guest_test_conversions(uint64_t gpa_base) { struct { uint64_t offset; @@ -96,17 +100,11 @@ static void guest_code(void) GUEST_STAGE(PAGE_SIZE, SZ_2M), GUEST_STAGE(SZ_2M, PAGE_SIZE), }; - const uint8_t init_p = 0xcc; uint64_t j; int i; - /* Memory should be shared by default. */ - memset((void *)DATA_GPA, ~init_p, DATA_SIZE); - REQUEST_HOST_RW_SHARED(DATA_GPA, DATA_SIZE, ~init_p, init_p); - memcmp_g(DATA_GPA, init_p, DATA_SIZE); - for (i = 0; i < ARRAY_SIZE(stages); i++) { - uint64_t gpa = DATA_GPA + stages[i].offset; + uint64_t gpa = gpa_base + stages[i].offset; uint64_t size = stages[i].size; uint8_t p1 = 0x11; uint8_t p2 = 0x22; @@ -140,11 +138,11 @@ static void guest_code(void) * that shared memory still holds the initial pattern. */ memcmp_g(gpa, p2, size); - if (gpa > DATA_GPA) - memcmp_g(DATA_GPA, init_p, gpa - DATA_GPA); - if (gpa + size < DATA_GPA + DATA_SIZE) + if (gpa > gpa_base) + memcmp_g(gpa_base, init_p, gpa - gpa_base); + if (gpa + size < gpa_base + DATA_SIZE) memcmp_g(gpa + size, init_p, - (DATA_GPA + DATA_SIZE) - (gpa + size)); + (gpa_base + DATA_SIZE) - (gpa + size)); /* * Convert odd-number page frames back to shared to verify KVM @@ -182,6 +180,19 @@ static void guest_code(void) /* Reset the shared memory back to the initial pattern. */ memset((void *)gpa, init_p, size); } +} + +static void guest_code(uint64_t gpa_base, uint32_t iterations) +{ + int i; + + /* Memory should be shared by default. */ + memset((void *)gpa_base, ~init_p, DATA_SIZE); + REQUEST_HOST_RW_SHARED(gpa_base, DATA_SIZE, ~init_p, init_p); + memcmp_g(gpa_base, init_p, DATA_SIZE); + + for (i = 0; i < iterations; i++) + guest_test_conversions(gpa_base); GUEST_DONE(); } @@ -203,15 +214,27 @@ static void handle_exit_hypercall(struct kvm_vcpu *vcpu) run->hypercall.ret = 0; } -static void test_invalidation_code_unbound(struct kvm_vm *vm) +static uint64_t data_gpa_base_for_vcpu_id(uint8_t n) +{ + return DATA_GPA_BASE + n * DATA_GPA_SPACING; +} + +static void test_invalidation_code_unbound(struct kvm_vm *vm, uint8_t nr_memslots, + off_t data_size) { - uint32_t fd; - uint64_t offset; - struct userspace_mem_region *region; + struct { + uint32_t fd; + uint64_t offset; + } params[KVM_MAX_VCPUS]; + int i; + + for (i = 0; i < nr_memslots; i++) { + struct userspace_mem_region *region; - region = memslot2region(vm, DATA_SLOT); - fd = region->region.restrictedmem_fd; - offset = region->region.restrictedmem_offset; + region = memslot2region(vm, DATA_SLOT_BASE + i); + params[i].fd = region->region.restrictedmem_fd; + params[i].offset = region->region.restrictedmem_offset; + } kvm_vm_free(vm); @@ -220,33 +243,24 @@ static void test_invalidation_code_unbound(struct kvm_vm *vm) * the vm. We do allocation and truncation to exercise the restrictedmem * code. There should be no issues after the unbinding happens. */ - if (fallocate(fd, 0, offset, DATA_SIZE)) - TEST_FAIL("Unexpected error in fallocate"); - if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, - offset, DATA_SIZE)) - TEST_FAIL("Unexpected error in fallocate"); + for (i = 0; i < nr_memslots; i++) { + if (fallocate(params[i].fd, 0, params[i].offset, data_size)) + TEST_FAIL("Unexpected error in fallocate"); + if (fallocate(params[i].fd, + FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, + params[i].offset, data_size)) + TEST_FAIL("Unexpected error in fallocate"); + } + } -static void test_mem_conversions(enum vm_mem_backing_src_type src_type) +static void test_mem_conversions_for_vcpu(struct kvm_vm *vm, struct kvm_vcpu *vcpu, + uint32_t iterations) { - struct kvm_vcpu *vcpu; struct kvm_run *run; - struct kvm_vm *vm; struct ucall uc; - const struct vm_shape shape = { - .mode = VM_MODE_DEFAULT, - .type = KVM_X86_PROTECTED_VM, - }; - - vm = vm_create_shape_with_one_vcpu(shape, &vcpu, guest_code); - - vm_enable_cap(vm, KVM_CAP_EXIT_HYPERCALL, (1 << KVM_HC_MAP_GPA_RANGE)); - - vm_userspace_mem_region_add(vm, src_type, DATA_GPA, DATA_SLOT, - DATA_SIZE / vm->page_size, KVM_MEM_PRIVATE); - - virt_map(vm, DATA_GPA, DATA_GPA, DATA_SIZE / vm->page_size); + vcpu_args_set(vcpu, 2, data_gpa_base_for_vcpu_id(vcpu->id), iterations); run = vcpu->run; for ( ;; ) { @@ -287,40 +301,123 @@ static void test_mem_conversions(enum vm_mem_backing_src_type src_type) break; } case UCALL_DONE: - goto done; + return; default: TEST_FAIL("Unknown ucall 0x%lx.", uc.cmd); } } +} + +struct thread_args { + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + uint32_t iterations; +}; + +void *thread_function(void *input) +{ + struct thread_args *args = (struct thread_args *)input; + + test_mem_conversions_for_vcpu(args->vm, args->vcpu, args->iterations); + + return NULL; +} + +static void add_memslot_for_vcpu( + struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, uint8_t vcpu_id) +{ + uint64_t gpa = data_gpa_base_for_vcpu_id(vcpu_id); + uint32_t slot = DATA_SLOT_BASE + vcpu_id; + uint64_t npages = DATA_SIZE / vm->page_size; + + vm_userspace_mem_region_add(vm, src_type, gpa, slot, npages, + KVM_MEM_PRIVATE); +} + +static void test_mem_conversions(enum vm_mem_backing_src_type src_type, + uint8_t nr_vcpus, uint32_t iterations) +{ + struct kvm_vcpu *vcpus[KVM_MAX_VCPUS]; + pthread_t threads[KVM_MAX_VCPUS]; + struct thread_args args[KVM_MAX_VCPUS]; + struct kvm_vm *vm; + + int i; + int npages_for_all_vcpus; + + const struct vm_shape shape = { + .mode = VM_MODE_DEFAULT, + .type = KVM_X86_PROTECTED_VM, + }; + + vm = __vm_create_with_vcpus(shape, nr_vcpus, 0, guest_code, vcpus); + + vm_enable_cap(vm, KVM_CAP_EXIT_HYPERCALL, (1 << KVM_HC_MAP_GPA_RANGE)); + + npages_for_all_vcpus = DATA_SIZE / vm->page_size * nr_vcpus; + virt_map(vm, DATA_GPA_BASE, DATA_GPA_BASE, npages_for_all_vcpus); + + for (i = 0; i < nr_vcpus; i++) + add_memslot_for_vcpu(vm, src_type, i); + + for (i = 0; i < nr_vcpus; i++) { + args[i].vm = vm; + args[i].vcpu = vcpus[i]; + args[i].iterations = iterations; + + pthread_create(&threads[i], NULL, thread_function, &args[i]); + } + + for (i = 0; i < nr_vcpus; i++) + pthread_join(threads[i], NULL); + + test_invalidation_code_unbound(vm, nr_vcpus, DATA_SIZE); +} -done: - test_invalidation_code_unbound(vm); +static void usage(const char *command) +{ + puts(""); + printf("usage: %s [-h] [-s mem-type] [-n number-of-vcpus] [-i number-of-iterations]\n", + command); + puts(""); + backing_src_help("-s"); + puts(""); + puts(" -n: specify the number of vcpus to run memory conversion"); + puts(" tests in parallel on. (default: 2)"); + puts(""); + puts(" -i: specify the number iterations of memory conversion"); + puts(" tests to run. (default: 10)"); + puts(""); } int main(int argc, char *argv[]) { enum vm_mem_backing_src_type src_type = DEFAULT_VM_MEM_SRC; + uint8_t nr_vcpus = 2; + uint32_t iterations = 10; int opt; TEST_REQUIRE(kvm_has_cap(KVM_CAP_EXIT_HYPERCALL)); TEST_REQUIRE(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_PROTECTED_VM)); - while ((opt = getopt(argc, argv, "hs:")) != -1) { + while ((opt = getopt(argc, argv, "hs:n:i:")) != -1) { switch (opt) { + case 'n': + nr_vcpus = atoi_positive("nr_vcpus", optarg); + break; + case 'i': + iterations = atoi_positive("iterations", optarg); + break; case 's': src_type = parse_backing_src_type(optarg); break; case 'h': default: - puts(""); - printf("usage: %s [-h] [-s mem-type]\n", argv[0]); - puts(""); - backing_src_help("-s"); - puts(""); + usage(argv[0]); exit(0); } } - test_mem_conversions(src_type); + test_mem_conversions(src_type, nr_vcpus, iterations); return 0; } From patchwork Thu Mar 16 00:30:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 70524 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp204577wrt; Wed, 15 Mar 2023 17:34:30 -0700 (PDT) X-Google-Smtp-Source: AK7set+kB7qqfFGFDce7+zBpb1SjsDJHqrBaTbnvguAhh7tMWO7Dmq/BFtMkUANsPNjT4cXHZ1Fb X-Received: by 2002:a17:902:c94f:b0:19c:f80c:df90 with SMTP id i15-20020a170902c94f00b0019cf80cdf90mr1520881pla.45.1678926870099; Wed, 15 Mar 2023 17:34:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678926870; cv=none; d=google.com; s=arc-20160816; b=yu4w1WETGiLVxcTQpuhYNgzPkP0PDqcAnAKu68T18hV2bT7usXKiG2fBHtKIcDtXet /q2RXAYFIw7SqelbKQzrI8CpqP6dcR5NOL1zDLZmi334K9DQoTbIotQeGHyQul1EIkNJ hAAfhezT5pLK1a1MPyXqV9Lf/Frp4pXMbCUIuDc8KyMLyMSJ1K0x2yyxDtyFGBlRPAuW wNMWL8Z2LHR0yfiCsHmfI3N91oky3X3h9+7iSc1ONZ65JI5Ay774GwkG30NJxOep7QEt 0ML8zeaewGg1LFc9BUVFRDZBIAxbZxVzFh0d6Coao9tC7IckGtuxX3sZPIGHJSY2kYuT wh+Q== 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=36lklAKITNKcvpGi9bBaV0pg92rnpur43Kuea+YFpfM=; b=letzE3dI6KvBV5m/J0+1dEjJGKOhIcvMBwlfrYuUPmVQ3lrxWJ0peC/skPj75zm1Ev X/WgZ3bc0u96nf/ERdOwKSLoqpHmqefCN0LrGyfcasU00Nz3trM2XCBF/EmpIARAWu7w fYIbYPwJBb7b2cA/GC5WI2+Bf2vXBEbkoS6Om69yqaaIYMgPAyER7oGSCIOuQQe92GpW JdpU+VgZn/QNCzgvzw8/k1WYiBx0CjFpyD52tG4sS31HJVMqaYc9ggW/EUh/MSQqAn0A JFayJSwqf81E4HPrzY2Ch6nJkAjPWK+Djga/hyI6BPYNZx6B/JZ/ycjQCD9RhBusqrWi HgeA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=swpVj1Sm; 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 kv7-20020a17090328c700b001963a56658fsi6277433plb.105.2023.03.15.17.34.15; Wed, 15 Mar 2023 17:34:30 -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=swpVj1Sm; 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 S233253AbjCPAcl (ORCPT + 99 others); Wed, 15 Mar 2023 20:32:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233273AbjCPAcB (ORCPT ); Wed, 15 Mar 2023 20:32:01 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D85DA591D for ; Wed, 15 Mar 2023 17:31:27 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id u4-20020a170902bf4400b0019e30a57694so5199pls.20 for ; Wed, 15 Mar 2023 17:31:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678926685; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=36lklAKITNKcvpGi9bBaV0pg92rnpur43Kuea+YFpfM=; b=swpVj1SmNmgvWlTTZKcMha4lkIplKfRmILDkyWfcGRLXWLuIlK2d6RzDWw2bYkkCHP soiW4nXc7/hBbAleU8BqoI0AbG96N9laDULGWA+oYmBxb9+GZWi0B0T26gJnbwIFOCwo 5kpRG9JizdS1Y6EbQlta+wo8Fhd+ryKKJGTqSzbsLYaWB10pCI4jhMlJ9DGRJ4DSAtGk 3aahsJCHmb9Uk47t1vgWw/SYjxk2SvIyeXgaVgaNp1TPH4oC+vl3KgGYbWyD1TlOcBi1 gvD1hR4NtBHZOvW4SAlG3B1IpFX0JT8Zq0hcF2742mhE+c1PzdSmxBYs2Pwk5wn4zjuL RaVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678926685; 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=36lklAKITNKcvpGi9bBaV0pg92rnpur43Kuea+YFpfM=; b=4M/o9ThNvJSHGBPMI8ZjmGoVJgv/8Zg6vL0kxUIUvYVVtOiRH7vTSw076fbdT5mHQd 2YglH9iqZ0dOtsVCm6B94BNDz9L+f54CuS5hmuanI9l5NLlmfc6yWYLiAErHOaGqHtkU XJOE0RF8sQE7BNCfz54YXCXoVaiECXtXK/VhblU9+2nC8u8WryoPX+KioJuox9ZRnG5V q0gs6swvV7Lm38HD5pWUm+7GiQPMiplh241rN+hIB1Czk2ASS64LUVC8B/wvqp2FHsKX o0vxR58mnNXabLEjtfK9WQ02aufhhVWK0XbUfRJYCdL8FpxTLwCQqQm+jF4VQeEbVKBC /n6A== X-Gm-Message-State: AO0yUKWQpFi/HzsEaEP20F8HCwcqaaHXjNW/fHdE2I47tdh3RDhJnOHN DL8EcoHdqrhst7jTgFM3F+cfIes7jJhfha9COw== X-Received: from ackerleytng-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1f5f]) (user=ackerleytng job=sendgmr) by 2002:a17:902:d50d:b0:1a0:5402:b17f with SMTP id b13-20020a170902d50d00b001a05402b17fmr634600plg.6.1678926685342; Wed, 15 Mar 2023 17:31:25 -0700 (PDT) Date: Thu, 16 Mar 2023 00:30:59 +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 06/10] KVM: selftests: Default private_mem_conversions_test to use 1 memslot for test data 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?1760482421782164390?= X-GMAIL-MSGID: =?utf-8?q?1760482421782164390?= Default the private/shared memory conversion tests to use a single memslot, while executing on multiple vCPUs in parallel, to stress-test the restrictedmem subsystem. Also add a flag to allow multiple memslots to be used. Signed-off-by: Ackerley Tng --- .../kvm/x86_64/private_mem_conversions_test.c | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c b/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c index 14aa90e9a89b..afaf8d0e52e6 100644 --- a/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c +++ b/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c @@ -335,7 +335,8 @@ static void add_memslot_for_vcpu( } static void test_mem_conversions(enum vm_mem_backing_src_type src_type, - uint8_t nr_vcpus, uint32_t iterations) + uint8_t nr_vcpus, uint32_t iterations, + bool use_multiple_memslots) { struct kvm_vcpu *vcpus[KVM_MAX_VCPUS]; pthread_t threads[KVM_MAX_VCPUS]; @@ -355,6 +356,16 @@ static void test_mem_conversions(enum vm_mem_backing_src_type src_type, vm_enable_cap(vm, KVM_CAP_EXIT_HYPERCALL, (1 << KVM_HC_MAP_GPA_RANGE)); npages_for_all_vcpus = DATA_SIZE / vm->page_size * nr_vcpus; + + if (use_multiple_memslots) { + for (i = 0; i < nr_vcpus; i++) + add_memslot_for_vcpu(vm, src_type, i); + } else { + vm_userspace_mem_region_add( + vm, src_type, DATA_GPA_BASE, DATA_SLOT_BASE, + npages_for_all_vcpus, KVM_MEM_PRIVATE); + } + virt_map(vm, DATA_GPA_BASE, DATA_GPA_BASE, npages_for_all_vcpus); for (i = 0; i < nr_vcpus; i++) @@ -371,13 +382,16 @@ static void test_mem_conversions(enum vm_mem_backing_src_type src_type, for (i = 0; i < nr_vcpus; i++) pthread_join(threads[i], NULL); - test_invalidation_code_unbound(vm, nr_vcpus, DATA_SIZE); + if (!use_multiple_memslots) + test_invalidation_code_unbound(vm, 1, DATA_SIZE * nr_vcpus); + else + test_invalidation_code_unbound(vm, nr_vcpus, DATA_SIZE); } static void usage(const char *command) { puts(""); - printf("usage: %s [-h] [-s mem-type] [-n number-of-vcpus] [-i number-of-iterations]\n", + printf("usage: %s [-h] [-m] [-s mem-type] [-n number-of-vcpus] [-i number-of-iterations]\n", command); puts(""); backing_src_help("-s"); @@ -388,6 +402,8 @@ static void usage(const char *command) puts(" -i: specify the number iterations of memory conversion"); puts(" tests to run. (default: 10)"); puts(""); + puts(" -m: use multiple memslots (default: use 1 memslot)"); + puts(""); } int main(int argc, char *argv[]) @@ -395,12 +411,13 @@ int main(int argc, char *argv[]) enum vm_mem_backing_src_type src_type = DEFAULT_VM_MEM_SRC; uint8_t nr_vcpus = 2; uint32_t iterations = 10; + bool use_multiple_memslots = false; int opt; TEST_REQUIRE(kvm_has_cap(KVM_CAP_EXIT_HYPERCALL)); TEST_REQUIRE(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_PROTECTED_VM)); - while ((opt = getopt(argc, argv, "hs:n:i:")) != -1) { + while ((opt = getopt(argc, argv, "mhs:n:i:")) != -1) { switch (opt) { case 'n': nr_vcpus = atoi_positive("nr_vcpus", optarg); @@ -411,6 +428,9 @@ int main(int argc, char *argv[]) case 's': src_type = parse_backing_src_type(optarg); break; + case 'm': + use_multiple_memslots = true; + break; case 'h': default: usage(argv[0]); @@ -418,6 +438,6 @@ int main(int argc, char *argv[]) } } - test_mem_conversions(src_type, nr_vcpus, iterations); + test_mem_conversions(src_type, nr_vcpus, iterations, use_multiple_memslots); return 0; } From patchwork Thu Mar 16 00:31:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 70527 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp204997wrt; Wed, 15 Mar 2023 17:35:48 -0700 (PDT) X-Google-Smtp-Source: AK7set85a/YMjBhJY5/6cOdOcONuzkhgBs/du3WnfWHGxkzmaf6DpftODWiC7fUIujHVQKqTR3qK X-Received: by 2002:a17:903:246:b0:19e:bad3:4c6 with SMTP id j6-20020a170903024600b0019ebad304c6mr1472084plh.58.1678926948311; Wed, 15 Mar 2023 17:35:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678926948; cv=none; d=google.com; s=arc-20160816; b=q3XZGdmnpqizkWwvORgkYMDL65lwJ/wRbkSVM1KnQK85PCeDEa/fUKG2jthPnwrVTn GKIC76mFtNjbAQkIV24+DfHaQqNpFEaEE2ivZjlpeVXaaiIN1UKy/DXVwld092C41XNI 9RxN7EPLxo1ZaqWtmW4CiDGvO4e5XX8yg5Q6lsFrrtJ7ThQdSd7oK4GZCmgeJmd0WkfU j+GbixXFPl1dc59DFaYuOEO1UTONYkpDJQxblOpjFNkx84epATsNloeGY7mvk5yAyYoA 5JQBEUQ5LSUJYtN56qgFj/yvNSJ5vqPIGX7oAyjEiWKnOJxnUgngAPbYHKCHGBzD1yO/ AAJA== 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=JBLPG3XthGG9faiSvk5U/ET6qjyhuWi/SwHOth1Ucac=; b=qpiztYSezSryGtUNL1qYZKkhqrXXAbekoTAtzUpbV0COuF9beFhjUixRCBLJBha9tA zpf8TL7oiWTqR/hw3IKq6pUANe/Sp5fAjC5MFHK19F20hiSXmnf/nQ3YiUrqKGrXP0S2 PPtUsiQyd98OJkkmYgKMUZmE3JIeyRbpOIA0YsYzrW2L5M2R8aq0HQIvNNPfNqw1Vg3V EvWI+ozTeBYAfB9Bzwo4sHt+NxaAkCv1M6LDWB5VOMqYy9odr/i3LXMIY1PJgkCMJaLL +GQDyijZiw+pqujfOAgwKF14myE0X6zDrqprOto1ZNv7aHgfHdkRzdRgbVdegddK1dV+ KyAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=I+hk1kHX; 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 h15-20020a170902f54f00b0019a9c0e9adasi6901773plf.278.2023.03.15.17.35.33; Wed, 15 Mar 2023 17:35:48 -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=I+hk1kHX; 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 S233288AbjCPAcp (ORCPT + 99 others); Wed, 15 Mar 2023 20:32:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233277AbjCPAcB (ORCPT ); Wed, 15 Mar 2023 20:32:01 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 107B6A5682 for ; Wed, 15 Mar 2023 17:31:27 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id nm11-20020a17090b19cb00b0023d0c90d851so71625pjb.1 for ; Wed, 15 Mar 2023 17:31:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678926687; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=JBLPG3XthGG9faiSvk5U/ET6qjyhuWi/SwHOth1Ucac=; b=I+hk1kHXyTdyRHWR/E+Fe4D/KjC/hhFWZULnWPbpGXImZDU13NH2r0CvjRWyfCgLxX /nkIHP6zbtD+kRkLZz1i9msvu36P2j1F1ed2x92tbTloVbSMlV2L5X7YmVhhKmZ4DqvM XpXOnOkjDklzkljzqZGV/y4Nh+8aXvLV39ccn20yIQi1+cuwUBgQwtL3GYk3OKYOFzt5 fGTKHpxrFE/fRCl2mKHPSZMCdRAxw4bOce1hR/tDvln+tyvmSAorwJz0NO2G3/AQeBf+ ctLv6vLizdn+6uzwfijXtM/b902mBW+WM3hgf9E7bWyK12V6rTdbKghkmJDtmACEkOkZ v8Yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678926687; 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=JBLPG3XthGG9faiSvk5U/ET6qjyhuWi/SwHOth1Ucac=; b=nEyb9V+EeLh8f/7/OxHmBYUKDDagbJO/0UunCrWWw6cyd0PO6IYAoQFrHRTWi0Fv7L eDhB2LTV1o2yzAwAIRjqA5YhmhxPCbVsFdW/C80RhRXfpy2i9bqYIdPb35/7z+lvXlQP jfpZHhNitWsBk6rnL2TGBuzuv5zOLhPBc7I4Bq5Sgg8A+GyVlbw+0VAPx0GIl833yxsU /Q1Lvh4zhLF3rjBGIdTvOBcs2aWWCnWfXVT7hDMbkP75lpKFLtedb/Bsby+/qhrl2B8K YF8NPLHLWhNmvyNOUFp1IjiSICDj1yvA1+++GIszvTqxP9kPNEcWC0sCz725cNlz3VI2 rmuA== X-Gm-Message-State: AO0yUKV+B3NbehltLA/i80dYPbUNa+Evri9Tfb4BuGVmCFhvNC2GNgsj 2wmuYzgYTHq3+TERSlNmSA81dLUjMAQmKoczww== X-Received: from ackerleytng-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1f5f]) (user=ackerleytng job=sendgmr) by 2002:a17:902:e749:b0:1a0:4aa3:3a9a with SMTP id p9-20020a170902e74900b001a04aa33a9amr581092plf.2.1678926686991; Wed, 15 Mar 2023 17:31:26 -0700 (PDT) Date: Thu, 16 Mar 2023 00:31:00 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.40.0.rc2.332.ga46443480c-goog Message-ID: <59ff32ad0aa9e3533a96064d1ae07aba11f55924.1678926164.git.ackerleytng@google.com> Subject: [RFC PATCH 07/10] KVM: selftests: Add vm_userspace_mem_region_add_with_restrictedmem 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?1760482503670379031?= X-GMAIL-MSGID: =?utf-8?q?1760482503670379031?= Provide new function to allow restrictedmem's fd and offset to be specified in selftests. No functional change intended to vm_userspace_mem_region_add. Signed-off-by: Ackerley Tng --- .../selftests/kvm/include/kvm_util_base.h | 4 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 46 +++++++++++++++++-- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index b6531a4063bb..c1ac82332ca4 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -486,6 +486,10 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, uint64_t guest_paddr, uint32_t slot, uint64_t npages, uint32_t flags); +void vm_userspace_mem_region_add_with_restrictedmem(struct kvm_vm *vm, + enum vm_mem_backing_src_type src_type, + uint64_t guest_paddr, uint32_t slot, uint64_t npages, + uint32_t flags, int restrictedmem_fd, uint64_t restrictedmem_offset); void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags); void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa); diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index d0e6b10f140f..d6bfcfc5cdea 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -898,6 +898,43 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, uint64_t guest_paddr, uint32_t slot, uint64_t npages, uint32_t flags) +{ + int restrictedmem_fd; + + restrictedmem_fd = flags & KVM_MEM_PRIVATE ? memfd_restricted(0) : 0; + vm_userspace_mem_region_add_with_restrictedmem( + vm, src_type, guest_paddr, slot, npages, flags, + restrictedmem_fd, 0); +} + +/* + * VM Userspace Memory Region Add With restrictedmem + * + * Input Args: + * vm - Virtual Machine + * src_type - Storage source for this region. + * NULL to use anonymous memory. + * guest_paddr - Starting guest physical address + * slot - KVM region slot + * npages - Number of physical pages + * flags - KVM memory region flags (e.g. KVM_MEM_LOG_DIRTY_PAGES) + * restrictedmem_fd - restrictedmem_fd for use with restrictedmem + * restrictedmem_offset - offset within restrictedmem_fd to be used + * + * Output Args: None + * + * Return: None + * + * Allocates a memory area of the number of pages specified by npages + * and maps it to the VM specified by vm, at a starting physical address + * given by guest_paddr. The region is created with a KVM region slot + * given by slot, which must be unique and < KVM_MEM_SLOTS_NUM. The + * region is created with the flags given by flags. + */ +void vm_userspace_mem_region_add_with_restrictedmem(struct kvm_vm *vm, + enum vm_mem_backing_src_type src_type, + uint64_t guest_paddr, uint32_t slot, uint64_t npages, + uint32_t flags, int restrictedmem_fd, uint64_t restrictedmem_offset) { int ret; struct userspace_mem_region *region; @@ -1011,8 +1048,8 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, region->backing_src_type = src_type; if (flags & KVM_MEM_PRIVATE) { - region->region.restrictedmem_fd = memfd_restricted(0); - region->region.restrictedmem_offset = 0; + region->region.restrictedmem_fd = restrictedmem_fd; + region->region.restrictedmem_offset = restrictedmem_offset; TEST_ASSERT(region->region.restrictedmem_fd >= 0, "Failed to create restricted memfd"); @@ -1030,10 +1067,11 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION2 IOCTL failed,\n" " rc: %i errno: %i\n" " slot: %u flags: 0x%x\n" - " guest_phys_addr: 0x%lx size: 0x%lx restricted fd: %d\n", + " guest_phys_addr: 0x%lx size: 0x%lx\n" + " restricted fd: %d restricted_offset: 0x%llx\n", ret, errno, slot, flags, guest_paddr, (uint64_t) region->region.memory_size, - region->region.restrictedmem_fd); + region->region.restrictedmem_fd, region->region.restrictedmem_offset); /* Add to quick lookup data structures */ vm_userspace_mem_region_gpa_insert(&vm->regions.gpa_tree, region); From patchwork Thu Mar 16 00:31:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 70528 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp207425wrt; Wed, 15 Mar 2023 17:42:44 -0700 (PDT) X-Google-Smtp-Source: AK7set/8I3+vSj/EJ7MVbptU07by31wzNRdYqCA7clUCfr681yXJZOGIMb1QsoaqJ+aJJFAn4pwZ X-Received: by 2002:aa7:940a:0:b0:625:65b0:474d with SMTP id x10-20020aa7940a000000b0062565b0474dmr1232674pfo.20.1678927364137; Wed, 15 Mar 2023 17:42:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678927364; cv=none; d=google.com; s=arc-20160816; b=FTsMgA8jYb+B8mIMs3Nj2f6cO7+yG/njFg8q2+irC53PNVNk7fc8UvK3DdRIcsFL0J fFdHnS5wV2n+d/w4RzHncQi03a6DM3GSfQMM0KjmhWcvoMRloBwXNKzQT7ekFTCZgygL XUndjrkxBfBH1r0O1QYuhCjXOCRlcm9PC2NwxwzaXzppbVXEJgDtfep6Wxq3eWig4wqE QEsCnJo3BTBUSg572pbVB2aYgFYL0qioX4uGlcAdd+mt4GJJJF2pXEYXSWIj0Wh8zW7B jZN1GwDweEL23pljKL3r61JWXe5aMDFR4farZzjsKCGzOGaHXsPFlycTpKUw3rSnjH+M cwTA== 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=K90xoVX+0RlzLAw3SfN8qTyLgxMNh2Rq4fRe4RbG85I=; b=DdSRj+XkSZGb3smC5hjzRu3+SxZ5VXEJ2jKAohsFF6uP1YBpLDVXourNk8tOHZ8Gxp D82JKHJayi0xIGl5C4aEaBWe+XWGflVLeBNlX6gFASpkPQ5NwWa9bzzqOfCiwHj5XEF8 7tSzRSdUZC+CQIgpogwU+7P0sV44AZTshKWQs33pezzg7ixD9ef8DUZM7JXbmwDEjAzK wno0Eh5VuaB2BK2ddDMbYSoRiZAly8etC1tJo0q2EUrWbzPL1JeGhePG4psH9DSo+Oek pqJUHYjs6cbk3gFvaH82HNoDDTKA+GHMVAJv4HQA2Ci6pjezp/c9NjhdcixXeDvTFzKx woAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=d8uc3IAp; 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 r20-20020a6560d4000000b00508caaf69besi6124428pgv.862.2023.03.15.17.42.31; Wed, 15 Mar 2023 17:42:44 -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=d8uc3IAp; 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 S233151AbjCPAc4 (ORCPT + 99 others); Wed, 15 Mar 2023 20:32:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34528 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233199AbjCPAcE (ORCPT ); Wed, 15 Mar 2023 20:32:04 -0400 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0BE65A616B for ; Wed, 15 Mar 2023 17:31:31 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id i15-20020aa78d8f000000b005edc307b103so166065pfr.17 for ; Wed, 15 Mar 2023 17:31:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678926688; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=K90xoVX+0RlzLAw3SfN8qTyLgxMNh2Rq4fRe4RbG85I=; b=d8uc3IApbYmz26P2pZurWd/mdMnPn+5Qfc7rz6kuRm/Rdx7nODA9CSqEzNbHzvos3x RveM5uzlBa4alqsey/A+ZQ+F9FkBIhKp+vzmydZ7x8yVdpJ1dFg58aZJtFtCTBrdqTeM jtglH5cHaz9xRzfcMN6bC6InRMKXNsMGC/lp63mFpBGrilMy6mUNN3C7pjzFUdepgkuU +cgfLXerxqml3OAS1MvZtFFYDjNKV6KkSDelOKwW/Ftb9Njw0KqVRUjV1fgWAjc/PHx5 vlmTiVMmyjWJKxE37zalK36nTXI7Daq2qNoskHBh/u2Z2VdJEZ3uNpydLhqA1aIDw3qB CSMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678926688; 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=K90xoVX+0RlzLAw3SfN8qTyLgxMNh2Rq4fRe4RbG85I=; b=wmD3Qata56jlMr9DiCwe0QqQmFe19PcV5sTgowm+R/a0zuECSBT3f13YgmPkk6Jn9f hsNJC3q5ipepZZiKXOWryOrGmMSCjBU1MO9rFrNruMEe0QH03l28JQrcszdbhsyKXc7L zXV92iVmUaqJ6Vi4rO5QSUYPr75Edhr8Lc7JnllQbC0Z9wBL7rrhYOVqwwM4lRueDyQZ qKM3JWlJ1agl23g3kufss3qkaLwFyd/z+8ktAf/6ilra+YOpTxVlJmmZYe97ao3C0bUS PKfLkhYMiebyRCSlJoa2WpPXZdom+dMjUebrtNM28NZbGbzCxBqRKSswwWz9qLggD34k lsOw== X-Gm-Message-State: AO0yUKU4qFZloBZH9lE29SyNa3dS6LmdC9hUZDZdTt2PSKUYRIT20Zoo dj0jnbCNs84nsCVubt7JhW+JzCZRVI2gsC+RqA== X-Received: from ackerleytng-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1f5f]) (user=ackerleytng job=sendgmr) by 2002:a05:6a00:198d:b0:5a9:9713:1dc with SMTP id d13-20020a056a00198d00b005a9971301dcmr677531pfl.6.1678926688561; Wed, 15 Mar 2023 17:31:28 -0700 (PDT) Date: Thu, 16 Mar 2023 00:31:01 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.40.0.rc2.332.ga46443480c-goog Message-ID: <287d6e84dc788d84599392ca5d65864201f9a6a4.1678926164.git.ackerleytng@google.com> Subject: [RFC PATCH 08/10] KVM: selftests: Default private_mem_conversions_test to use 1 restrictedmem file for test data 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?1760482939822346712?= X-GMAIL-MSGID: =?utf-8?q?1760482939822346712?= Default the private/shared memory conversion tests to use a single file (when multiple memslots are requested), while executing on multiple vCPUs in parallel, to stress-test the restrictedmem subsystem. Also add a flag to allow multiple files to be used. Signed-off-by: Ackerley Tng --- .../kvm/x86_64/private_mem_conversions_test.c | 52 ++++++++++++++----- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c b/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c index afaf8d0e52e6..ca30f0f05c39 100644 --- a/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c +++ b/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c @@ -324,7 +324,8 @@ void *thread_function(void *input) } static void add_memslot_for_vcpu( - struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, uint8_t vcpu_id) + struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, uint8_t vcpu_id, + int restrictedmem_fd, uint64_t restrictedmem_offset) { uint64_t gpa = data_gpa_base_for_vcpu_id(vcpu_id); uint32_t slot = DATA_SLOT_BASE + vcpu_id; @@ -336,7 +337,8 @@ static void add_memslot_for_vcpu( static void test_mem_conversions(enum vm_mem_backing_src_type src_type, uint8_t nr_vcpus, uint32_t iterations, - bool use_multiple_memslots) + bool use_multiple_memslots, + bool use_different_restrictedmem_files) { struct kvm_vcpu *vcpus[KVM_MAX_VCPUS]; pthread_t threads[KVM_MAX_VCPUS]; @@ -356,21 +358,28 @@ static void test_mem_conversions(enum vm_mem_backing_src_type src_type, vm_enable_cap(vm, KVM_CAP_EXIT_HYPERCALL, (1 << KVM_HC_MAP_GPA_RANGE)); npages_for_all_vcpus = DATA_SIZE / vm->page_size * nr_vcpus; + virt_map(vm, DATA_GPA_BASE, DATA_GPA_BASE, npages_for_all_vcpus); if (use_multiple_memslots) { - for (i = 0; i < nr_vcpus; i++) - add_memslot_for_vcpu(vm, src_type, i); + int fd = memfd_restricted(0); + int offset = 0; + + for (i = 0; i < nr_vcpus; i++) { + if (use_different_restrictedmem_files) { + if (i > 0) + fd = memfd_restricted(0); + } else { + offset = i * DATA_GPA_SPACING; + } + + add_memslot_for_vcpu(vm, src_type, i, fd, offset); + } } else { vm_userspace_mem_region_add( vm, src_type, DATA_GPA_BASE, DATA_SLOT_BASE, npages_for_all_vcpus, KVM_MEM_PRIVATE); } - virt_map(vm, DATA_GPA_BASE, DATA_GPA_BASE, npages_for_all_vcpus); - - for (i = 0; i < nr_vcpus; i++) - add_memslot_for_vcpu(vm, src_type, i); - for (i = 0; i < nr_vcpus; i++) { args[i].vm = vm; args[i].vcpu = vcpus[i]; @@ -382,7 +391,7 @@ static void test_mem_conversions(enum vm_mem_backing_src_type src_type, for (i = 0; i < nr_vcpus; i++) pthread_join(threads[i], NULL); - if (!use_multiple_memslots) + if (!use_multiple_memslots || !use_different_restrictedmem_files) test_invalidation_code_unbound(vm, 1, DATA_SIZE * nr_vcpus); else test_invalidation_code_unbound(vm, nr_vcpus, DATA_SIZE); @@ -391,8 +400,9 @@ static void test_mem_conversions(enum vm_mem_backing_src_type src_type, static void usage(const char *command) { puts(""); - printf("usage: %s [-h] [-m] [-s mem-type] [-n number-of-vcpus] [-i number-of-iterations]\n", - command); + printf("usage: %s\n", command); + printf(" [-h] [-m] [-f] [-s mem-type]\n"); + printf(" [-n number-of-vcpus] [-i number-of-iterations]\n"); puts(""); backing_src_help("-s"); puts(""); @@ -404,6 +414,9 @@ static void usage(const char *command) puts(""); puts(" -m: use multiple memslots (default: use 1 memslot)"); puts(""); + puts(" -f: use different restrictedmem files for each memslot"); + puts(" (default: use 1 restrictedmem file for all memslots)"); + puts(""); } int main(int argc, char *argv[]) @@ -412,12 +425,13 @@ int main(int argc, char *argv[]) uint8_t nr_vcpus = 2; uint32_t iterations = 10; bool use_multiple_memslots = false; + bool use_different_restrictedmem_files = false; int opt; TEST_REQUIRE(kvm_has_cap(KVM_CAP_EXIT_HYPERCALL)); TEST_REQUIRE(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_PROTECTED_VM)); - while ((opt = getopt(argc, argv, "mhs:n:i:")) != -1) { + while ((opt = getopt(argc, argv, "fmhs:n:i:")) != -1) { switch (opt) { case 'n': nr_vcpus = atoi_positive("nr_vcpus", optarg); @@ -431,6 +445,9 @@ int main(int argc, char *argv[]) case 'm': use_multiple_memslots = true; break; + case 'f': + use_different_restrictedmem_files = true; + break; case 'h': default: usage(argv[0]); @@ -438,6 +455,13 @@ int main(int argc, char *argv[]) } } - test_mem_conversions(src_type, nr_vcpus, iterations, use_multiple_memslots); + if (!use_multiple_memslots && use_different_restrictedmem_files) { + printf("Overriding -f flag: "); + puts("Using just 1 restrictedmem file since only 1 memslot is to be used."); + use_different_restrictedmem_files = false; + } + + test_mem_conversions(src_type, nr_vcpus, iterations, use_multiple_memslots, + use_different_restrictedmem_files); return 0; } 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[]) From patchwork Thu Mar 16 00:31:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 70526 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp204946wrt; Wed, 15 Mar 2023 17:35:39 -0700 (PDT) X-Google-Smtp-Source: AK7set8/WHfnc7eMZ1qIm7GF2XUbfFRtlhw5D/r4rKUsb7odg4YZd4X1l53v0LBNdsUh+fw6bAuk X-Received: by 2002:a17:903:300c:b0:1a0:48c6:3b43 with SMTP id o12-20020a170903300c00b001a048c63b43mr1148414pla.37.1678926938993; Wed, 15 Mar 2023 17:35:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678926938; cv=none; d=google.com; s=arc-20160816; b=YSpFRmB8bYNJ18ymBjbmcyK7HPT3R5XS2d1orC5WJsGVZogBneyoHxBLk6CUJbc92h +d+dU520HOALG0AsFlZYVy3CAFjRX7uh4ERRY8Mhas/MpaO1aHrDk2XaDVVQgw8osOIm cbInOO0k+lDpgPZXUJSthx0F3rph5Og/bp+Ax2MU4rF/UkmfLr65hcIOHU32grIbZKhg VnOyBUG7iWhwm8nKpxdk3uOaFjusGdYfa4TqIfk+dtzYiMLbHMk/Ia9ZFQWgpzY44N5i SSRW6D5qo0itXKrlhVq42pstKS934ikqXDQlVngZRkHBgfPU3Mteg5c1eH3IbD5Ql4fQ gVoA== 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=YxU1hhaZ4SyfL9lxoIcMNBPPAq0yfPS0xuipesTDclo=; b=xeEObXi9ZcbyBa73arzmVhSgFPR1OAx0XdU1lNkA/9OW0TsIC14A+Qwqoqqfxq0Dj4 ADk3AXQvQ2kBLSuvs+9ydp9GdRoKaryaL53jdc2xUGyHd2J+qv7HkhjDQy0D3hn/cknn WsRhz+qXjuBjE+fjN/fFxxOjvNQFZQn2qXSEuH8JOE2Rcu/QeXk2FGPm8yhUtZBJx7Zu hWzASqnChP2UMDBb5bVl+QZIgmDV0TAMUjs8zSY+KEdHz3iadCcqYmU+9W3aoq5TRzMw j1f3pqVYiXGV9Iw0nAruh/mN4VSeBNamyAB45wh5Zqdjo5hlxbvTNeCMgPeCXBKprATn uWZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=W0G3AmVB; 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 w8-20020a634908000000b004fb98a13f0fsi6264469pga.144.2023.03.15.17.35.24; Wed, 15 Mar 2023 17:35:38 -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=W0G3AmVB; 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 S233155AbjCPAdY (ORCPT + 99 others); Wed, 15 Mar 2023 20:33:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233360AbjCPAcO (ORCPT ); Wed, 15 Mar 2023 20:32:14 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD2ADA674D for ; Wed, 15 Mar 2023 17:31:38 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id m12-20020a6562cc000000b0050bdfabc8e2so15879pgv.9 for ; Wed, 15 Mar 2023 17:31:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678926692; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=YxU1hhaZ4SyfL9lxoIcMNBPPAq0yfPS0xuipesTDclo=; b=W0G3AmVBhmaEPW1mzeOzfktEstHk/ayT4Fca4WpYB+uqnQXqFONecZpHfNEM3EVkI8 dMoU+aBolGRnKrvqkSWbCeJFkilUlbcTqWcVH0y2O7kmHEk9EOK0BEV0KFKuPFrnCkbs QOotzJYDWHJggJes5/KIvM96osSnaCvae3qKB3LUHRBB+uNwEOmmHmd8zvTbRGoEVjUL rVFdyBsTEprlo4ciiWa3+M8yKhD16Tn+oY0bqfI0h7Rz5RgpgT08FQynab2lujb51lp7 x7X7TlFctPEGFqxyshGyufJ1XL1Zen+FPj5jvwOG/5BAnYWpY1i72h9z5SjlNs7jYsNa hwdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678926692; 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=YxU1hhaZ4SyfL9lxoIcMNBPPAq0yfPS0xuipesTDclo=; b=3DJWWf5lMBnEMgy89i61Z9hmuIbd521QAZ8aD2CqMCYwwMCzgrAUjn8UxC+e9NGrKT uM8srS6Pn7ujXhHDGZXV/qRqVV2O6wvgPmpTMSeyUGwnQXze4Vo0wF9v4coGjvE1l993 XYxXMxqVgsXjDoFaXcEtZcllOgZtkWb/VJ0zJrZadyGw/BQjds8Pfk4sokvSbsPgD6wR r6Bu2Ut3EcmRSsa66PolmyVmtof+zN8Y/O/ytumcgNyh22Qqft9xRgx+7VXpB/93Omxq PFmvEhbgylq9Dx2i6wtMmUV2603Oq4jAOuy2L3rsKbcCfVQVpqZil6MB+1oiYwdZbI7j nodQ== X-Gm-Message-State: AO0yUKVCDMq0Xe4OL/C56k4aufbhhKIHNJe59cxbk9AYVc5n22duFmp5 +hUnpQPiYNu/oSGTsTd54wjJdlinY5It8r5U4Q== X-Received: from ackerleytng-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1f5f]) (user=ackerleytng job=sendgmr) by 2002:a65:530d:0:b0:502:fd12:83ce with SMTP id m13-20020a65530d000000b00502fd1283cemr347381pgq.5.1678926691968; Wed, 15 Mar 2023 17:31:31 -0700 (PDT) Date: Thu, 16 Mar 2023 00:31:03 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.40.0.rc2.332.ga46443480c-goog Message-ID: <90aaa4ab85fa5e3d5641793e2a4873282eb16556.1678926164.git.ackerleytng@google.com> Subject: [RFC PATCH 10/10] KVM: selftests: Test KVM exit behavior for private memory/access 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?1760482493960458123?= X-GMAIL-MSGID: =?utf-8?q?1760482493960458123?= "Testing private access when memslot gets deleted" tests the behavior of KVM when a private memslot gets deleted while the VM is using the private memslot. When KVM looks up the deleted (slot = NULL) memslot, KVM should exit to userspace with KVM_EXIT_MEMORY_FAULT. In the second test, upon a private access to non-private memslot, KVM should also exit to userspace with KVM_EXIT_MEMORY_FAULT. Signed-off-by: Ackerley Tng --- tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/private_mem_kvm_exits_test.c | 124 ++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/private_mem_kvm_exits_test.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index bafee3c43b2e..0ad588852a1d 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -80,6 +80,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/nested_exceptions_test TEST_GEN_PROGS_x86_64 += x86_64/platform_info_test TEST_GEN_PROGS_x86_64 += x86_64/pmu_event_filter_test TEST_GEN_PROGS_x86_64 += x86_64/private_mem_conversions_test +TEST_GEN_PROGS_x86_64 += x86_64/private_mem_kvm_exits_test TEST_GEN_PROGS_x86_64 += x86_64/set_boot_cpu_id TEST_GEN_PROGS_x86_64 += x86_64/set_sregs_test TEST_GEN_PROGS_x86_64 += x86_64/smaller_maxphyaddr_emulation_test diff --git a/tools/testing/selftests/kvm/x86_64/private_mem_kvm_exits_test.c b/tools/testing/selftests/kvm/x86_64/private_mem_kvm_exits_test.c new file mode 100644 index 000000000000..c8667dfbbf0a --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/private_mem_kvm_exits_test.c @@ -0,0 +1,124 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2022, Google LLC. + */ +#include "kvm_util_base.h" +#include +#include +#include +#include "kvm_util.h" +#include "processor.h" +#include "test_util.h" + +/* Arbitrarily selected to avoid overlaps with anything else */ +#define EXITS_TEST_GVA 0xc0000000 +#define EXITS_TEST_GPA EXITS_TEST_GVA +#define EXITS_TEST_NPAGES 1 +#define EXITS_TEST_SIZE (EXITS_TEST_NPAGES * PAGE_SIZE) +#define EXITS_TEST_SLOT 10 + +static uint64_t guest_repeatedly_read(void) +{ + volatile uint64_t value; + + while (true) + value = *((uint64_t *) EXITS_TEST_GVA); + + return value; +} + +static uint32_t run_vcpu_get_exit_reason(struct kvm_vcpu *vcpu) +{ + vcpu_run(vcpu); + + return vcpu->run->exit_reason; +} + +const struct vm_shape protected_vm_shape = { + .mode = VM_MODE_DEFAULT, + .type = KVM_X86_PROTECTED_VM, +}; + +static void test_private_access_memslot_deleted(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + pthread_t vm_thread; + void *thread_return; + uint32_t exit_reason; + + vm = vm_create_shape_with_one_vcpu(protected_vm_shape, &vcpu, + guest_repeatedly_read); + + vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, + EXITS_TEST_GPA, EXITS_TEST_SLOT, + EXITS_TEST_NPAGES, + KVM_MEM_PRIVATE); + + virt_map(vm, EXITS_TEST_GVA, EXITS_TEST_GPA, EXITS_TEST_NPAGES); + + /* Request to access page privately */ + vm_mem_map_shared_or_private(vm, EXITS_TEST_GPA, EXITS_TEST_SIZE, false); + + pr_info("Testing private access when memslot gets deleted\n"); + + pthread_create(&vm_thread, NULL, + (void *(*)(void *))run_vcpu_get_exit_reason, + (void *)vcpu); + + vm_mem_region_delete(vm, EXITS_TEST_SLOT); + + pthread_join(vm_thread, &thread_return); + exit_reason = (uint32_t)(uint64_t)thread_return; + + ASSERT_EQ(exit_reason, KVM_EXIT_MEMORY_FAULT); + ASSERT_EQ(vcpu->run->memory.flags, KVM_MEMORY_EXIT_FLAG_PRIVATE); + ASSERT_EQ(vcpu->run->memory.gpa, EXITS_TEST_GPA); + ASSERT_EQ(vcpu->run->memory.size, EXITS_TEST_SIZE); + + pr_info("\t ... PASSED\n"); + + kvm_vm_free(vm); +} + +static void test_private_access_memslot_not_private(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + uint32_t exit_reason; + + vm = vm_create_shape_with_one_vcpu(protected_vm_shape, &vcpu, + guest_repeatedly_read); + + /* Add a non-private memslot (flags = 0) */ + vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, + EXITS_TEST_GPA, EXITS_TEST_SLOT, + EXITS_TEST_NPAGES, 0); + + virt_map(vm, EXITS_TEST_GVA, EXITS_TEST_GPA, EXITS_TEST_NPAGES); + + /* Request to access page privately */ + vm_set_memory_attributes(vm, EXITS_TEST_GPA, EXITS_TEST_SIZE, + KVM_MEMORY_ATTRIBUTE_PRIVATE); + + pr_info("Testing private access to non-private memslot\n"); + + exit_reason = run_vcpu_get_exit_reason(vcpu); + + ASSERT_EQ(exit_reason, KVM_EXIT_MEMORY_FAULT); + ASSERT_EQ(vcpu->run->memory.flags, KVM_MEMORY_EXIT_FLAG_PRIVATE); + ASSERT_EQ(vcpu->run->memory.gpa, EXITS_TEST_GPA); + ASSERT_EQ(vcpu->run->memory.size, EXITS_TEST_SIZE); + + pr_info("\t ... PASSED\n"); + + kvm_vm_free(vm); +} + +int main(int argc, char *argv[]) +{ + TEST_REQUIRE(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_PROTECTED_VM)); + + test_private_access_memslot_deleted(); + test_private_access_memslot_not_private(); +}