From nobody Sun Apr 26 16: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 E4739C43334 for ; Fri, 24 Jun 2022 17:19:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232105AbiFXRS7 (ORCPT ); Fri, 24 Jun 2022 13:18:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39922 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231199AbiFXRSx (ORCPT ); Fri, 24 Jun 2022 13:18:53 -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 EA2DADD6 for ; Fri, 24 Jun 2022 10:18:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1656091130; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3ufRTEzneH/kSvKyDxUKVLcSwThbG62Wnx3AVy3a8ow=; b=XmE4ABpLfq/HFSQf/Gee2dCgzI0jmjgNj7Mxz1+tMD7Bk8jjeUp6g0QXa83IVKyLUEkPmE xgYwZlRp533ZC5ywBf9r+ShWWqjTfjYGjCkAgXps0ZBLglIGMaJdOhLXxj76pSuOJvFyY0 q3Rb48wt+JVJrS64XokcEF4vcjWY23M= 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-641-xmyJLnTsMjeZute6Im-4Cw-1; Fri, 24 Jun 2022 13:18:49 -0400 X-MC-Unique: xmyJLnTsMjeZute6Im-4Cw-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 349BF3C0E22D; Fri, 24 Jun 2022 17:18:49 +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 1D673492C3B; Fri, 24 Jun 2022 17:18:49 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Subject: [PATCH v2 1/8] KVM: x86: complete fast IN directly with complete_emulator_pio_in() Date: Fri, 24 Jun 2022 13:18:41 -0400 Message-Id: <20220624171848.2801602-2-pbonzini@redhat.com> In-Reply-To: <20220624171848.2801602-1-pbonzini@redhat.com> References: <20220624171848.2801602-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Use complete_emulator_pio_in() directly when completing fast PIO, there's no need to bounce through emulator_pio_in(): the comment about ECX changing doesn't apply to fast PIO, which isn't used for string I/O. No functional change intended. Signed-off-by: Paolo Bonzini --- arch/x86/kvm/x86.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index d88600e41ff8..d66a873f4427 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8871,11 +8871,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); --=20 2.31.1 From nobody Sun Apr 26 16: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 46253C43334 for ; Fri, 24 Jun 2022 17:19:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232159AbiFXRTP (ORCPT ); Fri, 24 Jun 2022 13:19:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38578 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229480AbiFXRSz (ORCPT ); Fri, 24 Jun 2022 13:18:55 -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 E12572DAAF for ; Fri, 24 Jun 2022 10:18:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1656091132; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jGYFcUhWUfpQReKmOabENcUHhZ3P86Vd8MgvEnhAkXA=; b=ZC4p3yW/g0QOyW3v7SgS07pvoyRh1iG1ERsrkx6HKQLPX8HoGWm8G1zwOUf3mXvroMZK4h x2A+BK+UXf+4MDwV89l3QyM+f6wcCcmLlGwWXtZu+Vg9l4aC69pAx4heVgbyXT6VBuZXzg uG9QAYt6cdjwZDpwxOwVhBSG9rtqwmQ= 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-199-zn4jSehBNpiJla3JuzhOvA-1; Fri, 24 Jun 2022 13:18:49 -0400 X-MC-Unique: zn4jSehBNpiJla3JuzhOvA-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 539322919EC1; Fri, 24 Jun 2022 17:18:49 +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 3C9A1492C3B; Fri, 24 Jun 2022 17:18:49 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Subject: [PATCH v2 2/8] KVM: x86: inline kernel_pio into its sole caller Date: Fri, 24 Jun 2022 13:18:42 -0400 Message-Id: <20220624171848.2801602-3-pbonzini@redhat.com> In-Reply-To: <20220624171848.2801602-1-pbonzini@redhat.com> References: <20220624171848.2801602-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 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. No functional change intended. Signed-off-by: Paolo Bonzini --- arch/x86/kvm/x86.c | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index d66a873f4427..524a96d26399 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7575,37 +7575,31 @@ 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: 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 Sun Apr 26 16: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 CB676C43334 for ; Fri, 24 Jun 2022 17:19:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232312AbiFXRTV (ORCPT ); Fri, 24 Jun 2022 13:19:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40036 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232135AbiFXRS4 (ORCPT ); Fri, 24 Jun 2022 13:18:56 -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 3E9F968035 for ; Fri, 24 Jun 2022 10:18:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1656091133; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JFkbFE9Pn4FMqfM3ZOYs/tb5dPTZApHuXMpgEvAcvYo=; b=f8xwCJZirKYaELdOcgMziHbVNIc+m6TMecndNuHeaeK/7ttkVq8XwGUSZlERcxebY9iJSd kmqJY55Z3t1nhAn4kMUkldbxMkR+wdSW9KKAzAT33MgBYdOZYlUYJafZa0f0euJlWMek23 j94v5t+9VL0nKaPyefH4HaUQOWsfMks= 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-547-cj6eGGT4O7Otfs5tffU3Fw-1; Fri, 24 Jun 2022 13:18:49 -0400 X-MC-Unique: cj6eGGT4O7Otfs5tffU3Fw-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 72E7085A589; Fri, 24 Jun 2022 17:18:49 +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 5BD6E492C3B; Fri, 24 Jun 2022 17:18:49 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Subject: [PATCH v2 3/8] KVM: x86: drop PIO from unregistered devices Date: Fri, 24 Jun 2022 13:18:43 -0400 Message-Id: <20220624171848.2801602-4-pbonzini@redhat.com> In-Reply-To: <20220624171848.2801602-1-pbonzini@redhat.com> References: <20220624171848.2801602-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" KVM protects the device list with SRCU, and therefore different calls to kvm_io_bus_read()/kvm_io_bus_write() can very well see different incarnations of kvm->buses. If userspace unregisters a device while vCPUs are running there is no well-defined result. This patch applies a safe fallback by returning early from emulator_pio_in_out(). This corresponds to returning zeroes from IN, and dropping the writes on the floor for OUT. Signed-off-by: Paolo Bonzini --- arch/x86/kvm/x86.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 524a96d26399..5a56d39bd81f 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7593,8 +7593,19 @@ static int emulator_pio_in_out(struct kvm_vcpu *vcpu= , int size, 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; + + if (r) { + if (i =3D=3D 0) + goto userspace_io; + + /* + * Userspace must have unregistered the device while PIO + * was running. Drop writes / read as 0 (the buffer + * was zeroed in __emulator_pio_in). + */ + break; + } + data +=3D size; } return 1; @@ -7606,7 +7617,6 @@ static int emulator_pio_in_out(struct kvm_vcpu *vcpu,= int size, vcpu->run->io.data_offset =3D KVM_PIO_PAGE_OFFSET * PAGE_SIZE; vcpu->run->io.count =3D count; vcpu->run->io.port =3D port; - return 0; } =20 --=20 2.31.1 From nobody Sun Apr 26 16: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 869CCC43334 for ; Fri, 24 Jun 2022 17:19:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232320AbiFXRTa (ORCPT ); Fri, 24 Jun 2022 13:19:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232136AbiFXRS4 (ORCPT ); Fri, 24 Jun 2022 13:18:56 -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 3D4AE49B72 for ; Fri, 24 Jun 2022 10:18:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1656091133; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1hrqZhznmf7in1ihxZqdR3dERc2u3SLv+rWTvwfVqKo=; b=BFrbNP7NHzglPAbdlsb1XL2zDCg9a6VsHPV3eNBNGTvcJh//qKNCT29Pd5w+p2NeBFEO/n Tnc5CjlR+7q5R15uvHE3yIGbpBQR7jvhMplAYVK8VJw2+4tUO+wCUxkADBbf6uJK7kJt4i Jk0VFCYQ9ldNJV87Y4QAdgUuNlkm0Kg= 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-203-xDrcKZkpNrW1sS-BI2gNnQ-1; Fri, 24 Jun 2022 13:18:49 -0400 X-MC-Unique: xDrcKZkpNrW1sS-BI2gNnQ-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9472E81D9DA; Fri, 24 Jun 2022 17:18:49 +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 7B040492C3B; Fri, 24 Jun 2022 17:18:49 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Subject: [PATCH v2 4/8] KVM: x86: move all vcpu->arch.pio* setup in emulator_pio_in_out() Date: Fri, 24 Jun 2022 13:18:44 -0400 Message-Id: <20220624171848.2801602-5-pbonzini@redhat.com> In-Reply-To: <20220624171848.2801602-1-pbonzini@redhat.com> References: <20220624171848.2801602-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 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. The vcpu->arch.pio* fields still need to be filled even when the PIO is handled in-kernel as __emulator_pio_in() is always followed by complete_emulator_pio_in(). But after fixing that, it will be possible to 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 | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h index bc85622e28b2..2120d7c060a9 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 5a56d39bd81f..368d0d4d56ff 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7576,17 +7576,25 @@ 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) { + /* The buffer is only used in complete_emulator_pio_in(). */ + WARN_ON(data); + 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) @@ -7623,9 +7631,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) @@ -7674,9 +7680,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 Sun Apr 26 16: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 A519AC43334 for ; Fri, 24 Jun 2022 17:19:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232152AbiFXRTK (ORCPT ); Fri, 24 Jun 2022 13:19:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38962 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232111AbiFXRSy (ORCPT ); Fri, 24 Jun 2022 13:18:54 -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 E1BB74ECD9 for ; Fri, 24 Jun 2022 10:18:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1656091131; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Fg5ajEwsaDOi0mRWYQQiQq63ErgdP2nROPRnrIqW490=; b=DfjEY49OucQywoMQe6S2qaUqLMmlR0gdi3RRsQtaaeAJKvFqBDSI6ubji4rx1W+XcfSB0A G2z0qoJ3zJ/vHO8VrehDNB3kZ/vdyeEaQU4GRqFdIKpa8RRWR9zzUDR+sby+YCUWv6BSb6 kS+g8Xx7CTte6xRI3e5Dx/6GlmG5quY= 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-498-gbrX7sijMUqUT3oXtBPjnA-1; Fri, 24 Jun 2022 13:18:50 -0400 X-MC-Unique: gbrX7sijMUqUT3oXtBPjnA-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B36F72919EC2; Fri, 24 Jun 2022 17:18:49 +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 9C2A7492C3B; Fri, 24 Jun 2022 17:18:49 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Subject: [PATCH v2 5/8] KVM: x86: wean in-kernel PIO from vcpu->arch.pio* Date: Fri, 24 Jun 2022 13:18:45 -0400 Message-Id: <20220624171848.2801602-6-pbonzini@redhat.com> In-Reply-To: <20220624171848.2801602-1-pbonzini@redhat.com> References: <20220624171848.2801602-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 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; but also vcpu->pio.size must not be used in kvm_sev_es_outs(), because it will not be updated for in-kernel OUT. Signed-off-by: Paolo Bonzini --- arch/x86/kvm/x86.c | 73 ++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 41 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 368d0d4d56ff..716ae5c92687 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7583,19 +7583,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) { - /* The buffer is only used in complete_emulator_pio_in(). */ - WARN_ON(data); - 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); @@ -7608,9 +7595,10 @@ static int emulator_pio_in_out(struct kvm_vcpu *vcpu= , int size, =20 /* * Userspace must have unregistered the device while PIO - * was running. Drop writes / read as 0 (the buffer - * was zeroed in __emulator_pio_in). + * was running. Drop writes / read as 0. */ + if (in) + memset(data, 0, size * (count - i)); break; } =20 @@ -7619,6 +7607,16 @@ static int emulator_pio_in_out(struct kvm_vcpu *vcpu= , int size, return 1; =20 userspace_io: + 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; @@ -7629,15 +7627,19 @@ static int emulator_pio_in_out(struct kvm_vcpu *vcp= u, 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) { int size =3D vcpu->arch.pio.size; - unsigned count =3D vcpu->arch.pio.count; + unsigned int count =3D vcpu->arch.pio.count; memcpy(val, vcpu->arch.pio_data, size * count); trace_kvm_pio(KVM_PIO_IN, vcpu->arch.pio.port, size, count, vcpu->arch.pi= o_data); vcpu->arch.pio.count =3D 0; @@ -7654,16 +7656,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. */ - } else { - int r =3D __emulator_pio_in(vcpu, size, port, count); - if (!r) - return r; - - /* Results already available, fall through. */ + complete_emulator_pio_in(vcpu, val); + return 1; } =20 - complete_emulator_pio_in(vcpu, val); - return 1; + return __emulator_pio_in(vcpu, size, port, val, count); } =20 static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt, @@ -7678,14 +7675,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, @@ -13245,7 +13236,7 @@ static int kvm_sev_es_outs(struct kvm_vcpu *vcpu, u= nsigned int size, =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; + vcpu->arch.sev_pio_data +=3D count * size; if (!ret) break; =20 @@ -13261,20 +13252,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; @@ -13286,11 +13277,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 Sun Apr 26 16: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 5ED88C433EF for ; Fri, 24 Jun 2022 17:19:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232194AbiFXRTB (ORCPT ); Fri, 24 Jun 2022 13:19:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232110AbiFXRSx (ORCPT ); Fri, 24 Jun 2022 13:18:53 -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 98D6626117 for ; Fri, 24 Jun 2022 10:18:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1656091131; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LpEiLmZkg93bGPFh7PJljlKo06GeGjtWHik84ASBrwI=; b=gwTJPNqE57hHN1PEhvkTRxMLq0n/bCgA8CTLBbNpeDMrylef+F2fofDdDPTjx/nw8uccbw SAxP9SDpPeM4foVPvnisloaCZxqwMyIDQobIeQLaT3b4nkWg6Ix7Zs/fG+8zBsZKUv2n3y R9OmutbHpffSwkqRF3SExRh29N9b5bc= 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-551-6tfvGRB6NTik30nH9iH82Q-1; Fri, 24 Jun 2022 13:18:50 -0400 X-MC-Unique: 6tfvGRB6NTik30nH9iH82Q-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D2C8F1818801; Fri, 24 Jun 2022 17:18:49 +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 BB94D492C3B; Fri, 24 Jun 2022 17:18:49 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Subject: [PATCH v2 6/8] KVM: x86: wean fast IN from emulator_pio_in Date: Fri, 24 Jun 2022 13:18:46 -0400 Message-Id: <20220624171848.2801602-7-pbonzini@redhat.com> In-Reply-To: <20220624171848.2801602-1-pbonzini@redhat.com> References: <20220624171848.2801602-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Use __emulator_pio_in() directly for fast PIO instead of bouncing through emulator_pio_in() now that __emulator_pio_in() fills "val" when handling in-kernel PIO. vcpu->arch.pio.count is guaranteed to be '0', so this a pure nop. 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 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 716ae5c92687..b824ffc63b17 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8886,7 +8886,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 Sun Apr 26 16: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 56427C433EF for ; Fri, 24 Jun 2022 17:19:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232201AbiFXRTe (ORCPT ); Fri, 24 Jun 2022 13:19:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39536 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231228AbiFXRSx (ORCPT ); Fri, 24 Jun 2022 13:18:53 -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 E1D2C4FC63 for ; Fri, 24 Jun 2022 10:18:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1656091131; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=M2VwHIzlgf6suh30aCXnA2oy96t8J5oIFCGlpyPJlL0=; b=hGFUvUq74DaZIiH8waaYrc64Ll1O+5UZH5IqI1/JsLndDQKJ13y0zSw35YUliJM8EvSr36 milW6ZpqHdkrPpVkoh7o6W/+upy0g/X7jQbKC0GiyR8ZPZMZKpzN5UHavNQiQ0ngc2gnYk QAvXOUEQMzt7KiZb3D5siWLalidSpZo= 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-663-TBuJJVdHPdGQ85PvV6Cz5Q-1; Fri, 24 Jun 2022 13:18:50 -0400 X-MC-Unique: TBuJJVdHPdGQ85PvV6Cz5Q-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F26CA801231; Fri, 24 Jun 2022 17:18:49 +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 DAD87492C3B; Fri, 24 Jun 2022 17:18:49 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Subject: [PATCH v2 7/8] KVM: x86: de-underscorify __emulator_pio_in Date: Fri, 24 Jun 2022 13:18:47 -0400 Message-Id: <20220624171848.2801602-8-pbonzini@redhat.com> In-Reply-To: <20220624171848.2801602-1-pbonzini@redhat.com> References: <20220624171848.2801602-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 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 | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index b824ffc63b17..1a017a5a680b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7626,8 +7626,8 @@ 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, - unsigned short port, void *val, unsigned int count) +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); if (r) @@ -7645,9 +7645,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. @@ -7660,15 +7662,7 @@ static int emulator_pio_in(struct kvm_vcpu *vcpu, in= t size, return 1; } =20 - return __emulator_pio_in(vcpu, size, port, val, count); -} - -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); - + return emulator_pio_in(vcpu, size, port, val, count); } =20 static int emulator_pio_out(struct kvm_vcpu *vcpu, int size, @@ -8886,7 +8880,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; @@ -13277,7 +13271,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 Sun Apr 26 16: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 C8075C433EF for ; Fri, 24 Jun 2022 17:19:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232297AbiFXRTR (ORCPT ); Fri, 24 Jun 2022 13:19:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232140AbiFXRS4 (ORCPT ); Fri, 24 Jun 2022 13:18:56 -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 CCB06680A3 for ; Fri, 24 Jun 2022 10:18:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1656091133; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=VvU7t8xFORBUFemBDoOZqJ1ShL7jV39QnUV8wtDd2Po=; b=AWcMS/HT9KJAkgKCyUUlvwtDrSbjEb5XxV9IAAq+ajXS5BKqdHYlhpUbda85SezFI20Onp c24+7zl+AohJzDKfKeIIFRFd5pnKNMT2cm8HSS5eIBbtjp0MTqcIKtkkQW1pdkEHTldTWH DujhvlRw0uQPXSKxblJNR/ooZWVCRaI= 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-53-_-PGNz0UO3maiMH9t1iTtw-1; Fri, 24 Jun 2022 13:18:50 -0400 X-MC-Unique: _-PGNz0UO3maiMH9t1iTtw-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1DF9F3C0E22E; Fri, 24 Jun 2022 17:18:50 +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 063BF492C3B; Fri, 24 Jun 2022 17:18:50 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Subject: [PATCH v2 8/8] KVM: SEV-ES: reuse advance_sev_es_emulated_ins for OUT too Date: Fri, 24 Jun 2022 13:18:48 -0400 Message-Id: <20220624171848.2801602-9-pbonzini@redhat.com> In-Reply-To: <20220624171848.2801602-1-pbonzini@redhat.com> References: <20220624171848.2801602-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 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 1a017a5a680b..567d13405445 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13206,6 +13206,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 @@ -13229,8 +13235,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 * size; + advance_sev_es_emulated_pio(vcpu, count, size); if (!ret) break; =20 @@ -13246,12 +13251,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; @@ -13259,7 +13258,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; @@ -13275,7 +13274,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