From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (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 B49475474F for ; Tue, 3 Mar 2026 02:24:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772504656; cv=none; b=pc2erzDIc5aR6MhydF1g1sqbAnobzSKh+GOctoofv5i+GRO3JoboI4TqUJzd9kEpiOxyW93RSjtxnqgC30iJfPdO04Y2VTrB5wMQhPw5U3CVNZWcyhJ+MjSaXXvkvidwOm/AkhK4HOsS9GqswHy57oP9JasJjr0I/5VDM6eDMu4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772504656; c=relaxed/simple; bh=uZtpxAFpi1G6SUIWdfOSzJAaL5AiGi7GdvzVrHawOYw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XGBwsZUkmSxM3Dyir6OHDm5IDyOz7ZDcsD0IvmCczL1JyWoCiMGwa1z9RBtungWPhqq1lyo6xBKZtp3O8AJ+gO06vwRRQkku7uG1aTXxFu8r4XRXTARzR2b2gE4mgROm69FMwoJ/eVANmeNh5UXU8g8hgpB6sQyBAdhafEyfKKs= 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=C6cHuxXy; arc=none smtp.client-ip=209.85.215.201 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="C6cHuxXy" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b6ce1b57b9cso4120470a12.1 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=lists.linux.dev; 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=C6cHuxXyllNWlTqd6Q/4mybZ8bwPxK3+RvFpAVrduUEvLZWrNv3dv/k49gih/byJao u8L2JUA2hfRHPSSLson1IgL6bHOvFHYxbPFWQMVTTHIGPjf5OEK4cnZbo2yW0cvse+6J gvIV+v78lsU8To8Md1wq1g6LwfhotWx3P7fylLkOWUVQHzsKIjVRYyNXq06a8ogYIT1i UKsfxPofV+u5Xo5KFVSMcltehpwQRLUZSDsFdMPU2lJlMjCiAkHP4ifPJyr1HfPNSstC LSKmXVGOrZFy2bnlNTzKvdmgRtF27zr4QknuLhEK72T+6GelNLCYU+v0gezh0K4+J36j +c1Q== 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=vtaEzN8S2p/r6jmz8BP9572ZwxzHN805CwBwuAfAmJGOsbtF7/0sHAbt5HwRHeI2bZ uLjC8exy9uUGP50v5AbW3HPfqTftFop2PSMAIin8VTvkIripEl4FihM/dk9rZRzk6kQ8 WGSzhCj5ehGitcfFdXGX+JiCPVWLtjg7w4XYIGN6J2HI9tK1cV07xa2+/RRLbsH24cmp 9sJTH8Dp2nrJfMKAY/Eab0IbDNxdsYgzMAZjH8LoAplswVbqteddCEURySk9r/qyEaRU ybsgwD2MkGY09tQD2aUnbSPVfkP0vCxF3xcTfWll9DWe7tIsSAxRgD/FTub+sqA2Bbhx Js8Q== X-Forwarded-Encrypted: i=1; AJvYcCXRCmOUM4/yvXx1H5Hj6drcFCOwz4aTWOPEbVUe7ij+aGAjCOTb69iAn/FpOTlJVL/Rt2xzsh7wfYTN@lists.linux.dev X-Gm-Message-State: AOJu0YzjQyn/rpsNc79S6n98IaEChMx9SflNUkPeOCaxlhDm85ZlvToC I2zpNF+XrrJgEft39EVAgtBFBA/g/M0XhkNxS1pQIusj22dhAUGCZaPBOD9rBhLCqaEPx+9Ix3Q +iFEx9A== 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-coco@lists.linux.dev 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); }