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: 13176841 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0B7EAC76195 for ; Thu, 16 Mar 2023 00:31:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232383AbjCPAbc (ORCPT ); Wed, 15 Mar 2023 20:31:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233094AbjCPAb2 (ORCPT ); Wed, 15 Mar 2023 20:31:28 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DAA32A0B3B for ; Wed, 15 Mar 2023 17:31:18 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id z31-20020a25a122000000b00b38d2b9a2e9so126739ybh.3 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=EM+jC/Rkp+7MlrhxK9ZX/VWCEH6cwKhP9knlxoOsZK2KLxZ9JO+fR8xHAb1f30z/zL /kTI/LTzpPwA33T+HRG8EHahXBhum5ZfgEXL556bwCekDFvDT3YLiQKnCu7a/DsOGadN zZlisY2c8aoSUntm3fz2Cq4FT8+KkPfbKxZ5PLkn3DpFknGO5zAR9QdeG4siFg2rmvjm tHNRmlCKXxzUPBjH/UE+fKvocb3LOYftBSPMoJ1UWPTHrbi2YBdrimQRlsoZYad/LS8a Y5LCFzRqyXHSVeY+X7DkftJnMvvl69blhTSc1dobOUGRdSm5PH5MvFtP9pfTY3mqPr7+ ZMjg== X-Gm-Message-State: AO0yUKUkN2ypTuM78jFiIddM86xjaSsJTO5M7LBYC8GeERZqUiFFCyC/ /6SMdeW2uGddt02apWNWKAGiIHp43ER/kkVxbg== X-Google-Smtp-Source: AK7set+ivCVtWOdQbcZQUI9O6fTh7qxu3KKcJWd7M8I6rNN0cLeTsP1prymLMADMTsLDtTPgMIN1PCmQzWdKXvBTyw== 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 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org 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: 13176842 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 65D7EC6FD1D for ; Thu, 16 Mar 2023 00:31:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233170AbjCPAbu (ORCPT ); Wed, 15 Mar 2023 20:31:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231972AbjCPAba (ORCPT ); Wed, 15 Mar 2023 20:31:30 -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 3F9DF9EF6D for ; Wed, 15 Mar 2023 17:31:19 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id p39-20020a056a0026e700b0062315c420d6so161661pfw.21 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=1678926679; 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=EA4KfJjpxzMBA8ocOUJxskwPBqMOEPIbHdsbcZbccXhBPZcwGYtMOllXq3ZDv1ORrB QiGXSeCrawIV7Ob35Ad38AzhJQ6BmfNPeqo9bO2jMmgbTKuCwOKW52r8qmivCl54zpk4 88+8R8AG50cALhrkX6rRkPAgkUbqtisQ5dY73WaxL3k9+gqFrwu9ZnTOHigxZwCRoFw5 oEoLsQ0vn6DI5C8JkbS3LjnqPEppdpTWdzwEEBPNvCCLSrBJZbkjPpf2Wve4VtPR3n5N YKB4fD3PfGLhtkzjKoNrJ8iamdLexrkhHgeNyx0cQIIYAFnZZoWRNc9PQUU2ONHo7FAf gprw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678926679; 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=FyYaJMwZsmlEo7WJ5jd/lzDTN6cQxq5F3Y2vyQ0r01OqK0QsoeNmuuQt8Xd8qdfggs lHUXwr5yx90kbGpscPdHrrQabW490UBiZwVPI/QKQWt1Y1d4MCI7aBTCQdsB1yv9umvz Q8+8VnUhPJBe8QWOexQFC85RJjBvOtjHsVGxp+se7kF2v5mK8Z4vN6sd72AP7qyn9jv8 ga3Cm/TYzGY43DyHooxP6633zu4hLiAZy6hSh2LRH6lj/FdZrg4Y+ysnjFpk6U9098df /BeRU4b4kKrokoeDTtU8+46CHL1V0GkeycqVaM9hCLEphEaiAOOuGWz4npwNyvkvBYW/ J6Jw== X-Gm-Message-State: AO0yUKXY2IoWDWktOheFvlNYmqxMJVooo11TmbZShUgeieEZxExm8tl0 4A0bUa5CMaEQz8pcPqLSuKFeplSJxFqKemdc0A== X-Google-Smtp-Source: AK7set8pxVyKhYzeYJbLh/hwRd4Uu3vEs7a87hlm2FvbWgBf9tOHOVoa9Y6GzuIXe4LVFtgGHHuaVLgOE52DzxfEww== 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 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org 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: 13176843 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2C2E3C61DA4 for ; Thu, 16 Mar 2023 00:31:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233245AbjCPAbw (ORCPT ); Wed, 15 Mar 2023 20:31:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34552 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233179AbjCPAba (ORCPT ); Wed, 15 Mar 2023 20:31:30 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E0406A2F13 for ; Wed, 15 Mar 2023 17:31:21 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id t2-20020a632d02000000b005075b896422so5017pgt.19 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=SnSvmEJ8sxvHavPpNjo+D9OIugZ2C0VHFJUwA6itQbR59buEB/EkN0vpaEkz6gvevP gM+v28id5Lev6Rr5QaVur48p7+BMHaJy2st4tOga9ys2akFO64AKvBDKWaCkqmMlIyS8 RC1QTvkgmEjpyEJx1ieXACdk80CCeVyUNnWRyM+f1DSH1qhSIIsJmV4vBj9rkFbkDzFp iYqcbADlBakRPBpVa2MKLmjTl3tN3PN+K9LTfDuAk7E7gSY5yAGhu3S79igZwgV2xn2O K55qP0/e9+a3ssXWxZNWEv70gjn74Iaa4hIhq/BQ6kFxlN/pwfnWN9XuFpLTROMa8Lpq UE5w== X-Gm-Message-State: AO0yUKVKwcl/QS6+/hn9/joKFrZRgOg2mRQx4HQkF+Ii7g+P1wDlaNIM ugle4Qr/i225T2KDo7diuEFykAVV+8L8WUyEwg== X-Google-Smtp-Source: AK7set+j36PrTQ1uWKwZ/ZTMviUfrC3nfQJHH3bSUBCARqM/YyB/PYWAqeXRAUTM8xOy7rllnybTZtBO0uvKhOtP8A== 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 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org 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: 13176844 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CABD4C761AF for ; Thu, 16 Mar 2023 00:32:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233335AbjCPAcF (ORCPT ); Wed, 15 Mar 2023 20:32:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233207AbjCPAbp (ORCPT ); Wed, 15 Mar 2023 20:31:45 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92262A42F0 for ; Wed, 15 Mar 2023 17:31:23 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5447558ae68so98837b3.13 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=7/C/pwNbNruJ7nbQUPazSo3d5ngPXAw2+T/pwAeANtETv2j6+kSDSfhZu2/7Ik6tEF pQ4LhLMCYc54/6Kt8Hhf+owaRhQ+mpXKiw/KJMemQD7WHkf2OT9Y656slzyB2xA/yY56 Al41bkEkj0ohXr5lS5vTMaSE6BV9xxuiOp1SPxmEkh+A30YLbmFBVkXpoRlDD1E4J350 jwicPLeqYr4gMWF0V83CcX3QwhfuKCsIcerQC2Obx7gm5ViMg/XMjQ3PmUB8qK+ZcHRG 7iU2xBzNF0KaqvjPUCgIYrdn+haI9WSxm70VtVdrnabivFTE+P0jX4ZGCl9HquvunBCs ncEg== X-Gm-Message-State: AO0yUKUDQUuIZuJazOWwn/7f0FSlSjsEoOGSOb7GUehcCDZ91IJkPK5T A8sx2TcWRQbgNh5t98IwDgw5ONfXzzEH+bJljQ== X-Google-Smtp-Source: AK7set9tsl6JIRx5mbMVZPA/DONTxMkHWtMyPLSYdDprwtuWdlOsvzYeVaLj2tQwnFWT9YsS5B+6yQbk5CSUogkZdg== 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 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org 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: 13176845 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2B4BBC61DA4 for ; Thu, 16 Mar 2023 00:32:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233230AbjCPAcJ (ORCPT ); Wed, 15 Mar 2023 20:32:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35026 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233227AbjCPAbq (ORCPT ); Wed, 15 Mar 2023 20:31:46 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1EAB5A4014 for ; Wed, 15 Mar 2023 17:31:25 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id t25-20020a252d19000000b00b341ad1b626so86349ybt.19 for ; Wed, 15 Mar 2023 17:31:24 -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=czxXogU4vg+qzm7tq/35wEiGSSUG+WudeFyjqd4sLo5mGhM6N3BPvCLBH4cWly2SLE m01L0v/MiXJ+d5SXcF7ywsnvYiRvcnMLLr9oUgvmjbwiX9nOukjLXIvjSPPwaUCxOkCi bwsKUCKpKco+R+xIcrUB0JKallNF6ZU2NNpMOF5emb5E9Qet8TYudEEFiPxmF6ba1Jd1 tM+5+8daF9VIiO/FPmfr1x8PGX5uEtTl9v0gZIkNEW4mciQwuemEnEuK5NSlFOVHWeSl W3Txn1NStmlH52ssCyUYU41k4t4TU47uk6riWNsa2UErHPK2SDHx/32UoQ9OfIVcbQO8 vtlg== X-Gm-Message-State: AO0yUKXgKVa+0PgIZgV5DJS4O2h9lSOg07/6bStyvlMy16voOwfLbGeV 5616eAbS8HO/g8hieFXUyjmOV3fFzUm0yvnOdw== X-Google-Smtp-Source: AK7set9qSNzfiSuUSuac6czSQv9JIGaIPRBH/C++HGZYNA2V+6Yj/KtOeknnwR+NdK2YlHb02GTz9vZIhxkpteuF/Q== 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 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org 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: 13176846 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 21A55C7618B for ; Thu, 16 Mar 2023 00:32:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232898AbjCPAc1 (ORCPT ); Wed, 15 Mar 2023 20:32:27 -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 S233153AbjCPAcA (ORCPT ); Wed, 15 Mar 2023 20:32:00 -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 5363BA3350 for ; Wed, 15 Mar 2023 17:31:27 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id n33-20020a17090a5aa400b0023b4f444476so68421pji.3 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=H+HmOgoolQ1zj7JGfKikRcJw248/WGLhobXtnwdG9fWHrLmGtYWBBq4ha6nHlB01mQ gPr6OMLVQEthtY/X5ELUpAWcxgO3VHHKLZ6FD+a9OLcQJ1v19Km3baSIsGYSUiS+yqLt Dhi+s9LtTy17dKJv5lbH0YNzk2xkpj6S80EQquaBzMDdQ9aiK8CbWXI0K5KCWfp8bQkz DbE0QgDBAQ+vtMYZ9XKpr24+qmUD+GLMInLOvChXXZitF1sB3A0pnVC2r+IrO/iGRzwC dC/cOD+N6rB4DBjn/tiWDQtGfM3s+meB+y65t6wKgRL8NQH1cqfEk6KDwWN5vzLHiHCY ttbQ== X-Gm-Message-State: AO0yUKU4quVXJRjtvGUc2/C40r5svzkDr+8oc8MriwGjcABEvh+2vqCB 7ox8mlJRV2a9CMnJDFDmU2iq67EGaByIeVAsaw== X-Google-Smtp-Source: AK7set9HhZ92PQ2OitTC4KZtYEJK0EXqsjjwLnoPjbnEyW7GD+pO7NNEnen5rh0nKaexCpnSy3z8RFk0Cysc1DaScg== 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 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org 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: 13176847 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B10CAC7618B for ; Thu, 16 Mar 2023 00:32:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233397AbjCPAcl (ORCPT ); Wed, 15 Mar 2023 20:32:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34500 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233082AbjCPAcB (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 0F9F5A54D0 for ; Wed, 15 Mar 2023 17:31:27 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id v15-20020a17090a458f00b0023816b2f381so69901pjg.2 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=fHXx0Y9Pc05S2i5CJjWASyJ2s6f/V67Ck0obWb3DJsqrLvqe+VYhrIlszqlzX01w6C F0APEKfYxYpXVoTzUHunhFB9floSz77MIoJv5zg6oh0lCSa1LQ+qGeCiQ7IEnwDs5Pa9 thEVL76qhuGu/8RicmRP+UhFUOM4hzKsfcFzATvO+HTe3MWnSjELyXIq/eH3eHw9ZJwP uyNs/YTkadXkXZnrLO1Srr+f0S/ExxZxewATNgUqJxbjZHzYq6jN0J0wa8R+5enwneEl 7wAeAayeecoVLHGZIg0Kjd2WjPTf3GSyN4Jm4jcVahqXB5dlAx/7mVXGCNSUj5c2Hmak uUbQ== X-Gm-Message-State: AO0yUKX8ORBz1XS6sXPS3vUlh2tV6dzg+DnoHQgRHiLR6mYaVeAm8FUF gLehAwAYZczoFW0Y8cVGT5MmFvwyn6ogjL61MQ== X-Google-Smtp-Source: AK7set8wyHBYS0yNjAY2Bu6NPZk7sDhPcWzuxoadwL3xHiIjGr+DZnA4k/ntT0jv/7rXx9SUD16F0j0DYIAvrg8IcQ== 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 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org 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: 13176849 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 51B11C7618D for ; Thu, 16 Mar 2023 00:33:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233356AbjCPAdB (ORCPT ); Wed, 15 Mar 2023 20:33:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34526 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233214AbjCPAcF (ORCPT ); Wed, 15 Mar 2023 20:32:05 -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 7548CA02B7 for ; Wed, 15 Mar 2023 17:31:30 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id g12-20020aa7874c000000b0062519d49a5dso172518pfo.12 for ; Wed, 15 Mar 2023 17:31:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=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=AWvzxFzWOR9Azy+meVqVFFM1UkRaiF5TmP8i9xB81Gl4PiCdjPVR+WNhF97Y9lKWtL Yyh1ogXbjlAex9+xnidBlEjkdKiQnmTmKQeL6RWhO8Bb2wzzvVdzmw6wRo27KR1oYdk6 TJ9MoXgeRhv8o/M7gc9uN7tA4Ux3Ry3UALGdzU7ilUbhjp7O25rBq/Hl5UEXP97RIWPT NJIURQCtVIubzeRiVMr7JFvuBGc2g/Biwe884Z0phdrLY1OMJEqvb24mJuez0UlaeDq1 y4SitD3hJagvOpXOBSHefbODY0Eia9xVpHpKJ/vJitE9L0HdoVwnQ+6j2nEgz+CT2J0p M7Kw== X-Gm-Message-State: AO0yUKXVq3WfOgoPXMEE0/It69K9t8dD3I7JxAerAoelB1Sa6pLFmSmC PEDLWQRNvzVI5jg1m5QYqNImiy6WS0i/wqswBA== X-Google-Smtp-Source: AK7set81FNM6aZ2KKc0RpczFG5Lx+WgCWICz/KzlEJw1NjziYveSgQLqdoN474MmozxZF5HPAo3b3AmF9lnSjz983g== 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 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org 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: 13176848 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B795BC61DA4 for ; Thu, 16 Mar 2023 00:32:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230477AbjCPAcx (ORCPT ); Wed, 15 Mar 2023 20:32:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35576 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233311AbjCPAcE (ORCPT ); Wed, 15 Mar 2023 20:32:04 -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 32251A617D for ; Wed, 15 Mar 2023 17:31:32 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id z9-20020a170902708900b0019f271f33aeso24172plk.9 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=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=ShbgVzEPl9DkssetWB5zd3Dsg6bxi+K43325F45z1uWOaxa74IqhPpkysLjNCOQIE1 SitZl8EUJPwktMCYRmfzKN+SsGbaeCzMF8c+KbF4irvWDOIMoE4+DYY2MiEApnglwSrc H6+IJUb5g57czGgeXIj3n6Y4VLygk4HOSNvyGenKi+slDk4x3KV6DI0XvHQDG3F2hO0+ fFIkH1Gq51h9R2q8U38IpgEx1LeaVObsc2fVZiRgI6gyZEwKjYi0ez1PidHpuWYH8wTl Sfg8H8o4P6womRF3SYZ/ix3+MSY5ksgCdI6MRbzoRkCGbrF4MSBL/Vc5y+9Ox2TPELs1 LNag== X-Gm-Message-State: AO0yUKVxA+o3Q0dgy24LU+zLLce32xARGVqUNYeid/aZUx5xNPgJnlyV 3LPLuEmrCw3YRZuXTQMnJBtjhhjgYm2qPA6vaw== X-Google-Smtp-Source: AK7set8gW1ruUe+jZfx4vo3AOKGV2qgKk/o0c2WgfJNyT6l/gKi5/qNPPNY0SZ250Rg6tfQ0OBKmkU3mHKKgzrPR/w== X-Received: from ackerleytng-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1f5f]) (user=ackerleytng job=sendgmr) by 2002:a65:6392:0:b0:50b:cc91:5534 with SMTP id h18-20020a656392000000b0050bcc915534mr320262pgv.2.1678926690322; Wed, 15 Mar 2023 17:31:30 -0700 (PDT) Date: Thu, 16 Mar 2023 00:31:02 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.40.0.rc2.332.ga46443480c-goog Message-ID: Subject: [RFC PATCH 09/10] KVM: selftests: Add tests around sharing a restrictedmem fd From: Ackerley Tng To: kvm@vger.kernel.org, linux-api@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, qemu-devel@nongnu.org Cc: aarcange@redhat.com, ak@linux.intel.com, akpm@linux-foundation.org, arnd@arndb.de, bfields@fieldses.org, bp@alien8.de, chao.p.peng@linux.intel.com, corbet@lwn.net, dave.hansen@intel.com, david@redhat.com, ddutile@redhat.com, dhildenb@redhat.com, hpa@zytor.com, hughd@google.com, jlayton@kernel.org, jmattson@google.com, joro@8bytes.org, jun.nakajima@intel.com, kirill.shutemov@linux.intel.com, linmiaohe@huawei.com, luto@kernel.org, mail@maciej.szmigiero.name, mhocko@suse.com, michael.roth@amd.com, mingo@redhat.com, naoya.horiguchi@nec.com, pbonzini@redhat.com, qperret@google.com, rppt@kernel.org, seanjc@google.com, shuah@kernel.org, steven.price@arm.com, tabba@google.com, tglx@linutronix.de, vannapurve@google.com, vbabka@suse.cz, vkuznets@redhat.com, wanpengli@tencent.com, wei.w.wang@intel.com, x86@kernel.org, yu.c.zhang@linux.intel.com, Ackerley Tng Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org 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: 13176850 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 58FF8C7618D for ; Thu, 16 Mar 2023 00:33:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233409AbjCPAdV (ORCPT ); Wed, 15 Mar 2023 20:33:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35728 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233353AbjCPAcG (ORCPT ); Wed, 15 Mar 2023 20:32:06 -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 D1521A64BF for ; Wed, 15 Mar 2023 17:31:37 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id l19-20020a056a0016d300b006257255adb4so172144pfc.13 for ; Wed, 15 Mar 2023 17:31:37 -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=E33zGxMvH+Kh+YBqVvZiDAzxZB6+9BmOi4xVKHuYUQsp44rPeBrw+CxK9LHVGb3JOn F1o8LF3ww0MsHtKWr+F60C57CDu/FtMyDJ3Wx4eo2DfqOh5C9Gqmcx70crPrb8o9VgnD AmtMxzcbfyxgiDZF5ulhxi72A9WguGBQ3EedEXXOacgtpKSyOlTw9zPjnTB0ERr9gCNb uzp+iFfuAbCcvRWo9dUIgYwLdlgDEtxzWZWXaZHmYQ+Jo5SAnwH42tww8Y1eKIg4g+/x KnSMHpONytN95ouMQ1yFvpDGY0XUsIyc1/j1AZaNRKCVx+i8cY4UkBqkkvhod2hekCJ6 0U5A== X-Gm-Message-State: AO0yUKUWtxnOHMgDufHU0oHhJ/0WK5l+SP3psHj25HPzGvhGomf9qbao QJPm6UAmpihzf7gkbOsuH2b3PhuwFVCJPNogrQ== X-Google-Smtp-Source: AK7set/r1L80ZGqvN1Jqjj7ZwUbt+f+1AOqGwiuE9AJvattpTxM2ZIa1Wyqx7LP6pavKwLlYqhK3jR4rdgiNHjRokg== 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 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org "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(); +}