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 D66C335A29E 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=1756303995; cv=none; b=clI9jzd6ZV9WIQujkWQWq6nd0J8zIqvc+fk0hxYt5f74Zb2fUPwPtgE/5LId2DewA6ZHLmp5ko+JhUO0w9vDvTfotWe51kfKlmfWz/s2XToVRxURfxSTukS9M53qN2iiLDfoDsqfQjuNSMAkkn7e9zdxrvEi6Fx/c6wmzpPwnk0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756303995; c=relaxed/simple; bh=DbwcQqcSgOgf9ZSwsPbdu37ylfuV2VigX8RYWvus3Uo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SQ/cbDw4lOhaA69ixlWjeCid8EdtA5y/zJr/3ePulMsZqbuhFe2EbxqIsufqNwK2yxL8dK5NR+Dwj1h6lIeV6BnOfUB+mPl3H2AuL/qb0J3aDQ+ZJB2oDF5Q6dfJ6L8IfMLQgCbFeJa46LTmMju3CL1faq6q+psa36aqayz0K6c= 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=zmpwboY5; 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="zmpwboY5" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-246cf6af36dso39640075ad.0 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.linux.dev; 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=zmpwboY5lQOuPjdzy10tKOtcDg/FEElxZowy8ow5PgySCgl47ps6EwXkmPCF3zAf4r hspDc3GuuXVT+zH6APRzLSgGhgpwJCekDfgFZrqroHGNEInzLqB8769mNl/MMcl53Xdx 3bcGwfG2lWbZ/G2uKaLNuzeRBW1qZX80iwYKZcb7svAAJrpYKpRfyscsmpICnUzM5i6C djNM+zIQyvhMcSwrwMdh/19bjgLuImfUtwNCSXWMctKu6l3iOIpIGSgGl2eCibrn81K9 ZVTtbmZQ+MQ4y4A4z066FpXCbhw32xISLZcW+a46iTkScBg1zl7iXtfdQUMSrpEI3DzF GRFw== 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=lnzyUm13+nCa/pQ1MsN0LMZjg5qjiqH/LYQp+UryJJQ7dgWZBYyVnBk1Gnv7ZfdWsf uC1ZAAQKiLar6/T53PRuGl9JpTHCarqHGPADek8dtO3OTFqpqVNGGREF9tkuhLNBdPTR +Cigj8iQ5FZFXXSOoBHRRyG+jDYNSCepBi+fOGKPdOCZUUZs5QUjmkbNHq4e1hcFB8zx Z8o6cGli7ZjgKbSAiVR9DvsSPDtfG7FpNhmXNdCiI6yHvVP2UWOFLg4QzUuB4dvCj8L+ DoDE5qXbe9/P0coMI30551Pwej6zDce9s26kw+yjcnvoaZIpsk+0iYoSj3BvuL7VxSv1 AQqw== X-Forwarded-Encrypted: i=1; AJvYcCUbZeRwWXlxYicJqm9b3w5nwiLOttBRHeo3u6PLVpr2tHwtc7Ee11oPudQtVSpQs0SnlMw=@lists.linux.dev X-Gm-Message-State: AOJu0YxczDLK/IGl9/0D6ogMreIt3t4/PgsHCIxedrJ1nU33lnDd4dNN eZD9fHXti/tEi/F2K2pRQn78QQb1dRTkmcI+2fQRtP2Oc0JnGR7FL8f5MP3mEl9t6xa+SnbnwdE TE2CdmQ== 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: imx@lists.linux.dev 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.