From edd011045dadc70d9c389040f750896ee9f12630 Mon Sep 17 00:00:00 2001 From: Andrew Morton Date: Tue, 20 Jan 2004 03:11:23 -0800 Subject: [PATCH] [PATCH] One-shot support for epoll From: Davide Libenzi The attached patch implements the one-shot support for epoll. Because of the way epoll works (hooking f_op->poll()) the ET behavior is not really ET because it might happen that, while data is still available to read (for the EPOLLIN case), another chunk will become available triggering another event. While those conditions can be easily be handled in userspace, the absolute triviality of the patch and the avoidance of user/kernel space switches and f_op->poll() calls, make IMHO worth doing this inside epoll itself. --- fs/eventpoll.c | 14 ++++++++++++++ include/linux/eventpoll.h | 3 +++ 2 files changed, 17 insertions(+) diff --git a/fs/eventpoll.c b/fs/eventpoll.c index 9c37105b8a5bf..128465c75aefd 100644 --- a/fs/eventpoll.c +++ b/fs/eventpoll.c @@ -93,6 +93,8 @@ #define EPI_SLAB_DEBUG 0 #endif /* #if DEBUG_EPI != 0 */ +/* Epoll private bits inside the event mask */ +#define EP_PRIVATE_BITS (EPOLLONESHOT | EPOLLET) /* Maximum number of poll wake up nests we are allowing */ #define EP_MAX_POLLWAKE_NESTS 4 @@ -1306,6 +1308,15 @@ static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync) write_lock_irqsave(&ep->lock, flags); + /* + * If the event mask does not contain any poll(2) event, we consider the + * descriptor to be disabled. This condition is likely the effect of the + * EPOLLONESHOT bit that disables the descriptor when an event is received, + * until the next EPOLL_CTL_MOD will be issued. + */ + if (!(epi->event.events & ~EP_PRIVATE_BITS)) + goto is_disabled; + /* If this file is already in the ready list we exit soon */ if (EP_IS_LINKED(&epi->rdllink)) goto is_linked; @@ -1322,6 +1333,7 @@ is_linked: if (waitqueue_active(&ep->poll_wait)) pwake++; +is_disabled: write_unlock_irqrestore(&ep->lock, flags); /* We have to call this outside the lock */ @@ -1458,6 +1470,8 @@ static int ep_send_events(struct eventpoll *ep, struct list_head *txlist, eventcnt += eventbuf; eventbuf = 0; } + if (epi->event.events & EPOLLONESHOT) + epi->event.events &= EP_PRIVATE_BITS; } } diff --git a/include/linux/eventpoll.h b/include/linux/eventpoll.h index 1ece9f1192206..136c6315e6af3 100644 --- a/include/linux/eventpoll.h +++ b/include/linux/eventpoll.h @@ -22,6 +22,9 @@ #define EPOLL_CTL_DEL 2 #define EPOLL_CTL_MOD 3 +/* Set the One Shot behaviour for the target file descriptor */ +#define EPOLLONESHOT (1 << 30) + /* Set the Edge Triggered behaviour for the target file descriptor */ #define EPOLLET (1 << 31) -- 2.53.0