Skip to content

Commit 296e236

Browse files
davideltorvalds
authored andcommitted
epoll: fix epoll's own poll (update)
Signed-off-by: Davide Libenzi <davidel@xmailserver.org> Cc: Pavel Pisa <pisa@cmp.felk.cvut.cz> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 5071f97 commit 296e236

File tree

1 file changed

+57
-53
lines changed

1 file changed

+57
-53
lines changed

fs/eventpoll.c

+57-53
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,7 @@ static int ep_scan_ready_list(struct eventpoll *ep,
454454
int error, pwake = 0;
455455
unsigned long flags;
456456
struct epitem *epi, *nepi;
457-
struct list_head txlist;
458-
459-
INIT_LIST_HEAD(&txlist);
457+
LIST_HEAD(txlist);
460458

461459
/*
462460
* We need to lock this because we could be hit by
@@ -473,8 +471,7 @@ static int ep_scan_ready_list(struct eventpoll *ep,
473471
* in a lockless way.
474472
*/
475473
spin_lock_irqsave(&ep->lock, flags);
476-
list_splice(&ep->rdllist, &txlist);
477-
INIT_LIST_HEAD(&ep->rdllist);
474+
list_splice_init(&ep->rdllist, &txlist);
478475
ep->ovflist = NULL;
479476
spin_unlock_irqrestore(&ep->lock, flags);
480477

@@ -514,8 +511,8 @@ static int ep_scan_ready_list(struct eventpoll *ep,
514511

515512
if (!list_empty(&ep->rdllist)) {
516513
/*
517-
* Wake up (if active) both the eventpoll wait list and the ->poll()
518-
* wait list (delayed after we release the lock).
514+
* Wake up (if active) both the eventpoll wait list and
515+
* the ->poll() wait list (delayed after we release the lock).
519516
*/
520517
if (waitqueue_active(&ep->wq))
521518
wake_up_locked(&ep->wq);
@@ -632,21 +629,23 @@ static int ep_eventpoll_release(struct inode *inode, struct file *file)
632629
return 0;
633630
}
634631

635-
static int ep_read_events_proc(struct eventpoll *ep, struct list_head *head, void *priv)
632+
static int ep_read_events_proc(struct eventpoll *ep, struct list_head *head,
633+
void *priv)
636634
{
637635
struct epitem *epi, *tmp;
638636

639637
list_for_each_entry_safe(epi, tmp, head, rdllink) {
640638
if (epi->ffd.file->f_op->poll(epi->ffd.file, NULL) &
641639
epi->event.events)
642640
return POLLIN | POLLRDNORM;
643-
else
641+
else {
644642
/*
645643
* Item has been dropped into the ready list by the poll
646644
* callback, but it's not actually ready, as far as
647645
* caller requested events goes. We can remove it here.
648646
*/
649647
list_del_init(&epi->rdllink);
648+
}
650649
}
651650

652651
return 0;
@@ -674,7 +673,7 @@ static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait)
674673
pollflags = ep_call_nested(&poll_readywalk_ncalls, EP_MAX_NESTS,
675674
ep_poll_readyevents_proc, ep, ep);
676675

677-
return pollflags != -1 ? pollflags: 0;
676+
return pollflags != -1 ? pollflags : 0;
678677
}
679678

680679
/* File callbacks that implement the eventpoll file behaviour */
@@ -872,9 +871,10 @@ static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
872871
add_wait_queue(whead, &pwq->wait);
873872
list_add_tail(&pwq->llink, &epi->pwqlist);
874873
epi->nwait++;
875-
} else
874+
} else {
876875
/* We have to signal that an error occurred */
877876
epi->nwait = -1;
877+
}
878878
}
879879

880880
static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi)
@@ -1055,62 +1055,65 @@ static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_even
10551055
return 0;
10561056
}
10571057

1058-
static int ep_send_events_proc(struct eventpoll *ep, struct list_head *head, void *priv)
1058+
static int ep_send_events_proc(struct eventpoll *ep, struct list_head *head,
1059+
void *priv)
10591060
{
10601061
struct ep_send_events_data *esed = priv;
10611062
int eventcnt;
1062-
unsigned int revents;
1063+
unsigned int revents;
10631064
struct epitem *epi;
10641065
struct epoll_event __user *uevent;
10651066

1066-
/*
1067+
/*
10671068
* We can loop without lock because we are passed a task private list.
10681069
* Items cannot vanish during the loop because ep_scan_ready_list() is
10691070
* holding "mtx" during this call.
1070-
*/
1071+
*/
10711072
for (eventcnt = 0, uevent = esed->events;
10721073
!list_empty(head) && eventcnt < esed->maxevents;) {
10731074
epi = list_first_entry(head, struct epitem, rdllink);
10741075

10751076
list_del_init(&epi->rdllink);
10761077

1077-
revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL) &
1078-
epi->event.events;
1078+
revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL) &
1079+
epi->event.events;
10791080

1080-
/*
1081+
/*
10811082
* If the event mask intersect the caller-requested one,
10821083
* deliver the event to userspace. Again, ep_scan_ready_list()
10831084
* is holding "mtx", so no operations coming from userspace
10841085
* can change the item.
1085-
*/
1086-
if (revents) {
1086+
*/
1087+
if (revents) {
10871088
if (__put_user(revents, &uevent->events) ||
10881089
__put_user(epi->event.data, &uevent->data))
1089-
return eventcnt ? eventcnt: -EFAULT;
1090-
eventcnt++;
1090+
return eventcnt ? eventcnt : -EFAULT;
1091+
eventcnt++;
10911092
uevent++;
1092-
if (epi->event.events & EPOLLONESHOT)
1093-
epi->event.events &= EP_PRIVATE_BITS;
1094-
else if (!(epi->event.events & EPOLLET))
1095-
/*
1096-
* If this file has been added with Level Trigger
1097-
* mode, we need to insert back inside the ready
1098-
* list, so that the next call to epoll_wait()
1099-
* will check again the events availability.
1100-
* At this point, noone can insert into ep->rdllist
1101-
* besides us. The epoll_ctl() callers are locked
1102-
* out by ep_scan_ready_list() holding "mtx" and
1103-
* the poll callback will queue them in ep->ovflist.
1104-
*/
1105-
list_add_tail(&epi->rdllink, &ep->rdllist);
1106-
}
1107-
}
1093+
if (epi->event.events & EPOLLONESHOT)
1094+
epi->event.events &= EP_PRIVATE_BITS;
1095+
else if (!(epi->event.events & EPOLLET)) {
1096+
/*
1097+
* If this file has been added with Level
1098+
* Trigger mode, we need to insert back inside
1099+
* the ready list, so that the next call to
1100+
* epoll_wait() will check again the events
1101+
* availability. At this point, noone can insert
1102+
* into ep->rdllist besides us. The epoll_ctl()
1103+
* callers are locked out by
1104+
* ep_scan_ready_list() holding "mtx" and the
1105+
* poll callback will queue them in ep->ovflist.
1106+
*/
1107+
list_add_tail(&epi->rdllink, &ep->rdllist);
1108+
}
1109+
}
1110+
}
11081111

11091112
return eventcnt;
11101113
}
11111114

1112-
static int ep_send_events(struct eventpoll *ep, struct epoll_event __user *events,
1113-
int maxevents)
1115+
static int ep_send_events(struct eventpoll *ep,
1116+
struct epoll_event __user *events, int maxevents)
11141117
{
11151118
struct ep_send_events_data esed;
11161119

@@ -1194,40 +1197,41 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
11941197
*/
11951198
SYSCALL_DEFINE1(epoll_create1, int, flags)
11961199
{
1197-
int error;
1198-
struct eventpoll *ep = NULL;
1200+
int error, fd = -1;
1201+
struct eventpoll *ep;
11991202

12001203
/* Check the EPOLL_* constant for consistency. */
12011204
BUILD_BUG_ON(EPOLL_CLOEXEC != O_CLOEXEC);
12021205

1206+
if (flags & ~EPOLL_CLOEXEC)
1207+
return -EINVAL;
1208+
12031209
DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d)\n",
12041210
current, flags));
12051211

1206-
error = -EINVAL;
1207-
if (flags & ~EPOLL_CLOEXEC)
1208-
goto error_return;
1209-
12101212
/*
1211-
* Create the internal data structure ("struct eventpoll").
1213+
* Create the internal data structure ( "struct eventpoll" ).
12121214
*/
12131215
error = ep_alloc(&ep);
1214-
if (error < 0)
1216+
if (error < 0) {
1217+
fd = error;
12151218
goto error_return;
1219+
}
12161220

12171221
/*
12181222
* Creates all the items needed to setup an eventpoll file. That is,
12191223
* a file structure and a free file descriptor.
12201224
*/
1221-
error = anon_inode_getfd("[eventpoll]", &eventpoll_fops, ep,
1222-
flags & O_CLOEXEC);
1223-
if (error < 0)
1225+
fd = anon_inode_getfd("[eventpoll]", &eventpoll_fops, ep,
1226+
flags & O_CLOEXEC);
1227+
if (fd < 0)
12241228
ep_free(ep);
12251229

12261230
error_return:
12271231
DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n",
1228-
current, flags, error));
1232+
current, flags, fd));
12291233

1230-
return error;
1234+
return fd;
12311235
}
12321236

12331237
SYSCALL_DEFINE1(epoll_create, int, size)

0 commit comments

Comments
 (0)