From patchwork Wed Mar 23 03:17:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: CGEL X-Patchwork-Id: 12789400 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 C8ADAC433EF for ; Wed, 23 Mar 2022 03:18:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A6C0B6B0072; Tue, 22 Mar 2022 23:18:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A1B976B0073; Tue, 22 Mar 2022 23:18:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8BC316B0074; Tue, 22 Mar 2022 23:18:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0225.hostedemail.com [216.40.44.225]) by kanga.kvack.org (Postfix) with ESMTP id 7D2CB6B0072 for ; Tue, 22 Mar 2022 23:18:45 -0400 (EDT) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 340ABA32C8 for ; Wed, 23 Mar 2022 03:18:45 +0000 (UTC) X-FDA: 79274193810.28.B5FC8AE Received: from mail-qv1-f43.google.com (mail-qv1-f43.google.com [209.85.219.43]) by imf20.hostedemail.com (Postfix) with ESMTP id B3DCC1C0011 for ; Wed, 23 Mar 2022 03:18:44 +0000 (UTC) Received: by mail-qv1-f43.google.com with SMTP id kk12so324392qvb.13 for ; Tue, 22 Mar 2022 20:18:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=HuZMniKUiLdmMq0S/smNDGx4nZr+J2VHjPsTBck+11s=; b=Tse20CMLZPb2xi5LDlL7G8zi9e/ZhtX9/urs8GbZmvyP4uSFlC4CZWjoI4aA/TJyvz qghQYdMJQ+pyEUGbMWo9FBTfHMapfLu7tHIX4XirSbAIpnoBipCMJ6EZ03PmxIMGhjW1 okzbtJ8uJ4EO1lPxEQfPOwGJAmG6jP26zu52g0/HxhUJMotkPKJ/sSRDXG721rlw1qzD QEsaNlShMRACfhXdIbQfo4xyjU9cz+lcVORvKr2GnNQ1VmYSylY5FU2JtkhwI2EV7Yqf qAaK9AiJ9R8/45dVaAXa9fu6S8fKx3RMOF7IPXHIED5AFMxcPAxaDrpl4iXWR43LCKrV dnzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=HuZMniKUiLdmMq0S/smNDGx4nZr+J2VHjPsTBck+11s=; b=nBYUYS9YjtondI8c8qomAfUyRSBCZGBxur9rDSPzemYset4QsnaUCZimSh8C6vGaf3 KosWpBOIXu0oJPAgCrEHm3hS+8YK4fbIYVEn1uq1r1jUkzZWjWk72MnrOZcD67ZQ92vH kzODxL+8R6DEtd36MsjOXqmLKV0/5M1NpMSQ4aMdGRRyt05D55TVh6d8qnmDpW32jZNl HlMn6a9oIq3gg3+unsI9TEuQZEi/MoiE9jV5yPs8mjUSZEj/eLWYSrjKRg2nLyaUf3zN IKqaSGoOlypFr6qdxfRdvKjCV0KAguuNsATiVoN54jbV4bYT13UQqwxoSwammHeBN/Cp 5PDQ== X-Gm-Message-State: AOAM533tspDH+XeyHlqzgli3DDWI3uCOWkLuwJqTIXfESubqBBSdVa2s bCEpjxy9A1OFmOVUujskwuo= X-Google-Smtp-Source: ABdhPJzhksxHW4/rsLVdg+jn6M3FiJF1MkqN5pSAr6Z+9q+YhbyelyH/4MqEL6Bw9r3GhLufC8dKrg== X-Received: by 2002:ad4:5ccb:0:b0:441:1abe:1b8a with SMTP id iu11-20020ad45ccb000000b004411abe1b8amr11696377qvb.7.1648005523965; Tue, 22 Mar 2022 20:18:43 -0700 (PDT) Received: from localhost.localdomain ([193.203.214.57]) by smtp.gmail.com with ESMTPSA id w13-20020a05622a134d00b002e2280215f6sm2108254qtk.54.2022.03.22.20.18.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Mar 2022 20:18:43 -0700 (PDT) From: cgel.zte@gmail.com X-Google-Original-From: yang.yang29@zte.com.cn To: akpm@linux-foundation.org, david@redhat.com Cc: yang.yang29@zte.com.cn, dave.hansen@linux.intel.com, yang.shi@linux.alibaba.com, ran.xiaokai@zte.com.cn, saravanand@fb.com, minchan@kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, xu xin Subject: [PATCH] mm/vmstat: add events for ksm cow Date: Wed, 23 Mar 2022 03:17:31 +0000 Message-Id: <20220323031730.2342930-1-yang.yang29@zte.com.cn> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: u6afd7qmzdso9o9uax5psqs8nxani6qo Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Tse20CML; spf=pass (imf20.hostedemail.com: domain of cgel.zte@gmail.com designates 209.85.219.43 as permitted sender) smtp.mailfrom=cgel.zte@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: B3DCC1C0011 X-HE-Tag: 1648005524-574462 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: From: Yang Yang Users may use ksm by calling madvise(, , MADV_MERGEABLE) when they want to save memory, it's a tradeoff by suffering delay on ksm cow. Users can get to know how much memory ksm saved by reading /sys/kernel/mm/ksm/pages_sharing, but they don't know what's the costs of ksm cow, and this is important of some delay sensitive tasks. So add ksm cow events to help users evaluate whether or how to use ksm. Signed-off-by: Yang Yang Reviewed-by: xu xin Reviewed-by: Ran Xiaokai Reported-by: kernel test robot --- include/linux/vm_event_item.h | 2 ++ mm/memory.c | 18 +++++++++++++++--- mm/vmstat.c | 2 ++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/include/linux/vm_event_item.h b/include/linux/vm_event_item.h index 16a0a4fd000b..6f32be04212f 100644 --- a/include/linux/vm_event_item.h +++ b/include/linux/vm_event_item.h @@ -131,6 +131,8 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT, SWAP_RA_HIT, #ifdef CONFIG_KSM KSM_SWPIN_COPY, + KSM_COW_SUCCESS, + KSM_COW_FAIL, #endif #endif #ifdef CONFIG_X86 diff --git a/mm/memory.c b/mm/memory.c index 4111f97c91a0..06c92f322cdb 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3257,6 +3257,8 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf) __releases(vmf->ptl) { struct vm_area_struct *vma = vmf->vma; + vm_fault_t ret = 0; + bool ksm = 0; if (userfaultfd_pte_wp(vma, *vmf->pte)) { pte_unmap_unlock(vmf->pte, vmf->ptl); @@ -3294,6 +3296,7 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf) */ if (PageAnon(vmf->page)) { struct page *page = vmf->page; + ksm = PageKsm(page); /* * We have to verify under page lock: these early checks are @@ -3302,7 +3305,7 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf) * * PageKsm() doesn't necessarily raise the page refcount. */ - if (PageKsm(page) || page_count(page) > 3) + if (ksm || page_count(page) > 3) goto copy; if (!PageLRU(page)) /* @@ -3316,7 +3319,7 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf) goto copy; if (PageSwapCache(page)) try_to_free_swap(page); - if (PageKsm(page) || page_count(page) != 1) { + if (ksm || page_count(page) != 1) { unlock_page(page); goto copy; } @@ -3339,7 +3342,16 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf) get_page(vmf->page); pte_unmap_unlock(vmf->pte, vmf->ptl); - return wp_page_copy(vmf); + ret = wp_page_copy(vmf); + + if (ksm) { + if (unlikely(ret & VM_FAULT_ERROR)) + count_vm_event(KSM_COW_FAIL); + else + count_vm_event(KSM_COW_SUCCESS); + } + + return ret; } static void unmap_mapping_range_vma(struct vm_area_struct *vma, diff --git a/mm/vmstat.c b/mm/vmstat.c index d5cc8d739fac..a2c29a5206ec 100644 --- a/mm/vmstat.c +++ b/mm/vmstat.c @@ -1390,6 +1390,8 @@ const char * const vmstat_text[] = { "swap_ra_hit", #ifdef CONFIG_KSM "ksm_swpin_copy", + "ksm_cow_success", + "ksm_cow_fail", #endif #endif #ifdef CONFIG_X86