Skip to contentSkip to conversation

accel/habanalabs: check for NULL block_device before dereferencing

PATCH New TAKANO Mitsuhiro 1 participant 1 message Nov 28

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 accel/habanalabs: check for NULL block_device before dereferencing

In hl_dio_fd_register(), bd->bd_disk is dereferenced right after reading sb->s_bdev, before bd is checked for NULL. If the file descriptor refers to a file on a non-block-backed filesystem (such as tmpfs or NFS), sb->s_bdev is NULL and dereferencing bd->bd_disk triggers a NULL pointer dereference.

Check bd before dereferencing bd->bd_disk.

Fixes 8cbacc9a2703 ("accel/habanalabs: add NVMe Direct I/O (HLDIO) infrastructure")

---
Verification: compile-tested on x86_64 with CONFIG_DRM_ACCEL_HABANALABS=m and CONFIG_HL_HLDIO=y (make W=1 drivers/accel/habanalabs/common/hldio.o; 0 warnings) and checked with scripts/checkpatch.pl --strict. Checked against linux-next (next-20261127) and lore.kernel.org for duplicates. Not runtime-tested on Habanalabs hardware.

The change and the changelog text were produced with an AI coding assistant (Gemini) working from the tree; I reviewed the diff and the reasoning, and I am responsible for the submission.

---
 drivers/accel/habanalabs/common/hldio.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/accel/habanalabs/common/hldio.c +3 -3
index c33c817a9..aa3b38be9 100644
@@ -94,8 +94,6 @@ static int hl_dio_fd_register(struct hl_ctx *ctx, int fd, struct hl_dio_fd *f)
 
 	inode = file_inode(f->filp);
 	sb = inode->i_sb;
-	bd = sb->s_bdev;
-	gd = bd->bd_disk;
 
 	if (inode->i_blocks << sb->s_blocksize_bits < i_size_read(inode)) {
 		dev_err(hdev->dev, "sparse files are not currently supported\n");
@@ -103,11 +101,13 @@ static int hl_dio_fd_register(struct hl_ctx *ctx, int fd, struct hl_dio_fd *f)
 		goto fput;
 	}
 
-	if (!bd || !gd) {
+	bd = sb->s_bdev;
+	if (!bd || !bd->bd_disk) {
 		dev_err(hdev->dev, "invalid block device\n");
 		rc = -ENODEV;
 		goto fput;
 	}
+	gd = bd->bd_disk;
 	/* Get the underlying device from the block device */
 	disk_dev = disk_to_dev(gd);
 	if (!dma_pci_p2pdma_supported(disk_dev)) {

---
base-commit: f93eb7cfc4bfa5d46340f360873726e3e77f2365
change-id: 20261128-b4-hldio-null-bdev-79a0ab174f01

Best regards,

Signature · 1 line
TAKANO Mitsuhiro <[email protected]>