The patch titled Subject: epoll: simplify and optimize busy loop logic has been added to the -mm tree. Its filename is epoll-simplify-and-optimize-busy-loop-logic.patch This patch should soon appear at https://ozlabs.org/~akpm/mmots/broken-out/epoll-simplify-and-optimize-busy-loop-logic.patch and later at https://ozlabs.org/~akpm/mmotm/broken-out/epoll-simplify-and-optimize-busy-loop-logic.patch Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next and is updated there every 3-4 working days ------------------------------------------------------ From: Soheil Hassas Yeganeh <soheil@xxxxxxxxxx> Subject: epoll: simplify and optimize busy loop logic ep_events_available() is called multiple times around the busy loop logic, even though the logic is generally not used. ep_reset_busy_poll_napi_id() is similarly always called, even when busy loop is not used. Eliminate ep_reset_busy_poll_napi_id() and inline it inside ep_busy_loop(). Make ep_busy_loop() return whether there are any events available after the busy loop. This will eliminate unnecessary loads and branches, and simplifies the loop. Link: https://lkml.kernel.org/r/20201106231635.3528496-6-soheil.kdev@xxxxxxxxx Signed-off-by: Soheil Hassas Yeganeh <soheil@xxxxxxxxxx> Reviewed-by: Eric Dumazet <edumazet@xxxxxxxxxx> Reviewed-by: Willem de Bruijn <willemb@xxxxxxxxxx> Reviewed-by: Khazhismel Kumykov <khazhy@xxxxxxxxxx> Cc: Guantao Liu <guantaol@xxxxxxxxxx> Cc: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- fs/eventpoll.c | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) --- a/fs/eventpoll.c~epoll-simplify-and-optimize-busy-loop-logic +++ a/fs/eventpoll.c @@ -391,19 +391,26 @@ static bool ep_busy_loop_end(void *p, un * busy loop will return if need_resched or ep_events_available. * * we must do our busy polling with irqs enabled + * + * Returns whether new events are available after a successful busy loop. */ -static void ep_busy_loop(struct eventpoll *ep, int nonblock) +static bool ep_busy_loop(struct eventpoll *ep, int nonblock) { unsigned int napi_id = READ_ONCE(ep->napi_id); - if ((napi_id >= MIN_NAPI_ID) && net_busy_loop_on()) + if ((napi_id >= MIN_NAPI_ID) && net_busy_loop_on()) { napi_busy_loop(napi_id, nonblock ? NULL : ep_busy_loop_end, ep); -} - -static inline void ep_reset_busy_poll_napi_id(struct eventpoll *ep) -{ - if (ep->napi_id) + if (ep_events_available(ep)) + return true; + /* + * Busy poll timed out. Drop NAPI ID for now, we can add + * it back in when we have moved a socket with a valid NAPI + * ID onto the ready list. + */ ep->napi_id = 0; + return false; + } + return false; } /* @@ -444,12 +451,9 @@ static inline void ep_set_busy_poll_napi #else -static inline void ep_busy_loop(struct eventpoll *ep, int nonblock) -{ -} - -static inline void ep_reset_busy_poll_napi_id(struct eventpoll *ep) +static inline bool ep_busy_loop(struct eventpoll *ep, int nonblock) { + return false; } static inline void ep_set_busy_poll_napi_id(struct epitem *epi) @@ -1857,21 +1861,13 @@ static int ep_poll(struct eventpoll *ep, } fetch_events: - - if (!ep_events_available(ep)) - ep_busy_loop(ep, timed_out); - eavail = ep_events_available(ep); + if (!eavail) + eavail = ep_busy_loop(ep, timed_out); + if (eavail) goto send_events; - /* - * Busy poll timed out. Drop NAPI ID for now, we can add - * it back in when we have moved a socket with a valid NAPI - * ID onto the ready list. - */ - ep_reset_busy_poll_napi_id(ep); - do { if (signal_pending(current)) return -EINTR; _ Patches currently in -mm which might be from soheil@xxxxxxxxxx are epoll-check-for-events-when-removing-a-timed-out-thread-from-the-wait-queue.patch epoll-simplify-signal-handling.patch epoll-pull-fatal-signal-checks-into-ep_send_events.patch epoll-move-eavail-next-to-the-list_empty_careful-check.patch epoll-simplify-and-optimize-busy-loop-logic.patch epoll-pull-all-code-between-fetch_events-and-send_event-into-the-loop.patch epoll-replace-gotos-with-a-proper-loop.patch epoll-eliminate-unnecessary-lock-for-zero-timeout.patch