[RFC,06/15] epoll: introduce various of helpers for user structure lengths calculations
diff mbox series

Message ID 20190109164025.24554-7-rpenyaev@suse.de
State Superseded
Headers show
Series
  • epoll: support pollable epoll from userspace
Related show

Commit Message

Roman Penyaev Jan. 9, 2019, 4:40 p.m. UTC
Helpers for lengths-from-number and number-from-lengths calculations.
Among them:

  ep_polled_by_user()
	- returns true if epoll was created with EPOLL_USERPOLL

  ep_user_ring_events_available()
	- returns true if there is something in user ring buffer

Signed-off-by: Roman Penyaev <rpenyaev@suse.de>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Davidlohr Bueso <dbueso@suse.de>
Cc: Jason Baron <jbaron@akamai.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Andrea Parri <andrea.parri@amarulasolutions.com>
Cc: linux-fsdevel@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
---
 fs/eventpoll.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)

Patch
diff mbox series

diff --git a/fs/eventpoll.c b/fs/eventpoll.c
index 9ec682b6488f..ae288f62aa4c 100644
--- a/fs/eventpoll.c
+++ b/fs/eventpoll.c
@@ -438,6 +438,65 @@  static void ep_nested_calls_init(struct nested_calls *ncalls)
 	spin_lock_init(&ncalls->lock);
 }
 
+static inline unsigned int to_items_length(unsigned int nr)
+{
+	struct eventpoll *ep;
+
+	return (sizeof(*ep->user_header) +
+		(nr << ilog2(sizeof(ep->user_header->items[0]))));
+}
+
+static inline unsigned int to_index_length(unsigned int nr)
+{
+	struct eventpoll *ep;
+
+	return nr << ilog2(sizeof(*ep->user_index));
+}
+
+static inline unsigned int to_items_bm_length(unsigned int nr)
+{
+	return ALIGN(nr, 8) >> 3;
+}
+
+static inline unsigned int to_items_nr(unsigned int len)
+{
+	struct eventpoll *ep;
+
+	return (len - sizeof(*ep->user_header)) >>
+		ilog2(sizeof(ep->user_header->items[0]));
+}
+
+static inline unsigned int to_items_bm_nr(unsigned int len)
+{
+	return len << 3;
+}
+
+static inline unsigned int ep_max_items_nr(struct eventpoll *ep)
+{
+	return to_items_nr(ep->header_length);
+}
+
+static inline unsigned int ep_max_index_nr(struct eventpoll *ep)
+{
+	return ep->index_length >> ilog2(sizeof(*ep->user_index));
+}
+
+static inline unsigned int ep_max_items_bm_nr(struct eventpoll *ep)
+{
+	return to_items_bm_nr(ep->items_bm_length);
+}
+
+static inline bool ep_polled_by_user(struct eventpoll *ep)
+{
+	return !!ep->user_header;
+}
+
+static inline bool ep_user_ring_events_available(struct eventpoll *ep)
+{
+	return ep_polled_by_user(ep) &&
+		ep->user_header->head != ep->user_header->tail;
+}
+
 /**
  * ep_events_available - Checks if ready events might be available.
  *