]> www.infradead.org Git - nvme.git/commitdiff
nvme: Add error path for xa_store in nvme_init_effects
authorKeisuke Nishimura <keisuke.nishimura@inria.fr>
Mon, 16 Dec 2024 15:27:20 +0000 (16:27 +0100)
committerKeith Busch <kbusch@kernel.org>
Sun, 12 Jan 2025 22:11:29 +0000 (14:11 -0800)
The xa_store() may fail due to memory allocation failure because there
is no guarantee that the index NVME_CSI_NVM is already used. This fix
introduces a new function to handle the error path.

Fixes: cc115cbe12d9 ("nvme: always initialize known command effects")
Signed-off-by: Keisuke Nishimura <keisuke.nishimura@inria.fr>
Reviewed-by: Sagi Grimberg <sagi@grimberg.me>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Keith Busch <kbusch@kernel.org>
drivers/nvme/host/core.c

index 4bdd5144af7c4ea9fc422b6b86fe128d6c663729..2a0555856795e60ca6daf3b98bd92a5a5edde6a3 100644 (file)
@@ -3175,6 +3175,25 @@ free_data:
        return ret;
 }
 
+static int nvme_init_effects_log(struct nvme_ctrl *ctrl,
+               u8 csi, struct nvme_effects_log **log)
+{
+       struct nvme_effects_log *effects, *old;
+
+       effects = kzalloc(sizeof(*effects), GFP_KERNEL);
+       if (effects)
+               return -ENOMEM;
+
+       old = xa_store(&ctrl->cels, csi, effects, GFP_KERNEL);
+       if (xa_is_err(old)) {
+               kfree(effects);
+               return xa_err(old);
+       }
+
+       *log = effects;
+       return 0;
+}
+
 static void nvme_init_known_nvm_effects(struct nvme_ctrl *ctrl)
 {
        struct nvme_effects_log *log = ctrl->effects;
@@ -3221,10 +3240,9 @@ static int nvme_init_effects(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
        }
 
        if (!ctrl->effects) {
-               ctrl->effects = kzalloc(sizeof(*ctrl->effects), GFP_KERNEL);
-               if (!ctrl->effects)
-                       return -ENOMEM;
-               xa_store(&ctrl->cels, NVME_CSI_NVM, ctrl->effects, GFP_KERNEL);
+               ret = nvme_init_effects_log(ctrl, NVME_CSI_NVM, &ctrl->effects);
+               if (ret < 0)
+                       return ret;
        }
 
        nvme_init_known_nvm_effects(ctrl);