From patchwork Mon Oct 5 08:13:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Righi X-Patchwork-Id: 11816225 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 90ABF112E for ; Mon, 5 Oct 2020 08:14:33 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3D27C20781 for ; Mon, 5 Oct 2020 08:14:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3D27C20781 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=canonical.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id C9CDA6B0062; Mon, 5 Oct 2020 04:14:31 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id BFFE16B0068; Mon, 5 Oct 2020 04:14:31 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A7B048E0001; Mon, 5 Oct 2020 04:14:31 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0088.hostedemail.com [216.40.44.88]) by kanga.kvack.org (Postfix) with ESMTP id 72B666B0062 for ; Mon, 5 Oct 2020 04:14:31 -0400 (EDT) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 011B71EE6 for ; Mon, 5 Oct 2020 08:14:31 +0000 (UTC) X-FDA: 77337159942.28.story94_0616ccc271bc Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin28.hostedemail.com (Postfix) with ESMTP id CE8736C05 for ; Mon, 5 Oct 2020 08:14:30 +0000 (UTC) X-Spam-Summary: 1,0,0,ce4cfe0024503abb,d41d8cd98f00b204,andrea.righi@canonical.com,,RULES_HIT:41:355:379:541:800:960:966:968:973:988:989:1260:1311:1314:1345:1359:1431:1437:1515:1535:1542:1711:1730:1747:1777:1792:2196:2198:2199:2200:2393:2559:2562:2693:2731:3138:3139:3140:3141:3142:3353:3865:3866:3867:3868:3871:4321:4385:4605:5007:6119:6120:6261:7901:7903:10004:11026:11658:11914:12043:12296:12297:12438:12517:12519:12555:12895:12986:13141:13230:13894:14096:14181:14394:14721:14819:21060:21080:21444:21451:21627:21990:30034:30054,0,RBL:91.189.89.112:@canonical.com:.lbl8.mailshell.net-62.8.15.100 64.201.201.201;04y8jdyo8kt3nres5aq9qpnwtcdo7occ79zrddakg474kpd977kutw6o4zhxnu7.npdmc5pi87dq47b6xk7gd7o36migitgm7w61p6393s8xm3dxi5egxizxj33qmp1.4-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: story94_0616ccc271bc X-Filterd-Recvd-Size: 5658 Received: from youngberry.canonical.com (youngberry.canonical.com [91.189.89.112]) by imf24.hostedemail.com (Postfix) with ESMTP for ; Mon, 5 Oct 2020 08:14:30 +0000 (UTC) Received: from mail-wr1-f70.google.com ([209.85.221.70]) by youngberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1kPLdc-0001Hu-Ta for linux-mm@kvack.org; Mon, 05 Oct 2020 08:14:29 +0000 Received: by mail-wr1-f70.google.com with SMTP id 33so1376065wrf.22 for ; Mon, 05 Oct 2020 01:14:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=YOOKKc/NsxErESy/u7hg3zhXpqetp++Zy7NLfv5ic88=; b=iGeQITIKNnVxfINr+gyIlPAxWDchJC0LExwxmkmqZoEPlVAqL5PH8o6/0iJUVp9BIS GfK+Bom/4oQo3Wfi+JtJlIonTXPyYhECuHzLEWfov3X3qWThhIRXykgaz/RNSzciXFxP Qb/GqvtFC2Fqplw4XnwJuIS/jyX9PcOu3XSDUVS5WsrBA7/Vo/RRfqAGVUPQ4/IlA6KJ 5Fyx6fMDXFGSrwg0bYrbXKlDh6uXKUDoR5Ann7fIK7zjgaqxCpa5+8jiSy3pCJIXObHq QIFX+QUcJvx53Jg8D/CPyde/ZMBYFJ/VfN7JEwm6+I+ur6LLXgBnFIW/W22z6N++BPEw TWwA== X-Gm-Message-State: AOAM533PTxH72OOgJlDyvz5R6QyKkrQUjiTla4eBBgY5jSo6Nbmzyw/v Oqz7ERthgt1yQ1dngSM2TBUeIfISWLYeXZ2wwNypdBtvk3nhUqKBD2vN7YGHspwwyG+7I/1/xCU kr0V8JqyYrq7rXCQySwKdh7d+J33I X-Received: by 2002:a5d:67d0:: with SMTP id n16mr17497446wrw.198.1601885668390; Mon, 05 Oct 2020 01:14:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxtKOpH0FCV6s7xth+z5GIgfZ6KMfLvYYR4CHJf3bHlkFzgc8Y/X0OX+gbrjhuJR5dAK1ORUw== X-Received: by 2002:a5d:67d0:: with SMTP id n16mr17497429wrw.198.1601885668147; Mon, 05 Oct 2020 01:14:28 -0700 (PDT) Received: from xps-13-7390.homenet.telecomitalia.it (host-79-36-133-218.retail.telecomitalia.it. [79.36.133.218]) by smtp.gmail.com with ESMTPSA id a15sm13168855wrn.3.2020.10.05.01.14.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Oct 2020 01:14:27 -0700 (PDT) From: Andrea Righi To: Michal Hocko , Vladimir Davydov Cc: Li Zefan , Tejun Heo , Johannes Weiner , Andrew Morton , Luigi Semenzato , "Rafael J . Wysocki" , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org Subject: [PATCH RFC v2 1/2] mm: memcontrol: make shrink_all_memory() memcg aware Date: Mon, 5 Oct 2020 10:13:12 +0200 Message-Id: <20201005081313.732745-2-andrea.righi@canonical.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20201005081313.732745-1-andrea.righi@canonical.com> References: <20201005081313.732745-1-andrea.righi@canonical.com> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Allow to specify a memcg when calling shrink_all_memory() to reclaim some memory from a specific cgroup. Moreover, make shrink_all_memory() always available and do not depend on having CONFIG_HIBERNATION enabled. This is required by the opportunistic memory reclaim feature. Signed-off-by: Andrea Righi --- include/linux/swap.h | 9 ++++++++- mm/vmscan.c | 6 +++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 661046994db4..1490b09a6e6c 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -368,7 +368,14 @@ extern unsigned long mem_cgroup_shrink_node(struct mem_cgroup *mem, gfp_t gfp_mask, bool noswap, pg_data_t *pgdat, unsigned long *nr_scanned); -extern unsigned long shrink_all_memory(unsigned long nr_pages); +extern unsigned long +__shrink_all_memory(unsigned long nr_pages, struct mem_cgroup *memcg); + +static inline unsigned long shrink_all_memory(unsigned long nr_pages) +{ + return __shrink_all_memory(nr_pages, NULL); +} + extern int vm_swappiness; extern int remove_mapping(struct address_space *mapping, struct page *page); diff --git a/mm/vmscan.c b/mm/vmscan.c index 466fc3144fff..ac04d5e16c42 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -3986,7 +3986,6 @@ void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order, wake_up_interruptible(&pgdat->kswapd_wait); } -#ifdef CONFIG_HIBERNATION /* * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of * freed pages. @@ -3995,7 +3994,8 @@ void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order, * LRU order by reclaiming preferentially * inactive > active > active referenced > active mapped */ -unsigned long shrink_all_memory(unsigned long nr_to_reclaim) +unsigned long +__shrink_all_memory(unsigned long nr_to_reclaim, struct mem_cgroup *memcg) { struct scan_control sc = { .nr_to_reclaim = nr_to_reclaim, @@ -4006,6 +4006,7 @@ unsigned long shrink_all_memory(unsigned long nr_to_reclaim) .may_unmap = 1, .may_swap = 1, .hibernation_mode = 1, + .target_mem_cgroup = memcg, }; struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); unsigned long nr_reclaimed; @@ -4023,7 +4024,6 @@ unsigned long shrink_all_memory(unsigned long nr_to_reclaim) return nr_reclaimed; } -#endif /* CONFIG_HIBERNATION */ /* * This kswapd start function will be called by init and node-hot-add. From patchwork Mon Oct 5 08:13:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Righi X-Patchwork-Id: 11816227 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 70D9D112E for ; Mon, 5 Oct 2020 08:14:35 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2D0012100A for ; Mon, 5 Oct 2020 08:14:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2D0012100A Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=canonical.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 3C3036B0068; Mon, 5 Oct 2020 04:14:33 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 34C6D8E0001; Mon, 5 Oct 2020 04:14:33 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 19FA76B006E; Mon, 5 Oct 2020 04:14:33 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0203.hostedemail.com [216.40.44.203]) by kanga.kvack.org (Postfix) with ESMTP id DB8476B0068 for ; Mon, 5 Oct 2020 04:14:32 -0400 (EDT) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 80607181AE869 for ; Mon, 5 Oct 2020 08:14:32 +0000 (UTC) X-FDA: 77337159984.25.price39_3d085dd271bc Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin25.hostedemail.com (Postfix) with ESMTP id 55E7F1804E3A0 for ; Mon, 5 Oct 2020 08:14:32 +0000 (UTC) X-Spam-Summary: 1,0,0,31293532604c90c2,d41d8cd98f00b204,andrea.righi@canonical.com,,RULES_HIT:41:355:379:421:541:800:960:973:988:989:1260:1311:1314:1345:1359:1431:1437:1500:1515:1535:1544:1605:1711:1730:1747:1777:1792:1801:2198:2199:2393:2559:2562:2693:2904:3138:3139:3140:3141:3142:3865:3866:3867:3868:3870:3871:3872:3874:4118:4321:4423:4605:5007:6119:6120:6261:7875:7901:7903:10004:11026:11232:11473:11658:11914:12043:12291:12294:12296:12297:12438:12517:12519:12555:12895:13869:13894:14181:14394:14721:21060:21080:21444:21451:21627:21795:21990:30005:30029:30034:30051:30054,0,RBL:91.189.89.112:@canonical.com:.lbl8.mailshell.net-64.201.201.201 62.8.15.100;04yg6cgrko4uw5bwkh3ekd6nfjt7koc5dk3tfidt3i7ddbe1wkao5a8mh46fjag.7gxqktpb933mdpgn3mnijqpq6ndomdm84w93zbuiy86si5ukk98jwdumy5fs5az.a-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: price39_3d085dd271bc X-Filterd-Recvd-Size: 7993 Received: from youngberry.canonical.com (youngberry.canonical.com [91.189.89.112]) by imf38.hostedemail.com (Postfix) with ESMTP for ; Mon, 5 Oct 2020 08:14:31 +0000 (UTC) Received: from mail-wr1-f69.google.com ([209.85.221.69]) by youngberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1kPLde-0001Im-9V for linux-mm@kvack.org; Mon, 05 Oct 2020 08:14:30 +0000 Received: by mail-wr1-f69.google.com with SMTP id o6so3686867wrp.1 for ; Mon, 05 Oct 2020 01:14:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=a3RK306ViIXSDm7E/OHB8KsWv3MXYnGA8qRg0vSmwRg=; b=fOYE3fQhpCXt/9XDueEDd0QFhRc2o9m4+XzyKOe2Ud5qcEJPewdOIINEOQy8k2opei mj9rxQUHxy/aEEX8T40YYkiwl3pN3VEuMWPV4ZCshrMgwMzq2CqlIPdKUK4cCuktx70R toNXdp9HhM2HKqLEmATk0UqyM8/WubFffKXqVN5wNBSL/ihEoTXOgHOoB8S6RZ2Lmmnh w4Y/ayqSydYOYE3r+kOdCFrKSrDhg4Pa8qU7a5Q2gO0puu9PgCGLDHLRzobOl4LxsQMz urNr/ESlajVX0Ce7CUoaBUbL5qx9gSo8APiDP/01MXuYhI8LdjDG/pae2NDuV3hyVGMv 0rKA== X-Gm-Message-State: AOAM531K+F065MjjoZ5s4iu9Es2QyJa2V2KNa70B70U23UbGF+WCrsLq q44Gg26OawTAGrgt6Nt64uPc7EDrdQZW8hXPAo5uNbh1Pe4nsXSzXlePyFDqPcMeOJ/grG7tOnt gQciJs3uA2bFGewia2jl01DU38fkQ X-Received: by 2002:a7b:c111:: with SMTP id w17mr16169212wmi.28.1601885669741; Mon, 05 Oct 2020 01:14:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxK2e5XsCLt9z+P6jBHXXdu+RZxdp5xEs60J4eTYgbaA18QOia0wcIZVKu26ncPtxuUD2XYNA== X-Received: by 2002:a7b:c111:: with SMTP id w17mr16169172wmi.28.1601885669454; Mon, 05 Oct 2020 01:14:29 -0700 (PDT) Received: from xps-13-7390.homenet.telecomitalia.it (host-79-36-133-218.retail.telecomitalia.it. [79.36.133.218]) by smtp.gmail.com with ESMTPSA id a15sm13168855wrn.3.2020.10.05.01.14.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Oct 2020 01:14:28 -0700 (PDT) From: Andrea Righi To: Michal Hocko , Vladimir Davydov Cc: Li Zefan , Tejun Heo , Johannes Weiner , Andrew Morton , Luigi Semenzato , "Rafael J . Wysocki" , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org Subject: [PATCH RFC v2 2/2] mm: memcontrol: introduce opportunistic memory reclaim Date: Mon, 5 Oct 2020 10:13:13 +0200 Message-Id: <20201005081313.732745-3-andrea.righi@canonical.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20201005081313.732745-1-andrea.righi@canonical.com> References: <20201005081313.732745-1-andrea.righi@canonical.com> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Opportunistic memory reclaim allows user-space to trigger an artificial memory pressure condition and force the system to reclaim memory (drop caches, swap out anonymous memory, etc.). This feature is provided by adding a new file to each memcg: memory.swap.reclaim. Writing a number to this file forces a memcg to reclaim memory up to that number of bytes ("max" means as much memory as possible). Reading from the this file returns the amount of bytes reclaimed in the last opportunistic memory reclaim attempt. Memory reclaim can be interrupted sending a signal to the process that is writing to memory.swap.reclaim (i.e., to set a timeout for the whole memory reclaim run). Signed-off-by: Andrea Righi --- Documentation/admin-guide/cgroup-v2.rst | 18 ++++++++ include/linux/memcontrol.h | 4 ++ mm/memcontrol.c | 59 +++++++++++++++++++++++++ 3 files changed, 81 insertions(+) diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst index baa07b30845e..2850a5cb4b1e 100644 --- a/Documentation/admin-guide/cgroup-v2.rst +++ b/Documentation/admin-guide/cgroup-v2.rst @@ -1409,6 +1409,24 @@ PAGE_SIZE multiple when read back. Swap usage hard limit. If a cgroup's swap usage reaches this limit, anonymous memory of the cgroup will not be swapped out. + memory.swap.reclaim + A read-write single value file that can be used to trigger + opportunistic memory reclaim. + + The string written to this file represents the amount of memory to be + reclaimed (special value "max" means "as much memory as possible"). + + When opportunistic memory reclaim is started the system will be put + into an artificial memory pressure condition and memory will be + reclaimed by dropping clean page cache pages, swapping out anonymous + pages, etc. + + NOTE: it is possible to interrupt the memory reclaim sending a signal + to the writer of this file. + + Reading from memory.swap.reclaim returns the amount of bytes reclaimed + in the last attempt. + memory.swap.events A read-only flat-keyed file which exists on non-root cgroups. The following entries are defined. Unless specified diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index d0b036123c6a..0c90d989bdc1 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -306,6 +306,10 @@ struct mem_cgroup { bool tcpmem_active; int tcpmem_pressure; +#ifdef CONFIG_MEMCG_SWAP + unsigned long nr_swap_reclaimed; +#endif + #ifdef CONFIG_MEMCG_KMEM /* Index in the kmem_cache->memcg_params.memcg_caches array */ int kmemcg_id; diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 6877c765b8d0..b98e9bbd61b0 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -7346,6 +7346,60 @@ static int swap_events_show(struct seq_file *m, void *v) return 0; } +/* + * Try to reclaim some memory in the system, stop when one of the following + * conditions occurs: + * - at least "nr_pages" have been reclaimed + * - no more pages can be reclaimed + * - current task explicitly interrupted by a signal (e.g., user space + * timeout) + * + * @nr_pages - amount of pages to be reclaimed (0 means "as many pages as + * possible"). + */ +static unsigned long +do_mm_reclaim(struct mem_cgroup *memcg, unsigned long nr_pages) +{ + unsigned long nr_reclaimed = 0; + + while (nr_pages > 0) { + unsigned long reclaimed; + + if (signal_pending(current)) + break; + reclaimed = __shrink_all_memory(nr_pages, memcg); + if (!reclaimed) + break; + nr_reclaimed += reclaimed; + nr_pages -= min_t(unsigned long, reclaimed, nr_pages); + } + return nr_reclaimed; +} + +static ssize_t swap_reclaim_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 nr_to_reclaim; + int err; + + buf = strstrip(buf); + err = page_counter_memparse(buf, "max", &nr_to_reclaim); + if (err) + return err; + memcg->nr_swap_reclaimed = do_mm_reclaim(memcg, nr_to_reclaim); + + return nbytes; +} + +static u64 swap_reclaim_read(struct cgroup_subsys_state *css, + struct cftype *cft) +{ + struct mem_cgroup *memcg = mem_cgroup_from_css(css); + + return memcg->nr_swap_reclaimed << PAGE_SHIFT; +} + static struct cftype swap_files[] = { { .name = "swap.current", @@ -7370,6 +7424,11 @@ static struct cftype swap_files[] = { .file_offset = offsetof(struct mem_cgroup, swap_events_file), .seq_show = swap_events_show, }, + { + .name = "swap.reclaim", + .write = swap_reclaim_write, + .read_u64 = swap_reclaim_read, + }, { } /* terminate */ };