[2/3] i2c: muxes: i2c-demux-pinctrl: Use of_get_i2c_adapter_by_node()

Message ID 20231019073533.386563-3-herve.codina@bootlin.com
State New
Headers
Series Fix i2c mux module refcount issues |

Commit Message

Herve Codina Oct. 19, 2023, 7:35 a.m. UTC
  i2c-demux-pinctrl uses the pair of_find_i2c_adapter_by_node() /
i2c_put_adapter(). These pair alone is not correct to properly lock the
I2C parent adapter.

Indeed,	i2c_put_adapter() decrements the module refcount while
of_find_i2c_adapter_by_node() does not increment it. This leads to an
overflow over zero of the parent adapter user counter.

Use the	dedicated function, of_get_i2c_adapter_by_node(), to handle
correctly the module refcount.

Signed-off-by: Herve Codina <herve.codina@bootlin.com>
---
 drivers/i2c/muxes/i2c-demux-pinctrl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Peter Rosin Oct. 19, 2023, 9:26 a.m. UTC | #1
Hi!

2023-10-19 at 09:35, Herve Codina wrote:
> i2c-demux-pinctrl uses the pair of_find_i2c_adapter_by_node() /
> i2c_put_adapter(). These pair alone is not correct to properly lock the
> I2C parent adapter.
> 
> Indeed,	i2c_put_adapter() decrements the module refcount while
> of_find_i2c_adapter_by_node() does not increment it. This leads to an
> overflow over zero of the parent adapter user counter.

Perhaps "... underflow of the parent module refcount."?

> 
> Use the	dedicated function, of_get_i2c_adapter_by_node(), to handle
> correctly the module refcount.
> 
> Signed-off-by: Herve Codina <herve.codina@bootlin.com>

Good find!

Fixes: 50a5ba876908 ("i2c: mux: demux-pinctrl: add driver")
Acked-by: Peter Rosin <peda@axentia.se>

Cheers,
Peter

> ---
>  drivers/i2c/muxes/i2c-demux-pinctrl.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/i2c/muxes/i2c-demux-pinctrl.c b/drivers/i2c/muxes/i2c-demux-pinctrl.c
> index 22f2280eab7f..9f2e4aa28159 100644
> --- a/drivers/i2c/muxes/i2c-demux-pinctrl.c
> +++ b/drivers/i2c/muxes/i2c-demux-pinctrl.c
> @@ -61,7 +61,7 @@ static int i2c_demux_activate_master(struct i2c_demux_pinctrl_priv *priv, u32 ne
>  	if (ret)
>  		goto err;
>  
> -	adap = of_find_i2c_adapter_by_node(priv->chan[new_chan].parent_np);
> +	adap = of_get_i2c_adapter_by_node(priv->chan[new_chan].parent_np);
>  	if (!adap) {
>  		ret = -ENODEV;
>  		goto err_with_revert;
  
Herve Codina Oct. 19, 2023, 9:46 a.m. UTC | #2
Hi Peter,
On Thu, 19 Oct 2023 11:26:14 +0200
Peter Rosin <peda@axentia.se> wrote:

> Hi!
> 
> 2023-10-19 at 09:35, Herve Codina wrote:
> > i2c-demux-pinctrl uses the pair of_find_i2c_adapter_by_node() /
> > i2c_put_adapter(). These pair alone is not correct to properly lock the
> > I2C parent adapter.
> > 
> > Indeed,	i2c_put_adapter() decrements the module refcount while
> > of_find_i2c_adapter_by_node() does not increment it. This leads to an
> > overflow over zero of the parent adapter user counter.  
> 
> Perhaps "... underflow of the parent module refcount."?

Will be changed in the next iteration

> 
> > 
> > Use the	dedicated function, of_get_i2c_adapter_by_node(), to handle
> > correctly the module refcount.
> > 
> > Signed-off-by: Herve Codina <herve.codina@bootlin.com>  
> 
> Good find!
> 
> Fixes: 50a5ba876908 ("i2c: mux: demux-pinctrl: add driver")
> Acked-by: Peter Rosin <peda@axentia.se>
> 
Will be added in the next iteration.

Thanks for the review,
Hervé
  

Patch

diff --git a/drivers/i2c/muxes/i2c-demux-pinctrl.c b/drivers/i2c/muxes/i2c-demux-pinctrl.c
index 22f2280eab7f..9f2e4aa28159 100644
--- a/drivers/i2c/muxes/i2c-demux-pinctrl.c
+++ b/drivers/i2c/muxes/i2c-demux-pinctrl.c
@@ -61,7 +61,7 @@  static int i2c_demux_activate_master(struct i2c_demux_pinctrl_priv *priv, u32 ne
 	if (ret)
 		goto err;
 
-	adap = of_find_i2c_adapter_by_node(priv->chan[new_chan].parent_np);
+	adap = of_get_i2c_adapter_by_node(priv->chan[new_chan].parent_np);
 	if (!adap) {
 		ret = -ENODEV;
 		goto err_with_revert;