From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f177.google.com (mail-qk1-f177.google.com [209.85.222.177]) (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 A694854723 for ; Thu, 18 Dec 2025 14:06:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.177 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766066816; cv=none; b=VCMYk6vbnUSEbuLxCSUNsTx5YqCC5EI8hedxOgdTxAU28TuWhx8Y2LwYI9JcreCEvaO4k/0M86jT7hXig+9B7d+UpmkPWeWkawRj9O6cFBaFHnzIpubZP1OzImKyg5sJESAJLWtW2eb8NDuoCrT8MGdVV2yBqhDN9vtUwFOqKOk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766066816; c=relaxed/simple; bh=/rp2XK3YF6HyMaRXJUuw+7Ik4GDj4jVK7HmO8KQIZGk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=q5AHxq/2uHoAh3cVbenD28KufP7UikpSlsx8zNhHSmXzTt3iGW83OcFJDAdWxiUto1ckIaVKB9W5T3qmmZOI1cCw1i6ya73u099Ca6vIzjapmjjlpsOabeMzaLNAgWp8e34CDVHNqDgv5DBHQxrYOcSLVAN3/vRmAq+TOxBCnYA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=cmpxchg.org; spf=pass smtp.mailfrom=cmpxchg.org; dkim=pass (2048-bit key) header.d=cmpxchg.org header.i=@cmpxchg.org header.b=SXFK6SgV; arc=none smtp.client-ip=209.85.222.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=cmpxchg.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cmpxchg.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=cmpxchg.org header.i=@cmpxchg.org header.b="SXFK6SgV" Received: by mail-qk1-f177.google.com with SMTP id af79cd13be357-8b2f0f9e4cbso53817885a.0 for ; Thu, 18 Dec 2025 06:06:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg.org; s=google; t=1766066811; x=1766671611; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=sR30ixwBE7Y68ILuHxMSkeae6AHX5P9JPZmuAQuOZlI=; b=SXFK6SgVuAO/YTbAeobW+pH3NI/GnLR1QdJfKp4h1Cw0XWJtyDadEVuCTgPolzZzRV 5X7bdXjlYqzSJamauu992M5Xr0HsalVb2QndlKXu+8i5uovcW2UcLZuJVKB+g5CXbITe K1Z4zP+plYvYreQVefdqgRsySCbNV9NzA3Z4YbsVYfX5qEAMeUwhnM2cvmFs9EN8aIpD Hh+A4/5Q065K5DgVg5wK4p9Pv29za34wdHhSEkztO60TDdlknqEn7ftERrfDmS8DHl+C hUKK3zdXRMkFEXdevZlEcs6Zrd9V/yl6gw2/bjB7ql1Skb58NZP3n71imLJpTlBd6J3o 3B2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766066811; x=1766671611; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sR30ixwBE7Y68ILuHxMSkeae6AHX5P9JPZmuAQuOZlI=; b=Jphn20Jg62O+Dv2xCa3WpX+Eb1X6/IDnPnAg0l3ofKsZsnAMTWV+jiKhxYsiofxUZs A1oOVhdEK0Mljf1QWvQo8X9y4QRD+ZCQiY4g3WoVA8cDFkgS5ERM2r2Wr57ttHG8Onw+ KkTiHbBGlfxjHF/0Mk0XraOMoGCaMc8xPTVX8TMlhRYUi0rqHfhH1LaT34b4TG7j1JDp nwcMQii0zsb8o1Htpyfz2DsSSyWmUBebcknnLXDD4HisPb5ATO/prDHHr7/EM16hzyld 2wcTwP4UDfUD9mZIVcXJC0Sld8FOONEes7LQJmphe5bdCARHhR3/wXN0hDsycngKBqkt rfwg== X-Forwarded-Encrypted: i=1; AJvYcCW6hPkmuiIq5aHI0nG48JcbvJCBxeU0QwSoPcyMYKDP4qUeSSM5tc0TjMR06wQgNpuxDfpLoy5y4b7hDko=@vger.kernel.org X-Gm-Message-State: AOJu0Yw1hzwrLkC2iot5S/FKAHuUCSULPN3jMDCgE/kPRJVLegumijs4 3twKXPlyJRsn0tKMccl5Z7/L8kYkiDcZMQsDl3D/6HXxc5CakAsMA4lDvgtwbxQ3TSY= X-Gm-Gg: AY/fxX5LO8/P6quRVGbEcnAZp9o+GDku/9ncQSCrhOZM7PoNKDjjfoJ+szuWcs4M5TP 4kvLlyOVapqIXul3R1zdtcQKbdLOBYIwQ3qk7BizCl5UslN42ODakJjjGekwpmMcf5ZZ4MKSbnc jKEJOLbIhslHbJeNueKaBU79P7F8/lhy471UWvcusju1ZM80PCRyS/bG2Z75oAefc/AvighOmF6 6w0vZD/jLtqlia3KPfSgcVYcohWTyTpQm4hciCjTos943rwdTrb6PS1TYx/oY1UXH9E1UPwURyE PkMmrL8YdPmZzMBmKPUtQvsbhJRPa+ZQJWV8Xl7c19um+brZHRzL4k+lXzJeGImuISMS7i8dbFV /riZ2fERYQd1PAeaFgBYSm4ytiayBwJl2dahgYTzGLZfQEw/uDrt1lCR0+H2tR0wqKdRP3s1wLm 8LEU3jBfEj2A== X-Google-Smtp-Source: AGHT+IHGiY8Hg/MD1OdXod9gyMroAAQViBd9bZlpYzLebqdk9FpWA5fcGAjVIG5zyNvPrE0drHPDtQ== X-Received: by 2002:a05:620a:400e:b0:8a3:90cb:9224 with SMTP id af79cd13be357-8bee670c438mr434656285a.2.1766066811117; Thu, 18 Dec 2025 06:06:51 -0800 (PST) Received: from localhost ([2603:7000:c01:2716:929a:4aff:fe16:c778]) by smtp.gmail.com with ESMTPSA id af79cd13be357-8beeb5f2911sm168014685a.17.2025.12.18.06.06.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Dec 2025 06:06:49 -0800 (PST) Date: Thu, 18 Dec 2025 09:06:45 -0500 From: Johannes Weiner To: Qi Zheng Cc: hughd@google.com, mhocko@suse.com, roman.gushchin@linux.dev, shakeel.butt@linux.dev, muchun.song@linux.dev, david@kernel.org, lorenzo.stoakes@oracle.com, ziy@nvidia.com, harry.yoo@oracle.com, imran.f.khan@oracle.com, kamalesh.babulal@oracle.com, axelrasmussen@google.com, yuanchu@google.com, weixugc@google.com, chenridong@huaweicloud.com, mkoutny@suse.com, akpm@linux-foundation.org, hamzamahfooz@linux.microsoft.com, apais@linux.microsoft.com, lance.yang@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, Muchun Song , Qi Zheng Subject: Re: [PATCH v2 27/28] mm: memcontrol: eliminate the problem of dying memory cgroup for LRU folios 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, Dec 17, 2025 at 03:27:51PM +0800, Qi Zheng wrote: > From: Muchun Song > > Pagecache pages are charged at allocation time and hold a reference > to the original memory cgroup until reclaimed. Depending on memory > pressure, page sharing patterns between different cgroups and cgroup > creation/destruction rates, many dying memory cgroups can be pinned > by pagecache pages, reducing page reclaim efficiency and wasting > memory. Converting LRU folios and most other raw memory cgroup pins > to the object cgroup direction can fix this long-living problem. This is already in the coverletter. Please describe here what the patch itself does. IOW, now that everything is set up, switch folio->memcg_data pointers to objcgs, update the accessors, and execute reparenting on cgroup death. > Finally, folio->memcg_data of LRU folios and kmem folios will always > point to an object cgroup pointer. The folio->memcg_data of slab > folios will point to an vector of object cgroups. > @@ -223,22 +223,55 @@ static inline void __memcg_reparent_objcgs(struct mem_cgroup *src, > > static inline void reparent_locks(struct mem_cgroup *src, struct mem_cgroup *dst) > { > + int nid, nest = 0; > + > spin_lock_irq(&objcg_lock); > + for_each_node(nid) { > + spin_lock_nested(&mem_cgroup_lruvec(src, > + NODE_DATA(nid))->lru_lock, nest++); > + spin_lock_nested(&mem_cgroup_lruvec(dst, > + NODE_DATA(nid))->lru_lock, nest++); > + } > } Looks okay to me. If this should turn out to be a scalability problem in practice, we can make objcgs per-node, and then reparent lru/objcg pairs on a per-node basis without nesting locks. > static inline void reparent_unlocks(struct mem_cgroup *src, struct mem_cgroup *dst) > { > + int nid; > + > + for_each_node(nid) { > + spin_unlock(&mem_cgroup_lruvec(dst, NODE_DATA(nid))->lru_lock); > + spin_unlock(&mem_cgroup_lruvec(src, NODE_DATA(nid))->lru_lock); > + } > spin_unlock_irq(&objcg_lock); > } > > +static void memcg_reparent_lru_folios(struct mem_cgroup *src, > + struct mem_cgroup *dst) > +{ > + if (lru_gen_enabled()) > + lru_gen_reparent_memcg(src, dst); > + else > + lru_reparent_memcg(src, dst); > +} > + > static void memcg_reparent_objcgs(struct mem_cgroup *src) > { > struct obj_cgroup *objcg = rcu_dereference_protected(src->objcg, true); > struct mem_cgroup *dst = parent_mem_cgroup(src); > > +retry: > + if (lru_gen_enabled()) > + max_lru_gen_memcg(dst); > + > reparent_locks(src, dst); > + if (lru_gen_enabled() && !recheck_lru_gen_max_memcg(dst)) { > + reparent_unlocks(src, dst); > + cond_resched(); > + goto retry; > + } > > __memcg_reparent_objcgs(src, dst); > + memcg_reparent_lru_folios(src, dst); Please inline memcg_reparent_lru_folios() here, to keep the lru vs lrugen switching as "flat" as possible: if (lru_gen_enabled()) { if (!recheck_lru_gen_max_memcgs(parent)) { reparent_unlocks(memcg, parent); cond_resched(); goto retry; } lru_gen_reparent_memcg(memcg, parent); } else { lru_reparent_memcg(memcg, parent); } > @@ -989,6 +1022,8 @@ struct mem_cgroup *get_mem_cgroup_from_current(void) > /** > * get_mem_cgroup_from_folio - Obtain a reference on a given folio's memcg. > * @folio: folio from which memcg should be extracted. > + * > + * The folio and objcg or memcg binding rules can refer to folio_memcg(). See folio_memcg() for folio->objcg/memcg binding rules.