From 14629a65e6b3764bfefd145aefcead2f0a1d54c6 Mon Sep 17 00:00:00 2001 From: Luis Chamberlain Date: Mon, 13 Nov 2023 22:13:09 +0800 Subject: [PATCH 1/2] scsi: sd: Add error handling support for add_disk() mainline inclusion from mainline-v5.16-rc1 commit 2a7a891f4c406822801ecd676b076c64de072c9e category: bugfix bugzilla: https://gitee.com/openeuler/kernel/issues/I81XCK Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=2a7a891f4c406822801ecd676b076c64de072c9e ---------------------------------------- We never checked for errors on add_disk() as this function returned void. Now that this is fixed, use the shiny new error handling. As with the error handling for device_add() we follow the same logic and just put the device so that cleanup is done via the scsi_disk_release(). Link: https://lore.kernel.org/r/20211015233028.2167651-2-mcgrof@kernel.org Reviewed-by: Christoph Hellwig Acked-by: Martin K. Petersen Signed-off-by: Luis Chamberlain Signed-off-by: Martin K. Petersen conflict: drivers/scsi/sd.c Signed-off-by: Zhong Jinghua Signed-off-by: Li Nan (cherry picked from commit f172bfbe9506d15a05b4b91e0b80866893c2dba0) --- drivers/scsi/sd.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c index b63262d7abcf..d6ea5bf7e64b 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -3492,7 +3492,13 @@ static int sd_probe(struct device *dev) pm_runtime_set_autosuspend_delay(dev, sdp->host->hostt->rpm_autosuspend_delay); } - device_add_disk(dev, gd, NULL); + + error = device_add_disk_safe(dev, gd, NULL); + if (error) { + put_device(&sdkp->dev); + goto out; + } + blk_delete_region(disk_devt(sdkp->disk), SD_MINORS, sd_default_probe); if (sdkp->capacity) sd_dif_config_host(sdkp); -- Gitee From e0efe8529b6adba74329c9c682e0d1eb5093664b Mon Sep 17 00:00:00 2001 From: Zhong Jinghua Date: Mon, 13 Nov 2023 22:13:10 +0800 Subject: [PATCH 2/2] scsi: sd: Clean up sdkp if device_add_disk() failed hulk inclusion category: bugfix bugzilla: https://gitee.com/openeuler/kernel/issues/I81XCK ---------------------------------------- If device_add_disk fail, we should clear the resources requested by device_add, otherwise sdkp is not be cleared and will cause memory leaks. Fix it by call device_del to clean the sdkp. Fixes: 2a7a891f4c40 ("scsi: sd: Add error handling support for add_disk()") Signed-off-by: Zhong Jinghua Signed-off-by: Li Nan (cherry picked from commit e7afdd19f16a579ae443816fb502bf3b236adffa) --- drivers/scsi/sd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c index d6ea5bf7e64b..1025f659310c 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -3495,7 +3495,7 @@ static int sd_probe(struct device *dev) error = device_add_disk_safe(dev, gd, NULL); if (error) { - put_device(&sdkp->dev); + device_unregister(&sdkp->dev); goto out; } -- Gitee