From nobody Mon Dec 1 21:31:19 2025 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 A0E69338934 for ; Mon, 1 Dec 2025 17:48:44 +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=1764611326; cv=none; b=LKIWUTNX9GXMLzCsxOd102F0NDPUrn8Xl3+xc5Qpp0sk0v2CZVR/Vq1YbJiqBqeufF7JtefLZ+CGEwomYWZ4UHRbPm1YbCXvYMiFlA0fyIUZEtOU8cgs6l/eExurSV9MbaW8ojVh94bvsIX4/4EHJepdqqfsycEBaJ5Y9f9Er/A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764611326; c=relaxed/simple; bh=OcfwoB6Kj8JJUHkEuJL+57c0usvPfc1xOqYjyTiK7Zo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uetdJok8/Kh1I7yghrUJlWRDrP7l0F9FFlanGo8xEdjqqAzGb2Ede0o2jHNXcWMuHeeyXW/zejlxiSjcNYd7abDTiR7/oy+olxoUmR0gPaV+VOCjPDFczMB/ZEPuwloUhVSsOQ1Y2zemJHTRosocnavXrW3dnvP/DGc0XLEYoGI= 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=SWssd26z; 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="SWssd26z" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1764611323; 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=aApNpV/y96GsuiGhXxscelXEjRrHIOopNsyX6f8svSs=; b=SWssd26zMUsX+eWF+hAJu4G3eZD5VIXa+Q18Qq5RamjiwMArUYTlYkWWLJ6HZ9/Ine1xOt 9qJj7ahADmBUuEcvphRB3hfEvGxZdxU0bERULtD3ofxOyHEwfVEKinaOwiVO4ncF6LD4Ro BVbQKaHAQ2uJMT/SwuCIKk3DzINk33A= Received: from mx-prod-mc-06.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-658-Bpno6CU_M2GZO9HDt3xq0Q-1; Mon, 01 Dec 2025 12:48:40 -0500 X-MC-Unique: Bpno6CU_M2GZO9HDt3xq0Q-1 X-Mimecast-MFC-AGG-ID: Bpno6CU_M2GZO9HDt3xq0Q_1764611315 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 71C23180057A; Mon, 1 Dec 2025 17:48:35 +0000 (UTC) Received: from h1.redhat.com (unknown [10.22.66.60]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 6E9A8180087B; Mon, 1 Dec 2025 17:48:25 +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 v13 mm-new 07/16] khugepaged: introduce collapse_max_ptes_none helper function Date: Mon, 1 Dec 2025 10:46:18 -0700 Message-ID: <20251201174627.23295-8-npache@redhat.com> In-Reply-To: <20251201174627.23295-1-npache@redhat.com> References: <20251201174627.23295-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.4.1 on 10.30.177.111 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 will limit mTHP collapse support to two max_ptes_none values, 0 and HPAGE_PMD_NR - 1. This effectively supports two modes: - max_ptes_none=3D0: never introduce new none-pages for mTHP collapse. - max_ptes_none=3D511 (on 4k pagesz): Always collapse to the highest available mTHP order. This removes the possiblilty of "creep", while not modifying any uAPI expectations. A warning will be emitted if any non-supported max_ptes_none value is configured with mTHP enabled. 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 | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 8dab49c53128..f425238d5d4f 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -463,6 +463,44 @@ 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, we currently only support khugepaged_max_pte_none v= alues + * of 0 or (HPAGE_PMD_NR - 1). Any other value will emit a warning and no = mTHP + * collapse will be attempted + * + * 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) +{ + /* ignore max_ptes_none limits */ + if (full_scan) + return HPAGE_PMD_NR - 1; + + if (!is_mthp_order(order)) + return khugepaged_max_ptes_none; + + /* Zero/non-present collapse disabled. */ + if (!khugepaged_max_ptes_none) + return 0; + + if (khugepaged_max_ptes_none =3D=3D HPAGE_PMD_NR - 1) + return (1 << order) - 1; + + pr_warn_once("mTHP collapse only supports max_ptes_none values of 0 or %d= \n", + HPAGE_PMD_NR - 1); + return -EINVAL; +} + void khugepaged_enter_vma(struct vm_area_struct *vma, vm_flags_t vm_flags) { @@ -550,7 +588,10 @@ 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); + + if (max_ptes_none =3D=3D -EINVAL) + goto out; =20 for (_pte =3D pte; _pte < pte + nr_pages; _pte++, addr +=3D PAGE_SIZE) { --=20 2.51.1