* [PATCH] media: imon: Always take ictx->lock when changing the IR protocol
@ 2026-12-04 18:50 Praneeth Balasubramanian
0 siblings, 0 replies; only message in thread
From: Praneeth Balasubramanian @ 2026-12-04 18:50 UTC (permalink / raw)
To: Sean Young, Mauro Carvalho Chehab
Cc: Hans Verkuil, Tetsuo Handa, linux-media, linux-kernel,
Praneeth Balasubramanian, stable
imon_ir_change_protocol() is called from rc_register_device() during
probe, with ictx->lock held by imon_init_intf0(), and from
store_protocols() without it. It tells the two apart with
mutex_trylock(): if the lock is already taken, it assumes that its
caller holds it. But another task may hold it instead, for example
lcd_write() while send_packet() waits up to 10 seconds for a packet to
complete. The protocol change then runs send_packet() without the lock.
On devices that send with control URBs it refills ictx->tx_urb while
that URB is in flight, pointing its setup packet at a new control
request, and frees the request when usb_submit_urb() fails with -EBUSY,
while the host controller may still use it. The lockdep_assert_held()
added to send_packet() by commit 7019553ab850 ("media: imon: grab lock
earlier in imon_ir_change_protocol()") reports this case, but nothing
prevents it.
The trylock cannot simply become mutex_lock(). While probe holds
ictx->lock, rc_register_device() can open the new device, which takes
rdev->lock, and store_protocols() holds rdev->lock when it calls
imon_ir_change_protocol().
Drop ictx->lock around rc_register_device() in imon_init_rdev(), so that
neither caller holds it, and take it unconditionally in
imon_ir_change_protocol(). The lock order is then always rdev->lock
before ictx->lock. imon_init_rdev() takes the lock again before it
returns, so the rest of the probe is unchanged.
The unlocked call was found by a static check that turns the locking
rule in a function's comment into a Clang context-analysis requirement
on its callers.
Fixes: 23ef710e1a6c ("[media] imon: add conditional locking in change_protocol")
Cc: stable@vger.kernel.org
Signed-off-by: Praneeth Balasubramanian <praneethb2002@gmail.com>
---
Notes:
This was found by a compiler-based check, part of research at UCSB SecLab.
The locking rule stated in the kernel-doc comment of the called function
was turned into a Clang context-analysis requirement (__must_hold) on that
function, and Clang 23 with CONFIG_WARN_CONTEXT_ANALYSIS then checked every
caller. An LLM was used only to turn the comment into a machine-readable
rule; the report itself comes from the compiler. The patch and changelog
were drafted with Claude Opus 5.5. I reviewed the patch and take
responsibility for it.
Testing: builds without new warnings with W=1 (x86_64, clang 23,
allmodconfig). Not runtime-tested; I do not have this hardware, and I
have not observed the race myself.
drivers/media/rc/imon.c | 22 +++++++++++++++-------
1 file changed, 15 insertions(+), 7 deletions(-)
diff --git a/drivers/media/rc/imon.c b/drivers/media/rc/imon.c
index 56958576a13f..f1f0bd336f21 100644
--- a/drivers/media/rc/imon.c
+++ b/drivers/media/rc/imon.c
@@ -1102,20 +1102,21 @@ static void imon_touch_display_timeout(struct timer_list *t)
* This function gets called two different ways, one way is from
* rc_register_device, for initial protocol selection/setup, and the other is
* via a userspace-initiated protocol change request, either by direct sysfs
- * prodding or by something like ir-keytable. In the rc_register_device case,
- * the imon context lock is already held, but when initiated from userspace,
- * it is not, so we must acquire it prior to calling send_packet, which
- * requires that the lock is held.
+ * prodding or by something like ir-keytable. In neither case is the imon
+ * context lock held (imon_init_rdev() drops it around rc_register_device),
+ * so we must acquire it prior to calling send_packet, which requires that
+ * the lock is held.
*/
static int imon_ir_change_protocol(struct rc_dev *rc, u64 *rc_proto)
{
int retval;
struct imon_context *ictx = rc->priv;
struct device *dev = ictx->dev;
- const bool unlock = mutex_trylock(&ictx->lock);
unsigned char ir_proto_packet[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86 };
+ mutex_lock(&ictx->lock);
+
if (*rc_proto && !(*rc_proto & rc->allowed_protocols))
dev_warn(dev, "Looks like you're trying to use an IR protocol this device does not support\n");
@@ -1147,8 +1148,7 @@ static int imon_ir_change_protocol(struct rc_dev *rc, u64 *rc_proto)
ictx->pad_mouse = false;
out:
- if (unlock)
- mutex_unlock(&ictx->lock);
+ mutex_unlock(&ictx->lock);
return retval;
}
@@ -1999,7 +1999,15 @@ static struct rc_dev *imon_init_rdev(struct imon_context *ictx)
else
rdev->map_name = RC_MAP_IMON_PAD;
+ /*
+ * rc_register_device() calls imon_ir_change_protocol(), which takes
+ * ictx->lock, and may open the device, which takes rdev->lock. Since
+ * store_protocols() calls imon_ir_change_protocol() with rdev->lock
+ * held, ictx->lock must not be held here.
+ */
+ mutex_unlock(&ictx->lock);
ret = rc_register_device(rdev);
+ mutex_lock(&ictx->lock);
if (ret < 0) {
dev_err(ictx->dev, "remote input dev register failed\n");
goto out;
--
2.25.1
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-12-04 18:50 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 18:50 [PATCH] media: imon: Always take ictx->lock when changing the IR protocol Praneeth Balasubramanian
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox