[06/13] proc: convert /proc/self to _print_integer()
diff mbox series

Message ID 20180827231503.26899-6-adobriyan@gmail.com
State New, archived
Headers show
Series
  • [01/13] seq_file: rewrite seq_puts() in terms of seq_write()
Related show

Commit Message

Alexey Dobriyan Aug. 27, 2018, 11:14 p.m. UTC
Benchmark readlink("/proc/self") 2^23 times:

	8.205992458 seconds time elapsed ( +-  0.15% )
	7.535168869 seconds time elapsed ( +-  0.09% )

	-8.2%

Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---
 fs/proc/self.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

Comments

David Laight Aug. 29, 2018, 9:50 a.m. UTC | #1
From: Alexey Dobriyan
> Sent: 28 August 2018 00:15
> ---
>  fs/proc/self.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/proc/self.c b/fs/proc/self.c
> index 127265e5c55f..b2279412237b 100644
> --- a/fs/proc/self.c
> +++ b/fs/proc/self.c
> @@ -14,6 +14,7 @@ static const char *proc_self_get_link(struct dentry *dentry,
>  {
>  	struct pid_namespace *ns = proc_pid_ns(inode);
>  	pid_t tgid = task_tgid_nr_ns(current, ns);
> +	char buf[10], *p = buf + sizeof(buf);
>  	char *name;
> 
>  	if (!tgid)
> @@ -22,7 +23,11 @@ static const char *proc_self_get_link(struct dentry *dentry,
>  	name = kmalloc(10 + 1, dentry ? GFP_KERNEL : GFP_ATOMIC);
>  	if (unlikely(!name))
>  		return dentry ? ERR_PTR(-ENOMEM) : ERR_PTR(-ECHILD);
> -	sprintf(name, "%u", tgid);
> +

Best not to 'hide' the initialisation of 'p' at the top of the function.
Much easier to see what is going on if it is moved here.

> +	p = _print_integer_u32(p, tgid);

or just:
	p = _print_integer(buf + sizeof(buf), tgid);

(What a horrid interface ...)

> +	memcpy(name, p, buf + sizeof(buf) - p);
> +	name[buf + sizeof(buf) - p] = '\0';
> +
>  	set_delayed_call(done, kfree_link, name);
>  	return name;
>  }
> --
> 2.16.4

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
Alexey Dobriyan Aug. 30, 2018, 1:13 p.m. UTC | #2
On Wed, Aug 29, 2018 at 09:50:50AM +0000, David Laight wrote:
> From: Alexey Dobriyan
> > Sent: 28 August 2018 00:15
> > ---
> >  fs/proc/self.c | 7 ++++++-
> >  1 file changed, 6 insertions(+), 1 deletion(-)
> > 
> > diff --git a/fs/proc/self.c b/fs/proc/self.c
> > index 127265e5c55f..b2279412237b 100644
> > --- a/fs/proc/self.c
> > +++ b/fs/proc/self.c
> > @@ -14,6 +14,7 @@ static const char *proc_self_get_link(struct dentry *dentry,
> >  {
> >  	struct pid_namespace *ns = proc_pid_ns(inode);
> >  	pid_t tgid = task_tgid_nr_ns(current, ns);
> > +	char buf[10], *p = buf + sizeof(buf);
> >  	char *name;
> > 
> >  	if (!tgid)
> > @@ -22,7 +23,11 @@ static const char *proc_self_get_link(struct dentry *dentry,
> >  	name = kmalloc(10 + 1, dentry ? GFP_KERNEL : GFP_ATOMIC);
> >  	if (unlikely(!name))
> >  		return dentry ? ERR_PTR(-ENOMEM) : ERR_PTR(-ECHILD);
> > -	sprintf(name, "%u", tgid);
> > +
> 
> Best not to 'hide' the initialisation of 'p' at the top of the function.
> Much easier to see what is going on if it is moved here.

It was written that way to have initialization near the buffer,
so that it is obvious that pointer and buffer are connected and
making

	p = _print_integer_uNN(p, x);

standard building block which can be copypasted left and right and
still be correct.

> > +	p = _print_integer_u32(p, tgid);
> 
> or just:
> 	p = _print_integer(buf + sizeof(buf), tgid);
> 
> (What a horrid interface ...)

It is. The problem is that adding bound checking will get you current
vsnprintf() and number() and num_to_str().

> > +	memcpy(name, p, buf + sizeof(buf) - p);
> > +	name[buf + sizeof(buf) - p] = '\0';
> > +
> >  	set_delayed_call(done, kfree_link, name);
> >  	return name;
> >  }

Patch
diff mbox series

diff --git a/fs/proc/self.c b/fs/proc/self.c
index 127265e5c55f..b2279412237b 100644
--- a/fs/proc/self.c
+++ b/fs/proc/self.c
@@ -14,6 +14,7 @@  static const char *proc_self_get_link(struct dentry *dentry,
 {
 	struct pid_namespace *ns = proc_pid_ns(inode);
 	pid_t tgid = task_tgid_nr_ns(current, ns);
+	char buf[10], *p = buf + sizeof(buf);
 	char *name;
 
 	if (!tgid)
@@ -22,7 +23,11 @@  static const char *proc_self_get_link(struct dentry *dentry,
 	name = kmalloc(10 + 1, dentry ? GFP_KERNEL : GFP_ATOMIC);
 	if (unlikely(!name))
 		return dentry ? ERR_PTR(-ENOMEM) : ERR_PTR(-ECHILD);
-	sprintf(name, "%u", tgid);
+
+	p = _print_integer_u32(p, tgid);
+	memcpy(name, p, buf + sizeof(buf) - p);
+	name[buf + sizeof(buf) - p] = '\0';
+
 	set_delayed_call(done, kfree_link, name);
 	return name;
 }