linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexey Dobriyan <adobriyan@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: linux-kernel@vger.kernel.org, linux@rasmusvillemoes.dk
Subject: Re: [PATCH 02/12] Add parse_integer() (replacement for simple_strto*())
Date: Sun, 10 May 2015 16:52:03 +0300	[thread overview]
Message-ID: <20150510135203.GC1717@p183.telecom.by> (raw)
In-Reply-To: <20150508134646.6b9bf4158d220b65c5a922f9@linux-foundation.org>

On Fri, May 08, 2015 at 01:46:46PM -0700, Andrew Morton wrote:
> On Fri, 8 May 2015 21:30:29 +0300 Alexey Dobriyan <adobriyan@gmail.com> wrote:
> 
> > kstrto*() and kstrto*_from_user() family of functions were added
> > to help with parsing one integer written as string to proc/sysfs/debugfs
> > files. But they have a limitation: string passed must end with \0 or \n\0.
> > There are enough places where kstrto*() functions can't be used because of
> > this limitation. Trivial example: major:minor "%u:%u".
> > 
> > Currently the only way to parse everything is simple_strto*() functions.
> > But they are suboptimal:
> > * they do not detect overflow (can be fixed, but no one bothered since ~0.99.11),
> > * there are only 4 of them -- long and "long long" versions,
> >   This leads to silent truncation in the most simple case:
> > 
> > 	val = strtoul(s, NULL, 0);
> > 
> > * half of the people think that "char **endp" argument is necessary and
> >   add unnecessary variable.
> > 
> > OpenBSD people, fed up with how complex correct integer parsing is, added
> > strtonum(3) to fixup for deficiencies of libc-style integer parsing:
> > http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/strtonum.3?query=strtonum&arch=i386
> > 
> > It'd be OK to copy that but it relies on "errno" and fixed strings as
> > error reporting channel which I think is not OK for kernel.
> > strtonum() also doesn't report number of characted consumed.
> > 
> > What to do?
> > 
> > Enter parse_integer().
> 
>  fs/binfmt_misc.c               |   12 
>  fs/cachefiles/daemon.c         |   84 ++--
>  fs/dcache.c                    |    2 
>  fs/ext2/super.c                |    6 
>  fs/ext3/super.c                |    7 
>  fs/ext4/super.c                |   15 
>  fs/inode.c                     |    2 
>  fs/libfs.c                     |   26 -
>  fs/namespace.c                 |    4 
>  fs/ocfs2/cluster/heartbeat.c   |   54 +-
>  fs/ocfs2/cluster/nodemanager.c |   50 +-
>  fs/ocfs2/stack_user.c          |   52 +-
>  include/linux/kernel.h         |  129 -------
>  include/linux/parse-integer.h  |  188 ++++++++++
>  lib/Kconfig.debug              |    3 
>  lib/Makefile                   |    2 
>  lib/cmdline.c                  |   42 +-
>  lib/kstrtox.c                  |  254 -------------
>  lib/kstrtox.h                  |    1 
>  lib/parse-integer.c            |  222 ++++++++++++
>  lib/parser.c                   |   33 -
>  lib/swiotlb.c                  |    2 
>  lib/test-kstrtox.c             |    6 
>  lib/test-parse-integer.c       |  563 +++++++++++++++++++++++++++++++
>  lib/vsprintf.c                 |   81 ++--
>  mm/memcontrol.c                |   19 -
>  mm/memtest.c                   |    2 
>  mm/page_alloc.c                |    2 
>  mm/shmem.c                     |   14 
>  29 files changed, 1242 insertions(+), 635 deletions(-)
> 
> So not counting lib/test-parse-integer.c, it's a net addition of 44
> lines.  That's OK.
> 
> My overall reaction to this is "oh god, not again".  Is it really worth
> it?

I think giving good examples to people is always worth it :-)

> > +#define parse_integer(s, base, val)	\
> > +({					\
> > +	const char *_s = (s);		\
> > +	unsigned int _base = (base);	\
> > +	typeof(&(val)[0]) _val = (val);	\
> > +					\
> > +	__builtin_choose_expr(						\
> > +	__builtin_types_compatible_p(typeof(_val), signed char *),	\
> > +	_parse_integer_sc(_s, _base, (void *)_val),			\
> > +	__builtin_choose_expr(						\
> > +	__builtin_types_compatible_p(typeof(_val), unsigned char *),	\
> > +	_parse_integer_uc(_s, _base, (void *)_val),			\
> > +	__builtin_choose_expr(						\
> > +	__builtin_types_compatible_p(typeof(_val), short *),		\
> > +	_parse_integer_s(_s, _base, (void *)_val),			\
> > +	__builtin_choose_expr(						\
> > +	__builtin_types_compatible_p(typeof(_val), unsigned short *),	\
> > +	_parse_integer_us(_s, _base, (void *)_val),			\
> > +	__builtin_choose_expr(						\
> > +	__builtin_types_compatible_p(typeof(_val), int *),		\
> > +	_parse_integer_i(_s, _base, (void *)_val),			\
> > +	__builtin_choose_expr(						\
> > +	__builtin_types_compatible_p(typeof(_val), unsigned int *),	\
> > +	_parse_integer_u(_s, _base, (void *)_val),			\
> > +	__builtin_choose_expr(						\
> > +	__builtin_types_compatible_p(typeof(_val), long *) && sizeof(long) == 4,\
> > +	_parse_integer_i(_s, _base, (void *)_val),			\
> > +	__builtin_choose_expr(						\
> > +	__builtin_types_compatible_p(typeof(_val), long *) && sizeof(long) == 8,\
> > +	_parse_integer_ll(_s, _base, (void *)_val),			\
> > +	__builtin_choose_expr(						\
> > +	__builtin_types_compatible_p(typeof(_val), unsigned long *) && sizeof(unsigned long) == 4,\
> > +	_parse_integer_u(_s, _base, (void *)_val),			\
> > +	__builtin_choose_expr(						\
> > +	__builtin_types_compatible_p(typeof(_val), unsigned long *) && sizeof(unsigned long) == 8,\
> > +	_parse_integer_ull(_s, _base, (void *)_val),			\
> > +	__builtin_choose_expr(						\
> > +	__builtin_types_compatible_p(typeof(_val), long long *),	\
> > +	_parse_integer_ll(_s, _base, (void *)_val),			\
> > +	__builtin_choose_expr(						\
> > +	__builtin_types_compatible_p(typeof(_val), unsigned long long *),\
> > +	_parse_integer_ull(_s, _base, (void *)_val),			\
> > +	_parse_integer_link_time_error()))))))))))));	\
> > +})
> 
> Wow.

Yep, but blame the language not me.
Macro would be simpler with _Generic, but it is too early for that.

> > +/* internal, do not use */
> > +int _parse_integer_sc(const char *s, unsigned int base, signed char *val);
> > +int _parse_integer_uc(const char *s, unsigned int base, unsigned char *val);
> > +int _parse_integer_s(const char *s, unsigned int base, short *val);
> > +int _parse_integer_us(const char *s, unsigned int base, unsigned short *val);
> > +int _parse_integer_i(const char *s, unsigned int base, int *val);
> > +int _parse_integer_u(const char *s, unsigned int base, unsigned int *val);
> > +int _parse_integer_ll(const char *s, unsigned int base, long long *val);
> > +int _parse_integer_ull(const char *s, unsigned int base, unsigned long long *val);
> 
> These all have fairly lengthy implementations.  Could it all be done
> with a single function?
> 
> int __parse_integer(const char *s, unsigned int base, unsigned int size, void *val);
> 
> Where "size" is 1,2,4,8 with the top bit set if signed?

The question is why bother.

With smallish VM config I was testing, .text size difference is very small,
LOC-wise the difference doesn't exist as well (additional error checking adds
lines, not code per se). In the end _parse_integer() and simple_strto*() functions
will be removed as well.

With 4-arg dispatch function, every callsite will be more bloated:

	simple_strto*	(pointer, pointer, int)
	parse_integer	(pointer, int, pointer)
	4-arg		(pointer, int, int, pointer)

I think this code is way understandable than any mask-shift alternative:

	int _parse_integer_sc(const char *s, unsigned int base, signed char *val)
	{
	        long long tmp;
	        int rv;

	        rv = _parse_integer_ll(s, base, &tmp);
	        if (rv < 0)
	                return rv;
	        if (tmp != (signed char)tmp)
	                return -ERANGE;
	        *val = tmp;
	        return rv;
	}

  parent reply	other threads:[~2015-05-10 13:52 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-08 18:29 [PATCH 01/12] kstrto*: accept "-0" for signed conversion Alexey Dobriyan
2015-05-08 18:30 ` [PATCH 02/12] Add parse_integer() (replacement for simple_strto*()) Alexey Dobriyan
2015-05-08 20:46   ` Andrew Morton
2015-05-08 21:52     ` Rasmus Villemoes
2015-05-10 13:52     ` Alexey Dobriyan [this message]
2015-05-13 12:19       ` Alexey Dobriyan
2015-05-10 15:52   ` Noel Grandin
2015-07-09 19:28   ` Andrew Morton
2015-07-10  6:46     ` Alexey Dobriyan
2015-05-08 18:31 ` [PATCH 03/12] parse_integer: add runtime testsuite Alexey Dobriyan
2015-05-08 18:33 ` [PATCH 04/12] parse-integer: rewrite kstrto*() Alexey Dobriyan
2015-05-08 18:33 ` [PATCH 05/12] parse_integer: convert scanf() Alexey Dobriyan
2015-05-08 18:34 ` [PATCH 06/12] scanf: fix type range overflow Alexey Dobriyan
2015-05-08 18:35 ` [PATCH 07/12] parse_integer: convert lib/ Alexey Dobriyan
2015-05-08 18:35 ` [PATCH 08/12] parse_integer: convert mm/ Alexey Dobriyan
2015-05-08 18:36 ` [PATCH 09/12] parse_integer: convert fs/ Alexey Dobriyan
2015-05-08 18:37 ` [PATCH 10/37] parse_integer: convert fs/cachefiles/ Alexey Dobriyan
2015-05-08 18:39 ` [PATCH 11/12] parse_integer: convert ext2, ext3, ext4 Alexey Dobriyan
2015-05-08 18:40 ` [PATCH 12/12] parse_integer: convert fs/ocfs2/ Alexey Dobriyan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150510135203.GC1717@p183.telecom.by \
    --to=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --subject='Re: [PATCH 02/12] Add parse_integer() (replacement for simple_strto*())' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).