From nobody Fri Dec 19 10:22:39 2025 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 2D56514B96E for ; Wed, 26 Mar 2025 03:38:31 +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=1742960315; cv=none; b=oU/Zem2cAcewcb1o3/KwgEYPvyAMJjLPsc2eRkWLB3BUJ8W7sE7VZXQrUAKoicfr4j6xoOLm34mrun8ZwhkVIUziFITFinxRiJ94gjgx2YYebpzGXnr0xJUW6Rz1h+UrjCj6G1ARSc5s+NLHDvqF6Ol57amtigS1SRQSZkXulnQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742960315; c=relaxed/simple; bh=UrQoTHRlyNlyNlMyojiO33lh4yHUrggD+W4DSvNq4tM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bRGDGF/wV7JW7Rmejo08yhfcKFgHrqkdf0PmJNPm0ZckfVBbNEMUZfFxKV//p8sMmQ3yUbsbi2DebHvG8AlIxOe+e2gZRyVfvvlRNyQ72yMNsu6A5xllHUsraodC46/iZwKC5YgHwMKkIyV/Ohg6Mw+vphIDRE/5Sp2uQ+mcwyo= 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=iMVtPqA+; 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="iMVtPqA+" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1742960304; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=MAxRD8P9W2N0YRcf5gQPGY2DJk1Zq+bjVf3MR/WuNdQ=; b=iMVtPqA+QAQKgF5/QKYzL92sXBywrRPAUdtTIUa31zXHbU32Cqwg7Zd/i2uXGoUJXQS75y8BBju57mPl62eHMoSx9yifAmQH3yXoyCtG6Z1c0ECaHk3yXZRUOzzAIHwzCTmtE2OvKkonZb/K/Lvm8rTlOqsDfKbRdxXqF0lz4Kc= Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WT1p3wj_1742960303 cluster:ay36) by smtp.aliyun-inc.com; Wed, 26 Mar 2025 11:38:23 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, 21cnbao@gmail.com, ryan.roberts@arm.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/2] selftests: mincore: fix tmpfs mincore test failure Date: Wed, 26 Mar 2025 11:38:10 +0800 Message-ID: <99a3e190d38b08a2b96ede952a29893bffdb3432.1742960003.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" When running mincore test cases, I encountered the following failures: " mincore_selftest.c:359:check_tmpfs_mmap:Expected ra_pages (511) =3D=3D 0 (0) mincore_selftest.c:360:check_tmpfs_mmap:Read-ahead pages found in memory check_tmpfs_mmap: Test terminated by assertion FAIL global.check_tmpfs_mmap not ok 5 global.check_tmpfs_mmap FAILED: 4 / 5 tests passed " The reason for the test case failure is that my system automatically enabled tmpfs large folio allocation by adding the 'transparent_hugepage_tmpfs=3Dal= ways' cmdline. However, the test case still expects the tmpfs mounted on /dev/shm= to allocate small folios, which leads to assertion failures when verifying rea= dahead pages. To fix this issue, remount tmpfs to a new test directory and set the 'huge= =3Dnever' parameter to avoid allocating large folios, which can pass the test. Signed-off-by: Baolin Wang --- .../selftests/mincore/mincore_selftest.c | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/mincore/mincore_selftest.c b/tools/tes= ting/selftests/mincore/mincore_selftest.c index e949a43a6145..e8d7a3a4739f 100644 --- a/tools/testing/selftests/mincore/mincore_selftest.c +++ b/tools/testing/selftests/mincore/mincore_selftest.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include =20 @@ -283,7 +284,7 @@ TEST(check_file_mmap) free(vec); } =20 - +#define INPUT_MAX 80 /* * Test mincore() behavior on a page backed by a tmpfs file. This test * performs the same steps as the previous one. However, we don't expect @@ -291,6 +292,9 @@ TEST(check_file_mmap) */ TEST(check_tmpfs_mmap) { + char tmpfs_template[] =3D "/tmp/check_tmpfs_XXXXXX"; + const char *tmpfs_loc =3D mkdtemp(tmpfs_template); + char testfile[INPUT_MAX]; unsigned char *vec; int vec_size; char *addr; @@ -300,6 +304,10 @@ TEST(check_tmpfs_mmap) int i; int ra_pages =3D 0; =20 + ASSERT_NE(NULL, tmpfs_loc) { + TH_LOG("Can't mkdir tmpfs dentry\n"); + } + page_size =3D sysconf(_SC_PAGESIZE); vec_size =3D FILE_SIZE / page_size; if (FILE_SIZE % page_size) @@ -311,7 +319,18 @@ TEST(check_tmpfs_mmap) } =20 errno =3D 0; - fd =3D open("/dev/shm", O_TMPFILE | O_RDWR, 0600); + /* Do not use large folios for tmpfs mincore testing */ + retval =3D mount("tmpfs", tmpfs_loc, "tmpfs", 0, "huge=3Dnever,size=3D4M"= ); + ASSERT_EQ(0, retval) { + TH_LOG("Unable to mount tmpfs for testing\n"); + } + + retval =3D snprintf(testfile, INPUT_MAX, "%s/test_file", tmpfs_loc); + ASSERT_GE(INPUT_MAX, retval) { + TH_LOG("Unable to create a tmpfs for testing\n"); + } + + fd =3D open(testfile, O_CREAT|O_RDWR, 0664); ASSERT_NE(-1, fd) { TH_LOG("Can't create temporary file: %s", strerror(errno)); @@ -363,6 +382,8 @@ TEST(check_tmpfs_mmap) munmap(addr, FILE_SIZE); close(fd); free(vec); + umount(tmpfs_loc); + rmdir(tmpfs_loc); } =20 TEST_HARNESS_MAIN --=20 2.43.5 From nobody Fri Dec 19 10:22:39 2025 Received: from out199-18.us.a.mail.aliyun.com (out199-18.us.a.mail.aliyun.com [47.90.199.18]) (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 B2B1114B96E for ; Wed, 26 Mar 2025 03:38:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=47.90.199.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742960326; cv=none; b=FxbvAHswRl7n7Cn7zHsLSTFy2TtRnWlGAEU6XA1xIH50JvIUZGskjgpC+rxN3KciuaEQpPhSeTrAFwmKk56kgbIJ/y3nYYZvnBeGsUP+ZM/B18XRro4qpWUdHe+80GOmrrOyi1bSS+Wte5/lAHtDf6zD3SnwqrB3StGYvn/db5c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742960326; c=relaxed/simple; bh=kzuBG+mctN9JNDwceI3PhK/fkfhKbsz2BUjJMajn7ew=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ODjrqhvGOJFduxW3WMDTeTVpPDfj6whad69tS11WGJW+aCCtPipjpDVUi21rKYCNTJl1oUOXltv5Af0+kk71Nn/WKtkvn8vcQmDg4KHw6qzfeO7tDqeM5mg+pMmzudy6PTkHSUOwjLJAob7ZJRQt/dyrGlrAKlrMk9LOv7xaCZM= 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=mz6fdK0n; arc=none smtp.client-ip=47.90.199.18 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="mz6fdK0n" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1742960304; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=5NwoAahKKVIW1jwVHjfRD7hDZG4CMMysDy/7TbSbzCE=; b=mz6fdK0n95h+NiUBpVWdTyhVY16F6o1CJAgKqSbaVRiSrqXLg8LNHIv2fVHQa+k7accuCfN5sxOMotAnJQLLYugr54kgBklCjmCqWq4XHXt1GN3Iyf6pWACy12g6HHekujqMzW9a9cK0vdm35TDnzVZAXWc4zrIoVj7i2tQf3CE= Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WT1p3x3_1742960303 cluster:ay36) by smtp.aliyun-inc.com; Wed, 26 Mar 2025 11:38:24 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, 21cnbao@gmail.com, ryan.roberts@arm.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/2] mm: mincore: use folio_pte_batch() to batch process large folios Date: Wed, 26 Mar 2025 11:38:11 +0800 Message-ID: <7ad05bc9299de5d954fb21a2da57f46dd6ec59d0.1742960003.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" When I tested the mincore() syscall, I observed that it takes longer with 64K mTHP enabled on my Arm64 server. The reason is the mincore_pte_range() still checks each PTE individually, even when the PTEs are contiguous, which is not efficient. Thus we can use folio_pte_batch() to get the batch number of the present contiguous PTEs, which can improve the performance. I tested the mincore() syscall with 1G anonymous memory populated with 64K mTHP, and observed an obvious performance improvement: w/o patch w/ patch changes 6022us 1115us +81% Moreover, I also tested mincore() with disabling mTHP/THP, and did not see any obvious regression. Signed-off-by: Baolin Wang --- mm/mincore.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/mm/mincore.c b/mm/mincore.c index 832f29f46767..88be180b5550 100644 --- a/mm/mincore.c +++ b/mm/mincore.c @@ -21,6 +21,7 @@ =20 #include #include "swap.h" +#include "internal.h" =20 static int mincore_hugetlb(pte_t *pte, unsigned long hmask, unsigned long = addr, unsigned long end, struct mm_walk *walk) @@ -105,6 +106,7 @@ static int mincore_pte_range(pmd_t *pmd, unsigned long = addr, unsigned long end, pte_t *ptep; unsigned char *vec =3D walk->private; int nr =3D (end - addr) >> PAGE_SHIFT; + int step, i; =20 ptl =3D pmd_trans_huge_lock(pmd, vma); if (ptl) { @@ -118,16 +120,31 @@ static int mincore_pte_range(pmd_t *pmd, unsigned lon= g addr, unsigned long end, walk->action =3D ACTION_AGAIN; return 0; } - for (; addr !=3D end; ptep++, addr +=3D PAGE_SIZE) { + for (; addr !=3D end; ptep +=3D step, addr +=3D step * PAGE_SIZE) { pte_t pte =3D ptep_get(ptep); =20 + step =3D 1; /* We need to do cache lookup too for pte markers */ if (pte_none_mostly(pte)) __mincore_unmapped_range(addr, addr + PAGE_SIZE, vma, vec); - else if (pte_present(pte)) - *vec =3D 1; - else { /* pte is a swap entry */ + else if (pte_present(pte)) { + if (pte_batch_hint(ptep, pte) > 1) { + struct folio *folio =3D vm_normal_folio(vma, addr, pte); + + if (folio && folio_test_large(folio)) { + const fpb_t fpb_flags =3D FPB_IGNORE_DIRTY | + FPB_IGNORE_SOFT_DIRTY; + int max_nr =3D (end - addr) / PAGE_SIZE; + + step =3D folio_pte_batch(folio, addr, ptep, pte, + max_nr, fpb_flags, NULL, NULL, NULL); + } + } + + for (i =3D 0; i < step; i++) + vec[i] =3D 1; + } else { /* pte is a swap entry */ swp_entry_t entry =3D pte_to_swp_entry(pte); =20 if (non_swap_entry(entry)) { @@ -146,7 +163,7 @@ static int mincore_pte_range(pmd_t *pmd, unsigned long = addr, unsigned long end, #endif } } - vec++; + vec +=3D step; } pte_unmap_unlock(ptep - 1, ptl); out: --=20 2.43.5