public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: LKML <linux-kernel@vger.kernel.org>
Cc: Christoph Hellwig <hch@infradead.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Ingo Molnar <mingo@elte.hu>,
	clark.williams@gmail.com
Subject: [PATCH] on_each_task - let modules iterate a function on each task
Date: Wed, 15 Aug 2007 14:07:47 -0400	[thread overview]
Message-ID: <1187201267.3848.16.camel@localhost.localdomain> (raw)

Hi all,

I'm writing a module that can do a dynamic isolcpus at run time. This is
much easier to do in the kernel than in userspace.  In order to do this,
I need to iterate over all tasks in the system but I could not find a
way to do this from a module.

So instead of exporting the tasklist_lock (and opening up a great big
can of worms), I wrote this utility function that will allow a module to
iterate some function over all tasks in the system.

Comments and flames welcome.

Signed-off-by: Steven Rostedt <rostedt@goodmis.org>

Index: linux-2.6.21.6-rt21/kernel/sched.c
===================================================================
--- linux-2.6.21.6-rt21.orig/kernel/sched.c
+++ linux-2.6.21.6-rt21/kernel/sched.c
@@ -7065,6 +7065,31 @@ void normalize_rt_tasks(void)
 
 #endif /* CONFIG_MAGIC_SYSRQ */
 
+/**
+ * on_each_task - run a function on every task.
+ * @func - function to call on the task
+ * @data - data to pass in to func.
+ *
+ * Iterate a function over all tasks in the system.
+ */
+void on_each_task(int(*func)(struct task_struct *t, void *d), void *data)
+{
+	struct task_struct *g, *p;
+
+	read_lock(&tasklist_lock);
+
+	do_each_thread(g, p) {
+
+		/* do_each_thread is a double loop! */
+		if (func(p, data))
+			goto out;
+
+	} while_each_thread(g, p);
+ out:
+	read_unlock(&tasklist_lock);
+}
+EXPORT_SYMBOL_GPL(on_each_task);
+
 #ifdef CONFIG_IA64
 /*
  * These functions are only useful for the IA64 MCA handling.
Index: linux-2.6.21.6-rt21/include/linux/sched.h
===================================================================
--- linux-2.6.21.6-rt21.orig/include/linux/sched.h
+++ linux-2.6.21.6-rt21/include/linux/sched.h
@@ -2079,6 +2079,8 @@ extern int sched_create_sysfs_power_savi
 
 extern void normalize_rt_tasks(void);
 
+extern void on_each_task(int(*func)(struct task_struct *t, void *d), void *data);
+
 #ifdef CONFIG_TASK_XACCT
 static inline void add_rchar(struct task_struct *tsk, ssize_t amt)
 {



                 reply	other threads:[~2007-08-15 18:09 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=1187201267.3848.16.camel@localhost.localdomain \
    --to=rostedt@goodmis.org \
    --cc=akpm@linux-foundation.org \
    --cc=clark.williams@gmail.com \
    --cc=hch@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    /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