From mboxrd@z Thu Jan 1 00:00:00 1970 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 7C8B2E77188 for ; Tue, 14 Jan 2025 15:46:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0FA7C6B0083; Tue, 14 Jan 2025 10:46:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0AA42280001; Tue, 14 Jan 2025 10:46:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E8D026B0089; Tue, 14 Jan 2025 10:46:27 -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 C48A86B0083 for ; Tue, 14 Jan 2025 10:46:27 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 4B0181A0B38 for ; Tue, 14 Jan 2025 15:46:27 +0000 (UTC) X-FDA: 83006484414.30.9024FAE Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf18.hostedemail.com (Postfix) with ESMTP id C9C9E1C0005 for ; Tue, 14 Jan 2025 15:46:24 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=VI5uJdUl; spf=pass (imf18.hostedemail.com: domain of mcasquer@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=mcasquer@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736869585; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Po7sFqkgiNpWHgnYbDTLwK+PJsUBPIEB6Kwf5vaywco=; b=AMzzPMsGcJ3WTQULHLIEws2q+HgXrfshVa26gPnR5YRRog4r02N9lftNGtr2Woj6XPbm3D S9azMLfyZ/xM3X8xE/rVcLtk0Kel6XxQU7QA9yhygchrEp94LrOh05XvKj2qx+/mvY2Nwm 43uIDzYvTZ5KZDG/AnpPx9zbPttl5ek= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=VI5uJdUl; spf=pass (imf18.hostedemail.com: domain of mcasquer@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=mcasquer@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736869585; a=rsa-sha256; cv=none; b=gnVoag1AwlRjMkAtkIapeqiC/zGgqA7z8TodBwx6ZZ1+p5VyDrPLUv2Ep4NVuf/KAsUXtC TRz6y4f+o10ywtpSMfr3SpVSywxR9fUXLsDtJ2HUZJBkevYmL5rB+PNesRY0kJBAbO2Mef ypcJBel63tsFMIUm86sMRwedwajwunA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736869584; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Po7sFqkgiNpWHgnYbDTLwK+PJsUBPIEB6Kwf5vaywco=; b=VI5uJdUl9tHKny3HTs1PIVF65ptEz5QGILUy1xVfWyh1ZEKAhINb8LDngVCi6kxb7WCizw AsIQQibep9F7VLqUbVBioJfdq+yrWZeMmZk0CyJ9S9pM/uOj4ViN7pQ8KF0EUtMk8XEV7y suDaOzGUJOURZKmSGvWutoB5tJTWxDk= Received: from mail-lf1-f71.google.com (mail-lf1-f71.google.com [209.85.167.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-17-eXDKJwygOIWqLObEJhA3-A-1; Tue, 14 Jan 2025 10:46:22 -0500 X-MC-Unique: eXDKJwygOIWqLObEJhA3-A-1 X-Mimecast-MFC-AGG-ID: eXDKJwygOIWqLObEJhA3-A Received: by mail-lf1-f71.google.com with SMTP id 2adb3069b0e04-53e3bb933easo3091587e87.3 for ; Tue, 14 Jan 2025 07:46:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736869581; x=1737474381; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Po7sFqkgiNpWHgnYbDTLwK+PJsUBPIEB6Kwf5vaywco=; b=P8qa7oVi10vHs9EBLJ5cNaY3ZKajEyU7X+LWF9jwOX97QVgtnPV5RRI+ivi/l7kktt ix7vX1medINlLYn39tgAPW9Lx3SgxJUl4xm0M54d6G3IIqK3/izFO2eqtZV9iAquOAJm MYBOb5ebFH0TO+nekgiyF3DYpr7ejpPD4e1MuFTqSrslvJCP/vWwOsiRqKj9DoFTElMw eFWKQLcUi9/S/yo70lTxm3lkPU/AqLDeqghB/c8/SVFXOV0Qfj6ma/fUuugnf9rfaI/X AeOgM7VmEzPwIxt+5weCyn2yccFpdJM+0C5DOaR549LttXpo9owtvDreP3QFT+8mVIB1 uVVg== X-Forwarded-Encrypted: i=1; AJvYcCXFf/xK4Nph9dTeOnxvxUV88Tlxce/ZcuJpzmC/D+jaNJWePLY36RrreFWAiNDfi7slo4KNpYxYHg==@kvack.org X-Gm-Message-State: AOJu0YwA58yoNgI7x+PDsBoT66irks08PqQLeNWuM6HijMbGoMUobCmp Jz/yKi06cMo6WagyjTIIifGrECisQtw1FrGt5DwfH7+pMurEJPYfgcxIHJpugm8WLPU7+n3shKv 4PHQYft/9pK8M/cpnVEfzozA982nt68hNbkrjVooJIs6drqKOf8LQOvxdVB/JPi49HKyweVDFLK aYKRMxrtHfHIhzOv3bTDzrh8k= X-Gm-Gg: ASbGnctvjKv+i2uQc2ZvThcyJi21CL/DJodgMjMM4gbJ3IRZ8+IwLA+3GAI8q4aojtW au0hDL4Y7W8sUQkdAp2xaM34E8ZFbCnA7vc0TVy0= X-Received: by 2002:a05:6512:2395:b0:542:62e6:4517 with SMTP id 2adb3069b0e04-542845a6e4amr9119541e87.12.1736869580870; Tue, 14 Jan 2025 07:46:20 -0800 (PST) X-Google-Smtp-Source: AGHT+IGX+fhtb56T2Rw9G3WQOoy+tQ06Xt8/tniSED3WskgU41w/L1OqJZex71LoSnemXhht/EINT4HHtclNivAaN2Y= X-Received: by 2002:a05:6512:2395:b0:542:62e6:4517 with SMTP id 2adb3069b0e04-542845a6e4amr9119528e87.12.1736869580392; Tue, 14 Jan 2025 07:46:20 -0800 (PST) MIME-Version: 1.0 References: <20250110174034304QOb8eDoqtFkp3_t8mqnqc@zte.com.cn> In-Reply-To: <20250110174034304QOb8eDoqtFkp3_t8mqnqc@zte.com.cn> From: Mario Casquero Date: Tue, 14 Jan 2025 16:46:08 +0100 X-Gm-Features: AbW1kvY1Q6s4-03Ic3hBYPuGXV99wbPD42iHvf9Tw3ShFrSa5uqs9q-bXf_bX2c Message-ID: Subject: Re: [PATCH v5] ksm: add ksm involvement information for each process To: xu.xin16@zte.com.cn Cc: akpm@linux-foundation.org, david@redhat.com, linux-kernel@vger.kernel.org, wang.yaxin@zte.com.cn, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, yang.yang29@zte.com.cn X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: BsFgaO2S3KbPYbG8SISUTITnlRS8La-Iksg4E-rJSs0_1736869581 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X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Un/xOSIRIuE5U7xa5bR9m8v3GBjxcek6 5LkqRElL H7ye8wPwmf2qL2GSLASb9/6+Z/UEudoIhT9eKfNZevLUVygAMa53wEKkc7ArB3lPTHiXMtaX5Jo3KkT/BOfXTp7ix8u8GjuvVLo4uEUMGqt4wi6cLJX1KjWhi6qRLuCMUyOayNXOVOsAvjr9NnuklPOMX25Sud8FHcqB0VcPSnxjF+tFykLxC6Vh0JL4sp2idDySk2RR6iMPkgvu4yXS9aOWkPgrUjAyMSxUYtBsNAI5CIvPqNV+rgLoDt9jWfnQHcExwk08lcAob6WtMTiFMohbXICccAIpF6EwA/lwP4kQdZFkg3+Yrvz2fXd0q5V9i4z/zQXe2R3a8R1+5uXPDET6Bp+S8OGodSauf3WYa5KxTK88wR9+ZGpq0i5nYeDOmREQHYs2dR8jH7do= 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: List-Subscribe: List-Unsubscribe: This patch has been successfully tested. After starting the KSM and obtaining the PID of a process, e.g. qemu-kvm PID, now the new items can be checked. echo 1 > /sys/kernel/mm/ksm/run pid=3D$(pgrep -f "qemu-kvm") cat /proc/$pid/ksm_stat ksm_rmap_items 70692 ksm_zero_pages 0 ksm_merging_pages 4211 ksm_process_profit 12723968 ksm_merge_any: no ksm_mergeable: yes Tested-by: Mario Casquero On Fri, Jan 10, 2025 at 10:52=E2=80=AFAM wrote: > > From: xu xin > > In /proc//ksm_stat, Add two extra ksm involvement items including > KSM_mergeable and KSM_merge_any. It helps administrators to > better know the system's KSM behavior at process level. > > ksm_merge_any: yes/no > whether the process'mm is added by prctl() into the candidate lis= t > of KSM or not, and fully enabled at process level. > > ksm_mergeable: yes/no > whether any VMAs of the process'mm are currently applicable to KSM. > > Purpose > =3D=3D=3D=3D=3D=3D=3D > These two items are just to improve the observability of KSM at process > level, so that users can know if a certain process has enable KSM. > > For example, if without these two items, when we look at > /proc//ksm_stat and there's no merging pages found, We are not sure > whether it is because KSM was not enabled or because KSM did not > successfully merge any pages. > > Althrough "mg" in /proc//smaps indicate VM_MERGEABLE, it's opaque > and not very obvious for non professionals. > > Signed-off-by: xu xin > Cc: Wang Yaxin > --- > Changelog v4 -> v5: > 1. Update the documentation. > 2. Correct a comment sentence and add purpose statment in commit message. > --- > Documentation/filesystems/proc.rst | 66 ++++++++++++++++++++++++++++++++= ++++++ > fs/proc/base.c | 11 +++++++ > include/linux/ksm.h | 1 + > mm/ksm.c | 19 +++++++++++ > 4 files changed, 97 insertions(+) > > diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesyste= ms/proc.rst > index 6a882c57a7e7..916f83203de0 100644 > --- a/Documentation/filesystems/proc.rst > +++ b/Documentation/filesystems/proc.rst > @@ -48,6 +48,7 @@ fixes/update part 1.1 Stefani Seibold June 9 2009 > 3.11 /proc//patch_state - Livepatch patch operation state > 3.12 /proc//arch_status - Task architecture specific information > 3.13 /proc//fd - List of symlinks to open files > + 3.14 /proc/ > 4 Configuring procfs > 4.1 Mount options > @@ -2232,6 +2233,71 @@ The number of open files for the process is stored= in 'size' member > of stat() output for /proc//fd for fast access. > ------------------------------------------------------- > > +3.14 /proc/ +-------------------------------------------------------------------- > +When CONFIG_KSM is enabled, each process has this file which displays > +the information of ksm merging status. > + > +Example > +~~~~~~~ > + > +:: > + > + / # cat /proc/self/ksm_stat > + ksm_rmap_items 0 > + ksm_zero_pages 0 > + ksm_merging_pages 0 > + ksm_process_profit 0 > + ksm_merge_any: no > + ksm_mergeable: no > + > +Description > +~~~~~~~~~~~ > + > +ksm_rmap_items > +^^^^^^^^^^^^^^ > + > +The number of ksm_rmap_item structure in use. The structure of > +ksm_rmap_item is to store the reverse mapping information for virtual > +addresses. KSM will generate a ksm_rmap_item for each ksm-scanned page > +of the process. > + > +ksm_zero_pages > +^^^^^^^^^^^^^^ > + > +When /sys/kernel/mm/ksm/use_zero_pages is enabled, it represent how many > +empty pages are merged with kernel zero pages by KSM. > + > +ksm_merging_pages > +^^^^^^^^^^^^^^^^^ > + > +It represents how many pages of this process are involved in KSM merging > +(not including ksm_zero_pages). It is the same with what > +/proc//ksm_merging_pages shows. > + > +ksm_process_profit > +^^^^^^^^^^^^^^^^^^ > + > +The profit that KSM brings (Saved bytes). KSM can save memory by merging > +identical pages, but also can consume additional memory, because it need= s > +to generate a number of rmap_items to save each scanned page's brief rma= p > +information. Some of these pages may be merged, but some may not be able= d > +to be merged after being checked several times, which are unprofitable > +memory consumed. > + > +ksm_merge_any > +^^^^^^^^^^^^^ > + > +It specifies whether the process'mm is added by prctl() into the candida= te list > +of KSM or not, and KSM scanning is fully enabled at process level. > + > +ksm_mergeable > +^^^^^^^^^^^^^ > + > +It specifies whether any VMAs of the process'mm are currently applicable= to KSM. > + > +More information about KSM can be found at Documentation/admin-guide/mm/= ksm.rst. > + > > Chapter 4: Configuring procfs > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D > diff --git a/fs/proc/base.c b/fs/proc/base.c > index 0edf14a9840e..a50b222a5917 100644 > --- a/fs/proc/base.c > +++ b/fs/proc/base.c > @@ -3269,6 +3269,7 @@ static int proc_pid_ksm_stat(struct seq_file *m, st= ruct pid_namespace *ns, > struct pid *pid, struct task_struct *task= ) > { > struct mm_struct *mm; > + int ret =3D 0; > > mm =3D get_task_mm(task); > if (mm) { > @@ -3276,6 +3277,16 @@ static int proc_pid_ksm_stat(struct seq_file *m, s= truct pid_namespace *ns, > seq_printf(m, "ksm_zero_pages %ld\n", mm_ksm_zero_pages(m= m)); > seq_printf(m, "ksm_merging_pages %lu\n", mm->ksm_merging_= pages); > seq_printf(m, "ksm_process_profit %ld\n", ksm_process_pro= fit(mm)); > + seq_printf(m, "ksm_merge_any: %s\n", > + test_bit(MMF_VM_MERGE_ANY, &mm->flags) ? = "yes" : "no"); > + ret =3D mmap_read_lock_killable(mm); > + if (ret) { > + mmput(mm); > + return ret; > + } > + seq_printf(m, "ksm_mergeable: %s\n", > + ksm_process_mergeable(mm) ? "yes" : "no")= ; > + mmap_read_unlock(mm); > mmput(mm); > } > > diff --git a/include/linux/ksm.h b/include/linux/ksm.h > index 6a53ac4885bb..d73095b5cd96 100644 > --- a/include/linux/ksm.h > +++ b/include/linux/ksm.h > @@ -93,6 +93,7 @@ void folio_migrate_ksm(struct folio *newfolio, struct f= olio *folio); > void collect_procs_ksm(const struct folio *folio, const struct page *pag= e, > struct list_head *to_kill, int force_early); > long ksm_process_profit(struct mm_struct *); > +bool ksm_process_mergeable(struct mm_struct *mm); > > #else /* !CONFIG_KSM */ > > diff --git a/mm/ksm.c b/mm/ksm.c > index 7ac59cde626c..be2eb1778225 100644 > --- a/mm/ksm.c > +++ b/mm/ksm.c > @@ -3263,6 +3263,25 @@ static void wait_while_offlining(void) > #endif /* CONFIG_MEMORY_HOTREMOVE */ > > #ifdef CONFIG_PROC_FS > +/* > + * The process is mergeable only if any VMA is currently > + * applicable to KSM. > + * > + * The mmap lock must be held in read mode. > + */ > +bool ksm_process_mergeable(struct mm_struct *mm) > +{ > + struct vm_area_struct *vma; > + > + mmap_assert_locked(mm); > + VMA_ITERATOR(vmi, mm, 0); > + for_each_vma(vmi, vma) > + if (vma->vm_flags & VM_MERGEABLE) > + return true; > + > + return false; > +} > + > long ksm_process_profit(struct mm_struct *mm) > { > return (long)(mm->ksm_merging_pages + mm_ksm_zero_pages(mm)) * PA= GE_SIZE - > -- > 2.15.2 >