From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.181 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796459464; b=WtDI+I/ogVChbsHNax4AujvTK63EpRpq12xtfcISRNkvJHhi4RQ6xpT7M8hUBNEmCu8E qat+rOJ7SqHCNNOwzjCcf2IzGL3FZypAQkTfcfj1QWn21cHHY6iRAvZHmU8EbtRF3/XdT sg6z5xpmBbOM86qhTTntul4wq8B6dsdweOXrtl7M639UgyOFpbEs0GEyVo2Wp64FVyspy Gb92Co1s6hdB2chn03iGUA0MvHRU5qrrvj843qXJX3j44b5sQ+0Ky4umFLoVS5nfRJ7U9 JhqRlxJL+CFh0Cpv1HcxEt/cJbsDPk5O3w6pOSqzg9AOqonfIoo8X50tRIjgr/Md9wA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796459464; 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=b7NS5VjWHBGqszmq1yLLDbVRIEpdd9nMEt4Sq6/h+nE=; b=1UZR2BgzYtAKJWDmwkdhJFkzp9E8hc6WB9Mi9kK+0PlqiRKS8/6DesyDjmIU9hR/Ip7e fn9hzsS1fcr32rHuQ82Exh6X59sZzLIBgDKU7nbSLK+YSFFSb94+NtrviqPmAvzWTwYGC 3Vxo/8YjXKkYdEUjDVt9CPHQUTLPfiAXSrRWH+w4sb529Cs3r6gTFf5XEspIxvQYFj0Qv 6x8TJcLGsPtd2lUs2uKtxVRSg5V1D3vHToBMk47nClKIBnHTLeD0JIKB1kgESQDC2jj8I RKsuZ3pxf0BAsH85HON0OyZE9ZuBfNWmK+qreAt8nkCO8AA2J7CkyeCDC0xsWl2U5OA== 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=Ynz4TGL8; arc=none smtp.remote-ip=74.125.82.181 Received: from mail-dy1-f181.google.com (mail-dy1-f181.google.com [74.125.82.181]) (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 01361606A32F for ; Sat, 5 Dec 2026 08:31:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 01361606A32F 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=Ynz4TGL8 Received: by mail-dy1-f181.google.com with SMTP id 5a478bee46e88-386c71feeeaso2198650eec.0 for ; Sat, 05 Dec 2026 00:31:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796459463; x=1797064263; 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=b7NS5VjWHBGqszmq1yLLDbVRIEpdd9nMEt4Sq6/h+nE=; b=Ynz4TGL8e1mHfEwE3H1nlGUUrgRzHnuX6+6ThNqlzwJb7vMFzbMpYwHW0aiWoHV2hv zqR4vz2mKkCVOl2m1CoeFH+COfz71gAeXZqcEwrUl5XGd0n7d71b0bpYNclq0P6bl3Bi mMLSE7rL4f6EEfQ3I4YBzxA1ihzG1AS5mruotK1vq1kLKIG/4ibC5A2DCeOy6/eAJ11L XMTC2+7aB7AvqEUlnhb44tX7orKdanGAWeSitQJgTsTIoWkAyDZj6GLWeNtcoN2u/PMc bookajce1hvDxlSDiwbiPHlZVh1rfJY2Mq6x2WU7FfNLqCNHnAbc69zQ6sIvqMZfu80Y 8DpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796459463; x=1797064263; 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=b7NS5VjWHBGqszmq1yLLDbVRIEpdd9nMEt4Sq6/h+nE=; b=xRg8Gn8Q/VSdARJRkXIcy4kum/q8fSzd5Wo1WDcBRgCTDFlw1fkwGoc8g19Q6+T51a BU6bAM2iKU28PyZQHzeEewafgAuWiBV3A/U1LQzTUAqlkqJRI69ks5JMZWrYz5GY2IEj s9fovumBLzNcvrMG/ynCIA4lUY7N/7ELlG4goo68FY5s+TrITzIXWxV2iBPD4OjIT7ru 0LKnIGKSEduoNKZoejWliuI7W0cV//VvQAgTbR1lubgGkEGpvYSOz7Mty96CwREQd22H tEK2samCvcIhFXs+w/GIZZQLFmBU/kZlL7/6ShWha0qRVdGqvWEFL35qFhcBsNBsjd/X wf6A== X-Forwarded-Encrypted: i=1; AL+hErELQ0IAWoR5sDXE981L5QctEchoJ8FwhyxMgcXddSKgI4e/SBNqLv3/p7sdiBP2awUvjQcEzq9/n8y/LG0=@vger.kernel.org X-Gm-Message-State: APuU4Z+O78O5XJRLq9I0otS8H1FCq3tF5YqeAfueWU98fedZvP98Ig+5 svfWJRvm6mhCVLJBFCza67FrDKH7meO1DgO2x7eBMjzGgwovt5qKdg01 X-Gm-Gg: AZFdTZds5uD1sk2dKN3GEqzINLQUroR6yysulQYk0ivdPDms697xVm5Zs6CBpcN12bC TQGopZOKxqSXn4qmz/xv0aDq27VnkGbTdZgtadD1ya8PFxeqyT/VzLvt79Bh1M7e/dog5zxt7FM X+8fqua/oalyVD+cA6uYGKLS/yiueP2E7RPrxNsz9YkHeLqYW52ltGFAXg3dWKgYiEsh501ee4u Rr5eGaOx3nw6xfNE7Mod497tEH1PpItZWI/2cp2z31w+H/vh0ECIkpYiROSEEac1me33sofXSP4 WehzwmDVFXb0r3gETlOMrHR9JMdtCyuJRtPuFDb+22zAWxYiOp2/Laru+ZOLOoOYypIQEqbu5St YQS1B5JvMvZx1yzEzH6M20yWQDtzBReItHcSS5B0SUmixK+QIbXjUm81oEwq1Lp9dvCkVxfX44L vErSVG0UZWfxGrlZa3d5p6irbNecfxtMDL2WlG4jyHxPBMJgu4wIWi9kJ3VilFcTTxP4suWyA6v j1JODFor7SDsSCeUIcAXFFQQyHsKFTRCS9IO8tOUY9kLDZZKaB358IJJdHxajYUSY6Rwn40sAid dFyKeP26H+EMVcc5423TEfh3yAKExEnt/ISMEOprfiwfYqmXydmDe92kIixGrlhVS7M= X-Received: by 2002:a05:693c:884f:b0:386:de41:be4e with SMTP id 5a478bee46e88-387521de421mr4299802eec.27.1796459462653; Sat, 05 Dec 2026 00:31:02 -0800 (PST) Received: from localhost.localdomain (wood.seclab.cs.ucsb.edu. [128.111.49.37]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-3875220c35asm16840767eec.22.2026.12.05.00.31.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 00:31:02 -0800 (PST) From: Praneeth Balasubramanian To: Konstantin Sinyuk , Koby Elbaz , Oded Gabbay Cc: Praneeth Balasubramanian , stable@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] accel/habanalabs: Fix timestamp record release on reset Date: Sat, 5 Dec 2026 00:30:56 -0800 Message-Id: <20261205083056.2602922-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 On reset, wake_pending_user_interrupt_threads() releases the pending timestamp registrations. For each record, under ts_list_lock, it unlinks the record, puts its timestamp buffer and then puts its CQ buffer. This has three problems: - The record is part of the kernel buffer of the timestamp buffer. If that put drops the last reference, the buffer is freed, and reading pend->ts_reg_info.cq_cb for the next put is a use-after-free. - The last put frees the buffer with vfree(), which may sleep, while the spinlock is held with interrupts off. The interrupt handler path defers these puts to a workqueue for this reason. - ts_reg_info.in_use stays set. A compute reset can leave the process running. If it then registers the same timestamp offset again, unregister_timestamp_node() sees in_use, unlinks the record a second time and puts both buffers again. Under the lock, save the two buffer pointers, unlink the record and clear in_use. Then drop the lock and put the references. The pointers are saved first because the first put may free the record. unregister_timestamp_node() also clears in_use and unlinks the record under the lock before putting the references. This was found by an LLM-based audit that checks code against the rules stated in kernel comments. Fixes: 9158bf69e74f ("habanalabs: Timestamps buffers registration") Fixes: 1157b5d6b3b0 ("accel/habanalabs: optimize timestamp registration handler") Cc: stable@vger.kernel.org Signed-off-by: Praneeth Balasubramanian --- Notes: Changes in v2: - Rewrote the comment and the changelog. v1 said the code does what unregister_timestamp_node() does, which is only partly true (Sashiko). No code change apart from the comment. v1: https://lore.kernel.org/all/20261205063651.2523441-1-praneethb2002@gmail.com/ This was found by an LLM-based audit, part of research at UCSB SecLab. An LLM turned a kernel comment into a rule, and three LLM votes checked the code against it. The fix and changelog were drafted with GLM-5.3, then checked against the source and partly rewritten with Claude Opus 5.5. The v2 changes were made with Claude Opus 5.5 after it checked the Sashiko review against the source. I reviewed the patch and take responsibility for it. Testing: builds without new warnings with W=1 (x86_64 allmodconfig), clang 23. checkpatch.pl is clean. Not runtime-tested, and there is no reproducer. I do not have this hardware. The kernel-doc of hl_mmap_mem_buf_put() says it "Shall be called from an interrupt context", but its release may sleep, as the comment above handle_registration_node() in irq.c says. It may be meant to say "not". This patch does not change that comment. .../habanalabs/common/command_submission.c | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/drivers/accel/habanalabs/common/command_submission.c b/drivers/accel/habanalabs/common/command_submission.c index 8a3febc2089e..e823d3553e68 100644 --- a/drivers/accel/habanalabs/common/command_submission.c +++ b/drivers/accel/habanalabs/common/command_submission.c @@ -1098,6 +1098,8 @@ static void wake_pending_user_interrupt_threads(struct hl_user_interrupt *interrupt) { struct hl_user_pending_interrupt *pend, *temp; + struct hl_mmap_mem_buf *buf; + struct hl_cb *cq_cb; unsigned long flags; spin_lock_irqsave(&interrupt->wait_list_lock, flags); @@ -1108,10 +1110,23 @@ wake_pending_user_interrupt_threads(struct hl_user_interrupt *interrupt) spin_unlock_irqrestore(&interrupt->wait_list_lock, flags); spin_lock_irqsave(&interrupt->ts_list_lock, flags); - list_for_each_entry_safe(pend, temp, &interrupt->ts_list_head, list_node) { + while (!list_empty(&interrupt->ts_list_head)) { + pend = list_first_entry(&interrupt->ts_list_head, + struct hl_user_pending_interrupt, list_node); + buf = pend->ts_reg_info.buf; + cq_cb = pend->ts_reg_info.cq_cb; list_del(&pend->list_node); - hl_mmap_mem_buf_put(pend->ts_reg_info.buf); - hl_cb_put(pend->ts_reg_info.cq_cb); + pend->ts_reg_info.in_use = false; + spin_unlock_irqrestore(&interrupt->ts_list_lock, flags); + + /* + * Put the references without the lock: the release may + * sleep, and it may free the buffer that holds pend. + */ + hl_mmap_mem_buf_put(buf); + hl_cb_put(cq_cb); + + spin_lock_irqsave(&interrupt->ts_list_lock, flags); } spin_unlock_irqrestore(&interrupt->ts_list_lock, flags); } base-commit: 29beb21c21ada7207692b2d69fc58dcd15dab6e9 -- 2.25.1