From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 A06C133EB16 for ; Thu, 28 Aug 2025 17:15:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756401321; cv=none; b=RnlQE4j4KCB2PWfHKWlf5mkP/Ph4gSKMJvv0iourQqMychYsXzDLEAGz4cthwRKCFGEUpKls+4PgIT5PyK4enwwk0YOA342FcjmNqLhwuYNGqFMKgplDkaItkYCmhcQkAH6bhO1qMiYL9NTZmSCkOxKsCN+E5QtZduS6KbRnFzg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756401321; c=relaxed/simple; bh=tjLq4yotJVvrKtVjK5XLYb7jE1fQQx1FF/sxrbky9es=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BP6BiVXVlINbZdUjizqIR2C2mk/pirZJYbQpewDDr8WtLvSOoEmVF2qf0MG8BFPqjM0nqDhvHXXZplCK+Hm6X7Hyjup9xY/8IbLOW8AoRQx2CUQraxnSdTM+jZs363hyFQeHF+ciFjj23RTKW/f7wxRHp4A9AzhCdjMB0uwi0xU= 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=G6GpVf2D; arc=none smtp.client-ip=209.85.216.74 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="G6GpVf2D" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-32769224506so1196727a91.2 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.linux.dev; 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=G6GpVf2D5BnUkfiAsZtDHEc4J4NyG8t+OMT6i9sel0FI4PpnfGQrWtYz/wWiosgYyP VVn+QClwo21G5uN8xM8Ic1IS77kjWOhDYjsf5tqAT0ISvtOfACAElewpomgGLXevMNzg Xck4FZu3lxz/3xqpN/vwrjgqotEkcZpbhf4TDKDXiqQm8qPE8Bt4kFJaiVPx7eKqUyOl JtuYuyYRz4TQ4PILt1H90lkLg4ApT/PZ0u9DuHVd2SBQOq9kmTxlJcasXPdjRLKkXzlZ bPYinAbh9Wa8zKq4QvhvXk3jJoL30cgbcYZkuhbhg50PZ+OpuAkHvaIDSSo/NmQy5MMD /qHQ== 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=VC/dYS7+JfHKNSFSvxgpnGGOjpZkErsDWNKjkz4EXZA01/OVzldbzDZAPrXx1TJKH5 Y92kR2fuOkx/3KdSmynmzPqcgsa3AH0MIdmgs7lUsLtCzJjxzXHQdcVlvhRaljYMLs/n CBKCS3p/ONrtOASSMdumyYZC6WrslH48WLzOqVZHEd59zC4qUATN5toeD9eJqeQ5icve 19aqAP0w3TDzaxK3bGUd5dv1CmyTVyWeI5KN8jEM5tHp0+jpIgvBGl0ch7HgK5HhEAif TQrSzgdxUMTz7zSnaWQS+IhDHS3rj6gJiUOolEpB2+4dXRDEVIpLCiDRq7czhQsmkEhx iNJA== X-Forwarded-Encrypted: i=1; AJvYcCVPQJCDltGKXxEaxylH44a2MhgcM40P5IG86E1GxsKwF0e6EJnhwWumHmpsP1GSQF3ksd4=@lists.linux.dev X-Gm-Message-State: AOJu0YxbRy6/MvUSTuGi61VMMxnWLB6oFAdPmI2BNAnUG0k91gl88NfG 4/KO/dj5zzmEpf8u9hDKLa1hOKPNxAqbwFe48VLHZtU2RMjaZJV5yvmg3KAyq/yOL8EJ2xAPpl4 zbY/LKQ== 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> 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> <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 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().