All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] can: c_can: Use platform id data when OF data is absent
@ 2026-06-24  5:49 Pengpeng Hou
  2026-06-24  9:53 ` Vincent Mailhol
  2026-06-25  5:41 ` [PATCH v2] " Pengpeng Hou
  0 siblings, 2 replies; 5+ messages in thread
From: Pengpeng Hou @ 2026-06-24  5:49 UTC (permalink / raw)
  To: Marc Kleine-Budde, Vincent Mailhol, Pengpeng Hou; +Cc: linux-can, linux-kernel

The platform driver keeps controller metadata in both the OF match table
and the platform id table.  Probe reads the metadata with
device_get_match_data(), which does not fall back to platform id-table
driver_data.

When the device is matched through the platform id table, drvdata can
therefore be NULL before it is dereferenced for msg_obj_num and the
controller type.  Fall back to platform_get_device_id() when firmware
match data is not available.

Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn>
---
 drivers/net/can/c_can/c_can_platform.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/drivers/net/can/c_can/c_can_platform.c b/drivers/net/can/c_can/c_can_platform.c
index 19c86b94a40e..564c9e5b4c2c 100644
--- a/drivers/net/can/c_can/c_can_platform.c
+++ b/drivers/net/can/c_can/c_can_platform.c
@@ -263,9 +263,17 @@ static int c_can_plat_probe(struct platform_device *pdev)
 	int irq;
 	struct clk *clk;
 	const struct c_can_driver_data *drvdata;
+	const struct platform_device_id *id;
 	struct device_node *np = pdev->dev.of_node;
 
 	drvdata = device_get_match_data(&pdev->dev);
+	if (!drvdata) {
+		id = platform_get_device_id(pdev);
+		if (!id)
+			return -ENODEV;
+
+		drvdata = (const struct c_can_driver_data *)id->driver_data;
+	}
 
 	/* get the appropriate clk */
 	clk = devm_clk_get(&pdev->dev, NULL);
-- 
2.50.1 (Apple Git-155)


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] can: c_can: Use platform id data when OF data is absent
  2026-06-24  5:49 [PATCH] can: c_can: Use platform id data when OF data is absent Pengpeng Hou
@ 2026-06-24  9:53 ` Vincent Mailhol
  2026-06-24 18:22   ` Vincent Mailhol
  2026-06-25  5:41 ` [PATCH v2] " Pengpeng Hou
  1 sibling, 1 reply; 5+ messages in thread
From: Vincent Mailhol @ 2026-06-24  9:53 UTC (permalink / raw)
  To: Pengpeng Hou, Marc Kleine-Budde; +Cc: linux-can, linux-kernel

On 24/06/2026 at 07:49, Pengpeng Hou wrote:
> The platform driver keeps controller metadata in both the OF match table
> and the platform id table.  Probe reads the metadata with
> device_get_match_data(), which does not fall back to platform id-table
> driver_data.
> 
> When the device is matched through the platform id table, drvdata can
> therefore be NULL before it is dereferenced for msg_obj_num and the
> controller type.  Fall back to platform_get_device_id() when firmware
> match data is not available.

So, you are telling me that this patch fixes a NULL pointer
dereference? In that case, it needs to be backported. Please add a
Fixes: tag.

Fixes: 5e6c3454b405 ("net: can: Use device_get_match_data()")

> Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn>
> ---
>  drivers/net/can/c_can/c_can_platform.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/drivers/net/can/c_can/c_can_platform.c b/drivers/net/can/c_can/c_can_platform.c
> index 19c86b94a40e..564c9e5b4c2c 100644
> --- a/drivers/net/can/c_can/c_can_platform.c
> +++ b/drivers/net/can/c_can/c_can_platform.c
> @@ -263,9 +263,17 @@ static int c_can_plat_probe(struct platform_device *pdev)
>  	int irq;
>  	struct clk *clk;
>  	const struct c_can_driver_data *drvdata;
> +	const struct platform_device_id *id;
>  	struct device_node *np = pdev->dev.of_node;
>  
>  	drvdata = device_get_match_data(&pdev->dev);
> +	if (!drvdata) {
> +		id = platform_get_device_id(pdev);
> +		if (!id)
> +			return -ENODEV;
> +
> +		drvdata = (const struct c_can_driver_data *)id->driver_data;
> +	}

Reduce the visibility of id:

	if (!drvdata) {
		const struct platform_device_id *id;

		id = platform_get_device_id(pdev);
		if (!id)
			return -ENODEV;

		drvdata = (const struct c_can_driver_data *)id->driver_data;
	}

>  
>  	/* get the appropriate clk */
>  	clk = devm_clk_get(&pdev->dev, NULL);

With the Fixes: tag added and above nitpick addressed:

Reviewed-by: Vincent Mailhol <mailhol@kernel.org>


Yours sincerely,
Vincent Mailhol

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] can: c_can: Use platform id data when OF data is absent
  2026-06-24  9:53 ` Vincent Mailhol
@ 2026-06-24 18:22   ` Vincent Mailhol
  0 siblings, 0 replies; 5+ messages in thread
From: Vincent Mailhol @ 2026-06-24 18:22 UTC (permalink / raw)
  To: Pengpeng Hou, Marc Kleine-Budde; +Cc: linux-can, linux-kernel

On 24/06/2026 at 11:53, Vincent Mailhol wrote:
> On 24/06/2026 at 07:49, Pengpeng Hou wrote:
>> The platform driver keeps controller metadata in both the OF match table
>> and the platform id table.  Probe reads the metadata with
>> device_get_match_data(), which does not fall back to platform id-table
>> driver_data.
>>
>> When the device is matched through the platform id table, drvdata can
>> therefore be NULL before it is dereferenced for msg_obj_num and the
>> controller type.  Fall back to platform_get_device_id() when firmware
>> match data is not available.
> 
> So, you are telling me that this patch fixes a NULL pointer
> dereference? In that case, it needs to be backported. Please add a
> Fixes: tag.

Also, were you able to trigger that NULL pointer dereference?

> Fixes: 5e6c3454b405 ("net: can: Use device_get_match_data()")
> 
>> Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn>
>> ---
>>  drivers/net/can/c_can/c_can_platform.c | 8 ++++++++
>>  1 file changed, 8 insertions(+)
>>
>> diff --git a/drivers/net/can/c_can/c_can_platform.c b/drivers/net/can/c_can/c_can_platform.c
>> index 19c86b94a40e..564c9e5b4c2c 100644
>> --- a/drivers/net/can/c_can/c_can_platform.c
>> +++ b/drivers/net/can/c_can/c_can_platform.c
>> @@ -263,9 +263,17 @@ static int c_can_plat_probe(struct platform_device *pdev)
>>  	int irq;
>>  	struct clk *clk;
>>  	const struct c_can_driver_data *drvdata;
>> +	const struct platform_device_id *id;
>>  	struct device_node *np = pdev->dev.of_node;
>>  
>>  	drvdata = device_get_match_data(&pdev->dev);
>> +	if (!drvdata) {
>> +		id = platform_get_device_id(pdev);
>> +		if (!id)
>> +			return -ENODEV;
>> +
>> +		drvdata = (const struct c_can_driver_data *)id->driver_data;
>> +	}
> 
> Reduce the visibility of id:
> 
> 	if (!drvdata) {
> 		const struct platform_device_id *id;
> 
> 		id = platform_get_device_id(pdev);
> 		if (!id)
> 			return -ENODEV;
> 
> 		drvdata = (const struct c_can_driver_data *)id->driver_data;
> 	}
> 
>>  
>>  	/* get the appropriate clk */
>>  	clk = devm_clk_get(&pdev->dev, NULL);
> 
> With the Fixes: tag added and above nitpick addressed:
> 
> Reviewed-by: Vincent Mailhol <mailhol@kernel.org>


Yours sincerely,
Vincent Mailhol


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2] can: c_can: Use platform id data when OF data is absent
  2026-06-24  5:49 [PATCH] can: c_can: Use platform id data when OF data is absent Pengpeng Hou
  2026-06-24  9:53 ` Vincent Mailhol
@ 2026-06-25  5:41 ` Pengpeng Hou
  2026-06-25  5:50   ` sashiko-bot
  1 sibling, 1 reply; 5+ messages in thread
From: Pengpeng Hou @ 2026-06-25  5:41 UTC (permalink / raw)
  To: Marc Kleine-Budde, Vincent Mailhol
  Cc: Pengpeng Hou, Rob Herring, linux-can, linux-kernel, stable

The platform driver keeps controller metadata in both the OF match table
and the platform id table. Probe reads the metadata with
device_get_match_data(), which does not fall back to platform id-table
driver_data.

When the device is matched through the platform id table, drvdata can
therefore be NULL before it is dereferenced for msg_obj_num and the
controller type. Fall back to platform_get_device_id() when firmware
match data is not available.

Fixes: 5e6c3454b405 ("net: can: Use device_get_match_data()")
Cc: stable@vger.kernel.org
Reviewed-by: Vincent Mailhol <mailhol@kernel.org>
Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn>
---
Changes in v2:
- Add the Fixes tag requested by Vincent.
- Scope the platform id variable inside the fallback block.
- Carry Vincent's Reviewed-by for the requested v2 shape.

 drivers/net/can/c_can/c_can_platform.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/drivers/net/can/c_can/c_can_platform.c b/drivers/net/can/c_can/c_can_platform.c
index 19c86b94a40e..8a0c88839d24 100644
--- a/drivers/net/can/c_can/c_can_platform.c
+++ b/drivers/net/can/c_can/c_can_platform.c
@@ -267,6 +267,14 @@ static int c_can_plat_probe(struct platform_device *pdev)
 	struct device_node *np = pdev->dev.of_node;
 
 	drvdata = device_get_match_data(&pdev->dev);
+	if (!drvdata) {
+		const struct platform_device_id *id;
+
+		id = platform_get_device_id(pdev);
+		if (!id)
+			return -ENODEV;
+		drvdata = (const struct c_can_driver_data *)id->driver_data;
+	}
 
 	/* get the appropriate clk */
 	clk = devm_clk_get(&pdev->dev, NULL);
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] can: c_can: Use platform id data when OF data is absent
  2026-06-25  5:41 ` [PATCH v2] " Pengpeng Hou
@ 2026-06-25  5:50   ` sashiko-bot
  0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-06-25  5:50 UTC (permalink / raw)
  To: Pengpeng Hou
  Cc: Marc Kleine-Budde, linux-can, Vincent Mailhol, Oleksij Rempel,
	Oliver Hartkopp

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the regression fixed by this patch in `c_can_platform.c` is also present in `flexcan-core.c`, which was modified by the same original commit but remains unfixed. Commit 5e6c3454b405 incorrectly replaced `of_match_device()` and `platform_get_device_id()` checks with a single call to `device_get_match_data()`. Because `device_get_match_data()` does not fall back to `platform_device_id` tables, any driver matching via platform ID will receive a NULL pointer. In `flexcan-core.c`, the legacy non-DT platform "flexcan-mcf5441x" is matched via `flexcan_id_table`. When probed, `device_get_match_data()` returns NULL, which `flexcan_probe()` immediately dereferences (`devtype_data->quirks`), resulting in a kernel panic.
--

commit c16eef82e68ac57940a1f18250576d39cf8e239b
Author: Pengpeng Hou <pengpeng@iscas.ac.cn>

can: c_can: Use platform id data when OF data is absent
    
The platform driver keeps controller metadata in both the OF match table
and the platform id table. Probe reads the metadata with
device_get_match_data(), which does not fall back to platform id-table
driver_data.

When the device is matched through the platform id table, drvdata can
therefore be NULL before it is dereferenced for msg_obj_num and the
controller type. Fall back to platform_get_device_id() when firmware
match data is not available.

> diff --git a/drivers/net/can/c_can/c_can_platform.c b/drivers/net/can/c_can/c_can_platform.c
> index 19c86b94a40e4..d3529b4cc856e 100644
> --- a/drivers/net/can/c_can/c_can_platform.c
> +++ b/drivers/net/can/c_can/c_can_platform.c
> @@ -266,6 +266,14 @@ static int c_can_plat_probe(struct platform_device *pdev)
>  	struct device_node *np = pdev->dev.of_node;
>  
>  	drvdata = device_get_match_data(&pdev->dev);
> +	if (!drvdata) {
> +		const struct platform_device_id *id;
> +
> +		id = platform_get_device_id(pdev);
> +		if (!id)
> +			return -ENODEV;
> +		drvdata = (const struct c_can_driver_data *)id->driver_data;
> +	}

[Severity: High]
This isn't a bug introduced by this patch, but this patch fixes a regression
that seems to also be present in another driver modified by the same original
commit (5e6c3454b405).

In drivers/net/can/flexcan/flexcan-core.c in flexcan_probe(), the driver
matches via flexcan_id_table for the legacy non-DT platform flexcan-mcf5441x:

    devtype_data = device_get_match_data(&pdev->dev);

    if ((devtype_data->quirks & FLEXCAN_QUIRK_SUPPORT_FD) && ...

When probed on a non-DT platform device, device_get_match_data() returns
NULL, which is immediately dereferenced.

Should a similar fallback to platform_get_device_id() be added to
flexcan_probe() to prevent a NULL pointer dereference on these platforms?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260625054152.68413-1-pengpeng@iscas.ac.cn?part=1

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-06-25  5:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-24  5:49 [PATCH] can: c_can: Use platform id data when OF data is absent Pengpeng Hou
2026-06-24  9:53 ` Vincent Mailhol
2026-06-24 18:22   ` Vincent Mailhol
2026-06-25  5:41 ` [PATCH v2] " Pengpeng Hou
2026-06-25  5:50   ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.