linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/3] tools/nolibc: add a new syscall helper
@ 2023-06-07 11:28 Zhangjin Wu
  2023-06-07 11:30 ` [PATCH v3 1/3] tools/nolibc: sys.h: add a syscall return helper Zhangjin Wu
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Zhangjin Wu @ 2023-06-07 11:28 UTC (permalink / raw)
  To: thomas, w; +Cc: falcon, arnd, linux-kernel, linux-kselftest, linux-riscv

Willy, Thomas

This is the revision of the v2 syscall helpers [1], it is based on
20230606-nolibc-rv32+stkp7a of [2]. It doesn't conflict with the v4 of
-ENOSYS patchset [3], so, it is ok to simply merge both of them.

This revision mainly applied Thomas' method, removed the __syscall()
helper and replaced it with __sysret() instead, because __syscall()
looks like _syscall() and syscall(), it may mixlead the developers.

Changes from v2 -> v3:

* tools/nolibc: sys.h: add a syscall return helper

  * The __syscall() is removed.

  * Align the code style of __sysret() with the others, and use
    __inline__ instead of inline (like stdlib.h) to let it work with
    the default -std=c89 in tools/testing/selftests/nolibc/Makefile

* tools/nolibc: unistd.h: apply __sysret() helper

  As v2.

* tools/nolibc: sys.h: apply __sysret() helper

  replaced __syscall() with __sysret() and merged two separated patches of v2 to one.

Did run-user tests for rv32 (with [3]), rv64 and arm64.

BTW, two questions for Thomas,

* This commit 659a49abc9c2 ("tools/nolibc: validate C89 compatibility")
  enables -std=c89, why not gnu11 used by kernel ? ;-)

* Do we need to tune the order of the macros in unistd.h like this:

    #define _syscall(N, ...) __sysret(my_syscall##N(__VA_ARGS__))
    #define _syscall_n(N, ...) _syscall(N, __VA_ARGS__)
    #define __syscall_narg(_0, _1, _2, _3, _4, _5, _6, N, ...) N
    #define _sycall_narg(...) __syscall_narg(__VA_ARGS__, 6, 5, 4, 3, 2, 1, 0)
    #define syscall(...) _syscall_n(_sycall_narg(__VA_ARGS__), ##__VA_ARGS__)

  Before, It works but seems not put in using order:

    #define _syscall(N, ...) __sysret(my_syscall##N(__VA_ARGS__))
    #define _sycall_narg(...) __syscall_narg(__VA_ARGS__, 6, 5, 4, 3, 2, 1, 0)
    #define __syscall_narg(_0, _1, _2, _3, _4, _5, _6, N, ...) N
    #define _syscall_n(N, ...) _syscall(N, __VA_ARGS__)
    #define syscall(...) _syscall_n(_sycall_narg(__VA_ARGS__), ##__VA_ARGS__)

Thanks.

Best regards,
Zhangjin

---
[1]: https://lore.kernel.org/linux-riscv/cover.1686036862.git.falcon@tinylab.org/
[2]: https://git.kernel.org/pub/scm/linux/kernel/git/wtarreau/nolibc.git
[3]: https://lore.kernel.org/linux-riscv/cover.1686128703.git.falcon@tinylab.org/T/#t

Zhangjin Wu (3):
  tools/nolibc: sys.h: add a syscall return helper
  tools/nolibc: unistd.h: apply __sysret() helper
  tools/nolibc: sys.h: apply __sysret() helper

 tools/include/nolibc/sys.h    | 364 +++++-----------------------------
 tools/include/nolibc/unistd.h |  11 +-
 2 files changed, 55 insertions(+), 320 deletions(-)

-- 
2.25.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v3 1/3] tools/nolibc: sys.h: add a syscall return helper
  2023-06-07 11:28 [PATCH v3 0/3] tools/nolibc: add a new syscall helper Zhangjin Wu
@ 2023-06-07 11:30 ` Zhangjin Wu
  2023-06-07 11:31 ` [PATCH v3 2/3] tools/nolibc: unistd.h: apply __sysret() helper Zhangjin Wu
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Zhangjin Wu @ 2023-06-07 11:30 UTC (permalink / raw)
  To: thomas, w
  Cc: falcon, arnd, linux-kernel, linux-kselftest, linux-riscv,
	Thomas Weißschuh

Most of the library routines share the same syscall return logic:

  In general, a 0 return value indicates success.  A -1 return value
  indicates an error, and an error number is stored in errno. [1]

Let's add a __sysret() helper for the above logic to simplify the coding
and shrink the code lines too.

Thomas suggested to use inline function instead of macro for __sysret().

Willy suggested to make __sysret() be always inline.

[1]: https://man7.org/linux/man-pages/man2/syscall.2.html

Suggested-by: Willy Tarreau <w@1wt.eu>
Link: https://lore.kernel.org/linux-riscv/ZH1+hkhiA2+ItSvX@1wt.eu/
Suggested-by: Thomas Weißschuh <linux@weissschuh.net>
Link: https://lore.kernel.org/linux-riscv/ea4e7442-7223-4211-ba29-70821e907888@t-8ch.de/
Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
---
 tools/include/nolibc/sys.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
index 856249a11890..150777207468 100644
--- a/tools/include/nolibc/sys.h
+++ b/tools/include/nolibc/sys.h
@@ -28,6 +28,16 @@
 #include "errno.h"
 #include "types.h"
 
+/* Syscall return helper, set errno as -ret when ret < 0 */
+static __inline__ __attribute__((unused, always_inline))
+long __sysret(long ret)
+{
+	if (ret < 0) {
+		SET_ERRNO(-ret);
+		ret = -1;
+	}
+	return ret;
+}
 
 /* Functions in this file only describe syscalls. They're declared static so
  * that the compiler usually decides to inline them while still being allowed
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v3 2/3] tools/nolibc: unistd.h: apply __sysret() helper
  2023-06-07 11:28 [PATCH v3 0/3] tools/nolibc: add a new syscall helper Zhangjin Wu
  2023-06-07 11:30 ` [PATCH v3 1/3] tools/nolibc: sys.h: add a syscall return helper Zhangjin Wu
@ 2023-06-07 11:31 ` Zhangjin Wu
  2023-06-07 11:32 ` [PATCH v3 3/3] tools/nolibc: sys.h: " Zhangjin Wu
  2023-06-07 21:41 ` [PATCH v3 0/3] tools/nolibc: add a new syscall helper Thomas Weißschuh
  3 siblings, 0 replies; 6+ messages in thread
From: Zhangjin Wu @ 2023-06-07 11:31 UTC (permalink / raw)
  To: thomas, w; +Cc: falcon, arnd, linux-kernel, linux-kselftest, linux-riscv

Use __sysret() to shrink the whole _syscall() to oneline code.

Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
---
 tools/include/nolibc/unistd.h | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)

diff --git a/tools/include/nolibc/unistd.h b/tools/include/nolibc/unistd.h
index c20b2fbf065e..7e3c005d28ba 100644
--- a/tools/include/nolibc/unistd.h
+++ b/tools/include/nolibc/unistd.h
@@ -56,16 +56,7 @@ int tcsetpgrp(int fd, pid_t pid)
 	return ioctl(fd, TIOCSPGRP, &pid);
 }
 
-#define _syscall(N, ...)                                                      \
-({                                                                            \
-	long _ret = my_syscall##N(__VA_ARGS__);                               \
-	if (_ret < 0) {                                                       \
-		SET_ERRNO(-_ret);                                             \
-		_ret = -1;                                                    \
-	}                                                                     \
-	_ret;                                                                 \
-})
-
+#define _syscall(N, ...) __sysret(my_syscall##N(__VA_ARGS__))
 #define _sycall_narg(...) __syscall_narg(__VA_ARGS__, 6, 5, 4, 3, 2, 1, 0)
 #define __syscall_narg(_0, _1, _2, _3, _4, _5, _6, N, ...) N
 #define _syscall_n(N, ...) _syscall(N, __VA_ARGS__)
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v3 3/3] tools/nolibc: sys.h: apply __sysret() helper
  2023-06-07 11:28 [PATCH v3 0/3] tools/nolibc: add a new syscall helper Zhangjin Wu
  2023-06-07 11:30 ` [PATCH v3 1/3] tools/nolibc: sys.h: add a syscall return helper Zhangjin Wu
  2023-06-07 11:31 ` [PATCH v3 2/3] tools/nolibc: unistd.h: apply __sysret() helper Zhangjin Wu
@ 2023-06-07 11:32 ` Zhangjin Wu
  2023-06-07 21:41 ` [PATCH v3 0/3] tools/nolibc: add a new syscall helper Thomas Weißschuh
  3 siblings, 0 replies; 6+ messages in thread
From: Zhangjin Wu @ 2023-06-07 11:32 UTC (permalink / raw)
  To: thomas, w; +Cc: falcon, arnd, linux-kernel, linux-kselftest, linux-riscv

Use __sysret() to shrink most of the library routines to oneline code.

Removed 266 lines of duplicated code.

Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
---
 tools/include/nolibc/sys.h | 354 +++++--------------------------------
 1 file changed, 44 insertions(+), 310 deletions(-)

diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
index 150777207468..4fbefe5adf93 100644
--- a/tools/include/nolibc/sys.h
+++ b/tools/include/nolibc/sys.h
@@ -76,13 +76,7 @@ void *sys_brk(void *addr)
 static __attribute__((unused))
 int brk(void *addr)
 {
-	void *ret = sys_brk(addr);
-
-	if (!ret) {
-		SET_ERRNO(ENOMEM);
-		return -1;
-	}
-	return 0;
+	return __sysret(sys_brk(addr) ? 0 : -ENOMEM);
 }
 
 static __attribute__((unused))
@@ -112,13 +106,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 __sysret(sys_chdir(path));
 }
 
 
@@ -141,13 +129,7 @@ int sys_chmod(const char *path, mode_t mode)
 static __attribute__((unused))
 int chmod(const char *path, mode_t mode)
 {
-	int ret = sys_chmod(path, mode);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_chmod(path, mode));
 }
 
 
@@ -170,13 +152,7 @@ int sys_chown(const char *path, uid_t owner, gid_t group)
 static __attribute__((unused))
 int chown(const char *path, uid_t owner, gid_t group)
 {
-	int ret = sys_chown(path, owner, group);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_chown(path, owner, group));
 }
 
 
@@ -193,13 +169,7 @@ int sys_chroot(const char *path)
 static __attribute__((unused))
 int chroot(const char *path)
 {
-	int ret = sys_chroot(path);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_chroot(path));
 }
 
 
@@ -216,13 +186,7 @@ int sys_close(int fd)
 static __attribute__((unused))
 int close(int fd)
 {
-	int ret = sys_close(fd);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_close(fd));
 }
 
 
@@ -239,13 +203,7 @@ int sys_dup(int fd)
 static __attribute__((unused))
 int dup(int fd)
 {
-	int ret = sys_dup(fd);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_dup(fd));
 }
 
 
@@ -268,13 +226,7 @@ int sys_dup2(int old, int new)
 static __attribute__((unused))
 int dup2(int old, int new)
 {
-	int ret = sys_dup2(old, new);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_dup2(old, new));
 }
 
 
@@ -292,13 +244,7 @@ int sys_dup3(int old, int new, int flags)
 static __attribute__((unused))
 int dup3(int old, int new, int flags)
 {
-	int ret = sys_dup3(old, new, flags);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_dup3(old, new, flags));
 }
 #endif
 
@@ -316,13 +262,7 @@ int sys_execve(const char *filename, char *const argv[], char *const envp[])
 static __attribute__((unused))
 int execve(const char *filename, char *const argv[], char *const envp[])
 {
-	int ret = sys_execve(filename, argv, envp);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_execve(filename, argv, envp));
 }
 
 
@@ -369,13 +309,7 @@ pid_t sys_fork(void)
 static __attribute__((unused))
 pid_t fork(void)
 {
-	pid_t ret = sys_fork();
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_fork());
 }
 
 
@@ -392,13 +326,7 @@ int sys_fsync(int fd)
 static __attribute__((unused))
 int fsync(int fd)
 {
-	int ret = sys_fsync(fd);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_fsync(fd));
 }
 
 
@@ -415,13 +343,7 @@ int sys_getdents64(int fd, struct linux_dirent64 *dirp, int count)
 static __attribute__((unused))
 int getdents64(int fd, struct linux_dirent64 *dirp, int count)
 {
-	int ret = sys_getdents64(fd, dirp, count);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_getdents64(fd, dirp, count));
 }
 
 
@@ -459,13 +381,7 @@ pid_t sys_getpgid(pid_t pid)
 static __attribute__((unused))
 pid_t getpgid(pid_t pid)
 {
-	pid_t ret = sys_getpgid(pid);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_getpgid(pid));
 }
 
 
@@ -545,15 +461,7 @@ static unsigned long getauxval(unsigned long key);
 static __attribute__((unused))
 long getpagesize(void)
 {
-	long ret;
-
-	ret = getauxval(AT_PAGESZ);
-	if (!ret) {
-		SET_ERRNO(ENOENT);
-		return -1;
-	}
-
-	return ret;
+	return __sysret(getauxval(AT_PAGESZ) ?: -ENOENT);
 }
 
 
@@ -570,13 +478,7 @@ int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
 static __attribute__((unused))
 int gettimeofday(struct timeval *tv, struct timezone *tz)
 {
-	int ret = sys_gettimeofday(tv, tz);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_gettimeofday(tv, tz));
 }
 
 
@@ -614,13 +516,7 @@ int sys_ioctl(int fd, unsigned long req, void *value)
 static __attribute__((unused))
 int ioctl(int fd, unsigned long req, void *value)
 {
-	int ret = sys_ioctl(fd, req, value);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_ioctl(fd, req, value));
 }
 
 /*
@@ -636,13 +532,7 @@ int sys_kill(pid_t pid, int signal)
 static __attribute__((unused))
 int kill(pid_t pid, int signal)
 {
-	int ret = sys_kill(pid, signal);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_kill(pid, signal));
 }
 
 
@@ -665,13 +555,7 @@ int sys_link(const char *old, const char *new)
 static __attribute__((unused))
 int link(const char *old, const char *new)
 {
-	int ret = sys_link(old, new);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_link(old, new));
 }
 
 
@@ -688,13 +572,7 @@ off_t sys_lseek(int fd, off_t offset, int whence)
 static __attribute__((unused))
 off_t lseek(int fd, off_t offset, int whence)
 {
-	off_t ret = sys_lseek(fd, offset, whence);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_lseek(fd, offset, whence));
 }
 
 
@@ -717,13 +595,7 @@ int sys_mkdir(const char *path, mode_t mode)
 static __attribute__((unused))
 int mkdir(const char *path, mode_t mode)
 {
-	int ret = sys_mkdir(path, mode);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_mkdir(path, mode));
 }
 
 
@@ -746,13 +618,7 @@ long sys_mknod(const char *path, mode_t mode, dev_t dev)
 static __attribute__((unused))
 int mknod(const char *path, mode_t mode, dev_t dev)
 {
-	int ret = sys_mknod(path, mode, dev);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_mknod(path, mode, dev));
 }
 
 #ifndef MAP_SHARED
@@ -810,13 +676,7 @@ int sys_munmap(void *addr, size_t length)
 static __attribute__((unused))
 int munmap(void *addr, size_t length)
 {
-	int ret = sys_munmap(addr, length);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_munmap(addr, length));
 }
 
 /*
@@ -836,13 +696,7 @@ int mount(const char *src, const char *tgt,
           const char *fst, unsigned long flags,
           const void *data)
 {
-	int ret = sys_mount(src, tgt, fst, flags, data);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_mount(src, tgt, fst, flags, data));
 }
 
 
@@ -876,13 +730,7 @@ int open(const char *path, int flags, ...)
 		va_end(args);
 	}
 
-	ret = sys_open(path, flags, mode);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_open(path, flags, mode));
 }
 
 
@@ -902,13 +750,7 @@ static __attribute__((unused))
 int prctl(int option, unsigned long arg2, unsigned long arg3,
 		      unsigned long arg4, unsigned long arg5)
 {
-	int ret = sys_prctl(option, arg2, arg3, arg4, arg5);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_prctl(option, arg2, arg3, arg4, arg5));
 }
 
 
@@ -925,13 +767,7 @@ int sys_pivot_root(const char *new, const char *old)
 static __attribute__((unused))
 int pivot_root(const char *new, const char *old)
 {
-	int ret = sys_pivot_root(new, old);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_pivot_root(new, old));
 }
 
 
@@ -960,13 +796,7 @@ int sys_poll(struct pollfd *fds, int nfds, int timeout)
 static __attribute__((unused))
 int poll(struct pollfd *fds, int nfds, int timeout)
 {
-	int ret = sys_poll(fds, nfds, timeout);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_poll(fds, nfds, timeout));
 }
 
 
@@ -983,13 +813,7 @@ ssize_t sys_read(int fd, void *buf, size_t count)
 static __attribute__((unused))
 ssize_t read(int fd, void *buf, size_t count)
 {
-	ssize_t ret = sys_read(fd, buf, count);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_read(fd, buf, count));
 }
 
 
@@ -1007,13 +831,7 @@ ssize_t sys_reboot(int magic1, int magic2, int cmd, void *arg)
 static __attribute__((unused))
 int reboot(int cmd)
 {
-	int ret = sys_reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, 0);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, 0));
 }
 
 
@@ -1030,13 +848,7 @@ int sys_sched_yield(void)
 static __attribute__((unused))
 int sched_yield(void)
 {
-	int ret = sys_sched_yield();
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_sched_yield());
 }
 
 
@@ -1076,13 +888,7 @@ int sys_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeva
 static __attribute__((unused))
 int select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
 {
-	int ret = sys_select(nfds, rfds, wfds, efds, timeout);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_select(nfds, rfds, wfds, efds, timeout));
 }
 
 
@@ -1099,13 +905,7 @@ int sys_setpgid(pid_t pid, pid_t pgid)
 static __attribute__((unused))
 int setpgid(pid_t pid, pid_t pgid)
 {
-	int ret = sys_setpgid(pid, pgid);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_setpgid(pid, pgid));
 }
 
 
@@ -1122,13 +922,7 @@ pid_t sys_setsid(void)
 static __attribute__((unused))
 pid_t setsid(void)
 {
-	pid_t ret = sys_setsid();
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_setsid());
 }
 
 #if defined(__NR_statx)
@@ -1145,13 +939,7 @@ int sys_statx(int fd, const char *path, int flags, unsigned int mask, struct sta
 static __attribute__((unused))
 int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf)
 {
-	int ret = sys_statx(fd, path, flags, mask, buf);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_statx(fd, path, flags, mask, buf));
 }
 #endif
 
@@ -1231,13 +1019,7 @@ int sys_stat(const char *path, struct stat *buf)
 static __attribute__((unused))
 int stat(const char *path, struct stat *buf)
 {
-	int ret = sys_stat(path, buf);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_stat(path, buf));
 }
 
 
@@ -1260,13 +1042,7 @@ int sys_symlink(const char *old, const char *new)
 static __attribute__((unused))
 int symlink(const char *old, const char *new)
 {
-	int ret = sys_symlink(old, new);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_symlink(old, new));
 }
 
 
@@ -1300,13 +1076,7 @@ int sys_umount2(const char *path, int flags)
 static __attribute__((unused))
 int umount2(const char *path, int flags)
 {
-	int ret = sys_umount2(path, flags);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_umount2(path, flags));
 }
 
 
@@ -1329,13 +1099,7 @@ int sys_unlink(const char *path)
 static __attribute__((unused))
 int unlink(const char *path)
 {
-	int ret = sys_unlink(path);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_unlink(path));
 }
 
 
@@ -1354,38 +1118,20 @@ pid_t sys_wait4(pid_t pid, int *status, int options, struct rusage *rusage)
 static __attribute__((unused))
 pid_t wait(int *status)
 {
-	pid_t ret = sys_wait4(-1, status, 0, NULL);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_wait4(-1, status, 0, NULL));
 }
 
 static __attribute__((unused))
 pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage)
 {
-	pid_t ret = sys_wait4(pid, status, options, rusage);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_wait4(pid, status, options, rusage));
 }
 
 
 static __attribute__((unused))
 pid_t waitpid(pid_t pid, int *status, int options)
 {
-	pid_t ret = sys_wait4(pid, status, options, NULL);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_wait4(pid, status, options, NULL));
 }
 
 
@@ -1402,13 +1148,7 @@ ssize_t sys_write(int fd, const void *buf, size_t count)
 static __attribute__((unused))
 ssize_t write(int fd, const void *buf, size_t count)
 {
-	ssize_t ret = sys_write(fd, buf, count);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_write(fd, buf, count));
 }
 
 
@@ -1425,13 +1165,7 @@ int sys_memfd_create(const char *name, unsigned int flags)
 static __attribute__((unused))
 int memfd_create(const char *name, unsigned int flags)
 {
-	ssize_t ret = sys_memfd_create(name, flags);
-
-	if (ret < 0) {
-		SET_ERRNO(-ret);
-		ret = -1;
-	}
-	return ret;
+	return __sysret(sys_memfd_create(name, flags));
 }
 
 /* make sure to include all global symbols */
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v3 0/3] tools/nolibc: add a new syscall helper
  2023-06-07 11:28 [PATCH v3 0/3] tools/nolibc: add a new syscall helper Zhangjin Wu
                   ` (2 preceding siblings ...)
  2023-06-07 11:32 ` [PATCH v3 3/3] tools/nolibc: sys.h: " Zhangjin Wu
@ 2023-06-07 21:41 ` Thomas Weißschuh
  2023-06-08 10:10   ` Zhangjin Wu
  3 siblings, 1 reply; 6+ messages in thread
From: Thomas Weißschuh @ 2023-06-07 21:41 UTC (permalink / raw)
  To: Zhangjin Wu; +Cc: w, arnd, linux-kernel, linux-kselftest, linux-riscv

On 2023-06-07 19:28:58+0800, Zhangjin Wu wrote:
> Willy, Thomas
> 
> This is the revision of the v2 syscall helpers [1], it is based on
> 20230606-nolibc-rv32+stkp7a of [2]. It doesn't conflict with the v4 of
> -ENOSYS patchset [3], so, it is ok to simply merge both of them.
> 
> This revision mainly applied Thomas' method, removed the __syscall()
> helper and replaced it with __sysret() instead, because __syscall()
> looks like _syscall() and syscall(), it may mixlead the developers.
> 
> Changes from v2 -> v3:
> 
> * tools/nolibc: sys.h: add a syscall return helper
> 
>   * The __syscall() is removed.
> 
>   * Align the code style of __sysret() with the others, and use
>     __inline__ instead of inline (like stdlib.h) to let it work with
>     the default -std=c89 in tools/testing/selftests/nolibc/Makefile
> 
> * tools/nolibc: unistd.h: apply __sysret() helper
> 
>   As v2.
> 
> * tools/nolibc: sys.h: apply __sysret() helper
> 
>   replaced __syscall() with __sysret() and merged two separated patches of v2 to one.
> 
> Did run-user tests for rv32 (with [3]), rv64 and arm64.
> 
> BTW, two questions for Thomas,
> 
> * This commit 659a49abc9c2 ("tools/nolibc: validate C89 compatibility")
>   enables -std=c89, why not gnu11 used by kernel ? ;-)

Because nolibc needs to support whatever its users need.
As nolibc is header-only all of it needs to work everywhere.
C89 should work for everybody :-)

The kernel on the other hand is compiled standalone and is not limited
by its users.

See the discussion here:

https://lore.kernel.org/all/20230328-nolibc-c99-v2-0-c989f2289222@weissschuh.net/
https://lore.kernel.org/all/20230328-nolibc-c99-v1-1-a8302fb19f19@weissschuh.net/

> * Do we need to tune the order of the macros in unistd.h like this:
> 
>     #define _syscall(N, ...) __sysret(my_syscall##N(__VA_ARGS__))
>     #define _syscall_n(N, ...) _syscall(N, __VA_ARGS__)
>     #define __syscall_narg(_0, _1, _2, _3, _4, _5, _6, N, ...) N
>     #define _sycall_narg(...) __syscall_narg(__VA_ARGS__, 6, 5, 4, 3, 2, 1, 0)
>     #define syscall(...) _syscall_n(_sycall_narg(__VA_ARGS__), ##__VA_ARGS__)
> 
>   Before, It works but seems not put in using order:
> 
>     #define _syscall(N, ...) __sysret(my_syscall##N(__VA_ARGS__))
>     #define _sycall_narg(...) __syscall_narg(__VA_ARGS__, 6, 5, 4, 3, 2, 1, 0)
>     #define __syscall_narg(_0, _1, _2, _3, _4, _5, _6, N, ...) N
>     #define _syscall_n(N, ...) _syscall(N, __VA_ARGS__)
>     #define syscall(...) _syscall_n(_sycall_narg(__VA_ARGS__), ##__VA_ARGS__)

Not sure it makes a big difference.
If you want to change it, go for it.

> Thanks.
> 
> Best regards,
> Zhangjin
> 
> ---
> [1]: https://lore.kernel.org/linux-riscv/cover.1686036862.git.falcon@tinylab.org/
> [2]: https://git.kernel.org/pub/scm/linux/kernel/git/wtarreau/nolibc.git
> [3]: https://lore.kernel.org/linux-riscv/cover.1686128703.git.falcon@tinylab.org/T/#t
> 
> Zhangjin Wu (3):
>   tools/nolibc: sys.h: add a syscall return helper
>   tools/nolibc: unistd.h: apply __sysret() helper
>   tools/nolibc: sys.h: apply __sysret() helper
> 
>  tools/include/nolibc/sys.h    | 364 +++++-----------------------------
>  tools/include/nolibc/unistd.h |  11 +-
>  2 files changed, 55 insertions(+), 320 deletions(-)

For the full series:

Reviewed-by: Thomas Weißschuh <linux@weissschuh.net>

Thanks,
Thomas

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3 0/3] tools/nolibc: add a new syscall helper
  2023-06-07 21:41 ` [PATCH v3 0/3] tools/nolibc: add a new syscall helper Thomas Weißschuh
@ 2023-06-08 10:10   ` Zhangjin Wu
  0 siblings, 0 replies; 6+ messages in thread
From: Zhangjin Wu @ 2023-06-08 10:10 UTC (permalink / raw)
  To: thomas; +Cc: arnd, falcon, linux-kernel, linux-kselftest, linux-riscv, w

> On 2023-06-07 19:28:58+0800, Zhangjin Wu wrote:
> > Willy, Thomas
> > 
> > This is the revision of the v2 syscall helpers [1], it is based on
> > 20230606-nolibc-rv32+stkp7a of [2]. It doesn't conflict with the v4 of
> > -ENOSYS patchset [3], so, it is ok to simply merge both of them.
> > 
> > This revision mainly applied Thomas' method, removed the __syscall()
> > helper and replaced it with __sysret() instead, because __syscall()
> > looks like _syscall() and syscall(), it may mixlead the developers.
> >
(...)
> > BTW, two questions for Thomas,
> > 
> > * This commit 659a49abc9c2 ("tools/nolibc: validate C89 compatibility")
> >   enables -std=c89, why not gnu11 used by kernel ? ;-)
> 
> Because nolibc needs to support whatever its users need.
> As nolibc is header-only all of it needs to work everywhere.
> C89 should work for everybody :-)
> 

Get it, thanks.

> The kernel on the other hand is compiled standalone and is not limited
> by its users.
> 
> See the discussion here:
> 
> https://lore.kernel.org/all/20230328-nolibc-c99-v2-0-c989f2289222@weissschuh.net/
> https://lore.kernel.org/all/20230328-nolibc-c99-v1-1-a8302fb19f19@weissschuh.net/
>

Thanks very much for sharing the whole history info.

And as the your commit 063b6bc5b39f ("tools/nolibc: use __inline__ syntax")
explains, the 'inline' keyword has been used in many headers of include/uapi/,
so, how our -std=c89 work with them? I did find the clue eventually, here maybe:

    $ grep -n inline scripts/headers_install.sh
    11:	echo "asm/inline/volatile keywords."
    37:	s/(^|[[:space:](])(inline|asm|volatile)([[:space:](]|$)/\1__\2__\3/g

The headers_install target helped us convert all of the new keywords to the old
ones, it's magic ;-)

So, it should work if people not want to try a -I/path/to/include/uapi/, I did
this for musl before, even If we do this, this may help:

    diff --git a/tools/include/nolibc/std.h b/tools/include/nolibc/std.h
    index 933bc0be7e1c..33d546cf9af0 100644
    --- a/tools/include/nolibc/std.h
    +++ b/tools/include/nolibc/std.h
    @@ -7,6 +7,14 @@
     #ifndef _NOLIBC_STD_H
     #define _NOLIBC_STD_H
     
    +#ifndef NOLIBC_TEST
    +#ifndef __STDC_VERSION__
    +#define inline __inline__
    +#define asm __asm__
    +#define volatile __volatile__
    +#endif
    +#endif
    +
     /* Declare a few quite common macros and types that usually are in stdlib.h,
      * stdint.h, ctype.h, unistd.h and a few other common locations. Please place
      * integer type definitions and generic macros here, but avoid OS-specific and
    diff --git a/tools/testing/selftests/nolibc/Makefile b/tools/testing/selftests/nolibc/Makefile
    index 4a3a105e1fdf..46f061a4458a 100644
    --- a/tools/testing/selftests/nolibc/Makefile
    +++ b/tools/testing/selftests/nolibc/Makefile
    @@ -78,7 +78,7 @@ endif

     CFLAGS_s390 = -m64
     CFLAGS_STACKPROTECTOR ?= $(call cc-option,-mstack-protector-guard=global $(call cc-option,-fstack-protector-all))
    -CFLAGS  ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 \
    +CFLAGS  ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 -DNOLIBC_TEST \
                    $(call cc-option,-fno-stack-protector) \
                    $(CFLAGS_$(ARCH)) $(CFLAGS_STACKPROTECTOR)

Is this worth a new patch? I do think it is not required.

> > * Do we need to tune the order of the macros in unistd.h like this:
> > 
> >     #define _syscall(N, ...) __sysret(my_syscall##N(__VA_ARGS__))
> >     #define _syscall_n(N, ...) _syscall(N, __VA_ARGS__)
> >     #define __syscall_narg(_0, _1, _2, _3, _4, _5, _6, N, ...) N
> >     #define _sycall_narg(...) __syscall_narg(__VA_ARGS__, 6, 5, 4, 3, 2, 1, 0)
> >     #define syscall(...) _syscall_n(_sycall_narg(__VA_ARGS__), ##__VA_ARGS__)
> > 
> >   Before, It works but seems not put in using order:
> > 
> >     #define _syscall(N, ...) __sysret(my_syscall##N(__VA_ARGS__))
> >     #define _sycall_narg(...) __syscall_narg(__VA_ARGS__, 6, 5, 4, 3, 2, 1, 0)
> >     #define __syscall_narg(_0, _1, _2, _3, _4, _5, _6, N, ...) N
> >     #define _syscall_n(N, ...) _syscall(N, __VA_ARGS__)
> >     #define syscall(...) _syscall_n(_sycall_narg(__VA_ARGS__), ##__VA_ARGS__)
> 
> Not sure it makes a big difference.
> If you want to change it, go for it.
>

Only switched two of them, oh, just found the '_sycall_narg' did miss a 's'
character, it may be really worth a patch now, I know why I focused on the
order so much, because the missing 's' made it not aligned well ;-)
 
> >
(...) 
> >  tools/include/nolibc/sys.h    | 364 +++++-----------------------------
> >  tools/include/nolibc/unistd.h |  11 +-
> >  2 files changed, 55 insertions(+), 320 deletions(-)
> 
> For the full series:
> 
> Reviewed-by: Thomas Weißschuh <linux@weissschuh.net>
>

Thanks a lot, I'm really appreciated.

Best regards,
Zhangjin
 
> Thanks,
> Thomas

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-06-08 10:10 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-07 11:28 [PATCH v3 0/3] tools/nolibc: add a new syscall helper Zhangjin Wu
2023-06-07 11:30 ` [PATCH v3 1/3] tools/nolibc: sys.h: add a syscall return helper Zhangjin Wu
2023-06-07 11:31 ` [PATCH v3 2/3] tools/nolibc: unistd.h: apply __sysret() helper Zhangjin Wu
2023-06-07 11:32 ` [PATCH v3 3/3] tools/nolibc: sys.h: " Zhangjin Wu
2023-06-07 21:41 ` [PATCH v3 0/3] tools/nolibc: add a new syscall helper Thomas Weißschuh
2023-06-08 10:10   ` Zhangjin Wu

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).