From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.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 73D3D3F8EC3 for ; Fri, 26 Jun 2026 14:27:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782484075; cv=none; b=OCuUdawCjg+U8kmWkVDwR6GWQGiGLb63I5lxg8LIqFfSGPPBKgkvIGy/PBSeqbxKQfW4bOZ89JkdtWPz5fWrFyN1t6BklEV61A3j0/KZqRH30L8DUFZHM3Lip9jFXn1AeCNChL2JK/nsVcMFaKf2htVmXcSy/TvD+3AJmEt8D40= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782484075; c=relaxed/simple; bh=j74mLa4an6FUKWcGbPm5zIuMfCmCm8jiEpyP9u+oRMU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sGhvqBp2KkjEmsRbgBYDaiiSRaY8CECm4h8KbwlTw2Yq3lg02QiGBewdBHtuafQwriqNpl6edh/W66082vEZh613sm9RSNKg1kfDmnuzUugF7RBgrLCQQ9fo4W7dwhvs3HXYFGM7u4/8cK3bKoyhMPd1HC8PdFTRZ08mNbW/UXc= 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=lgco9qfK; arc=none smtp.client-ip=209.85.214.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="lgco9qfK" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2c6a9bbcc53so10109815ad.2 for ; Fri, 26 Jun 2026 07:27:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1782484068; x=1783088868; 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=cCJJmjJsx9ZpoPuR0fj0nKjX7eTBmSdWBoum/bdsThU=; b=lgco9qfKmV6qETXpFHw3OUczF/0PHsaUKQdqAgpAW618cM8+oFlas1jr9HZiMlzTm0 nts1PFIFfdjKfHYu9+BgLtLp9+HxGmkGemrM6iTUDoEPnHBu49RJev3y1q+ASXtb2ArZ dVWqADCrah1QgiiyxNSZ/bRVG0p8NHR68m1mbdoRcKgxfOJ+sYLu/kNBxt4QML+ynHrW WEnZetWy0d1cNba2nYlGg2MWfTg1NZLPi9PYsgM7nql6s30ZkLVA/nDHJUxCGF+WhHaG 1yuz6vj4KXM/dR/zO4wbpYXVV8alcF3cWbNr52DS/N8Lo6K/PcCOlpf4ypJXA6yP4NRk sP3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782484068; x=1783088868; 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=cCJJmjJsx9ZpoPuR0fj0nKjX7eTBmSdWBoum/bdsThU=; b=FqJRJ0vFJPOiDXvliQF6qagu8thhP3hILye2Cb1khHoCLXiCux/jRc/6P00XDmwV56 NNSKNFYIDKDxakklBxaP7hY4/Q0ECb0r21wLxE8LB/CLOUrluQLEVIeLhAmAI8HofrC+ 7nex7nJZZ0QEDecx7hudNvF/yXeUK7XTHg4YgUJQ3SwJqungs8QjBV+uBaO11Uvl9XEv PTFswDClr/GPggHzQs3FSTngrUsKDcVnB+BpmWETtMdO4jHhQTLVgpN/nHlFEl4/usho vC4edyRkZAawvWawsj6HDIlJ0VbNejFJgwWDrEDgqrLFwgDtVMuDM4Whg/FFoU2dl7nx uEfw== X-Forwarded-Encrypted: i=1; AHgh+RqeXKMtrprk2GvpHXnUHQjGVZrf0jBjiVr8Ms/i0iPe2OXT3hBGrFLcaUYDg++oJpcDVfE6AXivNo7MwWk=@vger.kernel.org X-Gm-Message-State: AOJu0Yyr1SSHT39okGWBJaouYImhJatfJL3zYRub/5/Gmh1dAMhSh2Ka yOogyuSnV/z5mLcWITUwES2Nm25gOmpwkPl8phSo7tcxqCLKuQZg5xbkrnzdj/0Rni9haNhJ0SX dW6fLsQ== X-Received: from plqt1.prod.google.com ([2002:a17:902:a5c1:b0:2c0:a677:42a6]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:3b88:b0:2c1:ea95:8297 with SMTP id d9443c01a7336-2c7fc75a973mr71403235ad.7.1782484067938; Fri, 26 Jun 2026 07:27:47 -0700 (PDT) Date: Fri, 26 Jun 2026 07:27:47 -0700 In-Reply-To: <123bd3932db959a9c26b9ede46632e482dbdc4ab.camel@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260625223623.3376478-1-seanjc@google.com> <20260625223623.3376478-5-seanjc@google.com> <123bd3932db959a9c26b9ede46632e482dbdc4ab.camel@infradead.org> Message-ID: Subject: Re: [PATCH v3 04/10] KVM: x86/xen: Punt singleshot timer hcalls to userspace if Xen vCPU ID isn't set From: Sean Christopherson To: David Woodhouse Cc: Vitaly Kuznetsov , Paolo Bonzini , Paul Durrant , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+5b32c49cd8f005e65654@syzkaller.appspotmail.com, syzbot+5d2b94b77112148d1744@syzkaller.appspotmail.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable On Fri, Jun 26, 2026, David Woodhouse wrote: > On Thu, 2026-06-25 at 15:36 -0700, Sean Christopherson wrote: > > Explicitly invalidate KVM's internal Xen vCPU ID during vCPU creation > > instead of *trying* to set the Xen ID to the vCPU index by default, and > > forward singleshot timer hypercalls to userspace if the VMM hasn't set = the > > Xen ID via KVM_XEN_VCPU_ATTR_TYPE_VCPU_ID.=C2=A0 Using the vCPU's index= as its > > default Xen ID is reasonable in concept, but in practice is horribly fl= awed > > as the index is left as '0' until after vCPU initialization completes, = i.e. > > every vCPU gets a Xen ID of '0' by default. > >=20 > > Forward hypercalls to userspace instead of trying to salvage any kind o= f > > default behavior, as all userspace implementations that support multipl= e > > vCPUs either don't enable the timer, are guaranteed to set Xen ID, or w= ork > > only because *all* guests also screw up the singleshot timer hypercalls= . > > The last scenarios is extremely unlikely given that Linux-as-a-guest us= es > > the actual Xen vCPU ID when making timer hypercalls.=C2=A0 In other wor= ds, for > > all intents and purposes, KVM's ABI is already that userspace must set = the > > Xen vCPU ID, so just commit to that ABI. > >=20 > > Note, KVM's handling of KVM_XEN_VCPU_ATTR_TYPE_VCPU_ID restricts the ID= to > > KVM_MAX_VCPUS, so there's no chance of a valid ID colliding with -1u. >=20 > Nit: XEN_VCPU_ID_INVALID isn't -1u; it's U32_MAX. >=20 > I'd have been slightly happier if you'd used an explicit -1U, perhaps > with a name of its own (although I guess we wouldn't want to call it > KVM_VCPU_IDX_INVALID in the *Xen* part). Ya, I was 50/50 on having KVM define its own INVALID macro versus reusing w= hat the Xen-the-guest has. I didn't want to open code the literal, because tha= t would require open coding a somewhat magical value in multiple locations. = And having XEN_VCPU_ID_INVALID and KVM_XEN_VCPU_ID_INVALID, with the same value= , seemed even worse than reusing a macro that isn't strictly owned by KVM. What if I also add a compile-timer assertion? In the end, KVM doesn't care= what value XEN_VCPU_ID_INVALID uses, so long as it can't colled with what is all= owed by KVM_XEN_VCPU_ATTR_TYPE_VCPU_ID. diff --git arch/x86/kvm/xen.c arch/x86/kvm/xen.c index 3ed6686e0a1a..5c60ed9ef2cc 100644 --- arch/x86/kvm/xen.c +++ arch/x86/kvm/xen.c @@ -1103,6 +1103,8 @@ int kvm_xen_vcpu_set_attr(struct kvm_vcpu *vcpu, stru= ct kvm_xen_vcpu_attr *data) break; =20 case KVM_XEN_VCPU_ATTR_TYPE_VCPU_ID: + BUILD_BUG_ON(XEN_VCPU_ID_INVALID < KVM_MAX_VCPUS); + if (data->u.vcpu_id >=3D KVM_MAX_VCPUS) r =3D -EINVAL; else {