From patchwork Fri Aug 12 10:11:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: CGEL X-Patchwork-Id: 12942101 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 97AA0C25B0F for ; Fri, 12 Aug 2022 10:11:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2A9FB8E0002; Fri, 12 Aug 2022 06:11:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 259D68E0001; Fri, 12 Aug 2022 06:11:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 121DB8E0002; Fri, 12 Aug 2022 06:11:59 -0400 (EDT) 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 065578E0001 for ; Fri, 12 Aug 2022 06:11:59 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id CA959141B08 for ; Fri, 12 Aug 2022 10:11:58 +0000 (UTC) X-FDA: 79790524716.27.4E267A5 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) by imf25.hostedemail.com (Postfix) with ESMTP id 578DBA0077 for ; Fri, 12 Aug 2022 10:11:58 +0000 (UTC) Received: by mail-pf1-f174.google.com with SMTP id f28so568792pfk.1 for ; Fri, 12 Aug 2022 03:11:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=V7yMXZa3MywkOFBvlYMPqSqmCaZ5JNGIAqlkaSLZ/kM=; b=GUsQrErKwYfApaFW7t1aycaDDOcT4RNUbr8JoqRV/JeKU6zJZtEoluxB2oppeJz+c+ uicrtsV0V2YYg4yrptx59NDO9hjcFwCQiWINbbZGDntxrXtgrUandYeo6JfRBNNiXqkO X+F5nDt6ITpzGdKmFJoiXZrl/hWAbj0LnZXL3QzrrrQwOS+wwzlGzn3bCbcHPl445y+U nk6kGyOxAT30qQy9rI9gbbQR79VBd0/FrHsxTCpwyW0NOfsh42UQiDnh6mRvNi2gHABd Klf3QM2nSSx8nIdpGoCKb1s+uqWvK8CgBYm/CnLRpaCRweC3Ap/D0sRqbw92h24VZ2TH Ol6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=V7yMXZa3MywkOFBvlYMPqSqmCaZ5JNGIAqlkaSLZ/kM=; b=3R2BaxE9bEM/UDB3L0G8hBVNy+tPIF7XLNTO8j1GqKQGxm43x9VO27p0LMU7zi9gIo +eMlJ3kbo0WB9+GWDJ0MpneH5O2s9TOvdlhhXByenlRMOSE7XvPHZGmS3lYt5LzGlMGO 4IN7tPN267PhHAhZX3MlNjoPJtexkp14HVM+AiXO3cf7K8c8NP+NlNRl4VVx8rrPrdzK RmszKD21Di3G67bwlrwyquqKPJ0A5/nHmLVRQ9BcKuKQas5qY6fmhm1Yv19jlqEyOmk+ pl9RwOoJGnGTltz4SH7Z5D3oDlwDrkTQ/Zx2jk70ubwJ31t6CBoUpu2QFqJzpNcLyq5w 1LGg== X-Gm-Message-State: ACgBeo0R3CQVQyaBKAwszTWrRaiRV//zeuoumL3La5sjxen9TtV0isrD qq0fphQvLCNgYgPKatC0XSo= X-Google-Smtp-Source: AA6agR7ruzvjPLVoqRS4ooTCRVR7Zek3i441oZSHEk/8OA3W9OWSqoZisu1kEC4y/u4cMpl7eVVPYw== X-Received: by 2002:a05:6a00:328e:b0:52d:44e0:297c with SMTP id ck14-20020a056a00328e00b0052d44e0297cmr2943039pfb.64.1660299117343; Fri, 12 Aug 2022 03:11:57 -0700 (PDT) Received: from localhost.localdomain ([193.203.214.57]) by smtp.gmail.com with ESMTPSA id gq20-20020a17090b105400b001f52fa1704csm16110031pjb.3.2022.08.12.03.11.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Aug 2022 03:11:57 -0700 (PDT) From: cgel.zte@gmail.com X-Google-Original-From: xu.xin16@zte.com.cn To: akpm@linux-foundation.org, willy@infradead.org Cc: hughd@google.com, izik.eidus@ravellosystems.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, xu.xin16@zte.com.cn, CGEL Subject: [PATCH v2 1/5] ksm: add a auto-run mode of ksm Date: Fri, 12 Aug 2022 10:11:52 +0000 Message-Id: <20220812101152.41479-1-xu.xin16@zte.com.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220812101102.41422-1-xu.xin16@zte.com.cn> References: <20220812101102.41422-1-xu.xin16@zte.com.cn> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660299118; 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:dkim-signature; bh=V7yMXZa3MywkOFBvlYMPqSqmCaZ5JNGIAqlkaSLZ/kM=; b=jca6d1uDVpgYtqs1f1tKLozJL8pJ4cApN0CCaa83Tu0Qw/efHEBleAZfJZW+gAHfJj1tV+ P3XsRpAC5llJK+HsBpCc9dDc+FCSt9zQq0rIzzPzenRTzBz8e/iwai33VX7wjgn7Qu6NkT MEP96ZmfGKoH1bDNz31XinOK0aIkZmc= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=GUsQrErK; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf25.hostedemail.com: domain of cgel.zte@gmail.com designates 209.85.210.174 as permitted sender) smtp.mailfrom=cgel.zte@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660299118; a=rsa-sha256; cv=none; b=ydCC7jPVFzuuAcDymBzwYcrGrR58ANcfjqVDDwetI2KhVpi70Atop+5bHbVlKxHoo4Qp9x MBqShiLZRmnYcEPOVx2pmF87Ee9Lgi8eWnq+JHoHYc87gYlCY4EGVmjL4w3Qlcmt9bLNmN HCsBp+9NsVV9+POVD4IABMt+6oo/zUo= X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 578DBA0077 X-Rspam-User: X-Stat-Signature: os9jzzyb7htg8rbbgicue4u3hq14odsw Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=GUsQrErK; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf25.hostedemail.com: domain of cgel.zte@gmail.com designates 209.85.210.174 as permitted sender) smtp.mailfrom=cgel.zte@gmail.com X-HE-Tag: 1660299118-895325 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: xu xin Add a new running state auto-mode to ksm. This is to pave the way for subsequent real optimization features. Use it by: echo 8 > /sys/kernel/mm/ksm/run Signed-off-by: xu xin Signed-off-by: CGEL --- mm/ksm.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 2f315c69fa2c..c80d908221a4 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -290,6 +290,7 @@ static int ksm_nr_node_ids = 1; #define KSM_RUN_MERGE 1 #define KSM_RUN_UNMERGE 2 #define KSM_RUN_OFFLINE 4 +#define KSM_RUN_AUTO 8 static unsigned long ksm_run = KSM_RUN_STOP; static void wait_while_offlining(void); @@ -2416,7 +2417,9 @@ static void ksm_do_scan(unsigned int scan_npages) static int ksmd_should_run(void) { - return (ksm_run & KSM_RUN_MERGE) && !list_empty(&ksm_mm_head.mm_list); + if (!list_empty(&ksm_mm_head.mm_list)) + return ksm_run & KSM_RUN_AUTO || ksm_run & KSM_RUN_MERGE; + return 0; } static int ksm_scan_thread(void *nothing) @@ -2916,7 +2919,7 @@ static ssize_t run_store(struct kobject *kobj, struct kobj_attribute *attr, err = kstrtouint(buf, 10, &flags); if (err) return -EINVAL; - if (flags > KSM_RUN_UNMERGE) + if (flags > KSM_RUN_UNMERGE && flags != KSM_RUN_AUTO) return -EINVAL; /* @@ -2942,7 +2945,7 @@ static ssize_t run_store(struct kobject *kobj, struct kobj_attribute *attr, } mutex_unlock(&ksm_thread_mutex); - if (flags & KSM_RUN_MERGE) + if (flags & KSM_RUN_MERGE || flags & KSM_RUN_AUTO) wake_up_interruptible(&ksm_thread_wait); return count; From patchwork Fri Aug 12 10:12:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: CGEL X-Patchwork-Id: 12942102 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 A8769C25B0F for ; Fri, 12 Aug 2022 10:12:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EDF0D8E0003; Fri, 12 Aug 2022 06:12:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E8EC28E0001; Fri, 12 Aug 2022 06:12:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D30378E0003; Fri, 12 Aug 2022 06:12:08 -0400 (EDT) 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 C656F8E0001 for ; Fri, 12 Aug 2022 06:12:08 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id AB15E1C5FF2 for ; Fri, 12 Aug 2022 10:12:08 +0000 (UTC) X-FDA: 79790525136.03.42AC649 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) by imf25.hostedemail.com (Postfix) with ESMTP id 49187A018C for ; Fri, 12 Aug 2022 10:12:08 +0000 (UTC) Received: by mail-pg1-f174.google.com with SMTP id r69so447186pgr.2 for ; Fri, 12 Aug 2022 03:12:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=3RiuHL8cuPFNRdOQLrbL2ARJQLP3txMsqxB/DqBpHYo=; b=PO+YfNP7KvjF2zdmrmg/gzcou8Nbp4XOJlJjIiqFLzBP2VVWJK4AujsgLDH3VUlo41 xuxdRdR1edLV2Oixgvzo/tOfW6ht3FHrXcrb0KPTEu9xVgySElv0RJzAtXuwC+DDOAIi 21Zl59qjzNZUvkVy/dFUGG1qkbMPxjfNe++YhnttjiOdl8WZf77H3BRopKPYKfc/x7UU 732zYyCXLdqk63zY3974mToWDa13wP/rKKl2X+hpiDCLqpHiboKmEotx26W8+146voUV 5zm1r+XfHr1NYw7FgeOVDG701wh4B+fOu1ybyI3kKcFgKOewsk4VTJR++CKRUcVy5yFc tHhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=3RiuHL8cuPFNRdOQLrbL2ARJQLP3txMsqxB/DqBpHYo=; b=u1N64dKTw4YfQJlrwJS65lccFkB92jydjHWg+Ggp1lJdPfpQxmVKJGWsJIZ3Rpdka6 csbFH2QUNp30LJm8ik32WrJn9ps9+cGtIkRigKdtygsBiGsTcWIDGVBFPvMjmR5KGBIs CkbHKSN4NLkldeE2/s72Wz1HRLr7dFIazcPZQ1VhfShu6sb32qw8sco3pCWl6zF6y0qg O8ED085nhVlPaska7giDDO/ht4jmV3XS4oPS0UxOJuMivmgRKc7bVOSAWRo2l9lFrCxo +OrCiDzufCxkzhybHNan5H2KJRETtIMu5P4mAZybNfROUE7Wk1WFSD+U1Sx0V8LW/Y9U +a/w== X-Gm-Message-State: ACgBeo0GZlTNQAR2d3QDmyTRvtnYmFv40NVEN+NB40nS7M8jUz2tU4uU oTqUdoBdCDsJ9HGkcnLNFZw= X-Google-Smtp-Source: AA6agR7Fd93QkM030LuiG9mHSEbbXsNDiHX6pJcZ8w61nSyMQdFIuvFz43JGwYRt9ZJeuAcXovAWyg== X-Received: by 2002:a05:6a00:17a8:b0:52e:6e3e:9ff with SMTP id s40-20020a056a0017a800b0052e6e3e09ffmr3217306pfg.42.1660299127293; Fri, 12 Aug 2022 03:12:07 -0700 (PDT) Received: from localhost.localdomain ([193.203.214.57]) by smtp.gmail.com with ESMTPSA id mp2-20020a17090b190200b001f319e9b9e5sm5134115pjb.16.2022.08.12.03.12.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Aug 2022 03:12:06 -0700 (PDT) From: cgel.zte@gmail.com X-Google-Original-From: xu.xin16@zte.com.cn To: akpm@linux-foundation.org, willy@infradead.org Cc: hughd@google.com, izik.eidus@ravellosystems.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, xu.xin16@zte.com.cn, CGEL Subject: [PATCH v2 2/5] ksm: implement scan-enhanced algorithm Date: Fri, 12 Aug 2022 10:12:02 +0000 Message-Id: <20220812101202.41533-1-xu.xin16@zte.com.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220812101102.41422-1-xu.xin16@zte.com.cn> References: <20220812101102.41422-1-xu.xin16@zte.com.cn> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660299128; a=rsa-sha256; cv=none; b=09jsS+twxsv4DRruBJbZgqGpgxCjIBUYra5/VE+zif6zgXk/pLWND9pbxCsPAvuwnxOyic l7dka2LUmt6xf6pomz1rshhM6Df0XSyXrEhF4sMiSxkIJyjD/Q2ogH1+DwcfkdvUZ1QqHB p8hr0CKGuB0OMLuQ6OnSlO+7gId9IKw= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=PO+YfNP7; spf=pass (imf25.hostedemail.com: domain of cgel.zte@gmail.com designates 209.85.215.174 as permitted sender) smtp.mailfrom=cgel.zte@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660299128; 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:dkim-signature; bh=3RiuHL8cuPFNRdOQLrbL2ARJQLP3txMsqxB/DqBpHYo=; b=FEE7mtqxWHpkNku5setUROd0AT7y2ehjBG4tuQYHHX+6HPUBHBllNJQKnCgRK4kXLtwdel xFTK1jtIO7ih6lHLQg5iqZH+MvLnprU8rahJiFWXXon9c5pHuBiyrlLjVURHhD6NDSEaVU RUsJE+SicX2cx4r12jfGmWW8aJdp5DA= X-Stat-Signature: m7cins3opwdj1rphkko6t58c4cbp6pu6 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 49187A018C Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=PO+YfNP7; spf=pass (imf25.hostedemail.com: domain of cgel.zte@gmail.com designates 209.85.215.174 as permitted sender) smtp.mailfrom=cgel.zte@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspam-User: X-HE-Tag: 1660299128-647574 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: xu xin Implement the scan-enhanced algorithm of auto mode. In this algorithm, after every time of scanning, if new ksm pages are obtained, it will double pages_to_scan for the next scanning until the general multiplying factor is not less than max_scanning_factor. If no new ksm pages are obtained, then reset pages_to_scan to the default value. We add the sysfs klob of max_scanning_factor to limit scanning factor's excessive growth. Signed-off-by: CGEL Signed-off-by: xu xin --- mm/ksm.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 98 insertions(+), 2 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index c80d908221a4..f416f168a6da 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -131,6 +131,10 @@ struct mm_slot { * @address: the next address inside that to be scanned * @rmap_list: link to the next rmap to be scanned in the rmap_list * @seqnr: count of completed full scans (needed when removing unstable node) + * @new_ksmpages: count of the new merged KSM pages in the current scanning + * of mm_lists (cleared after every turn of ksm_do_scan() ends) + * @prev_ksmpages: the record of the new merged KSM pages in the last turn of + * scanning by ksm_do_scan(). * * There is only the one ksm_scan instance of this cursor structure. */ @@ -139,6 +143,8 @@ struct ksm_scan { unsigned long address; struct rmap_item **rmap_list; unsigned long seqnr; + unsigned long new_ksmpages; + unsigned long prev_ksmpages; }; /** @@ -277,6 +283,19 @@ static unsigned int zero_checksum __read_mostly; /* Whether to merge empty (zeroed) pages with actual zero pages */ static bool ksm_use_zero_pages __read_mostly; +/* + * Work in auto-mode. + * The multiplicative factor of pages_to_scan. + * Real pages to scan equals to the product of scanning_factor + * and pages_to_scan + */ +#define INIT_SCANNING_FACTOR 1 +static unsigned int scanning_factor = INIT_SCANNING_FACTOR; + +/* The upper limit of scanning_factor */ +#define DEFAULT_MAX_SCANNING_FACTOR 16 +static unsigned int max_scanning_factor = DEFAULT_MAX_SCANNING_FACTOR; + #ifdef CONFIG_NUMA /* Zeroed when merging across nodes is not allowed */ static unsigned int ksm_merge_across_nodes = 1; @@ -2031,6 +2050,8 @@ static void stable_tree_append(struct rmap_item *rmap_item, rmap_item->address |= STABLE_FLAG; hlist_add_head(&rmap_item->hlist, &stable_node->hlist); + ksm_scan.new_ksmpages++; + if (rmap_item->hlist.next) ksm_pages_sharing++; else @@ -2422,6 +2443,41 @@ static int ksmd_should_run(void) return 0; } +/* + * Work in auto mode, the scan-enhanced algorithm. + * current_factor: the current scanning_factor. + * return: the scanning_factor caculated by scan-enhanced algorithm. + */ +static unsigned int scan_enhanced_algorithm(unsigned int current_factor) +{ + unsigned int next_factor; + unsigned int max, min; + + /* + * The calculation is divied into three cases as follows: + * + * Case 1: when new_ksmpages > prev_ksmpages * 1/2, get the + * next factor by double the current factor. + * Case 2: when 0 < new_ksmpages < prev_ksmpages * 1/2, keep + * the factor unchanged. + * Case 3: when new_ksmpages equals 0, then get the next + * factor by halfing the current factor. + */ + max = READ_ONCE(max_scanning_factor); + min = INIT_SCANNING_FACTOR; + if (ksm_scan.new_ksmpages * 2 > ksm_scan.prev_ksmpages) { + next_factor = current_factor << 1; /* Doubling */ + if (next_factor > max) + next_factor = max; + } else if (ksm_scan.new_ksmpages == 0) { + next_factor = current_factor >> 1; /* Halfing */ + next_factor = next_factor < min ? min : next_factor; + } else + next_factor = current_factor; + + return next_factor; +} + static int ksm_scan_thread(void *nothing) { unsigned int sleep_ms; @@ -2432,8 +2488,19 @@ static int ksm_scan_thread(void *nothing) while (!kthread_should_stop()) { mutex_lock(&ksm_thread_mutex); wait_while_offlining(); - if (ksmd_should_run()) - ksm_do_scan(ksm_thread_pages_to_scan); + if (ksmd_should_run()) { + if (ksm_run & KSM_RUN_AUTO) { + ksm_do_scan(ksm_thread_pages_to_scan * scanning_factor); + + scanning_factor = scan_enhanced_algorithm(scanning_factor); + /* + * Reset ksm_scan.new_ksmpages after + * updating scanning_factor by scan_enhanced_algorithm. + */ + ksm_scan.new_ksmpages = 0; + } else + ksm_do_scan(ksm_thread_pages_to_scan); + } mutex_unlock(&ksm_thread_mutex); try_to_freeze(); @@ -2904,6 +2971,34 @@ static ssize_t pages_to_scan_store(struct kobject *kobj, } KSM_ATTR(pages_to_scan); +static ssize_t max_scanning_factor_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%u\n", max_scanning_factor); +} + +static ssize_t max_scanning_factor_store(struct kobject *kobj, + struct kobj_attribute *attr, + const char *buf, size_t count) +{ + unsigned int value, max; + int err; + + err = kstrtouint(buf, 10, &value); + if (err) + return -EINVAL; + + max = totalram_pages() / ksm_thread_pages_to_scan; + + if (value < 1 && value > max) + return -EINVAL; + + max_scanning_factor = value; + + return count; +} +KSM_ATTR(max_scanning_factor); + static ssize_t run_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { @@ -3161,6 +3256,7 @@ KSM_ATTR_RO(full_scans); static struct attribute *ksm_attrs[] = { &sleep_millisecs_attr.attr, &pages_to_scan_attr.attr, + &max_scanning_factor_attr.attr, &run_attr.attr, &pages_shared_attr.attr, &pages_sharing_attr.attr, From patchwork Fri Aug 12 10:12:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: CGEL X-Patchwork-Id: 12942103 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 A728FC25B0E for ; Fri, 12 Aug 2022 10:12:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0124F8E0005; Fri, 12 Aug 2022 06:12:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F03F98E0001; Fri, 12 Aug 2022 06:12:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DCC1E8E0005; Fri, 12 Aug 2022 06:12:18 -0400 (EDT) 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 D0C408E0001 for ; Fri, 12 Aug 2022 06:12:18 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 9E937A1BD2 for ; Fri, 12 Aug 2022 10:12:18 +0000 (UTC) X-FDA: 79790525556.28.F26194C Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) by imf02.hostedemail.com (Postfix) with ESMTP id 3D93880192 for ; Fri, 12 Aug 2022 10:12:18 +0000 (UTC) Received: by mail-pf1-f180.google.com with SMTP id p125so564589pfp.2 for ; Fri, 12 Aug 2022 03:12:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=Zow/Qe/P2NEQDMvwyeUd/99vAJcANYj6amOMdI5Aoqg=; b=Mfg+p9sl9EKee4oeLXvUpcn98hONemFjE5MlOv9aqEVL98PWkiyFxu2JleR+qqciZf EAEc/jMEw1qYl9MZ4fuy81YcBMj39ao2+ky+HEGNAVDEmtevdQCgY9dUEi1473EOqVtO Ld0dhDUYIHPwcKxxqrND4fdBLqPHyTK0mXZd0EKWVmD7UxKYZULNdG13O+ZunaHZJZaQ +vbaIM/okQZMyB0YLly11rIXt+EAU675o9MvODyp94OA5XRbiYXvnj0fFhV5srQ9f/hk uEA1qpyRGt0etlJeYq/e0nz4yKpZnFsjUo3qQhmJguu7qoIAyGn2/eTZAAoBN2FWgCLO mRuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=Zow/Qe/P2NEQDMvwyeUd/99vAJcANYj6amOMdI5Aoqg=; b=tb6QUP7iSFz1+2HyW+2TqQEN492r+0rQc9HBAdhzpB3A51N+27B/ChV3dF2RjDyIcn XeV0joSG0qOm5XjiL2MhjfcFtFKE1hRduxAD5nMGZPIOICHw4wAXcc4U8me1vYyQOVln XauIqJ7LkXDHigazcnxIEYoDLMR8zr1Bb/SKkmXJggpm0au0BcoxT08AGJOYw9j3qYoz 9iY/LeoHj7WMj0keoncUIGe3fYyRvZJRIg6kA46IjnOv0Fg+q3L9Ax4x9DmqWveXrBah f2zX5ps9lu/HErPvmOtSNq9/wanTJvFzQ4mLT/RPXXi5BfIJSy4pd4g4gXGVHr64uRvr 9Vxw== X-Gm-Message-State: ACgBeo2eQST7xoitg/cHcfpK0ZcLlAsgLLobHgQqks5egg7i4sIhS2y/ EWa+Zs+5Kx/mgn+ZF2T8olI= X-Google-Smtp-Source: AA6agR5skhqPaePy5qtgLQ5EWyQuWhL4KUe6VyWBMJ1w9oMX6T2nxssa3zNoHs9qCSvhfdGWBnddZw== X-Received: by 2002:a65:4605:0:b0:41c:3d73:9385 with SMTP id v5-20020a654605000000b0041c3d739385mr2481969pgq.168.1660299137278; Fri, 12 Aug 2022 03:12:17 -0700 (PDT) Received: from localhost.localdomain ([193.203.214.57]) by smtp.gmail.com with ESMTPSA id 196-20020a6218cd000000b0052b94e757ecsm1203269pfy.213.2022.08.12.03.12.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Aug 2022 03:12:17 -0700 (PDT) From: cgel.zte@gmail.com X-Google-Original-From: xu.xin16@zte.com.cn To: akpm@linux-foundation.org, willy@infradead.org Cc: hughd@google.com, izik.eidus@ravellosystems.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, xu.xin16@zte.com.cn, CGEL Subject: [PATCH v2 3/5] ksm: let ksmd auto-work with memory threshold Date: Fri, 12 Aug 2022 10:12:12 +0000 Message-Id: <20220812101212.41587-1-xu.xin16@zte.com.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220812101102.41422-1-xu.xin16@zte.com.cn> References: <20220812101102.41422-1-xu.xin16@zte.com.cn> MIME-Version: 1.0 ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Mfg+p9sl; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf02.hostedemail.com: domain of cgel.zte@gmail.com designates 209.85.210.180 as permitted sender) smtp.mailfrom=cgel.zte@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660299138; a=rsa-sha256; cv=none; b=Z1BRZGf1WA6v9ugcN/WhpFLFENTWQt5Ls3cIm229fZz5eMOJuWGHw/jvZeOWkywcazgVIQ MuBkk0UHqVqNNyH1z5SF0wKHazpnRwPHvHlcgezq0pe4CvMEpO1M7nSJXsYuxWoQuXyBMT vH05SDF3O16BA7fAAEuoDGw8lb+SvGk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660299138; 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:dkim-signature; bh=Zow/Qe/P2NEQDMvwyeUd/99vAJcANYj6amOMdI5Aoqg=; b=TUSybLKj8LHnGpHSfh/v3uO62+SpAw1pFSz9Lnh4i0gg4CwLo2wWwLVLbtZmA1xX2+2beN LMxfwu4uFJYz0WTQKFGjt3jCXqkmJBEbQWRCRohI2C3rNiMCGS9SLtmTENGZ3ucUM1g1eQ BeXab0U5Ig2KeuMQmBF8x4mvvHLv/eg= X-Rspam-User: Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Mfg+p9sl; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf02.hostedemail.com: domain of cgel.zte@gmail.com designates 209.85.210.180 as permitted sender) smtp.mailfrom=cgel.zte@gmail.com X-Stat-Signature: 78ofcmrro7mhiu1k95updk4ixbinigf7 X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 3D93880192 X-HE-Tag: 1660299138-907854 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: xu xin When memory is sufficient, merging pages to save memory is not very much needed, and it also inceases delays of COW for user application. So set a memory threshold, when free memory is lower than the threshold, ksmd will be triggered to compare and merge pages. And to avoid ping-pong effect due to the threshold, ksmd needs to try to merge pages until free memory is larger than (threshold + total_memory * 1/16). Before free memory is lower than the threshold, ksmd will still scan pages at a very low speed, to calculate their checksum but not to compare and merge pages. | | ----(Threshold + total_memory/16)-------- | | ------Threshold------ | | | |_____ksmd try to merge pages__| We also add a new sysfs klob auto_threshold_percent for user to be able to tune. Signed-off-by: xu xin Signed-off-by: CGEL --- mm/ksm.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 2 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index f416f168a6da..c5fd4f520f4a 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -296,6 +296,17 @@ static unsigned int scanning_factor = INIT_SCANNING_FACTOR; #define DEFAULT_MAX_SCANNING_FACTOR 16 static unsigned int max_scanning_factor = DEFAULT_MAX_SCANNING_FACTOR; +/* + * Work in auto mode. + * Value: 0~100. Default 20 means "20%". When free memory is lower + * than this total memory * ksm_auto_threshold/100, auto_triggered + * will be set true. + */ +unsigned int ksm_auto_threshold = 20; + +/* Work in auto-mode. Whether trigger ksmd to compare and merge pages */ +static bool auto_triggered; + #ifdef CONFIG_NUMA /* Zeroed when merging across nodes is not allowed */ static unsigned int ksm_merge_across_nodes = 1; @@ -2431,11 +2442,61 @@ static void ksm_do_scan(unsigned int scan_npages) rmap_item = scan_get_next_rmap_item(&page); if (!rmap_item) return; - cmp_and_merge_page(page, rmap_item); + if (ksm_run & KSM_RUN_AUTO && !auto_triggered) { + /* + * This should happens only when ksm_run is KSM_RUN_AUTO + * and free memory threshold still not reached. + * The reason to calculate it's checksum is to reduce the + * waiting time the rmap_item is added to unstable tree. + */ + rmap_item->oldchecksum = calc_checksum(page); + } else + cmp_and_merge_page(page, rmap_item); + put_page(page); } } +#define RIGHT_SHIFT_FOUR_BIT 4 +/* Work in auto mode, should reset auto_triggered ? */ +static bool should_stop_ksmd_to_merge(void) +{ + unsigned long total_ram_pages, free_pages; + unsigned int threshold; + + total_ram_pages = totalram_pages(); + free_pages = global_zone_page_state(NR_FREE_PAGES); + threshold = READ_ONCE(ksm_auto_threshold); + + return free_pages > (total_ram_pages * threshold / 100) + + (total_ram_pages >> RIGHT_SHIFT_FOUR_BIT); +} + +/* Work in auto mode, should ksmd start to merge ? */ +static bool should_trigger_ksmd_to_merge(void) +{ + unsigned long total_ram_pages, free_pages; + unsigned int threshold; + + total_ram_pages = totalram_pages(); + free_pages = global_zone_page_state(NR_FREE_PAGES); + threshold = READ_ONCE(ksm_auto_threshold); + + return free_pages < (total_ram_pages * threshold / 100); +} + +static inline void trigger_ksmd_to_merge(void) +{ + if (!auto_triggered) + auto_triggered = true; +} + +static inline void stop_ksmd_to_merge(void) +{ + if (auto_triggered) + auto_triggered = false; +} + static int ksmd_should_run(void) { if (!list_empty(&ksm_mm_head.mm_list)) @@ -2478,6 +2539,8 @@ static unsigned int scan_enhanced_algorithm(unsigned int current_factor) return next_factor; } +#define SLOW_SCAN_PAGES 5 /* Used when ksmd is not triggered to merge*/ + static int ksm_scan_thread(void *nothing) { unsigned int sleep_ms; @@ -2490,7 +2553,10 @@ static int ksm_scan_thread(void *nothing) wait_while_offlining(); if (ksmd_should_run()) { if (ksm_run & KSM_RUN_AUTO) { - ksm_do_scan(ksm_thread_pages_to_scan * scanning_factor); + if (!auto_triggered) + ksm_do_scan(SLOW_SCAN_PAGES); + else + ksm_do_scan(ksm_thread_pages_to_scan * scanning_factor); scanning_factor = scan_enhanced_algorithm(scanning_factor); /* @@ -2498,6 +2564,11 @@ static int ksm_scan_thread(void *nothing) * updating scanning_factor by scan_enhanced_algorithm. */ ksm_scan.new_ksmpages = 0; + + if (should_trigger_ksmd_to_merge()) + trigger_ksmd_to_merge(); + else if (should_stop_ksmd_to_merge()) + stop_ksmd_to_merge(); } else ksm_do_scan(ksm_thread_pages_to_scan); } @@ -3047,6 +3118,32 @@ static ssize_t run_store(struct kobject *kobj, struct kobj_attribute *attr, } KSM_ATTR(run); +static ssize_t auto_threshold_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%u\n", ksm_auto_threshold); +} + +static ssize_t auto_threshold_store(struct kobject *kobj, + struct kobj_attribute *attr, + const char *buf, size_t count) +{ + unsigned int value; + int err; + + err = kstrtouint(buf, 10, &value); + if (err) + return -EINVAL; + + if (value > 100) + return -EINVAL; + + ksm_auto_threshold = value; + + return count; +} +KSM_ATTR(auto_threshold); + #ifdef CONFIG_NUMA static ssize_t merge_across_nodes_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) @@ -3258,6 +3355,7 @@ static struct attribute *ksm_attrs[] = { &pages_to_scan_attr.attr, &max_scanning_factor_attr.attr, &run_attr.attr, + &auto_threshold_attr.attr, &pages_shared_attr.attr, &pages_sharing_attr.attr, &pages_unshared_attr.attr, @@ -3289,6 +3387,7 @@ static int __init ksm_init(void) zero_checksum = calc_checksum(ZERO_PAGE(0)); /* Default to false for backwards compatibility */ ksm_use_zero_pages = false; + auto_triggered = false; err = ksm_slab_init(); if (err) From patchwork Fri Aug 12 10:12:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: CGEL X-Patchwork-Id: 12942104 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 2804AC00140 for ; Fri, 12 Aug 2022 10:12:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C1FC98E0006; Fri, 12 Aug 2022 06:12:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BCF978E0001; Fri, 12 Aug 2022 06:12:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A97B28E0006; Fri, 12 Aug 2022 06:12:29 -0400 (EDT) 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 9D00D8E0001 for ; Fri, 12 Aug 2022 06:12:29 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 80C78C1B88 for ; Fri, 12 Aug 2022 10:12:29 +0000 (UTC) X-FDA: 79790526018.16.CFB469E Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) by imf01.hostedemail.com (Postfix) with ESMTP id 1654840198 for ; Fri, 12 Aug 2022 10:12:28 +0000 (UTC) Received: by mail-pj1-f51.google.com with SMTP id c19-20020a17090ae11300b001f2f94ed5c6so7007364pjz.1 for ; Fri, 12 Aug 2022 03:12:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=1LS1w+4gYbBOJ3fgeWTMSBRNuWyP/hHFUM4MmFgbaww=; b=msf5OTF6rIik/luYMAcgB2uHDsWClBvHa1lbsogrHOmhX/n4FGKKX6qj6huLVdGGzM ICrQklW4Llnhk7Cyj44kXIqUJIXhyyrrKsvx+cNnGwdwXG4FZCKzwCXaRKw0Jdwu6/Be DQlu8saNmWgFOWG8h6+e0upSdMKZ2xgRkAzxSSe7F3jMKdYlqo83I6pww/xD/wwIfTax huQ3rvNDB/dzqZX0XhmNlADww25qcPliC0EW9QNCAw19rOE9d3j8dalYcd90puCvT3vO xWKxHZut21wxhoy+khsI7tDM3wgJfD0wFJJL3sO6aNdYvxnqrgZlxEpMnH5tXCJ5QdeJ CViA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=1LS1w+4gYbBOJ3fgeWTMSBRNuWyP/hHFUM4MmFgbaww=; b=WzmSGHI8LEA94UumJVl2Gw8E3CFA4FuewOBKrK8oXH7qVwnwm/LK72qeWSnzZYk4JP aeFjdZt1ZqnOuwh7JKvHuEXn5RWeAuSGFc80aYTehnycwEv4oerhEmR6mPLXtRo0oI0u kDvUwWd+aYJgbE1jIUWMDRTuAKKJ+nEB2h+OjeEcItGFrgGV8eg2pToGsqW2jmkrvtbq VqV20mc3//aKaM0rTRWPEGw3pZa4pz29LYZmWnyA3vgpv+ku0pTf86sx3SDrVOXdyOwL a75GQeRUhNWFnpW8PXqZvvJiQ9xkZGvDR8cmSDnMOhH047ns7z286xP9+EQP+1CS52gL aGew== X-Gm-Message-State: ACgBeo0hJXKcd9sr1svTXZGxcnNAiL5JBqjVnPoc18fnOeHtzNiHTRx3 2E6tv4+EsP+7cnnMaPs2lw4= X-Google-Smtp-Source: AA6agR7DyayXx1NhvFW/woPbk9r5UkcpGW14C9iIDwV3bfot22BChYdXQe8pWawSmvcYPnZeOr7zDQ== X-Received: by 2002:a17:90b:3b49:b0:1f4:df09:d671 with SMTP id ot9-20020a17090b3b4900b001f4df09d671mr3408405pjb.129.1660299148170; Fri, 12 Aug 2022 03:12:28 -0700 (PDT) Received: from localhost.localdomain ([193.203.214.57]) by smtp.gmail.com with ESMTPSA id mg21-20020a17090b371500b001f30b100e04sm4973885pjb.15.2022.08.12.03.12.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Aug 2022 03:12:27 -0700 (PDT) From: cgel.zte@gmail.com X-Google-Original-From: xu.xin16@zte.com.cn To: akpm@linux-foundation.org, willy@infradead.org Cc: hughd@google.com, izik.eidus@ravellosystems.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, xu.xin16@zte.com.cn, CGEL Subject: [PATCH v2 4/5] ksm: show ksmd status for auto mode Date: Fri, 12 Aug 2022 10:12:23 +0000 Message-Id: <20220812101223.41641-1-xu.xin16@zte.com.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220812101102.41422-1-xu.xin16@zte.com.cn> References: <20220812101102.41422-1-xu.xin16@zte.com.cn> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660299149; a=rsa-sha256; cv=none; b=hsPQ5iTzxcCLSQwTlDeZ04ncN3jOBQJQk0fwc7dHO5LBSMWWX4H/XDYz/c4ert8L4ciRNx 8qWC1X16qiXha4av2s5bRnDRHedD0zT4LTjdUNIaL/n2zmVm+VS0znjTdhIvLLCiaPisUv d+VLWUEBiXuK4FMxHjk7t2uFTydCSAE= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=msf5OTF6; spf=pass (imf01.hostedemail.com: domain of cgel.zte@gmail.com designates 209.85.216.51 as permitted sender) smtp.mailfrom=cgel.zte@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660299149; 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:dkim-signature; bh=1LS1w+4gYbBOJ3fgeWTMSBRNuWyP/hHFUM4MmFgbaww=; b=qas1GB1jr3RTi01AcnR1pMNvPNpG60F3RxYL0LmmTVwIFe+2Qqz14GQwW3uRm048jb86Ui rgz0eBAmOgsnD8Yq5hlWlFhxzNdHRMqFOM9LIhb2Yv8sssqK3I1AYNTuXcLVyrtqn6q6uL uybOQKyHBjle4Fa/Lf48CG2PkUCApIc= X-Stat-Signature: tmahyec67z9je63c86t6qcgqemmpdeyf X-Rspamd-Queue-Id: 1654840198 X-Rspam-User: X-Rspamd-Server: rspam03 Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=msf5OTF6; spf=pass (imf01.hostedemail.com: domain of cgel.zte@gmail.com designates 209.85.216.51 as permitted sender) smtp.mailfrom=cgel.zte@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-HE-Tag: 1660299148-207534 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: xu xin Add a sysfs interface of ksmd_status to show some details related with auto-mode. Signed-off-by: xu xin Signed-off-by: CGEL --- mm/ksm.c | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/mm/ksm.c b/mm/ksm.c index c5fd4f520f4a..478bcf26bfcd 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -307,6 +307,9 @@ unsigned int ksm_auto_threshold = 20; /* Work in auto-mode. Whether trigger ksmd to compare and merge pages */ static bool auto_triggered; +/* Count of times that ksmd is triggered due to low free memory */ +static unsigned long triggered_times; + #ifdef CONFIG_NUMA /* Zeroed when merging across nodes is not allowed */ static unsigned int ksm_merge_across_nodes = 1; @@ -2487,8 +2490,10 @@ static bool should_trigger_ksmd_to_merge(void) static inline void trigger_ksmd_to_merge(void) { - if (!auto_triggered) + if (!auto_triggered) { + triggered_times++; auto_triggered = true; + } } static inline void stop_ksmd_to_merge(void) @@ -3118,6 +3123,40 @@ static ssize_t run_store(struct kobject *kobj, struct kobj_attribute *attr, } KSM_ATTR(run); +static ssize_t ksmd_status_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + int len = 0; + unsigned int mergeable_mms = 0; + struct list_head *pos; + + list_for_each(pos, &ksm_mm_head.mm_list) + mergeable_mms++; + + if (ksm_run & KSM_RUN_AUTO) { + len += sysfs_emit_at(buf, len, "mode: auto\n"); + len += sysfs_emit_at(buf, len, "auto_triggered: %d\n", + auto_triggered); + len += sysfs_emit_at(buf, len, "mergeable_mms: %u\n", + mergeable_mms); + len += sysfs_emit_at(buf, len, "scanning_factor: %u\n", + scanning_factor); + len += sysfs_emit_at(buf, len, "triggered_times: %lu\n", + triggered_times); + } else if (ksm_run & KSM_RUN_MERGE) { + len += sysfs_emit_at(buf, len, "mode: on\n"); + len += sysfs_emit_at(buf, len, "mergeable_mms: %u\n", + mergeable_mms); + } else if (ksm_run & KSM_RUN_UNMERGE) + len += sysfs_emit_at(buf, len, "mode: unmerge\n"); + else + len += sysfs_emit_at(buf, len, "mode: off\n"); + + + return len; +} +KSM_ATTR_RO(ksmd_status); + static ssize_t auto_threshold_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { @@ -3355,6 +3394,7 @@ static struct attribute *ksm_attrs[] = { &pages_to_scan_attr.attr, &max_scanning_factor_attr.attr, &run_attr.attr, + &ksmd_status_attr.attr, &auto_threshold_attr.attr, &pages_shared_attr.attr, &pages_sharing_attr.attr, From patchwork Fri Aug 12 10:12:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: CGEL X-Patchwork-Id: 12942105 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 9ACFCC00140 for ; Fri, 12 Aug 2022 10:12:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0FCC08E0002; Fri, 12 Aug 2022 06:12:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0ACB58E0001; Fri, 12 Aug 2022 06:12:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EB7628E0002; Fri, 12 Aug 2022 06:12:40 -0400 (EDT) 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 DF9588E0001 for ; Fri, 12 Aug 2022 06:12:40 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C174C121B96 for ; Fri, 12 Aug 2022 10:12:40 +0000 (UTC) X-FDA: 79790526480.08.BCAA173 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf31.hostedemail.com (Postfix) with ESMTP id 0F68620181 for ; Fri, 12 Aug 2022 10:12:38 +0000 (UTC) Received: by mail-pl1-f182.google.com with SMTP id 13so357758plo.12 for ; Fri, 12 Aug 2022 03:12:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=IpSj0Sq8UAMHTbMdNu1xXveZ87+P76UE2ASjQ2JMJlc=; b=Nzmqou1m1In9MEM2g6XrvC75pcAb7kJ7eyXkF5G3pNQ1KGFCkkyhZfm1FFOMzJL8Sb wEkhoXPBtVUH5kcH+b8mNITQ7WOfpj16nsHRj9Dv9HdQL05a9784KOy2dcDMhvU4wnNC PIyuwsvQq6NmuOO6Vv905QkLU48asuhCGtImT8342gCyX1tdxXfBGpoB5biLMsDGsDpE V309aXZ2t2fOPMlLSQIOqpHN0iOlzMy0nc+1GtU5K8KiAxv02k6asHbWnRKQ3wR8l3n1 fcYDgMSqIhiTp5uihoTIi1u2SFhpVeIdtRfFP4z7ZxXmEuBaSCfF+/eOQy4DNTtbAz/u hqmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=IpSj0Sq8UAMHTbMdNu1xXveZ87+P76UE2ASjQ2JMJlc=; b=BAsOg3rkyGnu7I8GZGooPYY00lcnaot/UW5VlbM3rnbFpDF/VEyU2gMl2LJ5IbGjjj nw8zjxgXnuRlifixBQuX5YP97QT8+0sY3ljhjHYflIYp0m+8b4K4NbaZfXCs7C+hQLm9 b6CkdzPMY4aaQuq+6avy3xCI+BVDakSp9QrctyNZuD5+VCjjkiM6zwnh6CNjjY/PyN0Y MkqcjPst4L257qMi4AXYNsa+8KRFjc4LmbKRJLdxHLDUhkqEPOvdxE+jql9cLCMiIlTm OBqoXdZsECC561xBnsSscOnzgLlKPg466WFO/Nz1xIopoSP3myEl7o2LAZSCFa3Vwmb0 aCXA== X-Gm-Message-State: ACgBeo1C5Lxa/wqW6LspgyBEv8n6STfRNDQUQgjeLy1dSG1kONrtqjde lOlW5Gl74kWJtcIj1kgT8EM= X-Google-Smtp-Source: AA6agR5vit+nBjvOQAz7r9uQj9RxdtIsJCGv8WzecvBLL3ZBrv4ebR8a0On4oWsQTEsB6a1yW2o+sw== X-Received: by 2002:a17:903:1104:b0:16c:2f71:7803 with SMTP id n4-20020a170903110400b0016c2f717803mr3415537plh.101.1660299158168; Fri, 12 Aug 2022 03:12:38 -0700 (PDT) Received: from localhost.localdomain ([193.203.214.57]) by smtp.gmail.com with ESMTPSA id v127-20020a626185000000b0052aaff953aesm1204839pfb.115.2022.08.12.03.12.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Aug 2022 03:12:37 -0700 (PDT) From: cgel.zte@gmail.com X-Google-Original-From: xu.xin16@zte.com.cn To: akpm@linux-foundation.org, willy@infradead.org Cc: hughd@google.com, izik.eidus@ravellosystems.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, xu.xin16@zte.com.cn, CGEL Subject: [PATCH v2 5/5] ksm: add tests of ksm auto mode Date: Fri, 12 Aug 2022 10:12:33 +0000 Message-Id: <20220812101233.41695-1-xu.xin16@zte.com.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220812101102.41422-1-xu.xin16@zte.com.cn> References: <20220812101102.41422-1-xu.xin16@zte.com.cn> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660299159; 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:dkim-signature; bh=IpSj0Sq8UAMHTbMdNu1xXveZ87+P76UE2ASjQ2JMJlc=; b=Q0Tnc5M01iWuQd5RS8O08iMQDckDN48IwXZ49Sp+Ry+By3C0lonpT0/F1TYferXOUhNyPX uZ8DMJRSI0Dy9Nq0PJs9EHL8uCfUJni2tsusmH4cIFeAwA5mNYlqAZbVW3X8vPIfUkZCVf /uj7KNtdoMtG8D3pi6f+UkA4XfoLTsM= ARC-Authentication-Results: i=1; imf31.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Nzmqou1m; spf=pass (imf31.hostedemail.com: domain of cgel.zte@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=cgel.zte@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660299159; a=rsa-sha256; cv=none; b=cmsHaXh/mpsI5XM/dJlNNoPo8WKrOi4+Wu92at4sfcR2pZDeGSzimHPz5XPp16YEIB5Wz9 sterq8szWxUgcZ8tdtiQ/3okneCSkzE2JjSLtblqW3fUh6T3fXiEFQwgCfqoTFHAuWyCbJ SwNOgIlppcj4Pll3tO74ltq6dFvslXM= X-Stat-Signature: po7enpz8tbnfenzfa5hp6587iswwu714 X-Rspamd-Queue-Id: 0F68620181 Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Nzmqou1m; spf=pass (imf31.hostedemail.com: domain of cgel.zte@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=cgel.zte@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspam-User: X-Rspamd-Server: rspam09 X-HE-Tag: 1660299158-772908 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: xu xin Add tests to verify the effectiveness and cpu consumption of auto mode of KSM. The tests requires no other MERGEABLE-madvised vm areas in system than the areas our testing process allocs. Signed-off-by: xu xin Signed-off-by: CGEL --- tools/testing/selftests/vm/.gitignore | 1 + tools/testing/selftests/vm/Makefile | 1 + tools/testing/selftests/vm/test-ksm-auto.c | 273 +++++++++++++++++++++ 3 files changed, 275 insertions(+) create mode 100644 tools/testing/selftests/vm/test-ksm-auto.c diff --git a/tools/testing/selftests/vm/.gitignore b/tools/testing/selftests/vm/.gitignore index 31e5eea2a9b9..1cd8816c055d 100644 --- a/tools/testing/selftests/vm/.gitignore +++ b/tools/testing/selftests/vm/.gitignore @@ -34,3 +34,4 @@ local_config.* soft-dirty split_huge_page_test ksm_tests +test-ksm-auto diff --git a/tools/testing/selftests/vm/Makefile b/tools/testing/selftests/vm/Makefile index d9fa6a9ea584..002d9482c37f 100644 --- a/tools/testing/selftests/vm/Makefile +++ b/tools/testing/selftests/vm/Makefile @@ -54,6 +54,7 @@ TEST_GEN_FILES += userfaultfd TEST_GEN_PROGS += soft-dirty TEST_GEN_PROGS += split_huge_page_test TEST_GEN_FILES += ksm_tests +TEST_GEN_FILES += test-ksm-auto ifeq ($(MACHINE),x86_64) CAN_BUILD_I386 := $(shell ./../x86/check_cc.sh "$(CC)" ../x86/trivial_32bit_program.c -m32) diff --git a/tools/testing/selftests/vm/test-ksm-auto.c b/tools/testing/selftests/vm/test-ksm-auto.c new file mode 100644 index 000000000000..0d71593e008e --- /dev/null +++ b/tools/testing/selftests/vm/test-ksm-auto.c @@ -0,0 +1,273 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define KSM_CLEAR_MODE "2\n" +#define KSM_NORMAL_MODE "1\n" +#define KSM_AUTO_MODE "8\n" + +#define PAGESIZE (4*1024) +/* Don't change the value, it will afffect the result */ +#define TOTAL_MADVISE_SIZE (300*1024*1024) + +char *ksm_run_file = "/sys/kernel/mm/ksm/run"; +char *ksm_auto_threshold_file = "/sys/kernel/mm/ksm/auto_threshold"; +char *ksm_pages_volatile_file = "/sys/kernel/mm/ksm/pages_volatile"; +char *ksm_pages_sharing_file = "/sys/kernel/mm/ksm/pages_sharing"; + +#define SHAPE_FULL 1 +#define SHAPE_SPARSE 2 +/* They are related to the shape of memory */ +int final_pages[3] = {0, 76500, 42}; + +static char *mmap_and_madvise(long long size, int advise) +{ + char *ptr; + int err; + + err = 0; + + ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); + if (!ptr) + return NULL; + + err = madvise(ptr, size, advise); + if (err) { + perror("Madvise failed\n"); + free(ptr); + return NULL; + } + + return ptr; +} + +void make_samepage_ares(char *ptr, int size, int shape_type) +{ + int i, j; + char rnd_num; + + switch (shape_type) { + case SHAPE_FULL: + for (i = 0; i < (size / PAGESIZE); i++) + memset(ptr + (i * PAGESIZE), 0x1, PAGESIZE); + break; + case SHAPE_SPARSE: + /* Make pages different */ + j = 0; + for (i = 1; i < (size / PAGESIZE); i++) { + ptr[i * PAGESIZE + (j%PAGESIZE)] = j%127 + 1; + j++; + } + for (i = 0; i < (size / PAGESIZE); i += 1800) + memset(ptr + (i * PAGESIZE), -1, PAGESIZE); + } + + return; +} + +int read_file(char *file, char *buffer, int buf_len) +{ + FILE *fp; + size_t result; + long lSize; + + fp = fopen(file, "r"); + if (!fp) + return -1; + + fseek(fp, 0, SEEK_END); + lSize = ftell(fp); + rewind(fp); + + memset(buffer, 0, buf_len); + result = fread(buffer, 1, buf_len, fp); + if (result == 0) + return -1; + + fclose(fp); + + return 0; +} + +int write_file(char *file, const char *buffer, int len) +{ + FILE *fp; + size_t result; + + fp = fopen(file, "w+"); + if (!fp) + return -1; + + result = fwrite(buffer, len, 1, fp); + if (result == 0) + return -1; + + fclose(fp); + + return 0; +} + +static inline void get_orig_info(int *run, int *auto_threshold) +{ + char buffer[50]; + + /* Read the original state of ksm/run */ + if (read_file(ksm_run_file, buffer, sizeof(buffer))) { + printf("read file %s failed\n", ksm_run_file); + exit(1); + } + *run = atoi(buffer); + + if (read_file(ksm_auto_threshold_file, buffer, sizeof(buffer))) { + printf("read file: %s failed\n", ksm_auto_threshold_file); + exit(1); + } + *auto_threshold = atoi(buffer); +} + +static inline void restore_orig_state(int run, int auto_threshold) +{ + char buffer[50]; + + /* restore the original state */ + memset(buffer, 0, sizeof(buffer)); + snprintf(buffer, sizeof(buffer) - 1, "%d\n", run); + if (write_file(ksm_run_file, buffer, sizeof(buffer))) { + printf("write file %s failed\n", ksm_run_file); + exit(1); + } + + memset(buffer, 0, sizeof(buffer)); + snprintf(buffer, sizeof(buffer) - 1, "%d\n", auto_threshold); + if (write_file(ksm_auto_threshold_file, buffer, sizeof(buffer))) { + printf("write file %s failed\n", ksm_run_file); + exit(1); + } +} + +void set_ksmd_run_mode(char *mode) +{ + if (write_file(ksm_run_file, mode, 2)) { + printf("Failed: write 1 to %s\n", ksm_auto_threshold_file); + exit(1); + } +} + +static inline void wait_ksmpages_converged(int final_pages) +{ + int pages_sharing; + char buffer[50]; + + for (;;) { + if (read_file(ksm_pages_sharing_file, buffer, sizeof(buffer))) { + printf("read file %s failed\n", ksm_pages_sharing_file); + exit(1); + } + + pages_sharing = atoi(buffer); + if (pages_sharing >= final_pages) + break; + } +} + +void print_shape(int shape_type) +{ + switch (shape_type) { + case SHAPE_FULL: + printf("Now the shape of memory area is full-samepages:\n"); + printf("[xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx]\n\n"); + break; + case SHAPE_SPARSE: + printf("Now the shape of memory area is sparse-samepages:\n"); + printf("[xx] [xx] [xx] \n\n"); + break; + } +} + +void print_ksmd_cpu_comsuption(void) +{ + system("(ps x| grep \"ksmd\" | grep -v grep | awk \'{print $1}\' |" + " xargs -i cat /proc/{}/stat) | awk \'{print \"ksm current " + "cpu total slice: \" $14+$15+$16+$17}\'"); +} + +void test_ksmd_performance(char *madvise_area, int shape_type) +{ + struct timeval tv_start, tv_end; + + make_samepage_ares(madvise_area, TOTAL_MADVISE_SIZE, shape_type); + print_shape(shape_type); + + /********* Start to time ksmd's normal-run mode **********/ + printf("Start to test normal-run ksmd...\n"); + + print_ksmd_cpu_comsuption(); + + set_ksmd_run_mode(KSM_CLEAR_MODE); + set_ksmd_run_mode(KSM_NORMAL_MODE); + + gettimeofday(&tv_start, NULL); + + wait_ksmpages_converged(final_pages[shape_type]); + + gettimeofday(&tv_end, NULL); + printf("ksm normal-run's merging time: %lf seconds\n", + ((tv_end.tv_sec * 1000000 + tv_end.tv_usec) - + (tv_start.tv_sec * 1000000 + tv_start.tv_usec))/1000000.0); + + /******* Start to time ksmd's auto-run mode **********/ + print_ksmd_cpu_comsuption(); + + printf("Start to test auto-run ksmd...\n"); + set_ksmd_run_mode(KSM_CLEAR_MODE); + set_ksmd_run_mode(KSM_AUTO_MODE); + if (write_file(ksm_auto_threshold_file, "99\n", 2)) + printf("Failed: write 1 to %s\n", ksm_auto_threshold_file); + + gettimeofday(&tv_start, NULL); + + wait_ksmpages_converged(shape_type); + + gettimeofday(&tv_end, NULL); + printf("ksm auto-run's merging time: %lf seconds\n", + ((tv_end.tv_sec * 1000000 + tv_end.tv_usec) - + (tv_start.tv_sec * 1000000 + tv_start.tv_usec))/1000000.0); + + print_ksmd_cpu_comsuption(); +} + +int main(int argc, char **argv) +{ + char *madvise_area; + int orig_run, orig_auto_threshold; + + /* Get the original state of ksm */ + get_orig_info(&orig_run, &orig_auto_threshold); + printf("Now we mmap 300MB anouymous memory for testing.\n" + "There are two type of TEST which have different shape of\n" + "samepage areas.\n" + "Note: the test requires no other MERGEABLE-madvised vm areas\n" + "in system than the areas our testing process allocs.\n"); + madvise_area = mmap_and_madvise(TOTAL_MADVISE_SIZE, MADV_MERGEABLE); + if (!madvise_area) { + printf("madvise failed\n"); + exit(1); + } + + printf("\n****************** TEST 1 ******************\n"); + test_ksmd_performance(madvise_area, SHAPE_FULL); + printf("\n****************** TEST 2 ******************\n"); + test_ksmd_performance(madvise_area, SHAPE_SPARSE); + + /* Restore the original state */ + restore_orig_state(orig_run, orig_auto_threshold); + + return 0; +}