From mboxrd@z Thu Jan 1 00:00:00 1970 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 29E94C4708C for ; Tue, 6 Dec 2022 02:26:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 894628E0002; Mon, 5 Dec 2022 21:26:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8448C8E0001; Mon, 5 Dec 2022 21:26:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 70C4D8E0002; Mon, 5 Dec 2022 21:26:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 620CA8E0001 for ; Mon, 5 Dec 2022 21:26:27 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 311731405C8 for ; Tue, 6 Dec 2022 02:26:27 +0000 (UTC) X-FDA: 80210292414.16.C05CB1A Received: from mail-vs1-f47.google.com (mail-vs1-f47.google.com [209.85.217.47]) by imf21.hostedemail.com (Postfix) with ESMTP id D10281C000C for ; Tue, 6 Dec 2022 02:26:26 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=rFudDs4g; spf=pass (imf21.hostedemail.com: domain of yuzhao@google.com designates 209.85.217.47 as permitted sender) smtp.mailfrom=yuzhao@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1670293586; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hdjfrrxtP9xiGB9sAYuCv9FJ5prKvsWKlbLWo8hywZk=; b=IHw0XZvL6jYVZ8YOEpqAaY0zIipUE0vfmSmWUsRNLcXVfPp4rE+0ta8R9L8gQ22nscPAhk Ma4PvlTNINQGpG+x0RYB/xZaE2G158FaWTLdakyFW9gKg+DfA9jqGuiz2a35gw40jAg++9 wvdVutaZdp16RvFeKH2a2GfOWscAbDE= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=rFudDs4g; spf=pass (imf21.hostedemail.com: domain of yuzhao@google.com designates 209.85.217.47 as permitted sender) smtp.mailfrom=yuzhao@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670293586; a=rsa-sha256; cv=none; b=PTAYeqlBIqPLv5MO28yz8Vfa/d5wQh/2VgR2TT7erLaXZZFXYYJlP1arPMK8Oj3CZ547RJ Ezlzq75F6Ey3hKbJtJufq537NTMnho1LP5pJtVs8RjhTo7l04ZbaMYLnWFuk0vUVkZdB0M UhpW2LZUCUwi0VomEcKaS4G17FXLTgs= Received: by mail-vs1-f47.google.com with SMTP id 125so12909340vsi.9 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=sbY8YbLREUQuE+QXZe1+PKBW9L1kG/RTREFRkpigomSVkmrwAc+Y/BqeH89OnN8iN0 sL9irqFjsWxXjwKRSQjl4v92qnGx4GFyhPithvVAlmWy8Sn5QCZtD+pEC1cbKJrck2zI 81fYra5iWDtogcTTw6ktfggEuWMW3yGO0wduFMApQ3/313Lvti/GDmjvwxpQOBCNhUJU teYItgxSGiuDzz1wbWW3fVVNQOwcH0Y1NsH3kyWM1BktXnMtC4zDoul9xDhrO7gGFQtn yaXUJS0p5s54MHltbqQbsRQpSkPwMmx5jp+htQiiOQveI9XJFyrUE97/y/6Fmd+Csahm Ebgg== X-Gm-Message-State: ANoB5pm+fo98w2yeOAJ8kaVRndUOl9RKGehroDrGSX/Lw+XiK3APcW4I Bp6YJPoolm3+CBAjhhIIIDBnHn03AyUJvfUyfhD6vw== X-Google-Smtp-Source: AA0mqf4dMxjD2xVUW+vJG45m0GKek5Tavy/kzCRTDt4WgkzUSop7V8KvOixu0hG8WB3T39+NySDNeippx4ybT44fRZg= 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-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: D10281C000C X-Stat-Signature: bw15jib433t85mrd545on9brfj8y94ix X-Spamd-Result: default: False [-2.90 / 9.00]; BAYES_HAM(-6.00)[100.00%]; SORBS_IRL_BL(3.00)[209.85.217.47:from]; BAD_REP_POLICIES(0.10)[]; RCVD_NO_TLS_LAST(0.10)[]; MIME_GOOD(-0.10)[text/plain]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; FROM_EQ_ENVFROM(0.00)[]; DMARC_POLICY_ALLOW(0.00)[google.com,reject]; RCPT_COUNT_SEVEN(0.00)[8]; DKIM_TRACE(0.00)[google.com:+]; TO_MATCH_ENVRCPT_SOME(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[linux-mm@kvack.org]; R_DKIM_ALLOW(0.00)[google.com:s=20210112]; ARC_SIGNED(0.00)[hostedemail.com:s=arc-20220608:i=1]; FROM_HAS_DN(0.00)[]; R_SPF_ALLOW(0.00)[+ip4:209.85.128.0/17]; TO_DN_SOME(0.00)[]; ARC_NA(0.00)[] X-Rspam-User: X-HE-Tag: 1670293586-156715 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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.