From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.127.231.111 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1792640866; b=yc2F5D2xZby9XoEbwKtmqeV9hjoQgveL7u4GSrNWMXpS+ck5udD81yrHmwBFRs3NmlyT wpGk0k4qjAS+Gsi0jrXTg1SeLvPU+2I2ovOQEGCaBeCF/Uv61F8+aAxXjSIppHJ1SEliW W09fdLIi0pjiKBBG0878R5w47rzVP3zHZcBLaWud5A3eZadI1usFJ2JnafYy40aemuuw5 bWvK3zSpTxue06MpIMVE7gmwbJLIFU1H9IQ/vgUW78ndCns+MGOOhvcO+UT9gwnoFXDDw 08jC956TS60c8UEQ0JUGUQCh2Op3qU3s7KqPJQ0rPPr7xeE+1JyYAPBFg0tUj7YXfRw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792640866; h=DMARC-Filter:DKIM-Signature:Message-Id:Mime-Version:Cc:Subject: References:X-Mailer:X-Lms-Return-Path:Content-Type: Content-Transfer-Encoding:To:From:Date:X-Original-From:In-Reply-To; bh=WGcAsm2Fdf4fA3iYp2iJ468MdCKcNJG+g/TQikU8VjE=; b=vXUri8kxzLqUcDWrse2jxA3WSLedoqXHCTC5snf5FCaD014ovm0o3nVkLwnKrEYnSG7S 7fe77CEmQSZMY/DWoIf1KnV/UNMCf+lpS2q4CUTAGOt32OzG26oXe4IBc6AIzxTCQJOOB 4NAHFdVpcWxqCSzrgjEDKmXARQZm0ZtbIvdyrJPDvL4LiRjCMN14GMCPVwxstbQtf0Lw2 sMUErx9+2siI4vN3QmQK1tJBC81QQ267D8hHWusivOLQD5WqflFNpnuDT4f0IFHWo1JN3 +1waBMqH/qXH1JynoR00sSrtb2panl+ICND8O4hOPpb0z30fejh1qZNMy1WcLKp43iQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass header.d=bytedance.com header.i=@bytedance.com header.a=rsa-sha256 header.s=2212171451 header.b=pTYh9XOh; arc=none smtp.remote-ip=209.127.231.111 Received: from va-2-111.ptr.blmpb.com (va-2-111.ptr.blmpb.com [209.127.231.111]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E3E05606A31D for ; Thu, 22 Oct 2026 03:47:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E3E05606A31D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=bytedance.com header.i=@bytedance.com header.a=rsa-sha256 header.s=2212171451 header.b=pTYh9XOh DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=2212171451; d=bytedance.com; t=1792640861; h=from:subject: mime-version:from:date:message-id:subject:to:cc:reply-to:content-type: mime-version:in-reply-to:message-id; bh=WGcAsm2Fdf4fA3iYp2iJ468MdCKcNJG+g/TQikU8VjE=; b=pTYh9XOhZvvQm45qVB4SyZQC/Rr5ZBPu6Q37UakLO+g/O9hZZ7G6Kca8Gw+XJb5ujf2TdX 744RVEZJymB/4lCPnXQPH+e6ZLadmFSKhtZFYG7MjsSsK4h/SpFlRHT2mHpEE/UqUJ0Kf8 v7Y2cJD1aL6K3lqmLP5RFJncqThy9xYYLOP8lE296EZ2/aPVtTSeCmhnX+O2blsre1dAW4 +s7vG/fFDZNqg6q+ulNyutNWjrKo+0pCOFGh3JPc3gd6gVjzMp92liMkoMXUXbJ2Holy1g FWlzI7RytuWx/UybqSV2azw9OvMpyDg6qy8WGCL8lSFe1i8zc9EFB/KTvOOW1Q== Message-Id: <20261022034543.51226-4-lizhe.67@bytedance.com> 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 Cc: , , "Li Zhe" , Subject: [PATCH v3 3/3] mm/hugetlb: fix mremap deadlock when PMD sharing is possible References: <20261022034543.51226-1-lizhe.67@bytedance.com> X-Mailer: git-send-email 2.45.2 X-Lms-Return-Path: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit To: , , , , , , From: "Li Zhe" Date: Thu, 22 Oct 2026 11:45:43 +0800 X-Original-From: Li Zhe In-Reply-To: <20261022034543.51226-1-lizhe.67@bytedance.com> 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. Fixes: 550a7d60bd5e ("mm, hugepages: add mremap() support for hugepage backed vma") Cc: stable@vger.kernel.org Co-developed-by: David Hildenbrand (Arm) Signed-off-by: David Hildenbrand (Arm) Signed-off-by: Li Zhe --- mm/hugetlb.c | 47 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 3cc33ac85a15b..dc6fc3fe4d4a3 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -124,6 +124,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 folio *folio, + 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) @@ -5171,6 +5175,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) { @@ -5187,6 +5193,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; @@ -5206,9 +5230,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 folio *folio, 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 folio *folio, + zap_flags_t zap_flags) { struct mm_struct *mm = vma->vm_mm; const bool folio_provided = !!folio; @@ -5226,13 +5251,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) { @@ -5361,6 +5379,15 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, if (folio_provided) break; } +} + +void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, + unsigned long start, unsigned long end, + struct folio *folio, 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, folio, zap_flags); tlb_end_vma(tlb, vma); huge_pmd_unshare_flush(tlb, vma); -- 2.20.1