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 B1438C4708C for ; Tue, 6 Dec 2022 01:29:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 082508E0002; Mon, 5 Dec 2022 20:29:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 00BA88E0001; Mon, 5 Dec 2022 20:29:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DC8B98E0002; Mon, 5 Dec 2022 20:29:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id CD2108E0001 for ; Mon, 5 Dec 2022 20:29:08 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 9DAEC8039F for ; Tue, 6 Dec 2022 01:29:08 +0000 (UTC) X-FDA: 80210147976.22.15D3645 Received: from mail-ua1-f46.google.com (mail-ua1-f46.google.com [209.85.222.46]) by imf23.hostedemail.com (Postfix) with ESMTP id 3E42A140004 for ; Tue, 6 Dec 2022 01:29:08 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=TdeU6qpg; spf=pass (imf23.hostedemail.com: domain of yuzhao@google.com designates 209.85.222.46 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=1670290148; 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=pSDhDqOudVpfHmVoymf2tIcEElKGOfpGSM98qvPhOzI=; b=2BiNxgt+ELJywUpi5XSnwsYfBwJeMALsyeWpFHxCXG1UiAQ4B+q698IihAHRA/Bg6gnvbi YgS+FSvhk7JZgP/9fy9e9FuCuYjdfd/4r8YYhlNtG/EV+5Rk15KSVGxV85hhga50rru4Pt 7H0LBuXL2bc2ci80SKkNdxyXhn+4tqs= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=TdeU6qpg; spf=pass (imf23.hostedemail.com: domain of yuzhao@google.com designates 209.85.222.46 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=1670290148; a=rsa-sha256; cv=none; b=PqtzCmHeWEN8RheRUZzkg5d7vk/IP9sZJe5ZUcFOTAO4tWBWlRGy76I/xkfy0t7Lqxt0KS mq6skIuAHkRX5eGOsuZPhYmHEFQTSyA+kVgG71Id9zfwD+rv+fww9RDsvjgJaSXznmHDkI UBVJTsvIzcQXAkD563wyMg0JpOvDFyA= Received: by mail-ua1-f46.google.com with SMTP id q22so1966601uap.7 for ; Mon, 05 Dec 2022 17:29:07 -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=pSDhDqOudVpfHmVoymf2tIcEElKGOfpGSM98qvPhOzI=; b=TdeU6qpgv5peqyVsBJhLUvZJvLuXvVK/AE9Ue6gk8DvQWSafztvlow4ohux0JQWHOm Dfjhq3nITLPlKvATxZbufa5m0JgRhj1w77D7ope4tYypzgJev4e6PiZbKeH2dKSJRPC1 Cxy71OyarL2lcpSkUjjvStploQC8AZZqpsMsYBZ+HxofhT5UDe7AcXif+JdX+aEn4I9Z 8fmr/uoSOWKAqILq2M2sYX8kSsz2pfMOPGueqxKopbl4cLqwlJ+whwxoTmBYBXySFzG5 vYdvy8EOGfYgRlskfcmqMNzL/SwYaqrBJkUjDc6fhdNEz3iyE710lpjNEG9G+9ppR34A wUYA== 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=pSDhDqOudVpfHmVoymf2tIcEElKGOfpGSM98qvPhOzI=; b=l/5KIyDXSZ8xWjEzcIPIVbsvkvgOHOhr6AoPMrEAR3yVx2dXRyLHFdNJDE1rRh+Pz3 DsP0+bcc1KCttNG7YuhtxNLJyrJNOv3c0D0EAylnhcdVnqCzb9VNJiQV7Sy6xiIwMejV zkHb7S4u50BKj5z9nOJjwxbwmt1jYDCIqZtvfi/p03Toiq9dw+T9oMbBexJ1iyAowWFt 7JZCcs8m6Vp6hy2p2K7oA3TWIMuY8Oqndr9J4IdXDJeMITR/i4g4xWHHvnLxCQBg+28U rze1gW5/Gg8DeAIa2Fev1aiR/SqNemShmFMLKkDxfCyQtHHeTj7TU7fQ4chMsaLJMkf7 2Caw== X-Gm-Message-State: ANoB5pl1d8h/Qc0ODyTTM8gnvbtXARsu9QoTxI6eiQMrRGXDta87Mc9o vaYG7z2HAoii1S5ol440ozn531gcY1W4wQhXx8SPFQ== X-Google-Smtp-Source: AA0mqf6zhsb8ptPE9bRgCtcTDeQeR35GW+Hd02TEKsTRcWZ4+gCd8Srr097Kknn287pti4r/o9sNTOnzTe6BQAvDRuA= X-Received: by 2002:ab0:6f52:0:b0:419:1fa3:9618 with SMTP id r18-20020ab06f52000000b004191fa39618mr23490504uat.11.1670290147298; Mon, 05 Dec 2022 17:29:07 -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 18:28:31 -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-Queue-Id: 3E42A140004 X-Stat-Signature: uox86cia4e584y3w4tif6yi6q3kyqpfx X-Rspam-User: X-Spamd-Result: default: False [-2.90 / 9.00]; BAYES_HAM(-6.00)[100.00%]; SORBS_IRL_BL(3.00)[209.85.222.46: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-Rspamd-Server: rspam08 X-HE-Tag: 1670290148-64815 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 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.) > I believe that if I don't do the refactoring, I'll have to repeat the > unused parameters in the syscall, and make unpack_shadow > a non-static function (along with all the locally defined macros like > WORKINGSET_SHIFT). I think it would get quite messy there too. > > But more importantly, I'm a bit concerned that the logic for > determining the recency of the eviction might change in the > future, which would break the cachestat syscall unknowingly... > > Let me know what you think about this, Yu! Your call :)