public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [patch 1/2] cpusets: extract mmarray loading from update_nodemask
@ 2007-10-25 22:54 David Rientjes
  2007-10-25 22:54 ` [patch 2/2] cpusets: add interleave_over_allowed option David Rientjes
  0 siblings, 1 reply; 98+ messages in thread
From: David Rientjes @ 2007-10-25 22:54 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Andi Kleen, Paul Jackson, Christoph Lameter, Lee Schermerhorn,
	linux-kernel

Extract a helper function from update_nodemask() to load an array of
mm_struct pointers with references to each task's mm_struct that is
currently attached to a given cpuset.

This will be used later for other purposes where memory policies need to
be rebound for each task attached to a cpuset.

Cc: Andi Kleen <ak@suse.de>
Cc: Paul Jackson <pj@sgi.com>
Cc: Christoph Lameter <clameter@sgi.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
Signed-off-by: David Rientjes <rientjes@google.com>
---
 kernel/cpuset.c |  130 ++++++++++++++++++++++++++++++++++---------------------
 1 files changed, 81 insertions(+), 49 deletions(-)

diff --git a/kernel/cpuset.c b/kernel/cpuset.c
--- a/kernel/cpuset.c
+++ b/kernel/cpuset.c
@@ -702,6 +702,79 @@ done:
 	/* Don't kfree(doms) -- partition_sched_domains() does that. */
 }
 
+/*
+ * Loads mmarray with pointers to all the mm_struct's of tasks attached to
+ * cpuset cs.
+ *
+ * The reference count to each mm is incremented before loading it into the
+ * array, so put_cpuset_mm_array() must be called after this function to
+ * decrement each reference count and free the memory allocated for mmarray
+ * via this function.
+ */
+static struct mm_struct **get_cpuset_mm_array(const struct cpuset *cs,
+					      int *ntasks)
+{
+	struct mm_struct **mmarray;
+	struct task_struct *p;
+	struct cgroup_iter it;
+	int count;
+	int fudge;
+
+	*ntasks = 0;
+	fudge = 10;				/* spare mmarray[] slots */
+	fudge += cpus_weight(cs->cpus_allowed);	/* imagine one fork-bomb/cpu */
+	/*
+	 * Allocate mmarray[] to hold mm reference for each task in cpuset cs.
+	 * Can't kmalloc GFP_KERNEL while holding tasklist_lock.  We could use
+	 * GFP_ATOMIC, but with a few more lines of code, we can retry until
+	 * we get a big enough mmarray[] w/o using GFP_ATOMIC.
+	 */
+	while (1) {
+		count = cgroup_task_count(cs->css.cgroup);  /* guess */
+		count += fudge;
+		mmarray = kmalloc(count * sizeof(*mmarray), GFP_KERNEL);
+		if (!mmarray)
+			return NULL;
+		read_lock(&tasklist_lock);		/* block fork */
+		if (cgroup_task_count(cs->css.cgroup) <= count)
+			break;				/* got enough */
+		read_unlock(&tasklist_lock);		/* try again */
+		kfree(mmarray);
+	}
+
+	/* Load up mmarray[] with mm reference for each task in cpuset. */
+	cgroup_iter_start(cs->css.cgroup, &it);
+	while ((p = cgroup_iter_next(cs->css.cgroup, &it))) {
+		struct mm_struct *mm;
+
+		if (*ntasks >= count) {
+			printk(KERN_WARNING
+				"Cpuset mempolicy rebind incomplete.\n");
+			break;
+		}
+		mm = get_task_mm(p);
+		if (!mm)
+			continue;
+		mmarray[(*ntasks)++] = mm;
+	}
+	cgroup_iter_end(cs->css.cgroup, &it);
+	read_unlock(&tasklist_lock);
+	return mmarray;
+}
+
+/*
+ * Decrements the reference count to each mm in mmarray and frees the memory
+ * allocated for mmarray.
+ *
+ * To be used in conjunction with get_cpuset_mm_array().
+ */
+static void put_cpuset_mm_array(struct mm_struct **mmarray, int ntasks)
+{
+	while (ntasks-- > 0)
+		mmput(mmarray[ntasks]);
+	kfree(mmarray);
+}
+
 static inline int started_after_time(struct task_struct *t1,
 				     struct timespec *time,
 				     struct task_struct *t2)
@@ -915,13 +988,10 @@ static int update_nodemask(struct cpuset *cs, char *buf)
 {
 	struct cpuset trialcs;
 	nodemask_t oldmem;
-	struct task_struct *p;
 	struct mm_struct **mmarray;
-	int i, n, ntasks;
+	int i, n;
 	int migrate;
-	int fudge;
 	int retval;
-	struct cgroup_iter it;
 
 	/*
 	 * top_cpuset.mems_allowed tracks node_stats[N_HIGH_MEMORY];
@@ -963,50 +1033,12 @@ static int update_nodemask(struct cpuset *cs, char *buf)
 	mutex_unlock(&callback_mutex);
 
 	cpuset_being_rebound = cs;		/* causes mpol_copy() rebind */
-
-	fudge = 10;				/* spare mmarray[] slots */
-	fudge += cpus_weight(cs->cpus_allowed);	/* imagine one fork-bomb/cpu */
 	retval = -ENOMEM;
-
-	/*
-	 * Allocate mmarray[] to hold mm reference for each task
-	 * in cpuset cs.  Can't kmalloc GFP_KERNEL while holding
-	 * tasklist_lock.  We could use GFP_ATOMIC, but with a
-	 * few more lines of code, we can retry until we get a big
-	 * enough mmarray[] w/o using GFP_ATOMIC.
-	 */
-	while (1) {
-		ntasks = cgroup_task_count(cs->css.cgroup);  /* guess */
-		ntasks += fudge;
-		mmarray = kmalloc(ntasks * sizeof(*mmarray), GFP_KERNEL);
-		if (!mmarray)
-			goto done;
-		read_lock(&tasklist_lock);		/* block fork */
-		if (cgroup_task_count(cs->css.cgroup) <= ntasks)
-			break;				/* got enough */
-		read_unlock(&tasklist_lock);		/* try again */
-		kfree(mmarray);
-	}
-
-	n = 0;
-
-	/* Load up mmarray[] with mm reference for each task in cpuset. */
-	cgroup_iter_start(cs->css.cgroup, &it);
-	while ((p = cgroup_iter_next(cs->css.cgroup, &it))) {
-		struct mm_struct *mm;
-
-		if (n >= ntasks) {
-			printk(KERN_WARNING
-				"Cpuset mempolicy rebind incomplete.\n");
-			break;
-		}
-		mm = get_task_mm(p);
-		if (!mm)
-			continue;
-		mmarray[n++] = mm;
-	}
-	cgroup_iter_end(cs->css.cgroup, &it);
-	read_unlock(&tasklist_lock);
+	mmarray = get_cpuset_mm_array(cs, &n);
+	if (!mmarray)
+		goto done;
+	if (!n)
+		goto done_success;
 
 	/*
 	 * Now that we've dropped the tasklist spinlock, we can
@@ -1028,12 +1060,12 @@ static int update_nodemask(struct cpuset *cs, char *buf)
 		mpol_rebind_mm(mm, &cs->mems_allowed);
 		if (migrate)
 			cpuset_migrate_mm(mm, &oldmem, &cs->mems_allowed);
-		mmput(mm);
 	}
 
 	/* We're done rebinding vma's to this cpusets new mems_allowed. */
-	kfree(mmarray);
 	cpuset_being_rebound = NULL;
+done_success:
+	put_cpuset_mm_array(mmarray, n);
 	retval = 0;
 done:
 	return retval;

^ permalink raw reply	[flat|nested] 98+ messages in thread

end of thread, other threads:[~2007-10-31  0:29 UTC | newest]

Thread overview: 98+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-10-25 22:54 [patch 1/2] cpusets: extract mmarray loading from update_nodemask David Rientjes
2007-10-25 22:54 ` [patch 2/2] cpusets: add interleave_over_allowed option David Rientjes
2007-10-25 23:37   ` Christoph Lameter
2007-10-25 23:56     ` David Rientjes
2007-10-26  0:28       ` Christoph Lameter
2007-10-26  1:55         ` Paul Jackson
2007-10-26  2:11           ` David Rientjes
2007-10-26  2:29             ` Paul Jackson
2007-10-26  2:45               ` David Rientjes
2007-10-26  3:14                 ` Paul Jackson
2007-10-26  3:58                   ` David Rientjes
2007-10-26  4:34                     ` Paul Jackson
2007-10-26 15:37                     ` Lee Schermerhorn
2007-10-26 17:04                       ` Paul Jackson
2007-10-26 17:28                         ` Lee Schermerhorn
2007-10-26 20:21                         ` Michael Kerrisk
2007-10-26 20:25                           ` Paul Jackson
2007-10-26 20:33                             ` Michael Kerrisk
2007-10-26 15:30             ` Lee Schermerhorn
2007-10-26 18:46               ` David Rientjes
2007-10-26 19:00                 ` Paul Jackson
2007-10-26 20:45                   ` David Rientjes
2007-10-26 21:05                     ` Christoph Lameter
2007-10-26 21:08                       ` David Rientjes
2007-10-26 21:12                         ` Christoph Lameter
2007-10-26 21:15                           ` David Rientjes
2007-10-26 21:13                     ` Lee Schermerhorn
2007-10-26 21:17                       ` Christoph Lameter
2007-10-26 21:26                         ` Lee Schermerhorn
2007-10-26 21:37                           ` Christoph Lameter
2007-10-29 15:00                             ` Lee Schermerhorn
2007-10-29 17:33                               ` Paul Jackson
2007-10-29 17:46                                 ` Lee Schermerhorn
2007-10-29 20:35                               ` Christoph Lameter
2007-10-26 21:18                       ` David Rientjes
2007-10-26 21:31                         ` Lee Schermerhorn
2007-10-26 21:39                           ` David Rientjes
2007-10-27  1:07                             ` Paul Jackson
2007-10-27  1:26                               ` Christoph Lameter
2007-10-27  2:41                                 ` Paul Jackson
2007-10-27  2:50                                   ` Christoph Lameter
2007-10-27  5:16                                     ` Paul Jackson
2007-10-27  6:07                                       ` Christoph Lameter
2007-10-27  8:36                                         ` Paul Jackson
2007-10-27 17:47                                           ` Christoph Lameter
2007-10-27 20:59                                             ` Paul Jackson
2007-10-27 17:50                                   ` David Rientjes
2007-10-27 23:19                                     ` Paul Jackson
2007-10-28 18:19                                       ` David Rientjes
2007-10-28 23:46                                         ` Paul Jackson
2007-10-29  1:04                                           ` David Rientjes
2007-10-29  4:27                                             ` Paul Jackson
2007-10-29  4:47                                               ` David Rientjes
2007-10-29  5:45                                                 ` Paul Jackson
2007-10-29  7:00                                                   ` David Rientjes
2007-10-29  7:26                                                     ` Paul Jackson
2007-10-30 22:53                                                       ` David Rientjes
2007-10-30 23:17                                                         ` Paul Jackson
2007-10-30 23:25                                                           ` David Rientjes
2007-10-31  0:03                                                             ` Paul Jackson
2007-10-31  0:05                                                             ` Paul Jackson
2007-10-29  7:15                                                 ` Paul Jackson
2007-10-30 23:12                                                   ` David Rientjes
2007-10-30 23:44                                                     ` Paul Jackson
2007-10-30 23:53                                                       ` David Rientjes
2007-10-31  0:29                                                         ` Paul Jackson
2007-10-29 16:54                                       ` Lee Schermerhorn
2007-10-29 19:40                                         ` Paul Jackson
2007-10-29 19:45                                         ` Paul Jackson
2007-10-29 19:57                                         ` Paul Jackson
2007-10-29 20:02                                         ` Paul Jackson
2007-10-27 17:45                               ` David Rientjes
2007-10-27 21:22                                 ` Paul Jackson
2007-10-29 15:10                             ` Lee Schermerhorn
2007-10-29 18:41                               ` Paul Jackson
2007-10-29 19:01                                 ` Lee Schermerhorn
2007-10-30 23:17                                 ` David Rientjes
2007-10-31  0:03                                   ` Paul Jackson
2007-10-30 22:57                               ` David Rientjes
2007-10-30 23:46                                 ` Paul Jackson
2007-10-26 20:43                 ` Lee Schermerhorn
2007-10-26 15:18         ` Lee Schermerhorn
2007-10-26 17:36           ` Christoph Lameter
2007-10-26 18:45           ` David Rientjes
2007-10-26 19:02             ` Paul Jackson
2007-10-27 19:16             ` David Rientjes
2007-10-29 16:23               ` Lee Schermerhorn
2007-10-29 17:35                 ` Andi Kleen
2007-10-29 19:35                 ` Paul Jackson
2007-10-29 20:36                   ` Christoph Lameter
2007-10-29 21:08                   ` Andi Kleen
2007-10-29 22:48                     ` Paul Jackson
2007-10-30 19:47                     ` Paul Jackson
2007-10-30 20:20                       ` Lee Schermerhorn
2007-10-30 20:26                         ` Paul Jackson
2007-10-30 20:27                       ` Andi Kleen
2007-10-26  1:13   ` Paul Jackson
2007-10-26  1:30     ` David Rientjes

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox