From nobody Mon Feb 9 12:23:22 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8E3E834DCEE for ; Wed, 22 Oct 2025 18:39:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761158371; cv=none; b=WyvsKYJUNwKZy6uWhbUrpg1Slm2p8m4kTspYoud4xR0rE6W5WrATwGfRjvsbNK2JSw+j8AAEyoBf1f/cKUW+zviJfvi5SvK1xYo4BuYqIN4HpNRWIo/N5Z2IcK8+3t1GHmc+tLIcGlOO3kCNgTNtJ6p1pzeeMXoSmq6N1AZHpME= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761158371; c=relaxed/simple; bh=udc3lQ5qAqYENhuSsmZKCiBf6b+fPZg0MkdxeumsA+I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lsapjHyP8yJT8E8vUeJp5x7feyoiPXJlkRLou0+/xbztq1t3rV+RGiUxMc1txzdBmZ+vrFZXVLVDmwMygN0MDKcousodYudE1Vh7FeFxdQQo1HTYkXfVDYgUvIXUjoXRmrUkFSdqmrh5ASX22B/whmLdGIHq0izMdRkwBfvQrCc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=B+lUIrpe; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="B+lUIrpe" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1761158368; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zERItD4tpTSTLZu1EAujIdQUQv57c0MEV45Tz1yZd+8=; b=B+lUIrpelvqWAeV2UnnGVmmU5x4i4+sQBaailrZ6zjYpCpCNb+SuSLZmb9czOqYJq07qwf iNciu2lt31a7NDxNqaIlCiHiMgPNvFxixEbwRoquogW4VL+ACG5K5F4DSWE/irCJgTrym6 NhP0Yhkf8vB8z4YQ+Ic46gSpfPAOFe8= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-182-CNNjUI14NuKWDUrtXV7l9w-1; Wed, 22 Oct 2025 14:39:25 -0400 X-MC-Unique: CNNjUI14NuKWDUrtXV7l9w-1 X-Mimecast-MFC-AGG-ID: CNNjUI14NuKWDUrtXV7l9w_1761158360 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B9AF718002C9; Wed, 22 Oct 2025 18:39:19 +0000 (UTC) Received: from h1.redhat.com (unknown [10.22.64.41]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 04CFA19560B4; Wed, 22 Oct 2025 18:39:09 +0000 (UTC) From: Nico Pache To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-mm@kvack.org, linux-doc@vger.kernel.org Cc: david@redhat.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, lorenzo.stoakes@oracle.com, Liam.Howlett@oracle.com, ryan.roberts@arm.com, dev.jain@arm.com, corbet@lwn.net, rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, akpm@linux-foundation.org, baohua@kernel.org, willy@infradead.org, peterx@redhat.com, wangkefeng.wang@huawei.com, usamaarif642@gmail.com, sunnanyong@huawei.com, vishal.moola@gmail.com, thomas.hellstrom@linux.intel.com, yang@os.amperecomputing.com, kas@kernel.org, aarcange@redhat.com, raquini@redhat.com, anshuman.khandual@arm.com, catalin.marinas@arm.com, tiwai@suse.de, will@kernel.org, dave.hansen@linux.intel.com, jack@suse.cz, cl@gentwo.org, jglisse@google.com, surenb@google.com, zokeefe@google.com, hannes@cmpxchg.org, rientjes@google.com, mhocko@suse.com, rdunlap@infradead.org, hughd@google.com, richard.weiyang@gmail.com, lance.yang@linux.dev, vbabka@suse.cz, rppt@kernel.org, jannh@google.com, pfalcato@suse.de Subject: [PATCH v12 mm-new 06/15] khugepaged: introduce collapse_max_ptes_none helper function Date: Wed, 22 Oct 2025 12:37:08 -0600 Message-ID: <20251022183717.70829-7-npache@redhat.com> In-Reply-To: <20251022183717.70829-1-npache@redhat.com> References: <20251022183717.70829-1-npache@redhat.com> 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 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Type: text/plain; charset="utf-8" The current mechanism for determining mTHP collapse scales the khugepaged_max_ptes_none value based on the target order. This introduces an undesirable feedback loop, or "creep", when max_ptes_none is set to a value greater than HPAGE_PMD_NR / 2. With this configuration, a successful collapse to order N will populate enough pages to satisfy the collapse condition on order N+1 on the next scan. This leads to unnecessary work and memory churn. To fix this issue introduce a helper function that caps the max_ptes_none to HPAGE_PMD_NR / 2 - 1 (255 on 4k page size). The function also scales the max_ptes_none number by the (PMD_ORDER - target collapse order). The limits can be ignored by passing full_scan=3Dtrue, this is useful for madvise_collapse (which ignores limits), or in the case of collapse_scan_pmd(), allows the full PMD to be scanned when mTHP collapse is available. Signed-off-by: Nico Pache --- mm/khugepaged.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 4ccebf5dda97..286c3a7afdee 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -459,6 +459,39 @@ void __khugepaged_enter(struct mm_struct *mm) wake_up_interruptible(&khugepaged_wait); } =20 +/** + * collapse_max_ptes_none - Calculate maximum allowed empty PTEs for colla= pse + * @order: The folio order being collapsed to + * @full_scan: Whether this is a full scan (ignore limits) + * + * For madvise-triggered collapses (full_scan=3Dtrue), all limits are bypa= ssed + * and allow up to HPAGE_PMD_NR - 1 empty PTEs. + * + * For PMD-sized collapses (order =3D=3D HPAGE_PMD_ORDER), use the configu= red + * khugepaged_max_ptes_none value. + * + * For mTHP collapses, scale down the max_ptes_none proportionally to the = folio + * order, but caps it at HPAGE_PMD_NR/2-1 to prevent a collapse feedback l= oop. + * + * Return: Maximum number of empty PTEs allowed for the collapse operation + */ +static unsigned int collapse_max_ptes_none(unsigned int order, bool full_s= can) +{ + unsigned int max_ptes_none; + + /* ignore max_ptes_none limits */ + if (full_scan) + return HPAGE_PMD_NR - 1; + + if (order =3D=3D HPAGE_PMD_ORDER) + return khugepaged_max_ptes_none; + + max_ptes_none =3D min(khugepaged_max_ptes_none, HPAGE_PMD_NR/2 - 1); + + return max_ptes_none >> (HPAGE_PMD_ORDER - order); + +} + void khugepaged_enter_vma(struct vm_area_struct *vma, vm_flags_t vm_flags) { @@ -546,7 +579,7 @@ static int __collapse_huge_page_isolate(struct vm_area_= struct *vma, pte_t *_pte; int none_or_zero =3D 0, shared =3D 0, result =3D SCAN_FAIL, referenced = =3D 0; const unsigned long nr_pages =3D 1UL << order; - int max_ptes_none =3D khugepaged_max_ptes_none >> (HPAGE_PMD_ORDER - orde= r); + int max_ptes_none =3D collapse_max_ptes_none(order, !cc->is_khugepaged); =20 for (_pte =3D pte; _pte < pte + nr_pages; _pte++, addr +=3D PAGE_SIZE) { --=20 2.51.0