From patchwork Tue Apr 18 05:13:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13215030 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B4BD5C77B71 for ; Tue, 18 Apr 2023 05:17:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BCDC68E0001; Tue, 18 Apr 2023 01:16:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B7D4F6B0072; Tue, 18 Apr 2023 01:16:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A6BFA8E0001; Tue, 18 Apr 2023 01:16:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 9849D6B0071 for ; Tue, 18 Apr 2023 01:16:59 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 51648A0129 for ; Tue, 18 Apr 2023 05:16:59 +0000 (UTC) X-FDA: 80693352558.17.710B55C Received: from 66-220-144-179.mail-mxout.facebook.com (66-220-144-179.mail-mxout.facebook.com [66.220.144.179]) by imf05.hostedemail.com (Postfix) with ESMTP id B503D100013 for ; Tue, 18 Apr 2023 05:16:57 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=none; spf=neutral (imf05.hostedemail.com: 66.220.144.179 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681795017; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references; bh=cDWvA1YBthhP+EW2UljwJ5nYmCO0gRuhn2OIbohob48=; b=RROa0qZQhPdGJG9ySCmb9Ror0eh1yZMCDOGgJcpvRXfcFln2lsw70KWST8tFerzhSeNi2r yv1CR9GYY4cHx/SZ4H8U2GbwdEDkop6jMmKdlcxl/vJlJ1iDJA1eV8j2DRUjlNOuk53uUc CnTN6r+mroIl9X5hQk7Al8rIfgG6lQk= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=none; spf=neutral (imf05.hostedemail.com: 66.220.144.179 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681795017; a=rsa-sha256; cv=none; b=eSbKIKm2419yqPnaKLdWdlBededXnu3yYd6sx60G9xp5LeueBFTIoX5SZuNbCcDzzKcnle S14fk5nFlVtfQ+SRg3qEJw7TwC66HZ21HcVBu6WHnSTnFBSriv5nPdT2chzGf/5sN23/R0 LatDGaPnxa0EzAnpGqQMhPlMeNsPGRI= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id C84683AA01EF; Mon, 17 Apr 2023 22:14:10 -0700 (PDT) From: Stefan Roesch To: kernel-team@fb.com Cc: shr@devkernel.io, linux-mm@kvack.org, riel@surriel.com, mhocko@suse.com, david@redhat.com, linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org, akpm@linux-foundation.org, hannes@cmpxchg.org, willy@infradead.org Subject: [PATCH v9 0/3] mm: process/cgroup ksm support Date: Mon, 17 Apr 2023 22:13:39 -0700 Message-Id: <20230418051342.1919757-1-shr@devkernel.io> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Stat-Signature: 5h1fawcyuzxuo9tfok8z96fakyaxogxq X-Rspam-User: X-Rspamd-Queue-Id: B503D100013 X-Rspam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Ec X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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//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