From patchwork Tue Nov 22 20:38:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mina Almasry X-Patchwork-Id: 24553 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp2428686wrr; Tue, 22 Nov 2022 12:40:49 -0800 (PST) X-Google-Smtp-Source: AA0mqf7DI+TRywupHVen93gfNhhMTFXleUWGDvEkzpcpqvi5C3oEOR4mUEB9CxBCmxp6QqWyAWRJ X-Received: by 2002:a17:902:efcf:b0:186:9cce:c59 with SMTP id ja15-20020a170902efcf00b001869cce0c59mr6282060plb.120.1669149649630; Tue, 22 Nov 2022 12:40:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669149649; cv=none; d=google.com; s=arc-20160816; b=ShDJUDhscJbzdFuvIzl/WZlTCs+Meyc6mi/Q5cBMoX8p/uYQ8BAZojZpTgH1l6d+WY j+4m1Wla+pYClbkuCArBuWCfv+b+5W0Tgj+BT28iLSHnwhsmg8gSIC0kz5LCVwmI/udb x+s7Vf7mlZfihpaS8TDF+Qi7udjp6hlSYvwPo+GDOlqvRN5mnnHKDWiytVsnDmOhWWA2 V+Gty57YFybCRGdf8kGCevbwMVy9taV/rm0HeMD96gTR8oRyamJAxzIzF9LAO/BcKWdt 5pJ7Mik2qKIprBNhBl96pIdU1DmFY0dVFBsnUs48el7fz/4pUqMTlFV/jomQUTtm+czy JMlw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=mH2xKyLbtphuPdUJ732OjxOQc7w7f5rhMLqPKs8ZczA=; b=0Bxba6C7YGMG1rRwP4gPMj67NeO/HNVdAlhVWJ1hCmoruszyrsS7trixUogclUFf1C j2i3b7N+9IMJKGK97NYMFSqfLg4/Ss+TzLrqcJY6pmDYPRd3Zh/5r4aRSDG+j1VmZ8nM P45cnKjBwPJL78pP5EJ3OLJ1xqh0x+r5Of6qe10mMJCT77Vzb6PL8avGUSX4ZuvysGls sxL1iB8x0DxYee7cct46G8ds2LFG1MKP13VuaOPnk0U4wWsZoH5XbCFB6oTmQTY8vi91 DwttXSPzxaVJ5EnKU7gej3qJaERbRbXyp8f/eGiE1882qQxABVRfEaDVXRv1b/XpvKxJ Q/PA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=qdsmec8o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a10-20020a65640a000000b0044c3ec9ea71si870959pgv.630.2022.11.22.12.40.36; Tue, 22 Nov 2022 12:40:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=qdsmec8o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234739AbiKVUjK (ORCPT + 99 others); Tue, 22 Nov 2022 15:39:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234512AbiKVUjF (ORCPT ); Tue, 22 Nov 2022 15:39:05 -0500 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A601D697C5 for ; Tue, 22 Nov 2022 12:39:04 -0800 (PST) Received: by mail-pg1-x54a.google.com with SMTP id p7-20020a631e47000000b0047691854a86so9085055pgm.16 for ; Tue, 22 Nov 2022 12:39:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=mH2xKyLbtphuPdUJ732OjxOQc7w7f5rhMLqPKs8ZczA=; b=qdsmec8olNuJkHjim4tCNOzdWa/frQp8m5lufYNaqejkZMLoMUUyW+CWmAkGziybpf XVtKd/0LwOGTSKPzuW0laRVzBchDj/eYAtuTCSNB8IMPYcpApfCWC2zjH37lPwrTquSo e6VwPwg/ku5WTWy2ImXR0wgUFLZ1Bya8Re9NroqrrjtTrQdgeKHCixYvONLr+VBOSK7D y/z7y3/mili8PPvIi+kePEDEmzvJZh09v92ohyzbQ5RuROFwKw5KgQsz7VHG+PzHsW5E 1yg6L/6VSUWRD0wFpHrfizjK3o+Nif6zqdPMbBMhrgAWMlNgfhUP/5lOq0C7Wj8y1WM3 OO7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=mH2xKyLbtphuPdUJ732OjxOQc7w7f5rhMLqPKs8ZczA=; b=VNk9QvOUjkdQ3NPvDnKGiIxw1VSM8zh5+H5tySS9TVbIT98Eceplu2QuyEWnywb6on Ltb0DLHx7+SxoTDB1i1JPIU//jja+rf1OCGDopXWxEkTq+pq924o5QlmNMmeFETrTIMt GA0o3pYzJs4GdG5MmebvYYf6vRj0TAXS70EX/XsUeYXq3iZ2FzOKpdCdvFAIQhwn/agZ ZGpf8dikdoJkf2f6Toii/EqNI3Nx6nZOAydKTgaCixIiAoLM3fqogXz1sMduQTXFy74v aUSVfOFJcCYwAtvaGWsIYsbgu4xd0nPVNOIeWA6trhfkO1WFaLOmeJGElmhE7177cG0z o2Og== X-Gm-Message-State: ANoB5pn99rWThp9cfFqM9CZbaRfgv3Eh0xeqwJfRjYQKRPS4FV41SQYp X9RHZ0nkJ5uMDRbIA5dvwC7o3iTwc2453oIDbw== X-Received: from almasrymina.svl.corp.google.com ([2620:15c:2d4:203:b7d0:497d:abe4:3e6f]) (user=almasrymina job=sendgmr) by 2002:a63:7143:0:b0:46f:918e:7338 with SMTP id b3-20020a637143000000b0046f918e7338mr6371506pgn.8.1669149544152; Tue, 22 Nov 2022 12:39:04 -0800 (PST) Date: Tue, 22 Nov 2022 12:38:46 -0800 In-Reply-To: <20221122203850.2765015-1-almasrymina@google.com> Mime-Version: 1.0 References: <20221122203850.2765015-1-almasrymina@google.com> X-Mailer: git-send-email 2.38.1.584.g0f3c55d4c2-goog Message-ID: <20221122203850.2765015-2-almasrymina@google.com> Subject: [RFC PATCH v1] mm: Add memory.demote for proactive demotion only From: Mina Almasry To: Huang Ying , Yang Shi , Yosry Ahmed , Tim Chen , weixugc@google.com, shakeelb@google.com, gthelen@google.com, fvdl@google.com, Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Andrew Morton Cc: Mina Almasry , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750230262874346638?= X-GMAIL-MSGID: =?utf-8?q?1750230262874346638?= Add the proactive demotion interface memory.demote. This interface can be used as follows: echo "1m" > memory.demote At this command the kernel will attempt to demote 1m of memory from this cgroup. The kernel may not be able to demote the full amount requested by the userspace and in that case EAGAIN would be returned to the user (similar to memory.reclaim). The kernel will only attempt to demote pages with this interface. It will not attempt any other kind of reclaim (swap, writeback or reclaiming clean file pages). Signed-off-by: Mina Almasry --- mm/memcontrol.c | 38 ++++++++++++++++++++++++++++++++++++++ mm/vmscan.c | 18 ++++++++++++++---- 2 files changed, 52 insertions(+), 4 deletions(-) -- 2.38.1.584.g0f3c55d4c2-goog diff --git a/mm/memcontrol.c b/mm/memcontrol.c index fd4ff1c865a2..427c79e467eb 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -6623,6 +6623,39 @@ static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf, return nbytes; } +static ssize_t memory_demote(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 int nr_retries = MAX_RECLAIM_RETRIES; + unsigned long nr_to_demote, nr_demoted = 0; + unsigned int reclaim_options = MEMCG_RECLAIM_ONLY_DEMOTE; + int err; + + buf = strstrip(buf); + err = page_counter_memparse(buf, "", &nr_to_demote); + if (err) + return err; + + while (nr_demoted < nr_to_demote) { + unsigned long demoted; + + if (signal_pending(current)) + return -EINTR; + + demoted = try_to_free_mem_cgroup_pages( + memcg, nr_to_demote - nr_demoted, GFP_KERNEL, + reclaim_options); + + if (!demoted && !nr_retries--) + return -EAGAIN; + + nr_demoted += demoted; + } + + return nbytes; +} + static struct cftype memory_files[] = { { .name = "current", @@ -6691,6 +6724,11 @@ static struct cftype memory_files[] = { .flags = CFTYPE_NS_DELEGATABLE, .write = memory_reclaim, }, + { + .name = "demote", + .flags = CFTYPE_NS_DELEGATABLE, + .write = memory_demote, + }, { } /* terminate */ }; diff --git a/mm/vmscan.c b/mm/vmscan.c index dea05ad8ece5..8c1f5416d789 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1657,12 +1657,13 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, LIST_HEAD(demote_folios); unsigned int nr_reclaimed = 0; unsigned int pgactivate = 0; - bool do_demote_pass; + bool do_demote_pass, only_demote_pass; struct swap_iocb *plug = NULL; memset(stat, 0, sizeof(*stat)); cond_resched(); do_demote_pass = can_demote(pgdat->node_id, sc); + only_demote_pass = sc->demotion == 2; retry: while (!list_empty(folio_list)) { @@ -2091,10 +2092,19 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, nr_reclaimed += demote_folio_list(&demote_folios, pgdat); /* Folios that could not be demoted are still in @demote_folios */ if (!list_empty(&demote_folios)) { - /* Folios which weren't demoted go back on @folio_list for retry: */ + /* + * Folios which weren't demoted go back on @folio_list. + */ list_splice_init(&demote_folios, folio_list); - do_demote_pass = false; - goto retry; + + /* + * goto retry to reclaim the undemoted folios in folio_list if + * desired. + */ + if (!only_demote_pass) { + do_demote_pass = false; + goto retry; + } } pgactivate = stat->nr_activate[0] + stat->nr_activate[1];