* [PATCH v3 0/4] uacce: driver fixes for memory leaks and state management
@ 2025-10-21 13:49 Chenghai Huang
2025-10-21 13:50 ` [PATCH v3 1/4] uacce: fix for cdev memory leak Chenghai Huang
` (3 more replies)
0 siblings, 4 replies; 6+ messages in thread
From: Chenghai Huang @ 2025-10-21 13:49 UTC (permalink / raw)
To: gregkh, zhangfei.gao, wangzhou1
Cc: linux-kernel, linux-crypto, fanghao11, shenyang39, liulongfang,
qianweili, linwenkai6
This patch series addresses several issues in the uacce:
1.Memory leak fix when device registration fails.
2.Fix sysfs file creation conditions.
3.Add error reporting for unsupported mremap operations.
4.Ensuring safe queue release with proper state management.
---
Changes in v3:
- Move the checks for the 'isolate_strategy_show' and
'isolate_strategy_store' functions to their respective call sites.
- Use kobject_put to release the cdev memory instead of modifying
cdev to be a static structure member.
- Link to v2: https://lore.kernel.org/all/20250916144811.1799687-1-huangchenghai2@huawei.com/
Changes in v2:
- Use cdev_init to allocate cdev memory to ensure that memory leaks
are avoided.
- Supplement the reason for intercepting the remapping operation.
- Add "cc: stable@vger.kernel.org" to paths with fixed.
- Link to v1: https://lore.kernel.org/all/20250822103904.3776304-1-huangchenghai2@huawei.com/
Chenghai Huang (2):
uacce: fix isolate sysfs check condition
uacce: ensure safe queue release with state management
Wenkai Lin (1):
uacce: fix for cdev memory leak
Yang Shen (1):
uacce: implement mremap in uacce_vm_ops to return -EPERM
drivers/misc/uacce/uacce.c | 55 ++++++++++++++++++++++++++++++--------
1 file changed, 44 insertions(+), 11 deletions(-)
--
2.33.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v3 1/4] uacce: fix for cdev memory leak
2025-10-21 13:49 [PATCH v3 0/4] uacce: driver fixes for memory leaks and state management Chenghai Huang
@ 2025-10-21 13:50 ` Chenghai Huang
2025-10-21 13:50 ` [PATCH v3 2/4] uacce: fix isolate sysfs check condition Chenghai Huang
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Chenghai Huang @ 2025-10-21 13:50 UTC (permalink / raw)
To: gregkh, zhangfei.gao, wangzhou1
Cc: linux-kernel, linux-crypto, fanghao11, shenyang39, liulongfang,
qianweili, linwenkai6
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v3 2/4] uacce: fix isolate sysfs check condition
2025-10-21 13:49 [PATCH v3 0/4] uacce: driver fixes for memory leaks and state management Chenghai Huang
2025-10-21 13:50 ` [PATCH v3 1/4] uacce: fix for cdev memory leak Chenghai Huang
@ 2025-10-21 13:50 ` Chenghai Huang
2025-10-22 1:34 ` huangchenghai
2025-10-21 13:50 ` [PATCH v3 3/4] uacce: implement mremap in uacce_vm_ops to return -EPERM Chenghai Huang
2025-10-21 13:50 ` [PATCH v3 4/4] uacce: ensure safe queue release with state management Chenghai Huang
3 siblings, 1 reply; 6+ messages in thread
From: Chenghai Huang @ 2025-10-21 13:50 UTC (permalink / raw)
To: gregkh, zhangfei.gao, wangzhou1
Cc: linux-kernel, linux-crypto, fanghao11, shenyang39, liulongfang,
qianweili, linwenkai6
uacce supports the device isolation feature. If the driver
implements the isolate_err_threshold_read and
isolate_err_threshold_write callback functions, uacce will create
sysfs files now. Users can read and configure the isolation policy
through sysfs. Currently, sysfs files are created as long as either
isolate_err_threshold_read or isolate_err_threshold_write callback
functions are present.
However, accessing a non-existent callback function may cause the
system to crash. Therefore, check whether the function exists
before calling isolate_err_threshold_read or
isolate_err_threshold_write.
Fixes: e3e289fbc0b5 ("uacce: supports device isolation feature")
Cc: stable@vger.kernel.org
Signed-off-by: Chenghai Huang <huangchenghai2@huawei.com>
---
drivers/misc/uacce/uacce.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/drivers/misc/uacce/uacce.c b/drivers/misc/uacce/uacce.c
index 9b82a6731832..e3433d95640a 100644
--- a/drivers/misc/uacce/uacce.c
+++ b/drivers/misc/uacce/uacce.c
@@ -382,6 +382,9 @@ static ssize_t isolate_strategy_show(struct device *dev, struct device_attribute
struct uacce_device *uacce = to_uacce_device(dev);
u32 val;
+ if (!uacce->ops->isolate_err_threshold_read)
+ return -ENOENT;
+
val = uacce->ops->isolate_err_threshold_read(uacce);
return sysfs_emit(buf, "%u\n", val);
@@ -394,6 +397,9 @@ static ssize_t isolate_strategy_store(struct device *dev, struct device_attribut
unsigned long val;
int ret;
+ if (!uacce->ops->isolate_err_threshold_write)
+ return -ENOENT;
+
if (kstrtoul(buf, 0, &val) < 0)
return -EINVAL;
@@ -440,9 +446,7 @@ static umode_t uacce_dev_is_visible(struct kobject *kobj,
(!uacce->qf_pg_num[UACCE_QFRT_DUS])))
return 0;
- if (attr == &dev_attr_isolate_strategy.attr &&
- (!uacce->ops->isolate_err_threshold_read &&
- !uacce->ops->isolate_err_threshold_write))
+ if (attr == &dev_attr_isolate_strategy.attr)
return 0;
if (attr == &dev_attr_isolate.attr && !uacce->ops->get_isolate_state)
--
2.33.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v3 3/4] uacce: implement mremap in uacce_vm_ops to return -EPERM
2025-10-21 13:49 [PATCH v3 0/4] uacce: driver fixes for memory leaks and state management Chenghai Huang
2025-10-21 13:50 ` [PATCH v3 1/4] uacce: fix for cdev memory leak Chenghai Huang
2025-10-21 13:50 ` [PATCH v3 2/4] uacce: fix isolate sysfs check condition Chenghai Huang
@ 2025-10-21 13:50 ` Chenghai Huang
2025-10-21 13:50 ` [PATCH v3 4/4] uacce: ensure safe queue release with state management Chenghai Huang
3 siblings, 0 replies; 6+ messages in thread
From: Chenghai Huang @ 2025-10-21 13:50 UTC (permalink / raw)
To: gregkh, zhangfei.gao, wangzhou1
Cc: linux-kernel, linux-crypto, fanghao11, shenyang39, liulongfang,
qianweili, linwenkai6
From: Yang Shen <shenyang39@huawei.com>
The current uacce_vm_ops does not support the mremap operation of
vm_operations_struct. Implement .mremap to return -EPERM to remind
users.
The reason we need to explicitly disable mremap is that when the
driver does not implement .mremap, it uses the default mremap
method. This could lead to a risk scenario:
An application might first mmap address p1, then mremap to p2,
followed by munmap(p1), and finally munmap(p2). Since the default
mremap copies the original vma's vm_private_data (i.e., q) to the
new vma, both munmap operations would trigger vma_close, causing
q->qfr to be freed twice(qfr will be set to null here, so repeated
release is ok).
Fixes: 015d239ac014 ("uacce: add uacce driver")
Cc: stable@vger.kernel.org
Signed-off-by: Yang Shen <shenyang39@huawei.com>
Signed-off-by: Chenghai Huang <huangchenghai2@huawei.com>
---
drivers/misc/uacce/uacce.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/drivers/misc/uacce/uacce.c b/drivers/misc/uacce/uacce.c
index e3433d95640a..747efb2d36f5 100644
--- a/drivers/misc/uacce/uacce.c
+++ b/drivers/misc/uacce/uacce.c
@@ -214,8 +214,14 @@ static void uacce_vma_close(struct vm_area_struct *vma)
}
}
+static int uacce_vma_mremap(struct vm_area_struct *area)
+{
+ return -EPERM;
+}
+
static const struct vm_operations_struct uacce_vm_ops = {
.close = uacce_vma_close,
+ .mremap = uacce_vma_mremap,
};
static int uacce_fops_mmap(struct file *filep, struct vm_area_struct *vma)
--
2.33.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v3 4/4] uacce: ensure safe queue release with state management
2025-10-21 13:49 [PATCH v3 0/4] uacce: driver fixes for memory leaks and state management Chenghai Huang
` (2 preceding siblings ...)
2025-10-21 13:50 ` [PATCH v3 3/4] uacce: implement mremap in uacce_vm_ops to return -EPERM Chenghai Huang
@ 2025-10-21 13:50 ` Chenghai Huang
3 siblings, 0 replies; 6+ messages in thread
From: Chenghai Huang @ 2025-10-21 13:50 UTC (permalink / raw)
To: gregkh, zhangfei.gao, wangzhou1
Cc: linux-kernel, linux-crypto, fanghao11, shenyang39, liulongfang,
qianweili, linwenkai6
Directly calling `put_queue` carries risks since it cannot
guarantee that resources of `uacce_queue` have been fully released
beforehand. So adding a `stop_queue` operation for the
UACCE_CMD_PUT_Q command and leaving the `put_queue` operation to
the final resource release ensures safety.
Queue states are defined as follows:
- UACCE_Q_ZOMBIE: Initial state
- UACCE_Q_INIT: After opening `uacce`
- UACCE_Q_STARTED: After `start` is issued via `ioctl`
When executing `poweroff -f` in virt while accelerator are still
working, `uacce_fops_release` and `uacce_remove` may execute
concurrently. This can cause `uacce_put_queue` within
`uacce_fops_release` to access a NULL `ops` pointer. Therefore, add
state checks to prevent accessing freed pointers.
Fixes: 015d239ac014 ("uacce: add uacce driver")
Cc: stable@vger.kernel.org
Signed-off-by: Chenghai Huang <huangchenghai2@huawei.com>
Signed-off-by: Yang Shen <shenyang39@huawei.com>
---
drivers/misc/uacce/uacce.c | 28 +++++++++++++++++++++-------
1 file changed, 21 insertions(+), 7 deletions(-)
diff --git a/drivers/misc/uacce/uacce.c b/drivers/misc/uacce/uacce.c
index 747efb2d36f5..cfebca4c0012 100644
--- a/drivers/misc/uacce/uacce.c
+++ b/drivers/misc/uacce/uacce.c
@@ -40,20 +40,34 @@ static int uacce_start_queue(struct uacce_queue *q)
return 0;
}
-static int uacce_put_queue(struct uacce_queue *q)
+static int uacce_stop_queue(struct uacce_queue *q)
{
struct uacce_device *uacce = q->uacce;
- if ((q->state == UACCE_Q_STARTED) && uacce->ops->stop_queue)
+ if (q->state != UACCE_Q_STARTED)
+ return 0;
+
+ if (uacce->ops->stop_queue)
uacce->ops->stop_queue(q);
- if ((q->state == UACCE_Q_INIT || q->state == UACCE_Q_STARTED) &&
- uacce->ops->put_queue)
+ q->state = UACCE_Q_INIT;
+
+ return 0;
+}
+
+static void uacce_put_queue(struct uacce_queue *q)
+{
+ struct uacce_device *uacce = q->uacce;
+
+ uacce_stop_queue(q);
+
+ if (q->state != UACCE_Q_INIT)
+ return;
+
+ if (uacce->ops->put_queue)
uacce->ops->put_queue(q);
q->state = UACCE_Q_ZOMBIE;
-
- return 0;
}
static long uacce_fops_unl_ioctl(struct file *filep,
@@ -80,7 +94,7 @@ static long uacce_fops_unl_ioctl(struct file *filep,
ret = uacce_start_queue(q);
break;
case UACCE_CMD_PUT_Q:
- ret = uacce_put_queue(q);
+ ret = uacce_stop_queue(q);
break;
default:
if (uacce->ops->ioctl)
--
2.33.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v3 2/4] uacce: fix isolate sysfs check condition
2025-10-21 13:50 ` [PATCH v3 2/4] uacce: fix isolate sysfs check condition Chenghai Huang
@ 2025-10-22 1:34 ` huangchenghai
0 siblings, 0 replies; 6+ messages in thread
From: huangchenghai @ 2025-10-22 1:34 UTC (permalink / raw)
To: gregkh, zhangfei.gao, wangzhou1
Cc: linux-kernel, linux-crypto, fanghao11, shenyang39, liulongfang,
qianweili, linwenkai6
在 2025/10/21 21:50, Chenghai Huang 写道:
> uacce supports the device isolation feature. If the driver
> implements the isolate_err_threshold_read and
> isolate_err_threshold_write callback functions, uacce will create
> sysfs files now. Users can read and configure the isolation policy
> through sysfs. Currently, sysfs files are created as long as either
> isolate_err_threshold_read or isolate_err_threshold_write callback
> functions are present.
>
> However, accessing a non-existent callback function may cause the
> system to crash. Therefore, check whether the function exists
> before calling isolate_err_threshold_read or
> isolate_err_threshold_write.
>
> Fixes: e3e289fbc0b5 ("uacce: supports device isolation feature")
> Cc: stable@vger.kernel.org
> Signed-off-by: Chenghai Huang <huangchenghai2@huawei.com>
> ---
> drivers/misc/uacce/uacce.c | 10 +++++++---
> 1 file changed, 7 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/misc/uacce/uacce.c b/drivers/misc/uacce/uacce.c
> index 9b82a6731832..e3433d95640a 100644
> --- a/drivers/misc/uacce/uacce.c
> +++ b/drivers/misc/uacce/uacce.c
> @@ -382,6 +382,9 @@ static ssize_t isolate_strategy_show(struct device *dev, struct device_attribute
> struct uacce_device *uacce = to_uacce_device(dev);
> u32 val;
>
> + if (!uacce->ops->isolate_err_threshold_read)
> + return -ENOENT;
> +
> val = uacce->ops->isolate_err_threshold_read(uacce);
>
> return sysfs_emit(buf, "%u\n", val);
> @@ -394,6 +397,9 @@ static ssize_t isolate_strategy_store(struct device *dev, struct device_attribut
> unsigned long val;
> int ret;
>
> + if (!uacce->ops->isolate_err_threshold_write)
> + return -ENOENT;
> +
> if (kstrtoul(buf, 0, &val) < 0)
> return -EINVAL;
>
> @@ -440,9 +446,7 @@ static umode_t uacce_dev_is_visible(struct kobject *kobj,
> (!uacce->qf_pg_num[UACCE_QFRT_DUS])))
> return 0;
>
> - if (attr == &dev_attr_isolate_strategy.attr &&
> - (!uacce->ops->isolate_err_threshold_read &&
> - !uacce->ops->isolate_err_threshold_write))
> + if (attr == &dev_attr_isolate_strategy.attr)
> return 0;
sorry,I send the wrong version of patch 2.
Intercept the creation of sysfs if neither read nor write exists;
create sysfs if either is supported, but intercept unsupported
operations at the call site.
I will send the right version in v4.
Thanks,
Chenghai
>
> if (attr == &dev_attr_isolate.attr && !uacce->ops->get_isolate_state)
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2025-10-22 1:34 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-10-21 13:49 [PATCH v3 0/4] uacce: driver fixes for memory leaks and state management Chenghai Huang
2025-10-21 13:50 ` [PATCH v3 1/4] uacce: fix for cdev memory leak Chenghai Huang
2025-10-21 13:50 ` [PATCH v3 2/4] uacce: fix isolate sysfs check condition Chenghai Huang
2025-10-22 1:34 ` huangchenghai
2025-10-21 13:50 ` [PATCH v3 3/4] uacce: implement mremap in uacce_vm_ops to return -EPERM Chenghai Huang
2025-10-21 13:50 ` [PATCH v3 4/4] uacce: ensure safe queue release with state management Chenghai Huang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®