Skip to contentSkip to conversation

test driver to analyse vmalloc allocator

RFC PATCH Superseded Uladzislau Rezki (Sony) 4 participants 11 messages Nov 13, 2018 – Nov 15, 2018

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Uladzislau Rezki (Sony)

author maintainer
#0 lore raw

cover test driver to analyse vmalloc allocator

Hello.

As an outcome of https://lkml.org/lkml/2018/10/19/786 discussion there was an interest in stress/performance test suite. It was developed to analyse a vmalloc allocator from performance, stability point of view and compare the new approach with current one.

I have explained in the commit message in detail how to use this test driver, so please have look at: vmalloc: add test driver to analyse vmalloc allocator

I think it is pretty easy and handy to use. I am not sure if i need to create kind of run.sh or vmalloc.sh in tools/testing/selftests/ to configure the test module over misc device or so to apply different configurations and trigger the test.

This driver uses one internal function that is not accessible from the kernel module, thus as a workaround i use kallsyms_lookup_name("__vmalloc_node_range") to find the symbol.

Also, i need to mention one thing here this test suite allowed me to identify some issues in current design. So please refer to the link i pointed above.

Uladzislau Rezki (Sony) (1):
vmalloc: add test driver to analyse vmalloc allocator

 lib/Kconfig.debug  |  12 ++
 lib/Makefile       |   1 +
 lib/test_vmalloc.c | 543 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 556 insertions(+)
 create mode 100644 lib/test_vmalloc.c
Signature · 1 line
2.11.0
author maintainer
#1 lore raw

RFC PATCH 1/1 vmalloc: add test driver to analyse vmalloc allocator

This adds a new kernel module for analysis of vmalloc allocator. It is only enabled as a module. There are two main reasons this module should be used for. Those are performance evaluation and stressing of vmalloc subsystem.

It consists of several test cases. As of now there are 8. The module has four parameters we can specify, therefore change the behaviour.

1) run_test_mask - set of tests to be run

0 fix_size_alloc_test
1 full_fit_alloc_test
2 long_busy_list_alloc_test
3 random_size_alloc_test
4 fix_align_alloc_test
5 random_size_align_alloc_test
6 align_shift_alloc_test
7 pcpu_alloc_test

By default all tests are in run test mask. If you want to select some specific tests it is possible to pass the mask. For example for first, second and fourth tests we go with (1 << 0 | 1 << 1 | 1 << 3) that is 11 value.

2) test_repeat_count - how many times each test should be repeated By default it is one time per test. It is possible to pass any number. As high the value is the test duration gets increased.

3) single_cpu_test - use one CPU to run the tests
By default this parameter is set to false. It means that all online CPUs execute tests. By setting it to 1, the tests are executed by first online CPU only.

4) sequential_test_order - run tests in sequential order
By default this parameter is set to false. It means that before running tests the order is shuffled. It is possible to make it sequential, just set it to 1.

Performance analysis:
In order to evaluate performance of vmalloc allocations, usually it makes sense to use only one CPU that runs tests, use sequential order, number of repeat tests can be different as well as set of test mask.

For example if we want to run all tests, to use one CPU and repeat each test 3 times. Insert the module passing following parameters:

single_cpu_test=1 sequential_test_order=1 test_repeat_count=3

with following output:

<snip>
Summary: fix_size_alloc_test 3 passed, 0 failed, test_count: 3, average: 918249 usec
Summary: full_fit_alloc_test 3 passed, 0 failed, test_count: 3, average: 1046232 usec
Summary: long_busy_list_alloc_test 3 passed, 0 failed, test_count: 3, average: 12000280 usec
Summary: random_size_alloc_test 3 passed, 0 failed, test_count: 3, average: 6184357 usec
Summary: fix_align_alloc_test 3 passed, 0 failed, test_count: 3, average: 2319067 usec
Summary: random_size_align_alloc_test 3 passed, 0 failed, test_count: 3, average: 2858425 usec
Summary: align_shift_alloc_test 0 passed, 3 failed, test_count: 3, average: 373 usec
Summary: pcpu_alloc_test 3 passed, 0 failed, test_count: 3, average: 93407 usec All test took CPU0=197829986888 cycles
<snip>

The align_shift_alloc_test is expected to be failed.

Stressing:
In order to stress the vmalloc subsystem we run all available test cases on all available CPUs simultaneously. In order to prevent constant behaviour pattern, the test cases array is shuffled by default to randomize the order of test execution.

For example if we want to run all tests(default), use all online CPUs(default) with shuffled order(default) and to repeat each test 30 times. The command would be like:

modprobe vmalloc_test test_repeat_count=30

Expected results are the system is alive, there are no any BUG_ONs or Kernel Panics the tests are completed, no memory leaks.

---
 lib/Kconfig.debug  |  12 ++
 lib/Makefile       |   1 +
 lib/test_vmalloc.c | 546 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 559 insertions(+)
 create mode 100644 lib/test_vmalloc.c
lib/Kconfig.debug +12 -0
index 8838d1158d19..ca8a1a55d777 100644
@@ -1851,6 +1851,18 @@ config TEST_LKM
 
 	  If unsure, say N.
 
+config TEST_VMALLOC
+	tristate "Test module for stress/performance analysis of vmalloc allocator"
+	default n
+	depends on m
+	help
+	  This builds the "vmalloc_test" module that should be used for
+	  stress and performance analysis. So, any new change for vmalloc
+	  subsystem can be evaluated from performance and stability point
+	  of view.
+
+	  If unsure, say N.
+
 config TEST_USER_COPY
 	tristate "Test user/kernel boundary protections"
 	default n
lib/Makefile +1 -0
index 90dc5520b784..2caa6161b417 100644
@@ -57,6 +57,7 @@ UBSAN_SANITIZE_test_ubsan.o := y
 obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o
 obj-$(CONFIG_TEST_LIST_SORT) += test_list_sort.o
 obj-$(CONFIG_TEST_LKM) += test_module.o
+obj-$(CONFIG_TEST_VMALLOC) += test_vmalloc.o
 obj-$(CONFIG_TEST_OVERFLOW) += test_overflow.o
 obj-$(CONFIG_TEST_RHASHTABLE) += test_rhashtable.o
 obj-$(CONFIG_TEST_SORT) += test_sort.o
lib/test_vmalloc.c new file +546 -0

546 lines not on this page · raw message

Signature · 1 line
2.11.0
maintainer
#2 lore raw

Re: [RFC PATCH 1/1] vmalloc: add test driver to analyse vmalloc allocator

Quoted 70 lines · On Tue, 13 Nov 2018 16:16:29 +0100 "Uladzislau Rezki (Sony)"…

This adds a new kernel module for analysis of vmalloc allocator. It is only enabled as a module. There are two main reasons this module should be used for. Those are performance evaluation and stressing of vmalloc subsystem.

It consists of several test cases. As of now there are 8. The module has four parameters we can specify, therefore change the behaviour.

1) run_test_mask - set of tests to be run

0 fix_size_alloc_test
1 full_fit_alloc_test
2 long_busy_list_alloc_test
3 random_size_alloc_test
4 fix_align_alloc_test
5 random_size_align_alloc_test
6 align_shift_alloc_test
7 pcpu_alloc_test

By default all tests are in run test mask. If you want to select some specific tests it is possible to pass the mask. For example for first, second and fourth tests we go with (1 << 0 | 1 << 1 | 1 << 3) that is 11 value.

2) test_repeat_count - how many times each test should be repeated By default it is one time per test. It is possible to pass any number. As high the value is the test duration gets increased.

3) single_cpu_test - use one CPU to run the tests
By default this parameter is set to false. It means that all online CPUs execute tests. By setting it to 1, the tests are executed by first online CPU only.

4) sequential_test_order - run tests in sequential order
By default this parameter is set to false. It means that before running tests the order is shuffled. It is possible to make it sequential, just set it to 1.

Performance analysis:
In order to evaluate performance of vmalloc allocations, usually it makes sense to use only one CPU that runs tests, use sequential order, number of repeat tests can be different as well as set of test mask.

For example if we want to run all tests, to use one CPU and repeat each test 3 times. Insert the module passing following parameters:

single_cpu_test=1 sequential_test_order=1 test_repeat_count=3

with following output:

<snip>
Summary: fix_size_alloc_test 3 passed, 0 failed, test_count: 3, average: 918249 usec
Summary: full_fit_alloc_test 3 passed, 0 failed, test_count: 3, average: 1046232 usec
Summary: long_busy_list_alloc_test 3 passed, 0 failed, test_count: 3, average: 12000280 usec
Summary: random_size_alloc_test 3 passed, 0 failed, test_count: 3, average: 6184357 usec
Summary: fix_align_alloc_test 3 passed, 0 failed, test_count: 3, average: 2319067 usec
Summary: random_size_align_alloc_test 3 passed, 0 failed, test_count: 3, average: 2858425 usec
Summary: align_shift_alloc_test 0 passed, 3 failed, test_count: 3, average: 373 usec
Summary: pcpu_alloc_test 3 passed, 0 failed, test_count: 3, average: 93407 usec All test took CPU0=197829986888 cycles
<snip>

The align_shift_alloc_test is expected to be failed.

Stressing:
In order to stress the vmalloc subsystem we run all available test cases on all available CPUs simultaneously. In order to prevent constant behaviour pattern, the test cases array is shuffled by default to randomize the order of test execution.

Quoted 9 lines

For example if we want to run all tests(default), use all online CPUs(default) with shuffled order(default) and to repeat each test 30 times. The command would be like:

modprobe vmalloc_test test_repeat_count=30

Expected results are the system is alive, there are no any BUG_ONs or Kernel Panics the tests are completed, no memory leaks.

Seems useful.

Yes, there are plenty of scripts in tools/testing/selftests which load a kernel module for the testing so a vmalloc test under tools/testing/selftests/vm would be appropriate.

Generally the tests under tools/testing/selftests are for testing userspace-visible interfaces, and generally linux-specific ones. But that doesn't mean that we shouldn't add tests for internal functionality.

Quoted 48 lines

...

lib/test_vmalloc.c new file +45 -0
+static int test_func(void *private)
+{
+	struct test_driver *t = private;
+	cpumask_t newmask = CPU_MASK_NONE;
+	int random_array[ARRAY_SIZE(test_case_array)];
+	int index, repeat, i, j, ret;
+	ktime_t kt;
+
+	cpumask_set_cpu(t->cpu, &newmask);
+	set_cpus_allowed_ptr(current, &newmask);
+
+	atomic_inc(&tests_running);
+	wait_for_completion(&completion1);
+
+	for (i = 0; i < ARRAY_SIZE(test_case_array); i++)
+		random_array[i] = i;
+
+	if (!sequential_test_order)
+		shuffle_array(random_array, ARRAY_SIZE(test_case_array));
+
+	t->start = get_cycles();
+	for (i = 0; i < ARRAY_SIZE(test_case_array); i++) {
+		index = random_array[i];
+
+		/*
+		 * Skip tests if run_test_mask has been specified.
+		 */
+		if (!((run_test_mask & (1 << index)) >> index))
+			continue;
+
+		repeat = per_cpu_test_data[t->cpu][index].test_count;
+
+		kt = ktime_get();
+		for (j = 0; j < repeat; j++) {
+			ret = test_case_array[index].test_func();
+			if (!ret)
+				per_cpu_test_data[t->cpu][index].test_passed++;
+			else
+				per_cpu_test_data[t->cpu][index].test_failed++;
+		}
+
+		/*
+		 * Take an average time that test took.
+		 */
+		per_cpu_test_data[t->cpu][index].time =

lib/test_vmalloc.c line 419

Quoted 10 lines
lib/test_vmalloc.c new file +10 -0
+			ktime_us_delta(ktime_get(), kt) / repeat;
+	}
+	t->stop = get_cycles();
+
+	atomic_inc(&phase1_complete);
+	wait_for_completion(&completion2);
+
+	atomic_dec(&tests_running);
+	set_current_state(TASK_UNINTERRUPTIBLE);
+	schedule();

This looks odd. What causes this thread to wake up again?

Quoted 19 lines
lib/test_vmalloc.c new file +16 -0
+	return 0;
+}
+
 
...
 
+	if (single_cpu_test) {
+		cpumask_clear(&cpus_run_test_mask);
+
+		cpumask_set_cpu(cpumask_first(cpu_online_mask),
+			&cpus_run_test_mask);
+	}
+
+	for_each_cpu(cpu, &cpus_run_test_mask) {
+		struct test_driver *t = &per_cpu_test_driver[cpu];
+
+		t->cpu = cpu;
+		t->task = kthread_run(test_func, t, "test%d", cpu);
+		if (IS_ERR(t->task)) {

lib/test_vmalloc.c line 483

Quoted 10 lines
lib/test_vmalloc.c new file +10 -0
+			pr_err("Failed to start test func\n");
+			return;
+		}
+	}
+
+	/* Wait till all processes are running */
+	while (atomic_read(&tests_running) <
+			cpumask_weight(&cpus_run_test_mask)) {
+		set_current_state(TASK_UNINTERRUPTIBLE);
+		schedule_timeout(10);

schedule_timeout_interruptible(). Or, better, plain old msleep().

lib/test_vmalloc.c line 491

Quoted 8 lines
lib/test_vmalloc.c new file +8 -0
+	}
+	complete_all(&completion1);
+
+	/* Wait till all processes have completed phase 1 */
+	while (atomic_read(&phase1_complete) <
+			cpumask_weight(&cpus_run_test_mask)) {
+		set_current_state(TASK_UNINTERRUPTIBLE);
+		schedule_timeout(10);

Ditto.

Quoted 22 lines
lib/test_vmalloc.c new file +22 -0
+	}
+	complete_all(&completion2);
+
+	while (atomic_read(&tests_running)) {
+		set_current_state(TASK_UNINTERRUPTIBLE);
+		schedule_timeout(10);
+	}
+
+	for_each_cpu(cpu, &cpus_run_test_mask) {
+		struct test_driver *t = &per_cpu_test_driver[cpu];
+		int i;
+
+		kthread_stop(t->task);
+
+		for (i = 0; i < ARRAY_SIZE(test_case_array); i++) {
+			if (!((run_test_mask & (1 << i)) >> i))
+				continue;
+
+			pr_info(
+				"Summary: %s %d passed, %d failed, test_count: %d, average: %llu usec\n",
+				test_case_array[i].test_name,
+				per_cpu_test_data[cpu][i].test_passed,

lib/test_vmalloc.c line 523

Quoted 10 lines
lib/test_vmalloc.c new file +10 -0
+				per_cpu_test_data[cpu][i].test_failed,
+				per_cpu_test_data[cpu][i].test_count,
+				per_cpu_test_data[cpu][i].time);
+		}
+
+		pr_info("All test took CPU%d=%lu cycles\n",
+			cpu, t->stop - t->start);
+	}
+
+	schedule_timeout(200);

This doesn't actually do anything when we're in state TASK_RUNNING.

lib/test_vmalloc.c line 535

Quoted 12 lines
lib/test_vmalloc.c new file +12 -0
+}
+
+static int vmalloc_test_init(void)
+{
+	__my_vmalloc_node_range =
+		(void *) kallsyms_lookup_name("__vmalloc_node_range");
+
+	if (__my_vmalloc_node_range)
+		do_concurrent_test();
+
+	return -EAGAIN; /* Fail will directly unload the module */
+}

It's unclear why this module needs access to the internal __vmalloc_node_range(). Please fully explain this in the changelog.

Then, let's just export the thing. (I expect this module needs a Kconfig dependency on CONFIG_KALLSYMS, btw). A suitable way of doing that would be

/* Exported for lib/test_vmalloc.c. Please do not use elsewhere */ EXPORT_SYMBOL_GPL(__vmalloc_node_range);

Quoted 3 lines

...

Generally speaking, I hope this code can use existing kernel infrastructure more completely. All that fiddling with atomic counters, completions and open-coded schedule() calls can perhaps be replaced with refcounts, counting semapores (rswems), mutexes, etc? I mean, from a quick glance, a lot of that code appears to be doing just what rwsems and mutexes do?

#3 lore raw

Re: [RFC PATCH 1/1] vmalloc: add test driver to analyse vmalloc allocator

On Tue 13-11-18 14:10:46, Andrew Morton wrote:
[...]

Quoted 14 lines

lib/test_vmalloc.c line 535

Quoted 10 lines
lib/test_vmalloc.c new file +10 -0
+static int vmalloc_test_init(void)
+{
+	__my_vmalloc_node_range =
+		(void *) kallsyms_lookup_name("__vmalloc_node_range");
+
+	if (__my_vmalloc_node_range)
+		do_concurrent_test();
+
+	return -EAGAIN; /* Fail will directly unload the module */
+}

It's unclear why this module needs access to the internal __vmalloc_node_range(). Please fully explain this in the changelog.

Quoted 6 lines

Then, let's just export the thing. (I expect this module needs a Kconfig dependency on CONFIG_KALLSYMS, btw). A suitable way of doing that would be

/* Exported for lib/test_vmalloc.c. Please do not use elsewhere */ EXPORT_SYMBOL_GPL(__vmalloc_node_range);

There was a previous discussion that testing for internal infrastructure is useful quite often and such a testing module needs an access to such an internal infrastructure. Exporting those symbols via standard EXPORT_SYMBOL_GPL is far from optimal because we can be pretty much sure an abuse will arise sooner than later. I was proposing EXPORT_SYMBOL_SELFTEST that would link only against testing modules.

If that is not viable for some reason then kallsyms_lookup_name is a dirty-but-usable workaround.

Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer
#4 lore raw

Re: [RFC PATCH 1/1] vmalloc: add test driver to analyse vmalloc allocator

Quoted 23 lines · On Wed, 14 Nov 2018 16:17:37 +0100 Michal Hocko <[email protected]> wrote:

On Tue 13-11-18 14:10:46, Andrew Morton wrote:
[...]

Quoted 14 lines

lib/test_vmalloc.c line 535

Quoted 10 lines
lib/test_vmalloc.c new file +10 -0
+static int vmalloc_test_init(void)
+{
+	__my_vmalloc_node_range =
+		(void *) kallsyms_lookup_name("__vmalloc_node_range");
+
+	if (__my_vmalloc_node_range)
+		do_concurrent_test();
+
+	return -EAGAIN; /* Fail will directly unload the module */
+}

It's unclear why this module needs access to the internal __vmalloc_node_range(). Please fully explain this in the changelog.

Quoted 6 lines

Then, let's just export the thing. (I expect this module needs a Kconfig dependency on CONFIG_KALLSYMS, btw). A suitable way of doing that would be

/* Exported for lib/test_vmalloc.c. Please do not use elsewhere */ EXPORT_SYMBOL_GPL(__vmalloc_node_range);

Quoted 6 lines

There was a previous discussion that testing for internal infrastructure is useful quite often and such a testing module needs an access to such an internal infrastructure. Exporting those symbols via standard EXPORT_SYMBOL_GPL is far from optimal because we can be pretty much sure an abuse will arise sooner than later. I was proposing EXPORT_SYMBOL_SELFTEST that would link only against testing modules.

That's rather overdoing things, I think. If someone uses a dont-use-this symbol then they get to own both pieces when it breaks.

We could simply do

#define EXPORT_SYMBOL_SELFTEST EXPORT_SYMBOL_GPL

then write a script which checks the tree for usages of the thus-tagged symbols outside tools/testing and lib/ (?)

Quoted 2 lines

If that is not viable for some reason then kallsyms_lookup_name is a dirty-but-usable workaround.

Well yes. It adds a dependency on CONFIG_KALLSYMS and will cause silent breakage if __vmalloc_node_range gets renamed, has its arguments changed, etc.

#5 lore raw

Re: [RFC PATCH 1/1] vmalloc: add test driver to analyse vmalloc allocator

Quoted 32 lines · Andrew Morton
Quoted 23 lines · On Wed, 14 Nov 2018 16:17:37 +0100 Michal Hocko <[email protected]> wrote:

On Tue 13-11-18 14:10:46, Andrew Morton wrote:
[...]

Quoted 14 lines

lib/test_vmalloc.c line 535

Quoted 10 lines
lib/test_vmalloc.c new file +10 -0
+static int vmalloc_test_init(void)
+{
+	__my_vmalloc_node_range =
+		(void *) kallsyms_lookup_name("__vmalloc_node_range");
+
+	if (__my_vmalloc_node_range)
+		do_concurrent_test();
+
+	return -EAGAIN; /* Fail will directly unload the module */
+}

It's unclear why this module needs access to the internal __vmalloc_node_range(). Please fully explain this in the changelog.

Quoted 6 lines

Then, let's just export the thing. (I expect this module needs a Kconfig dependency on CONFIG_KALLSYMS, btw). A suitable way of doing that would be

/* Exported for lib/test_vmalloc.c. Please do not use elsewhere */ EXPORT_SYMBOL_GPL(__vmalloc_node_range);

Quoted 6 lines

There was a previous discussion that testing for internal infrastructure is useful quite often and such a testing module needs an access to such an internal infrastructure. Exporting those symbols via standard EXPORT_SYMBOL_GPL is far from optimal because we can be pretty much sure an abuse will arise sooner than later. I was proposing EXPORT_SYMBOL_SELFTEST that would link only against testing modules.

Quoted 2 lines

That's rather overdoing things, I think. If someone uses a dont-use-this symbol then they get to own both pieces when it breaks.

I do not think this has ever worked out. People are abusing internal stuff and then we have to live with that. That is my experience at least.

Quoted 6 lines

We could simply do

#define EXPORT_SYMBOL_SELFTEST EXPORT_SYMBOL_GPL

then write a script which checks the tree for usages of the thus-tagged symbols outside tools/testing and lib/ (?)

and then yell at people? We can try it out of course. The namespace would be quite clear and we could document the supported usage pattern. We also want to make EXPORT_SYMBOL_SELFTEST conditional. EXPORTs are not free and we do not want to add them if the whole testing infrastructure is disabled (assuming there is a global one for that).

Quoted 6 lines
Quoted 2 lines

If that is not viable for some reason then kallsyms_lookup_name is a dirty-but-usable workaround.

Well yes. It adds a dependency on CONFIG_KALLSYMS and will cause silent breakage if __vmalloc_node_range gets renamed, has its arguments changed, etc.

Yeah, I've said dirty ;)

Signature · 2 lines
Michal Hocko
SUSE Labs
#6 lore raw

Re: [RFC PATCH 1/1] vmalloc: add test driver to analyse vmalloc allocator

Quoted 11 lines · Michal Hocko
Quoted 4 lines · Andrew Morton

#define EXPORT_SYMBOL_SELFTEST EXPORT_SYMBOL_GPL

then write a script which checks the tree for usages of the thus-tagged symbols outside tools/testing and lib/ (?)

and then yell at people? We can try it out of course. The namespace would be quite clear and we could document the supported usage pattern. We also want to make EXPORT_SYMBOL_SELFTEST conditional. EXPORTs are not free and we do not want to add them if the whole testing infrastructure is disabled (assuming there is a global one for that).

How about adding

#ifdef CONFIG_VMALLOC_TEST
int run_internal_vmalloc_tests(void)
{
...
}
EXPORT_SYMBOL_GPL(run_internal_vmalloc_tests);
#endif

to vmalloc.c? That would also allow calling functions which are marked as static, not just functions which aren't exported to modules.

#7 lore raw

Re: [RFC PATCH 1/1] vmalloc: add test driver to analyse vmalloc allocator

Quoted 23 lines · Matthew Wilcox
Quoted 11 lines · Michal Hocko
Quoted 4 lines · Andrew Morton

#define EXPORT_SYMBOL_SELFTEST EXPORT_SYMBOL_GPL

then write a script which checks the tree for usages of the thus-tagged symbols outside tools/testing and lib/ (?)

and then yell at people? We can try it out of course. The namespace would be quite clear and we could document the supported usage pattern. We also want to make EXPORT_SYMBOL_SELFTEST conditional. EXPORTs are not free and we do not want to add them if the whole testing infrastructure is disabled (assuming there is a global one for that).

How about adding

#ifdef CONFIG_VMALLOC_TEST
int run_internal_vmalloc_tests(void)
{
...
}
EXPORT_SYMBOL_GPL(run_internal_vmalloc_tests);
#endif

Quoted 2 lines

to vmalloc.c? That would also allow calling functions which are marked as static, not just functions which aren't exported to modules.

Yes that would be easier but do we want to pollute the normal code with testing? This looks messy to me.

Signature · 2 lines
Michal Hocko
SUSE Labs
#8 lore raw

Re: [RFC PATCH 1/1] vmalloc: add test driver to analyse vmalloc allocator

Quoted 11 lines · Michal Hocko
Quoted 10 lines · Matthew Wilcox

How about adding

#ifdef CONFIG_VMALLOC_TEST
int run_internal_vmalloc_tests(void)
{
...
}
EXPORT_SYMBOL_GPL(run_internal_vmalloc_tests);
#endif

Quoted 5 lines
Quoted 2 lines

to vmalloc.c? That would also allow calling functions which are marked as static, not just functions which aren't exported to modules.

Yes that would be easier but do we want to pollute the normal code with testing? This looks messy to me.

I don't think it's necessarily the worst thing in the world if random people browsing the file are forced to read test-cases ;-)

There's certainly a spectrum of possibilities here, one end being to basically just re-export static functions, and the other end putting every vmalloc test into vmalloc.c. vmalloc.c is pretty big at 70kB, but on the other hand, it's the 18th largest file in mm/ (can you believe page_alloc.c is 230kB?!)

maintainer
#9 lore raw

Re: [RFC PATCH 1/1] vmalloc: add test driver to analyse vmalloc allocator

Quoted 15 lines · On Thu, 15 Nov 2018 05:47:06 -0800 Matthew Wilcox <[email protected]> wrote:
Quoted 14 lines · Michal Hocko
Quoted 12 lines · Matthew Wilcox

How about adding

#ifdef CONFIG_VMALLOC_TEST
int run_internal_vmalloc_tests(void)
{
...
}
EXPORT_SYMBOL_GPL(run_internal_vmalloc_tests);
#endif

to vmalloc.c? That would also allow calling functions which are marked as static, not just functions which aren't exported to modules.

Quoted 8 lines
Quoted 2 lines

Yes that would be easier but do we want to pollute the normal code with testing? This looks messy to me.

I don't think it's necessarily the worst thing in the world if random people browsing the file are forced to read test-cases ;-)

There's certainly a spectrum of possibilities here, one end being to basically just re-export static functions,

Yes, if we're to it this way then a basic

#ifdef CONFIG_VMALLOC_TEST
EXPORT_SYMBOL_GPL(__vmalloc_node_range);
#endif

should suffice. If the desired symbol was a static one, a little non-static wrapper would be needed as well.

Quoted 4 lines

and the other end putting
every vmalloc test into vmalloc.c. vmalloc.c is pretty big at 70kB, but on the other hand, it's the 18th largest file in mm/ (can you believe page_alloc.c is 230kB?!)

author maintainer
#10 lore raw

Re: [RFC PATCH 1/1] vmalloc: add test driver to analyse vmalloc allocator

In reply to Andrew Morton
Quoted 84 lines · Andrew Morton
Quoted 70 lines · On Tue, 13 Nov 2018 16:16:29 +0100 "Uladzislau Rezki (Sony)"…

This adds a new kernel module for analysis of vmalloc allocator. It is only enabled as a module. There are two main reasons this module should be used for. Those are performance evaluation and stressing of vmalloc subsystem.

It consists of several test cases. As of now there are 8. The module has four parameters we can specify, therefore change the behaviour.

1) run_test_mask - set of tests to be run

0 fix_size_alloc_test
1 full_fit_alloc_test
2 long_busy_list_alloc_test
3 random_size_alloc_test
4 fix_align_alloc_test
5 random_size_align_alloc_test
6 align_shift_alloc_test
7 pcpu_alloc_test

By default all tests are in run test mask. If you want to select some specific tests it is possible to pass the mask. For example for first, second and fourth tests we go with (1 << 0 | 1 << 1 | 1 << 3) that is 11 value.

2) test_repeat_count - how many times each test should be repeated By default it is one time per test. It is possible to pass any number. As high the value is the test duration gets increased.

3) single_cpu_test - use one CPU to run the tests
By default this parameter is set to false. It means that all online CPUs execute tests. By setting it to 1, the tests are executed by first online CPU only.

4) sequential_test_order - run tests in sequential order
By default this parameter is set to false. It means that before running tests the order is shuffled. It is possible to make it sequential, just set it to 1.

Performance analysis:
In order to evaluate performance of vmalloc allocations, usually it makes sense to use only one CPU that runs tests, use sequential order, number of repeat tests can be different as well as set of test mask.

For example if we want to run all tests, to use one CPU and repeat each test 3 times. Insert the module passing following parameters:

single_cpu_test=1 sequential_test_order=1 test_repeat_count=3

with following output:

<snip>
Summary: fix_size_alloc_test 3 passed, 0 failed, test_count: 3, average: 918249 usec
Summary: full_fit_alloc_test 3 passed, 0 failed, test_count: 3, average: 1046232 usec
Summary: long_busy_list_alloc_test 3 passed, 0 failed, test_count: 3, average: 12000280 usec
Summary: random_size_alloc_test 3 passed, 0 failed, test_count: 3, average: 6184357 usec
Summary: fix_align_alloc_test 3 passed, 0 failed, test_count: 3, average: 2319067 usec
Summary: random_size_align_alloc_test 3 passed, 0 failed, test_count: 3, average: 2858425 usec
Summary: align_shift_alloc_test 0 passed, 3 failed, test_count: 3, average: 373 usec
Summary: pcpu_alloc_test 3 passed, 0 failed, test_count: 3, average: 93407 usec All test took CPU0=197829986888 cycles
<snip>

The align_shift_alloc_test is expected to be failed.

Stressing:
In order to stress the vmalloc subsystem we run all available test cases on all available CPUs simultaneously. In order to prevent constant behaviour pattern, the test cases array is shuffled by default to randomize the order of test execution.

Quoted 9 lines

For example if we want to run all tests(default), use all online CPUs(default) with shuffled order(default) and to repeat each test 30 times. The command would be like:

modprobe vmalloc_test test_repeat_count=30

Expected results are the system is alive, there are no any BUG_ONs or Kernel Panics the tests are completed, no memory leaks.

Seems useful.

Quoted 9 lines

Yes, there are plenty of scripts in tools/testing/selftests which load a kernel module for the testing so a vmalloc test under tools/testing/selftests/vm would be appropriate.

Generally the tests under tools/testing/selftests are for testing userspace-visible interfaces, and generally linux-specific ones. But that doesn't mean that we shouldn't add tests for internal functionality.

OK, i got it. Will add the script there.

Quoted 52 lines
Quoted 52 lines

...

lib/test_vmalloc.c new file +49 -0
+static int test_func(void *private)
+{
+	struct test_driver *t = private;
+	cpumask_t newmask = CPU_MASK_NONE;
+	int random_array[ARRAY_SIZE(test_case_array)];
+	int index, repeat, i, j, ret;
+	ktime_t kt;
+
+	cpumask_set_cpu(t->cpu, &newmask);
+	set_cpus_allowed_ptr(current, &newmask);
+
+	atomic_inc(&tests_running);
+	wait_for_completion(&completion1);
+
+	for (i = 0; i < ARRAY_SIZE(test_case_array); i++)
+		random_array[i] = i;
+
+	if (!sequential_test_order)
+		shuffle_array(random_array, ARRAY_SIZE(test_case_array));
+
+	t->start = get_cycles();
+	for (i = 0; i < ARRAY_SIZE(test_case_array); i++) {
+		index = random_array[i];
+
+		/*
+		 * Skip tests if run_test_mask has been specified.
+		 */
+		if (!((run_test_mask & (1 << index)) >> index))
+			continue;
+
+		repeat = per_cpu_test_data[t->cpu][index].test_count;
+
+		kt = ktime_get();
+		for (j = 0; j < repeat; j++) {
+			ret = test_case_array[index].test_func();
+			if (!ret)
+				per_cpu_test_data[t->cpu][index].test_passed++;
+			else
+				per_cpu_test_data[t->cpu][index].test_failed++;
+		}
+
+		/*
+		 * Take an average time that test took.
+		 */
+		per_cpu_test_data[t->cpu][index].time =
+			ktime_us_delta(ktime_get(), kt) / repeat;
+	}
+	t->stop = get_cycles();
+
Quoted 9 lines

lib/test_vmalloc.c line 419

Quoted 6 lines
lib/test_vmalloc.c new file +6 -0
+	atomic_inc(&phase1_complete);
+	wait_for_completion(&completion2);
+
+	atomic_dec(&tests_running);
+	set_current_state(TASK_UNINTERRUPTIBLE);
+	schedule();

This looks odd. What causes this thread to wake up again?

Basically, i reused the old code from another module. I agree that looks odd. Will revise and rewrite.

Quoted 23 lines
Quoted 23 lines
lib/test_vmalloc.c new file +20 -0
+	return 0;
+}
+
 
...
 
+	if (single_cpu_test) {
+		cpumask_clear(&cpus_run_test_mask);
+
+		cpumask_set_cpu(cpumask_first(cpu_online_mask),
+			&cpus_run_test_mask);
+	}
+
+	for_each_cpu(cpu, &cpus_run_test_mask) {
+		struct test_driver *t = &per_cpu_test_driver[cpu];
+
+		t->cpu = cpu;
+		t->task = kthread_run(test_func, t, "test%d", cpu);
+		if (IS_ERR(t->task)) {
+			pr_err("Failed to start test func\n");
+			return;
+		}
+	}
Quoted 9 lines

lib/test_vmalloc.c line 483

Quoted 6 lines
lib/test_vmalloc.c new file +6 -0
+
+	/* Wait till all processes are running */
+	while (atomic_read(&tests_running) <
+			cpumask_weight(&cpus_run_test_mask)) {
+		set_current_state(TASK_UNINTERRUPTIBLE);
+		schedule_timeout(10);

schedule_timeout_interruptible(). Or, better, plain old msleep().

Will revise and rewrite.

Quoted 11 lines

lib/test_vmalloc.c line 491

Quoted 8 lines
lib/test_vmalloc.c new file +8 -0
+	}
+	complete_all(&completion1);
+
+	/* Wait till all processes have completed phase 1 */
+	while (atomic_read(&phase1_complete) <
+			cpumask_weight(&cpus_run_test_mask)) {
+		set_current_state(TASK_UNINTERRUPTIBLE);
+		schedule_timeout(10);

Ditto.

Will revise and rewrite.

Quoted 26 lines
Quoted 26 lines
lib/test_vmalloc.c new file +26 -0
+	}
+	complete_all(&completion2);
+
+	while (atomic_read(&tests_running)) {
+		set_current_state(TASK_UNINTERRUPTIBLE);
+		schedule_timeout(10);
+	}
+
+	for_each_cpu(cpu, &cpus_run_test_mask) {
+		struct test_driver *t = &per_cpu_test_driver[cpu];
+		int i;
+
+		kthread_stop(t->task);
+
+		for (i = 0; i < ARRAY_SIZE(test_case_array); i++) {
+			if (!((run_test_mask & (1 << i)) >> i))
+				continue;
+
+			pr_info(
+				"Summary: %s %d passed, %d failed, test_count: %d, average: %llu usec\n",
+				test_case_array[i].test_name,
+				per_cpu_test_data[cpu][i].test_passed,
+				per_cpu_test_data[cpu][i].test_failed,
+				per_cpu_test_data[cpu][i].test_count,
+				per_cpu_test_data[cpu][i].time);
+		}
Quoted 9 lines

lib/test_vmalloc.c line 523

Quoted 6 lines
lib/test_vmalloc.c new file +6 -0
+
+		pr_info("All test took CPU%d=%lu cycles\n",
+			cpu, t->stop - t->start);
+	}
+
+	schedule_timeout(200);

This doesn't actually do anything when we're in state TASK_RUNNING.

Agree. Will revise and rewrite.

Quoted 16 lines

lib/test_vmalloc.c line 535

Quoted 12 lines
lib/test_vmalloc.c new file +12 -0
+}
+
+static int vmalloc_test_init(void)
+{
+	__my_vmalloc_node_range =
+		(void *) kallsyms_lookup_name("__vmalloc_node_range");
+
+	if (__my_vmalloc_node_range)
+		do_concurrent_test();
+
+	return -EAGAIN; /* Fail will directly unload the module */
+}

It's unclear why this module needs access to the internal __vmalloc_node_range(). Please fully explain this in the changelog.

Quoted 7 lines

Then, let's just export the thing. (I expect this module needs a Kconfig dependency on CONFIG_KALLSYMS, btw). A suitable way of doing that would be

/* Exported for lib/test_vmalloc.c. Please do not use elsewhere */ EXPORT_SYMBOL_GPL(__vmalloc_node_range);

I think Matthew Wilcox made the good proposal about placing test cases directly into vmalloc.c and export them:

#ifdef CONFIG_VMALLOC_TEST
int run_test_1(void)
{
...
}
EXPORT_SYMBOL_GPL(run_test_1);
...
#endif

probably it makes sense unless somebody does not want to see those tests in the vmalloc.c file.

Quoted 11 lines
Quoted 3 lines

...

Generally speaking, I hope this code can use existing kernel infrastructure more completely. All that fiddling with atomic counters, completions and open-coded schedule() calls can perhaps be replaced with refcounts, counting semapores (rswems), mutexes, etc? I mean, from a quick glance, a lot of that code appears to be doing just what rwsems and mutexes do?

Will try to make it more easier and less complicated, so will revise and rewrite.

Thanks for your comments!

--
Vlad Rezki