Re: [PATCH v2 34/35] mm/hugetlb_cgroup: charge faults to mm->owner

From: Muchun Song

Date: Fri Dec 04 2026 - 01:34:27 EST




> On Dec 3, 2026, at 19:37, David Hildenbrand (Arm) <david@xxxxxxxxxx> wrote:
>
> 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@xxxxxxxxxx>
> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>

Acked-by: Muchun Song (ByteDance) <muchun.song@xxxxxxxxx>

Thanks.