Skip to contentSkip to conversation

cpufreq: dt-platdev: Fix module autoloading

RFC PATCH In review Javier Martinez Canillas 4 participants 11 messages Nov 19, 2024 – Nov 25, 2024 CPU frequency scaling framework

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 cpufreq: dt-platdev: Fix module autoloading

This driver can be built as a module since commit 3b062a086984 ("cpufreq:
dt-platdev: Support building as module"), but unfortunately this caused a regression because the cputfreq-dt-platdev.ko module does not autoload.

Usually, this is solved by just using the MODULE_DEVICE_TABLE() macro to export all the device IDs as module aliases. But this driver is special due how matches with devices and decides what platform supports.

There are two of_device_id lists, an allow list that are for CPU devices that always match and a deny list that's for devices that must not match.

The driver registers a cpufreq-dt platform device for all the CPU device nodes that either are in the allow list or contain an operating-points-v2 property and are not in the deny list.

For the former just add a MODULE_DEVICE_TABLE(), and for the latter add a module alias. That way the driver would always be autoloaded when needed.

Fixes 3b062a086984 ("cpufreq: dt-platdev: Support building as module")

---
Posting as an RFC because I don't have a platform that uses this driver but I'll let Radu test since he reported by issue.

 drivers/cpufreq/cpufreq-dt-platdev.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/cpufreq/cpufreq-dt-platdev.c +13 -0
index 2a3e8bd317c9..7ae7c897c249 100644
@@ -97,6 +97,7 @@ static const struct of_device_id allowlist[] __initconst = {
 
 	{ }
 };
+MODULE_DEVICE_TABLE(of, allowlist);
 
 /*
  * Machines for which the cpufreq device is *not* created, mostly used for
@@ -236,4 +237,16 @@ static int __init cpufreq_dt_platdev_init(void)
 }
 core_initcall(cpufreq_dt_platdev_init);
 MODULE_DESCRIPTION("Generic DT based cpufreq platdev driver");
+/*
+ * The module alias is needed because the driver automatically registers a
+ * platform device for any CPU device node that has an operating-points-v2
+ * property and is not in the block list.
+ *
+ * For this reason the MODULE_DEVICE_TABLE() macro can only export aliases
+ * of the devices in the allow list, which means that the driver will not
+ * autoload for devices whose cpufreq-dt will be registered automatically.
+ *
+ * Adding an "of:N*T*Coperating-points-v2" alias is a workaround for this.
+ */
+MODULE_ALIAS("of:N*T*Coperating-points-v2");
 MODULE_LICENSE("GPL");
Signature · 1 line
2.47.0
maintainer
#1 lore raw

+Rob/Arnd,

Quoted 33 lines · Javier Martinez Canillas

This driver can be built as a module since commit 3b062a086984 ("cpufreq:
dt-platdev: Support building as module"), but unfortunately this caused a regression because the cputfreq-dt-platdev.ko module does not autoload.

Usually, this is solved by just using the MODULE_DEVICE_TABLE() macro to export all the device IDs as module aliases. But this driver is special due how matches with devices and decides what platform supports.

There are two of_device_id lists, an allow list that are for CPU devices that always match and a deny list that's for devices that must not match.

The driver registers a cpufreq-dt platform device for all the CPU device nodes that either are in the allow list or contain an operating-points-v2 property and are not in the deny list.

For the former just add a MODULE_DEVICE_TABLE(), and for the latter add a module alias. That way the driver would always be autoloaded when needed.

Fixes 3b062a086984 ("cpufreq: dt-platdev: Support building as module")

---
Posting as an RFC because I don't have a platform that uses this driver but I'll let Radu test since he reported by issue.

 drivers/cpufreq/cpufreq-dt-platdev.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/cpufreq/cpufreq-dt-platdev.c +0 -0
index 2a3e8bd317c9..7ae7c897c249 100644

drivers/cpufreq/cpufreq-dt-platdev.c static const struct of_device_id allowlist[] __initconst = { · line 100

Quoted 4 lines
drivers/cpufreq/cpufreq-dt-platdev.c +1 -0
@@ -97,6 +97,7 @@ static const struct of_device_id allowlist[] __initconst = {
 
 	{ }
 };
+MODULE_DEVICE_TABLE(of, allowlist);

This is fine obviously.

Quoted 8 lines
drivers/cpufreq/cpufreq-dt-platdev.c +2 -0
 /*
  * Machines for which the cpufreq device is *not* created, mostly used for
@@ -236,4 +237,16 @@ static int __init cpufreq_dt_platdev_init(void)
 }
 core_initcall(cpufreq_dt_platdev_init);
 MODULE_DESCRIPTION("Generic DT based cpufreq platdev driver");
+/*
+ * The module alias is needed because the driver automatically registers a

drivers/cpufreq/cpufreq-dt-platdev.c static int __init cpufreq_dt_platdev_init(void) · line 251

Quoted 10 lines
drivers/cpufreq/cpufreq-dt-platdev.c +10 -0
+ * platform device for any CPU device node that has an operating-points-v2
+ * property and is not in the block list.
+ *
+ * For this reason the MODULE_DEVICE_TABLE() macro can only export aliases
+ * of the devices in the allow list, which means that the driver will not
+ * autoload for devices whose cpufreq-dt will be registered automatically.
+ *
+ * Adding an "of:N*T*Coperating-points-v2" alias is a workaround for this.
+ */
+MODULE_ALIAS("of:N*T*Coperating-points-v2");

How does this work? This will autoload the module for any platform with "operating-points-v2" property in the DT ? The driver though works only if the property is in the CPU node, while now we will try to load this driver even if a non-CPU node has the property now.

I am not sure what's the best way forward to fix this.

Arnd, Rob, any inputs ?

Quoted 1 line
drivers/cpufreq/cpufreq-dt-platdev.c +0 -0
 MODULE_LICENSE("GPL");
Signature · 1 line
viresh
#2 lore raw

Viresh Kumar <[email protected]> writes:

Hello Viresh,

Quoted 37 lines

+Rob/Arnd,

Quoted 34 lines · Javier Martinez Canillas

This driver can be built as a module since commit 3b062a086984 ("cpufreq:
dt-platdev: Support building as module"), but unfortunately this caused a regression because the cputfreq-dt-platdev.ko module does not autoload.

Usually, this is solved by just using the MODULE_DEVICE_TABLE() macro to export all the device IDs as module aliases. But this driver is special due how matches with devices and decides what platform supports.

There are two of_device_id lists, an allow list that are for CPU devices that always match and a deny list that's for devices that must not match.

The driver registers a cpufreq-dt platform device for all the CPU device nodes that either are in the allow list or contain an operating-points-v2 property and are not in the deny list.

For the former just add a MODULE_DEVICE_TABLE(), and for the latter add a module alias. That way the driver would always be autoloaded when needed.

Fixes 3b062a086984 ("cpufreq: dt-platdev: Support building as module")

---
Posting as an RFC because I don't have a platform that uses this driver but I'll let Radu test since he reported by issue.

 drivers/cpufreq/cpufreq-dt-platdev.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/cpufreq/cpufreq-dt-platdev.c +0 -0
index 2a3e8bd317c9..7ae7c897c249 100644
@@ -97,6 +97,7 @@ static const struct of_device_id allowlist[] __initconst = {
 
Quoted 6 lines

drivers/cpufreq/cpufreq-dt-platdev.c static const struct of_device_id allowlist[] __initconst = { · line 100

Quoted 3 lines
drivers/cpufreq/cpufreq-dt-platdev.c +1 -0
 	{ }
 };
+MODULE_DEVICE_TABLE(of, allowlist);

This is fine obviously.

Yeah, this part is trivial.

Quoted 15 lines
Quoted 15 lines
drivers/cpufreq/cpufreq-dt-platdev.c +9 -0
 /*
  * Machines for which the cpufreq device is *not* created, mostly used for
@@ -236,4 +237,16 @@ static int __init cpufreq_dt_platdev_init(void)
 }
 core_initcall(cpufreq_dt_platdev_init);
 MODULE_DESCRIPTION("Generic DT based cpufreq platdev driver");
+/*
+ * The module alias is needed because the driver automatically registers a
+ * platform device for any CPU device node that has an operating-points-v2
+ * property and is not in the block list.
+ *
+ * For this reason the MODULE_DEVICE_TABLE() macro can only export aliases
+ * of the devices in the allow list, which means that the driver will not
+ * autoload for devices whose cpufreq-dt will be registered automatically.
+ *
Quoted 9 lines

drivers/cpufreq/cpufreq-dt-platdev.c static int __init cpufreq_dt_platdev_init(void) · line 251

Quoted 3 lines
drivers/cpufreq/cpufreq-dt-platdev.c +3 -0
+ * Adding an "of:N*T*Coperating-points-v2" alias is a workaround for this.
+ */
+MODULE_ALIAS("of:N*T*Coperating-points-v2");

How does this work? This will autoload the module for any platform with "operating-points-v2" property in the DT ? The driver though works only if the property is in the CPU node, while now we will try to load this driver even if a non-CPU node has the property now.

Will autload the driver for any platform that has a Device Tree node with a compatible = "operating-points-v2" (assuming that this node will be a phandle for the "operating-points-v2" property.

For example, in the case of the following DT snippet:

cpus {
        cpu@0 {
                operating-points-v2     = <&cpu0_opp_table>;
        };
};
cpu0_opp_table: opp_table {
        compatible = "operating-points-v2";

...
};

It will autoload if OF finds the opp_table node, but it register the cpufreq-dt device only if there's a cpu@0 with a "operating-points-v2" property.

Yes, there may be false positives because the autload semantics don't exactly match the criteria for the driver to "match" but I believe is better to load it and not use for those cases, than needing the driver and not autoloading it.

Quoted 2 lines

I am not sure what's the best way forward to fix this.

I couldn't find another way to solve it, if you have a better idea please let me know. But IMO we should either workaround like this or revert the commit that changed the driver's Kconfig symbol to be tristate.

Quoted 7 lines

Arnd, Rob, any inputs ?

Quoted 1 line
drivers/cpufreq/cpufreq-dt-platdev.c +0 -0
 MODULE_LICENSE("GPL");
Signature · 1 line
viresh
Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat
maintainer
#3 lore raw
Quoted 24 lines · Javier Martinez Canillas

Will autload the driver for any platform that has a Device Tree node with a compatible = "operating-points-v2" (assuming that this node will be a phandle for the "operating-points-v2" property.

For example, in the case of the following DT snippet:

cpus {
        cpu@0 {
                operating-points-v2     = <&cpu0_opp_table>;
        };
};
cpu0_opp_table: opp_table {
        compatible = "operating-points-v2";

...
};

It will autoload if OF finds the opp_table node, but it register the cpufreq-dt device only if there's a cpu@0 with a "operating-points-v2" property.

Yes, there may be false positives because the autload semantics don't exactly match the criteria for the driver to "match" but I believe is better to load it and not use for those cases, than needing the driver and not autoloading it.

Quoted 6 lines
Quoted 2 lines

I am not sure what's the best way forward to fix this.

I couldn't find another way to solve it, if you have a better idea please let me know. But IMO we should either workaround like this or revert the commit that changed the driver's Kconfig symbol to be tristate.

Yeah, this needs to be fixed and this patch is one of the ways. Lets see if Arnd or Rob have something to add, else can apply this patch.

Signature · 1 line
viresh
#4 lore raw
Quoted 28 lines · Viresh Kumar
Quoted 27 lines · Javier Martinez Canillas

Will autload the driver for any platform that has a Device Tree node with a compatible = "operating-points-v2" (assuming that this node will be a phandle for the "operating-points-v2" property.

For example, in the case of the following DT snippet:

cpus {
        cpu@0 {
                operating-points-v2     = <&cpu0_opp_table>;
        };
};
cpu0_opp_table: opp_table {
        compatible = "operating-points-v2";

...
};

It will autoload if OF finds the opp_table node, but it register the cpufreq-dt device only if there's a cpu@0 with a "operating-points-v2" property.

Yes, there may be false positives because the autload semantics don't exactly match the criteria for the driver to "match" but I believe is better to load it and not use for those cases, than needing the driver and not autoloading it.

Quoted 2 lines

I am not sure what's the best way forward to fix this.

Quoted 7 lines
Quoted 3 lines

I couldn't find another way to solve it, if you have a better idea please let me know. But IMO we should either workaround like this or revert the commit that changed the driver's Kconfig symbol to be tristate.

Yeah, this needs to be fixed and this patch is one of the ways. Lets see if Arnd or Rob have something to add, else can apply this patch.

Ok. Please notice though that this is an RFC, since all my arm64 machines have their own CPUFreq driver and are not using cpufreq-dt-platdev. So I would not apply it until someone actually tested the patch.

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat
#5 lore raw
Quoted 34 lines · Javier Martinez Canillas
Quoted 32 lines · Viresh Kumar
Quoted 30 lines · Javier Martinez Canillas

Will autload the driver for any platform that has a Device Tree node with a compatible = "operating-points-v2" (assuming that this node will be a phandle for the "operating-points-v2" property.

For example, in the case of the following DT snippet:

cpus {
        cpu@0 {
                operating-points-v2     = <&cpu0_opp_table>; };
};

cpu0_opp_table: opp_table {
        compatible = "operating-points-v2";
...
};

It will autoload if OF finds the opp_table node, but it register the cpufreq-dt device only if there's a cpu@0 with a "operating-points-v2" property.

Yes, there may be false positives because the autload semantics don't exactly match the criteria for the driver to "match" but I believe is better to load it and not use for those cases, than needing the driver and not autoloading it.

Quoted 2 lines

I am not sure what's the best way forward to fix this.

I couldn't find another way to solve it, if you have a better idea please let me know. But IMO we should either workaround like this or revert the commit that changed the driver's Kconfig symbol to be tristate.

Quoted 7 lines
Quoted 3 lines

Yeah, this needs to be fixed and this patch is one of the ways. Lets see if Arnd or Rob have something to add, else can apply this patch.

Ok. Please notice though that this is an RFC, since all my arm64 machines have their own CPUFreq driver and are not using cpufreq-dt-platdev. So I would not apply it until someone actually tested the patch.

I tested the patch on a Renesas R-Car S4 Spider (r8a779f0-spider.dts) board, and it didn't work. I think the problem is that the OPP table DT node does not have a corresponding device instance that is registered, and therefore no modalias uevent is reported to udev/kmod.

FWIW, the OPP table is defined at the top of r8a779f0.dtsi and referenced just a few more lines below, where the CPU nodes are defined.

As far as I understand, there are two options to fix this:
1. Revert the patch that allows the cpufreq-dt-platdev driver to be built as a module. There's little benefit in allowing that anyway because the overhead at init time is minimal when the driver is unused, and driver can't be unloaded.
2. Modify the driver and create an explicit of_device_id table of supported platforms for v2 too (like the existing one for v1) and use that instead of the cpu0_node_has_opp_v2_prop() call and the blacklist. That would of course also eliminate the blacklist.

--
Best regards,
Radu Rendec

#6 lore raw

Radu Rendec <[email protected]> writes:

Hello Radu,

Quoted 39 lines · Javier Martinez Canillas
Quoted 38 lines
Quoted 35 lines · Viresh Kumar
Quoted 24 lines · Javier Martinez Canillas

Will autload the driver for any platform that has a Device Tree node with a compatible = "operating-points-v2" (assuming that this node will be a phandle for the "operating-points-v2" property.

For example, in the case of the following DT snippet:

cpus {
        cpu@0 {
                operating-points-v2     = <&cpu0_opp_table>; };
};

cpu0_opp_table: opp_table {
        compatible = "operating-points-v2";
...
};

It will autoload if OF finds the opp_table node, but it register the cpufreq-dt device only if there's a cpu@0 with a "operating-points-v2" property.

Yes, there may be false positives because the autload semantics don't exactly match the criteria for the driver to "match" but I believe is better to load it and not use for those cases, than needing the driver and not autoloading it.

Quoted 6 lines
Quoted 2 lines

I am not sure what's the best way forward to fix this.

I couldn't find another way to solve it, if you have a better idea please let me know. But IMO we should either workaround like this or revert the commit that changed the driver's Kconfig symbol to be tristate.

Yeah, this needs to be fixed and this patch is one of the ways. Lets see if Arnd or Rob have something to add, else can apply this patch.

Quoted 9 lines
Quoted 3 lines

Ok. Please notice though that this is an RFC, since all my arm64 machines have their own CPUFreq driver and are not using cpufreq-dt-platdev. So I would not apply it until someone actually tested the patch.

I tested the patch on a Renesas R-Car S4 Spider (r8a779f0-spider.dts) board, and it didn't work. I think the problem is that the OPP table DT node does not have a corresponding device instance that is registered, and therefore no modalias uevent is reported to udev/kmod.

Thanks for testing! Bummer that the workaround didn't work. But that's why I asked you to test. You know, like Donald Knuth said: "Beware of bugs in the above code; I have only proved it correct, not tried it" :)

Quoted 4 lines

FWIW, the OPP table is defined at the top of r8a779f0.dtsi and referenced just a few more lines below, where the CPU nodes are defined.

Quoted 10 lines

As far as I understand, there are two options to fix this:
1. Revert the patch that allows the cpufreq-dt-platdev driver to be built as a module. There's little benefit in allowing that anyway because the overhead at init time is minimal when the driver is unused, and driver can't be unloaded.
2. Modify the driver and create an explicit of_device_id table of supported platforms for v2 too (like the existing one for v1) and use that instead of the cpu0_node_has_opp_v2_prop() call and the blacklist. That would of course also eliminate the blacklist.

Agreed with this. Likely (1) is the easiest path and (2) would make the driver more aligned with the rest of the kernel (that have a list of OF device IDs to autoload / match instead of some custom logic).

But I guess that (2) would be riskier, since not adding a platform that uses v2 will cause a regression.

Quoted 4 lines

--
Best regards,
Radu Rendec

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat
maintainer
#7 lore raw
Quoted 6 lines · Javier Martinez Canillas

Agreed with this. Likely (1) is the easiest path and (2) would make the driver more aligned with the rest of the kernel (that have a list of OF device IDs to autoload / match instead of some custom logic).

But I guess that (2) would be riskier, since not adding a platform that uses v2 will cause a regression.

I am inclined to go with (1) here and have applied a patch from Andreas Kemnade, which he sent sometime back. I have used your commit log though, since it was more descriptive.

Signature · 1 line
viresh
#8 lore raw

Viresh Kumar <[email protected]> writes:

Hello Viresh,

Quoted 5 lines · Javier Martinez Canillas
Quoted 4 lines

Agreed with this. Likely (1) is the easiest path and (2) would make the driver more aligned with the rest of the kernel (that have a list of OF device IDs to autoload / match instead of some custom logic).

Quoted 7 lines
Quoted 2 lines

But I guess that (2) would be riskier, since not adding a platform that uses v2 will cause a regression.

I am inclined to go with (1) here and have applied a patch from Andreas Kemnade, which he sent sometime back. I have used your commit log though, since it was more descriptive.

That sounds good to me, thanks for solving this issue!

Quoted 3 lines
Signature · 1 line
viresh
Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat
#9 lore raw
In reply to Javier Martinez Canillas
Quoted 72 lines · Javier Martinez Canillas

Radu Rendec <[email protected]> writes:

Hello Radu,

Quoted 39 lines · Javier Martinez Canillas
Quoted 38 lines
Quoted 35 lines · Viresh Kumar
Quoted 24 lines · Javier Martinez Canillas

Will autload the driver for any platform that has a Device Tree node with a compatible = "operating-points-v2" (assuming that this node will be a phandle for the "operating-points-v2" property.

For example, in the case of the following DT snippet:

cpus {
        cpu@0 {
                operating-points-v2     = <&cpu0_opp_table>; };
};

cpu0_opp_table: opp_table {
        compatible = "operating-points-v2";
...
};

It will autoload if OF finds the opp_table node, but it register the cpufreq-dt device only if there's a cpu@0 with a "operating-points-v2" property.

Yes, there may be false positives because the autload semantics don't exactly match the criteria for the driver to "match" but I believe is better to load it and not use for those cases, than needing the driver and not autoloading it.

Quoted 6 lines
Quoted 2 lines

I am not sure what's the best way forward to fix this.

I couldn't find another way to solve it, if you have a better idea please let me know. But IMO we should either workaround like this or revert the commit that changed the driver's Kconfig symbol to be tristate.

Yeah, this needs to be fixed and this patch is one of the ways. Lets see if Arnd or Rob have something to add, else can apply this patch.

Quoted 9 lines
Quoted 3 lines

Ok. Please notice though that this is an RFC, since all my arm64 machines have their own CPUFreq driver and are not using cpufreq-dt-platdev. So I would not apply it until someone actually tested the patch.

I tested the patch on a Renesas R-Car S4 Spider (r8a779f0-spider.dts) board, and it didn't work. I think the problem is that the OPP table DT node does not have a corresponding device instance that is registered, and therefore no modalias uevent is reported to udev/kmod.

Thanks for testing! Bummer that the workaround didn't work. But that's why I asked you to test. You know, like Donald Knuth said: "Beware of bugs in the above code; I have only proved it correct, not tried it" :)

Quoted 4 lines

FWIW, the OPP table is defined at the top of r8a779f0.dtsi and referenced just a few more lines below, where the CPU nodes are defined.

Quoted 10 lines

As far as I understand, there are two options to fix this:
1. Revert the patch that allows the cpufreq-dt-platdev driver to be built as a module. There's little benefit in allowing that anyway because the overhead at init time is minimal when the driver is unused, and driver can't be unloaded.
2. Modify the driver and create an explicit of_device_id table of supported platforms for v2 too (like the existing one for v1) and use that instead of the cpu0_node_has_opp_v2_prop() call and the blacklist. That would of course also eliminate the blacklist.

Quoted 6 lines

Agreed with this. Likely (1) is the easiest path and (2) would make the driver more aligned with the rest of the kernel (that have a list of OF device IDs to autoload / match instead of some custom logic).

But I guess that (2) would be riskier, since not adding a platform that uses v2 will cause a regression.

Also, 2 probably wouldn't work. Devices under /cpus don't get a struct device created for them, so it wouldn't probe.

Maxime

#10 lore raw

Maxime Ripard <[email protected]> writes:

Hello Maxime,

Quoted 1 line · Javier Martinez Canillas

[...]

Quoted 19 lines
Quoted 19 lines
Quoted 4 lines

FWIW, the OPP table is defined at the top of r8a779f0.dtsi and referenced just a few more lines below, where the CPU nodes are defined.

Quoted 10 lines

As far as I understand, there are two options to fix this:
1. Revert the patch that allows the cpufreq-dt-platdev driver to be built as a module. There's little benefit in allowing that anyway because the overhead at init time is minimal when the driver is unused, and driver can't be unloaded.
2. Modify the driver and create an explicit of_device_id table of supported platforms for v2 too (like the existing one for v1) and use that instead of the cpu0_node_has_opp_v2_prop() call and the blacklist. That would of course also eliminate the blacklist.

Agreed with this. Likely (1) is the easiest path and (2) would make the driver more aligned with the rest of the kernel (that have a list of OF device IDs to autoload / match instead of some custom logic).

Quoted 6 lines
Quoted 2 lines

But I guess that (2) would be riskier, since not adding a platform that uses v2 will cause a regression.

Also, 2 probably wouldn't work. Devices under /cpus don't get a struct device created for them, so it wouldn't probe.

Yeah, but the cpufreq-dt-platdev driver has a of_device_id table for the OPPv1 that matches the actual SoC, not the cpu device nodes.

So for (2) I was thinking to do the same. But yes, agreed tha (1) is the best option here.

Quoted 1 line

Maxime

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat