From patchwork Mon Jan 23 17:37:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13112639 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 58302C38142 for ; Mon, 23 Jan 2023 17:38:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 571366B0085; Mon, 23 Jan 2023 12:38:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 546DA6B0082; Mon, 23 Jan 2023 12:38:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 37E5B6B0085; Mon, 23 Jan 2023 12:38:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 257256B0081 for ; Mon, 23 Jan 2023 12:38:18 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 0A47D41778 for ; Mon, 23 Jan 2023 17:38:18 +0000 (UTC) X-FDA: 80386772676.12.195B13A Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf06.hostedemail.com (Postfix) with ESMTP id 4744818001E for ; Mon, 23 Jan 2023 17:38:16 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=none; spf=neutral (imf06.hostedemail.com: 66.220.144.178 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=1674495496; 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:in-reply-to:references:references; bh=GB7nbTR042OPBT5940sQcmscKilViV2E/45SbY5p8lA=; b=51hDk06G0Wy+6pRW2wsm8UeNBU3Dccto82+BuaYrUfawiy1QVirPSgY3sgStI0SxevQF8V DEqwexKgVSKj5sqhGCqSmqpvTCQJe7lgzmPVHOjuV0OJvAaeaeCOmPU6SqgaAPkv7r/VWd uxaIzeCI4aHTt0C/pMVYmswWqFpGZAw= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=none; spf=neutral (imf06.hostedemail.com: 66.220.144.178 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=1674495496; a=rsa-sha256; cv=none; b=ulVfOz1P+cO8eHnoOmn1pgruIH/s8fiypVA8QQ8u9HAzRU15aP/GvSGZvkWsJw+vk0U2yW IA5Aoj3koIH+tJxlupY5wij1lo4n7aoKVYBHhO15ddQPWUGtJAG3rw95Xik7+7wbkrfjya jNAXaDzEMuoH7UHgIiKR9aNTx3swlVs= Received: by dev0134.prn3.facebook.com (Postfix, from userid 425415) id 41C5F5616BD3; Mon, 23 Jan 2023 09:37:56 -0800 (PST) From: Stefan Roesch To: linux-mm@kvack.org Cc: shr@devkernel.io, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND RFC PATCH v1 01/20] mm: add new flag to enable ksm per process Date: Mon, 23 Jan 2023 09:37:29 -0800 Message-Id: <20230123173748.1734238-2-shr@devkernel.io> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230123173748.1734238-1-shr@devkernel.io> References: <20230123173748.1734238-1-shr@devkernel.io> MIME-Version: 1.0 X-Stat-Signature: 5kxhtsxq5dpaw6trxk9i9jj4ezrhy79m X-Rspam-User: X-Rspamd-Queue-Id: 4744818001E X-Rspamd-Server: rspam06 X-HE-Tag: 1674495496-270133 X-HE-Meta: U2FsdGVkX194XykDdpsYc381DE4WjWcwRZcLzKQ6lnybCAjbgPQgEGimslIwdpw6wnfOVOT36EBbC8FqBfwXbv0jrvqle93oQVIK7XktaH+3PtbYYH5wLjITwWCc7NQss6n8ypCafqvcKltTvCDaWRUaPw+6o0WtoA+VqfVikX0jJD4vg5PzdbpmI2v4Eu2cSPioqbLQQ3n2YE2DE+50Eo0rubCN+PjGjQlL+Ic0v216cJYXbnLQGm3lsV08p6wnLNW8zJWSOE+p+8WCLlU3li1I9vEraD7RPJqbIGF2nqhwz4kaEUsn8XlRsIO3vNum7SAwm4qdeMHBbLwjUNs2nJLHV9PSagxsk8hBmJAm6TCWUqOfFolY9VdGk93R3CNXGWyKWH+OC5r75gU3ckE2luq/dx3VGYU2Sdl1MT0XKnRaKLsgPJaqlTpSwDHD3rTDE9gFhqgcVDW69JV1zKP/LS7C45VIJTz7Z8bChOlzV3TJisX5Bz4T3e9x17ccUVazJ+KwUZJEfXSZTNulQLvHSrtwiWDWeEcwF0diCYpEwC46TKjfzLZr9X/X80NVx9blVasubVVIbRJgaIkNrHmeywlkuf6Zw08nMNFlgDcgetlY4L1B0XEw4aSM9IjBl74XgWUHtTd/JuA6Fzsl5RLZyu/Bt7CBu9XyOkcLjys1KBXU4C+SJo8PNS+0Au2Pfc34vvsKfpUCzXVxGxZzh0paQn0izeWwkO4C9eg2oiCCuHLTtgFKvnIk0GiJyjoKsL77fXFYRPYG4yAmFRRWrlDCaZLV1k9zfhwFsJMoKaoMbBX767WQeYGVwuiDv3aOuXvWDIR3/DyCcMTVkEo1mNZRbZRLtgoY7ysL2Vt1ff1W+cyMmY+yJYSdUkfACYM2aPWyWgObWkU/kqQL+UrlIrUuUshitFN51HJBnQPgQMCKXJkzG4ypp7ONiknag2AnfNmYu01QFrl2PI++3BvAOio gXP0s4ke mvOd7iI5FK/PSQjymAMqwIDl86qCXaShWxdBnJjS22T4yZNo= 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: This introduces the new flag MMF_VM_MERGE_ANY flag. When this flag is set, kernel samepage merging (ksm) gets enabled for all vma's of a process. Signed-off-by: Stefan Roesch --- include/linux/sched/coredump.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/linux/sched/coredump.h b/include/linux/sched/coredump.h index 8270ad7ae14c..0e0de5b7469f 100644 --- a/include/linux/sched/coredump.h +++ b/include/linux/sched/coredump.h @@ -86,4 +86,5 @@ static inline int get_dumpable(struct mm_struct *mm) #define MMF_INIT_MASK (MMF_DUMPABLE_MASK | MMF_DUMP_FILTER_MASK |\ MMF_DISABLE_THP_MASK) +#define MMF_VM_MERGE_ANY 29 #endif /* _LINUX_SCHED_COREDUMP_H */ From patchwork Mon Jan 23 17:37:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13112636 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 BD371C05027 for ; Mon, 23 Jan 2023 17:38:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4AC3F6B0083; Mon, 23 Jan 2023 12:38:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 435EB6B0082; Mon, 23 Jan 2023 12:38:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2FCB56B0081; Mon, 23 Jan 2023 12:38:17 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 2049A6B007E for ; Mon, 23 Jan 2023 12:38:17 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id E3E25C21F8 for ; Mon, 23 Jan 2023 17:38:16 +0000 (UTC) X-FDA: 80386772592.24.EC1C5D9 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf16.hostedemail.com (Postfix) with ESMTP id 461BA18002C for ; Mon, 23 Jan 2023 17:38:15 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf16.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674495495; 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:in-reply-to:references:references; bh=ukh/p78BTzUSB71/CCunrEk0jjIr0HEMFKTMZ8Ud6rI=; b=05y30Ei9noaB8SHYJMh7QudpNLGBdcA9FZ5NC1nsJpgaI6ossUCso1Y3zayOKo7VSIncd6 5sIUqyJxpMiHu6WO7ScSExpx9PSMTl8OLC9M35HC2c+yMcEDcfBdlWtwpTFYWKaYQk0hLt Mje0LEpBRR7pcv8S2DO00LUqtZjAG1k= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf16.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674495495; a=rsa-sha256; cv=none; b=G+ttFYwz3jGMHS1jAvLilZxSg79oTPN0nG1to2tzr7Eke7ECLW40+UeiBxYKeO8QYCs8Lv h9wDInecdF8rtNDyAEwo7LOB/DF2vS7VGDifCSvr+aJO7+uQvQagBS+YZL4lQ6Sc/raEmt umIX+fOF556nD1N/anwHHp6g4PawnD4= Received: by dev0134.prn3.facebook.com (Postfix, from userid 425415) id 45FC35616BD5; Mon, 23 Jan 2023 09:37:56 -0800 (PST) From: Stefan Roesch To: linux-mm@kvack.org Cc: shr@devkernel.io, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND RFC PATCH v1 02/20] mm: add flag to __ksm_enter Date: Mon, 23 Jan 2023 09:37:30 -0800 Message-Id: <20230123173748.1734238-3-shr@devkernel.io> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230123173748.1734238-1-shr@devkernel.io> References: <20230123173748.1734238-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 461BA18002C X-Stat-Signature: ubdia7xnaare5y97xuqahb14bsbbxo7p X-Rspam-User: X-HE-Tag: 1674495495-376536 X-HE-Meta: U2FsdGVkX19F6saSyT5ySF8z3+0R+Z7RDV/cjw9mRYkVZ/FkXQUtrHQWEfsg2RSUa9qHCnJevIo17W1cTXkvtqe0bPKVhBX7mb36YYOqD5FPPAwg7lsNoCnb52yn7mjV7BSar+q3sjJUsVnOoKRrdlVu7ZK0syyoNOytTPIZSthfDzDBJw7zfSEnorfSKIWR0yjRSNFTa2bQYectWtpXtVG/Sado6OZy0OKpef9s++8fa1VNMrAXOMZhRt87m8VcGGkv3epM1Vm6sP9iSx0kBD7B4gqpNAG9oLRqGiCip4tBDFvDHbxuVoemBO5yofz1hXhd76Iddakfy2ynVyiLWPkcnutY2PsZ6YyVSpJIvNh0LZzX46fZQmQ8g7OGucer+/sLOAbSwruOfKCiHEq6Bo53RfL26yUkh5urSEcdZ5HCDZQrc5ujMMhIANxBlU8Hlj3uwN3RHw692aVqllS8C94x3+otULZzgNnhndRHDedMN+v4HyDWt+52PqDb3Rm67Lcv1Ep8LeMDQL2kycyUnJhcdIqAdEaKtQqjQKWtwqutaq9EYGuQzeDwPqkUvHLiKpADiq4f1VPV6A1FQ4+IY2c6cyQJfIvvIX+bO13Ta/lvb2AOUCZhRiz1JM2ccVzGwuBHkbEUI05DI6f7ePTv8R+aiiEg64a2xHppKDQKEGw1DnEyC4mRQl73rsqgrzvTn4m7/SyZp4EqpHvJ2JUBfnxeDyhiTaZqYciPNA9wH1S82uJFHhPsECxyHE2u1+d+TaEKzoIK4ULFUqlwxEJrz0YnR5WfmrSMsAnRBMxFvM8B9j/AM9pzXvztFeXNDiX7Q2RRiwqlyiK5Yl1pKWaV2DG1D4pxk2lu3SkwrLXCFjGA/ZUNGd+PBDQ2ErwGHOteWJpdGHP1eRP7jl+tVuQ+zBXSUEfOgAMXPt+6oBMhq6ri4qBzfCpbakYycp1wmaNGrKk4wIOTAHUdYX9NonJ lfdRX0Xm komwXCyEmJrxPHc2c/SNxpGeE4YW0pYtNrg8Uy3yRkAaH1mUsOtSyq/4jiaaREINayRfvsNAhU5mGWZ4Ml017Ueg269cmjh4bxQPm 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: This change adds the flag parameter to __ksm_enter. This allows to distinguish if ksm was called by prctl or madvise. Signed-off-by: Stefan Roesch --- include/linux/ksm.h | 6 ++++-- mm/ksm.c | 9 +++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/include/linux/ksm.h b/include/linux/ksm.h index 7e232ba59b86..50e6b56092f3 100644 --- a/include/linux/ksm.h +++ b/include/linux/ksm.h @@ -18,13 +18,15 @@ #ifdef CONFIG_KSM int ksm_madvise(struct vm_area_struct *vma, unsigned long start, unsigned long end, int advice, unsigned long *vm_flags); -int __ksm_enter(struct mm_struct *mm); void __ksm_exit(struct mm_struct *mm); +int __ksm_enter(struct mm_struct *mm, int flag); static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) { + if (test_bit(MMF_VM_MERGE_ANY, &oldmm->flags)) + return __ksm_enter(mm, MMF_VM_MERGE_ANY); if (test_bit(MMF_VM_MERGEABLE, &oldmm->flags)) - return __ksm_enter(mm); + return __ksm_enter(mm, MMF_VM_MERGEABLE); return 0; } diff --git a/mm/ksm.c b/mm/ksm.c index dd02780c387f..d84a244fe224 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2493,8 +2493,9 @@ int ksm_madvise(struct vm_area_struct *vma, unsigned long start, return 0; #endif - if (!test_bit(MMF_VM_MERGEABLE, &mm->flags)) { - err = __ksm_enter(mm); + if (!test_bit(MMF_VM_MERGEABLE, &mm->flags) && + !test_bit(MMF_VM_MERGE_ANY, &mm->flags)) { + err = __ksm_enter(mm, MMF_VM_MERGEABLE); if (err) return err; } @@ -2520,7 +2521,7 @@ int ksm_madvise(struct vm_area_struct *vma, unsigned long start, } EXPORT_SYMBOL_GPL(ksm_madvise); -int __ksm_enter(struct mm_struct *mm) +int __ksm_enter(struct mm_struct *mm, int flag) { struct ksm_mm_slot *mm_slot; struct mm_slot *slot; @@ -2553,7 +2554,7 @@ int __ksm_enter(struct mm_struct *mm) list_add_tail(&slot->mm_node, &ksm_scan.mm_slot->slot.mm_node); spin_unlock(&ksm_mmlist_lock); - set_bit(MMF_VM_MERGEABLE, &mm->flags); + set_bit(flag, &mm->flags); mmgrab(mm); if (needs_wakeup) From patchwork Mon Jan 23 17:37:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13112637 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 0853BC38142 for ; Mon, 23 Jan 2023 17:38:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 768086B007E; Mon, 23 Jan 2023 12:38:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6F2A56B0085; Mon, 23 Jan 2023 12:38:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4AA9A6B0080; Mon, 23 Jan 2023 12:38:17 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 31C1A6B007E for ; Mon, 23 Jan 2023 12:38:17 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id ED72D822B6 for ; Mon, 23 Jan 2023 17:38:16 +0000 (UTC) X-FDA: 80386772592.25.1AE6107 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf17.hostedemail.com (Postfix) with ESMTP id 3983740005 for ; Mon, 23 Jan 2023 17:38:15 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf17.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674495495; 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:in-reply-to:references:references; bh=euBbnlChzh5NyXkxd3+rY9w1+gYxyY5ES33Gy0DFQi0=; b=8T2USIz+xO/j9m73u+EWfzN07plHyYhtmbyG1LmWuk6wgNVaj+9oCSgdEnyU5CD1eTSwe3 PreqrKBBQ9kSDyf63CD0qmOa78p1njU/F8+uikVWVRyrDabSRPBkWHIjSa9Znwls6/8QDX cxLttxhhmQ5zd4zsUfgXLlIkse8mnxY= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf17.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674495495; a=rsa-sha256; cv=none; b=6yMrnU08iCKc0yGiLHf0m4kEcePSXKuMhjf1Pqza4CHyKGaa7N++4ZVbYiCLLC0Sgi7S+j z/smdT/X0gZFPcwrACSdvHzWIgJwJSgEYi0/VlBK4IG8r7wbzRbiMQ8XasgHv5v7Tb/gKi Dl3jjPT/vve0/3kzXkbFEAfLk7iYdrg= Received: by dev0134.prn3.facebook.com (Postfix, from userid 425415) id 4A5615616BD7; Mon, 23 Jan 2023 09:37:56 -0800 (PST) From: Stefan Roesch To: linux-mm@kvack.org Cc: shr@devkernel.io, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND RFC PATCH v1 03/20] mm: add flag to __ksm_exit call Date: Mon, 23 Jan 2023 09:37:31 -0800 Message-Id: <20230123173748.1734238-4-shr@devkernel.io> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230123173748.1734238-1-shr@devkernel.io> References: <20230123173748.1734238-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 3983740005 X-Stat-Signature: t1gqxf3gdyus3c5xuwjeg3q8k1bhknic X-HE-Tag: 1674495495-747103 X-HE-Meta: U2FsdGVkX18SIU9UQTvxMlzcyyetRvKBhkAdEiGJwK42PKrCCQZgFBeTf6H+39rNXLBMCfy+DQS4ry7Po5O5HP/7MmGFktVey1K+uV4lPxlxfTXOBzjdAnw87ZGcypB+kPAySjzrPu4cVkBmXPPB+nxHdo0EOL7DUzwTdqg6wL1WTyjjPtQEgQUpYN3khWxCkjPLOrs5imS51v3n16xrpyEt6hNMSdlY2OwA7p0vNXsGlclnIE2D6e9+1xIWbpABZKzouMifs82ckG4jm8206Zqcauv7eGvHCwF1AERn73mOKQlYfJY0zsUelYsMdxdErC7htXaufnSj6pfN4F+mdcmSsYczy7N2K8wur1PwzQOS5oqGELHKhIr4xmNQshpMe4UMOk0UPFHm2wmtgZ7TpVWv6AyHTN3JK6QYMCDJ0rzKCam6Y017wmsp08UWWtR79mjzkehTU1ufQG/U9kDHaemFB/Qq1Mp8KAMaRChErgfKYKzeXuSRagQEFoLpxbN0aWuR99snZgGuKqrM0wLqEltSd+U+DNQd3pt2ZU3aWTxRDdl0zqybFRgw0mqa/TJD+YKIFhVYfxrC5YVTZzWR/+e0ghIfk8IaGHGBtPBjpHd880mGBYjuoHhItinR91OwiliOM9ODLxgX3j6uVhbGI3z7K8eGo5DtzyAS87QkNAepXlCQz4glQj4ae6Mxq3m4dqAfGcVEIrgCe5zShSoE7f0KMEUGeM/LMvug8fD5KsSTs4iUR+G/Qhu80J0ej4Clt8vMRnjTXyEMq4ZNBFs3oAlW5gtp7+eNmuCt90Ms39iA9uvhnntyntGAZSEM2Cv/GrzltXIIhk8VoYs+GPZehBF7gBjTXOsAGXsLbp973rHBAF+qR10CPFYkCj7mimJB5XYxxppzDj3KGGr1VMpKR3Qd8jLVwbnBUB2qG4jyrKba36U1cU5HcoyLxbNUk6SlYIJs/qEJcL/aU4Y018H 24yLhjN0 usuMh/GckrLKzK90OS+IHkcexxWwAYVolbbI+uvg2lmPZFeWIWzyaMYI9mJAzhn2sbtsWUDVUX0IZH5nJhwxMd8KapaY4MYDc7hJE 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: This adds the flag parameter to the __ksm_exit() call. This allows to distinguish if this call is for an prctl or madvise invocation. Signed-off-by: Stefan Roesch --- include/linux/ksm.h | 8 +++++--- mm/ksm.c | 8 ++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/include/linux/ksm.h b/include/linux/ksm.h index 50e6b56092f3..d38a05a36298 100644 --- a/include/linux/ksm.h +++ b/include/linux/ksm.h @@ -18,8 +18,8 @@ #ifdef CONFIG_KSM int ksm_madvise(struct vm_area_struct *vma, unsigned long start, unsigned long end, int advice, unsigned long *vm_flags); -void __ksm_exit(struct mm_struct *mm); int __ksm_enter(struct mm_struct *mm, int flag); +void __ksm_exit(struct mm_struct *mm, int flag); static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) { @@ -32,8 +32,10 @@ static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) static inline void ksm_exit(struct mm_struct *mm) { - if (test_bit(MMF_VM_MERGEABLE, &mm->flags)) - __ksm_exit(mm); + if (test_bit(MMF_VM_MERGE_ANY, &mm->flags)) + __ksm_exit(mm, MMF_VM_MERGE_ANY); + else if (test_bit(MMF_VM_MERGEABLE, &mm->flags)) + __ksm_exit(mm, MMF_VM_MERGEABLE); } /* diff --git a/mm/ksm.c b/mm/ksm.c index d84a244fe224..83796328574c 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2563,12 +2563,16 @@ int __ksm_enter(struct mm_struct *mm, int flag) return 0; } -void __ksm_exit(struct mm_struct *mm) +void __ksm_exit(struct mm_struct *mm, int flag) { struct ksm_mm_slot *mm_slot; struct mm_slot *slot; int easy_to_free = 0; + if (!(current->flags & PF_EXITING) && flag == MMF_VM_MERGE_ANY && + test_bit(MMF_VM_MERGE_ANY, &mm->flags)) + clear_bit(MMF_VM_MERGE_ANY, &mm->flags); + /* * This process is exiting: if it's straightforward (as is the * case when ksmd was never running), free mm_slot immediately. @@ -2595,7 +2599,7 @@ void __ksm_exit(struct mm_struct *mm) if (easy_to_free) { mm_slot_free(mm_slot_cache, mm_slot); - clear_bit(MMF_VM_MERGEABLE, &mm->flags); + clear_bit(flag, &mm->flags); mmdrop(mm); } else if (mm_slot) { mmap_write_lock(mm); From patchwork Mon Jan 23 17:37:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13112634 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 301FCC05027 for ; Mon, 23 Jan 2023 17:38:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8A41F6B007B; Mon, 23 Jan 2023 12:38:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 855776B007D; Mon, 23 Jan 2023 12:38:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6CD586B007E; Mon, 23 Jan 2023 12:38:13 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 5C8FD6B007B for ; Mon, 23 Jan 2023 12:38:13 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id EEE6D1222C8 for ; Mon, 23 Jan 2023 17:38:12 +0000 (UTC) X-FDA: 80386772424.19.0FD43A2 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf03.hostedemail.com (Postfix) with ESMTP id 004F620017 for ; Mon, 23 Jan 2023 17:38:10 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf03.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674495491; 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:in-reply-to:references:references; bh=/hifLxhpa0/uSr3tT88zM9tTa9s6CvyvsChFXbISRug=; b=qp+jtTbkGemeGJJn0oIftiHv0QPYJ9NApf9OeqGV9FZTfS69fbY6lhk99fz5pjFJEITSS5 CvjZ26Of/69jjw34CE8znUs+HJ8mFOHjS38B/M2f6JyvVFjkSPxaQX986/56xqWP8pYz+y Wsu9Ya9R7dJf7cyuzvo26LlnaoJRaas= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf03.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674495491; a=rsa-sha256; cv=none; b=VJkJv35mXA/gCzgkkVjn6QmgYunQjdtXErYFKX33cCaLQf0gJbihYq+Br+izb1N7ZGiv3t CI0d/MSudOGGnlr/MDyGVp9EfjY+EeyA27L1dCSRTLZDcdKjJuVhXds6BSR3wUDnKl2OK2 6Zz5+RgOuG0RmUHtEoKGDpszDmn++KY= Received: by dev0134.prn3.facebook.com (Postfix, from userid 425415) id 4EC195616BD9; Mon, 23 Jan 2023 09:37:56 -0800 (PST) From: Stefan Roesch To: linux-mm@kvack.org Cc: shr@devkernel.io, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND RFC PATCH v1 04/20] mm: invoke madvise for all vmas in scan_get_next_rmap_item Date: Mon, 23 Jan 2023 09:37:32 -0800 Message-Id: <20230123173748.1734238-5-shr@devkernel.io> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230123173748.1734238-1-shr@devkernel.io> References: <20230123173748.1734238-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 004F620017 X-Stat-Signature: d85umwagq9mdh3zqxk1ry3tw6mbqiek5 X-HE-Tag: 1674495490-286865 X-HE-Meta: U2FsdGVkX19NYdd8hO51H/SnatbEbACuVEVDk3b2Fxt/YpWHVnJXFx+hUXfLeny5gScr6rE/OK148lJ01dJ/oKNITgs8Qwz/KqGgYE8cBrCobrkhONmwOdMWpGygtLAx4H7TNTeedOTwYxIiZE97rSkJ8Y/RCwhoHbwDrr03pkEWhIMG1JHNx0cAVMXRkcWNDaQEpvUZvlv2PR7fwB+d1ZUlmaVVfomxrUUETKw8CNFPUXWshRI7hejSdcKUNNjTYE6v5Oqrs9XWJajFOcTcfzKm9rNfX/cSh8rXDbfyLpvEbq1olqgBevDjrwtaJ83GOWtj5AfHQgCsWkddILMcLILyKMuBaUZe7HtxpIzi3R4F2+k8+buejchgWVL08Wqn//cZWqmHBo5jteuaCzqPdhNYPF0Mbs423592Qi1PGFJRwkuFSiLpK2Yo2IIYhlICUrDiwy1uyV+b52lDVMqb3IJ9BSa2/HbIjVKvjHYyxD2ErOPRk8yWTkq2WZt4KygB18PZpqg4gl9BzI4dmZUes/P1yaO1qFbHboj77yfyZ2uhI5BANn0vPC6igEP/7e780qLJkFvl9MNqOG5AWIL67nIcWskSFuA+No1Nkzj/M5YqirQMoLDXM1Q75WrAWZWEF13qoKQOr/Vt3RSKQorlxSoztCWNlJXU9kJ7IknCeX7ueLRgs8kzHcrod7DnBzs64nTdfgS0Agmzlp6i2A/rmEC8sSU+8kpnWfFdkfRQjByrCYGlug3EY/DpRkrRmz2+IRu6nf3SOJAgS2cNsMTA+C7ed+Y6wj7uhuwIR5p0nPyUZVUlgUij6u9xBi7mU8VirtED2bZBpNYum7P3ffYD3PaDxYlRHiH+7o+A81ZNK1NvQgZpGvaSMrGVa5l7mEYLulnlwosQEqmW0Az+pI+yLPveVR8cLty0MzGirER443PkDb7A1hY14UMPviaPMrliF5N6pLG9fVqHyudMbpy K4E0cZOu NCcmKbpw6XhXLAhpxyabWzroqpddVzELhUbQwR/4XdP1oKXV6yMmw1/zWkKLKEEGHdEmI3zLKvZvOGltRKvds7T/o/vgfz0gydNPM 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: If the new flag MMF_VM_MERGE_ANY has been set for a process, iterate over all the vmas and enable ksm if possible. For the vmas that can be ksm enabled this is only done once. Signed-off-by: Stefan Roesch --- mm/ksm.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/mm/ksm.c b/mm/ksm.c index 83796328574c..967eda719fab 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -1013,6 +1013,7 @@ static int unmerge_and_remove_all_rmap_items(void) mm_slot_free(mm_slot_cache, mm_slot); clear_bit(MMF_VM_MERGEABLE, &mm->flags); + clear_bit(MMF_VM_MERGE_ANY, &mm->flags); mmdrop(mm); } else spin_unlock(&ksm_mmlist_lock); @@ -2243,6 +2244,17 @@ static struct ksm_rmap_item *get_next_rmap_item(struct ksm_mm_slot *mm_slot, return rmap_item; } +static bool vma_ksm_mergeable(struct vm_area_struct *vma) +{ + if (vma->vm_flags & VM_MERGEABLE) + return true; + + if (test_bit(MMF_VM_MERGE_ANY, &vma->vm_mm->flags)) + return true; + + return false; +} + static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page) { struct mm_struct *mm; @@ -2319,8 +2331,20 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page) goto no_vmas; for_each_vma(vmi, vma) { - if (!(vma->vm_flags & VM_MERGEABLE)) + if (!vma_ksm_mergeable(vma)) continue; + if (!(vma->vm_flags & VM_MERGEABLE)) { + unsigned long flags = vma->vm_flags; + + /* madvise failed, use next vma */ + if (ksm_madvise(vma, vma->vm_start, vma->vm_end, MADV_MERGEABLE, &flags)) + continue; + /* vma, not supported as being mergeable */ + if (!(flags & VM_MERGEABLE)) + continue; + + vma->vm_flags = flags; + } if (ksm_scan.address < vma->vm_start) ksm_scan.address = vma->vm_start; if (!vma->anon_vma) @@ -2389,6 +2413,7 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page) mm_slot_free(mm_slot_cache, mm_slot); clear_bit(MMF_VM_MERGEABLE, &mm->flags); + clear_bit(MMF_VM_MERGE_ANY, &mm->flags); mmap_read_unlock(mm); mmdrop(mm); } else { From patchwork Mon Jan 23 17:37:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13112641 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 E6602C38142 for ; Mon, 23 Jan 2023 17:38:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6F7376B0082; Mon, 23 Jan 2023 12:38:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6CF9F6B0087; Mon, 23 Jan 2023 12:38:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 56E7E6B0088; Mon, 23 Jan 2023 12:38:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 4212E6B0082 for ; Mon, 23 Jan 2023 12:38:21 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id E27681A12BB for ; Mon, 23 Jan 2023 17:38:20 +0000 (UTC) X-FDA: 80386772760.03.C448322 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf27.hostedemail.com (Postfix) with ESMTP id 3662C40003 for ; Mon, 23 Jan 2023 17:38:19 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=none; spf=neutral (imf27.hostedemail.com: 66.220.144.178 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=1674495499; 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:in-reply-to:references:references; bh=dkTe6hQ1qB/aSBv2e8jZ4aQ5awR1SuR87QIIhlPyTwQ=; b=pMHsaBf35NZZT+JK9JnwcZ5f8yuXRzwA/i1YLUYLs4OUjh67ECZiw4O0qJC8FdoV/ZyGea 2OxwZIKzAbWtUi7sIMXu8F9LKBpTjNzdaSykqpzA1GMkcmLxA9dg80QyVlm6PZqqNqYdGH qzBzHDGWfgQQixWgTjbA338iFIedors= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none; spf=neutral (imf27.hostedemail.com: 66.220.144.178 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=1674495499; a=rsa-sha256; cv=none; b=gc64QCX0SDkNZZu03jQ6v3tyTx83hEJG7Rnc3dNr1cFFzMH4wSizrJf7xgOOvVHb7P72z+ 2LMe2jZhnWkL7dpUF45kT9hMYuF7wh8xVFDI3SJOHC5dXGUIvfYZvUktSQxNho1eQd/Fjp 2JlQgzbsYeq9b4okCDNdOPO55x517Ys= Received: by dev0134.prn3.facebook.com (Postfix, from userid 425415) id 530B85616BDB; Mon, 23 Jan 2023 09:37:56 -0800 (PST) From: Stefan Roesch To: linux-mm@kvack.org Cc: shr@devkernel.io, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND RFC PATCH v1 05/20] mm: support disabling of ksm for a process Date: Mon, 23 Jan 2023 09:37:33 -0800 Message-Id: <20230123173748.1734238-6-shr@devkernel.io> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230123173748.1734238-1-shr@devkernel.io> References: <20230123173748.1734238-1-shr@devkernel.io> MIME-Version: 1.0 X-Stat-Signature: x1zbq5mrgmkfumxhqubegfazuk6mir43 X-Rspam-User: X-Rspamd-Queue-Id: 3662C40003 X-Rspamd-Server: rspam06 X-HE-Tag: 1674495499-583660 X-HE-Meta: U2FsdGVkX1/TpNx6FmfPgzqnpwWJJ1ubFXziUSMULg1KblEWJElyDKB9iXs4CBYbhW0JoUSrN6OTkKR7nW7X9UGiO8t1RxRqDOKsZRNGxM4rk5nYipsxGpak4KRhxnVTnBQRobS4L/jnjmow4IhdccbM+kO6wYM3u0uIijEgOS0Xj0LAGIGqWbaCzmK6YooDpXtmhcgRjZlK25Sc4hvoLTJOI0ksJUQVj5RCp9fuoHjvgzC4lpSUs9oLNJz3/H9u4n7qPAjvhW8BsUj7tDhCNpreRWAghJCn1BuKHSPYIjrPKyV0xPEyWpXSmArzxoY6NKMxu85f9p2O2smzNoX7ddSv1DIzHMRv/ulZw0Rl1Ubhcp77dl2IGj5zSgL6S1Aqo98BMaFA7jxFTd4BKCSdWSsCIXX10pUzHBf3lm9i23271ZvxmXO2cNEKXCQsBIguLiwauGHxbBzlib65ijurbzkQV72ETzp99MVl2A02GXy+XQdPW9aOzohozWitgd35gKDfQLPUf5O4AS8Eqz5oEgMMKrdDN95rE+t7M3w4xyFCGoKSBFZ7IblmWMtOrA0EuSk7Wuzono6d/LaDQi9hWs4LqqPM15p/7wETPdGdRsyUkfan6RjbUkOlMUUDA1JTuO3D83GL1lKMeEPsPmz2NKVVgdMDfsga7W88ldO/tq9YfZ1hlYBhfkjVv8bx8MvubmIqa25l85gYwbsQ3fQ5A8EKqDEt7cLxEjNoFjjIv9OTdOVrOEDmQ7cT/8rEU8KhBegeptcPPdQukJNTwJYCQXr/V4z9xqrf5YpvSxrEo7JSj1VF7ESssIzpW1NX8ERMp+fywwYQ4q7YNHyUo9pBP8z6GTvOyELPhtOALXO7fcBafgepwz1oEzGXhxqiGP4036TYM1itHkZWuGSwJDbX4Rb3lEjQW1Imj756Y+53Ld1HKkTjUPK4tQPkYB8zJEshG16/X2SLVz/qyuTs89W nobxUj+m NxcQUAeAOQbWyoLj9R2u1H8JP2G9j6lWyv0paYOF7wdP5ft+86Lr6F9pNyQ== 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: This adds the ability to disable ksm for a process if ksm has been enabled for the process. Signed-off-by: Stefan Roesch --- mm/ksm.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/mm/ksm.c b/mm/ksm.c index 967eda719fab..5fa6b46dfa3b 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2588,6 +2588,27 @@ int __ksm_enter(struct mm_struct *mm, int flag) return 0; } +static void unmerge_vmas(struct mm_struct *mm) +{ + struct vm_area_struct *vma; + struct vma_iterator vmi; + + vma_iter_init(&vmi, mm, 0); + + mmap_read_lock(mm); + for_each_vma(vmi, vma) { + if (vma->vm_flags & VM_MERGEABLE) { + unsigned long flags = vma->vm_flags; + + if (ksm_madvise(vma, vma->vm_start, vma->vm_end, MADV_UNMERGEABLE, &flags)) + continue; + + vma->vm_flags = flags; + } + } + mmap_read_unlock(mm); +} + void __ksm_exit(struct mm_struct *mm, int flag) { struct ksm_mm_slot *mm_slot; @@ -2595,8 +2616,10 @@ void __ksm_exit(struct mm_struct *mm, int flag) int easy_to_free = 0; if (!(current->flags & PF_EXITING) && flag == MMF_VM_MERGE_ANY && - test_bit(MMF_VM_MERGE_ANY, &mm->flags)) + test_bit(MMF_VM_MERGE_ANY, &mm->flags)) { clear_bit(MMF_VM_MERGE_ANY, &mm->flags); + unmerge_vmas(mm); + } /* * This process is exiting: if it's straightforward (as is the From patchwork Mon Jan 23 17:37:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13112635 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 1D289C54EAA for ; Mon, 23 Jan 2023 17:38:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 300F76B007D; Mon, 23 Jan 2023 12:38:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1ED776B007E; Mon, 23 Jan 2023 12:38:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0167D6B0080; Mon, 23 Jan 2023 12:38:13 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id DF5946B007D for ; Mon, 23 Jan 2023 12:38:13 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id AB75BA14FD for ; Mon, 23 Jan 2023 17:38:13 +0000 (UTC) X-FDA: 80386772466.05.7A2854E Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf02.hostedemail.com (Postfix) with ESMTP id 9D68D80029 for ; Mon, 23 Jan 2023 17:38:11 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf02.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674495491; 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:in-reply-to:references:references; bh=ucXKiMMl7/A42LPHk0QGuSkEvmMvvR8fx4BZygA5Bmg=; b=0HUS+3YSxoCE1TX1PM+eH5e5e1F9UO5oZ71I0voJvNaP/hR7XXVvNob209vHQQdQFhzMPm EZkL0X1bQRBfLiZSQZvkLrehKfWsRamjCiVSR9ZjtmemlWP2LjRz2HPqwExevJsnUmyiEy BZOKfn/F471HcZZxim1JOOzMhRuKFX8= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf02.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674495491; a=rsa-sha256; cv=none; b=46tuQNJVaVn0nmNm6G0udijWeVjs08GLgtxRMTqmuXgDik+xZ8mVY7YLBkWPLQFORHylCK jvTVJCtHIyi6Q4VZWpjnLD2LtCKA67++iFkAuIqy+kU66UbqNCxFB3Og0ax8sJ7F0AQPEn y4pP0UjIdKsU5cFYUlECWNu4d2CB9ZU= Received: by dev0134.prn3.facebook.com (Postfix, from userid 425415) id 572115616BDD; Mon, 23 Jan 2023 09:37:56 -0800 (PST) From: Stefan Roesch To: linux-mm@kvack.org Cc: shr@devkernel.io, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND RFC PATCH v1 06/20] mm: add new prctl option to get and set ksm for a process Date: Mon, 23 Jan 2023 09:37:34 -0800 Message-Id: <20230123173748.1734238-7-shr@devkernel.io> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230123173748.1734238-1-shr@devkernel.io> References: <20230123173748.1734238-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Queue-Id: 9D68D80029 X-Stat-Signature: 4td6pp1jkq6od6wwioxx4bfk8449tist X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1674495491-17009 X-HE-Meta: U2FsdGVkX18vf8kxaACfChxK6AWDrAJD6mQEjPc6oqjmeLfKEVNMdie9SXVVtJipcGzAi8hTRFaXoYBz5n/0HSgS4pwoTjn4GF8nhs16/Fv6QwyMXD/m2cknPAGioYEHy/iztpWUUzA6rO7j417EjHrYUnMZLqCdEFdgWwtI+Q6tpWON4xILCk4M70qzKzOWSLAfLa5liPV307cFcrVfRioyQ1tw8bR7hmiMKVPY8ebIQyRkMrO80Tk3jPHnPfQJHyxzyLgMiGr8VH3uUJjL+15bwQlzghRcW5OYf8Agy10fiNQeMUM5L5WcxcTBcNxVBtHEeNnI3Wg1oMkV5Dh22szmaVLmVGGz/LsIs1loNI2Tq9A1GctgQVJZM1sVLnU+tpqRF6NMH3tdlcgtcCzOLF495SFvgwR5qis256lcRGX7Erg3/eHwh1ThUCvxtcR5bihi4IDqf6/HjlRM+ng3mjJeTYn7j5zF3choYbuUNAnqeTGaeAgKEWmC/Gf3dKBzbrc0oxeh4MNuHh+0r1UxQBBPomxTqo4iJ5PD9HnLCovrYTDskdzzp4zwzLHYiw5UqmaTiyYYs6XtVAAYaD92yoV5HAyu10SR3kqymob95kOMl7TB6Hv6K/3krNEp3moUPGUiWUibw10HfY8HiKH7Hx6jSEnM/Zj/BNiGeDtHYQWN59CyE4CQwTmjjc+1dhPYoWglsELNh4cBFU64UvJK/sTozJPganBY/aa4S4F2rST3npChE3aURZtpm5T0d+XJ7IzYXXSnu6qnsv2kSvCTKUsxGpFwVj7JDet2PaK9Vy0mL2fjUKohQrHoUiAIuxcdzYfK1UIFKhlAliNrcGDqJY8/dhDmM3QWQoBESffiKyGzqazAIbfPJt/GVOppCjGj9vIGCHSZ/Eq1P74ahQxqyz3+xMpBxsTgdnUwc8wp5/vtuAHO8QKxBCQwfSIDuwsxQt2XLPbEOWOfpVsTluz 4RFOmx/V P7Z3URU5Lfn3sn/CAMFM8kaSDr/QjNavelv1LlJiaDpKuZux1RIrEHULIy2RDeVpYtOs4iwQ2/Qu77CY= 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: This adds two new options to the prctl system call - enable ksm for all vmas of a process (if the vmas support it). - query if ksm has been enabled for a process. Signed-off-by: Stefan Roesch --- include/uapi/linux/prctl.h | 2 ++ kernel/sys.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index a5e06dcbba13..b8ba85f18e43 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -284,4 +284,6 @@ struct prctl_mm_map { #define PR_SET_VMA 0x53564d41 # define PR_SET_VMA_ANON_NAME 0 +#define PR_SET_MEMORY_MERGE 65 +#define PR_GET_MEMORY_MERGE 66 #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index 5fd54bf0e886..de218e3b6f42 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -2626,6 +2627,34 @@ 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: + if (!capable(CAP_SYS_RESOURCE)) + return -EPERM; + + if (arg2) { + if (mmap_write_lock_killable(me->mm)) + return -EINTR; + + if (test_bit(MMF_VM_MERGEABLE, &me->mm->flags)) + error = -EINVAL; + else if (!test_bit(MMF_VM_MERGE_ANY, &me->mm->flags)) + error = __ksm_enter(me->mm, MMF_VM_MERGE_ANY); + mmap_write_unlock(me->mm); + } else { + __ksm_exit(me->mm, MMF_VM_MERGE_ANY); + } + break; + case PR_GET_MEMORY_MERGE: + if (!capable(CAP_SYS_RESOURCE)) + return -EPERM; + + if (arg2 || arg3 || arg4 || arg5) + return -EINVAL; + + error = !!test_bit(MMF_VM_MERGE_ANY, &me->mm->flags); + break; +#endif default: error = -EINVAL; break; From patchwork Mon Jan 23 17:37:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13112638 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 91615C25B50 for ; Mon, 23 Jan 2023 17:38:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B3F046B0080; Mon, 23 Jan 2023 12:38:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AED376B0081; Mon, 23 Jan 2023 12:38:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 945026B0082; Mon, 23 Jan 2023 12:38:17 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 6EBA26B0081 for ; Mon, 23 Jan 2023 12:38:17 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 4285A1A2BB2 for ; Mon, 23 Jan 2023 17:38:17 +0000 (UTC) X-FDA: 80386772634.19.7F6CA52 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf04.hostedemail.com (Postfix) with ESMTP id 852E740021 for ; Mon, 23 Jan 2023 17:38:15 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=none; spf=neutral (imf04.hostedemail.com: 66.220.144.178 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=1674495495; a=rsa-sha256; cv=none; b=Gt3yU12Dwiyd1vXM99a0ocmU+doz38/Z6xo+fRkYvjeH/XHlG6NKBVD6EHOHontZ9ElfMU 6O0xBO8nTO5wtVh4F0H4aJSmyXsHaVOnL4XbrStDWt2qeArfGB5QIgg6+9R+nrQ8V0Uq9u RgWdlSAQc9SughsouBRy1BZ0nSoGGgs= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=none; spf=neutral (imf04.hostedemail.com: 66.220.144.178 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=1674495495; 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:in-reply-to:references:references; bh=Q+yy9dVQzb3LP9jKQiRsz9lchW7Eo0LuwsAlK7jS7SI=; b=6aeXpERvJYZ4kIi8HnMGFDeakgHA+DCqsJlSGJ790z6j52Xs4E/661QFKKgP7L848xn1Qn tXtZ4Y4lZF9BIfEMpIjbensfXKAA0NLENk7CI9eZRmvRha+Il8ZSIS/evgu1x8KyGJrfL4 /xQdvi7eG0BL1q1TO/8kBfCIblisJtk= Received: by dev0134.prn3.facebook.com (Postfix, from userid 425415) id 5D2035616BDF; Mon, 23 Jan 2023 09:37:56 -0800 (PST) From: Stefan Roesch To: linux-mm@kvack.org Cc: shr@devkernel.io, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND RFC PATCH v1 07/20] mm: add tracepoints to ksm Date: Mon, 23 Jan 2023 09:37:35 -0800 Message-Id: <20230123173748.1734238-8-shr@devkernel.io> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230123173748.1734238-1-shr@devkernel.io> References: <20230123173748.1734238-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 852E740021 X-Rspamd-Server: rspam01 X-Stat-Signature: qdreewxisxfz43ions378kjoex7kckkm X-HE-Tag: 1674495495-860636 X-HE-Meta: U2FsdGVkX18KfcQx/qXQxKKGHCzpzihxek26FjJaGFNLsIM8TitxijkvVew8x3dg0a1vuieDGlhxzHpCX/cni9r6l57VCc4DI815M7xXUk/LmjQJ3v7bDwPDni1mxmN9nZKN2sL73el8TnnuWuqD5Maq9DNe2sQZ8byiBLokOGZrJz9i8PFbw/lS5k+DOkjl5CCD01OCf2L2P7ydl/oB86VTjBuSrNpbs0m+D68+lusC7CpOFxxZVuo6er1LHOPZEzXYjGWq4F2LocOK6FxtGBlmdyJ47UodSJUi8i/kcZjpGemX1AsxYT3irCxg0YHMx8ENshBAhZL/5oTUClX8IERn62+VBbzSOIIAcSI7AD4KSBrVFUOF4ZDqWvqv9zYJ3tGnnO9gUtXmVENfFu4P7sKBggDcAylwZ/x9iWyKrMElfhym8Lieg0lqW6m+l4nRrXy2SEro8xWybcgHpoEHGIwsW0JQPUplVNgEpm+IfvE4I5PCoZWdtYAE9L/o7wR/D52tmHGKiyG8qYldgEN7cWX9MlnS3Y/xvo89pxKbJ8HKwqLHOhcxdHYD8EimU4G3JyxacK8cR6vjY0mYtENfbh0SGtdA8vlseWp7FvgKi4iNRAvzYB8ygW1C/mcxdB56Q5DmzABz8SJ5Jh1Df0Uewk07iKFUtuO3h8BfyRA851zADB+Ipe/4w3Zm0r8lawXxPuuQ5rHEncm7tw631tYnHDQ0a/dBzzOryoy2B6huBuKnfgE2BFS8grc6e8KUwmXm6aN5I8lCsxYZ9RuFj1WT/RaT6CPnPWJ4NMxKEhVCC75PWmTIbnS+J201HG2clsKjwj/BenSEnSZXxArCbXl/XUWijIsZnNfypnE91TBjTxSULpNBhtLAL+D1BeNgMHyMnlwvBUwTq55QbMAE93L5nIROS3qTNcBMDVEUtIZtK9AhfkinO42zJEcGIo0zPBXlmqiPGhN/AcxpUhm5im3 S1Mu5bJD czqHDtC8F7ClXbqRWDPz5azsrigofH+2inRQ3bdoe9aaWJns= 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: This adds the following tracepoints to ksm: - start / stop scan - ksm enter / exit - merge a page - merge a page with ksm - remove a page - remove a rmap item Signed-off-by: Stefan Roesch Reviewed-by: Steven Rostedt (Google) --- MAINTAINERS | 1 + include/trace/events/ksm.h | 257 +++++++++++++++++++++++++++++++++++++ mm/ksm.c | 20 ++- 3 files changed, 276 insertions(+), 2 deletions(-) create mode 100644 include/trace/events/ksm.h diff --git a/MAINTAINERS b/MAINTAINERS index 123216b76534..990a28bdc263 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -13482,6 +13482,7 @@ F: include/linux/memory_hotplug.h F: include/linux/mm.h F: include/linux/mmzone.h F: include/linux/pagewalk.h +F: include/trace/events/ksm.h F: mm/ F: tools/testing/selftests/vm/ diff --git a/include/trace/events/ksm.h b/include/trace/events/ksm.h new file mode 100644 index 000000000000..727d8f6dbc3f --- /dev/null +++ b/include/trace/events/ksm.h @@ -0,0 +1,257 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM ksm + +#if !defined(_TRACE_KSM_H) || defined(TRACE_HEADER_MULTI_READ) +#define _TRACE_KSM_H + +#include + +/** + * ksm_scan_template - called for start / stop scan + * + * @seq: sequence number of scan + * @rmap_entries: actual number of rmap entries + * + * Allows to trace the start / stop of a ksm scan. + */ +DECLARE_EVENT_CLASS(ksm_scan_template, + + TP_PROTO(int seq, u32 rmap_entries), + + TP_ARGS(seq, rmap_entries), + + TP_STRUCT__entry( + __field(int, seq) + __field(u32, rmap_entries) + ), + + TP_fast_assign( + __entry->seq = seq; + __entry->rmap_entries = rmap_entries; + ), + + TP_printk("seq %d rmap size %d", + __entry->seq, __entry->rmap_entries) +); + +/** + * ksm_start_scan - called after a new ksm scan is started + * + * @seq: sequence number of scan + * @rmap_entries: actual number of rmap entries + * + * Allows to trace the start of a ksm scan. + */ +DEFINE_EVENT(ksm_scan_template, ksm_start_scan, + + TP_PROTO(int seq, u32 rmap_entries), + + TP_ARGS(seq, rmap_entries) +); + +/** + * ksm_stop_scan - called after a new ksm scan has completed + * + * @seq: sequence number of scan + * @rmap_entries: actual number of rmap entries + * + * Allows to trace the completion of a ksm scan. + */ +DEFINE_EVENT(ksm_scan_template, ksm_stop_scan, + + TP_PROTO(int seq, u32 rmap_entries), + + TP_ARGS(seq, rmap_entries) +); + +/** + * ksm_enter - called after a new process has been added / removed from ksm + * + * @mm: address of the mm object of the process + * @flag: specified flags + * + * Allows to trace the when a process has been added or removed from ksm. + */ +DECLARE_EVENT_CLASS(ksm_enter_exit_template, + + TP_PROTO(void *mm, unsigned int flag), + + TP_ARGS(mm, flag), + + TP_STRUCT__entry( + __field(void *, mm) + __field(unsigned int, flag) + ), + + TP_fast_assign( + __entry->mm = mm; + __entry->flag = flag; + ), + + TP_printk("mm %p flag %u", + __entry->mm, __entry->flag) +); + +/** + * ksm_enter - called after a new process has been added to ksm + * + * @mm: address of the mm object of the process + * @flag: specified flags + * + * Allows to trace the when a process has been added to ksm. + */ +DEFINE_EVENT(ksm_enter_exit_template, ksm_enter, + + TP_PROTO(void *mm, unsigned int flag), + + TP_ARGS(mm, flag) +); + +/** + * ksm_exit - called after a new process has been removed from ksm + * + * @mm: address of the mm object of the process + * @flag: specified flags + * + * Allows to trace the when a process has been removed from ksm. + */ +DEFINE_EVENT(ksm_enter_exit_template, ksm_exit, + + TP_PROTO(void *mm, unsigned int flag), + + TP_ARGS(mm, flag) +); + +/** + * ksm_merge_one_page - called after a page has been merged + * + * @pfn: page frame number of ksm page + * @rmap_item: address of rmap_item object + * @mm: address of the process mm struct + * @err: success + * + * Allows to trace the ksm merging of individual pages. + */ +TRACE_EVENT(ksm_merge_one_page, + + TP_PROTO(unsigned long pfn, void *rmap_item, void *mm, int err), + + TP_ARGS(pfn, rmap_item, mm, err), + + TP_STRUCT__entry( + __field(unsigned long, pfn) + __field(void *, rmap_item) + __field(void *, mm) + __field(int, err) + ), + + TP_fast_assign( + __entry->pfn = pfn; + __entry->rmap_item = rmap_item; + __entry->mm = mm; + __entry->err = err; + ), + + TP_printk("ksm pfn %lu rmap_item %p mm %p error %d", + __entry->pfn, __entry->rmap_item, __entry->mm, __entry->err) +); + +/** + * ksm_merge_with_ksm_page - called after a page has been merged with a ksm page + * + * @ksm_page: address ksm page + * @pfn: page frame number of ksm page + * @rmap_item: address of rmap_item object + * @mm: address of the mm object of the process + * @err: success + * + * Allows to trace the merging of a page with a ksm page. + */ +TRACE_EVENT(ksm_merge_with_ksm_page, + + TP_PROTO(void *ksm_page, unsigned long pfn, void *rmap_item, void *mm, int err), + + TP_ARGS(ksm_page, pfn, rmap_item, mm, err), + + TP_STRUCT__entry( + __field(void *, ksm_page) + __field(unsigned long, pfn) + __field(void *, rmap_item) + __field(void *, mm) + __field(int, err) + ), + + TP_fast_assign( + __entry->ksm_page = ksm_page; + __entry->pfn = pfn; + __entry->rmap_item = rmap_item; + __entry->mm = mm; + __entry->err = err; + ), + + TP_printk("%spfn %lu rmap_item %p mm %p error %d", + (__entry->ksm_page ? "ksm " : ""), + __entry->pfn, __entry->rmap_item, __entry->mm, __entry->err) +); + +/** + * ksm_remove_ksm_page - called after a ksm page has been removed + * + * @pfn: page frame number of ksm page + * + * Allows to trace the removing of stable ksm pages. + */ +TRACE_EVENT(ksm_remove_ksm_page, + + TP_PROTO(unsigned long pfn), + + TP_ARGS(pfn), + + TP_STRUCT__entry( + __field(unsigned long, pfn) + ), + + TP_fast_assign( + __entry->pfn = pfn; + ), + + TP_printk("pfn %lu", __entry->pfn) +); + +/** + * ksm_remove_rmap_item - called after a rmap_item has been removed from the + * stable tree + * + * @pfn: page frame number of ksm page + * @rmap_item: address of rmap_item object + * @mm: address of the process mm struct + * + * Allows to trace the removal of pages from the stable tree list. + */ +TRACE_EVENT(ksm_remove_rmap_item, + + TP_PROTO(unsigned long pfn, void *rmap_item, void *mm), + + TP_ARGS(pfn, rmap_item, mm), + + TP_STRUCT__entry( + __field(unsigned long, pfn) + __field(void *, rmap_item) + __field(void *, mm) + ), + + TP_fast_assign( + __entry->pfn = pfn; + __entry->rmap_item = rmap_item; + __entry->mm = mm; + ), + + TP_printk("pfn %lu rmap_item %p mm %p", + __entry->pfn, __entry->rmap_item, __entry->mm) +); + +#endif /* _TRACE_KSM_H */ + +/* This part must be outside protection */ +#include diff --git a/mm/ksm.c b/mm/ksm.c index 5fa6b46dfa3b..88a969849955 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -45,6 +45,9 @@ #include "internal.h" #include "mm_slot.h" +#define CREATE_TRACE_POINTS +#include + #ifdef CONFIG_NUMA #define NUMA(x) (x) #define DO_NUMA(x) do { (x); } while (0) @@ -633,10 +636,12 @@ static void remove_node_from_stable_tree(struct ksm_stable_node *stable_node) BUG_ON(stable_node->rmap_hlist_len < 0); hlist_for_each_entry(rmap_item, &stable_node->hlist, hlist) { - if (rmap_item->hlist.next) + if (rmap_item->hlist.next) { + trace_ksm_remove_rmap_item(stable_node->kpfn, rmap_item, rmap_item->mm); ksm_pages_sharing--; - else + } else { ksm_pages_shared--; + } rmap_item->mm->ksm_merging_pages--; @@ -657,6 +662,7 @@ static void remove_node_from_stable_tree(struct ksm_stable_node *stable_node) BUILD_BUG_ON(STABLE_NODE_DUP_HEAD <= &migrate_nodes); BUILD_BUG_ON(STABLE_NODE_DUP_HEAD >= &migrate_nodes + 1); + trace_ksm_remove_ksm_page(stable_node->kpfn); if (stable_node->head == &migrate_nodes) list_del(&stable_node->list); else @@ -1319,6 +1325,8 @@ static int try_to_merge_with_ksm_page(struct ksm_rmap_item *rmap_item, get_anon_vma(vma->anon_vma); out: mmap_read_unlock(mm); + trace_ksm_merge_with_ksm_page(kpage, page_to_pfn(kpage ? kpage : page), + rmap_item, mm, err); return err; } @@ -2137,6 +2145,8 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite if (vma) { err = try_to_merge_one_page(vma, page, ZERO_PAGE(rmap_item->address)); + trace_ksm_merge_one_page(page_to_pfn(ZERO_PAGE(rmap_item->address)), + rmap_item, mm, err); } else { /* * If the vma is out of date, we do not need to @@ -2270,6 +2280,8 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page) mm_slot = ksm_scan.mm_slot; if (mm_slot == &ksm_mm_head) { + trace_ksm_start_scan(ksm_scan.seqnr, ksm_rmap_items); + /* * A number of pages can hang around indefinitely on per-cpu * pagevecs, raised page count preventing write_protect_page @@ -2433,6 +2445,7 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page) if (mm_slot != &ksm_mm_head) goto next_mm; + trace_ksm_stop_scan(ksm_scan.seqnr, ksm_rmap_items); ksm_scan.seqnr++; return NULL; } @@ -2585,6 +2598,7 @@ int __ksm_enter(struct mm_struct *mm, int flag) if (needs_wakeup) wake_up_interruptible(&ksm_thread_wait); + trace_ksm_enter(mm, flag); return 0; } @@ -2653,6 +2667,8 @@ void __ksm_exit(struct mm_struct *mm, int flag) mmap_write_lock(mm); mmap_write_unlock(mm); } + + trace_ksm_exit(mm, flag); } struct page *ksm_might_need_to_copy(struct page *page, From patchwork Mon Jan 23 17:37:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13112640 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 A3E7CC05027 for ; Mon, 23 Jan 2023 17:38:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F33C16B0081; Mon, 23 Jan 2023 12:38:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EE4BB6B0082; Mon, 23 Jan 2023 12:38:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D83BA6B0087; Mon, 23 Jan 2023 12:38:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id C6FC26B0081 for ; Mon, 23 Jan 2023 12:38:18 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 9F289822BE for ; Mon, 23 Jan 2023 17:38:18 +0000 (UTC) X-FDA: 80386772676.30.E23937E Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf17.hostedemail.com (Postfix) with ESMTP id 01EE84002E for ; Mon, 23 Jan 2023 17:38:16 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf17.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674495497; 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:in-reply-to:references:references; bh=0zP3lOdxr+0UCksWUl3LEQ9z7YRL6nH+bNz8mNUcGwc=; b=uwzYT/dW8ChtNL20sz0CXzhKjSPM9/5jCkt0FjqLLMbnTbgTub5uEOuRVqevjOt0SfdCl2 wUzIU04QjHhuQejKUuelUGjfMUx7a1bTcoHMJfwJPXA4a/adVVfR5B+esSb9bpm2W/NUT8 ihOWx9t63XwV93F3q+T6g+Tx6gO60cY= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf17.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674495497; a=rsa-sha256; cv=none; b=RL+bqaVGWZNgxVSKJOOdLwCswmGhtCufothXjZxt6yMQQ9TRN0eO5TxrV6TvJN+LNDBF2X bXzhb+9m+wjXBk7SFJSSeveUa2U7CaYGPOD/iBqRKdVP3h5P5fkj9YB5DKeD41qL9tFeNf SJtRcYHAjs19fIiyIKKld4Nfl2jDz6I= Received: by dev0134.prn3.facebook.com (Postfix, from userid 425415) id 5F7EA5616BE1; Mon, 23 Jan 2023 09:37:56 -0800 (PST) From: Stefan Roesch To: linux-mm@kvack.org Cc: shr@devkernel.io, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND RFC PATCH v1 08/20] mm: split off pages_volatile function Date: Mon, 23 Jan 2023 09:37:36 -0800 Message-Id: <20230123173748.1734238-9-shr@devkernel.io> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230123173748.1734238-1-shr@devkernel.io> References: <20230123173748.1734238-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 01EE84002E X-Stat-Signature: tufn9j5of8tfdo4dmwngw3kziqdoyhqx X-HE-Tag: 1674495496-478371 X-HE-Meta: U2FsdGVkX1/SkuxGZ/IvOl07GgHY2+1QWYc09UVvrcwZPOmNJWVS2pMOaE5573UHocAc/3mX9SjVoWY64odEstmgteiFNrryHfvMJbBfCzEc8yH0FqkVj/5iRP0SOXWamICp0FCDTnSn5NzdnzXNBRlXvoSDitoljR3CF60FWE3AGCczi1pHXQudrotpBE1JpPu+b09Kt/20uc+Bra4o2/wyWe5jWdQ4N7gs6oZ75qByixRZ7QLBc1xcqCbNBiuO3WsE2caTmxALwrf4t20CbKbdCCoUXuUI6HH76AcShlYB9hhLBPNM+awxS+RFehWHG1AZcQTQ31AknFwyA4wHgx5O13+r4/U9B4+OzNU67qr95zk1I5t/m7IXQzTM5ljjuG03gsySsHIAtAVDvvS+PrpXHVJxS9DyRzlu3uLwa9M84ZY4UMGq+tOJswLH6rZRsfCJOONFuFaYiunW0+cKcv0rZ0oDddOqrHMxkVwsGiD6MjkNt5lINuiqpFZcnhHai7UIMQGqayKuX/viLdcE6C0kalQktD/l/fSDecJE2Nc2xijt0WxfipJ18Blj5JjX1vJ9Yyy5tG9/yOz1E66DkXjCOo2/iILZ2VglG5s3pc7ZvBsVw/LiIOKzyH6MI0dD4m6F5DwHMgIWzOd2k/68of1IIcwtdMMDa7R01v4qB3d8JetEu+z+xPaQXgFCzTWnRYSjyTRx2Qk6vq8Mk7nZvOL9y0PcMNeY/E593AZjpAr4C/lmyPYe7qQv+UmCu0fVsSBGjAM6+p8au6ZNxl9m/2cBpi7YyybmAIH4/hO6vj8KH1KQvLV7SNUk/RDDWXmgIkwExXrYqoZgVT25ujeXEzysBvTdgo3jiazfQ9Aj7MKEMaNeD/w5G2uxntHzf9OVZAO7SBQKYLjRGsY02aJ7mkt2i7yp7gGEWMk6NGkbV9O4UTquqcpLApmBj2sZ1DjfWn/wb0qQk397/BJ8OCy zOgl79IQ t402wgvkSe3/g2zOWsU6tYySE6c6i2Vckqz0TOHb16UXpdIKl+SLYza9AWicsNmoxcZvpjE8rdw4S3DfzyJdDcdupAZBbFpthc4to 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: This splits off the pages_volatile function. The next patch will use this function. Signed-off-by: Stefan Roesch --- mm/ksm.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 88a969849955..19f434f97dc1 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -3182,8 +3182,7 @@ static ssize_t pages_unshared_show(struct kobject *kobj, } KSM_ATTR_RO(pages_unshared); -static ssize_t pages_volatile_show(struct kobject *kobj, - struct kobj_attribute *attr, char *buf) +static long pages_volatile(void) { long ksm_pages_volatile; @@ -3195,7 +3194,14 @@ static ssize_t pages_volatile_show(struct kobject *kobj, */ if (ksm_pages_volatile < 0) ksm_pages_volatile = 0; - return sysfs_emit(buf, "%ld\n", ksm_pages_volatile); + + return ksm_pages_volatile; +} + +static ssize_t pages_volatile_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%ld\n", pages_volatile()); } KSM_ATTR_RO(pages_volatile); From patchwork Mon Jan 23 17:37:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13112642 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 4616FC25B50 for ; Mon, 23 Jan 2023 17:38:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B3C0E6B0087; Mon, 23 Jan 2023 12:38:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AECCA6B0088; Mon, 23 Jan 2023 12:38:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 80E786B0089; Mon, 23 Jan 2023 12:38:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 61E616B0087 for ; Mon, 23 Jan 2023 12:38:22 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 2EC9A1C6B8B for ; Mon, 23 Jan 2023 17:38:22 +0000 (UTC) X-FDA: 80386772844.18.EB1C4FD Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf11.hostedemail.com (Postfix) with ESMTP id 4EA3540020 for ; Mon, 23 Jan 2023 17:38:20 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=none; spf=neutral (imf11.hostedemail.com: 66.220.144.178 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=1674495500; a=rsa-sha256; cv=none; b=CzI70CpTWtkHLc0KeiJozmkDejo8RuHhukr1j1M0v8+ovhCL3bX2KL73vBjGOLoDgGaIgR Cu5NgYVBw486zTHL8mrlyHxHOHIphS9beGeFGCn6zOceHNUp4QHdsVWCc3h5rUH/tEmjVC 7rx0/FrLl3koRwalrMrGZ3KoqXAEjGs= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=none; spf=neutral (imf11.hostedemail.com: 66.220.144.178 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=1674495500; 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:in-reply-to:references:references; bh=BL99fR2JiovciqZqm6mH4hPxhKKNFtoaiqSvJzgiEIg=; b=Al3VnFUx9LPaQ0b7P5lwkuskcXafAUdsUeMlk1Arr0VdRQhOJ7qXxB4y4XeYMv4GI29Aqq PNAuoPUI6YjJF9qGxP9c3LlA5tTOtYbcTOM42cN1hh726duGrbQ2nklDd7uHLLmLeq54ir bznHNNDpL7jjXxJxElGFiHCC+RjOs/U= Received: by dev0134.prn3.facebook.com (Postfix, from userid 425415) id 636A05616BE3; Mon, 23 Jan 2023 09:37:56 -0800 (PST) From: Stefan Roesch To: linux-mm@kvack.org Cc: shr@devkernel.io, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND RFC PATCH v1 09/20] mm: expose general_profit metric Date: Mon, 23 Jan 2023 09:37:37 -0800 Message-Id: <20230123173748.1734238-10-shr@devkernel.io> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230123173748.1734238-1-shr@devkernel.io> References: <20230123173748.1734238-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 4EA3540020 X-Rspamd-Server: rspam01 X-Stat-Signature: 8di1u35515wh1fcuta7d454yesqrzzey X-HE-Tag: 1674495500-679015 X-HE-Meta: U2FsdGVkX19ipMVMJbQfjy2VX2p4syp1lHQChKnnpLLhA8aqvwBtcVYZCZcF+5fj/DTeOeHbws4lwQE6sYsEwZf/C5IhrYzQMmK0Tn9KRdMyulV8v+ZWzS5DoErCZSBvsXHRAcpaVxVAlrR0K+ngDA79as3vV8Vyh+51riRelBa4ZEr0FsHo/aXL65oE8fIB9w8QQBINQ6B8TIXpyjpViD2rbYwMiN/Pv2APT/uv/h0/f/dJ/ssZh2YKPRFfFUSkWLe+qn0MI+fhPtoENolgiaKe3SssOPSCqj2TcGVEjV+JeKoMUlu7QaSLpXtIDjvqg/iZXmoaJ4tnWNu3WLTU6KSM0ZF3AOb7rgA3mdwmkXLc2lb2j5OQYiWlZ0QArKMi+m0bq0si+Aeeyiy1dfSblKrRPTkcYm4xjOZO8QhFU7XmL5MCzGD9EBz5aD6HSr4X4lsvAaBbpKgHCA/yST03v7SQrr7a3650kVyazrMTC3CJ85SfcacEeBsTarUuuTiKN7KfIGNPTMUmosrwx8VUr2R+gp1KCU1eLivqNNxmwX5ppUkfsuaVCNs78h4mWyYmy/t/JKHoay7RzZgv8PRAyLqfX+6zK2iyVNdJ1UyBnYao8FTW+WGJU29DKWFbCHf22jmYH4Rk+XCp3IOpe4ElPTnuiAwAXXyFPa+OOfByYTCblhSzUboHrqvUH3+Er8bOKS+Vd75YbwO8XfCV+Dm86l0CwxyTFrbbWtg3rR0YKlu88G0y6lNzxop5+v8VqVY7aCi5o+eoZXfxAZm6mkYNRZ6mwaXYIkoe446nrlrW/NPAq5M2y62tajO1sZ5A1Z7krxqGvU+oTndGj9v4MUqM/yvAO+gBayDXC8T9+N1WV3Hc8KzVXbPnfBPFCrke22pyutfrp2ib+Y7zkMXj5u6ty6TRZ3O7Fg17Ho1LDVeZnMkQgjNwA1HfP4srfSc+igEFfmwv6mX4d5Ee1xMSeKz T1IpeveF XaLa/WgkmqHDmrJznvOue0t6PnBKrl/vVvw8E0HC9T4q8U2gmKABbj0u3stAa+axNcLU1B0QQXO/VBNyIwH4SYbAMy38s2cKXTQwJ 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: The documentation mentions a general profit metric, however this metric is not calculated. In addition the formula depends on the size of internal structures, which makes it more difficult for an administrator to make the calculation. Adding the metric for a better user experience. Signed-off-by: Stefan Roesch --- mm/ksm.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/mm/ksm.c b/mm/ksm.c index 19f434f97dc1..d29454a802a0 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -3205,6 +3205,21 @@ static ssize_t pages_volatile_show(struct kobject *kobj, } KSM_ATTR_RO(pages_volatile); +static ssize_t general_profit_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + long general_profit; + long all_rmap_items; + + all_rmap_items = ksm_max_page_sharing + ksm_pages_shared + + ksm_pages_unshared + pages_volatile(); + general_profit = ksm_pages_sharing * PAGE_SIZE - + all_rmap_items * sizeof(struct ksm_rmap_item); + + return sysfs_emit(buf, "%ld\n", general_profit); +} +KSM_ATTR_RO(general_profit); + static ssize_t stable_node_dups_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { @@ -3269,6 +3284,7 @@ static struct attribute *ksm_attrs[] = { &stable_node_dups_attr.attr, &stable_node_chains_prune_millisecs_attr.attr, &use_zero_pages_attr.attr, + &general_profit_attr.attr, NULL, }; From patchwork Mon Jan 23 17:37:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13112644 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 170B6C05027 for ; Mon, 23 Jan 2023 17:38:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8F2256B0089; Mon, 23 Jan 2023 12:38:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8A3B86B008A; Mon, 23 Jan 2023 12:38:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 76BF86B008C; Mon, 23 Jan 2023 12:38:29 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 630356B0089 for ; Mon, 23 Jan 2023 12:38:29 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 3F9CC161327 for ; Mon, 23 Jan 2023 17:38:29 +0000 (UTC) X-FDA: 80386773138.19.CDB136E Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf21.hostedemail.com (Postfix) with ESMTP id 15C6F1C002B for ; Mon, 23 Jan 2023 17:38:26 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=none; spf=neutral (imf21.hostedemail.com: 66.220.144.178 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=1674495507; 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:in-reply-to:references:references; bh=CA0SVXvV73URjako0cuzE5C6ugRhDCRksA+GC3YJ0zQ=; b=aNoKarWJ6YmC1uWMKjcbPqJ+zFRNl7E6BEmfPM2foNymSR5rLv5z5rLAdHwC4854BVMnRG FIZH2scnqrrT3suqIOcutNbIwijXRGF+c5HfeqO6rNfoHO0opCPJNWgkeXUC1cdL0+RSlP e+Qi79j4jWcuyXM90ynmu6qAKmq2TVg= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=none; spf=neutral (imf21.hostedemail.com: 66.220.144.178 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=1674495507; a=rsa-sha256; cv=none; b=ycrYRi9B/PI7bv/ncM1OyDVAKoX9A3YsG02MF+V+qr66+Fy4hnDhyqOpk6XfubKeN+qKbx U1W96MYAZrbqsvd2LFads9fngJIjHQtHWUoG6nbGgsB3eG8VoTl0SJO+Yi12PMobChs8MO 9YaCn1T7q77dFxOpUKqFGTrzMETSQ7g= Received: by dev0134.prn3.facebook.com (Postfix, from userid 425415) id 67ABE5616BE5; Mon, 23 Jan 2023 09:37:56 -0800 (PST) From: Stefan Roesch To: linux-mm@kvack.org Cc: shr@devkernel.io, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND RFC PATCH v1 10/20] docs: document general_profit sysfs knob Date: Mon, 23 Jan 2023 09:37:38 -0800 Message-Id: <20230123173748.1734238-11-shr@devkernel.io> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230123173748.1734238-1-shr@devkernel.io> References: <20230123173748.1734238-1-shr@devkernel.io> MIME-Version: 1.0 X-Stat-Signature: fh17d4bg9gh5wp6gs7ny8rgc3girsjwg X-Rspam-User: X-Rspamd-Queue-Id: 15C6F1C002B X-Rspamd-Server: rspam06 X-HE-Tag: 1674495506-884728 X-HE-Meta: U2FsdGVkX19egJkYnDJjqGr1RV6noh82Ml7stMOylGX+YyL0KEXPEHMdvfrogLqBhoPL7lLhc7v9w+9D83cMEX67NnncVwkHPb9UpsNB+wME7fHt6O6LMUmRIsxfDcz9pLS7RmXg5jU0NR2CsmNE+vfqfvBsVgq5pt2EEIeQEqfjiCKqQKCBk4zIlU24VBIYVduYJaQHaHs4RnFcxT/ueQJkHs9lMCb9+LGLJKI3rUc3IhhPOM6IeBEcxVqrAFZBGIUlNadsoQCSKu4TM92q+csSDDhmIEgrJXXxb2e1/WAfI7gVXwwiWly6K4RR/AWeJ+avDm+Og7W0hajUDAS3cExjVk5E4QlXX+GuYmkjWW0tMva/8KqypjXlS8ZPqxsw46fs83kOoeqEEobwMTpR1b5RqEwvBnBlCNRsYCHv2x3HoZ//dJyhT74IEwSK/uQJX9tz3pNHoSSFxIMzR3PfNiFn/XkiNbRzQIAQ1Bz/1LtxCi3nczZvGq0SFHETSfMLuLsx6XDdrZWkFnDEGsJ9qCnpf8V23kMkfNiv8qJ+V+29C7ZQ6pRJAbHfvo922S5UaXTPIxZKfKUMX3PR01qfs8joCOKYyLR7AG9gbUCUvpwjdE7hTitRFGaAZu4v0t0xsNKlV9K3cMjryZF1Ap/o3sAJStUFwMyuhNq0kF2fGwMBY/HHD5f8+yipmjk4CmxFXo64cnQKiWRtJ0llqzY5+8JgR6dJB24NMEafA4K2W6nFI96Jy4SL8Jb6sRoa17QqjNTcT9waCWG9IOcdlkb17V6kmajupWOoGG8X7EOfn4gNsno8FAz4t8E/Eamb2ZaIRg1upjTxh2CZ53IpbjUSlRa7w0y5Hn1Bg0XTnSjZNIEFrTOF64kzspfuhRpolYuPkpUyhcCNg5E8TRcChFrRi7UTv1d5d0czei3fG1XYtujEv1dUq3QhkDkkHZuMbmTTg4pTENA9HrUtK0bEMwx fDzY+X6X 0PX4ArLk0BKswvfzo++EAp0VDWgVZgJHBEv5U/OnhWnBR/gcSCNuC0I4XeRtVYhgFYGrq0u7ac3ZtTLbtwoXF7wku1yGthL53iSWuXeJB+3FiBhbXW1pHI/DeFhLEIFEA12zrcnQSgSoXZ3nPeS3k3wH+aMLD1UcaYowjhGhsXL5FWOnUk4fjPFfmHQ== 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: This adds documentation for the new /sys/kernel/mm/ksm/general_profit knob. Signed-off-by: Stefan Roesch --- Documentation/ABI/testing/sysfs-kernel-mm-ksm | 8 ++++++++ Documentation/admin-guide/mm/ksm.rst | 2 ++ 2 files changed, 10 insertions(+) diff --git a/Documentation/ABI/testing/sysfs-kernel-mm-ksm b/Documentation/ABI/testing/sysfs-kernel-mm-ksm index d244674a9480..7768e90f7a8f 100644 --- a/Documentation/ABI/testing/sysfs-kernel-mm-ksm +++ b/Documentation/ABI/testing/sysfs-kernel-mm-ksm @@ -51,3 +51,11 @@ Description: Control merging pages across different NUMA nodes. When it is set to 0 only pages from the same node are merged, otherwise pages from all nodes can be merged together (default). + +What: /sys/kernel/mm/ksm/general_profit +Date: January 2023 +KernelVersion: 6.1 +Contact: Linux memory management mailing list +Description: Measure how effective KSM is. + general_profit: how effective is KSM. The formula for the + calculation is in Documentation/admin-guide/mm/ksm.rst. diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-guide/mm/ksm.rst index fb6ba2002a4b..72189bf1283e 100644 --- a/Documentation/admin-guide/mm/ksm.rst +++ b/Documentation/admin-guide/mm/ksm.rst @@ -159,6 +159,8 @@ stable_node_chains_prune_millisecs The effectiveness of KSM and MADV_MERGEABLE is shown in ``/sys/kernel/mm/ksm/``: +general_profit + how effective is KSM. The calculation is explained below. pages_shared how many shared pages are being used pages_sharing From patchwork Mon Jan 23 17:37: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: 13112645 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 8AA06C25B50 for ; Mon, 23 Jan 2023 17:38:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D863D6B008A; Mon, 23 Jan 2023 12:38:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D0F1C6B008C; Mon, 23 Jan 2023 12:38:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BD72C6B0092; Mon, 23 Jan 2023 12:38:30 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id A89B36B008A for ; Mon, 23 Jan 2023 12:38:30 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 7564BC0A39 for ; Mon, 23 Jan 2023 17:38:30 +0000 (UTC) X-FDA: 80386773180.05.FAB1202 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf15.hostedemail.com (Postfix) with ESMTP id BD4CBA0011 for ; Mon, 23 Jan 2023 17:38:28 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf15.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674495508; 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:in-reply-to:references:references; bh=FWkDwqnduO/ODHyQgdE5Zu/r/6qm0MdGBf1NS8vrglw=; b=QCtRyGSVFYrCVy5iEija+LGqXSdc2OjeJlIULMBFx0OshQ8EBfvJIKFhgV6JxW4f5RW0sx 4n1VV9JKGovBiXFuSlbwuF1ma8BWWFlR/KhrMNQRr108IO6+3YtCyN73A38NsyCgbK1+C9 2/zLNjfzdvD5dBhgLt7Y7W6DCG0VEKM= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf15.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674495508; a=rsa-sha256; cv=none; b=7MfufwMn+kBKtv5zxqs4O6srL3petFZ3pqE6ATFQ333Kk8Q6NnedRg4DqVuRtHbMYwj6ZZ rF6jNo/r91X+1mbLciL2ZgaLEdWmeYrtH7GJZkx4r84baHEJ5sGPoW3/t1WyHSAMFc0USS PBGkskL/5Qq6PzDIf1KDVOtPZGUhwrE= Received: by dev0134.prn3.facebook.com (Postfix, from userid 425415) id 6B83A5616BE7; Mon, 23 Jan 2023 09:37:56 -0800 (PST) From: Stefan Roesch To: linux-mm@kvack.org Cc: shr@devkernel.io, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND RFC PATCH v1 11/20] mm: calculate ksm process profit metric Date: Mon, 23 Jan 2023 09:37:39 -0800 Message-Id: <20230123173748.1734238-12-shr@devkernel.io> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230123173748.1734238-1-shr@devkernel.io> References: <20230123173748.1734238-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Queue-Id: BD4CBA0011 X-Stat-Signature: uhkbi8jj33gebup69mtfxgwtzcixd7fa X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1674495508-482496 X-HE-Meta: U2FsdGVkX18Nfa+dBLklpwbxvE/M9TpM7/PNnf+jg5nYngNG9+mN98nf64PN5Wg6T08/QfPTXYH23O/LrToJIexnj4N9PSzy69jJcBBNHqBHdm+u0N5aqIQN8kcrRLaxk08uwaxCjjfS5EbCJ3H+hab358SESkEkIKeHMm9uYjKGk/Jdv1qXbub75KOAe73HNO/NLshJnUrPHwqbrxFifFB2HNiRMaczWgpgOctiSvC2u/+VMPOeK0EMsGmDrwNZI0fefW/b/Nx28gbuVgttZJJI86ERso/7JegoqCuN6fNY/Zg9gaieVuR9D8P2/6TUZSBNISc3OyMNv3PMmABxNLhOO+BE2Cmi4uZVB97arQ0QjO7f4lp5efzov6KrHO+Hkc34+tFWZsZXHlpstvoJPZjus5+lGEqyL4ivKGl41XE93XBY8xstaCGnNmWOCDFhv498AD3dW8vEt42Afb8XRwjySiE61i/4sMfy0pBlGm3y8W16IvgUFY8odrk2qYfBu8IsbuWY+y1HdFAEXnwgrrBdsSf2DQ0CFTY2jCf1/YTQfAXGy6aQLRgw+xwC6CwdKxEuSdED+zkNIVtP6N+xvmpAV52EkrABgu+sQ3DpGcjzjezrmB5lqdxWbB5nG3umw+BPIQDljAmspRZvrShb7mPCqmxTo813GPDTO/03dn4L5wY9zeCy4gODCi3UbyOscetKgZzGuTATFcDphf+Ah9PrEqQ7LKgNt3NtDBVj2A+4dqpueOBhj3OAZ2DaK8FFmtvaIKsdTfZf095edMM01PygI3XCTw74RDmtxG1EGApB+LaAR1VTAWsjW46kp78iXFB+R6YjuIGgm3LVlyaOhHsbsVQnc65zkXUwDHjkkY1MN3xfvAThXNp2W1U2WT8RyGNzmTzpB907/ZwrLpwo/Prg0X5h2vrGOXGUKqIKGx4oasoiRM15ZJNsN3sr6kC/aYAXchrWFY4ke0DYkwE xw4sLv/E IKdl+wUd+uzqpWb9WZS+5hSlkhUCz1AJC7+SHDY+ljs/gDaHziRInjcodBg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000048, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The ksm documentation mentions the process profit metric and how to calculate it. This adds the calculation of the metric. Signed-off-by: Stefan Roesch --- include/linux/ksm.h | 4 ++++ mm/ksm.c | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/include/linux/ksm.h b/include/linux/ksm.h index d38a05a36298..a18cd03efcfb 100644 --- a/include/linux/ksm.h +++ b/include/linux/ksm.h @@ -55,6 +55,10 @@ struct page *ksm_might_need_to_copy(struct page *page, void rmap_walk_ksm(struct folio *folio, struct rmap_walk_control *rwc); void folio_migrate_ksm(struct folio *newfolio, struct folio *folio); +#ifdef CONFIG_PROC_FS +long ksm_process_profit(struct mm_struct *); +#endif /* CONFIG_PROC_FS */ + #else /* !CONFIG_KSM */ static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) diff --git a/mm/ksm.c b/mm/ksm.c index d29454a802a0..288689b59527 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2935,6 +2935,14 @@ static void wait_while_offlining(void) } #endif /* CONFIG_MEMORY_HOTREMOVE */ +#ifdef CONFIG_PROC_FS +long ksm_process_profit(struct mm_struct *mm) +{ + return (long)mm->ksm_merging_pages * PAGE_SIZE - + mm->ksm_rmap_items * sizeof(struct ksm_rmap_item); +} +#endif /* CONFIG_PROC_FS */ + #ifdef CONFIG_SYSFS /* * This all compiles without CONFIG_SYSFS, but is a waste of space. From patchwork Mon Jan 23 17:37:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13112643 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 A5081C38142 for ; Mon, 23 Jan 2023 17:38:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2D9456B0088; Mon, 23 Jan 2023 12:38:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 288AF6B0089; Mon, 23 Jan 2023 12:38:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0B4326B008A; Mon, 23 Jan 2023 12:38:22 -0500 (EST) 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 D92276B0089 for ; Mon, 23 Jan 2023 12:38:22 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B4163C2206 for ; Mon, 23 Jan 2023 17:38:22 +0000 (UTC) X-FDA: 80386772844.10.D7A6FEA Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf27.hostedemail.com (Postfix) with ESMTP id F14004001F for ; Mon, 23 Jan 2023 17:38:20 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=none; spf=neutral (imf27.hostedemail.com: 66.220.144.178 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=1674495501; 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:in-reply-to:references:references; bh=8P5nJvBBBXt+f7B98ljEhcX6LRnkeWeqEmjDASSaXMo=; b=74Hh4F3ER2mquWvxoUlXApcIIUne9MlW75prEl3I/EkHDZWC31GPp79u3k1Vd5xYN9m3kV 6yt5eyfwBB+IjyQmCmYo8s1ixbEFTPpl1/v7K7iLB5Qlf2jRed9fcqjomiGdBO/i77+Kyu 1fI2OHgB859Z3dLgUjOzD2QwQ6Ssle0= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none; spf=neutral (imf27.hostedemail.com: 66.220.144.178 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=1674495501; a=rsa-sha256; cv=none; b=WfU9mzfxVSriP76pVpVUTnyfTUElFzIiJlj0JqFHUBbdPsPUuc19RtwlB+q20LHSjt32w7 Yl8ZOlM0z4p9lJcnd6D046QC9I1pK8k4ucTixOahndWhRrfDt15dR72V3kA7xg0iMfw7wv P2VfUtI72DMSvfdpQjtqq2C5zJgjUKw= Received: by dev0134.prn3.facebook.com (Postfix, from userid 425415) id 6F5EE5616BE9; Mon, 23 Jan 2023 09:37:56 -0800 (PST) From: Stefan Roesch To: linux-mm@kvack.org Cc: shr@devkernel.io, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND RFC PATCH v1 12/20] mm: add ksm_merge_type() function Date: Mon, 23 Jan 2023 09:37:40 -0800 Message-Id: <20230123173748.1734238-13-shr@devkernel.io> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230123173748.1734238-1-shr@devkernel.io> References: <20230123173748.1734238-1-shr@devkernel.io> MIME-Version: 1.0 X-Stat-Signature: i8tikc43g3o5ymky17w5teftpt6yapzs X-Rspam-User: X-Rspamd-Queue-Id: F14004001F X-Rspamd-Server: rspam06 X-HE-Tag: 1674495500-275656 X-HE-Meta: U2FsdGVkX1+TSVS+L6TLVJ9djF9zL3jNjTBS6W5bZT81aF1uAeZBcUDr5EEtjPIuPsYIb8bXbpLlHb43J5hnCN9DYPb7SAT9z+hs0ew//HjayrXY3bxbW+QsD+e+NcvkZQlpVGjhv/EIwRWiV1Kqk4h0w1cYhYhKfxkkmWITILf8r5WbDeqBAVsC0Vd6J7QpFk7PL+Sszqbzljo54/knEAALn9V9XSTOBFNdRaKYOBLgRAjxT4DJ92U4yqKQh1ZVL24wJ8Gbv3YLhQwVPi9bhk7aaDlCsAowrdLQsJ6QBOzHx/R+f9+dKG4sp9EhwkMUeHbRQMZQ/B0CbCvQ7hxijEFphoXjQ2DI2wVtJEjmxpXqao6PEMTwzvBWW+kDfodjzpMEk3eqCwpbhYzPHxxiitGadz6gtGD3Ajn8CZsXMISENOgI163fTVl0Qyda3xrpM2i+9AjRLIpa8AgNWzkrefvuMubEvsJyK5TLEJ2Ml00kPmKE4Y0Ep0CoYmPn9oC1DQaI20VQmA7PSGVAqp4VzH9V8Fn376BHR/FM8jtgqsvw0W3oJ7zRFBx5Sg3Jka3fSyhI9ei/U+bKaPO7RPCPpau7RvKlo8WwaP0Ey9Mo6MinbqyN+wHhgcBWp2r2I54ARaTBZ2zIkKc3G3afczCP7KE3fp33bTGU7BvjD2TbEo2r/d1eBscxxsP6H42ZKnS/eUNV8DKGNixtMvPl3ArI6842ZclKgorJhRjHmcJx7PfvTHFx3mQyFZQF8AyDRM67aVFf5RQVyxuoPsefii3fbHOCHZISUR+9+0Ge9kBvSGRmQPfsUEH3a0fE47CDqbuyU8STYi1QYenNR/of2VdLsLtrSc8vioriiaW7nEq0AgpiB4lIHFBstTxtx1O1Fq/sRSkIFp5rTveqmwvkYRl9/zqhjBMKDqDWnVo20Rj9L/GDHOO77l4KLynj4DrqjPL7me/Mu7gho9KG0gQn7je aojhBwQp elX2LEJzKXG6gpOg1iPdgFfFglHkxKVwNVbszlt6jLntR3cP6W8QJOXCCisGe2k8U1WX+ID6hJoqNskqRGj9o7uwRE8ckGq27yWVG 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: This adds the ksm_merge_type function. The function returns the merge type for the process. For madvise it returns "madvise", for prctl it returns "process" and otherwise it returns "none". Signed-off-by: Stefan Roesch --- include/linux/ksm.h | 1 + mm/ksm.c | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/include/linux/ksm.h b/include/linux/ksm.h index a18cd03efcfb..d5f69f18ee5a 100644 --- a/include/linux/ksm.h +++ b/include/linux/ksm.h @@ -57,6 +57,7 @@ void folio_migrate_ksm(struct folio *newfolio, struct folio *folio); #ifdef CONFIG_PROC_FS long ksm_process_profit(struct mm_struct *); +const char *ksm_merge_type(struct mm_struct *mm); #endif /* CONFIG_PROC_FS */ #else /* !CONFIG_KSM */ diff --git a/mm/ksm.c b/mm/ksm.c index 288689b59527..57183deaf529 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2941,6 +2941,17 @@ long ksm_process_profit(struct mm_struct *mm) return (long)mm->ksm_merging_pages * PAGE_SIZE - mm->ksm_rmap_items * sizeof(struct ksm_rmap_item); } + +/* Return merge type name as string. */ +const char *ksm_merge_type(struct mm_struct *mm) +{ + if (test_bit(MMF_VM_MERGE_ANY, &mm->flags)) + return "process"; + else if (test_bit(MMF_VM_MERGEABLE, &mm->flags)) + return "madvise"; + else + return "none"; +} #endif /* CONFIG_PROC_FS */ #ifdef CONFIG_SYSFS From patchwork Mon Jan 23 17:37:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13112648 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 7E49DC25B50 for ; Mon, 23 Jan 2023 17:38:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 178596B0092; Mon, 23 Jan 2023 12:38:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 127CA6B007B; Mon, 23 Jan 2023 12:38:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F316F6B0095; Mon, 23 Jan 2023 12:38:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id E2CA56B0092 for ; Mon, 23 Jan 2023 12:38:40 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 9558B41C9E for ; Mon, 23 Jan 2023 17:38:40 +0000 (UTC) X-FDA: 80386773600.26.EEAFC49 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf25.hostedemail.com (Postfix) with ESMTP id 490DAA000C for ; Mon, 23 Jan 2023 17:38:37 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf25.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674495518; 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:in-reply-to:references:references; bh=OEwH+3PoAg0i63suZqVpZUf8rAexTmeuq4F/YsaSxAk=; b=jBvuFqn2qTSP0JjQh7cd9kdQ/Fc8L9tLabp5U+3efNUpFTas1GqoQonPjuXjpUQCD9N8Re T92EXgPEj8mLtA6oKu2Q72RZwhAxPcx4ef8nCD1nhCiuoSMiCAHduIS41V7FCNE6AFDjKe TR8v6m6TyXzsXtIsyKqslUX6HoGqwNs= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf25.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674495518; a=rsa-sha256; cv=none; b=lHD41N5RgL5SHYSvbwiILB/NaAO4M1wCPHxO7ivcqGm5FQWDDWWJCNSxT3Jce1QvQe7kNr MnuO1FiFz2q3ypaypMDmv23lNSw9mlbqcWvmTyXUXx8OhjLE8DQlpwn3YuOTlSPYJpiu05 uHjwVxEMljvukSWn4Nl+/z72+cUOkGg= Received: by dev0134.prn3.facebook.com (Postfix, from userid 425415) id 732985616BEB; Mon, 23 Jan 2023 09:37:56 -0800 (PST) From: Stefan Roesch To: linux-mm@kvack.org Cc: shr@devkernel.io, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND RFC PATCH v1 13/20] mm: expose ksm process profit metric in ksm_stat Date: Mon, 23 Jan 2023 09:37:41 -0800 Message-Id: <20230123173748.1734238-14-shr@devkernel.io> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230123173748.1734238-1-shr@devkernel.io> References: <20230123173748.1734238-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Queue-Id: 490DAA000C X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: fs51qop95we9h1cxtzfnytz5oedeyqf5 X-HE-Tag: 1674495517-310361 X-HE-Meta: U2FsdGVkX19+yxZl/j/Yc7KOYjhO3cRTe9O5cPpWV2GXF41XHfkJsj4pF/HULKGm6p0YNPm/duXNjULwJfZIgLBhsGBY+pnxqEEZ0bnBfYVysk9OBywvaGpcBEhZCR6Ez/zbLk04Wd+KQRpLSwc+/71ATJ/GVzLoFwWv8xj7hgn7sgm6wzhkNri915iP8fmL/kpbltOn+OIEmgg5oZuApKy+qxM+hDkz7kErJGvpOPmxN6RpDk0UtIqryjie1mD0MWnrPkOpSnAbaAZOxRKLu8YyRLjWIB2riRMLU3iJJrpUd5JVBWH5rG1MFei6mYkdxQ5ECbLb54jvlStDSGZHB/5idYxyzziazNjGhDpwbp34dD0ODO/XfPnGJLI4/NdpjbQXGlb4iKIynHAgye/H9X5xZOpSvdxz+xqQwYqapgKwfiMkdUA4fCnMyU3NOSkpD5sACcCckTr1kVMcmYT1eqmjYIxpPgLJ9d5YaN1e+bkM1QinA6xC5gZqHDrf9CZLLsErZk28CqHQtVRFJj3S/sY+H1Xk6xWS/NQDSS5OwOn/RKHEpqNddPmkU7xUbpovE3uTI33tU0tVNKQyFnQygPtsPCD2BCFnsTod5aqU0JA6aokQZvX4iYkMj15EVe0ScGpnGXWwtuFFS9158uj4CNIHtm8LnS685eX0FhHMd7zhe7xasz0ycnX1rXBcBuOXoKRbjqFugpMyNHwhj3WHbpnAydJ7Y0wJ6vHwetvQbu6ckFukI2+TOEy9DgBOwUI+W/GQ/y1/LFVM6GJfGCg+YPiGE3+cvxMZ76sCo82FPJBL8LXFcWFnHjMH/h0OJe+JFS14xwQf/YiQmG8X/UCHkFz0/D0UgrlBjWRNM2pli5z5w+VZDDIhu7b4jZZ2kTtkzLjizZ3XOUNglZCmQlRU0LbvCRMBorV5EFM7HhFd8hY0SlbLoMuKYpAoMNTh9DV3wOlawW5tev3JK0ePHCh RBROclUX UcKIAfwt/0qupamSRAZIV6PdrRRd1tkj7CX/WV5aAgpqhDzqSu2byq6LMjGtDBsJCoD1kWxBclc+JuyhJD+/Sl9gxUOo/AMtxdKcf X-Bogosity: Ham, tests=bogofilter, spamicity=0.000328, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This exposes the ksm process profit metric in /proc//ksm_stat. The documentation mentions the formula for the ksm process profit metric, however it does not calculate it. In addition the formula depends on the size of internal structures. So it makes sense to expose it. Signed-off-by: Stefan Roesch --- fs/proc/base.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/fs/proc/base.c b/fs/proc/base.c index 9e479d7d202b..a2b7779f76f6 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -96,6 +96,7 @@ #include #include #include +#include #include #include "internal.h" #include "fd.h" @@ -3207,6 +3208,8 @@ static int proc_pid_ksm_stat(struct seq_file *m, struct pid_namespace *ns, mm = get_task_mm(task); if (mm) { seq_printf(m, "ksm_rmap_items %lu\n", mm->ksm_rmap_items); + seq_printf(m, "ksm_merging_pages %lu\n", mm->ksm_merging_pages); + seq_printf(m, "ksm_process_profit %ld\n", ksm_process_profit(mm)); mmput(mm); } From patchwork Mon Jan 23 17:37:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13112649 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 89235C05027 for ; Mon, 23 Jan 2023 17:38:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8A4116B007B; Mon, 23 Jan 2023 12:38:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8552D6B0096; Mon, 23 Jan 2023 12:38:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 67E5F6B0095; Mon, 23 Jan 2023 12:38:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 58CA76B007B for ; Mon, 23 Jan 2023 12:38:41 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 2EFC2C0829 for ; Mon, 23 Jan 2023 17:38:41 +0000 (UTC) X-FDA: 80386773642.22.83A60FF Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf01.hostedemail.com (Postfix) with ESMTP id 719BB4001C for ; Mon, 23 Jan 2023 17:38:39 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf01.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674495519; 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:in-reply-to:references:references; bh=M9n46KmmTPQMjnZG+W9yInSyzHIuWxq53EhKeS5hBuA=; b=AaYsXi7cNbHsDnhHgLgTX+9/1i0FHYhJl1e5B4b/TQYN2qgy6dttRSnOsIYw3BQov6fTnx PLn75biNRK40b+yO509myKZavykolw7rtxDN2Edk9ALJNdltQUgOf1uJ/OKaBkflwT9oBF 5MHwljt5id+HENxHubavN/BaUg+SNpw= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf01.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674495519; a=rsa-sha256; cv=none; b=r/Eqv3gT5yjXrG3EKqXJvWD9zNUfiRcCGqZcHIagHY7jkkonMvxtDuyt0D95olCMFQhNR+ 4CZ+kzdVGF0i2SFC+BxRcZr50qzBB6WXTpINfctRstmC9avkWOyuLdjZiu8z+D91oSPz0Y 8q6Zqi2XPuSbNukXqPe2IBrlrKLIKbk= Received: by dev0134.prn3.facebook.com (Postfix, from userid 425415) id 76E965616BED; Mon, 23 Jan 2023 09:37:56 -0800 (PST) From: Stefan Roesch To: linux-mm@kvack.org Cc: shr@devkernel.io, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND RFC PATCH v1 14/20] mm: expose ksm merge type in ksm_stat Date: Mon, 23 Jan 2023 09:37:42 -0800 Message-Id: <20230123173748.1734238-15-shr@devkernel.io> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230123173748.1734238-1-shr@devkernel.io> References: <20230123173748.1734238-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 719BB4001C X-Stat-Signature: pxpfkj45zz8hotjfe5khufxgmy6pja9b X-HE-Tag: 1674495519-914403 X-HE-Meta: U2FsdGVkX1/khQk+A9osBCweAuJx7OZbZ8yhSIPeD+tXVYlFtYm83uFrnCjzs87ynMtHdGL95Bhrm1yUtCdxKCpq0HAprM//fOnnr6S6roo5gXGjdgT0f9JECSsPK3nDvSy82VJiotU2lHCdpys4FpiZmrDcqEks1ShMm9AEN0G5Bq2exSrccNiDAfTmTF64eYwlwjKMLpGMdYpyYEuDO2eTngJFXjpc+G0yd2/+hnv1tW6LlCXe3CZW69lTeYMUKq6+R5tC8LTuETdxKrpJAfmGB3l/4L93EeK826/SobPzQqA9JBbm2+hGULmGBMVP8YETiXYYLy6WSn/IzEdVxq7duvHYL1KcTNXo7xTKzYW0EvckM7fuMPK4Tee8uKERTvm2LlTzY5vIYZtYoOXUqEuYeCW6Ixrum9BRqSr29OCC/N1pHE/SJkL+V4kB14/cgIEgQwdwDoC/Pb5+sUA7z+j7Pl0H5xLFqP3h3QNy/TnhH8yHPLpp95NHbKQ5r9Su6zuidrF9/v1MPdL57j0kEWrA/e/QavrijMXTfjn1mxRLlyllxZdNZ07liwAJYpEHOjgMThJJPBRDfHrWofELzcozdq//R289ungAM+nHnyW0NPABg54RjmLC2zl81mbR+E5JMugb2OkUkrJzR3unsVVRvRWsApO+3FQxgPrFCBz8tj2PlcaZTCOsmRDPdjthVYDJrKGqdZ+A36c08a2zsztSP1X9t7Z/222RbT6OfcdoLvoDO7rz3fmOTJw7eikf2FhV7Tg0ypWtWuNXktad0CVZqqsJEmVXivskKPm2wlT+hDCW6/HN7CEpq/oXkQTGMngjYrVSxvR+EVJIIN6tcJdCLWGh681V3ABP5HLvv5ogHk/O2ggX+EVqp++04Ou/HSHQed/6DgRLxb1T2I+qBRLnh7SOLH6gUxalUB4QmgFhql8gs2x2zHaCO1oY6qo7ijvg/BiMCj2scFeb2XB ng+OADvs zC++Q7hmp3wWgUSfsTZuJvwZsgIcil3MmG46x1+mNfayOEnUK+DNPD2+cH1UiL2zOkD+37zKslABR4Kv8Uak7gN8vvMArGMBA5QF1 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: This exposes the ksm process type in /proc//ksm_stat. The name of the value is ksm_merge_type. Signed-off-by: Stefan Roesch --- fs/proc/base.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fs/proc/base.c b/fs/proc/base.c index a2b7779f76f6..2b63a70fb7a6 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -3200,6 +3200,7 @@ static int proc_pid_ksm_merging_pages(struct seq_file *m, struct pid_namespace * return 0; } + static int proc_pid_ksm_stat(struct seq_file *m, struct pid_namespace *ns, struct pid *pid, struct task_struct *task) { @@ -3209,6 +3210,7 @@ static int proc_pid_ksm_stat(struct seq_file *m, struct pid_namespace *ns, if (mm) { seq_printf(m, "ksm_rmap_items %lu\n", mm->ksm_rmap_items); seq_printf(m, "ksm_merging_pages %lu\n", mm->ksm_merging_pages); + seq_printf(m, "ksm_merge_type %s\n", ksm_merge_type(mm)); seq_printf(m, "ksm_process_profit %ld\n", ksm_process_profit(mm)); mmput(mm); } From patchwork Mon Jan 23 17:37:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13112646 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 688A5C05027 for ; Mon, 23 Jan 2023 17:38:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F33596B008C; Mon, 23 Jan 2023 12:38:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EE2F06B0092; Mon, 23 Jan 2023 12:38:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D84026B0093; Mon, 23 Jan 2023 12:38:33 -0500 (EST) 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 BB6B76B008C for ; Mon, 23 Jan 2023 12:38:33 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 7E67BA1812 for ; Mon, 23 Jan 2023 17:38:33 +0000 (UTC) X-FDA: 80386773306.01.7A41DFA Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf09.hostedemail.com (Postfix) with ESMTP id D3059140021 for ; Mon, 23 Jan 2023 17:38:31 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf09.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674495512; 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:in-reply-to:references:references; bh=48BaoTp/2HJ6bS79iL9g9GlQiiTNNuHFinc8DbFXwQM=; b=h6q5QPTPJ4q+cguJPGWl1BsZI4TzOMev52mjJPzttkzPI7JxSO8sOb2LNu5iIRgSVps2Cd tix6YMSz5h8n730jWviGMY0wZA6AM4+H8ag3dlc9cLJsamR+HPC1RyiwePBrlavTvCYMyJ wy9PSRUFdk4+8XZ17z4wIavQTe91h6c= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf09.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674495512; a=rsa-sha256; cv=none; b=HEBjywP9FNxY7U2FxguDTDAw25r3ErvYouCkn3RdaJ+OyvBzxUDbOSx92SSFG2OllLaW7z e6CwgnNlPSce4yvQAFM8H6BlaDCE9qZs/fZXvGDUDIZBXJJQzE/tbKt1pZE2kkwPSc5SY7 lUEJOPh5iT2vGGtinBfZa/7j5qrM8Ec= Received: by dev0134.prn3.facebook.com (Postfix, from userid 425415) id 7BBE85616BF1; Mon, 23 Jan 2023 09:37:56 -0800 (PST) From: Stefan Roesch To: linux-mm@kvack.org Cc: shr@devkernel.io, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND RFC PATCH v1 15/20] docs: document new procfs ksm knobs Date: Mon, 23 Jan 2023 09:37:43 -0800 Message-Id: <20230123173748.1734238-16-shr@devkernel.io> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230123173748.1734238-1-shr@devkernel.io> References: <20230123173748.1734238-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Queue-Id: D3059140021 X-Stat-Signature: i1ug1ijxyak9kmjwzsoq17w8rbja4xxt X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1674495511-97021 X-HE-Meta: U2FsdGVkX1/KGsG0zJwgnWqU7IZhyCpeVsyPzHH9/tPJ5Q5H68kHQw2PqNPbCzxCMp64mHx5V0W4GLs7qmctRM10A1GQSVBFMMyiZnQgcNiFi7ow4WNxy18f2kSTPWd5hd9+XJxqGqgrWtqWHERuy0bQD2vo0aSmWVotjPR+j2mommqvUOFBDm7W9Uz8s2UCxH82BZz0P6OFSGKTsYQd3l4Anm4dowVEjOn4jHciHeea0R/nH+PfjO8Lbfcntb3y0LE86DhJ5/VLmltI+CpMFoHUFtSmrDtF+nZcLB9XdV9XyG7V8F5/OssCVOrpYvL8NYnVpndVmnwIzwj5y1CAdL+aZV6pvwBVuCHVx5tivN3y5nJrzEiMc427zXr2EQBs1Afx9YehJBKuANvjI6+xBHhlVb3m+OB9PaOGkJ2DT+d5ZEn5vWnOC8DuuoqupXVMQE1N9Be+4XcJr+lbPvMhv6oCA+tTC5Tmr5eVheR1qaMKHuEYvDO8/37pzd9JrCTxB3tU6fqakKsujgk8bHDPgTIhrktH6hfJtJBXTtKbj7jAE/cpViDjPpHCqcsJAFBV1y9froHR/fjcQAiCoYSVBQXJyWQkkBm/n4DlltliMbkaA/aq5Ipoq11yxN7hoBDhv9QwDkDNteUPbadL4RhF8zpSQ0a4zpHjtex2Pl3Fil9UbHx7OjD11oee60Ps7ufmmBZP2hBaQruvceuUeBJyl5KwUjnJjpz9P73e/6WYQDUaeLIKZwirkkQXYV3N/58w4c3fUFkqZ8Y6oehIvrpIEaLwWmVXkbzMGiKOCflB5AcFYaYZt/5vAKdl8+rHqvxX52UnXZhTlyf1PfuNQzbZpZXEngSolKag9nrfQ9RzRmM0+XLmEB3vnN0xcqU+ue0A4P6xmAcJmyquQ0+LEsytQGYuRAVJFo+klt/aM1UQ9SpyVF38xdBkvHWiifRaLfBNlT6R9bfOP4Y43uFIw1z Wq80TQ8c yQ+AT7CgrD2I7F+EKA3OWNmKFCAspeeYEy0/GLP4Pu/nJcrI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000006, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This documents the ksm_process_profit and ksm_merge_type settings in ksm_stat. Signed-off-by: Stefan Roesch --- Documentation/admin-guide/mm/ksm.rst | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-guide/mm/ksm.rst index 72189bf1283e..70896f01d641 100644 --- a/Documentation/admin-guide/mm/ksm.rst +++ b/Documentation/admin-guide/mm/ksm.rst @@ -211,7 +211,8 @@ several times, which are unprofitable memory consumed. ksm_rmap_items * sizeof(rmap_item). where ksm_merging_pages is shown under the directory ``/proc//``, - and ksm_rmap_items is shown in ``/proc//ksm_stat``. + and ksm_rmap_items is shown in ``/proc//ksm_stat``. The process profit + is also shown in ``/proc//ksm_stat`` as ksm_process_profit. From the perspective of application, a high ratio of ``ksm_rmap_items`` to ``ksm_merging_pages`` means a bad madvise-applied policy, so developers or @@ -222,6 +223,9 @@ so if the ``ksm_rmap_items/ksm_merging_pages`` ratio exceeds 64 on 64-bit CPU or exceeds 128 on 32-bit CPU, then the app's madvise policy should be dropped, because the ksm profit is approximately zero or negative. +The ksm_merge_type in ``/proc//ksm_stat`` shows the merge type of the +process. Valid values are ``none``, ``madvise`` and ``process``. + Monitoring KSM events ===================== From patchwork Mon Jan 23 17:37:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13112651 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 1B189C25B50 for ; Mon, 23 Jan 2023 17:38:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 71E0E6B0095; Mon, 23 Jan 2023 12:38:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6CCB16B0096; Mon, 23 Jan 2023 12:38:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 594766B0098; Mon, 23 Jan 2023 12:38:42 -0500 (EST) 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 406076B0095 for ; Mon, 23 Jan 2023 12:38:42 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 200BE822E0 for ; Mon, 23 Jan 2023 17:38:42 +0000 (UTC) X-FDA: 80386773684.08.90DE730 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf19.hostedemail.com (Postfix) with ESMTP id 7DA1F1A0015 for ; Mon, 23 Jan 2023 17:38:40 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf19.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674495520; 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:in-reply-to:references:references; bh=GYQtk/S5fG1KUGiyaQMgcUYBq4HZdmgsoe9i0bSjtmI=; b=aOUvbAF+mC6sZVopo30a+Icdb0MBUKsuICxnbX/2AreX0f+v/w/9ItwpSpGN0f1KqgH7Y3 rM95R5Y1FvjJMDLU0T/Q/czUt6VKu4nmf5HgkKtfZkyE3oGXbf0YhWjJPqKx6X4h8t1KpH koDDvdDvolc5/QUQeJ1qExqQkQVTsxE= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf19.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674495520; a=rsa-sha256; cv=none; b=rDeGF1GjGaEy1qC1Uys1zXnnhC+VYvU0NBfdJo3TUWz+MfMsgWfIPnutOeyhJrlAubceTL hVMhgX21iJo5oaaJAfcHa/bA28UmW4Boq6JPvSLhSzuacAXdhJ/XgMNOICBlr/GRqVhaM8 V1njlqm+omyCTDJyJtLYY+G0A2BOO9c= Received: by dev0134.prn3.facebook.com (Postfix, from userid 425415) id 7FBAC5616BF3; Mon, 23 Jan 2023 09:37:56 -0800 (PST) From: Stefan Roesch To: linux-mm@kvack.org Cc: shr@devkernel.io, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND RFC PATCH v1 16/20] tools: add new prctl flags to prctl in tools dir Date: Mon, 23 Jan 2023 09:37:44 -0800 Message-Id: <20230123173748.1734238-17-shr@devkernel.io> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230123173748.1734238-1-shr@devkernel.io> References: <20230123173748.1734238-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Queue-Id: 7DA1F1A0015 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: njqtkkqddhqbzfqixktbbs1pz78hngjy X-HE-Tag: 1674495520-907839 X-HE-Meta: U2FsdGVkX1+HPoatuLzVeHA7LYeOqzufPy1xTlmEC1Elm3Wen+wsaAysismT/AOinx8QiEsGcnjl1uXJc/au2JbtgHjMSfGnG8cBgfERCrXJ9DKDdvET1Yoq5vG8HXVG+xzbVl4cNQWwTY/owYIie8nhkR3IrkxTjb3KmOvj+laiTYtiUehDE6wXMWfekDMJm+lRZjfQzxglvcWejXd2RP9VFiB5p5oy/2JurADAr9QyhQpsmHVRbcYrwN+tXBkbJMc9WAiVmNXQATG0XbO0lxZqEmRUjpkZJgGGVU+Gup/RSdkFPIkKlCaRPNQO7tTDkDCwe10OPASQGo5ayddk4jHN17qFsu9mym2ykmlTi4DphTfpVZxspwUHNMN3o8zViSXfFd1jAuXCIDtTFsgAoFN6zlXYDwWgRYaE5yZCWTuWSb72NH7KxuJ93d6TGXDe94BnadARi0qpM6PFTkqwCAp8yptU+tSR8DSjAvvRzvTnhrYxOA0pq1Pd/noZERqkvtY1TLOS9MhwG2SjIj5ra7yib+fTl/BHcaXwetqXhVr9TbwytwPjsQzKvcUQPGcZgLj6HZNH4Z8sth2Nga2gFyOnt+E9UQQkCVPmorv+xq9mKf52xEjok8u9zPsEByEqn40nu/Ds9pnbNUqH4CIg8wrLYCPnFWd0ramVSjLRV8YG6c0Ru+dmMtrhy/lRHIHhi28khaEfxUlfilrGo2oNFfRWxIxGxAqDmRgUmWs/crCaEUMBXv+cF13PF5ACeQdDmj3fFKEBORmLiE94WixsMuq88QvMc69R9Jvwdh0Vn9X+88cQn+dAZYpv8LMlTyZj3Q6vnvWpUamZQ8yOHatYEdwvgk8V7qh2vzHvDgRsZG3Rqkt3bLbVR28Ws//0+kUXDpq0lmyQrq3nXikrAlmX/vUXHx3h8YDIbxGbX+vqCCEBLZdCOSijz36oK6nIEbWz5+doab4WqvmysIxigz6 PepO+P38 R4DPI/pPZ+aDPSh/elqjUomd6oaS98hq0/VCo6v/7YizGMZXB0DW0RjnUpQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000056, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This adds the new prctl flags to the include file prct.h in the tools directory. This makes sure they are available for testing. Signed-off-by: Stefan Roesch --- tools/include/uapi/linux/prctl.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/include/uapi/linux/prctl.h b/tools/include/uapi/linux/prctl.h index a5e06dcbba13..b8ba85f18e43 100644 --- a/tools/include/uapi/linux/prctl.h +++ b/tools/include/uapi/linux/prctl.h @@ -284,4 +284,6 @@ struct prctl_mm_map { #define PR_SET_VMA 0x53564d41 # define PR_SET_VMA_ANON_NAME 0 +#define PR_SET_MEMORY_MERGE 65 +#define PR_GET_MEMORY_MERGE 66 #endif /* _LINUX_PRCTL_H */ From patchwork Mon Jan 23 17:37:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13112653 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 BB228C38142 for ; Mon, 23 Jan 2023 17:38:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 23C206B007E; Mon, 23 Jan 2023 12:38:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1ECD36B0098; Mon, 23 Jan 2023 12:38:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 064986B0099; Mon, 23 Jan 2023 12:38:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id E481C6B007E for ; Mon, 23 Jan 2023 12:38:44 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id AB66C1C8382 for ; Mon, 23 Jan 2023 17:38:44 +0000 (UTC) X-FDA: 80386773768.08.5FE9E89 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf08.hostedemail.com (Postfix) with ESMTP id D446416001D for ; Mon, 23 Jan 2023 17:38:42 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=none; spf=neutral (imf08.hostedemail.com: 66.220.144.178 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=1674495523; 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:in-reply-to:references:references; bh=QtuSiKqGbQErcVVkdQ69vUod5ag1AqdUM9sufVQBvro=; b=t3/JDDL+gVDbO+0Vllv6ACCemT1hCCmLnZ/xszeXqNacnwQYHhSvRTEqVcIp+Mtv/DOnnt lq1pwzKTu3rAoHr+OHDS0t1JUPaYnwLRQk5FVOIjjAjnk5KfUlnKr8FgQFDw21nDS7bo3T tUMNMJduFAOdjce073Zpzuou+9ReSlE= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=none; spf=neutral (imf08.hostedemail.com: 66.220.144.178 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=1674495523; a=rsa-sha256; cv=none; b=gfxQHWu24gtAouCXk0RBLqIAxmfmfmj/vCxGenV5ZRg9GOGR5QNVnISOww0nYRvdPy1UT7 4BduEYjrPBZFqF4d18PQhFbaVzfgaW2eQuiNAm5JHjn6wb7sXy0sWw/QKNTwZYpEA14F1T FGro2t4kstHLOjEi+cmmpJrCC8o6oxI= Received: by dev0134.prn3.facebook.com (Postfix, from userid 425415) id 8434B5616BF5; Mon, 23 Jan 2023 09:37:56 -0800 (PST) From: Stefan Roesch To: linux-mm@kvack.org Cc: shr@devkernel.io, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND RFC PATCH v1 17/20] selftests/vm: add KSM prctl merge test Date: Mon, 23 Jan 2023 09:37:45 -0800 Message-Id: <20230123173748.1734238-18-shr@devkernel.io> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230123173748.1734238-1-shr@devkernel.io> References: <20230123173748.1734238-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: q4h9cc1r3p3867hizzj454wkph941rcg X-Rspamd-Queue-Id: D446416001D X-HE-Tag: 1674495522-121162 X-HE-Meta: U2FsdGVkX1/T8bipyPkCq5Oq5H42+iqVBqw1XrjrdiO/1v5FQ2jAq0dFFR8e8rY2+hJ3hOUgGBWoUvwmJZlAqjG/fVgzx576sxrR3BabM9H7osMDqU4TzfzcH6QSFUTVB/fyj2MNGcScp24eBGVq/Ha7Yj2SWOAYOPErOD/i5EGLqTihAdTpGS8KyalK03wwA0B3M5mSGrZxgDIlqTFFXy0S5jSQwt2CPDm9hesXowQG3wvlpUmZIV4zN6EWXvGxLx5mnp/gKNzhsJyI1fxGn5juGbV7QeNjou4vUxk4Ds+A7ESwRH1l0Jf8eHrxBWfs+m/XfEDIBZrbxBgwp3nS4KSEHD7KwxDhb8nDBgfaHVimE9KQKfHitVCagJyGsZkoI6u07S8RJehxGuWPEyNQnM+h2itV5HwZ6gML97ZqAO+VtdMiGHdpJhY4AR3PU4CjLuJBmqawNNrosA7UHkb9QIiCnHuc0oHCnF+6cPbXNpCFTAQh++w7FuCFGbeJMmKJ4Dnk+HyqgGP89UuKszL3GldvPOyZaEikwABbx1RvAkezDKwiBlfaEmdhjtFOd5aZu1V3evT982enUiSKKNCZiq1XfNnKuxvnhByuqSKNt1ZAE4eCp9mg64FyDO6uGLK2s+f4lPpZ391o2mvVQ2H68bLFkhaZfipZt/Eck1sjNjz9oRUibtCUPSYASQNY0JB5W1w8IhMZrg2RDOFkX/muoU2Ppc5BY0lEjk5vbgXogxq8SVYtNxKBWUNnftK3vrHjC60txd2BHTB0Un1xFH0CXNU2MmyAw8GefgBOOoFGzXAh7p7srQDvfhB8TB9pOInNmYiGzyihQLPxblnH+NQLCcRH/0QbJ9msDIWMA91Rectl5HBMpXcQjCbou2bLnXeJ8C2l1fBWjXX5tuR5RLgSWazYXj9hFoMArUz1DV9GXpUmOTd25Mi3NoJTfkytLZjUzmt8cHVRj81k7r+J+Kt REWxdXdP n6zVAyTdBA1c8mS59qA7xHgufUi5bGlHFyz7FUt3Tu5xJkXg= 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: This adds the -t option to the ksm_tests program. The -t flag allows to specify if it should use madvise or prctl ksm merging. Signed-off-by: Stefan Roesch --- tools/testing/selftests/vm/Makefile | 3 +- tools/testing/selftests/vm/ksm_tests.c | 116 +++++++++++++++++-------- 2 files changed, 80 insertions(+), 39 deletions(-) diff --git a/tools/testing/selftests/vm/Makefile b/tools/testing/selftests/vm/Makefile index 89c14e41bd43..8a50802d8700 100644 --- a/tools/testing/selftests/vm/Makefile +++ b/tools/testing/selftests/vm/Makefile @@ -25,7 +25,8 @@ MACHINE ?= $(shell echo $(uname_M) | sed -e 's/aarch64.*/arm64/' -e 's/ppc64.*/p # LDLIBS. MAKEFLAGS += --no-builtin-rules -CFLAGS = -Wall -I $(top_srcdir) -I $(top_srcdir)/usr/include $(EXTRA_CFLAGS) $(KHDR_INCLUDES) +CFLAGS = -Wall -I $(top_srcdir)/tools/include/uapi +CFLAGS += -I $(top_srcdir) -I $(top_srcdir)/usr/include $(EXTRA_CFLAGS) $(KHDR_INCLUDES) LDLIBS = -lrt -lpthread TEST_GEN_FILES = cow TEST_GEN_FILES += compaction_test diff --git a/tools/testing/selftests/vm/ksm_tests.c b/tools/testing/selftests/vm/ksm_tests.c index f9eb4d67e0dd..386a0929c8a3 100644 --- a/tools/testing/selftests/vm/ksm_tests.c +++ b/tools/testing/selftests/vm/ksm_tests.c @@ -1,6 +1,8 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include +#include #include #include #include @@ -21,6 +23,7 @@ #define KSM_PROT_STR_DEFAULT "rw" #define KSM_USE_ZERO_PAGES_DEFAULT false #define KSM_MERGE_ACROSS_NODES_DEFAULT true +#define KSM_MERGE_TYPE_DEFAULT 0 #define MB (1ul << 20) struct ksm_sysfs { @@ -33,6 +36,12 @@ struct ksm_sysfs { unsigned long use_zero_pages; }; +enum ksm_merge_type { + KSM_MERGE_MADVISE, + KSM_MERGE_PRCTL, + KSM_MERGE_LAST = KSM_MERGE_PRCTL +}; + enum ksm_test_name { CHECK_KSM_MERGE, CHECK_KSM_UNMERGE, @@ -129,6 +138,10 @@ static void print_help(void) printf(" -m: change merge_across_nodes tunable\n" " Default: %d\n", KSM_MERGE_ACROSS_NODES_DEFAULT); printf(" -s: the size of duplicated memory area (in MiB)\n"); + printf(" -t: KSM merge type\n" + " Default: 0\n" + " 0: madvise merging\n" + " 1: prctl merging\n"); exit(0); } @@ -176,12 +189,21 @@ static int ksm_do_scan(int scan_count, struct timespec start_time, int timeout) return 0; } -static int ksm_merge_pages(void *addr, size_t size, struct timespec start_time, int timeout) +static int ksm_merge_pages(int merge_type, void *addr, size_t size, + struct timespec start_time, int timeout) { - if (madvise(addr, size, MADV_MERGEABLE)) { - perror("madvise"); - return 1; + if (merge_type == KSM_MERGE_MADVISE) { + if (madvise(addr, size, MADV_MERGEABLE)) { + perror("madvise"); + return 1; + } + } else if (merge_type == KSM_MERGE_PRCTL) { + if (prctl(PR_SET_MEMORY_MERGE, 1)) { + perror("prctl"); + return 1; + } } + if (ksm_write_sysfs(KSM_FP("run"), 1)) return 1; @@ -266,7 +288,8 @@ static int ksm_restore(struct ksm_sysfs *ksm_sysfs) return 0; } -static int check_ksm_merge(int mapping, int prot, long page_count, int timeout, size_t page_size) +static int check_ksm_merge(int merge_type, int mapping, int prot, + long page_count, int timeout, size_t page_size) { void *map_ptr; struct timespec start_time; @@ -281,13 +304,16 @@ static int check_ksm_merge(int mapping, int prot, long page_count, int timeout, if (!map_ptr) return KSFT_FAIL; - 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); return KSFT_PASS; } @@ -297,7 +323,7 @@ static int check_ksm_merge(int mapping, int prot, long page_count, int timeout, return KSFT_FAIL; } -static int check_ksm_unmerge(int mapping, int prot, int timeout, size_t page_size) +static int check_ksm_unmerge(int merge_type, int mapping, int prot, int timeout, size_t page_size) { void *map_ptr; struct timespec start_time; @@ -313,7 +339,7 @@ static int check_ksm_unmerge(int mapping, int prot, int timeout, size_t page_siz if (!map_ptr) return KSFT_FAIL; - 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; /* change 1 byte in each of the 2 pages -- KSM must automatically unmerge them */ @@ -337,8 +363,8 @@ static int check_ksm_unmerge(int mapping, int prot, int timeout, size_t page_siz return KSFT_FAIL; } -static int check_ksm_zero_page_merge(int mapping, int prot, long page_count, int timeout, - bool use_zero_pages, size_t page_size) +static int check_ksm_zero_page_merge(int merge_type, int mapping, int prot, long page_count, + int timeout, bool use_zero_pages, size_t page_size) { void *map_ptr; struct timespec start_time; @@ -356,7 +382,7 @@ static int check_ksm_zero_page_merge(int mapping, int prot, long page_count, int if (!map_ptr) return KSFT_FAIL; - 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; /* @@ -402,8 +428,8 @@ static int get_first_mem_node(void) return get_next_mem_node(numa_max_node()); } -static int check_ksm_numa_merge(int mapping, int prot, int timeout, bool merge_across_nodes, - size_t page_size) +static int check_ksm_numa_merge(int merge_type, int mapping, int prot, int timeout, + bool merge_across_nodes, size_t page_size) { void *numa1_map_ptr, *numa2_map_ptr; struct timespec start_time; @@ -439,8 +465,8 @@ static int check_ksm_numa_merge(int mapping, int prot, int timeout, bool merge_a memset(numa2_map_ptr, '*', page_size); /* try to merge the pages */ - if (ksm_merge_pages(numa1_map_ptr, page_size, start_time, timeout) || - ksm_merge_pages(numa2_map_ptr, page_size, start_time, timeout)) + if (ksm_merge_pages(merge_type, numa1_map_ptr, page_size, start_time, timeout) || + ksm_merge_pages(merge_type, numa2_map_ptr, page_size, start_time, timeout)) goto err_out; /* @@ -466,7 +492,8 @@ static int check_ksm_numa_merge(int mapping, int prot, int timeout, bool merge_a return KSFT_FAIL; } -static int ksm_merge_hugepages_time(int mapping, int prot, int timeout, size_t map_size) +static int ksm_merge_hugepages_time(int merge_type, int mapping, int prot, + int timeout, size_t map_size) { void *map_ptr, *map_ptr_orig; struct timespec start_time, end_time; @@ -508,7 +535,7 @@ static int ksm_merge_hugepages_time(int mapping, int prot, int timeout, size_t m perror("clock_gettime"); goto err_out; } - if (ksm_merge_pages(map_ptr, map_size, start_time, timeout)) + if (ksm_merge_pages(merge_type, map_ptr, map_size, start_time, timeout)) goto err_out; if (clock_gettime(CLOCK_MONOTONIC_RAW, &end_time)) { perror("clock_gettime"); @@ -533,7 +560,7 @@ static int ksm_merge_hugepages_time(int mapping, int prot, int timeout, size_t m return KSFT_FAIL; } -static int ksm_merge_time(int mapping, int prot, int timeout, size_t map_size) +static int ksm_merge_time(int merge_type, int mapping, int prot, int timeout, size_t map_size) { void *map_ptr; struct timespec start_time, end_time; @@ -549,7 +576,7 @@ static int ksm_merge_time(int mapping, int prot, int timeout, size_t map_size) perror("clock_gettime"); goto err_out; } - if (ksm_merge_pages(map_ptr, map_size, start_time, timeout)) + if (ksm_merge_pages(merge_type, map_ptr, map_size, start_time, timeout)) goto err_out; if (clock_gettime(CLOCK_MONOTONIC_RAW, &end_time)) { perror("clock_gettime"); @@ -574,7 +601,7 @@ static int ksm_merge_time(int mapping, int prot, int timeout, size_t map_size) return KSFT_FAIL; } -static int ksm_unmerge_time(int mapping, int prot, int timeout, size_t map_size) +static int ksm_unmerge_time(int merge_type, int mapping, int prot, int timeout, size_t map_size) { void *map_ptr; struct timespec start_time, end_time; @@ -589,7 +616,7 @@ static int ksm_unmerge_time(int mapping, int prot, int timeout, size_t map_size) perror("clock_gettime"); goto err_out; } - if (ksm_merge_pages(map_ptr, map_size, start_time, timeout)) + if (ksm_merge_pages(merge_type, map_ptr, map_size, start_time, timeout)) goto err_out; if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) { @@ -621,7 +648,7 @@ static int ksm_unmerge_time(int mapping, int prot, int timeout, size_t map_size) return KSFT_FAIL; } -static int ksm_cow_time(int mapping, int prot, int timeout, size_t page_size) +static int ksm_cow_time(int merge_type, int mapping, int prot, int timeout, size_t page_size) { void *map_ptr; struct timespec start_time, end_time; @@ -660,7 +687,7 @@ static int ksm_cow_time(int mapping, int prot, int timeout, size_t page_size) memset(map_ptr + page_size * i, '+', i / 2 + 1); memset(map_ptr + page_size * (i + 1), '+', i / 2 + 1); } - 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; if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) { @@ -697,6 +724,7 @@ int main(int argc, char *argv[]) int ret, opt; int prot = 0; int ksm_scan_limit_sec = KSM_SCAN_LIMIT_SEC_DEFAULT; + int merge_type = KSM_MERGE_TYPE_DEFAULT; long page_count = KSM_PAGE_COUNT_DEFAULT; size_t page_size = sysconf(_SC_PAGESIZE); struct ksm_sysfs ksm_sysfs_old; @@ -705,7 +733,7 @@ int main(int argc, char *argv[]) bool merge_across_nodes = KSM_MERGE_ACROSS_NODES_DEFAULT; long size_MB = 0; - while ((opt = getopt(argc, argv, "ha:p:l:z:m:s:MUZNPCHD")) != -1) { + while ((opt = getopt(argc, argv, "ha:p:l:z:m:s:t:MUZNPCHD")) != -1) { switch (opt) { case 'a': prot = str_to_prot(optarg); @@ -745,6 +773,17 @@ int main(int argc, char *argv[]) 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); + } + break; case 'M': break; case 'U': @@ -795,35 +834,36 @@ int main(int argc, char *argv[]) switch (test_name) { case CHECK_KSM_MERGE: - ret = check_ksm_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, page_count, + ret = check_ksm_merge(merge_type, MAP_PRIVATE | MAP_ANONYMOUS, prot, page_count, ksm_scan_limit_sec, page_size); break; case CHECK_KSM_UNMERGE: - ret = check_ksm_unmerge(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec, - page_size); + ret = check_ksm_unmerge(merge_type, MAP_PRIVATE | MAP_ANONYMOUS, prot, + ksm_scan_limit_sec, page_size); break; case CHECK_KSM_ZERO_PAGE_MERGE: - ret = check_ksm_zero_page_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, page_count, - ksm_scan_limit_sec, use_zero_pages, page_size); + ret = check_ksm_zero_page_merge(merge_type, MAP_PRIVATE | MAP_ANONYMOUS, prot, + page_count, ksm_scan_limit_sec, use_zero_pages, + page_size); break; case CHECK_KSM_NUMA_MERGE: - ret = check_ksm_numa_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec, - merge_across_nodes, page_size); + ret = check_ksm_numa_merge(merge_type, MAP_PRIVATE | MAP_ANONYMOUS, prot, + ksm_scan_limit_sec, merge_across_nodes, page_size); break; case KSM_MERGE_TIME: if (size_MB == 0) { printf("Option '-s' is required.\n"); return KSFT_FAIL; } - ret = ksm_merge_time(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec, - size_MB); + ret = ksm_merge_time(merge_type, MAP_PRIVATE | MAP_ANONYMOUS, prot, + ksm_scan_limit_sec, size_MB); break; case KSM_MERGE_TIME_HUGE_PAGES: if (size_MB == 0) { printf("Option '-s' is required.\n"); return KSFT_FAIL; } - ret = ksm_merge_hugepages_time(MAP_PRIVATE | MAP_ANONYMOUS, prot, + ret = ksm_merge_hugepages_time(merge_type, MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec, size_MB); break; case KSM_UNMERGE_TIME: @@ -831,12 +871,12 @@ int main(int argc, char *argv[]) printf("Option '-s' is required.\n"); return KSFT_FAIL; } - ret = ksm_unmerge_time(MAP_PRIVATE | MAP_ANONYMOUS, prot, + ret = ksm_unmerge_time(merge_type, MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec, size_MB); break; case KSM_COW_TIME: - ret = ksm_cow_time(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec, - page_size); + ret = ksm_cow_time(merge_type, MAP_PRIVATE | MAP_ANONYMOUS, prot, + ksm_scan_limit_sec, page_size); break; } From patchwork Mon Jan 23 17:37:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13112650 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 7BE2EC54EAA for ; Mon, 23 Jan 2023 17:38:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DFB946B0093; Mon, 23 Jan 2023 12:38:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D5CC06B0095; Mon, 23 Jan 2023 12:38:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A9C9A6B0098; Mon, 23 Jan 2023 12:38:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 7B4676B0093 for ; Mon, 23 Jan 2023 12:38:41 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 5AE1C162226 for ; Mon, 23 Jan 2023 17:38:41 +0000 (UTC) X-FDA: 80386773642.17.9B1336B Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf29.hostedemail.com (Postfix) with ESMTP id 860C312000F for ; Mon, 23 Jan 2023 17:38:39 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf29.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674495519; 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:in-reply-to:references:references; bh=qo39C54PDBSxRE2rOgUDTJu01fh/PEDP/brvH8YsOkc=; b=7emCvSyUEeG0ger1C0rOSSrBMYdrOB8sPwwgH4h2nYxE4gf3KYNCOvIEXnAIt73fLwV78y 6VWEeP/2/IuIwzx1XItQ7r7Xg0qbpWDeKB6W81P6az1+6ZHLz1+FIMefx0pWubOtwYIAWa lPdcIAK/p2kjV7sxXCHFDeihgXyBVpQ= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf29.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674495519; a=rsa-sha256; cv=none; b=E2qbU0tXhii9Tw4p58dbxhs8+2mzoBq3w/bQzJ9sJlgzHF3DYUR/jInssHR6h+btnDFWlt txmrpdKOHV5LCkMAaYpCRqUI6R31f+SyIJXXbDQoUwzWYCwac8F5qprFo8/jQ/U9VZrZoN DcaKTQohB3eBnkWzHSVdRFOCTuY+4Xo= Received: by dev0134.prn3.facebook.com (Postfix, from userid 425415) id 8864D5616BF7; Mon, 23 Jan 2023 09:37:56 -0800 (PST) From: Stefan Roesch To: linux-mm@kvack.org Cc: shr@devkernel.io, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND RFC PATCH v1 18/20] selftests/vm: add KSM get merge type test Date: Mon, 23 Jan 2023 09:37:46 -0800 Message-Id: <20230123173748.1734238-19-shr@devkernel.io> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230123173748.1734238-1-shr@devkernel.io> References: <20230123173748.1734238-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 860C312000F X-Stat-Signature: 5cwyo4s7ysxjkhgg1fhti63sgyiqk9ji X-HE-Tag: 1674495519-777232 X-HE-Meta: U2FsdGVkX19h8k2GsQI6CjTSOhUcxH8zO74GHR7uYE6mGovosQ/8zMIW8As5SlYYOx5uy8xo0cmbrDQikU6/N7FDfxjzqWOLrtcS/QBJWxKZII9TL0pxJzCi9b/pUaEtAcoHLm8VeoyqrZKBKX+lEWaNT1y3BTV1+0EvUEsH2GYgpGzciLH+lfpcC+Qdxdd7bnv4eKys6PigUO0zBMPxhdMsws3dg/F15m8G6lFJc4BE+pzuWXaiL+ZggxeQNo/P7KbmGmm7I3WxwmYG9XrUy4APq9RXJkuk0FsH3dRYglCapKR7I6Ox0Buq/7Py/pbVtunGtiVcTUN0hjxVidG97XKqw6zd/w+v8+eoIK6FGYDGBoxy75Zby9IvsSyEtCY21xbnHEJ8O6A1mfNz3J0vrLS/GPyScLMrRN/uHU7mwf6Mxw6u9A1+ntQ6QUMCjliXmbVPGGTHtVXSHaTsuTAt26+CVbMMBaLh0Kz68eyZklx/UDRTrLWL/4c2NAmMrHEddijk4uM4NDHzcdcF+H+ncyk+JNX8iw9+5VmZb2Y070KO+BI7/sqJkUeQFnJa4d8srY01zXJj1HiJ7amJso4DLfpXY3Cp7V+7HOeNbPgawlWzhVuA++vLkvM8jwVwtIEf8kIoaGzP+q+2faubsYt3eZfsFxkzgAWD0XW/geqOtunhlYUGgAHw/normLejKmCIBNjicGio9xOIHe5UUrHh3khf3lAOCY16zXWZBA1vCsF7Hkgbq6x0OJnpEPHv0iQSK6H53fyYQcWrzwgaeMZCz0mh/9HJI3McJfTUFY1o3ADMvBuCpB2cHD1S+CV2HPwUceyWgUSALa4koflGmKo6mr1ElDZ5GGb40R0snT1UF757Dsu9FNn45QEstq0Vsb7s2e3TNM7wMcryYTM6JpjgdGar/TBdTohPkq+kRi4fanDz7uAubkFCNDxdlKViit2WXvSpygIIAChCfoestL7 uFIXQT5l H5ffPeVuBnvo9hRxri5JPEaSlhnt4qk3KzkgrPfJRJ1nUDMQ= 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: This adds the -G flag to the ksm_tests to query if prctl has been used to enable ksm merging. Signed-off-by: Stefan Roesch --- tools/testing/selftests/vm/ksm_tests.c | 37 ++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/vm/ksm_tests.c b/tools/testing/selftests/vm/ksm_tests.c index 386a0929c8a3..9667cb3b8c6a 100644 --- a/tools/testing/selftests/vm/ksm_tests.c +++ b/tools/testing/selftests/vm/ksm_tests.c @@ -45,6 +45,7 @@ enum ksm_merge_type { enum ksm_test_name { CHECK_KSM_MERGE, CHECK_KSM_UNMERGE, + CHECK_KSM_GET_MERGE_TYPE, CHECK_KSM_ZERO_PAGE_MERGE, CHECK_KSM_NUMA_MERGE, KSM_MERGE_TIME, @@ -124,7 +125,8 @@ static void print_help(void) " -D evaluate unmerging time and speed when disabling KSM.\n" " For this test, the size of duplicated memory area (in MiB)\n" " must be provided using -s option\n" - " -C evaluate the time required to break COW of merged pages.\n\n"); + " -C evaluate the time required to break COW of merged pages.\n" + " -G query merge mode\n\n"); printf(" -a: specify the access protections of pages.\n" " must be of the form [rwx].\n" @@ -323,6 +325,31 @@ static int check_ksm_merge(int merge_type, int mapping, int prot, return KSFT_FAIL; } +static int check_ksm_get_merge_type(void) +{ + if (prctl(PR_SET_MEMORY_MERGE, 1)) { + perror("prctl set"); + return 1; + } + + int is_on = prctl(PR_GET_MEMORY_MERGE, 0); + + if (prctl(PR_SET_MEMORY_MERGE, 0)) { + perror("prctl set"); + return 1; + } + + int is_off = prctl(PR_GET_MEMORY_MERGE, 0); + + if (is_on && is_off) { + printf("OK\n"); + return KSFT_PASS; + } + + printf("Not OK\n"); + return KSFT_FAIL; +} + static int check_ksm_unmerge(int merge_type, int mapping, int prot, int timeout, size_t page_size) { void *map_ptr; @@ -733,7 +760,7 @@ int main(int argc, char *argv[]) bool merge_across_nodes = KSM_MERGE_ACROSS_NODES_DEFAULT; long size_MB = 0; - while ((opt = getopt(argc, argv, "ha:p:l:z:m:s:t:MUZNPCHD")) != -1) { + while ((opt = getopt(argc, argv, "ha:p:l:z:m:s:t:GMUZNPCHD")) != -1) { switch (opt) { case 'a': prot = str_to_prot(optarg); @@ -792,6 +819,9 @@ int main(int argc, char *argv[]) case 'Z': test_name = CHECK_KSM_ZERO_PAGE_MERGE; break; + case 'G': + test_name = CHECK_KSM_GET_MERGE_TYPE; + break; case 'N': test_name = CHECK_KSM_NUMA_MERGE; break; @@ -841,6 +871,9 @@ int main(int argc, char *argv[]) ret = check_ksm_unmerge(merge_type, MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec, page_size); break; + case CHECK_KSM_GET_MERGE_TYPE: + ret = check_ksm_get_merge_type(); + break; case CHECK_KSM_ZERO_PAGE_MERGE: ret = check_ksm_zero_page_merge(merge_type, MAP_PRIVATE | MAP_ANONYMOUS, prot, page_count, ksm_scan_limit_sec, use_zero_pages, From patchwork Mon Jan 23 17:37:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13112654 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 B5FDFC05027 for ; Mon, 23 Jan 2023 17:38:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7B9416B009A; Mon, 23 Jan 2023 12:38:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6F40B6B0099; Mon, 23 Jan 2023 12:38:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 595116B009A; Mon, 23 Jan 2023 12:38:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 42C4E6B0098 for ; Mon, 23 Jan 2023 12:38:47 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 194DCC179E for ; Mon, 23 Jan 2023 17:38:47 +0000 (UTC) X-FDA: 80386773894.10.C72FC6C Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf11.hostedemail.com (Postfix) with ESMTP id 71B2A4000B for ; Mon, 23 Jan 2023 17:38:45 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=none; spf=neutral (imf11.hostedemail.com: 66.220.144.178 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=1674495525; 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:in-reply-to:references:references; bh=3bAJwifag69+je0WBSnlVAQ9+kANBDi2Pdxva4sytJM=; b=TQGxRxP9O/Rw3SQ1JC96vTipyfsqw4kaeUivewOVOIspbfVY5fs330BXVtGNSJc21ASCgQ uKbGYouunSi7OxiRij3B3BlLkOT6i3oW/Uym3NvhgITXy1R+XJASoUVIvoQBwcv0mLGXMX b43qGAvrUpgXg2Wy4p6z1axqfcsZDto= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=none; spf=neutral (imf11.hostedemail.com: 66.220.144.178 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=1674495525; a=rsa-sha256; cv=none; b=P5zLA7ImVhcPJ6gTgXtZVNDJtLcgRWeJQd8kLXtcm0HFIb0wUqeFvcZ0xk+7jvBw1Z4r6C vYD4J5a8q3bg7ywzX76G+12se6+oFhjZ1mHQlRYeh2JSR5OX8vjIk3dQbkOfzCA4EDNvek I39SLm/UZRWuQABEqqlorM0lAJg/IOM= Received: by dev0134.prn3.facebook.com (Postfix, from userid 425415) id 8CE0B5616BF9; Mon, 23 Jan 2023 09:37:56 -0800 (PST) From: Stefan Roesch To: linux-mm@kvack.org Cc: shr@devkernel.io, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND RFC PATCH v1 19/20] selftests/vm: add KSM fork test Date: Mon, 23 Jan 2023 09:37:47 -0800 Message-Id: <20230123173748.1734238-20-shr@devkernel.io> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230123173748.1734238-1-shr@devkernel.io> References: <20230123173748.1734238-1-shr@devkernel.io> MIME-Version: 1.0 X-Stat-Signature: x5d1dm5388o6hiiqgwzwgrekfp3o9pyb X-Rspam-User: X-Rspamd-Queue-Id: 71B2A4000B X-Rspamd-Server: rspam06 X-HE-Tag: 1674495525-296276 X-HE-Meta: U2FsdGVkX19/c9ocVigTz45QmsqN84S2JCFHij+FtdHmHSOmPkxN8rKmmwNnlXpc5WM1snzFIgwrnNn/f0qvvk0uyk+T9z0Sy+uVIdNnwHXlfDBjWx4lncc1XJ6pIh4Utb8dmUguDwkS9ikQ4Xsatc29VmxJLUqLZE96DklIBhayJ8UQqSI15AX9k/mGdfPaXbuWcpj2mHUPaz5m5zYrFc6yLFVtCWJ/Q9JwhYkjn1mdATkkJWX/TB5alHU1fL4RhhqxjFvcL/BYhJ3VNZsJrJmIwgEI3pRMGDFnkEPDrK5QWDspztzfZTcPABOqaR4DzzcUQqywhQ2IX3YOou5XxLHBjsalqK3hvU5gHE4TAj79iDCaC58nOVbjDG8C77n8zWOXHJiB0lMlKq8AEGxTjaZIdfh4aWfmEVoUWL97S4dudqBqOcY3skGacWhKdFWQEWuhEoEwZyOz0Re6odiB3orASpfRnIlDD/XoEx28qYF9fNI8ECVIBOBdiYsJE5OLV9ilpNZYEhGDW/RJ5aY7dCKSVxQzNS8WkVAGW3MCswJy2YUcGBNqD++WlmigLP+ZrJV8F2uf8fttvPcI2l5l7s/5k/CAeEdvEByqtcbryBVTrl8LQHqgXeRGyI3LfoFzhDt8cXWNFUrWHn9TDonV6MZwkAPqr5QOWh1YYAhjTxS8lFfGzYc6133WPrjtjkFi3hl7vggvM9lqBz7jxDSY0K/+P+lgY7gspTpa4gojTFiCnk/lRGfErYPEFk+zEVYcY8ReHJ93tDp849dz6c/Zgm3rQP6Wmd6kQtcNTo6XmCZ50PfZQlzIjjpdZ7a8XwXfQ3r2rLjk7ZV8yoRbdP8bsT44rqNS4EJtwwq3mykWpFYQNN3fwrthyj+60aVE1nmirCWB2KP3w9rEJlzpwCAnBXZoKr2B7FVZOtauntuqNYGfi0hlBkdv9cceiLPKdYaVIBg9azC2nlqRnTg9hd2 2AnQrcHC AXdrdsUMZB+KXaoNik2pslAtrpR19CN4Z05Omzf9BUTWf4B7nsuTHd1pKBw== 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: Add fork test to verify that the MMF_VM_MERGE_ANY flag is inherited by the child process. Signed-off-by: Stefan Roesch --- tools/testing/selftests/vm/ksm_tests.c | 53 +++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/vm/ksm_tests.c b/tools/testing/selftests/vm/ksm_tests.c index 9667cb3b8c6a..a0a48ac43b29 100644 --- a/tools/testing/selftests/vm/ksm_tests.c +++ b/tools/testing/selftests/vm/ksm_tests.c @@ -44,6 +44,7 @@ enum ksm_merge_type { enum ksm_test_name { CHECK_KSM_MERGE, + CHECK_KSM_MERGE_FORK, CHECK_KSM_UNMERGE, CHECK_KSM_GET_MERGE_TYPE, CHECK_KSM_ZERO_PAGE_MERGE, @@ -126,7 +127,8 @@ static void print_help(void) " For this test, the size of duplicated memory area (in MiB)\n" " must be provided using -s option\n" " -C evaluate the time required to break COW of merged pages.\n" - " -G query merge mode\n\n"); + " -G query merge mode\n" + " -F evaluate that the KSM process flag is inherited\n\n"); printf(" -a: specify the access protections of pages.\n" " must be of the form [rwx].\n" @@ -325,6 +327,47 @@ static int check_ksm_merge(int merge_type, int mapping, int prot, return KSFT_FAIL; } +/* Verify that prctl ksm flag is inherited. */ +static int check_ksm_fork(void) +{ + int rc = KSFT_FAIL; + pid_t child_pid; + + if (prctl(PR_SET_MEMORY_MERGE, 1)) { + perror("prctl"); + return KSFT_FAIL; + } + + child_pid = fork(); + if (child_pid == 0) { + int is_on = prctl(PR_GET_MEMORY_MERGE, 0); + + if (!is_on) + exit(KSFT_FAIL); + + exit(KSFT_PASS); + } + + if (child_pid < 0) + goto out; + + if (waitpid(child_pid, &rc, 0) < 0) + rc = KSFT_FAIL; + + if (prctl(PR_SET_MEMORY_MERGE, 0)) { + perror("prctl"); + rc = KSFT_FAIL; + } + +out: + if (rc == KSFT_PASS) + printf("OK\n"); + else + printf("Not OK\n"); + + return rc; +} + static int check_ksm_get_merge_type(void) { if (prctl(PR_SET_MEMORY_MERGE, 1)) { @@ -760,7 +803,7 @@ int main(int argc, char *argv[]) bool merge_across_nodes = KSM_MERGE_ACROSS_NODES_DEFAULT; long size_MB = 0; - while ((opt = getopt(argc, argv, "ha:p:l:z:m:s:t:GMUZNPCHD")) != -1) { + while ((opt = getopt(argc, argv, "ha:p:l:z:m:s:t:FGMUZNPCHD")) != -1) { switch (opt) { case 'a': prot = str_to_prot(optarg); @@ -811,6 +854,9 @@ int main(int argc, char *argv[]) merge_type = atoi(optarg); } break; + case 'F': + test_name = CHECK_KSM_MERGE_FORK; + break; case 'M': break; case 'U': @@ -867,6 +913,9 @@ int main(int argc, char *argv[]) ret = check_ksm_merge(merge_type, MAP_PRIVATE | MAP_ANONYMOUS, prot, page_count, ksm_scan_limit_sec, page_size); break; + case CHECK_KSM_MERGE_FORK: + ret = check_ksm_fork(); + break; case CHECK_KSM_UNMERGE: ret = check_ksm_unmerge(merge_type, MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec, page_size); From patchwork Mon Jan 23 17:37:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13112652 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 C4F89C05027 for ; Mon, 23 Jan 2023 17:38:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D10DB6B0096; Mon, 23 Jan 2023 12:38:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C721F6B0098; Mon, 23 Jan 2023 12:38:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A9F946B0099; Mon, 23 Jan 2023 12:38:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 88A796B0098 for ; Mon, 23 Jan 2023 12:38:42 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 3F19BC2B04 for ; Mon, 23 Jan 2023 17:38:42 +0000 (UTC) X-FDA: 80386773684.08.C3B9A3F Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf20.hostedemail.com (Postfix) with ESMTP id 85C091C001C for ; Mon, 23 Jan 2023 17:38:40 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=none; spf=neutral (imf20.hostedemail.com: 66.220.144.178 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=1674495520; a=rsa-sha256; cv=none; b=3iu43tXdWgQ3hdjEoSaQlcRlTzW32a6kXnMa2Ta1NaV+s7BYZ5Qfa/Jp8UKMENknQdz+Gl YQNTOqQddmuUjmg9reYVCspiN0c+1ehXE7ZI8QVRktYS98bFIbZE+Rd3hEKTdSCEYXEnI9 Spk+6RnEGKbS44ZHPoUfdfAb6iEQiFE= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=none; spf=neutral (imf20.hostedemail.com: 66.220.144.178 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=1674495520; 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:in-reply-to:references:references; bh=taEcnC/WZm0w3gtaQ6mI5xKRplneQ9QYqvw5OXfWRYE=; b=Vd2DyfPWMiyXZ1ACMhDlOkSsWxdcgzLkysUYkBPoIT1ck2vAub+198ImhtPL7MdUAk44xM fanzQXs0GdYPVTt/Mrrq6fynNE7IOVCQFLLqYleJboeXDVSXHrl8eMZ6kYghoeAj3If+32 dN1Rkx4D+s0Ra/HY25u0wTCGVO+cAmU= Received: by dev0134.prn3.facebook.com (Postfix, from userid 425415) id 910905616BFB; Mon, 23 Jan 2023 09:37:56 -0800 (PST) From: Stefan Roesch To: linux-mm@kvack.org Cc: shr@devkernel.io, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND RFC PATCH v1 20/20] selftests/vm: add two functions for debugging merge outcome Date: Mon, 23 Jan 2023 09:37:48 -0800 Message-Id: <20230123173748.1734238-21-shr@devkernel.io> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230123173748.1734238-1-shr@devkernel.io> References: <20230123173748.1734238-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 85C091C001C X-Rspamd-Server: rspam01 X-Stat-Signature: h7mdz3ynryqiagc1h61fkmpbma3jh9qy X-HE-Tag: 1674495520-567002 X-HE-Meta: U2FsdGVkX18jW8ZRlgodpIQ3usLFGlyOQ4SUPgihnNsrROYMMzQoESkFC8adi4esp5UHkB2hJjEB0QcxbId6tQUOh9OtpNvrgxVkoodUM4LwnqAmsGpLrO9Q7HqFHc3is/DgucVz/JYzHLIXTrjFKLLBWSL4xGnmdH+IxEMhiLeLYfq5XI2p6DXnw/Nk+v0qy80M7rSizkFBiQO/S09Oqc2EB44m0eutgmD0v4KPjGRbIGO3jRrTeBosjb++G0Qv3wh9+AEMv72nIMzqUyt65efwnZoz+YwDwiT24iDJ76kFun1UeTOwezotezNSx1r2Ual6r5mvleer+jk9/JdR0ehe9n9S5oTwCA6S1Nfv8XHTyb0UE9i5XH9zHiEYeJ3XGeCYNo4J5/Twa9KP4+bINDPAX+xO2uq47erFiX8AD0fdjPHqqwHaIGyW0KegeC2/Ag5+MfNnIh9WXcIHVHKPUy8n5BpKztzjkNbsE0cl9nGKXD30c/GI4NGThHiSRH3NLopacmV16Elbkoq0GL+w7ZZCg/D1jYgruOPKGTyyTIyFRcT1sWg73xBgEa/c2fsGojqJAMc/Jey+/kY8HKgkvhu/E9B6AgQUdRSMcm/VsKCB4IF3b0RPPuJMKUmQy5VkS7mlgaZdWpPM6TMiYVD2w1jUU9eyG8T7g6Pc3bK0SdZZc9SJYHhZCZ+Uh6YNOvWeBdGHUFbO69SLpwos6Fzdg6Nuy39I3phRowTbAM4A+QIhCTZuRHQW3Ln6z3x+MJ6dM8UWuwSRAtA/6Bj2dNVL3hHO1sLTftr9LM+BJu/udgqsjKO7Rn19rYkSXyUhnqzciy3P+pJasjOCfc+cc6jZGNwc8oO3cp4n/6aoEzYG41g5/Qmcf+Bh2zHvyRayoW/TmegrRSo5k4oPAzp96u/wukIVem1ztxZ7ajCThFYNc5YbdOpim74eAHmEwI+CHFt/MPTBoj/XO+LKhlv5twp pI9+lfYS DCOx3X95QfQwSZBDqfHuqNPJReu8RZHuXz56jI31ZuB3onm0= 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: This adds two functions to report the metrics in /proc/self/ksm_stat and /sys/kernel/debug/mm/ksm. The debugging can be enabled with the following command line: make -C tools/testing/selftests TARGETS="vm" --keep-going \ EXTRA_CFLAGS=-DDEBUG=1 Signed-off-by: Stefan Roesch --- tools/testing/selftests/vm/ksm_tests.c | 54 ++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tools/testing/selftests/vm/ksm_tests.c b/tools/testing/selftests/vm/ksm_tests.c index a0a48ac43b29..9fb21b982dc9 100644 --- a/tools/testing/selftests/vm/ksm_tests.c +++ b/tools/testing/selftests/vm/ksm_tests.c @@ -93,6 +93,55 @@ static int ksm_read_sysfs(const char *file_path, unsigned long *val) return 0; } +#ifdef DEBUG +static void ksm_print_sysfs(void) +{ + unsigned long max_page_sharing, pages_sharing, pages_shared; + unsigned long full_scans, pages_unshared, pages_volatile; + unsigned long stable_node_chains, stable_node_dups; + long general_profit; + + if (ksm_read_sysfs(KSM_FP("pages_shared"), &pages_shared) || + ksm_read_sysfs(KSM_FP("pages_sharing"), &pages_sharing) || + ksm_read_sysfs(KSM_FP("max_page_sharing"), &max_page_sharing) || + ksm_read_sysfs(KSM_FP("full_scans"), &full_scans) || + ksm_read_sysfs(KSM_FP("pages_unshared"), &pages_unshared) || + ksm_read_sysfs(KSM_FP("pages_volatile"), &pages_volatile) || + ksm_read_sysfs(KSM_FP("stable_node_chains"), &stable_node_chains) || + ksm_read_sysfs(KSM_FP("stable_node_dups"), &stable_node_dups) || + ksm_read_sysfs(KSM_FP("general_profit"), (unsigned long *)&general_profit)) + return; + + printf("pages_shared : %lu\n", pages_shared); + printf("pages_sharing : %lu\n", pages_sharing); + printf("max_page_sharing : %lu\n", max_page_sharing); + printf("full_scans : %lu\n", full_scans); + printf("pages_unshared : %lu\n", pages_unshared); + printf("pages_volatile : %lu\n", pages_volatile); + printf("stable_node_chains: %lu\n", stable_node_chains); + printf("stable_node_dups : %lu\n", stable_node_dups); + printf("general_profit : %ld\n", general_profit); +} + +static void ksm_print_procfs(void) +{ + const char *file_name = "/proc/self/ksm_stat"; + char buffer[512]; + FILE *f = fopen(file_name, "r"); + + if (!f) { + fprintf(stderr, "f %s\n", file_name); + perror("fopen"); + return; + } + + while (fgets(buffer, sizeof(buffer), f)) + printf("%s", buffer); + + fclose(f); +} +#endif + static int str_to_prot(char *prot_str) { int prot = 0; @@ -237,6 +286,11 @@ static bool assert_ksm_pages_count(long dupl_page_count) ksm_read_sysfs(KSM_FP("max_page_sharing"), &max_page_sharing)) return false; +#ifdef DEBUG + ksm_print_sysfs(); + ksm_print_procfs(); +#endif + /* * Since there must be at least 2 pages for merging and 1 page can be * shared with the limited number of pages (max_page_sharing), sometimes