Skip to contentSkip to conversation

clk: sunxi-ng: Use of_device_get_match_data()

PATCH In review Rosen Penev 3 participants 3 messages May 19

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 clk: sunxi-ng: Use of_device_get_match_data()

Use of_device_get_match_data() to fetch the RTC CCU match data directly instead of open-coding an of_match_device() lookup.

This also lets the driver drop the of_device.h include.

Assisted-by Codex:GPT-5.5
---
 drivers/clk/sunxi-ng/ccu-sun6i-rtc.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/clk/sunxi-ng/ccu-sun6i-rtc.c +2 -5
index f6bfeba009e8..a3cf0dde05be 100644
@@ -9,7 +9,6 @@
 #include <linux/io.h>
 #include <linux/module.h>
 #include <linux/of.h>
-#include <linux/of_device.h>
 
 #include <linux/clk/sunxi-ng.h>
 
@@ -353,14 +352,12 @@ int sun6i_rtc_ccu_probe(struct device *dev, void __iomem *reg)
 {
 	const struct sun6i_rtc_match_data *data;
 	struct clk *ext_osc32k_clk = NULL;
-	const struct of_device_id *match;
 
 	/* This driver is only used for newer variants of the hardware. */
-	match = of_match_device(sun6i_rtc_ccu_match, dev);
-	if (!match)
+	data = of_device_get_match_data(dev);
+	if (!data)
 		return 0;
 
-	data = match->data;
 	have_iosc_calibration = data->have_iosc_calibration;
 
 	if (data->have_ext_osc32k) {

--
2.54.0

#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] Replacing of_match_device() with of_device_get_match_data() fetches match data from the parent…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] Replacing of_match_device() with of_device_get_match_data() fetches match data from the parent RTC driver, leading to an invalid pointer dereference and a kernel panic.
- [High] Bypassing the local CCU match table breaks device-specific configuration and introduces dead code.
--

commit 64f27933b5ce1d6dc6bf932d297fcbf5ea1680b5
Author: Rosen Penev <[email protected]>

clk: sunxi-ng: Use of_device_get_match_data()

This commit changes sun6i_rtc_ccu_probe() to use of_device_get_match_data() instead of explicitly calling of_match_device() with sun6i_rtc_ccu_match. This allows dropping the linux/of_device.h include.

Quoted 4 lines
drivers/clk/sunxi-ng/ccu-sun6i-rtc.c +0 -0
index f6bfeba009e8e..a3cf0dde05bed 100644

[ ... ]

drivers/clk/sunxi-ng/ccu-sun6i-rtc.c int sun6i_rtc_ccu_probe(struct device *dev, void __iomem *reg) · line 357

Quoted 10 lines
drivers/clk/sunxi-ng/ccu-sun6i-rtc.c +1 -3
@@ -353,14 +352,12 @@ int sun6i_rtc_ccu_probe(struct device *dev, void __iomem *reg)
 {
 	const struct sun6i_rtc_match_data *data;
 	struct clk *ext_osc32k_clk = NULL;
-	const struct of_device_id *match;
 
 	/* This driver is only used for newer variants of the hardware. */
-	match = of_match_device(sun6i_rtc_ccu_match, dev);
-	if (!match)
+	data = of_device_get_match_data(dev);

Does this cause an invalid pointer dereference?

Since sun6i_rtc_ccu_probe() is called by the main RTC driver in drivers/rtc/rtc-sun6i.c, the dev passed here is bound to that driver. Calling of_device_get_match_data(dev) retrieves the match data from the main RTC driver's match table (sun6i_rtc_dt_ids), not the local sun6i_rtc_ccu_match table.

The main RTC driver stores integer flags in its match data:

.data = (void *)RTC_LINEAR_DAY

Will this result in the data pointer evaluating to an integer like 0x1?

Additionally, does bypassing the local CCU match table break device-specific configurations?

The sun6i_rtc_ccu_match array contains specialized CCU parameters for devices like allwinner,sun55i-a523-rtc. Relying on the main RTC driver's match table means the lookup will fall back to generic settings, such as those for the r329, instead of applying the specific settings required for the a523.

Does removing the reference to sun6i_rtc_ccu_match also leave the array entirely unused, turning it into dead code?

drivers/clk/sunxi-ng/ccu-sun6i-rtc.c int sun6i_rtc_ccu_probe(struct device *dev, void __iomem *reg) · line 361

Quoted 5 lines
drivers/clk/sunxi-ng/ccu-sun6i-rtc.c +1 -1
+	if (!data)
 		return 0;
 
-	data = match->data;
 	have_iosc_calibration = data->have_iosc_calibration;

If data points to an invalid address like 0x1, won't dereferencing it here cause a kernel panic on newer SoCs?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Rosen Penev

Hi Rosen,

Quoted 24 lines · Rosen Penev

Use of_device_get_match_data() to fetch the RTC CCU match data directly instead of open-coding an of_match_device() lookup.

This also lets the driver drop the of_device.h include.

Assisted-by Codex:GPT-5.5
---
 drivers/clk/sunxi-ng/ccu-sun6i-rtc.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/clk/sunxi-ng/ccu-sun6i-rtc.c +0 -1
index f6bfeba009e8..a3cf0dde05be 100644
@@ -9,7 +9,6 @@
 #include <linux/io.h>
 #include <linux/module.h>
 #include <linux/of.h>
-#include <linux/of_device.h>
 
 #include <linux/clk/sunxi-ng.h>
 

drivers/clk/sunxi-ng/ccu-sun6i-rtc.c int sun6i_rtc_ccu_probe(struct device *dev, void __iomem *reg) · line 357

Quoted 9 lines
drivers/clk/sunxi-ng/ccu-sun6i-rtc.c +1 -3
@@ -353,14 +352,12 @@ int sun6i_rtc_ccu_probe(struct device *dev, void __iomem *reg)
 {
 	const struct sun6i_rtc_match_data *data;
 	struct clk *ext_osc32k_clk = NULL;
-	const struct of_device_id *match;
 
 	/* This driver is only used for newer variants of the hardware. */
-	match = of_match_device(sun6i_rtc_ccu_match, dev);
-	if (!match)
+	data = of_device_get_match_data(dev);

sun6i_rtc_ccu_match becomes unused after this change. It'll use the table associated the dev from drivers/rtc/rtc-sun6i.c.

Brian

Quoted 10 lines
drivers/clk/sunxi-ng/ccu-sun6i-rtc.c +1 -2
+	if (!data)
 		return 0;
 
-	data = match->data;
 	have_iosc_calibration = data->have_iosc_calibration;
 
 	if (data->have_ext_osc32k) {
--

2.54.0