From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5EB1418FDDB for ; Thu, 16 Oct 2025 05:35:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760592905; cv=none; b=mjXt4hA0DQkh3SZJT6lESuM4BCPg9bcZ7bPA7XQIgRYo8VQVtXQl0EBo4kV+83QoLf/wQmwuuZCE2HQJ27uxIFiWxebZ3CIuqNzqfmUTZ98ik7XRQQT+BPhkaw7UTTMnYnMhLfslMiA4DtIZKICnF9kX6cIMj5vlP89nRlNT9mA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760592905; c=relaxed/simple; bh=48Z5KAUMPHa/5lWu4xCW/al6Y78KkmQvsayIbMH4BJE=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=aThsx7DkSrlwxti4JKcIuOBnl4S/8wEwR26y2d2220MYV59eM6N7puzHsLVqgcTCldWoW7KFBmY2VgL0FES8PpQQC/zo++Y5OYv6sUxbE0K4YFs/CLPMqPzG45IAcprd+Li6pJsehX6nV1o3Ym+ovF3LHv+5xfPsmIsQHqAtgPw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 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 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 65C441688; Wed, 15 Oct 2025 22:34:53 -0700 (PDT) Received: from [10.163.68.150] (unknown [10.163.68.150]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 9D6D23F6A8; Wed, 15 Oct 2025 22:34:56 -0700 (PDT) Message-ID: Date: Thu, 16 Oct 2025 11:04:52 +0530 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH mm-new 1/1] mm/khugepaged: guard is_zero_pfn() calls with pte_present() To: Lance Yang , akpm@linux-foundation.org, david@redhat.com, lorenzo.stoakes@oracle.com Cc: ziy@nvidia.com, baolin.wang@linux.alibaba.com, Liam.Howlett@oracle.com, npache@redhat.com, ryan.roberts@arm.com, baohua@kernel.org, ioworker0@gmail.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org References: <20251016033643.10848-1-lance.yang@linux.dev> Content-Language: en-US From: Dev Jain In-Reply-To: <20251016033643.10848-1-lance.yang@linux.dev> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 16/10/25 9:06 am, Lance Yang wrote: > From: Lance Yang > > A non-present entry, like a swap PTE, contains completely different data > (swap type and offset). pte_pfn() doesn't know this, so if we feed it a > non-present entry, it will spit out a junk PFN. > > What if that junk PFN happens to match the zeropage's PFN by sheer > chance? While really unlikely, this would be really bad if it did. > > So, let's fix this potential bug by ensuring all calls to is_zero_pfn() > in khugepaged.c are properly guarded by a pte_present() check. > > Suggested-by: Lorenzo Stoakes > Signed-off-by: Lance Yang > --- Thanks, I missed this. > mm/khugepaged.c | 13 ++++++++----- > 1 file changed, 8 insertions(+), 5 deletions(-) > > diff --git a/mm/khugepaged.c b/mm/khugepaged.c > index d635d821f611..0341c3d13e9e 100644 > --- a/mm/khugepaged.c > +++ b/mm/khugepaged.c > @@ -516,7 +516,7 @@ static void release_pte_pages(pte_t *pte, pte_t *_pte, > pte_t pteval = ptep_get(_pte); > unsigned long pfn; > > - if (pte_none(pteval)) > + if (!pte_present(pteval)) There should be no chance that we end up with a pteval which is not none *and* not present, if you look at the callers of release_pte_pages. So perhaps we should keep this either the same, or, after "if(pte_none(pteval))", do a WARN_ON_ONCE(!pte_present(pteval))? > continue; > pfn = pte_pfn(pteval); > if (is_zero_pfn(pfn)) > @@ -690,9 +690,10 @@ static void __collapse_huge_page_copy_succeeded(pte_t *pte, > address += nr_ptes * PAGE_SIZE) { > nr_ptes = 1; > pteval = ptep_get(_pte); > - if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) { > + if (pte_none(pteval) || > + (pte_present(pteval) && is_zero_pfn(pte_pfn(pteval)))) { > add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1); > - if (is_zero_pfn(pte_pfn(pteval))) { > + if (!pte_none(pteval)) { > /* > * ptl mostly unnecessary. > */ > @@ -794,7 +795,8 @@ static int __collapse_huge_page_copy(pte_t *pte, struct folio *folio, > unsigned long src_addr = address + i * PAGE_SIZE; > struct page *src_page; > > - if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) { > + if (pte_none(pteval) || > + (pte_present(pteval) && is_zero_pfn(pte_pfn(pteval)))) { > clear_user_highpage(page, src_addr); > continue; > } > @@ -1294,7 +1296,8 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm, > goto out_unmap; > } > } > - if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) { > + if (pte_none(pteval) || > + (pte_present(pteval) && is_zero_pfn(pte_pfn(pteval)))) { > ++none_or_zero; > if (!userfaultfd_armed(vma) && > (!cc->is_khugepaged ||