Skip to content

Commit 1493c47

Browse files
soheilhytorvalds
authored andcommitted
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@gmail.com Signed-off-by: Soheil Hassas Yeganeh <soheil@google.com> Reviewed-by: Eric Dumazet <edumazet@google.com> Reviewed-by: Willem de Bruijn <willemb@google.com> Reviewed-by: Khazhismel Kumykov <khazhy@google.com> Cc: Guantao Liu <guantaol@google.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent e411596 commit 1493c47

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

fs/eventpoll.c

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -389,19 +389,24 @@ static bool ep_busy_loop_end(void *p, unsigned long start_time)
389389
*
390390
* we must do our busy polling with irqs enabled
391391
*/
392-
static void ep_busy_loop(struct eventpoll *ep, int nonblock)
392+
static bool ep_busy_loop(struct eventpoll *ep, int nonblock)
393393
{
394394
unsigned int napi_id = READ_ONCE(ep->napi_id);
395395

396-
if ((napi_id >= MIN_NAPI_ID) && net_busy_loop_on())
396+
if ((napi_id >= MIN_NAPI_ID) && net_busy_loop_on()) {
397397
napi_busy_loop(napi_id, nonblock ? NULL : ep_busy_loop_end, ep, false,
398398
BUSY_POLL_BUDGET);
399-
}
400-
401-
static inline void ep_reset_busy_poll_napi_id(struct eventpoll *ep)
402-
{
403-
if (ep->napi_id)
399+
if (ep_events_available(ep))
400+
return true;
401+
/*
402+
* Busy poll timed out. Drop NAPI ID for now, we can add
403+
* it back in when we have moved a socket with a valid NAPI
404+
* ID onto the ready list.
405+
*/
404406
ep->napi_id = 0;
407+
return false;
408+
}
409+
return false;
405410
}
406411

407412
/*
@@ -441,12 +446,9 @@ static inline void ep_set_busy_poll_napi_id(struct epitem *epi)
441446

442447
#else
443448

444-
static inline void ep_busy_loop(struct eventpoll *ep, int nonblock)
445-
{
446-
}
447-
448-
static inline void ep_reset_busy_poll_napi_id(struct eventpoll *ep)
449+
static inline bool ep_busy_loop(struct eventpoll *ep, int nonblock)
449450
{
451+
return false;
450452
}
451453

452454
static inline void ep_set_busy_poll_napi_id(struct epitem *epi)
@@ -1772,21 +1774,13 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
17721774
}
17731775

17741776
fetch_events:
1775-
1776-
if (!ep_events_available(ep))
1777-
ep_busy_loop(ep, timed_out);
1778-
17791777
eavail = ep_events_available(ep);
1778+
if (!eavail)
1779+
eavail = ep_busy_loop(ep, timed_out);
1780+
17801781
if (eavail)
17811782
goto send_events;
17821783

1783-
/*
1784-
* Busy poll timed out. Drop NAPI ID for now, we can add
1785-
* it back in when we have moved a socket with a valid NAPI
1786-
* ID onto the ready list.
1787-
*/
1788-
ep_reset_busy_poll_napi_id(ep);
1789-
17901784
do {
17911785
if (signal_pending(current))
17921786
return -EINTR;

0 commit comments

Comments
 (0)