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 34FFA3BAD92 for ; Thu, 25 Jun 2026 22:36:30 +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=1782426991; cv=none; b=WdVoJAb0DoipOXakhzfT/Gz0NCKy2jfU6r+qrsE3Mw9X4Prgtvtwa67joeHroAOlkw5CUdrpqJ1lnx7gXO46MUdtHhs94fp8Nj4hY1CCnMaH9E7fl38Lu59D7e9MZegJ6bK1Mrl8PvPjZcCXU3J/AZXACrSxcETGR7ei6mXPLlw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782426991; c=relaxed/simple; bh=kL83qZkJZi4Aj2Er+eHbDD6C37v6WYXWwKerURyzRrY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SyX6XuSqMMiDOuVUrVncWbCSoZsxN4C5SHOOX/MC03kzuy+koznoLuunpmOuSOOnDqBEHYUVn03yfxxXGqi8NxkwYwuUWk/RbueumAHknK1qFrpxsR+d2XJDbZb1ENHyyqF93lsGyf82oAN94YCoWnY3Ni9SYkqqJJoLJrn7zJ8= 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=OlGXV6um; 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="OlGXV6um" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-c8bad49b171so159885a12.3 for ; Thu, 25 Jun 2026 15:36:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1782426989; x=1783031789; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=nYe2f5ROlk+rOZAgZLHnz85wyy4NST3pKojrwpDQPdM=; b=OlGXV6umzbN5Ek/RJ6USetBIRekCG3u9/LQa+SQN3Sa5CwzrDfPGj9du4hACKkVylQ IopKRQmelpUjSALyR3XaRVE2x78DlwjuHaVkSAvbFHg7V0V/nCL9W5Gan+Ao3CfM0VK1 5cVohgzZvs1TJf0s16yUqwB343jcaPKF/MyQkKX6M1as39VmsjoJ43OqBOEtE//L2crI JRFlxbqGnx9QHL2qynP41C3pHCRpUdq+nvZ+BUz59bJDYLdi/EhY0u3FcjDuxIIek4yo 716A+a1CbTxG4opxdR2shfYami9F6NfhK7xnnpOd/gRHev0acKteICl+FkFs2DBzGrJy 4s3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782426989; x=1783031789; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=nYe2f5ROlk+rOZAgZLHnz85wyy4NST3pKojrwpDQPdM=; b=Djz72lIeaATj3dvOuqa+gAd8Im+hZtkrxsQpumwvo4YQvHSWUkBnfcwd+DpDr/N1IA 5Pg7ODIrp3bRNhEUoi6wB/Vxp3sYpNuYHFfGMC2yi0Lea960ihI2Xo8W07GyVXy63xrF oeMtTbituOhVDMC2SIYTWHJzXMmku1d23+Tx9W9vhh9vurppO1zdK8NER9Lj6z2zQN/n Bmm2T5sLrjfVr/LpOeBim6WEwBTmDJn7Vs+03/pMuC8O5Mb4x3u7SAPmidS3d9f7WT5k KTuh87h1Zkq/BIs7K3UtDeBCXuozaDq3FL3T+cIVT/CxvKlbMG2csL9np21irxB3enRc MOLA== X-Forwarded-Encrypted: i=1; AFNElJ/ky/Yad4UFB5j/l+ow8PM/+At4SpxHinrvQxKkmjQTh8yrAXvuloAD/KOPzeiApzKrGob+iyNCYT0kAcg=@vger.kernel.org X-Gm-Message-State: AOJu0YxDi7S3a39lIjVd7os3Gz1OaXJsNLPklc8QsWUgR1D704wz+3eJ IW9iDhy143nHSl3xdINXw17lmjJzEHZaEolNIc3BCbIpZY6Jz2gsoJUXFwKjxqR5XYtzJk+b0Ud Ytjd6vw== X-Received: from pgbgf14.prod.google.com ([2002:a05:6a02:2cce:b0:c92:3cad:b6c5]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6300:404c:b0:3b3:4406:202c with SMTP id adf61e73a8af0-3bd4ac79687mr5015657637.10.1782426989277; Thu, 25 Jun 2026 15:36:29 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 25 Jun 2026 15:36:17 -0700 In-Reply-To: <20260625223623.3376478-1-seanjc@google.com> 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> X-Mailer: git-send-email 2.55.0.rc0.799.gd6f94ed593-goog Message-ID: <20260625223623.3376478-5-seanjc@google.com> Subject: [PATCH v3 04/10] KVM: x86/xen: Punt singleshot timer hcalls to userspace if Xen vCPU ID isn't set From: Sean Christopherson To: Vitaly Kuznetsov , Sean Christopherson , Paolo Bonzini , David Woodhouse , Paul Durrant Cc: 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" 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. Using the vCPU's index as its default Xen ID is reasonable in concept, but in practice is horribly flawed as the index is left as '0' until after vCPU initialization completes, i.e. every vCPU gets a Xen ID of '0' by default. Forward hypercalls to userspace instead of trying to salvage any kind of default behavior, as all userspace implementations that support multiple vCPUs either don't enable the timer, are guaranteed to set Xen ID, or work only because *all* guests also screw up the singleshot timer hypercalls. The last scenarios is extremely unlikely given that Linux-as-a-guest uses the actual Xen vCPU ID when making timer hypercalls. In other words, for all intents and purposes, KVM's ABI is already that userspace must set the Xen vCPU ID, so just commit to that ABI. 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. Link: https://lore.kernel.org/all/20260612233017.1F9771F000E9@smtp.kernel.org Suggested-by: David Woodhouse Signed-off-by: Sean Christopherson --- arch/x86/kvm/xen.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 694b31c1fcc9..27c1aeeab8af 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -1610,6 +1611,9 @@ static bool kvm_xen_hcall_vcpu_op(struct kvm_vcpu *vcpu, bool longmode, int cmd, if (!kvm_xen_timer_enabled(vcpu)) return false; + if (vcpu->arch.xen.vcpu_id == XEN_VCPU_ID_INVALID) + return false; + switch (cmd) { case VCPUOP_set_singleshot_timer: if (vcpu->arch.xen.vcpu_id != vcpu_id) { @@ -2299,7 +2303,7 @@ static bool kvm_xen_hcall_evtchn_send(struct kvm_vcpu *vcpu, u64 param, u64 *r) void kvm_xen_init_vcpu(struct kvm_vcpu *vcpu) { - vcpu->arch.xen.vcpu_id = vcpu->vcpu_idx; + vcpu->arch.xen.vcpu_id = XEN_VCPU_ID_INVALID; vcpu->arch.xen.poll_evtchn = 0; timer_setup(&vcpu->arch.xen.poll_timer, cancel_evtchn_poll, 0); -- 2.55.0.rc0.799.gd6f94ed593-goog