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=1796297884; b=N3oNOr/4wXmR6hKfS0sDQ3tQSOQhVyG2V1dzyBQurqg4kFc0oIzWFb6hNHdYZ5W8sdJN R3I9mmuFcaJHDfSn3eglVbB41WDqwI1pCepdHHFPcLoQVgZ4SkEJhNKHH/wUCYpROClOX aZgacf9OyXLqz+gQxSpYkVK2vZ6FutvogQy1OiNxcb+J7VowUndyycM2uysYPZgj8Itth 1Z0v84uMV9/f2qFmhXrSghy2nmt+ko7PnHkVy6oOcXbIjV8y/v2Px4lA0VNZU0Kbf+N6B RihJHHZZVwWS55WFtcVL+P2X7PhUrDEHox1Uwi7vHn3xXMNFIct/TPaB5jOihOXAG4g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796297884; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=tMygRg7/JPkpNJXB9KOMXZMvIN62qnxw+kFtQ5ufuo4=; b=AirL/iZvjYhrsG5KeM29ETp3YaK0cUljo7ipDNPTdYRnGQwvsJQXzG2k3626pyAD9dvO LewLZx/QbN1OiZsTGhdExUcYOQ2Fd8PnLsviAsDB7ac0JjEgGTGJaPKw7gXZf0uz3uFQE jUBn64OHMFKddiHJvt3eApHA7JEAM5v9Uf3drI4ItKsnVCQO2ax+rV4dTJ+8O/t4ZOe7Z 1zPxxG+sEvwRF/yWZpvYZxzhhTjv0bXNsWlXxwIUwAGjJkfGRGh1H3Sv+ZHKsfWlhCK5u XDHXyFNV1fz1MpmJTyrY/mzCnjfHv3IYRW95l75zl2rQaFwpsjbRiIPPYUMbT4zvSyg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=KA9MSxmJ; 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 93BF1606A322; Thu, 3 Dec 2026 11:38:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=KA9MSxmJ Received: by smtp.kernel.org (Postfix) with ESMTPSA id BDB2A1F000C1; Thu, 3 Dec 2026 11:37:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796297884; bh=tMygRg7/JPkpNJXB9KOMXZMvIN62qnxw+kFtQ5ufuo4=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=KA9MSxmJM+Am3oA0drXsGjZXcLsUpKeKsTn9kRDImFCjbsrhZnaVus51dBp3tzskT UvjyA7mInSeT8fRMu9q85fulbQfs3ef/l1+v/MzYpIuLmwPRpYnnXbv8OB59vstaB3 s3lMI3VYubquh9xPwt+RPxt4r+kVp6rq8Iu6eER9SZauZgPLFpYQjk7RchOlLtrsHN PfE+4l1cvvPIJO2/c70jiigN5PnmKI40FambFT+rVHJ4Ucr03Tig8LmoBjqxQiQf0O qjFHdgvSiWpEZV0fpwBTTDQBv0NVBdDkB/IQ8odD02SnwP4kJHOnueA0/GVoVK8ruN 1AQ8zDhXu4UMA== From: "David Hildenbrand (Arm)" Date: Thu, 03 Dec 2026 12:37:24 +0100 Subject: [PATCH v2 01/35] mm/hugetlb: charge the copy made at fork to the parent 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="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261203-hugetlb_fixes_2-v2-1-9bbeabb555d2@kernel.org> References: <20261203-hugetlb_fixes_2-v2-0-9bbeabb555d2@kernel.org> In-Reply-To: <20261203-hugetlb_fixes_2-v2-0-9bbeabb555d2@kernel.org> To: Muchun Song , Oscar Salvador , Andrew Morton , Hongfu Li , Jinmeng Zhou , Mina Almasry , Davidlohr Bueso , Naoya Horiguchi , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Michal Hocko , Jonathan Corbet , Shuah Khan , Randy Dunlap , Vivek Kasireddy , Mel Gorman , Hugh Dickins , Baolin Wang , Dave Airlie , Jason Gunthorpe , Andy Whitcroft , Roman Gushchin , Barry Song , Andrea Arcangeli , Lorenzo Stoakes , Suren Baghdasaryan , Shakeel Butt , Pedro Falcato , Jann Horn , Guillaume Morin , Yang Shi , James Houghton , Rik van Riel , Harry Yoo , Lance Yang , Axel Rasmussen , Peter Xu , Sasha Levin , Kiryl Shutsemau , Christian Brauner , "Steven Rostedt (Google)" , Hongbo Li , Gregory Price , Zi Yan , Joshua Hahn , Rakie Kim , Ying Huang , Matthew Brost , Byungchul Park , Alistair Popple , Andi Kleen , David Rientjes , Joern Engel , "Aneesh Kumar K.V" Cc: linux-mm@kvack.org, linux-doc@vger.kernel.org, Gerd Hoffmann , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Johannes Weiner , Chris Mason , Jiaqi Yan , "David Hildenbrand (Arm)" X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3280; i=david@kernel.org; h=from:subject:message-id; bh=+7rHZt5Lp/kATTqgAUeZmPufIa5ERVyde6DjI+MDFQg=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLIFQyr7dqT5/4nJurLe+C234685J7gK1cW/P7vtNHPPI Z+f2l7HO0pZGMQ4GGTFFFmCgu8bvHy5rrbajY8ZZg4rE8gQBi5OAZgItzfDP5vXB5cevJr/jO3V Lq+nF1bmflr7p+m970Hd7Qef3DYT0LdgZJgYF7w8M1gxk91Kpeeg4KLQqlqWugXLm1b531j0I+P YBy4A X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A When copying page tables during fork(), the child is not yet assigned to the right memcg. When copying possibly pinned anon hugetlb folios, we must charge them to the parent's memcg, otherwise we can end up allocating more hugetlb folios than allowed. Let's fix it by charging the copy to the parent's mm, like we do when copying small folios in copy_present_page(), introducing a new helper function alloc_hugetlb_folio_for_fork(). This is a hotfix for v7.4. Fixes: c2deb1356d82 ("mm/hugetlb: charge folios to the target mm's memcg") Reported-by: Chris Mason Acked-by: Michal Hocko Reviewed-by: Muchun Song (ByteDance) Acked-by: Oscar Salvador (SUSE) Tested-by: Hongfu Li Reviewed-by: Hongfu Li Acked-by: Harry Yoo (Meta) Signed-off-by: David Hildenbrand (Arm) --- mm/hugetlb.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index d0e66fb306221..f46c85fc0cd0b 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3103,8 +3103,9 @@ typedef enum { * allocation). New call sites should (probably) never set it to true!! * When it's set, the allocation will bypass all vma level reservations. */ -struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, - unsigned long addr, bool cow_from_owner) +static struct folio *__alloc_hugetlb_folio(struct vm_area_struct *vma, + unsigned long addr, bool cow_from_owner, + struct mm_struct *memcg_mm) { struct hugepage_subpool *spool = subpool_vma(vma); struct hstate *h = hstate_vma(vma); @@ -3187,7 +3188,7 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, .nodemask = nodemask, }; - folio = hugetlb_alloc_folio(h, &mpoli, vma->vm_mm, alloc_flags); + folio = hugetlb_alloc_folio(h, &mpoli, memcg_mm, alloc_flags); mpol_cond_put(mpol); @@ -3249,6 +3250,23 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, return ERR_PTR(ret); } +struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, + unsigned long addr, bool cow_from_owner) +{ + return __alloc_hugetlb_folio(vma, addr, cow_from_owner, vma->vm_mm); +} + +/* + * Allocate a folio for a copy in the child VMA during fork(), charged to the + * parent's mm: the child is not in its memory cgroup yet. + */ +static struct folio *alloc_hugetlb_folio_for_fork(struct vm_area_struct *dst_vma, + unsigned long addr, struct mm_struct *src_mm) +{ + return __alloc_hugetlb_folio(dst_vma, addr, /* cow_from_owner = */ false, + src_mm); +} + static __init void *alloc_bootmem(struct hstate *h, int nid, bool node_exact) { if (hugetlb_early_cma(h)) @@ -5110,7 +5128,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, spin_unlock(src_ptl); spin_unlock(dst_ptl); /* Do not use reserve as it's private owned */ - new_folio = alloc_hugetlb_folio(dst_vma, addr, false); + new_folio = alloc_hugetlb_folio_for_fork(dst_vma, addr, src); if (IS_ERR(new_folio)) { folio_put(pte_folio); ret = PTR_ERR(new_folio); -- 2.56.1