devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Dmitry Rokosov <DDRokosov@sberdevices.ru>
Cc: Jonathan Cameron <Jonathan.Cameron@Huawei.com>,
	"robh+dt@kernel.org" <robh+dt@kernel.org>,
	"stano.jakubek@gmail.com" <stano.jakubek@gmail.com>,
	"shawnguo@kernel.org" <shawnguo@kernel.org>,
	"lars@metafoo.de" <lars@metafoo.de>,
	"andy.shevchenko@gmail.com" <andy.shevchenko@gmail.com>,
	"stephan@gerhold.net" <stephan@gerhold.net>,
	"linux-iio@vger.kernel.org" <linux-iio@vger.kernel.org>,
	"devicetree@vger.kernel.org" <devicetree@vger.kernel.org>,
	kernel <kernel@sberdevices.ru>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v1 2/3] iio: add MEMSensing MSA311 3-axis accelerometer driver
Date: Sun, 22 May 2022 11:36:54 +0100	[thread overview]
Message-ID: <20220522113654.0e3c0023@jic23-huawei> (raw)
In-Reply-To: <20220518122515.aby5lbb4xusr6pdt@CAB-WSD-L081021.sigma.sbrf.ru>

On Wed, 18 May 2022 12:25:59 +0000
Dmitry Rokosov <DDRokosov@sberdevices.ru> wrote:

> Hi Jonathan,
> 
> I have two items to be discussed about iio_trigger_get().
> Please see my questions below and correct me if I'm wrong.
> 
> On Tue, Apr 26, 2022 at 08:24:10PM +0300, Dmitry Rokosov wrote:
> > > > +							       "%s-new-data",
> > > > +							       indio_dev->name);
> > > > +		if (!msa311->new_data_trig) {
> > > > +			dev_err(&i2c->dev, "cannot allocate new data trig\n");
> > > > +			err = -ENOMEM;
> > > > +			goto err_lock_destroy;
> > > > +		}
> > > > +
> > > > +		msa311->new_data_trig->dev.parent = &i2c->dev;
> > > > +		msa311->new_data_trig->ops = &msa311_new_data_trig_ops;
> > > > +		iio_trigger_set_drvdata(msa311->new_data_trig, indio_dev);
> > > > +		indio_dev->trig = msa311->new_data_trig;  
> > > 
> > > This will create a double free if you were to change the trigger.
> > > 		indio_dev->trig = iio_trigger_get(trig);
> > >   
> > 
> > I didn't take into account other trigger usage.
> > I'll rework this place for the v2.
> >   
> 
> The first one problem is module_get() calling for trigger get()
> semantic.
> I've applied iio_trigger_get() function to acquire module refcnt,
> but I've faced with rmmod busy problem. IIO driver module doesn't want to
> stop and unload due to not having zero module refcnt.

One option is to remove the trigger from sysfs - write an empty string
current_trigger, but you are right this is a bit of a mess.

Probably the best option is just don't assign the trigger automatically at all.

This was what we almost always went with in the past.  If a driver
supports multiple triggers (and if it doesn't why expose the trigger at allm
there is no obligation to do so?)
then it's a policy decision to associate a trigger in the first place
so shouldn't really happen in kernel.

There is a corner case for drivers which can only use a particular trigger,
but that trigger can be more generally used (validate_trigger provided, but
not validate_device).  Another corner case is drivers that didn't expose
a trigger, but later gain support for other triggers then we need to set
the default value.


> Syscall delete_module() tries to stop module first and after calls
> driver exit() function (which executes devm_* handlers inside, including IIO
> trigger unregister). It means we have the chicken or the egg dilemma here.
> Module can't be unloaded until module refcnt is not zero and we can't
> execute IIO trigger unregister (decrease module refcnt) only when module
> refcnt is zero.
> I suppose the possible solution to such a problem is a different semantic
> for internal triggers (inside driver itself) and external drivers (like
> hwtimer trigger). What do you think?

Potentially though it's going to be tricky as a driver doesn't generally
have any way to know they are internal and we need to be careful not to
underflow the reference counts.  We could hid a flag somewhere and
add an iio_trigger_get_same_owner() or something that sets that flag allowing
us to decide not to drop the reference count it if is automatically unassociated.
In the path where you get:
1) iio_trigger_get_same_owner() on probe
2) sysfs write changes to another trigger.
3) sysfs write back to original trigger
it is reasonable to assume the need to clear the trigger
before driver removal is possible, whereas clearing the trigger association
if only step 1 happened is no intuitive.

> 
> The second one issue is located in the different IIO drivers. Some modules
> call iio_trigger_get() before iio_trigger_register(), trig->owner is not
> initialized to the right value (THIS_MODULE) and we don't acquire refcnt
> for proper driver object.

Ah. Good point. I guess we missed that when we were moving over to
automated setting of the module.

> I'm going to send patchset to problem driver set, but I can test only
> buildable status for such modules, are you okay with that?
That should be fine.  I can't immediately think of a case where it would
be a problem as the iio_device_register() should be later and until that happens
nothing can turn on the trigger - so there shouldn't be any other races.

Jonathan

> 


  reply	other threads:[~2022-05-22 10:28 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-19 15:45 [PATCH v1 0/3] iio: accel: add MSA311 accelerometer driver Rokosov Dmitry Dmitrievich
2022-04-19 15:45 ` [PATCH v1 1/3] dt-bindings: vendor-prefixes: add MEMSensing Microsystems Co., Ltd Rokosov Dmitry Dmitrievich
2022-04-26 20:28   ` Rob Herring
2022-04-27  9:01     ` Dmitry Rokosov
2022-04-19 15:45 ` [PATCH v1 2/3] iio: add MEMSensing MSA311 3-axis accelerometer driver Rokosov Dmitry Dmitrievich
2022-04-20 10:50   ` Jonathan Cameron
2022-04-26 17:23     ` Dmitry Rokosov
2022-04-28 19:41       ` Jonathan Cameron
2022-05-18 12:25       ` Dmitry Rokosov
2022-05-22 10:36         ` Jonathan Cameron [this message]
2022-05-25 17:53           ` Dmitry Rokosov
2022-04-20 10:54   ` Jonathan Cameron
2022-04-21 14:07     ` Dmitry Rokosov
2022-04-19 15:45 ` [PATCH v1 3/3] dt-bindings: iio: accel: add dt-binding schema for msa311 accel driver Rokosov Dmitry Dmitrievich
2022-04-20  8:51   ` Jonathan Cameron
2022-04-21 13:55     ` Dmitry Rokosov
2022-04-26 20:31   ` Rob Herring
2022-04-27  8:58     ` Dmitry Rokosov
2022-05-04 18:36     ` Dmitry Rokosov
2022-05-04 20:48       ` Jonathan Cameron

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=20220522113654.0e3c0023@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=DDRokosov@sberdevices.ru \
    --cc=Jonathan.Cameron@Huawei.com \
    --cc=andy.shevchenko@gmail.com \
    --cc=devicetree@vger.kernel.org \
    --cc=kernel@sberdevices.ru \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=robh+dt@kernel.org \
    --cc=shawnguo@kernel.org \
    --cc=stano.jakubek@gmail.com \
    --cc=stephan@gerhold.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).