[PATCH 2/2] rapidio: rio-scan: fix error handling in rio_scan_alloc_net()

From: Yang Yingliang
Date: Sat Nov 12 2022 - 08:01:49 EST


This patch fixes two error handling issues:

rio_free_net() calls device_unregister(), if allocate idtab fails
in rio_scan_alloc_net(), the device has not been registered, in
this error case, can not call rio_free_net(), use kfree() instead.

If rio_add_net() returns error in rio_scan_alloc_net(), it's need
call put_device() to free memory allocated in rio_alloc_net() and
dev_set_name().

Fixes: e6b585ca6e81 ("rapidio: move net allocation into core code")
Signed-off-by: Yang Yingliang <yangyingliang@xxxxxxxxxx>
---
drivers/rapidio/rio-scan.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/rapidio/rio-scan.c b/drivers/rapidio/rio-scan.c
index 19b0c33f4a62..2350c0cb2329 100644
--- a/drivers/rapidio/rio-scan.c
+++ b/drivers/rapidio/rio-scan.c
@@ -850,7 +850,7 @@ static struct rio_net *rio_scan_alloc_net(struct rio_mport *mport,

if (idtab == NULL) {
pr_err("RIO: failed to allocate destID table\n");
- rio_free_net(net);
+ kfree(net);
net = NULL;
} else {
net->enum_data = idtab;
@@ -867,7 +867,10 @@ static struct rio_net *rio_scan_alloc_net(struct rio_mport *mport,
dev_set_name(&net->dev, "rnet_%d", net->id);
net->dev.parent = &mport->dev;
net->dev.release = rio_scan_release_dev;
- rio_add_net(net);
+ if (rio_add_net(net)) {
+ put_device(&net->dev);
+ net = NULL;
+ }
}

return net;
--
2.25.1