From 8e4ca6495ecdddc018cafa14f957871aa9e488a9 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 7c8bfb26c995..0a83320deb14 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -3498,7 +3498,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->security) { sdkp->opal_dev = init_opal_dev(sdkp, &sd_sec_submit); -- Gitee From bfcadf1e9ffec0c27f49bfab11b945ec7eeadb30 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 0a83320deb14..5385805e06a8 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -3501,7 +3501,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