epoll locks changes and cleanups

Changes the rwlock to a spinlock, and drops the use-count variable.
Operations are always bound by the mutex now, so the use-count is no more
needed.  For the same reason, the rwlock can become a simple spinlock.

Signed-off-by: Davide Libenzi <davidel@xmailserver.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
Davide Libenzi 2007-05-15 01:40:47 -07:00 committed by Linus Torvalds
parent d47de16c72
commit c7ea763025

View File

@ -1,6 +1,6 @@
/* /*
* fs/eventpoll.c ( Efficent event polling implementation ) * fs/eventpoll.c (Efficent event polling implementation)
* Copyright (C) 2001,...,2006 Davide Libenzi * Copyright (C) 2001,...,2007 Davide Libenzi
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -44,8 +44,8 @@
* There are three level of locking required by epoll : * There are three level of locking required by epoll :
* *
* 1) epmutex (mutex) * 1) epmutex (mutex)
* 2) ep->mtx (mutes) * 2) ep->mtx (mutex)
* 3) ep->lock (rw_lock) * 3) ep->lock (spinlock)
* *
* The acquire order is the one listed above, from 1 to 3. * The acquire order is the one listed above, from 1 to 3.
* We need a spinlock (ep->lock) because we manipulate objects * We need a spinlock (ep->lock) because we manipulate objects
@ -140,6 +140,12 @@ struct epitem {
/* List header used to link this structure to the eventpoll ready list */ /* List header used to link this structure to the eventpoll ready list */
struct list_head rdllink; struct list_head rdllink;
/*
* Works together "struct eventpoll"->ovflist in keeping the
* single linked chain of items.
*/
struct epitem *next;
/* The file descriptor information this item refers to */ /* The file descriptor information this item refers to */
struct epoll_filefd ffd; struct epoll_filefd ffd;
@ -152,23 +158,11 @@ struct epitem {
/* The "container" of this item */ /* The "container" of this item */
struct eventpoll *ep; struct eventpoll *ep;
/* The structure that describe the interested events and the source fd */
struct epoll_event event;
/*
* Used to keep track of the usage count of the structure. This avoids
* that the structure will desappear from underneath our processing.
*/
atomic_t usecnt;
/* List header used to link this item to the "struct file" items list */ /* List header used to link this item to the "struct file" items list */
struct list_head fllink; struct list_head fllink;
/* /* The structure that describe the interested events and the source fd */
* Works together "struct eventpoll"->ovflist in keeping the struct epoll_event event;
* single linked chain of items.
*/
struct epitem *next;
}; };
/* /*
@ -178,7 +172,7 @@ struct epitem {
*/ */
struct eventpoll { struct eventpoll {
/* Protect the this structure access */ /* Protect the this structure access */
rwlock_t lock; spinlock_t lock;
/* /*
* This mutex is used to ensure that files are not removed * This mutex is used to ensure that files are not removed
@ -393,79 +387,12 @@ static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi)
} }
} }
/*
* Unlink the "struct epitem" from all places it might have been hooked up.
* This function must be called with write IRQ lock on "ep->lock".
*/
static int ep_unlink(struct eventpoll *ep, struct epitem *epi)
{
int error;
/*
* It can happen that this one is called for an item already unlinked.
* The check protect us from doing a double unlink ( crash ).
*/
error = -ENOENT;
if (!ep_rb_linked(&epi->rbn))
goto error_return;
/*
* Clear the event mask for the unlinked item. This will avoid item
* notifications to be sent after the unlink operation from inside
* the kernel->userspace event transfer loop.
*/
epi->event.events = 0;
/*
* At this point is safe to do the job, unlink the item from our rb-tree.
* This operation togheter with the above check closes the door to
* double unlinks.
*/
ep_rb_erase(&epi->rbn, &ep->rbr);
/*
* If the item we are going to remove is inside the ready file descriptors
* we want to remove it from this list to avoid stale events.
*/
if (ep_is_linked(&epi->rdllink))
list_del_init(&epi->rdllink);
error = 0;
error_return:
DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_unlink(%p, %p) = %d\n",
current, ep, epi->ffd.file, error));
return error;
}
/*
* Increment the usage count of the "struct epitem" making it sure
* that the user will have a valid pointer to reference.
*/
static void ep_use_epitem(struct epitem *epi)
{
atomic_inc(&epi->usecnt);
}
/*
* Decrement ( release ) the usage count by signaling that the user
* has finished using the structure. It might lead to freeing the
* structure itself if the count goes to zero.
*/
static void ep_release_epitem(struct epitem *epi)
{
if (atomic_dec_and_test(&epi->usecnt))
kmem_cache_free(epi_cache, epi);
}
/* /*
* Removes a "struct epitem" from the eventpoll RB tree and deallocates * Removes a "struct epitem" from the eventpoll RB tree and deallocates
* all the associated resources. * all the associated resources. Must be called with "mtx" held.
*/ */
static int ep_remove(struct eventpoll *ep, struct epitem *epi) static int ep_remove(struct eventpoll *ep, struct epitem *epi)
{ {
int error;
unsigned long flags; unsigned long flags;
struct file *file = epi->ffd.file; struct file *file = epi->ffd.file;
@ -485,26 +412,21 @@ static int ep_remove(struct eventpoll *ep, struct epitem *epi)
list_del_init(&epi->fllink); list_del_init(&epi->fllink);
spin_unlock(&file->f_ep_lock); spin_unlock(&file->f_ep_lock);
/* We need to acquire the write IRQ lock before calling ep_unlink() */ if (ep_rb_linked(&epi->rbn))
write_lock_irqsave(&ep->lock, flags); ep_rb_erase(&epi->rbn, &ep->rbr);
/* Really unlink the item from the RB tree */ spin_lock_irqsave(&ep->lock, flags);
error = ep_unlink(ep, epi); if (ep_is_linked(&epi->rdllink))
list_del_init(&epi->rdllink);
write_unlock_irqrestore(&ep->lock, flags); spin_unlock_irqrestore(&ep->lock, flags);
if (error)
goto error_return;
/* At this point it is safe to free the eventpoll item */ /* At this point it is safe to free the eventpoll item */
ep_release_epitem(epi); kmem_cache_free(epi_cache, epi);
error = 0; DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_remove(%p, %p)\n",
error_return: current, ep, file));
DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_remove(%p, %p) = %d\n",
current, ep, file, error));
return error; return 0;
} }
static void ep_free(struct eventpoll *ep) static void ep_free(struct eventpoll *ep)
@ -574,10 +496,10 @@ static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait)
poll_wait(file, &ep->poll_wait, wait); poll_wait(file, &ep->poll_wait, wait);
/* Check our condition */ /* Check our condition */
read_lock_irqsave(&ep->lock, flags); spin_lock_irqsave(&ep->lock, flags);
if (!list_empty(&ep->rdllist)) if (!list_empty(&ep->rdllist))
pollflags = POLLIN | POLLRDNORM; pollflags = POLLIN | POLLRDNORM;
read_unlock_irqrestore(&ep->lock, flags); spin_unlock_irqrestore(&ep->lock, flags);
return pollflags; return pollflags;
} }
@ -636,7 +558,7 @@ static int ep_alloc(struct eventpoll **pep)
if (!ep) if (!ep)
return -ENOMEM; return -ENOMEM;
rwlock_init(&ep->lock); spin_lock_init(&ep->lock);
mutex_init(&ep->mtx); mutex_init(&ep->mtx);
init_waitqueue_head(&ep->wq); init_waitqueue_head(&ep->wq);
init_waitqueue_head(&ep->poll_wait); init_waitqueue_head(&ep->poll_wait);
@ -652,20 +574,18 @@ static int ep_alloc(struct eventpoll **pep)
} }
/* /*
* Search the file inside the eventpoll tree. It add usage count to * Search the file inside the eventpoll tree. The RB tree operations
* the returned item, so the caller must call ep_release_epitem() * are protected by the "mtx" mutex, and ep_find() must be called with
* after finished using the "struct epitem". * "mtx" held.
*/ */
static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd) static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd)
{ {
int kcmp; int kcmp;
unsigned long flags;
struct rb_node *rbp; struct rb_node *rbp;
struct epitem *epi, *epir = NULL; struct epitem *epi, *epir = NULL;
struct epoll_filefd ffd; struct epoll_filefd ffd;
ep_set_ffd(&ffd, file, fd); ep_set_ffd(&ffd, file, fd);
read_lock_irqsave(&ep->lock, flags);
for (rbp = ep->rbr.rb_node; rbp; ) { for (rbp = ep->rbr.rb_node; rbp; ) {
epi = rb_entry(rbp, struct epitem, rbn); epi = rb_entry(rbp, struct epitem, rbn);
kcmp = ep_cmp_ffd(&ffd, &epi->ffd); kcmp = ep_cmp_ffd(&ffd, &epi->ffd);
@ -674,12 +594,10 @@ static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd)
else if (kcmp < 0) else if (kcmp < 0)
rbp = rbp->rb_left; rbp = rbp->rb_left;
else { else {
ep_use_epitem(epi);
epir = epi; epir = epi;
break; break;
} }
} }
read_unlock_irqrestore(&ep->lock, flags);
DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_find(%p) -> %p\n", DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_find(%p) -> %p\n",
current, file, epir)); current, file, epir));
@ -702,7 +620,7 @@ static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *k
DNPRINTK(3, (KERN_INFO "[%p] eventpoll: poll_callback(%p) epi=%p ep=%p\n", DNPRINTK(3, (KERN_INFO "[%p] eventpoll: poll_callback(%p) epi=%p ep=%p\n",
current, epi->ffd.file, epi, ep)); current, epi->ffd.file, epi, ep));
write_lock_irqsave(&ep->lock, flags); spin_lock_irqsave(&ep->lock, flags);
/* /*
* If the event mask does not contain any poll(2) event, we consider the * If the event mask does not contain any poll(2) event, we consider the
@ -745,7 +663,7 @@ is_linked:
pwake++; pwake++;
out_unlock: out_unlock:
write_unlock_irqrestore(&ep->lock, flags); spin_unlock_irqrestore(&ep->lock, flags);
/* We have to call this outside the lock */ /* We have to call this outside the lock */
if (pwake) if (pwake)
@ -796,6 +714,9 @@ static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi)
rb_insert_color(&epi->rbn, &ep->rbr); rb_insert_color(&epi->rbn, &ep->rbr);
} }
/*
* Must be called with "mtx" held.
*/
static int ep_insert(struct eventpoll *ep, struct epoll_event *event, static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
struct file *tfile, int fd) struct file *tfile, int fd)
{ {
@ -816,7 +737,6 @@ static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
epi->ep = ep; epi->ep = ep;
ep_set_ffd(&epi->ffd, tfile, fd); ep_set_ffd(&epi->ffd, tfile, fd);
epi->event = *event; epi->event = *event;
atomic_set(&epi->usecnt, 1);
epi->nwait = 0; epi->nwait = 0;
epi->next = EP_UNACTIVE_PTR; epi->next = EP_UNACTIVE_PTR;
@ -827,7 +747,9 @@ static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
/* /*
* Attach the item to the poll hooks and get current event bits. * Attach the item to the poll hooks and get current event bits.
* We can safely use the file* here because its usage count has * We can safely use the file* here because its usage count has
* been increased by the caller of this function. * been increased by the caller of this function. Note that after
* this operation completes, the poll callback can start hitting
* the new item.
*/ */
revents = tfile->f_op->poll(tfile, &epq.pt); revents = tfile->f_op->poll(tfile, &epq.pt);
@ -844,12 +766,15 @@ static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
list_add_tail(&epi->fllink, &tfile->f_ep_links); list_add_tail(&epi->fllink, &tfile->f_ep_links);
spin_unlock(&tfile->f_ep_lock); spin_unlock(&tfile->f_ep_lock);
/* We have to drop the new item inside our item list to keep track of it */ /*
write_lock_irqsave(&ep->lock, flags); * Add the current item to the RB tree. All RB tree operations are
* protected by "mtx", and ep_insert() is called with "mtx" held.
/* Add the current item to the rb-tree */ */
ep_rbtree_insert(ep, epi); ep_rbtree_insert(ep, epi);
/* We have to drop the new item inside our item list to keep track of it */
spin_lock_irqsave(&ep->lock, flags);
/* If the file is already "ready" we drop it inside the ready list */ /* If the file is already "ready" we drop it inside the ready list */
if ((revents & event->events) && !ep_is_linked(&epi->rdllink)) { if ((revents & event->events) && !ep_is_linked(&epi->rdllink)) {
list_add_tail(&epi->rdllink, &ep->rdllist); list_add_tail(&epi->rdllink, &ep->rdllist);
@ -861,7 +786,7 @@ static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
pwake++; pwake++;
} }
write_unlock_irqrestore(&ep->lock, flags); spin_unlock_irqrestore(&ep->lock, flags);
/* We have to call this outside the lock */ /* We have to call this outside the lock */
if (pwake) if (pwake)
@ -879,10 +804,10 @@ error_unregister:
* We need to do this because an event could have been arrived on some * We need to do this because an event could have been arrived on some
* allocated wait queue. * allocated wait queue.
*/ */
write_lock_irqsave(&ep->lock, flags); spin_lock_irqsave(&ep->lock, flags);
if (ep_is_linked(&epi->rdllink)) if (ep_is_linked(&epi->rdllink))
list_del_init(&epi->rdllink); list_del_init(&epi->rdllink);
write_unlock_irqrestore(&ep->lock, flags); spin_unlock_irqrestore(&ep->lock, flags);
kmem_cache_free(epi_cache, epi); kmem_cache_free(epi_cache, epi);
error_return: error_return:
@ -891,7 +816,7 @@ error_return:
/* /*
* Modify the interest event mask by dropping an event if the new mask * Modify the interest event mask by dropping an event if the new mask
* has a match in the current file status. * has a match in the current file status. Must be called with "mtx" held.
*/ */
static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_event *event) static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_event *event)
{ {
@ -913,36 +838,29 @@ static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_even
*/ */
revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL); revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL);
write_lock_irqsave(&ep->lock, flags); spin_lock_irqsave(&ep->lock, flags);
/* Copy the data member from inside the lock */ /* Copy the data member from inside the lock */
epi->event.data = event->data; epi->event.data = event->data;
/* /*
* If the item is not linked to the RB tree it means that it's on its * If the item is "hot" and it is not registered inside the ready
* way toward the removal. Do nothing in this case. * list, push it inside. If the item is not "hot" and it is currently
* registered inside the ready list, unlink it.
*/ */
if (ep_rb_linked(&epi->rbn)) { if (revents & event->events) {
/* if (!ep_is_linked(&epi->rdllink)) {
* If the item is "hot" and it is not registered inside the ready list_add_tail(&epi->rdllink, &ep->rdllist);
* list, push it inside. If the item is not "hot" and it is currently
* registered inside the ready list, unlink it.
*/
if (revents & event->events) {
if (!ep_is_linked(&epi->rdllink)) {
list_add_tail(&epi->rdllink, &ep->rdllist);
/* Notify waiting tasks that events are available */ /* Notify waiting tasks that events are available */
if (waitqueue_active(&ep->wq)) if (waitqueue_active(&ep->wq))
__wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE | __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE |
TASK_INTERRUPTIBLE); TASK_INTERRUPTIBLE);
if (waitqueue_active(&ep->poll_wait)) if (waitqueue_active(&ep->poll_wait))
pwake++; pwake++;
}
} }
} }
spin_unlock_irqrestore(&ep->lock, flags);
write_unlock_irqrestore(&ep->lock, flags);
/* We have to call this outside the lock */ /* We have to call this outside the lock */
if (pwake) if (pwake)
@ -975,11 +893,11 @@ static int ep_send_events(struct eventpoll *ep, struct epoll_event __user *event
* have the poll callback to queue directly on ep->rdllist, * have the poll callback to queue directly on ep->rdllist,
* because we are doing it in the loop below, in a lockless way. * because we are doing it in the loop below, in a lockless way.
*/ */
write_lock_irqsave(&ep->lock, flags); spin_lock_irqsave(&ep->lock, flags);
list_splice(&ep->rdllist, &txlist); list_splice(&ep->rdllist, &txlist);
INIT_LIST_HEAD(&ep->rdllist); INIT_LIST_HEAD(&ep->rdllist);
ep->ovflist = NULL; ep->ovflist = NULL;
write_unlock_irqrestore(&ep->lock, flags); spin_unlock_irqrestore(&ep->lock, flags);
/* /*
* We can loop without lock because this is a task private list. * We can loop without lock because this is a task private list.
@ -1028,7 +946,7 @@ static int ep_send_events(struct eventpoll *ep, struct epoll_event __user *event
errxit: errxit:
write_lock_irqsave(&ep->lock, flags); spin_lock_irqsave(&ep->lock, flags);
/* /*
* During the time we spent in the loop above, some other events * During the time we spent in the loop above, some other events
* might have been queued by the poll callback. We re-insert them * might have been queued by the poll callback. We re-insert them
@ -1064,7 +982,7 @@ errxit:
if (waitqueue_active(&ep->poll_wait)) if (waitqueue_active(&ep->poll_wait))
pwake++; pwake++;
} }
write_unlock_irqrestore(&ep->lock, flags); spin_unlock_irqrestore(&ep->lock, flags);
mutex_unlock(&ep->mtx); mutex_unlock(&ep->mtx);
@ -1092,7 +1010,7 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
MAX_SCHEDULE_TIMEOUT : (timeout * HZ + 999) / 1000; MAX_SCHEDULE_TIMEOUT : (timeout * HZ + 999) / 1000;
retry: retry:
write_lock_irqsave(&ep->lock, flags); spin_lock_irqsave(&ep->lock, flags);
res = 0; res = 0;
if (list_empty(&ep->rdllist)) { if (list_empty(&ep->rdllist)) {
@ -1119,9 +1037,9 @@ retry:
break; break;
} }
write_unlock_irqrestore(&ep->lock, flags); spin_unlock_irqrestore(&ep->lock, flags);
jtimeout = schedule_timeout(jtimeout); jtimeout = schedule_timeout(jtimeout);
write_lock_irqsave(&ep->lock, flags); spin_lock_irqsave(&ep->lock, flags);
} }
__remove_wait_queue(&ep->wq, &wait); __remove_wait_queue(&ep->wq, &wait);
@ -1131,7 +1049,7 @@ retry:
/* Is it worth to try to dig for events ? */ /* Is it worth to try to dig for events ? */
eavail = !list_empty(&ep->rdllist); eavail = !list_empty(&ep->rdllist);
write_unlock_irqrestore(&ep->lock, flags); spin_unlock_irqrestore(&ep->lock, flags);
/* /*
* Try to transfer events to user space. In case we get 0 events and * Try to transfer events to user space. In case we get 0 events and
@ -1276,12 +1194,6 @@ asmlinkage long sys_epoll_ctl(int epfd, int op, int fd,
error = -ENOENT; error = -ENOENT;
break; break;
} }
/*
* The function ep_find() increments the usage count of the structure
* so, if this is not NULL, we need to release it.
*/
if (epi)
ep_release_epitem(epi);
mutex_unlock(&ep->mtx); mutex_unlock(&ep->mtx);
error_tgt_fput: error_tgt_fput:
@ -1388,7 +1300,7 @@ asmlinkage long sys_epoll_pwait(int epfd, struct epoll_event __user *events,
if (sigmask) { if (sigmask) {
if (error == -EINTR) { if (error == -EINTR) {
memcpy(&current->saved_sigmask, &sigsaved, memcpy(&current->saved_sigmask, &sigsaved,
sizeof(sigsaved)); sizeof(sigsaved));
set_thread_flag(TIF_RESTORE_SIGMASK); set_thread_flag(TIF_RESTORE_SIGMASK);
} else } else
sigprocmask(SIG_SETMASK, &sigsaved, NULL); sigprocmask(SIG_SETMASK, &sigsaved, NULL);