2024-03-18 01:29:31

by Li Zhijian

[permalink] [raw]
Subject: [PATCH v3 1/4] HID: hid-picolcd*: Convert sprintf/scnprintf to sysfs_emit/sysfs_emit_at

Per filesystems/sysfs.rst, show() should only use sysfs_emit()
or sysfs_emit_at() when formatting the value to be returned to user space.

coccinelle complains that there are still a couple of functions that use
snprintf(). Convert them to sysfs_emit().

scnprintf() will be converted as weel if they have.

Generally, this patch is generated by
make coccicheck M=<path/to/file> MODE=patch \
COCCI=scripts/coccinelle/api/device_attr_show.cocci

No functional change intended

CC: "Bruno Prémont" <[email protected]>
CC: Jiri Kosina <[email protected]>
CC: Benjamin Tissoires <[email protected]>
CC: [email protected]
Signed-off-by: Li Zhijian <[email protected]>
---
V3:
Covert more file(drivers/hid/hid-picolcd_fb.c) as suggested by Bruno

This is a part of the work "Fix coccicheck device_attr_show warnings"[1]
Split them per subsystem so that the maintainer can review it easily
[1] https://lore.kernel.org/lkml/[email protected]/
Signed-off-by: Li Zhijian <[email protected]>
---
drivers/hid/hid-picolcd_core.c | 6 +++---
drivers/hid/hid-picolcd_fb.c | 4 ++--
2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/hid/hid-picolcd_core.c b/drivers/hid/hid-picolcd_core.c
index bbda231a7ce3..fa46fb6eab3f 100644
--- a/drivers/hid/hid-picolcd_core.c
+++ b/drivers/hid/hid-picolcd_core.c
@@ -256,9 +256,9 @@ static ssize_t picolcd_operation_mode_show(struct device *dev,
struct picolcd_data *data = dev_get_drvdata(dev);

if (data->status & PICOLCD_BOOTLOADER)
- return snprintf(buf, PAGE_SIZE, "[bootloader] lcd\n");
+ return sysfs_emit(buf, "[bootloader] lcd\n");
else
- return snprintf(buf, PAGE_SIZE, "bootloader [lcd]\n");
+ return sysfs_emit(buf, "bootloader [lcd]\n");
}

static ssize_t picolcd_operation_mode_store(struct device *dev,
@@ -301,7 +301,7 @@ static ssize_t picolcd_operation_mode_delay_show(struct device *dev,
{
struct picolcd_data *data = dev_get_drvdata(dev);

- return snprintf(buf, PAGE_SIZE, "%hu\n", data->opmode_delay);
+ return sysfs_emit(buf, "%hu\n", data->opmode_delay);
}

static ssize_t picolcd_operation_mode_delay_store(struct device *dev,
diff --git a/drivers/hid/hid-picolcd_fb.c b/drivers/hid/hid-picolcd_fb.c
index d7dddd99d325..369c78d70e66 100644
--- a/drivers/hid/hid-picolcd_fb.c
+++ b/drivers/hid/hid-picolcd_fb.c
@@ -424,9 +424,9 @@ static ssize_t picolcd_fb_update_rate_show(struct device *dev,
if (ret >= PAGE_SIZE)
break;
else if (i == fb_update_rate)
- ret += scnprintf(buf+ret, PAGE_SIZE-ret, "[%u] ", i);
+ ret += sysfs_emit_at(buf, ret, "[%u] ", i);
else
- ret += scnprintf(buf+ret, PAGE_SIZE-ret, "%u ", i);
+ ret += sysfs_emit_at(buf, ret, "%u ", i);
if (ret > 0)
buf[min(ret, (size_t)PAGE_SIZE)-1] = '\n';
return ret;
--
2.29.2



2024-03-18 01:30:18

by Li Zhijian

[permalink] [raw]
Subject: [PATCH v3 2/4] HID: hid-sensor-custom: Convert sprintf/snprintf to sysfs_emit

Per filesystems/sysfs.rst, show() should only use sysfs_emit()
or sysfs_emit_at() when formatting the value to be returned to user space.

coccinelle complains that there are still a couple of functions that use
snprintf(). Convert them to sysfs_emit().

sprintf() will be converted as weel if they have.

Generally, this patch is generated by
make coccicheck M=<path/to/file> MODE=patch \
COCCI=scripts/coccinelle/api/device_attr_show.cocci

No functional change intended

CC: Jiri Kosina <[email protected]>
CC: Jonathan Cameron <[email protected]>
CC: Srinivas Pandruvada <[email protected]>
CC: Benjamin Tissoires <[email protected]>
CC: [email protected]
CC: [email protected]
Reviewed-by: Jonathan Cameron <[email protected]>
Signed-off-by: Li Zhijian <[email protected]>
---
V3:
rewrap the line as will be under 80 chars and add Reviewed-by # Jonathan
This is a part of the work "Fix coccicheck device_attr_show warnings"[1]
Split them per subsystem so that the maintainer can review it easily
[1] https://lore.kernel.org/lkml/[email protected]/
---
drivers/hid/hid-sensor-custom.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/drivers/hid/hid-sensor-custom.c b/drivers/hid/hid-sensor-custom.c
index d85398721659..ac214777d7d9 100644
--- a/drivers/hid/hid-sensor-custom.c
+++ b/drivers/hid/hid-sensor-custom.c
@@ -155,7 +155,7 @@ static ssize_t enable_sensor_show(struct device *dev,
{
struct hid_sensor_custom *sensor_inst = dev_get_drvdata(dev);

- return sprintf(buf, "%d\n", sensor_inst->enable);
+ return sysfs_emit(buf, "%d\n", sensor_inst->enable);
}

static int set_power_report_state(struct hid_sensor_custom *sensor_inst,
@@ -372,14 +372,13 @@ static ssize_t show_value(struct device *dev, struct device_attribute *attr,
sizeof(struct hid_custom_usage_desc),
usage_id_cmp);
if (usage_desc)
- return snprintf(buf, PAGE_SIZE, "%s\n",
- usage_desc->desc);
+ return sysfs_emit(buf, "%s\n", usage_desc->desc);
else
- return sprintf(buf, "not-specified\n");
+ return sysfs_emit(buf, "not-specified\n");
} else
return -EINVAL;

- return sprintf(buf, "%d\n", value);
+ return sysfs_emit(buf, "%d\n", value);
}

static ssize_t store_value(struct device *dev, struct device_attribute *attr,
--
2.29.2


2024-03-18 18:19:41

by Christophe JAILLET

[permalink] [raw]
Subject: Re: [PATCH v3 1/4] HID: hid-picolcd*: Convert sprintf/scnprintf to sysfs_emit/sysfs_emit_at

Le 18/03/2024 à 02:28, Li Zhijian a écrit :
> Per filesystems/sysfs.rst, show() should only use sysfs_emit()
> or sysfs_emit_at() when formatting the value to be returned to user space.
>
> coccinelle complains that there are still a couple of functions that use
> snprintf(). Convert them to sysfs_emit().
>
> scnprintf() will be converted as weel if they have.
>
> Generally, this patch is generated by
> make coccicheck M=<path/to/file> MODE=patch \
> COCCI=scripts/coccinelle/api/device_attr_show.cocci
>
> No functional change intended
>
> CC: "Bruno Prémont" <[email protected]>
> CC: Jiri Kosina <[email protected]>
> CC: Benjamin Tissoires <[email protected]>
> CC: [email protected]
> Signed-off-by: Li Zhijian <[email protected]>
> ---
> V3:
> Covert more file(drivers/hid/hid-picolcd_fb.c) as suggested by Bruno
>
> This is a part of the work "Fix coccicheck device_attr_show warnings"[1]
> Split them per subsystem so that the maintainer can review it easily
> [1] https://lore.kernel.org/lkml/[email protected]/
> Signed-off-by: Li Zhijian <[email protected]>
> ---
> drivers/hid/hid-picolcd_core.c | 6 +++---
> drivers/hid/hid-picolcd_fb.c | 4 ++--
> 2 files changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/hid/hid-picolcd_core.c b/drivers/hid/hid-picolcd_core.c
> index bbda231a7ce3..fa46fb6eab3f 100644
> --- a/drivers/hid/hid-picolcd_core.c
> +++ b/drivers/hid/hid-picolcd_core.c
> @@ -256,9 +256,9 @@ static ssize_t picolcd_operation_mode_show(struct device *dev,
> struct picolcd_data *data = dev_get_drvdata(dev);
>
> if (data->status & PICOLCD_BOOTLOADER)
> - return snprintf(buf, PAGE_SIZE, "[bootloader] lcd\n");
> + return sysfs_emit(buf, "[bootloader] lcd\n");
> else
> - return snprintf(buf, PAGE_SIZE, "bootloader [lcd]\n");
> + return sysfs_emit(buf, "bootloader [lcd]\n");
> }
>
> static ssize_t picolcd_operation_mode_store(struct device *dev,
> @@ -301,7 +301,7 @@ static ssize_t picolcd_operation_mode_delay_show(struct device *dev,
> {
> struct picolcd_data *data = dev_get_drvdata(dev);
>
> - return snprintf(buf, PAGE_SIZE, "%hu\n", data->opmode_delay);
> + return sysfs_emit(buf, "%hu\n", data->opmode_delay);
> }
>
> static ssize_t picolcd_operation_mode_delay_store(struct device *dev,
> diff --git a/drivers/hid/hid-picolcd_fb.c b/drivers/hid/hid-picolcd_fb.c
> index d7dddd99d325..369c78d70e66 100644
> --- a/drivers/hid/hid-picolcd_fb.c
> +++ b/drivers/hid/hid-picolcd_fb.c
> @@ -424,9 +424,9 @@ static ssize_t picolcd_fb_update_rate_show(struct device *dev,

Hi,

just above we have:
for (i = 1; i <= PICOLCDFB_UPDATE_RATE_LIMIT; i++)

> if (ret >= PAGE_SIZE)
> break;

and PICOLCDFB_UPDATE_RATE_LIMIT is 10, so it is not possible to have ret
>= PAGE_SIZE. Should it happen, sysfs_emit_at() handles it.
So, this test can also be removed, IMHO.

CJ

> else if (i == fb_update_rate)
> - ret += scnprintf(buf+ret, PAGE_SIZE-ret, "[%u] ", i);
> + ret += sysfs_emit_at(buf, ret, "[%u] ", i);
> else
> - ret += scnprintf(buf+ret, PAGE_SIZE-ret, "%u ", i);
> + ret += sysfs_emit_at(buf, ret, "%u ", i);
> if (ret > 0)
> buf[min(ret, (size_t)PAGE_SIZE)-1] = '\n';
> return ret;


2024-03-19 01:22:51

by Li Zhijian

[permalink] [raw]
Subject: Re: [PATCH v3 1/4] HID: hid-picolcd*: Convert sprintf/scnprintf to sysfs_emit/sysfs_emit_at



On 19/03/2024 02:17, Christophe JAILLET wrote:
> Le 18/03/2024 à 02:28, Li Zhijian a écrit :
>> Per filesystems/sysfs.rst, show() should only use sysfs_emit()
>> or sysfs_emit_at() when formatting the value to be returned to user space.
>>
>> coccinelle complains that there are still a couple of functions that use
>> snprintf(). Convert them to sysfs_emit().
>>
>> scnprintf() will be converted as weel if they have.
>>
>> Generally, this patch is generated by
>> make coccicheck M=<path/to/file> MODE=patch \
>> COCCI=scripts/coccinelle/api/device_attr_show.cocci
>>
>> No functional change intended
>>
>> CC: "Bruno Prémont" <[email protected]>
>> CC: Jiri Kosina <[email protected]>
>> CC: Benjamin Tissoires <[email protected]>
>> CC: [email protected]
>> Signed-off-by: Li Zhijian <[email protected]>
>> ---
>> V3:
>>     Covert more file(drivers/hid/hid-picolcd_fb.c) as suggested by Bruno
>>
>> This is a part of the work "Fix coccicheck device_attr_show warnings"[1]
>> Split them per subsystem so that the maintainer can review it easily
>> [1] https://lore.kernel.org/lkml/[email protected]/
>> Signed-off-by: Li Zhijian <[email protected]>
>> ---
>>   drivers/hid/hid-picolcd_core.c | 6 +++---
>>   drivers/hid/hid-picolcd_fb.c   | 4 ++--
>>   2 files changed, 5 insertions(+), 5 deletions(-)
>>
>> diff --git a/drivers/hid/hid-picolcd_core.c b/drivers/hid/hid-picolcd_core.c
>> index bbda231a7ce3..fa46fb6eab3f 100644
>> --- a/drivers/hid/hid-picolcd_core.c
>> +++ b/drivers/hid/hid-picolcd_core.c
>> @@ -256,9 +256,9 @@ static ssize_t picolcd_operation_mode_show(struct device *dev,
>>       struct picolcd_data *data = dev_get_drvdata(dev);
>>       if (data->status & PICOLCD_BOOTLOADER)
>> -        return snprintf(buf, PAGE_SIZE, "[bootloader] lcd\n");
>> +        return sysfs_emit(buf, "[bootloader] lcd\n");
>>       else
>> -        return snprintf(buf, PAGE_SIZE, "bootloader [lcd]\n");
>> +        return sysfs_emit(buf, "bootloader [lcd]\n");
>>   }
>>   static ssize_t picolcd_operation_mode_store(struct device *dev,
>> @@ -301,7 +301,7 @@ static ssize_t picolcd_operation_mode_delay_show(struct device *dev,
>>   {
>>       struct picolcd_data *data = dev_get_drvdata(dev);
>> -    return snprintf(buf, PAGE_SIZE, "%hu\n", data->opmode_delay);
>> +    return sysfs_emit(buf, "%hu\n", data->opmode_delay);
>>   }
>>   static ssize_t picolcd_operation_mode_delay_store(struct device *dev,
>> diff --git a/drivers/hid/hid-picolcd_fb.c b/drivers/hid/hid-picolcd_fb.c
>> index d7dddd99d325..369c78d70e66 100644
>> --- a/drivers/hid/hid-picolcd_fb.c
>> +++ b/drivers/hid/hid-picolcd_fb.c
>> @@ -424,9 +424,9 @@ static ssize_t picolcd_fb_update_rate_show(struct device *dev,
>
> Hi,
>
> just above we have:
>     for (i = 1; i <= PICOLCDFB_UPDATE_RATE_LIMIT; i++)
>
>>           if (ret >= PAGE_SIZE)
>>               break;
>
> and PICOLCDFB_UPDATE_RATE_LIMIT is 10, so it is not possible to have ret >= PAGE_SIZE. Should it happen, sysfs_emit_at() handles it.
> So, this test can also be removed, IMHO.

Good catch, i will update it.


Thanks
Zhijian

>
> CJ
>
>>           else if (i == fb_update_rate)
>> -            ret += scnprintf(buf+ret, PAGE_SIZE-ret, "[%u] ", i);
>> +            ret += sysfs_emit_at(buf, ret, "[%u] ", i);
>>           else
>> -            ret += scnprintf(buf+ret, PAGE_SIZE-ret, "%u ", i);
>> +            ret += sysfs_emit_at(buf, ret, "%u ", i);
>>       if (ret > 0)
>>           buf[min(ret, (size_t)PAGE_SIZE)-1] = '\n';
>>       return ret;
>