linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] iio/accel/stk8312: Improve unlocking of a mutex in two functions
@ 2017-10-25 18:55 SF Markus Elfring
  2017-10-26 16:09 ` Jonathan Cameron
  0 siblings, 1 reply; 5+ messages in thread
From: SF Markus Elfring @ 2017-10-25 18:55 UTC (permalink / raw)
  To: linux-iio, Hans de Goede, Hartmut Knaack, Jonathan Cameron,
	Lars-Peter Clausen, Peter Meerwald-Stadler
  Cc: LKML, kernel-janitors

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Wed, 25 Oct 2017 20:46:18 +0200

* Adjust jump targets so that a call of the function "mutex_unlock"
  is mostly stored at the end of these function implementations.

* Replace four calls by goto statements.

* Adjust condition checks.

This issue was detected by using the Coccinelle software.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 drivers/iio/accel/stk8312.c | 35 ++++++++++++++++++-----------------
 1 file changed, 18 insertions(+), 17 deletions(-)

diff --git a/drivers/iio/accel/stk8312.c b/drivers/iio/accel/stk8312.c
index cacc0da2f874..5275ab886e39 100644
--- a/drivers/iio/accel/stk8312.c
+++ b/drivers/iio/accel/stk8312.c
@@ -344,24 +344,24 @@ static int stk8312_read_raw(struct iio_dev *indio_dev,
 			return -EBUSY;
 		mutex_lock(&data->lock);
 		ret = stk8312_set_mode(data, data->mode | STK8312_MODE_ACTIVE);
-		if (ret < 0) {
-			mutex_unlock(&data->lock);
-			return ret;
-		}
+		if (ret)
+			goto unlock;
+
 		ret = stk8312_read_accel(data, chan->address);
 		if (ret < 0) {
 			stk8312_set_mode(data,
 					 data->mode & (~STK8312_MODE_ACTIVE));
-			mutex_unlock(&data->lock);
-			return ret;
+			goto unlock;
 		}
 		*val = sign_extend32(ret, 7);
 		ret = stk8312_set_mode(data,
 				       data->mode & (~STK8312_MODE_ACTIVE));
+unlock:
 		mutex_unlock(&data->lock);
-		if (ret < 0)
-			return ret;
-		return IIO_VAL_INT;
+		if (!ret)
+			ret = IIO_VAL_INT;
+
+		return ret;
 	case IIO_CHAN_INFO_SCALE:
 		*val = stk8312_scale_table[data->range - 1][0];
 		*val2 = stk8312_scale_table[data->range - 1][1];
@@ -444,17 +444,15 @@ static irqreturn_t stk8312_trigger_handler(int irq, void *p)
 						    data->buffer);
 		if (ret < STK8312_ALL_CHANNEL_SIZE) {
 			dev_err(&data->client->dev, "register read failed\n");
-			mutex_unlock(&data->lock);
-			goto err;
+			goto unlock_after_failure;
 		}
 	} else {
 		for_each_set_bit(bit, indio_dev->active_scan_mask,
 				 indio_dev->masklength) {
 			ret = stk8312_read_accel(data, bit);
-			if (ret < 0) {
-				mutex_unlock(&data->lock);
-				goto err;
-			}
+			if (ret < 0)
+				goto unlock_after_failure;
+
 			data->buffer[i++] = ret;
 		}
 	}
@@ -462,10 +460,13 @@ static irqreturn_t stk8312_trigger_handler(int irq, void *p)
 
 	iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
 					   pf->timestamp);
-err:
+notify_trigger:
 	iio_trigger_notify_done(indio_dev->trig);
-
 	return IRQ_HANDLED;
+
+unlock_after_failure:
+	mutex_unlock(&data->lock);
+	goto notify_trigger;
 }
 
 static irqreturn_t stk8312_data_rdy_trig_poll(int irq, void *private)
-- 
2.14.3

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

* Re: [PATCH] iio/accel/stk8312: Improve unlocking of a mutex in two functions
  2017-10-25 18:55 [PATCH] iio/accel/stk8312: Improve unlocking of a mutex in two functions SF Markus Elfring
@ 2017-10-26 16:09 ` Jonathan Cameron
  2017-10-27 10:15   ` SF Markus Elfring
  0 siblings, 1 reply; 5+ messages in thread
From: Jonathan Cameron @ 2017-10-26 16:09 UTC (permalink / raw)
  To: SF Markus Elfring
  Cc: linux-iio, Hans de Goede, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, LKML, kernel-janitors

On Wed, 25 Oct 2017 20:55:23 +0200
SF Markus Elfring <elfring@users.sourceforge.net> wrote:

> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Wed, 25 Oct 2017 20:46:18 +0200
> 
> * Adjust jump targets so that a call of the function "mutex_unlock"
>   is mostly stored at the end of these function implementations.
> 
> * Replace four calls by goto statements.
> 
> * Adjust condition checks.
> 
> This issue was detected by using the Coccinelle software.
> 
> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>

Hi Markus,

Thanks for this.  One small change would make the first case
easier to read in my mind.   We are already fairly deeply
indented here and having labels within switch blocks can be
difficult to read.

As such I would factor out this block as a separate read function.
That will make it a little more readable.

In the second case, the jump backwards just makes the code harder
to read than it currently is.

There is no firm rule about error handling in one place. If it
leads to more complex flow as here, don't do it.

Jonathan

> ---
>  drivers/iio/accel/stk8312.c | 35 ++++++++++++++++++-----------------
>  1 file changed, 18 insertions(+), 17 deletions(-)
> 
> diff --git a/drivers/iio/accel/stk8312.c b/drivers/iio/accel/stk8312.c
> index cacc0da2f874..5275ab886e39 100644
> --- a/drivers/iio/accel/stk8312.c
> +++ b/drivers/iio/accel/stk8312.c
> @@ -344,24 +344,24 @@ static int stk8312_read_raw(struct iio_dev *indio_dev,
>  			return -EBUSY;
>  		mutex_lock(&data->lock);
>  		ret = stk8312_set_mode(data, data->mode | STK8312_MODE_ACTIVE);
> -		if (ret < 0) {
> -			mutex_unlock(&data->lock);
> -			return ret;
> -		}
> +		if (ret)
> +			goto unlock;
> +
>  		ret = stk8312_read_accel(data, chan->address);
>  		if (ret < 0) {
>  			stk8312_set_mode(data,
>  					 data->mode & (~STK8312_MODE_ACTIVE));

Hmm. I'd like to factor out the set mode as well, but then we risk eating the
first error - so I suppose this is the best we can do.
> -			mutex_unlock(&data->lock);
> -			return ret;
> +			goto unlock;
>  		}
>  		*val = sign_extend32(ret, 7);
>  		ret = stk8312_set_mode(data,
>  				       data->mode & (~STK8312_MODE_ACTIVE));
> +unlock:
>  		mutex_unlock(&data->lock);
> -		if (ret < 0)
> -			return ret;
> -		return IIO_VAL_INT;
> +		if (!ret)
> +			ret = IIO_VAL_INT;

> +
> +		return ret;
>  	case IIO_CHAN_INFO_SCALE:
>  		*val = stk8312_scale_table[data->range - 1][0];
>  		*val2 = stk8312_scale_table[data->range - 1][1];
> @@ -444,17 +444,15 @@ static irqreturn_t stk8312_trigger_handler(int irq, void *p)
>  						    data->buffer);
>  		if (ret < STK8312_ALL_CHANNEL_SIZE) {
>  			dev_err(&data->client->dev, "register read failed\n");
> -			mutex_unlock(&data->lock);
> -			goto err;
> +			goto unlock_after_failure;
>  		}
>  	} else {
>  		for_each_set_bit(bit, indio_dev->active_scan_mask,
>  				 indio_dev->masklength) {
>  			ret = stk8312_read_accel(data, bit);
> -			if (ret < 0) {
> -				mutex_unlock(&data->lock);
> -				goto err;
> -			}
> +			if (ret < 0)
> +				goto unlock_after_failure;
> +
>  			data->buffer[i++] = ret;
>  		}
>  	}
> @@ -462,10 +460,13 @@ static irqreturn_t stk8312_trigger_handler(int irq, void *p)
>  
>  	iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
>  					   pf->timestamp);
> -err:
> +notify_trigger:
>  	iio_trigger_notify_done(indio_dev->trig);
> -
>  	return IRQ_HANDLED;
> +
> +unlock_after_failure:
> +	mutex_unlock(&data->lock);

No. This construction fails the easy to read case.  The original code
was cleaner.  The reason to bring mutex_unlocks into one place is to simplify
the code.  That isn't the case here.

> +	goto notify_trigger;
>  }
>  
>  static irqreturn_t stk8312_data_rdy_trig_poll(int irq, void *private)


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

* Re: iio/accel/stk8312: Improve unlocking of a mutex in two functions
  2017-10-26 16:09 ` Jonathan Cameron
@ 2017-10-27 10:15   ` SF Markus Elfring
  2017-10-27 11:15     ` Peter Meerwald-Stadler
  0 siblings, 1 reply; 5+ messages in thread
From: SF Markus Elfring @ 2017-10-27 10:15 UTC (permalink / raw)
  To: Jonathan Cameron, linux-iio
  Cc: Hans de Goede, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, LKML, kernel-janitors

> In the second case, the jump backwards just makes the code harder
> to read than it currently is.

Maybe …

But I proposed an other source code layout for useful reasons.


> There is no firm rule about error handling in one place.

There are some design options available.


> If it leads to more complex flow as here, don't do it.

I would appreciate to clarify such a view a bit more.
How would you like to achieve a complete and efficient
exception handling in shown places?

Regards,
Markus

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

* Re: iio/accel/stk8312: Improve unlocking of a mutex in two functions
  2017-10-27 10:15   ` SF Markus Elfring
@ 2017-10-27 11:15     ` Peter Meerwald-Stadler
  2017-10-27 12:37       ` SF Markus Elfring
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Meerwald-Stadler @ 2017-10-27 11:15 UTC (permalink / raw)
  To: SF Markus Elfring; +Cc: Jonathan Cameron, linux-iio, LKML, kernel-janitors

[-- Attachment #1: Type: text/plain, Size: 848 bytes --]

Hello,

> Maybe …
> But I proposed an other source code layout for useful reasons.

I think there is a (hidden) cost of having pure cleanup patches:
they make backporting fixes harder (across the cleanup)

stylistic changes must have a clear benefit, readability is subjective, 
consistency per se doesn't buy anything

the discussion how code should be written in the first place is separate
from the discussion what is worth fixing up lateron (IMHO)

> > There is no firm rule about error handling in one place.
> 
> There are some design options available.
> 
> 
> > If it leads to more complex flow as here, don't do it.
> 
> I would appreciate to clarify such a view a bit more.
> How would you like to achieve a complete and efficient
> exception handling in shown places?

regards, p.

-- 

Peter Meerwald-Stadler
Mobile: +43 664 24 44 418

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

* Re: iio/accel/stk8312: Improve unlocking of a mutex in two functions
  2017-10-27 11:15     ` Peter Meerwald-Stadler
@ 2017-10-27 12:37       ` SF Markus Elfring
  0 siblings, 0 replies; 5+ messages in thread
From: SF Markus Elfring @ 2017-10-27 12:37 UTC (permalink / raw)
  To: Peter Meerwald-Stadler, linux-iio; +Cc: Jonathan Cameron, LKML, kernel-janitors

>> But I proposed an other source code layout for useful reasons.
> 
> I think there is a (hidden) cost of having pure cleanup patches:
> they make backporting fixes harder (across the cleanup)

There the usual software development consequences to consider
if you dare to change the source code at all.


> stylistic changes must have a clear benefit, readability is subjective, 
> consistency per se doesn't buy anything

There are also different opinions involved.


> the discussion how code should be written in the first place is separate
> from the discussion what is worth fixing up lateron (IMHO)

Are you looking for a corresponding agreement?

Regards,
Markus

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

end of thread, other threads:[~2017-10-27 12:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-10-25 18:55 [PATCH] iio/accel/stk8312: Improve unlocking of a mutex in two functions SF Markus Elfring
2017-10-26 16:09 ` Jonathan Cameron
2017-10-27 10:15   ` SF Markus Elfring
2017-10-27 11:15     ` Peter Meerwald-Stadler
2017-10-27 12:37       ` SF Markus Elfring

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).