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=1796381081; b=P1nVGIvPljNwpmPvC0sbMvIHjAGSfXU5+9WCfRCKtrVqE+/SkNDHXqUKUTwR5aly8eLC /jknyIAqBKwr7i7tc3G4t3DkWcCiEzKdBBfzq5t0rBnycRN2FAt8BmtaXyTrVzj0ELF+v Rv/FLGMhORGIXigU7r8Tu4xOUbmeC1fN9sgZUTWGpE5k6SdriB/zW44wDvXPr6LEHPMTB sIXqAwycdJfgE1RHwlMUSOse54i/TA3biAk+fOlo2oI6PLUa/wg9RUC/OjVGw1BkQf1Xi qzJkXyVfIvsAaUwzSUzidczVt8ly8qzT5reiBynKhv/uV23VbdgZSO2S08xbbCniY+Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796381081; 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=GYWzqCZb2PD11oAwBv+XPKcADJ5erFDQdIiE4290OEE=; b=w4C4PpiuFCZpcHV1j7+aAqdoEDcQKFbOdCuxbdPjJZ6sSWEKj4zUvvArM1kHobA8e+OX e1mCoBAZPXBhb7fmcik+uN42V01V9DikfGmeHueWyjGO6R6Cmzu7UDUH2zQhBpnPay+z3 6URu9+CO8q6CqczyisbA8/jXHPdaKemTnAcxdQIh/uuPCWdxyNamfc0qggrB0rQoOx2GZ WZ6iluvTXPq+8nT7nccO0dJv8YSOB8BDQ0n+ySWqlbT24+ncElUSqYrcMpB3WIU+TIJnr 9FBYwId7bkxieWHsLPKITq2b8NI5oIB3Z7kIi3eoTHBWsZaz+TN+cTFuf/Apxu2RvNA== 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=CKmIAcDO; 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 36D01606A322; Fri, 4 Dec 2026 10:44:41 +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=CKmIAcDO Received: by smtp.kernel.org (Postfix) with ESMTPSA id 989AB1F000CE; Fri, 4 Dec 2026 10:44:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796381081; bh=GYWzqCZb2PD11oAwBv+XPKcADJ5erFDQdIiE4290OEE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=CKmIAcDOt35ib8MI2YIoVSvrDDEoRlT0WOl/yjRqIJHoucd3jRW55CkbEEOnJX5wD 8yX/aMay5t1YXhsLl1KjlsUWoNUzxJ0jmBe6RlRw4Ub+ms1n7SlOyhHeKbI4m2IvCX +TnhaHI+Z/GSINu2L8t9IWtQhduACdmn+tH1rPVM= 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 7.3 550/742] mm/userfaultfd: fix PTE table overruns in UFFDIO_MOVE Date: Fri, 4 Dec 2026 19:06:54 +0900 Message-ID: <20261204100649.767226610@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 7.3-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 @@ -1518,6 +1519,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 @@ -4896,13 +4896,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 @@ -1293,8 +1293,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);