All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 2/2] Make res_counter hierarchical
@ 2008-03-07 15:32 ` Pavel Emelyanov
  0 siblings, 0 replies; 48+ messages in thread
From: Pavel Emelyanov @ 2008-03-07 15:32 UTC (permalink / raw)
  To: Balbir Singh, KAMEZAWA Hiroyuki, Paul Menage; +Cc: Linux Containers, Linux MM

This allows us two things basically:

1. If the subgroup has the limit higher than its parent has
   then the one will get more memory than allowed.
2. When we will need to account for a resource in more than
   one place, we'll be able to use this technics.

   Look, consider we have a memory limit and swap limit. The
   memory limit is the limit for the sum of RSS, page cache
   and swap usage. To account for this gracefuly, we'll set
   two counters:

	   res_counter mem_counter;
	   res_counter swap_counter;

   attach mm to the swap one

	   mm->mem_cnt = &swap_counter;

   and make the swap_counter be mem's child. That's it. If we
   want hierarchical support, then the tree will look like this:

   mem_counter_top
    swap_counter_top <- mm_struct living at top
     mem_counter_sub
      swap_counter_sub <- mm_struct living at sub

Signed-off-by: Pavel Emelyanov <xemul-GEFAQzZX7r8dnm+yROfE0A@public.gmane.org>

---
 include/linux/res_counter.h |   11 ++++++++++-
 kernel/res_counter.c        |   36 +++++++++++++++++++++++++++++-------
 mm/memcontrol.c             |    9 ++++++---
 3 files changed, 45 insertions(+), 11 deletions(-)

diff --git a/include/linux/res_counter.h b/include/linux/res_counter.h
index 2c4deb5..a27105e 100644
--- a/include/linux/res_counter.h
+++ b/include/linux/res_counter.h
@@ -41,6 +41,10 @@ struct res_counter {
 	 * the routines below consider this to be IRQ-safe
 	 */
 	spinlock_t lock;
+	/*
+	 * the parent counter. used for hierarchical resource accounting
+	 */
+	struct res_counter *parent;
 };
 
 /**
@@ -80,7 +84,12 @@ enum {
  * helpers for accounting
  */
 
-void res_counter_init(struct res_counter *counter);
+/*
+ * the parent pointer is set only once - during the counter
+ * initialization. caller then must itself provide that this
+ * pointer is valid during the new counter lifetime
+ */
+void res_counter_init(struct res_counter *counter, struct res_counter *parent);
 
 /*
  * charge - try to consume more resource.
diff --git a/kernel/res_counter.c b/kernel/res_counter.c
index f1f20c2..046f6f4 100644
--- a/kernel/res_counter.c
+++ b/kernel/res_counter.c
@@ -13,10 +13,11 @@
 #include <linux/res_counter.h>
 #include <linux/uaccess.h>
 
-void res_counter_init(struct res_counter *counter)
+void res_counter_init(struct res_counter *counter, struct res_counter *parent)
 {
 	spin_lock_init(&counter->lock);
 	counter->limit = (unsigned long long)LLONG_MAX;
+	counter->parent = parent;
 }
 
 int res_counter_charge_locked(struct res_counter *counter, unsigned long val)
@@ -36,10 +37,26 @@ int res_counter_charge(struct res_counter *counter, unsigned long val)
 {
 	int ret;
 	unsigned long flags;
+	struct res_counter *c, *unroll_c;
+
+	local_irq_save(flags);
+	for (c = counter; c != NULL; c = c->parent) {
+		spin_lock(&c->lock);
+		ret = res_counter_charge_locked(c, val);
+		spin_unlock(&c->lock);
+		if (ret < 0)
+			goto unroll;
+	}
+	local_irq_restore(flags);
+	return 0;
 
-	spin_lock_irqsave(&counter->lock, flags);
-	ret = res_counter_charge_locked(counter, val);
-	spin_unlock_irqrestore(&counter->lock, flags);
+unroll:
+	for (unroll_c = counter; unroll_c != c; unroll_c = unroll_c->parent) {
+		spin_lock(&unroll_c->lock);
+		res_counter_uncharge_locked(unroll_c, val);
+		spin_unlock(&unroll_c->lock);
+	}
+	local_irq_restore(flags);
 	return ret;
 }
 
@@ -54,10 +71,15 @@ void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val)
 void res_counter_uncharge(struct res_counter *counter, unsigned long val)
 {
 	unsigned long flags;
+	struct res_counter *c;
 
-	spin_lock_irqsave(&counter->lock, flags);
-	res_counter_uncharge_locked(counter, val);
-	spin_unlock_irqrestore(&counter->lock, flags);
+	local_irq_save(flags);
+	for (c = counter; c != NULL; c = c->parent) {
+		spin_lock(&c->lock);
+		res_counter_uncharge_locked(c, val);
+		spin_unlock(&c->lock);
+	}
+	local_irq_restore(flags);
 }
 
 
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index e5c741a..61db79c 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -976,19 +976,22 @@ static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
 static struct cgroup_subsys_state *
 mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
 {
-	struct mem_cgroup *mem;
+	struct mem_cgroup *mem, *parent;
 	int node;
 
 	if (unlikely((cont->parent) == NULL)) {
 		mem = &init_mem_cgroup;
 		init_mm.mem_cgroup = mem;
-	} else
+		parent = NULL;
+	} else {
 		mem = kzalloc(sizeof(struct mem_cgroup), GFP_KERNEL);
+		parent = mem_cgroup_from_cont(cont->parent);
+	}
 
 	if (mem == NULL)
 		return ERR_PTR(-ENOMEM);
 
-	res_counter_init(&mem->res);
+	res_counter_init(&mem->res, parent ? &parent->res : NULL);
 
 	memset(&mem->info, 0, sizeof(mem->info));
 
-- 
1.5.3.4

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

end of thread, other threads:[~2008-04-03 12:26 UTC | newest]

Thread overview: 48+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-03-07 15:32 [PATCH 2/2] Make res_counter hierarchical Pavel Emelyanov
2008-03-07 15:32 ` Pavel Emelyanov
     [not found] ` <47D16004.7050204-GEFAQzZX7r8dnm+yROfE0A@public.gmane.org>
2008-03-08  4:45   ` KAMEZAWA Hiroyuki
2008-03-08  4:45     ` KAMEZAWA Hiroyuki
     [not found]     ` <20080308134514.434f38f4.kamezawa.hiroyu-+CUm20s59erQFUHtdCDX3A@public.gmane.org>
2008-03-11  8:15       ` Pavel Emelyanov
2008-03-11  8:15         ` Pavel Emelyanov
     [not found]         ` <47D63FBC.1010805-GEFAQzZX7r8dnm+yROfE0A@public.gmane.org>
2008-03-11  8:32           ` KAMEZAWA Hiroyuki
2008-03-11  8:32             ` KAMEZAWA Hiroyuki
     [not found]             ` <20080311173225.937935eb.kamezawa.hiroyu-+CUm20s59erQFUHtdCDX3A@public.gmane.org>
2008-03-11  8:38               ` Pavel Emelyanov
2008-03-11  8:38                 ` Pavel Emelyanov
     [not found]                 ` <47D6451A.7090807-GEFAQzZX7r8dnm+yROfE0A@public.gmane.org>
2008-03-11  9:03                   ` Daisuke Nishimura
2008-03-11  9:03                     ` Daisuke Nishimura
2008-03-11  9:07                   ` KAMEZAWA Hiroyuki
2008-03-11  9:07                     ` KAMEZAWA Hiroyuki
2008-03-11  8:57           ` Paul Menage
2008-03-11  8:57             ` Paul Menage
     [not found]             ` <6599ad830803110157u71fe6c3cse125d0202610413b-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2008-03-11  9:13               ` KAMEZAWA Hiroyuki
2008-03-11  9:13                 ` KAMEZAWA Hiroyuki
     [not found]                 ` <20080311181325.c0bf6b90.kamezawa.hiroyu-+CUm20s59erQFUHtdCDX3A@public.gmane.org>
2008-03-11  9:11                   ` Paul Menage
2008-03-11  9:11                     ` Paul Menage
     [not found]                     ` <6599ad830803110211u1cb48874l30aa75d21dc2b23-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2008-03-11  9:16                       ` Balbir Singh
2008-03-11  9:16                         ` Balbir Singh
     [not found]                         ` <47D64E0A.3090907-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
2008-03-11  9:39                           ` KAMEZAWA Hiroyuki
2008-03-11  9:39                             ` KAMEZAWA Hiroyuki
     [not found]                             ` <20080311183940.11695e41.kamezawa.hiroyu-+CUm20s59erQFUHtdCDX3A@public.gmane.org>
2008-03-11  9:53                               ` Balbir Singh
2008-03-11  9:53                                 ` Balbir Singh
     [not found]                                 ` <47D65680.4090106-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
2008-03-11 10:03                                   ` KAMEZAWA Hiroyuki
2008-03-11 10:03                                     ` KAMEZAWA Hiroyuki
2008-03-11 15:56                           ` Paul Menage
2008-03-11 15:56                             ` Paul Menage
     [not found]                             ` <6599ad830803110856j5333f032n2e26fb51111a839c-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2008-03-11 15:59                               ` Balbir Singh
2008-03-11 15:59                                 ` Balbir Singh
2008-03-08 19:06   ` Balbir Singh
2008-03-08 19:06     ` Balbir Singh
     [not found]     ` <47D2E3C2.8050402-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
2008-03-11  8:17       ` Pavel Emelyanov
2008-03-11  8:17         ` Pavel Emelyanov
     [not found]         ` <47D64037.3000705-GEFAQzZX7r8dnm+yROfE0A@public.gmane.org>
2008-03-11  8:24           ` Balbir Singh
2008-03-11  8:24             ` Balbir Singh
     [not found]             ` <47D641B8.5000701-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
2008-03-11  8:40               ` Pavel Emelyanov
2008-03-11  8:40                 ` Pavel Emelyanov
2008-03-12 23:05   ` YAMAMOTO Takashi
2008-03-12 23:05     ` YAMAMOTO Takashi
     [not found]     ` <20080312230541.CB9021E703D-Pcsii4f/SVk@public.gmane.org>
2008-03-12 23:36       ` YAMAMOTO Takashi
2008-03-12 23:36         ` YAMAMOTO Takashi
2008-03-13  8:56       ` Pavel Emelyanov
2008-03-13  8:56         ` Pavel Emelyanov
2008-04-02 15:26   ` Balbir Singh
     [not found]     ` <47F3A5BF.1080301-23VcF4HTsmIX0ybBhKVfKdBPR1lH4CV8@public.gmane.org>
2008-04-03 12:26       ` Pavel Emelyanov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.