From nobody Wed Dec 17 14:20:36 2025 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (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 75BD02868AD for ; Mon, 13 Oct 2025 12:51:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760359905; cv=none; b=oHX+FIkdhsO5yqWZ2iTZuTpeL/Mfmxw9a22GDClYd4WWcwMdpt9ZcC/Bc1qYn5Gr30droLJY0QrQf8lqsJx3ONK49rfe6tzy/bbbYwOIaTjY5+RKn/zNkOe80b+xA5GFTP9zYTVvr/WRX3Gn5N+FkY/JAWhcVEUpTudbZvu6yvY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760359905; c=relaxed/simple; bh=J/dzWtKuI6KBjBEpLOQM0goLapAMplM4s0j7SX3F+KQ=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=TKKP35XAylKL6JwYFyNCPCsrecB9cexIzX/3o3JDfgOT0EjcPAtZ0PwlR1wfrmVnMrpdoxvBjJ+L7RP7wVp/EhfNfG1IleWTWuZk1gh7oHxZy4amQCo8l0vjVO7KRmj6EZD9gzxgkvg7Boa7RN7LS1jwZvk+ihMvlI0pyp1lGCM= 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=GS7Jg828; arc=none smtp.client-ip=209.85.216.52 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="GS7Jg828" Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-33082c95fd0so5096906a91.1 for ; Mon, 13 Oct 2025 05:51:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1760359904; x=1760964704; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=qp4XEa7ToS5FljKv+e4kbXkPaJ8IiCtEps0yhLGiUvo=; b=GS7Jg828Dy3pvC7EdxvX78txIDyDUBH+bLWj+ewTYxpPQt6m26mMSXAaCOXe6Sn4iK wD3x/nFsbfuJemey99taA+7QrUZEEIeHcLh2jwhQC75mZwR5zjI3VnAMdT7Mxg/Tfiz5 7IRx73c1Ok1JyxM1MEAKDWeOT3lE4j1BgewnpQLPBMhbjSTWMnSHpAwyUKiNZKhoRrxH LVn5AQI1FMFglCBipRCYgC5Ru/l64A1N7l5v7aIRsnYgiZDtLzBxFbPx9S1BRinvADtK aLsxc4/z2QpcK+Sj2QCLPX1nO9iIXH4Viz0D88ZQPni/Up+YXluoEBbysLkRL62iTsgs 8Tkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760359904; x=1760964704; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=qp4XEa7ToS5FljKv+e4kbXkPaJ8IiCtEps0yhLGiUvo=; b=jVwHoeJALiub+iRGIdD+gH5Mc9YJRmJLhnxpKURixn89U/wPaz3ePV8KE1Xgmdngak 2XyGh5ueYmb+B49hQ7D/inqTuL9wk1nwfSBbxMIhRyy4iPjoMdsMxlTXn/8GkXnoSITG 9usvWOAXSLPBah6kmcWD9lC9sDOuf2pJ9eFERjguGX/PbXjSBEeQgfsyQc+NUoMYpROw 4xLUQHjc90LxupmaEGWhZs9me3Zy0fEEgbzEoQVi04PnSWwtqYggQoEF/SPVzcP7Bvrq Bl4b5QtG3CuLx6PQixKnSkIkGI1f6/iILAjy7DW5AOr4rGztW8z7aLaTggfTCaLD3uTe g8gw== X-Forwarded-Encrypted: i=1; AJvYcCXCyiiWUKpcFphyk1ILgmqPPK6rIH81n7clwFnzjl/UY586AnAJ8mNHa69F4sPOyR0elCCmdOoE3oUxiyQ=@vger.kernel.org X-Gm-Message-State: AOJu0YwSQ2DZVpeDFjAEXx9v4IhisGEEpWvHZVAtKr0NeGkE3wEzI/1o F5Pdr2th996TVcSaZzDuDQMrydvuF6ppxOCiuR9ICIHsK9at4I4S3CRa X-Gm-Gg: ASbGncuvkAw5+0C350GZt/2pIJpXmljhSgHgDDPnrVCfRBHnwsQ1vE7o+sRCnBUyWy9 XOLGjQBc4YXT0s8IgVqr6VnbtqpZrQhGscLAMOvtPx/v4s4GcwfzQP1p5xaFrKbaUIxhTqZK00c qgYszVhgMGKFqk6EqIIMELJQHDtSxKXnSl37Wgm9xTVEidsQdXYePKBpPytMhwZma2t8d3yAalW eD+KdYuJp7ka3azKRqXmOmFifut2LTOdYK7COpZlLYlze+GBg88s8AHGz1XmTyDQUjS5EbTGpHJ lq8dYtc1Yyfq4UZYCGD7KCF955RSHD8Ths8XZIcp2R+Tpvy5F9f/rO0N2NhiBM8NsXMoaD4APy+ JYIBOnJSSVwB52JwcJ+crdEiKc3IXn7zK5LATUT0kglg8ySGFVuJcPnhtSMl6ag/yaA== X-Google-Smtp-Source: AGHT+IHCsN9tB1NFXjLxHlC1i+3jugsgr76PwZZhuE+ita/pwHESbjDT12QASzBbG7b8q5FB7Uoukw== X-Received: by 2002:a17:90b:17ca:b0:32b:a332:7a0a with SMTP id 98e67ed59e1d1-33b5114b648mr28154449a91.1.1760359903593; Mon, 13 Oct 2025 05:51:43 -0700 (PDT) Received: from localhost.localdomain ([47.82.118.6]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-33b5288043fsm9567422a91.0.2025.10.13.05.51.38 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 13 Oct 2025 05:51:42 -0700 (PDT) From: fuqiang wang To: Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , Maxim Levitsky , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: fuqiang wang , yu chen , dongxu zhang Subject: [PATCH RESEND] avoid hv timer fallback to sw timer if delay exceeds period Date: Mon, 13 Oct 2025 20:51:17 +0800 Message-ID: <20251013125117.87739-1-fuqiang.wng@gmail.com> X-Mailer: git-send-email 2.47.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When the guest uses the APIC periodic timer, if the delay exceeds the period, the delta will be negative. nsec_to_cycles() may then convert this delta into an absolute value larger than guest_l1_tsc, resulting in a negative tscdeadline. Since the hv timer supports a maximum bit width of cpu_preemption_timer_multi + 32, this causes the hv timer setup to fail and switch to the sw timer. Moreover, due to the commit 98c25ead5eda ("KVM: VMX: Move preemption timer <=3D> hrtimer dance to common x86"), if the guest is using the sw timer before blocking, it will continue to use the sw timer after being woken up, and will not switch back to the hv timer until the relevant APIC timer register is reprogrammed. Since the periodic timer does not require frequent APIC timer register programming, the guest may continue to use the software timer for an extended period. The reproduction steps and patch verification results at link [1]. [1]: https://github.com/cai-fuqiang/kernel_test/tree/master/period_timer_te= st Fixes: 98c25ead5eda ("KVM: VMX: Move preemption timer <=3D> hrtimer dance t= o common x86") Signed-off-by: fuqiang wang --- arch/x86/kvm/lapic.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 5fc437341e03..afd349f4d933 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2036,6 +2036,9 @@ static void advance_periodic_target_expiration(struct= kvm_lapic *apic) u64 tscl =3D rdtsc(); ktime_t delta; =20 + u64 delta_cycles_u; + u64 delta_cycles_s; + /* * Synchronize both deadlines to the same time source or * differences in the periods (caused by differences in the @@ -2047,8 +2050,11 @@ static void advance_periodic_target_expiration(struc= t kvm_lapic *apic) ktime_add_ns(apic->lapic_timer.target_expiration, apic->lapic_timer.period); delta =3D ktime_sub(apic->lapic_timer.target_expiration, now); + delta_cycles_u =3D nsec_to_cycles(apic->vcpu, abs(delta)); + delta_cycles_s =3D delta > 0 ? delta_cycles_u : -delta_cycles_u; + apic->lapic_timer.tscdeadline =3D kvm_read_l1_tsc(apic->vcpu, tscl) + - nsec_to_cycles(apic->vcpu, delta); + delta_cycles_s; } =20 static void start_sw_period(struct kvm_lapic *apic) --=20 2.47.0