Add close_range01, SAFE_DUP2 and SAFE_CLONE
v2 cover Add close_range01, SAFE_DUP2 and SAFE_CLONE
Hi,
This series adds a test for the new system call close_range. The kernel selftests already added some tests, so I based this on those.
Also of note, is the addition of tst_clone and SAFE_CLONE which mirror the clone3 interface. This is not really necessary for this test as it is unlikely close_range is backported, but clone3 is not. OTOH I think it will be usefull for other tests especially as the libc clone wrapper seems to force the user to allocate the stack even though the kernel can handle this. Also the clone_args is growing rapidly.
The old API has ltp_clone* functions, but I think we will have an easier time tracking the clone3 interface, than what we were doing with the old API.
V2:
* Add docs for tst_clone
* Add lapi/close_range.h
Richard Palethorpe (5):
API: Add close_range syscall wrapper
API: Add SAFE_DUP2
API: Fix clone.h
API: Add tst_clone
close_range: Add test
configure.ac | 2 + doc/test-writing-guidelines.txt | 27 ++- include/lapi/clone.h | 5 +- include/lapi/close_range.h | 28 +++ include/lapi/syscalls/aarch64.in | 1 + include/lapi/syscalls/arc.in | 1 + include/lapi/syscalls/arm.in | 1 + include/lapi/syscalls/hppa.in | 1 + include/lapi/syscalls/i386.in | 1 + include/lapi/syscalls/ia64.in | 1 + include/lapi/syscalls/mips_n32.in | 1 + include/lapi/syscalls/mips_n64.in | 1 + include/lapi/syscalls/mips_o32.in | 1 + include/lapi/syscalls/powerpc.in | 1 + include/lapi/syscalls/powerpc64.in | 1 + include/lapi/syscalls/s390.in | 1 + include/lapi/syscalls/s390x.in | 1 + include/lapi/syscalls/sh.in | 1 + include/lapi/syscalls/sparc.in | 1 + include/lapi/syscalls/sparc64.in | 1 + include/lapi/syscalls/x86_64.in | 1 + include/tst_clone.h | 28 +++ include/tst_safe_macros.h | 5 + include/tst_test.h | 2 +- lib/tst_clone.c | 46 ++++ lib/tst_safe_macros.c | 18 ++ lib/tst_test.c | 25 +++ .../kernel/syscalls/close_range/.gitignore | 1 + .../kernel/syscalls/close_range/Makefile | 10 + .../syscalls/close_range/close_range01.c | 200 ++++++++++++++++++ 30 files changed, 406 insertions(+), 8 deletions(-) create mode 100644 include/lapi/close_range.h create mode 100644 lib/tst_clone.c create mode 100644 testcases/kernel/syscalls/close_range/.gitignore create mode 100644 testcases/kernel/syscalls/close_range/Makefile create mode 100644 testcases/kernel/syscalls/close_range/close_range01.c
Signature · 1 line
2.30.0
PATCH v2 1/5 API: Add close_range syscall wrapper
Add the syscall number and a wrapper function. Presently there is no wrapper function in libc.
--- configure.ac | 2 ++ include/lapi/close_range.h | 28 ++++++++++++++++++++++++++++ include/lapi/syscalls/aarch64.in | 1 + include/lapi/syscalls/arc.in | 1 + include/lapi/syscalls/arm.in | 1 + include/lapi/syscalls/hppa.in | 1 + include/lapi/syscalls/i386.in | 1 + include/lapi/syscalls/ia64.in | 1 + include/lapi/syscalls/mips_n32.in | 1 + include/lapi/syscalls/mips_n64.in | 1 + include/lapi/syscalls/mips_o32.in | 1 + include/lapi/syscalls/powerpc.in | 1 + include/lapi/syscalls/powerpc64.in | 1 + include/lapi/syscalls/s390.in | 1 + include/lapi/syscalls/s390x.in | 1 + include/lapi/syscalls/sh.in | 1 + include/lapi/syscalls/sparc.in | 1 + include/lapi/syscalls/sparc64.in | 1 + include/lapi/syscalls/x86_64.in | 1 + 19 files changed, 47 insertions(+) create mode 100644 include/lapi/close_range.h
configure.ac +2 -0
@@ -43,6 +43,7 @@ AC_CHECK_HEADERS_ONCE([ \ linux/can.h \ linux/cgroupstats.h \ linux/cryptouser.h \ + linux/close_range.h \ linux/dccp.h \ linux/fs.h \ linux/genetlink.h \
@@ -77,6 +78,7 @@ AC_SUBST(HAVE_FTS_H, $have_fts) AC_CHECK_FUNCS_ONCE([ \ clone3 \ + close_range \ copy_file_range \ epoll_pwait \ execveat \
include/lapi/close_range.h new file +28 -0
@@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* Copyright (c) 2021 SUSE LLC */ + +#ifndef LAPI_CLOSE_RANGE__ +# define LAPI_CLOSE_RANGE__ + +# include "lapi/syscalls.h" + +# ifdef HAVE_LINUX_CLOSE_RANGE_H +# include <linux/close_range.h> +# endif + +# ifndef CLOSE_RANGE_UNSHARE +# define CLOSE_RANGE_UNSHARE (1U << 1) +# endif + +# ifndef CLOSE_RANGE_CLOEXEC +# define CLOSE_RANGE_CLOEXEC (1U << 2) +# endif + +# ifndef HAVE_CLOSE_RANGE +static inline int close_range (unsigned int fd,unsigned int max_fd, +unsigned int flags) +{ +return tst_syscall (__NR_close_range, fd, max_fd, flags); +} +# endif +#endif /* LAPI_CLOSE_RANGE_H__ */
include/lapi/syscalls/aarch64.in +1 -0
@@ -310,6 +310,7 @@ fsmount 432 fspick 433 pidfd_open 434 clone3 435 +close_range 436 openat2 437 pidfd_getfd 438 _sysctl 1078
include/lapi/syscalls/arc.in +1 -0
@@ -310,5 +310,6 @@ fsmount 432 fspick 433 pidfd_open 434 clone3 435 +close_range 436 openat2 437 pidfd_getfd 438
include/lapi/syscalls/arm.in +1 -0
@@ -388,5 +388,6 @@ fsmount (__NR_SYSCALL_BASE+432) fspick (__NR_SYSCALL_BASE+433) pidfd_open (__NR_SYSCALL_BASE+434) clone3 (__NR_SYSCALL_BASE+435) +close_range (__NR_SYSCALL_BASE+436) openat2 (__NR_SYSCALL_BASE+437) pidfd_getfd (__NR_SYSCALL_BASE+438)
include/lapi/syscalls/hppa.in +1 -0
@@ -39,3 +39,4 @@ fsconfig 431 fsmount 432 fspick 433 pidfd_open 434 +close_range 436
include/lapi/syscalls/i386.in +1 -0
@@ -424,5 +424,6 @@ fsmount 432 fspick 433 pidfd_open 434 clone3 435 +close_range 436 openat2 437 pidfd_getfd 438
include/lapi/syscalls/ia64.in +1 -0
@@ -337,5 +337,6 @@ fsconfig 1455 fsmount 1456 fspick 1457 pidfd_open 1458 +close_range 1460 openat2 1461 pidfd_getfd 1462
include/lapi/syscalls/mips_n32.in +1 -0
@@ -362,5 +362,6 @@ fsmount 432 fspick 433 pidfd_open 434 clone3 435 +close_range 436 openat2 437 pidfd_getfd 438
include/lapi/syscalls/mips_n64.in +1 -0
@@ -338,5 +338,6 @@ fsmount 432 fspick 433 pidfd_open 434 clone3 435 +close_range 436 openat2 437 pidfd_getfd 438
include/lapi/syscalls/mips_o32.in +1 -0
@@ -408,5 +408,6 @@ fsmount 432 fspick 433 pidfd_open 434 clone3 435 +close_range 436 openat2 437 pidfd_getfd 438
include/lapi/syscalls/powerpc.in +1 -0
@@ -417,5 +417,6 @@ fsmount 432 fspick 433 pidfd_open 434 clone3 435 +close_range 436 openat2 437 pidfd_getfd 438
include/lapi/syscalls/powerpc64.in +1 -0
@@ -417,5 +417,6 @@ fsmount 432 fspick 433 pidfd_open 434 clone3 435 +close_range 436 openat2 437 pidfd_getfd 438
include/lapi/syscalls/s390.in +1 -0
@@ -404,5 +404,6 @@ fsmount 432 fspick 433 pidfd_open 434 clone3 435 +close_range 436 openat2 437 pidfd_getfd 438
include/lapi/syscalls/s390x.in +1 -0
@@ -352,5 +352,6 @@ fsmount 432 fspick 433 pidfd_open 434 clone3 435 +close_range 436 openat2 437 pidfd_getfd 438
include/lapi/syscalls/sh.in +1 -0
@@ -398,5 +398,6 @@ fsconfig 431 fsmount 432 fspick 433 pidfd_open 434 +close_range 436 openat2 437 pidfd_getfd 438
include/lapi/syscalls/sparc.in +1 -0
@@ -403,5 +403,6 @@ fsconfig 431 fsmount 432 fspick 433 pidfd_open 434 +close_range 436 openat2 437 pidfd_getfd 438
include/lapi/syscalls/sparc64.in +1 -0
@@ -368,5 +368,6 @@ fsconfig 431 fsmount 432 fspick 433 pidfd_open 434 +close_range 436 openat2 437 pidfd_getfd 438
include/lapi/syscalls/x86_64.in +1 -0
@@ -345,6 +345,7 @@ fsmount 432 fspick 433 pidfd_open 434 clone3 435 +close_range 436 openat2 437 pidfd_getfd 438 rt_sigaction 512
Signature · 1 line
2.30.0
PATCH v2 2/5 API: Add SAFE_DUP2
In reply to Richard Palethorpe--- include/tst_safe_macros.h | 5 +++++ lib/tst_safe_macros.c | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+)
include/tst_safe_macros.h +5 -0
@@ -50,6 +50,11 @@ int safe_dup(const char *file, const int lineno, int oldfd);#define SAFE_DUP (oldfd) \ safe_dup(__FILE__, __LINE__, (oldfd)) +int safe_dup2 (const char *file,const int lineno,int oldfd,int newfd); + +#define SAFE_DUP2 (oldfd, newfd) \ + safe_dup2(__FILE__, __LINE__, (oldfd), (newfd)) +#define SAFE_GETCWD (buf, size) \ safe_getcwd(__FILE__, __LINE__, NULL, (buf), (size))
lib/tst_safe_macros.c +18 -0
@@ -415,6 +415,24 @@ int safe_dup(const char *file, const int lineno, int oldfd)return rval; } +int safe_dup2 (const char *file,const int lineno,int oldfd,int newfd) +{ +int rval; + + rval =dup2 (oldfd, newfd); + +if (rval ==-1 ) { +tst_brk_ (file, lineno,TBROK |TERRNO , +"dup2(%i, %i) failed" , oldfd, newfd); + }else if (rval != newfd) { +tst_brk_ (file, lineno,TBROK |TERRNO , +"Invalid dup2(%i, %i) return value %d" , + oldfd, newfd, rval); + } + +return rval; +} +sighandler_t safe_signal (const char *file,const int lineno,int signum,sighandler_t handler) {
Signature · 1 line
2.30.0
PATCH v2 3/5 API: Fix clone.h
In reply to Richard PalethorpeFunctions in headers should be static inline. Also add stdint.h because it results in confusing error messages if it is missing.
--- include/lapi/clone.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
include/lapi/clone.h +3 -2
@@ -10,6 +10,7 @@#include <sys/syscall.h> #include <linux/types.h> #include <sched.h> +#include <stdint.h> #include "config.h" #include "lapi/syscalls.h"
@@ -26,7 +27,7 @@ struct clone_args {uint64_t __attribute__((aligned (8 ))) tls; }; -int clone3 (struct clone_args *args,size_t size) +static inline int clone3 (struct clone_args *args,size_t size) {return tst_syscall (__NR_clone3, args, size); }
@@ -36,7 +37,7 @@ int clone3(struct clone_args *args, size_t size)#define CLONE_PIDFD 0x00001000/* set if a pidfd should be placed in parent */ #endif -void clone3_supported_by_kernel (void ) +static inline void clone3_supported_by_kernel (void ) {if ((tst_kvercmp (5 ,3 ,0 )) <0 ) {/* Check if the syscall is backported on an older kernel */
Signature · 1 line
2.30.0
PATCH v2 4/5 API: Add tst_clone
In reply to Richard PalethorpeThe raw clone system call and clone3 have relatively simple interfaces if the stack pointer is set to NULL. The libc wrapper complicates things hugely. So introduce an interface similar to clone3, but that falls back to clone.
Not all features of clone3 are implemented in clone, but we could either return TCONF or implement them in user land.
--- doc/test-writing-guidelines.txt | 27 +++++++++++++++---- include/tst_clone.h | 28 ++++++++++++++++++++ include/tst_test.h | 2 +- lib/tst_clone.c | 46 +++++++++++++++++++++++++++++++++ lib/tst_test.c | 25 ++++++++++++++++++ 5 files changed, 122 insertions(+), 6 deletions(-) create mode 100644 lib/tst_clone.c
doc/test-writing-guidelines.txt +22 -5
@@ -547,9 +547,9 @@ void tst_flush(void); Flush output streams, handling errors appropriately. This function is rarely needed when you have to flush the output streams -before calling 'fork()' or 'clone()'. Note that the 'SAFE_FORK()' calls this -function automatically. See 3.4 FILE buffers and fork() for explanation why is -this needed. +before calling 'fork()' or 'clone()'. Note that the 'SAFE_FORK()' and 'SAFE_CLONE()' +calls this function automatically. See 3.4 FILE buffers and fork() for explanation +why is this needed. 2.2.3 Test temporary directory ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -732,10 +732,10 @@ This happens when 'stdout' gets redirected to a file. In this case, the 'stdout' is not line buffered, but block buffered. Hence after a fork content of the buffers will be printed by the parent and each of the children. -To avoid that you should use 'SAFE_FORK()'. +To avoid that you should use 'SAFE_FORK()', 'SAFE_CLONE()' or 'tst_clone()'. IMPORTANT: You have to set the '.forks_child' flag in the test structure - if your testcase forks. + if your testcase forks or calls 'SAFE_CLONE()'. 2.2.8 Doing the test in the child process ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -769,6 +769,23 @@ The 'tst_reap_children()' function makes the process wait for all of its children and exits with 'tst_brk(TBROK, ...)' if any of them returned a non zero exit code. +When using 'SAFE_CLONE' or 'tst_clone', this may not work depending on +the parameters passed to clone. The following call to 'SAFE_CLONE' is +identical to 'fork()', so will work as expected. + +[source,c] +-------------------------------------------------------------------------------- +const struct tst_clone_args args = { + .exit_signal = SIGCHLD, +}; + +SAFE_CLONE(&args); +-------------------------------------------------------------------------------- + +If 'exit_signal' is set to something else, then this will break +'tst_reap_children'. It's not expected that all parameters to clone will +work with the LTP library unless specific action is taken by the test code. + .Using 'tst_res()' from binaries started by 'exec()' [source,c] -------------------------------------------------------------------------------
include/tst_clone.h +28 -0
@@ -5,6 +5,34 @@#ifndef TST_CLONE_H__ #define TST_CLONE_H__ +#ifdef TST_TEST_H__ + +/* The parts of clone3's clone_args we support */ +struct tst_clone_args { +uint64_t flags; +uint64_t exit_signal; +}; + +/* clone3 with fallbacks to clone when possible. Be aware that it +* returns -1 if clone3 fails (except ENOSYS), but -2 if clone fails. +* +* Without CLONE_VM this acts like fork so you may want to set +* tst_test.forks_child (safe_clone requires this). +* +* You should set exit_signal to SIGCHLD for +* tst_reap_children. Otherwise you must call wait with the +* appropriate parameters. +*/ +pid_t tst_clone (const struct tst_clone_args *args); + +pid_t safe_clone (const char *file,const int lineno, +const struct tst_clone_args *args); + +/* "Safe" version of tst_clone */ +#define SAFE_CLONE (args) safe_clone(__FILE__, __LINE__, args) + +#endif /* TST_TEST_H__ */ +/* Functions from lib/cloner.c */ int ltp_clone (unsigned long flags,int (*fn)(void *arg),void *arg,size_t stack_size,void *stack);
include/tst_test.h +1 -1
@@ -29,7 +29,6 @@#include "tst_process_state.h" #include "tst_atomic.h" #include "tst_kvercmp.h" -#include "tst_clone.h" #include "tst_kernel.h" #include "tst_minmax.h" #include "tst_get_bad_addr.h"
@@ -94,6 +93,7 @@ pid_t safe_fork(const char *filename, unsigned int lineno);#include "tst_safe_macros.h" #include "tst_safe_file_ops.h" #include "tst_safe_net.h" +#include "tst_clone.h" /* * Wait for all children and exit with TBROK if
lib/tst_clone.c new file +46 -0
46 lines not on this page · raw message
lib/tst_test.c +25 -0
@@ -424,6 +424,31 @@ pid_t safe_fork(const char *filename, unsigned int lineno)return pid; } +pid_t safe_clone (const char *file,const int lineno, +const struct tst_clone_args *args) +{ +pid_t pid; + +if (!tst_test->forks_child) +tst_brk (TBROK ,"test.forks_child must be set!" ); + + pid =tst_clone (args); + +switch (pid) { +case -1 : +tst_brk_ (file, lineno,TBROK |TERRNO ,"clone3 failed" ); +break ; +case -2 : +tst_brk_ (file, lineno,TBROK |TERRNO ,"clone failed" ); +return -1 ; + } + +if (!pid) +atexit (tst_free_all); + +return pid; +} + static struct option { char *optstr; char *help;
Signature · 1 line
2.30.0
PATCH v2 5/5 close_range: Add test
In reply to Richard Palethorpe--- .../kernel/syscalls/close_range/.gitignore | 1 + .../kernel/syscalls/close_range/Makefile | 10 + .../syscalls/close_range/close_range01.c | 200 ++++++++++++++++++ 3 files changed, 211 insertions(+) create mode 100644 testcases/kernel/syscalls/close_range/.gitignore create mode 100644 testcases/kernel/syscalls/close_range/Makefile create mode 100644 testcases/kernel/syscalls/close_range/close_range01.c
testcases/kernel/syscalls/close_range/.gitignore new file +1 -0
@@ -0,0 +1 @@ +close_range01
testcases/kernel/syscalls/close_range/Makefile new file +10 -0
@@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# Copyright (c) 2019-2021 Linux Test Project + +top_srcdir ?= ../../../.. + +include $(top_srcdir)/include/mk/testcases.mk + +CFLAGS += -D_GNU_SOURCE + +include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/close_range/close_range01.c new file +200 -0
200 lines not on this page · raw message
Signature · 1 line
2.30.0
PATCH v2 5/5 close_range: Add test
Hi Richie,
Quoted 5 lines
Signed-off-by Richard Palethorpe <[email protected]>--- .../kernel/syscalls/close_range/.gitignore | 1 + .../kernel/syscalls/close_range/Makefile | 10 + .../syscalls/close_range/close_range01.c | 200 ++++++++++++++++++
It looks like you haven't added close_range01 to runtest file. It should probably go to syscalls and Cyril can add it before merging.
Patchset LGTM and compiles ok.
Thanks!
Kind regards,
Petr
PATCH v2 5/5 close_range: Add test
Hi!
Quoted 6 lines
Quoted 3 lines
.../kernel/syscalls/close_range/.gitignore | 1 + .../kernel/syscalls/close_range/Makefile | 10 + .../syscalls/close_range/close_range01.c | 200 ++++++++++++++++++It looks like you haven't added
close_range01to runtest file. It should probably go to syscalls and Cyril can add it before merging.
That's what I did along with a few more minor changes and pushed.
Quoted 1 line
Patchset LGTM and compiles ok.
Sorry I haven't included your review in the tags.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH v2 5/5 close_range: Add test
Hi Cyril,
Quoted 9 lines
Hi!
Quoted 6 lines
Quoted 3 lines
.../kernel/syscalls/close_range/.gitignore | 1 + .../kernel/syscalls/close_range/Makefile | 10 + .../syscalls/close_range/close_range01.c | 200 ++++++++++++++++++It looks like you haven't added
close_range01to runtest file. It should probably go to syscalls and Cyril can add it before merging.That's what I did along with a few more minor changes and pushed.
Great :).
Quoted 3 lines
Quoted 1 line
Patchset LGTM and compiles ok.
Sorry I haven't included your review in the tags.
np :). Thanks for handling this.
Kind regards,
Petr
PATCH v2 5/5 close_range: Add test
In reply to Richard PalethorpeHi!
Pushed with small changes, thanks.
* Added a runtest file entry
* Add .needs_root which is what we have for tests that utilize loop devices
* Reformatted the top level comment so it's picked by docparser
* Shortened the git hash to 12 characters, since that's what we have in the rest of the tests
Also we are missing simple test for a negative cases, that is:
* Check that EINVAL is returned if we pass ~0 to flags
* And check that EINVAL is returned when fd < fd_max as well
And one more a bit more complicated test:
We should also get EMFILE if we do CLOSE_RANGE_UNSHARE and the parent rlimit is exhausted.
Will you write these tests as well, or should I have a look?
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH v2 5/5 close_range: Add test
Hello,
Quoted 7 lines · Cyril Hrubis
Hi!
Pushed with small changes, thanks.* Added a runtest file entry
* Add .needs_root which is what we have for tests that utilize loop devices
Ah, of course. I guess in that case we may also want to drop CAP_SYS_ADMIN in caps.
Quoted 6 lines
* Reformatted the top level comment so it's picked by docparser
* Shortened the git hash to 12 characters, since that's what we have in the rest of the tests
Ah, OK.
Quoted 5 lines
Also we are missing simple test for a negative cases, that is:
* Check that EINVAL is returned if we pass ~0 to flags
Quoted 7 lines
* And check that EINVAL is returned when fd <
fd_maxas wellAnd one more a bit more complicated test:
We should also get EMFILE if we do
CLOSE_RANGE_UNSHAREand the parent rlimit is exhausted.
Indeed those may be missing in the self tests as well.
Quoted 3 lines
Will you write these tests as well, or should I have a look?
If you want to do it today very quickly, then feel free. Otherwise I will do it next week.
Signature · 2 lines
Thank you, Richard.