From nobody Thu Apr 16 00:40:33 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1CF12C43217 for ; Tue, 22 Nov 2022 20:39:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234748AbiKVUjL (ORCPT ); Tue, 22 Nov 2022 15:39:11 -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-Google-Smtp-Source: AA0mqf4nRSDZsJA5udKOk+WPQ7He8G7ypX1TuK+m1EL8dLhG9pG68x3W9SVyDcAVprESbrBcmMcBBy7AvLCfO1+7Ag== 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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_fil= e *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 =3D mem_cgroup_from_css(of_css(of)); + unsigned int nr_retries =3D MAX_RECLAIM_RETRIES; + unsigned long nr_to_demote, nr_demoted =3D 0; + unsigned int reclaim_options =3D MEMCG_RECLAIM_ONLY_DEMOTE; + int err; + + buf =3D strstrip(buf); + err =3D 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 =3D try_to_free_mem_cgroup_pages( + memcg, nr_to_demote - nr_demoted, GFP_KERNEL, + reclaim_options); + + if (!demoted && !nr_retries--) + return -EAGAIN; + + nr_demoted +=3D demoted; + } + + return nbytes; +} + static struct cftype memory_files[] =3D { { .name =3D "current", @@ -6691,6 +6724,11 @@ static struct cftype memory_files[] =3D { .flags =3D CFTYPE_NS_DELEGATABLE, .write =3D memory_reclaim, }, + { + .name =3D "demote", + .flags =3D CFTYPE_NS_DELEGATABLE, + .write =3D 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_h= ead *folio_list, LIST_HEAD(demote_folios); unsigned int nr_reclaimed =3D 0; unsigned int pgactivate =3D 0; - bool do_demote_pass; + bool do_demote_pass, only_demote_pass; struct swap_iocb *plug =3D NULL; memset(stat, 0, sizeof(*stat)); cond_resched(); do_demote_pass =3D can_demote(pgdat->node_id, sc); + only_demote_pass =3D sc->demotion =3D=3D 2; retry: while (!list_empty(folio_list)) { @@ -2091,10 +2092,19 @@ static unsigned int shrink_folio_list(struct list_h= ead *folio_list, nr_reclaimed +=3D 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 =3D false; - goto retry; + + /* + * goto retry to reclaim the undemoted folios in folio_list if + * desired. + */ + if (!only_demote_pass) { + do_demote_pass =3D false; + goto retry; + } } pgactivate =3D stat->nr_activate[0] + stat->nr_activate[1]; -- 2.38.1.584.g0f3c55d4c2-goog