Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Praneeth Balasubramanian <praneethb2002@gmail.com>
To: Konstantin Sinyuk <ksinyuk@kernel.org>,
	Koby Elbaz <elbaz.koby@gmail.com>,
	Oded Gabbay <ogabbay@kernel.org>
Cc: Praneeth Balasubramanian <praneethb2002@gmail.com>,
	stable@vger.kernel.org, dri-devel@lists.freedesktop.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH] accel/habanalabs: Fix timestamp record release on reset
Date: Fri,  4 Dec 2026 22:36:51 -0800	[thread overview]
Message-ID: <20261205063651.2523441-1-praneethb2002@gmail.com> (raw)

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.

Do what unregister_timestamp_node() does: under the lock, take the
pointers, unlink the record and clear in_use. Then drop the lock and put
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:
    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. 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    | 22 ++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/drivers/accel/habanalabs/common/command_submission.c b/drivers/accel/habanalabs/common/command_submission.c
index 8a3febc2089e..8bff77bf42f8 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,24 @@ 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);
+
+		/*
+		 * As in unregister_timestamp_node(), 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


                 reply	other threads:[~2026-12-05  6:36 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261205063651.2523441-1-praneethb2002@gmail.com \
    --to=praneethb2002@gmail.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=elbaz.koby@gmail.com \
    --cc=ksinyuk@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ogabbay@kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox