From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mta1.migadu.com (out-47.mta1.migadu.com [95.215.58.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 62D8430FF2A for ; Tue, 18 Aug 2026 02:40:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787020844; cv=none; b=MNTLSqFzOS/uChRcjabNYthKKyARYVNB0A1rqFxFGiROWVWpHEPX2lX53ZffaK0AgikqdRSiTUzp/mMrJCEsXUDjzzzAfGUucoexZTPAA600q7iIM9x/Z2znPNGz8+NG8bM1y8UXT0WNm5fCaZAn3zwhE/KW+Nh6MXGdzBPDn68= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787020844; c=relaxed/simple; bh=VjTzbzJ5U7fulfyPD/S6iBjuQ+IHP/erX76i5LQT7Og=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=gH0m/OEylhahs9ZuCM0829nRAVYj4v0d3bJncFnaRo9gjzxQj6t4PA9SV+lktSYyW+Ez76RfEazxfPBKLmp4W5cu7fF0sIYpb3WTzfRmYJgiAh14jr8/YLlnp0NZdOHsbyoL3oDQiXXPN6Qbb4hLNQJlbCo115oQkhw4WrOjD1I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=c2dFeVSl; arc=none smtp.client-ip=95.215.58.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="c2dFeVSl" X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=VjTzbzJ5U7fulfyPD/S6iBjuQ+IHP/erX76i5LQT7Og=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1787020839; v=1; x=1787625639; b=c2dFeVSlgzJ1LnSRu8P9fBpSd4jqTo2vYQvM6hdrbUFBIIDrVmSnSro3n02v+JSv8ezhn66w plwXV3TZatHPXhHqounD+WSYxk87PcuyIo6T6F6zBqMuG36jem8Kzo8YAQChd6LWLmnna+X4SMr u+88g73pEEggZp4uzZs6Gf6Y= X-Envelope-To: linux-kernel@vger.kernel.org Received: from localhost (3.112.29.171) by mta11.migadu.com with ESMTPS id 0adc373bf01fa8f4; Tue, 18 Aug 2026 02:40:39 +0000 X-Migadu-Flow: FLOW_OUT Date: Tue, 18 Aug 2026 10:40:35 +0800 From: Baoquan He To: kasong@tencent.com Cc: linux-mm@kvack.org, Andrew Morton , Johannes Weiner , Muchun Song , Qi Zheng , Ying Huang , Chris Li , Nico Pache , Usama Arif , Michal Hocko , Roman Gushchin , Shakeel Butt , David Hildenbrand , Lorenzo Stoakes , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Vlastimil Babka , Suren Baghdasaryan , Kemeng Shi , Nhat Pham , Youngjun Park , Zi Yan , Gregory Price , "Matthew Wilcox (Oracle)" , Baolin Wang , Ryan Roberts , Dev Jain , Lance Yang , Hugh Dickins , SeongJae Park , David Rientjes , Yu Zhao , Vernon Yang , Zicheng Wang , Chen Ridong , Tal Zussman , Kairui Song , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org Subject: Re: [PATCH RFC 04/15] mm/mglru: make generation page counters atomic Message-ID: References: <20260804-mglru-fg-v1-0-4d8dad39dad6@tencent.com> <20260804-mglru-fg-v1-4-4d8dad39dad6@tencent.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260804-mglru-fg-v1-4-4d8dad39dad6@tencent.com> On 08/04/26 at 03:47am, Kairui Song via B4 Relay wrote: > From: Kairui Song > > No feature change, convert them to atomic so we can update them without > holding the LRU lock. There is no risk of overflow. The reader always > compares and uses zero instead if the counter values are negative. It > follows final consistency. It's better to tell this is a preparation patch, and who will use them in the coming patch or function. Otherwise we can't see why we need it if it's no feature change. The code change looks good to me. > > Signed-off-by: Kairui Song > --- > include/linux/mm_inline.h | 6 ++---- > include/linux/mmzone.h | 2 +- > mm/vmscan.c | 20 ++++++++++---------- > 3 files changed, 13 insertions(+), 15 deletions(-) > > diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h > index 4076e3f7dcc8..018a2f54a5c9 100644 > --- a/include/linux/mm_inline.h > +++ b/include/linux/mm_inline.h > @@ -245,11 +245,9 @@ static inline void lru_gen_update_size(struct lruvec *lruvec, struct folio *foli > VM_WARN_ON_ONCE(old_gen == -1 && new_gen == -1); > > if (old_gen >= 0) > - WRITE_ONCE(lrugen->nr_pages[old_gen][type][zone], > - lrugen->nr_pages[old_gen][type][zone] - delta); > + atomic_long_sub(delta, &lrugen->nr_pages[old_gen][type][zone]); > if (new_gen >= 0) > - WRITE_ONCE(lrugen->nr_pages[new_gen][type][zone], > - lrugen->nr_pages[new_gen][type][zone] + delta); > + atomic_long_add(delta, &lrugen->nr_pages[new_gen][type][zone]); > > /* addition */ > if (old_gen < 0) { > diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h > index 8048c6b0544d..4225dab760ba 100644 > --- a/include/linux/mmzone.h > +++ b/include/linux/mmzone.h > @@ -572,7 +572,7 @@ struct lru_gen_folio { > /* the multi-gen LRU lists, lazily sorted on eviction */ > struct list_head folios[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES]; > /* the multi-gen LRU sizes, eventually consistent */ > - long nr_pages[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES]; > + atomic_long_t nr_pages[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES]; > /* the exponential moving average of refaulted */ > unsigned long avg_refaulted[ANON_AND_FILE][MAX_NR_TIERS]; > /* the exponential moving average of evicted+protected */ > diff --git a/mm/vmscan.c b/mm/vmscan.c > index f5b0a7c63a3a..b02d2ec8ff4b 100644 > --- a/mm/vmscan.c > +++ b/mm/vmscan.c > @@ -3354,8 +3354,7 @@ static void reset_batch_size(struct lru_gen_mm_walk *walk) > continue; > > walk->nr_pages[gen][type][zone] = 0; > - WRITE_ONCE(lrugen->nr_pages[gen][type][zone], > - lrugen->nr_pages[gen][type][zone] + delta); > + atomic_long_add(delta, &lrugen->nr_pages[gen][type][zone]); > > if (lru_gen_is_active(lruvec, gen)) > lru += LRU_ACTIVE; > @@ -4044,8 +4043,8 @@ static bool inc_max_seq(struct lruvec *lruvec, unsigned long seq, int swappiness > for (type = 0; type < ANON_AND_FILE; type++) { > for (zone = 0; zone < MAX_NR_ZONES; zone++) { > enum lru_list lru = type * LRU_INACTIVE_FILE; > - long delta = lrugen->nr_pages[prev][type][zone] - > - lrugen->nr_pages[next][type][zone]; > + long delta = atomic_long_read(&lrugen->nr_pages[prev][type][zone]) - > + atomic_long_read(&lrugen->nr_pages[next][type][zone]); > > if (!delta) > continue; > @@ -4163,7 +4162,8 @@ static unsigned long lruvec_evictable_size(struct lruvec *lruvec, int swappiness > for (seq = min_seq[type]; seq <= max_seq; seq++) { > gen = lru_gen_from_seq(seq); > for (zone = 0; zone < MAX_NR_ZONES; zone++) > - total += max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L); > + total += max(atomic_long_read(&lrugen->nr_pages[gen][type][zone]), > + 0L); > } > } > > @@ -4598,7 +4598,7 @@ static void __lru_gen_reparent_memcg(struct lruvec *child_lruvec, struct lruvec > > for (i = 0; i < get_nr_gens(child_lruvec, type); i++) { > int gen = lru_gen_from_seq(child_lrugen->max_seq - i); > - long nr_pages = child_lrugen->nr_pages[gen][type][zone]; > + long nr_pages = atomic_long_read(&child_lrugen->nr_pages[gen][type][zone]); > int child_lru_active = lru_gen_is_active(child_lruvec, gen) ? LRU_ACTIVE : 0; > int parent_lru_active = lru_gen_is_active(parent_lruvec, gen) ? LRU_ACTIVE : 0; > > @@ -4606,9 +4606,8 @@ static void __lru_gen_reparent_memcg(struct lruvec *child_lruvec, struct lruvec > list_splice_tail_init(&child_lrugen->folios[gen][type][zone], > &parent_lrugen->folios[gen][type][zone]); > > - WRITE_ONCE(child_lrugen->nr_pages[gen][type][zone], 0); > - WRITE_ONCE(parent_lrugen->nr_pages[gen][type][zone], > - parent_lrugen->nr_pages[gen][type][zone] + nr_pages); > + atomic_long_set(&child_lrugen->nr_pages[gen][type][zone], 0); > + atomic_long_add(nr_pages, &parent_lrugen->nr_pages[gen][type][zone]); > > if (lru_gen_is_active(child_lruvec, gen) != lru_gen_is_active(parent_lruvec, gen)) { > __update_lru_size(child_lruvec, lru + child_lru_active, zone, -nr_pages); > @@ -5650,7 +5649,8 @@ static int lru_gen_seq_show(struct seq_file *m, void *v) > char mark = full && seq < min_seq[type] ? 'x' : ' '; > > for (zone = 0; zone < MAX_NR_ZONES; zone++) > - size += max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L); > + size += max(atomic_long_read(&lrugen->nr_pages[gen][type][zone]), > + 0L); > > seq_printf(m, " %10lu%c", size, mark); > } > > -- > 2.55.0 > >