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=1796298332; b=zJehdsU2r+TigeFLowHNmvg6Qkp/4LPJbR3paFETHqbY6OMBzifagIFms3aS27Mb8+yq 6m3plydgxFDEuVBD7FY+JXo/frew/go9SoeS+A63UiiFz9PftIdCvvEv5bNnzO0d5223/ vp8x6mcWzYlj7mgm5Go1MmFXm9ata2KnkOpCHsLtTlYpBOwfL+Yw7zFQvwKMccjL6XOhI nBJjlsPcBU9hmL6LrRvQ3J7amgTiNV77GIRkSc2//cbg3G+HhYyYsgXLCqT0ufa2UDv4M hJzgRNWzYXbVH78Ws+tMaEl3dNZopRY/ESwHDHOBX67U66TL0IwqiWRZ4cNDrZfCxWQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796298332; 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=SDEXuY6JLFM9DKmfQzE18Ni0xAzpy9/CcZGQXiuw5nM=; b=0LAw18djNSVmrmzCT7AT/EbMpkYXeKTaiUWSo+3DQG8ZCCGbtoXOOkxfdsiUXxhykpX9 0ltbiUmQY+wKhqdxOGnyChkJo2JoVIleSbZJeHmBm76hvglVFG4FefPL3UQuvyLy9QhK0 Q6FN2yCfkHw/vtkdsiBKxlLaMwsD0DJUQIKrbRSwLg8pCse9zr4s+bZHlS93ofupz0PZY 7ntINA3VOJw9tsC7n4eOe1MSctsSPs8IKOiJjeLBQ3/vxBE2t7/UmDLuWkAy9qbQMhq+J vEQ3WIKPplstqIwfpoh0lkClB0qvd/jaXIXQH1pk2fRNrXzvfOC9I3oYNd4f0Sp4JTw== 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=oRqPcyjT; 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 E92C3606A322; Thu, 3 Dec 2026 11:45:31 +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=oRqPcyjT Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8C0091F000C1; Thu, 3 Dec 2026 11:45:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796298331; bh=SDEXuY6JLFM9DKmfQzE18Ni0xAzpy9/CcZGQXiuw5nM=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=oRqPcyjTDpoNuTRJhi60dQlt5+4VLwv4t9ZfpM3tLNGh4A7C7SuSgMaggdWox6XyY H241gEggEaiwGILQb8UY+/EYQWHDme4X/7Uv30mO3tbzvrTehIAcuHk+Ne4PDSuU7z C5j6+/riTrQzeeAwnb12VuhOaTVvjQObTsKh7JgewgduBJnOyXz0NWhh71liW1ihVJ 6eDLHf5DxiM6G3DyRDwK5z8GDu53qNwRXCNyf1rL9bM2mRZcfAWr3rAgrbwaf1KiEJ sqqfdXhYtn3BtOiRBYHecTYcu+rpNGYCoxtZd578DS6Loh67xWvjDw+ZjpH0I8zNJ7 o2THd148UoAuA== From: "David Hildenbrand (Arm)" Date: Thu, 03 Dec 2026 12:37:54 +0100 Subject: [PATCH v2 31/35] mm/hugetlb: count migration entries at fork and unmap X-Mailing-List: linux-doc@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-31-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=2527; i=david@kernel.org; h=from:subject:message-id; bh=rEtaoSH8GN9S8ztJzOfUpESSemsork7ag/98vUcYlhc=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLIFQ2rX3C61cVn8ucb9h7vqvD1P1XgCGD7UyRoFhGqsO bDmV9KHjlIWBjEOBlkxRZag4PsGL1+uq61242OGmcPKBDKEgYtTACYypYqRYdWyqm3Vq/sXVtzc 3ft88ZonxaKh3+tnWWnMaFF90Maf/JyR4U1/QYjr8Yyq6Xob1NQPsS06FtbocbiySLHhh9qGSf9 ZuAE= X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A HugetlbPages in /proc//status can be wrong, and even drop below zero, if we fork() or unmap while mapped hugetlb folios are being migrated. There is an inconsistency: when installing migration entries we don't adjust the counter, treating them as if they were present. However, when copying or unmapping a migration entry, we don't adjust the counter like we would for a present entry. Let's fix it by adjusting the counter also for migration entries in these cases. Add pte_is_migration_entry() like we did for other softleaf types already. This has low urgency and given that this has been around forever and likely nobody cares, let's not CC stable. Fixes: 5d317b2b6536 ("mm: hugetlb: proc: add HugetlbPages field to /proc/PID/status") Reported-by: Chris Mason Signed-off-by: David Hildenbrand (Arm) --- include/linux/leafops.h | 11 +++++++++++ mm/hugetlb.c | 3 +++ 2 files changed, 14 insertions(+) diff --git a/include/linux/leafops.h b/include/linux/leafops.h index 4c1476ae32343..c315a3531485e 100644 --- a/include/linux/leafops.h +++ b/include/linux/leafops.h @@ -587,6 +587,17 @@ static inline bool pte_is_uffd_marker(pte_t pte) return false; } +/** + * pte_is_migration_entry() - Does this PTE entry encode a migration entry? + * @pte: PTE entry. + * + * Returns: true if the PTE encodes a migration entry, otherwise false. + */ +static inline bool pte_is_migration_entry(pte_t pte) +{ + return softleaf_is_migration(softleaf_from_pte(pte)); +} + #if defined(CONFIG_ZONE_DEVICE) && defined(CONFIG_ARCH_HAS_PMD_SOFTLEAVES) /** diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 1c7259d80615e..c1fbe66d30332 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5153,6 +5153,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, if (!userfaultfd_protected(dst_vma)) entry = pte_swp_clear_uffd(entry); set_huge_pte_at(dst, addr, dst_pte, entry, sz); + hugetlb_count_add(npages, dst); } else if (unlikely(pte_is_marker(entry))) { const pte_marker marker = copy_pte_marker(softleaf, dst_vma); @@ -5502,6 +5503,8 @@ static void hugetlb_unmap_range(struct mmu_gather *tlb, spin_unlock(ptl); continue; } + if (pte_is_migration_entry(pte)) + hugetlb_count_sub(pages_per_huge_page(h), mm); /* * If the pte was wr-protected by uffd-wp in any of the * swap forms, meanwhile the caller does not want to -- 2.56.1