Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp4344356rdb; Mon, 11 Dec 2023 17:07:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IEobCXQzBRnA/+HHDLUx0DOt21CNcPvSkkAzBY4blECNOKP1uBPrnuOI0Q9ms+rr5RcRbzM X-Received: by 2002:a05:6a20:3d82:b0:190:c386:3eb4 with SMTP id s2-20020a056a203d8200b00190c3863eb4mr2054589pzi.47.1702343244755; Mon, 11 Dec 2023 17:07:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702343244; cv=none; d=google.com; s=arc-20160816; b=c8+aVIHc3MvEEXjuPwvneqPnoI3B3uFU5e+iLyK8Sfk8k+egC4ivaCIYWd7eB0Td4l x53sc6Qvh9ToAAfwl6wPkCMeGGsYkeJJzYLfYCPyv4JzOusMsthlqiOa8VxtpJfJwF1U jAv2nlCwNzbsL9/eGpawltMknmJDDfidhLiDWPj4Cl35h95qecnvIsWC50om9l1BWWye FJfPeAyHkhuQdypCPkxboQuESQvUN3IzQDQ5NJUTGcegH5DsOWIOdhTqUFwB3k89XweZ hqE5kggL9pf8n2gCDp6M7MtFpfZ/96t2VmRpf+zn4Q7jvQkSfYJUTEevFMU1lDjavaGS 5TqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=d/cWCTzQ+1u8sf594BtBwr5TiC/bLaU/S8GAKRjknjo=; fh=oUZ8u0qXeG/6QqNtzgnz4hUIzIOQOQayYlduACSG0OM=; b=eW/aZwIISD1tcP/D+1pH0Mvr0yOuuKxWYPj6nmvYpx3p8ehmzA1zMUeRL8CXxzN+ux 6oPzHeC1Y/tMabk3dXE0hIdJeJltL9FayMDWJvB/eUcQ2HE8sek2nCVf8CSJzQ+O04/U xP9v8TxQQsG5tN+vzTEJ8D4TV8t/6BMu8pRPxR4LgBCAEiTNRADUZuFvgWUAtuklImCv xlnq2XzZ1dP+wtQYH8fDhYvuo5TcfvHdmO8fYd7uMXbFSMFLf/PZYh0VcWyH744tOMu0 pA+pzoLE5Ts9ouT64sEpP6Fs1tASPz+geA7kGXGmdkSm+TdIgzYWWgONH24EVYY4knkx Neyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=EP1BbKtW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id y7-20020a170902b48700b001d068dfe6e5si6848676plr.31.2023.12.11.17.07.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 17:07:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=EP1BbKtW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 82476807D434; Mon, 11 Dec 2023 17:07:21 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345645AbjLLBHD (ORCPT + 99 others); Mon, 11 Dec 2023 20:07:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345638AbjLLBHC (ORCPT ); Mon, 11 Dec 2023 20:07:02 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4395FAB for ; Mon, 11 Dec 2023 17:07:08 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C5245C43395 for ; Tue, 12 Dec 2023 01:07:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1702343227; bh=MDets05mPCDoqLxyZIXukdeciXvfqDz5mf6mMUdN2sM=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=EP1BbKtWWaBQvGIDk2Hvi9L+UYeZNhtroplbMAv4+6AOEMlK9FiFAJu3sLFOtrRmp rCm3llUhRUAm1nytlNBhtwfWBccJSw6d8IXmR0eL+JmSmtvwk4o4Gmij8s1wCglQV4 9vgx8+yHxEk2+VLCs5cgT5IYk3GhA6ci0ezuKIOUpEzUpE+skYvIaqOmuXr3ypnnUn n58VZNGX97+59IlwUSj5TonA5d0jTtWG2xDRA3dysBuYNy7PzpxWEdb0gBoG60+8R7 mUkQb1FfCcFa8FV4E/I9e6TVTGpjWYpj0SRveBKPqwdkfkja7BJItoB51Ae1PP0Cjl Al12d+wQu0OpQ== Received: by mail-oa1-f48.google.com with SMTP id 586e51a60fabf-1fb33059466so3509750fac.2 for ; Mon, 11 Dec 2023 17:07:07 -0800 (PST) X-Gm-Message-State: AOJu0Yw+LWiF+E1LCbvs2xsXZIzka3Xvj/TsmcjUyrVlY5O7vbnMvCOv In7kMLdJXvQb1SBtuf7+k7bHjBxpqkMHocfI4qePUw== X-Received: by 2002:a05:6358:d598:b0:170:2abc:6e34 with SMTP id ms24-20020a056358d59800b001702abc6e34mr5702692rwb.19.1702343226888; Mon, 11 Dec 2023 17:07:06 -0800 (PST) MIME-Version: 1.0 References: <20231211140419.1298178-1-schatzberg.dan@gmail.com> <20231211140419.1298178-2-schatzberg.dan@gmail.com> In-Reply-To: <20231211140419.1298178-2-schatzberg.dan@gmail.com> From: Chris Li Date: Mon, 11 Dec 2023 17:06:54 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH V3 1/1] mm: add swapiness= arg to memory.reclaim To: Dan Schatzberg Cc: Johannes Weiner , Roman Gushchin , Yosry Ahmed , Huan Yang , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, Tejun Heo , Zefan Li , Jonathan Corbet , Michal Hocko , Shakeel Butt , Muchun Song , Andrew Morton , David Hildenbrand , Matthew Wilcox , Kefeng Wang , "Vishal Moola (Oracle)" , Yue Zhao , Hugh Dickins Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Mon, 11 Dec 2023 17:07:21 -0800 (PST) Hi Dan, Thank you for the patch. On Mon, Dec 11, 2023 at 6:04=E2=80=AFAM Dan Schatzberg wrote: > > Allow proactive reclaimers to submit an additional swappiness=3D > argument to memory.reclaim. This overrides the global or per-memcg > swappiness setting for that reclaim attempt. I am curious what prompted you to develop this patch. I understand what this patch does, just want to know more of your background story why this is needed. > > For example: > > echo "2M swappiness=3D0" > /sys/fs/cgroup/memory.reclaim > > will perform reclaim on the rootcg with a swappiness setting of 0 (no > swap) regardless of the vm.swappiness sysctl setting. > > Signed-off-by: Dan Schatzberg > --- > Documentation/admin-guide/cgroup-v2.rst | 15 ++++++- > include/linux/swap.h | 3 +- > mm/memcontrol.c | 55 ++++++++++++++++++++----- > mm/vmscan.c | 13 +++++- > 4 files changed, 70 insertions(+), 16 deletions(-) > > diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admi= n-guide/cgroup-v2.rst > index 3f85254f3cef..fc2b379dbd0f 100644 > --- a/Documentation/admin-guide/cgroup-v2.rst > +++ b/Documentation/admin-guide/cgroup-v2.rst > @@ -1282,8 +1282,8 @@ PAGE_SIZE multiple when read back. > This is a simple interface to trigger memory reclaim in the > target cgroup. > > - This file accepts a single key, the number of bytes to reclaim. > - No nested keys are currently supported. > + This file accepts a string which containers thhe number of bytes Just as Yosry points out, there is some typo here. "contains the" > + to reclaim. > > Example:: > > @@ -1304,6 +1304,17 @@ PAGE_SIZE multiple when read back. > This means that the networking layer will not adapt based on > reclaim induced by memory.reclaim. > > + This file also allows the user to specify the swappiness value > + to be used for the reclaim. For example: > + > + echo "1G swappiness=3D60" > memory.reclaim > + > + The above instructs the kernel to perform the reclaim with > + a swappiness value of 60. Note that this has the same semantics > + as the vm.swappiness sysctl - it sets the relative IO cost of > + reclaiming anon vs file memory but does not allow for reclaiming > + specific amounts of anon or file memory. > + > memory.peak > A read-only single value file which exists on non-root > cgroups. > diff --git a/include/linux/swap.h b/include/linux/swap.h > index f6dd6575b905..ebc20d094609 100644 > --- a/include/linux/swap.h > +++ b/include/linux/swap.h > @@ -410,7 +410,8 @@ extern unsigned long try_to_free_pages(struct zonelis= t *zonelist, int order, > extern unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *mem= cg, > unsigned long nr_pages, > gfp_t gfp_mask, > - unsigned int reclaim_op= tions); > + unsigned int reclaim_op= tions, > + int swappiness); > extern unsigned long mem_cgroup_shrink_node(struct mem_cgroup *mem, > gfp_t gfp_mask, bool nosw= ap, > pg_data_t *pgdat, > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > index 1c1061df9cd1..74598c17d3cc 100644 > --- a/mm/memcontrol.c > +++ b/mm/memcontrol.c > @@ -63,6 +63,7 @@ > #include > #include > #include > +#include > #include > #include "internal.h" > #include > @@ -2449,7 +2450,7 @@ static unsigned long reclaim_high(struct mem_cgroup= *memcg, > psi_memstall_enter(&pflags); > nr_reclaimed +=3D try_to_free_mem_cgroup_pages(memcg, nr_= pages, > gfp_mask, > - MEMCG_RECLAIM_MAY= _SWAP); > + MEMCG_RECLAIM_MAY= _SWAP, -1); Instead of passing -1, maybe we can use mem_cgroup_swappiness(memcg); And maybe remove the -1 test from the get_scan_count(). "" static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc, unsigned long *nr) { struct pglist_data *pgdat =3D lruvec_pgdat(lruvec); struct mem_cgroup *memcg =3D lruvec_memcg(lruvec); unsigned long anon_cost, file_cost, total_cost; int swappiness =3D sc->swappiness !=3D -1 ? sc->swappiness : mem_cgroup_swappiness(memcg); "" In other words, I feel it is cleaner if try_to_free_mem_cgroup_pages accept the swappiness as it is. There is no second guessing the value if it is -1, then the internal function gets the swappiness from mem_cgroup_swappiness(). Maybe we can completely remove the -1 special default value? > psi_memstall_leave(&pflags); > } while ((memcg =3D parent_mem_cgroup(memcg)) && > !mem_cgroup_is_root(memcg)); > @@ -2740,7 +2741,7 @@ static int try_charge_memcg(struct mem_cgroup *memc= g, gfp_t gfp_mask, > > psi_memstall_enter(&pflags); > nr_reclaimed =3D try_to_free_mem_cgroup_pages(mem_over_limit, nr_= pages, > - gfp_mask, reclaim_opt= ions); > + gfp_mask, reclaim_opt= ions, -1); Same here. > psi_memstall_leave(&pflags); > > if (mem_cgroup_margin(mem_over_limit) >=3D nr_pages) > @@ -3660,7 +3661,7 @@ static int mem_cgroup_resize_max(struct mem_cgroup = *memcg, > } > > if (!try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL, > - memsw ? 0 : MEMCG_RECLAIM_MAY_SWA= P)) { > + memsw ? 0 : MEMCG_RECLAIM_MAY_SWA= P, -1)) { Same here. > ret =3D -EBUSY; > break; > } > @@ -3774,7 +3775,7 @@ static int mem_cgroup_force_empty(struct mem_cgroup= *memcg) > return -EINTR; > > if (!try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL, > - MEMCG_RECLAIM_MAY_SWAP)= ) > + MEMCG_RECLAIM_MAY_SWAP,= -1)) Here too. > nr_retries--; > } > > @@ -6720,7 +6721,7 @@ static ssize_t memory_high_write(struct kernfs_open= _file *of, > } > > reclaimed =3D try_to_free_mem_cgroup_pages(memcg, nr_page= s - high, > - GFP_KERNEL, MEMCG_RECLAIM_MAY_SWA= P); > + GFP_KERNEL, MEMCG_RECLAIM_MAY_SWA= P, -1); Here too. > > if (!reclaimed && !nr_retries--) > break; > @@ -6769,7 +6770,7 @@ static ssize_t memory_max_write(struct kernfs_open_= file *of, > > if (nr_reclaims) { > if (!try_to_free_mem_cgroup_pages(memcg, nr_pages= - max, > - GFP_KERNEL, MEMCG_RECLAIM_MAY_SWA= P)) > + GFP_KERNEL, MEMCG_RECLAIM_MAY_SWA= P, -1)) Here too. > nr_reclaims--; > continue; > } > @@ -6895,6 +6896,16 @@ static ssize_t memory_oom_group_write(struct kernf= s_open_file *of, > return nbytes; > } > > +enum { > + MEMORY_RECLAIM_SWAPPINESS =3D 0, > + MEMORY_RECLAIM_NULL, > +}; > + > +static const match_table_t if_tokens =3D { What this is called "if_tokens"? I am trying to figure out what "if" refers= to. > + { MEMORY_RECLAIM_SWAPPINESS, "swappiness=3D%d"}, > + { MEMORY_RECLAIM_NULL, NULL }, > +}; > + Do we foresee a lot of tunable for the try to free page? I see. You want to use match_token() to do the keyword parsing. > static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf, > size_t nbytes, loff_t off) > { > @@ -6902,12 +6913,33 @@ static ssize_t memory_reclaim(struct kernfs_open_= file *of, char *buf, > unsigned int nr_retries =3D MAX_RECLAIM_RETRIES; > unsigned long nr_to_reclaim, nr_reclaimed =3D 0; > unsigned int reclaim_options; > - int err; > + char *old_buf, *start; > + substring_t args[MAX_OPT_ARGS]; > + int swappiness =3D -1; > > buf =3D strstrip(buf); > - err =3D page_counter_memparse(buf, "", &nr_to_reclaim); > - if (err) > - return err; > + > + old_buf =3D buf; > + nr_to_reclaim =3D memparse(buf, &buf) / PAGE_SIZE; > + if (buf =3D=3D old_buf) > + return -EINVAL; > + > + buf =3D strstrip(buf); > + > + while ((start =3D strsep(&buf, " ")) !=3D NULL) { > + if (!strlen(start)) > + continue; > + switch (match_token(start, if_tokens, args)) { > + case MEMORY_RECLAIM_SWAPPINESS: > + if (match_int(&args[0], &swappiness)) > + return -EINVAL; > + if (swappiness < 0 || swappiness > 200) Agree with Yosry on the 200 magic value. I am also wondering if there is an easier way to just parse one keyword. Will using strcmp("swappiness=3D") be a bad idea? I haven't tried it myself though. > + return -EINVAL; > + break; > + default: > + return -EINVAL; > + } > + } > > reclaim_options =3D MEMCG_RECLAIM_MAY_SWAP | MEMCG_RECLAIM_PROACT= IVE; > while (nr_reclaimed < nr_to_reclaim) { > @@ -6926,7 +6958,8 @@ static ssize_t memory_reclaim(struct kernfs_open_fi= le *of, char *buf, > > reclaimed =3D try_to_free_mem_cgroup_pages(memcg, > min(nr_to_reclaim - nr_reclaimed,= SWAP_CLUSTER_MAX), > - GFP_KERNEL, reclaim_options); > + GFP_KERNEL, reclaim_options, > + swappiness); > > if (!reclaimed && !nr_retries--) > return -EAGAIN; > diff --git a/mm/vmscan.c b/mm/vmscan.c > index 506f8220c5fe..a20965b20177 100644 > --- a/mm/vmscan.c > +++ b/mm/vmscan.c > @@ -136,6 +136,9 @@ struct scan_control { > /* Always discard instead of demoting to lower tier memory */ > unsigned int no_demotion:1; > > + /* Swappiness value for reclaim, if -1 use memcg/global value */ > + int swappiness; > + It would be great if we can get rid of the -1 case. > /* Allocation order */ > s8 order; > > @@ -2327,7 +2330,8 @@ static void get_scan_count(struct lruvec *lruvec, s= truct scan_control *sc, > struct pglist_data *pgdat =3D lruvec_pgdat(lruvec); > struct mem_cgroup *memcg =3D lruvec_memcg(lruvec); > unsigned long anon_cost, file_cost, total_cost; > - int swappiness =3D mem_cgroup_swappiness(memcg); > + int swappiness =3D sc->swappiness !=3D -1 ? > + sc->swappiness : mem_cgroup_swappiness(memcg); Let's see if we can get rid of the -1. Now I see the -1 is actually introduced by this patch, should be doable. > u64 fraction[ANON_AND_FILE]; > u64 denominator =3D 0; /* gcc */ > enum scan_balance scan_balance; > @@ -2608,6 +2612,9 @@ static int get_swappiness(struct lruvec *lruvec, st= ruct scan_control *sc) > mem_cgroup_get_nr_swap_pages(memcg) < MIN_LRU_BATCH) > return 0; > > + if (sc->swappiness !=3D -1) > + return sc->swappiness; > + Get rid of that too. Thank you. Chris > return mem_cgroup_swappiness(memcg); > } > > @@ -6433,7 +6440,8 @@ unsigned long mem_cgroup_shrink_node(struct mem_cgr= oup *memcg, > unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, > unsigned long nr_pages, > gfp_t gfp_mask, > - unsigned int reclaim_options) > + unsigned int reclaim_options, > + int swappiness) > { > unsigned long nr_reclaimed; > unsigned int noreclaim_flag; > @@ -6448,6 +6456,7 @@ unsigned long try_to_free_mem_cgroup_pages(struct m= em_cgroup *memcg, > .may_unmap =3D 1, > .may_swap =3D !!(reclaim_options & MEMCG_RECLAIM_MAY_SWAP= ), > .proactive =3D !!(reclaim_options & MEMCG_RECLAIM_PROACTI= VE), > + .swappiness =3D swappiness, > }; > /* > * Traverse the ZONELIST_FALLBACK zonelist of the current node to= put > -- > 2.34.1 > >