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 160B9C8303F for ; Thu, 28 Aug 2025 17:15:22 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 8289510EA59; Thu, 28 Aug 2025 17:15:21 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="U1vmJQuL"; dkim-atps=neutral Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) by gabe.freedesktop.org (Postfix) with ESMTPS id 8EA9110EA58 for ; Thu, 28 Aug 2025 17:15:19 +0000 (UTC) Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-325e31cecd6so1072053a91.3 for ; Thu, 28 Aug 2025 10:15:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1756401319; x=1757006119; darn=lists.freedesktop.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=g/D1R1ws5ojuN4WG3pCUGyU24UMqyGj6lojr+YSYQ0c=; b=U1vmJQuLewXbWwDYMROWIh9ioIbwYsAWVqk5XfQl0adeNoFvLXZFXXlev9iz92jZha mLQmuAUowXzr10nQqJEIYLCIbcp9yzifkSnS6pi270qIR6/gGbLZPrvWqPLaH5UCg4m0 fwlpOEOLaO0aUeDWWPoAtxeqiO504/RziIUxgEFM1LJ4Bt0BoB3vZeVb+yP0ofQk5ZiX bYzEh5bcJcndK/i1gsacbvgoNYb4iP6hFnxnbzyG1Q13hrLOPQoQznKFdwueAnA2qc2X /VIcJt+EPbdtXgWqNRrpJOBpRNhw2zMK2D6PiQ6wAthM77kG7xhyminMq6SasZVal18b 0qtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756401319; x=1757006119; h=content-transfer-encoding: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=g/D1R1ws5ojuN4WG3pCUGyU24UMqyGj6lojr+YSYQ0c=; b=dNGTFJ83/OWU/llH9DxY9QrdZpiKHNClISqsPpV1JJ3u7lDiFGUbgesYFV+96oMKtR w86i+7c2Pnykh6QVVF2VyBc5n1uKBdbWJgz5pdo30+Yijge4327EFREfGBLV2VR+g6eX F7tZ6pSXT2akHhVxs093lgt3GaYetcG4ex+iox6ja1tXpUbLJdevjtolAPG2dTTu73D3 oxMoy20jVySuJ7U6NgOcpkAnJwr/psdflPGLegZsGSO/a6zUohspNUWkYANAWORCElQw tv7z1O/0DOBxbSLMSHgYZR4YXxocREhKK3nbYAgUgPwugdNsZa749Gj4Fzd4HfPm0u40 r+Ag== X-Forwarded-Encrypted: i=1; AJvYcCWEP80fYDK4u9ICinI1ED1at7f21fe/eMhgRP2qzoKthn9RkUfDlbNPEh+eWMTshMzwYatrgeDgJ3M=@lists.freedesktop.org X-Gm-Message-State: AOJu0Yw4uRgAvjhE3iGPsuslURLASRskrc4T/CotLMcAba1kW6au5Y9q m8QNQkDmkgov8gwGiGZLNaNwRAgwJyUXhEM63H70w2GR2FxEpDKCPqeE3zq7CAvLQ3+ENSwb3gt zeDaGxw== X-Google-Smtp-Source: AGHT+IEgfCMAj/kPGbx0Dj2C82B+XByTzBEnlSaJgGQHGzaozLp7T19v0PG28aEIfyLg5E6WkydfWYNcBOM= X-Received: from pjhk31.prod.google.com ([2002:a17:90a:4ca2:b0:327:9b90:7a79]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1dc3:b0:327:cec7:b8c6 with SMTP id 98e67ed59e1d1-327cec7cb53mr1738031a91.32.1756401318775; Thu, 28 Aug 2025 10:15:18 -0700 (PDT) Date: Thu, 28 Aug 2025 10:15:17 -0700 In-Reply-To: <874d821e-8ea3-40ac-921b-c19bb380a456@kylinos.cn> Mime-Version: 1.0 References: <20250827023202.10310-1-zhangzihuan@kylinos.cn> <20250827023202.10310-3-zhangzihuan@kylinos.cn> <874d821e-8ea3-40ac-921b-c19bb380a456@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="utf-8" Content-Transfer-Encoding: quoted-printable 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 Thu, Aug 28, 2025, Zihuan Zhang wrote: > > Hmm, this is technically buggy. __free() won't invoke put_cpufreq_poli= cy() until > > policy goes out of scope, and so using __free() means the code is effec= tively: > >=20 > > if (IS_ENABLED(CONFIG_CPU_FREQ)) { > > struct cpufreq_policy *policy; > > int cpu; > >=20 > > cpu =3D get_cpu(); > > policy =3D cpufreq_cpu_get(cpu); > > if (policy && policy->cpuinfo.max_freq) > > max_tsc_khz =3D policy->cpuinfo.max_freq; > > put_cpu(); > >=20 > > if (policy) > > cpufreq_cpu_put(policy); > > } ... > Yes, this will indeed change the execution order. > Can you accept that?=20 No, because it's buggy. > Personally, I don=E2=80=99t think it=E2=80=99s ideal either. >=20 > if (IS_ENABLED(CONFIG_CPU_FREQ)) { > int cpu; > cpu =3D get_cpu(); > { > struct cpufreq_policy *policy __free(put_cpufreq_policy) =3D cpufreq_= cpu_get(cpu); > if (policy && policy->cpuinfo.max_freq) > max_tsc_khz =3D policy->cpuinfo.max_freq; > } > put_cpu(); >=20 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 } >=20 > Other places may also have the same issue, >=20 > maybe we should consider introducing a macro to handle this properly, > so that initialization and cleanup are well defined without changing > the existing order unexpected. >=20 > like this: >=20 > #define WITH_CPUFREQ_POLICY(cpu) {\ >=20 > for(struct cpufreq_policy *policy __free(put_cpufreq_policy) =3D \ > cpufreq_cpu_get(cpu); \ > policy;) >=20 > Then Use it: >=20 > if (IS_ENABLED(CONFIG_CPU_FREQ)) { > int cpu; > cpu =3D get_cpu(); > WITH_CPUFREQ_POLICY(cpu){ > if (policy->cpuinfo.max_freq) > max_tsc_khz =3D policy->cpuinfo.max_freq; > } > put_cpu(); This all feels very forced, in the sense that we have a shiny new tool and = are trying to use it everywhere without thinking critically about whether or no= t doing so is actually an improvement. At a glance, this is literally the only instance in the entire kernel where= the CPU to use is grabbed immediately before the policy. =20 $ git grep -B 20 cpufreq_cpu_get | grep -e get_cpu -e smp_processor_id arch/x86/kvm/x86.c- cpu =3D get_cpu(); drivers/cpufreq/cppc_cpufreq.c-static int cppc_get_cpu_power(struct devic= e *cpu_dev, drivers/cpufreq/cppc_cpufreq.c-static int cppc_get_cpu_cost(struct device= *cpu_dev, unsigned long KHz, drivers/cpufreq/mediatek-cpufreq-hw.c-mtk_cpufreq_get_cpu_power(struct de= vice *cpu_dev, unsigned long *uW, Probably because KVM's usage is rather bizarre and honestly kind of dumb. = But KVM has had this behavior for 15+ years, so as weird as it is, I'm not incl= ined to change it without a really, really strong reason to do so, e.g. to itera= te over all CPUs or something. So given that this is the only intance of the problem patter, I think it ma= kes sense to leave KVM as-is, and not spend a bunch of time trying to figure ou= t how to make KVM's usage play nice with __free().