[PATCH 2/2] sh: superhyway: use put_device() if device_register fail

From: Arvind Yadav
Date: Wed Mar 07 2018 - 06:04:20 EST


if device_register() returned an error! Always use put_device()
to give up the reference initialized.

Signed-off-by: Arvind Yadav <arvind.yadav.cs@xxxxxxxxx>
---
drivers/sh/superhyway/superhyway.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/drivers/sh/superhyway/superhyway.c b/drivers/sh/superhyway/superhyway.c
index 348836b..98922e7 100644
--- a/drivers/sh/superhyway/superhyway.c
+++ b/drivers/sh/superhyway/superhyway.c
@@ -54,6 +54,7 @@ int superhyway_add_device(unsigned long base, struct superhyway_device *sdev,
struct superhyway_bus *bus)
{
struct superhyway_device *dev = sdev;
+ int ret;

if (!dev) {
dev = kzalloc(sizeof(struct superhyway_device), GFP_KERNEL);
@@ -87,7 +88,11 @@ int superhyway_add_device(unsigned long base, struct superhyway_device *sdev,

superhyway_devices++;

- return device_register(&dev->dev);
+ ret = device_register(&dev->dev);
+ if (ret)
+ put_device(&dev->dev);
+
+ return ret;
}

int superhyway_add_devices(struct superhyway_bus *bus,
@@ -110,8 +115,10 @@ static int __init superhyway_init(void)
int ret;

ret = device_register(&superhyway_bus_device);
- if (unlikely(ret))
+ if (unlikely(ret)) {
+ put_device(&superhyway_bus_device);
return ret;
+ }

for (bus = superhyway_channels; bus->ops; bus++)
ret |= superhyway_scan_bus(bus);
--
1.9.1