diff mbox series

drivers/base/memory: simplify outputting of valid_zones_show()

Message ID 20250107100941.1169222-1-ruansy.fnst@fujitsu.com (mailing list archive)
State New
Headers show
Series drivers/base/memory: simplify outputting of valid_zones_show() | expand

Commit Message

Shiyang Ruan Jan. 7, 2025, 10:09 a.m. UTC
No need to specific position at the first writing to the buf because the
@len is always 0 at this time.  Use sysfs_emit() instead to simplify it.

Signed-off-by: Shiyang Ruan <ruansy.fnst@fujitsu.com>
---
 drivers/base/memory.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

Comments

David Hildenbrand Jan. 7, 2025, 10:15 a.m. UTC | #1
On 07.01.25 11:09, Shiyang Ruan wrote:
> No need to specific position at the first writing to the buf because the
> @len is always 0 at this time.  Use sysfs_emit() instead to simplify it.
> 
> Signed-off-by: Shiyang Ruan <ruansy.fnst@fujitsu.com>
> ---
>   drivers/base/memory.c | 6 ++----
>   1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/base/memory.c b/drivers/base/memory.c
> index 67858eeb92ed..d77a83c9af39 100644
> --- a/drivers/base/memory.c
> +++ b/drivers/base/memory.c
> @@ -469,19 +469,17 @@ static ssize_t valid_zones_show(struct device *dev,
>   		default_zone = mem->zone;
>   		if (!default_zone)
>   			return sysfs_emit(buf, "%s\n", "none");
> -		len += sysfs_emit_at(buf, len, "%s", default_zone->name);
> -		goto out;
> +		return sysfs_emit(buf, "%s\n", default_zone->name);

We can go one step further and do:

return sysfs_emit(buf, "%s\n",
		  mem->zone ? mem->zone->name : "none");

Avoiding setting/checking default_zone.
Shiyang Ruan Jan. 7, 2025, 10:28 a.m. UTC | #2
在 2025/1/7 18:15, David Hildenbrand 写道:
> On 07.01.25 11:09, Shiyang Ruan wrote:
>> No need to specific position at the first writing to the buf because the
>> @len is always 0 at this time.  Use sysfs_emit() instead to simplify it.
>>
>> Signed-off-by: Shiyang Ruan <ruansy.fnst@fujitsu.com>
>> ---
>>   drivers/base/memory.c | 6 ++----
>>   1 file changed, 2 insertions(+), 4 deletions(-)
>>
>> diff --git a/drivers/base/memory.c b/drivers/base/memory.c
>> index 67858eeb92ed..d77a83c9af39 100644
>> --- a/drivers/base/memory.c
>> +++ b/drivers/base/memory.c
>> @@ -469,19 +469,17 @@ static ssize_t valid_zones_show(struct device *dev,
>>           default_zone = mem->zone;
>>           if (!default_zone)
>>               return sysfs_emit(buf, "%s\n", "none");
>> -        len += sysfs_emit_at(buf, len, "%s", default_zone->name);
>> -        goto out;
>> +        return sysfs_emit(buf, "%s\n", default_zone->name);
> 
> We can go one step further and do:
> 
> return sysfs_emit(buf, "%s\n",
>            mem->zone ? mem->zone->name : "none");
> 
> Avoiding setting/checking default_zone.

I have thought about it but it would cause the line over long.  Now that 
it doesn't matter, I'll make the chage as you suggested and send a new 
version.

Thanks!

--
Ruan.
David Hildenbrand Jan. 7, 2025, 10:33 a.m. UTC | #3
On 07.01.25 11:28, Shiyang Ruan wrote:
> 
> 
> 在 2025/1/7 18:15, David Hildenbrand 写道:
>> On 07.01.25 11:09, Shiyang Ruan wrote:
>>> No need to specific position at the first writing to the buf because the
>>> @len is always 0 at this time.  Use sysfs_emit() instead to simplify it.
>>>
>>> Signed-off-by: Shiyang Ruan <ruansy.fnst@fujitsu.com>
>>> ---
>>>    drivers/base/memory.c | 6 ++----
>>>    1 file changed, 2 insertions(+), 4 deletions(-)
>>>
>>> diff --git a/drivers/base/memory.c b/drivers/base/memory.c
>>> index 67858eeb92ed..d77a83c9af39 100644
>>> --- a/drivers/base/memory.c
>>> +++ b/drivers/base/memory.c
>>> @@ -469,19 +469,17 @@ static ssize_t valid_zones_show(struct device *dev,
>>>            default_zone = mem->zone;
>>>            if (!default_zone)
>>>                return sysfs_emit(buf, "%s\n", "none");
>>> -        len += sysfs_emit_at(buf, len, "%s", default_zone->name);
>>> -        goto out;
>>> +        return sysfs_emit(buf, "%s\n", default_zone->name);
>>
>> We can go one step further and do:
>>
>> return sysfs_emit(buf, "%s\n",
>>             mem->zone ? mem->zone->name : "none");
>>
>> Avoiding setting/checking default_zone.
> 
> I have thought about it but it would cause the line over long.

I think it fits even into 80 chars just fine if you split it across two 
lines like I suggested.

But yeah, a single line might be fine as well.
diff mbox series

Patch

diff --git a/drivers/base/memory.c b/drivers/base/memory.c
index 67858eeb92ed..d77a83c9af39 100644
--- a/drivers/base/memory.c
+++ b/drivers/base/memory.c
@@ -469,19 +469,17 @@  static ssize_t valid_zones_show(struct device *dev,
 		default_zone = mem->zone;
 		if (!default_zone)
 			return sysfs_emit(buf, "%s\n", "none");
-		len += sysfs_emit_at(buf, len, "%s", default_zone->name);
-		goto out;
+		return sysfs_emit(buf, "%s\n", default_zone->name);
 	}
 
 	default_zone = zone_for_pfn_range(MMOP_ONLINE, nid, group,
 					  start_pfn, nr_pages);
 
-	len += sysfs_emit_at(buf, len, "%s", default_zone->name);
+	len += sysfs_emit(buf, "%s", default_zone->name);
 	len += print_allowed_zone(buf, len, nid, group, start_pfn, nr_pages,
 				  MMOP_ONLINE_KERNEL, default_zone);
 	len += print_allowed_zone(buf, len, nid, group, start_pfn, nr_pages,
 				  MMOP_ONLINE_MOVABLE, default_zone);
-out:
 	len += sysfs_emit_at(buf, len, "\n");
 	return len;
 }