From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 AB5223019D6 for ; Tue, 14 Apr 2026 22:39:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776206341; cv=none; b=IshIW8y//N9w5Nr6IVBxFA94DvJy/lYHZ4ulVFPJKTdTGZ5El2tMTQtdWnWb1FJOaVc1+ks54fA5iuy84yMCSU+CdmxSxNQbWuNZXb/P7iVPVQX1KdiWzDgaInH0bGWK/aGmXbAPMywb8yH3xK5Py0B3p16DullGjfd+iULJPUY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776206341; c=relaxed/simple; bh=jyNNu6CF3tM975V6PcONy5U9p2ftLc0N1pfgPHoAIaY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=X8BCZE2xudGez0PN7pr9EZ19/hs1DLI7Y9pNmm5B3MLseoQEKzIvrckUSvqfOxhHgYqAsMNu6FKeE+Wdjrxr1XCc2QVp/zfRVh++N+b3qFtyY8JDoMQZuSt71uE4V8qEKG7FLGB8eo+bsqAuvjVjrHJmm/BVvxJ+6ZOnAqzWpTc= 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=MUFnMN4A; arc=none smtp.client-ip=209.85.210.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="MUFnMN4A" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-82f07078eaaso3262697b3a.0 for ; Tue, 14 Apr 2026 15:39:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776206340; x=1776811140; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=stm35/IY3TODjr+mpw9XhLFzvYJAzwSOp9T5zlC+DrI=; b=MUFnMN4ANRzgFqwRky9PFVLn0BcMAc4H+eWZJrGAf2FtSaYixTAgBT4TRwo4NTgCMt 3wkgLvO+JzlJVI4WKydzkoC/6uygWr8Yh+AlYbzUd0QqtcmCXXSDYZDwj0S/v0rvavUd o45/bHnLAz8ZM6vSpYolFTZL4JFWGcgSQzfQ3RPIyC0oJ0JnVM1Z2ut6VAM+Nim/iE/j xLvoXLVIWBO0zx/cDSju+9y/YGbCAiIP4MVHLSTcHP9SIghk9pqZqKh0rd04sjeOhgki rpZ1FyzhoAKNnn0FsPW5Qi9konRYk8I0hz0XcKPbYU5gUphNMcTlmOD9D3lDdM05kQHN jdvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776206340; x=1776811140; h=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=stm35/IY3TODjr+mpw9XhLFzvYJAzwSOp9T5zlC+DrI=; b=oSYt5/YzkyhU39Q8+ObC339cBCw71FOjgGxHPaTuxC3wZABGaWzlxVEUUV5PS7fN4D 71sx8dny92+/Eng1je2cI02k9ppKBk2MAkKSD4e9h6Dr7IOlWAUbGy+6iqoq3nGe8Hgr n8aGrTtc6awFwQYZegGPIKbbEnH9fh5makHniQ21q6QuQ20cAuiyjwJGe0uj7z34XYwN st7QI3/1ADPTMZlO97gQ7uBUMWAoOm/fV1EDhDVzL2yXPiykB5j4p1NPdKxr7IGfmrNC EeTGtU7EO/cymxaBTBjKvgYV/vvTt9T+Ct8gHGk6Z5eyMNv+ZKs2aEStduxaCJQPnISi KwTQ== X-Forwarded-Encrypted: i=1; AFNElJ9cnpPKuUjmmSG+D7w5bCfY3c5GciKJusy11jMT1wVv+LYNUKfKGIDmyzYPItljdmQXrdiOl0ElYYNw8iI=@vger.kernel.org X-Gm-Message-State: AOJu0Yw+Erzk0NkKw+J9GyySTjUsi69ZPfyAX0fTC0OK2JpRlEjmOlSN xmxb85LoSW1/AblsuPENz9t/dKaQZ/3CUVro/tzSkWR8MCRaiFm+S7hK4xnZ/19FGULyVNAg9p8 rL/Vlcw== X-Received: from pfbdh21.prod.google.com ([2002:a05:6a00:4795:b0:829:f706:70e4]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:a223:b0:82a:7471:eb90 with SMTP id d2e1a72fcca58-82f0c299062mr19287033b3a.30.1776206339773; Tue, 14 Apr 2026 15:38:59 -0700 (PDT) Date: Tue, 14 Apr 2026 15:38:58 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260409213333.1995382-1-seanjc@google.com> Message-ID: Subject: Re: [PATCH] KVM: Drop kvm_vcpu.ready to squash race where "ready" can get stuck "true" From: Sean Christopherson To: Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, zhanghao , Wanpeng Li Content-Type: text/plain; charset="us-ascii" On Tue, Apr 14, 2026, Paolo Bonzini wrote: > On 4/9/26 23:33, Sean Christopherson wrote: > > +static inline bool kvm_vcpu_is_runnable_and_scheduled_out(struct kvm_vcpu *vcpu) > > +{ > > + return READ_ONCE(vcpu->preempted) || > > + (READ_ONCE(vcpu->scheduled_out) && > > + READ_ONCE(vcpu->wants_to_run) && > > wants_to_run doesn't seem important here, because blocking will never be set > outside KVM_RUN (unlike scheduled_out which can be set within any > vcpu_load/vcpu_put pair, if you're unlucky enough). Oh, good point. > > + READ_ONCE(vcpu->stat.generic.blocking) && > > + !kvm_vcpu_is_blocking(vcpu)); > > If you get here you have done the finish_rcuwait() in kvm_vcpu_block(), > meaning that you've been already scheduled in, haven't you? Gah, yes. I didn't realize finish_rcuwait() is what actually completes the wakeup from KVM's perspective. > So, you would need something like this: > > static inline bool kvm_vcpu_is_runnable_and_scheduled_out(struct kvm_vcpu *vcpu) > { > if (READ_ONCE(vcpu->preempted)) > return true; > > if (!READ_ONCE(vcpu->scheduled_out)) > return false; > if (!READ_ONCE(vcpu->stat.generic.blocking)) Hmm, I think this could actually be: if (!kvm_vcpu_is_blocking(vcpu)) return false; Because my use of vcpu->stat.generic.blocking was purely due to missing that finish_rcuwait() is effectively what clears "blocking". That would narrow the window for false positives a little, e.g. would at least wait until after the kvm_arch_vcpu_blocking() call to treat the vCPU as blocking. > return false; > return rcuwait_was_woken(kvm_arch_vcpu_get_wait(vcpu)); > } > > // in rcuwait.h > static inline bool rcuwait_was_woken(struct rcuwait *w) > { > guard(rcu)(); > struct task_struct *t = rcu_access_pointer(w->task); > return t && !task_is_runnable(t); Ah, and I missed the task_is_runnable() check guarding vcpu->ready. I suspect I assumed kvm_vcpu_on_spin() would do that check.