mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ben Blum <bblum@google.com>
To: linux-kernel@vger.kernel.org,
	containers@lists.linux-foundation.org, akpm@linux-foundation.org,
	serue@us.ibm.com, lizf@cn.fujitsu.com, menage@google.com
Subject: [PATCH 3/3] Quick vmalloc vs kmalloc fix to the case where array size is too large
Date: Fri, 10 Jul 2009 16:02:05 -0700	[thread overview]
Message-ID: <20090710230205.16778.11707.stgit@hastromil.mtv.corp.google.com> (raw)
In-Reply-To: <20090710230043.16778.29656.stgit@hastromil.mtv.corp.google.com>

Quick vmalloc vs kmalloc fix to the case where array size is too large

Separates all pidlist allocation requests to a separate function that judges
based on the requested size whether or not the array needs to be vmalloced or
can be gotten via kmalloc, and similar for kfree/vfree. Should be replaced
entirely with a kernel-wide solution to this general problem.

Depends on cgroup-pidlist-namespace.patch, cgroup-procs.patch

Signed-off-by: Ben Blum <bblum@google.com>

---

 kernel/cgroup.c |   34 ++++++++++++++++++++++++++++------
 1 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/kernel/cgroup.c b/kernel/cgroup.c
index 33d89be..0ed85fa 100644
--- a/kernel/cgroup.c
+++ b/kernel/cgroup.c
@@ -48,6 +48,7 @@
 #include <linux/namei.h>
 #include <linux/smp_lock.h>
 #include <linux/pid_namespace.h>
+#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
 
 #include <asm/atomic.h>
 
@@ -2121,6 +2122,27 @@ int cgroup_scan_tasks(struct cgroup_scanner *scan)
  */
 
 /*
+ * The following two functions "fix" the issue where there are more pids
+ * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
+ * TODO: replace with a kernel-wide solution to this problem
+ */
+#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
+static inline void *pidlist_allocate(int count)
+{
+	if (PIDLIST_TOO_LARGE(count))
+		return vmalloc(count * sizeof(pid_t));
+	else
+		return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
+}
+static inline void pidlist_free(void *p)
+{
+	if (is_vmalloc_addr(p))
+		vfree(p);
+	else
+		kfree(p);
+}
+
+/*
  * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
  * If the new stripped list is sufficiently smaller and there's enough memory
  * to allocate a new buffer, will let go of the unneeded memory. Returns the
@@ -2160,10 +2182,10 @@ static int pidlist_uniq(pid_t **p, int length)
 	 * we'll just stay with what we've got.
 	 */
 	if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
-		newlist = kmalloc(dest * sizeof(pid_t), GFP_KERNEL);
+		newlist = pidlist_allocate(dest);
 		if (newlist) {
 			memcpy(newlist, list, dest * sizeof(pid_t));
-			kfree(list);
+			pidlist_free(list);
 			*p = newlist;
 		}
 	}
@@ -2243,7 +2265,7 @@ static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
 	 * show up until sometime later on.
 	 */
 	length = cgroup_task_count(cgrp);
-	array = kmalloc(length * sizeof(pid_t), GFP_KERNEL);
+	array = pidlist_allocate(length);
 	if (!array)
 		return -ENOMEM;
 	/* now, populate the array */
@@ -2268,11 +2290,11 @@ static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
 	}
 	l = cgroup_pidlist_find(cgrp, type);
 	if (!l) {
-		kfree(array);
+		pidlist_free(array);
 		return -ENOMEM;
 	}
 	/* store array, freeing old if necessary - lock already held */
-	kfree(l->list);
+	pidlist_free(l->list);
 	l->list = array;
 	l->length = length;
 	l->use_count++;
@@ -2433,7 +2455,7 @@ static void cgroup_release_pid_array(struct cgroup_pidlist *l)
 		/* we're the last user if refcount is 0; remove and free */
 		list_del(&l->links);
 		mutex_unlock(&l->owner->pidlist_mutex);
-		kfree(l->list);
+		pidlist_free(l->list);
 		put_pid_ns(l->key.ns);
 		up_write(&l->mutex);
 		kfree(l);


  parent reply	other threads:[~2009-07-10 23:02 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-10 23:01 [PATCH v2 0/3] CGroups: cgroup member list enhancement/fix Ben Blum
2009-07-10 23:01 ` [PATCH 1/3] Adds a read-only "procs" file similar to "tasks" that shows only unique tgids Ben Blum
2009-07-13  3:46   ` Li Zefan
2009-07-13 15:25     ` Benjamin Blum
2009-07-14 18:34   ` Dave Hansen
2009-07-14 21:26     ` Benjamin Blum
2009-07-14 21:49       ` Dave Hansen
2009-07-14 22:55         ` Benjamin Blum
2009-07-15  1:31         ` Li Zefan
2009-07-10 23:02 ` [PATCH 2/3] Ensures correct concurrent opening/reading of pidlists across pid namespaces Ben Blum
2009-07-11 21:59   ` Serge E. Hallyn
2009-07-10 23:02 ` Ben Blum [this message]
2009-07-13  3:03   ` [PATCH 3/3] Quick vmalloc vs kmalloc fix to the case where array size is too large Li Zefan
2009-07-13  6:03     ` KAMEZAWA Hiroyuki
2009-07-13 15:27       ` Benjamin Blum
2009-07-13 23:49         ` KAMEZAWA Hiroyuki
2009-07-14  3:50           ` Paul Menage
2009-07-14  3:53             ` KAMEZAWA Hiroyuki
2009-07-14  4:04               ` Paul Menage
2009-07-14  4:25                 ` KAMEZAWA Hiroyuki
2009-07-14 17:26                   ` Benjamin Blum
2009-07-14 17:28                   ` Paul Menage
2009-07-14 17:47                     ` Dave Hansen
2009-07-14 17:50                       ` Paul Menage
2009-07-14 21:30                         ` Benjamin Blum

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=20090710230205.16778.11707.stgit@hastromil.mtv.corp.google.com \
    --to=bblum@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=containers@lists.linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lizf@cn.fujitsu.com \
    --cc=menage@google.com \
    --cc=serue@us.ibm.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

all inboxes | Powered by JetHome®