Linux GPIO subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Atharva Tiwari" <atharvatiwarilinuxdev@gmail.com>
Cc: "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: [RFC PATCH 03/14] watchdog: add driver for apple smc
Date: Thu, 29 Oct 2026 09:17:44 +0000	[thread overview]
Message-ID: <sashiko-outbox-189018@kernel.org> (raw)
In-Reply-To: <20261029085504.3547-4-atharvatiwarilinuxdev@gmail.com>

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [High] watchdog: macsmc_wdt: u16 write to 8-bit NATJ key in macsmc_wdt_set_timeout()
- [High] watchdog: macsmc_wdt: macsmc_wdt_set_timeout() arms stopped watchdog
- [High] watchdog: macsmc_wdt: missing WDIOF_MAGICCLOSE, parent, and stop hooks
- [Medium] watchdog: macsmc_wdt: uninitialized default timeout in macsmc_wdt_probe()
- [Medium] mfd: macsmc: apple_smc_probe() only checks NATJ key for macsmc-wdt
- [Low] watchdog: macsmc_wdt: undocumented nowayout and timeout module parameters
--

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

watchdog: add driver for apple smc

This patch adds a watchdog driver for the Apple SMC on Intel Macs and
registers the macsmc-wdt MFD device when the NATJ SMC key is present.

> diff --git a/drivers/mfd/macsmc-core.c b/drivers/mfd/macsmc-core.c
> index b8905c834c448..1c14698285456 100644
> --- a/drivers/mfd/macsmc-core.c
> +++ b/drivers/mfd/macsmc-core.c
> @@ -33,6 +33,10 @@ static const struct mfd_cell apple_smc_acpi_rtc[] = {
>  	MFD_CELL_NAME("macsmc-rtc"),
>  };
>  
> +static const struct mfd_cell apple_smc_acpi_wdt[] = {
> +	MFD_CELL_NAME("macsmc-wdt"),
> +};
> +
>  int apple_smc_read(struct apple_smc *smc, smc_key key, void *buf, size_t size)
>  {
>  	guard(mutex)(&smc->mutex);
> @@ -147,6 +151,13 @@ int apple_smc_probe(struct device *dev, const struct apple_smc_backend_ops *ops,
>  				return ret;
>  		}
>  
> +		if (apple_smc_key_exists(smc, SMC_KEY(NATJ))) {
> +			ret = devm_mfd_add_devices(dev, -1, apple_smc_acpi_wdt,
> +				ARRAY_SIZE(apple_smc_acpi_wdt), NULL, 0, NULL);
> +			if (ret < 0)
> +				return ret;
> +		}

[Severity: Medium]
In drivers/mfd/macsmc-core.c, apple_smc_probe() only checks for SMC_KEY(NATJ)
before registering apple_smc_acpi_wdt, whereas macsmc_wdt_probe() in
drivers/watchdog/macsmc_wdt.c checks for SMC_KEY(OSWD) on newer SMCs and
falls back to using both SMC_KEY(NATJ) and SMC_KEY(NATi) on older SMCs.

If a newer SMC provides SMC_KEY(OSWD) without SMC_KEY(NATJ), or if
SMC_KEY(NATJ) is present without SMC_KEY(NATi) and SMC_KEY(OSWD), will
device enumeration mismatch the keys required by macsmc_wdt? Should
apple_smc_probe() check for SMC_KEY(OSWD) || (SMC_KEY(NATJ) && SMC_KEY(NATi))
instead?

[ ... ]

> diff --git a/drivers/watchdog/macsmc_wdt.c b/drivers/watchdog/macsmc_wdt.c
> new file mode 100644
> index 0000000000000..38f68d0d87c10
> --- /dev/null
> +++ b/drivers/watchdog/macsmc_wdt.c
> @@ -0,0 +1,140 @@
> +// SPDX-License-Identifier: GPL-2.0 OR MIT
> +/*
> + * Apple SMC Watchdog Driver
> + *
> + * Copyright (C) 2026 Atharva Tiwari <atharvatiwarilinuxdev@gmail.com>
> + */
> +
> +#include <linux/mfd/macsmc.h>
> +#include <linux/platform_device.h>
> +#include <linux/watchdog.h>
> +
> +#define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
> +
> +static bool nowayout = WATCHDOG_NOWAYOUT;
> +module_param(nowayout, bool, 0);
> +MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started default="
> +		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
> +
> +static int timeout = WATCHDOG_TIMEOUT; /* in secounds */
> +module_param(timeout, int, 0);
> +MODULE_PARM_DESC(timeout,
> +		 "Watchdog timeout in seconds. 1 <= timeout <= 255, default="
> +		 __MODULE_STRING(WATCHDOG_TIMEOUT) ".");

[Severity: Low]
Since nowayout and timeout are exposed as kernel command-line and module
parameters via module_param() in drivers/watchdog/macsmc_wdt.c, should they
be documented in Documentation/admin-guide/kernel-parameters.txt (and
Documentation/watchdog/watchdog-parameters.rst)? Also, MODULE_PARM_DESC for
nowayout is missing an opening parenthesis before default=.

> +
> +struct macsmc_wdt {
> +	struct apple_smc *smc;
> +	bool has_oswd; /* Newer version of watchdogs in macsmc */
> +	struct watchdog_device wdd;
> +};
> +
> +static int macsmc_wdt_set_timeout(struct watchdog_device *wdd, unsigned int timeout)
> +{
> +	struct macsmc_wdt *drv = watchdog_get_drvdata(wdd);
> +	int ret;
> +
> +	if (timeout)
> +		wdd->timeout = timeout;

[Severity: High]
When userspace stops the watchdog via ioctl(WDIOC_SETOPTIONS,
WDIOS_DISABLECARD) and then configures a new timeout via
ioctl(WDIOC_SETTIMEOUT), watchdog_set_timeout() in
drivers/watchdog/watchdog_dev.c invokes wdd->ops->set_timeout(wdd, timeout)
while WDOG_ACTIVE and WDOG_HW_RUNNING are cleared.

Because macsmc_wdt_set_timeout() unconditionally writes the non-zero timeout
to SMC_KEY(OSWD) or writes to SMC_KEY(NATi) and SMC_KEY(NATJ) without
checking watchdog_active(wdd), doesn't this immediately arm the hardware
countdown in the SMC while the watchdog core still considers the device
stopped?

In that state, watchdog_ping() sees !watchdog_hw_running(wdd) and returns 0
without feeding the hardware, and closing /dev/watchdog in watchdog_release()
sees !watchdog_active(wdd) and skips watchdog_stop(), leading to an
unstoppable system reset when the timer expires. Also, updating wdd->timeout
before the SMC writes complete leaves wdd->timeout out of sync if
apple_smc_write_u16() fails.

Should macsmc_wdt_set_timeout() only write to the SMC when
watchdog_active(wdd) is true (or when called from start/stop), and update
wdd->timeout only after any hardware writes succeed?

> +
> +	if (drv->has_oswd) {
> +		ret = apple_smc_write_u16(drv->smc, SMC_KEY(OSWD), (u16)(timeout << 8));
> +		if (ret < 0)
> +			return ret;
> +	} else {
> +		/*
> +		 * NATJ = 2, makes the watchdog do force restart if NATi == 0
> +		 * NATJ = 0, makes the watchdog do nothing if NATi == 0
> +		 */
> +		if (timeout == 0) {
> +			ret = apple_smc_write_u16(drv->smc, SMC_KEY(NATJ), 0);
> +			if (ret < 0)
> +				return ret;
> +		}
> +		ret = apple_smc_write_u16(drv->smc, SMC_KEY(NATi), (u16)(timeout << 8));
> +		if (ret < 0)
> +			return ret;
> +
> +		if (timeout != 0) {
> +			ret = apple_smc_write_u16(drv->smc, SMC_KEY(NATJ), 2);
> +			if (ret < 0)
> +				return ret;
> +		}
> +	}

[Severity: High]
On Apple SMC hardware, NATi and OSWD are 2-byte keys (ui16), whereas NATJ is
a 1-byte key (ui8). In macsmc_wdt_set_timeout(), calling
apple_smc_write_u16(drv->smc, SMC_KEY(NATJ), ...) passes a length of 2 bytes
to the SMC backend in drivers/platform/x86/macsmc-acpi.c:

drivers/platform/x86/macsmc-acpi.c:apple_smc_acpi_port_write() {
	...
	if (apple_smc_acpi_port_write_byte(smc, len)) {
		dev_dbg(smc->dev, "port/write: write len failed, key: 0x%X, len: 0x%lX\n",
				key, len);
		return -EIO;
	}
	...
}

Won't writing 2 bytes to the 1-byte NATJ key cause apple_smc_acpi_port_write()
and apple_smc_acpi_mmio_write() to fail with -EIO when starting or stopping
the watchdog on machines without OSWD? (Additionally, even if NATJ were 16-bit,
passing unshifted 2 on little-endian x86 sends 0x0200 in big-endian.) Should
the NATJ writes use apple_smc_write_u8() instead of apple_smc_write_u16()?

[ ... ]

> +static const struct watchdog_info macsmc_wdt_info = {
> +	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
> +	.identity = "Macsmc Watchdog timer",
> +};
> +
> +static int macsmc_wdt_probe(struct platform_device *pdev)
> +{
> +	struct macsmc_wdt *drv;
> +
> +	drv = devm_kzalloc(&pdev->dev, sizeof(struct macsmc_wdt), GFP_KERNEL);
> +	if (!drv)
> +		return -ENOMEM;
> +
> +	drv->smc = dev_get_drvdata(pdev->dev.parent);
> +	if (apple_smc_key_exists(drv->smc, SMC_KEY(OSWD)))
> +		drv->has_oswd = true;
> +	else
> +		drv->has_oswd = false;
> +
> +	drv->wdd.ops = &macsmc_wdt_ops;
> +	drv->wdd.info = &macsmc_wdt_info;
> +	drv->wdd.min_timeout = 1;
> +	drv->wdd.max_timeout = 255;
> +	watchdog_init_timeout(&drv->wdd, timeout, &pdev->dev);

[Severity: Medium]
In macsmc_wdt_probe(), drv is allocated with devm_kzalloc(), so
drv->wdd.timeout starts at 0 before calling watchdog_init_timeout(&drv->wdd,
timeout, &pdev->dev).

If a user passes timeout=0 (to request the default timeout) or an
out-of-range value (such as a negative value or > 255) to the module
parameter, watchdog_init_timeout() in drivers/watchdog/watchdog_core.c will
leave drv->wdd.timeout at 0. When /dev/watchdog is then opened,
macsmc_wdt_start() calls macsmc_wdt_set_timeout(wdd, 0), which runs the stop
sequence and leaves the hardware timer disabled while the kernel marks it
active.

Should drv->wdd.timeout be initialized to WATCHDOG_TIMEOUT before calling
watchdog_init_timeout(), and should the timeout module parameter be unsigned
int (uint)?

> +	watchdog_set_nowayout(&drv->wdd, nowayout);
> +	watchdog_set_drvdata(&drv->wdd, drv);
> +
> +	return devm_watchdog_register_device(&pdev->dev, &drv->wdd);
> +}
> +
> +static struct platform_driver macsmc_wdt_driver = {
> +	.probe = macsmc_wdt_probe,
> +	.driver = {
> +		.name = "macsmc-wdt"
> +	},
> +};

[Severity: High]
There are a few lifecycle details in macsmc_wdt_info, macsmc_wdt_probe(), and
macsmc_wdt_driver that affect how the watchdog behaves on daemon crash,
reboot, unbind, and suspend:

1. macsmc_wdt_info.options omits WDIOF_MAGICCLOSE. In watchdog_release() in
drivers/watchdog/watchdog_dev.c, if WDIOF_MAGICCLOSE is not set and nowayout
is disabled, closing /dev/watchdog without writing the magic 'V' character
still stops the watchdog. If a userspace watchdog daemon crashes, won't the
kernel disarm the SMC watchdog instead of letting it reset the machine?

2. Because the Apple SMC is an independent always-on microcontroller, won't
an active watchdog keep counting down and forcibly reset the machine during
system reboot/shutdown, driver unbind, or system suspend unless
watchdog_stop_on_reboot(&drv->wdd), watchdog_stop_on_unregister(&drv->wdd),
and PM suspend/resume callbacks are provided?

3. Should macsmc_wdt_probe() also set drv->wdd.parent = &pdev->dev before
calling devm_watchdog_register_device()?

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

  reply	other threads:[~2026-10-29  9:17 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-29  8:51 [RFC PATCH 00/14] Add macsmc support for Intel Macs, replacing applesmc Atharva Tiwari
2026-10-29  8:51 ` [RFC PATCH 01/14] treewide: Add backend for macsmc Atharva Tiwari
2026-10-29  9:20   ` sashiko-bot
2026-10-29  8:51 ` [RFC PATCH 02/14] platform/x86: add backend for macsmc in intel macs Atharva Tiwari
2026-10-29  9:23   ` sashiko-bot
2026-10-29 12:59   ` Ilpo Järvinen
2026-10-29  8:52 ` [RFC PATCH 03/14] watchdog: add driver for apple smc Atharva Tiwari
2026-10-29  9:17   ` sashiko-bot [this message]
2026-10-29 13:30   ` Andy Shevchenko
2026-10-29  8:52 ` [RFC PATCH 04/14] iio/light: add new driver for macsmc Atharva Tiwari
2026-10-29  9:17   ` sashiko-bot
2026-10-29  8:52 ` [RFC PATCH 05/14] iio/accel: add driver for apple SMS Atharva Tiwari
2026-10-29  9:20   ` sashiko-bot
2026-10-29  8:52 ` [RFC PATCH 06/14] leds: add support for macsmc kbl Atharva Tiwari
2026-10-29  9:16   ` sashiko-bot
2026-10-29  8:52 ` [RFC PATCH 07/14] rtc: add support for x86 macs in macsmc-rtc Atharva Tiwari
2026-10-29  9:15   ` sashiko-bot
2026-10-29  8:52 ` [RFC PATCH 08/14] treewide: Add a flag to detect the Apple T2 chip Atharva Tiwari
2026-10-29  8:52 ` [RFC PATCH 09/14] treewide: disable other rtc's in t2 macs Atharva Tiwari
2026-10-29  9:16   ` sashiko-bot
2026-10-29  8:52 ` [RFC PATCH 10/14] power/supply: add support for x86 macs in macsmc-power Atharva Tiwari
2026-10-29  9:19   ` sashiko-bot
2026-10-29  8:52 ` [RFC PATCH 11/14] acpi: disable battery and adapter for x86 apple machines Atharva Tiwari
2026-10-29  9:13   ` sashiko-bot
2026-10-29  8:52 ` [RFC PATCH 12/14] hwmon/macsmc: add more types Atharva Tiwari
2026-10-29  9:18   ` sashiko-bot
2026-10-29  8:52 ` [RFC PATCH 13/14] hwmon: add support for x86 macs in macsmc-hwmon Atharva Tiwari
2026-10-29  9:20   ` sashiko-bot
2026-10-29  8:52 ` [RFC PATCH 14/14] mfd/macsmc: add ioctl to access smc keys Atharva Tiwari
2026-10-29  9:22   ` Sasha Finkelstein
2026-10-29 14:39     ` Atharva Tiwari
2026-10-29 14:53       ` Sasha Finkelstein
2026-10-29 15:25         ` Aditya Garg
2026-10-29 15:42           ` Sasha Finkelstein
2026-10-29  9:26   ` 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-189018@kernel.org \
    --to=sashiko-bot@kernel.org \
    --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=rafael@kernel.org \
    --cc=roberto.sassu@huawei.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=serge@hallyn.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