From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CB321CC6B0F for ; Tue, 13 Oct 2026 14:11:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B34A16B008C; Tue, 13 Oct 2026 10:11:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AE5326B0092; Tue, 13 Oct 2026 10:11:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9D3436B0095; Tue, 13 Oct 2026 10:11:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 710D06B008C for ; Tue, 13 Oct 2026 10:11:24 -0400 (EDT) Received: from smtpin16.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay08.hostedemail.com (Postfix) with ESMTP id F41DE140BC8 for ; Tue, 13 Oct 2026 14:11:23 +0000 (UTC) X-FDA: 85317790446.16.9BE7396 Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf13.hostedemail.com (Postfix) with ESMTP id 48F0C2000C for ; Tue, 13 Oct 2026 14:11:22 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=WTTmcHwc; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf13.hostedemail.com: domain of ljs@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=ljs@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1791900682; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=PafVWtiiBVG/CnUNItxta5c64k8qSC/5mZ/lxcbSfuw=; b=xTiguDdS/O++60yWBc8NCiRiYahfY3gu7b7nna/sv/8mPZ6WdDymj2yQIeeJpuJpbWhKP7 6H/w9FD/dXAg4KYxSb+OtzZc+LQDcAMsRLoKGggNAYnalD2llK5NFfr3MeFAkbRnESITf8 8RJmLzA8irWBvkZsmoxonV4L0t9yyvM= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=WTTmcHwc; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf13.hostedemail.com: domain of ljs@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=ljs@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1791900682; b=PWf44cBKtcHXQoVe+ZVAKDIFAinx8zKDqYWtrNppTTPucMuZFdmNMxOmHLU0sYWBimX2Zz lEKChWj5d+QIGwTVxdA00qqbXTc+Qi44x0uovaobJ7QZZXx5rLxSfcI12Tx4utJxoVpCXk e/ZFBhyVZIbSJaBgU3ivgbuvjliTraM= Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 6D20943268; Tue, 13 Oct 2026 14:11:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 667CB1F000FF; Tue, 13 Oct 2026 14:11:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1791900681; bh=PafVWtiiBVG/CnUNItxta5c64k8qSC/5mZ/lxcbSfuw=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=WTTmcHwcZrWC6wIHZ4MKlhp2UbxVVfnyQhygIA7pwCuO27EWQQwjjy5Pr/G5PjLRS kgu/qr2UpU9jczKBTuspnklruSAY//9u1nNJhv6wDTzTLlaQiSxAxN4l/7OUtXVSsS utiaQToztMh3L+geyoayp0jZRBM8D+SMSgRuNA6V/DiZj52mLpNYo+qqKM+1qOhCJr ZsJYlUB39tnfy1QnhHMpH8iylJjTqh254YQk5QEC7K3wgtChYqUtFbdGRoChr4Qb4I Fq1tIiyCQ2UCxNRQSqHQ8V423ClS3wK9y4vLE2uPuMGFVKEsPPtakP49z36XcjbkOy ngOWAENHP5YmQ== Date: Tue, 13 Oct 2026 15:11:13 +0100 From: "Lorenzo Stoakes (ARM)" To: "David Hildenbrand (Arm)" Cc: Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Ying Huang , Baolin Wang , Barry Song , Ryan Roberts , Chuanhua Han , Kiryl Shutsemau , Kyle Zeng , linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH] mm: fix address wraparound when scanning large folio PTEs Message-ID: References: <20261013-folio_pte_scan_range-v1-1-c50dc30bf6ad@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261013-folio_pte_scan_range-v1-1-c50dc30bf6ad@kernel.org> X-Stat-Signature: t97maa54ffm5qkfok1bce6rr561z5gik X-Rspam-User: X-Rspamd-Queue-Id: 48F0C2000C X-Rspamd-Server: rspam08 X-HE-Tag: 1791900682-741565 X-HE-Meta: U2FsdGVkX1/RnNNzFP0BgOfXC9n0Quy5JgrlgaIwToDQ77J7+Cw3CSKV/C0pYQqLn+UkkrnS2E9NPae0bsC8xA/CJ5sax5Otj5OtKJl/hAryVxxOZZRR4Ry5retcsdjvonj74iAtzveHNF7sAJrwFyBvpVU7l2hATXIZTl/fRQhvuuWdtalCTukgqy22SOy0M5ETL7wx8R1f8qGvRnO5JbK3RlQ6AN6Al5JveAQexFEbI6+IA2ctNAfmZukzcIzm6HIVLGTdsjbt8TUydwX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GjV0zL5dw== Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Tue, Oct 13, 2026 at 02:30:37PM +0200, David Hildenbrand (Arm) wrote: > numa_rebuild_large_mapping() and do_swap_page() can be tricked into > generating an address wraparound, crashing the kernel. All that's required > is to map a tail page of a large folio close enough to the start/end of > the address space to e.g., make > > unsigned long addr_start = addr - (nr << PAGE_SHIFT); > > to wrap around. Yeah damn. I feel like we need to generalise the underflow/overflow checks we have arbitrarily around the place into helper functions. I know somebody schlopped something like that for one function in particular recently, but a non-schlopped, generalised version of that would be nice. > > Let's fix it by providing a better helper, folio_pte_scan_range(), that > takes care of staying within VMA and page table boundaries while > handling possible wraparounds. OK so this is effectively abstracting this very thing :) though I guess perhaps not quite a match for the general 'did we over/underflow stuff with the addr, size pair we just got'. Anyway that's something to think about separately I guess. > > Reported-by: Kyle Zeng > Fixes: d2136d749d76 ("mm: support multi-size THP numa balancing") > Fixes: 508758960b8d ("mm: swap: entirely map large folios found in swapcache") > Cc: stable@vger.kernel.org > Signed-off-by: David Hildenbrand (Arm) > --- > mm/memory.c | 43 +++++++++++++++++++++++++++++-------------- > 1 file changed, 29 insertions(+), 14 deletions(-) > > diff --git a/mm/memory.c b/mm/memory.c > index 1f5d5f7d39cd2..a3823010aa981 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -4933,6 +4933,27 @@ static void check_swap_exclusive(struct folio *folio, swp_entry_t entry, > } while (--nr_pages); > } > > +/* > + * Get the address range in which a folio might be mapped, assuming its pages > + * are mapped consecutively and the folio page at the given idx is mapped at > + * the given address. > + */ I know it's a static function but kdoc here might be nice given the number of params, including 2 output parameters. Also then can specify exclusive end, etc. > +static void folio_pte_scan_range(const struct folio *folio, > + const struct vm_area_struct *vma, unsigned long addr, > + unsigned long idx, unsigned long *start, unsigned long *end) I don't love the name - to me it implies to me that you're scanning the range. Maybe clamp_folio_pte_range()? I guess though clamp implies it limits an input range. Maybe get_folio_pte_range()? Also I think it might be useful to have it return the number of pages contained within the range? Then this below: + folio_pte_scan_range(folio, vma, address, idx, &folio_start, + &folio_end); + if (unlikely((folio_end - folio_start) >> PAGE_SHIFT != nr)) Could be: + if (unlikely(get_folio_pte_range(folio, vma, address, idx, + &folio_start, &folio_end)) != nr) OTOH, that's pretty ugly... :)) > +{ > + /* 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; OK so get the range of the PTE page table. > + const unsigned long lower = max(pmd_start, vma->vm_start); Then clamp to VMA start... > + const unsigned long upper = min(pmd_end - 1, vma->vm_end - 1) + 1; This is trickier :) I guess you're doing this in case either bound is right at wraparound somehow? But then couldn't the +1 push you over the edge? Oh wait no, because then vma->vm_end would have to equal 0, which would fail the other-place enforced invariant that vma->vm_start < vma->vm_end. So it's pmd_end that could overflow, hence the -1, +1 dance. Yeah I kind of hate that, that's a lot of 'just have to know' right there. Actually we have pmd_addr_end() already, could we use that? #define pmd_addr_end(addr, end) \ ({ unsigned long __boundary = ((addr) + PMD_SIZE) & PMD_MASK; \ (__boundary - 1 < (end) - 1)? __boundary: (end); \ }) That embeds the trick in the helper and it expresses what you're getting. Then it could be: const unsigned long pmd_start = addr & PMD_MASK; const unsigned long lower = max(pmd_start, vma->vm_start); const unsigned long upper = pmd_addr_end(addr, vma->vm_end); > + const unsigned long offset = idx << PAGE_SHIFT; > + > + /* Avoid any address wraparounds. */ > + *start = addr - min(addr - lower, offset); This is also tricky. This is clamping to max(lower, addr - offset) right, but being very careful about underflow? I don't love the addr - (addr - lower) = lower here, and it feels like it embeds a comparison there too. It's all very implicit. Actually maybe just naming addr - lower gets us there: const unsigned long max_offset = addr - lower; ... *start = addr - min(max_offset, offset); There's check_sub_overflow() or you could use a ternary also but both feel like they add more complexity than is useful. > + *end = addr + min(upper - addr, folio_size(folio) - offset); Same deal here. Maybe something like: const unsigned long max_remaining_bytes = upper - addr; ... *end = addr + min(max_remaining_bytes, folio_size(folio) - offset); > +} > + > /* > * We enter with either the VMA lock or the mmap_lock held (see > * FAULT_FLAG_VMA_LOCK), and pte mapped but not yet locked. > @@ -5121,16 +5142,15 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) > address = vmf->address; > ptep = vmf->pte; > if (folio_test_large(folio) && folio_test_swapcache(folio)) { > - int nr = folio_nr_pages(folio); > - unsigned long idx = folio_page_idx(folio, page); > - unsigned long folio_start = address - idx * PAGE_SIZE; > - unsigned long folio_end = folio_start + nr * PAGE_SIZE; > + const int nr = folio_nr_pages(folio); > + const unsigned long idx = folio_page_idx(folio, page); > + unsigned long folio_start, folio_end; > pte_t *folio_ptep; > pte_t folio_pte; > > - if (unlikely(folio_start < max(address & PMD_MASK, vma->vm_start))) > - goto check_folio; > - if (unlikely(folio_end > pmd_addr_end(address, vma->vm_end))) > + folio_pte_scan_range(folio, vma, address, idx, &folio_start, > + &folio_end); > + if (unlikely((folio_end - folio_start) >> PAGE_SHIFT != nr)) > goto check_folio; > > folio_ptep = vmf->pte - idx; > @@ -6280,16 +6300,11 @@ static void numa_rebuild_large_mapping(struct vm_fault *vmf, struct vm_area_stru > struct folio *folio, pte_t fault_pte, > bool ignore_writable, bool pte_write_upgrade) > { > - int nr = pte_pfn(fault_pte) - folio_pfn(folio); > + const unsigned long idx = pte_pfn(fault_pte) - folio_pfn(folio); > unsigned long start, end, addr = vmf->address; > - unsigned long addr_start = addr - (nr << PAGE_SHIFT); > - unsigned long pt_start = ALIGN_DOWN(addr, PMD_SIZE); > pte_t *start_ptep; > > - /* Stay within the VMA and within the page table. */ > - start = max3(addr_start, pt_start, vma->vm_start); > - end = min3(addr_start + folio_size(folio), pt_start + PMD_SIZE, > - vma->vm_end); > + folio_pte_scan_range(folio, vma, addr, idx, &start, &end); > start_ptep = vmf->pte - ((addr - start) >> PAGE_SHIFT); > > /* Restore all PTEs' mapping of the large folio */ > > --- > > base-commit: 634cbf5b794b734c78631b92e381cfc85f357bbe > > change-id: 20261013-folio_pte_scan_range-c6ca19cc8e9d > > -- > > Cheers, > > David > In general though the logic as a whole looks correct to me, AFAICT. -- Cheers, Lorenzo