From nobody Tue Apr 28 01:11:34 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 D8530CCA47E for ; Wed, 8 Jun 2022 12:13:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239057AbiFHMNZ (ORCPT ); Wed, 8 Jun 2022 08:13:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37422 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238907AbiFHMNF (ORCPT ); Wed, 8 Jun 2022 08:13:05 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D17A725F90D for ; Wed, 8 Jun 2022 05:12:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1654690377; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7hkgfT4Y/KkWaSvKTff3vE46TyR/ZoWx2c0NUGxnBns=; b=KZ62abVIC8bRWLn548+peHlQiEDFybve/pnsU3fYDFlrTWnQh5cqUhPoIGjGljeS4uOg43 ZRrJScwQ15qYsj14vBPBDgYq4AP7YTsNBRdNaRVEVl4S79lZuR/EX5YPzr4o0Vg0EMWnoK 1wG4XM5I1HmYHXghv82WaIPxsYFTra4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-204-0AgD7xcWMfGBFK-CtPi1tw-1; Wed, 08 Jun 2022 08:12:54 -0400 X-MC-Unique: 0AgD7xcWMfGBFK-CtPi1tw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2B893101AA4D; Wed, 8 Jun 2022 12:12:54 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0F7701415100; Wed, 8 Jun 2022 12:12:54 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com Subject: [PATCH 1/6] KVM: x86: inline kernel_pio into its sole caller Date: Wed, 8 Jun 2022 08:12:48 -0400 Message-Id: <20220608121253.867333-2-pbonzini@redhat.com> In-Reply-To: <20220608121253.867333-1-pbonzini@redhat.com> References: <20220608121253.867333-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The caller of kernel_pio already has arguments for most of what kernel_pio fishes out of vcpu->arch.pio. This is the first step towards ensuring that vcpu->arch.pio.* is only used when exiting to userspace. We can now also WARN if emulated PIO performs successful in-kernel iteratio= ns before having to fall back to userspace. The code is not ready for that, a= nd it should never happen. No functional change intended. Signed-off-by: Paolo Bonzini --- arch/x86/kvm/x86.c | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 79efdc19b4c8..2f9100f2564e 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7415,37 +7415,32 @@ static int emulator_cmpxchg_emulated(struct x86_emu= late_ctxt *ctxt, return emulator_write_emulated(ctxt, addr, new, bytes, exception); } =20 -static int kernel_pio(struct kvm_vcpu *vcpu, void *pd) -{ - int r =3D 0, i; - - for (i =3D 0; i < vcpu->arch.pio.count; i++) { - if (vcpu->arch.pio.in) - r =3D kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port, - vcpu->arch.pio.size, pd); - else - r =3D kvm_io_bus_write(vcpu, KVM_PIO_BUS, - vcpu->arch.pio.port, vcpu->arch.pio.size, - pd); - if (r) - break; - pd +=3D vcpu->arch.pio.size; - } - return r; -} - static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size, unsigned short port, unsigned int count, bool in) { + void *data =3D vcpu->arch.pio_data; + unsigned i; + int r; + vcpu->arch.pio.port =3D port; vcpu->arch.pio.in =3D in; - vcpu->arch.pio.count =3D count; + vcpu->arch.pio.count =3D count; vcpu->arch.pio.size =3D size; =20 - if (!kernel_pio(vcpu, vcpu->arch.pio_data)) - return 1; + for (i =3D 0; i < count; i++) { + if (in) + r =3D kvm_io_bus_read(vcpu, KVM_PIO_BUS, port, size, data); + else + r =3D kvm_io_bus_write(vcpu, KVM_PIO_BUS, port, size, data); + if (r) + goto userspace_io; + data +=3D size; + } + return 1; =20 +userspace_io: + WARN_ON(i !=3D 0); vcpu->run->exit_reason =3D KVM_EXIT_IO; vcpu->run->io.direction =3D in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; vcpu->run->io.size =3D size; --=20 2.31.1 From nobody Tue Apr 28 01:11:34 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 B9F29CCA47B for ; Wed, 8 Jun 2022 12:13:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238977AbiFHMNU (ORCPT ); Wed, 8 Jun 2022 08:13:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238883AbiFHMNA (ORCPT ); Wed, 8 Jun 2022 08:13:00 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D1A3825F91F for ; Wed, 8 Jun 2022 05:12:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1654690377; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GO+EJ1aLPgZO8T1Z66MtdKjDRuL7ln6n4VX5hpJLuQA=; b=bPXKGhhmDyArUzfCP7bq6vfeheJoPXTwgBfY3YDgf71e/SNU8VXxpJzUdbAujcA1l2V/E3 BC25yKYFmlKXdR8bdxlyPCumH+NFjETc7bcVs7bwMrQcyk+XLN11dNBqEqlUb+jT6aBNSh zoHYIMBROc8MJonJFYSpr3Vf1EuUCPc= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-383-kqWRqFtzMOSH1D9dfVpCFQ-1; Wed, 08 Jun 2022 08:12:54 -0400 X-MC-Unique: kqWRqFtzMOSH1D9dfVpCFQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5072E3C02B75; Wed, 8 Jun 2022 12:12:54 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 33B821415102; Wed, 8 Jun 2022 12:12:54 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com Subject: [PATCH 2/6] KVM: x86: move all vcpu->arch.pio* setup in emulator_pio_in_out Date: Wed, 8 Jun 2022 08:12:49 -0400 Message-Id: <20220608121253.867333-3-pbonzini@redhat.com> In-Reply-To: <20220608121253.867333-1-pbonzini@redhat.com> References: <20220608121253.867333-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" For now, this is basically an excuse to add back the void* argument to the function, while removing some knowledge of vcpu->arch.pio* from its callers. The WARN that vcpu->arch.pio.count is zero is also extended to OUT operations. We cannot do more as long as we have __emulator_pio_in always followed by complete_emulator_pio_in, which uses the vcpu->arch.pio* fields. But after fixing that, it will be possible to only populate the vcpu->arch.pio* fields on userspace exits. No functional change intended. Signed-off-by: Paolo Bonzini --- arch/x86/kvm/trace.h | 2 +- arch/x86/kvm/x86.c | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h index fd28dd40b813..2877c0e92823 100644 --- a/arch/x86/kvm/trace.h +++ b/arch/x86/kvm/trace.h @@ -154,7 +154,7 @@ TRACE_EVENT(kvm_xen_hypercall, =20 TRACE_EVENT(kvm_pio, TP_PROTO(unsigned int rw, unsigned int port, unsigned int size, - unsigned int count, void *data), + unsigned int count, const void *data), TP_ARGS(rw, port, size, count, data), =20 TP_STRUCT__entry( diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 2f9100f2564e..8e1e76d0378b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7416,17 +7416,22 @@ static int emulator_cmpxchg_emulated(struct x86_emu= late_ctxt *ctxt, } =20 static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size, - unsigned short port, + unsigned short port, void *data, unsigned int count, bool in) { - void *data =3D vcpu->arch.pio_data; unsigned i; int r; =20 + WARN_ON_ONCE(vcpu->arch.pio.count); vcpu->arch.pio.port =3D port; vcpu->arch.pio.in =3D in; vcpu->arch.pio.count =3D count; vcpu->arch.pio.size =3D size; + if (in) + memset(vcpu->arch.pio_data, 0, size * count); + else + memcpy(vcpu->arch.pio_data, data, size * count); + data =3D vcpu->arch.pio_data; =20 for (i =3D 0; i < count; i++) { if (in) @@ -7454,9 +7459,7 @@ static int emulator_pio_in_out(struct kvm_vcpu *vcpu,= int size, static int __emulator_pio_in(struct kvm_vcpu *vcpu, int size, unsigned short port, unsigned int count) { - WARN_ON(vcpu->arch.pio.count); - memset(vcpu->arch.pio_data, 0, size * count); - return emulator_pio_in_out(vcpu, size, port, count, true); + return emulator_pio_in_out(vcpu, size, port, NULL, count, true); } =20 static void complete_emulator_pio_in(struct kvm_vcpu *vcpu, void *val) @@ -7505,9 +7508,8 @@ static int emulator_pio_out(struct kvm_vcpu *vcpu, in= t size, { int ret; =20 - memcpy(vcpu->arch.pio_data, val, size * count); - trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data); - ret =3D emulator_pio_in_out(vcpu, size, port, count, false); + trace_kvm_pio(KVM_PIO_OUT, port, size, count, val); + ret =3D emulator_pio_in_out(vcpu, size, port, (void *)val, count, false); if (ret) vcpu->arch.pio.count =3D 0; =20 --=20 2.31.1 From nobody Tue Apr 28 01:11:34 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 723F5C433EF for ; Wed, 8 Jun 2022 12:13:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238929AbiFHMNK (ORCPT ); Wed, 8 Jun 2022 08:13:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37018 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238880AbiFHMNA (ORCPT ); Wed, 8 Jun 2022 08:13:00 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B761325BF8C for ; Wed, 8 Jun 2022 05:12:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1654690377; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7ZZuPTlsr0p/zFJmJe7+skZgHwdJ/X5lOwubK5xlBlQ=; b=feGV13HsHa3RnTzYrEzP86wWy0gMWCZYSWpzFO/v+Zs5PmLL3OUzOmGXYNyNDOLypvZoG3 nybX3O7SE1e+jUYbvqZh11fAM4SL1+GUYmo5BwtnpGRUlWJI+6OswlfUKLkJHlhepj7MSo lNLClGYiXtnP0eCQhXLoYuehhjCITi8= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-226-KYvN-GiRPma_N-xghQT3Ig-1; Wed, 08 Jun 2022 08:12:54 -0400 X-MC-Unique: KYvN-GiRPma_N-xghQT3Ig-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 74C39101AA52; Wed, 8 Jun 2022 12:12:54 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5886B1415102; Wed, 8 Jun 2022 12:12:54 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com Subject: [PATCH 3/6] KVM: x86: wean in-kernel PIO from vcpu->arch.pio* Date: Wed, 8 Jun 2022 08:12:50 -0400 Message-Id: <20220608121253.867333-4-pbonzini@redhat.com> In-Reply-To: <20220608121253.867333-1-pbonzini@redhat.com> References: <20220608121253.867333-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Make emulator_pio_in_out operate directly on the provided buffer as long as PIO is handled inside KVM. For input operations, this means that, in the case of in-kernel PIO, __emulator_pio_in does not have to be always followed by complete_emulator_pio_in. This affects emulator_pio_in and kvm_sev_es_ins; for the latter, that is why the call moves from advance_sev_es_emulated_ins to complete_sev_es_emulated_ins. For output, it means that vcpu->pio.count is never set unnecessarily and there is no need to clear it. Signed-off-by: Paolo Bonzini --- arch/x86/kvm/x86.c | 61 ++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 34 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 8e1e76d0378b..3b641cd2ff6f 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7423,16 +7423,6 @@ static int emulator_pio_in_out(struct kvm_vcpu *vcpu= , int size, int r; =20 WARN_ON_ONCE(vcpu->arch.pio.count); - vcpu->arch.pio.port =3D port; - vcpu->arch.pio.in =3D in; - vcpu->arch.pio.count =3D count; - vcpu->arch.pio.size =3D size; - if (in) - memset(vcpu->arch.pio_data, 0, size * count); - else - memcpy(vcpu->arch.pio_data, data, size * count); - data =3D vcpu->arch.pio_data; - for (i =3D 0; i < count; i++) { if (in) r =3D kvm_io_bus_read(vcpu, KVM_PIO_BUS, port, size, data); @@ -7446,6 +7436,16 @@ static int emulator_pio_in_out(struct kvm_vcpu *vcpu= , int size, =20 userspace_io: WARN_ON(i !=3D 0); + vcpu->arch.pio.port =3D port; + vcpu->arch.pio.in =3D in; + vcpu->arch.pio.count =3D count; + vcpu->arch.pio.size =3D size; + + if (in) + memset(vcpu->arch.pio_data, 0, size * count); + else + memcpy(vcpu->arch.pio_data, data, size * count); + vcpu->run->exit_reason =3D KVM_EXIT_IO; vcpu->run->io.direction =3D in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; vcpu->run->io.size =3D size; @@ -7457,9 +7457,13 @@ static int emulator_pio_in_out(struct kvm_vcpu *vcpu= , int size, } =20 static int __emulator_pio_in(struct kvm_vcpu *vcpu, int size, - unsigned short port, unsigned int count) + unsigned short port, void *val, unsigned int count) { - return emulator_pio_in_out(vcpu, size, port, NULL, count, true); + int r =3D emulator_pio_in_out(vcpu, size, port, val, count, true); + if (r) + trace_kvm_pio(KVM_PIO_IN, port, size, count, val); + + return r; } =20 static void complete_emulator_pio_in(struct kvm_vcpu *vcpu, void *val) @@ -7482,16 +7486,11 @@ static int emulator_pio_in(struct kvm_vcpu *vcpu, i= nt size, * shenanigans as KVM doesn't support modifying the rep count, * and the emulator ensures @count doesn't overflow the buffer. */ + complete_emulator_pio_in(vcpu, val); + return 1; } else { - int r =3D __emulator_pio_in(vcpu, size, port, count); - if (!r) - return r; - - /* Results already available, fall through. */ + return __emulator_pio_in(vcpu, size, port, val, count); } - - complete_emulator_pio_in(vcpu, val); - return 1; } =20 static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt, @@ -7506,14 +7505,8 @@ static int emulator_pio_out(struct kvm_vcpu *vcpu, i= nt size, unsigned short port, const void *val, unsigned int count) { - int ret; - trace_kvm_pio(KVM_PIO_OUT, port, size, count, val); - ret =3D emulator_pio_in_out(vcpu, size, port, (void *)val, count, false); - if (ret) - vcpu->arch.pio.count =3D 0; - - return ret; + return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false); } =20 static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt, @@ -13064,20 +13057,20 @@ static int kvm_sev_es_outs(struct kvm_vcpu *vcpu,= unsigned int size, static int kvm_sev_es_ins(struct kvm_vcpu *vcpu, unsigned int size, unsigned int port); =20 -static void advance_sev_es_emulated_ins(struct kvm_vcpu *vcpu) +static void advance_sev_es_emulated_ins(struct kvm_vcpu *vcpu, unsigned co= unt, int size) { - unsigned count =3D vcpu->arch.pio.count; - complete_emulator_pio_in(vcpu, vcpu->arch.sev_pio_data); vcpu->arch.sev_pio_count -=3D count; - vcpu->arch.sev_pio_data +=3D count * vcpu->arch.pio.size; + vcpu->arch.sev_pio_data +=3D count * size; } =20 static int complete_sev_es_emulated_ins(struct kvm_vcpu *vcpu) { + unsigned count =3D vcpu->arch.pio.count; int size =3D vcpu->arch.pio.size; int port =3D vcpu->arch.pio.port; =20 - advance_sev_es_emulated_ins(vcpu); + complete_emulator_pio_in(vcpu, vcpu->arch.sev_pio_data); + advance_sev_es_emulated_ins(vcpu, count, size); if (vcpu->arch.sev_pio_count) return kvm_sev_es_ins(vcpu, size, port); return 1; @@ -13089,11 +13082,11 @@ static int kvm_sev_es_ins(struct kvm_vcpu *vcpu, = unsigned int size, for (;;) { unsigned int count =3D min_t(unsigned int, PAGE_SIZE / size, vcpu->arch.sev_pio_count); - if (!__emulator_pio_in(vcpu, size, port, count)) + if (!__emulator_pio_in(vcpu, size, port, vcpu->arch.sev_pio_data, count)) break; =20 /* Emulation done by the kernel. */ - advance_sev_es_emulated_ins(vcpu); + advance_sev_es_emulated_ins(vcpu, count, size); if (!vcpu->arch.sev_pio_count) return 1; } --=20 2.31.1 From nobody Tue Apr 28 01:11:34 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 98701C43334 for ; Wed, 8 Jun 2022 12:13:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239231AbiFHMNw (ORCPT ); Wed, 8 Jun 2022 08:13:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37066 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238887AbiFHMNA (ORCPT ); Wed, 8 Jun 2022 08:13:00 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0B66425F92E for ; Wed, 8 Jun 2022 05:12:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1654690378; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ggbC2+qLXyMIhkrl+egmbVWenv3iYa8SCHIeX6BJa58=; b=h9E8+/A3nloIPuNKw86gBNZqRI7I8VHbGS3+iQdUHBMSKuj2unfxovQRzQRUMINsWS1bw1 9EFXOPzDG2yB5wvdZOkOq9tTK2nYKPKuz6B59d6WoomMsj/vTdrRftf4xu5xC9F216qu6L Zbab5NsHGMUrtM9acXtipRUFt2+/oCM= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-397-3brLBe_ZM4KIhFhmgCQHuw-1; Wed, 08 Jun 2022 08:12:55 -0400 X-MC-Unique: 3brLBe_ZM4KIhFhmgCQHuw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 999ED811E83; Wed, 8 Jun 2022 12:12:54 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7D1371415100; Wed, 8 Jun 2022 12:12:54 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com Subject: [PATCH 4/6] KVM: x86: wean fast IN from emulator_pio_in Date: Wed, 8 Jun 2022 08:12:51 -0400 Message-Id: <20220608121253.867333-5-pbonzini@redhat.com> In-Reply-To: <20220608121253.867333-1-pbonzini@redhat.com> References: <20220608121253.867333-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Now that __emulator_pio_in already fills "val" for in-kernel PIO, it is both simpler and clearer not to use emulator_pio_in. Use the appropriate function in kvm_fast_pio_in and complete_fast_pio_in, respectively __emulator_pio_in and complete_emulator_pio_in. emulator_pio_in_emulated is now the last caller of emulator_pio_in. No functional change intended. Signed-off-by: Paolo Bonzini --- arch/x86/kvm/x86.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 3b641cd2ff6f..aefcc71a7040 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8692,11 +8692,7 @@ static int complete_fast_pio_in(struct kvm_vcpu *vcp= u) /* For size less than 4 we merge, else we zero extend */ val =3D (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0; =20 - /* - * Since vcpu->arch.pio.count =3D=3D 1 let emulator_pio_in perform - * the copy and tracing - */ - emulator_pio_in(vcpu, vcpu->arch.pio.size, vcpu->arch.pio.port, &val, 1); + complete_emulator_pio_in(vcpu, &val); kvm_rax_write(vcpu, val); =20 return kvm_skip_emulated_instruction(vcpu); @@ -8711,7 +8707,7 @@ static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int= size, /* For size less than 4 we merge, else we zero extend */ val =3D (size < 4) ? kvm_rax_read(vcpu) : 0; =20 - ret =3D emulator_pio_in(vcpu, size, port, &val, 1); + ret =3D __emulator_pio_in(vcpu, size, port, &val, 1); if (ret) { kvm_rax_write(vcpu, val); return ret; --=20 2.31.1 From nobody Tue Apr 28 01:11:34 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 C91F5C433EF for ; Wed, 8 Jun 2022 12:13:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239013AbiFHMNW (ORCPT ); Wed, 8 Jun 2022 08:13:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37396 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238899AbiFHMNF (ORCPT ); Wed, 8 Jun 2022 08:13:05 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 21EE825F934 for ; Wed, 8 Jun 2022 05:12:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1654690378; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/cd0pfqoZ+Edyksi+uqsAtQqSZ+Labql7FxF90QuYp4=; b=OIlaRzzDq4IHPIQNj8U4Qb8cPMWx+0Mucs4QHD3nVkjJctPWk3LyafeuFosF7MdbrB9Tj1 +A3sn75lm+F5Z7x/zyYpuRZOLc8mKdBzPuEf+RSHGNkwBwaN1kQh5X3Xb0ntci0EDH69W5 EK/dJr8+SkYw8pKWgvJRHSUDoCbL7A4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-544-8IKirdguNSeXiLotzos6Ew-1; Wed, 08 Jun 2022 08:12:55 -0400 X-MC-Unique: 8IKirdguNSeXiLotzos6Ew-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BF30685A588; Wed, 8 Jun 2022 12:12:54 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id A1D1F1415100; Wed, 8 Jun 2022 12:12:54 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com Subject: [PATCH 5/6] KVM: x86: de-underscorify __emulator_pio_in Date: Wed, 8 Jun 2022 08:12:52 -0400 Message-Id: <20220608121253.867333-6-pbonzini@redhat.com> In-Reply-To: <20220608121253.867333-1-pbonzini@redhat.com> References: <20220608121253.867333-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Now all callers except emulator_pio_in_emulated are using __emulator_pio_in/complete_emulator_pio_in explicitly. Move the "either copy the result or attempt PIO" logic in emulator_pio_in_emulated, and rename __emulator_pio_in to just emulator_pio_in. Signed-off-by: Paolo Bonzini --- arch/x86/kvm/x86.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index aefcc71a7040..fd4382602f65 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7456,7 +7456,7 @@ static int emulator_pio_in_out(struct kvm_vcpu *vcpu,= int size, return 0; } =20 -static int __emulator_pio_in(struct kvm_vcpu *vcpu, int size, +static int emulator_pio_in(struct kvm_vcpu *vcpu, int size, unsigned short port, void *val, unsigned int count) { int r =3D emulator_pio_in_out(vcpu, size, port, val, count, true); @@ -7475,9 +7475,11 @@ static void complete_emulator_pio_in(struct kvm_vcpu= *vcpu, void *val) vcpu->arch.pio.count =3D 0; } =20 -static int emulator_pio_in(struct kvm_vcpu *vcpu, int size, - unsigned short port, void *val, unsigned int count) +static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt, + int size, unsigned short port, void *val, + unsigned int count) { + struct kvm_vcpu *vcpu =3D emul_to_vcpu(ctxt); if (vcpu->arch.pio.count) { /* * Complete a previous iteration that required userspace I/O. @@ -7489,18 +7491,10 @@ static int emulator_pio_in(struct kvm_vcpu *vcpu, i= nt size, complete_emulator_pio_in(vcpu, val); return 1; } else { - return __emulator_pio_in(vcpu, size, port, val, count); + return emulator_pio_in(vcpu, size, port, val, count); } } =20 -static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt, - int size, unsigned short port, void *val, - unsigned int count) -{ - return emulator_pio_in(emul_to_vcpu(ctxt), size, port, val, count); - -} - static int emulator_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port, const void *val, unsigned int count) @@ -8707,7 +8701,7 @@ static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int= size, /* For size less than 4 we merge, else we zero extend */ val =3D (size < 4) ? kvm_rax_read(vcpu) : 0; =20 - ret =3D __emulator_pio_in(vcpu, size, port, &val, 1); + ret =3D emulator_pio_in(vcpu, size, port, &val, 1); if (ret) { kvm_rax_write(vcpu, val); return ret; @@ -13078,7 +13072,7 @@ static int kvm_sev_es_ins(struct kvm_vcpu *vcpu, un= signed int size, for (;;) { unsigned int count =3D min_t(unsigned int, PAGE_SIZE / size, vcpu->arch.sev_pio_count); - if (!__emulator_pio_in(vcpu, size, port, vcpu->arch.sev_pio_data, count)) + if (!emulator_pio_in(vcpu, size, port, vcpu->arch.sev_pio_data, count)) break; =20 /* Emulation done by the kernel. */ --=20 2.31.1 From nobody Tue Apr 28 01:11:34 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 15B8EC433EF for ; Wed, 8 Jun 2022 12:13:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238950AbiFHMNP (ORCPT ); Wed, 8 Jun 2022 08:13:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37398 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238902AbiFHMNF (ORCPT ); Wed, 8 Jun 2022 08:13:05 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 50F8425FC73 for ; Wed, 8 Jun 2022 05:12:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1654690378; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rgGwuxnu2XznHcyK86tFDRI4P8omTRGQ5ct2XA85Ky0=; b=cUVZVAVcvHqmjeNnzostigYAr4fM8FxWSY6PbdikJGikOhMfWk6jEfYfDfqzR/EuZYImW0 SXecNdZiLtTefBStSdkkwLSZS65jmw4OzDKiS54eJiWbBQ8ap65tuwyxO9sieS58Orf1J9 pGcuX298+YGT/Wj0mV6eAMup/gI7fi0= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-471-hCayFzAGO-Sw-RzWpq7kpQ-1; Wed, 08 Jun 2022 08:12:55 -0400 X-MC-Unique: hCayFzAGO-Sw-RzWpq7kpQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E41D53C02B7B; Wed, 8 Jun 2022 12:12:54 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id C78BE1415100; Wed, 8 Jun 2022 12:12:54 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com Subject: [PATCH 6/6] KVM: SEV-ES: reuse advance_sev_es_emulated_ins for OUT too Date: Wed, 8 Jun 2022 08:12:53 -0400 Message-Id: <20220608121253.867333-7-pbonzini@redhat.com> In-Reply-To: <20220608121253.867333-1-pbonzini@redhat.com> References: <20220608121253.867333-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" complete_emulator_pio_in only has to be called by complete_sev_es_emulated_ins now; therefore, all that the function does now is adjust sev_pio_count and sev_pio_data. Which is the same for both IN and OUT. No functional change intended. Signed-off-by: Paolo Bonzini --- arch/x86/kvm/x86.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index fd4382602f65..a3651aa74ed7 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13007,6 +13007,12 @@ int kvm_sev_es_mmio_read(struct kvm_vcpu *vcpu, gp= a_t gpa, unsigned int bytes, } EXPORT_SYMBOL_GPL(kvm_sev_es_mmio_read); =20 +static void advance_sev_es_emulated_pio(struct kvm_vcpu *vcpu, unsigned co= unt, int size) +{ + vcpu->arch.sev_pio_count -=3D count; + vcpu->arch.sev_pio_data +=3D count * size; +} + static int kvm_sev_es_outs(struct kvm_vcpu *vcpu, unsigned int size, unsigned int port); =20 @@ -13030,8 +13036,7 @@ static int kvm_sev_es_outs(struct kvm_vcpu *vcpu, u= nsigned int size, int ret =3D emulator_pio_out(vcpu, size, port, vcpu->arch.sev_pio_data, = count); =20 /* memcpy done already by emulator_pio_out. */ - vcpu->arch.sev_pio_count -=3D count; - vcpu->arch.sev_pio_data +=3D count * vcpu->arch.pio.size; + advance_sev_es_emulated_pio(vcpu, count, size); if (!ret) break; =20 @@ -13047,12 +13052,6 @@ static int kvm_sev_es_outs(struct kvm_vcpu *vcpu, = unsigned int size, static int kvm_sev_es_ins(struct kvm_vcpu *vcpu, unsigned int size, unsigned int port); =20 -static void advance_sev_es_emulated_ins(struct kvm_vcpu *vcpu, unsigned co= unt, int size) -{ - vcpu->arch.sev_pio_count -=3D count; - vcpu->arch.sev_pio_data +=3D count * size; -} - static int complete_sev_es_emulated_ins(struct kvm_vcpu *vcpu) { unsigned count =3D vcpu->arch.pio.count; @@ -13060,7 +13059,7 @@ static int complete_sev_es_emulated_ins(struct kvm_= vcpu *vcpu) int port =3D vcpu->arch.pio.port; =20 complete_emulator_pio_in(vcpu, vcpu->arch.sev_pio_data); - advance_sev_es_emulated_ins(vcpu, count, size); + advance_sev_es_emulated_pio(vcpu, count, size); if (vcpu->arch.sev_pio_count) return kvm_sev_es_ins(vcpu, size, port); return 1; @@ -13076,7 +13075,7 @@ static int kvm_sev_es_ins(struct kvm_vcpu *vcpu, un= signed int size, break; =20 /* Emulation done by the kernel. */ - advance_sev_es_emulated_ins(vcpu, count, size); + advance_sev_es_emulated_pio(vcpu, count, size); if (!vcpu->arch.sev_pio_count) return 1; } --=20 2.31.1