Skip to contentSkip to conversation

add epoll_pwait2 syscall

PATCH Superseded Willem de Bruijn 4 participants 25 messages Nov 18, 2020 – Jan 11, 2021

Subsystems (19)
Arm (32-bit) x86 VFS PowerPC +15 more subsystems

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover add epoll_pwait2 syscall

From: Willem de Bruijn <[email protected]>

Enable nanosecond timeouts for epoll.

Analogous to pselect and ppoll, introduce an epoll_wait syscall variant that takes a struct timespec instead of int timeout.

See patch 1 for more details.

patch 1: new syscall
patch 2: selftest

Willem de Bruijn (2):
epoll: add nsec timeout support with epoll_pwait2 selftests/filesystems: expand epoll with epoll_pwait2

 arch/alpha/kernel/syscalls/syscall.tbl        |   1 +
 arch/arm/tools/syscall.tbl                    |   1 +
 arch/arm64/include/asm/unistd.h               |   2 +-
 arch/arm64/include/asm/unistd32.h             |   2 +
 arch/ia64/kernel/syscalls/syscall.tbl         |   1 +
 arch/m68k/kernel/syscalls/syscall.tbl         |   1 +
 arch/microblaze/kernel/syscalls/syscall.tbl   |   1 +
 arch/mips/kernel/syscalls/syscall_n32.tbl     |   1 +
 arch/mips/kernel/syscalls/syscall_n64.tbl     |   1 +
 arch/mips/kernel/syscalls/syscall_o32.tbl     |   1 +
 arch/parisc/kernel/syscalls/syscall.tbl       |   1 +
 arch/powerpc/kernel/syscalls/syscall.tbl      |   1 +
 arch/s390/kernel/syscalls/syscall.tbl         |   1 +
 arch/sh/kernel/syscalls/syscall.tbl           |   1 +
 arch/sparc/kernel/syscalls/syscall.tbl        |   1 +
 arch/x86/entry/syscalls/syscall_32.tbl        |   1 +
 arch/x86/entry/syscalls/syscall_64.tbl        |   1 +
 arch/xtensa/kernel/syscalls/syscall.tbl       |   1 +
 fs/eventpoll.c                                | 106 +++++++++++++++---
 include/linux/compat.h                        |   6 +
 include/linux/syscalls.h                      |   5 +
 include/uapi/asm-generic/unistd.h             |   4 +-
 kernel/sys_ni.c                               |   2 +
 .../filesystems/epoll/epoll_wakeup_test.c     |  70 ++++++++++++
 24 files changed, 193 insertions(+), 20 deletions(-)
Signature · 1 line
2.29.2.454.gaff20da3a2-goog
#1 lore raw

PATCH v3 1/2 epoll: add nsec timeout support with epoll_pwait2

From: Willem de Bruijn <[email protected]>

Add syscall epoll_pwait2, an epoll_wait variant with nsec resolution that replaces int timeout with struct timespec. It is equivalent otherwise.

int epoll_pwait2(int fd, struct epoll_event *events,
int maxevents,
const struct timespec *timeout, const sigset_t *sigset);

The underlying hrtimer is already programmed with nsec resolution. pselect and ppoll also set nsec resolution timeout with timespec.

The sigset_t in epoll_pwait has a compat variant. epoll_pwait2 needs the same.

For timespec, only support this new interface on 2038 aware platforms that define __kernel_timespec_t. So no CONFIG_COMPAT_32BIT_TIME.

Changes
v3:
- rewrite: add epoll_pwait2 syscall instead of epoll_create1 flag v2:
- cast to s64: avoid overflow on 32-bit platforms (Shuo Chen)
- minor commit message rewording

---

This version applies cleanly to linux-next-20201117.

---
 arch/alpha/kernel/syscalls/syscall.tbl      |   1 +
 arch/arm/tools/syscall.tbl                  |   1 +
 arch/arm64/include/asm/unistd.h             |   2 +-
 arch/arm64/include/asm/unistd32.h           |   2 +
 arch/ia64/kernel/syscalls/syscall.tbl       |   1 +
 arch/m68k/kernel/syscalls/syscall.tbl       |   1 +
 arch/microblaze/kernel/syscalls/syscall.tbl |   1 +
 arch/mips/kernel/syscalls/syscall_n32.tbl   |   1 +
 arch/mips/kernel/syscalls/syscall_n64.tbl   |   1 +
 arch/mips/kernel/syscalls/syscall_o32.tbl   |   1 +
 arch/parisc/kernel/syscalls/syscall.tbl     |   1 +
 arch/powerpc/kernel/syscalls/syscall.tbl    |   1 +
 arch/s390/kernel/syscalls/syscall.tbl       |   1 +
 arch/sh/kernel/syscalls/syscall.tbl         |   1 +
 arch/sparc/kernel/syscalls/syscall.tbl      |   1 +
 arch/x86/entry/syscalls/syscall_32.tbl      |   1 +
 arch/x86/entry/syscalls/syscall_64.tbl      |   1 +
 arch/xtensa/kernel/syscalls/syscall.tbl     |   1 +
 fs/eventpoll.c                              | 106 ++++++++++++++++----
 include/linux/compat.h                      |   6 ++
 include/linux/syscalls.h                    |   5 +
 include/uapi/asm-generic/unistd.h           |   4 +-
 kernel/sys_ni.c                             |   2 +
 23 files changed, 123 insertions(+), 20 deletions(-)
arch/alpha/kernel/syscalls/syscall.tbl +1 -0
index c5cc5bfa2062..e0a599bfb0e1 100644
@@ -481,3 +481,4 @@
 549	common	faccessat2			sys_faccessat2
 550	common	process_madvise			sys_process_madvise
 551	common	watch_mount			sys_watch_mount
+552	common	epoll_pwait2			sys_epoll_pwait2
arch/arm/tools/syscall.tbl +1 -0
index 47325b3b661a..dbde88a855b6 100644
@@ -455,3 +455,4 @@
 439	common	faccessat2			sys_faccessat2
 440	common	process_madvise			sys_process_madvise
 441	common	watch_mount			sys_watch_mount
+443	common	epoll_pwait2			sys_epoll_pwait2
arch/arm64/include/asm/unistd.h +1 -1
index 949788f5ba40..d1f7d35f986e 100644
@@ -38,7 +38,7 @@
 #define __ARM_NR_compat_set_tls		(__ARM_NR_COMPAT_BASE + 5)
 #define __ARM_NR_COMPAT_END		(__ARM_NR_COMPAT_BASE + 0x800)
 
-#define __NR_compat_syscalls		443
+#define __NR_compat_syscalls		444
 #endif
 
 #define __ARCH_WANT_SYS_CLONE
arch/arm64/include/asm/unistd32.h +2 -0
index c71c3fe0b6cd..b84e24a7e2c0 100644
@@ -893,6 +893,8 @@ __SYSCALL(__NR_process_madvise, sys_process_madvise)
 __SYSCALL(__NR_watch_mount, sys_watch_mount)
 #define __NR_memfd_secret 442
 __SYSCALL(__NR_memfd_secret, sys_memfd_secret)
+#define __NR_epoll_pwait2 443
+__SYSCALL(__NR_epoll_pwait2, sys_epoll_pwait2)
 
 /*
  * Please add new compat syscalls above this comment and update
arch/ia64/kernel/syscalls/syscall.tbl +1 -0
index 033244462350..c8809959636f 100644
@@ -362,3 +362,4 @@
 439	common	faccessat2			sys_faccessat2
 440	common	process_madvise			sys_process_madvise
 441	common	watch_mount			sys_watch_mount
+443	common	epoll_pwait2			sys_epoll_pwait2
arch/m68k/kernel/syscalls/syscall.tbl +1 -0
index efd3ecb3cdfc..dde585616bf8 100644
@@ -441,3 +441,4 @@
 439	common	faccessat2			sys_faccessat2
 440	common	process_madvise			sys_process_madvise
 441	common	watch_mount			sys_watch_mount
+443	common	epoll_pwait2			sys_epoll_pwait2
arch/microblaze/kernel/syscalls/syscall.tbl +1 -0
index 67ae5a5e4d21..4c09f27fedd0 100644
@@ -447,3 +447,4 @@
 439	common	faccessat2			sys_faccessat2
 440	common	process_madvise			sys_process_madvise
 441	common	watch_mount			sys_watch_mount
+443	common	epoll_pwait2			sys_epoll_pwait2
arch/mips/kernel/syscalls/syscall_n32.tbl +1 -0
index c59bc6acc47a..00921244242d 100644
@@ -380,3 +380,4 @@
 439	n32	faccessat2			sys_faccessat2
 440	n32	process_madvise			sys_process_madvise
 441	n32	watch_mount			sys_watch_mount
+443	n32	epoll_pwait2			sys_epoll_pwait2
arch/mips/kernel/syscalls/syscall_n64.tbl +1 -0
index 50bbb9517052..b7920f76358d 100644
@@ -356,3 +356,4 @@
 439	n64	faccessat2			sys_faccessat2
 440	n64	process_madvise			sys_process_madvise
 441	n64	watch_mount			sys_watch_mount
+443	n64	epoll_pwait2			sys_epoll_pwait2
arch/mips/kernel/syscalls/syscall_o32.tbl +1 -0
index 1d5644e221b4..2ab5ed500113 100644
@@ -429,3 +429,4 @@
 439	o32	faccessat2			sys_faccessat2
 440	o32	process_madvise			sys_process_madvise
 441	o32	watch_mount			sys_watch_mount
+443	o32	epoll_pwait2			sys_epoll_pwait2		compat_sys_epoll_pwait2
arch/parisc/kernel/syscalls/syscall.tbl +1 -0
index 4492cc2fce23..9caf7d969846 100644
@@ -439,3 +439,4 @@
 439	common	faccessat2			sys_faccessat2
 440	common	process_madvise			sys_process_madvise
 441	common	watch_mount			sys_watch_mount
+443	common	epoll_pwait2			sys_epoll_pwait2		compat_sys_epoll_pwait2
arch/powerpc/kernel/syscalls/syscall.tbl +1 -0
index ee122bfc2ddc..dce635420226 100644
@@ -531,3 +531,4 @@
 439	common	faccessat2			sys_faccessat2
 440	common	process_madvise			sys_process_madvise
 441	common	watch_mount			sys_watch_mount
+443	common	epoll_pwait2			sys_epoll_pwait2		compat_sys_epoll_pwait2
arch/s390/kernel/syscalls/syscall.tbl +1 -0
index b467b57d66d1..dedfa6db9f8d 100644
@@ -444,3 +444,4 @@
 439  common	faccessat2		sys_faccessat2			sys_faccessat2
 440  common	process_madvise		sys_process_madvise		sys_process_madvise
 441	common	watch_mount		sys_watch_mount			sys_watch_mount
+443  common	epoll_pwait2		sys_epoll_pwait2		sys_epoll_pwait2
arch/sh/kernel/syscalls/syscall.tbl +1 -0
index f9c7ca5cb969..f7a383ae2c48 100644
@@ -444,3 +444,4 @@
 439	common	faccessat2			sys_faccessat2
 440	common	process_madvise			sys_process_madvise
 441	common	watch_mount			sys_watch_mount
+443	common	epoll_pwait2			sys_epoll_pwait2
arch/sparc/kernel/syscalls/syscall.tbl +1 -0
index 341fd4ba053b..a6cdf450d0f4 100644
@@ -487,3 +487,4 @@
 439	common	faccessat2			sys_faccessat2
 440	common	process_madvise			sys_process_madvise
 441	common	watch_mount			sys_watch_mount
+443	common	epoll_pwait2			sys_epoll_pwait2
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
index 109e6681b8fa..9a4e8ec207fc 100644
@@ -447,3 +447,4 @@
 440	i386	process_madvise		sys_process_madvise
 441	i386	watch_mount		sys_watch_mount
 442	i386	memfd_secret		sys_memfd_secret
+443	i386	epoll_pwait2		sys_epoll_pwait2		compat_sys_epoll_pwait2
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
index 742cf17d7725..e2cb5f87e760 100644
@@ -364,6 +364,7 @@
 440	common	process_madvise		sys_process_madvise
 441	common	watch_mount		sys_watch_mount
 442	common	memfd_secret		sys_memfd_secret
+443	common	epoll_pwait2		sys_epoll_pwait2
 
 #
 # Due to a historical design error, certain syscalls are numbered differently
arch/xtensa/kernel/syscalls/syscall.tbl +1 -0
index 6225d87be81f..1c8ae5fd026e 100644
@@ -412,3 +412,4 @@
 439	common	faccessat2			sys_faccessat2
 440	common	process_madvise			sys_process_madvise
 441	common	watch_mount			sys_watch_mount
+443	common	epoll_pwait2			sys_epoll_pwait2
fs/eventpoll.c +88 -18

166 lines not on this page · raw message

include/linux/compat.h +6 -0
index 14d514233e1d..76dc28a2e7f1 100644
@@ -505,6 +505,12 @@ asmlinkage long compat_sys_epoll_pwait(int epfd,
 			int maxevents, int timeout,
 			const compat_sigset_t __user *sigmask,
 			compat_size_t sigsetsize);
+asmlinkage long compat_sys_epoll_pwait2(int epfd,
+			struct epoll_event __user *events,
+			int maxevents,
+			const struct __kernel_timespec __user *timeout,
+			const compat_sigset_t __user *sigmask,
+			compat_size_t sigsetsize);
 
 /* fs/fcntl.c */
 asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd,
include/linux/syscalls.h +5 -0
index e87a96ace85b..2ba45fb7874c 100644
@@ -362,6 +362,11 @@ asmlinkage long sys_epoll_pwait(int epfd, struct epoll_event __user *events,
 				int maxevents, int timeout,
 				const sigset_t __user *sigmask,
 				size_t sigsetsize);
+asmlinkage long sys_epoll_pwait2(int epfd, struct epoll_event __user *events,
+				 int maxevents,
+				 const struct __kernel_timespec __user *timeout,
+				 const sigset_t __user *sigmask,
+				 size_t sigsetsize);
 
 /* fs/fcntl.c */
 asmlinkage long sys_dup(unsigned int fildes);
include/uapi/asm-generic/unistd.h +3 -1
index 3bc087d14535..aa883388700e 100644
@@ -865,9 +865,11 @@ __SYSCALL(__NR_watch_mount, sys_watch_mount)
 #define __NR_memfd_secret 442
 __SYSCALL(__NR_memfd_secret, sys_memfd_secret)
 #endif
+#define __NR_epoll_pwait2 443
+__SC_COMP(__NR_epoll_pwait2, sys_epoll_pwait2, compat_sys_epoll_pwait2)
 
 #undef __NR_syscalls
-#define __NR_syscalls 443
+#define __NR_syscalls 444
 
 /*
  * 32 bit systems traditionally used different
kernel/sys_ni.c +2 -0
index 805fd7a668be..869aa6b5bf34 100644
@@ -68,6 +68,8 @@ COND_SYSCALL(epoll_create1);
 COND_SYSCALL(epoll_ctl);
 COND_SYSCALL(epoll_pwait);
 COND_SYSCALL_COMPAT(epoll_pwait);
+COND_SYSCALL(epoll_pwait2);
+COND_SYSCALL_COMPAT(epoll_pwait2);
 
 /* fs/fcntl.c */
 
Signature · 1 line
2.29.2.454.gaff20da3a2-goog
#2 lore raw

Re: [PATCH v3 1/2] epoll: add nsec timeout support with epoll_pwait2

fs/eventpoll.c static int ep_send_events(struct eventpoll *ep, · line 1724

Quoted 13 lines · Willem de Bruijn
fs/eventpoll.c +3 -5
-static inline struct timespec64 ep_set_mstimeout(long ms)
+static inline struct timespec64 ep_set_nstimeout(s64 timeout)
 {
-	struct timespec64 now, ts = {
-		.tv_sec = ms / MSEC_PER_SEC,
-		.tv_nsec = NSEC_PER_MSEC * (ms % MSEC_PER_SEC),
-	};
+	struct timespec64 now, ts;
 
+	ts = ns_to_timespec64(timeout);
 	ktime_get_ts64(&now);
 	return timespec64_add_safe(now, ts);
 }

Why do you pass around an s64 for timeout, converting it to and from a timespec64 instead of passing around a timespec64?

#3 lore raw

Re: [PATCH v3 1/2] epoll: add nsec timeout support with epoll_pwait2

Quoted 11 lines · Matthew Wilcox
Quoted 9 lines · Willem de Bruijn
fs/eventpoll.c +2 -5
-static inline struct timespec64 ep_set_mstimeout(long ms)
+static inline struct timespec64 ep_set_nstimeout(s64 timeout)
 {
-     struct timespec64 now, ts = {
-             .tv_sec = ms / MSEC_PER_SEC,
-             .tv_nsec = NSEC_PER_MSEC * (ms % MSEC_PER_SEC),
-     };
+     struct timespec64 now, ts;
Quoted 7 lines
Quoted 4 lines
+     ts = ns_to_timespec64(timeout);
      ktime_get_ts64(&now);
      return timespec64_add_safe(now, ts);

}

Why do you pass around an s64 for timeout, converting it to and from a timespec64 instead of passing around a timespec64?

I implemented both approaches. The alternative was no simpler. Conversion in existing epoll_wait, epoll_pwait and epoll_pwait (compat) becomes a bit more complex and adds a stack variable there if passing the timespec64 by reference. And in ep_poll the ternary timeout test > 0, 0, < 0 now requires checking both tv_secs and tv_nsecs. Based on that, I found this simpler. But no strong preference.

#4 lore raw

Re: [PATCH v3 1/2] epoll: add nsec timeout support with epoll_pwait2

Quoted 20 lines · Willem de Bruijn
Quoted 11 lines · Matthew Wilcox
Quoted 9 lines · Willem de Bruijn
fs/eventpoll.c +2 -5
-static inline struct timespec64 ep_set_mstimeout(long ms)
+static inline struct timespec64 ep_set_nstimeout(s64 timeout)
 {
-     struct timespec64 now, ts = {
-             .tv_sec = ms / MSEC_PER_SEC,
-             .tv_nsec = NSEC_PER_MSEC * (ms % MSEC_PER_SEC),
-     };
+     struct timespec64 now, ts;
Quoted 7 lines
Quoted 4 lines
+     ts = ns_to_timespec64(timeout);
      ktime_get_ts64(&now);
      return timespec64_add_safe(now, ts);

}

Why do you pass around an s64 for timeout, converting it to and from a timespec64 instead of passing around a timespec64?

Quoted 7 lines

I implemented both approaches. The alternative was no simpler. Conversion in existing epoll_wait, epoll_pwait and epoll_pwait (compat) becomes a bit more complex and adds a stack variable there if passing the timespec64 by reference. And in ep_poll the ternary timeout test > 0, 0, < 0 now requires checking both tv_secs and tv_nsecs. Based on that, I found this simpler. But no strong preference.

The 64-bit division can be fairly expensive on 32-bit architectures, at least when it doesn't get optimized into a multiply+shift.

Arnd

#5 lore raw

RE: [PATCH v3 1/2] epoll: add nsec timeout support with epoll_pwait2

From: Arnd Bergmann

Quoted 32 lines

Sent: 18 November 2020 15:38

Quoted 20 lines · Willem de Bruijn
Quoted 11 lines · Matthew Wilcox
Quoted 9 lines · Willem de Bruijn
fs/eventpoll.c +2 -5
-static inline struct timespec64 ep_set_mstimeout(long ms)
+static inline struct timespec64 ep_set_nstimeout(s64 timeout)
 {
-     struct timespec64 now, ts = {
-             .tv_sec = ms / MSEC_PER_SEC,
-             .tv_nsec = NSEC_PER_MSEC * (ms % MSEC_PER_SEC),
-     };
+     struct timespec64 now, ts;
Quoted 7 lines
Quoted 4 lines
+     ts = ns_to_timespec64(timeout);
      ktime_get_ts64(&now);
      return timespec64_add_safe(now, ts);

}

Why do you pass around an s64 for timeout, converting it to and from a timespec64 instead of passing around a timespec64?

Quoted 7 lines

I implemented both approaches. The alternative was no simpler. Conversion in existing epoll_wait, epoll_pwait and epoll_pwait (compat) becomes a bit more complex and adds a stack variable there if passing the timespec64 by reference. And in ep_poll the ternary timeout test > 0, 0, < 0 now requires checking both tv_secs and tv_nsecs. Based on that, I found this simpler. But no strong preference.

Quoted 2 lines

The 64-bit division can be fairly expensive on 32-bit architectures, at least when it doesn't get optimized into a multiply+shift.

I'd have thought you'd want to do everything in 64bit nanosecs. Conversions to/from any of the 'timespec' structure are expensive.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#6 lore raw

Re: [PATCH v3 1/2] epoll: add nsec timeout support with epoll_pwait2

Quoted 34 lines · David Laight

From: Arnd Bergmann

Quoted 32 lines

Sent: 18 November 2020 15:38

Quoted 27 lines · Willem de Bruijn
Quoted 11 lines · Matthew Wilcox
Quoted 9 lines · Willem de Bruijn
fs/eventpoll.c +2 -5
-static inline struct timespec64 ep_set_mstimeout(long ms)
+static inline struct timespec64 ep_set_nstimeout(s64 timeout)
 {
-     struct timespec64 now, ts = {
-             .tv_sec = ms / MSEC_PER_SEC,
-             .tv_nsec = NSEC_PER_MSEC * (ms % MSEC_PER_SEC),
-     };
+     struct timespec64 now, ts;
Quoted 7 lines
Quoted 4 lines
+     ts = ns_to_timespec64(timeout);
      ktime_get_ts64(&now);
      return timespec64_add_safe(now, ts);

}

Why do you pass around an s64 for timeout, converting it to and from a timespec64 instead of passing around a timespec64?

I implemented both approaches. The alternative was no simpler. Conversion in existing epoll_wait, epoll_pwait and epoll_pwait (compat) becomes a bit more complex and adds a stack variable there if passing the timespec64 by reference. And in ep_poll the ternary timeout test > 0, 0, < 0 now requires checking both tv_secs and tv_nsecs. Based on that, I found this simpler. But no strong preference.

Quoted 5 lines
Quoted 2 lines

The 64-bit division can be fairly expensive on 32-bit architectures, at least when it doesn't get optimized into a multiply+shift.

I'd have thought you'd want to do everything in 64bit nanosecs. Conversions to/from any of the 'timespec' structure are expensive.

I took another look at this.

The only real reason for the timespec64 is that select_estimate_accuracy takes that type. Which makes sense, because do_select does.

But for epoll, this is inefficient: in ep_set_mstimeout it calls ktime_get_ts64 to convert timeout to an offset from current time, only to pass it to select_estimate_accuracy to then perform another ktime_get_ts64 and subtract this to get back to (approx.) the original timeout.

How about a separate patch that adds epoll_estimate_accuracy with the same rules (wrt rt_task, current->timer_slack, nice and upper bound) but taking an s64 timeout.

One variation, since it is approximate, I suppose we could even replace division by a right shift?

After that, using s64 everywhere is indeed much simpler. And with that I will revise the new epoll_pwait2 interface to take a long long instead of struct timespec.

Apologies for the delay. I forgot that I'm only subscribed to netdev@ in my main email account.

#7 lore raw

Re: [PATCH v3 1/2] epoll: add nsec timeout support with epoll_pwait2

Quoted 10 lines · Willem de Bruijn

But for epoll, this is inefficient: in ep_set_mstimeout it calls ktime_get_ts64 to convert timeout to an offset from current time, only to pass it to select_estimate_accuracy to then perform another ktime_get_ts64 and subtract this to get back to (approx.) the original timeout.

How about a separate patch that adds epoll_estimate_accuracy with the same rules (wrt rt_task, current->timer_slack, nice and upper bound) but taking an s64 timeout.

Quoted 6 lines

One variation, since it is approximate, I suppose we could even replace division by a right shift?

After that, using s64 everywhere is indeed much simpler. And with that I will revise the new epoll_pwait2 interface to take a long long instead of struct timespec.

I think the userspace interface should take a struct timespec for consistency with ppoll and pselect. And epoll should use poll_select_set_timeout() to convert the relative timeout to an absolute endtime. Make epoll more consistent with select/poll, not less ...

#8 lore raw

Re: [PATCH v3 1/2] epoll: add nsec timeout support with epoll_pwait2

Quoted 15 lines · Matthew Wilcox
Quoted 13 lines · Willem de Bruijn

But for epoll, this is inefficient: in ep_set_mstimeout it calls ktime_get_ts64 to convert timeout to an offset from current time, only to pass it to select_estimate_accuracy to then perform another ktime_get_ts64 and subtract this to get back to (approx.) the original timeout.

How about a separate patch that adds epoll_estimate_accuracy with the same rules (wrt rt_task, current->timer_slack, nice and upper bound) but taking an s64 timeout.

One variation, since it is approximate, I suppose we could even replace division by a right shift?

Quoted 8 lines
Quoted 3 lines

After that, using s64 everywhere is indeed much simpler. And with that I will revise the new epoll_pwait2 interface to take a long long instead of struct timespec.

I think the userspace interface should take a struct timespec for consistency with ppoll and pselect. And epoll should use poll_select_set_timeout() to convert the relative timeout to an absolute endtime. Make epoll more consistent with select/poll, not less ...

Okay. The absolute time is also needed for schedule_hrtimeout_range, so it could not be entirely avoided, anyway.

#9 lore raw

Re: [PATCH v3 1/2] epoll: add nsec timeout support with epoll_pwait2

In reply to Matthew Wilcox
Quoted 7 lines · Matthew Wilcox
Quoted 5 lines · Willem de Bruijn

But for epoll, this is inefficient: in ep_set_mstimeout it calls ktime_get_ts64 to convert timeout to an offset from current time, only to pass it to select_estimate_accuracy to then perform another ktime_get_ts64 and subtract this to get back to (approx.) the original timeout.

Right, it would be good to avoid the second ktime_get_ts64(), as reading the clocksource itself can be expensive.

Quoted 6 lines
Quoted 6 lines

How about a separate patch that adds epoll_estimate_accuracy with the same rules (wrt rt_task, current->timer_slack, nice and upper bound) but taking an s64 timeout.

One variation, since it is approximate, I suppose we could even replace division by a right shift?

The right shift would work indeed, but it's also a bit ugly unless __estimate_accuracy() is changed to always use the same shift.

I see that on 32-bit ARM, select_estimate_accuracy() calls the external __aeabi_idiv() function to do the 32-bit division, so changing it to a shift would speed up select as well.

Changing select_estimate_accuracy() to take the relative timeout as an argument to avoid the extra ktime_get_ts64() should have a larger impact.

Quoted 8 lines
Quoted 3 lines

After that, using s64 everywhere is indeed much simpler. And with that I will revise the new epoll_pwait2 interface to take a long long instead of struct timespec.

I think the userspace interface should take a struct timespec for consistency with ppoll and pselect. And epoll should use poll_select_set_timeout() to convert the relative timeout to an absolute endtime. Make epoll more consistent with select/poll, not less ...

I don't see a problem with an s64 timeout if that makes the interface simpler by avoiding differences between the 32-bit and 64-bit ABIs.

More importantly, I think it should differ from poll/select by calculating and writing back the remaining timeout.

I don't know what the latest view on absolute timeouts at the syscall ABI is, it would probably simplify the implementation, but make it less consistent with the others. Futex uses absolute timeouts, but is itself inconsistent about that.

Arnd

#10 lore raw

Re: [PATCH v3 1/2] epoll: add nsec timeout support with epoll_pwait2

Quoted 23 lines · Arnd Bergmann
Quoted 7 lines · Matthew Wilcox
Quoted 5 lines · Willem de Bruijn

But for epoll, this is inefficient: in ep_set_mstimeout it calls ktime_get_ts64 to convert timeout to an offset from current time, only to pass it to select_estimate_accuracy to then perform another ktime_get_ts64 and subtract this to get back to (approx.) the original timeout.

Right, it would be good to avoid the second ktime_get_ts64(), as reading the clocksource itself can be expensive.

Quoted 6 lines
Quoted 6 lines

How about a separate patch that adds epoll_estimate_accuracy with the same rules (wrt rt_task, current->timer_slack, nice and upper bound) but taking an s64 timeout.

One variation, since it is approximate, I suppose we could even replace division by a right shift?

The right shift would work indeed, but it's also a bit ugly unless __estimate_accuracy() is changed to always use the same shift.

Quoted 7 lines

I see that on 32-bit ARM, select_estimate_accuracy() calls the external __aeabi_idiv() function to do the 32-bit division, so changing it to a shift would speed up select as well.

Changing select_estimate_accuracy() to take the relative timeout as an argument to avoid the extra ktime_get_ts64() should have a larger impact.

It could be done by having poll_select_set_timeout take an extra u64* slack, call select_estimate_accuracy before adding in the current time and then pass the slack down to do_select and do_sys_poll, also through core_sys_select and compat_core_sys_select.

It could be a patch independent from this new syscall. Since it changes poll_select_set_timeout it clearly has a conflict with the planned next revision of this. I can include it in the next patchset to decide whether it's worth it.

Quoted 12 lines
Quoted 8 lines
Quoted 3 lines

After that, using s64 everywhere is indeed much simpler. And with that I will revise the new epoll_pwait2 interface to take a long long instead of struct timespec.

I think the userspace interface should take a struct timespec for consistency with ppoll and pselect. And epoll should use poll_select_set_timeout() to convert the relative timeout to an absolute endtime. Make epoll more consistent with select/poll, not less ...

I don't see a problem with an s64 timeout if that makes the interface simpler by avoiding differences between the 32-bit and 64-bit ABIs.

Quoted 7 lines

More importantly, I think it should differ from poll/select by calculating and writing back the remaining timeout.

I don't know what the latest view on absolute timeouts at the syscall ABI is, it would probably simplify the implementation, but make it less consistent with the others. Futex uses absolute timeouts, but is itself inconsistent about that.

If the implementation internally uses poll_select_set_timeout and passes around timespec64 *, it won't matter much in terms of performance or implementation. Then there seems to be no downside to following the consistency argument.

#11 lore raw

Re: [PATCH v3 1/2] epoll: add nsec timeout support with epoll_pwait2

Quoted 18 lines · Willem de Bruijn
Quoted 11 lines · Arnd Bergmann

On Thu, Nov 19, 2020 at 3:31 PM Matthew Wilcox <[email protected]> wrote:
The right shift would work indeed, but it's also a bit ugly unless __estimate_accuracy() is changed to always use the same shift.

I see that on 32-bit ARM, select_estimate_accuracy() calls the external __aeabi_idiv() function to do the 32-bit division, so changing it to a shift would speed up select as well.

Changing select_estimate_accuracy() to take the relative timeout as an argument to avoid the extra ktime_get_ts64() should have a larger impact.

It could be done by having poll_select_set_timeout take an extra u64* slack, call select_estimate_accuracy before adding in the current time and then pass the slack down to do_select and do_sys_poll, also through core_sys_select and compat_core_sys_select.

Quoted 4 lines

It could be a patch independent from this new syscall. Since it changes poll_select_set_timeout it clearly has a conflict with the planned next revision of this. I can include it in the next patchset to decide whether it's worth it.

Yes, that sounds good, not sure how much rework this would require.

It would be easier to do if we first merged the native and compat native versions of select/pselect/ppoll by moving the in_compat_syscall() check into combined get_sigset() and get_fd_set() helpers. I would assume you have enough on your plate already and don't want to add that to it.

Quoted 11 lines
Quoted 10 lines

I don't see a problem with an s64 timeout if that makes the interface simpler by avoiding differences between the 32-bit and 64-bit ABIs.

More importantly, I think it should differ from poll/select by calculating and writing back the remaining timeout.

I don't know what the latest view on absolute timeouts at the syscall ABI is, it would probably simplify the implementation, but make it less consistent with the others. Futex uses absolute timeouts, but is itself inconsistent about that.

Quoted 4 lines

If the implementation internally uses poll_select_set_timeout and passes around timespec64 *, it won't matter much in terms of performance or implementation. Then there seems to be no downside to following the consistency argument.

Ok. So to clarify, you would stay with relative __kernel_timespec pointers and not copy back the remaining time, correct?

ARnd

#12 lore raw

Re: [PATCH v3 1/2] epoll: add nsec timeout support with epoll_pwait2

Quoted 26 lines · Arnd Bergmann
Quoted 18 lines · Willem de Bruijn
Quoted 11 lines · Arnd Bergmann

On Thu, Nov 19, 2020 at 3:31 PM Matthew Wilcox <[email protected]> wrote:
The right shift would work indeed, but it's also a bit ugly unless __estimate_accuracy() is changed to always use the same shift.

I see that on 32-bit ARM, select_estimate_accuracy() calls the external __aeabi_idiv() function to do the 32-bit division, so changing it to a shift would speed up select as well.

Changing select_estimate_accuracy() to take the relative timeout as an argument to avoid the extra ktime_get_ts64() should have a larger impact.

It could be done by having poll_select_set_timeout take an extra u64* slack, call select_estimate_accuracy before adding in the current time and then pass the slack down to do_select and do_sys_poll, also through core_sys_select and compat_core_sys_select.

Quoted 4 lines

It could be a patch independent from this new syscall. Since it changes poll_select_set_timeout it clearly has a conflict with the planned next revision of this. I can include it in the next patchset to decide whether it's worth it.

Quoted 7 lines

Yes, that sounds good, not sure how much rework this would require.

It would be easier to do if we first merged the native and compat native versions of select/pselect/ppoll by moving the in_compat_syscall() check into combined get_sigset() and get_fd_set() helpers. I would assume you have enough on your plate already and don't want to add that to it.

Thanks for the suggestion.

I do have an initial patchset. As expected, it does involve quite a bit of code churn to pass slack through the callers. I'll take a look at your suggestion to simplify it.

As is, the patchset is not ready to send to the list for possible merge. In the meantime, I did push the patchset to github at
https://github.com/wdebruij/linux/commits/epoll-nstimeo-1 . I can send a version marked RFC to the list if that's easier.

I made the slack specific changes in two separate patches, one to fs/select.c and one to fs/eventpoll.c, and placed these at the end of the patchset. So we could first finish the syscall and then send this as a separate patchset if it proves complex enough.

Btw, the other change, to convert epoll implementation to timespec64 before adding the syscall, equally adds some code churn compared to patch v3. But perhaps the end state is cleaner and more consistent.

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

I don't see a problem with an s64 timeout if that makes the interface simpler by avoiding differences between the 32-bit and 64-bit ABIs.

More importantly, I think it should differ from poll/select by calculating and writing back the remaining timeout.

I don't know what the latest view on absolute timeouts at the syscall ABI is, it would probably simplify the implementation, but make it less consistent with the others. Futex uses absolute timeouts, but is itself inconsistent about that.

Quoted 7 lines
Quoted 4 lines

If the implementation internally uses poll_select_set_timeout and passes around timespec64 *, it won't matter much in terms of performance or implementation. Then there seems to be no downside to following the consistency argument.

Ok. So to clarify, you would stay with relative __kernel_timespec pointers and not copy back the remaining time, correct?

That's my understanding, and the current implementation.

#13 lore raw

Re: [PATCH v3 1/2] epoll: add nsec timeout support with epoll_pwait2

Quoted 9 lines · Willem de Bruijn
Quoted 4 lines · Arnd Bergmann
Quoted 1 line · Willem de Bruijn

On Thu, Nov 19, 2020 at 10:45 AM Arnd Bergmann <[email protected]> wrote:

Thanks for the suggestion.

Quoted 8 lines

I do have an initial patchset. As expected, it does involve quite a bit of code churn to pass slack through the callers. I'll take a look at your suggestion to simplify it.

As is, the patchset is not ready to send to the list for possible merge. In the meantime, I did push the patchset to github at
https://github.com/wdebruij/linux/commits/epoll-nstimeo-1 . I can send a version marked RFC to the list if that's easier.

Looks all good to me, just two small things I noticed that you can address before sending the new series:

* The div_u64_rem() in ep_timeout_to_timespec() looks wrong, as you are actually dividing a 'long' that does not need it.

* In "epoll: wire up syscall epoll_pwait2", the alpha syscall has the wrong number, it
should be 110 higher than the others, not 109.

Quoted 3 lines

Btw, the other change, to convert epoll implementation to timespec64 before adding the syscall, equally adds some code churn compared to patch v3. But perhaps the end state is cleaner and more consistent.

Right, that's what I meant. If it causes too much churn, don't worry about it it.

Arndd

#14 lore raw

Re: [PATCH v3 1/2] epoll: add nsec timeout support with epoll_pwait2

Quoted 24 lines · Arnd Bergmann
Quoted 9 lines · Willem de Bruijn
Quoted 4 lines · Arnd Bergmann
Quoted 1 line · Willem de Bruijn

On Thu, Nov 19, 2020 at 10:45 AM Arnd Bergmann <[email protected]> wrote:

Thanks for the suggestion.

Quoted 8 lines

I do have an initial patchset. As expected, it does involve quite a bit of code churn to pass slack through the callers. I'll take a look at your suggestion to simplify it.

As is, the patchset is not ready to send to the list for possible merge. In the meantime, I did push the patchset to github at
https://github.com/wdebruij/linux/commits/epoll-nstimeo-1 . I can send a version marked RFC to the list if that's easier.

Looks all good to me, just two small things I noticed that you can address before sending the new series:

Quoted 6 lines

* The div_u64_rem() in ep_timeout_to_timespec() looks wrong, as you are actually dividing a 'long' that does not need it.

* In "epoll: wire up syscall epoll_pwait2", the alpha syscall has the wrong number, it
should be 110 higher than the others, not 109.

Thanks! I'll fix these up.

Quoted 6 lines
Quoted 3 lines

Btw, the other change, to convert epoll implementation to timespec64 before adding the syscall, equally adds some code churn compared to patch v3. But perhaps the end state is cleaner and more consistent.

Right, that's what I meant. If it causes too much churn, don't worry about it it.

I think it'll be better to split the patchsets:

epoll: convert internal api to timespec64
epoll: add syscall epoll_pwait2
epoll: wire up syscall epoll_pwait2 selftests/filesystems: expand epoll with epoll_pwait2

and

select: compute slack based on relative time
epoll: compute slack based on relative time

and judge the slack conversion on its own merit.

I also would rather not tie this up with the compat deduplication. Happy to take a stab at that though. On that note, when combining functions like

int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
fd_set __user *exp, struct timespec64 *end_time, u64 slack)

and

static int compat_core_sys_select(int n, compat_ulong_t __user *inp,
compat_ulong_t __user *outp, compat_ulong_t __user *exp, struct timespec64 *end_time, u64 slack)

by branching on in_compat_syscall() inside get_fd_set/set_fd_set and deprecating their compat_.. counterparts, what would the argument pointers look like? Or is that not the approach you have in mind?

#15 lore raw

Re: [PATCH v3 1/2] epoll: add nsec timeout support with epoll_pwait2

Quoted 10 lines · Willem de Bruijn
Quoted 1 line · Arnd Bergmann

On Fri, Nov 20, 2020 at 5:01 PM Willem de Bruijn <[email protected]> wrote:

I think it'll be better to split the patchsets:

epoll: convert internal api to timespec64
epoll: add syscall epoll_pwait2
epoll: wire up syscall epoll_pwait2 selftests/filesystems: expand epoll with epoll_pwait2

Quoted 6 lines

and

select: compute slack based on relative time
epoll: compute slack based on relative time

and judge the slack conversion on its own merit.

Yes, makes sense.

Quoted 10 lines

I also would rather not tie this up with the compat deduplication. Happy to take a stab at that though. On that note, when combining functions like

int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
fd_set __user *exp, struct timespec64 *end_time, u64 slack)

and

Quoted 7 lines

static int compat_core_sys_select(int n, compat_ulong_t __user *inp,
compat_ulong_t __user *outp, compat_ulong_t __user *exp, struct timespec64 *end_time, u64 slack)

by branching on in_compat_syscall() inside get_fd_set/set_fd_set and deprecating their compat_.. counterparts, what would the argument pointers look like? Or is that not the approach you have in mind?

In this case, the top-level entry points becomes unified, and you get the prototype from core_sys_select() with the native arguments.

I would imagine this can be done like the way I proposed for get_bitmap() in sys_migrate_pages:

https://lore.kernel.org/lkml/[email protected]/

Arnd

#16 lore raw

Re: [PATCH v3 1/2] epoll: add nsec timeout support with epoll_pwait2

Quoted 40 lines · Arnd Bergmann
Quoted 10 lines · Willem de Bruijn
Quoted 1 line · Arnd Bergmann

On Fri, Nov 20, 2020 at 5:01 PM Willem de Bruijn <[email protected]> wrote:

I think it'll be better to split the patchsets:

epoll: convert internal api to timespec64
epoll: add syscall epoll_pwait2
epoll: wire up syscall epoll_pwait2 selftests/filesystems: expand epoll with epoll_pwait2

Quoted 6 lines

and

select: compute slack based on relative time
epoll: compute slack based on relative time

and judge the slack conversion on its own merit.

Yes, makes sense.

Quoted 10 lines

I also would rather not tie this up with the compat deduplication. Happy to take a stab at that though. On that note, when combining functions like

int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
fd_set __user *exp, struct timespec64 *end_time, u64 slack)

and

Quoted 7 lines

static int compat_core_sys_select(int n, compat_ulong_t __user *inp,
compat_ulong_t __user *outp, compat_ulong_t __user *exp, struct timespec64 *end_time, u64 slack)

by branching on in_compat_syscall() inside get_fd_set/set_fd_set and deprecating their compat_.. counterparts, what would the argument pointers look like? Or is that not the approach you have in mind?

Quoted 7 lines

In this case, the top-level entry points becomes unified, and you get the prototype from core_sys_select() with the native arguments.

I would imagine this can be done like the way I proposed for get_bitmap() in sys_migrate_pages:

https://lore.kernel.org/lkml/[email protected]/

Coming back to this. Current patchset includes new select and poll selftests to verify the changes. I need to send a small kselftest patch for that first.

Assuming there's no time pressure, I will finish up and send the main changes after the merge window, for the next release then.

Current state against linux-next at
https://github.com/wdebruij/linux-next-mirror/tree/select-compat-1

#17 lore raw

Re: [PATCH v3 1/2] epoll: add nsec timeout support with epoll_pwait2

Quoted 11 lines · Willem de Bruijn
Quoted 5 lines · Arnd Bergmann

On Fri, Nov 20, 2020 at 11:28 PM Willem de Bruijn <[email protected]> wrote:
I would imagine this can be done like the way I proposed
for get_bitmap() in sys_migrate_pages:

https://lore.kernel.org/lkml/[email protected]/

Coming back to this. Current patchset includes new select and poll selftests to verify the changes. I need to send a small kselftest patch for that first.

Quoted 5 lines

Assuming there's no time pressure, I will finish up and send the main changes after the merge window, for the next release then.

Current state against linux-next at
https://github.com/wdebruij/linux-next-mirror/tree/select-compat-1

Ok, sounds good to me. I've had a (very brief) look and have one
suggestion: instead of open-coding the compat vs native mode in multiple places like

if (!in_compat_syscall())

     return copy_from_user(fdset, ufdset, FDS_BYTES(nr)) ? -EFAULT : 0;
else
     return compat_get_bitmap(fdset, ufdset, nr);

maybe move this into a separate function and call that where needed.

I've done this for the get_bitmap() function in my series at

https://git.kernel.org/pub/scm/linux/kernel/git/arnd/playground.git/commit/?h=compat-alloc-user-space-7&id=b1b23ebb12b635654a2060df49455167a142c5d2

The definition is slightly differrent for cpumask, nodemask and fd_set, so we'd need to try out the best way to structure the code to end up with the most readable version, but it should be possible when there are only three callers (and duplicating the function would be the end of the world either)

Arnd

#18 lore raw

Re: [PATCH v3 1/2] epoll: add nsec timeout support with epoll_pwait2

Quoted 33 lines · Arnd Bergmann
Quoted 11 lines · Willem de Bruijn
Quoted 5 lines · Arnd Bergmann

On Fri, Nov 20, 2020 at 11:28 PM Willem de Bruijn <[email protected]> wrote:
I would imagine this can be done like the way I proposed
for get_bitmap() in sys_migrate_pages:

https://lore.kernel.org/lkml/[email protected]/

Coming back to this. Current patchset includes new select and poll selftests to verify the changes. I need to send a small kselftest patch for that first.

Quoted 5 lines

Assuming there's no time pressure, I will finish up and send the main changes after the merge window, for the next release then.

Current state against linux-next at
https://github.com/wdebruij/linux-next-mirror/tree/select-compat-1

Ok, sounds good to me. I've had a (very brief) look and have one
suggestion: instead of open-coding the compat vs native mode in multiple places like

if (!in_compat_syscall())

     return copy_from_user(fdset, ufdset, FDS_BYTES(nr)) ? -EFAULT : 0;
else
     return compat_get_bitmap(fdset, ufdset, nr);

maybe move this into a separate function and call that where needed.

I've done this for the get_bitmap() function in my series at

Quoted 7 lines

https://git.kernel.org/pub/scm/linux/kernel/git/arnd/playground.git/commit/?h=compat-alloc-user-space-7&id=b1b23ebb12b635654a2060df49455167a142c5d2

The definition is slightly differrent for cpumask, nodemask and fd_set, so we'd need to try out the best way to structure the code to end up with the most readable version, but it should be possible when there are only three callers (and duplicating the function would be the end of the world either)

For fd_set there is only a single caller for each direction. Do you prefer helpers even so?

For sigmask, with three callers, something along the lines of this?

@@ -1138,10 +1135,7 @@ static int do_ppoll(struct pollfd __user
*ufds, unsigned int nfds,

                          return -EINVAL;
          }

- if (!in_compat_syscall())
- ret = set_user_sigmask(sigmask, sigsetsize);
- else
- ret = set_compat_user_sigmask(sigmask, sigsetsize);

  +       ret = set_maybe_compat_user_sigmask(sigmask, sigsetsize);
          if (ret)
                  return ret;

--- a/include/linux/compat.h
+++ b/include/linux/compat.h
@@ -942,6 +942,17 @@ static inline bool in_compat_syscall(void) {
return false; }

+static inline int set_maybe_compat_user_sigmask(const void __user *sigmask,
+ size_t sigsetsize)
+{
+#if defined CONFIG_COMPAT
+ if (unlikely(in_compat_syscall()))
+ return set_compat_user_sigmask(sigmask, sigsetsize);
+#endif
+
+ return set_user_sigmask(sigmask, sigsetsize);
+}

#19 lore raw

Re: [PATCH v3 1/2] epoll: add nsec timeout support with epoll_pwait2

Quoted 66 lines · Willem de Bruijn
Quoted 33 lines · Arnd Bergmann
Quoted 11 lines · Willem de Bruijn
Quoted 5 lines · Arnd Bergmann

On Fri, Nov 20, 2020 at 11:28 PM Willem de Bruijn <[email protected]> wrote:
I would imagine this can be done like the way I proposed
for get_bitmap() in sys_migrate_pages:

https://lore.kernel.org/lkml/[email protected]/

Coming back to this. Current patchset includes new select and poll selftests to verify the changes. I need to send a small kselftest patch for that first.

Quoted 5 lines

Assuming there's no time pressure, I will finish up and send the main changes after the merge window, for the next release then.

Current state against linux-next at
https://github.com/wdebruij/linux-next-mirror/tree/select-compat-1

Ok, sounds good to me. I've had a (very brief) look and have one
suggestion: instead of open-coding the compat vs native mode in multiple places like

if (!in_compat_syscall())

     return copy_from_user(fdset, ufdset, FDS_BYTES(nr)) ? -EFAULT : 0;
else
     return compat_get_bitmap(fdset, ufdset, nr);

maybe move this into a separate function and call that where needed.

I've done this for the get_bitmap() function in my series at

Quoted 7 lines

https://git.kernel.org/pub/scm/linux/kernel/git/arnd/playground.git/commit/?h=compat-alloc-user-space-7&id=b1b23ebb12b635654a2060df49455167a142c5d2

The definition is slightly differrent for cpumask, nodemask and fd_set, so we'd need to try out the best way to structure the code to end up with the most readable version, but it should be possible when there are only three callers (and duplicating the function would be the end of the world either)

For fd_set there is only a single caller for each direction. Do you prefer helpers even so?

For sigmask, with three callers, something along the lines of this?

@@ -1138,10 +1135,7 @@ static int do_ppoll(struct pollfd __user
*ufds, unsigned int nfds,

                          return -EINVAL;
          }

- if (!in_compat_syscall())
- ret = set_user_sigmask(sigmask, sigsetsize);
- else
- ret = set_compat_user_sigmask(sigmask, sigsetsize);

  +       ret = set_maybe_compat_user_sigmask(sigmask, sigsetsize);
          if (ret)
                  return ret;

--- a/include/linux/compat.h
+++ b/include/linux/compat.h
@@ -942,6 +942,17 @@ static inline bool in_compat_syscall(void) {
return false; }

Quoted 10 lines

+static inline int set_maybe_compat_user_sigmask(const void __user *sigmask,
+ size_t sigsetsize)
+{
+#if defined CONFIG_COMPAT
+ if (unlikely(in_compat_syscall()))
+ return set_compat_user_sigmask(sigmask, sigsetsize);
+#endif
+
+ return set_user_sigmask(sigmask, sigsetsize);
+}

set_user_sigmask is the only open-coded variant that is used more than once.

Because it is used in both select.c and eventpoll.c, a helper would have to live in compat.h. This then needs a new dependency on sched_signal.h.

So given that this is a simple branch, it might just make logic more complex, instead of less. I can add this change in a separate patch on top of the original three, to judge whether it is worthwhile.

#20 lore raw

Re: [PATCH v3 1/2] epoll: add nsec timeout support with epoll_pwait2

In reply to Willem de Bruijn
Quoted 37 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Add syscall epoll_pwait2, an epoll_wait variant with nsec resolution that replaces int timeout with struct timespec. It is equivalent otherwise.

int epoll_pwait2(int fd, struct epoll_event *events,
int maxevents,
const struct timespec *timeout, const sigset_t *sigset);

The underlying hrtimer is already programmed with nsec resolution. pselect and ppoll also set nsec resolution timeout with timespec.

The sigset_t in epoll_pwait has a compat variant. epoll_pwait2 needs the same.

For timespec, only support this new interface on 2038 aware platforms that define __kernel_timespec_t. So no CONFIG_COMPAT_32BIT_TIME.

Changes
v3:
- rewrite: add epoll_pwait2 syscall instead of epoll_create1 flag v2:
- cast to s64: avoid overflow on 32-bit platforms (Shuo Chen)
- minor commit message rewording

---

arch/x86/entry/syscalls/syscall_32.tbl +0 -0
index 109e6681b8fa..9a4e8ec207fc 100644

arch/x86/entry/syscalls/syscall_32.tbl line 450

Quoted 4 lines
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
@@ -447,3 +447,4 @@
 440    i386    process_madvise         sys_process_madvise
 441    i386    watch_mount             sys_watch_mount
 442    i386    memfd_secret            sys_memfd_secret
+443    i386    epoll_pwait2            sys_epoll_pwait2                compat_sys_epoll_pwait2

I should have caught this sooner, but this does not work as intended.

x86 will still call epoll_pwait2 with old_timespec32.

One approach is a separate epoll_pwait2_time64 syscall, similar to ppoll_time64. But that was added to work around legacy 32-bit ppoll. Not needed for a new API.

In libc, ppoll_time64 is declared with type struct __timespec64. That type is not defined in Linux uapi. Will need to look at this some more.

#21 lore raw

Re: [PATCH v3 1/2] epoll: add nsec timeout support with epoll_pwait2

Quoted 14 lines · Willem de Bruijn

arch/x86/entry/syscalls/syscall_32.tbl line 450

Quoted 9 lines
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
index 109e6681b8fa..9a4e8ec207fc 100644
@@ -447,3 +447,4 @@
 440    i386    process_madvise         sys_process_madvise
 441    i386    watch_mount             sys_watch_mount
 442    i386    memfd_secret            sys_memfd_secret
+443    i386    epoll_pwait2            sys_epoll_pwait2                compat_sys_epoll_pwait2

I should have caught this sooner, but this does not work as intended.

x86 will still call epoll_pwait2 with old_timespec32.

Quoted 7 lines

One approach is a separate epoll_pwait2_time64 syscall, similar to ppoll_time64. But that was added to work around legacy 32-bit ppoll. Not needed for a new API.

In libc, ppoll_time64 is declared with type struct __timespec64. That type is not defined in Linux uapi. Will need to look at this some more.

The libc __timespec64 corresponds to the __kernel_timespec64 structure in uapi. It is defined to only have 'long' nanoseconds member because that's what c99 and posix require, but the bits are in the position that matches the lower 32 bits of the 64-bit tv_nsec in the kernel, and get_timespec64() performs the necessary conversion to either check or zero the upper bits.

I think all you need in user space is to pass the timeout as a __timespec64 structure and add a conversion in the exported library interface.

Arnd

#22 lore raw

Re: [PATCH v3 1/2] epoll: add nsec timeout support with epoll_pwait2

Quoted 32 lines · Arnd Bergmann
Quoted 14 lines · Willem de Bruijn

arch/x86/entry/syscalls/syscall_32.tbl line 450

Quoted 9 lines
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
index 109e6681b8fa..9a4e8ec207fc 100644
@@ -447,3 +447,4 @@
 440    i386    process_madvise         sys_process_madvise
 441    i386    watch_mount             sys_watch_mount
 442    i386    memfd_secret            sys_memfd_secret
+443    i386    epoll_pwait2            sys_epoll_pwait2                compat_sys_epoll_pwait2

I should have caught this sooner, but this does not work as intended.

x86 will still call epoll_pwait2 with old_timespec32.

Quoted 7 lines

One approach is a separate epoll_pwait2_time64 syscall, similar to ppoll_time64. But that was added to work around legacy 32-bit ppoll. Not needed for a new API.

In libc, ppoll_time64 is declared with type struct __timespec64. That type is not defined in Linux uapi. Will need to look at this some more.

The libc __timespec64 corresponds to the __kernel_timespec64 structure in uapi. It is defined to only have 'long' nanoseconds member because that's what c99 and posix require, but the bits are in the position that matches the lower 32 bits of the 64-bit tv_nsec in the kernel, and get_timespec64() performs the necessary conversion to either check or zero the upper bits.

Quoted 3 lines

I think all you need in user space is to pass the timeout as a __timespec64 structure and add a conversion in the exported library interface.

Indeed, that resolves it, thanks.

I'll define that struct in the selftest and update the definition of sys_epoll_pwait2 there.

#23 lore raw

RFC PATCH 1/1 epoll_wait.2: add epoll_pwait2

In reply to Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Expand the epoll_wait page with epoll_pwait2, an epoll_wait variant that takes a struct timespec to enable nanosecond resolution timeout.

int epoll_pwait2(int fd, struct epoll_event *events,
int maxevents,
const struct timespec *timeout, const sigset_t *sigset);

---
 man2/epoll_wait.2 | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)
man2/epoll_wait.2 +29 -2
index 55890c82a53a..01047df28cb1 100644
@@ -22,7 +22,7 @@
 .\"
 .TH EPOLL_WAIT 2 2020-04-11 "Linux" "Linux Programmer's Manual"
 .SH NAME
-epoll_wait, epoll_pwait \- wait for an I/O event on an epoll file descriptor
+epoll_wait, epoll_pwait, epoll_pwait2 \- wait for an I/O event on an epoll file descriptor
 .SH SYNOPSIS
 .nf
 .B #include <sys/epoll.h>
@@ -32,6 +32,9 @@ epoll_wait, epoll_pwait \- wait for an I/O event on an epoll file descriptor
 .BI "int epoll_pwait(int " epfd ", struct epoll_event *" events ,
 .BI "               int " maxevents ", int " timeout ,
 .BI "               const sigset_t *" sigmask );
+.BI "int epoll_pwait2(int " epfd ", struct epoll_event *" events ,
+.BI "                int " maxevents ", const struct timespec *" timeout ,
+.BI "                const sigset_t *" sigmask );
 .fi
 .SH DESCRIPTION
 The
@@ -170,6 +173,25 @@ argument may be specified as NULL, in which case
 .BR epoll_pwait ()
 is equivalent to
 .BR epoll_wait ().
+.SS epoll_pwait2 ()
+The
+.BR epoll_pwait2 ()
+system call is equivalent to
+.BR epoll_pwait ()
+except for the
+.I timeout
+argument. It takes an argument of type
+.I timespec
+to be able to specify nanosecond resolution timeouts. This argument functions
+the same as in
+.BR pselect (2)
+and
+.BR ppoll (2).
+If
+.I timeout
+is NULL, then
+.BR epoll_pwait2 ()
+can block indefinitely.
 .SH RETURN VALUE
 When successful,
 .BR epoll_wait ()
@@ -217,6 +239,9 @@ Library support is provided in glibc starting with version 2.3.2.
 .BR epoll_pwait ()
 was added to Linux in kernel 2.6.19.
 Library support is provided in glibc starting with version 2.6.
+.PP
+.BR epoll_pwait2 ()
+was added to Linux in kernel 5.11.
 .SH CONFORMING TO
 .BR epoll_wait ()
 is Linux-specific.
@@ -267,7 +292,9 @@ this means that timeouts greater than 35.79 minutes are treated as infinity.
 .SS C library/kernel differences
 The raw
 .BR epoll_pwait ()
-system call has a sixth argument,
+and
+.BR epoll_pwait2 ()
+system calls have a sixth argument,
 .IR "size_t sigsetsize" ,
 which specifies the size in bytes of the
 .IR sigmask
Signature · 1 line
2.29.2.454.gaff20da3a2-goog
#24 lore raw

PATCH v3 2/2 selftests/filesystems: expand epoll with epoll_pwait2

In reply to Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Code coverage for the epoll_pwait2 syscall.

epoll62: Repeat basic test epoll1, but exercising the new syscall.
epoll63: Pass a timespec and exercise the timeout wakeup path.

---
 .../filesystems/epoll/epoll_wakeup_test.c     | 70 +++++++++++++++++++
 1 file changed, 70 insertions(+)
tools/testing/selftests/filesystems/epoll/epoll_wakeup_test.c +70 -0
index 8f82f99f7748..4d5656978746 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #define _GNU_SOURCE
+#include <asm/unistd.h>
 #include <poll.h>
 #include <unistd.h>
 #include <assert.h>
@@ -21,6 +22,18 @@ struct epoll_mtcontext
 	pthread_t waiter;
 };
 
+#ifndef __NR_epoll_pwait2
+#define __NR_epoll_pwait2 -1
+#endif
+
+static inline int sys_epoll_pwait2(int fd, struct epoll_event *events,
+				   int maxevents,
+				   const struct timespec *timeout,
+				   const sigset_t *sigset)
+{
+	return syscall(__NR_epoll_pwait2, fd, events, maxevents, timeout, sigset);
+}
+
 static void signal_handler(int signum)
 {
 }
@@ -3377,4 +3390,61 @@ TEST(epoll61)
 	close(ctx.evfd);
 }
 
+/* Equivalent to basic test epoll1, but exercising epoll_pwait2. */
+TEST(epoll62)
+{
+	int efd;
+	int sfd[2];
+	struct epoll_event e;
+
+	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
+
+	efd = epoll_create(1);
+	ASSERT_GE(efd, 0);
+
+	e.events = EPOLLIN;
+	ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e), 0);
+
+	ASSERT_EQ(write(sfd[1], "w", 1), 1);
+
+	EXPECT_EQ(sys_epoll_pwait2(efd, &e, 1, NULL, NULL), 1);
+	EXPECT_EQ(sys_epoll_pwait2(efd, &e, 1, NULL, NULL), 1);
+
+	close(efd);
+	close(sfd[0]);
+	close(sfd[1]);
+}
+
+/* Epoll_pwait2 basic timeout test. */
+TEST(epoll63)
+{
+	const int cfg_delay_ms = 10;
+	unsigned long long tdiff;
+	struct timespec ts;
+	int efd;
+	int sfd[2];
+	struct epoll_event e;
+
+	ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sfd), 0);
+
+	efd = epoll_create(1);
+	ASSERT_GE(efd, 0);
+
+	e.events = EPOLLIN;
+	ASSERT_EQ(epoll_ctl(efd, EPOLL_CTL_ADD, sfd[0], &e), 0);
+
+	ts.tv_sec = 0;
+	ts.tv_nsec = cfg_delay_ms * 1000 * 1000;
+
+	tdiff = msecs();
+	EXPECT_EQ(sys_epoll_pwait2(efd, &e, 1, &ts, NULL), 0);
+	tdiff = msecs() - tdiff;
+
+	EXPECT_GE(tdiff, cfg_delay_ms);
+
+	close(efd);
+	close(sfd[0]);
+	close(sfd[1]);
+}
+
 TEST_HARNESS_MAIN
Signature · 1 line
2.29.2.454.gaff20da3a2-goog