The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH v2] accel/habanalabs: Fix timestamp record release on reset
@ 2026-12-05  8:30 Praneeth Balasubramanian
  0 siblings, 0 replies; only message in thread
From: Praneeth Balasubramanian @ 2026-12-05  8:30 UTC (permalink / raw)
  To: Konstantin Sinyuk, Koby Elbaz, Oded Gabbay
  Cc: Praneeth Balasubramanian, stable, dri-devel, linux-kernel

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 <praneethb2002@gmail.com>
---

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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-05  8:31 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05  8:30 [PATCH v2] accel/habanalabs: Fix timestamp record release on reset Praneeth Balasubramanian

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox