[PATCH RFC v2 10/14] nvme: Pin a ctrl ref to every CDQ
From: Joel Granados
Date: Fri Jul 24 2026 - 07:23:53 EST
Make sure that the migration manager controller pointed by the CDQ does
not get freed unless all CDQs are terminated by user space.
Signed-off-by: Joel Granados <joel.granados@xxxxxxxxxx>
---
drivers/nvme/host/cdq.h | 23 +++++++++++++++--------
1 file changed, 15 insertions(+), 8 deletions(-)
diff --git a/drivers/nvme/host/cdq.h b/drivers/nvme/host/cdq.h
index aa9469af0c7ef08b9f8ebf93e468c7f892aee4b1..a8c714ab5fada4325463013c2ffb23a117cd8423 100644
--- a/drivers/nvme/host/cdq.h
+++ b/drivers/nvme/host/cdq.h
@@ -29,7 +29,7 @@ struct nvme_cdq_chunk {
struct cdq_nvme_queue {
u16 id;
- struct nvme_ctrl *ctrl;
+ struct nvme_ctrl *ctrl; // this is kref'ed for the life of the CDQ
u32 size_nbyte;
u16 mc_id; // migratable controller id
@@ -49,11 +49,20 @@ struct cdq_nvme_queue {
struct kref ref;
};
-/* Must not touch cdq->ctrl: Ctrl may have been freed */
-static inline void nvme_free_cdq(struct kref *ref)
-{
- kfree(container_of(ref, struct cdq_nvme_queue, ref));
-}
+/*
+ * nvme_free_cdq:
+ * - Is called with the cdq struct has no more krefs.
+ * - Will free (cdq) and decrease ctrl kref.
+ */
+void nvme_free_cdq(struct kref *ref);
+/* nvme_cdq_delete:
+ * - Sends a nvme delete cmd to the controller
+ * - removes (cdq) from controller xarray
+ * - frees the backing CDQ mem
+ * - Calls nvme_free_cdq if there are no more refs
+ */
+void nvme_delete_cdq(struct cdq_nvme_queue *cdq);
+int nvme_create_cdq(struct nvme_ctrl *ctrl, const u32 entry_nr, const u16 mc_id);
static inline void nvme_cdq_get(struct cdq_nvme_queue *cdq)
{
@@ -65,8 +74,6 @@ static inline void nvme_cdq_put(struct cdq_nvme_queue *cdq)
kref_put(&cdq->ref, nvme_free_cdq);
}
-void nvme_delete_cdq(struct cdq_nvme_queue *cdq);
-int nvme_create_cdq(struct nvme_ctrl *ctrl, const u32 entry_nr, const u16 mc_id);
void nvme_delete_cdqs_host(struct nvme_ctrl *ctrl);
void nvme_free_cdqs(struct nvme_ctrl *ctrl);
--
2.50.1