Skip to contentSkip to conversation

KVM: selftests: access_tracking_perf_test fixes for NUMA balancing and MGLRU

PATCH Superseded James Houghton 4 participants 12 messages Mar 27, 2025 – Mar 28, 2025

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover KVM: selftests: access_tracking_perf_test fixes for NUMA balancing and MGLRU

This is a follow-up from Maxim's recent v2[1] and the selftest changes from the v8 of the x86 lockless aging series[2].

With MGLRU, touching a page doesn't necessarily clear the Idle flag. This has come up in the past, and the recommendation was to use MGLRU generation numbers[3], which is what this series does.

With NUMA balancing, pages are temporarily mapped as PROT_NONE, so the SPTEs will be zapped, losing the Accessed bits. The fix here is, in the event we have lost access information to print a warning and continue with the test, just like what we do if the test is running a nested VM.

A flag is added for the user to specify if they wish for the test to always enforce or always skip this check.

Based on kvm/next.

[1]: https://lore.kernel.org/all/[email protected]/
[2]: https://lore.kernel.org/kvm/[email protected]/
[3]: https://lore.kernel.org/all/CAOUHufZeADNp_y=Ng+acmMMgnTR=ZGFZ7z-m6O47O=CmJauWjw@mail.gmail.com/

James Houghton (3):
cgroup: selftests: Move cgroup_util into its own library
KVM: selftests: Build and link selftests/cgroup/lib into KVM selftests
KVM: selftests: access_tracking_perf_test: Use MGLRU for access
tracking

Maxim Levitsky (1):
KVM: selftests: access_tracking_perf_test: Add option to skip the
sanity check

Sean Christopherson (1):
KVM: selftests: Extract guts of THP accessor to standalone sysfs
helpers

 tools/testing/selftests/cgroup/Makefile       |  21 +-
 .../selftests/cgroup/{ => lib}/cgroup_util.c  |   3 +-
 .../cgroup/{ => lib/include}/cgroup_util.h    |   4 +-
 .../testing/selftests/cgroup/lib/libcgroup.mk |  12 +
 tools/testing/selftests/kvm/Makefile.kvm      |   4 +-
 .../selftests/kvm/access_tracking_perf_test.c | 263 ++++++++++--
 .../selftests/kvm/include/lru_gen_util.h      |  51 +++
 .../testing/selftests/kvm/include/test_util.h |   1 +
 .../testing/selftests/kvm/lib/lru_gen_util.c  | 383 ++++++++++++++++++
 tools/testing/selftests/kvm/lib/test_util.c   |  42 +-
 10 files changed, 726 insertions(+), 58 deletions(-)
 rename tools/testing/selftests/cgroup/{ => lib}/cgroup_util.c (99%)
 rename tools/testing/selftests/cgroup/{ => lib/include}/cgroup_util.h (99%)
 create mode 100644 tools/testing/selftests/cgroup/lib/libcgroup.mk
 create mode 100644 tools/testing/selftests/kvm/include/lru_gen_util.h
 create mode 100644 tools/testing/selftests/kvm/lib/lru_gen_util.c

base-commit: 782f9feaa9517caf33186dcdd6b50a8f770ed29b

Signature · 1 line
2.49.0.395.g12beb8f557-goog
#1 lore raw

PATCH 1/5 KVM: selftests: Extract guts of THP accessor to standalone sysfs helpers

From: Sean Christopherson <[email protected]>

Extract the guts of thp_configured() and get_trans_hugepagesz() to standalone helpers so that the core logic can be reused for other sysfs files, e.g. to query numa_balancing.

Opportunistically assert that the initial fscanf() read at least one byte, and add a comment explaining the second call to fscanf().

---
 tools/testing/selftests/kvm/lib/test_util.c | 35 ++++++++++++++-------
 1 file changed, 24 insertions(+), 11 deletions(-)
tools/testing/selftests/kvm/lib/test_util.c +24 -11
index 8ed0b74ae8373..3dc8538f5d696 100644
@@ -132,37 +132,50 @@ void print_skip(const char *fmt, ...)
 	puts(", skipping test");
 }
 
-bool thp_configured(void)
+static bool test_sysfs_path(const char *path)
 {
-	int ret;
 	struct stat statbuf;
+	int ret;
 
-	ret = stat("/sys/kernel/mm/transparent_hugepage", &statbuf);
+	ret = stat(path, &statbuf);
 	TEST_ASSERT(ret == 0 || (ret == -1 && errno == ENOENT),
-		    "Error in stating /sys/kernel/mm/transparent_hugepage");
+		    "Error in stat()ing '%s'", path);
 
 	return ret == 0;
 }
 
-size_t get_trans_hugepagesz(void)
+bool thp_configured(void)
+{
+	return test_sysfs_path("/sys/kernel/mm/transparent_hugepage");
+}
+
+static size_t get_sysfs_val(const char *path)
 {
 	size_t size;
 	FILE *f;
 	int ret;
 
-	TEST_ASSERT(thp_configured(), "THP is not configured in host kernel");
-
-	f = fopen("/sys/kernel/mm/transparent_hugepage/hpage_pmd_size", "r");
-	TEST_ASSERT(f != NULL, "Error in opening transparent_hugepage/hpage_pmd_size");
+	f = fopen(path, "r");
+	TEST_ASSERT(f, "Error opening '%s'", path);
 
 	ret = fscanf(f, "%ld", &size);
+	TEST_ASSERT(ret > 0, "Error reading '%s'", path);
+
+	/* Re-scan the input stream to verify the entire file was read. */
 	ret = fscanf(f, "%ld", &size);
-	TEST_ASSERT(ret < 1, "Error reading transparent_hugepage/hpage_pmd_size");
-	fclose(f);
+	TEST_ASSERT(ret < 1, "Error reading '%s'", path);
 
+	fclose(f);
 	return size;
 }
 
+size_t get_trans_hugepagesz(void)
+{
+	TEST_ASSERT(thp_configured(), "THP is not configured in host kernel");
+
+	return get_sysfs_val("/sys/kernel/mm/transparent_hugepage/hpage_pmd_size");
+}
+
 size_t get_def_hugetlb_pagesz(void)
 {
 	char buf[64];
Signature · 1 line
2.49.0.395.g12beb8f557-goog
#2 lore raw

PATCH 2/5 KVM: selftests: access_tracking_perf_test: Add option to skip the sanity check

In reply to James Houghton

From: Maxim Levitsky <[email protected]>

Add an option to skip sanity check of number of still idle pages, and set it by default to skip, in case hypervisor or NUMA balancing is detected.

---
 .../selftests/kvm/access_tracking_perf_test.c | 61 ++++++++++++++++---
 .../testing/selftests/kvm/include/test_util.h |  1 +
 tools/testing/selftests/kvm/lib/test_util.c   |  7 +++
 3 files changed, 60 insertions(+), 9 deletions(-)
tools/testing/selftests/kvm/access_tracking_perf_test.c +52 -9
index 447e619cf856e..0e594883ec13e 100644
@@ -65,6 +65,16 @@ static int vcpu_last_completed_iteration[KVM_MAX_VCPUS];
 /* Whether to overlap the regions of memory vCPUs access. */
 static bool overlap_memory_access;
 
+/*
+ * If the test should only warn if there are too many idle pages (i.e., it is
+ * expected).
+ * -1: Not yet set.
+ *  0: We do not expect too many idle pages, so FAIL if too many idle pages.
+ *  1: Having too many idle pages is expected, so merely print a warning if
+ *     too many idle pages are found.
+ */
+static int idle_pages_warn_only = -1;
+
 struct test_params {
 	/* The backing source for the region of memory. */
 	enum vm_mem_backing_src_type backing_src;
@@ -177,18 +187,12 @@ static void mark_vcpu_memory_idle(struct kvm_vm *vm,
 	 * arbitrary; high enough that we ensure most memory access went through
 	 * access tracking but low enough as to not make the test too brittle
 	 * over time and across architectures.
-	 *
-	 * When running the guest as a nested VM, "warn" instead of asserting
-	 * as the TLB size is effectively unlimited and the KVM doesn't
-	 * explicitly flush the TLB when aging SPTEs.  As a result, more pages
-	 * are cached and the guest won't see the "idle" bit cleared.
 	 */
 	if (still_idle >= pages / 10) {
-#ifdef __x86_64__
-		TEST_ASSERT(this_cpu_has(X86_FEATURE_HYPERVISOR),
+		TEST_ASSERT(idle_pages_warn_only,
 			    "vCPU%d: Too many pages still idle (%lu out of %lu)",
 			    vcpu_idx, still_idle, pages);
-#endif
+
 		printf("WARNING: vCPU%d: Too many pages still idle (%lu out of %lu), "
 		       "this will affect performance results.\n",
 		       vcpu_idx, still_idle, pages);
@@ -328,6 +332,31 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 	memstress_destroy_vm(vm);
 }
 
+static int access_tracking_unreliable(void)
+{
+#ifdef __x86_64__
+	/*
+	 * When running nested, the TLB size is effectively unlimited and the
+	 * KVM doesn't explicitly flush the TLB when aging SPTEs.  As a result,
+	 * more pages are cached and the guest won't see the "idle" bit cleared.
+	 */
+	if (this_cpu_has(X86_FEATURE_HYPERVISOR)) {
+		puts("Skipping idle page count sanity check, because the test is run nested");
+		return 1;
+	}
+#endif
+	/*
+	 * When NUMA balancing is enabled, guest memory can be mapped
+	 * PROT_NONE, and the Accessed bits won't be queriable.
+	 */
+	if (is_numa_balancing_enabled()) {
+		puts("Skipping idle page count sanity check, because NUMA balancing is enabled");
+		return 1;
+	}
+
+	return 0;
+}
+
 static void help(char *name)
 {
 	puts("");
@@ -342,6 +371,12 @@ static void help(char *name)
 	printf(" -v: specify the number of vCPUs to run.\n");
 	printf(" -o: Overlap guest memory accesses instead of partitioning\n"
 	       "     them into a separate region of memory for each vCPU.\n");
+	printf(" -w: Control whether the test warns or fails if more than 10%\n"
+	       "     of pages are still seen as idle/old after accessing guest\n"
+	       "     memory.  >0 == warn only, 0 == fail, <0 == auto.  For auto\n"
+	       "     mode, the test fails by default, but switches to warn only\n"
+	       "     if NUMA balancing is enabled or the test detects it's running\n"
+	       "     in a VM.\n");
 	backing_src_help("-s");
 	puts("");
 	exit(0);
@@ -359,7 +394,7 @@ int main(int argc, char *argv[])
 
 	guest_modes_append_default();
 
-	while ((opt = getopt(argc, argv, "hm:b:v:os:")) != -1) {
+	while ((opt = getopt(argc, argv, "hm:b:v:os:w:")) != -1) {
 		switch (opt) {
 		case 'm':
 			guest_modes_cmdline(optarg);
@@ -376,6 +411,11 @@ int main(int argc, char *argv[])
 		case 's':
 			params.backing_src = parse_backing_src_type(optarg);
 			break;
+		case 'w':
+			idle_pages_warn_only =
+				atoi_non_negative("Idle pages warning",
+						  optarg);
+			break;
 		case 'h':
 		default:
 			help(argv[0]);
@@ -388,6 +428,9 @@ int main(int argc, char *argv[])
 		       "CONFIG_IDLE_PAGE_TRACKING is not enabled");
 	close(page_idle_fd);
 
+	if (idle_pages_warn_only == -1)
+		idle_pages_warn_only = access_tracking_unreliable();
+
 	for_each_guest_mode(run_test, &params);
 
 	return 0;
tools/testing/selftests/kvm/include/test_util.h +1 -0
index 77d13d7920cb8..c6ef895fbd9ab 100644
@@ -153,6 +153,7 @@ bool is_backing_src_hugetlb(uint32_t i);
 void backing_src_help(const char *flag);
 enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name);
 long get_run_delay(void);
+bool is_numa_balancing_enabled(void);
 
 /*
  * Whether or not the given source type is shared memory (as opposed to
tools/testing/selftests/kvm/lib/test_util.c +7 -0
index 3dc8538f5d696..03eb99af9b8de 100644
@@ -176,6 +176,13 @@ size_t get_trans_hugepagesz(void)
 	return get_sysfs_val("/sys/kernel/mm/transparent_hugepage/hpage_pmd_size");
 }
 
+bool is_numa_balancing_enabled(void)
+{
+	if (!test_sysfs_path("/proc/sys/kernel/numa_balancing"))
+		return false;
+	return get_sysfs_val("/proc/sys/kernel/numa_balancing") == 1;
+}
+
 size_t get_def_hugetlb_pagesz(void)
 {
 	char buf[64];
Signature · 1 line
2.49.0.395.g12beb8f557-goog
Reviewed-by on v1 2/5
#3 lore raw

Re: [PATCH 2/5] KVM: selftests: access_tracking_perf_test: Add option to skip the sanity check

Quoted 59 lines · James Houghton

From: Maxim Levitsky <[email protected]>

Add an option to skip sanity check of number of still idle pages, and set it by default to skip, in case hypervisor or NUMA balancing is detected.

---
 .../selftests/kvm/access_tracking_perf_test.c | 61 ++++++++++++++++---
 .../testing/selftests/kvm/include/test_util.h |  1 +
 tools/testing/selftests/kvm/lib/test_util.c   |  7 +++
 3 files changed, 60 insertions(+), 9 deletions(-)
tools/testing/selftests/kvm/access_tracking_perf_test.c +12 -8
index 447e619cf856e..0e594883ec13e 100644
@@ -65,6 +65,16 @@ static int vcpu_last_completed_iteration[KVM_MAX_VCPUS];
 /* Whether to overlap the regions of memory vCPUs access. */
 static bool overlap_memory_access;
 
+/*
+ * If the test should only warn if there are too many idle pages (i.e., it is
+ * expected).
+ * -1: Not yet set.
+ *  0: We do not expect too many idle pages, so FAIL if too many idle pages.
+ *  1: Having too many idle pages is expected, so merely print a warning if
+ *     too many idle pages are found.
+ */
+static int idle_pages_warn_only = -1;
+
 struct test_params {
 	/* The backing source for the region of memory. */
 	enum vm_mem_backing_src_type backing_src;
@@ -177,18 +187,12 @@ static void mark_vcpu_memory_idle(struct kvm_vm *vm,
 	 * arbitrary; high enough that we ensure most memory access went through
 	 * access tracking but low enough as to not make the test too brittle
 	 * over time and across architectures.
-	 *
-	 * When running the guest as a nested VM, "warn" instead of asserting
-	 * as the TLB size is effectively unlimited and the KVM doesn't
-	 * explicitly flush the TLB when aging SPTEs.  As a result, more pages
-	 * are cached and the guest won't see the "idle" bit cleared.
 	 */
 	if (still_idle >= pages / 10) {
-#ifdef __x86_64__
-		TEST_ASSERT(this_cpu_has(X86_FEATURE_HYPERVISOR),
+		TEST_ASSERT(idle_pages_warn_only,
 			    "vCPU%d: Too many pages still idle (%lu out of %lu)",
 			    vcpu_idx, still_idle, pages);
-#endif
+
 		printf("WARNING: vCPU%d: Too many pages still idle (%lu out of %lu), "
 		       "this will affect performance results.\n",
 		       vcpu_idx, still_idle, pages);
@@ -328,6 +332,31 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 	memstress_destroy_vm(vm);

tools/testing/selftests/kvm/access_tracking_perf_test.c static void run_test(enum vm_guest_mode mode, void *arg) · line 342

Quoted 10 lines
tools/testing/selftests/kvm/access_tracking_perf_test.c +8 -0
 }
 
+static int access_tracking_unreliable(void)
+{
+#ifdef __x86_64__
+	/*
+	 * When running nested, the TLB size is effectively unlimited and the
+	 * KVM doesn't explicitly flush the TLB when aging SPTEs.  As a result,
+	 * more pages are cached and the guest won't see the "idle" bit cleared.
+	 */

Tiny nitpick: nested on KVM, because on other hypervisors it might work differently, but overall most of them probably suffer from the same problem,
so its probably better to say something like that:

'When running nested, the TLB size might be effectively unlimited, for example this is the case when running on top of KVM L0'

tools/testing/selftests/kvm/access_tracking_perf_test.c static void run_test(enum vm_guest_mode mode, void *arg) · line 350

Quoted 8 lines
tools/testing/selftests/kvm/access_tracking_perf_test.c +8 -0
+	if (this_cpu_has(X86_FEATURE_HYPERVISOR)) {
+		puts("Skipping idle page count sanity check, because the test is run nested");
+		return 1;
+	}
+#endif
+	/*
+	 * When NUMA balancing is enabled, guest memory can be mapped
+	 * PROT_NONE, and the Accessed bits won't be queriable.

Tiny nitpick: the accessed bit in this case are lost, because KVM no longer propagates it from secondary to primary paging, and the secondary mapping might be lost due to zapping, and the biggest offender of this is NUMA balancing.

tools/testing/selftests/kvm/access_tracking_perf_test.c static void run_test(enum vm_guest_mode mode, void *arg) · line 358

Quoted 8 lines
tools/testing/selftests/kvm/access_tracking_perf_test.c +8 -0
+	 */
+	if (is_numa_balancing_enabled()) {
+		puts("Skipping idle page count sanity check, because NUMA balancing is enabled");
+		return 1;
+	}
+
+	return 0;
+}

Very good idea of extracting this logic into a function and documenting it.

Quoted 68 lines
tools/testing/selftests/kvm/access_tracking_perf_test.c +16 -1
+
 static void help(char *name)
 {
 	puts("");
@@ -342,6 +371,12 @@ static void help(char *name)
 	printf(" -v: specify the number of vCPUs to run.\n");
 	printf(" -o: Overlap guest memory accesses instead of partitioning\n"
 	       "     them into a separate region of memory for each vCPU.\n");
+	printf(" -w: Control whether the test warns or fails if more than 10%\n"
+	       "     of pages are still seen as idle/old after accessing guest\n"
+	       "     memory.  >0 == warn only, 0 == fail, <0 == auto.  For auto\n"
+	       "     mode, the test fails by default, but switches to warn only\n"
+	       "     if NUMA balancing is enabled or the test detects it's running\n"
+	       "     in a VM.\n");
 	backing_src_help("-s");
 	puts("");
 	exit(0);
@@ -359,7 +394,7 @@ int main(int argc, char *argv[])
 
 	guest_modes_append_default();
 
-	while ((opt = getopt(argc, argv, "hm:b:v:os:")) != -1) {
+	while ((opt = getopt(argc, argv, "hm:b:v:os:w:")) != -1) {
 		switch (opt) {
 		case 'm':
 			guest_modes_cmdline(optarg);
@@ -376,6 +411,11 @@ int main(int argc, char *argv[])
 		case 's':
 			params.backing_src = parse_backing_src_type(optarg);
 			break;
+		case 'w':
+			idle_pages_warn_only =
+				atoi_non_negative("Idle pages warning",
+						  optarg);
+			break;
 		case 'h':
 		default:
 			help(argv[0]);
@@ -388,6 +428,9 @@ int main(int argc, char *argv[])
 		       "CONFIG_IDLE_PAGE_TRACKING is not enabled");
 	close(page_idle_fd);
 
+	if (idle_pages_warn_only == -1)
+		idle_pages_warn_only = access_tracking_unreliable();
+
 	for_each_guest_mode(run_test, &params);
 
 	return 0;
tools/testing/selftests/kvm/include/test_util.h +1 -0
index 77d13d7920cb8..c6ef895fbd9ab 100644
@@ -153,6 +153,7 @@ bool is_backing_src_hugetlb(uint32_t i);
 void backing_src_help(const char *flag);
 enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name);
 long get_run_delay(void);
+bool is_numa_balancing_enabled(void);
 
 /*
  * Whether or not the given source type is shared memory (as opposed to
tools/testing/selftests/kvm/lib/test_util.c +0 -0
index 3dc8538f5d696..03eb99af9b8de 100644
@@ -176,6 +176,13 @@ size_t get_trans_hugepagesz(void)
 	return get_sysfs_val("/sys/kernel/mm/transparent_hugepage/hpage_pmd_size");
 }
 

tools/testing/selftests/kvm/lib/test_util.c size_t get_trans_hugepagesz(void) · line 188

Quoted 10 lines
tools/testing/selftests/kvm/lib/test_util.c +7 -0
+bool is_numa_balancing_enabled(void)
+{
+	if (!test_sysfs_path("/proc/sys/kernel/numa_balancing"))
+		return false;
+	return get_sysfs_val("/proc/sys/kernel/numa_balancing") == 1;
+}
+
 size_t get_def_hugetlb_pagesz(void)
 {
 	char buf[64];

Best regards,
Maxim Levitsky

#4 lore raw

Re: [PATCH 2/5] KVM: selftests: access_tracking_perf_test: Add option to skip the sanity check

Quoted 75 lines · Maxim Levitsky
Quoted 59 lines · James Houghton

From: Maxim Levitsky <[email protected]>

Add an option to skip sanity check of number of still idle pages, and set it by default to skip, in case hypervisor or NUMA balancing is detected.

---
 .../selftests/kvm/access_tracking_perf_test.c | 61 ++++++++++++++++--- .../testing/selftests/kvm/include/test_util.h |  1 + tools/testing/selftests/kvm/lib/test_util.c   |  7 +++

 3 files changed, 60 insertions(+), 9 deletions(-)
tools/testing/selftests/kvm/access_tracking_perf_test.c +12 -8
index 447e619cf856e..0e594883ec13e 100644
@@ -65,6 +65,16 @@ static int vcpu_last_completed_iteration[KVM_MAX_VCPUS];
  /* Whether to overlap the regions of memory vCPUs access. */
  static bool overlap_memory_access;
 
+/*
+ * If the test should only warn if there are too many idle pages (i.e., it is
+ * expected).
+ * -1: Not yet set.
+ *  0: We do not expect too many idle pages, so FAIL if too many idle pages.
+ *  1: Having too many idle pages is expected, so merely print a warning if
+ *     too many idle pages are found.
+ */
+static int idle_pages_warn_only = -1;
+
  struct test_params {
       /* The backing source for the region of memory. */
       enum vm_mem_backing_src_type backing_src;
@@ -177,18 +187,12 @@ static void mark_vcpu_memory_idle(struct kvm_vm *vm,
        * arbitrary; high enough that we ensure most memory access went through
        * access tracking but low enough as to not make the test too brittle
        * over time and across architectures.
-      *
-      * When running the guest as a nested VM, "warn" instead of asserting
-      * as the TLB size is effectively unlimited and the KVM doesn't
-      * explicitly flush the TLB when aging SPTEs.  As a result, more pages
-      * are cached and the guest won't see the "idle" bit cleared.
        */
       if (still_idle >= pages / 10) {
-#ifdef __x86_64__
-             TEST_ASSERT(this_cpu_has(X86_FEATURE_HYPERVISOR),
+             TEST_ASSERT(idle_pages_warn_only,
                           "vCPU%d: Too many pages still idle (%lu out of %lu)",
                           vcpu_idx, still_idle, pages);
-#endif
+

              printf("WARNING: vCPU%d: Too many pages still idle (%lu out of %lu), " "this will affect performance results.\n", vcpu_idx, still_idle, pages);

tools/testing/selftests/kvm/access_tracking_perf_test.c +0 -0
@@ -328,6 +332,31 @@ static void run_test(enum vm_guest_mode mode, void *arg)
       memstress_destroy_vm(vm);

tools/testing/selftests/kvm/access_tracking_perf_test.c static void run_test(enum vm_guest_mode mode, void *arg) · line 342

Quoted 10 lines
tools/testing/selftests/kvm/access_tracking_perf_test.c +8 -0
  }
 
+static int access_tracking_unreliable(void)
+{
+#ifdef __x86_64__
+     /*
+      * When running nested, the TLB size is effectively unlimited and the
+      * KVM doesn't explicitly flush the TLB when aging SPTEs.  As a result,
+      * more pages are cached and the guest won't see the "idle" bit cleared.
+      */

Tiny nitpick: nested on KVM, because on other hypervisors it might work differently, but overall most of them probably suffer from the same problem,
so its probably better to say something like that:

Quoted 2 lines

'When running nested, the TLB size might be effectively unlimited, for example this is the case when running on top of KVM L0'

Added this clarification (see below), thanks!

This wording was left as-is from before, but I agree that it could be better.

Quoted 11 lines

tools/testing/selftests/kvm/access_tracking_perf_test.c static void run_test(enum vm_guest_mode mode, void *arg) · line 350

Quoted 10 lines
tools/testing/selftests/kvm/access_tracking_perf_test.c +8 -0
+     if (this_cpu_has(X86_FEATURE_HYPERVISOR)) {
+             puts("Skipping idle page count sanity check, because the test is run nested");
+             return 1;
+     }
+#endif
+     /*
+      * When NUMA balancing is enabled, guest memory can be mapped
+      * PROT_NONE, and the Accessed bits won't be queriable.
Quoted 3 lines

Tiny nitpick: the accessed bit in this case are lost, because KVM no longer propagates it from secondary to primary paging, and the secondary mapping might be lost due to zapping, and the biggest offender of this is NUMA balancing.

Reworded this bit. The current wording is indeed misleading.

Quoted 10 lines

tools/testing/selftests/kvm/access_tracking_perf_test.c static void run_test(enum vm_guest_mode mode, void *arg) · line 358

Quoted 8 lines
tools/testing/selftests/kvm/access_tracking_perf_test.c +8 -0
+      */
+     if (is_numa_balancing_enabled()) {
+             puts("Skipping idle page count sanity check, because NUMA balancing is enabled");
+             return 1;
+     }
+
+     return 0;
+}

Very good idea of extracting this logic into a function and documenting it.

:)

Quoted 1 line

Thanks, though I've already included your Signed-off-by. I'll just keep your Signed-off-by and From:.

I'm applying the following diff for the next version:

tools/testing/selftests/kvm/access_tracking_perf_test.c +6 -5
index 0e594883ec13e..1770998c7675b 100644
@@ -336,9 +336,10 @@ static int access_tracking_unreliable(void)
 {
 #ifdef __x86_64__
 	/*
-	 * When running nested, the TLB size is effectively unlimited and the
-	 * KVM doesn't explicitly flush the TLB when aging SPTEs.  As a result,
-	 * more pages are cached and the guest won't see the "idle" bit cleared.
+	 * When running nested, the TLB size may be effectively unlimited (for
+	 * example, this is the case when running on KVM L0), and KVM doesn't
+	 * explicitly flush the TLB when aging SPTEs.  As a result, more pages
+	 * are cached and the guest won't see the "idle" bit cleared.
 	 */
 	if (this_cpu_has(X86_FEATURE_HYPERVISOR)) {
 		puts("Skipping idle page count sanity check, because the test is run nested");
@@ -346,8 +347,8 @@ static int access_tracking_unreliable(void)
 	}
 #endif
 	/*
-	 * When NUMA balancing is enabled, guest memory can be mapped
-	 * PROT_NONE, and the Accessed bits won't be queriable.
+	 * When NUMA balancing is enabled, guest memory will be unmapped to get
+	 * NUMA faults, dropping the Accessed bits.
 	 */
 	if (is_numa_balancing_enabled()) {
 		puts("Skipping idle page count sanity check, because NUMA balancing is enabled");
#5 lore raw

PATCH 3/5 cgroup: selftests: Move cgroup_util into its own library

In reply to James Houghton

KVM selftests will soon need to use some of the cgroup creation and deletion functionality from cgroup_util.

---
 tools/testing/selftests/cgroup/Makefile       | 21 ++++++++++---------
 .../selftests/cgroup/{ => lib}/cgroup_util.c  |  3 +--
 .../cgroup/{ => lib/include}/cgroup_util.h    |  4 ++--
 .../testing/selftests/cgroup/lib/libcgroup.mk | 12 +++++++++++
 4 files changed, 26 insertions(+), 14 deletions(-)
 rename tools/testing/selftests/cgroup/{ => lib}/cgroup_util.c (99%)
 rename tools/testing/selftests/cgroup/{ => lib/include}/cgroup_util.h (99%)
 create mode 100644 tools/testing/selftests/cgroup/lib/libcgroup.mk
tools/testing/selftests/cgroup/Makefile +11 -10
index 1b897152bab6e..e01584c2189ac 100644
@@ -21,14 +21,15 @@ TEST_GEN_PROGS += test_zswap
 LOCAL_HDRS += $(selfdir)/clone3/clone3_selftests.h $(selfdir)/pidfd/pidfd.h
 
 include ../lib.mk
+include lib/libcgroup.mk
 
-$(OUTPUT)/test_core: cgroup_util.c
-$(OUTPUT)/test_cpu: cgroup_util.c
-$(OUTPUT)/test_cpuset: cgroup_util.c
-$(OUTPUT)/test_freezer: cgroup_util.c
-$(OUTPUT)/test_hugetlb_memcg: cgroup_util.c
-$(OUTPUT)/test_kill: cgroup_util.c
-$(OUTPUT)/test_kmem: cgroup_util.c
-$(OUTPUT)/test_memcontrol: cgroup_util.c
-$(OUTPUT)/test_pids: cgroup_util.c
-$(OUTPUT)/test_zswap: cgroup_util.c
+$(OUTPUT)/test_core: $(LIBCGROUP_O)
+$(OUTPUT)/test_cpu: $(LIBCGROUP_O)
+$(OUTPUT)/test_cpuset: $(LIBCGROUP_O)
+$(OUTPUT)/test_freezer: $(LIBCGROUP_O)
+$(OUTPUT)/test_hugetlb_memcg: $(LIBCGROUP_O)
+$(OUTPUT)/test_kill: $(LIBCGROUP_O)
+$(OUTPUT)/test_kmem: $(LIBCGROUP_O)
+$(OUTPUT)/test_memcontrol: $(LIBCGROUP_O)
+$(OUTPUT)/test_pids: $(LIBCGROUP_O)
+$(OUTPUT)/test_zswap: $(LIBCGROUP_O)
tools/testing/selftests/cgroup/lib/cgroup_util.c renamed from tools/testing/selftests/cgroup/cgroup_util.c +1 -2
similarity index 99%
rename from tools/testing/selftests/cgroup/cgroup_util.c
rename to tools/testing/selftests/cgroup/lib/cgroup_util.c
index 1e2d46636a0ca..d5649486a11df 100644
@@ -16,8 +16,7 @@
 #include <sys/wait.h>
 #include <unistd.h>
 
-#include "cgroup_util.h"
-#include "../clone3/clone3_selftests.h"
+#include <cgroup_util.h>
 
 /* Returns read len on success, or -errno on failure. */
 static ssize_t read_text(const char *path, char *buf, size_t max_len)
tools/testing/selftests/cgroup/lib/include/cgroup_util.h renamed from tools/testing/selftests/cgroup/cgroup_util.h +2 -2
similarity index 99%
rename from tools/testing/selftests/cgroup/cgroup_util.h
rename to tools/testing/selftests/cgroup/lib/include/cgroup_util.h
index 19b131ee77072..7a0441e5eb296 100644
@@ -2,9 +2,9 @@
 #include <stdbool.h>
 #include <stdlib.h>
 
-#include "../kselftest.h"
-
+#ifndef PAGE_SIZE
 #define PAGE_SIZE 4096
+#endif
 
 #define MB(x) (x << 20)
 
tools/testing/selftests/cgroup/lib/libcgroup.mk new file +12 -0
new file mode 100644
index 0000000000000..2cbf07337c23f
@@ -0,0 +1,12 @@
+CGROUP_DIR := $(selfdir)/cgroup
+
+LIBCGROUP_C := lib/cgroup_util.c
+
+LIBCGROUP_O := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBCGROUP_C))
+
+CFLAGS += -I$(CGROUP_DIR)/lib/include
+
+$(LIBCGROUP_O): $(OUTPUT)/%.o : $(CGROUP_DIR)/%.c
+	$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
+
+EXTRA_CLEAN += $(LIBCGROUP_O)
Signature · 1 line
2.49.0.395.g12beb8f557-goog
maintainer
#6 lore raw

Re: [PATCH 3/5] cgroup: selftests: Move cgroup_util into its own library

Hello James.

Quoted 2 lines · James Houghton

KVM selftests will soon need to use some of the cgroup creation and deletion functionality from cgroup_util.

Thanks, I think cross-selftest sharing is better than duplicating similar code.

+Cc: Yafang as it may worth porting/unifying with
tools/testing/selftests/bpf/cgroup_helpers.h too

tools/testing/selftests/cgroup/lib/cgroup_util.c line 19

Quoted 9 lines
tools/testing/selftests/cgroup/lib/cgroup_util.c renamed from tools/testing/selftests/cgroup/cgroup_util.c +1 -2
@@ -16,8 +16,7 @@
 #include <sys/wait.h>
 #include <unistd.h>
 
-#include "cgroup_util.h"
-#include "../clone3/clone3_selftests.h"
+#include <cgroup_util.h>

The clone3_selftests.h header is not needed anymore?

Michal

#7 lore raw

Re: [PATCH 3/5] cgroup: selftests: Move cgroup_util into its own library

Quoted 19 lines · Michal Koutný

Hello James.

Quoted 2 lines · James Houghton

KVM selftests will soon need to use some of the cgroup creation and deletion functionality from cgroup_util.

Thanks, I think cross-selftest sharing is better than duplicating similar code.

+Cc: Yafang as it may worth porting/unifying with
tools/testing/selftests/bpf/cgroup_helpers.h too

Quoted 6 lines
tools/testing/selftests/cgroup/lib/cgroup_util.c renamed from tools/testing/selftests/cgroup/cgroup_util.c +0 -0
@@ -16,8 +16,7 @@
  #include <sys/wait.h>
  #include <unistd.h>
 
Quoted 5 lines

tools/testing/selftests/cgroup/lib/cgroup_util.c line 19

Quoted 3 lines
tools/testing/selftests/cgroup/lib/cgroup_util.c renamed from tools/testing/selftests/cgroup/cgroup_util.c +1 -2
-#include "cgroup_util.h"
-#include "../clone3/clone3_selftests.h"
+#include <cgroup_util.h>

The clone3_selftests.h header is not needed anymore?

Ah, sorry.

We do indeed still reference sys_clone3() from cgroup_util.c, so it should stay in (as "../../clone3/clone3_selftests.h"). I realize now that it compiled just fine because the call to sys_clone3() is dropped entirely when clone3_selftests.h is not included.

So I'll apply the following diff:

tools/testing/selftests/cgroup/lib/cgroup_util.c +2 -0
index d5649486a11df..fe15541f3a07d 100644
@@ -18,6 +18,8 @@
 
 #include <cgroup_util.h>
 
+#include "../../clone3/clone3_selftests.h"
+
 /* Returns read len on success, or -errno on failure. */
 static ssize_t read_text(const char *path, char *buf, size_t max_len)
 {
tools/testing/selftests/cgroup/lib/libcgroup.mk +3 -1
index 2cbf07337c23f..12323041a5ce6 100644
@@ -6,7 +6,9 @@ LIBCGROUP_O := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBCGROUP_C))
 
 CFLAGS += -I$(CGROUP_DIR)/lib/include
 
-$(LIBCGROUP_O): $(OUTPUT)/%.o : $(CGROUP_DIR)/%.c
+EXTRA_HDRS := $(selfdir)/clone3/clone3_selftests.h
+
+$(LIBCGROUP_O): $(OUTPUT)/%.o : $(CGROUP_DIR)/%.c $(EXTRA_HDRS)
 	$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
 
 EXTRA_CLEAN += $(LIBCGROUP_O)
#8 lore raw

Re: [PATCH 3/5] cgroup: selftests: Move cgroup_util into its own library

In reply to Michal Koutný
Quoted 4 lines · Michal Koutný

Hello James.

On Thu, Mar 27, 2025 at 01:23:48AM +0000, James Houghton <[email protected]> wrote:

Quoted 8 lines
Quoted 2 lines

KVM selftests will soon need to use some of the cgroup creation and deletion functionality from cgroup_util.

Thanks, I think cross-selftest sharing is better than duplicating similar code.

+Cc: Yafang as it may worth porting/unifying with
tools/testing/selftests/bpf/cgroup_helpers.h too

Thanks for pointing that out—that’s a good suggestion. We should also consider migrating the bpf cgroup helpers into the new libcgroup.

--
Regards
Yafang

#9 lore raw

PATCH 4/5 KVM: selftests: Build and link selftests/cgroup/lib into KVM selftests

In reply to James Houghton

libcgroup.o is built separately from KVM selftests and cgroup selftests, so different compiler flags used by the different selftests will not conflict with each other.

---
 tools/testing/selftests/kvm/Makefile.kvm | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/Makefile.kvm +2 -1
index f773f8f992494..c86a680f52b28 100644
@@ -219,6 +219,7 @@ OVERRIDE_TARGETS = 1
 # importantly defines, i.e. overwrites, $(CC) (unless `make -e` or `make CC=`,
 # which causes the environment variable to override the makefile).
 include ../lib.mk
+include ../cgroup/lib/libcgroup.mk
 
 INSTALL_HDR_PATH = $(top_srcdir)/usr
 LINUX_HDR_PATH = $(INSTALL_HDR_PATH)/include/
@@ -272,7 +273,7 @@ LIBKVM_S := $(filter %.S,$(LIBKVM))
 LIBKVM_C_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_C))
 LIBKVM_S_OBJ := $(patsubst %.S, $(OUTPUT)/%.o, $(LIBKVM_S))
 LIBKVM_STRING_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_STRING))
-LIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) $(LIBKVM_STRING_OBJ)
+LIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) $(LIBKVM_STRING_OBJ) $(LIBCGROUP_O)
 SPLIT_TEST_GEN_PROGS := $(patsubst %, $(OUTPUT)/%, $(SPLIT_TESTS))
 SPLIT_TEST_GEN_OBJ := $(patsubst %, $(OUTPUT)/$(ARCH)/%.o, $(SPLIT_TESTS))
 
Signature · 1 line
2.49.0.395.g12beb8f557-goog
#10 lore raw

PATCH 5/5 KVM: selftests: access_tracking_perf_test: Use MGLRU for access tracking

In reply to James Houghton

By using MGLRU's debugfs for invoking test_young() and clear_young(), we avoid page_idle's incompatibility with MGLRU, and we can mark pages as idle (clear_young()) much faster.

The ability to use page_idle is left in, as it is useful for kernels that do not have MGLRU built in. If MGLRU is enabled but is not usable (e.g. we can't access the debugfs mount), the test will fail, as page_idle is not compatible with MGLRU.

cgroup utility functions have been borrowed so that, when running with MGLRU, we can create a memcg in which to run our test.

Other MGLRU-debugfs-specific parsing code has been added to lru_gen_util.{c,h}.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/access_tracking_perf_test.c | 208 ++++++++--
 .../selftests/kvm/include/lru_gen_util.h      |  51 +++
 .../testing/selftests/kvm/lib/lru_gen_util.c  | 383 ++++++++++++++++++
 4 files changed, 617 insertions(+), 26 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/include/lru_gen_util.h
 create mode 100644 tools/testing/selftests/kvm/lib/lru_gen_util.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index c86a680f52b28..6ab0441238a7f 100644
@@ -8,6 +8,7 @@ LIBKVM += lib/elf.c
 LIBKVM += lib/guest_modes.c
 LIBKVM += lib/io.c
 LIBKVM += lib/kvm_util.c
+LIBKVM += lib/lru_gen_util.c
 LIBKVM += lib/memstress.c
 LIBKVM += lib/guest_sprintf.c
 LIBKVM += lib/rbtree.c
tools/testing/selftests/kvm/access_tracking_perf_test.c +182 -26

313 lines not on this page · raw message

tools/testing/selftests/kvm/include/lru_gen_util.h new file +51 -0

51 lines not on this page · raw message

tools/testing/selftests/kvm/lib/lru_gen_util.c new file +383 -0

383 lines not on this page · raw message

Signature · 1 line
2.49.0.395.g12beb8f557-goog
#11 lore raw

Re: [PATCH 5/5] KVM: selftests: access_tracking_perf_test: Use MGLRU for access tracking

tools/testing/selftests/kvm/access_tracking_perf_test.c static void run_test(enum vm_guest_mode mode, void *arg) · line 421

Quoted 12 lines · James Houghton
tools/testing/selftests/kvm/access_tracking_perf_test.c +4 -0
index 0e594883ec13e..1c8e43e18e4c6 100644
@@ -318,6 +415,15 @@ static void run_test(enum vm_guest_mode mode, void *arg)
        pr_info("\n");
        access_memory(vm, nr_vcpus, ACCESS_WRITE, "Populating memory");
 
+       if (use_lru_gen) {
+               struct memcg_stats stats;
+
+               /* Do an initial page table scan */

This comment is wrong, sorry. I'll just drop it.

I initially had a lru_gen_do_aging() here to verify that everything was tracked in the lru_gen debugfs, but everything is already tracked anyway. Doing an aging pass here means that the "control" write after this is writing to idle memory, so it ceases to be a control.

Quoted 8 lines
tools/testing/selftests/kvm/access_tracking_perf_test.c +5 -0
+               lru_gen_read_memcg_stats(&stats, TEST_MEMCG_NAME);
+               TEST_ASSERT(lru_gen_sum_memcg_stats(&stats) >= total_pages,
+                           "Not all pages accounted for. Was the memcg set up correctly?");
+       }
+
        /* As a control, read and write to the populated memory first. */
        access_memory(vm, nr_vcpus, ACCESS_WRITE, "Writing to populated memory");
        access_memory(vm, nr_vcpus, ACCESS_READ, "Reading from populated memory");