2012-05-17 16:25:45

by Cyrill Gorcunov

[permalink] [raw]
Subject: [rfc 4/4] fs, epoll: Add procfs fdinfo helper

This allow us to print out eventpoll target file descriptor,
events and data, so the /proc/pid/fdinfo/fd consists of

| pos: 0
| flags: 02
| tfd: 5 events: 1d data: ffffffffffffffff

Signed-off-by: Cyrill Gorcunov <[email protected]>
---
fs/eventpoll.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 82 insertions(+)

Index: linux-2.6.git/fs/eventpoll.c
===================================================================
--- linux-2.6.git.orig/fs/eventpoll.c
+++ linux-2.6.git/fs/eventpoll.c
@@ -37,6 +37,8 @@
#include <asm/io.h>
#include <asm/mman.h>
#include <linux/atomic.h>
+#include <linux/proc_fs.h>
+#include <linux/seq_file.h>

/*
* LOCKING:
@@ -1815,6 +1817,84 @@ SYSCALL_DEFINE6(epoll_pwait, int, epfd,

#endif /* HAVE_SET_RESTORE_SIGMASK */

+#ifdef CONFIG_PROC_FS
+
+#define FDINFO_SEQ_UNLOCKED (void *)(0L)
+#define FDINFO_SEQ_LOCKED (void *)(1L)
+
+static struct epitem *
+seq_lookup_epi(struct proc_fdinfo_extra *extra, struct eventpoll *ep, loff_t num)
+{
+ struct rb_node *rbp;
+
+ if (extra->private == FDINFO_SEQ_UNLOCKED) {
+ mutex_lock(&ep->mtx);
+ extra->private = FDINFO_SEQ_LOCKED;
+ }
+
+ for (rbp = rb_first(&ep->rbr); rbp; rbp = rb_next(rbp)) {
+ if (num-- == 0)
+ return rb_entry(rbp, struct epitem, rbn);
+ }
+
+ return NULL;
+}
+
+static void *seq_start(struct seq_file *m, loff_t *pos)
+{
+ struct proc_fdinfo_extra *extra = m->private;
+ struct eventpoll *ep = extra->fd_file->private_data;
+
+ return *pos ? seq_lookup_epi(extra, ep, *pos - 1) : NULL;
+}
+
+static void seq_stop(struct seq_file *m, void *v)
+{
+ struct proc_fdinfo_extra *extra = m->private;
+ struct eventpoll *ep = extra->fd_file->private_data;
+
+ if (extra->private == FDINFO_SEQ_LOCKED)
+ mutex_unlock(&ep->mtx);
+ extra->private = FDINFO_SEQ_UNLOCKED;
+}
+
+static void *seq_next(struct seq_file *m, void *p, loff_t *pos)
+{
+ struct proc_fdinfo_extra *extra = m->private;
+ struct eventpoll *ep = extra->fd_file->private_data;
+ ++*pos;
+ return (void *)seq_lookup_epi(extra, ep, *pos - 1);
+}
+
+static int seq_show(struct seq_file *m, void *v)
+{
+ struct epitem *epi = v;
+ seq_printf(m, "tfd: %8d events: %8x data: %16llx\n",
+ epi->ffd.fd, epi->event.events, (long long)epi->event.data);
+ return 0;
+}
+
+static const struct seq_operations ep_fdinfo_ops = {
+ .start = seq_start,
+ .next = seq_next,
+ .stop = seq_stop,
+ .show = seq_show,
+};
+
+static struct proc_fdinfo_helper ep_fdinfo_helper = {
+ .name = "epoll",
+ .ops = &ep_fdinfo_ops,
+ .probe = is_file_epoll,
+};
+
+static int __init ep_register_fdinfo_helper(void)
+{
+ return proc_register_fdinfo_helper(&ep_fdinfo_helper);
+}
+#else
+static void ep_register_fdinfo_helper(void) { }
+#endif /* CONFIG_PROC_FS */
+
static int __init eventpoll_init(void)
{
struct sysinfo si;
@@ -1847,6 +1927,8 @@ static int __init eventpoll_init(void)
pwq_cache = kmem_cache_create("eventpoll_pwq",
sizeof(struct eppoll_entry), 0, SLAB_PANIC, NULL);

+ ep_register_fdinfo_helper();
+
return 0;
}
fs_initcall(eventpoll_init);