From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.176 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796410259; b=4ic7OzYWUU7sh5AqkelRfKiS1RUEeV0okt6tLY8YBW2BGLd6c6aTrm/4ebJz5bdC7nzw W67kO1qPUQb6d0K+HuT+EeVDoXP+8umZ6X+jPCliDeSpVjlRN7Np3y2GhGEHl9ji/Seuk gPxHA+TNDGEuwqmRB5naXPu+x/gOVyissgYC5pM5vBY132huoJRf+itwErSiNwmE7F/bf IHHv8eR8ewmPDK+8HXzsrc0BbIMYSzHw0WaR2biZ8lZw65di1U7kO00O8WbqxvcgxSGYA hTt12PM74Gp2pjsQA6gOAtWd/sZKnTxXggJxygHbT0ePR613ZlnMNQEgqB3MWiOilEA== 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=UFgWKFdk8nMp0Oi8x9x3hmms9SeEZ/EVYYfhoUhz6Ts9lpptTcjxSXC4Rs+3W5yK2EQU 6M2N6R/llcKB2Vo4Zje3jmzsDF9D8VF6iLUYmskKj652DPoBZqIVHWcqAbcgeGtDrNtbW RV4IGkxDPQNHSRsI5EAXTFgCv7eAFJeMNa0wir38ZpuXSFSb8ESjwFqm9+KUCluFjYRVA XdWd6m0TDgJAiUt0uCWVG2UaGvfeqItIK9Xi2hKOJlV6i6s71/Y2mk/A3IM6bZ/vG+OBd ano6aY5PTiIsdp/C6cD9TlkfEXQg9sTvrBS3blpnd74d8uYqQUxR/qJzDhF8X/GOwMw== 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.176 Received: from mail-dy1-f176.google.com (mail-dy1-f176.google.com [74.125.82.176]) (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 22474606A32C for ; Fri, 4 Dec 2026 18:50:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 22474606A32C 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-f176.google.com with SMTP id 5a478bee46e88-38431138772so1817837eec.1 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=QPQfP9HTLZRCdKLLVoxT3ixwfljI+mgltAeovICkjRP7R0RpGmRe/CcGFp9d1cMh2w +AejpbPlNFVrG14EaNcS5RnWX/8MwUop03eVQeUzmkjWa1oJzRaTT9JQ5n1d1dcrADOE 3UwSvl+Z6ysOsY/ncJgOqwnJi+4hoTMnFH4QHIj3fWF+gMt/rOmwlZMY6J0xPTVjneA8 97WNofMrLZcRRcnZhOXCgsftAmQ+vmWlindt2Mq5InhP61ir/b3xV1C2rvikZwkeXi26 ooOeuzUuNnrAOFBRvDzg9DE5opigOepUugaVg25YoiN/vN6fqc1gx6tgi/OoiadLP1wo pNoQ== X-Forwarded-Encrypted: i=1; AL+hErFLwtAw3rtjsFCRlodwHmwqtAw0Dh6RPuNboSx6AqZajAE25z1AfIp1mKkGER3xbSTJeAZV3JgniaNJADI=@vger.kernel.org X-Gm-Message-State: AB4q+uHAxp30bCSCddIQZ6cf00aBuhFFUOnh/udMR104VJ50y46aN8x7 kv69Dy5J9yY3WTZkHhK1w+Bnj/dKh0sxTjvTddzWYdHALGHGL0T4QsnP X-Gm-Gg: AZFdTZceczyfN6I6zUBXJueBoFGm7Z+fP2D0hYEZWaXH6uz8eIc4nAUjfjIKgJF/UN0 LAWoOlgJBtAm8eF9qqITIHJJGomNKaUYUKUg5cKBFzh9AXQwnZ99xiAuspQ0c4/+QTMmAhaLf1N CgQZNm24dA6i3VkDI1MZSvJ33ZQmTVbA/rbqt221YiYR2but2il2saOnb6zUlk8rLGdytBA5pw8 vkWB4Womy2s/okmBCZUMpi08sC9W+wbyKJUn+4u8qtbdgVuyjHfhI6Y41aAxEXRFsNNEsN0NO0S azuu8L3V36etbrXixchI/n+Cd4xNA3MjDtEyNPD3oXv6LBBxsvQXhN63PAyGZpr6taNrvq4bVcq wKoGok1wTcGL2TJhuQsImHrbHKvmNb5TLsNn+FL5kZ0VD2S4IIwqHzaRAq7792g1EsIxqvE5i2t ApmSJ4OjZRMJJkV5+PSjYMQhS3XgRzuvlJHA0EXPiEn86iKW+8Ie1azEjx+Han1hh6p9Rq9E79O 9xwchVdslLVERRJo2THWcST1rkG8aMqfgqpVk543hFk+0S7bGlJSTXdWjMBSk9wGSQJy/h/8vgl Mf/R3A2ba8oGI/686wAO4qIAbmbEGhKas9oS4IjlNqyiirv1NRhTd9atD20hPdJIuKk= 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-kernel@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