From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.173 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796410259; b=otMfr8N982SAUPHULzZMabaoxlyibmmlG5vsNYw+RJ9cicptkVDPaPU2Qlvz7rMGfgBG lxtuKVA/HHV3qSVMTMRFXJ0zAkc7N8gKNzyNNU+i1uSdg9zLUWmuSsdh/++xf33anFXch Fkx/cXzwEG12AemSbuzRplr5EZPfXmu+LU0oC4q6H4aO8jnprjzX5rBcgxwDfPzffQKU1 iPPUq9glrzPDx1MrpI8FNnPqlSGu189UFlJE0OcSzi+VyXOMHD+NBuYag/ZkdpFiOG3uS BvQA33AKt1qCusowtl7AgoybjLYpmNJUXI2ZfInGN8MoQgICExmr/KfqCrQplkZDBoA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796410259; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-Id:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=ZXhjr0LQhmbS8C57jvn9nST5Y85ZMjv8jG1ocXT2M/Y=; b=HyhOBJm+AsQgA0ZyYszrdKQZJoK8Fb1AqKKawTkINwWXPzkHJ/K1It8EQLnqGoxAZM21 0WINcsqzxYttmDIFcXHzHV4kbaJdTI7W9GcD1KxbNePzMuCbdPqbfsy4oWtsG4MFZuQ58 cHdDrAz4ufcOIAP0AaexfsIovV16s1tQVgEwbO+DtFhtMk1m4ZwQfUvsRoQU4ZaLUbc4A NRIDeD3zDgD6l85YoGKWuwKqEejcaAIuUHqaAuC8WqaaDRUpnGd0ud2KAnJBhn4BcPPZS k5UuOQP5ROjYlNLTbXkhLArpmDbCsdzEVRLuj/2zo4+fO9rIXzG2E3BYDtuYDSXGfDA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Q19YsFwe; arc=none smtp.remote-ip=74.125.82.173 Received: from mail-dy1-f173.google.com (mail-dy1-f173.google.com [74.125.82.173]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2CBFE606A32D for ; Fri, 4 Dec 2026 18:50:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2CBFE606A32D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Q19YsFwe Received: by mail-dy1-f173.google.com with SMTP id 5a478bee46e88-386c71feeeaso1632748eec.0 for ; Fri, 04 Dec 2026 10:50:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796410258; x=1797015058; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=ZXhjr0LQhmbS8C57jvn9nST5Y85ZMjv8jG1ocXT2M/Y=; b=Q19YsFwei6/ko6siFnY5u7LG8B8bx1y4J4DtAh+YqHjn0RMZzvBJypj0BP8IOchkQa /k2jw8U618jgMS327rYbpZ9JbWNNkEY4sm5ve2muv/jFwbuf/Op7Dmhua+oj3OXjGUWL BVg8SMuiT94zbSMdeg3LStsw4cz9wogyUgWxWpcQrFCP2B4Zzg+7AiMgihxAO1CWdO7R cNX8Eo2ZL763hzQmKaN0+Hw2wTpLSnQelqkMXPJcWeNrXypnwccW4bMc6CcmdYY/7BfC YQZor4MBFNRDRNon5LibYvoCkqka9jFbBeFZXtYsXQVtx5sWjSBwy3Cha0Zzazgq33og AEJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796410258; x=1797015058; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=ZXhjr0LQhmbS8C57jvn9nST5Y85ZMjv8jG1ocXT2M/Y=; b=YkoSWzgpjDdBy68gftijLzPr5fCD/l8a6mSaA9uXHfKYUkH/d8xUzMcD77HhECR3Jo AOd7uNFXA9xiOlExwNTbz7CDC3evGyPQNyqlPIm1EPzgAk40tf6NoJlOr9N1I9w+Md7J 0bWwWcVAA3TMEmeIp+aJsoOBGN/krEnkUsMkCjHPzPUsRx8ZCKEZOc7TqEaU1JDDAOe3 IJjZZuiCx+2nCjy0lJcJ/l98u35o/w5qh3GBkX2wSVdXZ8TVFl9uMwMohymK/eynqHy8 W27MOoFTh9c1sq7o9VtiboILUFpIHHRLzKxLuhW8XZEHzurtYvaGqpPK08gY4+nLX5AO kTVw== X-Forwarded-Encrypted: i=1; AL+hErEf6O/B+CA4c/BFfTVH1kESRAug6DJTbKmZ5VrsieyFVpAIF0jWswPOS9BsoVGusHXWSuytNSt1Ajrj/A==@vger.kernel.org X-Gm-Message-State: AB4q+uGPBurC3IXrjWmmUWNhoNWFZ1HiJ6vVxMU9sXUdiSXCO0x27PWS bxT3cCJnqtZ+HMy/WEo3s+ZgiOPA+byItTtPfQjOdTKzm5G7hYtKOAAE X-Gm-Gg: AZFdTZcT9hWImldQqMuIYhbUmkd8IgQ/Rj2DI8C3STHJZMYyLt054FDI0kppw8Yl0Cs RgW0wFW2LjT60Ajr8z7b+SHs91BO3/iLK3KkzUckd438hkvjj4+ffSUkIPzaBUZoCzvOd5D9ZM0 p0kHCBYla3lfiAcW3ljU9czacA4W/6FC+qAsYD4TasvS2Mj+alHPaDHZpztNWjwKi4Ki27JNoey LAvCze99mg3Js3FI2+N0QLZxkdoWrce31q8Ftvem2nN8KeNgtb9zhEcxTNmly5bY+wINNQOi5xc 4cHRlW4YmlpW6Zxn6XIq0vGJnb7HiBxXmTFq5BaXXhh0McIK9V2+1FxrlEA7ryFGz/z0E9ZjN46 l2DVKpiXOQxr8Y9xNpyUM1kv3zuPgB80OsZbPvxtlVib0yWq3MSCDo2pu090VKHv2uDVBofQl5l e9mmwJMHgGRMDEnxukWbO04DdkCuzz0xHRUeAFVJzc6KIUpBYAntm+qJMaBo8F8tt4TCaTbe/2M oXq0fQuqk71HjI9SeXx+0+hTsR8/oZ4ImMuScvFu7D5mxBQ+p4JjSx5A9WBrUH723dekpvIS0Lh Af8X/B+B/DbIcJizvp3l5Om8Wc8KFCv4QniawdJrVszurdZYgHN7Dm6AYKJvv94kwyY= X-Received: by 2002:a05:7301:3f0a:b0:387:4f69:3c1a with SMTP id 5a478bee46e88-38752018ef9mr3669963eec.1.1796410258280; Fri, 04 Dec 2026 10:50:58 -0800 (PST) Received: from localhost.localdomain (wood.seclab.cs.ucsb.edu. [128.111.49.37]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-3875218c6a6sm10328311eec.5.2026.12.04.10.50.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 10:50:57 -0800 (PST) From: Praneeth Balasubramanian To: Sean Young , Mauro Carvalho Chehab Cc: Hans Verkuil , Tetsuo Handa , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Praneeth Balasubramanian , stable@vger.kernel.org Subject: [PATCH] media: imon: Always take ictx->lock when changing the IR protocol Date: Fri, 4 Dec 2026 10:50:55 -0800 Message-Id: <20261204185055.1687759-1-praneethb2002@gmail.com> X-Mailer: git-send-email 2.25.1 X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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