netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Evgeniy Polyakov <johnpol@2ka.mipt.ru>
To: lkml <linux-kernel@vger.kernel.org>
Cc: David Miller <davem@davemloft.net>,
	Ulrich Drepper <drepper@redhat.com>,
	Andrew Morton <akpm@osdl.org>,
	Evgeniy Polyakov <johnpol@2ka.mipt.ru>,
	netdev <netdev@vger.kernel.org>,
	Zach Brown <zach.brown@oracle.com>,
	Christoph Hellwig <hch@infradead.org>
Subject: [take11 2/3] kevent: poll/select() notifications.
Date: Thu, 17 Aug 2006 11:43:33 +0400	[thread overview]
Message-ID: <11558006133752@2ka.mipt.ru> (raw)
In-Reply-To: <115580061371@2ka.mipt.ru>


poll/select() notifications.

This patch includes generic poll/select and timer notifications.

kevent_poll works simialr to epoll and has the same issues (callback
is invoked not from internal state machine of the caller, but through
process awake).

Signed-off-by: Evgeniy Polyakov <johnpol@2ka.mitp.ru>

diff --git a/kernel/kevent/kevent_poll.c b/kernel/kevent/kevent_poll.c
new file mode 100644
index 0000000..75a75d1
--- /dev/null
+++ b/kernel/kevent/kevent_poll.c
@@ -0,0 +1,221 @@
+/*
+ * 	kevent_poll.c
+ * 
+ * 2006 Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
+ * All rights reserved.
+ * 
+ * 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/kernel.h>
+#include <linux/types.h>
+#include <linux/list.h>
+#include <linux/slab.h>
+#include <linux/spinlock.h>
+#include <linux/timer.h>
+#include <linux/file.h>
+#include <linux/kevent.h>
+#include <linux/poll.h>
+#include <linux/fs.h>
+
+static kmem_cache_t *kevent_poll_container_cache;
+static kmem_cache_t *kevent_poll_priv_cache;
+
+struct kevent_poll_ctl
+{
+	struct poll_table_struct 	pt;
+	struct kevent			*k;
+};
+
+struct kevent_poll_wait_container
+{
+	struct list_head		container_entry;
+	wait_queue_head_t		*whead;
+	wait_queue_t			wait;
+	struct kevent			*k;
+};
+
+struct kevent_poll_private
+{
+	struct list_head		container_list;
+	spinlock_t			container_lock;
+};
+
+static int kevent_poll_enqueue(struct kevent *k);
+static int kevent_poll_dequeue(struct kevent *k);
+static int kevent_poll_callback(struct kevent *k);
+
+static int kevent_poll_wait_callback(wait_queue_t *wait, 
+		unsigned mode, int sync, void *key)
+{
+	struct kevent_poll_wait_container *cont = 
+		container_of(wait, struct kevent_poll_wait_container, wait);
+	struct kevent *k = cont->k;
+	struct file *file = k->st->origin;
+	u32 revents;
+
+	revents = file->f_op->poll(file, NULL);
+
+	kevent_storage_ready(k->st, NULL, revents);
+
+	return 0;
+}
+
+static void kevent_poll_qproc(struct file *file, wait_queue_head_t *whead, 
+		struct poll_table_struct *poll_table)
+{
+	struct kevent *k = 
+		container_of(poll_table, struct kevent_poll_ctl, pt)->k;
+	struct kevent_poll_private *priv = k->priv;
+	struct kevent_poll_wait_container *cont;
+	unsigned long flags;
+
+	cont = kmem_cache_alloc(kevent_poll_container_cache, SLAB_KERNEL);
+	if (!cont) {
+		kevent_break(k);
+		return;
+	}
+		
+	cont->k = k;
+	init_waitqueue_func_entry(&cont->wait, kevent_poll_wait_callback);
+	cont->whead = whead;
+
+	spin_lock_irqsave(&priv->container_lock, flags);
+	list_add_tail(&cont->container_entry, &priv->container_list);
+	spin_unlock_irqrestore(&priv->container_lock, flags);
+
+	add_wait_queue(whead, &cont->wait);
+}
+
+static int kevent_poll_enqueue(struct kevent *k)
+{
+	struct file *file;
+	int err, ready = 0;
+	unsigned int revents;
+	struct kevent_poll_ctl ctl;
+	struct kevent_poll_private *priv;
+
+	file = fget(k->event.id.raw[0]);
+	if (!file)
+		return -ENODEV;
+
+	err = -EINVAL;
+	if (!file->f_op || !file->f_op->poll)
+		goto err_out_fput;
+
+	err = -ENOMEM;
+	priv = kmem_cache_alloc(kevent_poll_priv_cache, SLAB_KERNEL);
+	if (!priv)
+		goto err_out_fput;
+
+	spin_lock_init(&priv->container_lock);
+	INIT_LIST_HEAD(&priv->container_list);
+
+	k->priv = priv;
+
+	ctl.k = k;
+	init_poll_funcptr(&ctl.pt, &kevent_poll_qproc);
+
+	err = kevent_storage_enqueue(&file->st, k);
+	if (err)
+		goto err_out_free;
+
+	revents = file->f_op->poll(file, &ctl.pt);
+	if (revents & k->event.event) {
+		ready = 1;
+		kevent_poll_dequeue(k);
+	}
+	
+	return ready;
+
+err_out_free:
+	kmem_cache_free(kevent_poll_priv_cache, priv);
+err_out_fput:
+	fput(file);
+	return err;
+}
+
+static int kevent_poll_dequeue(struct kevent *k)
+{
+	struct file *file = k->st->origin;
+	struct kevent_poll_private *priv = k->priv;
+	struct kevent_poll_wait_container *w, *n;
+	unsigned long flags;
+
+	kevent_storage_dequeue(k->st, k);
+
+	spin_lock_irqsave(&priv->container_lock, flags);
+	list_for_each_entry_safe(w, n, &priv->container_list, container_entry) {
+		list_del(&w->container_entry);
+		remove_wait_queue(w->whead, &w->wait);
+		kmem_cache_free(kevent_poll_container_cache, w);
+	}
+	spin_unlock_irqrestore(&priv->container_lock, flags);
+	
+	kmem_cache_free(kevent_poll_priv_cache, priv);
+	k->priv = NULL;
+	
+	fput(file);
+
+	return 0;
+}
+
+static int kevent_poll_callback(struct kevent *k)
+{
+	struct file *file = k->st->origin;
+	unsigned int revents = file->f_op->poll(file, NULL);
+	return (revents & k->event.event);
+}
+
+static int __init kevent_poll_sys_init(void)
+{
+	struct kevent_callbacks pc = {
+		.callback = &kevent_poll_callback,
+		.enqueue = &kevent_poll_enqueue,
+		.dequeue = &kevent_poll_dequeue};
+
+	kevent_poll_container_cache = kmem_cache_create("kevent_poll_container_cache", 
+			sizeof(struct kevent_poll_wait_container), 0, 0, NULL, NULL);
+	if (!kevent_poll_container_cache) {
+		printk(KERN_ERR "Failed to create kevent poll container cache.\n");
+		return -ENOMEM;
+	}
+	
+	kevent_poll_priv_cache = kmem_cache_create("kevent_poll_priv_cache", 
+			sizeof(struct kevent_poll_private), 0, 0, NULL, NULL);
+	if (!kevent_poll_priv_cache) {
+		printk(KERN_ERR "Failed to create kevent poll private data cache.\n");
+		kmem_cache_destroy(kevent_poll_container_cache);
+		kevent_poll_container_cache = NULL;
+		return -ENOMEM;
+	}
+	
+	kevent_add_callbacks(&pc, KEVENT_POLL);
+
+	printk(KERN_INFO "Kevent poll()/select() subsystem has been initialized.\n");
+	return 0;
+}
+
+static struct lock_class_key kevent_poll_key;
+
+void kevent_poll_reinit(struct file *file)
+{
+	lockdep_set_class(&file->st.lock, &kevent_poll_key);
+}
+
+static void __exit kevent_poll_sys_fini(void)
+{
+	kmem_cache_destroy(kevent_poll_priv_cache);
+	kmem_cache_destroy(kevent_poll_container_cache);
+}
+
+module_init(kevent_poll_sys_init);
+module_exit(kevent_poll_sys_fini);

  reply	other threads:[~2006-08-17  7:43 UTC|newest]

Thread overview: 143+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <12345678912345.GA1898@2ka.mipt.ru>
2006-08-17  7:43 ` [take11 0/3] kevent: Generic event handling mechanism Evgeniy Polyakov
2006-08-17  7:43   ` [take11 1/3] kevent: Core files Evgeniy Polyakov
2006-08-17  7:43     ` Evgeniy Polyakov [this message]
2006-08-17  7:43       ` [take11 3/3] kevent: Timer notifications Evgeniy Polyakov
2006-08-21 10:19 ` [take12 0/3] kevent: Generic event handling mechanism Evgeniy Polyakov
2006-08-21 10:19   ` [take12 1/3] kevent: Core files Evgeniy Polyakov
2006-08-21 10:19     ` [take12 2/3] kevent: poll/select() notifications Evgeniy Polyakov
2006-08-21 10:19       ` [take12 3/3] kevent: Timer notifications Evgeniy Polyakov
2006-08-21 11:12         ` Christoph Hellwig
2006-08-21 11:18           ` Evgeniy Polyakov
2006-08-21 11:27             ` Arjan van de Ven
2006-08-21 11:59               ` Evgeniy Polyakov
2006-08-21 12:13                 ` Arjan van de Ven
2006-08-21 12:25                   ` Evgeniy Polyakov
2006-08-21 14:25             ` Thomas Gleixner
2006-08-22 18:25               ` Evgeniy Polyakov
2006-08-21 12:09           ` Evgeniy Polyakov
2006-08-22  4:36             ` Andrew Morton
2006-08-22  5:48               ` Evgeniy Polyakov
2006-08-21 12:37         ` [take12 4/3] kevent: Comment cleanup Evgeniy Polyakov
2006-08-23  8:51     ` [take12 1/3] kevent: Core files Eric Dumazet
2006-08-23  9:18       ` Evgeniy Polyakov
2006-08-23  9:23         ` Eric Dumazet
2006-08-23  9:29           ` Evgeniy Polyakov
2006-08-22  7:00   ` [take12 0/3] kevent: Generic event handling mechanism Nicholas Miell
2006-08-22  7:24     ` Evgeniy Polyakov
2006-08-22  8:17       ` Nicholas Miell
2006-08-22  8:23         ` David Miller
2006-08-22  8:59           ` Nicholas Miell
2006-08-22 14:59             ` James Morris
2006-08-22 20:00               ` Nicholas Miell
2006-08-22 20:36                 ` David Miller
2006-08-22 21:13                   ` Nicholas Miell
2006-08-22 21:25                     ` David Miller
2006-08-22 22:58                       ` Nicholas Miell
2006-08-22 23:46                         ` Ulrich Drepper
2006-08-23  1:51                           ` Nicholas Miell
2006-08-23  6:54                           ` Evgeniy Polyakov
2006-08-22  8:37         ` Evgeniy Polyakov
2006-08-22  9:29           ` Nicholas Miell
2006-08-22 10:03             ` Evgeniy Polyakov
2006-08-22 19:57               ` Nicholas Miell
2006-08-22 20:16                 ` Evgeniy Polyakov
2006-08-22 21:13                   ` Nicholas Miell
2006-08-22 21:37                     ` Randy.Dunlap
2006-08-22 22:01                       ` Andrew Morton
2006-08-22 22:17                         ` David Miller
2006-08-22 23:35                           ` Andrew Morton
2006-08-22 22:58                       ` Nicholas Miell
2006-08-22 23:06                         ` David Miller
2006-08-23  1:36                           ` The Proposed Linux kevent API (was: Re: [take12 0/3] kevent: Generic event handling mechanism.) Nicholas Miell
2006-08-23  2:01                             ` The Proposed Linux kevent API Howard Chu
2006-08-23  3:31                             ` David Miller
2006-08-23  3:47                               ` Nicholas Miell
2006-08-23  4:23                                 ` Nicholas Miell
2006-08-23  6:22                             ` The Proposed Linux kevent API (was: Re: [take12 0/3] kevent: Generic event handling mechanism.) Evgeniy Polyakov
2006-08-23  8:01                               ` Nicholas Miell
2006-08-23 18:24                             ` The Proposed Linux kevent API Stephen Hemminger
2006-08-22 23:22                         ` [take12 0/3] kevent: Generic event handling mechanism Randy.Dunlap
     [not found]         ` <b3f268590608220957g43a16d6bmde8a542f8ad8710b@mail.gmail.com>
2006-08-22 17:09           ` Jari Sundell
2006-08-22 18:01           ` Evgeniy Polyakov
2006-08-22 19:14             ` Jari Sundell
2006-08-22 19:47               ` Evgeniy Polyakov
2006-08-22 22:51                 ` Jari Sundell
2006-08-22 23:11                   ` Alexey Kuznetsov
2006-08-23  0:28                     ` Jari Sundell
2006-08-23  0:32                       ` David Miller
2006-08-23  0:43                         ` Jari Sundell
2006-08-23  6:56                           ` Evgeniy Polyakov
2006-08-23  7:07                             ` Andrew Morton
2006-08-23  7:10                               ` Evgeniy Polyakov
2006-08-23  9:58                                 ` Andi Kleen
2006-08-23 10:03                                   ` Evgeniy Polyakov
2006-08-23  7:35                               ` David Miller
2006-08-23  8:18                                 ` Nicholas Miell
2006-08-23  7:43                               ` Ian McDonald
2006-08-23  7:50                               ` Evgeniy Polyakov
2006-08-23 16:09                                 ` Andrew Morton
2006-08-23 16:22                                   ` Evgeniy Polyakov
2006-08-23  8:22                             ` Jari Sundell
2006-08-23  8:39                               ` Evgeniy Polyakov
2006-08-23  9:49                                 ` Jari Sundell
2006-08-23 10:20                                   ` Evgeniy Polyakov
2006-08-23 10:34                                     ` Jari Sundell
2006-08-23 10:51                                       ` Evgeniy Polyakov
2006-08-23 12:55                                         ` Jari Sundell
2006-08-23 13:11                                           ` Evgeniy Polyakov
2006-08-22 11:54   ` [PATCH] kevent_user: remove non-chardev interface Christoph Hellwig
2006-08-22 12:17     ` Evgeniy Polyakov
2006-08-22 12:27       ` Christoph Hellwig
2006-08-22 12:39         ` Evgeniy Polyakov
2006-08-22 11:55   ` [PATCH] kevent_user: use struct kevent_mring for the page ring Christoph Hellwig
2006-08-22 12:20     ` Evgeniy Polyakov
2006-08-23 11:24 ` [take13 0/3] kevent: Generic event handling mechanism Evgeniy Polyakov
2006-08-23 11:24   ` [take13 1/3] kevent: Core files Evgeniy Polyakov
2006-08-23 11:24     ` [take13 2/3] kevent: poll/select() notifications Evgeniy Polyakov
2006-08-23 11:24       ` [take13 3/3] kevent: Timer notifications Evgeniy Polyakov
2006-08-23 12:51     ` [take13 1/3] kevent: Core files Eric Dumazet
     [not found]       ` <20060823132753.GB29056@2ka.mipt.ru>
2006-08-23 13:44         ` Evgeniy Polyakov
2006-08-24 20:03     ` Christoph Hellwig
2006-08-25  5:48       ` Evgeniy Polyakov
2006-08-25  6:20         ` Andrew Morton
2006-08-25  6:32           ` Evgeniy Polyakov
2006-08-25  6:58             ` Andrew Morton
2006-08-25  7:20               ` Evgeniy Polyakov
2006-08-25  7:01           ` David Miller
2006-08-25  7:13             ` Andrew Morton
     [not found]   ` <Pine.LNX.4.63.0608231313370.8007@alpha.polcom.net>
     [not found]     ` <20060823122509.GA5744@2ka.mipt.ru>
     [not found]       ` <Pine.LNX.4.63.0608231437170.8007@alpha.polcom.net>
     [not found]         ` <20060823134227.GC29056@2ka.mipt.ru>
2006-08-23 18:56           ` [take13 0/3] kevent: Generic event handling mechanism Evgeniy Polyakov
2006-08-23 19:42             ` Evgeniy Polyakov
2006-08-25  9:54 ` [take14 " Evgeniy Polyakov
2006-08-25  9:54   ` [take14 1/3] kevent: Core files Evgeniy Polyakov
2006-08-25  9:54     ` [take14 2/3] kevent: poll/select() notifications Evgeniy Polyakov
2006-08-25  9:54       ` [take14 3/3] kevent: Timer notifications Evgeniy Polyakov
2006-08-27 21:03   ` [take14 0/3] kevent: Generic event handling mechanism Ulrich Drepper
2006-08-28  1:57     ` David Miller
2006-08-28  2:11       ` Ulrich Drepper
2006-08-28  2:40       ` Nicholas Miell
2006-08-28  2:59     ` Nicholas Miell
2006-08-28 11:47       ` Jari Sundell
2006-08-31  7:58     ` Evgeniy Polyakov
2006-09-09 16:10       ` Ulrich Drepper
2006-09-11  5:42         ` Evgeniy Polyakov
2006-09-04 10:14 ` [take15 0/4] " Evgeniy Polyakov
2006-09-04  9:58   ` Evgeniy Polyakov
2006-09-04 10:14   ` [take15 1/4] kevent: Core files Evgeniy Polyakov
2006-09-04 10:14     ` [take15 2/4] kevent: poll/select() notifications Evgeniy Polyakov
2006-09-04 10:14       ` [take15 3/4] kevent: Socket notifications Evgeniy Polyakov
2006-09-04 10:14         ` [take15 4/4] kevent: Timer notifications Evgeniy Polyakov
2006-09-05 13:39           ` Arnd Bergmann
2006-09-06  6:42             ` Evgeniy Polyakov
2006-09-05 13:28     ` [take15 1/4] kevent: Core files Arnd Bergmann
2006-09-06  6:51       ` Evgeniy Polyakov
2006-09-04 10:24   ` [take15 0/4] kevent: Generic event handling mechanism Evgeniy Polyakov
2006-09-06 11:55 ` [take16 " Evgeniy Polyakov
2006-09-06 11:55   ` [take16 1/4] kevent: Core files Evgeniy Polyakov
2006-09-06 11:55     ` [take16 2/4] kevent: poll/select() notifications Evgeniy Polyakov
2006-09-06 11:55       ` [take16 3/4] kevent: Socket notifications Evgeniy Polyakov
2006-09-06 11:55         ` [take16 4/4] kevent: Timer notifications Evgeniy Polyakov
2006-09-06 13:40     ` [take16 1/4] kevent: Core files Chase Venters
2006-09-06 13:54       ` Chase Venters
2006-09-06 14:03       ` Evgeniy Polyakov
2006-09-06 14:23         ` Chase Venters
2006-09-07  7:10           ` Evgeniy Polyakov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=11558006133752@2ka.mipt.ru \
    --to=johnpol@2ka.mipt.ru \
    --cc=akpm@osdl.org \
    --cc=davem@davemloft.net \
    --cc=drepper@redhat.com \
    --cc=hch@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=zach.brown@oracle.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).