This is a note to let you know that I've just added the patch titled RDMA/srp: Rework the srp_add_port() error path to the 5.15-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: rdma-srp-rework-the-srp_add_port-error-path.patch and it can be found in the queue-5.15 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. commit 8bc9d2fad66e4f5fafb78d84c52d9b9fee98f33d Author: Bart Van Assche <bvanassche@xxxxxxx> Date: Thu Aug 25 14:38:57 2022 -0700 RDMA/srp: Rework the srp_add_port() error path [ Upstream commit c8e4c23976554fb9dda1658bd1a3914b202815cd ] device_register() always calls device_initialize() so calling device_del() is safe even if device_register() fails. Implement the following advice from the comment block above device_register(): "NOTE: _Never_ directly free @dev after calling this function, even if it returned an error! Always use put_device() to give up the reference initialized in this function instead." Keep the kfree() call in the error path since srp_release_dev() does not free the host. Link: https://lore.kernel.org/r/20220825213900.864587-2-bvanassche@xxxxxxx Signed-off-by: Bart Van Assche <bvanassche@xxxxxxx> Signed-off-by: Leon Romanovsky <leon@xxxxxxxxxx> Stable-dep-of: b05398aff9ad ("RDMA/srp: Support more than 255 rdma ports") Signed-off-by: Sasha Levin <sashal@xxxxxxxxxx> diff --git a/drivers/infiniband/ulp/srp/ib_srp.c b/drivers/infiniband/ulp/srp/ib_srp.c index 2f4991cea98c..1435d375a6ad 100644 --- a/drivers/infiniband/ulp/srp/ib_srp.c +++ b/drivers/infiniband/ulp/srp/ib_srp.c @@ -3900,20 +3900,19 @@ static struct srp_host *srp_add_port(struct srp_device *device, u8 port) port); if (device_register(&host->dev)) - goto free_host; + goto put_host; if (device_create_file(&host->dev, &dev_attr_add_target)) - goto err_class; + goto put_host; if (device_create_file(&host->dev, &dev_attr_ibdev)) - goto err_class; + goto put_host; if (device_create_file(&host->dev, &dev_attr_port)) - goto err_class; + goto put_host; return host; -err_class: - device_unregister(&host->dev); - -free_host: +put_host: + device_del(&host->dev); + put_device(&host->dev); kfree(host); return NULL;