Received: by 10.192.165.148 with SMTP id m20csp1738647imm; Thu, 3 May 2018 04:46:31 -0700 (PDT) X-Google-Smtp-Source: AB8JxZoqH/XfyGb6Wl5H3Iu5ssc5qAB9U2D4+mkSBhMehqUqOZcC4kkUFeRZTXOLTXpesVYC4yOy X-Received: by 10.98.82.129 with SMTP id g123mr256000pfb.22.1525347991943; Thu, 03 May 2018 04:46:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525347991; cv=none; d=google.com; s=arc-20160816; b=pCeJH4leFs0aJ3YNUG5x60cPDkhluC0rL/HRexKW8jx75tWu0itwYRknpTfRUHqJRr jdBCpBVplsRxHEwWnxN/r3NEKbg5sdUE6f/3aSlQXZk1485ix6qkRG6rRlJTtfEOznOQ qET5xxfm8eLPsqoaHLf/OWjZFZlDiTCNloGjWLnwPsWlIAnByvwZNG//v3A+Fjtlmlco Dn//Zhq55cVUaxXF/8xRMweODGhpt35zYKXXUwNtWOWf2/g1dfLEIZf2TmQ9SxOZZnlJ jBQyQGC/M60VChQISanU3Wzdf+8lu7FDih3qmCYZKFicknnYEWI/nWyYydLhvnVSSc0N 1tOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:spamdiagnosticmetadata :spamdiagnosticoutput:mime-version:message-id:date:subject:cc:to :from:dkim-signature:dkim-signature:arc-authentication-results; bh=a51DHjtW10mCsrjCszLROq6U6zgAsxkyXOObgFxR348=; b=uTKJ19HNGvb/yVkq0c+pole0Y1H1Oi8sBNLOhFYbhvh95RlB0oNVl8tlx2jA3LFp6n zUGVgNaMKocQ46h+v0thF7TUPAzxAO9lqARZbDP0dIWiVRrwVetDnP7zLN0AJlAaw5zx fFuCERO5E5geWIOkEUIdy6ck6t1vVh4vENQxmpa7sfy3f8OxuYOwGH60sq53wnnfWBpi iaMfvPIReH5GyBVapc41DghpoMbM6Z67FzR27lUph630bPwmdJhdmqLNycK5m+5bEL6+ /n0dL0+Leiv8YMNcWbtwMoMWnqYZ1DjDjDpeBlL6/Y7LOQt8+PMNQuKChyYUzitcKyF3 96Dw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=nmios1n5; dkim=pass header.i=@fb.onmicrosoft.com header.s=selector1-fb-com header.b=ZGN8HFI/; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y8-v6si13545385pli.242.2018.05.03.04.46.17; Thu, 03 May 2018 04:46:31 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=nmios1n5; dkim=pass header.i=@fb.onmicrosoft.com header.s=selector1-fb-com header.b=ZGN8HFI/; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751432AbeECLp2 (ORCPT + 99 others); Thu, 3 May 2018 07:45:28 -0400 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:46990 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751121AbeECLpN (ORCPT ); Thu, 3 May 2018 07:45:13 -0400 Received: from pps.filterd (m0148461.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w43BXPoZ031244; Thu, 3 May 2018 04:45:04 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : mime-version : content-type; s=facebook; bh=a51DHjtW10mCsrjCszLROq6U6zgAsxkyXOObgFxR348=; b=nmios1n5kqlRfqvAa00pOc2wAEr1B8E/6lpzIuManNDSBS9MHNEoQEh8JzCVBaG9Nz5F TAk3m0Yj7x8PVDti/9QOdZMbuUR+Oc+Fh4NWLZejILaRGokRHXRn66wp2IYxueKCThGA ZPYZkyqXaKGtbp4YuSY/p0BvyKkjKBUetLQ= Received: from maileast.thefacebook.com ([199.201.65.23]) by mx0a-00082601.pphosted.com with ESMTP id 2hqye38ay9-1 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT); Thu, 03 May 2018 04:45:03 -0700 Received: from NAM02-BL2-obe.outbound.protection.outlook.com (192.168.183.28) by o365-in.thefacebook.com (192.168.177.25) with Microsoft SMTP Server (TLS) id 14.3.361.1; Thu, 3 May 2018 07:45:00 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.onmicrosoft.com; s=selector1-fb-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=a51DHjtW10mCsrjCszLROq6U6zgAsxkyXOObgFxR348=; b=ZGN8HFI/RaZyf8rjNAkP1DLPmHC36BRaWn6glge3uwhTSC0mAVqTPpJ8WgyUfgS5W/qTkp/CuIpAR9QlaEs/cEUsyBaXtGIIqiy6l6sM0zOszhUKolmrbTKHi2eoQ3h2CRGDoCbl1AdcLJ4f0OALqJQlHAb9GZBG1crcVSGxZHI= Received: from castle.thefacebook.com (2620:10d:c092:200::1:d1b9) by CO1PR15MB1079.namprd15.prod.outlook.com (2a01:111:e400:7b66::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.715.18; Thu, 3 May 2018 11:44:38 +0000 From: Roman Gushchin To: CC: , , Roman Gushchin , Johannes Weiner , Michal Hocko , Vladimir Davydov , Tejun Heo Subject: [PATCH v3 1/2] mm: introduce memory.min Date: Thu, 3 May 2018 12:43:57 +0100 Message-ID: <20180503114358.7952-1-guro@fb.com> X-Mailer: git-send-email 2.14.3 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [2620:10d:c092:200::1:d1b9] X-ClientProxiedBy: AM5PR0202CA0024.eurprd02.prod.outlook.com (2603:10a6:203:69::34) To CO1PR15MB1079.namprd15.prod.outlook.com (2a01:111:e400:7b66::9) X-MS-PublicTrafficType: Email X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(5600026)(4534165)(4627221)(201703031133081)(201702281549075)(2017052603328)(7153060)(7193020);SRVR:CO1PR15MB1079; X-Microsoft-Exchange-Diagnostics: 1;CO1PR15MB1079;3:paiOKpFyQQughDGErfAoK8P5mSI7z5F+l3EVXPUN4GjqBqvWWUXJVHbYMF8JlTR1kcITtlHt6rxt3OcjrUP+EpKa5pF/b+yOJwHFAvTR3zl2RvumHZIAD1YCghDTvS8Wb30KQnLRdl71niEZZwmtTBd7oCuowinZ39qgig94Upbu5fVGEIk9VgrTVEs68ZEaDlkxZVE/tCKW2QQg0wjcJWskvzQ3L/1gsc/1eloQgL71MTshLNP7MZb6otcg7Hda;25:z62OsHEI/+zLwv8xH+FADFNsyFPlIml5r3d9Yl3Mj5KdQf7Bi6Gvq9vhGm3/u2Ndix48qH5mfTqmb1OAJaKBdLZy0UAT+zyCwHROajCTht1AjxJhfuqmzInudazSOpeEJoskTHLvCrHWj6qjs6lZNuAHsbJ9x4l7fF1PDPSrXDOndrll7ABIXFDOmjFrFy+D+BQub0JE1la37TUfpl/YcfcJmRgMJz6z/44teExwhX1wzIIqftznSe+RCMeI4bGx0x4LgfiaB6xnv3AIqjUyEEf3Xqhxq5W76A4R1oMnctEjPZ8Nc42zLSH3WHz925TGgMgccrv4ARbHAhv/cREExQ==;31:kwv6X6XQTR7/3ABgsPbaZd+4+uNUsLjw3h24fJmJ06+XFAp6/bY/wJMeP1lf83kWUofVb+t6J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tVXRyR7bBO0KFPS/P9zSUoRhCJToTWAsuO7gdyzcvXoI= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(85827821059158)(67672495146484); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(8211001083)(6040522)(2401047)(5005006)(8121501046)(10201501046)(3002001)(93006095)(93001095)(3231254)(11241501184)(944501410)(52105095)(6041310)(20161123562045)(20161123560045)(20161123564045)(20161123558120)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(6072148)(201708071742011);SRVR:CO1PR15MB1079;BCL:0;PCL:0;RULEID:;SRVR:CO1PR15MB1079; X-Forefront-PRVS: 066153096A X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(346002)(376002)(39380400002)(396003)(39860400002)(366004)(199004)(189003)(2361001)(59450400001)(97736004)(106356001)(50466002)(16526019)(305945005)(68736007)(4326008)(186003)(2351001)(86362001)(105586002)(2906002)(5660300001)(48376002)(53936002)(53416004)(1076002)(46003)(39060400002)(2616005)(476003)(486006)(52396003)(52116002)(69596002)(47776003)(25786009)(6916009)(81166006)(81156014)(6512007)(6116002)(6506007)(36756003)(6666003)(478600001)(8676002)(386003)(50226002)(51416003)(8936002)(54906003)(316002)(7736002)(6486002)(16586007)(42262002);DIR:OUT;SFP:1102;SCL:1;SRVR:CO1PR15MB1079;H:castle.thefacebook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1;MX:1; Received-SPF: None (protection.outlook.com: fb.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;CO1PR15MB1079;23:TyeosOAZlEBtfB3UihBUAnrxGVsJNmE0G7u+fI8MI?= =?us-ascii?Q?eIjz4FhtJkiNs+8hVPsHIj5jta3dUfCBnvWaYoW0jxs2rTJ5cMRT6j/W+/Kg?= =?us-ascii?Q?IE9ZlnLlnZheZHg6ILwfd4l8MHgjDMOXmok/gkx+vi8hLZvhPV/gwZGE0onb?= =?us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qsmJ1loc59oljrsqoP8bLYW26RaqSNqp1DDJ1YMn21PgoWZEmjdDNGDhO/qY33Fyn3P0GC/uMmSOBmVlhzGBjpbccid9t5/Q7+0dhwh7uZ0JusJ/qNSzAksjT9J8peVv2MyG+zpn9QnKnsf1OOX8=;24:wIh8RqKtTJml5kVruFY2bkAfqUgY6Y85KHb/eSkiaew7cEJtgj4oqQ7rkCSt2Z/B8rYR0p6N+hBXTiIgcbOHdyn1hu3/oY6FJTlH5NE4Vxs= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;CO1PR15MB1079;7:WmuPoetRoqnPVGoG2ZHLcOs3dc/50jKlIeJJbsuKRKnaNCEy+2EnEh8a+kaFWU03TivSRQ9vnKMA7ZGjy/8m1pocH16W4+MsFKznvHb3BZVs3JDLm4//vBjSwMCC3KzWMnZlcQwxsSmRBxst46ktRcS2sOz/7xyj8zDv3qSg9HxoghDcNER9zyTCuPJpI8SRBQoBjV36aAn4jfJ6eUarLJD/EEN5WaaRrd6D7ul2NKyJIEVTk8Caf0AM9k+3Krx4;20:/BezrXrqnkAU/jojaEayFeniDxqyy0YhJpqt4HvGFCpnWOqQLpV1FyOCIjcw0oJ4Y2CVLEk2oEKdBchdLBR1zORIW1jdGYDhIzWrvbs+a4zyKBrpQlMfemLPNkm7b5SGZ2WFxOp++LgJtElfb21MF/o2D5xsM0azK30KENyy1P4= X-MS-Office365-Filtering-Correlation-Id: 6e057853-bff1-4920-637a-08d5b0eb418e X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 May 2018 11:44:38.9571 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6e057853-bff1-4920-637a-08d5b0eb418e X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8ae927fe-1255-47a7-a2af-5f3a069daaa2 X-MS-Exchange-Transport-CrossTenantHeadersStamped: CO1PR15MB1079 X-OriginatorOrg: fb.com X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-05-03_06:,, signatures=0 X-Proofpoint-Spam-Reason: safe X-FB-Internal: Safe Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Memory controller implements the memory.low best-effort memory protection mechanism, which works perfectly in many cases and allows protecting working sets of important workloads from sudden reclaim. But its semantics has a significant limitation: it works only as long as there is a supply of reclaimable memory. This makes it pretty useless against any sort of slow memory leaks or memory usage increases. This is especially true for swapless systems. If swap is enabled, memory soft protection effectively postpones problems, allowing a leaking application to fill all swap area, which makes no sense. The only effective way to guarantee the memory protection in this case is to invoke the OOM killer. It's possible to handle this case in userspace by reacting on MEMCG_LOW events; but there is still a place for a fail-safe in-kernel mechanism to provide stronger guarantees. This patch introduces the memory.min interface for cgroup v2 memory controller. It works very similarly to memory.low (sharing the same hierarchical behavior), except that it's not disabled if there is no more reclaimable memory in the system. Signed-off-by: Roman Gushchin Cc: Johannes Weiner Cc: Michal Hocko Cc: Vladimir Davydov Cc: Tejun Heo Acked-by: Johannes Weiner --- Documentation/cgroup-v2.txt | 24 ++++++++- include/linux/memcontrol.h | 15 ++++-- include/linux/page_counter.h | 11 +++- mm/memcontrol.c | 118 ++++++++++++++++++++++++++++++++++--------- mm/page_counter.c | 63 ++++++++++++++++------- mm/vmscan.c | 18 ++++++- 6 files changed, 199 insertions(+), 50 deletions(-) diff --git a/Documentation/cgroup-v2.txt b/Documentation/cgroup-v2.txt index 657fe1769c75..a413118b9c29 100644 --- a/Documentation/cgroup-v2.txt +++ b/Documentation/cgroup-v2.txt @@ -1002,6 +1002,26 @@ PAGE_SIZE multiple when read back. The total amount of memory currently being used by the cgroup and its descendants. + memory.min + A read-write single value file which exists on non-root + cgroups. The default is "0". + + Hard memory protection. If the memory usage of a cgroup + is within its effective min boundary, the cgroup's memory + won't be reclaimed under any conditions. If there is no + unprotected reclaimable memory available, OOM killer + is invoked. + + Effective low boundary is limited by memory.min values of + all ancestor cgroups. If there is memory.min overcommitment + (child cgroup or cgroups are requiring more protected memory + than parent will allow), then each child cgroup will get + the part of parent's protection proportional to its + actual memory usage below memory.min. + + Putting more memory than generally available under this + protection is discouraged and may lead to constant OOMs. + memory.low A read-write single value file which exists on non-root cgroups. The default is "0". @@ -1013,9 +1033,9 @@ PAGE_SIZE multiple when read back. Effective low boundary is limited by memory.low values of all ancestor cgroups. If there is memory.low overcommitment - (child cgroup or cgroups are requiring more protected memory, + (child cgroup or cgroups are requiring more protected memory than parent will allow), then each child cgroup will get - the part of parent's protection proportional to the its + the part of parent's protection proportional to its actual memory usage below memory.low. Putting more memory than generally available under this diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index a2dfb1872dca..3b65d092614f 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -59,6 +59,12 @@ enum memcg_memory_event { MEMCG_NR_MEMORY_EVENTS, }; +enum mem_cgroup_protection { + MEMCG_PROT_NONE, + MEMCG_PROT_LOW, + MEMCG_PROT_MIN, +}; + struct mem_cgroup_reclaim_cookie { pg_data_t *pgdat; int priority; @@ -297,7 +303,8 @@ static inline bool mem_cgroup_disabled(void) return !cgroup_subsys_enabled(memory_cgrp_subsys); } -bool mem_cgroup_low(struct mem_cgroup *root, struct mem_cgroup *memcg); +enum mem_cgroup_protection mem_cgroup_protected(struct mem_cgroup *root, + struct mem_cgroup *memcg); int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask, struct mem_cgroup **memcgp, @@ -756,10 +763,10 @@ static inline void memcg_memory_event(struct mem_cgroup *memcg, { } -static inline bool mem_cgroup_low(struct mem_cgroup *root, - struct mem_cgroup *memcg) +static inline enum mem_cgroup_protection mem_cgroup_protected( + struct mem_cgroup *root, struct mem_cgroup *memcg) { - return false; + return MEMCG_PROT_NONE; } static inline int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm, diff --git a/include/linux/page_counter.h b/include/linux/page_counter.h index 7902a727d3b6..bab7e57f659b 100644 --- a/include/linux/page_counter.h +++ b/include/linux/page_counter.h @@ -8,10 +8,16 @@ struct page_counter { atomic_long_t usage; - unsigned long max; + unsigned long min; unsigned long low; + unsigned long max; struct page_counter *parent; + /* effective memory.min and memory.min usage tracking */ + unsigned long emin; + atomic_long_t min_usage; + atomic_long_t children_min_usage; + /* effective memory.low and memory.low usage tracking */ unsigned long elow; atomic_long_t low_usage; @@ -47,8 +53,9 @@ bool page_counter_try_charge(struct page_counter *counter, unsigned long nr_pages, struct page_counter **fail); void page_counter_uncharge(struct page_counter *counter, unsigned long nr_pages); -int page_counter_set_max(struct page_counter *counter, unsigned long nr_pages); +void page_counter_set_min(struct page_counter *counter, unsigned long nr_pages); void page_counter_set_low(struct page_counter *counter, unsigned long nr_pages); +int page_counter_set_max(struct page_counter *counter, unsigned long nr_pages); int page_counter_memparse(const char *buf, const char *max, unsigned long *nr_pages); diff --git a/mm/memcontrol.c b/mm/memcontrol.c index db89468c231c..d298b06a7fad 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -4503,6 +4503,7 @@ static void mem_cgroup_css_offline(struct cgroup_subsys_state *css) } spin_unlock(&memcg->event_list_lock); + page_counter_set_min(&memcg->memory, 0); page_counter_set_low(&memcg->memory, 0); memcg_offline_kmem(memcg); @@ -4557,6 +4558,7 @@ static void mem_cgroup_css_reset(struct cgroup_subsys_state *css) page_counter_set_max(&memcg->memsw, PAGE_COUNTER_MAX); page_counter_set_max(&memcg->kmem, PAGE_COUNTER_MAX); page_counter_set_max(&memcg->tcpmem, PAGE_COUNTER_MAX); + page_counter_set_min(&memcg->memory, 0); page_counter_set_low(&memcg->memory, 0); memcg->high = PAGE_COUNTER_MAX; memcg->soft_limit = PAGE_COUNTER_MAX; @@ -5294,6 +5296,36 @@ static u64 memory_current_read(struct cgroup_subsys_state *css, return (u64)page_counter_read(&memcg->memory) * PAGE_SIZE; } +static int memory_min_show(struct seq_file *m, void *v) +{ + struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); + unsigned long min = READ_ONCE(memcg->memory.min); + + if (min == PAGE_COUNTER_MAX) + seq_puts(m, "max\n"); + else + seq_printf(m, "%llu\n", (u64)min * PAGE_SIZE); + + return 0; +} + +static ssize_t memory_min_write(struct kernfs_open_file *of, + char *buf, size_t nbytes, loff_t off) +{ + struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); + unsigned long min; + int err; + + buf = strstrip(buf); + err = page_counter_memparse(buf, "max", &min); + if (err) + return err; + + page_counter_set_min(&memcg->memory, min); + + return nbytes; +} + static int memory_low_show(struct seq_file *m, void *v) { struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); @@ -5561,6 +5593,12 @@ static struct cftype memory_files[] = { .flags = CFTYPE_NOT_ON_ROOT, .read_u64 = memory_current_read, }, + { + .name = "min", + .flags = CFTYPE_NOT_ON_ROOT, + .seq_show = memory_min_show, + .write = memory_min_write, + }, { .name = "low", .flags = CFTYPE_NOT_ON_ROOT, @@ -5616,19 +5654,24 @@ struct cgroup_subsys memory_cgrp_subsys = { }; /** - * mem_cgroup_low - check if memory consumption is in the normal range + * mem_cgroup_protected - check if memory consumption is in the normal range * @root: the top ancestor of the sub-tree being checked * @memcg: the memory cgroup to check * * WARNING: This function is not stateless! It can only be used as part * of a top-down tree iteration, not for isolated queries. * - * Returns %true if memory consumption of @memcg is in the normal range. + * Returns one of the following: + * MEMCG_PROT_NONE: cgroup memory is not protected + * MEMCG_PROT_LOW: cgroup memory is protected as long there is + * an unprotected supply of reclaimable memory from other cgroups. + * MEMCG_PROT_MIN: cgroup memory is protected * - * @root is exclusive; it is never low when looked at directly + * @root is exclusive; it is never protected when looked at directly * - * To provide a proper hierarchical behavior, effective memory.low value - * is used. + * To provide a proper hierarchical behavior, effective memory.min/low values + * are used. Below is the description of how effective memory.low is calculated. + * Effective memory.min values is calculated in the same way. * * Effective memory.low is always equal or less than the original memory.low. * If there is no memory.low overcommittment (which is always true for @@ -5673,51 +5716,78 @@ struct cgroup_subsys memory_cgrp_subsys = { * E/memory.current = 0 * * These calculations require constant tracking of the actual low usages - * (see propagate_low_usage()), as well as recursive calculation of - * effective memory.low values. But as we do call mem_cgroup_low() + * (see propagate_protected_usage()), as well as recursive calculation of + * effective memory.low values. But as we do call mem_cgroup_protected() * path for each memory cgroup top-down from the reclaim, * it's possible to optimize this part, and save calculated elow * for next usage. This part is intentionally racy, but it's ok, * as memory.low is a best-effort mechanism. */ -bool mem_cgroup_low(struct mem_cgroup *root, struct mem_cgroup *memcg) +enum mem_cgroup_protection mem_cgroup_protected(struct mem_cgroup *root, + struct mem_cgroup *memcg) { - unsigned long usage, low_usage, siblings_low_usage; - unsigned long elow, parent_elow; struct mem_cgroup *parent; + unsigned long emin, parent_emin; + unsigned long elow, parent_elow; + unsigned long usage; if (mem_cgroup_disabled()) - return false; + return MEMCG_PROT_NONE; if (!root) root = root_mem_cgroup; if (memcg == root) - return false; + return MEMCG_PROT_NONE; - elow = memcg->memory.low; usage = page_counter_read(&memcg->memory); - parent = parent_mem_cgroup(memcg); + if (!usage) + return MEMCG_PROT_NONE; + + emin = memcg->memory.min; + elow = memcg->memory.low; + parent = parent_mem_cgroup(memcg); if (parent == root) goto exit; + parent_emin = READ_ONCE(parent->memory.emin); + emin = min(emin, parent_emin); + if (emin && parent_emin) { + unsigned long min_usage, siblings_min_usage; + + min_usage = min(usage, memcg->memory.min); + siblings_min_usage = atomic_long_read( + &parent->memory.children_min_usage); + + if (min_usage && siblings_min_usage) + emin = min(emin, parent_emin * min_usage / + siblings_min_usage); + } + parent_elow = READ_ONCE(parent->memory.elow); elow = min(elow, parent_elow); + if (elow && parent_elow) { + unsigned long low_usage, siblings_low_usage; - if (!elow || !parent_elow) - goto exit; + low_usage = min(usage, memcg->memory.low); + siblings_low_usage = atomic_long_read( + &parent->memory.children_low_usage); - low_usage = min(usage, memcg->memory.low); - siblings_low_usage = atomic_long_read( - &parent->memory.children_low_usage); - - if (!low_usage || !siblings_low_usage) - goto exit; + if (low_usage && siblings_low_usage) + elow = min(elow, parent_elow * low_usage / + siblings_low_usage); + } - elow = min(elow, parent_elow * low_usage / siblings_low_usage); exit: + memcg->memory.emin = emin; memcg->memory.elow = elow; - return usage && usage <= elow; + + if (usage <= emin) + return MEMCG_PROT_MIN; + else if (usage <= elow) + return MEMCG_PROT_LOW; + else + return MEMCG_PROT_NONE; } /** diff --git a/mm/page_counter.c b/mm/page_counter.c index a5ff4cbc355a..de31470655f6 100644 --- a/mm/page_counter.c +++ b/mm/page_counter.c @@ -13,26 +13,38 @@ #include #include -static void propagate_low_usage(struct page_counter *c, unsigned long usage) +static void propagate_protected_usage(struct page_counter *c, + unsigned long usage) { - unsigned long low_usage, old; + unsigned long protected, old_protected; long delta; if (!c->parent) return; - if (!c->low && !atomic_long_read(&c->low_usage)) - return; + if (c->min || atomic_long_read(&c->min_usage)) { + if (usage <= c->min) + protected = usage; + else + protected = 0; + + old_protected = atomic_long_xchg(&c->min_usage, protected); + delta = protected - old_protected; + if (delta) + atomic_long_add(delta, &c->parent->children_min_usage); + } - if (usage <= c->low) - low_usage = usage; - else - low_usage = 0; + if (c->low || atomic_long_read(&c->low_usage)) { + if (usage <= c->low) + protected = usage; + else + protected = 0; - old = atomic_long_xchg(&c->low_usage, low_usage); - delta = low_usage - old; - if (delta) - atomic_long_add(delta, &c->parent->children_low_usage); + old_protected = atomic_long_xchg(&c->low_usage, protected); + delta = protected - old_protected; + if (delta) + atomic_long_add(delta, &c->parent->children_low_usage); + } } /** @@ -45,7 +57,7 @@ void page_counter_cancel(struct page_counter *counter, unsigned long nr_pages) long new; new = atomic_long_sub_return(nr_pages, &counter->usage); - propagate_low_usage(counter, new); + propagate_protected_usage(counter, new); /* More uncharges than charges? */ WARN_ON_ONCE(new < 0); } @@ -65,7 +77,7 @@ void page_counter_charge(struct page_counter *counter, unsigned long nr_pages) long new; new = atomic_long_add_return(nr_pages, &c->usage); - propagate_low_usage(counter, new); + propagate_protected_usage(counter, new); /* * This is indeed racy, but we can live with some * inaccuracy in the watermark. @@ -109,7 +121,7 @@ bool page_counter_try_charge(struct page_counter *counter, new = atomic_long_add_return(nr_pages, &c->usage); if (new > c->max) { atomic_long_sub(nr_pages, &c->usage); - propagate_low_usage(counter, new); + propagate_protected_usage(counter, new); /* * This is racy, but we can live with some * inaccuracy in the failcnt. @@ -118,7 +130,7 @@ bool page_counter_try_charge(struct page_counter *counter, *fail = c; goto failed; } - propagate_low_usage(counter, new); + propagate_protected_usage(counter, new); /* * Just like with failcnt, we can live with some * inaccuracy in the watermark. @@ -190,6 +202,23 @@ int page_counter_set_max(struct page_counter *counter, unsigned long nr_pages) } } +/** + * page_counter_set_min - set the amount of protected memory + * @counter: counter + * @nr_pages: value to set + * + * The caller must serialize invocations on the same counter. + */ +void page_counter_set_min(struct page_counter *counter, unsigned long nr_pages) +{ + struct page_counter *c; + + counter->min = nr_pages; + + for (c = counter; c; c = c->parent) + propagate_protected_usage(c, atomic_long_read(&c->usage)); +} + /** * page_counter_set_low - set the amount of protected memory * @counter: counter @@ -204,7 +233,7 @@ void page_counter_set_low(struct page_counter *counter, unsigned long nr_pages) counter->low = nr_pages; for (c = counter; c; c = c->parent) - propagate_low_usage(c, atomic_long_read(&c->usage)); + propagate_protected_usage(c, atomic_long_read(&c->usage)); } /** diff --git a/mm/vmscan.c b/mm/vmscan.c index 10c8a38c5eef..50055d72f294 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -2544,12 +2544,28 @@ static bool shrink_node(pg_data_t *pgdat, struct scan_control *sc) unsigned long reclaimed; unsigned long scanned; - if (mem_cgroup_low(root, memcg)) { + switch (mem_cgroup_protected(root, memcg)) { + case MEMCG_PROT_MIN: + /* + * Hard protection. + * If there is no reclaimable memory, OOM. + */ + continue; + case MEMCG_PROT_LOW: + /* + * Soft protection. + * Respect the protection only as long as + * there is an unprotected supply + * of reclaimable memory from other cgroups. + */ if (!sc->memcg_low_reclaim) { sc->memcg_low_skipped = 1; continue; } memcg_memory_event(memcg, MEMCG_LOW); + break; + case MEMCG_PROT_NONE: + break; } reclaimed = sc->nr_reclaimed; -- 2.14.3