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=1796382530; b=UeXC13TaQpwlEIBBnf+z2BB2QAh9ZbeYkuiZ+Xe+pxxlAtZX3G/Hz1LscbIHpfq5/39P 8G8A+CeaBfVraNKYQu1zw5bgad16fHG5TVOmcR4bHh/x5WmXMpdovTDCRBw20HdVv7Bjm sWqfwf6go0AMeAzQfjnMUETjGeKzWutsp9wJRKxsCyLeV88mylCRXbtaHQL8DAumCODg0 84QNWBeHbgROlIBfcFZfxYgrkDNg45+ie3Wfc1JUn91V9wT4LioGO8PRdS9lejH7hQMpa tAsmWY03dQSVOvHkp7scvoOnqqoAgytwaxjbXr9Sgy54xGHRe7Gs11n0Lt9FZMUHtGQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382530; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=fLRkF2NUhWcR4dYn5rN8cnuqTsbjED0H7lMO0c85JEg=; b=Ru6wQKOpNVK8QqWWiY2b6DbZ7Q9nbgauZGvCtQCtgfH5Xr4xic5qFCralF8uXx7BNKO1 fQViFvWhv1ZiWjaBKgNRbfTRn6lpVH0u1B6biW5cNgAaSoqh6H5PaZdFxV/hT6edqkMUH 9EJ6dmI579jQEti8Oix8oAjXWsmORYdNJOljB4k9fQdI/mLQhCXTCqlA95VGhHulpY25P bwSck65W7MsNaZRLzj8cuGPSgJGZhaho4lyDrDC6epGhmV7P5aFHQLGEX4wOrT5DymPcz Rvjz/pFt7YuO7FD0+bm9WwPIn8Xru+b4InHAjVTjPwPRtbUfnjE150/frZi60XTJzLA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=BII8PJdi; 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 C4108606A322; Fri, 4 Dec 2026 11:08:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=BII8PJdi Received: by smtp.kernel.org (Postfix) with ESMTPSA id 317B51F000C2; Fri, 4 Dec 2026 11:08:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796382530; bh=fLRkF2NUhWcR4dYn5rN8cnuqTsbjED0H7lMO0c85JEg=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=BII8PJdizpumjHu+LczuKDUTaak27qgHREATrZnO6qAz095xM1f3/kGiqhC7zBKTu 4E5w3h/RitHqm/Tr/YHGQgR8ibywKCjBO9RYqiAZk4mrKVgfthkRtX5C3TpysM6/kP l44QI18Tyq9iHjvXHuX5Hrj3Pzd/2EhMhzg5grzs= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Chris Mason , Baolin Wang , Lokesh Gidra , "David Hildenbrand (Arm)" Subject: [PATCH 6.18 492/703] mm/userfaultfd: fix PTE table overruns in UFFDIO_MOVE Date: Fri, 4 Dec 2026 19:06:31 +0900 Message-ID: <20261204100640.890207289@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100627.522266145@linuxfoundation.org> References: <20261204100627.522266145@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore 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 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: David Hildenbrand (Arm) commit 169169e26ab498e4a3ee07313e986176f4bc52f0 upstream. With a folded PMD, pmd_addr_end() simply returns the supplied end. move_present_ptes() can therefore walk past the source or destination PTE table during UFFDIO_MOVE. pmd_addr_end() is only intended for page table traversal, in patterns like "next = pmd_addr_end(addr, end);". With folded PMDs, a higher level has already clamped the range to the PTE table. Folded PMDs are uncommon, but can occur even on arm64, for example with 64K pages and a 42-bit virtual address space. Extract the calculation from folio_pte_range() into pte_table_clamped_end() and use it to clamp both ranges. This preserves the wraparound handling from commit f681d4580d5c ("mm: fix address wraparound when scanning large folio PTEs"). Fixes: 50944692052b ("userfaultfd: opportunistic TLB-flush batching for present pages in MOVE") Cc: stable@vger.kernel.org Reported-by: Chris Mason Reviewed-by: Baolin Wang Link: https://patch.msgid.link/20261111-pte_table_clamped_end-v1-1-2dbe339c487a@kernel.org Reviewed-by: Lokesh Gidra Signed-off-by: David Hildenbrand (Arm) Signed-off-by: Greg Kroah-Hartman --- include/linux/pgtable.h | 24 ++++++++++++++++++++++++ mm/memory.c | 7 +------ mm/userfaultfd.c | 4 ++-- 3 files changed, 27 insertions(+), 8 deletions(-) --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -12,6 +12,7 @@ #ifdef CONFIG_MMU #include +#include #include #include #include @@ -1264,6 +1265,29 @@ static inline void arch_swap_restore(swp }) #endif +/** + * pte_table_clamped_end() - Limit a range to a single PTE table. + * @addr: Start address. + * @end: Exclusive end address. + * + * Only use pmd_addr_end() when traversing page tables, in a pattern like:: + * + * next = pmd_addr_end(addr, end); + * + * Higher levels already bound the range when PMDs are folded. Use this + * helper elsewhere to clamp the range regardless of PMD folding. + * + * Return: The exclusive end of the PTE table containing @addr, capped at @end. + */ +static inline unsigned long pte_table_clamped_end(unsigned long addr, + unsigned long end) +{ + const unsigned long pmd_end = (addr & PMD_MASK) + PMD_SIZE; + + /* Compare inclusive ends to avoid wraparounds. */ + return min(pmd_end - 1, end - 1) + 1; +} + /* * When walking page tables, we usually want to skip any p?d_none entries; * and any p?d_bad entries - reporting the error before resetting to none. --- a/mm/memory.c +++ b/mm/memory.c @@ -4613,13 +4613,8 @@ static void folio_pte_range(const struct { /* Stay within the page table and VMA boundaries. */ const unsigned long pmd_start = addr & PMD_MASK; - const unsigned long pmd_end = pmd_start + PMD_SIZE; const unsigned long lower = max(pmd_start, vma->vm_start); - /* - * We cannot use pmd_addr_end(), as it does not clamp reliably. To - * avoid wraparounds, compute using the inclusive end. - */ - const unsigned long upper = min(pmd_end - 1, vma->vm_end - 1) + 1; + const unsigned long upper = pte_table_clamped_end(addr, vma->vm_end); const unsigned long offset = idx << PAGE_SHIFT; /* --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -1053,8 +1053,8 @@ static long move_present_ptes(struct mm_ unsigned long src_start = src_addr; unsigned long src_end; - len = pmd_addr_end(dst_addr, dst_addr + len) - dst_addr; - src_end = pmd_addr_end(src_addr, src_addr + len); + len = pte_table_clamped_end(dst_addr, dst_addr + len) - dst_addr; + src_end = pte_table_clamped_end(src_addr, src_addr + len); flush_cache_range(src_vma, src_addr, src_end); double_pt_lock(dst_ptl, src_ptl);