From nobody Tue Apr 7 18:51:42 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1C5B32DF707 for ; Fri, 27 Feb 2026 14:35:22 +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=1772202924; cv=none; b=SVMef4ysERKyFGGvMdk4Bn0A8g2Oe0EEd6xK6tfawZRfIJ18WlJwyElp97xci7ZX/nTjEf4mhnZlgVKDHEBbpl12mfpgBroHkl3u5MdP2XovXcI83s72oBnILmkw2UFiNKl6hYlmImq3Fpd+Rkv4XaR6UVx7WIZgEOmcKuZgUTY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772202924; c=relaxed/simple; bh=oBW2N/+rWAeR3secvkTYJC/cNlnwhvpYMP3vqttttJ0=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=uS+KT8fvGl1AzC1grA53dG53BbzpzIoWLZ6aZHhjD1zpSR9QV94ksqBxaIu7U7yqc5OOaaBVefFvDMH2wbng+YeKer2HXUEwyPXvFmQi8wjsbxqGwMJr7/pGvxfQxWPGvR95kLfhsSwy7mlng2waQsB4z0PdK1fIIcRGSIBQXFY= 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 429AE339; Fri, 27 Feb 2026 06:35:16 -0800 (PST) Received: from a080796.blr.arm.com (a080796.arm.com [10.164.21.51]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 982293F62B; Fri, 27 Feb 2026 06:35:18 -0800 (PST) From: Dev Jain To: akpm@linux-foundation.org, david@kernel.org, lorenzo.stoakes@oracle.com Cc: ziy@nvidia.com, baolin.wang@linux.alibaba.com, Liam.Howlett@oracle.com, npache@redhat.com, ryan.roberts@arm.com, dev.jain@arm.com, baohua@kernel.org, lance.yang@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] khugepaged: remove redundant index check for pmd-folios Date: Fri, 27 Feb 2026 20:05:01 +0530 Message-Id: <20260227143501.1488110-1-dev.jain@arm.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Claim: folio_order(folio) =3D=3D HPAGE_PMD_ORDER =3D> folio->index =3D=3D s= tart. Proof: Both loops in hpage_collapse_scan_file and collapse_file, which iterate on the xarray, have the invariant that start <=3D folio->index < start + HPAGE_PMD_NR ... (i) A folio is always naturally aligned in the pagecache, therefore folio_order =3D=3D HPAGE_PMD_ORDER =3D> IS_ALIGNED(folio->index, HPAGE_PMD_= NR) =3D=3D true ... (ii) thp_vma_allowable_order -> thp_vma_suitable_order requires that the virtual offsets in the VMA are aligned to the order, =3D> IS_ALIGNED(start, HPAGE_PMD_NR) =3D=3D true ... (iii) Combining (i), (ii) and (iii), the claim is proven. Therefore, remove this check. While at it, simplify the comments. Signed-off-by: Dev Jain Acked-by: David Hildenbrand (Arm) Reviewed-by: Anshuman Khandual Reviewed-by: Baolin Wang Reviewed-by: Lance Yang Reviewed-by: Lorenzo Stoakes --- v1->v2: - Remove the check instead of converting to VM_WARN_ON - While at it, simplify the comments Based on mm-new (8982358e1c87). mm/khugepaged.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 5f668c1dd0fe4..b7b4680d27ab1 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -2015,9 +2015,7 @@ static enum scan_result collapse_file(struct mm_struc= t *mm, unsigned long addr, * we locked the first folio, then a THP might be there already. * This will be discovered on the first iteration. */ - if (folio_order(folio) =3D=3D HPAGE_PMD_ORDER && - folio->index =3D=3D start) { - /* Maybe PMD-mapped */ + if (folio_order(folio) =3D=3D HPAGE_PMD_ORDER) { result =3D SCAN_PTE_MAPPED_HUGEPAGE; goto out_unlock; } @@ -2345,15 +2343,11 @@ static enum scan_result hpage_collapse_scan_file(st= ruct mm_struct *mm, continue; } =20 - if (folio_order(folio) =3D=3D HPAGE_PMD_ORDER && - folio->index =3D=3D start) { - /* Maybe PMD-mapped */ + if (folio_order(folio) =3D=3D HPAGE_PMD_ORDER) { result =3D SCAN_PTE_MAPPED_HUGEPAGE; /* - * For SCAN_PTE_MAPPED_HUGEPAGE, further processing - * by the caller won't touch the page cache, and so - * it's safe to skip LRU and refcount checks before - * returning. + * PMD-sized THP implies that we can only try + * retracting the PTE table. */ folio_put(folio); break; --=20 2.34.1