diff mbox series

[V9,08/17] cpufreq: Use enum for cpufreq flags that use BIT()

Message ID efbdd8212a90175c293313de961c34d13b9f4b43.1744366571.git.viresh.kumar@linaro.org (mailing list archive)
State Under Review
Headers show
Series Rust abstractions for clk, cpumask, cpufreq, OPP | expand

Commit Message

Viresh Kumar April 11, 2025, 10:55 a.m. UTC
The BIT() macro is too complex for Rust's bindgen to interpret as
integer constants. This results in many of the cpufreq macros being
undefined in Rust auto-generated bindings. By replacing the "#define"
macros with an "enum", we ensure that bindgen can properly evaluate
these values, enabling their seamless use in Rust code.

No intentional functional impact.

Suggested-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
 include/linux/cpufreq.h | 96 ++++++++++++++++++++++-------------------
 1 file changed, 51 insertions(+), 45 deletions(-)

Comments

Yury Norov April 11, 2025, 4:07 p.m. UTC | #1
On Fri, Apr 11, 2025 at 04:25:07PM +0530, Viresh Kumar wrote:
> The BIT() macro is too complex for Rust's bindgen to interpret as

The BIT() is as simple as '1 << nr'. How that it's so complex for that
tool to realize that '1 << 2' is the constant?

> integer constants. This results in many of the cpufreq macros being
> undefined in Rust auto-generated bindings. By replacing the "#define"
> macros with an "enum", we ensure that bindgen can properly evaluate
> these values, enabling their seamless use in Rust code.
> 
> No intentional functional impact.

The code you're wiping is perfectly correct. And you clearly state
that one of your tools is broken. Please fix your tool or find any
other solution.

We don't touch correct code, sorry.
 
> Suggested-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
> ---
>  include/linux/cpufreq.h | 96 ++++++++++++++++++++++-------------------
>  1 file changed, 51 insertions(+), 45 deletions(-)
> 
> diff --git a/include/linux/cpufreq.h b/include/linux/cpufreq.h
> index 400fee6427a5..354ae35fe708 100644
> --- a/include/linux/cpufreq.h
> +++ b/include/linux/cpufreq.h
> @@ -298,11 +298,12 @@ static inline void cpufreq_stats_record_transition(struct cpufreq_policy *policy
>   *                      CPUFREQ DRIVER INTERFACE                     *
>   *********************************************************************/
>  
> -#define CPUFREQ_RELATION_L 0  /* lowest frequency at or above target */
> -#define CPUFREQ_RELATION_H 1  /* highest frequency below or at target */
> -#define CPUFREQ_RELATION_C 2  /* closest frequency to target */
> -/* relation flags */
> -#define CPUFREQ_RELATION_E BIT(2) /* Get if possible an efficient frequency */
> +enum {
> +	CPUFREQ_RELATION_L = 0, /* lowest frequency at or above target */
> +	CPUFREQ_RELATION_H = BIT(0), /* highest frequency below or at target */
> +	CPUFREQ_RELATION_C = BIT(1), /* closest frequency to target */
> +	CPUFREQ_RELATION_E = BIT(2), /* Get if possible an efficient frequency */
> +};
>  
>  #define CPUFREQ_RELATION_LE (CPUFREQ_RELATION_L | CPUFREQ_RELATION_E)
>  #define CPUFREQ_RELATION_HE (CPUFREQ_RELATION_H | CPUFREQ_RELATION_E)
> @@ -424,52 +425,57 @@ struct cpufreq_driver {
>  
>  /* flags */
>  
> -/*
> - * Set by drivers that need to update internal upper and lower boundaries along
> - * with the target frequency and so the core and governors should also invoke
> - * the diver if the target frequency does not change, but the policy min or max
> - * may have changed.
> - */
> -#define CPUFREQ_NEED_UPDATE_LIMITS		BIT(0)
> +enum {
> +	/*
> +	 * Set by drivers that need to update internal upper and lower
> +	 * boundaries along with the target frequency and so the core and
> +	 * governors should also invoke the diver if the target frequency does
> +	 * not change, but the policy min or max may have changed.
> +	 */
> +	CPUFREQ_NEED_UPDATE_LIMITS		= BIT(0),
>  
> -/* loops_per_jiffy or other kernel "constants" aren't affected by frequency transitions */
> -#define CPUFREQ_CONST_LOOPS			BIT(1)
> +	/*
> +	 * loops_per_jiffy or other kernel "constants" aren't affected by
> +	 * frequency transitions.
> +	 */
> +	CPUFREQ_CONST_LOOPS			= BIT(1),
>  
> -/*
> - * Set by drivers that want the core to automatically register the cpufreq
> - * driver as a thermal cooling device.
> - */
> -#define CPUFREQ_IS_COOLING_DEV			BIT(2)
> +	/*
> +	 * Set by drivers that want the core to automatically register the
> +	 * cpufreq driver as a thermal cooling device.
> +	 */
> +	CPUFREQ_IS_COOLING_DEV			= BIT(2),
>  
> -/*
> - * This should be set by platforms having multiple clock-domains, i.e.
> - * supporting multiple policies. With this sysfs directories of governor would
> - * be created in cpu/cpu<num>/cpufreq/ directory and so they can use the same
> - * governor with different tunables for different clusters.
> - */
> -#define CPUFREQ_HAVE_GOVERNOR_PER_POLICY	BIT(3)
> +	/*
> +	 * This should be set by platforms having multiple clock-domains, i.e.
> +	 * supporting multiple policies. With this sysfs directories of governor
> +	 * would be created in cpu/cpu<num>/cpufreq/ directory and so they can
> +	 * use the same governor with different tunables for different clusters.
> +	 */
> +	CPUFREQ_HAVE_GOVERNOR_PER_POLICY	= BIT(3),
>  
> -/*
> - * Driver will do POSTCHANGE notifications from outside of their ->target()
> - * routine and so must set cpufreq_driver->flags with this flag, so that core
> - * can handle them specially.
> - */
> -#define CPUFREQ_ASYNC_NOTIFICATION		BIT(4)
> +	/*
> +	 * Driver will do POSTCHANGE notifications from outside of their
> +	 * ->target() routine and so must set cpufreq_driver->flags with this
> +	 *  flag, so that core can handle them specially.
> +	 */
> +	CPUFREQ_ASYNC_NOTIFICATION		= BIT(4),
>  
> -/*
> - * Set by drivers which want cpufreq core to check if CPU is running at a
> - * frequency present in freq-table exposed by the driver. For these drivers if
> - * CPU is found running at an out of table freq, we will try to set it to a freq
> - * from the table. And if that fails, we will stop further boot process by
> - * issuing a BUG_ON().
> - */
> -#define CPUFREQ_NEED_INITIAL_FREQ_CHECK	BIT(5)
> +	/*
> +	 * Set by drivers which want cpufreq core to check if CPU is running at
> +	 * a frequency present in freq-table exposed by the driver. For these
> +	 * drivers if CPU is found running at an out of table freq, we will try
> +	 * to set it to a freq from the table. And if that fails, we will stop
> +	 * further boot process by issuing a BUG_ON().
> +	 */
> +	CPUFREQ_NEED_INITIAL_FREQ_CHECK		= BIT(5),
>  
> -/*
> - * Set by drivers to disallow use of governors with "dynamic_switching" flag
> - * set.
> - */
> -#define CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING	BIT(6)
> +	/*
> +	 * Set by drivers to disallow use of governors with "dynamic_switching"
> +	 * flag set.
> +	 */
> +	CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING	= BIT(6),
> +};
>  
>  int cpufreq_register_driver(struct cpufreq_driver *driver_data);
>  void cpufreq_unregister_driver(struct cpufreq_driver *driver_data);
> -- 
> 2.31.1.272.g89b43f80a514
Miguel Ojeda April 11, 2025, 5:05 p.m. UTC | #2
On Fri, Apr 11, 2025 at 6:07 PM Yury Norov <yury.norov@gmail.com> wrote:
>
> The BIT() is as simple as '1 << nr'. How that it's so complex for that
> tool to realize that '1 << 2' is the constant?

It is a C macro which requires evaluating a C expression as the C
compiler would.

A solution (a workaround, really) in `bindgen` is coming, that
essentially forces `libclang` to evaluate it for us, but even if it
works perfectly fine already, it will require upgrading the minimum
version and so on, so it will take time.

For the moment, either we do something like this (a similar approach
was used for some `enum`s in Binder) or the values can be replicated
on the Rust side (ideally with a test somewhere to ensure they are in
sync). The latter may be best if the values have not changed much over
time, i.e. if there is low maintenance required.

I hope that helps.

Cheers,
Miguel
diff mbox series

Patch

diff --git a/include/linux/cpufreq.h b/include/linux/cpufreq.h
index 400fee6427a5..354ae35fe708 100644
--- a/include/linux/cpufreq.h
+++ b/include/linux/cpufreq.h
@@ -298,11 +298,12 @@  static inline void cpufreq_stats_record_transition(struct cpufreq_policy *policy
  *                      CPUFREQ DRIVER INTERFACE                     *
  *********************************************************************/
 
-#define CPUFREQ_RELATION_L 0  /* lowest frequency at or above target */
-#define CPUFREQ_RELATION_H 1  /* highest frequency below or at target */
-#define CPUFREQ_RELATION_C 2  /* closest frequency to target */
-/* relation flags */
-#define CPUFREQ_RELATION_E BIT(2) /* Get if possible an efficient frequency */
+enum {
+	CPUFREQ_RELATION_L = 0, /* lowest frequency at or above target */
+	CPUFREQ_RELATION_H = BIT(0), /* highest frequency below or at target */
+	CPUFREQ_RELATION_C = BIT(1), /* closest frequency to target */
+	CPUFREQ_RELATION_E = BIT(2), /* Get if possible an efficient frequency */
+};
 
 #define CPUFREQ_RELATION_LE (CPUFREQ_RELATION_L | CPUFREQ_RELATION_E)
 #define CPUFREQ_RELATION_HE (CPUFREQ_RELATION_H | CPUFREQ_RELATION_E)
@@ -424,52 +425,57 @@  struct cpufreq_driver {
 
 /* flags */
 
-/*
- * Set by drivers that need to update internal upper and lower boundaries along
- * with the target frequency and so the core and governors should also invoke
- * the diver if the target frequency does not change, but the policy min or max
- * may have changed.
- */
-#define CPUFREQ_NEED_UPDATE_LIMITS		BIT(0)
+enum {
+	/*
+	 * Set by drivers that need to update internal upper and lower
+	 * boundaries along with the target frequency and so the core and
+	 * governors should also invoke the diver if the target frequency does
+	 * not change, but the policy min or max may have changed.
+	 */
+	CPUFREQ_NEED_UPDATE_LIMITS		= BIT(0),
 
-/* loops_per_jiffy or other kernel "constants" aren't affected by frequency transitions */
-#define CPUFREQ_CONST_LOOPS			BIT(1)
+	/*
+	 * loops_per_jiffy or other kernel "constants" aren't affected by
+	 * frequency transitions.
+	 */
+	CPUFREQ_CONST_LOOPS			= BIT(1),
 
-/*
- * Set by drivers that want the core to automatically register the cpufreq
- * driver as a thermal cooling device.
- */
-#define CPUFREQ_IS_COOLING_DEV			BIT(2)
+	/*
+	 * Set by drivers that want the core to automatically register the
+	 * cpufreq driver as a thermal cooling device.
+	 */
+	CPUFREQ_IS_COOLING_DEV			= BIT(2),
 
-/*
- * This should be set by platforms having multiple clock-domains, i.e.
- * supporting multiple policies. With this sysfs directories of governor would
- * be created in cpu/cpu<num>/cpufreq/ directory and so they can use the same
- * governor with different tunables for different clusters.
- */
-#define CPUFREQ_HAVE_GOVERNOR_PER_POLICY	BIT(3)
+	/*
+	 * This should be set by platforms having multiple clock-domains, i.e.
+	 * supporting multiple policies. With this sysfs directories of governor
+	 * would be created in cpu/cpu<num>/cpufreq/ directory and so they can
+	 * use the same governor with different tunables for different clusters.
+	 */
+	CPUFREQ_HAVE_GOVERNOR_PER_POLICY	= BIT(3),
 
-/*
- * Driver will do POSTCHANGE notifications from outside of their ->target()
- * routine and so must set cpufreq_driver->flags with this flag, so that core
- * can handle them specially.
- */
-#define CPUFREQ_ASYNC_NOTIFICATION		BIT(4)
+	/*
+	 * Driver will do POSTCHANGE notifications from outside of their
+	 * ->target() routine and so must set cpufreq_driver->flags with this
+	 *  flag, so that core can handle them specially.
+	 */
+	CPUFREQ_ASYNC_NOTIFICATION		= BIT(4),
 
-/*
- * Set by drivers which want cpufreq core to check if CPU is running at a
- * frequency present in freq-table exposed by the driver. For these drivers if
- * CPU is found running at an out of table freq, we will try to set it to a freq
- * from the table. And if that fails, we will stop further boot process by
- * issuing a BUG_ON().
- */
-#define CPUFREQ_NEED_INITIAL_FREQ_CHECK	BIT(5)
+	/*
+	 * Set by drivers which want cpufreq core to check if CPU is running at
+	 * a frequency present in freq-table exposed by the driver. For these
+	 * drivers if CPU is found running at an out of table freq, we will try
+	 * to set it to a freq from the table. And if that fails, we will stop
+	 * further boot process by issuing a BUG_ON().
+	 */
+	CPUFREQ_NEED_INITIAL_FREQ_CHECK		= BIT(5),
 
-/*
- * Set by drivers to disallow use of governors with "dynamic_switching" flag
- * set.
- */
-#define CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING	BIT(6)
+	/*
+	 * Set by drivers to disallow use of governors with "dynamic_switching"
+	 * flag set.
+	 */
+	CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING	= BIT(6),
+};
 
 int cpufreq_register_driver(struct cpufreq_driver *driver_data);
 void cpufreq_unregister_driver(struct cpufreq_driver *driver_data);