From: Daisuke Nishimura <nishimura@mxp.nes.nec.co.jp>
To: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Cc: linux-mm@kvack.org, balbir@linux.vnet.ibm.com,
nishimura@mxp.nes.nec.co.jp
Subject: [PATCH -mm 1/5] memcg: replace res_counter
Date: Fri, 17 Oct 2008 19:56:01 +0900 [thread overview]
Message-ID: <20081017195601.0b9abda1.nishimura@mxp.nes.nec.co.jp> (raw)
In-Reply-To: <20081017194804.fce28258.nishimura@mxp.nes.nec.co.jp>
For mem+swap controller, we'll use special counter which has 2 values and
2 limit. Before doing that, replace current res_counter with new mem_counter.
This patch doen't have much meaning other than for clean up before mem+swap
controller. New mem_counter's counter is "unsigned long" and account resource by
# of pages. (I think "unsigned long" is safe under 32bit machines when we count
resource by # of pages rather than bytes.) No changes in user interface.
User interface is in "bytes".
Using "unsigned long long", we have to be nervous to read to temporal value
without lock.
Changelog: v2 -> v3
- fix trivial bugs
- rebased on memcg-update-v7
Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Signed-off-by: Daisuke Nishimura <nishimura@mxp.nes.nec.co.jp>
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index d5b492f..e1c20d2 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -17,10 +17,9 @@
* GNU General Public License for more details.
*/
-#include <linux/res_counter.h>
+#include <linux/mm.h>
#include <linux/memcontrol.h>
#include <linux/cgroup.h>
-#include <linux/mm.h>
#include <linux/smp.h>
#include <linux/page-flags.h>
#include <linux/backing-dev.h>
@@ -116,12 +115,21 @@ struct mem_cgroup_lru_info {
* no reclaim occurs from a cgroup at it's low water mark, this is
* a feature that will be implemented much later in the future.
*/
+struct mem_counter {
+ unsigned long pages;
+ unsigned long pages_limit;
+ unsigned long max_pages;
+ unsigned long failcnt;
+ spinlock_t lock;
+};
+
+
struct mem_cgroup {
struct cgroup_subsys_state css;
/*
* the counter to account for memory usage
*/
- struct res_counter res;
+ struct mem_counter res;
/*
* Per cgroup active and inactive list, similar to the
* per zone LRU lists.
@@ -158,6 +166,14 @@ pcg_default_flags[NR_CHARGE_TYPE] = {
0, /* FORCE */
};
+/* Private File ID for memory resource controller's interface */
+enum {
+ MEMCG_FILE_PAGE_LIMIT,
+ MEMCG_FILE_PAGE_USAGE,
+ MEMCG_FILE_PAGE_MAX_USAGE,
+ MEMCG_FILE_FAILCNT,
+};
+
/*
* Always modified under lru lock. Then, not necessary to preempt_disable()
*/
@@ -237,6 +253,81 @@ struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
struct mem_cgroup, css);
}
+/*
+ * counter for memory resource accounting.
+ */
+static void mem_counter_init(struct mem_cgroup *mem)
+{
+ memset(&mem->res, 0, sizeof(mem->res));
+ mem->res.pages_limit = ~0UL;
+ spin_lock_init(&mem->res.lock);
+}
+
+static int mem_counter_charge(struct mem_cgroup *mem, long num)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&mem->res.lock, flags);
+ if (mem->res.pages + num > mem->res.pages_limit)
+ goto busy_out;
+ mem->res.pages += num;
+ if (mem->res.pages > mem->res.max_pages)
+ mem->res.max_pages = mem->res.pages;
+ spin_unlock_irqrestore(&mem->res.lock, flags);
+ return 0;
+busy_out:
+ mem->res.failcnt++;
+ spin_unlock_irqrestore(&mem->res.lock, flags);
+ return -EBUSY;
+}
+
+static void mem_counter_uncharge_page(struct mem_cgroup *mem, long num)
+{
+ unsigned long flags;
+ spin_lock_irqsave(&mem->res.lock, flags);
+ mem->res.pages -= num;
+ spin_unlock_irqrestore(&mem->res.lock, flags);
+}
+
+static int mem_counter_set_pages_limit(struct mem_cgroup *mem,
+ unsigned long num)
+{
+ unsigned long flags;
+ int ret = -EBUSY;
+
+ spin_lock_irqsave(&mem->res.lock, flags);
+ if (mem->res.pages < num) {
+ mem->res.pages_limit = num;
+ ret = 0;
+ }
+ spin_unlock_irqrestore(&mem->res.lock, flags);
+ return ret;
+}
+
+static int mem_counter_check_under_pages_limit(struct mem_cgroup *mem)
+{
+ if (mem->res.pages < mem->res.pages_limit)
+ return 1;
+ return 0;
+}
+
+static void mem_counter_reset(struct mem_cgroup *mem, int member)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&mem->res.lock, flags);
+ switch (member) {
+ case MEMCG_FILE_PAGE_MAX_USAGE:
+ mem->res.max_pages = 0;
+ break;
+ case MEMCG_FILE_FAILCNT:
+ mem->res.failcnt = 0;
+ break;
+ }
+ spin_unlock_irqrestore(&mem->res.lock, flags);
+}
+
+
static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz,
struct page_cgroup *pc)
{
@@ -368,7 +459,7 @@ int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
* usage is recorded in bytes. But, here, we assume the number of
* physical pages can be represented by "long" on any arch.
*/
- total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
+ total = (long) (mem->res.pages) + 1L;
rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
return (int)((rss * 100L) / total);
}
@@ -692,7 +783,7 @@ static int __mem_cgroup_try_charge(struct mm_struct *mm,
}
- while (unlikely(res_counter_charge(&mem->res, PAGE_SIZE))) {
+ while (unlikely(mem_counter_charge(mem, 1))) {
if (!(gfp_mask & __GFP_WAIT))
goto nomem;
@@ -706,7 +797,7 @@ static int __mem_cgroup_try_charge(struct mm_struct *mm,
* Check the limit again to see if the reclaim reduced the
* current usage of the cgroup before giving up
*/
- if (res_counter_check_under_limit(&mem->res))
+ if (mem_counter_check_under_pages_limit(mem))
continue;
if (!nr_retries--) {
@@ -760,7 +851,7 @@ static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
*/
if (unlikely(PageCgroupUsed(pc))) {
unlock_page_cgroup(pc);
- res_counter_uncharge(&mem->res, PAGE_SIZE);
+ mem_counter_uncharge_page(mem, 1);
css_put(&mem->css);
return;
}
@@ -841,7 +932,7 @@ static int mem_cgroup_move_account(struct page_cgroup *pc,
if (spin_trylock(&to_mz->lru_lock)) {
__mem_cgroup_remove_list(from_mz, pc);
- res_counter_uncharge(&from->res, PAGE_SIZE);
+ mem_counter_uncharge_page(from, PAGE_SIZE);
pc->mem_cgroup = to;
__mem_cgroup_add_list(to_mz, pc, false);
ret = 0;
@@ -888,7 +979,7 @@ static int mem_cgroup_move_parent(struct page_cgroup *pc,
css_put(&parent->css);
/* uncharge if move fails */
if (ret)
- res_counter_uncharge(&parent->res, PAGE_SIZE);
+ mem_counter_uncharge_page(parent, 1);
return ret;
}
@@ -1005,7 +1096,7 @@ void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
return;
if (!mem)
return;
- res_counter_uncharge(&mem->res, PAGE_SIZE);
+ mem_counter_uncharge_page(mem, 1);
css_put(&mem->css);
}
@@ -1042,7 +1133,7 @@ __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
* We must uncharge here because "reuse" can occur just after we
* unlock this.
*/
- res_counter_uncharge(&mem->res, PAGE_SIZE);
+ mem_counter_uncharge_page(mem, 1);
unlock_page_cgroup(pc);
release_page_cgroup(pc);
return;
@@ -1174,7 +1265,7 @@ int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
do {
progress = try_to_free_mem_cgroup_pages(mem, gfp_mask);
- progress += res_counter_check_under_limit(&mem->res);
+ progress += mem_counter_check_under_pages_limit(mem);
} while (!progress && --retry);
css_put(&mem->css);
@@ -1189,8 +1280,12 @@ int mem_cgroup_resize_limit(struct mem_cgroup *memcg, unsigned long long val)
int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
int progress;
int ret = 0;
+ unsigned long new_lim = (unsigned long)(val >> PAGE_SHIFT);
- while (res_counter_set_limit(&memcg->res, val)) {
+ if (val & (PAGE_SIZE-1))
+ new_lim += 1;
+
+ while (mem_counter_set_pages_limit(memcg, new_lim)) {
if (signal_pending(current)) {
ret = -EINTR;
break;
@@ -1273,7 +1368,7 @@ static int mem_cgroup_force_empty(struct mem_cgroup *mem)
shrink = 0;
move_account:
- while (mem->res.usage > 0) {
+ while (mem->res.pages > 0) {
ret = -EBUSY;
if (atomic_read(&mem->css.cgroup->count) > 0)
goto out;
@@ -1316,7 +1411,7 @@ try_to_free:
}
/* try to free all pages in this cgroup */
shrink = 1;
- while (nr_retries && mem->res.usage > 0) {
+ while (nr_retries && mem->res.pages > 0) {
int progress;
progress = try_to_free_mem_cgroup_pages(mem,
GFP_HIGHUSER_MOVABLE);
@@ -1325,7 +1420,7 @@ try_to_free:
}
/* try move_account...there may be some *locked* pages. */
- if (mem->res.usage)
+ if (mem->res.pages)
goto move_account;
ret = 0;
goto out;
@@ -1333,13 +1428,43 @@ try_to_free:
static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
{
- return res_counter_read_u64(&mem_cgroup_from_cont(cont)->res,
- cft->private);
+ unsigned long long ret;
+ struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
+
+ switch (cft->private) {
+ case MEMCG_FILE_PAGE_LIMIT:
+ ret = (unsigned long long)mem->res.pages_limit << PAGE_SHIFT;
+ break;
+ case MEMCG_FILE_PAGE_USAGE:
+ ret = (unsigned long long)mem->res.pages << PAGE_SHIFT;
+ break;
+ case MEMCG_FILE_PAGE_MAX_USAGE:
+ ret = (unsigned long long)mem->res.max_pages << PAGE_SHIFT;
+ break;
+ case MEMCG_FILE_FAILCNT:
+ ret = (unsigned long long)mem->res.failcnt;
+ break;
+ default:
+ BUG();
+ }
+ return ret;
}
/*
* The user of this function is...
* RES_LIMIT.
*/
+static int call_memparse(const char *buf, unsigned long long *val)
+{
+ char *end;
+
+ *val = memparse((char *)buf, &end);
+ if (*end != '\0')
+ return -EINVAL;
+ *val = PAGE_ALIGN(*val);
+ return 0;
+}
+
+
static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
const char *buffer)
{
@@ -1348,9 +1473,9 @@ static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
int ret;
switch (cft->private) {
- case RES_LIMIT:
+ case MEMCG_FILE_PAGE_LIMIT:
/* This function does all necessary parse...reuse it */
- ret = res_counter_memparse_write_strategy(buffer, &val);
+ ret = call_memparse(buffer, &val);
if (!ret)
ret = mem_cgroup_resize_limit(memcg, val);
break;
@@ -1367,12 +1492,12 @@ static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
mem = mem_cgroup_from_cont(cont);
switch (event) {
- case RES_MAX_USAGE:
- res_counter_reset_max(&mem->res);
- break;
- case RES_FAILCNT:
- res_counter_reset_failcnt(&mem->res);
+ case MEMCG_FILE_PAGE_MAX_USAGE:
+ case MEMCG_FILE_FAILCNT:
+ mem_counter_reset(mem, event);
break;
+ default:
+ BUG();
}
return 0;
}
@@ -1436,24 +1561,24 @@ static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
static struct cftype mem_cgroup_files[] = {
{
.name = "usage_in_bytes",
- .private = RES_USAGE,
+ .private = MEMCG_FILE_PAGE_USAGE,
.read_u64 = mem_cgroup_read,
},
{
.name = "max_usage_in_bytes",
- .private = RES_MAX_USAGE,
+ .private = MEMCG_FILE_PAGE_MAX_USAGE,
.trigger = mem_cgroup_reset,
.read_u64 = mem_cgroup_read,
},
{
.name = "limit_in_bytes",
- .private = RES_LIMIT,
+ .private = MEMCG_FILE_PAGE_LIMIT,
.write_string = mem_cgroup_write,
.read_u64 = mem_cgroup_read,
},
{
.name = "failcnt",
- .private = RES_FAILCNT,
+ .private = MEMCG_FILE_FAILCNT,
.trigger = mem_cgroup_reset,
.read_u64 = mem_cgroup_read,
},
@@ -1578,7 +1703,7 @@ mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
return ERR_PTR(-ENOMEM);
}
- res_counter_init(&mem->res);
+ mem_counter_init(mem);
for_each_node_state(node, N_POSSIBLE)
if (alloc_mem_cgroup_per_zone_info(mem, node))
--
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>
next prev parent reply other threads:[~2008-10-17 10:56 UTC|newest]
Thread overview: 60+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-10-17 10:48 [RFC][PATCH -mm 0/5] mem+swap resource controller(trial patch) Daisuke Nishimura
2008-10-17 10:56 ` Daisuke Nishimura [this message]
2008-10-20 19:53 ` [PATCH -mm 1/5] memcg: replace res_counter Paul Menage
2008-10-21 1:14 ` KAMEZAWA Hiroyuki
2008-10-21 1:29 ` Paul Menage
2008-10-21 1:49 ` KAMEZAWA Hiroyuki
2008-10-21 2:15 ` Paul Menage
2008-10-21 2:50 ` KAMEZAWA Hiroyuki
2008-10-21 2:20 ` Paul Menage
2008-10-21 3:03 ` KAMEZAWA Hiroyuki
2008-10-21 6:30 ` Paul Menage
2008-10-21 5:30 ` Balbir Singh
2008-10-21 5:39 ` KAMEZAWA Hiroyuki
2008-10-21 6:20 ` [memcg BUG] unable to handle kernel NULL pointer derefence at 00000000 Li Zefan
2008-10-21 6:25 ` KAMEZAWA Hiroyuki
2008-10-21 6:28 ` Li Zefan
2008-10-21 6:38 ` Daisuke Nishimura
2008-10-21 6:54 ` KAMEZAWA Hiroyuki
2008-10-21 7:04 ` Li Zefan
2008-10-21 7:16 ` KAMEZAWA Hiroyuki
2008-10-21 7:21 ` Li Zefan
2008-10-21 8:18 ` KAMEZAWA Hiroyuki
2008-10-21 8:34 ` Mel Gorman
2008-10-21 8:38 ` KAMEZAWA Hiroyuki
2008-10-21 8:35 ` Li Zefan
2008-10-21 8:36 ` KAMEZAWA Hiroyuki
2008-10-21 8:57 ` KAMEZAWA Hiroyuki
2008-10-21 9:13 ` Li Zefan
2008-10-21 9:25 ` KAMEZAWA Hiroyuki
2008-10-21 9:54 ` Li Zefan
2008-10-21 10:14 ` KAMEZAWA Hiroyuki
2008-10-21 10:57 ` Li Zefan
2008-10-21 11:00 ` KAMEZAWA Hiroyuki
2008-10-21 11:09 ` KAMEZAWA Hiroyuki
2008-10-21 11:13 ` KAMEZAWA Hiroyuki
2008-10-21 11:19 ` Ingo Molnar
2008-10-21 11:23 ` KAMEZAWA Hiroyuki
2008-10-21 11:28 ` Ingo Molnar
2008-10-21 11:32 ` KAMEZAWA Hiroyuki
2008-10-21 11:38 ` Ingo Molnar
2008-10-22 2:13 ` Daisuke Nishimura
2008-10-22 2:31 ` KAMEZAWA Hiroyuki
2008-10-21 11:29 ` Balbir Singh
2008-10-21 11:34 ` KAMEZAWA Hiroyuki
2008-10-21 12:00 ` KAMEZAWA Hiroyuki
2008-10-21 12:14 ` Balbir Singh
2008-10-21 13:09 ` KAMEZAWA Hiroyuki
2008-10-21 13:25 ` Balbir Singh
2008-10-21 13:34 ` Balbir Singh
2008-10-21 13:44 ` [memcg BUG] unable to handle kernel NULL pointer derefence at00000000 亀澤 寛之
2008-10-21 10:58 ` [memcg BUG] unable to handle kernel NULL pointer derefence at 00000000 Balbir Singh
2008-10-21 9:33 ` Daisuke Nishimura
2008-10-21 9:41 ` KAMEZAWA Hiroyuki
2008-10-21 10:15 ` Daisuke Nishimura
2008-10-17 10:59 ` [PATCH -mm 2/5] memcg: mem_cgroup private ID Daisuke Nishimura
2008-10-17 11:01 ` [PATCH -mm 3/5] memcg: mem+swap controller Kconfig Daisuke Nishimura, KAMEZAWA Hiroyuki
2008-10-17 11:04 ` [PATCH -mm 4/5] memcg: mem+swap counter Daisuke Nishimura
2008-10-17 11:06 ` [PATCH -mm 5/5] memcg: mem+swap accounting Daisuke Nishimura
2008-10-20 0:24 ` [RFC][PATCH -mm 0/5] mem+swap resource controller(trial patch) KAMEZAWA Hiroyuki
2008-10-20 2:53 ` Daisuke Nishimura
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=20081017195601.0b9abda1.nishimura@mxp.nes.nec.co.jp \
--to=nishimura@mxp.nes.nec.co.jp \
--cc=balbir@linux.vnet.ibm.com \
--cc=kamezawa.hiroyu@jp.fujitsu.com \
--cc=linux-mm@kvack.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