linux-riscv.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Zhangjin Wu <falcon@tinylab.org>
To: thomas@t-8ch.de, w@1wt.eu
Cc: arnd@arndb.de, falcon@tinylab.org, linux-kernel@vger.kernel.org,
	linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org
Subject: Re: [PATCH v2 4/4] tools/nolibc: sys.h: apply __syscall() helper
Date: Wed,  7 Jun 2023 08:34:06 +0800	[thread overview]
Message-ID: <20230607003406.559638-1-falcon@tinylab.org> (raw)
In-Reply-To: <7e76f099-4198-421c-8157-430201970c4c@t-8ch.de>

> Hi Zhangjin,
> 
> On 2023-06-06 16:17:38+0800, Zhangjin Wu wrote:
> > Use __syscall() helper to shrink 252 lines of code.
> > 
> >     $ git show HEAD^:tools/include/nolibc/sys.h | wc -l
> >     1425
> >     $ git show HEAD:tools/include/nolibc/sys.h | wc -l
> >     1173
> >     $ echo "1425-1173" | bc -l
> >     252
> > 
> > Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
> > ---
> >  tools/include/nolibc/sys.h | 336 +++++--------------------------------
> >  1 file changed, 42 insertions(+), 294 deletions(-)
> > 
> > diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
> > index f6e3168b3e50..0cfc5157845a 100644
> > --- a/tools/include/nolibc/sys.h
> > +++ b/tools/include/nolibc/sys.h
> > @@ -108,13 +108,7 @@ int sys_chdir(const char *path)
> >  static __attribute__((unused))
> >  int chdir(const char *path)
> >  {
> > -	int ret = sys_chdir(path);
> > -
> > -	if (ret < 0) {
> > -		SET_ERRNO(-ret);
> > -		ret = -1;
> > -	}
> > -	return ret;
> > +	return __syscall(chdir, path);
> 
> To be honest I'm still not a big fan of the __syscall macro.
> It's a bit too magic for too little gain.
> 
> The commit message argues that the patches make the code shorter.
> 
> However doing 
> 
> __sysret(sys_chdir(path));
> 
> instead of
> 
> __syscall(chdir, path);
> 
> is only three characters longer and the same amout of lines.
>

Yeah, I do like your version too, it looks consise too, the only not
comfortable part is there are dual calls in one line.

> Otherwise we would have syscall() _syscall() and __syscall() each doing
> different things.
>

Yes, I'm worried about this too, although the compilers may help a
little, but it is too later.

Just brain storming, What about another non-similar name, for example,
__syswrap() or __sysin() ?

Or even convert __sysret() to __sysout() and __syscall() to __sysin(),
do you like it? or even __sysexit(), __sysentry(), but the __sysexit()
may be misused with sys_exit().

    /* Syscall return helper, set errno as -ret when ret < 0 */
    static __inline__ __attribute__((unused, always_inline))
    long __sysout(long ret)
    {
    	if (ret < 0) {
    		SET_ERRNO(-ret);
    		ret = -1;
    	}
    	return ret;
    }
    
    /* Syscall call helper, use syscall name instead of syscall number */
    #define __sysin(name, ...) __sysout(sys_##name(__VA_ARGS__))

    static __attribute__((unused))
    int brk(void *addr)
    {
    	return __sysout(sys_brk(addr) ? 0 : -ENOMEM);
    }

    static __attribute__((unused))
    int chdir(const char *path)
    {
    	return __sysin(chdir, path);
    }

If we really want something like __syscall()/__sysret(), I do think they
should be a pair ;-)

> And __syscall does not behave like a regular function.
> 
> The rest of the patchset looks great.
>

Thanks for your nice review.

> Maybe Willy can break the tie?
>

If there is no better solution, I think your version is also a first
step to go.

> 
> Thomas
> 
> 
> Note: If we figure out a way to build syscall() without macros I would
> like that also :-)

Yes, but it is not easy to cope with the variable number of arguments
without a macro.

BTW, do you like to convert the my_syscallN() of sys.h to the syscall()
you added? I do worry about it will make the checking of arguments
mismatch, exspecially, the checking of number of them hardly.

Best regards,
Zhangjin

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

  reply	other threads:[~2023-06-07  0:34 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-06  8:08 [PATCH v2 0/4] tools/nolibc: add two new syscall helpers Zhangjin Wu
2023-06-06  8:09 ` [PATCH v2 1/4] tools/nolibc: sys.h: add __syscall() and __sysret() helpers Zhangjin Wu
2023-06-06 10:33   ` Zhangjin Wu
2023-06-08 14:35   ` David Laight
2023-06-08 16:06     ` Thomas Weißschuh
2023-06-09  4:42       ` Zhangjin Wu
2023-06-09  9:15         ` David Laight
2023-06-06  8:11 ` [PATCH v2 2/4] tools/nolibc: unistd.h: apply __sysret() helper Zhangjin Wu
2023-06-06  8:16 ` [PATCH v2 3/4] tools/nolibc: sys.h: " Zhangjin Wu
2023-06-06  8:17 ` [PATCH v2 4/4] tools/nolibc: sys.h: apply __syscall() helper Zhangjin Wu
2023-06-06 18:36   ` Thomas Weißschuh
2023-06-07  0:34     ` Zhangjin Wu [this message]
2023-06-07  4:05       ` Willy Tarreau
2023-06-07  5:39         ` Zhangjin Wu
2023-06-07  6:05           ` Thomas Weißschuh
2023-06-07  6:38             ` Zhangjin Wu
2023-06-10 16:34           ` David Laight
2023-06-10 16:58             ` David Laight

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=20230607003406.559638-1-falcon@tinylab.org \
    --to=falcon@tinylab.org \
    --cc=arnd@arndb.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=thomas@t-8ch.de \
    --cc=w@1wt.eu \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).