All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Rosen Penev" <rosenp@gmail.com>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH 09/12] usb: apple-mfi-fastcharge: Configure runtime PM autosuspend
Date: Sun, 06 Dec 2026 03:05:08 +0000	[thread overview]
Message-ID: <sashiko-outbox-252102@kernel.org> (raw)
In-Reply-To: <92dbc6290a8c587e9078da7da146f66044d7d14d.1796524906.git.rosenp@gmail.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] usb: apple-mfi-fastcharge: NULL deref in mfi_fc_resume() before
  drvdata is set
- [High] usb: apple-mfi-fastcharge: usb_enable_autosuspend() breaks fast
  charging
- [Medium] usb: apple-mfi-fastcharge: missing usb_disable_autosuspend()
  on unbind
- [Medium] usb: apple-mfi-fastcharge: unpaired usb_enable_autosuspend()
  races resume
--

commit 52cde79222ebee5d1af8ec77b3372e905516d988
Author: Rosen Penev <rosenp@gmail.com>

usb: apple-mfi-fastcharge: Configure runtime PM autosuspend

This commit sets supports_autosuspend on mfi_fc_driver and calls
usb_enable_autosuspend() at the end of mfi_fc_probe() so the USB device
can runtime-autosuspend when idle.

> diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
> index fb4b975a62e94..83009b601da14 100644
> --- a/drivers/usb/misc/apple-mfi-fastcharge.c
> +++ b/drivers/usb/misc/apple-mfi-fastcharge.c
> @@ -212,6 +212,7 @@ static int mfi_fc_probe(struct usb_device *udev)
>  		return PTR_ERR(battery);
>  
>  	dev_set_drvdata(&udev->dev, mfi);
> +	usb_enable_autosuspend(udev);
>  
>  	return 0;
>  }

[Severity: High]
Does calling usb_enable_autosuspend() here break fast charging after the
default 2-second autosuspend delay?

After setting charge_type to Fast, apple_mfi_fc_set_property() immediately
calls pm_runtime_put_autosuspend().  With no runtime PM reference held and
no suspend callback to veto PMSG_AUTO_SUSPEND, an idle device enters USB
Suspend and loses its fast-charge state while charge_type stays Fast.

Unconditionally enabling autosuspend also overrides USB core's default
policy across all 0x05ac:0x1200..0x12ff devices (unlike other
generic_subclass drivers that only set supports_autosuspend) and adds a
40 ms usb_port_resume() stall on intermittent wakeups.

Dropping usb_enable_autosuspend() and preventing autosuspend while
POWER_SUPPLY_CHARGE_TYPE_FAST is active would avoid this regression.

[Severity: Medium]
Does mfi_fc_probe() leave autosuspend permanently enabled on udev after the
driver unbinds?

mfi_fc_driver defines no disconnect callback or devm action to call
usb_disable_autosuspend(udev).  Since pm_runtime_reinit() in
device_unbind_cleanup() returns early while pm_runtime_enabled() is true,
runtime_auto stays enabled after module unload or sysfs unbind.

> @@ -241,6 +242,7 @@ static struct usb_device_driver mfi_fc_driver = {
>  	.id_table =	mfi_fc_id_table,
>  	.match =	mfi_fc_match,
>  	.generic_subclass = 1,
> +	.supports_autosuspend = 1,
>  };

[Severity: High]
Can setting supports_autosuspend trigger a NULL pointer dereference in
mfi_fc_resume() before mfi_fc_probe() sets drvdata?

When binding to a runtime-suspended device (such as on rebind after unbind
leaves autosuspend enabled), really_probe() sets dev->driver before calling
usb_probe_device().  With supports_autosuspend and generic_subclass set,
usb_probe_device() no longer resumes udev before usb_generic_driver_probe().

Instead, usb_generic_driver_probe() -> usb_set_configuration() ->
usb_autoresume_device() -> usb_resume_device() invokes mfi_fc_resume()
before mfi_fc_probe() runs.  dev_get_drvdata(&udev->dev) is still NULL, so
guard(mutex)(&mfi->lock) dereferences NULL.

A concurrent charge_type write during probe can hit the same crash because
mfi_fc_probe() registers the power supply before calling dev_set_drvdata().

Checking for NULL in mfi_fc_resume() and setting drvdata before
devm_power_supply_register() would prevent both crashes.

[Severity: Medium]
Can an asynchronous runtime resume race with driver unbind and cause a
use-after-free of mfi in mfi_fc_resume()?

During unbind or rmmod while the device remains attached,
usb_generic_driver_disconnect() calls usb_autosuspend_device().  Without a
disconnect callback disabling autosuspend, udev can autosuspend before
device_unbind_cleanup() frees mfi and clears dev->driver:

CPU0 (unbind)                     CPU1 (pm_wq)
__device_release_driver()
  usb_unbind_device()
    usb_set_configuration(..., -1)
      usb_autosuspend_device()
        ... -> usb_port_suspend()
  device_unbind_cleanup()
    devres_release_all()
      mutex_destroy(&mfi->lock)
      kfree(mfi)
                                  usb_port_runtime_resume()
                                    pm_request_resume(&udev->dev)
                                  usb_runtime_resume()
                                    ... -> mfi_fc_resume()
                                      guard(mutex)(&mfi->lock)
                                        <-- use-after-free
    device_set_driver(dev, NULL)
    dev_set_drvdata(dev, NULL)

Because usb_runtime_resume() does not acquire device_lock(&udev->dev),
mfi_fc_resume() can touch freed mfi or sleep in usb_control_msg() across
devres_release_all() and module unload.

Calling usb_disable_autosuspend(udev) in a disconnect callback or devm
action before mfi is freed would synchronize runtime PM during unbind.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796524906.git.rosenp@gmail.com?part=9

  reply	other threads:[~2026-12-06  3:05 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-06  2:44 [PATCH 00/12] usb: apple-mfi-fastcharge: Fixes and cleanups Rosen Penev
2026-12-06  2:44 ` [PATCH 01/12] usb: apple-mfi-fastcharge: Convert to devm resources Rosen Penev
2026-12-06  3:00   ` sashiko-bot
2026-12-06  2:44 ` [PATCH 02/12] usb: apple-mfi-fastcharge: Use pm_runtime_resume_and_get() Rosen Penev
2026-12-06  2:44 ` [PATCH 03/12] usb: apple-mfi-fastcharge: Fix usb_control_msg() return check and timeout Rosen Penev
2026-12-06  2:44 ` [PATCH 04/12] usb: apple-mfi-fastcharge: Use POWER_SUPPLY_TYPE_USB instead of BATTERY Rosen Penev
2026-12-06  3:04   ` sashiko-bot
2026-12-06  2:44 ` [PATCH 05/12] usb: apple-mfi-fastcharge: Constify power_supply_property array Rosen Penev
2026-12-06  2:44 ` [PATCH 06/12] usb: apple-mfi-fastcharge: Use topology-based device name Rosen Penev
2026-12-06  2:44 ` [PATCH 07/12] usb: apple-mfi-fastcharge: Restore charge current on resume Rosen Penev
2026-12-06  3:05   ` sashiko-bot
2026-12-06  2:44 ` [PATCH 08/12] usb: apple-mfi-fastcharge: Serialize charge_type access with a mutex Rosen Penev
2026-12-06  3:07   ` sashiko-bot
2026-12-06  2:44 ` [PATCH 09/12] usb: apple-mfi-fastcharge: Configure runtime PM autosuspend Rosen Penev
2026-12-06  3:05   ` sashiko-bot [this message]
2026-12-06  2:44 ` [PATCH 10/12] usb: apple-mfi-fastcharge: Name the vendor power request Rosen Penev
2026-12-06  2:44 ` [PATCH 11/12] usb: apple-mfi-fastcharge: Rename battery variables to psy Rosen Penev
2026-12-06  2:58   ` sashiko-bot
2026-12-06  2:44 ` [PATCH 12/12] usb: apple-mfi-fastcharge: Simplify match and property_is_writeable Rosen Penev
2026-12-06  3:06   ` sashiko-bot
2026-12-06  3:07 ` [PATCH 00/12] usb: apple-mfi-fastcharge: Fixes and cleanups 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-252102@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=rosenp@gmail.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.