From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f193.google.com (mail-pf1-f193.google.com [209.85.210.193]) (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 7A18129B8DB for ; Fri, 19 Dec 2025 03:54:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.193 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766116458; cv=none; b=cYwD4QzZDIqxkNqhGnwn11VP77ku2A61ab7C3uTNJyqpAVbn+MFgK9qPsWdDPRMy5wlpq81NpRFdABEfeJMOtmMNySqbvame4fRBuHgiUK61687kEdFm2Kz718uFYt6gwFo6ApIrozEnBVWlmd3RIr6np6LViO2SxJziq9MOTow= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766116458; c=relaxed/simple; bh=5W48IL3bdW1PhdyVuywXEKn2tfcpwop424aGol93d3U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=U7J2Ud4wJFao0iPUqFzEr7ywzaVvAhq3pD00/kJC2ntXtonLcX7SDkwWSjZmjZv+P6buVABfWkb4mHrCnnc9JiXCWJTWoqLTxiyB8dSYnPYAV8CyjYP8jYGFfJbbrzMjkrvrRYf40hbXGFzaivfgV9CqsSvNTEGSG77pQiemgXc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=G91n7riW; arc=none smtp.client-ip=209.85.210.193 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="G91n7riW" Received: by mail-pf1-f193.google.com with SMTP id d2e1a72fcca58-7aa9be9f03aso1202856b3a.2 for ; Thu, 18 Dec 2025 19:54:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766116454; x=1766721254; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=xHHylnVK4XkJx4ZGgOulzOiaDq14MqhIJ3X9iv/iIfM=; b=G91n7riWyNG1KnGQ6kaD2ozT8uIpBqxgB12Ho4gLs6pOfw3bIcst1lSQTyIfOkrQdz eUnnjMNDf9WecySbVmVWnrd9brDbJwgmJyRt5xND+HbJLGoiTzAFns0+5k4b16LCycun SooVA/gMeTmfpf107NuqLrntSrz6mLaPz3ZHna91Ed/VnM97OPVY3q6jPLyrldbEuecK Qr7tabDESc/mamAutdfDuR3f1bsHiQTnlwT3RTkFzjrc547FpD85g5ZCYWOOWR710zCp YczIsFgWdOWerSbhnntRJhFvN5DrLD3u6nbcexTsm2sw+Uf63vR5/2QXHz+O9f+dFvt+ 9onw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766116454; x=1766721254; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=xHHylnVK4XkJx4ZGgOulzOiaDq14MqhIJ3X9iv/iIfM=; b=m5nQ1e2vy5UGr+x+83FvgrOGsPlt3+4+aeGCvXn/i76Emehn/iDeT/NLZM6uUuwvyR J3Dc3n9dWhWIwhhFmTfMRelHlT/Z0+ZNxpKoX7tFuvI4j9W3B7ywrnKVrcyGNPABKjPs vvdwVGWGrglfPZ5JFnL2PA6OqSfI51PHtDH2ITqs5ykZ7XjTtpvily2zAAcyu0HiavFD nXuoTBAhoNIlfII6EQKipMsj7GynACY4REZJ4aBrn43Uy8rKig7NKydgu18hPARtI6p5 BmnYcPJhvJTO6MQJOFVUuzU0S+ICFVlWZrbFNN3ZvlaFHOVVbqrzCW1EU5lL75PxJ6rr qmWQ== X-Forwarded-Encrypted: i=1; AJvYcCV327gaoBudSjXIGlN+ZeTx6FPoBSs5gXS30QeztqxagI6WfkSANDzpQGJxCwW6WAHPfGnF/nZPfpPFlF4=@vger.kernel.org X-Gm-Message-State: AOJu0YwzsDgcvTM0m7/Q2wh9lK5TuVs0IXrjdqxk4r9rPt0KibEn8cfN o14UdfW10NmvEtOUx9Pn5RlAAXpRLmnRDJkCdWn3H1XRml+wBr4FVr0A X-Gm-Gg: AY/fxX66648Isc+U5o5UVwKNxvuHpztWVxYgqo4nOfz1cqri/vj3yKOvx3xuBgbN7nB raEp7Lq5zhhfQmtfrUxKC6lE8uktU6fJ5wzt+b1P8rw+zQO7RFoTR2tR/+PFGEtrJAVXoMKdADg IoxfBZylCvDffiuktYPGIUFHqlAjCRn3W/JHG3ELh+b7XcjyT8Ad9BvwThJryZP4F0V0K3IRdrV BTYtRK6GpuLUYpr2vpAYGXt06u2fM61CL498tZPJ2uGYDRN6jZ7iK4ZoOyzIzf7PvLpnT7k5kGw fa6ZuBk24BWO9BeaY4ZG+/bZN85jGbG7n2f7FSsZVX+gn8cfbDk9KBh30PgvxIhVGIxKKNZWfgg fHYEBpSxRtPsYCqX2MbggSExWuhZFY/BhTCqdDtSikdWM940WcfwQ6B2JWBgDy5meJX+THXQJdn K7qoA17YIgrQ== X-Google-Smtp-Source: AGHT+IF0bhXQdMZ92vZEQxR6Fache4q5+RNfQoshWlrbjpqkOwVElhYo3bzP5BK/41CJOmVAmF81SQ== X-Received: by 2002:a05:6a20:2583:b0:34e:4352:6c65 with SMTP id adf61e73a8af0-376a9acee63mr1654219637.38.1766116453636; Thu, 18 Dec 2025 19:54:13 -0800 (PST) Received: from wanpengli.. ([175.170.92.22]) by smtp.googlemail.com with ESMTPSA id d9443c01a7336-2a2f3d4d36esm7368135ad.63.2025.12.18.19.54.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Dec 2025 19:54:13 -0800 (PST) From: Wanpeng Li To: Peter Zijlstra , Ingo Molnar , Thomas Gleixner , Paolo Bonzini , Sean Christopherson Cc: K Prateek Nayak , Christian Borntraeger , Steven Rostedt , Vincent Guittot , Juri Lelli , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Wanpeng Li Subject: [PATCH v2 9/9] KVM: Relaxed boost as safety net Date: Fri, 19 Dec 2025 11:53:33 +0800 Message-ID: <20251219035334.39790-10-kernellwp@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251219035334.39790-1-kernellwp@gmail.com> References: <20251219035334.39790-1-kernellwp@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Wanpeng Li Add a minimal two-round fallback mechanism in kvm_vcpu_on_spin() to avoid pathological stalls when the first round finds no eligible target. Round 1 applies strict IPI-aware candidate selection (existing behavior). Round 2 provides a relaxed scan gated only by preempted state as a safety net, addressing cases where IPI context is missed or the runnable set is transient. The second round is controlled by module parameter enable_relaxed_boost (bool, 0644, default on) to allow easy disablement by distributions if needed. Introduce the enable_relaxed_boost parameter, add a first_round flag, retry label, and reset of yielded counter. Gate the IPI-aware check in round 1 and use preempted-only gating in round 2. Keep churn minimal by reusing the same scan logic while preserving all existing heuristics, tracing, and bookkeeping. Signed-off-by: Wanpeng Li --- virt/kvm/kvm_main.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 45ede950314b..662a907a79e1 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -102,6 +102,9 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(halt_poll_ns_shrink); static bool allow_unsafe_mappings; module_param(allow_unsafe_mappings, bool, 0444); +static bool enable_relaxed_boost = true; +module_param(enable_relaxed_boost, bool, 0644); + /* * Ordering of locks: * @@ -4011,6 +4014,7 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode) struct kvm *kvm = me->kvm; struct kvm_vcpu *vcpu; int try = 3; + bool first_round = true; nr_vcpus = atomic_read(&kvm->online_vcpus); if (nr_vcpus < 2) @@ -4021,6 +4025,9 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode) kvm_vcpu_set_in_spin_loop(me, true); +retry: + yielded = 0; + /* * The current vCPU ("me") is spinning in kernel mode, i.e. is likely * waiting for a resource to become available. Attempt to yield to a @@ -4052,8 +4059,13 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode) if (kvm_vcpu_is_blocking(vcpu) && !vcpu_dy_runnable(vcpu)) continue; - /* IPI-aware candidate selection */ - if (!kvm_vcpu_is_good_yield_candidate(me, vcpu, yield_to_kernel_mode)) + /* IPI-aware candidate selection in first round */ + if (first_round && + !kvm_vcpu_is_good_yield_candidate(me, vcpu, yield_to_kernel_mode)) + continue; + + /* Minimal preempted gate for second round */ + if (!first_round && !READ_ONCE(vcpu->preempted)) continue; if (!kvm_vcpu_eligible_for_directed_yield(vcpu)) @@ -4067,6 +4079,16 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode) break; } } + + /* + * Second round: relaxed boost as safety net, with preempted gate. + * Only execute when enabled and when the first round yielded nothing. + */ + if (enable_relaxed_boost && first_round && yielded <= 0) { + first_round = false; + goto retry; + } + kvm_vcpu_set_in_spin_loop(me, false); /* Ensure vcpu is not eligible during next spinloop */ -- 2.43.0