Skip to contentSkip to conversation

mmc: fix return value check of mmc_add_host()

PATCH In review Yang Yingliang 2 participants 11 messages Nov 1, 2022 – Nov 8, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover mmc: fix return value check of mmc_add_host()

mmc_add_host() may return error, but it was ignored in some drivers, if we ignore it, the memory that allocated in mmc_alloc_host() will be leaked and it will lead a kernel crash because of deleting not added device in the remove path.

So fix this by checking the return value and calling mmc_free_host() in error path.

Yang Yingliang (9):
mmc: alcor: fix return value check of mmc_add_host()
mmc: moxart: fix return value check of mmc_add_host()
mmc: mxcmmc: fix return value check of mmc_add_host()
mmc: pxamci: fix return value check of mmc_add_host()
mmc: rtsx_pci: fix return value check of mmc_add_host()
mmc: rtsx_usb_sdmmc: fix return value check of mmc_add_host()
mmc: toshsd: fix return value check of mmc_add_host()
mmc: vub300: fix return value check of mmc_add_host()
mmc: wmt-sdmmc: fix return value check of mmc_add_host()

 drivers/mmc/host/alcor.c          |  5 ++++-
 drivers/mmc/host/moxart-mmc.c     |  4 +++-
 drivers/mmc/host/mxcmmc.c         |  4 +++-
 drivers/mmc/host/pxamci.c         |  7 ++++++-
 drivers/mmc/host/rtsx_pci_sdmmc.c |  9 ++++++++-
 drivers/mmc/host/rtsx_usb_sdmmc.c | 11 ++++++++++-
 drivers/mmc/host/toshsd.c         |  6 +++++-
 drivers/mmc/host/vub300.c         | 11 ++++++++---
 drivers/mmc/host/wmt-sdmmc.c      |  6 +++++-
 9 files changed, 52 insertions(+), 11 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/9 mmc: alcor: fix return value check of mmc_add_host()

mmc_add_host() may return error, if we ignore its return value, the memory that allocated in mmc_alloc_host() will be leaked and it will lead a kernel crash because of deleting not added device in the remove path.

So fix this by checking the return value and calling mmc_free_host() in the error path.

Fixes c5413ad815a6 ("mmc: add new Alcor Micro Cardreader SD/MMC driver")
---
 drivers/mmc/host/alcor.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/mmc/host/alcor.c +4 -1
index bfb8efeb7eb8..d01df01d4b4d 100644
@@ -1114,7 +1114,10 @@ static int alcor_pci_sdmmc_drv_probe(struct platform_device *pdev)
 	alcor_hw_init(host);
 
 	dev_set_drvdata(&pdev->dev, host);
-	mmc_add_host(mmc);
+	ret = mmc_add_host(mmc);
+	if (ret)
+		goto free_host;
+
 	return 0;
 
 free_host:
Signature · 1 line
2.25.1
#2 lore raw

PATCH 2/9 mmc: moxart: fix return value check of mmc_add_host()

In reply to Yang Yingliang

mmc_add_host() may return error, if we ignore its return value, the memory that allocated in mmc_alloc_host() will be leaked and it will lead a kernel crash because of deleting not added device in the remove path.

So fix this by checking the return value and goto error path which will call mmc_free_host().

Fixes 1b66e94e6b99 ("mmc: moxart: Add MOXA ART SD/MMC driver")
---
 drivers/mmc/host/moxart-mmc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/mmc/host/moxart-mmc.c +3 -1
index dfc3ffd5b1f8..52ed30f2d9f4 100644
@@ -665,7 +665,9 @@ static int moxart_probe(struct platform_device *pdev)
 		goto out;
 
 	dev_set_drvdata(dev, mmc);
-	mmc_add_host(mmc);
+	ret = mmc_add_host(mmc);
+	if (ret)
+		goto out;
 
 	dev_dbg(dev, "IRQ=%d, FIFO is %d bytes\n", irq, host->fifo_width);
 
Signature · 1 line
2.25.1
#3 lore raw

PATCH 3/9 mmc: mxcmmc: fix return value check of mmc_add_host()

In reply to Yang Yingliang

mmc_add_host() may return error, if we ignore its return value, the memory that allocated in mmc_alloc_host() will be leaked and it will lead a kernel crash because of deleting not added device in the remove path.

So fix this by checking the return value and goto error path which will call mmc_free_host().

Fixes d96be879ff46 ("mmc: Add a MX2/MX3 specific SDHC driver")
---
 drivers/mmc/host/mxcmmc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/mmc/host/mxcmmc.c +3 -1
index 2cf0413407ea..668f865f3efb 100644
@@ -1143,7 +1143,9 @@ static int mxcmci_probe(struct platform_device *pdev)
 
 	timer_setup(&host->watchdog, mxcmci_watchdog, 0);
 
-	mmc_add_host(mmc);
+	ret = mmc_add_host(mmc);
+	if (ret)
+		goto out_free_dma;
 
 	return 0;
 
Signature · 1 line
2.25.1
#4 lore raw

PATCH 4/9 mmc: pxamci: fix return value check of mmc_add_host()

In reply to Yang Yingliang

mmc_add_host() may return error, if we ignore its return value, the memory that allocated in mmc_alloc_host() will be leaked and it will lead a kernel crash because of deleting not added device in the remove path.

So fix this by checking the return value and goto error path which will call mmc_free_host(), besides, ->exit() need be called to uninit the pdata.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 drivers/mmc/host/pxamci.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/mmc/host/pxamci.c +6 -1
index e4003f6058eb..2a988f942b6c 100644
@@ -763,7 +763,12 @@ static int pxamci_probe(struct platform_device *pdev)
 			dev_warn(dev, "gpio_ro and get_ro() both defined\n");
 	}
 
-	mmc_add_host(mmc);
+	ret = mmc_add_host(mmc);
+	if (ret) {
+		if (host->pdata && host->pdata->exit)
+			host->pdata->exit(dev, mmc);
+		goto out;
+	}
 
 	return 0;
 
Signature · 1 line
2.25.1
#5 lore raw

PATCH 5/9 mmc: rtsx_pci: fix return value check of mmc_add_host()

In reply to Yang Yingliang

mmc_add_host() may return error, if we ignore its return value, the memory that allocated in mmc_alloc_host() will be leaked and it will lead a kernel crash because of deleting not added device in the remove path.

So fix this by checking the return value and calling mmc_free_host() in the error path, beside, runtime PM also needs be disabled.

Fixes ff984e57d36e ("mmc: Add realtek pcie sdmmc host driver")
---
 drivers/mmc/host/rtsx_pci_sdmmc.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/mmc/host/rtsx_pci_sdmmc.c +8 -1
index e1580f78c6b2..8098726dcc0b 100644
@@ -1474,6 +1474,7 @@ static int rtsx_pci_sdmmc_drv_probe(struct platform_device *pdev)
 	struct realtek_pci_sdmmc *host;
 	struct rtsx_pcr *pcr;
 	struct pcr_handle *handle = pdev->dev.platform_data;
+	int ret;
 
 	if (!handle)
 		return -ENXIO;
@@ -1511,7 +1512,13 @@ static int rtsx_pci_sdmmc_drv_probe(struct platform_device *pdev)
 	pm_runtime_mark_last_busy(&pdev->dev);
 	pm_runtime_use_autosuspend(&pdev->dev);
 
-	mmc_add_host(mmc);
+	ret = mmc_add_host(mmc);
+	if (ret) {
+		pm_runtime_dont_use_autosuspend(&pdev->dev);
+		pm_runtime_disable(&pdev->dev);
+		mmc_free_host(mmc);
+		return ret;
+	}
 
 	return 0;
 }
Signature · 1 line
2.25.1
#6 lore raw

PATCH 6/9 mmc: rtsx_usb_sdmmc: fix return value check of mmc_add_host()

In reply to Yang Yingliang

mmc_add_host() may return error, if we ignore its return value, the memory that allocated in mmc_alloc_host() will be leaked and it will lead a kernel crash because of deleting not added device in the remove path.

So fix this by checking the return value and calling mmc_free_host() in the error path, besides, led_classdev_unregister() and pm_runtime_disable() also need be called.

Fixes c7f6558d84af ("mmc: Add realtek USB sdmmc host driver")
---
 drivers/mmc/host/rtsx_usb_sdmmc.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/mmc/host/rtsx_usb_sdmmc.c +10 -1
index 5798aee06653..2c650cd58693 100644
@@ -1329,6 +1329,7 @@ static int rtsx_usb_sdmmc_drv_probe(struct platform_device *pdev)
 #ifdef RTSX_USB_USE_LEDS_CLASS
 	int err;
 #endif
+	int ret;
 
 	ucr = usb_get_intfdata(to_usb_interface(pdev->dev.parent));
 	if (!ucr)
@@ -1365,7 +1366,15 @@ static int rtsx_usb_sdmmc_drv_probe(struct platform_device *pdev)
 	INIT_WORK(&host->led_work, rtsx_usb_update_led);
 
 #endif
-	mmc_add_host(mmc);
+	ret = mmc_add_host(mmc);
+	if (ret) {
+#ifdef RTSX_USB_USE_LEDS_CLASS
+		led_classdev_unregister(&host->led);
+#endif
+		mmc_free_host(mmc);
+		pm_runtime_disable(&pdev->dev);
+		return ret;
+	}
 
 	return 0;
 }
Signature · 1 line
2.25.1
#7 lore raw

PATCH 7/9 mmc: toshsd: fix return value check of mmc_add_host()

In reply to Yang Yingliang

mmc_add_host() may return error, if we ignore its return value, the memory that allocated in mmc_alloc_host() will be leaked and it will lead a kernel crash because of deleting not added device in the remove path.

So fix this by checking the return value and goto error path which will call mmc_free_host(), besides, free_irq() also needs be called.

Fixes a5eb8bbd66cc ("mmc: add Toshiba PCI SD controller driver")
---
 drivers/mmc/host/toshsd.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/mmc/host/toshsd.c +5 -1
index 8d037c2071ab..497791ffada6 100644
@@ -651,7 +651,9 @@ static int toshsd_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 	if (ret)
 		goto unmap;
 
-	mmc_add_host(mmc);
+	ret = mmc_add_host(mmc);
+	if (ret)
+		goto free_irq;
 
 	base = pci_resource_start(pdev, 0);
 	dev_dbg(&pdev->dev, "MMIO %pa, IRQ %d\n", &base, pdev->irq);
@@ -660,6 +662,8 @@ static int toshsd_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 
 	return 0;
 
+free_irq:
+	free_irq(pdev->irq, host);
 unmap:
 	pci_iounmap(pdev, host->ioaddr);
 release:
Signature · 1 line
2.25.1
#8 lore raw

PATCH 8/9 mmc: vub300: fix return value check of mmc_add_host()

In reply to Yang Yingliang

mmc_add_host() may return error, if we ignore its return value, the memory that allocated in mmc_alloc_host() will be leaked and it will lead a kernel crash because of deleting not added device in the remove path.

So fix this by checking the return value and goto error path which will call mmc_free_host(), besides, the timer added before mmc_add_host() needs be del.

And this patch fixes another missing call mmc_free_host() if usb_control_msg() fails.

Fixes 88095e7b473a ("mmc: Add new VUB300 USB-to-SD/SDIO/MMC driver")
---
 drivers/mmc/host/vub300.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/mmc/host/vub300.c +8 -3
index 97beece62fec..ab36ec479747 100644
@@ -2299,14 +2299,14 @@ static int vub300_probe(struct usb_interface *interface,
 				0x0000, 0x0000, &vub300->system_port_status,
 				sizeof(vub300->system_port_status), 1000);
 	if (retval < 0) {
-		goto error4;
+		goto error5;
 	} else if (sizeof(vub300->system_port_status) == retval) {
 		vub300->card_present =
 			(0x0001 & vub300->system_port_status.port_flags) ? 1 : 0;
 		vub300->read_only =
 			(0x0010 & vub300->system_port_status.port_flags) ? 1 : 0;
 	} else {
-		goto error4;
+		goto error5;
 	}
 	usb_set_intfdata(interface, vub300);
 	INIT_DELAYED_WORK(&vub300->pollwork, vub300_pollwork_thread);
@@ -2329,8 +2329,13 @@ static int vub300_probe(struct usb_interface *interface,
 			 "USB vub300 remote SDIO host controller[%d]"
 			 "connected with no SD/SDIO card inserted\n",
 			 interface_to_InterfaceNumber(interface));
-	mmc_add_host(mmc);
+	retval = mmc_add_host(mmc);
+	if (retval)
+		goto error6;
+
 	return 0;
+error6:
+	del_timer_sync(&vub300->inactivity_timer);
 error5:
 	mmc_free_host(mmc);
 	/*
Signature · 1 line
2.25.1
#9 lore raw

PATCH 9/9 mmc: wmt-sdmmc: fix return value check of mmc_add_host()

In reply to Yang Yingliang

mmc_add_host() may return error, if we ignore its return value, the memory that allocated in mmc_alloc_host() will be leaked and it will lead a kernel crash because of deleting not added device in the remove path.

So fix this by checking the return value and goto error path which will call mmc_free_host(), besides, clk_disable_unprepare() also needs be called.

Fixes 3a96dff0f828 ("mmc: SD/MMC Host Controller for Wondermedia WM8505/WM8650")
---
 drivers/mmc/host/wmt-sdmmc.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/mmc/host/wmt-sdmmc.c +5 -1
index 9b5c503e3a3f..9aa3027ca25e 100644
@@ -856,11 +856,15 @@ static int wmt_mci_probe(struct platform_device *pdev)
 	/* configure the controller to a known 'ready' state */
 	wmt_reset_hardware(mmc);
 
-	mmc_add_host(mmc);
+	ret = mmc_add_host(mmc);
+	if (ret)
+		goto fail7;
 
 	dev_info(&pdev->dev, "WMT SDHC Controller initialized\n");
 
 	return 0;
+fail7:
+	clk_disable_unprepare(priv->clk_sdmmc);
 fail6:
 	clk_put(priv->clk_sdmmc);
 fail5_and_a_half:
Signature · 1 line
2.25.1
#10 lore raw
In reply to Yang Yingliang
Quoted 20 lines · Yang Yingliang

mmc_add_host() may return error, but it was ignored in some drivers, if we ignore it, the memory that allocated in mmc_alloc_host() will be leaked and it will lead a kernel crash because of deleting not added device in the remove path.

So fix this by checking the return value and calling mmc_free_host() in error path.

Yang Yingliang (9):
mmc: alcor: fix return value check of mmc_add_host()
mmc: moxart: fix return value check of mmc_add_host()
mmc: mxcmmc: fix return value check of mmc_add_host()
mmc: pxamci: fix return value check of mmc_add_host()
mmc: rtsx_pci: fix return value check of mmc_add_host()
mmc: rtsx_usb_sdmmc: fix return value check of mmc_add_host()
mmc: toshsd: fix return value check of mmc_add_host()
mmc: vub300: fix return value check of mmc_add_host()
mmc: wmt-sdmmc: fix return value check of mmc_add_host()

Quoted 11 lines
 drivers/mmc/host/alcor.c          |  5 ++++-
 drivers/mmc/host/moxart-mmc.c     |  4 +++-
 drivers/mmc/host/mxcmmc.c         |  4 +++-
 drivers/mmc/host/pxamci.c         |  7 ++++++-
 drivers/mmc/host/rtsx_pci_sdmmc.c |  9 ++++++++-
 drivers/mmc/host/rtsx_usb_sdmmc.c | 11 ++++++++++-
 drivers/mmc/host/toshsd.c         |  6 +++++-
 drivers/mmc/host/vub300.c         | 11 ++++++++---
 drivers/mmc/host/wmt-sdmmc.c      |  6 +++++-
 9 files changed, 52 insertions(+), 11 deletions(-)

Applied for next, thanks!

Kind regards
Uffe