From patchwork Wed Mar 14 15:15:32 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SF Markus Elfring X-Patchwork-Id: 10282429 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id A437D602BD for ; Wed, 14 Mar 2018 15:16:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8FBFD28AE2 for ; Wed, 14 Mar 2018 15:16:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8D95228A86; Wed, 14 Mar 2018 15:16:05 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 41B3E28AEB for ; Wed, 14 Mar 2018 15:16:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751402AbeCNPQD (ORCPT ); Wed, 14 Mar 2018 11:16:03 -0400 Received: from mout.web.de ([212.227.15.3]:52341 "EHLO mout.web.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750910AbeCNPQC (ORCPT ); Wed, 14 Mar 2018 11:16:02 -0400 Received: from [192.168.1.2] ([85.182.40.79]) by smtp.web.de (mrweb001 [213.165.67.108]) with ESMTPSA (Nemesis) id 0MRD4x-1f486G3Xrj-00UdTp; Wed, 14 Mar 2018 16:15:40 +0100 To: linux-iio@vger.kernel.org, Greg Kroah-Hartman , Hartmut Knaack , Jonathan Cameron , Lars-Peter Clausen , Peter Meerwald-Stadler , Pravin Shedge , Quentin Schulz Cc: LKML , kernel-janitors@vger.kernel.org From: SF Markus Elfring Subject: [PATCH] iio/gyro/bmg160_core: Improve unlocking of a mutex in five functions Message-ID: <16623de4-351d-135b-f3ff-701a465c5d92@users.sourceforge.net> Date: Wed, 14 Mar 2018 16:15:32 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 Content-Language: en-GB X-Provags-ID: V03:K0:5LIAtmnAtFMvbpgELhONepqMHFPO8p5Ana8ptHimgqIb9Km9zGG a7hddbXKhKZE3FTuOpzdm/8yvUGuxL+F1uwFB58F5NIfIH9udwFWtQPxVYzooZfbwsjvKQw FBDqzWzWphUZJ/e3EzQK9e4Sghk5iPl0XCjPmOrAW1hkqiqFoxLI9CfuS/Zfo42XPZIAKaB a0eMpiIMhAAQkYyCwnjgQ== X-UI-Out-Filterresults: notjunk:1; V01:K0:AVFQclTeDTc=:mQ7/4lJ4pLYikrEwJIPOQZ FzvDHRJ2h9NP1H9nt9GUS5W+aSlGWJjnkI/O6vw2g2pR7LkgGjs74Bw8CoN6NsVbBpmSzqyeU lW8bVENOTjmi5fncZHxQA6wrfvvr//b0nGiHYegb4J1/rNsNVENA9TaBut2VJm8Rbjgi73QyO pPeA7+g5mrDhTDRiOFmuzUt+Ikm0nqAcm+00hHzomrzrvLUXvWe1WB53n8Lc6KDJrqHBWfBNi g/oCdO046Q7cbqNsBkYZB99rQevCZLN5dup1XgHs0HzEfvOJMotPiDUNsVLzcQslQu9EguurJ /FcQmd/6HWs09q5vQi0hPFcmBp6b9ImBvceOeZyv12mwYkNVN3JhJyGeujanv8PZK+y+ZtMkq BeFBG/tMMqaUH9bTS7a1Q5uYTAaQyS6E5BM2j2+ejJ4lDOaxBjks0Lrf+V6mL9Rat+xAaVvvY +Z2Gxb99F9SdzKFiaI06+v1jynGjoDuDQ6USGXrVljS3zB7G3j1YcE1/oY2qMk5JY8xsE6n6O FUMikfOoT5KOul7BU7EO8BKFZ28MuASgoj7DxY2Flp/BecZueG9y1iPoCv7uP0yFLGRGFqY3r iiGyKLpH/7oK8iDri/x2pREJiK7LPOveRj2YrmEqP+qW8FYpNcsHg2ksq3FftVY7N5takkmgx JLaM7uUgW4GkkrQnIRLSeoEikakH09A9samp7hw2Vatn9in6QyGDFd9sjkq/NrnYn91LTCiqh weF7t0N2jwBEiZnuGBEcMlv5o1AUDeGc8vypvhgrXNer65E+ttSy6tpbr/SPGzqcEa0v7xPv7 NJTW/XWnR3I8SrzENZDR8WYhemhbt85+kQtpVypH8w2MZI1ivQ= Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Markus Elfring Date: Wed, 14 Mar 2018 16:06:49 +0100 * Add jump targets so that a call of the function "mutex_unlock" is stored only once in these function implementations. * Replace 19 calls by goto statements. This issue was detected by using the Coccinelle software. Signed-off-by: Markus Elfring --- drivers/iio/gyro/bmg160_core.c | 103 ++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 58 deletions(-) diff --git a/drivers/iio/gyro/bmg160_core.c b/drivers/iio/gyro/bmg160_core.c index 63ca31628a93..fa367fd7bc8c 100644 --- a/drivers/iio/gyro/bmg160_core.c +++ b/drivers/iio/gyro/bmg160_core.c @@ -499,21 +499,19 @@ static int bmg160_get_temp(struct bmg160_data *data, int *val) mutex_lock(&data->mutex); ret = bmg160_set_power_state(data, true); - if (ret < 0) { - mutex_unlock(&data->mutex); - return ret; - } + if (ret < 0) + goto unlock; ret = regmap_read(data->regmap, BMG160_REG_TEMP, &raw_val); if (ret < 0) { dev_err(dev, "Error reading reg_temp\n"); bmg160_set_power_state(data, false); - mutex_unlock(&data->mutex); - return ret; + goto unlock; } *val = sign_extend32(raw_val, 7); ret = bmg160_set_power_state(data, false); +unlock: mutex_unlock(&data->mutex); if (ret < 0) return ret; @@ -529,22 +527,20 @@ static int bmg160_get_axis(struct bmg160_data *data, int axis, int *val) mutex_lock(&data->mutex); ret = bmg160_set_power_state(data, true); - if (ret < 0) { - mutex_unlock(&data->mutex); - return ret; - } + if (ret < 0) + goto unlock; ret = regmap_bulk_read(data->regmap, BMG160_AXIS_TO_REG(axis), &raw_val, sizeof(raw_val)); if (ret < 0) { dev_err(dev, "Error reading axis %d\n", axis); bmg160_set_power_state(data, false); - mutex_unlock(&data->mutex); - return ret; + goto unlock; } *val = sign_extend32(le16_to_cpu(raw_val), 15); ret = bmg160_set_power_state(data, false); +unlock: mutex_unlock(&data->mutex); if (ret < 0) return ret; @@ -632,19 +628,16 @@ static int bmg160_write_raw(struct iio_dev *indio_dev, * mode to power on for other writes. */ ret = bmg160_set_power_state(data, true); - if (ret < 0) { - mutex_unlock(&data->mutex); - return ret; - } + if (ret < 0) + goto unlock; + ret = bmg160_set_bw(data, val); if (ret < 0) { bmg160_set_power_state(data, false); - mutex_unlock(&data->mutex); - return ret; + goto unlock; } - ret = bmg160_set_power_state(data, false); - mutex_unlock(&data->mutex); - return ret; + + goto set_power_state; case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: if (val2) return -EINVAL; @@ -653,18 +646,15 @@ static int bmg160_write_raw(struct iio_dev *indio_dev, ret = bmg160_set_power_state(data, true); if (ret < 0) { bmg160_set_power_state(data, false); - mutex_unlock(&data->mutex); - return ret; + goto unlock; } ret = bmg160_set_filter(data, val); if (ret < 0) { bmg160_set_power_state(data, false); - mutex_unlock(&data->mutex); - return ret; + goto unlock; } - ret = bmg160_set_power_state(data, false); - mutex_unlock(&data->mutex); - return ret; + + goto set_power_state; case IIO_CHAN_INFO_SCALE: if (val) return -EINVAL; @@ -672,24 +662,27 @@ static int bmg160_write_raw(struct iio_dev *indio_dev, mutex_lock(&data->mutex); /* Refer to comments above for the suspend mode ops */ ret = bmg160_set_power_state(data, true); - if (ret < 0) { - mutex_unlock(&data->mutex); - return ret; - } + if (ret < 0) + goto unlock; + ret = bmg160_set_scale(data, val2); if (ret < 0) { bmg160_set_power_state(data, false); - mutex_unlock(&data->mutex); - return ret; + goto unlock; } - ret = bmg160_set_power_state(data, false); - mutex_unlock(&data->mutex); - return ret; + + goto set_power_state; default: return -EINVAL; } return -EINVAL; + +set_power_state: + ret = bmg160_set_power_state(data, false); +unlock: + mutex_unlock(&data->mutex); + return ret; } static int bmg160_read_event(struct iio_dev *indio_dev, @@ -763,8 +756,8 @@ static int bmg160_write_event_config(struct iio_dev *indio_dev, if (!state && data->motion_trigger_on) { data->ev_enable_state = 0; - mutex_unlock(&data->mutex); - return 0; + ret = 0; + goto unlock; } /* * We will expect the enable and disable to do operation in @@ -776,22 +769,19 @@ static int bmg160_write_event_config(struct iio_dev *indio_dev, * is always on so sequence doesn't matter */ ret = bmg160_set_power_state(data, state); - if (ret < 0) { - mutex_unlock(&data->mutex); - return ret; - } + if (ret < 0) + goto unlock; ret = bmg160_setup_any_motion_interrupt(data, state); if (ret < 0) { bmg160_set_power_state(data, false); - mutex_unlock(&data->mutex); - return ret; + goto unlock; } data->ev_enable_state = state; +unlock: mutex_unlock(&data->mutex); - - return 0; + return ret; } static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("100 200 400 1000 2000"); @@ -919,8 +909,8 @@ static int bmg160_data_rdy_trigger_set_state(struct iio_trigger *trig, if (!state && data->ev_enable_state && data->motion_trigger_on) { data->motion_trigger_on = false; - mutex_unlock(&data->mutex); - return 0; + ret = 0; + goto unlock; } /* @@ -928,27 +918,24 @@ static int bmg160_data_rdy_trigger_set_state(struct iio_trigger *trig, * enable/disable operation order */ ret = bmg160_set_power_state(data, state); - if (ret < 0) { - mutex_unlock(&data->mutex); - return ret; - } + if (ret < 0) + goto unlock; + if (data->motion_trig == trig) ret = bmg160_setup_any_motion_interrupt(data, state); else ret = bmg160_setup_new_data_interrupt(data, state); if (ret < 0) { bmg160_set_power_state(data, false); - mutex_unlock(&data->mutex); - return ret; + goto unlock; } if (data->motion_trig == trig) data->motion_trigger_on = state; else data->dready_trigger_on = state; - +unlock: mutex_unlock(&data->mutex); - - return 0; + return ret; } static const struct iio_trigger_ops bmg160_trigger_ops = {