Linux USB
 help / color / mirror / Atom feed
From: Rosen Penev <rosenp@gmail.com>
To: Bastien Nocera <hadess@hadess.net>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 08/12] usb: apple-mfi-fastcharge: Serialize charge_type access with a mutex
Date: Sat,  5 Dec 2026 18:44:52 -0800	[thread overview]
Message-ID: <e9975a8ccbb9de751a58992b60e943d100020b45.1796524906.git.rosenp@gmail.com> (raw)
In-Reply-To: <cover.1796524906.git.rosenp@gmail.com>

Concurrent writes to charge_type from userspace can race: both
threads may read the old value, send their USB command, and write
the software state, leaving the cached charge_type mismatched with
the actual hardware state.

Add a per-device mutex and hold it across the USB control transfer
in set_charge_type, so the cached value always matches the last
current sent to the device. Update the cache only after the transfer
succeeds, which removes the need to roll it back on error. Take the
same lock in get_property and in the resume path. Initialize it with
devm_mutex_init() and use guard(mutex) for scoped locking.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/usb/misc/apple-mfi-fastcharge.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
index 85cf970e5f5e..fb4b975a62e9 100644
--- a/drivers/usb/misc/apple-mfi-fastcharge.c
+++ b/drivers/usb/misc/apple-mfi-fastcharge.c
@@ -7,6 +7,7 @@
 
 /* Standard include files */
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/power_supply.h>
 #include <linux/slab.h>
 #include <linux/usb.h>
@@ -44,6 +45,7 @@ MODULE_DEVICE_TABLE(usb, mfi_fc_id_table);
 struct mfi_device {
 	struct usb_device *udev;
 	int charge_type;
+	struct mutex lock;
 	struct power_supply_desc battery_desc;
 };
 
@@ -67,7 +69,6 @@ static int apple_mfi_fc_set_charge_type(struct mfi_device *mfi,
 					const union power_supply_propval *val)
 {
 	int current_ma;
-	int old_type;
 	int ret;
 
 	switch (val->intval) {
@@ -81,20 +82,20 @@ static int apple_mfi_fc_set_charge_type(struct mfi_device *mfi,
 		return -EINVAL;
 	}
 
+	guard(mutex)(&mfi->lock);
 	if (mfi->charge_type == val->intval) {
 		dev_dbg(&mfi->udev->dev, "charge type %d already set\n",
 			mfi->charge_type);
 		return 0;
 	}
 
-	old_type = mfi->charge_type;
-	mfi->charge_type = val->intval;
-
 	ret = apple_mfi_fc_send_current(mfi, current_ma);
 	if (ret < 0)
-		mfi->charge_type = old_type;
+		return ret;
 
-	return ret;
+	mfi->charge_type = val->intval;
+
+	return 0;
 }
 
 static int apple_mfi_fc_get_property(struct power_supply *psy,
@@ -106,9 +107,11 @@ static int apple_mfi_fc_get_property(struct power_supply *psy,
 	dev_dbg(&mfi->udev->dev, "prop: %d\n", psp);
 
 	switch (psp) {
-	case POWER_SUPPLY_PROP_CHARGE_TYPE:
+	case POWER_SUPPLY_PROP_CHARGE_TYPE: {
+		guard(mutex)(&mfi->lock);
 		val->intval = mfi->charge_type;
 		break;
+	}
 	case POWER_SUPPLY_PROP_SCOPE:
 		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
 		break;
@@ -176,11 +179,16 @@ static int mfi_fc_probe(struct usb_device *udev)
 	struct power_supply_desc *battery_desc;
 	struct power_supply *battery;
 	struct mfi_device *mfi;
+	int ret;
 
 	mfi = devm_kzalloc_obj(&udev->dev, *mfi);
 	if (!mfi)
 		return -ENOMEM;
 
+	ret = devm_mutex_init(&udev->dev, &mfi->lock);
+	if (ret)
+		return ret;
+
 	battery_desc = &mfi->battery_desc;
 	battery_desc->name = devm_kasprintf(&udev->dev, GFP_KERNEL, "apple_mfi_fastcharge_%s",
 						dev_name(&udev->dev));
@@ -212,6 +220,8 @@ static int mfi_fc_resume(struct usb_device *udev, pm_message_t message)
 {
 	struct mfi_device *mfi = dev_get_drvdata(&udev->dev);
 
+	guard(mutex)(&mfi->lock);
+
 	/*
 	 * A failure here must not fail the resume: the USB core would then
 	 * skip resuming the device's interfaces. Fall back to trickle so a
-- 
2.56.1


  parent reply	other threads:[~2026-12-06  2:45 UTC|newest]

Thread overview: 13+ 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  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  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  2:44 ` Rosen Penev [this message]
2026-12-06  2:44 ` [PATCH 09/12] usb: apple-mfi-fastcharge: Configure runtime PM autosuspend Rosen Penev
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:44 ` [PATCH 12/12] usb: apple-mfi-fastcharge: Simplify match and property_is_writeable Rosen Penev

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=e9975a8ccbb9de751a58992b60e943d100020b45.1796524906.git.rosenp@gmail.com \
    --to=rosenp@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=hadess@hadess.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    /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