All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ciro Pedrini" <ciro.pedrini@gmail.com>
Cc: linux-input@vger.kernel.org, dmitry.torokhov@gmail.com
Subject: Re: [PATCH v3 2/2] HID: lenovo-legion: Add keyboard backlight driver for Legion Gen 10
Date: Fri, 27 Nov 2026 01:20:25 +0000	[thread overview]
Message-ID: <sashiko-outbox-236394@kernel.org> (raw)
In-Reply-To: <20261127-legion-lighting-rfc-v3-2-15c55d9f15b7@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] HID: lenovo-legion: race on kbd->level in
  legion_brightness_set() and get()
--

commit 1968b7a56dc85b8ec700640dbc936c464e699f4b
Author: Ciro Pedrini <ciro.pedrini@gmail.com>

HID: lenovo-legion: Add keyboard backlight driver for Legion Gen 10

This commit adds a HID driver exposing the keyboard backlight brightness of
Lenovo Legion Gen 10 laptops as an LED class device with hardware brightness
change notifications.

> diff --git a/drivers/hid/hid-lenovo-legion.c b/drivers/hid/hid-lenovo-legion.c
> new file mode 100644
> index 0000000000000..cfd42c52500f1
> --- /dev/null
> +++ b/drivers/hid/hid-lenovo-legion.c
> @@ -0,0 +1,293 @@

[ ... ]

> +static int legion_brightness_set(struct led_classdev *led,
> +				 enum led_brightness value)
> +{
> +	struct legion_kbd *kbd = container_of(led, struct legion_kbd, led);
> +	u8 old = READ_ONCE(kbd->level);
> +	int ret;
> +
> +	if (value > led->max_brightness)
> +		return -EINVAL;
> +
> +	/* Before sending: the report of this set may come before we return. */
> +	WRITE_ONCE(kbd->level, value);
> +	ret = legion_simple_command(kbd, LEGION_OP_SET_BRIGHTNESS, value, NULL);
> +	if (ret)
> +		WRITE_ONCE(kbd->level, old);
> +
> +	return ret;
> +}
> +
> +static enum led_brightness legion_brightness_get(struct led_classdev *led)
> +{
> +	struct legion_kbd *kbd = container_of(led, struct legion_kbd, led);
> +	u8 value;
> +	int ret;
> +
> +	ret = legion_simple_command(kbd, LEGION_OP_GET_BRIGHTNESS, 0, &value);
> +	if (ret) {
> +		hid_dbg(kbd->hdev, "reading the brightness failed: %d\n", ret);
> +		return ret;
> +	}
> +
> +	if (value > led->max_brightness) {
> +		hid_dbg(kbd->hdev, "brightness out of range: %u\n", value);
> +		return -EIO;
> +	}
> +
> +	WRITE_ONCE(kbd->level, value);
> +	return value;
> +}

[Severity: Medium]
Can concurrent legion_brightness_set() and legion_brightness_get() calls race
on kbd->level?

In drivers/leds/led-class.c, brightness_store() releases led_cdev->led_access
after queuing set_brightness_work, so set_brightness_delayed() invokes
legion_brightness_set() without holding led_access while brightness_show()
can run legion_brightness_get() concurrently.  Because kbd->level is updated
outside kbd->lock in both functions, a get can overwrite the new level with
the old hardware value before the set command is sent:

CPU0 (set_brightness_work)        CPU1 (brightness_show)
legion_brightness_set(led, new)
  WRITE_ONCE(kbd->level, new)
  legion_simple_command(...)
    kzalloc_flex(...)
                                  legion_brightness_get(led)
                                    legion_simple_command(...)
                                      lock(&kbd->lock)
                                      /* reads old level */
                                      unlock(&kbd->lock)
                                    WRITE_ONCE(kbd->level, old)
    lock(&kbd->lock)
    /* sends SET_BRIGHTNESS(new) */
    unlock(&kbd->lock)
                                  legion_raw_event()
                                    /* ev->level (new) != kbd->level (old) */
                                    led_classdev_notify_brightness_hw_changed()
                                      <-- spurious hw_changed event

Similarly, if legion_brightness_set() or legion_raw_event() updates
kbd->level after legion_brightness_get() drops kbd->lock, the trailing
WRITE_ONCE(kbd->level, value) in legion_brightness_get() clobbers the newer
level with a stale value, which can also cause a subsequent hotkey event to
that stale level to be ignored.

Updating kbd->level under kbd->lock would avoid the race between set and
get.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127-legion-lighting-rfc-v3-0-15c55d9f15b7@gmail.com?part=2

  reply	other threads:[~2026-11-27  1:20 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  1:11 [PATCH v3 0/2] Keyboard backlight for the Lenovo Legion Pro 7 Gen 10 Ciro Pedrini
2026-11-27  1:11 ` Ciro Pedrini via B4 Relay
2026-11-27  1:11 ` [PATCH v3 1/2] platform/x86: ideapad-laptop: Ignore keyboard backlight hotkeys Ciro Pedrini
2026-11-27  1:11   ` Ciro Pedrini via B4 Relay
2026-11-27  1:11 ` [PATCH v3 2/2] HID: lenovo-legion: Add keyboard backlight driver for Legion Gen 10 Ciro Pedrini
2026-11-27  1:11   ` Ciro Pedrini via B4 Relay
2026-11-27  1:20   ` sashiko-bot [this message]
2026-11-28 12:01   ` Rong Zhang
2026-12-01 21:57     ` Ciro Pedrini
2026-12-01 22:03       ` Ciro Pedrini
2026-11-28 12:00 ` [PATCH v3 0/2] Keyboard backlight for the Lenovo Legion Pro 7 " Rong Zhang
2026-11-28 17:46 ` Marco Giunta
2026-11-28 18:43   ` Rong Zhang
2026-12-01 21:58   ` Ciro Pedrini
2026-12-01 22:03     ` Ciro Pedrini
2026-12-01 14:50 ` Ilpo Järvinen

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-236394@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=ciro.pedrini@gmail.com \
    --cc=dmitry.torokhov@gmail.com \
    --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 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.