Re: [PATCH 15/42] drivers/ptp: Convert snprintf to sysfs_emit

From: Zhijian Li (Fujitsu)
Date: Tue Jan 16 2024 - 00:53:52 EST




On 16/01/2024 13:01, Rahul Rameshbabu wrote:
> On Tue, 16 Jan, 2024 12:51:24 +0800 Li Zhijian <lizhijian@xxxxxxxxxxx> wrote:
>> 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().
>>
>>> ./drivers/ptp/ptp_sysfs.c:27:8-16: WARNING: please use sysfs_emit
>>
>> No functional change intended
>>
>> CC: Richard Cochran <richardcochran@xxxxxxxxx>
>> CC: netdev@xxxxxxxxxxxxxxx
>> Signed-off-by: Li Zhijian <lizhijian@xxxxxxxxxxx>
>> ---
>> drivers/ptp/ptp_sysfs.c | 3 +--
>> 1 file changed, 1 insertion(+), 2 deletions(-)
>>
>> diff --git a/drivers/ptp/ptp_sysfs.c b/drivers/ptp/ptp_sysfs.c
>> index f7a499a1bd39..49737ed6ef5f 100644
>> --- a/drivers/ptp/ptp_sysfs.c
>> +++ b/drivers/ptp/ptp_sysfs.c
>> @@ -24,8 +24,7 @@ static ssize_t max_phase_adjustment_show(struct device *dev,
>> {
>> struct ptp_clock *ptp = dev_get_drvdata(dev);
>>
>> - return snprintf(page, PAGE_SIZE - 1, "%d\n",
>> - ptp->info->getmaxphase(ptp->info));
>> + return sysfs_emit(page, "%d\n", ptp->info->getmaxphase(ptp->info));
>> }
>> static DEVICE_ATTR_RO(max_phase_adjustment);
>
> I authored the lines that are being changed here, so figured I should
> provide my review. Doesn't PTP_SHOW_INT in the same file also use
> snprintf in the same manner and should be changed to sysfs_emit?

Thanks for you review.
Yes, i think so, beside PTP_SHOW_INT, there are 3 other places not detected by coccinelle should be fixed.
I will update it in V2.

$ grep snprintf drivers/ptp/ptp_sysfs.c
return snprintf(page, PAGE_SIZE-1, "%d\n", ptp->info->var); \
cnt = snprintf(page, PAGE_SIZE, "%u %lld %u\n",
size = snprintf(page, PAGE_SIZE - 1, "%u\n", ptp->n_vclocks);
size = snprintf(page, PAGE_SIZE - 1, "%u\n", ptp->max_vclocks);


Thanks
Zhijian


>
> --
> Thanks,
>
> Rahul Rameshbabu