Linux Hardware Monitor development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Atharva Tiwari" <atharvatiwarilinuxdev@gmail.com>
Cc: "Ihor Odainyi" <pv4k@odayny.com>,
	"Antoine Sidem" <sidemantoine@gmail.com>,
	"Aditya Garg" <aditya.garg@linux.dev>,
	"Bjorn Helgaas" <bhelgaas@google.com>,
	"Thomas Gleixner" <tglx@kernel.org>,
	"Ingo Molnar" <mingo@redhat.com>,
	"Borislav Petkov" <bp@alien8.de>,
	"Dave Hansen" <dave.hansen@linux.intel.com>,
	x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	"Len Brown" <lenb@kernel.org>,
	"Linus Walleij" <linusw@kernel.org>,
	"Bartosz Golaszewski" <brgl@kernel.org>,
	"Sven Peter" <sven@kernel.org>, "Janne Grunau" <j@jannau.net>,
	"Neal Gompa" <neal@gompa.dev>,
	"Guenter Roeck" <linux@roeck-us.net>,
	"Jonathan Cameron" <jic23@kernel.org>,
	"David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>, "Lee Jones" <lee@kernel.org>,
	"Pavel Machek" <pavel@kernel.org>,
	"Griffin Kroah-Hartman" <griffin@kroah.com>,
	"Hans de Goede" <hansg@kernel.org>,
	"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
	"Sebastian Reichel" <sre@kernel.org>,
	"Alexandre Belloni" <alexandre.belloni@bootlin.com>,
	"Wim Van Sebroeck" <wim@linux-watchdog.org>,
	"Jarkko Sakkinen" <jarkko@kernel.org>,
	"Mimi Zohar" <zohar@linux.ibm.com>,
	"Roberto Sassu" <roberto.sassu@huawei.com>,
	"Dmitry Kasatkin" <dmitry.kasatkin@gmail.com>,
	"Eric Snowberg" <eric.snowberg@oracle.com>,
	"Paul Moore" <paul@paul-moore.com>,
	"James Morris" <jmorris@namei.org>,
	"Serge E. Hallyn" <serge@hallyn.com>,
	linux-pci@vger.kernel.org, linux-gpio@vger.kernel.org,
	linux-hwmon@vger.kernel.org, linux-leds@vger.kernel.org,
	mfd@lists.linux.dev, linux-rtc@vger.kernel.org,
	linux-watchdog@vger.kernel.org, keyrings@vger.kernel.org,
	linux-security-module@vger.kernel.org
Subject: Re: [PATCH 12/14] hwmon/macsmc: add more types
Date: Fri, 04 Dec 2026 14:37:04 +0000	[thread overview]
Message-ID: <sashiko-outbox-249988@kernel.org> (raw)
In-Reply-To: <20261204131619.5059-13-atharvatiwarilinuxdev@gmail.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] hwmon: (macsmc) missing endian conversion in
  macsmc_hwmon_read_key()
- [Medium] hwmon: (macsmc) unclamped s64 to long truncation in
  macsmc_hwmon_read_key()
- [Low] hwmon: macsmc: sparse __be16 type mismatches on SMC fixed-point
  I/O
- [Low] hwmon: macsmc: duplicate fixed-point decoding in
  macsmc_hwmon_read_key()
- [Low] hwmon: missing range check for fpe2 write in
  macsmc_hwmon_write_key()
--

commit fa0827d4a4896bab5a321d44d03d23f333c92b8e
Author: Atharva Tiwari <atharvatiwarilinuxdev@gmail.com>

hwmon/macsmc: add more types

This commit adds read support for common integer and fixed-point SMC key
types in macsmc_hwmon_read_key() and write support for fpe2 fan speed keys
in macsmc_hwmon_write_key().

> diff --git a/drivers/hwmon/macsmc-hwmon.c b/drivers/hwmon/macsmc-hwmon.c
> index 1500ec2cc9f83..14b68cf8c283e 100644
> --- a/drivers/hwmon/macsmc-hwmon.c
> +++ b/drivers/hwmon/macsmc-hwmon.c
> @@ -221,6 +221,313 @@ static int macsmc_hwmon_read_key(struct apple_smc *smc,

[ ... ]

> +	/* unsigned 16-bit integer */
> +	case __SMC_KEY('u', 'i', '1', '6'): {
> +		u16 ui16 = 0;
> +
> +		ret = apple_smc_read_u16(smc, sensor->macsmc_key, &ui16);
> +		if (ret)
> +			return ret;
> +
> +		*val = (s64)ui16 * scale;
> +		break;
> +	}

[Severity: High]
Do the ui16, si16, ui32, and si32 cases need endianness conversion?

The ACPI SMC backend copies raw big-endian firmware bytes without
swapping, as macsmc_battery_get_property() accounts for when smc->is_acpi
is true.  Using the raw values directly returns byte-swapped readings and
corrupted signs on Intel ACPI Macs.

Converting from big-endian when smc->is_acpi is true and from
little-endian otherwise (sign-extending si16 and si32 afterward) would
handle both Intel and Apple Silicon backends.

[ ... ]

> +	/* unsigned 32-bit integer */
> +	case __SMC_KEY('u', 'i', '3', '2'): {
> +		u32 ui32 = 0;
> +
> +		ret = apple_smc_read_u32(smc, sensor->macsmc_key, &ui32);
> +		if (ret)
> +			return ret;
> +
> +		*val = (s64)ui32 * scale;
> +		break;
> +	}

[Severity: Medium]
Can assigning the scaled s64 product directly to long *val truncate on
32-bit kernels?

SENSORS_MACSMC_HWMON does not depend on 64BIT, and macsmc_hwmon_read()
passes scale = 1000000 for power sensors.  On 32-bit builds where long is
32 bits, scaled values for ui16, si16, ui32, si32, spc3, spd2, and spf0
can exceed [LONG_MIN, LONG_MAX] and silently wrap or invert sign.

Clamping with clamp_t(s64, ..., LONG_MIN, LONG_MAX) before assigning to
*val would match the saturation done for flt and ioft above.

[ ... ]

> +	/* 2.14 unsigned fixed point decimal */
> +	case __SMC_KEY('f', 'p', '2', 'e'): {
> +		u16 fp2e;
> +
> +		ret = apple_smc_read_u16(smc, sensor->macsmc_key, &fp2e);
> +		if (ret)
> +			return ret;
> +
> +		*val = mult_frac((s64)be16_to_cpu(fp2e), scale, BIT(14));
> +		break;
> +	}

[Severity: Low]
Will sparse warn about __be16 type mismatches across the fixed-point
cases?

All 20 new fp* and sp* cases pass host-endian u16 or s16 variables to
be16_to_cpu(), and the fpe2 case in macsmc_hwmon_write_key() passes
cpu_to_be16() to apple_smc_write_u16().

Using __be16 buffers with apple_smc_read()/apple_smc_write() or dedicated
__be16 helpers would eliminate these sparse warnings.

> +
> +	/* 3.13 unsigned fixed point decimal */
> +	case __SMC_KEY('f', 'p', '3', 'd'): {
> +		u16 fp3d;
> +
> +		ret = apple_smc_read_u16(smc, sensor->macsmc_key, &fp3d);
> +		if (ret)
> +			return ret;
> +
> +		*val = mult_frac((s64)be16_to_cpu(fp3d), scale, BIT(13));
> +		break;
> +	}

[Severity: Low]
Can the 20 fp* and sp* cases be collapsed into a parameterized helper
instead of repeating the read and scaling boilerplate?

Aside from the repetition (and minor inconsistencies between BIT(n) and
1 << n divisors and variable initialization), similar fixed-point
decoding also exists in macsmc_battery_acpi_read() in
drivers/power/supply/macsmc-power.c and could potentially be shared.

[ ... ]

> @@ -263,6 +570,9 @@ static int macsmc_hwmon_write_key(struct apple_smc *smc,
>  	/* unsigned 8-bit integer */
>  	case __SMC_KEY('u', 'i', '8', ' '):
>  		return apple_smc_write_u8(smc, sensor->macsmc_key, val);
> +	/* 14.2 fixed point decimal */
> +	case __SMC_KEY('f', 'p', 'e', '2'):
> +		return apple_smc_write_u16(smc, sensor->macsmc_key, cpu_to_be16(val << 2));

[Severity: Low]
Should this check that val is within [0, 0x3fff] before shifting and
narrowing to 16 bits?

macsmc_hwmon_write_fan() only bounds val against fan->min and fan->max,
which macsmc_hwmon_create_fan() parses independently without requiring
them to be fpe2.  If a negative value reaches this path, val << 2 is
signed left-shift undefined behavior, and val >= 0x4000 silently wraps.

Returning -EINVAL when val < 0 || val >= 0x4000, matching
applesmc_hwmon_write() in drivers/hwmon/applesmc.c, would prevent both.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204131619.5059-1-atharvatiwarilinuxdev@gmail.com?part=12

  reply	other threads:[~2026-12-04 14:37 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 13:12 [PATCH 00/14] Add macsmc support for Intel Macs, replacing applesmc Atharva Tiwari
2026-12-04 13:12 ` [PATCH 01/14] treewide: Add backend for macsmc Atharva Tiwari
2026-12-04 13:56   ` sashiko-bot
2026-12-04 14:07   ` Andy Shevchenko
2026-12-04 16:45   ` Sven Peter
2026-12-04 13:12 ` [PATCH 02/14] platform/x86: add backend for macsmc in intel macs Atharva Tiwari
2026-12-04 13:58   ` sashiko-bot
2026-12-04 14:19   ` Andy Shevchenko
2026-12-04 14:52     ` Aditya Garg
2026-12-04 17:23       ` Andy Shevchenko
2026-12-04 18:34         ` Aditya Garg
2026-12-04 19:57     ` Atharva Tiwari
2026-12-04 20:23       ` Andy Shevchenko
2026-12-04 13:12 ` [PATCH 03/14] watchdog: add driver for apple smc Atharva Tiwari
2026-12-04 13:48   ` sashiko-bot
2026-12-04 13:12 ` [PATCH 04/14] iio/light: add new driver for macsmc Atharva Tiwari
2026-12-04 13:47   ` sashiko-bot
2026-12-04 13:12 ` [PATCH 05/14] iio/accel: add driver for apple SMS Atharva Tiwari
2026-12-04 14:11   ` sashiko-bot
2026-12-04 13:12 ` [PATCH 06/14] leds: add support for macsmc kbl Atharva Tiwari
2026-12-04 14:07   ` sashiko-bot
2026-12-04 13:12 ` [PATCH 07/14] rtc: add support for x86 macs in macsmc-rtc Atharva Tiwari
2026-12-04 14:12   ` Andy Shevchenko
2026-12-04 14:48     ` Aditya Garg
2026-12-04 17:06       ` Andy Shevchenko
2026-12-04 17:10         ` Aditya Garg
2026-12-04 17:18           ` Andy Shevchenko
2026-12-04 17:26       ` Sven Peter
2026-12-04 14:16   ` sashiko-bot
2026-12-04 13:12 ` [PATCH 08/14] treewide: Add a flag to detect the Apple T2 chip Atharva Tiwari
2026-12-04 14:11   ` sashiko-bot
2026-12-04 13:12 ` [PATCH 09/14] treewide: disable other rtc's in t2 macs Atharva Tiwari
2026-12-04 14:27   ` sashiko-bot
2026-12-04 13:12 ` [PATCH 10/14] power/supply: add support for x86 macs in macsmc-power Atharva Tiwari
2026-12-04 14:39   ` sashiko-bot
2026-12-04 13:12 ` [PATCH 11/14] acpi: disable battery and adapter for x86 apple machines Atharva Tiwari
2026-12-04 14:08   ` Andy Shevchenko
2026-12-04 14:56     ` Aditya Garg
2026-12-04 17:14       ` Andy Shevchenko
2026-12-04 18:51         ` Aditya Garg
2026-12-04 20:02           ` Andy Shevchenko
2026-12-04 20:13             ` Andy Shevchenko
2026-12-04 14:25   ` sashiko-bot
2026-12-04 13:12 ` [PATCH 12/14] hwmon/macsmc: add more types Atharva Tiwari
2026-12-04 14:37   ` sashiko-bot [this message]
2026-12-04 18:44     ` Guenter Roeck
2026-12-04 13:12 ` [PATCH 13/14] hwmon: add support for x86 macs in macsmc-hwmon Atharva Tiwari
2026-12-04 14:48   ` sashiko-bot
2026-12-04 18:45     ` Guenter Roeck
2026-12-04 20:13       ` Jarkko Sakkinen
2026-12-04 13:12 ` [PATCH 14/14] mfd/macsmc: add ioctl to access smc keys Atharva Tiwari
2026-12-04 13:40   ` Sasha Finkelstein
2026-12-04 19:43     ` Atharva Tiwari
2026-12-04 14:48   ` sashiko-bot
2026-12-04 14:20 ` [PATCH 00/14] Add macsmc support for Intel Macs, replacing applesmc Andy Shevchenko

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-249988@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=aditya.garg@linux.dev \
    --cc=alexandre.belloni@bootlin.com \
    --cc=andy@kernel.org \
    --cc=atharvatiwarilinuxdev@gmail.com \
    --cc=bhelgaas@google.com \
    --cc=bp@alien8.de \
    --cc=brgl@kernel.org \
    --cc=dave.hansen@linux.intel.com \
    --cc=dlechner@baylibre.com \
    --cc=dmitry.kasatkin@gmail.com \
    --cc=eric.snowberg@oracle.com \
    --cc=griffin@kroah.com \
    --cc=hansg@kernel.org \
    --cc=hpa@zytor.com \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=j@jannau.net \
    --cc=jarkko@kernel.org \
    --cc=jic23@kernel.org \
    --cc=jmorris@namei.org \
    --cc=keyrings@vger.kernel.org \
    --cc=lee@kernel.org \
    --cc=lenb@kernel.org \
    --cc=linusw@kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=linux-rtc@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=linux-watchdog@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=mfd@lists.linux.dev \
    --cc=mingo@redhat.com \
    --cc=neal@gompa.dev \
    --cc=nuno.sa@analog.com \
    --cc=paul@paul-moore.com \
    --cc=pavel@kernel.org \
    --cc=pv4k@odayny.com \
    --cc=rafael@kernel.org \
    --cc=roberto.sassu@huawei.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=serge@hallyn.com \
    --cc=sidemantoine@gmail.com \
    --cc=sre@kernel.org \
    --cc=sven@kernel.org \
    --cc=tglx@kernel.org \
    --cc=wim@linux-watchdog.org \
    --cc=x86@kernel.org \
    --cc=zohar@linux.ibm.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