Linux USB
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Derek J. Clark" <derekjohn.clark@gmail.com>
Cc: linux-input@vger.kernel.org, linux-usb@vger.kernel.org,
	dmitry.torokhov@gmail.com
Subject: Re: [PATCH 6/8] HID: hid-msi: Defer RGB updates while the MCU is suspended
Date: Fri, 04 Dec 2026 00:32:10 +0000	[thread overview]
Message-ID: <sashiko-outbox-248830@kernel.org> (raw)
In-Reply-To: <20261204001846.3801336-7-derekjohn.clark@gmail.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] HID: hid-msi: unconditional rgb_dirty clear in
  claw_write_rgb_state()
- [Medium] HID: hid-msi: TOCTOU race in cfg_setup_fn() overwrites
  concurrent RGB updates
- [Medium] HID: hid-msi: apply_store() commits stale MCU RAM while
  rgb_dirty is pending
--

commit bff64a28bf114d7d4e680c8a15718af9d92745dc
Author: Derek J. Clark <derekjohn.clark@gmail.com>

HID: hid-msi: Defer RGB updates while the MCU is suspended

This patch adds an rgb_dirty flag to struct claw_drvdata so RGB updates
scheduled while the MCU is suspended are tracked, replayed on resume, and
not overwritten by cfg_setup_fn() reading back MCU state.

> diff --git a/drivers/hid/hid-msi.c b/drivers/hid/hid-msi.c
> index d6134ff310f3a..d2cd0d7397aa4 100644
> --- a/drivers/hid/hid-msi.c
> +++ b/drivers/hid/hid-msi.c

[ ... ]

> @@ -1319,13 +1320,19 @@ static int claw_write_rgb_state(struct claw_drvdata *drvdata)
>  		/* Serialize the rgb_report and write it to MCU */
>  		ret = claw_hw_output_report(drvdata->hdev, CLAW_COMMAND_TYPE_WRITE_PROFILE_DATA,
>  					    (u8 *)&report, sizeof(report), 25);
> -		if (ret)
> +		if (ret) {
> +			/* Pairs with smp_load_acquire from cfg_resume_fn. */
> +			smp_store_release(&drvdata->rgb_dirty, true);
>  			return ret;
> +		}
>  
>  		/* Increment the write addr by the offset for the next frame */
>  		write_addr += CLAW_RGB_FRAME_OFFSET;
>  	}
>  
> +	/* Pairs with smp_load_acquire from cfg_resume_fn */
> +	smp_store_release(&drvdata->rgb_dirty, false);
> +
>  	return 0;
>  }

[Severity: Medium]
Can this unconditional clear of drvdata->rgb_dirty drop a newer RGB update
that arrived while claw_write_rgb_state() was transmitting an earlier one?

Because the RGB sysfs stores and claw_led_brightness_set() do not take
rom_mutex, a new update can set rgb_dirty and re-arm rgb_queue while an
in-flight claw_write_rgb_state() is still sending frames for an older
snapshot:

CPU0 (claw_rgb_queue_fn)           CPU1 (effect_store / claw_suspend)
claw_apply_rgb_state()
  claw_write_rgb_state()
    write frames for update A
                                   drvdata->rgb_effect = B
                                   claw_rgb_schedule()
                                     rgb_dirty = true
                                     mod_delayed_work(..., 50ms)
    rgb_dirty = false              <-- clears dirty for update B
                                   claw_suspend()
                                     cancel_delayed_work_sync(&rgb_queue)

If suspend runs before the re-armed work completes, rgb_queue is cancelled
with rgb_dirty already cleared (and if update B already blocked on
rom_mutex, the drvdata->suspended check in claw_write_rgb_state() returns
-EBUSY without re-marking rgb_dirty).  On resume, cfg_resume_fn() sees
rgb_dirty as false and never replays update B, leaving sysfs out of sync
with the MCU.

Tracking a monotonic generation counter incremented in claw_rgb_schedule(),
snapshotted before reading RGB state in claw_apply_rgb_state(), and only
clearing rgb_dirty when the generation still matches after all frames
succeed would avoid clobbering concurrent updates.

[ ... ]

> @@ -1762,6 +1778,13 @@ static void cfg_setup_fn(struct work_struct *work)
>  	gamepad_ready = true;
>  
>  prep_rgb:
> +	/* Pairs with smp_store_release from claw_rgb_schedule */
> +	dirty = smp_load_acquire(&drvdata->rgb_dirty);
> +	if (dirty) {
> +		rgb_ready = true;
> +		goto try_gamepad;
> +	}
> +
>  	ret = claw_read_rgb_config(drvdata->hdev);
>  	if (ret) {
>  		dev_err(&drvdata->hdev->dev,

[Severity: Medium]
Can an RGB update that arrives during claw_read_rgb_config() still be
overwritten by stale MCU data?

After a reset-resume, cfg_resume_fn() clears drvdata->suspended and
schedules cfg_setup_fn() while rgb_registered is already true.
cfg_setup_fn() checks rgb_dirty only once before claw_read_rgb_config()
sends 8 sequential read reports under profile_mutex:

CPU0 (cfg_setup_fn)                CPU1 (speed_store / HID irq)
dirty = rgb_dirty  /* false */
claw_read_rgb_config()
  send READ_PROFILE frame 0
                                   drvdata->rgb_speed = new_speed
                                   claw_rgb_schedule()
                                     rgb_dirty = true
                                   claw_profile_event()
                                     drvdata->rgb_speed = mcu_speed <-- lost
...
dirty = rgb_dirty  /* true */
claw_rgb_schedule()                <-- replays clobbered MCU values

claw_profile_event() acquires frame_lock without checking rgb_dirty and
overwrites rgb_frames[], rgb_speed, brightness, and subled_info with the
MCU reply, which cfg_setup_fn() then schedules to be written back.  Also,
because claw_read_rgb_config() does not hold rom_mutex, claw_rgb_queue_fn()
can fire mid-read and transmit a mix of old MCU frames and newly composed
frames.

[ ... ]

> @@ -1843,6 +1873,9 @@ static void cfg_resume_fn(struct work_struct *work)
>  	/* Pairs with smp_store_release from claw_resume in process context */
>  	resync = smp_load_acquire(&drvdata->resync);
>  
> +	/* Pairs with smp_store_release from claw_rgb_schedule. */
> +	rgb_dirty = smp_load_acquire(&drvdata->rgb_dirty);
> +
>  	scoped_guard(spinlock_irqsave, &drvdata->registration_lock) {
>  		/* Pairs with smp_store_release from cfg_setup_fn in system_percpu_wq context */
>  		gp_registered = smp_load_acquire(&drvdata->gp_registered);
> @@ -1852,6 +1885,8 @@ static void cfg_resume_fn(struct work_struct *work)
>  
>  	if (resync || !gp_registered || !rgb_registered)
>  		schedule_delayed_work(&drvdata->cfg_setup, msecs_to_jiffies(500));
> +	else if (rgb_dirty)
> +		claw_rgb_schedule(drvdata);
>  }

[Severity: Medium]
Can apply_store() commit stale or partially-written MCU RAM to ROM while
rgb_dirty is pending?

apply_store() only flushes rgb_queue and checks drvdata->suspended before
issuing CLAW_COMMAND_TYPE_SYNC_TO_ROM:

drivers/hid/hid-msi.c:apply_store() {
	...
	flush_delayed_work(&drvdata->rgb_queue);

	guard(mutex)(&drvdata->rom_mutex);
	if (drvdata->suspended)
		return -EBUSY;

	ret = claw_hw_output_report(hdev, CLAW_COMMAND_TYPE_SYNC_TO_ROM, NULL, 0, 0);
	...
}

When resync is true, cfg_resume_fn() clears drvdata->suspended and
schedules cfg_setup for 500 ms later without queuing rgb_queue, leaving a
500 ms window where suspended is false, rgb_dirty is true, and rgb_queue is
idle.  A mid-loop write error in claw_write_rgb_state() similarly leaves
rgb_dirty true with rgb_queue idle and partial frames in MCU RAM.  Writing
1 to apply in either state makes flush_delayed_work() a no-op and flashes
the stale or torn MCU RAM to ROM while reporting success.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204001846.3801336-1-derekjohn.clark@gmail.com?part=6

  reply	other threads:[~2026-12-04  0:32 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  0:18 [PATCH 0/8] MSI Claw suspend, resume and ROM commit fixes Derek J. Clark
2026-12-04  0:18 ` [PATCH 1/8] HID: hid-msi: Queue the RGB work item on system_dfl_wq Derek J. Clark
2026-12-04  0:18 ` [PATCH 2/8] HID: hid-msi: Move the ROM commit to an explicit apply attribute Derek J. Clark
2026-12-04  0:35   ` sashiko-bot
2026-12-04  0:18 ` [PATCH 3/8] HID: hid-msi: Protect the RGB state with frame_lock Derek J. Clark
2026-12-04  0:18 ` [PATCH 4/8] HID: hid-msi: Re-init on reset during resume Derek J. Clark
2026-12-04  0:37   ` sashiko-bot
2026-12-04  0:18 ` [PATCH 5/8] HID: hid-msi: Drain in-flight MCU traffic before suspend Derek J. Clark
2026-12-04  0:38   ` sashiko-bot
2026-12-04  0:18 ` [PATCH 6/8] HID: hid-msi: Defer RGB updates while the MCU is suspended Derek J. Clark
2026-12-04  0:32   ` sashiko-bot [this message]
2026-12-04  0:18 ` [PATCH 7/8] HID: hid-msi: Switch the RGB LED to brightness_set_blocking Derek J. Clark
2026-12-04  0:39   ` sashiko-bot
2026-12-04  0:18 ` [PATCH 8/8] USB: core: quirks: Add RESET_RESUME quirk for MSI Claw Derek J. Clark
2026-12-04  0:39 ` [PATCH 0/8] MSI Claw suspend, resume and ROM commit fixes 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-248830@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=derekjohn.clark@gmail.com \
    --cc=dmitry.torokhov@gmail.com \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-usb@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