mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] uacce: add device usage sysfs interface
@ 2026-09-21  7:51 Weili Qian
  2026-09-21  7:51 ` [PATCH 1/2] " Weili Qian
  2026-09-21  7:51 ` [PATCH 2/2] crypto: hisilicon/qm - implement uacce get_dev_usage callback Weili Qian
  0 siblings, 2 replies; 9+ messages in thread
From: Weili Qian @ 2026-09-21  7:51 UTC (permalink / raw)
  To: gregkh, --to=herbert, --to=zhangfei.gao, --to=wangzhou1,
	--cc=linux-crypto
  Cc: linux-kernel, --cc=huangchenghai2, --cc=liulongfang

Add a read-only dev_usage sysfs attribute so userspace can query device
usage through sysfs. Patch 1 adds the attribute and the get_dev_usage
callback to the UACCE framework; patch 2 implements the callback in the
HiSilicon QM driver.

Weili Qian (2):
  uacce: add device usage sysfs interface
  crypto: hisilicon/qm - implement uacce get_dev_usage callback

 Documentation/ABI/testing/sysfs-driver-uacce |  9 ++++
 drivers/crypto/hisilicon/debugfs.c           | 17 -------
 drivers/crypto/hisilicon/qm.c                | 52 ++++++++++++++++++++
 drivers/crypto/hisilicon/qm_common.h         |  1 +
 drivers/misc/uacce/uacce.c                   | 19 +++++++
 include/linux/uacce.h                        |  5 ++
 6 files changed, 86 insertions(+), 17 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 1/2] uacce: add device usage sysfs interface
  2026-09-21  7:51 [PATCH 0/2] uacce: add device usage sysfs interface Weili Qian
@ 2026-09-21  7:51 ` Weili Qian
  2026-09-21  8:12   ` Greg KH
  2026-09-21  7:51 ` [PATCH 2/2] crypto: hisilicon/qm - implement uacce get_dev_usage callback Weili Qian
  1 sibling, 1 reply; 9+ messages in thread
From: Weili Qian @ 2026-09-21  7:51 UTC (permalink / raw)
  To: gregkh, --to=herbert, --to=zhangfei.gao, --to=wangzhou1,
	--cc=linux-crypto
  Cc: linux-kernel, --cc=huangchenghai2, --cc=liulongfang

Userspace has no way to query the runtime usage of a UACCE
device; it can only be inferred indirectly from queue state, which is
neither accurate nor uniform across drivers.

Add a read-only dev_usage sysfs attribute and a get_dev_usage callback
in struct uacce_ops. A driver implementing the callback writes the
current usage as a percentage (0-100) string into the caller-provided
buffer and returns the number of bytes written; dev_usage_show()
appends the trailing newline. The attribute is hidden via
uacce_dev_is_visible() when the driver does not provide the callback.

The corresponding ABI entry is added to Documentation/ABI/testing/
sysfs-driver-uacce.

Signed-off-by: Weili Qian <qianweili@huawei.com>
---
 Documentation/ABI/testing/sysfs-driver-uacce |  9 +++++++++
 drivers/misc/uacce/uacce.c                   | 19 +++++++++++++++++++
 include/linux/uacce.h                        |  5 +++++
 3 files changed, 33 insertions(+)

diff --git a/Documentation/ABI/testing/sysfs-driver-uacce b/Documentation/ABI/testing/sysfs-driver-uacce
index d3f0b8f3c589..3e4af4c1e5a9 100644
--- a/Documentation/ABI/testing/sysfs-driver-uacce
+++ b/Documentation/ABI/testing/sysfs-driver-uacce
@@ -55,3 +55,12 @@ Date:           Feb 2020
 KernelVersion:  5.7
 Contact:        linux-accelerators@lists.ozlabs.org
 Description:    Size (bytes) of dus region queue file
+
+What:           /sys/class/uacce/<dev_name>/dev_usage
+Date:           Sep 2026
+KernelVersion:  7.3
+Contact:        linux-accelerators@lists.ozlabs.org
+Description:    (R) Current usage of the device, reported as a driver-defined
+                string of up to PAGE_SIZE - 1 bytes. Usage is expressed as a
+                percentage (0-100). The attribute is hidden if the driver does
+                not implement the get_dev_usage callback.
diff --git a/drivers/misc/uacce/uacce.c b/drivers/misc/uacce/uacce.c
index 45521d4a56d1..545ba35a590b 100644
--- a/drivers/misc/uacce/uacce.c
+++ b/drivers/misc/uacce/uacce.c
@@ -433,6 +433,20 @@ static ssize_t isolate_strategy_store(struct device *dev, struct device_attribut
 	return count;
 }
 
+static ssize_t dev_usage_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+	struct uacce_device *uacce = to_uacce_device(dev);
+	int ret;
+
+	ret = uacce->ops->get_dev_usage(uacce, buf, PAGE_SIZE - 1);
+	if (ret < 0)
+		return ret;
+
+	buf[ret++] = '\n';
+
+	return ret;
+}
+
 static DEVICE_ATTR_RO(api);
 static DEVICE_ATTR_RO(flags);
 static DEVICE_ATTR_RO(available_instances);
@@ -441,6 +455,7 @@ static DEVICE_ATTR_RO(region_mmio_size);
 static DEVICE_ATTR_RO(region_dus_size);
 static DEVICE_ATTR_RO(isolate);
 static DEVICE_ATTR_RW(isolate_strategy);
+static DEVICE_ATTR_RO(dev_usage);
 
 static struct attribute *uacce_dev_attrs[] = {
 	&dev_attr_api.attr,
@@ -451,6 +466,7 @@ static struct attribute *uacce_dev_attrs[] = {
 	&dev_attr_region_dus_size.attr,
 	&dev_attr_isolate.attr,
 	&dev_attr_isolate_strategy.attr,
+	&dev_attr_dev_usage.attr,
 	NULL,
 };
 
@@ -474,6 +490,9 @@ static umode_t uacce_dev_is_visible(struct kobject *kobj,
 	if (attr == &dev_attr_isolate.attr && !uacce->ops->get_isolate_state)
 		return 0;
 
+	if (attr == &dev_attr_dev_usage.attr && !uacce->ops->get_dev_usage)
+		return 0;
+
 	return attr->mode;
 }
 
diff --git a/include/linux/uacce.h b/include/linux/uacce.h
index e290c0269944..8f0b9765e4b6 100644
--- a/include/linux/uacce.h
+++ b/include/linux/uacce.h
@@ -34,6 +34,10 @@ struct uacce_qfile_region {
  * @get_isolate_state: get the device state after set the isolate strategy
  * @isolate_err_threshold_write: stored the isolate error threshold to the device
  * @isolate_err_threshold_read: read the isolate error threshold value from the device
+ * @get_dev_usage: get the device usage. Write a string describing current
+ *                 usage as a percentage (0-100) into @buf, at most @size
+ *                 bytes, without a trailing newline. Return the number of
+ *                 bytes written on success or a negative errno on failure.
  */
 struct uacce_ops {
 	int (*get_available_instances)(struct uacce_device *uacce);
@@ -50,6 +54,7 @@ struct uacce_ops {
 	enum uacce_dev_state (*get_isolate_state)(struct uacce_device *uacce);
 	int (*isolate_err_threshold_write)(struct uacce_device *uacce, u32 num);
 	u32 (*isolate_err_threshold_read)(struct uacce_device *uacce);
+	int (*get_dev_usage)(struct uacce_device *uacce, char *buf, int size);
 };
 
 /**
-- 
2.43.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 2/2] crypto: hisilicon/qm - implement uacce get_dev_usage callback
  2026-09-21  7:51 [PATCH 0/2] uacce: add device usage sysfs interface Weili Qian
  2026-09-21  7:51 ` [PATCH 1/2] " Weili Qian
@ 2026-09-21  7:51 ` Weili Qian
  1 sibling, 0 replies; 9+ messages in thread
From: Weili Qian @ 2026-09-21  7:51 UTC (permalink / raw)
  To: gregkh, --to=herbert, --to=zhangfei.gao, --to=wangzhou1,
	--cc=linux-crypto
  Cc: linux-kernel, --cc=huangchenghai2, --cc=liulongfang

The HiSilicon QM driver does not implement the get_dev_usage callback,
so the dev_usage sysfs attribute is hidden on its UACCE devices and
userspace cannot query per-channel usage through sysfs.

Implement hisi_qm_get_dev_usage and register it in uacce_qm_ops. For
each channel the callback reads the channel-usage register and derives
the usage via the existing qm_usage_percent() helper, moved to qm.c
with a shared declaration in qm_common.h to avoid duplication.

The channel-usage registers exist only on QM_HW_V5 and later, so the
callback returns -ENODEV on older revisions.

Signed-off-by: Weili Qian <qianweili@huawei.com>
---
 drivers/crypto/hisilicon/debugfs.c   | 17 ---------
 drivers/crypto/hisilicon/qm.c        | 52 ++++++++++++++++++++++++++++
 drivers/crypto/hisilicon/qm_common.h |  1 +
 3 files changed, 53 insertions(+), 17 deletions(-)

diff --git a/drivers/crypto/hisilicon/debugfs.c b/drivers/crypto/hisilicon/debugfs.c
index 3ee6de16e3f1..83dfbe74e704 100644
--- a/drivers/crypto/hisilicon/debugfs.c
+++ b/drivers/crypto/hisilicon/debugfs.c
@@ -1040,23 +1040,6 @@ void hisi_qm_show_last_dfx_regs(struct hisi_qm *qm)
 	}
 }
 
-static int qm_usage_percent(struct hisi_qm *qm, int chan_num)
-{
-	u32 val, used_bw, total_bw;
-
-	val = readl(qm->io_base + QM_CHANNEL_USAGE_OFFSET +
-				chan_num * QM_CHANNEL_ADDR_INTRVL);
-	used_bw = lower_16_bits(val);
-	total_bw = upper_16_bits(val);
-	if (!total_bw)
-		return -EIO;
-
-	if (total_bw <= used_bw)
-		return QM_MAX_DEV_USAGE;
-
-	return (used_bw * QM_DEV_USAGE_RATE) / total_bw;
-}
-
 static int qm_usage_show(struct seq_file *s, void *unused)
 {
 	struct hisi_qm *qm = s->private;
diff --git a/drivers/crypto/hisilicon/qm.c b/drivers/crypto/hisilicon/qm.c
index c01966a4a33f..6cd1e98dd9e3 100644
--- a/drivers/crypto/hisilicon/qm.c
+++ b/drivers/crypto/hisilicon/qm.c
@@ -2884,6 +2884,57 @@ static u32 hisi_qm_isolate_threshold_read(struct uacce_device *uacce)
 	return qm->isolate_data.err_threshold;
 }
 
+int qm_usage_percent(struct hisi_qm *qm, int chan_idx)
+{
+	u32 val, used_bw, total_bw;
+
+	val = readl(qm->io_base + QM_CHANNEL_USAGE_OFFSET +
+		    chan_idx * QM_CHANNEL_ADDR_INTRVL);
+	used_bw = lower_16_bits(val);
+	total_bw = upper_16_bits(val);
+	if (!total_bw)
+		return -EIO;
+
+	if (total_bw <= used_bw)
+		return QM_MAX_DEV_USAGE;
+
+	return (used_bw * QM_DEV_USAGE_RATE) / total_bw;
+}
+
+static int hisi_qm_get_dev_usage(struct uacce_device *uacce, char *buf, int size)
+{
+	struct hisi_qm *qm = uacce->priv;
+	struct qm_channel *channel_data = &qm->channel_data;
+	const char *sep = "";
+	int ret, i, len = 0;
+
+	if (qm->ver < QM_HW_V5)
+		return -ENODEV;
+
+	ret = qm_pm_get_sync(qm);
+	if (ret)
+		return ret;
+
+	for (i = 0; i < channel_data->channel_num; i++) {
+		ret = qm_usage_percent(qm, i);
+		if (ret < 0)
+			goto out;
+
+		if (len >= size)
+			break;
+
+		len += scnprintf(buf + len, size - len, "%s%s: %d",
+				 sep, channel_data->channel_name[i], ret);
+		sep = "\n";
+	}
+
+	ret = len;
+out:
+	qm_pm_put_sync(qm);
+
+	return ret;
+}
+
 static const struct uacce_ops uacce_qm_ops = {
 	.get_available_instances = hisi_qm_get_available_instances,
 	.get_queue = hisi_qm_uacce_get_queue,
@@ -2896,6 +2947,7 @@ static const struct uacce_ops uacce_qm_ops = {
 	.get_isolate_state = hisi_qm_get_isolate_state,
 	.isolate_err_threshold_write = hisi_qm_isolate_threshold_write,
 	.isolate_err_threshold_read = hisi_qm_isolate_threshold_read,
+	.get_dev_usage = hisi_qm_get_dev_usage,
 };
 
 static void qm_remove_uacce(struct hisi_qm *qm)
diff --git a/drivers/crypto/hisilicon/qm_common.h b/drivers/crypto/hisilicon/qm_common.h
index 0760bf55f13e..78bcf40bd851 100644
--- a/drivers/crypto/hisilicon/qm_common.h
+++ b/drivers/crypto/hisilicon/qm_common.h
@@ -75,5 +75,6 @@ struct qm_aeqc {
 int qm_set_and_get_xqc(struct hisi_qm *qm, u8 cmd, void *xqc, u32 qp_id, bool op);
 void hisi_qm_show_last_dfx_regs(struct hisi_qm *qm);
 void hisi_qm_set_algqos_init(struct hisi_qm *qm);
+int qm_usage_percent(struct hisi_qm *qm, int chan_idx);
 
 #endif
-- 
2.43.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/2] uacce: add device usage sysfs interface
  2026-09-21  7:51 ` [PATCH 1/2] " Weili Qian
@ 2026-09-21  8:12   ` Greg KH
  2026-09-22  9:11     ` qianweili
  0 siblings, 1 reply; 9+ messages in thread
From: Greg KH @ 2026-09-21  8:12 UTC (permalink / raw)
  To: Weili Qian
  Cc: herbert, zhangfei.gao, wangzhou1, linux-crypto, linux-kernel,
	huangchenghai2, liulongfang

On Mon, Sep 21, 2026 at 03:51:30PM +0800, Weili Qian wrote:
> Userspace has no way to query the runtime usage of a UACCE
> device; it can only be inferred indirectly from queue state, which is
> neither accurate nor uniform across drivers.
> 
> Add a read-only dev_usage sysfs attribute and a get_dev_usage callback
> in struct uacce_ops. A driver implementing the callback writes the
> current usage as a percentage (0-100) string into the caller-provided
> buffer and returns the number of bytes written; dev_usage_show()
> appends the trailing newline. The attribute is hidden via
> uacce_dev_is_visible() when the driver does not provide the callback.
> 
> The corresponding ABI entry is added to Documentation/ABI/testing/
> sysfs-driver-uacce.
> 
> Signed-off-by: Weili Qian <qianweili@huawei.com>
> ---
>  Documentation/ABI/testing/sysfs-driver-uacce |  9 +++++++++
>  drivers/misc/uacce/uacce.c                   | 19 +++++++++++++++++++
>  include/linux/uacce.h                        |  5 +++++
>  3 files changed, 33 insertions(+)
> 
> diff --git a/Documentation/ABI/testing/sysfs-driver-uacce b/Documentation/ABI/testing/sysfs-driver-uacce
> index d3f0b8f3c589..3e4af4c1e5a9 100644
> --- a/Documentation/ABI/testing/sysfs-driver-uacce
> +++ b/Documentation/ABI/testing/sysfs-driver-uacce
> @@ -55,3 +55,12 @@ Date:           Feb 2020
>  KernelVersion:  5.7
>  Contact:        linux-accelerators@lists.ozlabs.org
>  Description:    Size (bytes) of dus region queue file
> +
> +What:           /sys/class/uacce/<dev_name>/dev_usage
> +Date:           Sep 2026
> +KernelVersion:  7.3

That's not going to happen here :(

> +Contact:        linux-accelerators@lists.ozlabs.org
> +Description:    (R) Current usage of the device, reported as a driver-defined
> +                string of up to PAGE_SIZE - 1 bytes. Usage is expressed as a
> +                percentage (0-100). The attribute is hidden if the driver does
> +                not implement the get_dev_usage callback.
> diff --git a/drivers/misc/uacce/uacce.c b/drivers/misc/uacce/uacce.c
> index 45521d4a56d1..545ba35a590b 100644
> --- a/drivers/misc/uacce/uacce.c
> +++ b/drivers/misc/uacce/uacce.c
> @@ -433,6 +433,20 @@ static ssize_t isolate_strategy_store(struct device *dev, struct device_attribut
>  	return count;
>  }
>  
> +static ssize_t dev_usage_show(struct device *dev, struct device_attribute *attr, char *buf)
> +{
> +	struct uacce_device *uacce = to_uacce_device(dev);
> +	int ret;
> +
> +	ret = uacce->ops->get_dev_usage(uacce, buf, PAGE_SIZE - 1);

Why can't you use sysfs_emit()?  That way you don't have to worry about
PAGE_SIZE, and you don't have to do:

> +	if (ret < 0)
> +		return ret;
> +
> +	buf[ret++] = '\n';

That type of thing :(

Also, you got your math wrong above :(

> +
> +	return ret;
> +}
> +
>  static DEVICE_ATTR_RO(api);
>  static DEVICE_ATTR_RO(flags);
>  static DEVICE_ATTR_RO(available_instances);
> @@ -441,6 +455,7 @@ static DEVICE_ATTR_RO(region_mmio_size);
>  static DEVICE_ATTR_RO(region_dus_size);
>  static DEVICE_ATTR_RO(isolate);
>  static DEVICE_ATTR_RW(isolate_strategy);
> +static DEVICE_ATTR_RO(dev_usage);
>  
>  static struct attribute *uacce_dev_attrs[] = {
>  	&dev_attr_api.attr,
> @@ -451,6 +466,7 @@ static struct attribute *uacce_dev_attrs[] = {
>  	&dev_attr_region_dus_size.attr,
>  	&dev_attr_isolate.attr,
>  	&dev_attr_isolate_strategy.attr,
> +	&dev_attr_dev_usage.attr,
>  	NULL,
>  };
>  
> @@ -474,6 +490,9 @@ static umode_t uacce_dev_is_visible(struct kobject *kobj,
>  	if (attr == &dev_attr_isolate.attr && !uacce->ops->get_isolate_state)
>  		return 0;
>  
> +	if (attr == &dev_attr_dev_usage.attr && !uacce->ops->get_dev_usage)
> +		return 0;
> +
>  	return attr->mode;
>  }
>  
> diff --git a/include/linux/uacce.h b/include/linux/uacce.h
> index e290c0269944..8f0b9765e4b6 100644
> --- a/include/linux/uacce.h
> +++ b/include/linux/uacce.h
> @@ -34,6 +34,10 @@ struct uacce_qfile_region {
>   * @get_isolate_state: get the device state after set the isolate strategy
>   * @isolate_err_threshold_write: stored the isolate error threshold to the device
>   * @isolate_err_threshold_read: read the isolate error threshold value from the device
> + * @get_dev_usage: get the device usage. Write a string describing current
> + *                 usage as a percentage (0-100) into @buf, at most @size
> + *                 bytes, without a trailing newline. Return the number of
> + *                 bytes written on success or a negative errno on failure.

Why the newline thing?

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/2] uacce: add device usage sysfs interface
  2026-09-21  8:12   ` Greg KH
@ 2026-09-22  9:11     ` qianweili
  2026-09-22  9:21       ` Greg KH
  0 siblings, 1 reply; 9+ messages in thread
From: qianweili @ 2026-09-22  9:11 UTC (permalink / raw)
  To: Greg KH
  Cc: herbert, wangzhou1, linux-crypto, linux-kernel, huangchenghai2,
	liulongfang



On 2026/9/21 16:12, Greg KH wrote:
> On Mon, Sep 21, 2026 at 03:51:30PM +0800, Weili Qian wrote:
>> Userspace has no way to query the runtime usage of a UACCE
>> device; it can only be inferred indirectly from queue state, which is
>> neither accurate nor uniform across drivers.
>>
>> Add a read-only dev_usage sysfs attribute and a get_dev_usage callback
>> in struct uacce_ops. A driver implementing the callback writes the
>> current usage as a percentage (0-100) string into the caller-provided
>> buffer and returns the number of bytes written; dev_usage_show()
>> appends the trailing newline. The attribute is hidden via
>> uacce_dev_is_visible() when the driver does not provide the callback.
>>
>> The corresponding ABI entry is added to Documentation/ABI/testing/
>> sysfs-driver-uacce.
>>
>> Signed-off-by: Weili Qian <qianweili@huawei.com>
>> ---
>>   Documentation/ABI/testing/sysfs-driver-uacce |  9 +++++++++
>>   drivers/misc/uacce/uacce.c                   | 19 +++++++++++++++++++
>>   include/linux/uacce.h                        |  5 +++++
>>   3 files changed, 33 insertions(+)
>>
>> diff --git a/Documentation/ABI/testing/sysfs-driver-uacce b/Documentation/ABI/testing/sysfs-driver-uacce
>> index d3f0b8f3c589..3e4af4c1e5a9 100644
>> --- a/Documentation/ABI/testing/sysfs-driver-uacce
>> +++ b/Documentation/ABI/testing/sysfs-driver-uacce
>> @@ -55,3 +55,12 @@ Date:           Feb 2020
>>   KernelVersion:  5.7
>>   Contact:        linux-accelerators@lists.ozlabs.org
>>   Description:    Size (bytes) of dus region queue file
>> +
>> +What:           /sys/class/uacce/<dev_name>/dev_usage
>> +Date:           Sep 2026
>> +KernelVersion:  7.3
> That's not going to happen here :(
I'll change it to 7.4 in the next version.
>
>> +Contact:        linux-accelerators@lists.ozlabs.org
>> +Description:    (R) Current usage of the device, reported as a driver-defined
>> +                string of up to PAGE_SIZE - 1 bytes. Usage is expressed as a
>> +                percentage (0-100). The attribute is hidden if the driver does
>> +                not implement the get_dev_usage callback.
>> diff --git a/drivers/misc/uacce/uacce.c b/drivers/misc/uacce/uacce.c
>> index 45521d4a56d1..545ba35a590b 100644
>> --- a/drivers/misc/uacce/uacce.c
>> +++ b/drivers/misc/uacce/uacce.c
>> @@ -433,6 +433,20 @@ static ssize_t isolate_strategy_store(struct device *dev, struct device_attribut
>>   	return count;
>>   }
>>   
>> +static ssize_t dev_usage_show(struct device *dev, struct device_attribute *attr, char *buf)
>> +{
>> +	struct uacce_device *uacce = to_uacce_device(dev);
>> +	int ret;
>> +
>> +	ret = uacce->ops->get_dev_usage(uacce, buf, PAGE_SIZE - 1);
> Why can't you use sysfs_emit()?  That way you don't have to worry about
> PAGE_SIZE, and you don't have to do:
>
>> +	if (ret < 0)
>> +		return ret;
>> +
>> +	buf[ret++] = '\n';
> That type of thing :(
>
> Also, you got your math wrong above :(
sysfs_emit() is useful when the framework side knows the format string
upfront.  Here get_dev_usage is a driver callback that dynamically
generates content -- the format is not known to the framework, so
there is no format string to emit.  Having the callback write into a
temporary buffer and then sysfs_emit(buf, "%s", tmp) in the show
function would just add an unnecessary copy without gaining the
overflow protection that sysfs_emit normally provides.

But you're right that the manual newline append and the PAGE_SIZE - 1
math were unnecessary.  In the next version I'll rework the callback
contract so the driver writes the complete output (including any
trailing newlines) and dev_usage_show() just returns the callback
result directly:

     static ssize_t dev_usage_show(struct device *dev,
                                   struct device_attribute *attr, char *buf)
     {
         struct uacce_device *uacce = to_uacce_device(dev);

         return uacce->ops->get_dev_usage(uacce, buf, PAGE_SIZE);
     }

No more PAGE_SIZE - 1, no more manual newline append.  For example,
the HiSilicon QM driver writes one line per channel with a trailing
newline ("ch0: 50\nch1: 30\n"), and the framework passes it through
unchanged.

>
>> +
>> +	return ret;
>> +}
>> +
>>   static DEVICE_ATTR_RO(api);
>>   static DEVICE_ATTR_RO(flags);
>>   static DEVICE_ATTR_RO(available_instances);
>> @@ -441,6 +455,7 @@ static DEVICE_ATTR_RO(region_mmio_size);
>>   static DEVICE_ATTR_RO(region_dus_size);
>>   static DEVICE_ATTR_RO(isolate);
>>   static DEVICE_ATTR_RW(isolate_strategy);
>> +static DEVICE_ATTR_RO(dev_usage);
>>   
>>   static struct attribute *uacce_dev_attrs[] = {
>>   	&dev_attr_api.attr,
>> @@ -451,6 +466,7 @@ static struct attribute *uacce_dev_attrs[] = {
>>   	&dev_attr_region_dus_size.attr,
>>   	&dev_attr_isolate.attr,
>>   	&dev_attr_isolate_strategy.attr,
>> +	&dev_attr_dev_usage.attr,
>>   	NULL,
>>   };
>>   
>> @@ -474,6 +490,9 @@ static umode_t uacce_dev_is_visible(struct kobject *kobj,
>>   	if (attr == &dev_attr_isolate.attr && !uacce->ops->get_isolate_state)
>>   		return 0;
>>   
>> +	if (attr == &dev_attr_dev_usage.attr && !uacce->ops->get_dev_usage)
>> +		return 0;
>> +
>>   	return attr->mode;
>>   }
>>   
>> diff --git a/include/linux/uacce.h b/include/linux/uacce.h
>> index e290c0269944..8f0b9765e4b6 100644
>> --- a/include/linux/uacce.h
>> +++ b/include/linux/uacce.h
>> @@ -34,6 +34,10 @@ struct uacce_qfile_region {
>>    * @get_isolate_state: get the device state after set the isolate strategy
>>    * @isolate_err_threshold_write: stored the isolate error threshold to the device
>>    * @isolate_err_threshold_read: read the isolate error threshold value from the device
>> + * @get_dev_usage: get the device usage. Write a string describing current
>> + *                 usage as a percentage (0-100) into @buf, at most @size
>> + *                 bytes, without a trailing newline. Return the number of
>> + *                 bytes written on success or a negative errno on failure.
> Why the newline thing?
The "without a trailing newline" contract was over-engineered.  In the
next version the callback will write a string and the framework
returns it as-is; the driver decides the formatting.  Updated
kernel-doc:

     * @get_dev_usage: get the device usage. Write a string into @buf, at
     *                 most @size bytes. Values within the string are
     *                 percentages in the range 0-100; the exact format is
     *                 driver specific. Return the number of bytes written
     *                 on success or a negative errno on failure.

I'll also reword the ABI description to match: "Values within the
string are percentages in the range 0-100; the exact format is driver
specific", since the callback may emit multiple values (e.g. one per
channel) and the layout is driver-specific, not a single percentage as
the old wording implied.

Thanks for the review.

Weili

>
> thanks,
>
> greg k-h
>
> .
>


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/2] uacce: add device usage sysfs interface
  2026-09-22  9:11     ` qianweili
@ 2026-09-22  9:21       ` Greg KH
  2026-09-22 11:57         ` Weili Qian
  0 siblings, 1 reply; 9+ messages in thread
From: Greg KH @ 2026-09-22  9:21 UTC (permalink / raw)
  To: qianweili
  Cc: herbert, wangzhou1, linux-crypto, linux-kernel, huangchenghai2,
	liulongfang

On Tue, Sep 22, 2026 at 05:11:55PM +0800, qianweili wrote:
> 
> 
> On 2026/9/21 16:12, Greg KH wrote:
> > On Mon, Sep 21, 2026 at 03:51:30PM +0800, Weili Qian wrote:
> > > Userspace has no way to query the runtime usage of a UACCE
> > > device; it can only be inferred indirectly from queue state, which is
> > > neither accurate nor uniform across drivers.
> > > 
> > > Add a read-only dev_usage sysfs attribute and a get_dev_usage callback
> > > in struct uacce_ops. A driver implementing the callback writes the
> > > current usage as a percentage (0-100) string into the caller-provided
> > > buffer and returns the number of bytes written; dev_usage_show()
> > > appends the trailing newline. The attribute is hidden via
> > > uacce_dev_is_visible() when the driver does not provide the callback.
> > > 
> > > The corresponding ABI entry is added to Documentation/ABI/testing/
> > > sysfs-driver-uacce.
> > > 
> > > Signed-off-by: Weili Qian <qianweili@huawei.com>
> > > ---
> > >   Documentation/ABI/testing/sysfs-driver-uacce |  9 +++++++++
> > >   drivers/misc/uacce/uacce.c                   | 19 +++++++++++++++++++
> > >   include/linux/uacce.h                        |  5 +++++
> > >   3 files changed, 33 insertions(+)
> > > 
> > > diff --git a/Documentation/ABI/testing/sysfs-driver-uacce b/Documentation/ABI/testing/sysfs-driver-uacce
> > > index d3f0b8f3c589..3e4af4c1e5a9 100644
> > > --- a/Documentation/ABI/testing/sysfs-driver-uacce
> > > +++ b/Documentation/ABI/testing/sysfs-driver-uacce
> > > @@ -55,3 +55,12 @@ Date:           Feb 2020
> > >   KernelVersion:  5.7
> > >   Contact:        linux-accelerators@lists.ozlabs.org
> > >   Description:    Size (bytes) of dus region queue file
> > > +
> > > +What:           /sys/class/uacce/<dev_name>/dev_usage
> > > +Date:           Sep 2026
> > > +KernelVersion:  7.3
> > That's not going to happen here :(
> I'll change it to 7.4 in the next version.
> > 
> > > +Contact:        linux-accelerators@lists.ozlabs.org
> > > +Description:    (R) Current usage of the device, reported as a driver-defined
> > > +                string of up to PAGE_SIZE - 1 bytes. Usage is expressed as a
> > > +                percentage (0-100). The attribute is hidden if the driver does
> > > +                not implement the get_dev_usage callback.
> > > diff --git a/drivers/misc/uacce/uacce.c b/drivers/misc/uacce/uacce.c
> > > index 45521d4a56d1..545ba35a590b 100644
> > > --- a/drivers/misc/uacce/uacce.c
> > > +++ b/drivers/misc/uacce/uacce.c
> > > @@ -433,6 +433,20 @@ static ssize_t isolate_strategy_store(struct device *dev, struct device_attribut
> > >   	return count;
> > >   }
> > > +static ssize_t dev_usage_show(struct device *dev, struct device_attribute *attr, char *buf)
> > > +{
> > > +	struct uacce_device *uacce = to_uacce_device(dev);
> > > +	int ret;
> > > +
> > > +	ret = uacce->ops->get_dev_usage(uacce, buf, PAGE_SIZE - 1);
> > Why can't you use sysfs_emit()?  That way you don't have to worry about
> > PAGE_SIZE, and you don't have to do:
> > 
> > > +	if (ret < 0)
> > > +		return ret;
> > > +
> > > +	buf[ret++] = '\n';
> > That type of thing :(
> > 
> > Also, you got your math wrong above :(
> sysfs_emit() is useful when the framework side knows the format string
> upfront.  Here get_dev_usage is a driver callback that dynamically
> generates content -- the format is not known to the framework, so
> there is no format string to emit.  Having the callback write into a
> temporary buffer and then sysfs_emit(buf, "%s", tmp) in the show
> function would just add an unnecessary copy without gaining the
> overflow protection that sysfs_emit normally provides.

Then that is going to be a mess, sysfs files should be in a consistant
way, don't have random formats for the same filename depending on random
hardware types.  Use different sysfs files if you want to do that.

And this is just going to be a single value, nothing complex, so why do
you need a callback for that?

> But you're right that the manual newline append and the PAGE_SIZE - 1
> math were unnecessary.  In the next version I'll rework the callback
> contract so the driver writes the complete output (including any
> trailing newlines) and dev_usage_show() just returns the callback
> result directly:
> 
>     static ssize_t dev_usage_show(struct device *dev,
>                                   struct device_attribute *attr, char *buf)
>     {
>         struct uacce_device *uacce = to_uacce_device(dev);
> 
>         return uacce->ops->get_dev_usage(uacce, buf, PAGE_SIZE);

Why not have the get_dev_usage() return the value that you want to write
to the buffer, and then use sysfs_emit() to write it?  Then no need to
worry about PAGE_SIZE at all.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/2] uacce: add device usage sysfs interface
  2026-09-22  9:21       ` Greg KH
@ 2026-09-22 11:57         ` Weili Qian
  2026-09-22 12:13           ` Greg KH
  0 siblings, 1 reply; 9+ messages in thread
From: Weili Qian @ 2026-09-22 11:57 UTC (permalink / raw)
  To: Greg KH
  Cc: herbert, wangzhou1, linux-crypto, linux-kernel, huangchenghai2,
	liulongfang



On 2026/9/22 17:21, Greg KH wrote:
> On Tue, Sep 22, 2026 at 05:11:55PM +0800, qianweili wrote:
>>
>> On 2026/9/21 16:12, Greg KH wrote:
>>> On Mon, Sep 21, 2026 at 03:51:30PM +0800, Weili Qian wrote:
>>>> Userspace has no way to query the runtime usage of a UACCE
>>>> device; it can only be inferred indirectly from queue state, which is
>>>> neither accurate nor uniform across drivers.
>>>>
>>>> Add a read-only dev_usage sysfs attribute and a get_dev_usage callback
>>>> in struct uacce_ops. A driver implementing the callback writes the
>>>> current usage as a percentage (0-100) string into the caller-provided
>>>> buffer and returns the number of bytes written; dev_usage_show()
>>>> appends the trailing newline. The attribute is hidden via
>>>> uacce_dev_is_visible() when the driver does not provide the callback.
>>>>
>>>> The corresponding ABI entry is added to Documentation/ABI/testing/
>>>> sysfs-driver-uacce.
>>>>
>>>> Signed-off-by: Weili Qian <qianweili@huawei.com>
>>>> ---
>>>>    Documentation/ABI/testing/sysfs-driver-uacce |  9 +++++++++
>>>>    drivers/misc/uacce/uacce.c                   | 19 +++++++++++++++++++
>>>>    include/linux/uacce.h                        |  5 +++++
>>>>    3 files changed, 33 insertions(+)
>>>>
>>>> diff --git a/Documentation/ABI/testing/sysfs-driver-uacce b/Documentation/ABI/testing/sysfs-driver-uacce
>>>> index d3f0b8f3c589..3e4af4c1e5a9 100644
>>>> --- a/Documentation/ABI/testing/sysfs-driver-uacce
>>>> +++ b/Documentation/ABI/testing/sysfs-driver-uacce
>>>> @@ -55,3 +55,12 @@ Date:           Feb 2020
>>>>    KernelVersion:  5.7
>>>>    Contact:        linux-accelerators@lists.ozlabs.org
>>>>    Description:    Size (bytes) of dus region queue file
>>>> +
>>>> +What:           /sys/class/uacce/<dev_name>/dev_usage
>>>> +Date:           Sep 2026
>>>> +KernelVersion:  7.3
>>> That's not going to happen here :(
>> I'll change it to 7.4 in the next version.
>>>> +Contact:        linux-accelerators@lists.ozlabs.org
>>>> +Description:    (R) Current usage of the device, reported as a driver-defined
>>>> +                string of up to PAGE_SIZE - 1 bytes. Usage is expressed as a
>>>> +                percentage (0-100). The attribute is hidden if the driver does
>>>> +                not implement the get_dev_usage callback.
>>>> diff --git a/drivers/misc/uacce/uacce.c b/drivers/misc/uacce/uacce.c
>>>> index 45521d4a56d1..545ba35a590b 100644
>>>> --- a/drivers/misc/uacce/uacce.c
>>>> +++ b/drivers/misc/uacce/uacce.c
>>>> @@ -433,6 +433,20 @@ static ssize_t isolate_strategy_store(struct device *dev, struct device_attribut
>>>>    	return count;
>>>>    }
>>>> +static ssize_t dev_usage_show(struct device *dev, struct device_attribute *attr, char *buf)
>>>> +{
>>>> +	struct uacce_device *uacce = to_uacce_device(dev);
>>>> +	int ret;
>>>> +
>>>> +	ret = uacce->ops->get_dev_usage(uacce, buf, PAGE_SIZE - 1);
>>> Why can't you use sysfs_emit()?  That way you don't have to worry about
>>> PAGE_SIZE, and you don't have to do:
>>>
>>>> +	if (ret < 0)
>>>> +		return ret;
>>>> +
>>>> +	buf[ret++] = '\n';
>>> That type of thing :(
>>>
>>> Also, you got your math wrong above :(
>> sysfs_emit() is useful when the framework side knows the format string
>> upfront.  Here get_dev_usage is a driver callback that dynamically
>> generates content -- the format is not known to the framework, so
>> there is no format string to emit.  Having the callback write into a
>> temporary buffer and then sysfs_emit(buf, "%s", tmp) in the show
>> function would just add an unnecessary copy without gaining the
>> overflow protection that sysfs_emit normally provides.
> Then that is going to be a mess, sysfs files should be in a consistant
> way, don't have random formats for the same filename depending on random
> hardware types.  Use different sysfs files if you want to do that.
>
> And this is just going to be a single value, nothing complex, so why do
> you need a callback for that?
A single device may run multiple independent algorithms in parallel
-- e.g. the HiSilicon ZIP device has separate compression and
decompression engines whose usage rates are independent and cannot
be aggregated into one meaningful number.

Following your suggestion of different sysfs files, one approach is
to expose one file per algorithm.  Each file contains a single int
(0-100) formatted with sysfs_emit(), and the callback returns int.
But the number of algorithms is driver-specific (1 to 3 in the
HiSilicon drivers), so the framework would need to create attributes
dynamically at registration time, which adds complexity.

I'm not sure this is the best approach.  Do you have a better
suggestion for handling this case?

Thanks!
Weili
>
>> But you're right that the manual newline append and the PAGE_SIZE - 1
>> math were unnecessary.  In the next version I'll rework the callback
>> contract so the driver writes the complete output (including any
>> trailing newlines) and dev_usage_show() just returns the callback
>> result directly:
>>
>>      static ssize_t dev_usage_show(struct device *dev,
>>                                    struct device_attribute *attr, char *buf)
>>      {
>>          struct uacce_device *uacce = to_uacce_device(dev);
>>
>>          return uacce->ops->get_dev_usage(uacce, buf, PAGE_SIZE);
> Why not have the get_dev_usage() return the value that you want to write
> to the buffer, and then use sysfs_emit() to write it?  Then no need to
> worry about PAGE_SIZE at all.
>
> thanks,
>
> greg k-h
>
> .
>


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/2] uacce: add device usage sysfs interface
  2026-09-22 11:57         ` Weili Qian
@ 2026-09-22 12:13           ` Greg KH
  2026-09-22 13:11             ` Weili Qian
  0 siblings, 1 reply; 9+ messages in thread
From: Greg KH @ 2026-09-22 12:13 UTC (permalink / raw)
  To: Weili Qian
  Cc: herbert, wangzhou1, linux-crypto, linux-kernel, huangchenghai2,
	liulongfang

On Tue, Sep 22, 2026 at 07:57:14PM +0800, Weili Qian wrote:
> 
> 
> On 2026/9/22 17:21, Greg KH wrote:
> > On Tue, Sep 22, 2026 at 05:11:55PM +0800, qianweili wrote:
> > > 
> > > On 2026/9/21 16:12, Greg KH wrote:
> > > > On Mon, Sep 21, 2026 at 03:51:30PM +0800, Weili Qian wrote:
> > > > > Userspace has no way to query the runtime usage of a UACCE
> > > > > device; it can only be inferred indirectly from queue state, which is
> > > > > neither accurate nor uniform across drivers.
> > > > > 
> > > > > Add a read-only dev_usage sysfs attribute and a get_dev_usage callback
> > > > > in struct uacce_ops. A driver implementing the callback writes the
> > > > > current usage as a percentage (0-100) string into the caller-provided
> > > > > buffer and returns the number of bytes written; dev_usage_show()
> > > > > appends the trailing newline. The attribute is hidden via
> > > > > uacce_dev_is_visible() when the driver does not provide the callback.
> > > > > 
> > > > > The corresponding ABI entry is added to Documentation/ABI/testing/
> > > > > sysfs-driver-uacce.
> > > > > 
> > > > > Signed-off-by: Weili Qian <qianweili@huawei.com>
> > > > > ---
> > > > >    Documentation/ABI/testing/sysfs-driver-uacce |  9 +++++++++
> > > > >    drivers/misc/uacce/uacce.c                   | 19 +++++++++++++++++++
> > > > >    include/linux/uacce.h                        |  5 +++++
> > > > >    3 files changed, 33 insertions(+)
> > > > > 
> > > > > diff --git a/Documentation/ABI/testing/sysfs-driver-uacce b/Documentation/ABI/testing/sysfs-driver-uacce
> > > > > index d3f0b8f3c589..3e4af4c1e5a9 100644
> > > > > --- a/Documentation/ABI/testing/sysfs-driver-uacce
> > > > > +++ b/Documentation/ABI/testing/sysfs-driver-uacce
> > > > > @@ -55,3 +55,12 @@ Date:           Feb 2020
> > > > >    KernelVersion:  5.7
> > > > >    Contact:        linux-accelerators@lists.ozlabs.org
> > > > >    Description:    Size (bytes) of dus region queue file
> > > > > +
> > > > > +What:           /sys/class/uacce/<dev_name>/dev_usage
> > > > > +Date:           Sep 2026
> > > > > +KernelVersion:  7.3
> > > > That's not going to happen here :(
> > > I'll change it to 7.4 in the next version.
> > > > > +Contact:        linux-accelerators@lists.ozlabs.org
> > > > > +Description:    (R) Current usage of the device, reported as a driver-defined
> > > > > +                string of up to PAGE_SIZE - 1 bytes. Usage is expressed as a
> > > > > +                percentage (0-100). The attribute is hidden if the driver does
> > > > > +                not implement the get_dev_usage callback.
> > > > > diff --git a/drivers/misc/uacce/uacce.c b/drivers/misc/uacce/uacce.c
> > > > > index 45521d4a56d1..545ba35a590b 100644
> > > > > --- a/drivers/misc/uacce/uacce.c
> > > > > +++ b/drivers/misc/uacce/uacce.c
> > > > > @@ -433,6 +433,20 @@ static ssize_t isolate_strategy_store(struct device *dev, struct device_attribut
> > > > >    	return count;
> > > > >    }
> > > > > +static ssize_t dev_usage_show(struct device *dev, struct device_attribute *attr, char *buf)
> > > > > +{
> > > > > +	struct uacce_device *uacce = to_uacce_device(dev);
> > > > > +	int ret;
> > > > > +
> > > > > +	ret = uacce->ops->get_dev_usage(uacce, buf, PAGE_SIZE - 1);
> > > > Why can't you use sysfs_emit()?  That way you don't have to worry about
> > > > PAGE_SIZE, and you don't have to do:
> > > > 
> > > > > +	if (ret < 0)
> > > > > +		return ret;
> > > > > +
> > > > > +	buf[ret++] = '\n';
> > > > That type of thing :(
> > > > 
> > > > Also, you got your math wrong above :(
> > > sysfs_emit() is useful when the framework side knows the format string
> > > upfront.  Here get_dev_usage is a driver callback that dynamically
> > > generates content -- the format is not known to the framework, so
> > > there is no format string to emit.  Having the callback write into a
> > > temporary buffer and then sysfs_emit(buf, "%s", tmp) in the show
> > > function would just add an unnecessary copy without gaining the
> > > overflow protection that sysfs_emit normally provides.
> > Then that is going to be a mess, sysfs files should be in a consistant
> > way, don't have random formats for the same filename depending on random
> > hardware types.  Use different sysfs files if you want to do that.
> > 
> > And this is just going to be a single value, nothing complex, so why do
> > you need a callback for that?
> A single device may run multiple independent algorithms in parallel
> -- e.g. the HiSilicon ZIP device has separate compression and
> decompression engines whose usage rates are independent and cannot
> be aggregated into one meaningful number.

Then that can not be a sysfs file, as sysfs files are "one value per
file".

> Following your suggestion of different sysfs files, one approach is
> to expose one file per algorithm.  Each file contains a single int
> (0-100) formatted with sysfs_emit(), and the callback returns int.
> But the number of algorithms is driver-specific (1 to 3 in the
> HiSilicon drivers), so the framework would need to create attributes
> dynamically at registration time, which adds complexity.
> 
> I'm not sure this is the best approach.  Do you have a better
> suggestion for handling this case?

I don't know, just don't violate the one-version-per-file rule AND
always have the same type of data in the file with the same name (i.e.
don't have a file that can contain different types of data.)

This propose api seems to violate all of that, so I wouldn't recommend
it at all.

Why is this info needed in userspace at all?  What is userspace going to
do with it?

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/2] uacce: add device usage sysfs interface
  2026-09-22 12:13           ` Greg KH
@ 2026-09-22 13:11             ` Weili Qian
  0 siblings, 0 replies; 9+ messages in thread
From: Weili Qian @ 2026-09-22 13:11 UTC (permalink / raw)
  To: Greg KH
  Cc: herbert, wangzhou1, linux-crypto, linux-kernel, huangchenghai2,
	liulongfang



On 2026/9/22 20:13, Greg KH wrote:
> On Tue, Sep 22, 2026 at 07:57:14PM +0800, Weili Qian wrote:
>>
>> On 2026/9/22 17:21, Greg KH wrote:
>>> On Tue, Sep 22, 2026 at 05:11:55PM +0800, qianweili wrote:
>>>> On 2026/9/21 16:12, Greg KH wrote:
>>>>> On Mon, Sep 21, 2026 at 03:51:30PM +0800, Weili Qian wrote:
>>>>>> Userspace has no way to query the runtime usage of a UACCE
>>>>>> device; it can only be inferred indirectly from queue state, which is
>>>>>> neither accurate nor uniform across drivers.
>>>>>>
>>>>>> Add a read-only dev_usage sysfs attribute and a get_dev_usage callback
>>>>>> in struct uacce_ops. A driver implementing the callback writes the
>>>>>> current usage as a percentage (0-100) string into the caller-provided
>>>>>> buffer and returns the number of bytes written; dev_usage_show()
>>>>>> appends the trailing newline. The attribute is hidden via
>>>>>> uacce_dev_is_visible() when the driver does not provide the callback.
>>>>>>
>>>>>> The corresponding ABI entry is added to Documentation/ABI/testing/
>>>>>> sysfs-driver-uacce.
>>>>>>
>>>>>> Signed-off-by: Weili Qian <qianweili@huawei.com>
>>>>>> ---
>>>>>>     Documentation/ABI/testing/sysfs-driver-uacce |  9 +++++++++
>>>>>>     drivers/misc/uacce/uacce.c                   | 19 +++++++++++++++++++
>>>>>>     include/linux/uacce.h                        |  5 +++++
>>>>>>     3 files changed, 33 insertions(+)
>>>>>>
>>>>>> diff --git a/Documentation/ABI/testing/sysfs-driver-uacce b/Documentation/ABI/testing/sysfs-driver-uacce
>>>>>> index d3f0b8f3c589..3e4af4c1e5a9 100644
>>>>>> --- a/Documentation/ABI/testing/sysfs-driver-uacce
>>>>>> +++ b/Documentation/ABI/testing/sysfs-driver-uacce
>>>>>> @@ -55,3 +55,12 @@ Date:           Feb 2020
>>>>>>     KernelVersion:  5.7
>>>>>>     Contact:        linux-accelerators@lists.ozlabs.org
>>>>>>     Description:    Size (bytes) of dus region queue file
>>>>>> +
>>>>>> +What:           /sys/class/uacce/<dev_name>/dev_usage
>>>>>> +Date:           Sep 2026
>>>>>> +KernelVersion:  7.3
>>>>> That's not going to happen here :(
>>>> I'll change it to 7.4 in the next version.
>>>>>> +Contact:        linux-accelerators@lists.ozlabs.org
>>>>>> +Description:    (R) Current usage of the device, reported as a driver-defined
>>>>>> +                string of up to PAGE_SIZE - 1 bytes. Usage is expressed as a
>>>>>> +                percentage (0-100). The attribute is hidden if the driver does
>>>>>> +                not implement the get_dev_usage callback.
>>>>>> diff --git a/drivers/misc/uacce/uacce.c b/drivers/misc/uacce/uacce.c
>>>>>> index 45521d4a56d1..545ba35a590b 100644
>>>>>> --- a/drivers/misc/uacce/uacce.c
>>>>>> +++ b/drivers/misc/uacce/uacce.c
>>>>>> @@ -433,6 +433,20 @@ static ssize_t isolate_strategy_store(struct device *dev, struct device_attribut
>>>>>>     	return count;
>>>>>>     }
>>>>>> +static ssize_t dev_usage_show(struct device *dev, struct device_attribute *attr, char *buf)
>>>>>> +{
>>>>>> +	struct uacce_device *uacce = to_uacce_device(dev);
>>>>>> +	int ret;
>>>>>> +
>>>>>> +	ret = uacce->ops->get_dev_usage(uacce, buf, PAGE_SIZE - 1);
>>>>> Why can't you use sysfs_emit()?  That way you don't have to worry about
>>>>> PAGE_SIZE, and you don't have to do:
>>>>>
>>>>>> +	if (ret < 0)
>>>>>> +		return ret;
>>>>>> +
>>>>>> +	buf[ret++] = '\n';
>>>>> That type of thing :(
>>>>>
>>>>> Also, you got your math wrong above :(
>>>> sysfs_emit() is useful when the framework side knows the format string
>>>> upfront.  Here get_dev_usage is a driver callback that dynamically
>>>> generates content -- the format is not known to the framework, so
>>>> there is no format string to emit.  Having the callback write into a
>>>> temporary buffer and then sysfs_emit(buf, "%s", tmp) in the show
>>>> function would just add an unnecessary copy without gaining the
>>>> overflow protection that sysfs_emit normally provides.
>>> Then that is going to be a mess, sysfs files should be in a consistant
>>> way, don't have random formats for the same filename depending on random
>>> hardware types.  Use different sysfs files if you want to do that.
>>>
>>> And this is just going to be a single value, nothing complex, so why do
>>> you need a callback for that?
>> A single device may run multiple independent algorithms in parallel
>> -- e.g. the HiSilicon ZIP device has separate compression and
>> decompression engines whose usage rates are independent and cannot
>> be aggregated into one meaningful number.
> Then that can not be a sysfs file, as sysfs files are "one value per
> file".
>
>> Following your suggestion of different sysfs files, one approach is
>> to expose one file per algorithm.  Each file contains a single int
>> (0-100) formatted with sysfs_emit(), and the callback returns int.
>> But the number of algorithms is driver-specific (1 to 3 in the
>> HiSilicon drivers), so the framework would need to create attributes
>> dynamically at registration time, which adds complexity.
>>
>> I'm not sure this is the best approach.  Do you have a better
>> suggestion for handling this case?
> I don't know, just don't violate the one-version-per-file rule AND
> always have the same type of data in the file with the same name (i.e.
> don't have a file that can contain different types of data.)
>
> This propose api seems to violate all of that, so I wouldn't recommend
> it at all.
>
> Why is this info needed in userspace at all?  What is userspace going to
> do with it?

Userspace schedulers use this to decide whether to submit a task to
the hardware accelerator or fall back to CPU computation.  When a
process initializes, it reads the device usage and compares it
against a threshold: if the accelerator is busy, the process uses
CPU computation for its lifetime; if it's idle, the process
submits tasks to the accelerator.  This needs a stable,
programmatically readable interface.

The per-algorithm detail is necessary because a device may have
independent engines -- e.g. the HiSilicon ZIP device has separate
compression and decompression engines, and a process doing
compression should only check the compression usage, not the
decompression usage.  Aggregating them into one value would cause
wrong scheduling decisions.

To follow the one-value-per-file rule, I propose exposing one sysfs
file per algorithm, with the algorithm name in the filename:

     /sys/class/uacce/<dev>/dev_usage_compress
     /sys/class/uacce/<dev>/dev_usage_decompress

Each file contains a single integer (0-100) formatted with
sysfs_emit().  The filenames are driver-defined based on the
algorithms the device supports, but every dev_usage_* file always
returns the same type -- a single usage percentage.  Userspace
discovers the available files by listing the device directory.

Is this approach acceptable?

Thanks!
Weili
>
> thanks,
>
> greg k-h
>
> .
>


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-09-22 13:11 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-21  7:51 [PATCH 0/2] uacce: add device usage sysfs interface Weili Qian
2026-09-21  7:51 ` [PATCH 1/2] " Weili Qian
2026-09-21  8:12   ` Greg KH
2026-09-22  9:11     ` qianweili
2026-09-22  9:21       ` Greg KH
2026-09-22 11:57         ` Weili Qian
2026-09-22 12:13           ` Greg KH
2026-09-22 13:11             ` Weili Qian
2026-09-21  7:51 ` [PATCH 2/2] crypto: hisilicon/qm - implement uacce get_dev_usage callback Weili Qian

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®