From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796188743; b=TU3R2uWb74njRTamsvbcWyc5U9R5hHEMEBSpevVSvV4o6/rWg3C7xHfy3a4o0VtVHIir fwWobJToEpqc7jrNXzcgVo1nT1ac5tPH7Nm9E1dZsUQr/hVc7hCA1pT2Uw5kVm4cAgk/D 9Gocq8P1aDDYjDpINuzOcSQL9MXQ6kIbpmm+s7bzY8EemauztvTHZFN54m+uNY+LdNHY7 7QQYwDR4tAZoo+VEUrampPIdNuwhWdg13hy2wbp2PsYnk8+AjjMn6eoqD/lg4ewfsoMco BB7uHBdqUNup/d0Q2AT7wMWHzFyoEX28xzzYWoFU0zizqVmNbfjnultdP28f6vvq23w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796188743; h=DMARC-Filter:Received:Received:DKIM-Signature:Message-ID:Date: MIME-Version:User-Agent:Subject:To:Cc:References:Content-Language: From:In-Reply-To:Content-Type:Content-Transfer-Encoding; bh=NJQmqdHuUdDhDr0frRGhOW1roy4JICMoYUMvJ3jtmB0=; b=QEOTaXdZNgpstyfInsiTLzNbKucA2eOx3/5voSQaukfIZwZrm/AtsPdUqtUV0/0C7IBX ONcycHNkaY1Fm0gFfcGxy9ZxQmVKI5Ddwxe8UDS2DA3h5BEe9C44l4gX1UE9+MfjMv8VG nkBS6d0aHeI2CFtTGiKvEKwEYRVFrQBFqVLDCDylUJAK+Ir8g14Iwn15M/7LOPn2GJsZP KBBCzx1zWax9WweAM3Opvvsn5+KiXSd2MjzODa3OEL+AWwe/daTZ+s0EWZEzmCGvfIV22 juIOw8iBsp8wmfYsnBOG5PQxnHuYx4RWZZlh6dvHTtHTHIm6SKAowmA8kHOxy+lclAg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=bP6UTyuX; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3C0A2606A322; Wed, 2 Dec 2026 05:19:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3C0A2606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=bP6UTyuX Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 321531682; Tue, 1 Dec 2026 21:18:58 -0800 (PST) Received: from [10.164.148.53] (MacBook-Pro-3.blr.arm.com [10.164.148.53]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id F01723F632; Tue, 1 Dec 2026 21:18:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1796188741; bh=ZnnpoR5Ggt1GFRvs2Q6LMkFAidHEex8WkhFDMrHDdGw=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=bP6UTyuX3DRJ7rsWzmqM5gN7jbsZlfuDYtIO6+v9yVBB3S8CUHufs2WrVYAGFC+6k 1kAOug1ZYhCOc4iTEwNkE4iYLz1j0+8MuFobG5f6QDg+bpeL8UgleECL4Zf+2K40Of Lgs+lQEfk1aGP/Px/MvY4ZWkpD33WEipDZ4vU2L4= Message-ID: Date: Wed, 2 Dec 2026 10:48:34 +0530 X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 07/32] mm/khugepaged: check the VMA before walking page tables when collapsing To: "David Hildenbrand (Arm)" , Hugh Dickins , Baolin Wang , Andrew Morton , Mike Rapoport , Peter Xu , Michal Hocko , Kiryl Shutsemau , Manfred Spraul , Davidlohr Bueso , Cyrill Gorcunov , Zi Yan , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Barry Song , Lance Yang , Usama Arif , Lorenzo Stoakes , Rik van Riel , Vlastimil Babka , Harry Yoo , Jann Horn , Yang Shi , Zach O'Keefe , Song Liu , David Stevens , Kefeng Wang , Alex Williamson , Suren Baghdasaryan , Dave Jiang , Matthew Wilcox , Miaohe Lin , David Rientjes , Shakeel Butt , Jan Kara , Mel Gorman , Jason Gunthorpe , John Hubbard , Gerd Hoffmann , Vivek Kasireddy , Dave Airlie , Matthew Brost , Joshua Hahn , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Hagen Paul Pfeifer , James Bottomley , Felix Kuehling , Alex Sierra , Balbir Singh , Kairui Song , Qi Zheng , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baoquan He , Oscar Salvador , Muchun Song , Dan Williams , Rakie Kim , Hao Li , Christoph Lameter , Roman Gushchin , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt , Alexei Starovoitov , Xu Xin , Chengming Zhou , Shivank Garg Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-cxl@vger.kernel.org, linux-rt-devel@lists.linux.dev, SJ Park , stable@vger.kernel.org, Chris Mason References: <20261129-fixes_core_3-v1-0-f66094116801@kernel.org> <20261129-fixes_core_3-v1-7-f66094116801@kernel.org> Content-Language: en-US From: Dev Jain In-Reply-To: <20261129-fixes_core_3-v1-7-f66094116801@kernel.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 29/11/26 3:36 pm, David Hildenbrand (Arm) wrote: > MADV_COLLAPSE and khugepaged can end up traversing page tables of a VMA > through find_pmd_or_thp_or_none() before verifying that the VMA is still > as expected. > > Calling find_pmd_or_thp_or_none() on a mapping with PUDs > (hugetlb/dax/PFNMAP) is invalid, as it treats every present PUD entry as > a pointer to a PMD table. > > Consequently, we can end up interpreting the memory mapped by a PUD leaf > as a PMD table. > > Let's fix it by performing the thp_vma_allowable_order() check before > the page table walk. > > Further, let's also make find_pmd_or_thp_or_none() harder to get wrong > by rejecting any pgd/p4d/pud leaves. > > This has high urgency, as unprivileged users can trigger it, although > getting relevant PUD mappings (e.g., hugetlb) should be rather uncommon > in most setups. > > Fixes: 58ac9a8993a1 ("mm/khugepaged: attempt to map file/shmem-backed pte-mapped THPs by pmds") > Cc: stable@vger.kernel.org > Reported-by: Chris Mason > Signed-off-by: David Hildenbrand (Arm) > --- So for retract_page_tables(), when we use the struct address_space to traverse the VMAs, we cannot get a hugetlb or DAX VMA. But for the case described here, vma_lookup() can perhaps get a completely new VMA. Reviewed-by: Dev Jain > mm/khugepaged.c | 10 +++++----- > mm/rmap.c | 6 +++--- > 2 files changed, 8 insertions(+), 8 deletions(-) > > diff --git a/mm/khugepaged.c b/mm/khugepaged.c > index 93d4a6f159e81..b133c378dac72 100644 > --- a/mm/khugepaged.c > +++ b/mm/khugepaged.c > @@ -1894,11 +1894,6 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign > !range_in_vma(vma, haddr, haddr + HPAGE_PMD_SIZE)) > return SCAN_VMA_CHECK; > > - /* Fast check before locking page if already PMD-mapped */ > - result = find_pmd_or_thp_or_none(mm, haddr, &pmd); > - if (result == SCAN_PMD_MAPPED) > - return result; > - > /* > * If we are here, we've succeeded in replacing all the native pages > * in the page cache with a single hugepage. If a mm were to fault-in > @@ -1909,6 +1904,11 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign > if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_FORCED_COLLAPSE, PMD_ORDER)) > return SCAN_VMA_CHECK; > > + /* Quick check if already PMD-mapped before locking the folio. */ > + result = find_pmd_or_thp_or_none(mm, haddr, &pmd); > + if (result == SCAN_PMD_MAPPED) > + return result; > + > /* > * Keep pmd pgtable while the uffd bit is in use; see comment in > * retract_page_tables(). > diff --git a/mm/rmap.c b/mm/rmap.c > index 81a97b94ca4b9..e7e2a0df1780b 100644 > --- a/mm/rmap.c > +++ b/mm/rmap.c > @@ -883,15 +883,15 @@ pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address) > pmd_t *pmd = NULL; > > pgd = pgd_offset(mm, address); > - if (!pgd_present(*pgd)) > + if (!pgd_present(*pgd) || pgd_leaf(*pgd)) > goto out; > > p4d = p4d_offset(pgd, address); > - if (!p4d_present(*p4d)) > + if (!p4d_present(*p4d) || p4d_leaf(*p4d)) > goto out; > > pud = pud_offset(p4d, address); > - if (!pud_present(*pud)) > + if (!pud_present(*pud) || pud_leaf(*pud)) > goto out; > > pmd = pmd_offset(pud, address); >