linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Zhangjin Wu <falcon@tinylab.org>
To: w@1wt.eu
Cc: arnd@arndb.de, david.laight@aculab.com, falcon@tinylab.org,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	linux-riscv@lists.infradead.org, thomas@t-8ch.de
Subject: Re: [PATCH v5 10/14] tools/nolibc: __sysret: support syscalls who return a pointer
Date: Mon,  3 Jul 2023 19:15:41 +0800	[thread overview]
Message-ID: <20230703111541.496900-1-falcon@tinylab.org> (raw)
In-Reply-To: <ZKKdD/p4UkEavru6@1wt.eu>

Hi, Willy

> On Mon, Jul 03, 2023 at 04:36:51PM +0800, Zhangjin Wu wrote:
> > > Syscalls that return pointer use that -MAX_ERRNO range to encode errors
> > > (such as mmap()). I just do not know if there is a convention saying that
> > > other ones also restrict themselves to that range or not. If you find
> > > some info which guarantees that it's the case for all of them, then by
> > > all means let's proceed like this, but in this case it should be mentioned
> > > in the comment why we think it's valid to do this. For now it's presented
> > > as an opportunity only.
> > 
> > Currently, I only found a prove-in-use case in musl:
> > 
> >     https://elixir.bootlin.com/musl/latest/source/src/internal/syscall_ret.c:
> > 
> >     #include <errno.h>
> >     #include "syscall.h"
> > 
> >     long __syscall_ret(unsigned long r)
> >     {
> >     	if (r > -4096UL) {
> >     		errno = -r;
> >     		return -1;
> >     	}
> >     	return r;
> >     }
> > 
> > Our new implementation (based on the one used by mmap()) is almostly the same
> > as musl. Not sure if this is enough. I have tried to 'git blame' on
> > __syscall_ret() of musl to find some clue, but failed, because the function has
> > been added before importing into its git repo.
> 
> OK, we already used the glibc-saved registers in the past to determine
> the official list of clobbered registers (and the ABI spec was even
> updated based on this). Here, musl is sufficiently deployed to consider
> this as valid. You can simply go that route and mention in the commit
> message that while you found no official reference stating that this is
> valid for int/long returns, you found at least one other implementation
> relying on this (i.e. if the kernel ever changes it will cause breakage).
>

ok.

> > > Also, the rest of the commit message regarding uintptr_t (which we don't
> > > use), bit values and modular arithmetics is extremely confusing and not
> > > needed at all. What matters is only to know if we need to consider only
> > > values -MAX_ERRNO..-1 as error or all negative ones. If so, then it's
> > > obvious that ret >= (unsigned long)-MAX_ERRNO catches them all, as the
> > > current mmap() function already does with -4095UL.
> > >
> > 
> > Yes, will clean up the commit message, but at first, let's continue get
> > more information about which one is ok:
> > 
> > - -MAX_ERRNO..-1 as error, for sys_mmap (we know in nolibc) currently
> > 
> > - all negative ones, for others currently
> 
> You can double-check in glibc for example, but I'm starting to guess
> you'll find the same test as above, i.e. errors are exclusively >-4096,
> regardless of the expected return type.
>

Your guest is definitely true ;-)

Glibc has the same logic in its INLINE_SYSCALL() macro:

    https://elixir.bootlin.com/glibc/latest/source/sysdeps/unix/sysv/linux/sysdep.h

    #undef INTERNAL_SYSCALL_ERROR_P
    #define INTERNAL_SYSCALL_ERROR_P(val) \
      ((unsigned long int) (val) > -4096UL)
    
    #ifndef SYSCALL_ERROR_LABEL
    # define SYSCALL_ERROR_LABEL(sc_err)					\
      ({									\
        __set_errno (sc_err);						\
        -1L;								\
      })
    #endif
    
    /* Define a macro which expands into the inline wrapper code for a system
       call.  It sets the errno and returns -1 on a failure, or the syscall
       return value otherwise.  */
    #undef INLINE_SYSCALL
    #define INLINE_SYSCALL(name, nr, args...)				\
      ({									\
        long int sc_ret = INTERNAL_SYSCALL (name, nr, args);		\
        __glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (sc_ret))		\
        ? SYSCALL_ERROR_LABEL (INTERNAL_SYSCALL_ERRNO (sc_ret))		\
        : sc_ret;								\
      })

Nothing differs.

But 'git blame' has no clue to any 'spec' or 'standard' either.

- fcb78a55058fd, linux: Consolidate INLINE_SYSCALL

  Moved all of the arch specific INTERNAL_SYSCALL_ERROR_P() to common
  header

- 369b849f1a382, sysdeps/unix/sysv/linux/s390/s390-32/sysdep.h (INTERNAL_SYSCALL,...

  Firstly defined this macro: INTERNAL_SYSCALL_ERROR_P()

    $ git show 369b849f1a3 | grep "define.*INTERNAL_SYSCALL_ERROR_P"
    +#define INTERNAL_SYSCALL_ERROR_P(val)	((unsigned int) (val) >= 0xfffff001u)
    +#define INTERNAL_SYSCALL_ERROR_P(val)	((unsigned int) (val) >= 0xfffff001u)
    +#define INTERNAL_SYSCALL_ERROR_P(val)	((unsigned long) (val) >= -515L)
    +#define INTERNAL_SYSCALL_ERROR_P(val)	((unsigned long) (val) >= -4095L)

Willy, I plan to further use something like, is it ok for you?

    tools/include/nolibc/errno.h:

    -#define MAX_ERRNO 4095
    +#define MAX_ERRNO 4095UL

    tools/include/nolibc/sys.h:

    /* Syscall return helper for library routines
     * set errno as -ret when ret in [-MAX_ERRNO, -1]
     *
     * Note, No official reference states the errno range
     * here aligns with musl (src/internal/syscall_ret.c)
     * and glibc (sysdeps/unix/sysv/linux/sysdep.h)
     */
    static __inline__ __attribute__((unused, always_inline))
    long __sysret(unsigned long ret)
    {
            if (ret >= -MAX_ERRNO) {
                    SET_ERRNO(-(long)ret);
                    return -1;
            }
            return ret;
    }

Or we also directly use 4096UL here.

    static __inline__ __attribute__((unused, always_inline))
    long __sysret(unsigned long ret)
    {
            if (ret > -4096UL) {
                    SET_ERRNO(-(long)ret);
                    return -1;
            }
            return ret;
    }

Best regards,
Zhangjin

> Thanks!
> Willy

  reply	other threads:[~2023-07-03 11:16 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-19 15:40 [PATCH v4 00/10] tools/nolibc: add a new syscall helper Zhangjin Wu
2023-06-19 15:41 ` [PATCH v4 01/10] tools/nolibc: sys.h: add a syscall return helper Zhangjin Wu
2023-06-19 15:42 ` [PATCH v4 02/10] tools/nolibc: unistd.h: apply __sysret() helper Zhangjin Wu
2023-06-19 15:44 ` [PATCH v4 03/10] tools/nolibc: sys.h: " Zhangjin Wu
2023-06-19 15:45 ` [PATCH v4 04/10] tools/nolibc: unistd.h: reorder the syscall macros Zhangjin Wu
2023-06-19 15:47 ` [PATCH v4 05/10] tools/nolibc: add missing my_syscall6() for mips Zhangjin Wu
2023-06-19 15:48 ` [PATCH v4 06/10] tools/nolibc: __sysret: support syscalls who return a pointer Zhangjin Wu
2023-06-19 15:51 ` [PATCH v4 07/10] tools/nolibc: clean up mmap() support Zhangjin Wu
2023-06-21 18:48   ` Thomas Weißschuh
2023-06-22 19:08     ` Zhangjin Wu
2023-06-19 15:52 ` [PATCH v4 08/10] selftests/nolibc: add EXPECT_PTREQ, EXPECT_PTRNE and EXPECT_PTRER Zhangjin Wu
2023-06-19 15:54 ` [PATCH v4 09/10] selftests/nolibc: add sbrk_0 to test current brk getting Zhangjin Wu
2023-06-19 15:55 ` [PATCH v4 10/10] selftests/nolibc: add mmap and munmap test cases Zhangjin Wu
2023-06-19 16:14   ` Zhangjin Wu
2023-06-21 18:58   ` Thomas Weißschuh
2023-06-22 19:32     ` Zhangjin Wu
2023-06-22 19:56       ` Thomas Weißschuh
2023-06-24  7:47         ` Zhangjin Wu
2023-06-28 13:07 ` [PATCH v5 00/14] tools/nolibc: add a new syscall helper Zhangjin Wu
2023-06-28 13:08   ` [PATCH v5 01/14] tools/nolibc: sys.h: add a syscall return helper Zhangjin Wu
2023-06-28 13:11   ` [PATCH v5 02/14] tools/nolibc: unistd.h: apply __sysret() helper Zhangjin Wu
2023-06-28 13:13   ` [PATCH v5 03/14] tools/nolibc: sys.h: " Zhangjin Wu
2023-06-28 13:14   ` [PATCH v5 04/14] tools/nolibc: unistd.h: reorder the syscall macros Zhangjin Wu
2023-06-28 13:17   ` [PATCH v5 05/14] tools/nolibc: string.h: clean up multiple whitespaces with tab Zhangjin Wu
2023-06-28 13:19   ` [PATCH v5 06/14] tools/nolibc: arch-*.h: clean up multiple whitespaces Zhangjin Wu
2023-07-02 18:44     ` Willy Tarreau
2023-07-03 14:02       ` Zhangjin Wu
2023-06-28 13:22   ` [PATCH v5 07/14] tools/nolibc: arch-loongarch.h: shrink with SYSCALL_CLOBBERLIST Zhangjin Wu
2023-07-02 18:50     ` Willy Tarreau
2023-07-03 11:28       ` Zhangjin Wu
2023-06-28 13:31   ` [PATCH v5 08/14] tools/nolibc: arch-mips.h: " Zhangjin Wu
2023-06-28 13:37   ` [PATCH v5 09/14] tools/nolibc: add missing my_syscall6() for mips Zhangjin Wu
2023-07-02 18:55     ` Willy Tarreau
2023-07-03 10:13       ` Zhangjin Wu
2023-06-28 13:39   ` [PATCH v5 10/14] tools/nolibc: __sysret: support syscalls who return a pointer Zhangjin Wu
2023-07-02 19:17     ` Willy Tarreau
2023-07-03  8:36       ` Zhangjin Wu
2023-07-03 10:03         ` Willy Tarreau
2023-07-03 11:15           ` Zhangjin Wu [this message]
2023-06-28 13:41   ` [PATCH v5 11/14] tools/nolibc: clean up mmap() support Zhangjin Wu
2023-07-02 19:23     ` Willy Tarreau
2023-07-03  6:51       ` Zhangjin Wu
2023-06-28 13:44   ` [PATCH v5 12/14] selftests/nolibc: add EXPECT_PTREQ, EXPECT_PTRNE and EXPECT_PTRER Zhangjin Wu
2023-06-28 13:46   ` [PATCH v5 13/14] selftests/nolibc: add sbrk_0 to test current brk getting Zhangjin Wu
2023-06-28 13:51   ` [PATCH v5 14/14] selftests/nolibc: add mmap and munmap test cases Zhangjin Wu
2023-07-02 19:33     ` Willy Tarreau
2023-07-03  6:03       ` Zhangjin Wu
2023-07-03  7:25         ` Willy Tarreau
2023-07-03  8:06           ` Zhangjin Wu
2023-07-03  8:20             ` Thomas Weißschuh
2023-07-03  9:15               ` Zhangjin Wu
2023-07-03  9:56             ` Willy Tarreau
2023-07-03 11:24               ` Zhangjin Wu
2023-07-02 19:34   ` [PATCH v5 00/14] tools/nolibc: add a new syscall helper Willy Tarreau

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=20230703111541.496900-1-falcon@tinylab.org \
    --to=falcon@tinylab.org \
    --cc=arnd@arndb.de \
    --cc=david.laight@aculab.com \
    --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).