[PATCH 06/12] i2c: rcar: set device parent and of_node through the adapter struct

Bartosz Golaszewski posted 12 patches 1 month, 2 weeks ago
[PATCH 06/12] i2c: rcar: set device parent and of_node through the adapter struct
Posted by Bartosz Golaszewski 1 month, 2 weeks ago
Configure the parent device and the OF-node using dedicated fields in
struct i2c_adapter and avoid dereferencing the internal struct device.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
---
 drivers/i2c/busses/i2c-rcar.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/i2c/busses/i2c-rcar.c b/drivers/i2c/busses/i2c-rcar.c
index 5ce8f8e4856fb2bb21c9cd731ddbbd8f3622ca26..86826502533bf866ca81cb86ddd10b2a2be3c798 100644
--- a/drivers/i2c/busses/i2c-rcar.c
+++ b/drivers/i2c/busses/i2c-rcar.c
@@ -1149,8 +1149,8 @@ static int rcar_i2c_probe(struct platform_device *pdev)
 	adap->algo = &rcar_i2c_algo;
 	adap->class = I2C_CLASS_DEPRECATED;
 	adap->retries = 3;
-	adap->dev.parent = dev;
-	adap->dev.of_node = dev->of_node;
+	adap->parent = dev;
+	adap->of_node = dev->of_node;
 	adap->bus_recovery_info = &rcar_i2c_bri;
 	adap->quirks = &rcar_i2c_quirks;
 	i2c_set_adapdata(adap, priv);

-- 
2.47.3
Re: [PATCH 06/12] i2c: rcar: set device parent and of_node through the adapter struct
Posted by Geert Uytterhoeven 1 month ago
Hi Bartosz,

On Tue, 23 Dec 2025 at 11:05, Bartosz Golaszewski
<bartosz.golaszewski@oss.qualcomm.com> wrote:
> Configure the parent device and the OF-node using dedicated fields in
> struct i2c_adapter and avoid dereferencing the internal struct device.
>
> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>

Thanks for your patch!

> --- a/drivers/i2c/busses/i2c-rcar.c
> +++ b/drivers/i2c/busses/i2c-rcar.c
> @@ -1149,8 +1149,8 @@ static int rcar_i2c_probe(struct platform_device *pdev)
>         adap->algo = &rcar_i2c_algo;
>         adap->class = I2C_CLASS_DEPRECATED;
>         adap->retries = 3;
> -       adap->dev.parent = dev;
> -       adap->dev.of_node = dev->of_node;
> +       adap->parent = dev;
> +       adap->of_node = dev->of_node;
>         adap->bus_recovery_info = &rcar_i2c_bri;
>         adap->quirks = &rcar_i2c_quirks;
>         i2c_set_adapdata(adap, priv);

    [....]
    ret = rcar_i2c_clock_calculate(priv);
    [...]
    ret = i2c_add_numbered_adapter(adap);

As rcar_i2c_clock_calculate() uses rcar_i2c_priv_to_dev(priv):

    #define rcar_i2c_priv_to_dev(p)         ((p)->adap.dev.parent)

I believe this will deference dev.parent before it is initialized by
the newly added code in i2c_register_adapter().

Let's check...

    Unable to handle kernel NULL pointer dereference at virtual
address 0000024c when read
    [0000024c] *pgd=80000040004003, *pmd=00000000
    Internal error: Oops: 206 [#1] SMP ARM
    Modules linked in:
    CPU: 1 UID: 0 PID: 1 Comm: swapper/0 Not tainted
6.19.0-rc3-koelsch-04401-g9e22366a23cd #2288 NONE
    Hardware name: Generic R-Car Gen2 (Flattened Device Tree)
    PC is at device_property_read_u32_array+0x0/0x14
    LR is at i2c_parse_timing+0x14/0x30
    [...]
    Call trace:
     device_property_read_u32_array from i2c_parse_timing+0x14/0x30
     i2c_parse_timing from i2c_parse_fw_timings+0x28/0x12c
     i2c_parse_fw_timings from rcar_i2c_probe+0x1cc/0x534
     rcar_i2c_probe from platform_probe+0x58/0x90

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
Re: [PATCH 06/12] i2c: rcar: set device parent and of_node through the adapter struct
Posted by Bartosz Golaszewski 1 month ago
On Tue, Jan 6, 2026 at 10:14 AM Geert Uytterhoeven <geert@linux-m68k.org> wrote:
>
> Hi Bartosz,
>
> On Tue, 23 Dec 2025 at 11:05, Bartosz Golaszewski
> <bartosz.golaszewski@oss.qualcomm.com> wrote:
> > Configure the parent device and the OF-node using dedicated fields in
> > struct i2c_adapter and avoid dereferencing the internal struct device.
> >
> > Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
>
> Thanks for your patch!
>
> > --- a/drivers/i2c/busses/i2c-rcar.c
> > +++ b/drivers/i2c/busses/i2c-rcar.c
> > @@ -1149,8 +1149,8 @@ static int rcar_i2c_probe(struct platform_device *pdev)
> >         adap->algo = &rcar_i2c_algo;
> >         adap->class = I2C_CLASS_DEPRECATED;
> >         adap->retries = 3;
> > -       adap->dev.parent = dev;
> > -       adap->dev.of_node = dev->of_node;
> > +       adap->parent = dev;
> > +       adap->of_node = dev->of_node;
> >         adap->bus_recovery_info = &rcar_i2c_bri;
> >         adap->quirks = &rcar_i2c_quirks;
> >         i2c_set_adapdata(adap, priv);
>
>     [....]
>     ret = rcar_i2c_clock_calculate(priv);
>     [...]
>     ret = i2c_add_numbered_adapter(adap);
>
> As rcar_i2c_clock_calculate() uses rcar_i2c_priv_to_dev(priv):
>
>     #define rcar_i2c_priv_to_dev(p)         ((p)->adap.dev.parent)
>
> I believe this will deference dev.parent before it is initialized by
> the newly added code in i2c_register_adapter().
>
> Let's check...
>
>     Unable to handle kernel NULL pointer dereference at virtual
> address 0000024c when read
>     [0000024c] *pgd=80000040004003, *pmd=00000000
>     Internal error: Oops: 206 [#1] SMP ARM
>     Modules linked in:
>     CPU: 1 UID: 0 PID: 1 Comm: swapper/0 Not tainted
> 6.19.0-rc3-koelsch-04401-g9e22366a23cd #2288 NONE
>     Hardware name: Generic R-Car Gen2 (Flattened Device Tree)
>     PC is at device_property_read_u32_array+0x0/0x14
>     LR is at i2c_parse_timing+0x14/0x30
>     [...]
>     Call trace:
>      device_property_read_u32_array from i2c_parse_timing+0x14/0x30
>      i2c_parse_timing from i2c_parse_fw_timings+0x28/0x12c
>      i2c_parse_fw_timings from rcar_i2c_probe+0x1cc/0x534
>      rcar_i2c_probe from platform_probe+0x58/0x90
>

Thanks for the heads-up. I'm still waiting for Wolfram to decide if
these changes will be accepted or not before sending a v2.

Bartosz