[PATCH linux-next] genirq: use sysfs_emit() instead of scnprintf().

xie.ludan@zte.com.cn posted 1 patch 9 months, 1 week ago
There is a newer version of this series
kernel/irq/irqdesc.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
[PATCH linux-next] genirq: use sysfs_emit() instead of scnprintf().
Posted by xie.ludan@zte.com.cn 9 months, 1 week ago
From: XieLudan <xie.ludan@zte.com.cn>

Follow the advice in Documentation/filesystems/sysfs.rst:
show() should only use sysfs_emit() or sysfs_emit_at() when formatting
the value to be returned to user space.

Signed-off-by: XieLudan <xie.ludan@zte.com.cn>
---
 kernel/irq/irqdesc.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/kernel/irq/irqdesc.c b/kernel/irq/irqdesc.c
index 287830739783..8ffe12fe5af6 100644
--- a/kernel/irq/irqdesc.c
+++ b/kernel/irq/irqdesc.c
@@ -257,11 +257,11 @@ static ssize_t per_cpu_count_show(struct kobject *kobj,
 	for_each_possible_cpu(cpu) {
 		unsigned int c = irq_desc_kstat_cpu(desc, cpu);
 
-		ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%u", p, c);
+		ret += sysfs_emit(buf + ret, "%s%u", p, c);
 		p = ",";
 	}
 
-	ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
+	ret += sysfs_emit(buf + ret, "\n");
 	return ret;
 }
 IRQ_ATTR_RO(per_cpu_count);
@@ -274,7 +274,7 @@ static ssize_t chip_name_show(struct kobject *kobj,
 
 	raw_spin_lock_irq(&desc->lock);
 	if (desc->irq_data.chip && desc->irq_data.chip->name) {
-		ret = scnprintf(buf, PAGE_SIZE, "%s\n",
+		ret = sysfs_emit(buf, "%s\n",
 				desc->irq_data.chip->name);
 	}
 	raw_spin_unlock_irq(&desc->lock);
@@ -337,7 +337,7 @@ static ssize_t name_show(struct kobject *kobj,
 
 	raw_spin_lock_irq(&desc->lock);
 	if (desc->name)
-		ret = scnprintf(buf, PAGE_SIZE, "%s\n", desc->name);
+		ret = sysfs_emit(buf, "%s\n", desc->name);
 	raw_spin_unlock_irq(&desc->lock);
 
 	return ret;
@@ -354,14 +354,14 @@ static ssize_t actions_show(struct kobject *kobj,
 
 	raw_spin_lock_irq(&desc->lock);
 	for_each_action_of_desc(desc, action) {
-		ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
+		ret += sysfs_emit(buf + ret, "%s%s",
 				 p, action->name);
 		p = ",";
 	}
 	raw_spin_unlock_irq(&desc->lock);
 
 	if (ret)
-		ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
+		ret += sysfs_emit(buf + ret, "\n");
 
 	return ret;
 }
-- 
2.25.1
Re: [PATCH linux-next] genirq: use sysfs_emit() instead of scnprintf().
Posted by Jiri Slaby 9 months ago
Hi,

your e-mail is both multipart and base64 encoded :(. Pls fix up your 
setup first.

On 15. 03. 25, 7:17, xie.ludan@zte.com.cn wrote:
> From: XieLudan <xie.ludan@zte.com.cn>
> 
> 
> Follow the advice in Documentation/filesystems/sysfs.rst:
> 
> show() should only use sysfs_emit() or sysfs_emit_at() when formatting
> 
> the value to be returned to user space.
> 
> 
> Signed-off-by: XieLudan <xie.ludan@zte.com.cn>
> 
> ---
> 
>   kernel/irq/irqdesc.c | 12 ++++++------
> 
>   1 file changed, 6 insertions(+), 6 deletions(-)
> 
> 
> diff --git a/kernel/irq/irqdesc.c b/kernel/irq/irqdesc.c
> 
> index 287830739783..8ffe12fe5af6 100644
> 
> --- a/kernel/irq/irqdesc.c
> 
> +++ b/kernel/irq/irqdesc.c
> 
> @@ -257,11 +257,11 @@ static ssize_t per_cpu_count_show(struct kobject 
> *kobj,
> 
> for_each_possible_cpu(cpu) {
> 
> unsigned int c = irq_desc_kstat_cpu(desc, cpu);
> 
> -ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%u", p, c);
> 
> +ret += sysfs_emit(buf + ret, "%s%u", p, c);

Well, so the PAGE_SIZE boundary is never checked for this stacking write 
now, right?

thanks,
-- 
js
suse labs
Re: [PATCH linux-next] genirq: use sysfs_emit() instead of scnprintf().
Posted by Jiri Slaby 9 months ago
On 17. 03. 25, 7:07, Jiri Slaby wrote:
> Hi,
> 
> your e-mail is both multipart and base64 encoded :(. Pls fix up your 
> setup first.
> 
> On 15. 03. 25, 7:17, xie.ludan@zte.com.cn wrote:
>> From: XieLudan <xie.ludan@zte.com.cn>
>>
>>
>> Follow the advice in Documentation/filesystems/sysfs.rst:
>>
>> show() should only use sysfs_emit() or sysfs_emit_at() when formatting
>>
>> the value to be returned to user space.
>>
>>
>> Signed-off-by: XieLudan <xie.ludan@zte.com.cn>
>>
>> ---
>>
>>   kernel/irq/irqdesc.c | 12 ++++++------
>>
>>   1 file changed, 6 insertions(+), 6 deletions(-)
>>
>>
>> diff --git a/kernel/irq/irqdesc.c b/kernel/irq/irqdesc.c
>>
>> index 287830739783..8ffe12fe5af6 100644
>>
>> --- a/kernel/irq/irqdesc.c
>>
>> +++ b/kernel/irq/irqdesc.c
>>
>> @@ -257,11 +257,11 @@ static ssize_t per_cpu_count_show(struct kobject 
>> *kobj,
>>
>> for_each_possible_cpu(cpu) {
>>
>> unsigned int c = irq_desc_kstat_cpu(desc, cpu);
>>
>> -ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%u", p, c);
>>
>> +ret += sysfs_emit(buf + ret, "%s%u", p, c);
> 
> Well, so the PAGE_SIZE boundary is never checked for this stacking write 
> now, right?

^^^ not really clear what I mean, so:

If you advance buf by ret every time, sysfs_emit() still would only 
check if buf is not written more than "PAGE_SIZE" bytes. But the 
original code performs this proper "PAGE_SIZE - ret" check.

> thanks,

-- 
js
suse labs
Re: [PATCH linux-next] genirq: use sysfs_emit() instead of scnprintf().
Posted by Jiri Slaby 9 months ago
On 17. 03. 25, 7:12, Jiri Slaby wrote:
>>> --- a/kernel/irq/irqdesc.c
>>>
>>> +++ b/kernel/irq/irqdesc.c
>>>
>>> @@ -257,11 +257,11 @@ static ssize_t per_cpu_count_show(struct 
>>> kobject *kobj,
>>>
>>> for_each_possible_cpu(cpu) {
>>>
>>> unsigned int c = irq_desc_kstat_cpu(desc, cpu);
>>>
>>> -ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%u", p, c);
>>>
>>> +ret += sysfs_emit(buf + ret, "%s%u", p, c);
>>
>> Well, so the PAGE_SIZE boundary is never checked for this stacking 
>> write now, right?
> 
> ^^^ not really clear what I mean, so:
> 
> If you advance buf by ret every time, sysfs_emit() still would only 
> check if buf is not written more than "PAGE_SIZE" bytes. But the 
> original code performs this proper "PAGE_SIZE - ret" check.

Apparently, sysfs_emit_at() is your friend here.

>> thanks,
-- 
js
suse labs