Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp6439103rwb; Tue, 22 Nov 2022 13:27:06 -0800 (PST) X-Google-Smtp-Source: AA0mqf7EormazS0Qs5bI0sqBdywYr1Tc0TQ+muuFaC6B2YVBwOSgEBiYC7fEqWcEYX3wYR6zj1CO X-Received: by 2002:a50:fa86:0:b0:469:cb4d:a529 with SMTP id w6-20020a50fa86000000b00469cb4da529mr5734002edr.30.1669152425803; Tue, 22 Nov 2022 13:27:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669152425; cv=none; d=google.com; s=arc-20160816; b=LNn193EFtDoBGxK2o9Ay/jxU3ir1PCQOeUSzy+KEOm5LC2c7f5uWw7mYBoMgNQ1FiJ zTfxvAEy3fEgjhmuxg7czSEaEUGSM7mkm6q3isv1daNxK16mN6T88zZS3TkuU3vH7F6a XKtlIWJNQNmr5CRQMlre5HAnPRQt6pv6x9tgrkaqGR8Occfb8zRjUgMXBooodMo/pwWK jMRKtnBLkSbpnx977cG/IRl35U5IEHngKvO0R7jJQrRzE66Ey/OgRzvvzzBDHiPzprG8 jQsTl+r6svQlJ0kU2TcAQua735+LWr44MbcJc7qAyyxeDv7UI2DgujGSDCDX8w7JbsjU xJeA== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hp27-20020a1709073e1b00b00783ddc8b643si12739639ejc.317.2022.11.22.13.26.40; Tue, 22 Nov 2022 13:27:05 -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 + 89 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 Content-Type: text/plain; charset="UTF-8" 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 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(-) 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]; -- 2.38.1.584.g0f3c55d4c2-goog