linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
To: linux-mm@kvack.org, linux-kernel@vger.kernel.org
Cc: kosaki.motohiro@jp.fujitsu.com,
	Marcelo Tosatti <marcelo@kvack.org>,
	Daniel Spang <daniel.spang@gmail.com>,
	Rik van Riel <riel@redhat.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Alan Cox <alan@lxorguk.ukuu.org.uk>
Subject: [RFC][PATCH 8/8] mem_notify v5: support fasync feature
Date: Thu, 24 Jan 2008 13:26:11 +0900	[thread overview]
Message-ID: <20080124132452.1778.KOSAKI.MOTOHIRO@jp.fujitsu.com> (raw)
In-Reply-To: <20080124130348.1760.KOSAKI.MOTOHIRO@jp.fujitsu.com>

implement FASYNC capability to /dev/mem_notify.

<usage example>
        fd = open("/dev/mem_notify", O_RDONLY);

	fcntl(fd, F_SETOWN, getpid());

	flags = fcntl(fd, F_GETFL);
	fcntl(fd, F_SETFL, flags|FASYNC);  /* when low memory, receive SIGIO */
</usage example>


ChangeLog
	v5: new



Signed-off-by: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>

---
 mm/mem_notify.c |   95 +++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 90 insertions(+), 5 deletions(-)

Index: b/mm/mem_notify.c
===================================================================
--- a/mm/mem_notify.c	2008-01-23 23:09:08.000000000 +0900
+++ b/mm/mem_notify.c	2008-01-23 23:09:27.000000000 +0900
@@ -23,18 +23,58 @@
 #define PROC_WAKEUP_GUARD  (10*HZ)
 
 struct mem_notify_file_info {
-	unsigned long last_proc_notify;
+	unsigned long     last_proc_notify;
+	struct file      *file;
+
+	struct list_head  fa_list;
+	int	          fa_fd;
 };
 
 static DECLARE_WAIT_QUEUE_HEAD(mem_wait);
 static atomic_long_t nr_under_memory_pressure_zones = ATOMIC_LONG_INIT(0);
 static atomic_t nr_watcher_task = ATOMIC_INIT(0);
+static LIST_HEAD(mem_notify_fasync_list);
+static DEFINE_SPINLOCK(mem_notify_fasync_lock);
+static atomic_t nr_fasync_task = ATOMIC_INIT(0);
 
 atomic_long_t last_mem_notify = ATOMIC_LONG_INIT(INITIAL_JIFFIES);
 
+
+static void mem_notify_kill_fasync_nr(int sig, int band, int nr)
+{
+	struct mem_notify_file_info *iter, *saved_iter;
+	LIST_HEAD(l_fired);
+
+	if (!nr)
+		return;
+
+	spin_lock(&mem_notify_fasync_lock);
+
+	list_for_each_entry_safe_reverse(iter, saved_iter, &mem_notify_fasync_list, fa_list) {
+		struct fown_struct * fown;
+
+		fown = &iter->file->f_owner;
+		if (!(sig == SIGURG && fown->signum == 0))
+			send_sigio(fown, iter->fa_fd, band);
+
+		list_del(&iter->fa_list);
+		list_add(&iter->fa_list, &l_fired);
+		if(!--nr)
+			break;
+	}
+
+	/* rotate moving for FIFO wakeup */
+	list_splice(&l_fired, &mem_notify_fasync_list);
+
+	spin_unlock(&mem_notify_fasync_lock);
+}
+
+
 void __memory_pressure_notify(struct zone* zone, int pressure)
 {
 	int nr_wakeup;
+	int nr_poll_wakeup = 0;
+	int nr_fasync_wakeup = 0;
 	int flags;
 
 	spin_lock_irqsave(&mem_wait.lock, flags);
@@ -47,13 +87,18 @@ void __memory_pressure_notify(struct zon
 
 	if (pressure) {
 		int nr_watcher = atomic_read(&nr_watcher_task);
+		int nr_fasync = atomic_read(&nr_fasync_task);
 
 		nr_wakeup = (nr_watcher >> 4) + 1;
 		if (unlikely(nr_wakeup > 100))
 			nr_wakeup = 100;
 
+ 		nr_fasync_wakeup = nr_wakeup * nr_fasync/nr_watcher;
+ 		nr_poll_wakeup = nr_wakeup - nr_fasync_wakeup;
+
 		atomic_long_set(&last_mem_notify, jiffies);
-		wake_up_locked_nr(&mem_wait, nr_wakeup);
+		wake_up_locked_nr(&mem_wait, nr_poll_wakeup);
+ 		mem_notify_kill_fasync_nr(SIGIO, POLL_IN, nr_fasync_wakeup);
 	}
 
 	spin_unlock_irqrestore(&mem_wait.lock, flags);
@@ -71,6 +116,9 @@ static int mem_notify_open(struct inode 
 	}
 
 	info->last_proc_notify = INITIAL_JIFFIES;
+	INIT_LIST_HEAD(&info->fa_list);
+	info->file = file;
+	info->fa_fd = -1;
 	file->private_data = info;
 	atomic_inc(&nr_watcher_task);
 out:
@@ -79,7 +127,16 @@ out:
 
 static int mem_notify_release(struct inode *inode, struct file *file)
 {
-	kfree(file->private_data);
+	struct mem_notify_file_info *info = file->private_data;
+
+	spin_lock(&mem_notify_fasync_lock);
+	if (!list_empty(&info->fa_list)) {
+		list_del(&info->fa_list);
+		atomic_dec(&nr_fasync_task);
+	}
+	spin_unlock(&mem_notify_fasync_lock);
+
+	kfree(info);
 	atomic_dec(&nr_watcher_task);
 	return 0;
 }
@@ -106,9 +163,37 @@ out:
 	return retval;
 }
 
+static int mem_notify_fasync(int fd, struct file *filp, int on)
+{
+	struct mem_notify_file_info *info = filp->private_data;
+	int result = 0;
+
+	spin_lock(&mem_notify_fasync_lock);
+	if (on) {
+		if (list_empty(&info->fa_list)) {
+			info->fa_fd = fd;
+			list_add(&info->fa_list, &mem_notify_fasync_list);
+			result = 1;
+		} else {
+			info->fa_fd = fd;
+		}
+	} else {
+		if (!list_empty(&info->fa_list)) {
+			list_del_init(&info->fa_list);
+			info->fa_fd = -1;
+			result = -1;
+		}
+	}
+	if (result != 0)
+		atomic_add(result, &nr_fasync_task);
+	spin_unlock(&mem_notify_fasync_lock);
+	return abs(result);
+}
+
 struct file_operations mem_notify_fops = {
-	.open = mem_notify_open,
+	.open    = mem_notify_open,
 	.release = mem_notify_release,
-	.poll = mem_notify_poll,
+	.poll    = mem_notify_poll,
+	.fasync  = mem_notify_fasync,
 };
 EXPORT_SYMBOL(mem_notify_fops);


--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

      parent reply	other threads:[~2008-01-24  4:26 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-01-24  4:18 [RFC][PATCH 0/8] mem_notify v5 KOSAKI Motohiro
2008-01-24  4:19 ` [RFC][PATCH 1/8] mem_notify v5: introduce poll_wait_exclusive() new API KOSAKI Motohiro
2008-01-24  4:20 ` [RFC][PATCH 2/8] mem_notify v5: introduce wake_up_locked_nr() " KOSAKI Motohiro
2008-01-24  4:21 ` [RFC][PATCH 3/8] mem_notify v5: introduce /dev/mem_notify new device (the core of this patch series) KOSAKI Motohiro
2008-01-24 12:19   ` Daniel Spång
2008-01-25  3:33     ` KOSAKI Motohiro
2008-01-24  4:22 ` [RFC][PATCH 4/8] mem_notify v5: memory_pressure_notify() caller KOSAKI Motohiro
2008-01-24  4:22 ` [RFC][PATCH 5/8] mem_notify v5: add new mem_notify field to /proc/zoneinfo KOSAKI Motohiro
2008-01-24  4:23 ` [RFC][PATCH 6/8] mem_notify v5: (optional) fixed incorrect shrink_zone KOSAKI Motohiro
2008-01-24  4:24 ` [RFC][PATCH 7/8] mem_notify v5: ignore very small zone for prevent incorrect low mem notify KOSAKI Motohiro
2008-01-24  4:26 ` KOSAKI Motohiro [this message]

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=20080124132452.1778.KOSAKI.MOTOHIRO@jp.fujitsu.com \
    --to=kosaki.motohiro@jp.fujitsu.com \
    --cc=akpm@linux-foundation.org \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=daniel.spang@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=marcelo@kvack.org \
    --cc=riel@redhat.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