Linux IIO development
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: David Lechner <dlechner@baylibre.com>
Cc: "Jonathan Cameron" <Jonathan.Cameron@huawei.com>,
	linux-iio@vger.kernel.org,
	"“Luc Van Oostenryck”" <luc.vanoostenryck@gmail.com>
Subject: Re: [RFC PATCH 01/27] iio: core: Rework claim and release of direct mode to work with sparse.
Date: Sat, 11 Jan 2025 13:35:52 +0000	[thread overview]
Message-ID: <20250111133552.2a44c74e@jic23-huawei> (raw)
In-Reply-To: <c298f1bb-d0a1-42af-b237-9aa8e422ec1b@baylibre.com>

On Tue, 7 Jan 2025 10:09:15 -0600
David Lechner <dlechner@baylibre.com> wrote:

> On 1/7/25 8:24 AM, Jonathan Cameron wrote:
> > On Mon, 6 Jan 2025 17:14:12 -0600
> > David Lechner <dlechner@baylibre.com> wrote:
> >   
> >> On 1/5/25 11:25 AM, Jonathan Cameron wrote:  
> >>> From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> >>>
> >>> Initial thought was to do something similar to __cond_lock()
> >>>
> >>> 	do_iio_device_claim_direct_mode(iio_dev) ? : ({ __acquire(iio_dev); 0; })
> >>> + Appropriate static inline iio_device_release_direct_mode()
> >>>
> >>> However with that, sparse generates false positives. E.g.
> >>>
> >>> drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c:1811:17: warning: context imbalance in 'st_lsm6dsx_read_raw' - unexpected unlock    
> >>
> >> Even if false positives aren't technically wrong, if sparse is having a hard
> >> time reasoning about the code, then it is probably harder for humans to reason
> >> about the code as well. So rewriting these false positives anyway could be
> >> justified beyond just making the static analyzer happy.
> >>  
> >>>
> >>> So instead, this patch rethinks the return type and makes it more
> >>> 'conditional lock like' (which is part of what is going on under the hood
> >>> anyway) and return a boolean - true for successfully acquired, false for
> >>> did not acquire.    
> >>
> >> I think changing this function to return bool instead of int is nice change
> >> anyway since it makes writing the code less prone authors to trying to do
> >> something "clever" with the ret variable. And it also saves one one line of
> >> code.
> >>  
> >>>
> >>> To allow a migration path given the rework is now no trivial, take a leaf
> >>> out of the naming of the conditional guard we currently have for IIO
> >>> device direct mode and drop the _mode postfix from the new functions giving
> >>> iio_device_claim_direct() and iio_device_release_direct()
> >>>
> >>> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> >>> ---
> >>>  include/linux/iio/iio.h | 22 ++++++++++++++++++++++
> >>>  1 file changed, 22 insertions(+)
> >>>
> >>> diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h
> >>> index 56161e02f002..4ef2f9893421 100644
> >>> --- a/include/linux/iio/iio.h
> >>> +++ b/include/linux/iio/iio.h
> >>> @@ -662,6 +662,28 @@ int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp);
> >>>  int iio_device_claim_direct_mode(struct iio_dev *indio_dev);
> >>>  void iio_device_release_direct_mode(struct iio_dev *indio_dev);
> >>>  
> >>> +/*
> >>> + * Helper functions that allow claim and release of direct mode
> >>> + * in a fashion that doesn't generate false positives from sparse.
> >>> + */
> >>> +static inline bool iio_device_claim_direct(struct iio_dev *indio_dev) __cond_acquires(indio_dev)    
> >>
> >> Doesn't __cond_acquires depend on this patch [1] that doesn't look like it was
> >> ever picked up in sparse?
> >>
> >> [1]: https://lore.kernel.org/all/CAHk-=wjZfO9hGqJ2_hGQG3U_XzSh9_XaXze=HgPdvJbgrvASfA@mail.gmail.com/  
> > 
> > I wondered about that. It 'seems' to do the job anyway. I didn't fully
> > understand that thread so I just blindly tried it instead :)
> > 
> > This case is simpler that that thread, so maybe those acrobatics aren't
> > needed?  
> 
> I was not able to get a sparse warning without applying that patch to sparse
> first. My test method was to apply this series to my Linux tree and then
> comment out a iio_device_release_direct() line in a random driver.
> 
> And looking at the way the check works, this is exactly what I would expect.
> The negative output argument in __attribute__((context,x,0,-1)) means something
> different (check = 0) without the spare patch applied.
> 
Curious. I wasn't being remotely careful with what sparse version
i was running so just went with what Arch is carrying which turns out to be
a bit old.

Same test as you describe gives me:
  CHECK   drivers/iio/adc/ad4000.c
drivers/iio/adc/ad4000.c:533:12: warning: context imbalance in 'ad4000_read_raw' - different lock contexts for basic block

So I tried that with latest sparse from kernel.org and I still get that warning
which is what I'd expect to see.

Simple make C=1 W=1 build

I wonder what we have different?  Maybe it is missing some cases?

diff --git a/drivers/iio/adc/ad4000.c b/drivers/iio/adc/ad4000.c
index ef0acaafbcdb..6785d55ff53a 100644
--- a/drivers/iio/adc/ad4000.c
+++ b/drivers/iio/adc/ad4000.c
@@ -543,7 +543,7 @@ static int ad4000_read_raw(struct iio_dev *indio_dev,
                        return -EBUSY;
 
                ret = ad4000_single_conversion(indio_dev, chan, val);
-               iio_device_release_direct(indio_dev);
+//             iio_device_release_direct(indio_dev);
                return ret;
        case IIO_CHAN_INFO_SCALE:
                *val = st->scale_tbl[st->span_comp][0];

Was the test I ran today.

Jonathan





  reply	other threads:[~2025-01-11 13:35 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-05 17:25 [RFC PATCH 00/27] iio: improve handling of direct mode claim and release Jonathan Cameron
2025-01-05 17:25 ` [RFC PATCH 01/27] iio: core: Rework claim and release of direct mode to work with sparse Jonathan Cameron
2025-01-06 23:14   ` David Lechner
2025-01-07 14:24     ` Jonathan Cameron
2025-01-07 16:09       ` David Lechner
2025-01-11 13:35         ` Jonathan Cameron [this message]
2025-01-11 22:28           ` David Lechner
2025-01-19 18:03             ` Marcelo Schmitt
2025-01-25 11:59               ` Jonathan Cameron
2025-01-29 16:34                 ` Marcelo Schmitt
2025-01-19 19:29             ` Jonathan Cameron
2025-01-26 19:23               ` Jonathan Cameron
2025-01-05 17:25 ` [RFC PATCH 02/27] iio: chemical: scd30: Switch to sparse friendly claim/release_direct() Jonathan Cameron
2025-01-06 23:22   ` David Lechner
2025-01-05 17:25 ` [RFC PATCH 03/27] iio: temperature: tmp006: Stop using iio_device_claim_direct_scoped() Jonathan Cameron
2025-01-05 17:25 ` [RFC PATCH 04/27] iio: imu: st_lsm6dsx: Switch to sparse friendly claim/release_direct() Jonathan Cameron
2025-01-05 17:25 ` [RFC PATCH 05/27] iio: proximity: sx9310: Stop using iio_device_claim_direct_scoped() Jonathan Cameron
2025-01-05 17:25 ` [RFC PATCH 06/27] iio: proximity: sx9324: " Jonathan Cameron
2025-01-05 17:25 ` [RFC PATCH 07/27] iio: proximity: sx9360: " Jonathan Cameron
2025-01-05 17:25 ` [RFC PATCH 08/27] iio: accel: adxl367: " Jonathan Cameron
2025-01-05 17:25 ` [RFC PATCH 09/27] iio: adc: ad4000: " Jonathan Cameron
2025-01-06 23:19   ` David Lechner
2025-01-07 11:29   ` Marcelo Schmitt
2025-01-07 14:28     ` Jonathan Cameron
2025-01-11 13:37       ` Jonathan Cameron
2025-01-19 18:18         ` Marcelo Schmitt
2025-01-29 16:41   ` Marcelo Schmitt
2025-01-05 17:25 ` [RFC PATCH 10/27] iio: adc: ad4130: " Jonathan Cameron
2025-01-05 17:25 ` [RFC PATCH 11/27] " Jonathan Cameron
2025-01-05 17:25 ` [RFC PATCH 12/27] iio: adc: ad4695: " Jonathan Cameron
2025-01-05 17:25 ` [RFC PATCH 13/27] iio: adc: ad7606: " Jonathan Cameron
2025-01-05 17:25 ` [RFC PATCH 14/27] iio: adc: ad7625: " Jonathan Cameron
2025-01-05 17:26 ` [RFC PATCH 15/27] iio: adc: ad7779: " Jonathan Cameron
2025-01-05 17:26 ` [RFC PATCH 16/27] iio: adc: ad9467: " Jonathan Cameron
2025-01-05 17:26 ` [RFC PATCH 17/27] iio: adc: max1363: " Jonathan Cameron
2025-01-05 17:26 ` [RFC PATCH 18/27] iio: adc: rtq6056: " Jonathan Cameron
2025-01-05 17:26 ` [RFC PATCH 19/27] iio: adc: ti-adc161s626: " Jonathan Cameron
2025-01-05 17:26 ` [RFC PATCH 20/27] iio: adc: ti-ads1119: " Jonathan Cameron
2025-01-05 17:26 ` [RFC PATCH 21/27] iio: addac: ad74413r: " Jonathan Cameron
2025-01-05 17:26 ` [RFC PATCH 22/27] iio: chemical: ens160: " Jonathan Cameron
2025-01-05 17:26 ` [RFC PATCH 23/27] iio: dac: ad3552r-hs: " Jonathan Cameron
2025-01-05 17:26 ` [RFC PATCH 24/27] iio: dac: ad8460: " Jonathan Cameron
2025-01-05 17:26 ` [RFC PATCH 25/27] iio: dummy: " Jonathan Cameron
2025-01-05 17:26 ` [RFC PATCH 26/27] iio: imu: bmi323: " Jonathan Cameron
2025-01-05 17:26 ` [RFC PATCH 27/27] iio: light: bh1745: " Jonathan Cameron
2025-01-07 13:09 ` [RFC PATCH 00/27] iio: improve handling of direct mode claim and release Nuno Sá
2025-01-07 14:31   ` Jonathan Cameron
2025-01-07 16:07     ` Nuno Sá
2025-02-02 21:00 ` 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=20250111133552.2a44c74e@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=Jonathan.Cameron@huawei.com \
    --cc=dlechner@baylibre.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=luc.vanoostenryck@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