From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796340439; b=4dgSATm+BpOeMdn1LHBaeTWYjDNKX5u/qp/uqlE+4fVvXlHYtLpMqi+FZGOrfQ6Y3RY3 8D/KvYYlCM29NVeJpzp+PQGpDRWKmgAvZmzWdmczyXP8vGY88kQuPVscRi2egZXQdIRCl IxTAHfgHh2/B4L43eLTKUb/zvvQSROQ2sZ739q6RlS7aSMMwRkljOE8zB67kP/No+rTo4 t/XzRghcugDN72j0bR9Ub9L4LjxODm7DD2u1pxrk/avod1Si4G3RVt53gNNNp9xtTyqHh IdXDjvDdlBwQ0Fago0SFAyJjnmELfpwtyb9yWU3cSqYKIJIKoaduSOyTA1aTcrXTU+Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796340439; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-Id: In-Reply-To:References:X-Mailer:Mime-Version:Content-Type: Content-Transfer-Encoding; bh=6FHbYx4TC+90xOo5Dg5yCdk3Iml3jeiva2NLIJaBKCM=; b=KHb68oEaRYimVVFHmyzmn28QdSxVPYEU8Ho3c2TX6tm43K9vApRCMv4/71+WGQu2CxOt W05mkE5G57OO/D1lA2j//xy6wm1iHDBkFKWHFEYdjhuKQVxGcQ4cNjJUk0LaBYJY1Vcxz 0fVe7vhGbyMJY42OBeOhArU5HrP2QmC4wSbz7aEJLin9biXyAnTZAuFLFexD/deIwn+UX fB1AyuYgZ7slbiuxYB1XP3Cd5/SwvAyfBeM7bdaLhCWG93fSz2suZ4Ue5bXk8EoI2CmRm ALk6t6RCTjODnyhCq0Ro2T8brwozlgtME+AXpBymIt8nUCvRTyMa+6nu+l7zugm8Csw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=Y32noRFL; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 20C52606A322; Thu, 3 Dec 2026 23:27:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=Y32noRFL Received: by smtp.kernel.org (Postfix) with ESMTPSA id 534B61F000C2; Thu, 3 Dec 2026 23:27:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796340438; bh=6FHbYx4TC+90xOo5Dg5yCdk3Iml3jeiva2NLIJaBKCM=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=Y32noRFLU2FLpiMlDrkyLyRZWq0bT5H63EVlR0P4Z8jyq6XyXmBVuZ0KeCUrw/wpf 4mQEmj+jN0LNCQgnmv93o88lzU9mcXgjeF16i/7pKvzz/Xv0A0oJh6LzwHoBnuuDeb xORBwWeTbmMpz979LuQJoBNWyjURVrZlvexlfX2E= Date: Thu, 3 Dec 2026 15:27:17 -0800 From: Andrew Morton To: Daehyeon Ko <4ncienth@gmail.com> Cc: muchun.song@linux.dev, osalvador@suse.de, david@kernel.org, tj@kernel.org, hongfu.li@linux.dev, linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH v2 RESEND] mm/hugetlb_cgroup: prevent use-after-free during cgroup offlining Message-Id: <20261203152717.67a1fb404c483f2fb1b55560@linux-foundation.org> In-Reply-To: <20261106060347.637480-1-4ncienth@gmail.com> References: <20261106060347.637480-1-4ncienth@gmail.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Fri, 6 Nov 2026 15:03:47 +0900 Daehyeon Ko <4ncienth@gmail.com> wrote: > A normal hugetlb charge obtains the current task's hugetlb cgroup with > css_tryget(), charges its page counter, and drops the CSS reference before > the folio is committed under hugetlb_lock. > > The task can be migrated while the allocation is in progress. If the old > CSS is then offlined, its final active-folio scan can finish before the > charge becomes visible. The allocation can subsequently charge that dying > CSS and commit its pointer to the folio. Once the CSS is freed, uncharging > the folio accesses the freed hugetlb_cgroup, including its page counter and > per-node usage. > > Keep the reference acquired by the charge until commit or cancellation. At > commit time, while holding hugetlb_lock, move an uncommitted charge out of > each dying CSS to the first live ancestor. The hierarchical charge already > includes every ancestor, so moving it only requires canceling each dying > CSS's local count. > > This orders commit against the offline active-folio scan: either the scan > runs after commit and reparents the folio, or commit observes the dying CSS > after the scan and selects a live ancestor. The retained reference also > keeps the originally selected hugetlb_cgroup alive through this decision. > Reservation charges retain their existing CSS lifetime and are not > reparented. > > On an unmodified v7.3 kernel with a 128-page 2 MiB pool, a > capability-free uid 1000 allocator racing administrator rmdir reproduced > KASAN writes to a freed hugetlb_cgroup in three of three boots. Controller > disable reproduced the same fault. With this change, 5,000 rmdir rounds > with over 1 million successful faults and 2,000 controller-disable rounds > with over 1.6 million successful faults completed without KASAN, LOCKDEP, > page-counter, or refcount reports. > Could we please get some review of this patch? Thanks. > --- a/mm/hugetlb_cgroup.c > +++ b/mm/hugetlb_cgroup.c > @@ -310,11 +310,10 @@ static int __hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages, > css_put(&h_cg->css); > goto done; > } > - /* Reservations take a reference to the css because they do not get > - * reparented. > + /* > + * Hold the CSS reference until commit. Reservations keep it longer > + * because they are not reparented. > */ > - if (!rsvd) > - css_put(&h_cg->css); > done: > *ptr = h_cg; > return ret; > @@ -337,9 +336,26 @@ static void __hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages, > struct hugetlb_cgroup *h_cg, > struct folio *folio, bool rsvd) > { > + struct hugetlb_cgroup *charged_h_cg = h_cg; > + > if (hugetlb_cgroup_disabled() || !h_cg) > - return; > + goto out; > lockdep_assert_held(&hugetlb_lock); > + > + /* > + * A task can be migrated after selecting h_cg. Once offlining starts, > + * redirect the uncommitted charge to a live ancestor. hugetlb_lock > + * serializes this decision with the offline scan, which reparents > + * earlier commits. > + */ > + if (!rsvd) { > + while (css_is_dying(&h_cg->css)) { > + page_counter_cancel(&h_cg->hugepage[idx], nr_pages); > + /* The root CSS never dies. */ > + h_cg = parent_hugetlb_cgroup(h_cg); > + } > + } > + > __set_hugetlb_cgroup(folio, h_cg, rsvd); > if (!rsvd) { > unsigned long usage = > @@ -352,6 +368,9 @@ static void __hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages, > WRITE_ONCE(h_cg->nodeinfo[folio_nid(folio)]->usage[idx], > usage + nr_pages); > } > +out: > + if (!rsvd && charged_h_cg) > + css_put(&charged_h_cg->css); > } > > void hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages, > @@ -419,15 +438,14 @@ static void __hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages, > struct hugetlb_cgroup *h_cg, > bool rsvd) > { > - if (hugetlb_cgroup_disabled() || !h_cg) > + if (!h_cg) > return; > > page_counter_uncharge(__hugetlb_cgroup_counter_from_cgroup(h_cg, idx, > rsvd), > nr_pages); > > - if (rsvd) > - css_put(&h_cg->css); > + css_put(&h_cg->css); > } > > void hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages, >