Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1644573 > unrolled thread

Re: [PATCH 1/2] staging: lustre: lprocfs: Use kstrtouint_from_user

Started byGreg Kroah-Hartman <gregkh@linuxfoundation.org>
First post2017-05-18 16:00 +0200
Last post2017-05-19 05:10 +0200
Articles 4 — 3 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: [PATCH 1/2] staging: lustre: lprocfs: Use kstrtouint_from_user Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-18 16:00 +0200
    Re: [PATCH 1/2] staging: lustre: lprocfs: Use kstrtouint_from_user "Dilger, Andreas" <andreas.dilger@intel.com> - 2017-05-18 16:50 +0200
      Re: [PATCH 1/2] staging: lustre: lprocfs: Use kstrtouint_from_user Mathias Rav <mathiasrav@gmail.com> - 2017-05-18 17:20 +0200
        Re: [PATCH 1/2] staging: lustre: lprocfs: Use kstrtouint_from_user "Dilger, Andreas" <andreas.dilger@intel.com> - 2017-05-19 05:10 +0200

#1644573 — Re: [PATCH 1/2] staging: lustre: lprocfs: Use kstrtouint_from_user

FromGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Date2017-05-18 16:00 +0200
SubjectRe: [PATCH 1/2] staging: lustre: lprocfs: Use kstrtouint_from_user
Message-ID<tIu8a-3sl-15@gated-at.bofh.it>
On Thu, May 04, 2017 at 12:13:38PM -0400, Mathias Rav wrote:
> Prefer kstrtouint_from_user to copy_from_user+simple_strtoul.
> 
> The helper function lprocfs_wr_uint() is only used to implement
> "dump_granted_max" in debugfs.
> 
> Note the slight change in semantics: The previous implementation using
> simple_strtoul allows garbage after the number, whereas kstrtox only allows
> a trailing line break. The previous implementation allowed a write of zero
> bytes whereas kstrtox will return -EINVAL. Since this only affects a single
> debugfs endpoint, this should be a permissible slight change of semantics
> in exchange for 18 fewer lines of code.
> 
> Signed-off-by: Mathias Rav <mathiasrav@gmail.com>
> ---
>  .../lustre/lustre/obdclass/lprocfs_status.c        | 22 +---------------------
>  1 file changed, 1 insertion(+), 21 deletions(-)
> 
> diff --git a/drivers/staging/lustre/lustre/obdclass/lprocfs_status.c b/drivers/staging/lustre/lustre/obdclass/lprocfs_status.c
> index 1ec6e3767d81..338ce34d6514 100644
> --- a/drivers/staging/lustre/lustre/obdclass/lprocfs_status.c
> +++ b/drivers/staging/lustre/lustre/obdclass/lprocfs_status.c
> @@ -399,27 +399,7 @@ EXPORT_SYMBOL(lprocfs_rd_uint);
>  int lprocfs_wr_uint(struct file *file, const char __user *buffer,
>  		    unsigned long count, void *data)
>  {
> -	unsigned *p = data;
> -	char dummy[MAX_STRING_SIZE + 1], *end;
> -	unsigned long tmp;
> -
> -	if (count >= sizeof(dummy))
> -		return -EINVAL;
> -
> -	if (count == 0)
> -		return 0;
> -
> -	if (copy_from_user(dummy, buffer, count))
> -		return -EFAULT;
> -
> -	dummy[count] = '\0';
> -
> -	tmp = simple_strtoul(dummy, &end, 0);
> -	if (dummy == end)
> -		return -EINVAL;
> -
> -	*p = (unsigned int)tmp;
> -	return count;
> +	return kstrtouint_from_user(buffer, count, 0, (unsigned int *)data);

Why not just delete this whole function and have the callers make this
call instead?  No need for unneeded wrapper functions of core kernel
calls.

thanks,

greg k-h

[toc] | [next] | [standalone]


#1644623

From"Dilger, Andreas" <andreas.dilger@intel.com>
Date2017-05-18 16:50 +0200
Message-ID<tIuUz-44c-33@gated-at.bofh.it>
In reply to#1644573
On May 18, 2017, at 15:53, Greg Kroah-Hartman <gregkh@linuxfoundation.org> wrote:
> 
> On Thu, May 04, 2017 at 12:13:38PM -0400, Mathias Rav wrote:
>> Prefer kstrtouint_from_user to copy_from_user+simple_strtoul.
>> 
>> The helper function lprocfs_wr_uint() is only used to implement
>> "dump_granted_max" in debugfs.
>> 
>> Note the slight change in semantics: The previous implementation using
>> simple_strtoul allows garbage after the number, whereas kstrtox only allows
>> a trailing line break. The previous implementation allowed a write of zero
>> bytes whereas kstrtox will return -EINVAL. Since this only affects a single
>> debugfs endpoint, this should be a permissible slight change of semantics
>> in exchange for 18 fewer lines of code.
>> 
>> Signed-off-by: Mathias Rav <mathiasrav@gmail.com>
>> ---
>> .../lustre/lustre/obdclass/lprocfs_status.c        | 22 +---------------------
>> 1 file changed, 1 insertion(+), 21 deletions(-)
>> 
>> diff --git a/drivers/staging/lustre/lustre/obdclass/lprocfs_status.c b/drivers/staging/lustre/lustre/obdclass/lprocfs_status.c
>> index 1ec6e3767d81..338ce34d6514 100644
>> --- a/drivers/staging/lustre/lustre/obdclass/lprocfs_status.c
>> +++ b/drivers/staging/lustre/lustre/obdclass/lprocfs_status.c
>> @@ -399,27 +399,7 @@ EXPORT_SYMBOL(lprocfs_rd_uint);
>> int lprocfs_wr_uint(struct file *file, const char __user *buffer,
>> 		    unsigned long count, void *data)
>> {
>> -	unsigned *p = data;
>> -	char dummy[MAX_STRING_SIZE + 1], *end;
>> -	unsigned long tmp;
>> -
>> -	if (count >= sizeof(dummy))
>> -		return -EINVAL;
>> -
>> -	if (count == 0)
>> -		return 0;
>> -
>> -	if (copy_from_user(dummy, buffer, count))
>> -		return -EFAULT;
>> -
>> -	dummy[count] = '\0';
>> -
>> -	tmp = simple_strtoul(dummy, &end, 0);
>> -	if (dummy == end)
>> -		return -EINVAL;
>> -
>> -	*p = (unsigned int)tmp;
>> -	return count;
>> +	return kstrtouint_from_user(buffer, count, 0, (unsigned int *)data);
> 
> Why not just delete this whole function and have the callers make this
> call instead?  No need for unneeded wrapper functions of core kernel
> calls.

Even better, it looks like this function has no callers on the client and could just
be deleted entirely.

Cheers, Andreas
--
Andreas Dilger
Lustre Principal Architect
Intel Corporation

[toc] | [prev] | [next] | [standalone]


#1644668

FromMathias Rav <mathiasrav@gmail.com>
Date2017-05-18 17:20 +0200
Message-ID<tIvnz-4vr-5@gated-at.bofh.it>
In reply to#1644623
On Thu, 18 May 2017 14:48:25 +0000
"Dilger, Andreas" <andreas.dilger@intel.com> wrote:

> On May 18, 2017, at 15:53, Greg Kroah-Hartman <gregkh@linuxfoundation.org> wrote:
> > 
> > On Thu, May 04, 2017 at 12:13:38PM -0400, Mathias Rav wrote:  
> >> Prefer kstrtouint_from_user to copy_from_user+simple_strtoul.
> >> 
> >> The helper function lprocfs_wr_uint() is only used to implement
> >> "dump_granted_max" in debugfs.
> >> 
> >> Note the slight change in semantics: The previous implementation using
> >> simple_strtoul allows garbage after the number, whereas kstrtox only allows
> >> a trailing line break. The previous implementation allowed a write of zero
> >> bytes whereas kstrtox will return -EINVAL. Since this only affects a single
> >> debugfs endpoint, this should be a permissible slight change of semantics
> >> in exchange for 18 fewer lines of code.
> >> 
> >> Signed-off-by: Mathias Rav <mathiasrav@gmail.com>
> >> ---
> >> .../lustre/lustre/obdclass/lprocfs_status.c        | 22 +---------------------
> >> 1 file changed, 1 insertion(+), 21 deletions(-)
> >> 
> >> diff --git a/drivers/staging/lustre/lustre/obdclass/lprocfs_status.c b/drivers/staging/lustre/lustre/obdclass/lprocfs_status.c
> >> index 1ec6e3767d81..338ce34d6514 100644
> >> --- a/drivers/staging/lustre/lustre/obdclass/lprocfs_status.c
> >> +++ b/drivers/staging/lustre/lustre/obdclass/lprocfs_status.c
> >> @@ -399,27 +399,7 @@ EXPORT_SYMBOL(lprocfs_rd_uint);
> >> int lprocfs_wr_uint(struct file *file, const char __user *buffer,
> >> 		    unsigned long count, void *data)
> >> {
> >> -	unsigned *p = data;
> >> -	char dummy[MAX_STRING_SIZE + 1], *end;
> >> -	unsigned long tmp;
> >> -
> >> -	if (count >= sizeof(dummy))
> >> -		return -EINVAL;
> >> -
> >> -	if (count == 0)
> >> -		return 0;
> >> -
> >> -	if (copy_from_user(dummy, buffer, count))
> >> -		return -EFAULT;
> >> -
> >> -	dummy[count] = '\0';
> >> -
> >> -	tmp = simple_strtoul(dummy, &end, 0);
> >> -	if (dummy == end)
> >> -		return -EINVAL;
> >> -
> >> -	*p = (unsigned int)tmp;
> >> -	return count;
> >> +	return kstrtouint_from_user(buffer, count, 0, (unsigned int *)data);  
> > 
> > Why not just delete this whole function and have the callers make this
> > call instead?  No need for unneeded wrapper functions of core kernel
> > calls.  
> 
> Even better, it looks like this function has no callers on the client and could just
> be deleted entirely.

No, the functions lprocfs_{rd,wr}_uint are used once through a macro:
ldlm/ldlm_resource.c calls LPROC_SEQ_FOPS_RW_TYPE(ldlm_rw, uint)
to define ldlm_rw_uint_seq_{show,write}, which then calls
lprocfs_{rd,wr}_uint which in turn call seq_printf/kstrtouint_from_user.

It seems like much indirection for almost no gain besides hiding
access to ((seq_file *) file->private_data)->private in a macro.

If you agree, I will prepare a patch that switches ldlm_resource to
using the LPROC_SEQ_FOPS macro directly, which allows us to remove two
trivial wrappers.

/Mathias

[toc] | [prev] | [next] | [standalone]


#1645151

From"Dilger, Andreas" <andreas.dilger@intel.com>
Date2017-05-19 05:10 +0200
Message-ID<tIGsF-4nn-5@gated-at.bofh.it>
In reply to#1644668
On May 18, 2017, at 17:13, Mathias Rav <mathiasrav@gmail.com> wrote:
> 
> On Thu, 18 May 2017 14:48:25 +0000
> "Dilger, Andreas" <andreas.dilger@intel.com> wrote:
> 
>> On May 18, 2017, at 15:53, Greg Kroah-Hartman <gregkh@linuxfoundation.org> wrote:
>>> 
>>> On Thu, May 04, 2017 at 12:13:38PM -0400, Mathias Rav wrote:  
>>>> Prefer kstrtouint_from_user to copy_from_user+simple_strtoul.
>>>> 
>>>> The helper function lprocfs_wr_uint() is only used to implement
>>>> "dump_granted_max" in debugfs.
>>>> 
>>>> Note the slight change in semantics: The previous implementation using
>>>> simple_strtoul allows garbage after the number, whereas kstrtox only allows
>>>> a trailing line break. The previous implementation allowed a write of zero
>>>> bytes whereas kstrtox will return -EINVAL. Since this only affects a single
>>>> debugfs endpoint, this should be a permissible slight change of semantics
>>>> in exchange for 18 fewer lines of code.
>>>> 
>>>> Signed-off-by: Mathias Rav <mathiasrav@gmail.com>
>>>> ---
>>>> .../lustre/lustre/obdclass/lprocfs_status.c        | 22 +---------------------
>>>> 1 file changed, 1 insertion(+), 21 deletions(-)
>>>> 
>>>> diff --git a/drivers/staging/lustre/lustre/obdclass/lprocfs_status.c b/drivers/staging/lustre/lustre/obdclass/lprocfs_status.c
>>>> index 1ec6e3767d81..338ce34d6514 100644
>>>> --- a/drivers/staging/lustre/lustre/obdclass/lprocfs_status.c
>>>> +++ b/drivers/staging/lustre/lustre/obdclass/lprocfs_status.c
>>>> @@ -399,27 +399,7 @@ EXPORT_SYMBOL(lprocfs_rd_uint);
>>>> int lprocfs_wr_uint(struct file *file, const char __user *buffer,
>>>> 		    unsigned long count, void *data)
>>>> {
>>>> -	unsigned *p = data;
>>>> -	char dummy[MAX_STRING_SIZE + 1], *end;
>>>> -	unsigned long tmp;
>>>> -
>>>> -	if (count >= sizeof(dummy))
>>>> -		return -EINVAL;
>>>> -
>>>> -	if (count == 0)
>>>> -		return 0;
>>>> -
>>>> -	if (copy_from_user(dummy, buffer, count))
>>>> -		return -EFAULT;
>>>> -
>>>> -	dummy[count] = '\0';
>>>> -
>>>> -	tmp = simple_strtoul(dummy, &end, 0);
>>>> -	if (dummy == end)
>>>> -		return -EINVAL;
>>>> -
>>>> -	*p = (unsigned int)tmp;
>>>> -	return count;
>>>> +	return kstrtouint_from_user(buffer, count, 0, (unsigned int *)data);  
>>> 
>>> Why not just delete this whole function and have the callers make this
>>> call instead?  No need for unneeded wrapper functions of core kernel
>>> calls.  
>> 
>> Even better, it looks like this function has no callers on the client and could just
>> be deleted entirely.
> 
> No, the functions lprocfs_{rd,wr}_uint are used once through a macro:
> ldlm/ldlm_resource.c calls LPROC_SEQ_FOPS_RW_TYPE(ldlm_rw, uint)
> to define ldlm_rw_uint_seq_{show,write}, which then calls
> lprocfs_{rd,wr}_uint which in turn call seq_printf/kstrtouint_from_user.
> 
> It seems like much indirection for almost no gain besides hiding
> access to ((seq_file *) file->private_data)->private in a macro.
> 
> If you agree, I will prepare a patch that switches ldlm_resource to
> using the LPROC_SEQ_FOPS macro directly, which allows us to remove two
> trivial wrappers.

Please do.

Cheers, Andreas
--
Andreas Dilger
Lustre Principal Architect
Intel Corporation

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web