Commit 2b8d2053 authored by Keisuke Nishimura's avatar Keisuke Nishimura Committed by Jingtong Meng
Browse files

nvme: Add error path for xa_store in nvme_init_effects

stable inclusion
from stable-v6.6.76
commit 525dc0f60469fb315d900d7ebcc30a01384e9ad9
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/IBS4SF

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?h=linux-6.6.y&id=525dc0f60469fb315d900d7ebcc30a01384e9ad9



--------------------------------

[ Upstream commit d4a95adeabc6b5a39405e49c6d5ed14dd83682c4 ]

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: cc115cbe ("nvme: always initialize known command effects")
Signed-off-by: default avatarKeisuke Nishimura <keisuke.nishimura@inria.fr>
Reviewed-by: default avatarSagi Grimberg <sagi@grimberg.me>
Reviewed-by: default avatarChristoph Hellwig <hch@lst.de>
Signed-off-by: default avatarKeith Busch <kbusch@kernel.org>
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
Signed-off-by: default avatarJingtong Meng <jingtong.meng@windriver.com>
parent 9864ae0b
Loading
Loading
Loading
Loading
+22 −4
Original line number Diff line number Diff line
@@ -2945,6 +2945,25 @@ static int nvme_init_non_mdts_limits(struct nvme_ctrl *ctrl)
	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;
@@ -2991,10 +3010,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);