diff mbox series

[15/22] Input: iqs7222 - use cleanup facility for fwnodes

Message ID 20240904044825.1048256-1-dmitry.torokhov@gmail.com (mailing list archive)
State New
Headers show
Series Convert misc input drivers to use new cleanup facilities | expand

Commit Message

Dmitry Torokhov Sept. 4, 2024, 4:48 a.m. UTC
Use __free(fwnode_handle) cleanup facility to ensure that references to
acquired fwnodes are dropped at appropriate times automatically.

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
 drivers/input/misc/iqs7222.c | 30 ++++++++++++++----------------
 1 file changed, 14 insertions(+), 16 deletions(-)

Comments

Javier Carrasco Sept. 4, 2024, 10:50 a.m. UTC | #1
Hi Dmitry,

On 04/09/2024 06:48, Dmitry Torokhov wrote:
> Use __free(fwnode_handle) cleanup facility to ensure that references to
> acquired fwnodes are dropped at appropriate times automatically.
> 
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> ---
>  drivers/input/misc/iqs7222.c | 30 ++++++++++++++----------------
>  1 file changed, 14 insertions(+), 16 deletions(-)
> 
> diff --git a/drivers/input/misc/iqs7222.c b/drivers/input/misc/iqs7222.c
> index 9ca5a743f19f..d9b87606ff7a 100644
> --- a/drivers/input/misc/iqs7222.c
> +++ b/drivers/input/misc/iqs7222.c

...

> @@ -2818,9 +2813,9 @@ static int iqs7222_parse_reg_grp(struct iqs7222_private *iqs7222,
>  				 int reg_grp_index)
>  {
>  	struct i2c_client *client = iqs7222->client;
> -	struct fwnode_handle *reg_grp_node;
>  	int error;
>  

Nit: reg_grp_node could stay at the top (where it used to be), as you
are assigning it to NULL because there are no sensible assignments at
this point.

> +	struct fwnode_handle *reg_grp_node __free(fwnode_handle) = NULL;
>  	if (iqs7222_reg_grp_names[reg_grp]) {
>  		char reg_grp_name[16];
>  

Best regards,
Javier Carrasco
Dmitry Torokhov Sept. 4, 2024, 6:26 p.m. UTC | #2
On Wed, Sep 04, 2024 at 12:50:44PM +0200, Javier Carrasco wrote:
> Hi Dmitry,
> 
> On 04/09/2024 06:48, Dmitry Torokhov wrote:
> > Use __free(fwnode_handle) cleanup facility to ensure that references to
> > acquired fwnodes are dropped at appropriate times automatically.
> > 
> > Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> > ---
> >  drivers/input/misc/iqs7222.c | 30 ++++++++++++++----------------
> >  1 file changed, 14 insertions(+), 16 deletions(-)
> > 
> > diff --git a/drivers/input/misc/iqs7222.c b/drivers/input/misc/iqs7222.c
> > index 9ca5a743f19f..d9b87606ff7a 100644
> > --- a/drivers/input/misc/iqs7222.c
> > +++ b/drivers/input/misc/iqs7222.c
> 
> ...
> 
> > @@ -2818,9 +2813,9 @@ static int iqs7222_parse_reg_grp(struct iqs7222_private *iqs7222,
> >  				 int reg_grp_index)
> >  {
> >  	struct i2c_client *client = iqs7222->client;
> > -	struct fwnode_handle *reg_grp_node;
> >  	int error;
> >  
> 
> Nit: reg_grp_node could stay at the top (where it used to be), as you
> are assigning it to NULL because there are no sensible assignments at
> this point.
> 
> > +	struct fwnode_handle *reg_grp_node __free(fwnode_handle) = NULL;
> >  	if (iqs7222_reg_grp_names[reg_grp]) {
> >  		char reg_grp_name[16];

I think this follows Linus' guidance (in spirit) to combine declaration
and initialization for objects using __cleanup(). If it was Rust I'd
written it as

	let reg_grp_node = if let Some(...) { ... } else { ... };

so declaration and initialization would be the same, but with C this is
the closest I could come up with.

Thanks.
Javier Carrasco Sept. 4, 2024, 6:46 p.m. UTC | #3
On 04/09/2024 20:26, Dmitry Torokhov wrote:
> On Wed, Sep 04, 2024 at 12:50:44PM +0200, Javier Carrasco wrote:
>> Hi Dmitry,
>>
>> On 04/09/2024 06:48, Dmitry Torokhov wrote:
>>> Use __free(fwnode_handle) cleanup facility to ensure that references to
>>> acquired fwnodes are dropped at appropriate times automatically.
>>>
>>> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
>>> ---
>>>  drivers/input/misc/iqs7222.c | 30 ++++++++++++++----------------
>>>  1 file changed, 14 insertions(+), 16 deletions(-)
>>>
>>> diff --git a/drivers/input/misc/iqs7222.c b/drivers/input/misc/iqs7222.c
>>> index 9ca5a743f19f..d9b87606ff7a 100644
>>> --- a/drivers/input/misc/iqs7222.c
>>> +++ b/drivers/input/misc/iqs7222.c
>>
>> ...
>>
>>> @@ -2818,9 +2813,9 @@ static int iqs7222_parse_reg_grp(struct iqs7222_private *iqs7222,
>>>  				 int reg_grp_index)
>>>  {
>>>  	struct i2c_client *client = iqs7222->client;
>>> -	struct fwnode_handle *reg_grp_node;
>>>  	int error;
>>>  
>>
>> Nit: reg_grp_node could stay at the top (where it used to be), as you
>> are assigning it to NULL because there are no sensible assignments at
>> this point.
>>
>>> +	struct fwnode_handle *reg_grp_node __free(fwnode_handle) = NULL;
>>>  	if (iqs7222_reg_grp_names[reg_grp]) {
>>>  		char reg_grp_name[16];
> 
> I think this follows Linus' guidance (in spirit) to combine declaration
> and initialization for objects using __cleanup(). If it was Rust I'd
> written it as
> 
> 	let reg_grp_node = if let Some(...) { ... } else { ... };
> 
> so declaration and initialization would be the same, but with C this is
> the closest I could come up with.
> 
> Thanks.
> 

Combining the declaration and initialization was right, no doubt about
that. I was just nitpicking that the variable declaration could have
been done at the top, as it used to be. The same as you did, but not
separating the declaration from the rest as there are no instructions in
between.

My second thought was that you might be attempting to declare the
variable as near as possible to the "some" initialization, so you moved
it a little bit to get it closer :)

Either way, if you did that on purpose, then

Reviewed-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>
Jeff LaBundy Sept. 9, 2024, 12:12 a.m. UTC | #4
Hi Dmitry,

On Tue, Sep 03, 2024 at 09:48:25PM -0700, Dmitry Torokhov wrote:
> Use __free(fwnode_handle) cleanup facility to ensure that references to
> acquired fwnodes are dropped at appropriate times automatically.
> 
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> ---
>  drivers/input/misc/iqs7222.c | 30 ++++++++++++++----------------
>  1 file changed, 14 insertions(+), 16 deletions(-)
> 
> diff --git a/drivers/input/misc/iqs7222.c b/drivers/input/misc/iqs7222.c
> index 9ca5a743f19f..d9b87606ff7a 100644
> --- a/drivers/input/misc/iqs7222.c
> +++ b/drivers/input/misc/iqs7222.c
> @@ -2385,9 +2385,9 @@ static int iqs7222_parse_chan(struct iqs7222_private *iqs7222,
>  	for (i = 0; i < ARRAY_SIZE(iqs7222_kp_events); i++) {
>  		const char *event_name = iqs7222_kp_events[i].name;
>  		u16 event_enable = iqs7222_kp_events[i].enable;
> -		struct fwnode_handle *event_node;
>  
> -		event_node = fwnode_get_named_child_node(chan_node, event_name);
> +		struct fwnode_handle *event_node __free(fwnode_handle) =
> +			fwnode_get_named_child_node(chan_node, event_name);

This leaves a newline amongst the declarations, but not in between the
declarations and code. I don't feel strongly against this, but it's
opposite of what I understand to be more common; please let me know in
case I have misunderstood. This comment applies to the other chunks as
well.

>  		if (!event_node)
>  			continue;
>  
> @@ -2408,7 +2408,6 @@ static int iqs7222_parse_chan(struct iqs7222_private *iqs7222,
>  				dev_err(&client->dev,
>  					"Invalid %s press timeout: %u\n",
>  					fwnode_get_name(event_node), val);
> -				fwnode_handle_put(event_node);
>  				return -EINVAL;
>  			}
>  
> @@ -2418,7 +2417,6 @@ static int iqs7222_parse_chan(struct iqs7222_private *iqs7222,
>  			dev_err(&client->dev,
>  				"Failed to read %s press timeout: %d\n",
>  				fwnode_get_name(event_node), error);
> -			fwnode_handle_put(event_node);
>  			return error;
>  		}
>  
> @@ -2429,7 +2427,6 @@ static int iqs7222_parse_chan(struct iqs7222_private *iqs7222,
>  					    dev_desc->touch_link - (i ? 0 : 2),
>  					    &iqs7222->kp_type[chan_index][i],
>  					    &iqs7222->kp_code[chan_index][i]);
> -		fwnode_handle_put(event_node);
>  		if (error)
>  			return error;
>  
> @@ -2604,10 +2601,10 @@ static int iqs7222_parse_sldr(struct iqs7222_private *iqs7222,
>  
>  	for (i = 0; i < ARRAY_SIZE(iqs7222_sl_events); i++) {
>  		const char *event_name = iqs7222_sl_events[i].name;
> -		struct fwnode_handle *event_node;
>  		enum iqs7222_reg_key_id reg_key;
>  
> -		event_node = fwnode_get_named_child_node(sldr_node, event_name);
> +		struct fwnode_handle *event_node __free(fwnode_handle) =
> +			fwnode_get_named_child_node(sldr_node, event_name);
>  		if (!event_node)
>  			continue;
>  
> @@ -2639,7 +2636,6 @@ static int iqs7222_parse_sldr(struct iqs7222_private *iqs7222,
>  					      : sldr_setup[4 + reg_offset],
>  					    NULL,
>  					    &iqs7222->sl_code[sldr_index][i]);
> -		fwnode_handle_put(event_node);
>  		if (error)
>  			return error;
>  
> @@ -2742,9 +2738,9 @@ static int iqs7222_parse_tpad(struct iqs7222_private *iqs7222,
>  
>  	for (i = 0; i < ARRAY_SIZE(iqs7222_tp_events); i++) {
>  		const char *event_name = iqs7222_tp_events[i].name;
> -		struct fwnode_handle *event_node;
>  
> -		event_node = fwnode_get_named_child_node(tpad_node, event_name);
> +		struct fwnode_handle *event_node __free(fwnode_handle) =
> +			fwnode_get_named_child_node(tpad_node, event_name);
>  		if (!event_node)
>  			continue;
>  
> @@ -2760,7 +2756,6 @@ static int iqs7222_parse_tpad(struct iqs7222_private *iqs7222,
>  					    iqs7222_tp_events[i].link, 1566,
>  					    NULL,
>  					    &iqs7222->tp_code[i]);
> -		fwnode_handle_put(event_node);
>  		if (error)
>  			return error;
>  
> @@ -2818,9 +2813,9 @@ static int iqs7222_parse_reg_grp(struct iqs7222_private *iqs7222,
>  				 int reg_grp_index)
>  {
>  	struct i2c_client *client = iqs7222->client;
> -	struct fwnode_handle *reg_grp_node;
>  	int error;
>  
> +	struct fwnode_handle *reg_grp_node __free(fwnode_handle) = NULL;
>  	if (iqs7222_reg_grp_names[reg_grp]) {
>  		char reg_grp_name[16];
>  
> @@ -2838,14 +2833,17 @@ static int iqs7222_parse_reg_grp(struct iqs7222_private *iqs7222,
>  
>  	error = iqs7222_parse_props(iqs7222, reg_grp_node, reg_grp_index,
>  				    reg_grp, IQS7222_REG_KEY_NONE);
> +	if (error)
> +		return error;
>  
> -	if (!error && iqs7222_parse_extra[reg_grp])
> +	if (iqs7222_parse_extra[reg_grp]) {
>  		error = iqs7222_parse_extra[reg_grp](iqs7222, reg_grp_node,
>  						     reg_grp_index);
> +		if (error)
> +			return error;
> +	}
>  
> -	fwnode_handle_put(reg_grp_node);
> -
> -	return error;
> +	return 0;
>  }
>  
>  static int iqs7222_parse_all(struct iqs7222_private *iqs7222)
> -- 
> 2.46.0.469.g59c65b2a67-goog
> 

Kind regards,
Jeff LaBundy
Dmitry Torokhov Sept. 9, 2024, 1:34 a.m. UTC | #5
Hi Jeff,

On Sun, Sep 08, 2024 at 07:12:10PM -0500, Jeff LaBundy wrote:
> Hi Dmitry,
> 
> On Tue, Sep 03, 2024 at 09:48:25PM -0700, Dmitry Torokhov wrote:
> > Use __free(fwnode_handle) cleanup facility to ensure that references to
> > acquired fwnodes are dropped at appropriate times automatically.
> > 
> > Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> > ---
> >  drivers/input/misc/iqs7222.c | 30 ++++++++++++++----------------
> >  1 file changed, 14 insertions(+), 16 deletions(-)
> > 
> > diff --git a/drivers/input/misc/iqs7222.c b/drivers/input/misc/iqs7222.c
> > index 9ca5a743f19f..d9b87606ff7a 100644
> > --- a/drivers/input/misc/iqs7222.c
> > +++ b/drivers/input/misc/iqs7222.c
> > @@ -2385,9 +2385,9 @@ static int iqs7222_parse_chan(struct iqs7222_private *iqs7222,
> >  	for (i = 0; i < ARRAY_SIZE(iqs7222_kp_events); i++) {
> >  		const char *event_name = iqs7222_kp_events[i].name;
> >  		u16 event_enable = iqs7222_kp_events[i].enable;
> > -		struct fwnode_handle *event_node;
> >  
> > -		event_node = fwnode_get_named_child_node(chan_node, event_name);
> > +		struct fwnode_handle *event_node __free(fwnode_handle) =
> > +			fwnode_get_named_child_node(chan_node, event_name);
> 
> This leaves a newline amongst the declarations, but not in between the
> declarations and code. I don't feel strongly against this, but it's
> opposite of what I understand to be more common; please let me know in
> case I have misunderstood. This comment applies to the other chunks as
> well.

Right, so this is actually combining declaration and initialization case
that I mentioned in the other email, and it is closer in spirit to the
code and the allocation check below. That is why it is separated from
the declaration block.

Thanks.
Jeff LaBundy Sept. 10, 2024, 3:14 p.m. UTC | #6
Hi Dmitry,

On Sun, Sep 08, 2024 at 06:34:21PM -0700, Dmitry Torokhov wrote:
> Hi Jeff,
> 
> On Sun, Sep 08, 2024 at 07:12:10PM -0500, Jeff LaBundy wrote:
> > Hi Dmitry,
> > 
> > On Tue, Sep 03, 2024 at 09:48:25PM -0700, Dmitry Torokhov wrote:
> > > Use __free(fwnode_handle) cleanup facility to ensure that references to
> > > acquired fwnodes are dropped at appropriate times automatically.
> > > 
> > > Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> > > ---
> > >  drivers/input/misc/iqs7222.c | 30 ++++++++++++++----------------
> > >  1 file changed, 14 insertions(+), 16 deletions(-)
> > > 
> > > diff --git a/drivers/input/misc/iqs7222.c b/drivers/input/misc/iqs7222.c
> > > index 9ca5a743f19f..d9b87606ff7a 100644
> > > --- a/drivers/input/misc/iqs7222.c
> > > +++ b/drivers/input/misc/iqs7222.c
> > > @@ -2385,9 +2385,9 @@ static int iqs7222_parse_chan(struct iqs7222_private *iqs7222,
> > >  	for (i = 0; i < ARRAY_SIZE(iqs7222_kp_events); i++) {
> > >  		const char *event_name = iqs7222_kp_events[i].name;
> > >  		u16 event_enable = iqs7222_kp_events[i].enable;
> > > -		struct fwnode_handle *event_node;
> > >  
> > > -		event_node = fwnode_get_named_child_node(chan_node, event_name);
> > > +		struct fwnode_handle *event_node __free(fwnode_handle) =
> > > +			fwnode_get_named_child_node(chan_node, event_name);
> > 
> > This leaves a newline amongst the declarations, but not in between the
> > declarations and code. I don't feel strongly against this, but it's
> > opposite of what I understand to be more common; please let me know in
> > case I have misunderstood. This comment applies to the other chunks as
> > well.
> 
> Right, so this is actually combining declaration and initialization case
> that I mentioned in the other email, and it is closer in spirit to the
> code and the allocation check below. That is why it is separated from
> the declaration block.

Thanks again for the background information; please feel free to add:

Reviewed-by: Jeff LaBundy <jeff@labundy.com>

> 
> Thanks.
> 
> -- 
> Dmitry

Kind regards,
Jeff LaBundy
diff mbox series

Patch

diff --git a/drivers/input/misc/iqs7222.c b/drivers/input/misc/iqs7222.c
index 9ca5a743f19f..d9b87606ff7a 100644
--- a/drivers/input/misc/iqs7222.c
+++ b/drivers/input/misc/iqs7222.c
@@ -2385,9 +2385,9 @@  static int iqs7222_parse_chan(struct iqs7222_private *iqs7222,
 	for (i = 0; i < ARRAY_SIZE(iqs7222_kp_events); i++) {
 		const char *event_name = iqs7222_kp_events[i].name;
 		u16 event_enable = iqs7222_kp_events[i].enable;
-		struct fwnode_handle *event_node;
 
-		event_node = fwnode_get_named_child_node(chan_node, event_name);
+		struct fwnode_handle *event_node __free(fwnode_handle) =
+			fwnode_get_named_child_node(chan_node, event_name);
 		if (!event_node)
 			continue;
 
@@ -2408,7 +2408,6 @@  static int iqs7222_parse_chan(struct iqs7222_private *iqs7222,
 				dev_err(&client->dev,
 					"Invalid %s press timeout: %u\n",
 					fwnode_get_name(event_node), val);
-				fwnode_handle_put(event_node);
 				return -EINVAL;
 			}
 
@@ -2418,7 +2417,6 @@  static int iqs7222_parse_chan(struct iqs7222_private *iqs7222,
 			dev_err(&client->dev,
 				"Failed to read %s press timeout: %d\n",
 				fwnode_get_name(event_node), error);
-			fwnode_handle_put(event_node);
 			return error;
 		}
 
@@ -2429,7 +2427,6 @@  static int iqs7222_parse_chan(struct iqs7222_private *iqs7222,
 					    dev_desc->touch_link - (i ? 0 : 2),
 					    &iqs7222->kp_type[chan_index][i],
 					    &iqs7222->kp_code[chan_index][i]);
-		fwnode_handle_put(event_node);
 		if (error)
 			return error;
 
@@ -2604,10 +2601,10 @@  static int iqs7222_parse_sldr(struct iqs7222_private *iqs7222,
 
 	for (i = 0; i < ARRAY_SIZE(iqs7222_sl_events); i++) {
 		const char *event_name = iqs7222_sl_events[i].name;
-		struct fwnode_handle *event_node;
 		enum iqs7222_reg_key_id reg_key;
 
-		event_node = fwnode_get_named_child_node(sldr_node, event_name);
+		struct fwnode_handle *event_node __free(fwnode_handle) =
+			fwnode_get_named_child_node(sldr_node, event_name);
 		if (!event_node)
 			continue;
 
@@ -2639,7 +2636,6 @@  static int iqs7222_parse_sldr(struct iqs7222_private *iqs7222,
 					      : sldr_setup[4 + reg_offset],
 					    NULL,
 					    &iqs7222->sl_code[sldr_index][i]);
-		fwnode_handle_put(event_node);
 		if (error)
 			return error;
 
@@ -2742,9 +2738,9 @@  static int iqs7222_parse_tpad(struct iqs7222_private *iqs7222,
 
 	for (i = 0; i < ARRAY_SIZE(iqs7222_tp_events); i++) {
 		const char *event_name = iqs7222_tp_events[i].name;
-		struct fwnode_handle *event_node;
 
-		event_node = fwnode_get_named_child_node(tpad_node, event_name);
+		struct fwnode_handle *event_node __free(fwnode_handle) =
+			fwnode_get_named_child_node(tpad_node, event_name);
 		if (!event_node)
 			continue;
 
@@ -2760,7 +2756,6 @@  static int iqs7222_parse_tpad(struct iqs7222_private *iqs7222,
 					    iqs7222_tp_events[i].link, 1566,
 					    NULL,
 					    &iqs7222->tp_code[i]);
-		fwnode_handle_put(event_node);
 		if (error)
 			return error;
 
@@ -2818,9 +2813,9 @@  static int iqs7222_parse_reg_grp(struct iqs7222_private *iqs7222,
 				 int reg_grp_index)
 {
 	struct i2c_client *client = iqs7222->client;
-	struct fwnode_handle *reg_grp_node;
 	int error;
 
+	struct fwnode_handle *reg_grp_node __free(fwnode_handle) = NULL;
 	if (iqs7222_reg_grp_names[reg_grp]) {
 		char reg_grp_name[16];
 
@@ -2838,14 +2833,17 @@  static int iqs7222_parse_reg_grp(struct iqs7222_private *iqs7222,
 
 	error = iqs7222_parse_props(iqs7222, reg_grp_node, reg_grp_index,
 				    reg_grp, IQS7222_REG_KEY_NONE);
+	if (error)
+		return error;
 
-	if (!error && iqs7222_parse_extra[reg_grp])
+	if (iqs7222_parse_extra[reg_grp]) {
 		error = iqs7222_parse_extra[reg_grp](iqs7222, reg_grp_node,
 						     reg_grp_index);
+		if (error)
+			return error;
+	}
 
-	fwnode_handle_put(reg_grp_node);
-
-	return error;
+	return 0;
 }
 
 static int iqs7222_parse_all(struct iqs7222_private *iqs7222)