From: Wenkai Lin <linwenkai6@hisilicon.com>
In uacce_register(), if cdev_device_add() fails, we should properly
decrease the reference count of the cdev kobject and set uacce->cdev
to NULL to avoid potential use-after-free or double free issues.
This change adds proper error handling after cdev_device_add() fails,
ensuring that kobject_put() is called and uacce->cdev is cleared.
Fixes: 015d239ac014 ("uacce: add uacce driver")
Cc: stable@vger.kernel.org
Signed-off-by: Wenkai Lin <linwenkai6@hisilicon.com>
Signed-off-by: Chenghai Huang <huangchenghai2@huawei.com>
---
drivers/misc/uacce/uacce.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/drivers/misc/uacce/uacce.c b/drivers/misc/uacce/uacce.c
index 42e7d2a2a90c..9b82a6731832 100644
--- a/drivers/misc/uacce/uacce.c
+++ b/drivers/misc/uacce/uacce.c
@@ -519,6 +519,8 @@ EXPORT_SYMBOL_GPL(uacce_alloc);
*/
int uacce_register(struct uacce_device *uacce)
{
+ int ret;
+
if (!uacce)
return -ENODEV;
@@ -529,7 +531,14 @@ int uacce_register(struct uacce_device *uacce)
uacce->cdev->ops = &uacce_fops;
uacce->cdev->owner = THIS_MODULE;
- return cdev_device_add(uacce->cdev, &uacce->dev);
+ ret = cdev_device_add(uacce->cdev, &uacce->dev);
+ if (ret) {
+ kobject_put(&uacce->cdev->kobj);
+ uacce->cdev = NULL;
+ return ret;
+ }
+
+ return 0;
}
EXPORT_SYMBOL_GPL(uacce_register);
--
2.33.0
On Wed, 22 Oct 2025 at 10:12, Chenghai Huang <huangchenghai2@huawei.com> wrote:
>
> From: Wenkai Lin <linwenkai6@hisilicon.com>
>
> In uacce_register(), if cdev_device_add() fails, we should properly
> decrease the reference count of the cdev kobject and set uacce->cdev
> to NULL to avoid potential use-after-free or double free issues.
>
> This change adds proper error handling after cdev_device_add() fails,
> ensuring that kobject_put() is called and uacce->cdev is cleared.
>
> Fixes: 015d239ac014 ("uacce: add uacce driver")
> Cc: stable@vger.kernel.org
> Signed-off-by: Wenkai Lin <linwenkai6@hisilicon.com>
> Signed-off-by: Chenghai Huang <huangchenghai2@huawei.com>
> ---
> drivers/misc/uacce/uacce.c | 11 ++++++++++-
> 1 file changed, 10 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/misc/uacce/uacce.c b/drivers/misc/uacce/uacce.c
> index 42e7d2a2a90c..9b82a6731832 100644
> --- a/drivers/misc/uacce/uacce.c
> +++ b/drivers/misc/uacce/uacce.c
> @@ -519,6 +519,8 @@ EXPORT_SYMBOL_GPL(uacce_alloc);
> */
> int uacce_register(struct uacce_device *uacce)
> {
> + int ret;
> +
> if (!uacce)
> return -ENODEV;
>
> @@ -529,7 +531,14 @@ int uacce_register(struct uacce_device *uacce)
> uacce->cdev->ops = &uacce_fops;
> uacce->cdev->owner = THIS_MODULE;
>
> - return cdev_device_add(uacce->cdev, &uacce->dev);
> + ret = cdev_device_add(uacce->cdev, &uacce->dev);
> + if (ret) {
> + kobject_put(&uacce->cdev->kobj);
Not understanding this,
What if kobject_put(&p->kobj) is called twice?
cdev_device_add would call cdev_del->kobject_put(&p->kobj) as well.
If you think cdev_device_add has an issue, maybe fixing
cdev_device_add itself is better?
Thanks
在 2025/10/22 14:17, Zhangfei Gao 写道:
> On Wed, 22 Oct 2025 at 10:12, Chenghai Huang <huangchenghai2@huawei.com> wrote:
>> From: Wenkai Lin <linwenkai6@hisilicon.com>
>>
>> In uacce_register(), if cdev_device_add() fails, we should properly
>> decrease the reference count of the cdev kobject and set uacce->cdev
>> to NULL to avoid potential use-after-free or double free issues.
>>
>> This change adds proper error handling after cdev_device_add() fails,
>> ensuring that kobject_put() is called and uacce->cdev is cleared.
>>
>> Fixes: 015d239ac014 ("uacce: add uacce driver")
>> Cc: stable@vger.kernel.org
>> Signed-off-by: Wenkai Lin <linwenkai6@hisilicon.com>
>> Signed-off-by: Chenghai Huang <huangchenghai2@huawei.com>
>> ---
>> drivers/misc/uacce/uacce.c | 11 ++++++++++-
>> 1 file changed, 10 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/misc/uacce/uacce.c b/drivers/misc/uacce/uacce.c
>> index 42e7d2a2a90c..9b82a6731832 100644
>> --- a/drivers/misc/uacce/uacce.c
>> +++ b/drivers/misc/uacce/uacce.c
>> @@ -519,6 +519,8 @@ EXPORT_SYMBOL_GPL(uacce_alloc);
>> */
>> int uacce_register(struct uacce_device *uacce)
>> {
>> + int ret;
>> +
>> if (!uacce)
>> return -ENODEV;
>>
>> @@ -529,7 +531,14 @@ int uacce_register(struct uacce_device *uacce)
>> uacce->cdev->ops = &uacce_fops;
>> uacce->cdev->owner = THIS_MODULE;
>>
>> - return cdev_device_add(uacce->cdev, &uacce->dev);
>> + ret = cdev_device_add(uacce->cdev, &uacce->dev);
>> + if (ret) {
>> + kobject_put(&uacce->cdev->kobj);
> Not understanding this,
> What if kobject_put(&p->kobj) is called twice?
> cdev_device_add would call cdev_del->kobject_put(&p->kobj) as well.
>
> If you think cdev_device_add has an issue, maybe fixing
> cdev_device_add itself is better?
>
> Thanks
After more testing, a new conclusion was reached.
After the cdev_set_parent function in cdev_device_add is called,
the cdev and the device will share the same lifetime and the memory
of the cdev is managed by the device.
So when put_device is called in the uacce_remove, the memory of the cdev
will also be released, so there is no memory leak issue in the code.
In addition, when cdev_device_add fails, a flag should be set to detect
this situation to prevent the uacce_remove from incorrectly calling
cdev_device_del to release the unallocated resources.
This issue will be fixed in the next patch.
Thanks,
Wenkai
在 2025/10/22 14:17, Zhangfei Gao 写道:
> On Wed, 22 Oct 2025 at 10:12, Chenghai Huang <huangchenghai2@huawei.com> wrote:
>> From: Wenkai Lin <linwenkai6@hisilicon.com>
>>
>> In uacce_register(), if cdev_device_add() fails, we should properly
>> decrease the reference count of the cdev kobject and set uacce->cdev
>> to NULL to avoid potential use-after-free or double free issues.
>>
>> This change adds proper error handling after cdev_device_add() fails,
>> ensuring that kobject_put() is called and uacce->cdev is cleared.
>>
>> Fixes: 015d239ac014 ("uacce: add uacce driver")
>> Cc: stable@vger.kernel.org
>> Signed-off-by: Wenkai Lin <linwenkai6@hisilicon.com>
>> Signed-off-by: Chenghai Huang <huangchenghai2@huawei.com>
>> ---
>> drivers/misc/uacce/uacce.c | 11 ++++++++++-
>> 1 file changed, 10 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/misc/uacce/uacce.c b/drivers/misc/uacce/uacce.c
>> index 42e7d2a2a90c..9b82a6731832 100644
>> --- a/drivers/misc/uacce/uacce.c
>> +++ b/drivers/misc/uacce/uacce.c
>> @@ -519,6 +519,8 @@ EXPORT_SYMBOL_GPL(uacce_alloc);
>> */
>> int uacce_register(struct uacce_device *uacce)
>> {
>> + int ret;
>> +
>> if (!uacce)
>> return -ENODEV;
>>
>> @@ -529,7 +531,14 @@ int uacce_register(struct uacce_device *uacce)
>> uacce->cdev->ops = &uacce_fops;
>> uacce->cdev->owner = THIS_MODULE;
>>
>> - return cdev_device_add(uacce->cdev, &uacce->dev);
>> + ret = cdev_device_add(uacce->cdev, &uacce->dev);
>> + if (ret) {
>> + kobject_put(&uacce->cdev->kobj);
> Not understanding this,
> What if kobject_put(&p->kobj) is called twice?
> cdev_device_add would call cdev_del->kobject_put(&p->kobj) as well.
>
> If you think cdev_device_add has an issue, maybe fixing
> cdev_device_add itself is better?
>
> Thanks
Regarding your concern, cdev_device_add() use cdev_del rolls back
its cdev_add operations (add the refcount to 2) upon failure when
dev->devt is not null, the refcount of cdev is still 1 after the
rollback, so it does not free the memory of the cdev structure itself.
Therefore, in the error path of uacce_register, we still need to
manually call kobject_put to decrement the refcount of the kobject to
zero within cdev and free it.
Thanks,
Wenkai
© 2016 - 2026 Red Hat, Inc.