mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Arnav Kapoor <kapoorarnav43@gmail.com>
Cc: "David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	"Andy Shevchenko" <andriy.shevchenko@intel.com>,
	"Gwendal Grignou" <gwendal@chromium.org>,
	"Stephen Boyd" <swboyd@chromium.org>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/2] iio: proximity: sx9310: reject unsupported hardware gain values
Date: Sat, 7 Nov 2026 19:06:45 +0000	[thread overview]
Message-ID: <20261107190645.403d5556@jic23-hlaptop> (raw)
In-Reply-To: <20261107111710.442318-2-kapoorarnav43@gmail.com>

On Sat,  7 Nov 2026 16:47:03 +0530
Arnav Kapoor <kapoorarnav43@gmail.com> wrote:

> sx9310_write_gain() passes any value from userspace to ilog2() and then
> FIELD_PREP() into the 2-bit gain field without checking it. Only gains
> of 1, 2, 4 and 8 are supported (and listed in hardwaregain_available),
> but other values are not rejected; they are silently changed:
> 
>   - 3, 5, 6, 7 are rounded down to the next power of two;

There is nothing in the ABI rules that says a driver should not round these
values.  So whilst I don't mind a specific driver not doing that I'm not
sure we should modify the ABI of a driver to start rejecting them after
it has been out there for a while.  The only key thing is that value
read back after a write reflects the true scaling.

>   - 16 and larger wrap around in the 2-bit field, e.g. 16 sets a gain
>     of 1 and 32 a gain of 2;
>   - 0 and negative values set the maximum gain of 8, because ilog2(0)
>     is -1 and a negative int becomes a large unsigned value.

These two improvements are good and unlike rounding behaviour setting (including
reasonable rounding down to 32 - so anything that would not otherwise be 64) an
out of range scale or 0 / negative are not things correct userspace should
be doing.


> 
> The write returns success in every case, so userspace cannot tell that
> the gain it asked for was not applied.
> 
> Return -EINVAL for anything other than 1, 2, 4 or 8.
> 
> Fixes: 227c83faa2f8 ("iio: sx9310: Support hardware gain factor")
> Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
> ---
>  drivers/iio/proximity/sx9310.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
> index 2585b9c6fb69..27b5d604a409 100644
> --- a/drivers/iio/proximity/sx9310.c
> +++ b/drivers/iio/proximity/sx9310.c
> @@ -665,6 +665,9 @@ static int sx9310_write_gain(struct sx_common_data *data,
>  {
>  	unsigned int gain, mask;
>  
> +	if (val <= 0 || val > 8 || !is_power_of_2(val))
Upshot of above is check for <= 0 here.
> +		return -EINVAL;
> +
>  	gain = ilog2(val);
and check this fits in the field.  FIELD_FIT() will link that
explicitly to where it is being written. That only works here
because all the values that fit are valid (I think!)

Jonathan

>  
>  	switch (chan->channel) {


  reply	other threads:[~2026-11-07 19:06 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-07 11:17 [PATCH 0/2] iio: proximity: sx9310/sx9360: " Arnav Kapoor
2026-11-07 11:17 ` [PATCH 1/2] iio: proximity: sx9310: " Arnav Kapoor
2026-11-07 19:06   ` Jonathan Cameron [this message]
2026-11-07 11:17 ` [PATCH 2/2] iio: proximity: sx9360: " Arnav Kapoor
2026-11-07 19:07   ` 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=20261107190645.403d5556@jic23-hlaptop \
    --to=jic23@kernel.org \
    --cc=andriy.shevchenko@intel.com \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=gwendal@chromium.org \
    --cc=kapoorarnav43@gmail.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    --cc=swboyd@chromium.org \
    /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

all inboxes | Powered by JetHome®