From 5150821bee9ac453fc9c2ce571d7153c50d3e8c8 Mon Sep 17 00:00:00 2001 From: Taylor R Campbell Date: Sat, 18 Nov 2023 21:18:25 +0000 Subject: [PATCH 1/9] t_eventfd: Sort includes. No functional change intended. PR kern/57703 --- tests/lib/libc/sys/t_eventfd.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/lib/libc/sys/t_eventfd.c b/tests/lib/libc/sys/t_eventfd.c index 4eaea2aff3ba..60decab4e333 100644 --- a/tests/lib/libc/sys/t_eventfd.c +++ b/tests/lib/libc/sys/t_eventfd.c @@ -31,23 +31,23 @@ __COPYRIGHT("@(#) Copyright (c) 2020\ The NetBSD Foundation, inc. All rights reserved."); __RCSID("$NetBSD: t_eventfd.c,v 1.3 2022/02/20 15:21:14 thorpej Exp $"); -#include #include #include #include #include #include #include +#include + +#include #include #include #include -#include #include +#include #include #include -#include - struct helper_context { int efd; From 671cc692142081be6cae446fb41d723aaedbc6b7 Mon Sep 17 00:00:00 2001 From: Taylor R Campbell Date: Sat, 18 Nov 2023 21:37:02 +0000 Subject: [PATCH 2/9] eventfd(2): Expose this to rump. PR kern/57703 --- sys/kern/syscalls.master | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sys/kern/syscalls.master b/sys/kern/syscalls.master index b93cc4716771..b08836a01ee0 100644 --- a/sys/kern/syscalls.master +++ b/sys/kern/syscalls.master @@ -569,7 +569,7 @@ { ssize_t|sys||mq_timedreceive(mqd_t mqdes, \ char *msg_ptr, size_t msg_len, unsigned *msg_prio, \ const struct timespec50 *abs_timeout); } -267 STD { int|sys||eventfd(unsigned int val, int flags); } +267 STD RUMP { int|sys||eventfd(unsigned int val, int flags); } 268 UNIMPL 269 UNIMPL 270 STD RUMP { int|sys||__posix_rename(const char *from, \ From b38dd03c820829d986e629ea31343c4753ec2d04 Mon Sep 17 00:00:00 2001 From: Taylor R Campbell Date: Sat, 18 Nov 2023 21:39:15 +0000 Subject: [PATCH 3/9] regen (makesyscalls.sh) PR kern/57703 --- sys/kern/init_sysent.c | 4 +-- sys/kern/syscalls.c | 4 +-- sys/kern/syscalls_autoload.c | 4 +-- sys/kern/systrace_args.c | 2 +- sys/rump/include/rump/rump_syscalls.h | 7 ++++- sys/rump/librump/rumpkern/rump_syscalls.c | 36 +++++++++++++++++++++-- sys/rump/rump.sysmap | 1 + sys/sys/syscall.h | 2 +- sys/sys/syscallargs.h | 4 +-- 9 files changed, 49 insertions(+), 15 deletions(-) diff --git a/sys/kern/init_sysent.c b/sys/kern/init_sysent.c index 0e0521b6f2b6..3a2f9829360e 100644 --- a/sys/kern/init_sysent.c +++ b/sys/kern/init_sysent.c @@ -1,4 +1,4 @@ -/* $NetBSD: init_sysent.c,v 1.341 2023/07/28 18:20:28 christos Exp $ */ +/* $NetBSD$ */ /* * System call switch table. @@ -8,7 +8,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: init_sysent.c,v 1.341 2023/07/28 18:20:28 christos Exp $"); +__KERNEL_RCSID(0, "$NetBSD$"); #ifdef _KERNEL_OPT #include "opt_modular.h" diff --git a/sys/kern/syscalls.c b/sys/kern/syscalls.c index eb1cd306b687..240cf8c52a80 100644 --- a/sys/kern/syscalls.c +++ b/sys/kern/syscalls.c @@ -1,4 +1,4 @@ -/* $NetBSD: syscalls.c,v 1.329 2023/07/28 18:20:28 christos Exp $ */ +/* $NetBSD$ */ /* * System call names. @@ -8,7 +8,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: syscalls.c,v 1.329 2023/07/28 18:20:28 christos Exp $"); +__KERNEL_RCSID(0, "$NetBSD$"); #if defined(_KERNEL_OPT) #ifdef _KERNEL_OPT diff --git a/sys/kern/syscalls_autoload.c b/sys/kern/syscalls_autoload.c index c465d5da7b40..0e467ff5a4c1 100644 --- a/sys/kern/syscalls_autoload.c +++ b/sys/kern/syscalls_autoload.c @@ -1,4 +1,4 @@ -/* $NetBSD: syscalls_autoload.c,v 1.45 2023/07/28 18:20:28 christos Exp $ */ +/* $NetBSD$ */ /* * System call autoload table. @@ -8,7 +8,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: syscalls_autoload.c,v 1.45 2023/07/28 18:20:28 christos Exp $"); +__KERNEL_RCSID(0, "$NetBSD$"); #ifdef _KERNEL_OPT #include "opt_modular.h" diff --git a/sys/kern/systrace_args.c b/sys/kern/systrace_args.c index bc7d9927463a..3f84adee7809 100644 --- a/sys/kern/systrace_args.c +++ b/sys/kern/systrace_args.c @@ -1,4 +1,4 @@ -/* $NetBSD: systrace_args.c,v 1.51 2023/07/28 18:20:28 christos Exp $ */ +/* $NetBSD$ */ /* * System call argument to DTrace register array conversion. diff --git a/sys/rump/include/rump/rump_syscalls.h b/sys/rump/include/rump/rump_syscalls.h index a94e184823b3..5a1a03bcb812 100644 --- a/sys/rump/include/rump/rump_syscalls.h +++ b/sys/rump/include/rump/rump_syscalls.h @@ -1,4 +1,4 @@ -/* $NetBSD: rump_syscalls.h,v 1.128 2023/07/28 18:20:28 christos Exp $ */ +/* $NetBSD$ */ /* * System call protos in rump namespace. @@ -196,6 +196,10 @@ #define RUMP_SYS_RENAME_DUP3 rump___sysimpl_dup3 #endif +#ifndef RUMP_SYS_RENAME_EVENTFD +#define RUMP_SYS_RENAME_EVENTFD rump___sysimpl_eventfd +#endif + #ifndef RUMP_SYS_RENAME_EXTATTR_DELETE_FD #define RUMP_SYS_RENAME_EXTATTR_DELETE_FD rump___sysimpl_extattr_delete_fd #endif @@ -971,6 +975,7 @@ int rump_sys__ksem_trywait(intptr_t) __RENAME(RUMP_SYS_RENAME__KSEM_TRYWAIT); int rump_sys__ksem_getvalue(intptr_t, unsigned int *) __RENAME(RUMP_SYS_RENAME__KSEM_GETVALUE); int rump_sys__ksem_destroy(intptr_t) __RENAME(RUMP_SYS_RENAME__KSEM_DESTROY); int rump_sys__ksem_timedwait(intptr_t, const struct timespec *) __RENAME(RUMP_SYS_RENAME__KSEM_TIMEDWAIT); +int rump_sys_eventfd(unsigned int, int) __RENAME(RUMP_SYS_RENAME_EVENTFD); int rump_sys___posix_rename(const char *, const char *) __RENAME(RUMP_SYS_RENAME___POSIX_RENAME); int rump_sys_lchmod(const char *, mode_t) __RENAME(RUMP_SYS_RENAME_LCHMOD); int rump_sys_lchown(const char *, uid_t, gid_t) __RENAME(RUMP_SYS_RENAME_LCHOWN); diff --git a/sys/rump/librump/rumpkern/rump_syscalls.c b/sys/rump/librump/rumpkern/rump_syscalls.c index 7eda59547b24..3efd047a4af4 100644 --- a/sys/rump/librump/rumpkern/rump_syscalls.c +++ b/sys/rump/librump/rumpkern/rump_syscalls.c @@ -1,4 +1,4 @@ -/* $NetBSD: rump_syscalls.c,v 1.159 2023/07/28 18:20:29 christos Exp $ */ +/* $NetBSD$ */ /* * System call vector and marshalling for rump. @@ -15,7 +15,7 @@ #ifdef __NetBSD__ #include -__KERNEL_RCSID(0, "$NetBSD: rump_syscalls.c,v 1.159 2023/07/28 18:20:29 christos Exp $"); +__KERNEL_RCSID(0, "$NetBSD$"); #include #include @@ -3016,6 +3016,35 @@ __weak_alias(__ksem_timedwait,rump___sysimpl__ksem_timedwait); __strong_alias(_sys__ksem_timedwait,rump___sysimpl__ksem_timedwait); #endif /* RUMP_KERNEL_IS_LIBC */ +int rump___sysimpl_eventfd(unsigned int, int); +int +rump___sysimpl_eventfd(unsigned int val, int flags) +{ + register_t retval[2]; + int error = 0; + int rv = -1; + struct sys_eventfd_args callarg; + + memset(&callarg, 0, sizeof(callarg)); + SPARG(&callarg, val) = val; + SPARG(&callarg, flags) = flags; + + error = rsys_syscall(SYS_eventfd, &callarg, sizeof(callarg), retval); + rsys_seterrno(error); + if (error == 0) { + if (sizeof(int) > sizeof(register_t)) + rv = *(int *)retval; + else + rv = *retval; + } + return rv; +} +#ifdef RUMP_KERNEL_IS_LIBC +__weak_alias(eventfd,rump___sysimpl_eventfd); +__weak_alias(_eventfd,rump___sysimpl_eventfd); +__strong_alias(_sys_eventfd,rump___sysimpl_eventfd); +#endif /* RUMP_KERNEL_IS_LIBC */ + int rump___sysimpl___posix_rename(const char *, const char *); int rump___sysimpl___posix_rename(const char * from, const char * to) @@ -7732,8 +7761,9 @@ struct sysent rump_sysent[] = { .sy_call = (sy_call_t *)(void *)rumpns_sys_nomodule, }, /* 266 = mq_timedreceive */ { + ns(struct sys_eventfd_args), .sy_call = (sy_call_t *)(void *)rumpns_enosys, -}, /* 267 = eventfd */ + }, /* 267 = eventfd */ { .sy_flags = SYCALL_NOSYS, .sy_call = (sy_call_t *)(void *)rumpns_enosys, diff --git a/sys/rump/rump.sysmap b/sys/rump/rump.sysmap index 633666fb5447..07ef0d41eca7 100644 --- a/sys/rump/rump.sysmap +++ b/sys/rump/rump.sysmap @@ -102,6 +102,7 @@ 254 sys_nomodule _ksem_getvalue rump___sysimpl__ksem_getvalue 255 sys_nomodule _ksem_destroy rump___sysimpl__ksem_destroy 256 sys_nomodule _ksem_timedwait rump___sysimpl__ksem_timedwait +267 sys_eventfd eventfd rump___sysimpl_eventfd 270 sys___posix_rename __posix_rename rump___sysimpl___posix_rename 274 sys_lchmod lchmod rump___sysimpl_lchmod 275 sys_lchown lchown rump___sysimpl_lchown diff --git a/sys/sys/syscall.h b/sys/sys/syscall.h index e8b4451d5e1f..4f367c1fc88c 100644 --- a/sys/sys/syscall.h +++ b/sys/sys/syscall.h @@ -1,4 +1,4 @@ -/* $NetBSD: syscall.h,v 1.324 2023/07/28 18:20:28 christos Exp $ */ +/* $NetBSD$ */ /* * System call numbers. diff --git a/sys/sys/syscallargs.h b/sys/sys/syscallargs.h index e5598137fcd1..773da2eaa92d 100644 --- a/sys/sys/syscallargs.h +++ b/sys/sys/syscallargs.h @@ -1,4 +1,4 @@ -/* $NetBSD: syscallargs.h,v 1.307 2023/07/28 18:20:28 christos Exp $ */ +/* $NetBSD$ */ /* * System call argument lists. @@ -1736,13 +1736,11 @@ struct compat_50_sys_mq_timedreceive_args { check_syscall_args(compat_50_sys_mq_timedreceive) #endif /* !RUMP_CLIENT */ -#ifndef RUMP_CLIENT struct sys_eventfd_args { syscallarg(unsigned int) val; syscallarg(int) flags; }; check_syscall_args(sys_eventfd) -#endif /* !RUMP_CLIENT */ struct sys___posix_rename_args { syscallarg(const char *) from; From cf87676a08f3503f0ba3a3fb5dcc6f0e865571ec Mon Sep 17 00:00:00 2001 From: Taylor R Campbell Date: Sat, 18 Nov 2023 23:08:23 +0000 Subject: [PATCH 4/9] regen (makerumpsyscalls.sh) PR kern/57703 --- sys/rump/librump/rumpkern/rumpkern_syscalls.c | 6 ++++-- sys/rump/librump/rumpnet/rumpnet_syscalls.c | 4 ++-- sys/rump/librump/rumpvfs/rumpvfs_syscalls.c | 4 ++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/sys/rump/librump/rumpkern/rumpkern_syscalls.c b/sys/rump/librump/rumpkern/rumpkern_syscalls.c index 9092fb3ef0d7..27f509539f09 100644 --- a/sys/rump/librump/rumpkern/rumpkern_syscalls.c +++ b/sys/rump/librump/rumpkern/rumpkern_syscalls.c @@ -1,9 +1,9 @@ -/* $NetBSD: rumpkern_syscalls.c,v 1.9 2023/07/29 15:11:04 rin Exp $ */ +/* $NetBSD: makerumpsyscalls.sh,v 1.3 2016/01/26 23:24:55 pooka Exp $ */ /* AUTOMATICALLY GENERATED BY makerumpsyscalls.sh */ #include -__KERNEL_RCSID(0, "$NetBSD: rumpkern_syscalls.c,v 1.9 2023/07/29 15:11:04 rin Exp $"); +__KERNEL_RCSID(0, "$NetBSD: makerumpsyscalls.sh,v 1.3 2016/01/26 23:24:55 pooka Exp $"); #include @@ -51,6 +51,7 @@ extern sy_call_t sys_timer_create; extern sy_call_t sys_timer_delete; extern sy_call_t sys_timer_getoverrun; extern sy_call_t sys_modctl; +extern sy_call_t sys_eventfd; extern sy_call_t sys_getsid; extern sy_call_t sys_fktrace; extern sy_call_t sys_issetugid; @@ -122,6 +123,7 @@ static const struct rump_onesyscall mysys[] = { { 236, sys_timer_delete }, { 239, sys_timer_getoverrun }, { 246, sys_modctl }, + { 267, sys_eventfd }, { 286, sys_getsid }, { 288, sys_fktrace }, { 305, sys_issetugid }, diff --git a/sys/rump/librump/rumpnet/rumpnet_syscalls.c b/sys/rump/librump/rumpnet/rumpnet_syscalls.c index ff5169e83002..bf6abf80635a 100644 --- a/sys/rump/librump/rumpnet/rumpnet_syscalls.c +++ b/sys/rump/librump/rumpnet/rumpnet_syscalls.c @@ -1,9 +1,9 @@ -/* $NetBSD: rumpnet_syscalls.c,v 1.8 2023/07/29 15:11:04 rin Exp $ */ +/* $NetBSD: makerumpsyscalls.sh,v 1.3 2016/01/26 23:24:55 pooka Exp $ */ /* AUTOMATICALLY GENERATED BY makerumpsyscalls.sh */ #include -__KERNEL_RCSID(0, "$NetBSD: rumpnet_syscalls.c,v 1.8 2023/07/29 15:11:04 rin Exp $"); +__KERNEL_RCSID(0, "$NetBSD: makerumpsyscalls.sh,v 1.3 2016/01/26 23:24:55 pooka Exp $"); #include diff --git a/sys/rump/librump/rumpvfs/rumpvfs_syscalls.c b/sys/rump/librump/rumpvfs/rumpvfs_syscalls.c index 3cd395d78c84..6d27c9e149f8 100644 --- a/sys/rump/librump/rumpvfs/rumpvfs_syscalls.c +++ b/sys/rump/librump/rumpvfs/rumpvfs_syscalls.c @@ -1,9 +1,9 @@ -/* $NetBSD: rumpvfs_syscalls.c,v 1.10 2023/07/29 15:11:04 rin Exp $ */ +/* $NetBSD: makerumpsyscalls.sh,v 1.3 2016/01/26 23:24:55 pooka Exp $ */ /* AUTOMATICALLY GENERATED BY makerumpsyscalls.sh */ #include -__KERNEL_RCSID(0, "$NetBSD: rumpvfs_syscalls.c,v 1.10 2023/07/29 15:11:04 rin Exp $"); +__KERNEL_RCSID(0, "$NetBSD: makerumpsyscalls.sh,v 1.3 2016/01/26 23:24:55 pooka Exp $"); #include From f4dd2c5f1ac9f93442071979bd7fa43371c60fdd Mon Sep 17 00:00:00 2001 From: Taylor R Campbell Date: Sat, 18 Nov 2023 21:46:02 +0000 Subject: [PATCH 5/9] rumpkern: Include sys_eventfd.c. PR kern/57703 --- sys/rump/librump/rumpkern/Makefile.rumpkern | 1 + 1 file changed, 1 insertion(+) diff --git a/sys/rump/librump/rumpkern/Makefile.rumpkern b/sys/rump/librump/rumpkern/Makefile.rumpkern index 87de64c9287c..5c9d1e1ee535 100644 --- a/sys/rump/librump/rumpkern/Makefile.rumpkern +++ b/sys/rump/librump/rumpkern/Makefile.rumpkern @@ -131,6 +131,7 @@ SRCS+= init_sysctl_base.c \ subr_workqueue.c \ subr_xcall.c \ sys_descrip.c \ + sys_eventfd.c \ sys_generic.c \ sys_getrandom.c \ sys_module.c \ From 29fb15ca2cb94326658ec164a25a60b9ee01ef14 Mon Sep 17 00:00:00 2001 From: Taylor R Campbell Date: Sat, 18 Nov 2023 21:46:23 +0000 Subject: [PATCH 6/9] t_eventfd: Convert to rump so this doesn't crash your machine. PR kern/57703 --- tests/lib/libc/sys/Makefile | 2 +- tests/lib/libc/sys/t_eventfd.c | 237 +++++++++++++++++++++------------ 2 files changed, 155 insertions(+), 84 deletions(-) diff --git a/tests/lib/libc/sys/Makefile b/tests/lib/libc/sys/Makefile index 21e66aa74946..0a77cdf5168f 100644 --- a/tests/lib/libc/sys/Makefile +++ b/tests/lib/libc/sys/Makefile @@ -95,7 +95,7 @@ TESTS_C+= t_write SRCS.t_mprotect= t_mprotect.c ${SRCS_EXEC_PROT} t_mprotect_helper.c -LDADD.t_eventfd+= -lpthread +LDADD.t_eventfd+= -lpthread ${LIBRUMPBASE} LDADD.t_getpid+= -lpthread LDADD.t_mmap+= -lpthread LDADD.t_timerfd+= -lpthread diff --git a/tests/lib/libc/sys/t_eventfd.c b/tests/lib/libc/sys/t_eventfd.c index 60decab4e333..bcd2991fa06a 100644 --- a/tests/lib/libc/sys/t_eventfd.c +++ b/tests/lib/libc/sys/t_eventfd.c @@ -26,6 +26,8 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#define _KMEMUSER /* ERESTART */ + #include __COPYRIGHT("@(#) Copyright (c) 2020\ The NetBSD Foundation, inc. All rights reserved."); @@ -48,6 +50,47 @@ __RCSID("$NetBSD: t_eventfd.c,v 1.3 2022/02/20 15:21:14 thorpej Exp $"); #include #include +#include +#include + +static int /* XXX copypasta from libc/sys/eventfd_read.c, nix me */ +rump_sys_eventfd_read(int efd, eventfd_t *valp) +{ + eventfd_t val; + + switch (rump_sys_read(efd, &val, sizeof(val))) { + case -1: + return -1; + + case sizeof(val): + *valp = val; + return 0; + + default: + /* This should never happen, but... */ + errno = EIO; + return -1; + } +} + +static int /* XXX copypasta from libc/sys/eventfd_write.c, nix me */ +rump_sys_eventfd_write(int efd, eventfd_t val) +{ + + switch (rump_sys_write(efd, &val, sizeof(val))) { + case -1: + return -1; + + case sizeof(val): + return 0; + + default: + /* This should never happen, but... */ + errno = EIO; + return -1; + } +} + struct helper_context { int efd; @@ -141,7 +184,7 @@ eventfd_normal_helper(void * const v) /* Read the value. This will reset it to zero. */ ATF_REQUIRE(get_state(ctx) == 666); - ATF_REQUIRE(eventfd_read(ctx->efd, &efd_value) == 0); + ATF_REQUIRE(rump_sys_eventfd_read(ctx->efd, &efd_value) == 0); /* Assert the value. */ ATF_REQUIRE(efd_value == 0xcafebabe); @@ -152,7 +195,7 @@ eventfd_normal_helper(void * const v) ATF_REQUIRE(wait_barrier(ctx)); /* Read the value. */ - ATF_REQUIRE(eventfd_read(ctx->efd, &efd_value) == 0); + ATF_REQUIRE(rump_sys_eventfd_read(ctx->efd, &efd_value) == 0); /* Assert the value. */ ATF_REQUIRE(efd_value == 0xbeefcafe); @@ -174,9 +217,11 @@ ATF_TC_BODY(eventfd_normal, tc) pthread_t helper; void *join_val; + rump_init(); + init_helper_context(&ctx); - ATF_REQUIRE((ctx.efd = eventfd(0, 0)) >= 0); + ATF_REQUIRE((ctx.efd = rump_sys_eventfd(0, 0)) >= 0); ATF_REQUIRE(pthread_create(&helper, NULL, eventfd_normal_helper, &ctx) == 0); @@ -192,7 +237,7 @@ ATF_TC_BODY(eventfd_normal, tc) ATF_REQUIRE(get_state(&ctx) == 666); /* Write a distinct value; helper will assert it. */ - ATF_REQUIRE(eventfd_write(ctx.efd, 0xcafebabe) == 0); + ATF_REQUIRE(rump_sys_eventfd_write(ctx.efd, 0xcafebabe) == 0); /* Wait for helper to read the value. */ ATF_REQUIRE(wait_state(&ctx, 0)); @@ -200,8 +245,8 @@ ATF_TC_BODY(eventfd_normal, tc) /* Helper is now blocked in a barrier. */ /* Test additive property of the efd value. */ - ATF_REQUIRE(eventfd_write(ctx.efd, 0x0000cafe) == 0); - ATF_REQUIRE(eventfd_write(ctx.efd, 0xbeef0000) == 0); + ATF_REQUIRE(rump_sys_eventfd_write(ctx.efd, 0x0000cafe) == 0); + ATF_REQUIRE(rump_sys_eventfd_write(ctx.efd, 0xbeef0000) == 0); /* Satisfy the barrier; helper will read value and assert 0xbeefcafe. */ ATF_REQUIRE(wait_barrier(&ctx)); @@ -212,7 +257,7 @@ ATF_TC_BODY(eventfd_normal, tc) /* Reap the helper. */ ATF_REQUIRE(pthread_join(helper, &join_val) == 0); - (void) close(ctx.efd); + (void) rump_sys_close(ctx.efd); } /*****************************************************************************/ @@ -228,34 +273,37 @@ ATF_TC_BODY(eventfd_semaphore, tc) eventfd_t efd_value; int efd; - ATF_REQUIRE((efd = eventfd(3, EFD_SEMAPHORE | EFD_NONBLOCK)) >= 0); + rump_init(); + + ATF_REQUIRE((efd = rump_sys_eventfd(3, EFD_SEMAPHORE | EFD_NONBLOCK)) + >= 0); /* 3 reads should succeed without blocking. */ - ATF_REQUIRE(eventfd_read(efd, &efd_value) == 0); + ATF_REQUIRE(rump_sys_eventfd_read(efd, &efd_value) == 0); ATF_REQUIRE(efd_value == 1); - ATF_REQUIRE(eventfd_read(efd, &efd_value) == 0); + ATF_REQUIRE(rump_sys_eventfd_read(efd, &efd_value) == 0); ATF_REQUIRE(efd_value == 1); - ATF_REQUIRE(eventfd_read(efd, &efd_value) == 0); + ATF_REQUIRE(rump_sys_eventfd_read(efd, &efd_value) == 0); ATF_REQUIRE(efd_value == 1); /* This one should block. */ ATF_REQUIRE_ERRNO(EAGAIN, - eventfd_read(efd, &efd_value) == -1); + rump_sys_eventfd_read(efd, &efd_value) == -1); /* Add 1 to the semaphore. */ - ATF_REQUIRE(eventfd_write(efd, 1) == 0); + ATF_REQUIRE(rump_sys_eventfd_write(efd, 1) == 0); /* One more read allowed. */ - ATF_REQUIRE(eventfd_read(efd, &efd_value) == 0); + ATF_REQUIRE(rump_sys_eventfd_read(efd, &efd_value) == 0); ATF_REQUIRE(efd_value == 1); /* And this one again should block. */ ATF_REQUIRE_ERRNO(EAGAIN, - eventfd_read(efd, &efd_value) == -1); + rump_sys_eventfd_read(efd, &efd_value) == -1); - (void) close(efd); + (void) rump_sys_close(efd); } /*****************************************************************************/ @@ -276,12 +324,14 @@ ATF_TC_BODY(eventfd_select_poll_kevent_immed, tc) int kq; struct kevent kev[2]; - ATF_REQUIRE((efd = eventfd(0, EFD_NONBLOCK)) >= 0); + rump_init(); + + ATF_REQUIRE((efd = rump_sys_eventfd(0, EFD_NONBLOCK)) >= 0); - ATF_REQUIRE((kq = kqueue()) >= 0); + ATF_REQUIRE((kq = rump_sys_kqueue()) >= 0); EV_SET(&kev[0], efd, EVFILT_READ, EV_ADD, 0, 0, NULL); EV_SET(&kev[1], efd, EVFILT_WRITE, EV_ADD, 0, 0, NULL); - ATF_REQUIRE(kevent(kq, kev, 2, NULL, 0, &ts) == 0); + ATF_REQUIRE(rump_sys_kevent(kq, kev, 2, NULL, 0, &ts) == 0); /* * efd should be writable but not readable. Pass all of the @@ -291,7 +341,7 @@ ATF_TC_BODY(eventfd_select_poll_kevent_immed, tc) fds[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI | POLLOUT | POLLWRNORM | POLLWRBAND | POLLHUP; fds[0].revents = 0; - ATF_REQUIRE(poll(fds, 1, 0) == 1); + ATF_REQUIRE(rump_sys_poll(fds, 1, 0) == 1); ATF_REQUIRE(fds[0].revents == (POLLOUT | POLLWRNORM)); /* @@ -306,7 +356,8 @@ ATF_TC_BODY(eventfd_select_poll_kevent_immed, tc) FD_SET(efd, &readfds); FD_SET(efd, &writefds); FD_SET(efd, &exceptfds); - ATF_REQUIRE(select(efd + 1, &readfds, &writefds, &exceptfds, &tv) == 1); + ATF_REQUIRE(rump_sys_select(efd + 1, &readfds, &writefds, &exceptfds, + &tv) == 1); ATF_REQUIRE(!FD_ISSET(efd, &readfds)); ATF_REQUIRE(FD_ISSET(efd, &writefds)); ATF_REQUIRE(!FD_ISSET(efd, &exceptfds)); @@ -316,7 +367,7 @@ ATF_TC_BODY(eventfd_select_poll_kevent_immed, tc) * on efd. */ memset(kev, 0, sizeof(kev)); - ATF_REQUIRE(kevent(kq, NULL, 0, kev, 2, &ts) == 1); + ATF_REQUIRE(rump_sys_kevent(kq, NULL, 0, kev, 2, &ts) == 1); ATF_REQUIRE(kev[0].ident == (uintptr_t)efd); ATF_REQUIRE(kev[0].filter == EVFILT_WRITE); ATF_REQUIRE((kev[0].flags & (EV_EOF | EV_ERROR)) == 0); @@ -326,13 +377,13 @@ ATF_TC_BODY(eventfd_select_poll_kevent_immed, tc) * Write the maximum value into the eventfd. This should result * in the eventfd becoming readable but NOT writable. */ - ATF_REQUIRE(eventfd_write(efd, UINT64_MAX - 1) == 0); + ATF_REQUIRE(rump_sys_eventfd_write(efd, UINT64_MAX - 1) == 0); fds[0].fd = efd; fds[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI | POLLOUT | POLLWRNORM | POLLWRBAND | POLLHUP; fds[0].revents = 0; - ATF_REQUIRE(poll(fds, 1, 0) == 1); + ATF_REQUIRE(rump_sys_poll(fds, 1, 0) == 1); ATF_REQUIRE(fds[0].revents == (POLLIN | POLLRDNORM)); FD_ZERO(&readfds); @@ -343,7 +394,8 @@ ATF_TC_BODY(eventfd_select_poll_kevent_immed, tc) FD_SET(efd, &readfds); FD_SET(efd, &writefds); FD_SET(efd, &exceptfds); - ATF_REQUIRE(select(efd + 1, &readfds, &writefds, &exceptfds, &tv) == 1); + ATF_REQUIRE(rump_sys_select(efd + 1, &readfds, &writefds, &exceptfds, + &tv) == 1); ATF_REQUIRE(FD_ISSET(efd, &readfds)); ATF_REQUIRE(!FD_ISSET(efd, &writefds)); ATF_REQUIRE(!FD_ISSET(efd, &exceptfds)); @@ -353,14 +405,14 @@ ATF_TC_BODY(eventfd_select_poll_kevent_immed, tc) * on efd. */ memset(kev, 0, sizeof(kev)); - ATF_REQUIRE(kevent(kq, NULL, 0, kev, 2, &ts) == 1); + ATF_REQUIRE(rump_sys_kevent(kq, NULL, 0, kev, 2, &ts) == 1); ATF_REQUIRE(kev[0].ident == (uintptr_t)efd); ATF_REQUIRE(kev[0].filter == EVFILT_READ); ATF_REQUIRE((kev[0].flags & (EV_EOF | EV_ERROR)) == 0); ATF_REQUIRE(kev[0].data == (int64_t)(UINT64_MAX - 1)); - (void) close(kq); - (void) close(efd); + (void) rump_sys_close(kq); + (void) rump_sys_close(efd); } /*****************************************************************************/ @@ -380,12 +432,12 @@ eventfd_select_poll_kevent_block_helper(void * const v) fds[0].revents = 0; ATF_REQUIRE_ERRNO(EAGAIN, - eventfd_read(ctx->efd, &efd_value) == -1); + rump_sys_eventfd_read(ctx->efd, &efd_value) == -1); ATF_REQUIRE(wait_barrier(ctx)); ATF_REQUIRE(get_state(ctx) == 666); - ATF_REQUIRE(poll(fds, 1, INFTIM) == 1); + ATF_REQUIRE(rump_sys_poll(fds, 1, INFTIM) == 1); ATF_REQUIRE(fds[0].revents == (POLLIN | POLLRDNORM)); set_state(ctx, 0); @@ -400,12 +452,12 @@ eventfd_select_poll_kevent_block_helper(void * const v) fds[0].revents = 0; ATF_REQUIRE_ERRNO(EAGAIN, - eventfd_write(ctx->efd, UINT64_MAX - 1) == -1); + rump_sys_eventfd_write(ctx->efd, UINT64_MAX - 1) == -1); ATF_REQUIRE(wait_barrier(ctx)); ATF_REQUIRE(get_state(ctx) == 666); - ATF_REQUIRE(poll(fds, 1, INFTIM) == 1); + ATF_REQUIRE(rump_sys_poll(fds, 1, INFTIM) == 1); ATF_REQUIRE(fds[0].revents == (POLLOUT | POLLWRNORM)); set_state(ctx, 0); @@ -419,12 +471,13 @@ eventfd_select_poll_kevent_block_helper(void * const v) FD_SET(ctx->efd, &selfds); ATF_REQUIRE_ERRNO(EAGAIN, - eventfd_read(ctx->efd, &efd_value) == -1); + rump_sys_eventfd_read(ctx->efd, &efd_value) == -1); ATF_REQUIRE(wait_barrier(ctx)); ATF_REQUIRE(get_state(ctx) == 666); - ATF_REQUIRE(select(ctx->efd + 1, &selfds, NULL, NULL, NULL) == 1); + ATF_REQUIRE(rump_sys_select(ctx->efd + 1, &selfds, NULL, NULL, NULL) == + 1); ATF_REQUIRE(FD_ISSET(ctx->efd, &selfds)); set_state(ctx, 0); @@ -434,12 +487,13 @@ eventfd_select_poll_kevent_block_helper(void * const v) FD_SET(ctx->efd, &selfds); ATF_REQUIRE_ERRNO(EAGAIN, - eventfd_write(ctx->efd, UINT64_MAX - 1) == -1); + rump_sys_eventfd_write(ctx->efd, UINT64_MAX - 1) == -1); ATF_REQUIRE(wait_barrier(ctx)); ATF_REQUIRE(get_state(ctx) == 666); - ATF_REQUIRE(select(ctx->efd + 1, NULL, &selfds, NULL, NULL) == 1); + ATF_REQUIRE(rump_sys_select(ctx->efd + 1, NULL, &selfds, NULL, NULL) == + 1); ATF_REQUIRE(FD_ISSET(ctx->efd, &selfds)); set_state(ctx, 0); @@ -448,18 +502,18 @@ eventfd_select_poll_kevent_block_helper(void * const v) /* * Now, the same dance again, with kevent(). */ - ATF_REQUIRE((kq = kqueue()) >= 0); + ATF_REQUIRE((kq = rump_sys_kqueue()) >= 0); EV_SET(&kev[0], ctx->efd, EVFILT_READ, EV_ADD | EV_ONESHOT, 0, 0, NULL); - ATF_REQUIRE(kevent(kq, kev, 1, NULL, 0, NULL) == 0); + ATF_REQUIRE(rump_sys_kevent(kq, kev, 1, NULL, 0, NULL) == 0); ATF_REQUIRE_ERRNO(EAGAIN, - eventfd_read(ctx->efd, &efd_value) == -1); + rump_sys_eventfd_read(ctx->efd, &efd_value) == -1); ATF_REQUIRE(wait_barrier(ctx)); ATF_REQUIRE(get_state(ctx) == 666); - ATF_REQUIRE(kevent(kq, NULL, 0, kev, 1, NULL) == 1); + ATF_REQUIRE(rump_sys_kevent(kq, NULL, 0, kev, 1, NULL) == 1); ATF_REQUIRE(kev[0].ident == (uintptr_t)ctx->efd); ATF_REQUIRE(kev[0].filter == EVFILT_READ); ATF_REQUIRE((kev[0].flags & (EV_EOF | EV_ERROR)) == 0); @@ -470,15 +524,15 @@ eventfd_select_poll_kevent_block_helper(void * const v) EV_SET(&kev[0], ctx->efd, EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, NULL); - ATF_REQUIRE(kevent(kq, kev, 1, NULL, 0, NULL) == 0); + ATF_REQUIRE(rump_sys_kevent(kq, kev, 1, NULL, 0, NULL) == 0); ATF_REQUIRE_ERRNO(EAGAIN, - eventfd_write(ctx->efd, UINT64_MAX - 1) == -1); + rump_sys_eventfd_write(ctx->efd, UINT64_MAX - 1) == -1); ATF_REQUIRE(wait_barrier(ctx)); ATF_REQUIRE(get_state(ctx) == 666); - ATF_REQUIRE(kevent(kq, NULL, 0, kev, 1, NULL) == 1); + ATF_REQUIRE(rump_sys_kevent(kq, NULL, 0, kev, 1, NULL) == 1); ATF_REQUIRE(kev[0].ident == (uintptr_t)ctx->efd); ATF_REQUIRE(kev[0].filter == EVFILT_WRITE); ATF_REQUIRE((kev[0].flags & (EV_EOF | EV_ERROR)) == 0); @@ -487,7 +541,7 @@ eventfd_select_poll_kevent_block_helper(void * const v) ATF_REQUIRE(wait_barrier(ctx)); - (void) close(kq); + (void) rump_sys_close(kq); return NULL; } @@ -505,9 +559,11 @@ ATF_TC_BODY(eventfd_select_poll_kevent_block, tc) eventfd_t efd_value; void *join_val; + rump_init(); + init_helper_context(&ctx); - ATF_REQUIRE((ctx.efd = eventfd(0, EFD_NONBLOCK)) >= 0); + ATF_REQUIRE((ctx.efd = rump_sys_eventfd(0, EFD_NONBLOCK)) >= 0); ATF_REQUIRE(pthread_create(&helper, NULL, eventfd_select_poll_kevent_block_helper, @@ -526,7 +582,7 @@ ATF_TC_BODY(eventfd_select_poll_kevent_block, tc) * Write the max value to the eventfd so that it becomes readable * and unblocks the helper waiting in poll(). */ - ATF_REQUIRE(eventfd_write(ctx.efd, UINT64_MAX - 1) == 0); + ATF_REQUIRE(rump_sys_eventfd_write(ctx.efd, UINT64_MAX - 1) == 0); /* * Ensure the helper woke from the poll() call. @@ -547,7 +603,7 @@ ATF_TC_BODY(eventfd_select_poll_kevent_block, tc) * Now read the value, which will reset the eventfd to 0 and * unblock the poll() call. */ - ATF_REQUIRE(eventfd_read(ctx.efd, &efd_value) == 0); + ATF_REQUIRE(rump_sys_eventfd_read(ctx.efd, &efd_value) == 0); ATF_REQUIRE(efd_value == UINT64_MAX - 1); /* @@ -569,7 +625,7 @@ ATF_TC_BODY(eventfd_select_poll_kevent_block, tc) * and unblocks the helper waiting in select(). */ efd_value = UINT64_MAX - 1; - ATF_REQUIRE(eventfd_write(ctx.efd, UINT64_MAX - 1) == 0); + ATF_REQUIRE(rump_sys_eventfd_write(ctx.efd, UINT64_MAX - 1) == 0); /* * Ensure the helper woke from the select() call. @@ -590,7 +646,7 @@ ATF_TC_BODY(eventfd_select_poll_kevent_block, tc) * Now read the value, which will reset the eventfd to 0 and * unblock the select() call. */ - ATF_REQUIRE(eventfd_read(ctx.efd, &efd_value) == 0); + ATF_REQUIRE(rump_sys_eventfd_read(ctx.efd, &efd_value) == 0); ATF_REQUIRE(efd_value == UINT64_MAX - 1); /* @@ -612,7 +668,7 @@ ATF_TC_BODY(eventfd_select_poll_kevent_block, tc) * and unblocks the helper waiting in kevent(). */ efd_value = UINT64_MAX - 1; - ATF_REQUIRE(eventfd_write(ctx.efd, UINT64_MAX - 1) == 0); + ATF_REQUIRE(rump_sys_eventfd_write(ctx.efd, UINT64_MAX - 1) == 0); /* * Ensure the helper woke from the kevent() call. @@ -633,7 +689,7 @@ ATF_TC_BODY(eventfd_select_poll_kevent_block, tc) * Now read the value, which will reset the eventfd to 0 and * unblock the select() call. */ - ATF_REQUIRE(eventfd_read(ctx.efd, &efd_value) == 0); + ATF_REQUIRE(rump_sys_eventfd_read(ctx.efd, &efd_value) == 0); ATF_REQUIRE(efd_value == UINT64_MAX - 1); /* @@ -645,7 +701,7 @@ ATF_TC_BODY(eventfd_select_poll_kevent_block, tc) /* Reap the helper. */ ATF_REQUIRE(pthread_join(helper, &join_val) == 0); - (void) close(ctx.efd); + (void) rump_sys_close(ctx.efd); } /*****************************************************************************/ @@ -661,17 +717,21 @@ eventfd_restart_helper(void * const v) * Thius will not block because it was created with an initial * count of 1. */ - ATF_REQUIRE(eventfd_read(ctx->efd, &efd_value) == 0); + ATF_REQUIRE(rump_sys_eventfd_read(ctx->efd, &efd_value) == 0); ATF_REQUIRE(efd_value == 1); ATF_REQUIRE(wait_barrier(ctx)); /* * Block in read. The main thread will close the descriptor, - * which should unblock us and result in EBADF. + * which should unblock us and result in ERESTART; when we + * restart, this should fail with EBADF. */ ATF_REQUIRE(get_state(ctx) == 666); - ATF_REQUIRE_ERRNO(EBADF, eventfd_read(ctx->efd, &efd_value) == -1); + ATF_REQUIRE_ERRNO(ERESTART, rump_sys_eventfd_read(ctx->efd, &efd_value) + == -1); + ATF_REQUIRE_ERRNO(EBADF, rump_sys_eventfd_read(ctx->efd, &efd_value) + == -1); set_state(ctx, 0); ATF_REQUIRE(wait_barrier(ctx)); @@ -691,9 +751,11 @@ ATF_TC_BODY(eventfd_restart, tc) pthread_t helper; void *join_val; + rump_init(); + init_helper_context(&ctx); - ATF_REQUIRE((ctx.efd = eventfd(1, 0)) >= 0); + ATF_REQUIRE((ctx.efd = rump_sys_eventfd(1, 0)) >= 0); ATF_REQUIRE(pthread_create(&helper, NULL, eventfd_restart_helper, &ctx) == 0); @@ -711,7 +773,7 @@ ATF_TC_BODY(eventfd_restart, tc) * Close the descriptor. This should unblock the reader, * and cause it to receive EBADF. */ - ATF_REQUIRE(close(ctx.efd) == 0); + ATF_REQUIRE(rump_sys_close(ctx.efd) == 0); /* * Ensure that the helper woke from the read() call. @@ -733,8 +795,12 @@ ATF_TC_HEAD(eventfd_badflags, tc) } ATF_TC_BODY(eventfd_badflags, tc) { + + rump_init(); + ATF_REQUIRE_ERRNO(EINVAL, - eventfd(0, ~(EFD_SEMAPHORE | EFD_CLOEXEC | EFD_NONBLOCK)) == -1); + (rump_sys_eventfd(0, ~(EFD_SEMAPHORE | EFD_CLOEXEC | EFD_NONBLOCK)) + == -1)); } /*****************************************************************************/ @@ -750,29 +816,31 @@ ATF_TC_BODY(eventfd_bufsize, tc) eventfd_t efd_value[2]; int efd; - ATF_REQUIRE((efd = eventfd(1, EFD_NONBLOCK)) >= 0); + rump_init(); + + ATF_REQUIRE((efd = rump_sys_eventfd(1, EFD_NONBLOCK)) >= 0); ATF_REQUIRE_ERRNO(EINVAL, - read(efd, efd_value, sizeof(efd_value[0]) - 1) == -1); + rump_sys_read(efd, efd_value, sizeof(efd_value[0]) - 1) == -1); efd_value[0] = 0xdeadbeef; efd_value[1] = 0xdeadbeef; - ATF_REQUIRE(read(efd, efd_value, sizeof(efd_value)) == + ATF_REQUIRE(rump_sys_read(efd, efd_value, sizeof(efd_value)) == sizeof(efd_value[0])); ATF_REQUIRE(efd_value[0] == 1); ATF_REQUIRE(efd_value[1] == 0xdeadbeef); ATF_REQUIRE_ERRNO(EINVAL, - write(efd, efd_value, sizeof(efd_value[0]) - 1) == -1); - ATF_REQUIRE(write(efd, efd_value, sizeof(efd_value)) == + rump_sys_write(efd, efd_value, sizeof(efd_value[0]) - 1) == -1); + ATF_REQUIRE(rump_sys_write(efd, efd_value, sizeof(efd_value)) == sizeof(efd_value[0])); - ATF_REQUIRE(read(efd, efd_value, sizeof(efd_value)) == + ATF_REQUIRE(rump_sys_read(efd, efd_value, sizeof(efd_value)) == sizeof(efd_value[0])); ATF_REQUIRE(efd_value[0] == 1); ATF_REQUIRE(efd_value[1] == 0xdeadbeef); - (void) close(efd); + (void) rump_sys_close(efd); } /*****************************************************************************/ @@ -788,29 +856,32 @@ ATF_TC_BODY(eventfd_fcntl, tc) int efd; int val; - ATF_REQUIRE((efd = eventfd(1, 0)) >= 0); - ATF_REQUIRE((fcntl(efd, F_GETFL) & O_NONBLOCK) == 0); - ATF_REQUIRE(fcntl(efd, F_SETFL, O_NONBLOCK) == 0); - ATF_REQUIRE((fcntl(efd, F_GETFL) & O_NONBLOCK) != 0); - ATF_REQUIRE((fcntl(efd, F_GETFD) & FD_CLOEXEC) == 0); + rump_init(); + + ATF_REQUIRE((efd = rump_sys_eventfd(1, 0)) >= 0); + ATF_REQUIRE((rump_sys_fcntl(efd, F_GETFL) & O_NONBLOCK) == 0); + ATF_REQUIRE(rump_sys_fcntl(efd, F_SETFL, O_NONBLOCK) == 0); + ATF_REQUIRE((rump_sys_fcntl(efd, F_GETFL) & O_NONBLOCK) != 0); + ATF_REQUIRE((rump_sys_fcntl(efd, F_GETFD) & FD_CLOEXEC) == 0); - ATF_REQUIRE(ioctl(efd, FIONREAD, &val) == 0); + ATF_REQUIRE(rump_sys_ioctl(efd, FIONREAD, &val) == 0); ATF_REQUIRE(val == sizeof(eventfd_t)); - ATF_REQUIRE(ioctl(efd, FIONWRITE, &val) == 0); + ATF_REQUIRE(rump_sys_ioctl(efd, FIONWRITE, &val) == 0); ATF_REQUIRE(val == 0); - ATF_REQUIRE_ERRNO(ENOTTY, ioctl(efd, FIONSPACE, &val) == -1); - (void)close(efd); - - ATF_REQUIRE((efd = eventfd(1, EFD_NONBLOCK | EFD_CLOEXEC)) >= 0); - ATF_REQUIRE((fcntl(efd, F_GETFL) & ~O_ACCMODE) == O_NONBLOCK); - ATF_REQUIRE((fcntl(efd, F_GETFD) & FD_CLOEXEC) != 0); - ATF_REQUIRE(fcntl(efd, F_SETFD, 0) == 0); - ATF_REQUIRE((fcntl(efd, F_GETFD) & FD_CLOEXEC) == 0); - ATF_REQUIRE(fcntl(efd, F_SETFD, FD_CLOEXEC) == 0); - ATF_REQUIRE((fcntl(efd, F_GETFD) & FD_CLOEXEC) != 0); - (void)close(efd); + ATF_REQUIRE_ERRNO(ENOTTY, rump_sys_ioctl(efd, FIONSPACE, &val) == -1); + (void)rump_sys_close(efd); + + ATF_REQUIRE((efd = rump_sys_eventfd(1, EFD_NONBLOCK | EFD_CLOEXEC)) + >= 0); + ATF_REQUIRE((rump_sys_fcntl(efd, F_GETFL) & ~O_ACCMODE) == O_NONBLOCK); + ATF_REQUIRE((rump_sys_fcntl(efd, F_GETFD) & FD_CLOEXEC) != 0); + ATF_REQUIRE(rump_sys_fcntl(efd, F_SETFD, 0) == 0); + ATF_REQUIRE((rump_sys_fcntl(efd, F_GETFD) & FD_CLOEXEC) == 0); + ATF_REQUIRE(rump_sys_fcntl(efd, F_SETFD, FD_CLOEXEC) == 0); + ATF_REQUIRE((rump_sys_fcntl(efd, F_GETFD) & FD_CLOEXEC) != 0); + (void)rump_sys_close(efd); } /*****************************************************************************/ From 7725100c8b18771c5f7375e144427477e6dfed0b Mon Sep 17 00:00:00 2001 From: Taylor R Campbell Date: Sun, 19 Nov 2023 01:14:21 +0000 Subject: [PATCH 7/9] rump: New mechanism to interrupt rump_sys_* as if by a signal. PR kern/57705 --- sys/rump/include/rump/rump.h | 5 ++- sys/rump/librump/rumpkern/locks.c | 52 +++++++++++++++++++++++++---- sys/rump/librump/rumpkern/signals.c | 28 ++++++++++++++++ 3 files changed, 77 insertions(+), 8 deletions(-) diff --git a/sys/rump/include/rump/rump.h b/sys/rump/include/rump/rump.h index a4679d225fe7..48d1b794c95e 100644 --- a/sys/rump/include/rump/rump.h +++ b/sys/rump/include/rump/rump.h @@ -64,7 +64,8 @@ enum rump_sigmodel { RUMP_SIGMODEL_IGNORE, RUMP_SIGMODEL__HOST_NOTANYMORE, RUMP_SIGMODEL_RAISE, - RUMP_SIGMODEL_RECORD + RUMP_SIGMODEL_RECORD, + RUMP_SIGMODEL_RECORD_WAKEUP, }; /* flags to rump_lwproc_rfork */ @@ -124,6 +125,8 @@ int rump_init_server(const char *); int rump_daemonize_done(int); #define RUMP_DAEMONIZE_SUCCESS 0 +void rump_lwp_kill(struct lwp *, int); + #ifndef _KERNEL #include #include diff --git a/sys/rump/librump/rumpkern/locks.c b/sys/rump/librump/rumpkern/locks.c index 1b60b8e70706..bb3931708102 100644 --- a/sys/rump/librump/rumpkern/locks.c +++ b/sys/rump/librump/rumpkern/locks.c @@ -378,7 +378,7 @@ cv_destroy(kcondvar_t *cv) } static int -docvwait(kcondvar_t *cv, kmutex_t *mtx, struct timespec *ts) +docvwait(kcondvar_t *cv, kmutex_t *mtx, struct timespec *ts, int catch) { struct lwp *l = curlwp; int rv; @@ -395,9 +395,21 @@ docvwait(kcondvar_t *cv, kmutex_t *mtx, struct timespec *ts) UNLOCKED(mtx, false); - l->l_sched.info = cv; rv = 0; - if (ts) { + + if (catch) { + if (mtx != &proc_lock) + mutex_enter(&proc_lock); + } + l->l_sched.info = cv; + if (catch) { + rv = sigispending(curlwp, 0); + if (mtx != &proc_lock) + mutex_exit(&proc_lock); + } + if (rv) { + /* nothing */ + } else if (ts) { if (rumpuser_cv_timedwait(RUMPCV(cv), RUMPMTX(mtx), ts->tv_sec, ts->tv_nsec)) rv = EWOULDBLOCK; @@ -430,7 +442,15 @@ docvwait(kcondvar_t *cv, kmutex_t *mtx, struct timespec *ts) mutex_enter(mtx); rv = EINTR; } + if (catch) { + if (mtx != &proc_lock) + mutex_enter(&proc_lock); + } l->l_sched.info = NULL; + if (catch) { + if (mtx != &proc_lock) + mutex_exit(&proc_lock); + } return rv; } @@ -441,7 +461,7 @@ cv_wait(kcondvar_t *cv, kmutex_t *mtx) if (__predict_false(rump_threads == 0)) panic("cv_wait without threads"); - (void) docvwait(cv, mtx, NULL); + (void) docvwait(cv, mtx, NULL, /*catch*/0); } int @@ -450,7 +470,7 @@ cv_wait_sig(kcondvar_t *cv, kmutex_t *mtx) if (__predict_false(rump_threads == 0)) panic("cv_wait without threads"); - return docvwait(cv, mtx, NULL); + return docvwait(cv, mtx, NULL, /*catch*/1); } int @@ -460,17 +480,35 @@ cv_timedwait(kcondvar_t *cv, kmutex_t *mtx, int ticks) extern int hz; int rv; + if (ticks == 0) { + cv_wait(cv, mtx); + rv = 0; + } else { + ts.tv_sec = ticks / hz; + ts.tv_nsec = (ticks % hz) * (1000000000/hz); + rv = docvwait(cv, mtx, &ts, /*catch*/0); + } + + return rv; +} + +int +cv_timedwait_sig(kcondvar_t *cv, kmutex_t *mtx, int ticks) +{ + struct timespec ts; + extern int hz; + int rv; + if (ticks == 0) { rv = cv_wait_sig(cv, mtx); } else { ts.tv_sec = ticks / hz; ts.tv_nsec = (ticks % hz) * (1000000000/hz); - rv = docvwait(cv, mtx, &ts); + rv = docvwait(cv, mtx, &ts, /*catch*/1); } return rv; } -__strong_alias(cv_timedwait_sig,cv_timedwait); void cv_signal(kcondvar_t *cv) diff --git a/sys/rump/librump/rumpkern/signals.c b/sys/rump/librump/rumpkern/signals.c index 2eb06f188bf5..556678e08b4d 100644 --- a/sys/rump/librump/rumpkern/signals.c +++ b/sys/rump/librump/rumpkern/signals.c @@ -105,6 +105,22 @@ rumpsig_record(struct proc *p, int signo) } } +static void +rumpsig_record_wakeup(struct proc *p, int signo) +{ + struct lwp *l; + + if (!sigismember(&p->p_sigctx.ps_sigignore, signo)) { + sigaddset(&p->p_sigpend.sp_set, signo); + mutex_enter(p->p_lock); + LIST_FOREACH(l, &p->p_lwps, l_sibling) { + if (l->l_sched.info) + cv_broadcast(l->l_sched.info); + } + mutex_exit(p->p_lock); + } +} + typedef void (*rumpsig_fn)(struct proc *, int); static rumpsig_fn rumpsig = rumpsig_raise; @@ -135,6 +151,9 @@ rump_boot_setsigmodel(enum rump_sigmodel model) case RUMP_SIGMODEL_RECORD: rumpsig = rumpsig_record; break; + case RUMP_SIGMODEL_RECORD_WAKEUP: + rumpsig = rumpsig_record_wakeup; + break; /* for compat, though I doubt anyone is using it */ case RUMP_SIGMODEL__HOST_NOTANYMORE: @@ -143,6 +162,15 @@ rump_boot_setsigmodel(enum rump_sigmodel model) } } +void +rump_lwp_kill(struct lwp *l, int signo) +{ + + mutex_enter(&proc_lock); + psignal(l->l_proc, signo); + mutex_exit(&proc_lock); +} + void psignal(struct proc *p, int sig) { From b9eef60017c35ad6e27bd8293e8527a3611baf65 Mon Sep 17 00:00:00 2001 From: Taylor R Campbell Date: Sun, 19 Nov 2023 01:14:53 +0000 Subject: [PATCH 8/9] t_eventfd: Test interrupting read and write by signals. XXX This currently doesn't register correctly with ATF as an expected failure because of PR misc/57706. PR kern/57703 --- tests/lib/libc/sys/t_eventfd.c | 128 +++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/tests/lib/libc/sys/t_eventfd.c b/tests/lib/libc/sys/t_eventfd.c index bcd2991fa06a..fb62124ce2cb 100644 --- a/tests/lib/libc/sys/t_eventfd.c +++ b/tests/lib/libc/sys/t_eventfd.c @@ -53,6 +53,8 @@ __RCSID("$NetBSD: t_eventfd.c,v 1.3 2022/02/20 15:21:14 thorpej Exp $"); #include #include +#include "h_macros.h" + static int /* XXX copypasta from libc/sys/eventfd_read.c, nix me */ rump_sys_eventfd_read(int efd, eventfd_t *valp) { @@ -886,6 +888,130 @@ ATF_TC_BODY(eventfd_fcntl, tc) /*****************************************************************************/ +static pid_t eventfd_pid; +static struct lwp *eventfd_lwp; + +static void * +eventfd_read_signal_helper(void * const v) +{ + struct helper_context * const ctx = v; + eventfd_t efd_value; + int error; + + rump_pub_lwproc_newlwp(eventfd_pid); + eventfd_lwp = rump_pub_lwproc_curlwp(); + + ATF_REQUIRE(wait_barrier(ctx)); + + ATF_REQUIRE(rump_sys_eventfd_read(ctx->efd, &efd_value) == -1); + error = errno; + ATF_REQUIRE_MSG(error == EINTR, "errno=%d (%s)", error, + strerror(error)); + + return NULL; +} + +ATF_TC(eventfd_read_signal); +ATF_TC_HEAD(eventfd_read_signal, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Tests eventfd reads can be interrupted by signal"); +} +ATF_TC_BODY(eventfd_read_signal, tc) +{ + struct helper_context ctx; + pthread_t helper; + + atf_tc_expect_fail("PR kern/57703:" + " panic: kernel diagnostic assertion" + " \"efd->efd_has_read_waiters == false\" failed"); + + rump_boot_setsigmodel(RUMP_SIGMODEL_RECORD_WAKEUP); + rump_init(); + + RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG)); + RL(eventfd_pid = rump_sys_getpid()); + + init_helper_context(&ctx); + + RL(ctx.efd = rump_sys_eventfd(0, 0)); + RZ(pthread_create(&helper, NULL, &eventfd_read_signal_helper, &ctx)); + + ATF_REQUIRE(wait_barrier(&ctx)); /* wait for helper to start */ + (void)sleep(2); /* wait for the read to block */ + (void)alarm(1); /* set a deadline */ + rump_lwp_kill(eventfd_lwp, SIGUSR1); /* interrupt helper with signal */ + RZ(pthread_join(helper, NULL)); /* wait for helper to wake and fail */ + (void)alarm(0); /* clear deadline */ + + RL(rump_sys_close(ctx.efd)); +} + +static void * +eventfd_write_signal_helper(void * const v) +{ + struct helper_context * const ctx = v; + eventfd_t efd_value = 1; + int error; + + rump_pub_lwproc_newlwp(eventfd_pid); + eventfd_lwp = rump_pub_lwproc_curlwp(); + + ATF_REQUIRE(wait_barrier(ctx)); + + ATF_REQUIRE(rump_sys_eventfd_write(ctx->efd, efd_value) == -1); + error = errno; + ATF_REQUIRE_MSG(error == EINTR, "errno=%d (%s)", error, + strerror(error)); + + return NULL; +} + +ATF_TC(eventfd_write_signal); +ATF_TC_HEAD(eventfd_write_signal, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Tests eventfd writes can be interrupted by signal"); +} +ATF_TC_BODY(eventfd_write_signal, tc) +{ + struct helper_context ctx; + pthread_t helper; + + atf_tc_expect_fail("PR kern/57703:" + " panic: kernel diagnostic assertion" + " \"efd->efd_has_write_waiters == false\" failed"); + + rump_boot_setsigmodel(RUMP_SIGMODEL_RECORD_WAKEUP); + rump_init(); + + RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG)); + RL(eventfd_pid = rump_sys_getpid()); + + init_helper_context(&ctx); + + /* + * eventfd takes unsigned, not eventfd_t = uint64_t, as its + * initial value argument, so we have to do it in a separate + * eventfd_write call. + */ + RL(ctx.efd = rump_sys_eventfd(0, 0)); + RL(rump_sys_eventfd_write(ctx.efd, UINT64_MAX - 1)); + + RZ(pthread_create(&helper, NULL, &eventfd_write_signal_helper, &ctx)); + + ATF_REQUIRE(wait_barrier(&ctx)); /* wait for helper to start */ + (void)sleep(2); /* wait for the write to block */ + (void)alarm(1); /* set a deadline */ + rump_lwp_kill(eventfd_lwp, SIGUSR1); /* interrupt helper with signal */ + RZ(pthread_join(helper, NULL)); /* wait for helper to wake and fail */ + (void)alarm(0); /* clear deadline */ + + RL(rump_sys_close(ctx.efd)); +} + +/*****************************************************************************/ + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, eventfd_normal); @@ -896,6 +1022,8 @@ ATF_TP_ADD_TCS(tp) ATF_TP_ADD_TC(tp, eventfd_select_poll_kevent_block); ATF_TP_ADD_TC(tp, eventfd_restart); ATF_TP_ADD_TC(tp, eventfd_fcntl); + ATF_TP_ADD_TC(tp, eventfd_read_signal); + ATF_TP_ADD_TC(tp, eventfd_write_signal); return atf_no_error(); } From 2c624c91cccf6991aaedf753180935e353609b2c Mon Sep 17 00:00:00 2001 From: Taylor R Campbell Date: Sun, 19 Nov 2023 01:18:29 +0000 Subject: [PATCH 9/9] eventfd(2): Omit needless micro-optimization causing PR kern/57703. --- sys/kern/sys_eventfd.c | 26 ++++---------------------- tests/lib/libc/sys/t_eventfd.c | 8 -------- 2 files changed, 4 insertions(+), 30 deletions(-) diff --git a/sys/kern/sys_eventfd.c b/sys/kern/sys_eventfd.c index f7ced730012b..37359cbf1737 100644 --- a/sys/kern/sys_eventfd.c +++ b/sys/kern/sys_eventfd.c @@ -69,8 +69,6 @@ struct eventfd { eventfd_t efd_val; int64_t efd_nwaiters; bool efd_restarting; - bool efd_has_read_waiters; - bool efd_has_write_waiters; bool efd_is_semaphore; /* @@ -117,8 +115,6 @@ eventfd_destroy(struct eventfd * const efd) { KASSERT(efd->efd_nwaiters == 0); - KASSERT(efd->efd_has_read_waiters == false); - KASSERT(efd->efd_has_write_waiters == false); cv_destroy(&efd->efd_read_wait); cv_destroy(&efd->efd_write_wait); @@ -155,10 +151,8 @@ eventfd_wait(struct eventfd * const efd, int const fflag, bool const is_write) } if (is_write) { - efd->efd_has_write_waiters = true; waitcv = &efd->efd_write_wait; } else { - efd->efd_has_read_waiters = true; waitcv = &efd->efd_read_wait; } @@ -194,17 +188,11 @@ eventfd_wake(struct eventfd * const efd, bool const is_write) int pollev; if (is_write) { - if (efd->efd_has_read_waiters) { - waitcv = &efd->efd_read_wait; - efd->efd_has_read_waiters = false; - } + waitcv = &efd->efd_read_wait; sel = &efd->efd_read_sel; pollev = POLLIN | POLLRDNORM; } else { - if (efd->efd_has_write_waiters) { - waitcv = &efd->efd_write_wait; - efd->efd_has_write_waiters = false; - } + waitcv = &efd->efd_write_wait; sel = &efd->efd_write_sel; pollev = POLLOUT | POLLWRNORM; } @@ -537,14 +525,8 @@ eventfd_fop_restart(file_t * const fp) if (efd->efd_nwaiters != 0) { efd->efd_restarting = true; - if (efd->efd_has_read_waiters) { - cv_broadcast(&efd->efd_read_wait); - efd->efd_has_read_waiters = false; - } - if (efd->efd_has_write_waiters) { - cv_broadcast(&efd->efd_write_wait); - efd->efd_has_write_waiters = false; - } + cv_broadcast(&efd->efd_read_wait); + cv_broadcast(&efd->efd_write_wait); } mutex_exit(&efd->efd_lock); diff --git a/tests/lib/libc/sys/t_eventfd.c b/tests/lib/libc/sys/t_eventfd.c index fb62124ce2cb..f0459f4c73bf 100644 --- a/tests/lib/libc/sys/t_eventfd.c +++ b/tests/lib/libc/sys/t_eventfd.c @@ -922,10 +922,6 @@ ATF_TC_BODY(eventfd_read_signal, tc) struct helper_context ctx; pthread_t helper; - atf_tc_expect_fail("PR kern/57703:" - " panic: kernel diagnostic assertion" - " \"efd->efd_has_read_waiters == false\" failed"); - rump_boot_setsigmodel(RUMP_SIGMODEL_RECORD_WAKEUP); rump_init(); @@ -978,10 +974,6 @@ ATF_TC_BODY(eventfd_write_signal, tc) struct helper_context ctx; pthread_t helper; - atf_tc_expect_fail("PR kern/57703:" - " panic: kernel diagnostic assertion" - " \"efd->efd_has_write_waiters == false\" failed"); - rump_boot_setsigmodel(RUMP_SIGMODEL_RECORD_WAKEUP); rump_init();