From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 8A5003EF677 for ; Tue, 24 Mar 2026 12:01:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774353718; cv=none; b=tnzwN67/dVEbG11P/3sXMiem81pPokq5FoHhQ/EoftDI5du3IxRiEPdtPceQn3Dz23bKZdLpWOcusRvo7i0zsmebsGZJRqvlbjdFhYm2CucnqWcVMdorHCPX4shwEXkiu6uiFhygUfioHROx47KHsljN39EIyfp4qBC4uLhYRzc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774353718; c=relaxed/simple; bh=cI3LTNSwlHdB+Tez7H66TSvkT89A3gqoLgFKYhi3sog=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=u3U5/BqN6aRglLAIMXroMJbGRCxW7YJ4TXz3M0SsWaE6J4ls9zLEPobt1Xf0gCA2oFzOAsItB9fjuWmVjia2cMSGFcztMvr9ZHSxCfkjK7pCNHCdc3qMLRhe/CcFoNBmEk9N/h2dOzJBcdn7fSjm+PriQdicKavtA/fXLiFRmRg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DPYBjgOm; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DPYBjgOm" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8DD42C19424; Tue, 24 Mar 2026 12:01:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774353718; bh=cI3LTNSwlHdB+Tez7H66TSvkT89A3gqoLgFKYhi3sog=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=DPYBjgOmiwqojj0OOlQmeiFoeAjOhy5a4hPL7cvL7nLD5C4+QWaD7EhuT4cDnTTFL KDhbDHAE8OjuBoTqhO7Q5dGuekcm4zCU74y8HfxKqB19LBvGyI42HKwF30BYLnOYV+ 0eobGLo0qjWukpZ47RfZiP9BW95zAHZsW8aTDDZ9iQUJ1d+S0RJLwZb1UdDF87iIaC /VS9PsjquDrz+05boyy9JCTrxljSnLN+IipYHji7SdkDkch29lFrl5IgmnzJUMSZHc wFiWlSqEOys52SAB+2jQtG1HsHkF+6vrsOWuszBbRH+g8EFp9cRurteq1YWmbxXFhm /8ZC1l1baIedQ== Date: Tue, 24 Mar 2026 12:01:55 +0000 From: "Lorenzo Stoakes (Oracle)" To: Johannes Weiner Cc: Andrew Morton , David Hildenbrand , Shakeel Butt , Yosry Ahmed , Zi Yan , "Liam R. Howlett" , Usama Arif , Kiryl Shutsemau , Dave Chinner , Roman Gushchin , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3 6/7] mm: list_lru: introduce folio_memcg_list_lru_alloc() Message-ID: <58ea360a-54e1-468e-99b5-57b716026088@lucifer.local> References: <20260318200352.1039011-1-hannes@cmpxchg.org> <20260318200352.1039011-7-hannes@cmpxchg.org> 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: <20260318200352.1039011-7-hannes@cmpxchg.org> On Wed, Mar 18, 2026 at 03:53:24PM -0400, Johannes Weiner wrote: > memcg_list_lru_alloc() is called every time an object that may end up > on the list_lru is created. It needs to quickly check if the list_lru > heads for the memcg already exist, and allocate them when they don't. > > Doing this with folio objects is tricky: folio_memcg() is not stable > and requires either RCU protection or pinning the cgroup. But it's > desirable to make the existence check lightweight under RCU, and only > pin the memcg when we need to allocate list_lru heads and may block. > > In preparation for switching the THP shrinker to list_lru, add a > helper function for allocating list_lru heads coming from a folio. > > Reviewed-by: David Hildenbrand (Arm) > Signed-off-by: Johannes Weiner Logic LGTM, but would be nice to have some kdoc. With that addressed, feel free to add: Reviewed-by: Lorenzo Stoakes (Oracle) > --- > include/linux/list_lru.h | 12 ++++++++++++ > mm/list_lru.c | 39 ++++++++++++++++++++++++++++++++++----- > 2 files changed, 46 insertions(+), 5 deletions(-) > > diff --git a/include/linux/list_lru.h b/include/linux/list_lru.h > index 4afc02deb44d..4bd29b61c59a 100644 > --- a/include/linux/list_lru.h > +++ b/include/linux/list_lru.h > @@ -81,6 +81,18 @@ static inline int list_lru_init_memcg_key(struct list_lru *lru, struct shrinker > > int memcg_list_lru_alloc(struct mem_cgroup *memcg, struct list_lru *lru, > gfp_t gfp); > + > +#ifdef CONFIG_MEMCG > +int folio_memcg_list_lru_alloc(struct folio *folio, struct list_lru *lru, > + gfp_t gfp); Could we have a kdoc comment for this? Thanks! > +#else > +static inline int folio_memcg_list_lru_alloc(struct folio *folio, > + struct list_lru *lru, gfp_t gfp) > +{ > + return 0; > +} > +#endif > + > void memcg_reparent_list_lrus(struct mem_cgroup *memcg, struct mem_cgroup *parent); > > /** > diff --git a/mm/list_lru.c b/mm/list_lru.c > index b817c0f48f73..1ccdd45b1d14 100644 > --- a/mm/list_lru.c > +++ b/mm/list_lru.c > @@ -537,17 +537,14 @@ static inline bool memcg_list_lru_allocated(struct mem_cgroup *memcg, > return idx < 0 || xa_load(&lru->xa, idx); > } > > -int memcg_list_lru_alloc(struct mem_cgroup *memcg, struct list_lru *lru, > - gfp_t gfp) > +static int __memcg_list_lru_alloc(struct mem_cgroup *memcg, > + struct list_lru *lru, gfp_t gfp) > { > unsigned long flags; > struct list_lru_memcg *mlru = NULL; > struct mem_cgroup *pos, *parent; > XA_STATE(xas, &lru->xa, 0); > > - if (!list_lru_memcg_aware(lru) || memcg_list_lru_allocated(memcg, lru)) > - return 0; > - > gfp &= GFP_RECLAIM_MASK; > /* > * Because the list_lru can be reparented to the parent cgroup's > @@ -588,6 +585,38 @@ int memcg_list_lru_alloc(struct mem_cgroup *memcg, struct list_lru *lru, > > return xas_error(&xas); > } > + > +int memcg_list_lru_alloc(struct mem_cgroup *memcg, struct list_lru *lru, > + gfp_t gfp) > +{ > + if (!list_lru_memcg_aware(lru) || memcg_list_lru_allocated(memcg, lru)) > + return 0; > + return __memcg_list_lru_alloc(memcg, lru, gfp); > +} > + > +int folio_memcg_list_lru_alloc(struct folio *folio, struct list_lru *lru, > + gfp_t gfp) > +{ > + struct mem_cgroup *memcg; > + int res; > + > + if (!list_lru_memcg_aware(lru)) > + return 0; > + > + /* Fast path when list_lru heads already exist */ > + rcu_read_lock(); OK nice I see folio_memcg() explicitly states an RCU lock suffices.... > + memcg = folio_memcg(folio); > + res = memcg_list_lru_allocated(memcg, lru); ...And an xa_load() should also be RCU safe :) > + rcu_read_unlock(); > + if (likely(res)) > + return 0; So that's nice! > + > + /* Allocation may block, pin the memcg */ > + memcg = get_mem_cgroup_from_folio(folio); > + res = __memcg_list_lru_alloc(memcg, lru, gfp); > + mem_cgroup_put(memcg); > + return res; > +} > #else > static inline void memcg_init_list_lru(struct list_lru *lru, bool memcg_aware) > { > -- > 2.53.0 > Cheers, Lorenzo