From: Jonathan Cameron <jic23@kernel.org>
To: "H. Nikolaus Schaller" <hns@goldelico.com>
Cc: Linus Walleij <linus.walleij@linaro.org>,
Rob Herring <robh+dt@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
Charles Keepax <ckeepax@opensource.cirrus.com>,
Song Qiang <songqiang1304521@gmail.com>,
letux-kernel@openphoenux.org, Hartmut Knaack <knaack.h@gmx.de>,
Lars-Peter Clausen <lars@metafoo.de>,
Peter Meerwald-Stadler <pmeerw@pmeerw.net>,
linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH 5/9] iio: accel: bma180: convert to devm
Date: Wed, 20 Feb 2019 16:18:28 +0000 [thread overview]
Message-ID: <20190220161828.0c6fcd8e@archlinux> (raw)
In-Reply-To: <900f59b94ef2e1b5559b363c0d1af4fefccd0366.1550671256.git.hns@goldelico.com>
On Wed, 20 Feb 2019 15:00:52 +0100
"H. Nikolaus Schaller" <hns@goldelico.com> wrote:
> This simplifies the code a little.
It does, but at the cost of introducing potential race conditions.
Please don't do this. See below for why and a suggestion on how
to resolve things if you want to make this change safely.
Jonathan
>
> Signed-off-by: H. Nikolaus Schaller <hns@goldelico.com>
> ---
> drivers/iio/accel/bma180.c | 56 ++++++++++++++------------------------
> 1 file changed, 21 insertions(+), 35 deletions(-)
>
> diff --git a/drivers/iio/accel/bma180.c b/drivers/iio/accel/bma180.c
> index 248be67e4f41..3f5ee2d495d3 100644
> --- a/drivers/iio/accel/bma180.c
> +++ b/drivers/iio/accel/bma180.c
> @@ -738,7 +738,7 @@ static int bma180_probe(struct i2c_client *client,
>
> ret = data->part_info->chip_config(data);
> if (ret < 0)
> - goto err_chip_disable;
> + goto err;
>
> mutex_init(&data->mutex);
> indio_dev->dev.parent = &client->dev;
> @@ -748,12 +748,25 @@ static int bma180_probe(struct i2c_client *client,
> indio_dev->modes = INDIO_DIRECT_MODE;
> indio_dev->info = &bma180_info;
>
> + ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL,
> + bma180_trigger_handler, NULL);
> + if (ret < 0) {
> + dev_err(&client->dev, "unable to setup iio triggered buffer\n");
> + goto err;
> + }
> +
> + ret = devm_iio_device_register(&client->dev, indio_dev);
> + if (ret < 0) {
> + dev_err(&client->dev, "unable to register iio device\n");
> + goto err;
> + }
> +
> if (client->irq > 0) {
> - data->trig = iio_trigger_alloc("%s-dev%d", indio_dev->name,
> - indio_dev->id);
> + data->trig = devm_iio_trigger_alloc(&client->dev, "%s-dev%d",
> + indio_dev->name, indio_dev->id);
> if (!data->trig) {
> ret = -ENOMEM;
> - goto err_chip_disable;
> + goto err;
> }
>
> ret = devm_request_irq(&client->dev, client->irq,
> @@ -761,7 +774,7 @@ static int bma180_probe(struct i2c_client *client,
> "bma180_event", data->trig);
> if (ret) {
> dev_err(&client->dev, "unable to request IRQ\n");
> - goto err_trigger_free;
> + goto err;
> }
>
> data->trig->dev.parent = &client->dev;
> @@ -769,34 +782,14 @@ static int bma180_probe(struct i2c_client *client,
> iio_trigger_set_drvdata(data->trig, indio_dev);
> indio_dev->trig = iio_trigger_get(data->trig);
>
> - ret = iio_trigger_register(data->trig);
> + ret = devm_iio_trigger_register(&client->dev, data->trig);
> if (ret)
> - goto err_trigger_free;
> - }
> -
> - ret = iio_triggered_buffer_setup(indio_dev, NULL,
> - bma180_trigger_handler, NULL);
> - if (ret < 0) {
> - dev_err(&client->dev, "unable to setup iio triggered buffer\n");
> - goto err_trigger_unregister;
> - }
> -
> - ret = iio_device_register(indio_dev);
> - if (ret < 0) {
> - dev_err(&client->dev, "unable to register iio device\n");
> - goto err_buffer_cleanup;
> + goto err;
> }
>
> return 0;
>
> -err_buffer_cleanup:
> - iio_triggered_buffer_cleanup(indio_dev);
> -err_trigger_unregister:
> - if (data->trig)
> - iio_trigger_unregister(data->trig);
> -err_trigger_free:
> - iio_trigger_free(data->trig);
> -err_chip_disable:
> +err:
> data->part_info->chip_disable(data);
>
> return ret;
> @@ -807,13 +800,6 @@ static int bma180_remove(struct i2c_client *client)
> struct iio_dev *indio_dev = i2c_get_clientdata(client);
> struct bma180_data *data = iio_priv(indio_dev);
>
> - iio_device_unregister(indio_dev);
> - iio_triggered_buffer_cleanup(indio_dev);
> - if (data->trig) {
> - iio_trigger_unregister(data->trig);
> - iio_trigger_free(data->trig);
> - }
> -
Now we disable the device before removing it's userspace interface.
Not a good idea.
Generally I will insist on the remove order always being the precise
opposite of probe simply because it is obviously correct.
That includes cases which can be simply argued to be fine (not
this one which isn't). The reason is readability trumps saving
a few lines of code and it's a lot easier to check a probe and
remove function against each other if the order is maintained.
Of course, there are ways to do this by making all the unwind
managed, but you haven't done this here.
devm_add_action_or_reset is handy for this if you want to do it.
> mutex_lock(&data->mutex);
> data->part_info->chip_disable(data);
> mutex_unlock(&data->mutex);
next prev parent reply other threads:[~2019-02-20 16:18 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-02-20 14:00 [PATCH 0/9] iio mount matrix - revitalize missing bindings documentation and provide code for bmc150, bmg160, bma180, itg3200, hmc584x H. Nikolaus Schaller
2019-02-20 14:00 ` [PATCH 1/9] iio: document bindings for mounting matrices H. Nikolaus Schaller
2019-02-20 16:10 ` Jonathan Cameron
2019-02-20 16:18 ` H. Nikolaus Schaller
2019-02-20 21:34 ` Linus Walleij
2019-02-20 14:00 ` [PATCH 2/9] iio: bindings: clarifications for mount-matrix bindings H. Nikolaus Schaller
2019-02-20 14:00 ` [PATCH 3/9] iio: accel: bmc150: add mount matrix support H. Nikolaus Schaller
2019-02-20 16:07 ` Andy Shevchenko
2019-02-20 16:14 ` H. Nikolaus Schaller
2019-02-20 16:14 ` Jonathan Cameron
2019-02-20 16:20 ` H. Nikolaus Schaller
2019-02-20 17:09 ` Jonathan Cameron
2019-02-20 14:00 ` [PATCH 4/9] iio: accel: bma180: " H. Nikolaus Schaller
2019-02-20 14:00 ` [PATCH 5/9] iio: accel: bma180: convert to devm H. Nikolaus Schaller
2019-02-20 16:09 ` Andy Shevchenko
2019-02-20 16:15 ` H. Nikolaus Schaller
2019-02-20 16:18 ` Jonathan Cameron [this message]
2019-02-20 16:23 ` H. Nikolaus Schaller
2019-02-20 14:00 ` [PATCH 6/9] iio: gyro: bmg160: add mount matrix support H. Nikolaus Schaller
2019-02-20 14:00 ` [PATCH 7/9] iio: gyro: itg3200: " H. Nikolaus Schaller
2019-02-20 14:00 ` [PATCH 8/9] iio: magnetometer: bmc150: " H. Nikolaus Schaller
2019-02-20 14:00 ` [PATCH 9/9] iio: magnetometer: hmc5843: " H. Nikolaus Schaller
2019-02-20 16:19 ` Jonathan Cameron
2019-02-20 16:24 ` H. Nikolaus Schaller
2019-02-20 16:13 ` [PATCH 0/9] iio mount matrix - revitalize missing bindings documentation and provide code for bmc150, bmg160, bma180, itg3200, hmc584x Andy Shevchenko
2019-02-20 16:26 ` H. Nikolaus Schaller
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=20190220161828.0c6fcd8e@archlinux \
--to=jic23@kernel.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=ckeepax@opensource.cirrus.com \
--cc=devicetree@vger.kernel.org \
--cc=hns@goldelico.com \
--cc=knaack.h@gmx.de \
--cc=lars@metafoo.de \
--cc=letux-kernel@openphoenux.org \
--cc=linus.walleij@linaro.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=pmeerw@pmeerw.net \
--cc=robh+dt@kernel.org \
--cc=songqiang1304521@gmail.com \
/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).