Skip to content

Commit 7cdf7c2

Browse files
wdebruijtorvalds
authored andcommitted
epoll: convert internal api to timespec64
Patch series "add epoll_pwait2 syscall", v4. 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. This patch (of 4): Make epoll more consistent with select/poll: pass along the timeout as timespec64 pointer. In anticipation of additional changes affecting all three polling mechanisms: - add epoll_pwait2 syscall with timespec semantics, and share poll_select_set_timeout implementation. - compute slack before conversion to absolute time, to save one ktime_get_ts64 call. Link: https://lkml.kernel.org/r/20201121144401.3727659-1-willemdebruijn.kernel@gmail.com Link: https://lkml.kernel.org/r/20201121144401.3727659-2-willemdebruijn.kernel@gmail.com Signed-off-by: Willem de Bruijn <willemb@google.com> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Arnd Bergmann <arnd@arndb.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent e59d3c6 commit 7cdf7c2

1 file changed

Lines changed: 37 additions & 20 deletions

File tree

‎fs/eventpoll.c‎

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,15 +1712,25 @@ static int ep_send_events(struct eventpoll *ep,
17121712
return res;
17131713
}
17141714

1715-
static inline struct timespec64 ep_set_mstimeout(long ms)
1715+
static struct timespec64 *ep_timeout_to_timespec(struct timespec64 *to, long ms)
17161716
{
1717-
struct timespec64 now, ts = {
1718-
.tv_sec = ms / MSEC_PER_SEC,
1719-
.tv_nsec = NSEC_PER_MSEC * (ms % MSEC_PER_SEC),
1720-
};
1717+
struct timespec64 now;
1718+
1719+
if (ms < 0)
1720+
return NULL;
1721+
1722+
if (!ms) {
1723+
to->tv_sec = 0;
1724+
to->tv_nsec = 0;
1725+
return to;
1726+
}
1727+
1728+
to->tv_sec = ms / MSEC_PER_SEC;
1729+
to->tv_nsec = NSEC_PER_MSEC * (ms % MSEC_PER_SEC);
17211730

17221731
ktime_get_ts64(&now);
1723-
return timespec64_add_safe(now, ts);
1732+
*to = timespec64_add_safe(now, *to);
1733+
return to;
17241734
}
17251735

17261736
/**
@@ -1732,16 +1742,16 @@ static inline struct timespec64 ep_set_mstimeout(long ms)
17321742
* stored.
17331743
* @maxevents: Size (in terms of number of events) of the caller event buffer.
17341744
* @timeout: Maximum timeout for the ready events fetch operation, in
1735-
* milliseconds. If the @timeout is zero, the function will not block,
1736-
* while if the @timeout is less than zero, the function will block
1745+
* timespec. If the timeout is zero, the function will not block,
1746+
* while if the @timeout ptr is NULL, the function will block
17371747
* until at least one event has been retrieved (or an error
17381748
* occurred).
17391749
*
17401750
* Returns: Returns the number of ready events which have been fetched, or an
17411751
* error code, in case of error.
17421752
*/
17431753
static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
1744-
int maxevents, long timeout)
1754+
int maxevents, struct timespec64 *timeout)
17451755
{
17461756
int res, eavail, timed_out = 0;
17471757
u64 slack = 0;
@@ -1750,13 +1760,11 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
17501760

17511761
lockdep_assert_irqs_enabled();
17521762

1753-
if (timeout > 0) {
1754-
struct timespec64 end_time = ep_set_mstimeout(timeout);
1755-
1756-
slack = select_estimate_accuracy(&end_time);
1763+
if (timeout && (timeout->tv_sec | timeout->tv_nsec)) {
1764+
slack = select_estimate_accuracy(timeout);
17571765
to = &expires;
1758-
*to = timespec64_to_ktime(end_time);
1759-
} else if (timeout == 0) {
1766+
*to = timespec64_to_ktime(*timeout);
1767+
} else if (timeout) {
17601768
/*
17611769
* Avoid the unnecessary trip to the wait queue loop, if the
17621770
* caller specified a non blocking operation.
@@ -2175,7 +2183,7 @@ SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd,
21752183
* part of the user space epoll_wait(2).
21762184
*/
21772185
static int do_epoll_wait(int epfd, struct epoll_event __user *events,
2178-
int maxevents, int timeout)
2186+
int maxevents, struct timespec64 *to)
21792187
{
21802188
int error;
21812189
struct fd f;
@@ -2209,7 +2217,7 @@ static int do_epoll_wait(int epfd, struct epoll_event __user *events,
22092217
ep = f.file->private_data;
22102218

22112219
/* Time to fish for events ... */
2212-
error = ep_poll(ep, events, maxevents, timeout);
2220+
error = ep_poll(ep, events, maxevents, to);
22132221

22142222
error_fput:
22152223
fdput(f);
@@ -2219,7 +2227,10 @@ static int do_epoll_wait(int epfd, struct epoll_event __user *events,
22192227
SYSCALL_DEFINE4(epoll_wait, int, epfd, struct epoll_event __user *, events,
22202228
int, maxevents, int, timeout)
22212229
{
2222-
return do_epoll_wait(epfd, events, maxevents, timeout);
2230+
struct timespec64 to;
2231+
2232+
return do_epoll_wait(epfd, events, maxevents,
2233+
ep_timeout_to_timespec(&to, timeout));
22232234
}
22242235

22252236
/*
@@ -2230,6 +2241,7 @@ SYSCALL_DEFINE6(epoll_pwait, int, epfd, struct epoll_event __user *, events,
22302241
int, maxevents, int, timeout, const sigset_t __user *, sigmask,
22312242
size_t, sigsetsize)
22322243
{
2244+
struct timespec64 to;
22332245
int error;
22342246

22352247
/*
@@ -2240,7 +2252,9 @@ SYSCALL_DEFINE6(epoll_pwait, int, epfd, struct epoll_event __user *, events,
22402252
if (error)
22412253
return error;
22422254

2243-
error = do_epoll_wait(epfd, events, maxevents, timeout);
2255+
error = do_epoll_wait(epfd, events, maxevents,
2256+
ep_timeout_to_timespec(&to, timeout));
2257+
22442258
restore_saved_sigmask_unless(error == -EINTR);
22452259

22462260
return error;
@@ -2253,6 +2267,7 @@ COMPAT_SYSCALL_DEFINE6(epoll_pwait, int, epfd,
22532267
const compat_sigset_t __user *, sigmask,
22542268
compat_size_t, sigsetsize)
22552269
{
2270+
struct timespec64 to;
22562271
long err;
22572272

22582273
/*
@@ -2263,7 +2278,9 @@ COMPAT_SYSCALL_DEFINE6(epoll_pwait, int, epfd,
22632278
if (err)
22642279
return err;
22652280

2266-
err = do_epoll_wait(epfd, events, maxevents, timeout);
2281+
err = do_epoll_wait(epfd, events, maxevents,
2282+
ep_timeout_to_timespec(&to, timeout));
2283+
22672284
restore_saved_sigmask_unless(err == -EINTR);
22682285

22692286
return err;

0 commit comments

Comments
 (0)