mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Zhu Jun <zhujun2@cmss.chinamobile.com>
Cc: jic23@kernel.org, dlechner@baylibre.com, nuno.sa@analog.com,
	crofts@kernel.org, linux-iio@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v4] iio: temperature: mcp9600: Avoid overflow when scaling thresholds
Date: Fri, 13 Nov 2026 16:08:01 +0200	[thread overview]
Message-ID: <avcZwcY8HQrUjbF5@ashevche-desk.local> (raw)
In-Reply-To: <20261113080558.173889-1-zhujun2@cmss.chinamobile.com>

On Fri, Nov 13, 2026 at 04:04:56PM +0800, Zhu Jun wrote:
> mcp9600_write_thresh() scales the integer part by 1000000 in int
> arithmetic before clamping to the supported temperature range. The
> multiplication can overflow, so a request such as 3000 degrees can
> select the lower threshold instead of the upper threshold.
> 
> Cast MICRO to s64 before multiplying so that scaling and the fractional
> addition or subtraction use signed 64-bit arithmetic. Let the existing
> per-junction bounds clamp the result. Use MICRO consistently and use
> div_s64() for the final conversion to support 32-bit architectures.

...

>  #include <linux/interrupt.h>
>  #include <linux/irq.h>

>  #include <linux/math.h>

This can be dropped now as it's implied by math64.h.

> +#include <linux/math64.h>
>  #include <linux/minmax.h>
>  #include <linux/module.h>
> +#include <linux/units.h>

...

>  	struct mcp9600_data *data = iio_priv(indio_dev);
>  	struct i2c_client *client = data->client;
> -	int s_val, i;
> +	s64 s_val;
> +	int i;

Please, keep the reversed xmas tree order.

>  	s16 thresh;
>  	u8 hyst;

...

>  		/* Scale value to include decimal part into calculations */
> -		s_val = (val < 0) ? ((val * 1000000) - val2) :
> -				    ((val * 1000000) + val2);

> +		s_val = (val < 0) ? ((val * (s64)MICRO) - val2) :
> +				    ((val * (s64)MICRO) + val2);

I think we can avoid casting by doing

		s_val = val < 0 ? -((val * MICRO) + val2) : ((val * MICRO) + val2);

>  		if (chan->channel2 == IIO_MOD_TEMP_AMBIENT) {
>  			s_val = max(s_val, MCP9600_MIN_TEMP_COLD_JUNCTION_MICRO);
>  			s_val = min(s_val, MCP9600_MAX_TEMP_COLD_JUNCTION_MICRO);

This is also clamping.

...

> -		thresh = (s16)(s_val / (1000000 >> 4));
> +		thresh = (s16)div_s64(s_val, MICRO >> 4);

Do you need casting here?

>  		return i2c_smbus_write_word_swapped(client,
>  						    MCP9600_ALERT_LIMIT(i + 1),
>  						    thresh);

-- 
With Best Regards,
Andy Shevchenko



      reply	other threads:[~2026-11-13 14:08 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-13  8:04 Zhu Jun
2026-11-13 14:08 ` Andy Shevchenko [this message]

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=avcZwcY8HQrUjbF5@ashevche-desk.local \
    --to=andriy.shevchenko@intel.com \
    --cc=crofts@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=jic23@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    --cc=zhujun2@cmss.chinamobile.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

all inboxes | Powered by JetHome®