Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp6814567rwb; Mon, 5 Dec 2022 18:53:33 -0800 (PST) X-Google-Smtp-Source: AA0mqf5spQsMY7ftOcaJY4bLbhjeF5X4B/AzM0E0vhl942ZZNn19yx8HCcROjj32FDDk7mPzsJpW X-Received: by 2002:a05:6402:d74:b0:46d:1a33:2197 with SMTP id ec52-20020a0564020d7400b0046d1a332197mr485331edb.282.1670295213067; Mon, 05 Dec 2022 18:53:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670295213; cv=none; d=google.com; s=arc-20160816; b=KN4J+J1DLSjil2wWWHQD6KVwXSBZxUjGNSFwBbEc6f31PLKAm9qiBRlSCAW8HJ0GHy GtWWdQyPUDkNo1GpJkzV5XqMFCCVnH5RFO0cUK7MKVyT2gj7DT6xz77nQgd04KeixRWK ciMcet4FfTeDRHcA/JIdGosdjo5I7OQkq7ocKWDS0zt27kiN7kiatLn7Rl5/I6wyxL/a l918Rcf/VwaaF6Bm7R1DNPEThdOhD0zY1mjGupcB9udRTUXISe78W6pWhY5+nQjFu+PE ixXBeWf9QBoBip85gXDWFt042J4rQWAD234Ed0IqBgMpjGlTLLzcM0dq0WIqB0USrNrf z+gw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=KeehAapm7ZXRZtRJIFZHdeIO9R9klFHDW166tK18b2A=; b=uKhxIBFoDfgpwbliVElSviffcOKFjMlSbi93QDMcn0C1/FmELn6Akgd/TeT12TaMtm fD0GcmAPdhNUSOHUyyRk3Zt3aib7Yj0Vp67vH9ZoVqIg3jmd9xMDOzNecDdlTtPvIxD5 ZDyIgu6n8G07plpiBKQvYuYTToO+MHLz6b/ejdBrmYtXOolR5/ChyKkvHWc7Pt6OeLud 1pswx9acTao75KzBK1b1AJA1LrjwbYRuO8HWkDFlyztGB4U/QsCqCXh1TWOaGEs+ixin XX5JH6PxOhdDbXP9D1+Gc9ruBEmIRwuUOhsQIoWtIBQkturzGjMAlULmWKlNbKBZ2EyV Vbgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="dbIpZ/GO"; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nd32-20020a17090762a000b007a6c5a23a48si13724998ejc.551.2022.12.05.18.53.14; Mon, 05 Dec 2022 18:53:33 -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=@gmail.com header.s=20210112 header.b="dbIpZ/GO"; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233752AbiLFCW1 (ORCPT + 80 others); Mon, 5 Dec 2022 21:22:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45032 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231990AbiLFCWZ (ORCPT ); Mon, 5 Dec 2022 21:22:25 -0500 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8AB6425295 for ; Mon, 5 Dec 2022 18:22:24 -0800 (PST) Received: by mail-wr1-x42d.google.com with SMTP id d1so21514990wrs.12 for ; Mon, 05 Dec 2022 18:22:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=KeehAapm7ZXRZtRJIFZHdeIO9R9klFHDW166tK18b2A=; b=dbIpZ/GO5LIsCEE5CILRjO56Zfii5s9R4pXrRGRZFoiV88SQ11KOM4GkYhLbWalPS7 48vGdxyoMLUbwQe58AvBpuV7U/cbYY8lNwJ6/Dntibzv24BBM/3TiC3sJwB1NRIwYr9I kcE2x9Az35xvFv7zaXvA3z+E2W/FMiyOQ9t0C/a2BEEHgCBhc63hBlVfNct6gtx7aFaB 9WeN/4FKISvaZqrwB/4yKK7dKByBHPFf04oI9eFsuHeZ3PIIK5J3tI0sPUntFM7JnBKV IoDmrN2CYsvxYS9ro/G1oqMAhAPbd/OE65WQNVWUT6li3GnL+wjmnQfwA+eT2O9inOZW k/WA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=KeehAapm7ZXRZtRJIFZHdeIO9R9klFHDW166tK18b2A=; b=srhL2ak16t095rab+yX8mBmf2IIYM/GSpSLmgAWXx1ie5bfjbdfB7X0wLtaulZEaQu ikUsU2Inxk2pe7aMb7wz6ucxVLqnOqGRgsJBbWOeRDhtkz+O3IIVs73odSXGwPUl+x6F amBHin7RaRg4YYU+nJZtq+FpfP85nZtJ3P6GaxT0gnDl8kLy2YqlRHUepgOSNlX4HLPz O5sj/n5EwMmWa7qRQ4sDSPFCL7REbS6DuxKwVhXU+QN9xsJ7sdTXMMooiIrd9RW5tNDG g4/raxd52mstVmDYQkmetGe/0Z1VY7Tn0z6reaRMkWLaWlrEnZ15n4oGOvVVsvKIcCew cfaQ== X-Gm-Message-State: ANoB5pnm23dp652CpKaiMt75wHxdXsxAjpRA+ihlx45LJIY1wDborfA5 i3in167NsXgPE/8z/yeZ/MEycICMFxj9+1Ii7fU= X-Received: by 2002:a5d:4b45:0:b0:242:1ade:1145 with SMTP id w5-20020a5d4b45000000b002421ade1145mr22524441wrs.483.1670293342943; Mon, 05 Dec 2022 18:22:22 -0800 (PST) MIME-Version: 1.0 References: <20221205175140.1543229-1-nphamcs@gmail.com> <20221205175140.1543229-3-nphamcs@gmail.com> In-Reply-To: From: Nhat Pham Date: Mon, 5 Dec 2022 18:22:11 -0800 Message-ID: Subject: Re: [PATCH v2 2/4] workingset: refactor LRU refault to expose refault recency check To: Yu Zhao Cc: akpm@linux-foundation.org, hannes@cmpxchg.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, bfoster@redhat.com, willy@infradead.org, kernel-team@meta.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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 On Mon, Dec 5, 2022 at 5:29 PM Yu Zhao wrote: > > On Mon, Dec 5, 2022 at 6:19 PM Nhat Pham wrote: > > > > On Mon, Dec 5, 2022 at 3:49 PM Yu Zhao wrote: > > > > > > On Mon, Dec 5, 2022 at 10:51 AM Nhat Pham wrote: > > > > > > > > In preparation for computing recently evicted pages in cachestat, > > > > refactor workingset_refault and lru_gen_refault to expose a helper > > > > function that would test if an evicted page is recently evicted. > > > > > > > > Signed-off-by: Nhat Pham > > > > --- > > > > include/linux/swap.h | 1 + > > > > mm/workingset.c | 143 +++++++++++++++++++++++++++++-------------- > > > > 2 files changed, 99 insertions(+), 45 deletions(-) > > > > > > > > diff --git a/include/linux/swap.h b/include/linux/swap.h > > > > index a18cf4b7c724..dae6f6f955eb 100644 > > > > --- a/include/linux/swap.h > > > > +++ b/include/linux/swap.h > > > > @@ -361,6 +361,7 @@ static inline void folio_set_swap_entry(struct folio *folio, swp_entry_t entry) > > > > } > > > > > > > > /* linux/mm/workingset.c */ > > > > +bool workingset_test_recent(void *shadow, bool file, bool *workingset); > > > > void workingset_age_nonresident(struct lruvec *lruvec, unsigned long nr_pages); > > > > void *workingset_eviction(struct folio *folio, struct mem_cgroup *target_memcg); > > > > void workingset_refault(struct folio *folio, void *shadow); > > > > diff --git a/mm/workingset.c b/mm/workingset.c > > > > index 79585d55c45d..44b331ce3040 100644 > > > > --- a/mm/workingset.c > > > > +++ b/mm/workingset.c > > > > @@ -244,6 +244,30 @@ static void *lru_gen_eviction(struct folio *folio) > > > > return pack_shadow(mem_cgroup_id(memcg), pgdat, token, refs); > > > > } > > > > > > > > +/* > > > > + * Test if the folio is recently evicted. > > > > + * > > > > + * As a side effect, also populates the references with > > > > + * values unpacked from the shadow of the evicted folio. > > > > + */ > > > > +static bool lru_gen_test_recent(void *shadow, bool file, int *memcgid, > > > > + struct pglist_data **pgdat, unsigned long *token, bool *workingset) > > > > +{ > > > > + struct mem_cgroup *eviction_memcg; > > > > + struct lruvec *lruvec; > > > > + struct lru_gen_struct *lrugen; > > > > + unsigned long min_seq; > > > > + > > > > + unpack_shadow(shadow, memcgid, pgdat, token, workingset); > > > > + eviction_memcg = mem_cgroup_from_id(*memcgid); > > > > + > > > > + lruvec = mem_cgroup_lruvec(eviction_memcg, *pgdat); > > > > + lrugen = &lruvec->lrugen; > > > > + > > > > + min_seq = READ_ONCE(lrugen->min_seq[file]); > > > > + return !((*token >> LRU_REFS_WIDTH) != (min_seq & (EVICTION_MASK >> LRU_REFS_WIDTH))); > > > > +} > > > > > > Nit: not refactoring actually looks cleaner to me -- there are only a > > > few lines of duplicated code and you can get rid of 4 parameters > > > including the unused workingset in the next patch. > > > > (resending this because I forgot to forward to the rest of the > > group!) > > > > Thanks for the comment, Yu! > > > > Personally, I prefer refactoring this piece of logic - I do think that > > it is cleaner than copying the logic into the syscall implementation. > > Let me clarify. > > You can add > lru_gen_test_recent(void *shadow, bool file) > without refactoring the existing > lru_gen_refault(). > > Set the boilerplate aside, you only repeat one line of code, which is > the last line in the new function. > > (The boilerplate code is repeated in many places, and that's why it's > called boilerplate.) Oh maybe I've misunderstood your original comment. Your suggestion is to repeat the eviction recency check in workingset.c (i.e keep the *_gen_refault unchanged - just copy that logic to *_test_recent)?