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=1794631154; b=WiMHZ4oSD8ezUc+gYvnqvrZLw+QtvFQdvehFtsZ4RkCMXadCp6tDdlbpLUxfQqTIhH2W NlWb+MlZqPTajzAycX6dKID4XPo5vCYEJID0l7xRx5kNDCQNUuLskxxhE7Apl+X+P6Ods g27o3k5iDUtrOLWTUv9hozOHU8i8vj92K2VF12mVhsbDGf+KDjFMLt7XVeOtWw1kLLZVU 126STagIlcH/rGHpgOtuhg8tLXM15wBiSlbAmoJ6DvwsOcAPXeijM3V5tq1mrHDWUbLJr ZMWdSf2AixrVKEmeV1T4F05FwPtYWOMOF9DvKXQ6OegjYtv1ltwYhWLlAIEebRhcgtQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794631154; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=8s3Zjpq1Wn2hqpOmLwfJueGowbcwWVBXEz/KmjWMaa4=; b=5Z2A3uRwHt7gwqudPez0yT6kHId5ujpkglFVDoNLyfxD+p/ohZCbWrFfeEN8stbRuP0f gTXBABDtyqS6yVLo3cNtT7q+lIMWjEjz26Mk8/f10SzEzkhkwk8VJkkgZEdcgc2PYIy29 dmYjrphEtgEu43QzrgdlpCvBa/Rze9YplRAERytu8bR3Ot3tZN69VE9Gxgyr3A26yMaGa 05WfUq7SwZSDC9G0wtDcFKTDiQ5AdtD/DV7WFQq8iajhesw+C9JVpan+q8G69q1Ekgb4a Tni1maMJnPVrFm3A6T1LFUBu3MTogRqp54bKINBLAX4YTD7EfpaPr2nkOisN25dj+fg== 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=TwJ37vfB; 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 B5E25606A32D for ; Sat, 14 Nov 2026 04:39:14 +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=TwJ37vfB Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4F3211F000C4; Sat, 14 Nov 2026 04:39:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1794631154; bh=8s3Zjpq1Wn2hqpOmLwfJueGowbcwWVBXEz/KmjWMaa4=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=TwJ37vfB3RUqzTuuTQ2/Y15pe6UF3JVGxnv96/eY91g3jSAqElG0smvAR9OY9f1cK fIL0taC+3FUVyt9IVfNfsbiLJeaJvzkg1acbOX/YU9qOGGLcy2FMSdXNyRhtJwkW7C Ym7FKEKSpbax/Xrcqsw8QvPbV34IHG+AR5K1scq98bu+X+hz6+5leibGmvRr8sm5m4 p8mr30ygC+TYYelNAdmo2sI3o90wwJG4GMvEZi7AjZieY3lvURtanlirg9+4Ior3DU cOijqLaD4rl677C835opLmseeTuflig+4N59fXipWpuERCmm47hcW/5fabaae6+BkL BYtFPipLTYQ8Q== From: Sasha Levin To: stable@vger.kernel.org Cc: Li Zhe , "David Hildenbrand (Arm)" , Andrew Morton , Muchun Song , Oscar Salvador , Peter Xu , Mike Rapoport , "Jose A. Perez de Azpillaga" , Sasha Levin Subject: [PATCH 6.6.y] mm/hugetlb: fix mremap deadlock when PMD sharing is possible Date: Fri, 13 Nov 2026 23:39:12 -0500 Message-ID: <20261114043851.2942060-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261109201210-failed-6.6-92daa8652f88@kernel.org> References: <20261109201210-failed-6.6-92daa8652f88@kernel.org> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Li Zhe [ Upstream commit 92daa8652f88c8cfc05f4899a8408a04b02f211e ] move_hugetlb_page_tables() holds the mapping's i_mmap_rwsem for write while moving hugetlb PTEs. This prevents races with truncation and protects huge_pmd_unshare(), but the move loop must not call helpers that can acquire the same rwsem for read. huge_pte_alloc() can do that when the destination can use PMD sharing: it may call huge_pmd_share(), which takes i_mmap_rwsem for read while searching for a shareable PMD table. Calling it while the move path already holds i_mmap_rwsem for write can deadlock. Avoid that path for PMD-sized hugetlb mappings that can use PMD sharing. Instead of allocating or sharing the destination page table while moving, zap the source range and let the destination fault path populate or share the PMD lazily. Reuse the hugetlb unmap logic for the source-side zap by splitting the core loop out of __unmap_hugepage_range(). The wrapper keeps the existing tlb_start_vma(), tlb_end_vma() and huge_pmd_unshare_flush() behavior, while move_hugetlb_page_tables() can reuse the unmap loop with its existing mmu_gather. Link: https://lore.kernel.org/20261022034543.51226-4-lizhe.67@bytedance.com Fixes: 550a7d60bd5e ("mm, hugepages: add mremap() support for hugepage backed vma") Signed-off-by: Li Zhe Co-developed-by: David Hildenbrand (Arm) Signed-off-by: David Hildenbrand (Arm) Signed-off-by: Andrew Morton Cc: Muchun Song Cc: Oscar Salvador Cc: Peter Xu Cc: Mike Rapoport Cc: Jose A. Perez de Azpillaga Cc: [ sashal: adapted hugetlb unmap helpers to use struct page ref_page instead of struct folio ] Signed-off-by: Sasha Levin --- mm/hugetlb.c | 47 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 8e1da134d2a75..7c31def01df68 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -100,6 +100,10 @@ static int __huge_pmd_unshare(struct mmu_gather *tlb, bool check_locks); static void hugetlb_unshare_pmds(struct vm_area_struct *vma, unsigned long start, unsigned long end, bool take_locks); +static void hugetlb_unmap_range(struct mmu_gather *tlb, + struct vm_area_struct *vma, unsigned long start, + unsigned long end, struct page *ref_page, + zap_flags_t zap_flags); static struct resv_map *vma_resv_map(struct vm_area_struct *vma); static inline bool subpool_is_free(struct hugepage_subpool *spool) @@ -5303,6 +5307,8 @@ unsigned long move_hugetlb_page_tables(struct vm_area_struct *vma, for (; old_addr < old_end; old_addr += sz, new_addr += sz) { const unsigned long offset_to_last_entry = (old_addr | last_addr_mask) - old_addr; + const unsigned long offset_to_last_dst_entry = + (new_addr | last_addr_mask) - new_addr; src_pte = hugetlb_walk(vma, old_addr, sz); if (!src_pte) { @@ -5319,6 +5325,24 @@ unsigned long move_hugetlb_page_tables(struct vm_area_struct *vma, continue; } + /* + * Avoid entering the destination PMD sharing path while holding + * i_mmap_rwsem for write. Leave destination page table + * population to the fault path, which can share the PMD lazily. + */ + if (sz == PMD_SIZE && want_pmd_share(new_vma, new_addr)) { + const unsigned long unmap_size = + min(old_end - old_addr, + offset_to_last_dst_entry + sz); + + hugetlb_unmap_range(&tlb, vma, old_addr, + old_addr + unmap_size, NULL, + ZAP_FLAG_DROP_MARKER); + old_addr += unmap_size - sz; + new_addr += unmap_size - sz; + continue; + } + dst_pte = huge_pte_alloc(mm, new_vma, new_addr, sz); if (!dst_pte) break; @@ -5338,9 +5362,10 @@ unsigned long move_hugetlb_page_tables(struct vm_area_struct *vma, return len + old_addr - old_end; } -void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, - unsigned long start, unsigned long end, - struct page *ref_page, zap_flags_t zap_flags) +static void hugetlb_unmap_range(struct mmu_gather *tlb, + struct vm_area_struct *vma, unsigned long start, + unsigned long end, struct page *ref_page, + zap_flags_t zap_flags) { struct mm_struct *mm = vma->vm_mm; unsigned long address; @@ -5356,13 +5381,6 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, BUG_ON(start & ~huge_page_mask(h)); BUG_ON(end & ~huge_page_mask(h)); - /* - * This is a hugetlb vma, all the pte entries should point - * to huge page. - */ - tlb_change_page_size(tlb, sz); - tlb_start_vma(tlb, vma); - last_addr_mask = hugetlb_mask_last_page(h); address = start; for (; address < end; address += sz) { @@ -5447,6 +5465,15 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, if (ref_page) break; } +} + +void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, + unsigned long start, unsigned long end, + struct page *ref_page, zap_flags_t zap_flags) +{ + tlb_change_page_size(tlb, huge_page_size(hstate_vma(vma))); + tlb_start_vma(tlb, vma); + hugetlb_unmap_range(tlb, vma, start, end, ref_page, zap_flags); tlb_end_vma(tlb, vma); huge_pmd_unshare_flush(tlb, vma); -- 2.53.0