From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.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 D65A135A29C for ; Wed, 27 Aug 2025 14:13:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756303996; cv=none; b=fH9Aj1MQP8TNtgkXnjVFPsEMkHLDf2dDePw8eliaw+dz9zZYl3JRGDAAJnOIZrJClSWEzWseYqXEqaobgi1WTJaeC9Di+YAPGuV4Nu1TdPf7CQ7CkUKSajNRlOoSy9sCtRWY+0aqF/xxgF9ghk/Zbt765H5A0bMJ8GhloI9aLlQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756303996; c=relaxed/simple; bh=DbwcQqcSgOgf9ZSwsPbdu37ylfuV2VigX8RYWvus3Uo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ja5Yg71Csr01MH4mUbiOXjkILxbZ1E1GjT8hsI1JBpcvexOpZSolmuFqWaLS/wPFv6i4nSqiOmueBGRKlW3rTDjBYPZ5jDLUZFCCaKzbR8zr4wyhBwyd2IPgSr3ix+c62noHDGZY4Xzq4jBB61TaY8A1AU9V7HPb+SP3pKIfFn4= 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=dWTiy0/i; arc=none smtp.client-ip=209.85.214.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="dWTiy0/i" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-24636484391so59558415ad.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=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=oT9Qx0pL0526DK73EqyoLYw0wsYmSjn2bOtkKWj9+LI=; b=dWTiy0/irckCaQzk1WuEdn1/rIGs7BUH5HtaVC4UoJeOx0TpCeqeE6kk77pxAcqrnj 8LojKUuALAEpPlTJymXuhPzon4uOCNgzVgt0TkOgA6Z6gJ5ao7qNO5a0VaRMShCbvqEb 7Z62nl5DvJ20RiK5OTGZ86B3DOv8vFnsYY+sJzLivJ/iGpVMmBOsweBiRfyJ/72qPJCX PwBUcRJOon6Uj9jfopwF3gsWHxnnuYseo58d2oMPKXgA5TBeCfN+sZsEs6MYpfkwete7 EjDMl/KFvG2qvR/qwNw21N6nT6u+87ofdLzjCfMKz7iNC8r3Su2x+Dx8A/RBkzH8CXtw fv/Q== 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=AbmOfy6q5P0L8sDj1PW98CMxNgjU7NOlYNRO919dstoPhvRCbEoFaaTWlPQOWcx34p +lXTaB+P3oNhcBYEhcPWMgd/s+His/blLxUdi2dfk7Lca3sfBQ2XhV5ZzXegYJfk06wR x1KAYkTsAJ0bICgTVlQxj0l46cbrfsGk7dgZriECjTI/PGhnZzWtX6QH7AE749sRDINK Mn3CB4eOMRqL+E9tCYLq01M9TmUkqR/b5afokLy41u/FzJqhFhB5HOaFP5/ob1idY3Z1 Ky5DsMhaIcZ8/5Sw8DRl0gv86Oi9p+4YQahmBSLdaDwzr0BMUZGbGBxhAElS7w/87pgQ MW8A== X-Forwarded-Encrypted: i=1; AJvYcCUsVLFPNnW19eZj421dRR/URfHKlgAjq03+7yb4bACRQ/bLKQHO1FWfRZ7IWsbwh+XlSdL720enBA==@vger.kernel.org X-Gm-Message-State: AOJu0Yxj50VN8SB/+wl54PEX600XUHyRxMTeCEqwfov721Bzfm+ikOvr C/LkSyxSc2Iho/aZLnt1ys+nR8BfQ2Rkgwtv4KT0HIIzPo1RwDi2IZGpHuDEVbQiKGygtkW7Zix dD0ifWQ== 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> Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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" 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.