[PATCH] rpmb: fix error path in rpmb_dev_register()

Jens Wiklander
2024-08-30T06:14:17Z

Until this patch was rpmb_dev_register() always freeing rdev in the
error path. However, past device_register() it must not do that since
the memory is now managed by the device even if it failed to register
properly. So fix this by doing a device_put() before returning the error
code.

Fixes the smatch warning:
drivers/misc/rpmb-core.c:204 rpmb_dev_register()
warn: freeing device managed memory (leak): 'rdev'

Fixes: 1e9046e3a154 ("rpmb: add Replay Protected Memory Block (RPMB) subsystem")
Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
---
drivers/misc/rpmb-core.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/misc/rpmb-core.c b/drivers/misc/rpmb-core.c
index c8888267c222..9d41404f8b91 100644
--- a/drivers/misc/rpmb-core.c
+++ b/drivers/misc/rpmb-core.c
@@ -187,8 +187,10 @@ struct rpmb_dev *rpmb_dev_register(struct device *dev,
rdev->dev.parent = dev;

ret = device_register(&rdev->dev);
- if (ret)
- goto err_id_remove;
+ if (ret) {
+ put_device(&rdev->dev);
+ return ERR_PTR(ret);
+ }

dev_dbg(&rdev->dev, "registered device\n");

--
2.34.1