From nobody Wed Feb 11 03:51:32 2026 Received: from out30-112.freemail.mail.aliyun.com (out30-112.freemail.mail.aliyun.com [115.124.30.112]) (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 2A59B15820F for ; Mon, 19 Aug 2024 08:14:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.112 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724055293; cv=none; b=qJM7ZWnJI+IeSkaJD8RK+jorH6dopBU6xvcNgAiZiV9zE+L++Q4yuUIdZA3EFOUYjuj4a4nJR5nuJEWAo649WTfdkWP+MaUAlrzQG4fouYlwZNFROl1wZlvKnVule4SzLe9EwGPaxLvdKBZdqEM9zg68t7foiMUE56l0Z0hrppk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724055293; c=relaxed/simple; bh=Jk5paYBWA74YJEdDoc1w57dR5QTa0m1FG+hogFp6Jp0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qgCvJW8vLLzX1FzkdqEwppSYpSVU49w7+gfGQozJd0YQl9qx3Pl3SCNwOXGQM5rNLWnx3Sdbv776HGMtJX76TeF8zL26Pw5VPsB2l76bnC53Bs5job0DeDk5LEGC4RVZCN5d2Wp6vNajWPN4YRZ32O1Lzyfb/yWMoo7oWOSspu0= 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=Acp9cb/D; arc=none smtp.client-ip=115.124.30.112 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="Acp9cb/D" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1724055286; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=wXFLqHbVSHxFKCZqqifJmzb9vdz6whYXmBhNU0imZD0=; b=Acp9cb/DupVdSrhmtniMMNOLRLSPf+tSK5diilQexTp3OG0WHtJP4plCBcbvWXNNrM3oZCgEsPEzHUvZJU5Q309IXP02+C7IO0sKc/Vd70KZtfipB86kabQGLmtesDMvceYXA8tjZKoJp9VXFu/H5+f+C8S7/+7qlvGUJttNaxc= Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WD8W7Ls_1724055285) by smtp.aliyun-inc.com; Mon, 19 Aug 2024 16:14:46 +0800 From: Baolin Wang To: akpm@linux-foundation.org Cc: hughd@google.com, willy@infradead.org, david@redhat.com, 21cnbao@gmail.com, ryan.roberts@arm.com, shy828301@gmail.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/5] mm: khugepaged: expand the is_refcount_suitable() to support file folios Date: Mon, 19 Aug 2024 16:14:02 +0800 Message-Id: X-Mailer: git-send-email 2.39.3 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" Expand the is_refcount_suitable() to support reference checks for file foli= os, as preparation for supporting shmem mTHP collapse. Signed-off-by: Baolin Wang --- mm/khugepaged.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index cdd1d8655a76..f11b4f172e61 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -549,8 +549,14 @@ static bool is_refcount_suitable(struct folio *folio) int expected_refcount; =20 expected_refcount =3D folio_mapcount(folio); - if (folio_test_swapcache(folio)) + if (folio_test_anon(folio)) { + expected_refcount +=3D folio_test_swapcache(folio) ? + folio_nr_pages(folio) : 0; + } else { expected_refcount +=3D folio_nr_pages(folio); + if (folio_test_private(folio)) + expected_refcount++; + } =20 return folio_ref_count(folio) =3D=3D expected_refcount; } @@ -2285,8 +2291,7 @@ static int hpage_collapse_scan_file(struct mm_struct = *mm, unsigned long addr, break; } =20 - if (folio_ref_count(folio) !=3D - 1 + folio_mapcount(folio) + folio_test_private(folio)) { + if (!is_refcount_suitable(folio)) { result =3D SCAN_PAGE_COUNT; break; } --=20 2.39.3 From nobody Wed Feb 11 03:51:32 2026 Received: from out30-100.freemail.mail.aliyun.com (out30-100.freemail.mail.aliyun.com [115.124.30.100]) (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 7DC6215AAD6 for ; Mon, 19 Aug 2024 08:14:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.100 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724055294; cv=none; b=nC4CO57UWgBWJJFI+cEOg8ie3hUBfDnfW1k+R76EVqFERbmuxu8FjqSp3YGCjO8drYyH7Nn6hSXVMyjjWke0pJxxoIwV71NlyuiOrupgQ6CoBvXjEFwWJMHEIqb0zibL5K/k1+NrjEQ+93MOgklAnCBkYZfZ/8vzpXwsheM2TNo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724055294; c=relaxed/simple; bh=5fN542VEOuMC7wUi304QfjRXCRFt5ylwMXY6d3EVch8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=p65nQW8cpOyv0I4mzLIVYqHL94z1gsll9b1mN0JbEunAdLwVLkFrvsqfAvpAQC4bmLSGKmSzqyf6thl9ZzBxVaZiIjBVI/qSY/DPbZpJ8yESUmczu/Yi9Cd5zQ2IYlk9k8HLgIafhI1iHwLkzJ4vopCBEBbyoB+bWWDMSvm0RIY= 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=oWJLyi4k; arc=none smtp.client-ip=115.124.30.100 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="oWJLyi4k" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1724055287; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=mYH71MdwFDoWK+6jyn77RluPuidTuLdZqV2Iscl7EWk=; b=oWJLyi4kdNFu4Y6qRIIp6cKqAjksxETGpuOvAvvPneIQ371Swvaw5BNDXbqqqzYT5IxxjH/Wg1aVfcJAlEW4JLgeuloqhTIeVXk7TAEoL9+tsw1RM2GsXnsA2m9lxPR6Nk8QSsUo+SqLb/h4akeJix8jfJ/Glj/3Nk5umxTwsNQ= Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WD8W7MM_1724055286) by smtp.aliyun-inc.com; Mon, 19 Aug 2024 16:14:47 +0800 From: Baolin Wang To: akpm@linux-foundation.org Cc: hughd@google.com, willy@infradead.org, david@redhat.com, 21cnbao@gmail.com, ryan.roberts@arm.com, shy828301@gmail.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/5] mm: khugepaged: use the number of pages in the folio to check the reference count Date: Mon, 19 Aug 2024 16:14:03 +0800 Message-Id: X-Mailer: git-send-email 2.39.3 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" Use the number of pages in the folio to check the reference count as preparation for supporting shmem mTHP collapse. Signed-off-by: Baolin Wang Acked-by: David Hildenbrand --- mm/khugepaged.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index f11b4f172e61..60d95f08610c 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1994,7 +1994,7 @@ static int collapse_file(struct mm_struct *mm, unsign= ed long addr, /* * We control three references to the folio: * - we hold a pin on it; - * - one reference from page cache; + * - nr_pages reference from page cache; * - one from lru_isolate_folio; * If those are the only references, then any new usage * of the folio will have to fetch it from the page @@ -2002,7 +2002,7 @@ static int collapse_file(struct mm_struct *mm, unsign= ed long addr, * truncate, so any new usage will be blocked until we * unlock folio after collapse/during rollback. */ - if (folio_ref_count(folio) !=3D 3) { + if (folio_ref_count(folio) !=3D 2 + folio_nr_pages(folio)) { result =3D SCAN_PAGE_COUNT; xas_unlock_irq(&xas); folio_putback_lru(folio); @@ -2185,7 +2185,7 @@ static int collapse_file(struct mm_struct *mm, unsign= ed long addr, folio_clear_active(folio); folio_clear_unevictable(folio); folio_unlock(folio); - folio_put_refs(folio, 3); + folio_put_refs(folio, 2 + folio_nr_pages(folio)); } =20 goto out; --=20 2.39.3 From nobody Wed Feb 11 03:51:32 2026 Received: from out30-111.freemail.mail.aliyun.com (out30-111.freemail.mail.aliyun.com [115.124.30.111]) (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 7427715B560 for ; Mon, 19 Aug 2024 08:14:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.111 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724055300; cv=none; b=mdDArIKzB2zzbZyGc7bSDWysGqzoXxdJE4MoJAZ6zsNdvGRYs79u3UVIkO2jDJwlYJhQP+G/xU2NnK4j+r9A0/tzdiiy8FQylL1q7TsaDOWnRV/iPPIDiAKle2PpJFj52dGxM48ohaP3XDJqVivp4f1vMdTFE1FzyV/YMfy+9eA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724055300; c=relaxed/simple; bh=D4D96CGvyIdDn9IdkVMBDqkPKSa5YgZh94Mj5skAncA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jPF9gG7RwfcOanaTqIzY0c00ijpZYw8J1YoaQjzXp9DDkJEMBzDTrG6YdzsQQcwmlXK4xPcWs3/ocVaO9zwr1YThLsAmcbQELhd1v1PSLn7wrP3xaboIkaojgAPoq5+CmkmKgaZDbJUbOw9iQQlY2RNlC/gpTwbSp32iYwNVguI= 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=qVbW7OTO; arc=none smtp.client-ip=115.124.30.111 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="qVbW7OTO" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1724055288; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=xzFqhooDuGLHWOHvpjrYWo9DgZTyuxAAfwMQUt6kp8M=; b=qVbW7OTOmn2qGCUgqsHihyb8RKG3K3ipQjiTJ+Ao3uBuKP0An+MGd6d12a7ru68F7nDhhcDPqLEs5TSfaRv6sXVU/nplHBBmbpbBMfAf9BmSZQ4+oKLJmoNFu71zMtGr2PfU9AiF26GBl8B3Br3DcnnglBwukeCxRVoPG6cnekc= Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WD9CrIU_1724055287) by smtp.aliyun-inc.com; Mon, 19 Aug 2024 16:14:48 +0800 From: Baolin Wang To: akpm@linux-foundation.org Cc: hughd@google.com, willy@infradead.org, david@redhat.com, 21cnbao@gmail.com, ryan.roberts@arm.com, shy828301@gmail.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 3/5] mm: khugepaged: support shmem mTHP copy Date: Mon, 19 Aug 2024 16:14:04 +0800 Message-Id: <938e8cc5a15f841fdd343d324d8a6dd989cd52e0.1724054125.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 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" Iterate each subpage in the large folio to copy, as preparation for support= ing shmem mTHP collapse. Signed-off-by: Baolin Wang --- mm/khugepaged.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 60d95f08610c..91ee672db202 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -2060,17 +2060,22 @@ static int collapse_file(struct mm_struct *mm, unsi= gned long addr, index =3D start; dst =3D folio_page(new_folio, 0); list_for_each_entry(folio, &pagelist, lru) { + int i, nr_pages =3D folio_nr_pages(folio); + while (index < folio->index) { clear_highpage(dst); index++; dst++; } - if (copy_mc_highpage(dst, folio_page(folio, 0)) > 0) { - result =3D SCAN_COPY_MC; - goto rollback; + + for (i =3D 0; i < nr_pages; i++) { + if (copy_mc_highpage(dst, folio_page(folio, i)) > 0) { + result =3D SCAN_COPY_MC; + goto rollback; + } + index++; + dst++; } - index++; - dst++; } while (index < end) { clear_highpage(dst); --=20 2.39.3 From nobody Wed Feb 11 03:51:32 2026 Received: from out30-110.freemail.mail.aliyun.com (out30-110.freemail.mail.aliyun.com [115.124.30.110]) (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 849DD15B542 for ; Mon, 19 Aug 2024 08:14:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.110 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724055300; cv=none; b=r1ZJBvFD2DbbHUP+lHd56Egay0CjNqgUEQF7oKAfgwO+LSDrChOdJ81SRG01r34tnDaAa/o9NFbyegp0o59v9ZLuPUxX12yu+r6mCmt7W6xbHLxUTz/VUp8J5ZxQz8jDZ/hC0FgWuFOVviCHrdxMwHSIeRHI7azR6cVay2fESCg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724055300; c=relaxed/simple; bh=BbsXRtILYWc1jNA6sN6O5EgMT7i8APGH25zxp413B8c=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=YkvtEhzm731982HLby5I7GhF6e3tPY8jejxNqI6Qj4LZFfEBceBfe5KVIeyYT4yC2GtrhIKUVy4/Cb+PGyjIgNFwWBAvfu7dSn+mVswVrUTWh9dhv0jIYwgL6shtgvnnQkSPaqOM4jvySAajsdD3+Ymtd7gv3iM65qiqhajvfXo= 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=Q+71b/vS; arc=none smtp.client-ip=115.124.30.110 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="Q+71b/vS" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1724055289; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=6V7YyBIVTByMizq9TfzYvtX9oniYv5CXu0xU9TzqEDc=; b=Q+71b/vSzeLvPa9Hru58MJMT8WGyNqTkr/emE5ReLwwngGl2f5CbXolV3ePve8ZKtDUjzZFEq6rQelThzmmr6k9EOv4u63cj4NKhgxcumrKoIsQHMDbbfmlby4mnK26Sl6jRuFkp3WIFfqMK6mxvTiMCVvIUotAPsv8zffJynss= Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WD9CrIt_1724055288) by smtp.aliyun-inc.com; Mon, 19 Aug 2024 16:14:49 +0800 From: Baolin Wang To: akpm@linux-foundation.org Cc: hughd@google.com, willy@infradead.org, david@redhat.com, 21cnbao@gmail.com, ryan.roberts@arm.com, shy828301@gmail.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 4/5] mm: khugepaged: support shmem mTHP collapse Date: Mon, 19 Aug 2024 16:14:05 +0800 Message-Id: <8a2a87affac65f2ecdcea9917b5ae7a261689676.1724054125.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 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" Shmem already supports the allocation of mTHP, but khugepaged does not yet support collapsing mTHP folios. Now khugepaged is ready to support mTHP, and this patch enables the collapse of shmem mTHP. Signed-off-by: Baolin Wang --- mm/khugepaged.c | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 91ee672db202..4b35239b5e46 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1847,7 +1847,7 @@ static int collapse_file(struct mm_struct *mm, unsign= ed long addr, } } while (1); =20 - for (index =3D start; index < end; index++) { + for (index =3D start; index < end;) { xas_set(&xas, index); folio =3D xas_load(&xas); =20 @@ -1866,6 +1866,7 @@ static int collapse_file(struct mm_struct *mm, unsign= ed long addr, } } nr_none++; + index++; continue; } =20 @@ -1947,12 +1948,10 @@ static int collapse_file(struct mm_struct *mm, unsi= gned long addr, * we locked the first folio, then a THP might be there already. * This will be discovered on the first iteration. */ - if (folio_test_large(folio)) { - result =3D folio_order(folio) =3D=3D HPAGE_PMD_ORDER && - folio->index =3D=3D start - /* Maybe PMD-mapped */ - ? SCAN_PTE_MAPPED_HUGEPAGE - : SCAN_PAGE_COMPOUND; + if (folio_order(folio) =3D=3D HPAGE_PMD_ORDER && + folio->index =3D=3D start) { + /* Maybe PMD-mapped */ + result =3D SCAN_PTE_MAPPED_HUGEPAGE; goto out_unlock; } =20 @@ -2013,6 +2012,7 @@ static int collapse_file(struct mm_struct *mm, unsign= ed long addr, * Accumulate the folios that are being collapsed. */ list_add_tail(&folio->lru, &pagelist); + index +=3D folio_nr_pages(folio); continue; out_unlock: folio_unlock(folio); @@ -2265,16 +2265,10 @@ static int hpage_collapse_scan_file(struct mm_struc= t *mm, unsigned long addr, continue; } =20 - /* - * TODO: khugepaged should compact smaller compound pages - * into a PMD sized page - */ - if (folio_test_large(folio)) { - result =3D folio_order(folio) =3D=3D HPAGE_PMD_ORDER && - folio->index =3D=3D start - /* Maybe PMD-mapped */ - ? SCAN_PTE_MAPPED_HUGEPAGE - : SCAN_PAGE_COMPOUND; + if (folio_order(folio) =3D=3D HPAGE_PMD_ORDER && + folio->index =3D=3D start) { + /* Maybe PMD-mapped */ + result =3D SCAN_PTE_MAPPED_HUGEPAGE; /* * For SCAN_PTE_MAPPED_HUGEPAGE, further processing * by the caller won't touch the page cache, and so --=20 2.39.3 From nobody Wed Feb 11 03:51:32 2026 Received: from out30-118.freemail.mail.aliyun.com (out30-118.freemail.mail.aliyun.com [115.124.30.118]) (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 84DCA15B97B for ; Mon, 19 Aug 2024 08:14:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.118 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724055302; cv=none; b=Tqnq6ZXLzRPn6mw2+HV+/zS2UQk1TemL/QXG8Q07fSq6tXcgKCygteXdWGjRE2NJXjYAkALhx/y8locVLNrHNvyM5H9fXUpyyfYM5hPMavfU3mIfLO63/p/C7jFVSB0nOpQjmxlFVdMFFV2/sfC73fZEqBFRL1eEoPjpGORgph4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724055302; c=relaxed/simple; bh=0ewCSOF2c1G2qY1J0DIuEJ+k7ukyWOpw1yoCOGS9yLM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=nmvNQe8I11IirYCCxDs0LMGE+FudDMFheP0XTQL3cFaE++8LtSuyC9z/WZfX6FuITvgn56KkHZ2A6/dmXf4B4aco2jw2tfxAKtaaLb1VS1JTLsOsDvqmJPYSLowwVHTZ2b8tpsxNrqvb0psKIgz6H15WXBd4Vt6Id/Wen11yQtE= 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=lzaVWQPw; arc=none smtp.client-ip=115.124.30.118 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="lzaVWQPw" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1724055290; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=NETcifOYIb+Yf9TxOqDeY39PaKApxrCiukqpjtRmh74=; b=lzaVWQPwQl6Q1jSXpNxmtZPMNhrOnYTDm+NOLlsqSDpWYALMnsbcw5yRTWdJe6dcUbVk1PMMiJlYaeh/VsKB43Nc+a7Lkr6D2cBL22EXgeCYfaVZI6chNArhqUEqE/jp1VxoiwXNdqS+fbk8yd4WZ8qrbUL0q+cXA0yvxfREyV8= Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WD9CrJE_1724055289) by smtp.aliyun-inc.com; Mon, 19 Aug 2024 16:14:50 +0800 From: Baolin Wang To: akpm@linux-foundation.org Cc: hughd@google.com, willy@infradead.org, david@redhat.com, 21cnbao@gmail.com, ryan.roberts@arm.com, shy828301@gmail.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 5/5] selftests: mm: support shmem mTHP collapse testing Date: Mon, 19 Aug 2024 16:14:06 +0800 Message-Id: <12ec6a8b5fe51cd17db1a82c3ffa5006d4b22de3.1724054125.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 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" Add shmem mTHP collpase testing. Similar to the anonymous page, users can u= se the '-s' parameter to specify the shmem mTHP size for testing. Signed-off-by: Baolin Wang --- tools/testing/selftests/mm/khugepaged.c | 4 +- tools/testing/selftests/mm/thp_settings.c | 46 ++++++++++++++++++++--- tools/testing/selftests/mm/thp_settings.h | 9 ++++- 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selfte= sts/mm/khugepaged.c index 829320a519e7..56d4480e8d3c 100644 --- a/tools/testing/selftests/mm/khugepaged.c +++ b/tools/testing/selftests/mm/khugepaged.c @@ -1095,7 +1095,7 @@ static void usage(void) fprintf(stderr, "\n\tSupported Options:\n"); fprintf(stderr, "\t\t-h: This help message.\n"); fprintf(stderr, "\t\t-s: mTHP size, expressed as page order.\n"); - fprintf(stderr, "\t\t Defaults to 0. Use this size for anon allocation= s.\n"); + fprintf(stderr, "\t\t Defaults to 0. Use this size for anon or shmem a= llocations.\n"); exit(1); } =20 @@ -1209,6 +1209,8 @@ int main(int argc, char **argv) default_settings.khugepaged.pages_to_scan =3D hpage_pmd_nr * 8; default_settings.hugepages[hpage_pmd_order].enabled =3D THP_INHERIT; default_settings.hugepages[anon_order].enabled =3D THP_ALWAYS; + default_settings.shmem_hugepages[hpage_pmd_order].enabled =3D SHMEM_INHER= IT; + default_settings.shmem_hugepages[anon_order].enabled =3D SHMEM_ALWAYS; =20 save_settings(); thp_push_settings(&default_settings); diff --git a/tools/testing/selftests/mm/thp_settings.c b/tools/testing/self= tests/mm/thp_settings.c index a4163438108e..577eaab6266f 100644 --- a/tools/testing/selftests/mm/thp_settings.c +++ b/tools/testing/selftests/mm/thp_settings.c @@ -33,10 +33,11 @@ static const char * const thp_defrag_strings[] =3D { }; =20 static const char * const shmem_enabled_strings[] =3D { + "never", "always", "within_size", "advise", - "never", + "inherit", "deny", "force", NULL @@ -200,6 +201,7 @@ void thp_write_num(const char *name, unsigned long num) void thp_read_settings(struct thp_settings *settings) { unsigned long orders =3D thp_supported_orders(); + unsigned long shmem_orders =3D thp_shmem_supported_orders(); char path[PATH_MAX]; int i; =20 @@ -234,12 +236,24 @@ void thp_read_settings(struct thp_settings *settings) settings->hugepages[i].enabled =3D thp_read_string(path, thp_enabled_strings); } + + for (i =3D 0; i < NR_ORDERS; i++) { + if (!((1 << i) & shmem_orders)) { + settings->shmem_hugepages[i].enabled =3D SHMEM_NEVER; + continue; + } + snprintf(path, PATH_MAX, "hugepages-%ukB/shmem_enabled", + (getpagesize() >> 10) << i); + settings->shmem_hugepages[i].enabled =3D + thp_read_string(path, shmem_enabled_strings); + } } =20 void thp_write_settings(struct thp_settings *settings) { struct khugepaged_settings *khugepaged =3D &settings->khugepaged; unsigned long orders =3D thp_supported_orders(); + unsigned long shmem_orders =3D thp_shmem_supported_orders(); char path[PATH_MAX]; int enabled; int i; @@ -271,6 +285,15 @@ void thp_write_settings(struct thp_settings *settings) enabled =3D settings->hugepages[i].enabled; thp_write_string(path, thp_enabled_strings[enabled]); } + + for (i =3D 0; i < NR_ORDERS; i++) { + if (!((1 << i) & shmem_orders)) + continue; + snprintf(path, PATH_MAX, "hugepages-%ukB/shmem_enabled", + (getpagesize() >> 10) << i); + enabled =3D settings->shmem_hugepages[i].enabled; + thp_write_string(path, shmem_enabled_strings[enabled]); + } } =20 struct thp_settings *thp_current_settings(void) @@ -324,17 +347,18 @@ void thp_set_read_ahead_path(char *path) dev_queue_read_ahead_path[sizeof(dev_queue_read_ahead_path) - 1] =3D '\0'; } =20 -unsigned long thp_supported_orders(void) +static unsigned long __thp_supported_orders(bool is_shmem) { unsigned long orders =3D 0; char path[PATH_MAX]; char buf[256]; - int ret; - int i; + int ret, i; + char anon_dir[] =3D "enabled"; + char shmem_dir[] =3D "shmem_enabled"; =20 for (i =3D 0; i < NR_ORDERS; i++) { - ret =3D snprintf(path, PATH_MAX, THP_SYSFS "hugepages-%ukB/enabled", - (getpagesize() >> 10) << i); + ret =3D snprintf(path, PATH_MAX, THP_SYSFS "hugepages-%ukB/%s", + (getpagesize() >> 10) << i, is_shmem ? shmem_dir : anon_dir); if (ret >=3D PATH_MAX) { printf("%s: Pathname is too long\n", __func__); exit(EXIT_FAILURE); @@ -347,3 +371,13 @@ unsigned long thp_supported_orders(void) =20 return orders; } + +unsigned long thp_supported_orders(void) +{ + return __thp_supported_orders(false); +} + +unsigned long thp_shmem_supported_orders(void) +{ + return __thp_supported_orders(true); +} diff --git a/tools/testing/selftests/mm/thp_settings.h b/tools/testing/self= tests/mm/thp_settings.h index 71cbff05f4c7..876235a23460 100644 --- a/tools/testing/selftests/mm/thp_settings.h +++ b/tools/testing/selftests/mm/thp_settings.h @@ -22,10 +22,11 @@ enum thp_defrag { }; =20 enum shmem_enabled { + SHMEM_NEVER, SHMEM_ALWAYS, SHMEM_WITHIN_SIZE, SHMEM_ADVISE, - SHMEM_NEVER, + SHMEM_INHERIT, SHMEM_DENY, SHMEM_FORCE, }; @@ -46,6 +47,10 @@ struct khugepaged_settings { unsigned long pages_to_scan; }; =20 +struct shmem_hugepages_settings { + enum shmem_enabled enabled; +}; + struct thp_settings { enum thp_enabled thp_enabled; enum thp_defrag thp_defrag; @@ -54,6 +59,7 @@ struct thp_settings { struct khugepaged_settings khugepaged; unsigned long read_ahead_kb; struct hugepages_settings hugepages[NR_ORDERS]; + struct shmem_hugepages_settings shmem_hugepages[NR_ORDERS]; }; =20 int read_file(const char *path, char *buf, size_t buflen); @@ -76,5 +82,6 @@ void thp_save_settings(void); =20 void thp_set_read_ahead_path(char *path); unsigned long thp_supported_orders(void); +unsigned long thp_shmem_supported_orders(void); =20 #endif /* __THP_SETTINGS_H__ */ --=20 2.39.3