From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1208F1D8E01 for ; Tue, 3 Mar 2026 02:24:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772504657; cv=none; b=HzjDYEY6FKdPFNl7lwvOb3Om4SE7jfphw+dVjQmkW6hOPTT3xmxR5naYx4JuhnKbpkCvc/n/7++Ave93sfnWLkWIM8MA+rrzKHmPX+kraQaidNboT/gThmlnkuAeWL+Uy+WfW86IsalxEJbgXcxEL+Bi8MatHz7FbvRN4coMmTg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772504657; c=relaxed/simple; bh=uZtpxAFpi1G6SUIWdfOSzJAaL5AiGi7GdvzVrHawOYw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ASfHvTuxEJu2zJs/Z6+3210Caewwx3ZEUrCnuYGM02looah5pPnHrWCNBiwht5bzC5JMDmK+KswXiawULzBNOFKdCozEIuOFaUbgqdTftQR8Egdmjs6zOmOA9/3e8SKLaip6oBzNvk2DdypmgwxSixnWMXJEVOPcexEf8zpYafo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Urvx+Gfx; arc=none smtp.client-ip=209.85.215.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Urvx+Gfx" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b630b4d8d52so3288234a12.3 for ; Mon, 02 Mar 2026 18:24:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772504655; x=1773109455; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=hgEoULUEc/jsh9Z2U/YxOmzx6Hq08vsikzOFMt8dIRY=; b=Urvx+GfxXqd7qOYMf2GsZaY+OEp1xr6uEL+tYQpmFz7gekuDQ9jojxvGzimEC8t8Oo PSPx5PWC4BTGDu16DvWA2YiafR6BBP5q0LsyqmoyzpkiF2W93B71mZeQZHDuYNh6pN6D qxciI3wP6tgZJnjnGAfYXq4ipDO0g/6xYuYwNhKj84QjAI4R0+uXA6/hdnXpOBpnSDnQ rkgD4DNTAWYdFuvUGU3S2vutcH+8axBLt7tvFYrLvZtsyhO2PrdgKHgK6KDucm3Pe1fO LYbvYfuNMvXXi4Tyfo47kU7YP8oboeVx4/pZTmIM+dIbBnDpTFBBlGzkD9rKzdf/5VtA 0BZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772504655; x=1773109455; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=hgEoULUEc/jsh9Z2U/YxOmzx6Hq08vsikzOFMt8dIRY=; b=ByKPI9q69LKcCTw8hzBO3eJPc8vsWldZL6dh5sViyrgDO4PkqHG5oIsHPpLuUST3P3 Fb6pR1P0gnDGK7h3VP2nouDp9qCWzCHYXVPQBqZxzgAXAs2QIUGmJUUAm/4PyB4hzOoD EugPqChOqhaCdaHtAn2F9XLF74hXlfSDftYro0Lu/bFZhAombVtOwfMNucQxwpYX++rQ zbXTgYmqNKZp8+LEya6+qE1MdTL+7zE4IzYnDT8CX01OOmXbIYm2zSUb+sfZvaiUBspV C9c6DoYvWfb7Z+6oYHKN3YAlspm9q3+StqYwy+0PCLTlf7ni9OOkzOyz3h+FtRvKOY48 Fu9Q== X-Forwarded-Encrypted: i=1; AJvYcCWorB65PpNm0j71CBvtEqkChjAwYI6Q/0cQ+4EXj4+rVo65kYlpVEBjek1eUfHAF4qobVlCU+MxTsGbCTI=@vger.kernel.org X-Gm-Message-State: AOJu0YwnfV4h5hLHAKTC5omc7qqUGAan98DarSJUwD2sgZUgv8dhvUpA HerH3wZ2EnXBqHerVhhfLUZIue6QD5ZtXeP87nf7QrlAKHPCY7I29QnUJuFte5Zft9OYwyI8Ubg Xh4jZuQ== X-Received: from pgnn19.prod.google.com ([2002:a05:6a02:4f13:b0:c5e:9fe:7560]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:258d:b0:395:151c:4eda with SMTP id adf61e73a8af0-395c3ae7748mr13870512637.45.1772504654755; Mon, 02 Mar 2026 18:24:14 -0800 (PST) Date: Mon, 2 Mar 2026 18:24:13 -0800 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260225012049.920665-1-seanjc@google.com> <20260225012049.920665-15-seanjc@google.com> <8bed2406e9f5f30f8f01c1da731fae6e040da827.camel@intel.com> Message-ID: Subject: Re: [PATCH 14/14] KVM: x86: Add helpers to prepare kvm_run for userspace MMIO exit From: Sean Christopherson To: Rick P Edgecombe Cc: "pbonzini@redhat.com" , "kas@kernel.org" , "x86@kernel.org" , "zhangjiaji1@huawei.com" , "binbin.wu@linux.intel.com" , Xiaoyao Li , "linux-kernel@vger.kernel.org" , "thomas.lendacky@amd.com" , "kvm@vger.kernel.org" , "linux-coco@lists.linux.dev" , "michael.roth@amd.com" Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable On Wed, Feb 25, 2026, Sean Christopherson wrote: > On Wed, Feb 25, 2026, Rick P Edgecombe wrote: > > On Tue, 2026-02-24 at 17:20 -0800, Sean Christopherson wrote: > > > +static inline void __kvm_prepare_emulated_mmio_exit(struct kvm_vcpu = *vcpu, > > > + =C2=A0=C2=A0=C2=A0 gpa_t gpa, unsigned int len, > > > + =C2=A0=C2=A0=C2=A0 const void *data, > > > + =C2=A0=C2=A0=C2=A0 bool is_write) > > > +{ > > > + struct kvm_run *run =3D vcpu->run; > > > + > > > + run->mmio.len =3D min(8u, len); > >=20 > > I would think to extract this to a local var so it can be used twice. >=20 > Ya, either way works for me. The copy+paste is a little gross, but it's = also > unlikely that anyone is going to modify this code (or if they did, that a= ny goofs > wouldn't be immediately disastrous). Ooh, better idea. Since TDX is the only direct user of __kvm_prepare_emulated_mmio_exit() and it only supports lenths of 1, 2, 4, = and 8, kvm_prepare_emulated_mmio_exit() is the only path that actually needs to ca= p the length. Then the inner helper can assert a valid length. Doesn't change a= nything in practice, but I like the idea of making the caller be aware of the limit= ation (even if that caller is itself a helper). static inline void __kvm_prepare_emulated_mmio_exit(struct kvm_vcpu *vcpu, gpa_t gpa, unsigned int len, const void *data, bool is_write) { struct kvm_run *run =3D vcpu->run; KVM_BUG_ON(len > 8, vcpu->kvm); run->mmio.len =3D len; run->mmio.is_write =3D is_write; run->exit_reason =3D KVM_EXIT_MMIO; run->mmio.phys_addr =3D gpa; if (is_write) memcpy(run->mmio.data, data, len); } static inline void kvm_prepare_emulated_mmio_exit(struct kvm_vcpu *vcpu, struct kvm_mmio_fragment *frag) { WARN_ON_ONCE(!vcpu->mmio_needed || !vcpu->mmio_nr_fragments); __kvm_prepare_emulated_mmio_exit(vcpu, frag->gpa, min(8u, frag->len), frag->data, vcpu->mmio_is_write); }