nvmet: preserve device path on allocation failure

nvmet_ns_device_path_store() frees the old path before allocating its
replacement, losing the existing configuration if allocation fails.
Allocate the new path before freeing the old one.

Fixes: a07b4970f4 ("nvmet: add a generic NVMe target")
Signed-off-by: Julian Sun <sunjunchao@bytedance.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Keith Busch <kbusch@kernel.org>
This commit is contained in:
Julian Sun 2026-09-08 11:35:18 +08:00 committed by Keith Busch
parent 62b70e6c87
commit 2f271b8121

View File

@ -534,6 +534,7 @@ static ssize_t nvmet_ns_device_path_store(struct config_item *item,
struct nvmet_subsys *subsys = ns->subsys;
size_t len;
int ret;
char *new_path = NULL;
mutex_lock(&subsys->lock);
ret = -EBUSY;
@ -545,12 +546,14 @@ static ssize_t nvmet_ns_device_path_store(struct config_item *item,
if (!len)
goto out_unlock;
kfree(ns->device_path);
ret = -ENOMEM;
ns->device_path = kmemdup_nul(page, len, GFP_KERNEL);
if (!ns->device_path)
new_path = kmemdup_nul(page, len, GFP_KERNEL);
if (!new_path)
goto out_unlock;
kfree(ns->device_path);
ns->device_path = new_path;
mutex_unlock(&subsys->lock);
return count;