linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: SF Markus Elfring <elfring@users.sourceforge.net>
To: linux-iio@vger.kernel.org, Hans de Goede <hdegoede@redhat.com>,
	Hartmut Knaack <knaack.h@gmx.de>,
	Jonathan Cameron <jic23@kernel.org>,
	Lars-Peter Clausen <lars@metafoo.de>,
	Peter Meerwald-Stadler <pmeerw@pmeerw.net>
Cc: LKML <linux-kernel@vger.kernel.org>, kernel-janitors@vger.kernel.org
Subject: [PATCH] iio/accel/stk8312: Improve unlocking of a mutex in two functions
Date: Wed, 25 Oct 2017 20:55:23 +0200	[thread overview]
Message-ID: <ae9d7e7f-541e-ce0c-1d41-6b9793c98f78@users.sourceforge.net> (raw)

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

             reply	other threads:[~2017-10-25 18:55 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-25 18:55 SF Markus Elfring [this message]
2017-10-26 16:09 ` [PATCH] iio/accel/stk8312: Improve unlocking of a mutex in two functions 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ae9d7e7f-541e-ce0c-1d41-6b9793c98f78@users.sourceforge.net \
    --to=elfring@users.sourceforge.net \
    --cc=hdegoede@redhat.com \
    --cc=jic23@kernel.org \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=knaack.h@gmx.de \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pmeerw@pmeerw.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).