[rh7,2/2] mm/memcg: fix overcharging in cgroup try_charge() for !cache_charge

Submitted by Andrey Ryabinin on Feb. 26, 2018, 5:44 p.m.


Message ID 20180226174412.18700-2-aryabinin@virtuozzo.com
State New
Series "Series without cover letter"
Headers show

Commit Message

Andrey Ryabinin Feb. 26, 2018, 5:44 p.m.
Normally, try_charge() tries to charge 32 pages, and if succesfull,
puts the rest (32-nr_pages) into stock. So the next try_charge() looks
into the stock only and avoid calling expensive page_counter_try_charge(),
(at least untill stock has some pages).

This stopped to work after commit:

     15fd42f8580f ("mm/memcg: limit page cache in memcg hack")

If cache_charge=false in try_charge() we always call refill_stock() which
undoes stock charge and fallback to slowpath which charges 32 pages and puts
rest into the stock. So we have constantly growing stock and page_counter.
Eventually we hit the limit and call drain_all_stock_async() which uncharges
all accumulated stock pages from page_counter and page_counter goes
back to sane value. But before that happens mem_cgroup_enough_memory() may
read over-charges ->memsw counter and return ENOMEM to user-space.

Signed-off-by: Andrey Ryabinin <aryabinin@virtuozzo.com>
 mm/memcontrol.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

Patch hide | download patch | download mbox

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 50cbcdc38f60..b4d61d72ccbf 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2769,13 +2769,12 @@  retry:
 			goto charge;
-		if (cache_charge && !page_counter_try_charge(
-				&memcg->cache, nr_pages, &counter))
-			goto done;
-		refill_stock(memcg, nr_pages);
-		if (kmem_charge)
-			page_counter_uncharge(&memcg->kmem, nr_pages);
+		if (cache_charge && page_counter_try_charge(
+				&memcg->cache, nr_pages, &counter)) {
+			refill_stock(memcg, nr_pages);
+			goto charge;
+		}
+		goto done;