From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796395196; b=fifTFfQQiCqe0TaVpjBkM5o9S9xrYR86hrKDCBnZiWoFhcHXSuAsSiME0U4obxgosJYr lIk48vdKT7GmmN6WGsswppBu44b3UiQ9lVaiy9kVCtzlpSo7uAXZV7QrXLcEyGYnCYYmY azNuPKdZC/CWElVFWC4oR1Myb9iruSjyFT6u4U2gqWWQ83VoOzD+d8i/Exx3IJjHs4uNO S01M5YzDN7NH9/4QukCVORNpd9ZKp38y+M7GhWLZfgJYnhomFaoK5srNIwgdsJ5HdmwZI rShKOZGDRwmIUr+/Nic1TwjvkYE/BdsL1IjBfJoPRM7Xho6r/1lNhr/1gv0/8qEvhUA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796395196; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=Njln5u3ZuaRu6gymg8/cilvuzqz2WtN2WO8yR+/7ETU=; b=bkio7yy4NHiIEWOERiMDzrZsZ+/ea34oJfFH/ZtdjRYb4rZG61db23zKTcIycYaErbL1 FI0iTPr7VtgO031BxuhArxrx6DapiHzc5gzaM0A2Fa5Z+xpkQtPjLRNPbQFrVFbb2kV1+ KUgJ4XIRvH+QDZWt7lSbnTjA6vDMUjFyts1TyR1Af51hD9S2RLHjGC2gKLCDLU1XHqGmO 0Bnhk68gO9XJHgkV3so2N/XzrGWAadVeRDBybk1Vt+1PlBtxiLEnKIFeg0Q/90gsjyaAj qAe0sQJb6gYHRX2M5x40721CaSnjJWq+DoUEjdxiMjP9VTyYcxKvo+7K9nuL+yC6tOA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=kDifR1Dm; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D0A44606A322; Fri, 4 Dec 2026 14:39:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=kDifR1Dm Received: by smtp.kernel.org (Postfix) with ESMTPSA id AA8261F000C2; Fri, 4 Dec 2026 14:39:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796395196; bh=Njln5u3ZuaRu6gymg8/cilvuzqz2WtN2WO8yR+/7ETU=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=kDifR1Dm+l/IEMaJKPWqWhCLihv1pKziRfx9P0h3hyMUoj8zcV48gDaOxhgxmvsah mZ/syHUq/HnT7rnh3CZT5qvXvzi0pIjPgFBQDwudLHz4dII35GbIvzhDKRPoHJyAPO 4SnGwANeWgAgm4XW3Oe5vq5jIb0+DiGNd8aYk6+VDwwS/6MbsX3rHYaZ4X9E3DnuQz 09sYdP6+Zavx++OFRVmlxXwnVK1vj3d0MubQWe4jMFXttYDJOGsO66urJet1pPfG35 X8MHhvW6nEyfbPwTA07rQj+ZrRLF+eDQRSQ5KfxU829rNAAd48azch9ssEvAoKAZGn WbQV0L667N1jA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH 10/14] power/supply: add support for x86 macs in macsmc-power Reply-To: sashiko-reviews@lists.linux.dev To: "Atharva Tiwari" Cc: "Ihor Odainyi" , "Antoine Sidem" , "Aditya Garg" , "Andre Eikmeyer" , "Bjorn Helgaas" , "Thomas Gleixner" , "Ingo Molnar" , "Borislav Petkov" , "Dave Hansen" , x86@kernel.org, "H. Peter Anvin" , "Rafael J. Wysocki" , "Len Brown" , "Linus Walleij" , "Bartosz Golaszewski" , "Sven Peter" , "Janne Grunau" , "Neal Gompa" , "Guenter Roeck" , "Jonathan Cameron" , "David Lechner" , =?utf-8?b?TnVubyBTw6E=?= , "Andy Shevchenko" , "Lee Jones" , "Pavel Machek" , "Griffin Kroah-Hartman" , "Hans de Goede" , =?utf-8?b?SWxwbyBKw6RydmluZW4=?= , "Sebastian Reichel" , "Alexandre Belloni" , "Wim Van Sebroeck" , "Jarkko Sakkinen" , "Mimi Zohar" , "Roberto Sassu" , "Dmitry Kasatkin" , "Eric Snowberg" , "Paul Moore" , "James Morris" , "Serge E. Hallyn" , 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 In-Reply-To: <20261204131619.5059-11-atharvatiwarilinuxdev@gmail.com> References: <20261204131619.5059-1-atharvatiwarilinuxdev@gmail.com> <20261204131619.5059-11-atharvatiwarilinuxdev@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2026 14:39:54 +0000 X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 12 potential issues to consider: - [High] power: supply: macsmc: NULL deref of power->adapter in macsmc_power_probe() - [Medium] power: supply: macsmc: build failure when CONFIG_ACPI is disabled - [Medium] power: supply: macsmc: premature acpi_dev_put() in macsmc_power_remove() - [Medium] power: supply: macsmc: signed 64-bit overflow via BIT() in mult_frac() - [Medium] power: supply: macsmc: ENERGY_FULL* uses live B0AV instead of nominal voltage - [Medium] power: supply: macsmc: missing swab16() for B0TE on ACPI Macs - [Medium] power: supply: macsmc: AC properties registered without SMC key checks - [Medium] power: supply: macsmc: reversed OR probes non-existent CHIS on Intel Macs - [Low] power: supply: macsmc: duplicate float decoder in macsmc-power - [Low] power: supply: macsmc: sparse __be16 mismatch in macsmc_battery_acpi_read() - [Low] power: supply: macsmc: uninitialized voltage read on B0AV failure - [Low] power: supply: macsmc: repeated apple_smc_get_key_info() on runtime reads -- commit 82e1fded22961a0b758f2413ade4066e910b5ff5 Author: Atharva Tiwari power/supply: add support for x86 macs in macsmc-power This commit adds Intel x86 Mac support to the macsmc-power driver using ACPI adapter status and notifications along with big-endian and fixed-point SMC keys, exposing battery charge thresholds and AC power telemetry. > diff --git a/drivers/power/supply/macsmc-power.c b/drivers/power/supply/m= acsmc-power.c > index ced37a7019981..2ebf620a22c9c 100644 > --- a/drivers/power/supply/macsmc-power.c > +++ b/drivers/power/supply/macsmc-power.c [ ... ] > @@ -195,6 +204,41 @@ static int macsmc_battery_get_status(struct macsmc_p= ower *power) > return POWER_SUPPLY_STATUS_CHARGING; > } > =20 > +static int macsmc_battery_acpi_get_status(struct macsmc_power *power) > +{ [ ... ] > + status =3D acpi_evaluate_integer(power->adapter->handle, "_PSR", NULL, = &adapter_status); [Severity: Medium] Does this build when CONFIG_ACPI is disabled? CONFIG_MACSMC_POWER only depends on MFD_MACSMC_CORE, which does not depend on ACPI (for example on DeviceTree-only Apple Silicon ARM64 kernels). When CONFIG_ACPI is disabled, struct acpi_device is only forward-declared and acpi_evaluate_integer(), acpi_dev_install_notify_handler(), and acpi_dev_remove_notify_handler() have no stubs in include/acpi/acpi_bus.h. The same unguarded ACPI calls also appear in macsmc_ac_get_property(), macsmc_power_probe(), and macsmc_power_remove(). [ ... ] > @@ -322,6 +366,210 @@ static int macsmc_battery_get_capacity_level(struct= macsmc_power *power) [ ... ] > +static int macsmc_battery_acpi_read_f32_scaled(struct apple_smc *smc, sm= c_key key, > + int *p, int scale) [Severity: Low] Can macsmc_battery_acpi_read_f32_scaled() and the fixed-point decoders in macsmc_battery_acpi_read() be shared in macsmc-core instead of duplicating macsmc_hwmon_read_f32_scaled() and the fixed-point decoding from drivers/hwmon/macsmc-hwmon.c? Keeping separate copies has already led to drift between the two drivers, such as the unsigned BIT() divisor issue below. [ ... ] > +static int macsmc_battery_acpi_read(struct apple_smc *smc, smc_key key, = int *val, > + int scale) > +{ > + int ret; > + struct apple_smc_key_info info; > + > + ret =3D apple_smc_get_key_info(smc, key, &info); > + if (ret) > + return ret; [Severity: Low] Can the key info for these SMC keys be cached at probe time instead of calling apple_smc_get_key_info() on every property read? apple_smc_get_key_info() is uncached and performs a full synchronous SMC transaction under smc->mutex, doubling the hardware IPC round-trips for each runtime read of B0AP, TB0T, and the AC telemetry keys. [ ... ] > + /* 3.13 fixed point decimal */ > + case __SMC_KEY('f', 'p', '3', 'd'): { > + u16 fp3d; > + > + ret =3D apple_smc_read_u16(smc, key, &fp3d); > + if (ret) > + return ret; > + > + *val =3D mult_frac((s64)be16_to_cpu(fp3d), scale, BIT(13)); > + break; > + } [Severity: Low] Will sparse warn about passing host-endian u16 and s16 values to be16_to_cpu() across all nine fixed-point cases in macsmc_battery_acpi_read()? be16_to_cpu() expects __be16, whereas fp3d through sp87 are declared as plain u16 or s16. [ ... ] > + /* 3.12 signed fixed point decimal */ > + case __SMC_KEY('s', 'p', '3', 'c'): { > + s16 sp3c; > + > + ret =3D apple_smc_read_s16(smc, key, &sp3c); > + if (ret) > + return ret; > + > + *val =3D mult_frac((s64)(s16)be16_to_cpu(sp3c), scale, BIT(12)); > + break; > + } [Severity: Medium] Can BIT(12) as the divisor in mult_frac() trigger signed 64-bit overflow when sp3c is negative? BIT(12) has type unsigned long (64-bit on x86_64), so inside mult_frac() the division x_ / d_ converts negative s64 x_ to unsigned 64-bit and produces a quotient around 4.5e15. Multiplying that by scale =3D 1000000 in s64 overflows S64_MAX (triggering UBSAN under CONFIG_UBSAN_SIGNED_WRAP). The remaining signed fixed-point cases (sp4b through sp87) similarly pass unsigned BIT() divisors to mult_frac(), forcing unsigned division on negative s16 values. Using a signed divisor such as (1 << 12) avoids the unsigned promotion. [ ... ] > @@ -418,27 +734,64 @@ static int macsmc_battery_get_property(struct power= _supply *psy, [ ... ] > case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: > ret =3D apple_smc_read_u16(power->smc, SMC_KEY(B0DC), &vu16); > - val->intval =3D vu16 * power->nominal_voltage_mv; > + if (power->smc->is_acpi) { > + if (ret < 0) > + return ret; > + > + u16 voltage; > + > + ret =3D apple_smc_read_u16(power->smc, SMC_KEY(B0AV), &voltage); > + val->intval =3D vu16 * swab16(voltage); > + } else > + val->intval =3D vu16 * power->nominal_voltage_mv; > break; > case POWER_SUPPLY_PROP_ENERGY_FULL: > ret =3D apple_smc_read_u16(power->smc, SMC_KEY(B0FC), &vu16); > - val->intval =3D vu16 * power->nominal_voltage_mv; > + if (power->smc->is_acpi) { > + if (ret < 0) > + return ret; > + > + u16 voltage; > + > + ret =3D apple_smc_read_u16(power->smc, SMC_KEY(B0AV), &voltage); > + val->intval =3D swab16(vu16) * swab16(voltage); > + } else > + val->intval =3D vu16 * power->nominal_voltage_mv; > break; [Severity: Medium] Should ENERGY_FULL_DESIGN and ENERGY_FULL on ACPI Macs use power->nominal_voltage_mv instead of the live battery voltage B0AV? Multiplying B0DC and B0FC by live B0AV makes the reported design and full energy fluctuate with state of charge and load sag, distorting userspace battery wear calculations. macsmc_power_probe() already sets power->num_cells on ACPI Macs, so power->nominal_voltage_mv can be computed the same way as on Apple Silicon. > case POWER_SUPPLY_PROP_ENERGY_NOW: > ret =3D apple_smc_read_u16(power->smc, SMC_KEY(B0RM), &vu16); > - val->intval =3D macsmc_swap_b0rm(power, vu16) * power->nominal_voltage= _mv; > + if (power->smc->is_acpi) { > + if (ret < 0) > + return ret; > + > + u16 voltage; > + > + ret =3D apple_smc_read_u16(power->smc, SMC_KEY(B0AV), &voltage); > + val->intval =3D swab16(vu16) * swab16(voltage); > + } else > + val->intval =3D macsmc_swap_b0rm(power, vu16) * power->nominal_voltag= e_mv; > break; [Severity: Low] Does this read an uninitialized stack variable voltage if reading B0AV fails? In all three ENERGY_* cases, ret is not checked after apple_smc_read_u16(power->smc, SMC_KEY(B0AV), &voltage), so an SMC I/O error leaves voltage uninitialized when passed to swab16(). [ ... ] > @@ -667,18 +1093,23 @@ static int macsmc_power_probe(struct platform_devi= ce *pdev) [ ... ] > - if (apple_smc_key_exists(smc, SMC_KEY(CHIS))) > + if (apple_smc_key_exists(smc, SMC_KEY(CHIS)) || smc->is_acpi) > has_ac_adapter =3D true; [Severity: Medium] Should smc->is_acpi be checked before apple_smc_key_exists(smc, SMC_KEY(CHI= S))? With CHIS on the left side of ||, every PIO-mode Intel Mac probes the Apple-Silicon-only CHIS key and discards the result. When CHIS is missing, apple_smc_acpi_port_wait() stalls for 1-2 seconds while holding smc->mutex and clears smc->interrupt_enabled, forcing all subsequent SMC commands to fall back to 1 ms polling. [ ... ] > @@ -694,118 +1125,161 @@ static int macsmc_power_probe(struct platform_de= vice *pdev) [ ... ] > + if (apple_smc_key_exists(smc, SMC_KEY(B0TE))) > + props[nprops++] =3D POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW; [Severity: Medium] Is POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW missing a swab16() conversion for ACPI Macs? macsmc_battery_get_property() was updated to byte-swap B0TF for TIME_TO_FULL_NOW, but B0TE was left unchanged: drivers/power/supply/macsmc-power.c:macsmc_battery_get_property() { case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: ret =3D apple_smc_read_u16(power->smc, SMC_KEY(B0TE), &vu16); val->intval =3D vu16 =3D=3D 0xffff ? 0 : vu16 * 60; break; } On Intel ACPI Macs where B0TE is big-endian, reading time_to_empty_now returns corrupted byte-swapped values whenever B0TE is not 0xffff. [ ... ] > @@ -829,16 +1303,27 @@ static int macsmc_power_probe(struct platform_devi= ce *pdev) [ ... ] > + if (smc->is_acpi) { > + props[nprops++] =3D POWER_SUPPLY_PROP_CURRENT_NOW; > + props[nprops++] =3D POWER_SUPPLY_PROP_POWER_NOW; > + if (apple_smc_key_exists(power->smc, SMC_KEY(PD0R))) > + power->has_pd0r =3D true; > + /* MacBookPro5,x and 7,x dosent have VD0R key or any equivalent */ > + if (!power->has_battery || apple_smc_key_exists(power->smc, SMC_KEY(V= D0R))) > + props[nprops++] =3D POWER_SUPPLY_PROP_VOLTAGE_NOW; [Severity: Medium] Can registering CURRENT_NOW, POWER_NOW, and desktop VOLTAGE_NOW without checking for their backing SMC keys break macsmc-ac uevents? If ID0R/ID2R, PDTR/PD2R, or VD2R is absent (for example on a laptop with its battery disconnected where power->has_battery is false and macsmc_ac_get_property() queries desktop keys ID2R, PD2R, and VD2R), apple_smc_get_key_info() returns -EIO. Because add_prop_uevent() in drivers/power/supply/power_supply_sysfs.c only ignores -ENODEV, -ENODATA, and -EINVAL, returning -EIO fails power_supply_uevent() for the entire macsmc-ac device. [ ... ] > @@ -858,9 +1343,22 @@ static int macsmc_power_probe(struct platform_devic= e *pdev) > if (!power->batt && !power->ac) > return -ENODEV; > =20 > - power->nb.notifier_call =3D macsmc_power_event; > - blocking_notifier_chain_register(smc->event_handlers, &power->nb); > + if (!smc->is_acpi) { > + power->nb.notifier_call =3D macsmc_power_event; > + blocking_notifier_chain_register(smc->event_handlers, &power->nb); > + } else if (power->has_battery) { > + power->adapter =3D acpi_dev_get_first_match_dev("ACPI0003", NULL, -1); > + if (!power->adapter) > + return -ENODEV; > =20 > + ret =3D acpi_dev_install_notify_handler(power->adapter, ACPI_DEVICE_NO= TIFY, > + macsmc_power_acpi_notify, power); > + if (ret) { > + acpi_dev_put(power->adapter); > + power->adapter =3D NULL; > + return ret; > + } > + } > return 0; > } [Severity: High] Can power->adapter be dereferenced while NULL during macsmc_power_probe()? Both power->batt and power->ac are registered with devm_power_supply_register() before power->adapter is acquired. As soon as device_add() emits KOBJ_ADD, userspace (such as udevd or upowerd) or deferred_register_work can read status or online before power->adapter is assigned: CPU0 CPU1 macsmc_power_probe() devm_power_supply_register(batt) sysfs read: macsmc-battery/status macsmc_battery_acpi_get_status() power->adapter->handle <-- NULL deref power->adapter =3D acpi_dev_get_first_match_dev(...) The same NULL pointer dereference happens if acpi_dev_get_first_match_dev() or acpi_dev_install_notify_handler() fails and leaves power->adapter NULL until devres unregisters the power supplies. Acquiring power->adapter with a devm cleanup action before calling devm_power_supply_register() avoids both windows. > @@ -868,7 +1366,13 @@ static void macsmc_power_remove(struct platform_dev= ice *pdev) > { > struct macsmc_power *power =3D dev_get_drvdata(&pdev->dev); > =20 > - blocking_notifier_chain_unregister(power->smc->event_handlers, &power->= nb); > + if (!power->smc->is_acpi) > + blocking_notifier_chain_unregister(power->smc->event_handlers, &power-= >nb); > + else if (power->has_battery) { > + acpi_dev_remove_notify_handler(power->adapter, ACPI_DEVICE_NOTIFY, > + macsmc_power_acpi_notify); > + acpi_dev_put(power->adapter); > + } > } [Severity: Medium] Does calling acpi_dev_put(power->adapter) in macsmc_power_remove() drop the reference before power->batt and power->ac are unregistered? __device_release_driver() runs macsmc_power_remove() before devres_release_all() unregisters devm-managed power supplies, leaving a window where sysfs reads still dereference power->adapter->handle without a reference: CPU0 CPU1 __device_release_driver() macsmc_power_remove() acpi_dev_put(power->adapter) sysfs read: macsmc-battery/status macsmc_battery_acpi_get_status() power->adapter->handle <-- use-after-put devres_release_all() power_supply_unregister(...) Releasing power->adapter via a devm_add_action_or_reset() callback registered before devm_power_supply_register() guarantees both power supplies are unregistered before acpi_dev_put(). --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261204131619.5059= -1-atharvatiwarilinuxdev@gmail.com?part=3D10