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 33D85CA0FF9 for ; Wed, 27 Aug 2025 14:13:16 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 3152010E822; Wed, 27 Aug 2025 14:13:15 +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-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) by gabe.freedesktop.org (Postfix) with ESMTPS id C3FD410E822 for ; Wed, 27 Aug 2025 14:13:13 +0000 (UTC) Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-24895637fe1so10658585ad.2 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=X40owVf4h6ToGd4onVKQ3pDqG4B7U5mc5Qy5VtBIK+uyM4ScrMKY61lKp5i+qI4UXH eF3OCWOOz+ZA77BCS6eusJKst7b3gL5nrBpvwT/BkZkQyHt0ZoQC68I/yLoDL2d68CfR FR4gd01LiE+l+THhYI+juQI0NxpwxK6lQd2B21LfVLL9rPo/SN1Wfmn2iH7AFqPTO1Kg isK7Dmk8mGbOxhleN5MzVhSAAGmInYGDuyngTomm3a0a44r7FvGNVnYKepxBbceswg5G uacHUBDB9VbPj99NvlHSmMmQydjGdAQXW/hNcSIVmec9dBh+yA6aRVZM/zPpUR4/zGHW K1xA== X-Forwarded-Encrypted: i=1; AJvYcCUWxyQaj8fqipx0r7ksdrI6m91CLFBCC49UPGdsfZMs17vF1/SppeEPB6ZdaAYo0wZ8IkWEBlsZl2s=@lists.freedesktop.org X-Gm-Message-State: AOJu0Yxu9u4upZBf9GXAtPQgUpQfRn+Mb3jCfnWR/4rHIFFtcuyoWTl6 e0nKKvItrVjAVUCvuHhrFnlWoNpsS4WSxcuxla1UayeiugmgmI7fIPCOMZlXw4GzSuDqmavucPI qd/IS3w== 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: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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.