mbox series

[v9,0/3] mm: process/cgroup ksm support

Message ID 20230418051342.1919757-1-shr@devkernel.io (mailing list archive)
Headers show
Series mm: process/cgroup ksm support | expand

Message

Stefan Roesch April 18, 2023, 5:13 a.m. UTC
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:
- V9:
  - simplify __ksm_add_vma()
  - Adding return to test_prctl() in ksm_functional_tests.c
  - Removing assignment in defining debug variable in ksm_tests.c
  
- V8:
  - Refreshed to latest mm-unstable
  - Added check for arg3 - arg5 in prctl function
  - Don't return an error ksm_enable_merge_any if MMF_VM_MERGE_ANY bit is
    already set

  - unmap after merge in ksm_tests program
  - use tmp variable in main function in ksm_tests program
  
  - Specify all 5 parameters in call to prctl in test programs
  - Rename test_ksm_prctl() to test_prctl()
  - Skip if first test in test_prctl failed with skip result
  - Exit early for failures in test_prctl

  - Rename test_ksm_fork() to test_prctl_fork()
  - Similar changes to test_prctl also for test_prctl_fork()
  - Change number of test
  
- 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                                  |  27 +++
 mm/ksm.c                                      | 125 +++++++++++--
 mm/mmap.c                                     |   3 +
 tools/include/uapi/linux/prctl.h              |   2 +
 tools/testing/selftests/mm/Makefile           |   2 +-
 .../selftests/mm/ksm_functional_tests.c       |  91 ++++++++-
 tools/testing/selftests/mm/ksm_tests.c        | 172 ++++++++++++++----
 14 files changed, 414 insertions(+), 59 deletions(-)


base-commit: f80a6c7a37be043f7b074d1e19638675315e3566

Comments

David Hildenbrand April 18, 2023, 3:28 p.m. UTC | #1
This is a follow-up to [1]:
	[PATCH v9 0/3] mm: process/cgroup ksm support

which is not in mm-unstable yet (but soon? :) ). I'll be on vacation for
~2 weeks, so sending it out now as reply to [1].

(1) Make PR_SET_MEMORY_MERGE=0 unmerge pages like setting MADV_UNMERGEABLE
does, (2) add a selftest for it and (3) factor out disabling of KSM from
s390/gmap code.

Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Stefan Roesch <shr@devkernel.io>
Cc: Rik van Riel <riel@surriel.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
Cc: Janosch Frank <frankja@linux.ibm.com>
Cc: Claudio Imbrenda <imbrenda@linux.ibm.com>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Shuah Khan <shuah@kernel.org>

[1] https://lkml.kernel.org/r/20230418051342.1919757-1-shr@devkernel.io

David Hildenbrand (3):
  mm/ksm: unmerge and clear VM_MERGEABLE when setting
    PR_SET_MEMORY_MERGE=0
  selftests/ksm: ksm_functional_tests: add prctl unmerge test
  mm/ksm: move disabling KSM from s390/gmap code to KSM code

 arch/s390/mm/gmap.c                           | 20 +------
 include/linux/ksm.h                           |  7 +++
 kernel/sys.c                                  |  7 +--
 mm/ksm.c                                      | 58 +++++++++++++++++++
 .../selftests/mm/ksm_functional_tests.c       | 46 +++++++++++++--
 5 files changed, 107 insertions(+), 31 deletions(-)