Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp2586669imn; Tue, 2 Aug 2022 08:30:14 -0700 (PDT) X-Google-Smtp-Source: AGRyM1u2+irE1cSrxmfk8pdhFr4An2heF91vq/h17NsF90gHFif2OkV0ZTfaW155CdG9cIfguGyP X-Received: by 2002:a17:907:c24:b0:72b:6932:831a with SMTP id ga36-20020a1709070c2400b0072b6932831amr16369182ejc.51.1659454213924; Tue, 02 Aug 2022 08:30:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659454213; cv=none; d=google.com; s=arc-20160816; b=kgkAv3KMa4z634+ohZP+JKJk0vpzZ9kK1UbugcEi3a9BjroRnNPhU1emXKBm4fakMz WZSAj9sPzuTwCK4LHZjwgFifK7vB6R56wA8FiifozKeFJPNYLOssk6qtToh3okOEM4yE c4WOyDb3xPpMh7Ij+Y84DNhQlcJSQ1c6K3TtAkXLfSl/ASjDa3fwZmxZWPidH/G5H5yZ DqeDukzt2vEsz8FMjiNm5AnyugPgupFTnv3JzznNtG5grBhhCdGK62waSp9HnlVIOA45 hrKd+sjf7dhgDLM4ur4RRcBiv4Vdze/K45IOVF5mHiRbgRpF43rCn+g9UalhMpJPNinW zxTA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=sTUnnkEh4NNYaXKOrpgiX4GocnwHMt13gQIUfZPwT5Q=; b=BOI4xy/jjr6ZmETEgPXJLBdDE2uSBDpMz4Og8eRznBv89eD9X75LpWGf7o738TmSaM PrdTg5fv1TX3uqZMx1Ye5mUJTZxUaqyZho2UwW08EVmYCjOSg5dTPfzkBBJ60ECJob6H YiJBOn0NfqAwS7JdlZu8nd6XeCEFE2eJrhg/ZATxMu+OU/DAUi13VAGxvHyozD5dVtwt 8cdmbmf1oEadXGN9A3Ozy/a9T/5iNYypIR2uadKzdgroSzhvAKnHzfqTOQZCMWwnRjub scKgR/OK3W/FRZrp2C45YIMXQnqKC0v1udXAuZcnZVBNIZY1vs5e8Ydw8HCbFnMuA0hI l8mA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=XVHmeFYH; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id xa6-20020a170906fd8600b006fe4c66b711si14483638ejb.46.2022.08.02.08.29.49; Tue, 02 Aug 2022 08:30:13 -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=XVHmeFYH; 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 S237333AbiHBOrT (ORCPT + 99 others); Tue, 2 Aug 2022 10:47:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35192 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237115AbiHBOrO (ORCPT ); Tue, 2 Aug 2022 10:47:14 -0400 Received: from mail-lf1-x131.google.com (mail-lf1-x131.google.com [IPv6:2a00:1450:4864:20::131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97BE01F620 for ; Tue, 2 Aug 2022 07:47:10 -0700 (PDT) Received: by mail-lf1-x131.google.com with SMTP id x39so12707741lfu.7 for ; Tue, 02 Aug 2022 07:47:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=sTUnnkEh4NNYaXKOrpgiX4GocnwHMt13gQIUfZPwT5Q=; b=XVHmeFYHUbieZS2wrbrZqk04cu4LCv8SG7DF2kaGzB1SWJ32c8gevXmNe9xqZglLn1 cJWzXbyni5EPpuMpGH3AU1O7WnYvQkzsbXuUqecRj47r5KPmbVz/JVJ6HNxnCVyZoUhG 3e/TlfR1SNnk8ddzRl+nXGOt0CPbDjeotLGHQlG1doSqx6RqlmNfsnY97hUcu3IVgha5 LaE/z73yq0MUrjGuswBnlh8ZiOmT6qNgXMqu6EFLx5pCWXW7RkNaH3OraUWRPIyryWbe 3ide4rBv4ovrEg6EP0pe71sO3TWU10/3eobcFnF59FPIp8VFf6PtejKoSYLAnsb63Tp1 1BXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=sTUnnkEh4NNYaXKOrpgiX4GocnwHMt13gQIUfZPwT5Q=; b=lo6ugcaPm/FKCHFK0Jfvlfq5RNVUwI3VBLVDHPbPKLOAp1Fzs2ASixNvKZQoS08NhH DN54sBIKumbIgFngWNJIMNfKc+Lc+YlOoluWcuGy0SsAwzQp0vHBKCZMyyOo4ZOrbh79 YfSSkWRXEQXWp6NLyq8tqWrnBVuxi68HWhMbxRudswNAIoPBJoh9L8ur7FgGlmIp+ToM 7unpBLLznLswEIkcDu/cEPU4GDne46cxQItHfv4C2oNLs/gl0JbrArhosyaUaL4rcTX5 qGjhtY9veFsuV7HqUw+SZgnbscYAV+uNt8yoH4+s7W3I43CKGaDwj4+9iWXWB6suzxH6 dOQg== X-Gm-Message-State: AJIora92NuHHO/hXUp0ZLRSKn7fYe3exSoJqMC1gEslf2dXrrrBTHpls SboUzHYngdKghhZPt6vORVyA+admZCMkLS1cbureGg== X-Received: by 2002:a05:6512:21a7:b0:48a:a06e:1d21 with SMTP id c7-20020a05651221a700b0048aa06e1d21mr7435055lft.494.1659451627302; Tue, 02 Aug 2022 07:47:07 -0700 (PDT) MIME-Version: 1.0 References: <20220801201109.825284-1-pgonda@google.com> <20220801201109.825284-8-pgonda@google.com> <20220802095437.fbho3bwgs3yi3fur@kamzik> In-Reply-To: <20220802095437.fbho3bwgs3yi3fur@kamzik> From: Peter Gonda Date: Tue, 2 Aug 2022 08:46:55 -0600 Message-ID: Subject: Re: [V2 07/11] KVM: selftests: Consolidate boilerplate code in get_ucall() To: Andrew Jones Cc: kvm list , LKML , Marc Orr , Sean Christopherson , Michael Roth , "Lendacky, Thomas" , Joerg Roedel , Mingwei Zhang , Paolo Bonzini Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_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 On Tue, Aug 2, 2022 at 3:54 AM Andrew Jones wrote: > > On Mon, Aug 01, 2022 at 01:11:05PM -0700, Peter Gonda wrote: > > From: Sean Christopherson > > > > Consolidate the actual copying of a ucall struct from guest=>host into > > the common get_ucall(). Return a host virtual address instead of a guest > > virtual address even though the addr_gva2hva() part could be moved to > > get_ucall() too. Conceptually, get_ucall() is invoked from the host and > > should return a host virtual address (and returning NULL for "nothing to > > see here" is far superior to returning 0). > > The code does not match this description anymore, now that > ucall_arch_get_ucall() returns a gva (as a uint64_t), but the description > is good, the code is wrong. Please restore the spirit of Sean's patch > (particularly because it still says it's from Sean...) As discussed in the encrypted ucall patch I'll get this fixed up to return gva from ucall_arch_get_ucall() and return a vm_addr_t. > > Thanks, > drew > > > > > Use pointer shenanigans instead of an unnecessary bounce buffer when the > > caller of get_ucall() provides a valid pointer. > > > > Signed-off-by: Sean Christopherson > > Signed-off-by: Peter Gonda > > --- > > .../selftests/kvm/include/ucall_common.h | 8 ++------ > > .../testing/selftests/kvm/lib/aarch64/ucall.c | 13 +++---------- > > tools/testing/selftests/kvm/lib/riscv/ucall.c | 19 +++---------------- > > tools/testing/selftests/kvm/lib/s390x/ucall.c | 16 +++------------- > > .../testing/selftests/kvm/lib/ucall_common.c | 19 +++++++++++++++++++ > > .../testing/selftests/kvm/lib/x86_64/ucall.c | 16 +++------------- > > 6 files changed, 33 insertions(+), 58 deletions(-) > > > > diff --git a/tools/testing/selftests/kvm/include/ucall_common.h b/tools/testing/selftests/kvm/include/ucall_common.h > > index 5a85f5318bbe..c1bc8e33ef3f 100644 > > --- a/tools/testing/selftests/kvm/include/ucall_common.h > > +++ b/tools/testing/selftests/kvm/include/ucall_common.h > > @@ -27,9 +27,10 @@ struct ucall { > > void ucall_arch_init(struct kvm_vm *vm, void *arg); > > void ucall_arch_uninit(struct kvm_vm *vm); > > void ucall_arch_do_ucall(vm_vaddr_t uc); > > -uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc); > > +uint64_t 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); > > > > static inline void ucall_init(struct kvm_vm *vm, void *arg) > > { > > @@ -41,11 +42,6 @@ static inline void ucall_uninit(struct kvm_vm *vm) > > ucall_arch_uninit(vm); > > } > > > > -static inline uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc) > > -{ > > - return ucall_arch_get_ucall(vcpu, uc); > > -} > > - > > #define GUEST_SYNC_ARGS(stage, arg1, arg2, arg3, arg4) \ > > ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4) > > #define GUEST_SYNC(stage) ucall(UCALL_SYNC, 2, "hello", stage) > > diff --git a/tools/testing/selftests/kvm/lib/aarch64/ucall.c b/tools/testing/selftests/kvm/lib/aarch64/ucall.c > > index 1c81a6a5c1f2..d2f099caa9ab 100644 > > --- a/tools/testing/selftests/kvm/lib/aarch64/ucall.c > > +++ b/tools/testing/selftests/kvm/lib/aarch64/ucall.c > > @@ -78,24 +78,17 @@ void ucall_arch_do_ucall(vm_vaddr_t uc) > > uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc) > > { > > struct kvm_run *run = vcpu->run; > > - struct ucall ucall = {}; > > - > > - if (uc) > > - memset(uc, 0, sizeof(*uc)); > > > > if (run->exit_reason == KVM_EXIT_MMIO && > > run->mmio.phys_addr == (uint64_t)ucall_exit_mmio_addr) { > > - vm_vaddr_t gva; > > + uint64_t ucall_addr; > > > > TEST_ASSERT(run->mmio.is_write && run->mmio.len == 8, > > "Unexpected ucall exit mmio address access"); > > memcpy(&gva, run->mmio.data, sizeof(gva)); > > - memcpy(&ucall, addr_gva2hva(vcpu->vm, gva), sizeof(ucall)); > > > > - vcpu_run_complete_io(vcpu); > > - if (uc) > > - memcpy(uc, &ucall, sizeof(ucall)); > > + return ucall_addr; > > } > > > > - return ucall.cmd; > > + return 0; > > } > > diff --git a/tools/testing/selftests/kvm/lib/riscv/ucall.c b/tools/testing/selftests/kvm/lib/riscv/ucall.c > > index b1598f418c1f..3f000d0b705f 100644 > > --- a/tools/testing/selftests/kvm/lib/riscv/ucall.c > > +++ b/tools/testing/selftests/kvm/lib/riscv/ucall.c > > @@ -51,27 +51,15 @@ void ucall_arch_do_ucall(vm_vaddr_t uc) > > uc, 0, 0, 0, 0, 0); > > } > > > > -uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc) > > +uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu) > > { > > struct kvm_run *run = vcpu->run; > > - struct ucall ucall = {}; > > - > > - if (uc) > > - memset(uc, 0, sizeof(*uc)); > > > > if (run->exit_reason == KVM_EXIT_RISCV_SBI && > > run->riscv_sbi.extension_id == KVM_RISCV_SELFTESTS_SBI_EXT) { > > switch (run->riscv_sbi.function_id) { > > case KVM_RISCV_SELFTESTS_SBI_UCALL: > > - memcpy(&ucall, > > - addr_gva2hva(vcpu->vm, run->riscv_sbi.args[0]), > > - sizeof(ucall)); > > - > > - vcpu_run_complete_io(vcpu); > > - if (uc) > > - memcpy(uc, &ucall, sizeof(ucall)); > > - > > - break; > > + return 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"); > > @@ -80,6 +68,5 @@ uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc) > > break; > > } > > } > > - > > - return ucall.cmd; > > + return 0; > > } > > diff --git a/tools/testing/selftests/kvm/lib/s390x/ucall.c b/tools/testing/selftests/kvm/lib/s390x/ucall.c > > index 114cb4af295f..f7a5a7eb4aa8 100644 > > --- a/tools/testing/selftests/kvm/lib/s390x/ucall.c > > +++ b/tools/testing/selftests/kvm/lib/s390x/ucall.c > > @@ -20,13 +20,9 @@ void ucall_arch_do_ucall(vm_vaddr_t uc) > > asm volatile ("diag 0,%0,0x501" : : "a"(uc) : "memory"); > > } > > > > -uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc) > > +uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu) > > { > > struct kvm_run *run = vcpu->run; > > - struct ucall ucall = {}; > > - > > - if (uc) > > - memset(uc, 0, sizeof(*uc)); > > > > if (run->exit_reason == KVM_EXIT_S390_SIEIC && > > run->s390_sieic.icptcode == 4 && > > @@ -34,13 +30,7 @@ uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc) > > (run->s390_sieic.ipb >> 16) == 0x501) { > > int reg = run->s390_sieic.ipa & 0xf; > > > > - memcpy(&ucall, addr_gva2hva(vcpu->vm, run->s.regs.gprs[reg]), > > - sizeof(ucall)); > > - > > - vcpu_run_complete_io(vcpu); > > - if (uc) > > - memcpy(uc, &ucall, sizeof(ucall)); > > + return run->s.regs.gprs[reg]; > > } > > - > > - return ucall.cmd; > > + return 0; > > } > > diff --git a/tools/testing/selftests/kvm/lib/ucall_common.c b/tools/testing/selftests/kvm/lib/ucall_common.c > > index 749ffdf23855..a060252bab40 100644 > > --- a/tools/testing/selftests/kvm/lib/ucall_common.c > > +++ b/tools/testing/selftests/kvm/lib/ucall_common.c > > @@ -18,3 +18,22 @@ void ucall(uint64_t cmd, int nargs, ...) > > > > ucall_arch_do_ucall((vm_vaddr_t)&uc); > > } > > + > > +uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc) > > +{ > > + struct ucall ucall; > > + void *addr; > > + > > + if (!uc) > > + uc = &ucall; > > + > > + addr = addr_gva2hva(vcpu->vm, ucall_arch_get_ucall(vcpu)); > > + if (addr) { > > + memcpy(uc, addr, sizeof(*uc)); > > + vcpu_run_complete_io(vcpu); > > + } else { > > + memset(uc, 0, sizeof(*uc)); > > + } > > + > > + return uc->cmd; > > +} > > diff --git a/tools/testing/selftests/kvm/lib/x86_64/ucall.c b/tools/testing/selftests/kvm/lib/x86_64/ucall.c > > index 9f532dba1003..24746120a593 100644 > > --- a/tools/testing/selftests/kvm/lib/x86_64/ucall.c > > +++ b/tools/testing/selftests/kvm/lib/x86_64/ucall.c > > @@ -22,25 +22,15 @@ void ucall_arch_do_ucall(vm_vaddr_t uc) > > : : [port] "d" (UCALL_PIO_PORT), "D" (uc) : "rax", "memory"); > > } > > > > -uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc) > > +uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu) > > { > > struct kvm_run *run = vcpu->run; > > - struct ucall ucall = {}; > > - > > - if (uc) > > - memset(uc, 0, sizeof(*uc)); > > > > if (run->exit_reason == KVM_EXIT_IO && run->io.port == UCALL_PIO_PORT) { > > struct kvm_regs regs; > > > > vcpu_regs_get(vcpu, ®s); > > - memcpy(&ucall, addr_gva2hva(vcpu->vm, (vm_vaddr_t)regs.rdi), > > - sizeof(ucall)); > > - > > - vcpu_run_complete_io(vcpu); > > - if (uc) > > - memcpy(uc, &ucall, sizeof(ucall)); > > + return regs.rdi; > > } > > - > > - return ucall.cmd; > > + return 0; > > } > > -- > > 2.37.1.455.g008518b4e5-goog > >