Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp6832675rwb; Mon, 5 Dec 2022 19:14:52 -0800 (PST) X-Google-Smtp-Source: AA0mqf4OjXw2ebmAKeikL/i4zUTIK9QAlVjL8i1vVXywBMSePJQqArHWiJuBmqWJp/F4brZQgsXI X-Received: by 2002:a17:907:2721:b0:7bd:28b5:2fb with SMTP id d1-20020a170907272100b007bd28b502fbmr40654374ejl.169.1670296492653; Mon, 05 Dec 2022 19:14:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670296492; cv=none; d=google.com; s=arc-20160816; b=NWsPZq/J8LmM4Hh9fHq4iKE3h+/b8ihj9EnUbn5STOLlsPK8oiPwARtBafXO7znDFy TtKmvkBCAD8CPNCVDaw6HxwL2huZEokRwhL4p9h5W8kTzk0CoHTDkkgxMIYc5yYSi5bX GDg8A43g8WCUvO2rbJsprn66w+oDxERCjhav8ZobTbb4s0wJUrZv+0YiOdbOr6xgICPm 793m+gfsqusfiNVn1/LHes75ZA9+cOuubQwJ+OYk2a0REYn8mW7Ol9/83ErqTEuPh6Hj A4gMkBoY6Cly9hZFCdiuEJ3RUUlT/6g77zuCiWH7XtLqEoLOH8vPIrlsOvYQNqQyvEzX vHgg== 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=hdjfrrxtP9xiGB9sAYuCv9FJ5prKvsWKlbLWo8hywZk=; b=seV2H/xAqVcMwzM//IDxiokrRdYUgaun9mDyA3dzcjCvM4d34HOsFa0ITEVFoiJECu ZQlfDRU9mS0Z1Ahoq+Sb8o0hVHjmuUhdvtO3H5YyU2uKFtC9rea2olNnfPOAdx+cLRh5 zWOjAelavDu1bEsCNybE6VAjVj0p8EFRSxqeJ+ECWaawPdxbiJvtuhPOZZIQLOmwhznG MzZGRnmP+p5KtO1TPIdeW/jjbJjvq0ELiJYzAH7VDHLDStlcAf5y0mlR3lvWa2po9HHH TGgpQ0m3u/gF+YlrHc6irTq6V567Q2r7kbDxcZe56xkN4DEW7JxAy2KJxeaN9SPsrwD/ VEWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=rFudDs4g; 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 m12-20020a056402510c00b0046844a19058si1129234edd.288.2022.12.05.19.14.33; Mon, 05 Dec 2022 19:14:52 -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=rFudDs4g; 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 S233178AbiLFC0a (ORCPT + 79 others); Mon, 5 Dec 2022 21:26:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230090AbiLFC02 (ORCPT ); Mon, 5 Dec 2022 21:26:28 -0500 Received: from mail-vs1-xe30.google.com (mail-vs1-xe30.google.com [IPv6:2607:f8b0:4864:20::e30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E9FEE1B9D8 for ; Mon, 5 Dec 2022 18:26:26 -0800 (PST) Received: by mail-vs1-xe30.google.com with SMTP id k185so12943994vsc.2 for ; Mon, 05 Dec 2022 18:26:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.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=hdjfrrxtP9xiGB9sAYuCv9FJ5prKvsWKlbLWo8hywZk=; b=rFudDs4ghfzBxbrDDIPjfBamw4oCPgAV9f2T2ysykHhbtl7xlYErKn042CJj3e/C7Y RkzF7fWHAa0JoZKvQbb3pJJjxSwibbfnAhyj5zCC1ghH0eKdN4c9IKI/tp0GEl6bDL+z cWCXVcBX4LNgkVK21EZVmlMW+kpSHkIUwS7ZfqehQQC5MHSPlYSDHiEIGFJN0WoH0uJt y/y4/fYAhwnzRnAeN/Ri2Wu0WEm/em6nn8s3J0NmpboI+f3SyAY+T2WtyReflppAQT2d ZJEa+LOQGxlwafQ8f9V9LUqOJV3Hv+2IzAoMCHJRSzEvJEOJ4i66tGScEmyWnZq9QfXf BZmQ== 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=hdjfrrxtP9xiGB9sAYuCv9FJ5prKvsWKlbLWo8hywZk=; b=GhV7wNzqW1TYrmOwe3rtkx2TUrt5orj79HvR+mGTdSjnOGmzBwz2M1rgY1KUCxXRmu LcGSIRFveijPmo3ADbsoHYw+ouuy+YqMUNozCnUkFaIKvkwK4hRzTakXCtzDi6URNHKI r7W895tmRmePjnY/czwDq9o706A7beFeHsxQHRJ+Yl6IvfkfJ2+Ez0EFhDAJfO5S1EKc yxmlBxYHsZ18KJjupdBzarHIzqmRMlkaQC0ZwZa1usuyBVKRuKYtG7efmxkpsez7Q9AO 5lh2MWY9RifOq9QQ714FzhKso+vxIr/HeT1wmLSvsmoyt6SG7X7XiBYA0dZhvXNq/iSA SvXw== X-Gm-Message-State: ANoB5plEbFTjjzXt2BAiKWS4KYk6W605AEBhPtGxzO0Jx7t1zxZkm3q6 vnq4Rg2IT5Qr3rGM2OrhskkslGd0LfhB4l8ElZCn1Q== X-Received: by 2002:a67:c98e:0:b0:3ad:3d65:22b with SMTP id y14-20020a67c98e000000b003ad3d65022bmr38798394vsk.65.1670293585855; Mon, 05 Dec 2022 18:26:25 -0800 (PST) MIME-Version: 1.0 References: <20221205175140.1543229-1-nphamcs@gmail.com> <20221205175140.1543229-3-nphamcs@gmail.com> In-Reply-To: From: Yu Zhao Date: Mon, 5 Dec 2022 19:25:49 -0700 Message-ID: Subject: Re: [PATCH v2 2/4] workingset: refactor LRU refault to expose refault recency check To: Nhat Pham 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=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_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 On Mon, Dec 5, 2022 at 7:22 PM Nhat Pham wrote: > > 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)? Correct.