All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v5] HID: hid-msi: Guard against delayed 0x06 MCU ACKs
@ 2026-12-05  6:26 Kenhou Iijima
  2026-12-05  6:48 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Kenhou Iijima @ 2026-12-05  6:26 UTC (permalink / raw)
  To: Jiri Kosina, Benjamin Tissoires, Derek J . Clark
  Cc: Denis Benato, Zhouwang Huang, Ana Veto, linux-input, linux-kernel

On the MSI Claw handhelds (e.g. 0db0:1901, Claw 8 AI+ fw 2.30 and Claw
A8 fw 3.13), some timeout-0 commands answer with generic 0x06 ACKs whose
count and timing vary across commands and firmware versions:
- SYNC_TO_ROM (0x22) emits an immediate ACK (~4 ms) plus a flash
  commit ACK ~515-566 ms later on firmware 2.30, or only a single
  commit ACK at the same offset (observed intermittently on 2.30 and
  consistently on 3.13); back-to-back SYNC_TO_ROMs within a single
  commit window can also coalesce into one reply.
- SWITCH_MODE (0x24) can answer with a second ACK ~13.5-21.5 ms after
  the command (~4-8 ms after the first), though a follow-up command
  such as READ_GAMEPAD_MODE or another SWITCH_MODE immediately
  afterwards suppresses that second ACK.
- If a timeout-0 command such as SWITCH_MODE is issued while
  SYNC_TO_ROM is committing to flash, the MCU queues its 0x06 ACKs
  behind the flash commit and emits them in a ~16 ms burst right after
  the commit ACK.

Two bugs occur in CLAW_COMMAND_TYPE_ACK when these delayed 0x06 packets
arrive after timeout-0 returns:
1. waiting_cmd only ignores 0x06 when it equals CLAW_COMMAND_TYPE_NONE.
   If a delayed 0x06 arrives during a read command (e.g.
   READ_GAMEPAD_MODE after an RGB update), it completes send_cmd_complete
   early without parsing the read reply, returning stale cached data.
   Expected delayed ACKs also emit "Got unexpected ACK from MCU" at
   dev_warn level.
2. If a subsequent WRITE_PROFILE_DATA (0x21) executes inside the delayed
   ACK window of an earlier SYNC_TO_ROM, SWITCH_MODE, or post-commit
   queued ACK burst, the leftover 0x06 matches waiting_cmd ==
   WRITE_PROFILE_DATA and completes the new write prematurely.

Fix both parts without relying on fragile ACK counting:
- Complete send_cmd_complete on a generic 0x06 ACK only while
  waiting_cmd == CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA, and downgrade
  leftover 0x06 messages to dev_dbg including waiting_cmd.
- Track a 35 ms 0x06 quiet window (ack_quiet_until) for non-SYNC_TO_ROM
  timeout-0 commands and track the SYNC_TO_ROM commit window (700 ms
  fallback, 100 ms minimum commit threshold) via rom_sync_until,
  waking waiters through a shared waitqueue (ack_waitq). Wait for
  active windows only before issuing WRITE_PROFILE_DATA (serialized
  with SYNC_TO_ROM by rom_mutex) so reads and SWITCH_MODE stay
  unblocked.
- Run both waits outside cfg_mutex first so uncontended profile writes
  never delay concurrent readers; bound each wait to the remainder
  observed on entry and re-check ack_quiet_until once after acquiring
  cfg_mutex so a concurrent SWITCH_MODE or RESET_DEVICE can neither
  race past the quiet window nor starve WRITE_PROFILE_DATA. When a
  commit-window 0x06 arrives outside an active TX quiet window, mark
  the ROM sync settled, open a 35 ms quiet window to absorb any queued
  timeout-0 ACK burst, and wake ack_waitq.
- Clear and wake both windows via claw_clear_ack_windows() (taking only
  cmd_lock): in claw_remove(), clear before removing sysfs/LED
  attributes and before and after cancelling rgb_queue; in
  claw_suspend(), clear after cancelling delayed work, both before and
  under cfg_mutex.

Since every 0x06 packet is identical on the wire, a delayed reply that
lands after the 100 ms commit threshold is indistinguishable from the
real commit ACK. The 35 ms quiet window opened on such a reply covers
the observed queued-ACK burst, though this remains a heuristic without
firmware support.

Note that back-to-back WRITE_PROFILE_DATA stores after SYNC_TO_ROM now
wait typically ~515-566 ms (up to the 700 ms fallback if no commit ACK
is seen) for the flash commit window to settle instead of risking
premature completion; reads and mode switches do not queue behind
uncontended writes and no writes are dropped.

Fixes: a9517958e828 ("HID: hid-msi: Add MSI Claw configuration driver")
Reported-by: Derek J. Clark <derekjohn.clark@gmail.com>
Link: https://lore.kernel.org/r/20261201012506.2313913-1-derekjohn.clark@gmail.com
Signed-off-by: Kenhou Iijima <kenhou@kenhou.net>
---
Changes in v5 (since v4):
- Snapshot jiffies into a single `now` value and pass it into
  claw_ack_window_remaining() so the expiry check and remainder
  subtraction cannot observe two different volatile reads (Derek).
- Replace rom_sync_complete and the cfg_mutex-held msleep_interruptible()
  quiet-window loop with a shared waitqueue (ack_waitq) and run both
  waits in claw_hw_output_report() before acquiring cfg_mutex so
  uncontended profile writes never delay concurrent readers; bound each
  claw_wait_ack_window() call to the remainder observed on entry and
  re-check ack_quiet_until once under cfg_mutex so concurrent
  SWITCH_MODE or RESET_DEVICE commands can neither race past the quiet
  window, race with completion reinitialization, nor starve
  WRITE_PROFILE_DATA (Derek).
- Call claw_clear_ack_windows() (taking only cmd_lock and waking
  ack_waitq) before removing sysfs/LED attributes and before and after
  cancelling rgb_queue in claw_remove(), and both before and under
  cfg_mutex after cancelling delayed work in claw_suspend(), so active
  window waiters wake without holding rom_mutex or cfg_mutex (Derek).
- On a failed timeout-0 transmission, restore any still-live inherited
  ROM-sync or quiet window without clobbering a concurrent clear or
  newer IRQ-armed window, comment the "+1 jiffy" deadline rounding, and
  include <linux/lockdep.h> and <linux/wait.h> explicitly.
- Update the commit message with Derek's firmware 3.13 measurements on
  the Claw A8 and rapid SWITCH_MODE behavior, switch Closes: to Link:,
  and note the post-100 ms stray-reply heuristic limitation.
- Link to v4: https://lore.kernel.org/r/20261201164931.12935-1-kenhou@kenhou.net

 drivers/hid/hid-msi.c | 227 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 225 insertions(+), 2 deletions(-)

diff --git a/drivers/hid/hid-msi.c b/drivers/hid/hid-msi.c
index 23d731f700..611cd224f3 100644
--- a/drivers/hid/hid-msi.c
+++ b/drivers/hid/hid-msi.c
@@ -21,9 +21,11 @@
 #include <linux/delay.h>
 #include <linux/device.h>
 #include <linux/hid.h>
+#include <linux/jiffies.h>
 #include <linux/kobject.h>
 #include <linux/led-class-multicolor.h>
 #include <linux/leds.h>
+#include <linux/lockdep.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/pm.h>
@@ -32,6 +34,7 @@
 #include <linux/types.h>
 #include <linux/unaligned.h>
 #include <linux/usb.h>
+#include <linux/wait.h>
 #include <linux/workqueue.h>
 
 #include "hid-ids.h"
@@ -332,6 +335,12 @@ struct claw_drvdata {
 	enum claw_profile_ack_pending profile_pending;
 	struct completion orphan_ack_complete;
 	struct completion send_cmd_complete;
+	wait_queue_head_t ack_waitq;
+	unsigned long rom_sync_min_ack; /* earliest jiffies for commit ACK */
+	unsigned long rom_sync_until; /* SYNC_TO_ROM delayed ACK deadline */
+	unsigned long ack_quiet_until; /* quiet deadline before profile write */
+	bool rom_sync_pending; /* guarded by cmd_lock */
+	bool ack_quiet_pending; /* guarded by cmd_lock */
 	struct delayed_work cfg_resume;
 	struct delayed_work cfg_setup;
 	spinlock_t registration_lock; /* Lock for registration read/write */
@@ -486,10 +495,36 @@ static int claw_profile_event(struct claw_drvdata *drvdata, struct claw_command_
 	return 0;
 }
 
+static unsigned long claw_ack_window_remaining(struct claw_drvdata *drvdata,
+					       bool *pending, unsigned long until,
+					       unsigned long now)
+{
+	lockdep_assert_held(&drvdata->cmd_lock);
+
+	if (!*pending)
+		return 0;
+
+	if (time_before(now, until))
+		return until - now;
+
+	*pending = false;
+	return 0;
+}
+
+static void claw_clear_ack_windows(struct claw_drvdata *drvdata)
+{
+	scoped_guard(spinlock_irqsave, &drvdata->cmd_lock) {
+		drvdata->rom_sync_pending = false;
+		drvdata->ack_quiet_pending = false;
+		wake_up_all(&drvdata->ack_waitq);
+	}
+}
+
 static int claw_raw_event(struct claw_drvdata *drvdata, struct hid_report *report,
 			  u8 *data, int size)
 {
 	struct claw_command_report *cmd_rep;
+	unsigned long now;
 	int ret = 0;
 
 	if (size != CLAW_PACKET_SIZE)
@@ -525,14 +560,45 @@ static int claw_raw_event(struct claw_drvdata *drvdata, struct hid_report *repor
 
 		break;
 	case CLAW_COMMAND_TYPE_ACK:
+		/*
+		 * SYNC_TO_ROM can emit an immediate ACK (~4 ms) and a flash
+		 * commit ACK (~515-566 ms), or only the commit ACK. Once a
+		 * commit-window 0x06 arrives outside an active TX quiet
+		 * window, mark the ROM sync settled and open a 35 ms quiet
+		 * window to absorb any timeout-0 ACKs (e.g. SWITCH_MODE)
+		 * that the MCU queued behind the flash commit.
+		 */
+		now = jiffies;
+		if (!claw_ack_window_remaining(drvdata,
+					       &drvdata->ack_quiet_pending,
+					       drvdata->ack_quiet_until, now) &&
+		    drvdata->rom_sync_pending &&
+		    time_after_eq(now, drvdata->rom_sync_min_ack)) {
+			drvdata->rom_sync_pending = false;
+			/* +1 jiffy so the window never expires early */
+			drvdata->ack_quiet_until = now + msecs_to_jiffies(35) + 1;
+			drvdata->ack_quiet_pending = true;
+			wake_up_all(&drvdata->ack_waitq);
+		}
+
 		if (drvdata->orphan_ack_pending) {
 			drvdata->orphan_ack_pending = false;
 			complete(&drvdata->orphan_ack_complete);
 			break;
 		}
 
-		if (drvdata->waiting_cmd == CLAW_COMMAND_TYPE_NONE) {
-			dev_warn(&drvdata->hdev->dev, "Got unexpected ACK from MCU, ignoring\n");
+		if (drvdata->waiting_cmd != CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA) {
+			/*
+			 * Only WRITE_PROFILE_DATA waits for a generic 0x06 ACK;
+			 * reads wait for their own ACK command type. Delayed
+			 * 0x06 ACKs from SYNC_TO_ROM (~515-566 ms) or
+			 * SWITCH_MODE (~13.5-21.5 ms) arrive after timeout-0
+			 * returns and must not complete an in-flight read
+			 * early.
+			 */
+			dev_dbg(&drvdata->hdev->dev,
+				"Got unexpected ACK from MCU, ignoring (waiting CMD: %x)\n",
+				drvdata->waiting_cmd);
 			break;
 		}
 
@@ -562,6 +628,44 @@ static int msi_raw_event(struct hid_device *hdev, struct hid_report *report,
 	return claw_raw_event(drvdata, report, data, size);
 }
 
+static bool claw_ack_window_done(struct claw_drvdata *drvdata,
+				 bool *pending, unsigned long *until)
+{
+	scoped_guard(spinlock_irqsave, &drvdata->cmd_lock)
+		return !claw_ack_window_remaining(drvdata, pending,
+						  *until, jiffies);
+}
+
+/*
+ * Wait at most once for the window remainder observed on entry. Callers
+ * holding the serializing mutex (rom_mutex for rom_sync, cfg_mutex +
+ * rom_mutex for the final ack_quiet check) cannot see the window re-armed
+ * while sleeping; the opportunistic pre-cfg_mutex ack_quiet check must not
+ * chase re-arms or continuous SWITCH_MODE stores could starve the writer.
+ */
+static int claw_wait_ack_window(struct claw_drvdata *drvdata,
+				bool *pending, unsigned long *until)
+{
+	unsigned long remaining;
+	long wret;
+
+	scoped_guard(spinlock_irqsave, &drvdata->cmd_lock)
+		remaining = claw_ack_window_remaining(drvdata, pending,
+						      *until, jiffies);
+	if (!remaining)
+		return 0;
+
+	wret = wait_event_interruptible_timeout(drvdata->ack_waitq,
+						claw_ack_window_done(drvdata,
+								     pending,
+								     until),
+						remaining);
+	if (wret < 0)
+		return wret;
+
+	return 0;
+}
+
 /* Caller must hold drvdata->cfg_mutex. */
 static int __claw_hw_output_report(struct hid_device *hdev, u8 index, u8 *data,
 				   size_t len, unsigned int timeout)
@@ -569,7 +673,12 @@ static int __claw_hw_output_report(struct hid_device *hdev, u8 index, u8 *data,
 	unsigned char *dmabuf __free(kfree) = NULL;
 	u8 header[] = { CLAW_OUTPUT_REPORT_ID, 0, 0, 0x3c, index };
 	struct claw_drvdata *drvdata = hid_get_drvdata(hdev);
+	unsigned long prev_rom_min_ack = 0, prev_rom_until = 0;
+	unsigned long prev_quiet_until = 0, quiet_deadline = 0;
 	size_t header_size = ARRAY_SIZE(header);
+	unsigned long rom_deadline = 0;
+	bool prev_quiet_pending = false;
+	bool prev_rom_pending = false;
 	bool orphaned;
 	int ret;
 
@@ -604,9 +713,40 @@ static int __claw_hw_output_report(struct hid_device *hdev, u8 index, u8 *data,
 			drvdata->waiting_cmd = index;
 			drvdata->cmd_status = -ETIMEDOUT;
 		} else {
+			unsigned long now = jiffies;
+
 			reinit_completion(&drvdata->orphan_ack_complete);
 			drvdata->waiting_cmd = CLAW_COMMAND_TYPE_NONE;
 			drvdata->orphan_ack_pending = true;
+			if (index == CLAW_COMMAND_TYPE_SYNC_TO_ROM) {
+				bool *rp = &drvdata->rom_sync_pending;
+
+				prev_rom_pending =
+					claw_ack_window_remaining(drvdata, rp,
+								  drvdata->rom_sync_until,
+								  now) != 0;
+				prev_rom_min_ack = drvdata->rom_sync_min_ack;
+				prev_rom_until = drvdata->rom_sync_until;
+				/* +1 jiffy so the window never expires early */
+				rom_deadline = now + msecs_to_jiffies(700) + 1;
+				drvdata->rom_sync_min_ack = now + msecs_to_jiffies(100);
+				drvdata->rom_sync_until = rom_deadline;
+				drvdata->rom_sync_pending = true;
+			} else {
+				/* +1 jiffy so the window never expires early */
+				unsigned long deadline = now + msecs_to_jiffies(35) + 1;
+				bool *qp = &drvdata->ack_quiet_pending;
+
+				prev_quiet_pending =
+					claw_ack_window_remaining(drvdata, qp,
+								  drvdata->ack_quiet_until,
+								  now) != 0;
+				prev_quiet_until = drvdata->ack_quiet_until;
+				if (!prev_quiet_pending || time_after(deadline, prev_quiet_until))
+					drvdata->ack_quiet_until = deadline;
+				quiet_deadline = drvdata->ack_quiet_until;
+				drvdata->ack_quiet_pending = true;
+			}
 		}
 	}
 
@@ -640,6 +780,33 @@ static int __claw_hw_output_report(struct hid_device *hdev, u8 index, u8 *data,
 	scoped_guard(spinlock_irqsave, &drvdata->cmd_lock) {
 		drvdata->waiting_cmd = CLAW_COMMAND_TYPE_NONE;
 		drvdata->orphan_ack_pending = false;
+		if (!timeout) {
+			if (index == CLAW_COMMAND_TYPE_SYNC_TO_ROM) {
+				if (drvdata->rom_sync_pending &&
+				    drvdata->rom_sync_until == rom_deadline) {
+					drvdata->rom_sync_min_ack = prev_rom_min_ack;
+					drvdata->rom_sync_until = prev_rom_until;
+					if (!claw_ack_window_remaining(drvdata,
+								       &prev_rom_pending,
+								       prev_rom_until,
+								       jiffies)) {
+						drvdata->rom_sync_pending = false;
+						wake_up_all(&drvdata->ack_waitq);
+					}
+				}
+			} else if (drvdata->ack_quiet_pending &&
+				   drvdata->ack_quiet_until == quiet_deadline) {
+				/* Restore the quiet window this command inherited. */
+				drvdata->ack_quiet_until = prev_quiet_until;
+				if (!claw_ack_window_remaining(drvdata,
+							       &prev_quiet_pending,
+							       prev_quiet_until,
+							       jiffies)) {
+					drvdata->ack_quiet_pending = false;
+					wake_up_all(&drvdata->ack_waitq);
+				}
+			}
+		}
 	}
 	return ret;
 }
@@ -648,6 +815,46 @@ static int claw_hw_output_report(struct hid_device *hdev, u8 index, u8 *data,
 				 size_t len, unsigned int timeout)
 {
 	struct claw_drvdata *drvdata = hid_get_drvdata(hdev);
+	int ret;
+
+	if (index == CLAW_COMMAND_TYPE_SYNC_TO_ROM)
+		lockdep_assert_held(&drvdata->rom_mutex);
+
+	/*
+	 * Drain any pending SYNC_TO_ROM flash-commit ACK window (~515-566 ms)
+	 * outside cfg_mutex before issuing WRITE_PROFILE_DATA, so reads and
+	 * mode switches never queue behind the flash commit. Callers of
+	 * WRITE_PROFILE_DATA and SYNC_TO_ROM hold rom_mutex, so no new
+	 * SYNC_TO_ROM can re-arm rom_sync_until while waiting here.
+	 *
+	 * Then wait for any 35 ms 0x06 quiet window (from SWITCH_MODE,
+	 * RESET_DEVICE, or a post-commit queued ACK burst) outside cfg_mutex
+	 * first so uncontended writes never delay concurrent readers; if
+	 * another command armed a fresh quiet window before we acquired
+	 * cfg_mutex, finish that bounded <= 35 ms wait under cfg_mutex so
+	 * continuous mode switches cannot starve WRITE_PROFILE_DATA.
+	 */
+	if (index == CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA) {
+		lockdep_assert_held(&drvdata->rom_mutex);
+
+		ret = claw_wait_ack_window(drvdata, &drvdata->rom_sync_pending,
+					   &drvdata->rom_sync_until);
+		if (ret)
+			return ret;
+
+		ret = claw_wait_ack_window(drvdata, &drvdata->ack_quiet_pending,
+					   &drvdata->ack_quiet_until);
+		if (ret)
+			return ret;
+
+		guard(mutex)(&drvdata->cfg_mutex);
+		ret = claw_wait_ack_window(drvdata, &drvdata->ack_quiet_pending,
+					   &drvdata->ack_quiet_until);
+		if (ret)
+			return ret;
+
+		return __claw_hw_output_report(hdev, index, data, len, timeout);
+	}
 
 	guard(mutex)(&drvdata->cfg_mutex);
 	return __claw_hw_output_report(hdev, index, data, len, timeout);
@@ -1868,6 +2075,7 @@ static int claw_probe(struct hid_device *hdev, u8 ep)
 	spin_lock_init(&drvdata->rumble_lock);
 	init_completion(&drvdata->orphan_ack_complete);
 	init_completion(&drvdata->send_cmd_complete);
+	init_waitqueue_head(&drvdata->ack_waitq);
 	INIT_DELAYED_WORK(&drvdata->cfg_resume, &cfg_resume_fn);
 	INIT_DELAYED_WORK(&drvdata->cfg_setup, &cfg_setup_fn);
 	INIT_DELAYED_WORK(&drvdata->rgb_queue, &claw_rgb_queue_fn);
@@ -1946,6 +2154,8 @@ static void claw_remove(struct hid_device *hdev)
 		smp_store_release(&drvdata->rgb_registered, false);
 	}
 
+	claw_clear_ack_windows(drvdata);
+
 	if (gp_registered)
 		device_remove_group(&hdev->dev, &claw_gamepad_attr_group);
 
@@ -1953,7 +2163,11 @@ static void claw_remove(struct hid_device *hdev)
 		device_remove_group(drvdata->led_mc.led_cdev.dev, &claw_rgb_attr_group);
 		led_classdev_multicolor_unregister(&drvdata->led_mc);
 	}
+
+	/* Wake rgb_queue as well if a draining sysfs store re-armed a window. */
+	claw_clear_ack_windows(drvdata);
 	cancel_delayed_work_sync(&drvdata->rgb_queue);
+	claw_clear_ack_windows(drvdata);
 
 	hid_hw_close(hdev);
 }
@@ -2016,6 +2230,15 @@ static int claw_suspend(struct hid_device *hdev)
 	cancel_delayed_work_sync(&drvdata->cfg_setup);
 	cancel_delayed_work_sync(&drvdata->rgb_queue);
 
+	/*
+	 * Delayed ACKs across suspend will not arrive after resume. Clear
+	 * windows before taking cfg_mutex so any active window waiter wakes
+	 * first, then clear again under cfg_mutex.
+	 */
+	claw_clear_ack_windows(drvdata);
+	scoped_guard(mutex, &drvdata->cfg_mutex)
+		claw_clear_ack_windows(drvdata);
+
 	return 0;
 }
 
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v5] HID: hid-msi: Guard against delayed 0x06 MCU ACKs
  2026-12-05  6:26 [PATCH v5] HID: hid-msi: Guard against delayed 0x06 MCU ACKs Kenhou Iijima
@ 2026-12-05  6:48 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-05  6:48 UTC (permalink / raw)
  To: Kenhou Iijima; +Cc: linux-input, dmitry.torokhov

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] HID: hid-msi: 32-bit jiffies wraparound in
  claw_ack_window_remaining()
- [Medium] HID: hid-msi: claw_suspend() races with sysfs writers on ACK
  windows
- [Low] HID: hid-msi: claw_raw_event() omits rom_sync_until expiry check
- [Low] HID: hid-msi: claw_remove() allows draining writes to re-arm ACK
  windows
--

commit d4a415522b3d555f6f69674df166a57041041923
Author: Kenhou Iijima <kenhou@kenhou.net>

HID: hid-msi: Guard against delayed 0x06 MCU ACKs

This commit restricts generic 0x06 ACK completion to WRITE_PROFILE_DATA
commands and adds jiffies-based quiet and ROM-sync wait windows so delayed
MCU ACKs do not prematurely complete subsequent reads or profile writes.

Link: https://lore.kernel.org/r/20261201012506.2313913-1-derekjohn.clark@gmail.com

> diff --git a/drivers/hid/hid-msi.c b/drivers/hid/hid-msi.c
> index 23d731f700c90..611cd224f3143 100644
> --- a/drivers/hid/hid-msi.c
> +++ b/drivers/hid/hid-msi.c

[ ... ]

> @@ -486,10 +495,36 @@ static int claw_profile_event(struct claw_drvdata *drvdata, struct claw_command_
>  	return 0;
>  }
>  
> +static unsigned long claw_ack_window_remaining(struct claw_drvdata *drvdata,
> +					       bool *pending, unsigned long until,
> +					       unsigned long now)
> +{
> +	lockdep_assert_held(&drvdata->cmd_lock);
> +
> +	if (!*pending)
> +		return 0;
> +
> +	if (time_before(now, until))
> +		return until - now;
> +
> +	*pending = false;
> +	return 0;
> +}

[Severity: Medium]
Can ack_quiet_until and rom_sync_until wrap around on 32-bit kernels?

CONFIG_HID_MSI has no 64BIT dependency, and both pending flags expire lazily
with no timer.  Every SYNC_TO_ROM commit ACK leaves ack_quiet_pending set,
so after 2^31 jiffies (~24.85 days at HZ=1000) without clearing it,
time_before(now, until) flips back to true and returns up to 0x80000000.

claw_wait_ack_window() passes that value directly to
wait_event_interruptible_timeout() while holding rom_mutex, causing a
multi-week sleep, an indefinite MAX_SCHEDULE_TIMEOUT sleep, or a
negative-timeout dump_stack().

From claw_rgb_queue_fn() on system_wq this stalls the worker and hangs
claw_suspend() in cancel_delayed_work_sync() before claw_clear_ack_windows()
can wake it.  Later timeout-0 commands also fail to overwrite the wrapped
deadline in __claw_hw_output_report() because time_after() returns false.

Storing 64-bit timestamps via get_jiffies_64() or ktime_t would avoid this.

[ ... ]

> @@ -525,14 +560,45 @@ static int claw_raw_event(struct claw_drvdata *drvdata, struct hid_report *repor
>  
>  		break;
>  	case CLAW_COMMAND_TYPE_ACK:
> +		/*
> +		 * SYNC_TO_ROM can emit an immediate ACK (~4 ms) and a flash
> +		 * commit ACK (~515-566 ms), or only the commit ACK. Once a
> +		 * commit-window 0x06 arrives outside an active TX quiet
> +		 * window, mark the ROM sync settled and open a 35 ms quiet
> +		 * window to absorb any timeout-0 ACKs (e.g. SWITCH_MODE)
> +		 * that the MCU queued behind the flash commit.
> +		 */
> +		now = jiffies;
> +		if (!claw_ack_window_remaining(drvdata,
> +					       &drvdata->ack_quiet_pending,
> +					       drvdata->ack_quiet_until, now) &&
> +		    drvdata->rom_sync_pending &&
> +		    time_after_eq(now, drvdata->rom_sync_min_ack)) {
> +			drvdata->rom_sync_pending = false;
> +			/* +1 jiffy so the window never expires early */
> +			drvdata->ack_quiet_until = now + msecs_to_jiffies(35) + 1;
> +			drvdata->ack_quiet_pending = true;
> +			wake_up_all(&drvdata->ack_waitq);
> +		}

[Severity: Low]
Should this check claw_ack_window_remaining() on rom_sync_until instead of
testing bare drvdata->rom_sync_pending?

If a SYNC_TO_ROM commit ACK is missed or lands during a 35 ms quiet window,
rom_sync_pending stays true past 700 ms.  Any later 0x06 ACK outside an
active quiet window (such as a reply to a userspace hidraw command) is then
misclassified as that commit ACK and arms a spurious 35 ms quiet window.

Using claw_ack_window_remaining() on rom_sync_pending and rom_sync_until
would enforce the upper bound and lazily clear an expired window instead.

[ ... ]

> @@ -1946,6 +2154,8 @@ static void claw_remove(struct hid_device *hdev)
>  		smp_store_release(&drvdata->rgb_registered, false);
>  	}
>  
> +	claw_clear_ack_windows(drvdata);
> +
>  	if (gp_registered)
>  		device_remove_group(&hdev->dev, &claw_gamepad_attr_group);
>  
> @@ -1953,7 +2163,11 @@ static void claw_remove(struct hid_device *hdev)
>  		device_remove_group(drvdata->led_mc.led_cdev.dev, &claw_rgb_attr_group);
>  		led_classdev_multicolor_unregister(&drvdata->led_mc);
>  	}
> +
> +	/* Wake rgb_queue as well if a draining sysfs store re-armed a window. */
> +	claw_clear_ack_windows(drvdata);
>  	cancel_delayed_work_sync(&drvdata->rgb_queue);
> +	claw_clear_ack_windows(drvdata);
>  
>  	hid_hw_close(hdev);
>  }

[Severity: Low]
Can draining sysfs stores re-arm rom_sync_pending after the first
claw_clear_ack_windows() and stall device_remove_group()?

Profile-write stores (such as rumble_intensity_left_store()) check
gp_registered before acquiring rom_mutex and do not re-check it afterward.
Because claw_clear_ack_windows() clears the flags once without preventing
re-arming, a second draining store can sleep for the full 700 ms window
while claw_remove() waits on it before reaching the later clears:

CPU0 (claw_remove)         CPU1 (store 1)          CPU2 (store 2)
clear gp_registered                                (passed gp_registered)
claw_clear_ack_windows()                           mutex_lock(rom_mutex)
device_remove_group()      SYNC_TO_ROM ->
  (waits for store 2)        rom_sync_pending=true
                           mutex_unlock(rom_mutex)
                                                   WRITE_PROFILE_DATA ->
                                                     claw_wait_ack_window()
                                                       <-- stalls 700 ms

Multiple draining stores can chain this stall sequentially during unbind or
disconnect.  Latching a teardown flag under cmd_lock in claw_remove() and
checking it before arming or waiting on ACK windows would prevent re-arming.

[ ... ]

> @@ -2016,6 +2230,15 @@ static int claw_suspend(struct hid_device *hdev)
>  	cancel_delayed_work_sync(&drvdata->cfg_setup);
>  	cancel_delayed_work_sync(&drvdata->rgb_queue);
>  
> +	/*
> +	 * Delayed ACKs across suspend will not arrive after resume. Clear
> +	 * windows before taking cfg_mutex so any active window waiter wakes
> +	 * first, then clear again under cfg_mutex.
> +	 */
> +	claw_clear_ack_windows(drvdata);
> +	scoped_guard(mutex, &drvdata->cfg_mutex)
> +		claw_clear_ack_windows(drvdata);
> +
>  	return 0;
>  }

[Severity: Medium]
Can claw_suspend() wake a concurrent sysfs profile writer early and let it
send WRITE_PROFILE_DATA prematurely or re-arm rom_sync_pending across
suspend?

Under USB runtime autosuspend, hid_suspend() calls claw_suspend() without
freezing userspace and before hid_cease_io() or port suspend runs.  Because
claw_wait_ack_window() returns 0 on wake and claw_suspend() synchronizes
only on cfg_mutex (not rom_mutex), a profile store can race as follows:

CPU0 (runtime autosuspend)          CPU1 (rumble_intensity_left_store)
hid_suspend()                       mutex_lock(rom_mutex)
  claw_suspend()                    WRITE_PROFILE_DATA ->
    claw_clear_ack_windows()          claw_wait_ack_window() -> returns 0
    mutex_lock(cfg_mutex) [waits]     mutex_lock(cfg_mutex)
                                      __claw_hw_output_report()
                                        <-- sends inside active ACK window
                                      mutex_unlock(cfg_mutex)
    claw_clear_ack_windows()
    mutex_unlock(cfg_mutex)
    return 0
                                    SYNC_TO_ROM ->
                                      rom_sync_pending = true
                                      usbhid_output_report()
                                        <-- arms window across suspend

usbhid_output_report() submits a temporary URB via usb_interrupt_msg()
without checking HID_SUSPENDED, so both commands can succeed before the port
suspends.  WRITE_PROFILE_DATA then runs inside the active delayed-ACK
window, and SYNC_TO_ROM leaves rom_sync_pending and orphan_ack_pending set.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205062637.84074-1-kenhou@kenhou.net?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-05  6:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05  6:26 [PATCH v5] HID: hid-msi: Guard against delayed 0x06 MCU ACKs Kenhou Iijima
2026-12-05  6:48 ` sashiko-bot

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.