From nobody Mon Apr 13 13:31:06 2026 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 CA1DDC282E7 for ; Wed, 10 Aug 2022 15:21:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233245AbiHJPVk (ORCPT ); Wed, 10 Aug 2022 11:21:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37522 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233267AbiHJPVL (ORCPT ); Wed, 10 Aug 2022 11:21:11 -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 D69BD7859A for ; Wed, 10 Aug 2022 08:20:56 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id p6-20020a170902e74600b0016f3f326f62so9853998plf.16 for ; Wed, 10 Aug 2022 08:20:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=1bJnjZzG51Rg4FKSx2zZTcHWIUVVwczNqrIXz3SXrt0=; b=jr5IRsKwa9b5GEx36QBldnNldpQ1VyHMxngtx3BWtaLlllInHFAsefTGBieRedvrKJ KWSCD3zWbi70JuuYNc9z5gMuJdpgxy53NeQh4zt7FdxJq2rlvvS596qvqxO8eT9wgOMI /VNpiR+ZXy9OkXsWJj9q1dGuVqYxFdPNd+zVtOTbeHwrchI2ine2ZfGQ5jzpYJeMHIJk x805tyEPfo12+W7loGccs5XWrxNdX5sFrBR7cRU4DS8hhGGEXWZVMqSRShqaPx5ETMcY q6Fy4+U3x5GxvXoO4EUA8/yA1EwFi3+7LI2OhnlbYQB9Yy3j7STzn0oaf5G4lS4mpVje Igrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=1bJnjZzG51Rg4FKSx2zZTcHWIUVVwczNqrIXz3SXrt0=; b=zIKNe6zzXA3VVAlbUcPS++wYWrGfuD/O6e1894/oCGGIZFFvCiZMIBM/waxW5KMfWY OXBoJ3GBmJRyISvXyIG5gsu61EXiG/J5hoEMtD+8AmLTbyx9uFnpz69qQjWx8NX3HPe5 +yg0rlqFCfxR512qpzoaz4wTS0N2ZLciNGYAU7zikzec63tla6PY7+xV5SZ88fe192jO zyZWLkkZcDXymipWPkUcVMQ9HJw8MGXGi2yZtgz0R03wi5UctVinNDjF9OesWI5tEKGg HFRiwYwtZ52dRhdKNH0wZBjP8J7Uj/Gavn89OHgBruTWzhLu1a5T6dA0J4zW1hsVvpCu XOyg== X-Gm-Message-State: ACgBeo1Qrn3QUctz6uJtzaN11G2lIM31ATkggKztgacUL/NJcCIdgmmF IfJMiVQ5Fpa4NPQUlzoMgPy3AE+ZgLY= X-Google-Smtp-Source: AA6agR5CWuMFLA0UiEriC2+JGPiBqHahtIiZ/TWwaGGuDyDblnNAqKNFM4HUKLDEVFsCqCLBy+MkC3fN0EA= X-Received: from pgonda1.kir.corp.google.com ([2620:15c:29:203:b185:1827:5b23:bbe2]) (user=pgonda job=sendgmr) by 2002:a63:d047:0:b0:41d:d4e9:e182 with SMTP id s7-20020a63d047000000b0041dd4e9e182mr5206386pgi.328.1660144855753; Wed, 10 Aug 2022 08:20:55 -0700 (PDT) Date: Wed, 10 Aug 2022 08:20:32 -0700 In-Reply-To: <20220810152033.946942-1-pgonda@google.com> Message-Id: <20220810152033.946942-11-pgonda@google.com> Mime-Version: 1.0 References: <20220810152033.946942-1-pgonda@google.com> X-Mailer: git-send-email 2.37.1.559.g78731f0fdb-goog Subject: [V3 10/11] KVM: selftests: Add ucall pool based implementation From: Peter Gonda To: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, marcorr@google.com, seanjc@google.com, michael.roth@amd.com, thomas.lendacky@amd.com, joro@8bytes.org, mizhang@google.com, pbonzini@redhat.com, andrew.jones@linux.dev, vannapurve@google.com, Peter Gonda Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To add support for encrypted, SEV, guests in the ucall framework introduce a new "ucall pool" implementation. This was suggested in the thread on "[RFC PATCH 00/10] KVM: selftests: Add support for test-selectable ucall implementations". Using a listed as suggested there doesn't work well because the list is setup using HVAs not GVAs so use a bitmap + array solution instead to get the same pool of ucall structs result. This allows for guests with encryption enabled set up a pool of ucall structs in the guest's shared memory region. Suggested-by: Sean Christopherson Signed-off-by: Peter Gonda --- .../selftests/kvm/include/kvm_util_base.h | 2 + .../selftests/kvm/include/ucall_common.h | 13 +-- .../testing/selftests/kvm/lib/aarch64/ucall.c | 10 +- tools/testing/selftests/kvm/lib/riscv/ucall.c | 5 +- tools/testing/selftests/kvm/lib/s390x/ucall.c | 5 +- .../testing/selftests/kvm/lib/ucall_common.c | 108 +++++++++++++++++- .../testing/selftests/kvm/lib/x86_64/ucall.c | 6 +- 7 files changed, 131 insertions(+), 18 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/te= sting/selftests/kvm/include/kvm_util_base.h index 1a84d2d1d85b..baede0d118c5 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -103,6 +103,8 @@ struct kvm_vm { int stats_fd; struct kvm_stats_header stats_header; struct kvm_stats_desc *stats_desc; + + bool use_ucall_pool; }; =20 =20 diff --git a/tools/testing/selftests/kvm/include/ucall_common.h b/tools/tes= ting/selftests/kvm/include/ucall_common.h index 63bfc60be995..002a22e1cd1d 100644 --- a/tools/testing/selftests/kvm/include/ucall_common.h +++ b/tools/testing/selftests/kvm/include/ucall_common.h @@ -22,6 +22,9 @@ enum { struct ucall { uint64_t cmd; uint64_t args[UCALL_MAX_ARGS]; + + /* For ucall pool usage. */ + struct ucall *hva; }; =20 void ucall_arch_init(struct kvm_vm *vm, void *arg); @@ -32,15 +35,9 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu); void ucall(uint64_t cmd, int nargs, ...); uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc); =20 -static inline void ucall_init(struct kvm_vm *vm, void *arg) -{ - ucall_arch_init(vm, arg); -} +void ucall_init(struct kvm_vm *vm, void *arg); =20 -static inline void ucall_uninit(struct kvm_vm *vm) -{ - ucall_arch_uninit(vm); -} +void ucall_uninit(struct kvm_vm *vm); =20 #define GUEST_SYNC_ARGS(stage, arg1, arg2, arg3, arg4) \ ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4) diff --git a/tools/testing/selftests/kvm/lib/aarch64/ucall.c b/tools/testin= g/selftests/kvm/lib/aarch64/ucall.c index 132c0e98bf49..ee70531e8e51 100644 --- a/tools/testing/selftests/kvm/lib/aarch64/ucall.c +++ b/tools/testing/selftests/kvm/lib/aarch64/ucall.c @@ -81,12 +81,16 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) =20 if (run->exit_reason =3D=3D KVM_EXIT_MMIO && run->mmio.phys_addr =3D=3D (uint64_t)ucall_exit_mmio_addr) { - vm_vaddr_t gva; + uint64_t ucall_addr; =20 TEST_ASSERT(run->mmio.is_write && run->mmio.len =3D=3D 8, "Unexpected ucall exit mmio address access"); - memcpy(&gva, run->mmio.data, sizeof(gva)); - return addr_gva2hva(vcpu->vm, gva); + memcpy(&ucall_addr, run->mmio.data, sizeof(ucall_addr)); + + if (vcpu->vm->use_ucall_pool) + return (void *)ucall_addr; + else + return addr_gva2hva(vcpu->vm, ucall_addr); } =20 return NULL; diff --git a/tools/testing/selftests/kvm/lib/riscv/ucall.c b/tools/testing/= selftests/kvm/lib/riscv/ucall.c index 37e091d4366e..4bb5616df29f 100644 --- a/tools/testing/selftests/kvm/lib/riscv/ucall.c +++ b/tools/testing/selftests/kvm/lib/riscv/ucall.c @@ -59,7 +59,10 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) run->riscv_sbi.extension_id =3D=3D KVM_RISCV_SELFTESTS_SBI_EXT) { switch (run->riscv_sbi.function_id) { case KVM_RISCV_SELFTESTS_SBI_UCALL: - return addr_gva2hva(vcpu->vm, run->riscv_sbi.args[0]); + if (vcpu->vm->use_ucall_pool) + return (void *)run->riscv_sbi.args[0]; + else + return addr_gva2hva(vcpu->vm, run->riscv_sbi.args[0]); case KVM_RISCV_SELFTESTS_SBI_UNEXP: vcpu_dump(stderr, vcpu, 2); TEST_ASSERT(0, "Unexpected trap taken by guest"); diff --git a/tools/testing/selftests/kvm/lib/s390x/ucall.c b/tools/testing/= selftests/kvm/lib/s390x/ucall.c index 0f695a031d35..b24c6649877a 100644 --- a/tools/testing/selftests/kvm/lib/s390x/ucall.c +++ b/tools/testing/selftests/kvm/lib/s390x/ucall.c @@ -30,7 +30,10 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) (run->s390_sieic.ipb >> 16) =3D=3D 0x501) { int reg =3D run->s390_sieic.ipa & 0xf; =20 - return addr_gva2hva(vcpu->vm, run->s.regs.gprs[reg]); + if (vcpu->vm->use_ucall_pool) + return (void *)run->s.regs.gprs[reg]; + else + return addr_gva2hva(vcpu->vm, run->s.regs.gprs[reg]); } return NULL; } diff --git a/tools/testing/selftests/kvm/lib/ucall_common.c b/tools/testing= /selftests/kvm/lib/ucall_common.c index ced480860746..b6502a9420c4 100644 --- a/tools/testing/selftests/kvm/lib/ucall_common.c +++ b/tools/testing/selftests/kvm/lib/ucall_common.c @@ -1,22 +1,122 @@ // SPDX-License-Identifier: GPL-2.0-only #include "kvm_util.h" +#include "linux/types.h" +#include "linux/bitmap.h" +#include "linux/atomic.h" + +struct ucall_header { + DECLARE_BITMAP(in_use, KVM_MAX_VCPUS); + struct ucall ucalls[KVM_MAX_VCPUS]; +}; + +static bool use_ucall_pool; +static struct ucall_header *ucall_pool; + +void ucall_init(struct kvm_vm *vm, void *arg) +{ + struct ucall *uc; + struct ucall_header *hdr; + vm_vaddr_t vaddr; + int i; + + use_ucall_pool =3D vm->use_ucall_pool; + sync_global_to_guest(vm, use_ucall_pool); + if (!use_ucall_pool) + goto out; + + TEST_ASSERT(!ucall_pool, "Only a single encrypted guest at a time for uca= lls."); + vaddr =3D vm_vaddr_alloc_shared(vm, sizeof(*hdr), vm->page_size); + hdr =3D (struct ucall_header *)addr_gva2hva(vm, vaddr); + memset(hdr, 0, sizeof(*hdr)); + + for (i =3D 0; i < KVM_MAX_VCPUS; ++i) { + uc =3D &hdr->ucalls[i]; + uc->hva =3D uc; + } + + ucall_pool =3D (struct ucall_header *)vaddr; + sync_global_to_guest(vm, ucall_pool); + +out: + ucall_arch_init(vm, arg); +} + +void ucall_uninit(struct kvm_vm *vm) +{ + use_ucall_pool =3D false; + ucall_pool =3D NULL; + + if (!vm->memcrypt.encrypted) { + sync_global_to_guest(vm, use_ucall_pool); + sync_global_to_guest(vm, ucall_pool); + } + + ucall_arch_uninit(vm); +} + +static struct ucall *ucall_alloc(void) +{ + struct ucall *uc =3D NULL; + int i; + + if (!use_ucall_pool) + goto out; + + for (i =3D 0; i < KVM_MAX_VCPUS; ++i) { + if (!atomic_test_and_set_bit(i, ucall_pool->in_use)) { + uc =3D &ucall_pool->ucalls[i]; + memset(uc->args, 0, sizeof(uc->args)); + break; + } + } +out: + return uc; +} + +static inline size_t uc_pool_idx(struct ucall *uc) +{ + return uc->hva - ucall_pool->ucalls; +} + +static void ucall_free(struct ucall *uc) +{ + if (!use_ucall_pool) + return; + + clear_bit(uc_pool_idx(uc), ucall_pool->in_use); +} =20 void ucall(uint64_t cmd, int nargs, ...) { - struct ucall uc =3D {}; + struct ucall *uc; + struct ucall tmp =3D {}; va_list va; int i; =20 - WRITE_ONCE(uc.cmd, cmd); + uc =3D ucall_alloc(); + if (!uc) + uc =3D &tmp; + + WRITE_ONCE(uc->cmd, cmd); =20 nargs =3D min(nargs, UCALL_MAX_ARGS); =20 va_start(va, nargs); for (i =3D 0; i < nargs; ++i) - WRITE_ONCE(uc.args[i], va_arg(va, uint64_t)); + WRITE_ONCE(uc->args[i], va_arg(va, uint64_t)); va_end(va); =20 - ucall_arch_do_ucall((vm_vaddr_t)&uc); + /* + * When using the ucall pool implementation the @hva member of the ucall + * structs in the pool has been initialized to the hva of the ucall + * object. + */ + if (use_ucall_pool) + ucall_arch_do_ucall((vm_vaddr_t)uc->hva); + else + ucall_arch_do_ucall((vm_vaddr_t)uc); + + ucall_free(uc); } =20 uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc) diff --git a/tools/testing/selftests/kvm/lib/x86_64/ucall.c b/tools/testing= /selftests/kvm/lib/x86_64/ucall.c index ead9946399ab..07c1bc41fa5c 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/ucall.c +++ b/tools/testing/selftests/kvm/lib/x86_64/ucall.c @@ -30,7 +30,11 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) struct kvm_regs regs; =20 vcpu_regs_get(vcpu, ®s); - return addr_gva2hva(vcpu->vm, regs.rdi); + + if (vcpu->vm->use_ucall_pool) + return (void *)regs.rdi; + else + return addr_gva2hva(vcpu->vm, regs.rdi); } return NULL; } --=20 2.37.1.559.g78731f0fdb-goog