linux-doc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v7 0/3] mm: process/cgroup ksm support
@ 2023-04-13 23:31 Stefan Roesch
       [not found] ` <20230413233115.1878303-2-shr@devkernel.io>
       [not found] ` <20230413233115.1878303-4-shr@devkernel.io>
  0 siblings, 2 replies; 6+ messages in thread
From: Stefan Roesch @ 2023-04-13 23:31 UTC (permalink / raw)
  To: kernel-team
  Cc: shr, linux-mm, riel, mhocko, david, linux-kselftest, linux-doc,
	akpm, hannes, willy

So far KSM can only be enabled by calling madvise for memory regions. To
be able to use KSM for more workloads, KSM needs to have the ability to be
enabled / disabled at the process / cgroup level.

Use case 1:
The madvise call is not available in the programming language. An example for
this are programs with forked workloads using a garbage collected language without
pointers. In such a language madvise cannot be made available.

In addition the addresses of objects get moved around as they are garbage
collected. KSM sharing needs to be enabled "from the outside" for these type of
workloads.

Use case 2:
The same interpreter can also be used for workloads where KSM brings no
benefit or even has overhead. We'd like to be able to enable KSM on a workload
by workload basis.

Use case 3:
With the madvise call sharing opportunities are only enabled for the current
process: it is a workload-local decision. A considerable number of sharing
opportunities may exist across multiple workloads or jobs (if they are part
of the same security domain). Only a higler level entity like a job scheduler
or container can know for certain if its running one or more instances of a
job. That job scheduler however doesn't have the necessary internal workload
knowledge to make targeted madvise calls.

Security concerns:
In previous discussions security concerns have been brought up. The problem is
that an individual workload does not have the knowledge about what else is
running on a machine. Therefore it has to be very conservative in what memory
areas can be shared or not. However, if the system is dedicated to running
multiple jobs within the same security domain, its the job scheduler that has
the knowledge that sharing can be safely enabled and is even desirable.

Performance:
Experiments with using UKSM have shown a capacity increase of around 20%.

Here are the metrics from an instagram workload (taken from a machine with
64GB main memory):

   full_scans: 445
   general_profit: 20158298048
   max_page_sharing: 256
   merge_across_nodes: 1
   pages_shared: 129547
   pages_sharing: 5119146
   pages_to_scan: 4000
   pages_unshared: 1760924
   pages_volatile: 10761341
   run: 1
   sleep_millisecs: 20
   stable_node_chains: 167
   stable_node_chains_prune_millisecs: 2000
   stable_node_dups: 2751
   use_zero_pages: 0
   zero_pages_sharing: 0

After the service is running for 30 minutes to an hour, 4 to 5 million shared
pages are common for this workload when using KSM.


Detailed changes:

1. New options for prctl system command
This patch series adds two new options to the prctl system call. The first
one allows to enable KSM at the process level and the second one to query the
setting.

The setting will be inherited by child processes.

With the above setting, KSM can be enabled for the seed process of a cgroup
and all processes in the cgroup will inherit the setting.

2. Changes to KSM processing
When KSM is enabled at the process level, the KSM code will iterate over all
the VMA's and enable KSM for the eligible VMA's.

When forking a process that has KSM enabled, the setting will be inherited by
the new child process.

3. Add general_profit metric
The general_profit metric of KSM is specified in the documentation, but not
calculated. This adds the general profit metric to /sys/kernel/debug/mm/ksm.

4. Add more metrics to ksm_stat
This adds the process profit metric to /proc/<pid>/ksm_stat.

5. Add more tests to ksm_tests and ksm_functional_tests
This adds an option to specify the merge type to the ksm_tests. This allows to
test madvise and prctl KSM.

It also adds a two new tests to ksm_functional_tests: one to test the new
prctl options and the other one is a fork test to verify that the KSM process
setting is inherited by client processes.


Changes:
- V7:
  - Removed ksm_add_mm() function
  - added ksm_enable_merge_any() function
  - Made ksm_add_vmas() function static
  - Simplified ksm_fork function to only MMF_VM_MERGE_ANY bit
  - Moved setting of bit MMF_VM_MERGE_ANY to ksm_enable_merge_any()
  - Removed flag parameter from __ksm_enter
  - Removed flag parameter from __ksm_exit
  - Clear bit MMF_VM_MERGE_ANY in __ksm_exit
  - call ksm_add_vma only in mmap_region() and do_brk_flags()

  - Removed check_ksm_fork() and check_ksm_merge_type from ksm_tests
  - Removed -F and -G command line options
  - Removed enum options for above tests
  - Added -d option to enable debug mode
  - Added debug variable for storing debug option

- V6:
  - Fix error condition in prctl call
  - Remove ksm_merge_type function and ksm_stat output
  - Some minor changes like whitespace and removing a cast.
  
- V5:
  - When the prctl system call is invoked, mark all compatible VMA
    as mergeable
  - Instead of checcking during scan if VMA is mergeable, mark the VMA
    mergeable when the VMA is created (in case the VMA is compatible)
    - Remove earlier changes, they are no longer necessary
  - Unset the flag MMF_VM_MERGE_ANY in gmap_mark_unmergeable().
  - When unsetting the MMF_VM_MERGE_ANY flag with prctl, only unset the
    flag
  - Remove pages_volatile function (with the simplar general_profit calculation,
    the function is no longer needed)
  - Use simpler formula for calculation of general_profit

- V4:
  - removing check in prctl for MMF_VM_MERGEABLE in PR_SET_MEMORY_MERGE
    handling
  - Checking for VM_MERGEABLE AND MMF_VM_MERGE_ANY to avoid chaning vm_flags
    - This requires also checking that the vma is compatible. The
      compatibility check is provided by a new helper
    - processes which have set MMF_VM_MERGE_ANY, only need to call the
      helper and not madvise.
  - removed unmerge_vmas function, this function is no longer necessary,
    clearing the MMF_VM_MERGE_ANY bit is sufficient

- V3:
  - folded patch 1 - 6
  - folded patch 7 - 14
  - folded patch 15 - 19
  - Expanded on the use cases in the cover letter
  - Added a section on security concerns to the cover letter

- V2:
  - Added use cases to the cover letter
  - Removed the tracing patch from the patch series and posted it as an
    individual patch
  - Refreshed repo



Stefan Roesch (3):
  mm: add new api to enable ksm per process
  mm: add new KSM process and sysfs knobs
  selftests/mm: add new selftests for KSM

 Documentation/ABI/testing/sysfs-kernel-mm-ksm |   8 +
 Documentation/admin-guide/mm/ksm.rst          |   5 +-
 arch/s390/mm/gmap.c                           |   7 +
 fs/proc/base.c                                |   3 +
 include/linux/ksm.h                           |  25 ++-
 include/linux/sched/coredump.h                |   1 +
 include/uapi/linux/prctl.h                    |   2 +
 kernel/sys.c                                  |  25 +++
 mm/ksm.c                                      | 127 +++++++++++--
 mm/mmap.c                                     |   3 +
 tools/include/uapi/linux/prctl.h              |   2 +
 tools/testing/selftests/mm/Makefile           |   2 +-
 .../selftests/mm/ksm_functional_tests.c       |  77 +++++++-
 tools/testing/selftests/mm/ksm_tests.c        | 175 ++++++++++++++----
 14 files changed, 402 insertions(+), 60 deletions(-)

-- 
2.31.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v7 1/3] mm: add new api to enable ksm per process
       [not found] ` <20230413233115.1878303-2-shr@devkernel.io>
@ 2023-04-14 10:24   ` David Hildenbrand
  2023-04-14 20:53     ` Stefan Roesch
  0 siblings, 1 reply; 6+ messages in thread
From: David Hildenbrand @ 2023-04-14 10:24 UTC (permalink / raw)
  To: Stefan Roesch, kernel-team
  Cc: linux-mm, riel, mhocko, linux-kselftest, linux-doc, akpm, hannes,
	willy, Bagas Sanjaya

Thanks!

In general,

Acked-by: David Hildenbrand <david@redhat.com>

Two nits below, after staring at some other prctl implementations.

> +#define PR_SET_MEMORY_MERGE		67
> +#define PR_GET_MEMORY_MERGE		68
>   #endif /* _LINUX_PRCTL_H */
> diff --git a/kernel/sys.c b/kernel/sys.c
> index 495cd87d9bf4..8c2e50edeb18 100644
> --- a/kernel/sys.c
> +++ b/kernel/sys.c
> @@ -15,6 +15,7 @@
>   #include <linux/highuid.h>
>   #include <linux/fs.h>
>   #include <linux/kmod.h>
> +#include <linux/ksm.h>
>   #include <linux/perf_event.h>
>   #include <linux/resource.h>
>   #include <linux/kernel.h>
> @@ -2661,6 +2662,30 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
>   	case PR_SET_VMA:
>   		error = prctl_set_vma(arg2, arg3, arg4, arg5);
>   		break;
> +#ifdef CONFIG_KSM
> +	case PR_SET_MEMORY_MERGE:

Looking at some other code (PR_SET_NO_NEW_PRIVS/ PR_SET_THP_DISABLE) I 
wonder if we also want

if (arg3 || arg4 || arg5)
	return -EINVAL;

For PR_GET_MEMORY_MERGE it looks good already.

> +		if (mmap_write_lock_killable(me->mm))
> +			return -EINTR;
> +
> +		if (arg2) {
> +			error = ksm_enable_merge_any(me->mm);
> +		} else {
> +			/*
> +			 * TODO: we might want disable KSM on all VMAs and
> +			 * trigger unsharing to completely disable KSM.
> +			 */
> +			clear_bit(MMF_VM_MERGE_ANY, &me->mm->flags);
> +			error = 0;
> +		}
> +		mmap_write_unlock(me->mm);
> +		break;
> +	case PR_GET_MEMORY_MERGE:
> +		if (arg2 || arg3 || arg4 || arg5)
> +			return -EINVAL;
> +
> +		error = !!test_bit(MMF_VM_MERGE_ANY, &me->mm->flags);
> +		break;
> +#endif
>   	default:
>   		error = -EINVAL;
>   		break;

[...]

> +/**
> + * ksm_enable_merge_any - Add mm to mm ksm list and enable merging on all
> + *                        compatible VMA's
> + *
> + * @mm:  Pointer to mm
> + *
> + * Returns 0 on success, otherwise error code
> + */
> +int ksm_enable_merge_any(struct mm_struct *mm)
> +{
> +	int err;
> +
> +	if (test_bit(MMF_VM_MERGE_ANY, &mm->flags))
> +		return -EINVAL;


I'm curious, why is enabling the prctl() supposed to fail if already 
enabled? (it would not fail if disabling and already disabled)

For example, PR_SET_THP_DISABLE/PR_SET_NO_NEW_PRIVS doesn't fail if 
already set.

-- 
Thanks,

David / dhildenb


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v7 3/3] selftests/mm: add new selftests for KSM
       [not found] ` <20230413233115.1878303-4-shr@devkernel.io>
@ 2023-04-14 14:28   ` David Hildenbrand
  2023-04-14 20:54     ` Stefan Roesch
  0 siblings, 1 reply; 6+ messages in thread
From: David Hildenbrand @ 2023-04-14 14:28 UTC (permalink / raw)
  To: Stefan Roesch, kernel-team
  Cc: linux-mm, riel, mhocko, linux-kselftest, linux-doc, akpm, hannes,
	willy, Bagas Sanjaya

Thanks for moving the functional tests. Some more feedback forksm_functional_tests change. Writing tests in the
ksft testing framework can be a bit "special".


I'm seeing some weird test failures due to

prctl(PR_GET_MEMORY_MERGE, 0)

Apparently, these go away when using

prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0)

to explicitly force the other values to 0. Most probably, we should do that
for PR_SET_MEMORY_MERGE as well (especially if we check for the arguments as
well).

[...]

> @@ -15,8 +15,10 @@
>   #include <errno.h>
>   #include <fcntl.h>
>   #include <sys/mman.h>
> +#include <sys/prctl.h>
>   #include <sys/syscall.h>
>   #include <sys/ioctl.h>
> +#include <sys/wait.h>
>   #include <linux/userfaultfd.h>
>   
>   #include "../kselftest.h"
> @@ -326,9 +328,80 @@ static void test_unmerge_uffd_wp(void)
>   }
>   #endif
>   
> +/* Verify that KSM can be enabled / queried with prctl. */
> +static void test_ksm_prctl(void)

Maybe call this "test_prctl", because after all, these are all KSM tests.

> +{
> +	bool ret = false;
> +	int is_on;
> +	int is_off;
> +
> +	ksft_print_msg("[RUN] %s\n", __func__);
> +
> +	if (prctl(PR_SET_MEMORY_MERGE, 1)) {
> +		perror("prctl set");
> +		goto out;
> +	}
> +
> +	is_on = prctl(PR_GET_MEMORY_MERGE, 0);
> +	if (prctl(PR_SET_MEMORY_MERGE, 0)) {
> +		perror("prctl set");
> +		goto out;
> +	}
> +
> +	is_off = prctl(PR_GET_MEMORY_MERGE, 0);
> +	if (is_on && is_off)
> +		ret = true;
> +
> +out:
> +	ksft_test_result(ret, "prctl get / set\n");

The test fails if the kernel does not support PR_SET_MEMORY_MERGE.


I'd modify this test to:

(1) skip if the first PR_SET_MEMORY_MERGE=1 failed with EINVAL.
(2) distinguish for PR_GET_MEMORY_MERGE whether it returned an error or
     whether it returned a wrong value. Feel free to keep that as is, whatever
     you prefer.
(3) exit early for all failures, you get exactly one expected skip/pass/fail for the
     test and use specific test failure messages.
(4) Pass "0" for all other arguments of prctl.


Something like:

static void test_prctl(void)
{
	int ret;

	ksft_print_msg("[RUN] %s\n", __func__);

	ret = prctl(PR_SET_MEMORY_MERGE, 1, 0, 0, 0);
	if (ret < 0 && errno == EINVAL){
		ksft_test_result_skip("PR_SET_MEMORY_MERGE not supported\n");
		return;
	} else if (ret) {
		ksft_test_result_fail("PR_SET_MEMORY_MERGE=1 failed\n");
		return;
	}

	ret = prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0);
	if (ret < 0) {
		ksft_test_result_fail("PR_GET_MEMORY_MERGE failed\n");
		return;
	} else if (ret != 1) {
		ksft_test_result_fail("PR_SET_MEMORY_MERGE=1 not effective\n");
		return;
	}

	ret = prctl(PR_SET_MEMORY_MERGE, 0, 0, 0, 0);
	if (ret){
		ksft_test_result_fail("PR_SET_MEMORY_MERGE=0 failed\n");
		return;
	}

	ret = prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0);
	if (ret < 0) {
		ksft_test_result_fail("PR_GET_MEMORY_MERGE failed\n");
		return;
	} else if (ret != 0) {
		ksft_test_result_fail("PR_SET_MEMORY_MERGE=0 not effective\n");
		return;
	}

	ksft_test_result_pass("Setting/clearing PR_SET_MEMORY_MERGE works\n");
}


> +}
> +
> +/* Verify that prctl ksm flag is inherited. */
> +static void test_ksm_fork(void)

Maybe call it "test_prctl_fork"

> +{
> +	int status;
> +	bool ret = false;
> +	pid_t child_pid;
> +
> +	ksft_print_msg("[RUN] %s\n", __func__);
> +
> +	if (prctl(PR_SET_MEMORY_MERGE, 1)) {
> +		ksft_test_result_fail("prctl failed\n");
> +		goto out;
> +	}
> +
> +	child_pid = fork();
> +	if (child_pid == 0) {
> +		int is_on = 
> +
> +		if (!is_on)
> +			exit(-1);
> +
> +		exit(0);
> +	}
> +
> +	if (child_pid < 0) {
> +		ksft_test_result_fail("child pid < 0\n");
> +		goto out;> +
> +	if (waitpid(child_pid, &status, 0) < 0 || WEXITSTATUS(status) != 0) {
> +		ksft_test_result_fail("wait pid < 0\n");
> +		goto out;
> +	}
> +
> +	if (prctl(PR_SET_MEMORY_MERGE, 0))
> +		ksft_test_result_fail("prctl 2 failed\n");
> +	else
> +		ret = true;
> +
> +out:
> +	ksft_test_result(ret, "ksm_flag is inherited\n");
> +}

Again, test fails if kernel support is not around.

I'd modify this test to:

(1) skip if the first PR_SET_MEMORY_MERGE=1 failed with EINVAL just as in the other test.
(2) Use a simple exit(prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0)); in the child.
(3) exit early for all failures, you get exactly one expected skip/pass/fail for the
     test and use specific test failure messages.
(4) Split up the waitpid() check to test what failed.
(5) Pass "0" for all other arguments of prctl.


Something like:

static void test_prctl_fork(void)
{
	int ret, status;
	pid_t child_pid;

	ksft_print_msg("[RUN] %s\n", __func__);

	ret = prctl(PR_SET_MEMORY_MERGE, 1, 0, 0, 0);
	if (ret < 0 && errno == EINVAL){
		ksft_test_result_skip("PR_SET_MEMORY_MERGE not supported\n");
		return;
	} else if (ret) {
		ksft_test_result_fail("PR_SET_MEMORY_MERGE=1 failed\n");
		return;
	}

	child_pid = fork();
	if (!child_pid) {
		exit(prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0));
	} else if (child_pid < 0) {
		ksft_test_result_fail("fork() failed\n");
		return;
	}

	if (waitpid(child_pid, &status, 0) < 0) {
		ksft_test_result_fail("waitpid() failed\n");
		return;
	} else if (WEXITSTATUS(status) != 1) {
		ksft_test_result_fail("unexpected PR_GET_MEMORY_MERGE result in child\n");
		return;
	}

	if (prctl(PR_SET_MEMORY_MERGE, 0, 0, 0, 0)) {
		ksft_test_result_fail("PR_SET_MEMORY_MERGE=0 failed\n");
		return;
	}

	ksft_test_result_pass("PR_SET_MEMORY_MERGE value is inherited\n");
}



> +
>   int main(int argc, char **argv)
>   {
> -	unsigned int tests = 2;
> +	unsigned int tests = 6;

Assuming you execute exactly one ksft_test_result_skip/fail/pass on every path of your two
test, this would become "4".

>   	int err;
>   
>   #ifdef __NR_userfaultfd
> @@ -358,6 +431,8 @@ int main(int argc, char **argv)
>   #ifdef __NR_userfaultfd
>   	test_unmerge_uffd_wp();
>   #endif
> +	test_ksm_prctl();
> +	test_ksm_fork();
>   


With above outlined changes (feel free to integrate what you consider valuable),
on an older kernel I get:

$ sudo ./ksm_functional_tests
TAP version 13
1..5
# [RUN] test_unmerge
ok 1 Pages were unmerged
# [RUN] test_unmerge_discarded
ok 2 Pages were unmerged
# [RUN] test_unmerge_uffd_wp
ok 3 Pages were unmerged
# [RUN] test_prctl
ok 4 # SKIP PR_SET_MEMORY_MERGE not supported
# [RUN] test_prctl_fork
ok 5 # SKIP PR_SET_MEMORY_MERGE not supported
# Totals: pass:3 fail:0 xfail:0 xpass:0 skip:2 error:0


On a kernel with your patch #1:

# ./ksm_functional_tests
TAP version 13
1..5
# [RUN] test_unmerge
ok 1 Pages were unmerged
# [RUN] test_unmerge_discarded
ok 2 Pages were unmerged
# [RUN] test_unmerge_uffd_wp
ok 3 Pages were unmerged
# [RUN] test_prctl
ok 4 Setting/clearing PR_SET_MEMORY_MERGE works
# [RUN] test_prctl_fork
ok 5 PR_SET_MEMORY_MERGE value is inherited
# Totals: pass:5 fail:0 xfail:0 xpass:0 skip:0 error:0




>   	err = ksft_get_fail_cnt();
>   	if (err)
> diff --git a/tools/testing/selftests/mm/ksm_tests.c b/tools/testing/selftests/mm/ksm_tests.c
> index f9eb4d67e0dd..35b3828d44b4 100644
> --- a/tools/testing/selftests/mm/ksm_tests.c
> +++ b/tools/testing/selftests/mm/ksm_tests.c
> @@ -1,6 +1,8 @@
>   // SPDX-License-Identifier: GPL-2.0

[...]


Changes to ksm_tests mostly look good. Two comments:


> -	if (ksm_merge_pages(map_ptr, page_size * page_count, start_time, timeout))
> +	if (ksm_merge_pages(merge_type, map_ptr, page_size * page_count, start_time, timeout))
>   		goto err_out;
>   
>   	/* verify that the right number of pages are merged */
>   	if (assert_ksm_pages_count(page_count)) {
>   		printf("OK\n");
> -		munmap(map_ptr, page_size * page_count);
> +		if (merge_type == KSM_MERGE_MADVISE)
> +			munmap(map_ptr, page_size * page_count);
> +		else if (merge_type == KSM_MERGE_PRCTL)
> +			prctl(PR_SET_MEMORY_MERGE, 0);

Are you sure that we don't want to unmap here? I'd assume we want to unmap in either way.

[...]

> +		case 'd':
> +			debug = 1;
> +			break;
>   		case 's':
>   			size_MB = atoi(optarg);
>   			if (size_MB <= 0) {
>   				printf("Size must be greater than 0\n");
>   				return KSFT_FAIL;
>   			}
> +		case 't':
> +			{
> +				int tmp = atoi(optarg);
> +
> +				if (tmp < 0 || tmp > KSM_MERGE_LAST) {
> +					printf("Invalid merge type\n");
> +					return KSFT_FAIL;
> +				}
> +				merge_type = atoi(optarg);

You can simply reuse tmp

merge_type = tmp;




-- 
Thanks,

David / dhildenb


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v7 1/3] mm: add new api to enable ksm per process
  2023-04-14 10:24   ` [PATCH v7 1/3] mm: add new api to enable ksm per process David Hildenbrand
@ 2023-04-14 20:53     ` Stefan Roesch
  0 siblings, 0 replies; 6+ messages in thread
From: Stefan Roesch @ 2023-04-14 20:53 UTC (permalink / raw)
  To: David Hildenbrand
  Cc: kernel-team, linux-mm, riel, mhocko, linux-kselftest, linux-doc,
	akpm, hannes, willy, Bagas Sanjaya


David Hildenbrand <david@redhat.com> writes:

> Thanks!
>
> In general,
>
> Acked-by: David Hildenbrand <david@redhat.com>
>
> Two nits below, after staring at some other prctl implementations.
>
>> +#define PR_SET_MEMORY_MERGE		67
>> +#define PR_GET_MEMORY_MERGE		68
>>   #endif /* _LINUX_PRCTL_H */
>> diff --git a/kernel/sys.c b/kernel/sys.c
>> index 495cd87d9bf4..8c2e50edeb18 100644
>> --- a/kernel/sys.c
>> +++ b/kernel/sys.c
>> @@ -15,6 +15,7 @@
>>   #include <linux/highuid.h>
>>   #include <linux/fs.h>
>>   #include <linux/kmod.h>
>> +#include <linux/ksm.h>
>>   #include <linux/perf_event.h>
>>   #include <linux/resource.h>
>>   #include <linux/kernel.h>
>> @@ -2661,6 +2662,30 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
>>   	case PR_SET_VMA:
>>   		error = prctl_set_vma(arg2, arg3, arg4, arg5);
>>   		break;
>> +#ifdef CONFIG_KSM
>> +	case PR_SET_MEMORY_MERGE:
>
> Looking at some other code (PR_SET_NO_NEW_PRIVS/ PR_SET_THP_DISABLE) I wonder if
> we also want
>
> if (arg3 || arg4 || arg5)
> 	return -EINVAL;
>
I added the above check. It requires that we always specify all
parameters in the test programs. I also changed them accordingly.

> For PR_GET_MEMORY_MERGE it looks good already.
>
>> +		if (mmap_write_lock_killable(me->mm))
>> +			return -EINTR;
>> +
>> +		if (arg2) {
>> +			error = ksm_enable_merge_any(me->mm);
>> +		} else {
>> +			/*
>> +			 * TODO: we might want disable KSM on all VMAs and
>> +			 * trigger unsharing to completely disable KSM.
>> +			 */
>> +			clear_bit(MMF_VM_MERGE_ANY, &me->mm->flags);
>> +			error = 0;
>> +		}
>> +		mmap_write_unlock(me->mm);
>> +		break;
>> +	case PR_GET_MEMORY_MERGE:
>> +		if (arg2 || arg3 || arg4 || arg5)
>> +			return -EINVAL;
>> +
>> +		error = !!test_bit(MMF_VM_MERGE_ANY, &me->mm->flags);
>> +		break;
>> +#endif
>>   	default:
>>   		error = -EINVAL;
>>   		break;
>
> [...]
>
>> +/**
>> + * ksm_enable_merge_any - Add mm to mm ksm list and enable merging on all
>> + *                        compatible VMA's
>> + *
>> + * @mm:  Pointer to mm
>> + *
>> + * Returns 0 on success, otherwise error code
>> + */
>> +int ksm_enable_merge_any(struct mm_struct *mm)
>> +{
>> +	int err;
>> +
>> +	if (test_bit(MMF_VM_MERGE_ANY, &mm->flags))
>> +		return -EINVAL;
>
>
> I'm curious, why is enabling the prctl() supposed to fail if already enabled?
> (it would not fail if disabling and already disabled)
>

I changed that to not return an error in that case.
>
> For example, PR_SET_THP_DISABLE/PR_SET_NO_NEW_PRIVS doesn't fail if already set.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v7 3/3] selftests/mm: add new selftests for KSM
  2023-04-14 14:28   ` [PATCH v7 3/3] selftests/mm: add new selftests for KSM David Hildenbrand
@ 2023-04-14 20:54     ` Stefan Roesch
  2023-04-17  8:04       ` David Hildenbrand
  0 siblings, 1 reply; 6+ messages in thread
From: Stefan Roesch @ 2023-04-14 20:54 UTC (permalink / raw)
  To: David Hildenbrand
  Cc: kernel-team, linux-mm, riel, mhocko, linux-kselftest, linux-doc,
	akpm, hannes, willy, Bagas Sanjaya


David Hildenbrand <david@redhat.com> writes:

> Thanks for moving the functional tests. Some more feedback forksm_functional_tests change. Writing tests in the
> ksft testing framework can be a bit "special".
>
>
> I'm seeing some weird test failures due to
>
> prctl(PR_GET_MEMORY_MERGE, 0)
>
> Apparently, these go away when using
>
> prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0)
>

I changed the test programs to always specify all the 5 parameters.

> to explicitly force the other values to 0. Most probably, we should do that
> for PR_SET_MEMORY_MERGE as well (especially if we check for the arguments as
> well).
>
> [...]
>
>> @@ -15,8 +15,10 @@
>>   #include <errno.h>
>>   #include <fcntl.h>
>>   #include <sys/mman.h>
>> +#include <sys/prctl.h>
>>   #include <sys/syscall.h>
>>   #include <sys/ioctl.h>
>> +#include <sys/wait.h>
>>   #include <linux/userfaultfd.h>
>>     #include "../kselftest.h"
>> @@ -326,9 +328,80 @@ static void test_unmerge_uffd_wp(void)
>>   }
>>   #endif
>>   +/* Verify that KSM can be enabled / queried with prctl. */
>> +static void test_ksm_prctl(void)
>
> Maybe call this "test_prctl", because after all, these are all KSM tests.
>

I renamed it to test_prctl in the next version.

>> +{
>> +	bool ret = false;
>> +	int is_on;
>> +	int is_off;
>> +
>> +	ksft_print_msg("[RUN] %s\n", __func__);
>> +
>> +	if (prctl(PR_SET_MEMORY_MERGE, 1)) {
>> +		perror("prctl set");
>> +		goto out;
>> +	}
>> +
>> +	is_on = prctl(PR_GET_MEMORY_MERGE, 0);
>> +	if (prctl(PR_SET_MEMORY_MERGE, 0)) {
>> +		perror("prctl set");
>> +		goto out;
>> +	}
>> +
>> +	is_off = prctl(PR_GET_MEMORY_MERGE, 0);
>> +	if (is_on && is_off)
>> +		ret = true;
>> +
>> +out:
>> +	ksft_test_result(ret, "prctl get / set\n");
>
> The test fails if the kernel does not support PR_SET_MEMORY_MERGE.
>
>
> I'd modify this test to:
>
> (1) skip if the first PR_SET_MEMORY_MERGE=1 failed with EINVAL.
> (2) distinguish for PR_GET_MEMORY_MERGE whether it returned an error or
>     whether it returned a wrong value. Feel free to keep that as is, whatever
>     you prefer.
> (3) exit early for all failures, you get exactly one expected skip/pass/fail for the
>     test and use specific test failure messages.
> (4) Pass "0" for all other arguments of prctl.
>
>
> Something like:
>
> static void test_prctl(void)
> {
> 	int ret;
>
> 	ksft_print_msg("[RUN] %s\n", __func__);
>
> 	ret = prctl(PR_SET_MEMORY_MERGE, 1, 0, 0, 0);
> 	if (ret < 0 && errno == EINVAL){
> 		ksft_test_result_skip("PR_SET_MEMORY_MERGE not supported\n");
> 		return;
> 	} else if (ret) {
> 		ksft_test_result_fail("PR_SET_MEMORY_MERGE=1 failed\n");
> 		return;
> 	}
>
> 	ret = prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0);
> 	if (ret < 0) {
> 		ksft_test_result_fail("PR_GET_MEMORY_MERGE failed\n");
> 		return;
> 	} else if (ret != 1) {
> 		ksft_test_result_fail("PR_SET_MEMORY_MERGE=1 not effective\n");
> 		return;
> 	}
>
> 	ret = prctl(PR_SET_MEMORY_MERGE, 0, 0, 0, 0);
> 	if (ret){
> 		ksft_test_result_fail("PR_SET_MEMORY_MERGE=0 failed\n");
> 		return;
> 	}
>
> 	ret = prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0);
> 	if (ret < 0) {
> 		ksft_test_result_fail("PR_GET_MEMORY_MERGE failed\n");
> 		return;
> 	} else if (ret != 0) {
> 		ksft_test_result_fail("PR_SET_MEMORY_MERGE=0 not effective\n");
> 		return;
> 	}
>
> 	ksft_test_result_pass("Setting/clearing PR_SET_MEMORY_MERGE works\n");
> }
>
>

I made changes to the test program according to the code above.
>> +}
>> +
>> +/* Verify that prctl ksm flag is inherited. */
>> +static void test_ksm_fork(void)
>
> Maybe call it "test_prctl_fork"
>
I changed it to test_prctl_fork.

>> +{
>> +	int status;
>> +	bool ret = false;
>> +	pid_t child_pid;
>> +
>> +	ksft_print_msg("[RUN] %s\n", __func__);
>> +
>> +	if (prctl(PR_SET_MEMORY_MERGE, 1)) {
>> +		ksft_test_result_fail("prctl failed\n");
>> +		goto out;
>> +	}
>> +
>> +	child_pid = fork();
>> +	if (child_pid == 0) {
>> +		int is_on = +
>> +		if (!is_on)
>> +			exit(-1);
>> +
>> +		exit(0);
>> +	}
>> +
>> +	if (child_pid < 0) {
>> +		ksft_test_result_fail("child pid < 0\n");
>> +		goto out;> +
>> +	if (waitpid(child_pid, &status, 0) < 0 || WEXITSTATUS(status) != 0) {
>> +		ksft_test_result_fail("wait pid < 0\n");
>> +		goto out;
>> +	}
>> +
>> +	if (prctl(PR_SET_MEMORY_MERGE, 0))
>> +		ksft_test_result_fail("prctl 2 failed\n");
>> +	else
>> +		ret = true;
>> +
>> +out:
>> +	ksft_test_result(ret, "ksm_flag is inherited\n");
>> +}
>
> Again, test fails if kernel support is not around.
>
> I'd modify this test to:
>
> (1) skip if the first PR_SET_MEMORY_MERGE=1 failed with EINVAL just as in the other test.
> (2) Use a simple exit(prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0)); in the child.
> (3) exit early for all failures, you get exactly one expected skip/pass/fail for the
>     test and use specific test failure messages.
> (4) Split up the waitpid() check to test what failed.
> (5) Pass "0" for all other arguments of prctl.
>
>
> Something like:
>
> static void test_prctl_fork(void)
> {
> 	int ret, status;
> 	pid_t child_pid;
>
> 	ksft_print_msg("[RUN] %s\n", __func__);
>
> 	ret = prctl(PR_SET_MEMORY_MERGE, 1, 0, 0, 0);
> 	if (ret < 0 && errno == EINVAL){
> 		ksft_test_result_skip("PR_SET_MEMORY_MERGE not supported\n");
> 		return;
> 	} else if (ret) {
> 		ksft_test_result_fail("PR_SET_MEMORY_MERGE=1 failed\n");
> 		return;
> 	}
>
> 	child_pid = fork();
> 	if (!child_pid) {
> 		exit(prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0));
> 	} else if (child_pid < 0) {
> 		ksft_test_result_fail("fork() failed\n");
> 		return;
> 	}
>
> 	if (waitpid(child_pid, &status, 0) < 0) {
> 		ksft_test_result_fail("waitpid() failed\n");
> 		return;
> 	} else if (WEXITSTATUS(status) != 1) {
> 		ksft_test_result_fail("unexpected PR_GET_MEMORY_MERGE result in child\n");
> 		return;
> 	}
>
> 	if (prctl(PR_SET_MEMORY_MERGE, 0, 0, 0, 0)) {
> 		ksft_test_result_fail("PR_SET_MEMORY_MERGE=0 failed\n");
> 		return;
> 	}
>
> 	ksft_test_result_pass("PR_SET_MEMORY_MERGE value is inherited\n");
> }
>
>
>

I made changes to the test program according to the code above.
>> +
>>   int main(int argc, char **argv)
>>   {
>> -	unsigned int tests = 2;
>> +	unsigned int tests = 6;
>
> Assuming you execute exactly one ksft_test_result_skip/fail/pass on every path of your two
> test, this would become "4".
>
Changed it to 4.
>>   	int err;
>>     #ifdef __NR_userfaultfd
>> @@ -358,6 +431,8 @@ int main(int argc, char **argv)
>>   #ifdef __NR_userfaultfd
>>   	test_unmerge_uffd_wp();
>>   #endif
>> +	test_ksm_prctl();
>> +	test_ksm_fork();
>>
>
>
> With above outlined changes (feel free to integrate what you consider valuable),
> on an older kernel I get:
>
> $ sudo ./ksm_functional_tests
> TAP version 13
> 1..5
> # [RUN] test_unmerge
> ok 1 Pages were unmerged
> # [RUN] test_unmerge_discarded
> ok 2 Pages were unmerged
> # [RUN] test_unmerge_uffd_wp
> ok 3 Pages were unmerged
> # [RUN] test_prctl
> ok 4 # SKIP PR_SET_MEMORY_MERGE not supported
> # [RUN] test_prctl_fork
> ok 5 # SKIP PR_SET_MEMORY_MERGE not supported
> # Totals: pass:3 fail:0 xfail:0 xpass:0 skip:2 error:0
>
>
> On a kernel with your patch #1:
>
> # ./ksm_functional_tests
> TAP version 13
> 1..5
> # [RUN] test_unmerge
> ok 1 Pages were unmerged
> # [RUN] test_unmerge_discarded
> ok 2 Pages were unmerged
> # [RUN] test_unmerge_uffd_wp
> ok 3 Pages were unmerged
> # [RUN] test_prctl
> ok 4 Setting/clearing PR_SET_MEMORY_MERGE works
> # [RUN] test_prctl_fork
> ok 5 PR_SET_MEMORY_MERGE value is inherited
> # Totals: pass:5 fail:0 xfail:0 xpass:0 skip:0 error:0
>
>
>
>
>>   	err = ksft_get_fail_cnt();
>>   	if (err)
>> diff --git a/tools/testing/selftests/mm/ksm_tests.c b/tools/testing/selftests/mm/ksm_tests.c
>> index f9eb4d67e0dd..35b3828d44b4 100644
>> --- a/tools/testing/selftests/mm/ksm_tests.c
>> +++ b/tools/testing/selftests/mm/ksm_tests.c
>> @@ -1,6 +1,8 @@
>>   // SPDX-License-Identifier: GPL-2.0
>
> [...]
>
>
> Changes to ksm_tests mostly look good. Two comments:
>
>
>> -	if (ksm_merge_pages(map_ptr, page_size * page_count, start_time, timeout))
>> +	if (ksm_merge_pages(merge_type, map_ptr, page_size * page_count, start_time, timeout))
>>   		goto err_out;
>>     	/* verify that the right number of pages are merged */
>>   	if (assert_ksm_pages_count(page_count)) {
>>   		printf("OK\n");
>> -		munmap(map_ptr, page_size * page_count);
>> +		if (merge_type == KSM_MERGE_MADVISE)
>> +			munmap(map_ptr, page_size * page_count);
>> +		else if (merge_type == KSM_MERGE_PRCTL)
>> +			prctl(PR_SET_MEMORY_MERGE, 0);
>
> Are you sure that we don't want to unmap here? I'd assume we want to unmap in either way.
>
> [...]
>
I changed it to always unmap.

>> +		case 'd':
>> +			debug = 1;
>> +			break;
>>   		case 's':
>>   			size_MB = atoi(optarg);
>>   			if (size_MB <= 0) {
>>   				printf("Size must be greater than 0\n");
>>   				return KSFT_FAIL;
>>   			}
>> +		case 't':
>> +			{
>> +				int tmp = atoi(optarg);
>> +
>> +				if (tmp < 0 || tmp > KSM_MERGE_LAST) {
>> +					printf("Invalid merge type\n");
>> +					return KSFT_FAIL;
>> +				}
>> +				merge_type = atoi(optarg);
>
> You can simply reuse tmp
>
> merge_type = tmp;

Changed it.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v7 3/3] selftests/mm: add new selftests for KSM
  2023-04-14 20:54     ` Stefan Roesch
@ 2023-04-17  8:04       ` David Hildenbrand
  0 siblings, 0 replies; 6+ messages in thread
From: David Hildenbrand @ 2023-04-17  8:04 UTC (permalink / raw)
  To: Stefan Roesch
  Cc: kernel-team, linux-mm, riel, mhocko, linux-kselftest, linux-doc,
	akpm, hannes, willy, Bagas Sanjaya

On 14.04.23 22:54, Stefan Roesch wrote:
> 
> David Hildenbrand <david@redhat.com> writes:
> 
>> Thanks for moving the functional tests. Some more feedback forksm_functional_tests change. Writing tests in the
>> ksft testing framework can be a bit "special".
>>
>>
>> I'm seeing some weird test failures due to
>>
>> prctl(PR_GET_MEMORY_MERGE, 0)
>>
>> Apparently, these go away when using
>>
>> prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0)
>>
> 
> I changed the test programs to always specify all the 5 parameters.
> 

Thanks!

-- 
Thanks,

David / dhildenb


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-04-17  8:05 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-13 23:31 [PATCH v7 0/3] mm: process/cgroup ksm support Stefan Roesch
     [not found] ` <20230413233115.1878303-2-shr@devkernel.io>
2023-04-14 10:24   ` [PATCH v7 1/3] mm: add new api to enable ksm per process David Hildenbrand
2023-04-14 20:53     ` Stefan Roesch
     [not found] ` <20230413233115.1878303-4-shr@devkernel.io>
2023-04-14 14:28   ` [PATCH v7 3/3] selftests/mm: add new selftests for KSM David Hildenbrand
2023-04-14 20:54     ` Stefan Roesch
2023-04-17  8:04       ` David Hildenbrand

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).