From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 430724746D4; Thu, 10 Sep 2026 16:42:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789058581; cv=none; b=SdrHRTUafQkTV4r4sd39jXrKLxXbHEDriwfLH0MB2XqZfzPm8gH1wBT3X6t9ELyUbYiPhPlC9wooZ0k6ztdnpFQ1L8jMbKH9x8wswvtbULKT6QFFcx9iWX2qetwPOEOmraFgBDsfjyhEn9cAml0nqvIF5l988fIfRtXEGgILfDA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789058581; c=relaxed/simple; bh=8Ri/Spiu7X3VxJRCZ/3Kkau/nCdLiYyhq30upHZz5ug=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=na8GSRraqjkhn+F5Wh+UcHLoOdFn10D1c4Q+XrsFjN9+DLMM9wCkB4rDfpMGdusBA/6ArNEtK8sL7FqL4Y57hC1DiAG2UcjqYDR2izGZm4sgarTdwmUpdCXLe+AxUPZ9N8SEl7SkAaTccCnb+Rb7z6yPMjBW6TBSBqLWesiJwls= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Mx6t+Yj0; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Mx6t+Yj0" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B40211F000FF; Thu, 10 Sep 2026 16:42:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1789058578; bh=u+08F42gZ1mjtG948ilU+qH+waoqzEUJrlsSmxp12pg=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=Mx6t+Yj0PeebzMuIJqPgec3En+yEc3jysRhyQVcO/3xYfd6qRuj4KeQqGVNCzS2eL tCtcfzvlsBuCl4wxQp6RjOgXiieppplEvlrHF+GHBK62/oFrGU48yOaMvfA3o8/DKl lp6zHFol4XfSS/1l/cU6JxsVOB1VRQHK9uwzb3UaviBuy4O13O//74V+0i7C/ekLSM xY/O3Idda5zMFFYFFSaqcOcGCxMfdZ0Giof01bXvDxALY83lMxS2B1Z6K0pJsE++qd 8ttfLDK+ZFgQeeUlUGkjRs+XrSEr2l8SmGBsKMUNs/VdxT4ev53Rb0h/F/7fMsBQj5 lpuAfGS8Cb3Sg== Received: from phl-compute-02.internal (phl-compute-02.internal [10.202.2.42]) by mailfauth.ams.internal (Postfix) with ESMTP id 5C10C198003A; Thu, 10 Sep 2026 12:42:46 -0400 (EDT) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-02.internal (MEProxy); Thu, 10 Sep 2026 12:42:53 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: dmFkZTE6jQ7+ezdJvg0JCOHdeYm98uh8QoD3ppKlDCRkHPBf90NB4jh+ryx62TK4xbeazW OjmkN5eot9viRbZ00CYgDBHT2oH+RH5aQCFDDcusEjlg36Fb5ctpKOdLx985wH/FQip39p T7ct56TymSBRJpKxvgkeX9MwBtQkzBLcy1It/iWo2GFSKP14EPKpuBcDC6NZBPjPGuOYSt gDaf9qR90/vzX/n4LPNTXJAoXyXdh9FPu33aE25/XhViK2AK9AjkA9McfHxKUYwZZ4cNQy L9J1ZGy+68s9a/50qhSiH99Xat/XVOrftxEw7V9RkHk0x0GqdIBAFNnw6YCfe96nIenPCV unI4se8nbIGD4Kq9IYLNhYyv87qSPRhN+Krgj6j273FqV5FEWJwwMlgm5Sz+45zLTVppmx gHvNV0/P+4vzXnfFkqMihWIDJuG3ZV6EBEqsa8ZNbqO6blwIo2VGujQcKnZ0KB8O1A4cZh 9aVyRAJDPIwz5b4tC125/MeGSy186XR8QU31P4geQmSY4nJzZjY/iH6DhjfNoFwbPWMawa e5jKQkqOdLGPlpHmn82M4fAZGG4IXR1RdfGZek9O9ES1ZEMAINtyHU1UsXLyrbltnxGrWV KMFv8bBZJLT//oUQTE/4JmHaZi+S9YjPBcCpBWE6Rkuxj00ZtgLEzvfdjpDQ X-ME-Proxy: Feedback-ID: i10464835:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 10 Sep 2026 12:42:44 -0400 (EDT) Date: Thu, 10 Sep 2026 17:42:43 +0100 From: Kiryl Shutsemau To: Hugh Dickins Cc: Andrew Morton , Ackerley Tng , Alexander Viro , Alexandre Ghiti , Baolin Wang , Barry Song , Binbin Wu , Christian Brauner , Christoph Hellwig , Christoph Lameter , Claudio Imbrenda , David Hildenbrand , JP Kobryn , Jan Kara , Jens Axboe , Johannes Weiner , Kairui Song , Lance Yang , Leonardo Bras , Lorenzo Stoakes , Marcelo Tosatti , Matthew Wilcox , Mel Gorman , Miaohe Lin , Michal Hocko , Minchan Kim , Muchun Song , Oscar Salvador , Peter Zijlstra , Qi Zheng , Rik van Riel , Sebastian Andrzej Siewior , Shakeel Butt , Suren Baghdasaryan , Vlastimil Babka , Yang Shi , Yu Zhao , Zach O'Keefe , Zi Yan , linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH v2 07/26] mm/fbatch: LRU_NEXT_ACTIVATE bit to optimize folio_activate() Message-ID: References: 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: On Wed, Sep 09, 2026 at 02:55:41AM -0700, Hugh Dickins wrote: > Implement an equivalent to the old __lru_cache_activate_folio() > optimization, to activate a folio recently put in the lru_add fbatch, > without having to put it through the lru_activate fbatch too. Neither > lruvec lock nor lru bit can guard this safely and efficiently, so resort > to try_cmpxchg() on a further, LRU_NEXT_ACTIVATE bit in folio->lru_next. > > Signed-off-by: Hugh Dickins > --- > include/linux/mm_inline.h | 4 ++++ > mm/folio.c | 23 ++++++++++++++++++++--- > 2 files changed, 24 insertions(+), 3 deletions(-) > > diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h > index 8420b1276535..8f5efadf9c7c 100644 > --- a/include/linux/mm_inline.h > +++ b/include/linux/mm_inline.h > @@ -346,6 +346,7 @@ static inline void folio_migrate_refs(struct folio *new, const struct folio *old > enum { > LRU_NEXT_NEVER_TAIL = 0, /* Used by a tail's compound_head */ > LRU_NEXT_BATCHED = 1, /* Not used by any aligned pointer */ > + LRU_NEXT_ACTIVATE, > NR_LRU_NEXT_FLAGS > }; > > @@ -358,6 +359,9 @@ bool lru_add_del_folio(struct folio *folio) > if (!(lru_next & BIT(LRU_NEXT_BATCHED))) > return false; > > + if (lru_next & BIT(LRU_NEXT_ACTIVATE)) > + folio_set_active(folio); > + > WRITE_ONCE(folio->lru.next, LIST_POISON1); > /* BUG_ON(folio->lru_next & BIT(LRU_NEXT_BATCHED)); */ > > diff --git a/mm/folio.c b/mm/folio.c > index a18d8ef6afd5..0b75c3b69d5a 100644 > --- a/mm/folio.c > +++ b/mm/folio.c > @@ -256,15 +256,32 @@ static void lru_activate(struct lruvec *lruvec, struct folio *folio) > > void folio_activate(struct folio *folio) > { > + unsigned long lru_next; > + > if (folio_test_active(folio) || folio_test_unevictable(folio) || > !folio_test_lru(folio)) > return; > > /* > - * XXX: It is curiously difficult to recreate safely the old > - * __lru_cache_activate_folio() optimization (folio_set_active() > - * directly if it's on the local lru_add fbatch): revisit later. > + * This optimization is intended for the common case of folio > + * having been recently added to this CPU's lru_add fbatch. > + * But since other CPUs can now take it at any instant (after > + * a folio_test_clear_lru()), and we may be migrated to another > + * CPU, it is simplest just to extend the optimization to all CPUs. > + * > + * folio_set_active() would be unsafe without the lruvec lock, and > + * a folio_test_clear_lru() here might cause a racing drain of the > + * lru_add fbatch to skip its lru_add(): so use try_cmpxchg(). > */ > + lru_next = READ_ONCE(folio->lru_next); > + while (lru_next & BIT(LRU_NEXT_BATCHED)) { > + if (lru_next & BIT(LRU_NEXT_ACTIVATE)) > + return; > + if (try_cmpxchg(&folio->lru_next, &lru_next, > + lru_next | BIT(LRU_NEXT_ACTIVATE))) > + return; Hm. What prevents the folio from becoming unevictable under us here? I don't see anything. __folio_add_lru() wouldn't like it: VM_BUG_ON_FOLIO(folio_test_active(folio) && folio_test_unevictable(folio), folio); folio_lru_list() has the VM_BUG() too. I am not sure what the right fix is. Maybe lru_add_del_folio() should only call folio_set_active() on !folio_test_unevictable() folios? Or should we allow occasional active+unevictable so if they are munlocked, they will go directly to active list? > + } > + > folio_batch_add_and_move(folio, lru_activate); > } > > -- > 2.51.0 > -- Kiryl Shutsemau / Kirill A. Shutemov