Linux Input/HID development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Kenhou Iijima" <kenhou@kenhou.net>
Cc: linux-input@vger.kernel.org, dmitry.torokhov@gmail.com
Subject: Re: [PATCH v5] HID: hid-msi: Guard against delayed 0x06 MCU ACKs
Date: Sat, 05 Dec 2026 06:48:36 +0000	[thread overview]
Message-ID: <sashiko-outbox-251146@kernel.org> (raw)
In-Reply-To: <20261205062637.84074-1-kenhou@kenhou.net>

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

      reply	other threads:[~2026-12-05  6:48 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

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-251146@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dmitry.torokhov@gmail.com \
    --cc=kenhou@kenhou.net \
    --cc=linux-input@vger.kernel.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox