From: Johannes Weiner <hannes@cmpxchg.org>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Michal Hocko <mhocko@suse.cz>, Hugh Dickins <hughd@google.com>,
Tejun Heo <tj@kernel.org>,
Vladimir Davydov <vdavydov@parallels.com>,
cgroups@vger.kernel.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org
Subject: [patch 01/10] mm: memcontrol: fold mem_cgroup_do_charge()
Date: Thu, 29 May 2014 12:15:53 -0400 [thread overview]
Message-ID: <1401380162-24121-2-git-send-email-hannes@cmpxchg.org> (raw)
In-Reply-To: <1401380162-24121-1-git-send-email-hannes@cmpxchg.org>
This function was split out because mem_cgroup_try_charge() got too
big. But having essentially one sequence of operations arbitrarily
split in half is not good for reworking the code. Fold it back in.
Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>
---
mm/memcontrol.c | 166 ++++++++++++++++++++++----------------------------------
1 file changed, 64 insertions(+), 102 deletions(-)
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 4df733e13727..c3c10ab98355 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2552,80 +2552,6 @@ static int memcg_cpu_hotplug_callback(struct notifier_block *nb,
return NOTIFY_OK;
}
-
-/* See mem_cgroup_try_charge() for details */
-enum {
- CHARGE_OK, /* success */
- CHARGE_RETRY, /* need to retry but retry is not bad */
- CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
- CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
-};
-
-static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
- unsigned int nr_pages, unsigned int min_pages,
- bool invoke_oom)
-{
- unsigned long csize = nr_pages * PAGE_SIZE;
- struct mem_cgroup *mem_over_limit;
- struct res_counter *fail_res;
- unsigned long flags = 0;
- int ret;
-
- ret = res_counter_charge(&memcg->res, csize, &fail_res);
-
- if (likely(!ret)) {
- if (!do_swap_account)
- return CHARGE_OK;
- ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
- if (likely(!ret))
- return CHARGE_OK;
-
- res_counter_uncharge(&memcg->res, csize);
- mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
- flags |= MEM_CGROUP_RECLAIM_NOSWAP;
- } else
- mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
- /*
- * Never reclaim on behalf of optional batching, retry with a
- * single page instead.
- */
- if (nr_pages > min_pages)
- return CHARGE_RETRY;
-
- if (!(gfp_mask & __GFP_WAIT))
- return CHARGE_WOULDBLOCK;
-
- if (gfp_mask & __GFP_NORETRY)
- return CHARGE_NOMEM;
-
- ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
- if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
- return CHARGE_RETRY;
- /*
- * Even though the limit is exceeded at this point, reclaim
- * may have been able to free some pages. Retry the charge
- * before killing the task.
- *
- * Only for regular pages, though: huge pages are rather
- * unlikely to succeed so close to the limit, and we fall back
- * to regular pages anyway in case of failure.
- */
- if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret)
- return CHARGE_RETRY;
-
- /*
- * At task move, charge accounts can be doubly counted. So, it's
- * better to wait until the end of task_move if something is going on.
- */
- if (mem_cgroup_wait_acct_move(mem_over_limit))
- return CHARGE_RETRY;
-
- if (invoke_oom)
- mem_cgroup_oom(mem_over_limit, gfp_mask, get_order(csize));
-
- return CHARGE_NOMEM;
-}
-
/**
* mem_cgroup_try_charge - try charging a memcg
* @memcg: memcg to charge
@@ -2642,7 +2568,11 @@ static int mem_cgroup_try_charge(struct mem_cgroup *memcg,
{
unsigned int batch = max(CHARGE_BATCH, nr_pages);
int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
- int ret;
+ struct mem_cgroup *mem_over_limit;
+ struct res_counter *fail_res;
+ unsigned long nr_reclaimed;
+ unsigned long flags = 0;
+ unsigned long long size;
if (mem_cgroup_is_root(memcg))
goto done;
@@ -2662,44 +2592,76 @@ static int mem_cgroup_try_charge(struct mem_cgroup *memcg,
if (gfp_mask & __GFP_NOFAIL)
oom = false;
-again:
+retry:
if (consume_stock(memcg, nr_pages))
goto done;
- do {
- bool invoke_oom = oom && !nr_oom_retries;
+ size = batch * PAGE_SIZE;
+ if (!res_counter_charge(&memcg->res, size, &fail_res)) {
+ if (!do_swap_account)
+ goto done_restock;
+ if (!res_counter_charge(&memcg->memsw, size, &fail_res))
+ goto done_restock;
+ res_counter_uncharge(&memcg->res, size);
+ mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
+ flags |= MEM_CGROUP_RECLAIM_NOSWAP;
+ } else
+ mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
- /* If killed, bypass charge */
- if (fatal_signal_pending(current))
- goto bypass;
+ if (batch > nr_pages) {
+ batch = nr_pages;
+ goto retry;
+ }
- ret = mem_cgroup_do_charge(memcg, gfp_mask, batch,
- nr_pages, invoke_oom);
- switch (ret) {
- case CHARGE_OK:
- break;
- case CHARGE_RETRY: /* not in OOM situation but retry */
- batch = nr_pages;
- goto again;
- case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
- goto nomem;
- case CHARGE_NOMEM: /* OOM routine works */
- if (!oom || invoke_oom)
- goto nomem;
- nr_oom_retries--;
- break;
- }
- } while (ret != CHARGE_OK);
+ if (!(gfp_mask & __GFP_WAIT))
+ goto nomem;
- if (batch > nr_pages)
- refill_stock(memcg, batch - nr_pages);
-done:
- return 0;
+ if (gfp_mask & __GFP_NORETRY)
+ goto nomem;
+
+ nr_reclaimed = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
+
+ if (mem_cgroup_margin(mem_over_limit) >= batch)
+ goto retry;
+ /*
+ * Even though the limit is exceeded at this point, reclaim
+ * may have been able to free some pages. Retry the charge
+ * before killing the task.
+ *
+ * Only for regular pages, though: huge pages are rather
+ * unlikely to succeed so close to the limit, and we fall back
+ * to regular pages anyway in case of failure.
+ */
+ if (nr_reclaimed && batch <= (1 << PAGE_ALLOC_COSTLY_ORDER))
+ goto retry;
+ /*
+ * At task move, charge accounts can be doubly counted. So, it's
+ * better to wait until the end of task_move if something is going on.
+ */
+ if (mem_cgroup_wait_acct_move(mem_over_limit))
+ goto retry;
+
+ if (fatal_signal_pending(current))
+ goto bypass;
+
+ if (!oom)
+ goto nomem;
+
+ if (nr_oom_retries--)
+ goto retry;
+
+ mem_cgroup_oom(mem_over_limit, gfp_mask, get_order(batch));
nomem:
if (!(gfp_mask & __GFP_NOFAIL))
return -ENOMEM;
bypass:
return -EINTR;
+
+done_restock:
+ if (batch > nr_pages)
+ refill_stock(memcg, batch - nr_pages);
+done:
+ return 0;
}
/**
--
1.9.3
--
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:[~2014-05-29 16:16 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-05-29 16:15 [patch 0/9] mm: memcontrol: naturalize charge lifetime v2 Johannes Weiner
2014-05-29 16:15 ` Johannes Weiner [this message]
2014-06-03 12:38 ` [patch 01/10] mm: memcontrol: fold mem_cgroup_do_charge() Michal Hocko
2014-05-29 16:15 ` [patch 02/10] mm: memcontrol: rearrange charging fast path Johannes Weiner
2014-06-03 12:54 ` Michal Hocko
2014-05-29 16:15 ` [patch 03/10] mm: memcontrol: retry reclaim for oom-disabled and __GFP_NOFAIL charges Johannes Weiner
2014-06-03 13:20 ` Michal Hocko
2014-05-29 16:15 ` [patch 04/10] mm: memcontrol: reclaim at least once for __GFP_NORETRY Johannes Weiner
2014-06-03 13:16 ` Michal Hocko
2014-05-29 16:15 ` [patch 05/10] mm: memcontrol: catch root bypass in move precharge Johannes Weiner
2014-06-03 13:22 ` Michal Hocko
2014-05-29 16:15 ` [patch 06/10] mm: memcontrol: use root_mem_cgroup res_counter Johannes Weiner
2014-06-03 14:11 ` Michal Hocko
2014-05-29 16:15 ` [patch 07/10] mm: memcontrol: remove ordering between pc->mem_cgroup and PageCgroupUsed Johannes Weiner
2014-05-29 16:16 ` [patch 08/10] mm: memcontrol: do not acquire page_cgroup lock for kmem pages Johannes Weiner
2014-05-29 16:16 ` [patch 09/10] mm: memcontrol: rewrite charge API Johannes Weiner
2014-05-29 16:16 ` [patch 10/10] mm: memcontrol: rewrite uncharge API Johannes Weiner
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=1401380162-24121-2-git-send-email-hannes@cmpxchg.org \
--to=hannes@cmpxchg.org \
--cc=akpm@linux-foundation.org \
--cc=cgroups@vger.kernel.org \
--cc=hughd@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@suse.cz \
--cc=tj@kernel.org \
--cc=vdavydov@parallels.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