Linux Documentation
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Muchun Song <muchun.song@linux.dev>,
	 Oscar Salvador <osalvador@kernel.org>,
	 Andrew Morton <akpm@linux-foundation.org>,
	Hongfu Li <lihongfu@kylinos.cn>,
	 Jinmeng Zhou <jinmengzhou22@gmail.com>,
	 Mina Almasry <almasrymina@google.com>,
	Davidlohr Bueso <dave@stgolabs.net>,
	 Naoya Horiguchi <nao.horiguchi@gmail.com>,
	 "Liam R. Howlett" <liam@infradead.org>,
	Vlastimil Babka <vbabka@kernel.org>,
	 Mike Rapoport <rppt@kernel.org>, Michal Hocko <mhocko@suse.com>,
	 Jonathan Corbet <corbet@lwn.net>,
	Shuah Khan <skhan@linuxfoundation.org>,
	 Randy Dunlap <rdunlap@infradead.org>,
	 Vivek Kasireddy <vivek.kasireddy@intel.com>,
	Mel Gorman <mgorman@suse.de>,  Hugh Dickins <hughd@google.com>,
	 Baolin Wang <baolin.wang@linux.alibaba.com>,
	 Dave Airlie <airlied@redhat.com>, Jason Gunthorpe <jgg@ziepe.ca>,
	 Andy Whitcroft <apw@canonical.com>,
	 Roman Gushchin <roman.gushchin@linux.dev>,
	Barry Song <baohua@kernel.org>,
	 Andrea Arcangeli <aarcange@redhat.com>,
	Lorenzo Stoakes <ljs@kernel.org>,
	 Suren Baghdasaryan <surenb@google.com>,
	 Shakeel Butt <shakeel.butt@linux.dev>,
	Pedro Falcato <pfalcato@suse.de>,  Jann Horn <jannh@google.com>,
	Guillaume Morin <guillaume@morinfr.org>,
	 Yang Shi <shy828301@gmail.com>,
	James Houghton <jthoughton@google.com>,
	 Rik van Riel <riel@surriel.com>, Harry Yoo <harry@kernel.org>,
	 Lance Yang <lance.yang@linux.dev>,
	 Axel Rasmussen <axelrasmussen@google.com>,
	Peter Xu <peterx@redhat.com>,  Sasha Levin <sashal@kernel.org>,
	Kiryl Shutsemau <kas@kernel.org>,
	 Christian Brauner <brauner@kernel.org>,
	 "Steven Rostedt (Google)" <rostedt@goodmis.org>,
	 Hongbo Li <lihongbo22@huawei.com>,
	Gregory Price <gourry@gourry.net>,  Zi Yan <ziy@nvidia.com>,
	Joshua Hahn <joshua.hahnjy@gmail.com>,
	 Rakie Kim <rakie.kim@sk.com>,
	Ying Huang <ying.huang@linux.alibaba.com>,
	 Matthew Brost <matthew.brost@intel.com>,
	Byungchul Park <byungchul@sk.com>,
	 Alistair Popple <apopple@nvidia.com>,
	Andi Kleen <ak@linux.intel.com>,
	 David Rientjes <rientjes@google.com>,
	Joern Engel <joern@lazybastard.org>,
	 "Aneesh Kumar K.V" <aneesh.kumar@kernel.org>
Cc: linux-mm@kvack.org, linux-doc@vger.kernel.org,
	 Gerd Hoffmann <kraxel@redhat.com>,
	linux-kernel@vger.kernel.org,  linux-fsdevel@vger.kernel.org,
	Johannes Weiner <hannes@cmpxchg.org>,
	 Chris Mason <mason@kernel.org>, Jiaqi Yan <jiaqiyan@google.com>,
	 "David Hildenbrand (Arm)" <david@kernel.org>
Subject: [PATCH v2 34/35] mm/hugetlb_cgroup: charge faults to mm->owner
Date: Thu, 03 Dec 2026 12:37:57 +0100	[thread overview]
Message-ID: <20261203-hugetlb_fixes_2-v2-34-9bbeabb555d2@kernel.org> (raw)
In-Reply-To: <20261203-hugetlb_fixes_2-v2-0-9bbeabb555d2@kernel.org>

We can bypass hugetlb cgroup limits when a kernel thread faults in
hugetlb pages for a user process: __hugetlb_cgroup_charge_cgroup()
charges the hugetlb cgroup of current, and kernel threads usually live
in the root cgroup, which has no limit.

For example, a KVM's async page fault repeats a fault of a vCPU that
just failed at the limit in a kworker, which then charges the root
cgroup:

    vCPU thread                     kworker
    ===========                     =======
    __kvm_mmu_faultin_pfn()
      hva_to_pfn(), FOLL_NOWAIT
        hugetlb_alloc_folio()
          charge fails
        KVM_PFN_ERR_NEEDS_IO
      kvm_setup_async_pf()
                                    async_pf_execute()
                                      get_user_pages_remote()
                                        hugetlb_alloc_folio()
                                          charges the root

Let's fix it by charging the hugetlb cgroup of mm->owner whenever we
allocate or reserve for an mm, like memcg does. If we don't allocate for
a specific mm, we use current->mm, again like memcg. Unlike memcg, which
falls back to the root cgroup, we fail the charge if the mm has no owner
(anymore), as we would otherwise bypass the limits again. As mm->owner
depends on CONFIG_MEMCG, nothing changes without it.

This changes the charged hugetlb cgroup in these scenarios:

 (1) Kernel threads that fault on a user mm, such as KVM async page
     faults, kvm_xen_set_evtchn() in the irqfd work item and
     iommu_sva_handle_mm(), now charge the cgroup of mm->owner instead
     of the root cgroup.
 (2) vhost kthread workers, which join the cgroups of the task that
     created them, now charge the cgroup of mm->owner.
 (3) With cgroup v1, threads of a process that are in different hugetlb
     cgroups now all charge the cgroup of mm->owner.
 (4) Processes that fault on the mm of another process, such as a
     userfaultfd monitor (UFFDIO_COPY) or remote GUP through ptrace,
     /proc/pid/mem or process_vm_writev(), now charge the cgroup of the
     target's mm->owner instead of their own cgroup.

... which should just be the right thing to do.

This has low urgency, as it only affects hugetlb cgroup limits. Let's
not CC stable, as this might not be particularly easy to backport.

Fixes: 6d76dcf40405 ("hugetlb/cgroup: add charge/uncharge routines for hugetlb cgroup")
Reported-by: Chris Mason <mason@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
 include/linux/hugetlb_cgroup.h | 10 +++++-----
 mm/hugetlb.c                   | 12 +++++++-----
 mm/hugetlb_cgroup.c            | 32 +++++++++++++++++++++++++-------
 3 files changed, 37 insertions(+), 17 deletions(-)

diff --git a/include/linux/hugetlb_cgroup.h b/include/linux/hugetlb_cgroup.h
index 1c437eb2e1c30..97208bb9d9639 100644
--- a/include/linux/hugetlb_cgroup.h
+++ b/include/linux/hugetlb_cgroup.h
@@ -139,9 +139,9 @@ static inline void resv_map_put_hugetlb_cgroup_uncharge_info(
 }
 
 extern int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
-					struct hugetlb_cgroup **ptr);
+		struct mm_struct *mm, struct hugetlb_cgroup **ptr);
 extern int hugetlb_cgroup_charge_cgroup_rsvd(int idx, unsigned long nr_pages,
-					     struct hugetlb_cgroup **ptr);
+		struct mm_struct *mm, struct hugetlb_cgroup **ptr);
 extern void hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages,
 					 struct hugetlb_cgroup *h_cg,
 					 struct folio *folio);
@@ -225,14 +225,14 @@ static inline void resv_map_put_hugetlb_cgroup_uncharge_info(
 }
 
 static inline int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
-					       struct hugetlb_cgroup **ptr)
+		struct mm_struct *mm, struct hugetlb_cgroup **ptr)
 {
 	return 0;
 }
 
 static inline int hugetlb_cgroup_charge_cgroup_rsvd(int idx,
-						    unsigned long nr_pages,
-						    struct hugetlb_cgroup **ptr)
+		unsigned long nr_pages, struct mm_struct *mm,
+		struct hugetlb_cgroup **ptr)
 {
 	return 0;
 }
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 1b12277cd3f0a..92f27dd6c4604 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -2312,7 +2312,7 @@ struct folio *alloc_hugetlb_folio_reserve(struct hstate *h, int preferred_nid,
 	int idx = hstate_index(h);
 	int ret;
 
-	if (hugetlb_cgroup_charge_cgroup(idx, nr_pages, &h_cg))
+	if (hugetlb_cgroup_charge_cgroup(idx, nr_pages, NULL, &h_cg))
 		return NULL;
 
 	spin_lock_irq(&hugetlb_lock);
@@ -3010,7 +3010,8 @@ void wait_for_freed_hugetlb_folios(void)
  * hugetlb_alloc_folio - Allocate a hugetlb folio.
  * @h: Hugetlb state control block.
  * @mpoli: Interpreted memory policy to use for allocation.
- * @mm: Memory descriptor of the allocation target.
+ * @mm: Memory descriptor of the allocation target. With CONFIG_MEMCG, the
+ *      hugetlb cgroup of the owner of @mm is charged.
  * @alloc_flags: Flags controlling the allocation behavior.
  *
  * Allocates a hugetlb folio and handles cgroup charging and global hstate
@@ -3036,10 +3037,10 @@ struct folio *hugetlb_alloc_folio(struct hstate *h,
 	int ret;
 
 	if (charge_hugetlb_cgroup_rsvd &&
-	    hugetlb_cgroup_charge_cgroup_rsvd(idx, nr_pages, &h_cg_rsvd))
+	    hugetlb_cgroup_charge_cgroup_rsvd(idx, nr_pages, mm, &h_cg_rsvd))
 		return ERR_PTR(-ENOSPC);
 
-	if (hugetlb_cgroup_charge_cgroup(idx, nr_pages, &h_cg)) {
+	if (hugetlb_cgroup_charge_cgroup(idx, nr_pages, mm, &h_cg)) {
 		ret = -ENOSPC;
 		goto err_uncharge_hugetlb_cgroup_rsvd;
 	}
@@ -7044,7 +7045,8 @@ long hugetlb_reserve_pages(struct inode *inode,
 	}
 
 	err = hugetlb_cgroup_charge_cgroup_rsvd(hstate_index(h),
-				chg * pages_per_huge_page(h), &h_cg);
+						chg * pages_per_huge_page(h),
+						vma ? vma->vm_mm : NULL, &h_cg);
 	if (err < 0)
 		goto out_err;
 
diff --git a/mm/hugetlb_cgroup.c b/mm/hugetlb_cgroup.c
index 6ee771334a3bd..67446ad417571 100644
--- a/mm/hugetlb_cgroup.c
+++ b/mm/hugetlb_cgroup.c
@@ -281,9 +281,22 @@ static inline void hugetlb_event(struct hugetlb_cgroup *hugetlb, int idx,
 		 !hugetlb_cgroup_is_root(hugetlb));
 }
 
+static struct hugetlb_cgroup *hugetlb_cgroup_from_mm(struct mm_struct *mm)
+{
+	struct task_struct *task = current;
+
+#ifdef CONFIG_MEMCG
+	/* Like memcg, charge the owner of the mm we allocate for. */
+	if (!mm)
+		mm = current->mm;
+	if (mm)
+		task = rcu_dereference(mm->owner);
+#endif
+	return task ? hugetlb_cgroup_from_task(task) : NULL;
+}
+
 static int __hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
-					  struct hugetlb_cgroup **ptr,
-					  bool rsvd)
+		struct mm_struct *mm, struct hugetlb_cgroup **ptr, bool rsvd)
 {
 	int ret = 0;
 	struct page_counter *counter;
@@ -293,7 +306,12 @@ static int __hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
 		goto done;
 again:
 	rcu_read_lock();
-	h_cg = hugetlb_cgroup_from_task(current);
+	h_cg = hugetlb_cgroup_from_mm(mm);
+	if (!h_cg) {
+		rcu_read_unlock();
+		ret = -ENOMEM;
+		goto done;
+	}
 	if (!css_tryget(&h_cg->css)) {
 		rcu_read_unlock();
 		goto again;
@@ -318,15 +336,15 @@ static int __hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
 }
 
 int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
-				 struct hugetlb_cgroup **ptr)
+		struct mm_struct *mm, struct hugetlb_cgroup **ptr)
 {
-	return __hugetlb_cgroup_charge_cgroup(idx, nr_pages, ptr, false);
+	return __hugetlb_cgroup_charge_cgroup(idx, nr_pages, mm, ptr, false);
 }
 
 int hugetlb_cgroup_charge_cgroup_rsvd(int idx, unsigned long nr_pages,
-				      struct hugetlb_cgroup **ptr)
+		struct mm_struct *mm, struct hugetlb_cgroup **ptr)
 {
-	return __hugetlb_cgroup_charge_cgroup(idx, nr_pages, ptr, true);
+	return __hugetlb_cgroup_charge_cgroup(idx, nr_pages, mm, ptr, true);
 }
 
 /* Should be called with hugetlb_lock held */

-- 
2.56.1


  parent reply	other threads:[~2026-12-03 11:46 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 11:37 [PATCH v2 00/35] mm/hugetlb: various fixes (2) David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 01/35] mm/hugetlb: charge the copy made at fork to the parent David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 02/35] mm/hugetlb: restore the reservation if vma_needs_reservation() fails David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 03/35] mm/hugetlb: restore the reservation when racing with another reservation David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 04/35] mm/hugetlb: return the reservation to the subpool on error David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 05/35] mm/hugetlb: report reservations that we actually added from hugetlb_reserve_pages() David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 06/35] mm/hugetlb: keep HPAGE_RESV_UNMAPPED when forking David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 07/35] mm/hugetlb: always mark new page cache folios migratable David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 08/35] mm/memfd: mark new hugetlb " David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 09/35] mm/hugetlb: free surplus folios of other nodes on free David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 10/35] mm/hugetlb: wait for deferred frees before failing an allocation David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 11/35] mm/hugetlb_cma: reset hugetlb_cma_size if the arch doesn't support it David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 12/35] mm/hugetlb: fix truncated copy result in UFFDIO_COPY David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 13/35] fs/proc/task_mmu: don't clear VM_SOFTDIRTY on hugetlb VMAs David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 14/35] hugetlbfs: fix integer overflow when punching holes David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 15/35] hugetlbfs: don't zero poisoned pages in a hole punch David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 16/35] hugetlbfs: fix deadlock between hole punching and memory failure David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 17/35] hugetlbfs: don't overflow the pseudo VMA address in fallocate() David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 18/35] hugetlbfs: don't overflow the end of the unmap range on 32-bit David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 19/35] hugetlbfs: recheck the vma range after the lock wait David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 20/35] mm/hugetlb: don't zap non-present entries when unmapping a folio David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 21/35] hugetlbfs: don't skip private VMAs when unmapping on truncation David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 22/35] mm/hugetlb: serialize private faults with truncation David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 23/35] mm/hugetlb: check EOF for UFFDIO_COPY and UFFDIO_POISON David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 24/35] hugetlbfs: seal SHM_HUGETLB files against growth David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 25/35] hugetlbfs: zero all hugetlbfs-specific fields of new inodes David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 26/35] mm/rmap: don't fail unmapping hugetlb folios from private VMAs David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 27/35] mm/hugetlb: only check PMD table sharing for PMD-sized folios David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 28/35] mm/migrate: check hugetlb PMD sharing in move_pages() David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 29/35] mm/hugetlb: don't share page tables of a VMA during mprotect() David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 30/35] mm/hugetlb: unshare PMDs of a split VMA if allocating its lock fails David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 31/35] mm/hugetlb: count migration entries at fork and unmap David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 32/35] mm/hugetlb: stop accounting HugetlbPages once PMD tables were shared David Hildenbrand (Arm)
2026-12-03 11:37 ` [PATCH v2 33/35] mm/hugetlb_cgroup: track in-use folios per cgroup David Hildenbrand (Arm)
2026-12-03 11:37 ` David Hildenbrand (Arm) [this message]
2026-12-03 11:37 ` [PATCH v2 35/35] mm/hugetlb: resolve protnone faults without a uffd bit David Hildenbrand (Arm)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203-hugetlb_fixes_2-v2-34-9bbeabb555d2@kernel.org \
    --to=david@kernel.org \
    --cc=aarcange@redhat.com \
    --cc=airlied@redhat.com \
    --cc=ak@linux.intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=almasrymina@google.com \
    --cc=aneesh.kumar@kernel.org \
    --cc=apopple@nvidia.com \
    --cc=apw@canonical.com \
    --cc=axelrasmussen@google.com \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=brauner@kernel.org \
    --cc=byungchul@sk.com \
    --cc=corbet@lwn.net \
    --cc=dave@stgolabs.net \
    --cc=gourry@gourry.net \
    --cc=guillaume@morinfr.org \
    --cc=hannes@cmpxchg.org \
    --cc=harry@kernel.org \
    --cc=hughd@google.com \
    --cc=jannh@google.com \
    --cc=jgg@ziepe.ca \
    --cc=jiaqiyan@google.com \
    --cc=jinmengzhou22@gmail.com \
    --cc=joern@lazybastard.org \
    --cc=joshua.hahnjy@gmail.com \
    --cc=jthoughton@google.com \
    --cc=kas@kernel.org \
    --cc=kraxel@redhat.com \
    --cc=lance.yang@linux.dev \
    --cc=liam@infradead.org \
    --cc=lihongbo22@huawei.com \
    --cc=lihongfu@kylinos.cn \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=mason@kernel.org \
    --cc=matthew.brost@intel.com \
    --cc=mgorman@suse.de \
    --cc=mhocko@suse.com \
    --cc=muchun.song@linux.dev \
    --cc=nao.horiguchi@gmail.com \
    --cc=osalvador@kernel.org \
    --cc=peterx@redhat.com \
    --cc=pfalcato@suse.de \
    --cc=rakie.kim@sk.com \
    --cc=rdunlap@infradead.org \
    --cc=riel@surriel.com \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=rostedt@goodmis.org \
    --cc=rppt@kernel.org \
    --cc=sashal@kernel.org \
    --cc=shakeel.butt@linux.dev \
    --cc=shy828301@gmail.com \
    --cc=skhan@linuxfoundation.org \
    --cc=surenb@google.com \
    --cc=vbabka@kernel.org \
    --cc=vivek.kasireddy@intel.com \
    --cc=ying.huang@linux.alibaba.com \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox