Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: "Derek J. Clark" <derekjohn.clark@gmail.com>
To: jikos@kernel.org, bentiss@kernel.org
Cc: linux-input@vger.kernel.org,
	kernel@lists.opengamingcollective.org,
	gregkh@linuxfoundation.org, linux-usb@vger.kernel.org,
	pgriffais@valvesoftware.com, derekjohn.clark@gmail.com,
	stable@vger.kernel.org
Subject: [PATCH 3/8] HID: hid-msi: Protect the RGB state with frame_lock
Date: Thu,  3 Dec 2026 16:18:41 -0800	[thread overview]
Message-ID: <20261204001846.3801336-4-derekjohn.clark@gmail.com> (raw)
In-Reply-To: <20261204001846.3801336-1-derekjohn.clark@gmail.com>

The driver keeps one spinlock per data family and takes it on both sides
of the MCU. The reply handler and the attribute paths use the same lock
for the same field, and profile_lock only publishes which reply is
outstanding. The RGB family does not follow that. claw_profile_event()
writes rgb_frames, rgb_speed and led_cdev.brightness under frame_lock,
while effect_store(), enabled_store() and speed_store() wrote their field
under profile_lock, and claw_apply_rgb_state(), effect_show() and
enabled_show() read them with no lock at all.

Take frame_lock for the whole family. claw_apply_rgb_state() reads the
effect selection before calling the apply helpers, which take that lock
themselves to write the frame data, and claw_write_rgb_state() builds
its report from one critical section, so a store landing mid-sample
cannot produce a report that mixes values the MCU was never given.

Fixes: f27e2e3c80ed ("HID: hid-msi: Add RGB control interface")
Cc: stable@vger.kernel.org
Signed-off-by: Derek J. Clark <derekjohn.clark@gmail.com>
---
 drivers/hid/hid-msi.c | 47 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 35 insertions(+), 12 deletions(-)

diff --git a/drivers/hid/hid-msi.c b/drivers/hid/hid-msi.c
index 0e1e5e6f664d..c7b7399ef930 100644
--- a/drivers/hid/hid-msi.c
+++ b/drivers/hid/hid-msi.c
@@ -366,7 +366,7 @@ struct claw_drvdata {
 	enum claw_rgb_effect_index rgb_effect;
 	struct led_classdev_mc led_mc;
 	struct delayed_work rgb_queue;
-	spinlock_t frame_lock; /* lock for rgb_frames read/write */
+	spinlock_t frame_lock; /* Lock for RGB state read/write */
 	bool rgb_registered;
 	u8 rgb_frame_count;
 	bool rgb_enabled;
@@ -1273,8 +1273,7 @@ static int claw_read_rgb_config(struct hid_device *hdev)
 static int claw_write_rgb_state(struct claw_drvdata *drvdata)
 {
 	struct claw_rgb_report report = { {0x01, 0}, CLAW_RGB_FRAME_OFFSET, 0x00,
-			drvdata->rgb_frame_count, 0x09, drvdata->rgb_speed,
-			drvdata->led_mc.led_cdev.brightness };
+			drvdata->rgb_frame_count, 0x09, 0, 0 };
 	u16 write_addr = drvdata->rgb_addr;
 	int f, ret;
 
@@ -1284,6 +1283,11 @@ static int claw_write_rgb_state(struct claw_drvdata *drvdata)
 	if (!drvdata->rgb_frame_count)
 		return -EINVAL;
 
+	scoped_guard(spinlock_irqsave, &drvdata->frame_lock) {
+		report.speed = drvdata->rgb_speed;
+		report.brightness = drvdata->led_mc.led_cdev.brightness;
+	}
+
 	guard(mutex)(&drvdata->rom_mutex);
 	/* Loop through (up to) 8 pages of RGB data */
 	for (f = 0; f < drvdata->rgb_frame_count; f++) {
@@ -1446,10 +1450,18 @@ static int claw_apply_frostfire(struct claw_drvdata *drvdata)
 /* Apply current state to device */
 static int claw_apply_rgb_state(struct claw_drvdata *drvdata)
 {
-	if (!drvdata->rgb_enabled)
+	enum claw_rgb_effect_index effect;
+	bool enabled;
+
+	scoped_guard(spinlock_irqsave, &drvdata->frame_lock) {
+		enabled = drvdata->rgb_enabled;
+		effect = drvdata->rgb_effect;
+	}
+
+	if (!enabled)
 		return claw_apply_disabled(drvdata);
 
-	switch (drvdata->rgb_effect) {
+	switch (effect) {
 	case CLAW_RGB_EFFECT_MONOCOLOR:
 		return claw_apply_monocolor(drvdata);
 	case CLAW_RGB_EFFECT_BREATHE:
@@ -1505,7 +1517,7 @@ static ssize_t effect_store(struct device *dev,
 	if (ret < 0)
 		return ret;
 
-	scoped_guard(spinlock_irqsave, &drvdata->profile_lock)
+	scoped_guard(spinlock_irqsave, &drvdata->frame_lock)
 		drvdata->rgb_effect = ret;
 
 	mod_delayed_work(system_dfl_wq, &drvdata->rgb_queue, msecs_to_jiffies(50));
@@ -1519,6 +1531,7 @@ static ssize_t effect_show(struct device *dev,
 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
 	struct led_classdev_mc *led_mc = container_of(led_cdev, struct led_classdev_mc, led_cdev);
 	struct claw_drvdata *drvdata = container_of(led_mc, struct claw_drvdata, led_mc);
+	enum claw_rgb_effect_index effect;
 
 	scoped_guard(spinlock_irqsave, &drvdata->registration_lock) {
 		/* Pairs with smp_store_release from cfg_setup_fn in system_percpu_wq context */
@@ -1526,10 +1539,13 @@ static ssize_t effect_show(struct device *dev,
 			return -ENODEV;
 	}
 
-	if (drvdata->rgb_effect >= ARRAY_SIZE(claw_rgb_effect_text))
+	scoped_guard(spinlock_irqsave, &drvdata->frame_lock)
+		effect = drvdata->rgb_effect;
+
+	if (effect >= ARRAY_SIZE(claw_rgb_effect_text))
 		return -EINVAL;
 
-	return sysfs_emit(buf, "%s\n", claw_rgb_effect_text[drvdata->rgb_effect]);
+	return sysfs_emit(buf, "%s\n", claw_rgb_effect_text[effect]);
 }
 
 static DEVICE_ATTR_RW(effect);
@@ -1569,7 +1585,7 @@ static ssize_t enabled_store(struct device *dev,
 	if (ret)
 		return ret;
 
-	scoped_guard(spinlock_irqsave, &drvdata->profile_lock)
+	scoped_guard(spinlock_irqsave, &drvdata->frame_lock)
 		drvdata->rgb_enabled = val;
 
 	mod_delayed_work(system_dfl_wq, &drvdata->rgb_queue, msecs_to_jiffies(50));
@@ -1583,6 +1599,7 @@ static ssize_t enabled_show(struct device *dev,
 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
 	struct led_classdev_mc *led_mc = container_of(led_cdev, struct led_classdev_mc, led_cdev);
 	struct claw_drvdata *drvdata = container_of(led_mc, struct claw_drvdata, led_mc);
+	bool enabled;
 
 	scoped_guard(spinlock_irqsave, &drvdata->registration_lock) {
 		/* Pairs with smp_store_release from cfg_setup_fn in system_percpu_wq context */
@@ -1590,7 +1607,10 @@ static ssize_t enabled_show(struct device *dev,
 			return -ENODEV;
 	}
 
-	return sysfs_emit(buf, "%s\n", drvdata->rgb_enabled ? "true" : "false");
+	scoped_guard(spinlock_irqsave, &drvdata->frame_lock)
+		enabled = drvdata->rgb_enabled;
+
+	return sysfs_emit(buf, "%s\n", enabled ? "true" : "false");
 }
 static DEVICE_ATTR_RW(enabled);
 
@@ -1626,7 +1646,7 @@ static ssize_t speed_store(struct device *dev, struct device_attribute *attr,
 	/* 0 is fastest, invert value for intuitive userspace speed */
 	speed = 20 - val;
 
-	scoped_guard(spinlock_irqsave, &drvdata->profile_lock)
+	scoped_guard(spinlock_irqsave, &drvdata->frame_lock)
 		drvdata->rgb_speed = speed;
 
 	mod_delayed_work(system_dfl_wq, &drvdata->rgb_queue, msecs_to_jiffies(50));
@@ -1640,7 +1660,7 @@ static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
 	struct led_classdev_mc *led_mc = container_of(led_cdev, struct led_classdev_mc, led_cdev);
 	struct claw_drvdata *drvdata = container_of(led_mc, struct claw_drvdata, led_mc);
-	u8 speed = 20 - drvdata->rgb_speed;
+	u8 speed;
 
 	scoped_guard(spinlock_irqsave, &drvdata->registration_lock) {
 		/* Pairs with smp_store_release from cfg_setup_fn in system_percpu_wq context */
@@ -1648,6 +1668,9 @@ static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
 			return -ENODEV;
 	}
 
+	scoped_guard(spinlock_irqsave, &drvdata->frame_lock)
+		speed = 20 - drvdata->rgb_speed;
+
 	return sysfs_emit(buf, "%u\n", speed);
 }
 static DEVICE_ATTR_RW(speed);
-- 
2.56.0


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

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20261204001846.3801336-1-derekjohn.clark@gmail.com>
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:18 ` Derek J. Clark [this message]
2026-12-04  0:18 ` [PATCH 4/8] HID: hid-msi: Re-init on reset during resume Derek J. Clark
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:18 ` [PATCH 6/8] HID: hid-msi: Defer RGB updates while the MCU is suspended Derek J. Clark

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=20261204001846.3801336-4-derekjohn.clark@gmail.com \
    --to=derekjohn.clark@gmail.com \
    --cc=bentiss@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=jikos@kernel.org \
    --cc=kernel@lists.opengamingcollective.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=pgriffais@valvesoftware.com \
    --cc=stable@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