Commit 9cbdaa44 authored by Andrew Morton's avatar Andrew Morton Committed by Linus Torvalds

[PATCH] epoll-per-fd fix

From: Davide Libenzi <davidel@xmailserver.org>

Fix epoll to allow pushing of multiple file descriptors sharing the same
kernel's file*
parent 1cf2ec10
...@@ -245,6 +245,9 @@ struct epitem { ...@@ -245,6 +245,9 @@ struct epitem {
/* The "container" of this item */ /* The "container" of this item */
struct eventpoll *ep; struct eventpoll *ep;
/* The file descriptor this item refers to */
int fd;
/* The file this item refers to */ /* The file this item refers to */
struct file *file; struct file *file;
...@@ -285,15 +288,17 @@ static int ep_getfd(int *efd, struct inode **einode, struct file **efile); ...@@ -285,15 +288,17 @@ static int ep_getfd(int *efd, struct inode **einode, struct file **efile);
static int ep_alloc_pages(char **pages, int numpages); static int ep_alloc_pages(char **pages, int numpages);
static int ep_free_pages(char **pages, int numpages); static int ep_free_pages(char **pages, int numpages);
static int ep_file_init(struct file *file, unsigned int hashbits); static int ep_file_init(struct file *file, unsigned int hashbits);
static unsigned int ep_hash_index(struct eventpoll *ep, struct file *file); static unsigned int ep_hash_index(struct eventpoll *ep, struct file *file, int fd);
static struct list_head *ep_hash_entry(struct eventpoll *ep, unsigned int index); static struct list_head *ep_hash_entry(struct eventpoll *ep, unsigned int index);
static int ep_init(struct eventpoll *ep, unsigned int hashbits); static int ep_init(struct eventpoll *ep, unsigned int hashbits);
static void ep_free(struct eventpoll *ep); static void ep_free(struct eventpoll *ep);
static struct epitem *ep_find(struct eventpoll *ep, struct file *file); static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd);
static void ep_use_epitem(struct epitem *epi); static void ep_use_epitem(struct epitem *epi);
static void ep_release_epitem(struct epitem *epi); static void ep_release_epitem(struct epitem *epi);
static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead, poll_table *pt); static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
static int ep_insert(struct eventpoll *ep, struct epoll_event *event, struct file *tfile); poll_table *pt);
static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
struct file *tfile, int fd);
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);
static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi); static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi);
static int ep_unlink(struct eventpoll *ep, struct epitem *epi); static int ep_unlink(struct eventpoll *ep, struct epitem *epi);
...@@ -580,7 +585,7 @@ asmlinkage long sys_epoll_ctl(int epfd, int op, int fd, struct epoll_event *even ...@@ -580,7 +585,7 @@ asmlinkage long sys_epoll_ctl(int epfd, int op, int fd, struct epoll_event *even
down_write(&ep->sem); down_write(&ep->sem);
/* Try to lookup the file inside our hash table */ /* Try to lookup the file inside our hash table */
epi = ep_find(ep, tfile); epi = ep_find(ep, tfile, fd);
error = -EINVAL; error = -EINVAL;
switch (op) { switch (op) {
...@@ -588,7 +593,7 @@ asmlinkage long sys_epoll_ctl(int epfd, int op, int fd, struct epoll_event *even ...@@ -588,7 +593,7 @@ asmlinkage long sys_epoll_ctl(int epfd, int op, int fd, struct epoll_event *even
if (!epi) { if (!epi) {
epds.events |= POLLERR | POLLHUP; epds.events |= POLLERR | POLLHUP;
error = ep_insert(ep, &epds, tfile); error = ep_insert(ep, &epds, tfile, fd);
} else } else
error = -EEXIST; error = -EEXIST;
break; break;
...@@ -814,10 +819,11 @@ static int ep_file_init(struct file *file, unsigned int hashbits) ...@@ -814,10 +819,11 @@ static int ep_file_init(struct file *file, unsigned int hashbits)
/* /*
* Calculate the index of the hash relative to "file". * Calculate the index of the hash relative to "file".
*/ */
static unsigned int ep_hash_index(struct eventpoll *ep, struct file *file) static unsigned int ep_hash_index(struct eventpoll *ep, struct file *file, int fd)
{ {
unsigned long ptr = (unsigned long) file ^ (fd << ep->hashbits);
return (unsigned int) hash_ptr(file, ep->hashbits); return (unsigned int) hash_ptr((void *) ptr, ep->hashbits);
} }
...@@ -920,7 +926,7 @@ static void ep_free(struct eventpoll *ep) ...@@ -920,7 +926,7 @@ static void ep_free(struct eventpoll *ep)
* the returned item, so the caller must call ep_release_epitem() * the returned item, so the caller must call ep_release_epitem()
* after finished using the "struct epitem". * after finished using the "struct epitem".
*/ */
static struct epitem *ep_find(struct eventpoll *ep, struct file *file) static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd)
{ {
unsigned long flags; unsigned long flags;
struct list_head *lsthead, *lnk; struct list_head *lsthead, *lnk;
...@@ -928,11 +934,11 @@ static struct epitem *ep_find(struct eventpoll *ep, struct file *file) ...@@ -928,11 +934,11 @@ static struct epitem *ep_find(struct eventpoll *ep, struct file *file)
read_lock_irqsave(&ep->lock, flags); read_lock_irqsave(&ep->lock, flags);
lsthead = ep_hash_entry(ep, ep_hash_index(ep, file)); lsthead = ep_hash_entry(ep, ep_hash_index(ep, file, fd));
list_for_each(lnk, lsthead) { list_for_each(lnk, lsthead) {
epi = list_entry(lnk, struct epitem, llink); epi = list_entry(lnk, struct epitem, llink);
if (epi->file == file) { if (epi->file == file && epi->fd == fd) {
ep_use_epitem(epi); ep_use_epitem(epi);
break; break;
} }
...@@ -976,7 +982,8 @@ static void ep_release_epitem(struct epitem *epi) ...@@ -976,7 +982,8 @@ static void ep_release_epitem(struct epitem *epi)
* This is the callback that is used to add our wait queue to the * This is the callback that is used to add our wait queue to the
* target file wakeup lists. * target file wakeup lists.
*/ */
static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead, poll_table *pt) static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
poll_table *pt)
{ {
struct epitem *epi = EP_ITEM_FROM_EPQUEUE(pt); struct epitem *epi = EP_ITEM_FROM_EPQUEUE(pt);
struct eppoll_entry *pwq; struct eppoll_entry *pwq;
...@@ -995,7 +1002,8 @@ static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead, po ...@@ -995,7 +1002,8 @@ static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead, po
} }
static int ep_insert(struct eventpoll *ep, struct epoll_event *event, struct file *tfile) static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
struct file *tfile, int fd)
{ {
int error, revents, pwake = 0; int error, revents, pwake = 0;
unsigned long flags; unsigned long flags;
...@@ -1014,6 +1022,7 @@ static int ep_insert(struct eventpoll *ep, struct epoll_event *event, struct fil ...@@ -1014,6 +1022,7 @@ static int ep_insert(struct eventpoll *ep, struct epoll_event *event, struct fil
INIT_LIST_HEAD(&epi->pwqlist); INIT_LIST_HEAD(&epi->pwqlist);
epi->ep = ep; epi->ep = ep;
epi->file = tfile; epi->file = tfile;
epi->fd = fd;
epi->event = *event; epi->event = *event;
atomic_set(&epi->usecnt, 1); atomic_set(&epi->usecnt, 1);
epi->nwait = 0; epi->nwait = 0;
...@@ -1046,7 +1055,7 @@ static int ep_insert(struct eventpoll *ep, struct epoll_event *event, struct fil ...@@ -1046,7 +1055,7 @@ static int ep_insert(struct eventpoll *ep, struct epoll_event *event, struct fil
write_lock_irqsave(&ep->lock, flags); write_lock_irqsave(&ep->lock, flags);
/* Add the current item to the hash table */ /* Add the current item to the hash table */
list_add(&epi->llink, ep_hash_entry(ep, ep_hash_index(ep, tfile))); list_add(&epi->llink, ep_hash_entry(ep, ep_hash_index(ep, tfile, fd)));
/* 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)) {
...@@ -1065,8 +1074,8 @@ static int ep_insert(struct eventpoll *ep, struct epoll_event *event, struct fil ...@@ -1065,8 +1074,8 @@ static int ep_insert(struct eventpoll *ep, struct epoll_event *event, struct fil
if (pwake) if (pwake)
ep_poll_safewake(&psw, &ep->poll_wait); ep_poll_safewake(&psw, &ep->poll_wait);
DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_insert(%p, %p)\n", DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_insert(%p, %p, %d)\n",
current, ep, tfile)); current, ep, tfile, fd));
return 0; return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment