Skip to contentSkip to conversation

accel/habanalabs: publish mmap buffers after allocation

PATCH New Ruoyu Wang 2 participants 2 messages Jun 17

Subsystems (2)
lore
Share & tools
lore

Ruoyu Wang

author
#0 lore raw

PATCH 1/1 accel/habanalabs: publish mmap buffers after allocation

hl_mmap_mem_buf_alloc() inserts a new buffer into the memory manager IDR before initializing the descriptor and before running the behavior allocation callback.

Handles from this IDR are used by hl_mmap_mem_buf_get() and the mmap path. A racing lookup can therefore take the buffer kref or inspect behavior-private state before the buffer is ready.

Initialize the descriptor, run the behavior allocation callback, and only then publish the object in the IDR. If IDR allocation fails, release the behavior-private resources before freeing the descriptor.

---
 drivers/accel/habanalabs/common/memory_mgr.c | 30 ++++++++++----------
 1 file changed, 15 insertions(+), 15 deletions(-)
drivers/accel/habanalabs/common/memory_mgr.c +15 -15
index 9fdd34acf389f..b5b92dbbe5ddd 100644
@@ -156,6 +156,17 @@ hl_mmap_mem_buf_alloc(struct hl_mem_mgr *mmg,
 	if (!buf)
 		return NULL;
 
+	buf->mmg = mmg;
+	buf->behavior = behavior;
+	kref_init(&buf->refcount);
+
+	rc = buf->behavior->alloc(buf, gfp, args);
+	if (rc) {
+		dev_err(mmg->dev, "%s: Failure in buffer alloc callback %d\n",
+			behavior->topic, rc);
+		goto free_buf;
+	}
+
 	spin_lock(&mmg->lock);
 	rc = idr_alloc(&mmg->handles, buf, 1, 0, GFP_ATOMIC);
 	spin_unlock(&mmg->lock);
@@ -163,27 +174,16 @@ hl_mmap_mem_buf_alloc(struct hl_mem_mgr *mmg,
 		dev_err(mmg->dev,
 			"%s: Failed to allocate IDR for a new buffer, rc=%d\n",
 			behavior->topic, rc);
-		goto free_buf;
+		goto destroy_buf;
 	}
 
-	buf->mmg = mmg;
-	buf->behavior = behavior;
 	buf->handle = (((u64)rc | buf->behavior->mem_id) << PAGE_SHIFT);
-	kref_init(&buf->refcount);
-
-	rc = buf->behavior->alloc(buf, gfp, args);
-	if (rc) {
-		dev_err(mmg->dev, "%s: Failure in buffer alloc callback %d\n",
-			behavior->topic, rc);
-		goto remove_idr;
-	}
 
 	return buf;
 
-remove_idr:
-	spin_lock(&mmg->lock);
-	idr_remove(&mmg->handles, lower_32_bits(buf->handle >> PAGE_SHIFT));
-	spin_unlock(&mmg->lock);
+destroy_buf:
+	hl_mmap_mem_buf_destroy(buf);
+	return NULL;
 free_buf:
 	kfree(buf);
 	return NULL;
Signature · 1 line
2.51.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Use-After-Free and IDR corruption due to uninitialized buf->handle during concurrent access. --

commit 9226a7a050a5049d6df96f0ca7493dec14b9b65d
Author: Ruoyu Wang <[email protected]>

accel/habanalabs: publish mmap buffers after allocation

This commit attempts to prevent racing lookups on newly allocated mmap buffers by initializing the descriptor and running the behavior allocation callback before publishing the object in the IDR.

Quoted 4 lines
drivers/accel/habanalabs/common/memory_mgr.c +0 -0
index 9fdd34acf389f..b5b92dbbe5ddd 100644

[ ... ]

Quoted 1 line
drivers/accel/habanalabs/common/memory_mgr.c +0 -0
@@ -156,6 +156,17 @@ hl_mmap_mem_buf_alloc(struct hl_mem_mgr *mmg,

[ ... ]

Quoted 4 lines
drivers/accel/habanalabs/common/memory_mgr.c +0 -0
 	spin_lock(&mmg->lock);
 	rc = idr_alloc(&mmg->handles, buf, 1, 0, GFP_ATOMIC);
 	spin_unlock(&mmg->lock);

drivers/accel/habanalabs/common/memory_mgr.c hl_mmap_mem_buf_alloc(struct hl_mem_mgr *mmg, · line 180

Quoted 10 lines
drivers/accel/habanalabs/common/memory_mgr.c +1 -3
@@ -163,27 +174,16 @@ hl_mmap_mem_buf_alloc(struct hl_mem_mgr *mmg,
 		dev_err(mmg->dev,
 			"%s: Failed to allocate IDR for a new buffer, rc=%d\n",
 			behavior->topic, rc);
-		goto free_buf;
+		goto destroy_buf;
 	}
 
-	buf->mmg = mmg;
-	buf->behavior = behavior;
 	buf->handle = (((u64)rc | buf->behavior->mem_id) << PAGE_SHIFT);

[Severity: High]
This is a pre-existing issue, but does assigning buf->handle after releasing mmg->lock leave a window for use-after-free and IDR corruption?

If a concurrent thread discovers this buffer in the IDR via a lookup and then triggers a destruction path before buf->handle is set, the destruction path will use the uninitialized buf->handle (which is 0 from kzalloc) to calculate the IDR index:

hl_mmap_mem_buf_remove_idr_locked() {
    idr_remove(&buf->mmg->handles, lower_32_bits(buf->handle >> PAGE_SHIFT));
}

Because buf->handle is 0, this removes index 0 instead of the actual IDR index. The actual IDR entry is left pointing to the buffer, which is then freed by hl_mmap_mem_buf_destroy().

When the allocating thread resumes, it will write to buf->handle, causing a write-after-free, and leaving a dangling pointer in the IDR.

Could buf->handle be assigned while still holding mmg->lock to ensure the handle is fully initialized before the buffer is visible to other threads?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1