[PATCH] nvme-pci: After the memory is released, the corresponding pointer needs to point to empty

Liu Jing posted 1 patch 1 month ago
drivers/nvme/host/pci.c | 4 ++++
1 file changed, 4 insertions(+)
[PATCH] nvme-pci: After the memory is released, the corresponding pointer needs to point to empty
Posted by Liu Jing 1 month ago
The code frees memory pointed to by dev->queues. After freeing memory,
dev->queues should be set to NULL to avoid dangling Pointers.

Signed-off-by: Liu Jing <liujing@cmss.chinamobile.com>
---
 drivers/nvme/host/pci.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c
index 1cbff7537788..c1500c00f571 100644
--- a/drivers/nvme/host/pci.c
+++ b/drivers/nvme/host/pci.c
@@ -2731,7 +2731,9 @@ static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
 	nvme_free_tagset(dev);
 	put_device(dev->dev);
 	kfree(dev->queues);
+	dev->queues = NULL;
 	kfree(dev);
+	dev = NULL;
 }
 
 static void nvme_reset_work(struct work_struct *work)
@@ -3055,8 +3057,10 @@ static struct nvme_dev *nvme_pci_alloc_dev(struct pci_dev *pdev,
 out_put_device:
 	put_device(dev->dev);
 	kfree(dev->queues);
+	dev->queues = NULL;
 out_free_dev:
 	kfree(dev);
+	dev = NULL;
 	return ERR_PTR(ret);
 }
 
-- 
2.27.0
Re: [PATCH] nvme-pci: After the memory is released, the corresponding pointer needs to point to empty
Posted by Christoph Hellwig 1 month ago
On Tue, Oct 22, 2024 at 03:01:50AM +0800, Liu Jing wrote:
> The code frees memory pointed to by dev->queues. After freeing memory,
> dev->queues should be set to NULL to avoid dangling Pointers.

How claims that?  Clearing pointers clearly defeats redzoning in the
slab allocator so actually is counterproductive.