From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 EA6EE33CEB6 for ; Thu, 28 Aug 2025 17:15:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756401322; cv=none; b=IF75RVbXEAjZA7SHCsItz9FAm9TWxFUfdfuHuiGLBNtJ9LSOTEB3tMGUd+ZYgE8ft84oeWWxXuNsMlZf10fD2IQa9EheagWeoYeeQVB+FcDRTTgKhqpANqLgmaAXgdrhnpJJ2qKdsNxub8C0pE/vFbE2O8TRd55NCTT0P+iStIw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756401322; c=relaxed/simple; bh=tjLq4yotJVvrKtVjK5XLYb7jE1fQQx1FF/sxrbky9es=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Y41qkiwGt2sCXod4ZFSI8pvbN8hYlxM2KwI10p2CuQTXooF2bsLO6rEssIEM3AKPmcdNtR/ZZAmySe8yanCr/P5zXqt+YdAx4x4sbQlU+8LqAurvtySsCv2ng2Sx7GIECWWB8okwMNzwGhm2KA7rg0/dph3QceaAiYvltOJSSQA= 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=EJzdxybn; arc=none smtp.client-ip=209.85.216.73 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="EJzdxybn" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-327d1fea06eso322335a91.1 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=vger.kernel.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=EJzdxybnyxlQrk6qJR558wtNekNbgws953L2pneIZYbmyp9tAmsnaz2aSuhIUBM5zr yym6Q3hOD+h86ma+8fB5UYxWC0FsSh8WsgByN6NGuhj5XPnYnWvTGq97mGOESOdqxB8M UO5/YFoyUcXdWdRme2wq/ulZFp8KXOMX/N5wcNQCirgFPfS9pE37lzw+nKFk5IrmnDbO G5sw2Dwq2cicHF4lperkYSyAoX2g6OycdnZ1FjYe/p8IEgXy13BJvrhiJGJwM3VA8ugT uPqiFjyVbr8N6oCUmphfD2rJa6OFoETW3Ncm5ZacDEJ+l20U+VrrmUTHcEPjkrroNkNZ 4tIA== 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=Ojtl2WatQEw+8rcAEqV/9q26u09JeC/tUJ7KOb3IFBQASQtGenwXPu5auqsYKuh4OD 4U8eaW2pVlckaVOcmOFVSLAKgmddyTtv90XtBXYTAjc2sO43mIHP5sleXS5OtnX6oBzc dbMcNkHECvZxVSmrpsiQL5Wl+te1jXL9JL3sAHs8QIAT6k7USzSPlT6gW0dXfYl3qou9 CBydlr/aICg2XblCc+3tTvh49lQMxLadTRELYGE+h5QTLzQ1bzZKemxfjqc3m9COo7kJ T6SOB4FPvSQBZl5lwf0ID6wG6WY4UgLSjPaOgwNwEiXiG7nIq9evk4ydM04anjozFLjA hLzQ== X-Forwarded-Encrypted: i=1; AJvYcCUcN+4081gA6pVG068Bq4EtieVxM4fFzoH2do5xIevaK95WX2gQKxRs3atTmJ+bv8Zn7mA=@vger.kernel.org X-Gm-Message-State: AOJu0YxBwNep466zP2H65bPdegn3ihKV3184s/A+yDBRtNfqgyjJpYL6 ZngrbtLMFFdwJvs3jroF9OUdTGOaR2ihTYKAZa5aFYWTDhEGBcZTwvXERbA/VerhuMslxQRupXg i2yiqaQ== 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: kvm@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> <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().