Skip to content

Commit 37b5e52

Browse files
jbaron-lgtmtorvalds
authored andcommitted
epoll: remove ep_call_nested() from ep_eventpoll_poll()
The use of ep_call_nested() in ep_eventpoll_poll(), which is the .poll routine for an epoll fd, is used to prevent excessively deep epoll nesting, and to prevent circular paths. However, we are already preventing these conditions during EPOLL_CTL_ADD. In terms of too deep epoll chains, we do in fact allow deep nesting of the epoll fds themselves (deeper than EP_MAX_NESTS), however we don't allow more than EP_MAX_NESTS when an epoll file descriptor is actually connected to a wakeup source. Thus, we do not require the use of ep_call_nested(), since ep_eventpoll_poll(), which is called via ep_scan_ready_list() only continues nesting if there are events available. Since ep_call_nested() is implemented using a global lock, applications that make use of nested epoll can see large performance improvements with this change. Davidlohr said: : Improvements are quite obscene actually, such as for the following : epoll_wait() benchmark with 2 level nesting on a 80 core IvyBridge: : : ncpus vanilla dirty delta : 1 2447092 3028315 +23.75% : 4 231265 2986954 +1191.57% : 8 121631 2898796 +2283.27% : 16 59749 2902056 +4757.07% : 32 26837 2326314 +8568.30% : 64 12926 1341281 +10276.61% : : (http://linux-scalability.org/epoll/epoll-test.c) Link: http://lkml.kernel.org/r/1509430214-5599-1-git-send-email-jbaron@akamai.com Signed-off-by: Jason Baron <jbaron@akamai.com> Cc: Davidlohr Bueso <dave@stgolabs.net> Cc: Alexander Viro <viro@zeniv.linux.org.uk> Cc: Salman Qazi <sqazi@google.com> Cc: Hou Tao <houtao1@huawei.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 57a173b commit 37b5e52

1 file changed

Lines changed: 35 additions & 45 deletions

File tree

‎fs/eventpoll.c‎

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,6 @@ static DEFINE_MUTEX(epmutex);
276276
/* Used to check for epoll file descriptor inclusion loops */
277277
static struct nested_calls poll_loop_ncalls;
278278

279-
/* Used to call file's f_op->poll() under the nested calls boundaries */
280-
static struct nested_calls poll_readywalk_ncalls;
281-
282279
/* Slab cache used to allocate "struct epitem" */
283280
static struct kmem_cache *epi_cache __read_mostly;
284281

@@ -867,25 +864,49 @@ static int ep_eventpoll_release(struct inode *inode, struct file *file)
867864
return 0;
868865
}
869866

870-
static inline unsigned int ep_item_poll(struct epitem *epi, poll_table *pt)
867+
static int ep_read_events_proc(struct eventpoll *ep, struct list_head *head,
868+
void *priv);
869+
static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
870+
poll_table *pt);
871+
872+
/*
873+
* Differs from ep_eventpoll_poll() in that internal callers already have
874+
* the ep->mtx so we need to start from depth=1, such that mutex_lock_nested()
875+
* is correctly annotated.
876+
*/
877+
static unsigned int ep_item_poll(struct epitem *epi, poll_table *pt, int depth)
871878
{
879+
struct eventpoll *ep;
880+
bool locked;
881+
872882
pt->_key = epi->event.events;
883+
if (!is_file_epoll(epi->ffd.file))
884+
return epi->ffd.file->f_op->poll(epi->ffd.file, pt) &
885+
epi->event.events;
873886

874-
return epi->ffd.file->f_op->poll(epi->ffd.file, pt) & epi->event.events;
887+
ep = epi->ffd.file->private_data;
888+
poll_wait(epi->ffd.file, &ep->poll_wait, pt);
889+
locked = pt && (pt->_qproc == ep_ptable_queue_proc);
890+
891+
return ep_scan_ready_list(epi->ffd.file->private_data,
892+
ep_read_events_proc, &depth, depth,
893+
locked) & epi->event.events;
875894
}
876895

877896
static int ep_read_events_proc(struct eventpoll *ep, struct list_head *head,
878897
void *priv)
879898
{
880899
struct epitem *epi, *tmp;
881900
poll_table pt;
901+
int depth = *(int *)priv;
882902

883903
init_poll_funcptr(&pt, NULL);
904+
depth++;
884905

885906
list_for_each_entry_safe(epi, tmp, head, rdllink) {
886-
if (ep_item_poll(epi, &pt))
907+
if (ep_item_poll(epi, &pt, depth)) {
887908
return POLLIN | POLLRDNORM;
888-
else {
909+
} else {
889910
/*
890911
* Item has been dropped into the ready list by the poll
891912
* callback, but it's not actually ready, as far as
@@ -899,48 +920,20 @@ static int ep_read_events_proc(struct eventpoll *ep, struct list_head *head,
899920
return 0;
900921
}
901922

902-
static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
903-
poll_table *pt);
904-
905-
struct readyevents_arg {
906-
struct eventpoll *ep;
907-
bool locked;
908-
};
909-
910-
static int ep_poll_readyevents_proc(void *priv, void *cookie, int call_nests)
911-
{
912-
struct readyevents_arg *arg = priv;
913-
914-
return ep_scan_ready_list(arg->ep, ep_read_events_proc, NULL,
915-
call_nests + 1, arg->locked);
916-
}
917-
918923
static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait)
919924
{
920-
int pollflags;
921925
struct eventpoll *ep = file->private_data;
922-
struct readyevents_arg arg;
923-
924-
/*
925-
* During ep_insert() we already hold the ep->mtx for the tfile.
926-
* Prevent re-aquisition.
927-
*/
928-
arg.locked = wait && (wait->_qproc == ep_ptable_queue_proc);
929-
arg.ep = ep;
926+
int depth = 0;
930927

931928
/* Insert inside our poll wait queue */
932929
poll_wait(file, &ep->poll_wait, wait);
933930

934931
/*
935932
* Proceed to find out if wanted events are really available inside
936-
* the ready list. This need to be done under ep_call_nested()
937-
* supervision, since the call to f_op->poll() done on listed files
938-
* could re-enter here.
933+
* the ready list.
939934
*/
940-
pollflags = ep_call_nested(&poll_readywalk_ncalls, EP_MAX_NESTS,
941-
ep_poll_readyevents_proc, &arg, ep, current);
942-
943-
return pollflags != -1 ? pollflags : 0;
935+
return ep_scan_ready_list(ep, ep_read_events_proc,
936+
&depth, depth, false);
944937
}
945938

946939
#ifdef CONFIG_PROC_FS
@@ -1459,7 +1452,7 @@ static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
14591452
* this operation completes, the poll callback can start hitting
14601453
* the new item.
14611454
*/
1462-
revents = ep_item_poll(epi, &epq.pt);
1455+
revents = ep_item_poll(epi, &epq.pt, 1);
14631456

14641457
/*
14651458
* We have to check if something went wrong during the poll wait queue
@@ -1593,7 +1586,7 @@ static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_even
15931586
* Get current event bits. We can safely use the file* here because
15941587
* its usage count has been increased by the caller of this function.
15951588
*/
1596-
revents = ep_item_poll(epi, &pt);
1589+
revents = ep_item_poll(epi, &pt, 1);
15971590

15981591
/*
15991592
* If the item is "hot" and it is not registered inside the ready
@@ -1661,7 +1654,7 @@ static int ep_send_events_proc(struct eventpoll *ep, struct list_head *head,
16611654

16621655
list_del_init(&epi->rdllink);
16631656

1664-
revents = ep_item_poll(epi, &pt);
1657+
revents = ep_item_poll(epi, &pt, 1);
16651658

16661659
/*
16671660
* If the event mask intersect the caller-requested one,
@@ -2307,9 +2300,6 @@ static int __init eventpoll_init(void)
23072300
ep_nested_calls_init(&poll_safewake_ncalls);
23082301
#endif
23092302

2310-
/* Initialize the structure used to perform file's f_op->poll() calls */
2311-
ep_nested_calls_init(&poll_readywalk_ncalls);
2312-
23132303
/*
23142304
* We can have many thousands of epitems, so prevent this from
23152305
* using an extra cache line on 64-bit (and smaller) CPUs

0 commit comments

Comments
 (0)