Linux Hardware Monitor development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: skxby <2374604267@qq.com>
Cc: linux-hwmon@vger.kernel.org
Subject: Re: [PATCH 2/2] hwmon: (tmp108) add Rust TMP108 driver
Date: Fri, 16 Oct 2026 07:35:54 +0000	[thread overview]
Message-ID: <sashiko-outbox-173940@kernel.org> (raw)
In-Reply-To: <tencent_EF01D718C016C6E011BFDF39B38933C0A807@qq.com>

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] hwmon: tmp108: panic via arithmetic underflow in write_update_interval()
- [Medium] hwmon/tmp108: missing hardware initialization in init
- [Low] hwmon: hardware monitoring driver registered from outside drivers/hwmon/ violates layering
- [Low] kconfig: SAMPLE_RUST_TMP108 tristate config depends on HWMON=y preventing module builds
--

commit 78120c93b1be27841c1bf9909b41399a14b830f6
Author: skxby <2374604267@qq.com>

hwmon: (tmp108) add Rust TMP108 driver
    
Add a Rust driver for the TI TMP108 temperature sensor, mirroring the
existing C driver (drivers/hwmon/tmp108.c).

> diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig
> index 31d62533ef25f..18df4282756c3 100644
> --- a/samples/rust/Kconfig
> +++ b/samples/rust/Kconfig
[ ... ]
> +config SAMPLE_RUST_TMP108
> +	tristate "TMP108 Driver"
> +	depends on HWMON=y
> +	depends on I2C=y

[Severity: Low]
Does this introduce a Kconfig regression? If CONFIG_HWMON=m or CONFIG_I2C=m,
this prevents the sample driver from being built at all, even as a module.

Should standard dependency strings like depends on HWMON and
depends on I2C be used to inherit module constraints properly?

> diff --git a/samples/rust/tmp108.rs b/samples/rust/tmp108.rs
> new file mode 100755
> index 0000000000000..a070547f29ee4
> --- /dev/null
> +++ b/samples/rust/tmp108.rs
[ ... ]
> +fn find_closest_descending(x: c_long, samples: &[u16]) -> usize {
[ ... ]
> +    while i >= 1 {
> +        let mid = (samples[i] as c_long + samples[i - 1] as c_long) / 2;
> +        if x <= mid {
> +            let left = x - samples[i] as c_long;

[Severity: High]
Can this code panic due to an arithmetic underflow regression?

If userspace writes a large negative value (e.g., LONG_MIN) to the
update_interval sysfs attribute, it is passed unmodified to this function.

Subtracting a positive sample value from c_long::MIN will underflow here.

In debug builds or when CONFIG_RUST_OVERFLOW_CHECKS=y is set, this
triggers a panic, crashing the kernel. In release builds, it silently
wraps around and sets an incorrect hardware conversion rate.

Could we clamp the value before subtraction, or use saturating math?

[ ... ]
> +    fn write_update_interval(&self, value: c_long) -> Result {
> +        let idx = find_closest_descending(value, &SAMPLE_TIMES);

[Severity: High]
Is it possible to pass an unclamped value here?

Since the value passed from userspace via sysfs is not clamped before
calling find_closest_descending(), an extreme negative value can reach
the subtraction above and cause an underflow panic.

[ ... ]
> +    fn init(_module: &'static ThisModule) -> Result<Self> {
[ ... ]
> +        // Registration failure path: if `Registration::new` fails and `?` returns early, the
> +        // already-created i2c client is never unregistered. Such a leftover client keeps the
> +        // bus address 0x48 occupied forever, making every subsequent insmod fail with EBUSY.
> +        // The failure branch must therefore unregister the client explicitly (cleanup on the
> +        // hwmon side is handled by the hwmon core itself).
> +        let hwmon = match Registration::new(parent, c"tmp108", Tmp108::new(client)) {

[Severity: Low]
Does registering a hardware monitoring device from outside drivers/hwmon/
violate subsystem layering rules?

The HWMON subsystem guidelines state that hardware monitoring drivers should
reside in drivers/hwmon/. While this is a sample driver, directly using
the hwmon registration API from samples/rust/ bypasses this architectural
layout rule.

[Severity: Medium]
Are we missing hardware initialization for the TMP108 device during init?

Unlike the C driver, this code registers the hwmon device without enabling
the regulator or configuring the CONF register (e.g., writing
TMP108_MODE_CONTINUOUS and clearing TMP108_CONF_TM).

If the chip is in shutdown mode from the bootloader, continuous conversions
will not occur, and accessing sysfs attributes like temp1_input could
return stale temperature readings to userspace.

> +            Ok(hwmon) => hwmon,
> +            Err(e) => {
> +                // SAFETY: client was created successfully in this function (non-null,
> +                // non-ERR_PTR) and is not yet owned by hwmon (registration failed), so no
> +                // in-flight callback touches the pointer.
> +                unsafe { bindings::i2c_unregister_device(client) };
> +                return Err(e);
> +            }
> +        };
> +

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/tencent_95E7F9B5CE294CA4027A0CD4C550B454050A@qq.com?part=2

  reply	other threads:[~2026-10-16  7:35 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <179213545522.40444.8748988877722946371@qq.com>
2026-10-16  7:24 ` [PATCH 2/2] hwmon: (tmp108) add Rust TMP108 driver skxby
2026-10-16  7:35   ` sashiko-bot [this message]
2026-10-16  7:24 ` [PATCH 1/2] rust: hwmon: add hwmon abstractions skxby
2026-10-16  7:33   ` sashiko-bot

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=sashiko-outbox-173940@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=2374604267@qq.com \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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