From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4FDA5CA0FED for ; Wed, 27 Aug 2025 14:13:15 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id ADADD10E133; Wed, 27 Aug 2025 14:13:14 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="WBS57/cS"; dkim-atps=neutral Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by gabe.freedesktop.org (Postfix) with ESMTPS id C3C5910E133 for ; Wed, 27 Aug 2025 14:13:13 +0000 (UTC) Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-24636484391so59558445ad.3 for ; Wed, 27 Aug 2025 07:13:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1756303993; x=1756908793; darn=lists.freedesktop.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=oT9Qx0pL0526DK73EqyoLYw0wsYmSjn2bOtkKWj9+LI=; b=WBS57/cS9rxZbdAzOkaykFUzqP+f7SBhxnXEzpnOBScZPoVFEleWC4Tk3tcGS1l0GP YlyG5q4XuyArv9pPJ91ltDaP8H1qos8Hs5SF3IovIK4O6y0Aar3eHqkm4d0pVt1m121G MMnHehxsezBugqftJ1KCUT1ZCoeC53CqvHansy6go7IdMG7bY0S173GgAxORgiXQSFbU 6PFyA+YGe5T4fqpHiNEo+MJ9q5jOQXBTrXYHWiftDMfye9HAFIWJq5D1Jg/I1b5NRZ1i oxljLuKkRuiFGUcYtopucoB/82v8EKAnEgSPHCeTVH40EGrVlxJ+bpJ32sCTyJL4vz8h ZzPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756303993; x=1756908793; 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=oT9Qx0pL0526DK73EqyoLYw0wsYmSjn2bOtkKWj9+LI=; b=RB9SeKPSwmGlOMk77BZ14ab/vTTFC/bCU5JktGvBNkhW3F3L46sPhkoLwPOYHTxwwD pEiBuSQnDr6+saibfVhXijS3pAN2Aan+w0miQfAG7UFjKEH8IMwwof6U6lHrhmXhbMRU SbNjx1vk5l4IsAULZqXs5QWDZgQ4zMLciAv5BT7loDIm1PCpyUlehyEiixr7wRvsP5a+ k7DqojinHlV5FNbPDpWezXsYsH7UXZL7ui+DGvxxsV7cQnqhv06pf+styFXGWEaBAy1e 1nTPzgmlBbvq1o0+QfU/1NDdqvC0OGoLLU1oPJmAx/qNSOD2dnxeK1GQTFspTpH/N0tu YZNQ== X-Forwarded-Encrypted: i=1; AJvYcCWaRrS86LgTRb6VMsjy3zURqF48Zm3p8OANkrPoNxdHKCUdOWISz7QIY+xfXyKdb356AfakiVT9vs8=@lists.freedesktop.org X-Gm-Message-State: AOJu0Yx5U82OLEFKZVYd3mcRIe0YncZY2lC7QLKZGPb6sWcQOfsOiZzm owHJsrJn4SOK5780Sea2qXy35qj/PKNk+WpmdIOodu2w1r8V87CiaQV3BFrywK0RKrMVfdYUHhW 5bJPRFw== X-Google-Smtp-Source: AGHT+IGTSg2J3odWqRi5DzWDmdBYNRXtpWLMNIy0mlgNYPFM/YLwHCJkE7ruCavXPIcZq+6RDeJTdDhPxa4= X-Received: from plan10.prod.google.com ([2002:a17:903:404a:b0:248:7792:b8da]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1a8e:b0:246:620:a0b9 with SMTP id d9443c01a7336-2462efcaaa3mr229124795ad.61.1756303993097; Wed, 27 Aug 2025 07:13:13 -0700 (PDT) Date: Wed, 27 Aug 2025 07:13:11 -0700 In-Reply-To: <20250827023202.10310-3-zhangzihuan@kylinos.cn> Mime-Version: 1.0 References: <20250827023202.10310-1-zhangzihuan@kylinos.cn> <20250827023202.10310-3-zhangzihuan@kylinos.cn> Message-ID: Subject: Re: [PATCH v2 02/18] KVM: x86: Use __free(put_cpufreq_policy) for policy reference From: Sean Christopherson To: Zihuan Zhang Cc: "Rafael J . wysocki" , Viresh Kumar , Catalin Marinas , Will Deacon , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Markus Mayer , Florian Fainelli , Srinivas Pandruvada , Madhavan Srinivasan , Michael Ellerman , Krzysztof Kozlowski , Alim Akhtar , Thierry Reding , Jonathan Hunter , MyungJoo Ham , Kyungmin Park , Chanwoo Choi , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , David Airlie , Simona Vetter , Daniel Lezcano , Sascha Hauer , Shawn Guo , Eduardo Valentin , Keerthy , Matthias Brugger , AngeloGioacchino Del Regno , zhenglifeng , "H . Peter Anvin" , Zhang Rui , Len Brown , Nicholas Piggin , Christophe Leroy , Lukasz Luba , Pengutronix Kernel Team , Beata Michalska , Fabio Estevam , Pavel Machek , Sumit Gupta , Prasanna Kumar T S M , Sudeep Holla , Yicong Yang , linux-pm@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-acpi@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-samsung-soc@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-tegra@vger.kernel.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, imx@lists.linux.dev, linux-omap@vger.kernel.org, linux-mediatek@lists.infradead.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="us-ascii" X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" On Wed, Aug 27, 2025, Zihuan Zhang wrote: > Replace the manual cpufreq_cpu_put() with __free(put_cpufreq_policy) > annotation for policy references. This reduces the risk of reference > counting mistakes and aligns the code with the latest kernel style. > > No functional change intended. > > Signed-off-by: Zihuan Zhang > --- > arch/x86/kvm/x86.c | 10 ++++------ > 1 file changed, 4 insertions(+), 6 deletions(-) > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index a1c49bc681c4..2a825f4ec701 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -9492,16 +9492,14 @@ static void kvm_timer_init(void) > max_tsc_khz = tsc_khz; > > if (IS_ENABLED(CONFIG_CPU_FREQ)) { > - struct cpufreq_policy *policy; > + struct cpufreq_policy *policy __free(put_cpufreq_policy); > int cpu; > > cpu = get_cpu(); > policy = cpufreq_cpu_get(cpu); > - if (policy) { > - if (policy->cpuinfo.max_freq) > - max_tsc_khz = policy->cpuinfo.max_freq; > - cpufreq_cpu_put(policy); > - } > + if (policy && policy->cpuinfo.max_freq) > + max_tsc_khz = policy->cpuinfo.max_freq; > + > put_cpu(); Hmm, this is technically buggy. __free() won't invoke put_cpufreq_policy() until policy goes out of scope, and so using __free() means the code is effectively: if (IS_ENABLED(CONFIG_CPU_FREQ)) { struct cpufreq_policy *policy; int cpu; cpu = get_cpu(); policy = cpufreq_cpu_get(cpu); if (policy && policy->cpuinfo.max_freq) max_tsc_khz = policy->cpuinfo.max_freq; put_cpu(); if (policy) cpufreq_cpu_put(policy); } That's "fine" because the policy isn't truly referenced after preemption is disabled, the lifecycle of the policy doesn't rely on preemption being disabled, and KVM doesn't actually care which CPU is used to get the max frequency, i.e. this would technically be "fine" too: if (IS_ENABLED(CONFIG_CPU_FREQ)) { struct cpufreq_policy *policy; int cpu; cpu = get_cpu(); policy = cpufreq_cpu_get(cpu); put_cpu(); if (policy && policy->cpuinfo.max_freq) max_tsc_khz = policy->cpuinfo.max_freq; if (policy) cpufreq_cpu_put(policy); } But given that the code we have today is perfectly readable, I don't see any reason to switch to __free() given that's it's technically flawed. So I'm very strongly inclined to skip this patch and keep things as-is.