From nobody Sat Oct 4 06:37:16 2025 Received: from out30-98.freemail.mail.aliyun.com (out30-98.freemail.mail.aliyun.com [115.124.30.98]) (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 E67E02DAFA5 for ; Wed, 20 Aug 2025 09:07:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.98 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755680867; cv=none; b=fujpGvQn7EsNVW2W1hphM2ugp8rYYfMt2qBSS2+QJF2pPJsN3qI2PyxMB5wFeLKIQp3Z+7l9YE+wbKRBu37ihKdv3w6mdXrK804MiwGV0/mviPNlXtsvXGtfrx+5UoFaBhNsWRCMgDH5EcSkmaL2pSCBNtosr9b+4Yh2DFvsn3U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755680867; c=relaxed/simple; bh=VxWP1RtZGBoLLD4W5dwxuUIsJuYfdEtY8FUDOFtmi4Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HomEIhD8+nD2jK5Ps7s+D/WlZbkzGcTB23LUSTu+ZaC9OYmh7uDuUcjIdiKa6VEk6TKRdoiS6eORIFqO6dkcZdlF0Zo54EM5GOGTBPXBRXAgmL+Uv5vXH46mIy7/4qsJQfoTkfONB+Vhhhe8pJLmSt4gGpDQwaW/Cf23C41mdCo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=xrpJYgPT; arc=none smtp.client-ip=115.124.30.98 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="xrpJYgPT" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1755680856; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=Ei9u2YF++6MO0lYBeBOSJFHTc0M7jUeUTkqRnIOPTM8=; b=xrpJYgPTaQo7Q6rDSbM1KZX3XDCmznbywwzdc8gJemvJJieGYkBUWmtgKEagR/oJh0IDHQXvYNPS0olbDJ2Al17Q39RIYRQsth4Kkv3bpsdGoZw8I9JfF5fvFV4IkEZ6bb3LFhzhM/5Y9JLooiKBXz7yAsHlgdKBuiSh+bO+CVs= Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WmBICrd_1755680855 cluster:ay36) by smtp.aliyun-inc.com; Wed, 20 Aug 2025 17:07:35 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com, david@redhat.com, lorenzo.stoakes@oracle.com Cc: ziy@nvidia.com, Liam.Howlett@oracle.com, npache@redhat.com, ryan.roberts@arm.com, dev.jain@arm.com, baohua@kernel.org, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 02/11] mm: khugepaged: generalize collapse_file for mTHP support Date: Wed, 20 Aug 2025 17:07:13 +0800 Message-ID: <6a2f28b4541bbbc56ea9e07f24b67cef87899a50.1755677674.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: References: 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" Generalize the order of the collapse_file() function to support future file/shmem mTHP collapse. No functional changes in this patch. Signed-off-by: Baolin Wang --- mm/khugepaged.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 5d4493b77f3c..e64ed86d28ca 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -2064,21 +2064,23 @@ static void retract_page_tables(struct address_spac= e *mapping, pgoff_t pgoff) */ static int collapse_file(struct mm_struct *mm, unsigned long addr, struct file *file, pgoff_t start, - struct collapse_control *cc) + struct collapse_control *cc, + int order) { struct address_space *mapping =3D file->f_mapping; struct page *dst; struct folio *folio, *tmp, *new_folio; - pgoff_t index =3D 0, end =3D start + HPAGE_PMD_NR; + int nr_pages =3D 1 << order; + pgoff_t index =3D 0, end =3D start + nr_pages; LIST_HEAD(pagelist); - XA_STATE_ORDER(xas, &mapping->i_pages, start, HPAGE_PMD_ORDER); + XA_STATE_ORDER(xas, &mapping->i_pages, start, order); int nr_none =3D 0, result =3D SCAN_SUCCEED; bool is_shmem =3D shmem_file(file); =20 VM_BUG_ON(!IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && !is_shmem); - VM_BUG_ON(start & (HPAGE_PMD_NR - 1)); + VM_BUG_ON(start & (nr_pages - 1)); =20 - result =3D alloc_charge_folio(&new_folio, mm, cc, HPAGE_PMD_ORDER); + result =3D alloc_charge_folio(&new_folio, mm, cc, order); if (result !=3D SCAN_SUCCEED) goto out; =20 @@ -2426,14 +2428,14 @@ static int collapse_file(struct mm_struct *mm, unsi= gned long addr, * unwritten page. */ folio_mark_uptodate(new_folio); - folio_ref_add(new_folio, HPAGE_PMD_NR - 1); + folio_ref_add(new_folio, nr_pages - 1); =20 if (is_shmem) folio_mark_dirty(new_folio); folio_add_lru(new_folio); =20 /* Join all the small entries into a single multi-index entry. */ - xas_set_order(&xas, start, HPAGE_PMD_ORDER); + xas_set_order(&xas, start, order); xas_store(&xas, new_folio); WARN_ON_ONCE(xas_error(&xas)); xas_unlock_irq(&xas); @@ -2496,7 +2498,7 @@ static int collapse_file(struct mm_struct *mm, unsign= ed long addr, folio_put(new_folio); out: VM_BUG_ON(!list_empty(&pagelist)); - trace_mm_khugepaged_collapse_file(mm, new_folio, index, addr, is_shmem, f= ile, HPAGE_PMD_NR, result); + trace_mm_khugepaged_collapse_file(mm, new_folio, index, addr, is_shmem, f= ile, nr_pages, result); return result; } =20 @@ -2599,7 +2601,7 @@ static int collapse_scan_file(struct mm_struct *mm, u= nsigned long addr, result =3D SCAN_EXCEED_NONE_PTE; count_vm_event(THP_SCAN_EXCEED_NONE_PTE); } else { - result =3D collapse_file(mm, addr, file, start, cc); + result =3D collapse_file(mm, addr, file, start, cc, HPAGE_PMD_ORDER); } } =20 --=20 2.43.5