From patchwork Tue Dec 5 18:09:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13480549 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 CBF2AC10DCE for ; Tue, 5 Dec 2023 18:10:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 696426B0081; Tue, 5 Dec 2023 13:10:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 645F76B0083; Tue, 5 Dec 2023 13:10:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 50CFD6B0085; Tue, 5 Dec 2023 13:10:09 -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 4288B6B0081 for ; Tue, 5 Dec 2023 13:10:09 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 0A766160224 for ; Tue, 5 Dec 2023 18:10:09 +0000 (UTC) X-FDA: 81533553738.26.0B59F50 Received: from 66-220-144-179.mail-mxout.facebook.com (66-220-144-179.mail-mxout.facebook.com [66.220.144.179]) by imf27.hostedemail.com (Postfix) with ESMTP id 60A2D40025 for ; Tue, 5 Dec 2023 18:10:07 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf27.hostedemail.com: 66.220.144.179 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=1701799807; 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=UTjrGbU4I/RqcrucjyV8XvLhSKkL76lxJ1wr3bvi3b4=; b=eAt7FfYqZtB2gFiLsEU8jHW/ljBOeuhQUdy7nczLlTsZ58/jroJPYwidKrHKJ6IdqFvWKw 59tbnq4XoG5uWxZCVlUSc/sRhwu49t6uTTUQsPYdWy40/9DyMrkfG+gfZMCxa+oaGc+5Fx Da6x9cpLAq22q5Ow4Tv5FZ9Fe44d8p8= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf27.hostedemail.com: 66.220.144.179 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=1701799807; a=rsa-sha256; cv=none; b=uXYrkQFkFj12We7jAo5AbH+aPd9igBs5/3NA8UjlSHW2vA1Bmrr18ksDzA5uYbkGn8HAS2 u7P8aXESO/1v36oJp2Q5M3JOkY20Rtb7QTEFCOM+TARii1yBvjtGfQCM5LPutlS+DqvPVU xqBZcCQ1ICokLeYqGqbDfLtyz6e/J1A= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id AAA361067A2EE; Tue, 5 Dec 2023 10:09:54 -0800 (PST) From: Stefan Roesch To: kernel-team@fb.com Cc: shr@devkernel.io, david@redhat.com, oliver.sang@intel.com, linux-mm@kvack.org, ltp@lists.linux.it, pvorel@suse.cz, liwang@redhat.com Subject: [PATCH v4 1/2] mem: disable KSM smart scan for ksm tests Date: Tue, 5 Dec 2023 10:09:51 -0800 Message-Id: <20231205180952.3843711-2-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231205180952.3843711-1-shr@devkernel.io> References: <20231205180952.3843711-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Queue-Id: 60A2D40025 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: o43596ze7nua7c1qds798o8zada9wqdc X-HE-Tag: 1701799807-552508 X-HE-Meta: U2FsdGVkX1+yv4EZB07v99QEZU45e2eJokGBYMHlrS/8Y0LsAGNZl392ex5jjG5Gp+buRxX+/THRvyejr5zZAwuflw6VoKfdF7LeLH7VGx1vkDQ8bDfO9G0KyHqc8zWZC0n+J3OYgXcisW3qpCWiBeRoxlOxCtsPaX9gnP5eKrVUMDLv6EWS/nXgSr5gQMPNXeEUHYxSWE+rQo1pWlGO1jlS/Rq6RBZubHnz2RUC/Z1XqD76Mh6CuHEN/qmfca5C1ilo/esUJj8v0A/1uWyG1uYPspGK1DIjBeZSDQIGJN+jzUo6i6PB+xG7MvdG0vaPgs/MzKcwBjRVhjN9PTIg3FPemdeVDdWFseavXdfvwd/zlj9xS/AJzQEJZC32KhpiJwCLXHaaAEODyUw7tXZAuadGiukYVk04yvaNLDD0vLP6KPUCxbQRkZr+yLr3rtVi/cPexOpUZnTtAjcqLIVRavLraSsJGzz2Xr8oH78Cx8InDwYP6zp9xAdg1s5qMck6PFTomieDvzIIGaB4UNdFUnP6XwcQ+zJHBDxsakN7PpV/vKuHMZxjQVD4scIUE7h1kLgGv2q5RdYiOcmgfcBXBzkq7wcQx2rNqkMhri7IntahrIzSmViG/lNT0E82V1JVVn2p8AZIc6uHaXF8SUNtl4hr1o76eTN8a25Q7bDq6DXxd8D6pYSMoAeTlmxQurlV9gtyCwJxJn4DCh7sLdgMShGtJ42J3tXAa9MsEuzJPQrr1LhKweta8lziEZ/QckBJBdsi303OZkFCvsVC9l1WoMLzdF7INO5d8rIQs/Jz/FnpTph1Zhdduqt6+v6RWwd4Zt4P8OwJYz/hwcljreMNvOPrTwgDvolDV1oocgMlSStYvwiV46C5qSucUvPLsTiIaQOVOpVx4eg9B1I4T1whdas4u3QRlnzd7tNibk77+RmV1EhhXEL7Wey/4V3lNxPCjn5SUwrmVUA0VCPkt6X yU2/ziFa s5mg32gfR1eRp+cAeX8fyCiZld8lVY3xVlbHXSOLJSNMicbiVTqxaj6zoElIpMtm/0lQMzH58Gx3Es/oDl0Qj4wtG2XkQ2GXH2p3t 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 disables the "smart scan" KSM feature to make sure that the volatile count remains at 0. Signed-off-by: Stefan Roesch Reviewed-by: Petr Vorel Reported-by: kernel test robot Closes: https://lore.kernel.org/oe-lkp/202311161132.13d8ce5a-oliver.sang@intel.com --- testcases/kernel/mem/ksm/ksm01.c | 2 ++ testcases/kernel/mem/ksm/ksm02.c | 2 ++ testcases/kernel/mem/ksm/ksm03.c | 2 ++ testcases/kernel/mem/ksm/ksm04.c | 2 ++ testcases/kernel/mem/ksm/ksm05.c | 2 ++ testcases/kernel/mem/ksm/ksm06.c | 2 ++ 6 files changed, 12 insertions(+) diff --git a/testcases/kernel/mem/ksm/ksm01.c b/testcases/kernel/mem/ksm/ksm01.c index bcd095865..e2d3d9e00 100644 --- a/testcases/kernel/mem/ksm/ksm01.c +++ b/testcases/kernel/mem/ksm/ksm01.c @@ -86,6 +86,8 @@ static struct tst_test test = { TST_SR_SKIP_MISSING | TST_SR_TCONF_RO}, {"/sys/kernel/mm/ksm/merge_across_nodes", "1", TST_SR_SKIP_MISSING | TST_SR_TCONF_RO}, + {"/sys/kernel/mm/ksm/smart_scan", "0", + TST_SR_SKIP_MISSING | TST_SR_TBROK_RO}, {} }, .needs_kconfigs = (const char *const[]){ diff --git a/testcases/kernel/mem/ksm/ksm02.c b/testcases/kernel/mem/ksm/ksm02.c index bce639dce..3707de95d 100644 --- a/testcases/kernel/mem/ksm/ksm02.c +++ b/testcases/kernel/mem/ksm/ksm02.c @@ -107,6 +107,8 @@ static struct tst_test test = { TST_SR_SKIP_MISSING | TST_SR_TCONF_RO}, {"/sys/kernel/mm/ksm/merge_across_nodes", "1", TST_SR_SKIP_MISSING | TST_SR_TCONF_RO}, + {"/sys/kernel/mm/ksm/smart_scan", "0", + TST_SR_SKIP_MISSING | TST_SR_TBROK_RO}, {} }, .needs_kconfigs = (const char *const[]){ diff --git a/testcases/kernel/mem/ksm/ksm03.c b/testcases/kernel/mem/ksm/ksm03.c index 4a733269f..cff74700d 100644 --- a/testcases/kernel/mem/ksm/ksm03.c +++ b/testcases/kernel/mem/ksm/ksm03.c @@ -89,6 +89,8 @@ static struct tst_test test = { TST_SR_SKIP_MISSING | TST_SR_TCONF_RO}, {"/sys/kernel/mm/ksm/merge_across_nodes", "1", TST_SR_SKIP_MISSING | TST_SR_TCONF_RO}, + {"/sys/kernel/mm/ksm/smart_scan", "0", + TST_SR_SKIP_MISSING | TST_SR_TBROK_RO}, {} }, .needs_kconfigs = (const char *const[]){ diff --git a/testcases/kernel/mem/ksm/ksm04.c b/testcases/kernel/mem/ksm/ksm04.c index 4f1f2f721..9935e32d7 100644 --- a/testcases/kernel/mem/ksm/ksm04.c +++ b/testcases/kernel/mem/ksm/ksm04.c @@ -109,6 +109,8 @@ static struct tst_test test = { TST_SR_SKIP_MISSING | TST_SR_TCONF_RO}, {"/sys/kernel/mm/ksm/merge_across_nodes", "1", TST_SR_SKIP_MISSING | TST_SR_TCONF_RO}, + {"/sys/kernel/mm/ksm/smart_scan", "0", + TST_SR_SKIP_MISSING | TST_SR_TBROK_RO}, {} }, .needs_kconfigs = (const char *const[]){ diff --git a/testcases/kernel/mem/ksm/ksm05.c b/testcases/kernel/mem/ksm/ksm05.c index 1f58c8325..61e740843 100644 --- a/testcases/kernel/mem/ksm/ksm05.c +++ b/testcases/kernel/mem/ksm/ksm05.c @@ -89,6 +89,8 @@ static struct tst_test test = { .test_all = test_ksm, .save_restore = (const struct tst_path_val[]) { {"/sys/kernel/mm/ksm/run", "1", TST_SR_TBROK}, + {"/sys/kernel/mm/ksm/smart_scan", "0", + TST_SR_SKIP_MISSING | TST_SR_TBROK_RO}, {} }, .needs_kconfigs = (const char *const[]){ diff --git a/testcases/kernel/mem/ksm/ksm06.c b/testcases/kernel/mem/ksm/ksm06.c index 0b159e5c7..80fdf1e47 100644 --- a/testcases/kernel/mem/ksm/ksm06.c +++ b/testcases/kernel/mem/ksm/ksm06.c @@ -142,6 +142,8 @@ static struct tst_test test = { {"/sys/kernel/mm/ksm/run", NULL, TST_SR_TBROK}, {"/sys/kernel/mm/ksm/sleep_millisecs", NULL, TST_SR_TBROK}, {"/sys/kernel/mm/ksm/merge_across_nodes", NULL, TST_SR_TCONF}, + {"/sys/kernel/mm/ksm/smart_scan", "0", + TST_SR_SKIP_MISSING | TST_SR_TBROK_RO}, {} }, .needs_kconfigs = (const char *const[]){ From patchwork Tue Dec 5 18:09:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13480550 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 C8209C4167B for ; Tue, 5 Dec 2023 18:10:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4C11A6B0083; Tue, 5 Dec 2023 13:10:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 471D16B0085; Tue, 5 Dec 2023 13:10:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 33A5B6B0087; Tue, 5 Dec 2023 13:10:10 -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 2092E6B0083 for ; Tue, 5 Dec 2023 13:10:10 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id ECF611A0200 for ; Tue, 5 Dec 2023 18:10:09 +0000 (UTC) X-FDA: 81533553738.14.7A5D39D Received: from 66-220-144-179.mail-mxout.facebook.com (66-220-144-179.mail-mxout.facebook.com [66.220.144.179]) by imf09.hostedemail.com (Postfix) with ESMTP id 3699914003A for ; Tue, 5 Dec 2023 18:10:06 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf09.hostedemail.com: 66.220.144.179 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=1701799807; 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=ZPMvPCSI+1BociLpOgej5RQ4jlxExez50Sdby6RDTnc=; b=4VHg3S3NLgbTD7zZpZOjr4sFmTkFnE6ImPERBCdGNKlM5Hdv01tgUgUZcDZD8h/T+53JP9 Q08DH5w6M/wv3pGxsx39tTj8BrTedXQXABHbhGhVpemmW0YdeenhED0e9VQmH9L5jZ9oZw 1gTC21NcBzt0//ylQ2FmHb7T+gU5GT4= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf09.hostedemail.com: 66.220.144.179 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=1701799807; a=rsa-sha256; cv=none; b=eJBZletrkZaFLczyMe/WPs7yjPhIUxPee1duY1Muv2t+2IgoAbQH6v7794mVAi+WJxyGYg UJC6ZJlmAE3KwxgMm53FxFP7L4Mh2bf2hUM22LfeoFxeVJwZzZzotQXOqr9+8I2Zt1TcQd X7iPEBCpp1/yfdgDoHpX8DmLZCz0Ozo= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id BAE501067A2F2; Tue, 5 Dec 2023 10:09:54 -0800 (PST) From: Stefan Roesch To: kernel-team@fb.com Cc: shr@devkernel.io, david@redhat.com, oliver.sang@intel.com, linux-mm@kvack.org, ltp@lists.linux.it, pvorel@suse.cz, liwang@redhat.com Subject: [PATCH v4 2/2] add ksm test for smart-scan feature Date: Tue, 5 Dec 2023 10:09:52 -0800 Message-Id: <20231205180952.3843711-3-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231205180952.3843711-1-shr@devkernel.io> References: <20231205180952.3843711-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: whtjhdqd9gkg33kyjsuhgn4jdgqqzyme X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 3699914003A X-HE-Tag: 1701799806-901866 X-HE-Meta: U2FsdGVkX1+lh60h7T1FL1FJUN2fIaj4/PlVfFeQON86XWY5/KzOgZk2dWQOtlNLcgBnGpJ8nW0GL23kJoh4R/mKhUZkQQoor+bJQkvRizMtK2StLyBRrUQnvYOCslwsYNVS6YL7Pg12a7jy1tXByv3o1xeg0AtKhWCosaMpVcfkigcjkkXBtHmZDJbDY++bpFQgi2icSPQT1PR6WGVHKQk1aFHyTXD4zqlIqvX4i/0oVhfAVDg7hu3XNkweRTJkMGXALzeYDvsXM8wE6W+vjAGnochZPJplXIlsRTfE2mPTmHJ0P8mbInv8JxQwq46/jX50xoZ0G08HtB4eXE3EHERKDaYKNy788PJUC7/fb5r/GnPYthOnuAPVk8zHqwZXzN1N23tuhZYCTvREAtELbDeF6QRcKiBbeZ90g3PZUKiraC/0gT1DBtw8VTTPy7szO/SttNBHIvBTlljQF7IMw4+H1r/n3HPawgPh6VuwzhEQ3m3Lf8HaIlDPTw/JzkXw0qFWkr6aWF5YyWGNd714iQ59LP/2FVB4Mko90ozt9CKawu9X5BZ0gs2hOCEdz+2VwPWS7Jb1tin0AozKBTSxEEKYwcBzLxRQU0/4JH6pEeC0ipFShL2iRxvipRWRNQflpF83byuqfR2HYLj6tVF99XrMAhAOSy5X5lgYXXDIPtiNRhAsDdwkIAKUtUYUo+s9uLqccY5Tbo5uoQsLMj1LXr6AGU0eLrsqH2Jmo2KCVh6ALjRKBY/HqTbySSB2RFMJce9sORaBDFHyaYt0ko6LVQiRKloe4mtY7KAmT1O504XHBrG/2Ga9QsDWN5R0V78Cn0SUrdSHOTZdvv309j71LhQjwG6FSJrLm2UoFl4Y5pHKGSs3KJebwWgicNgX+UsIQ9cm6AudKpEI6b0N9pGM7RUerSAHnEVoCcVQU/jRQL6o5fZF0YnK8slEg7fA0gPP6KJdbsr3IpAU49qLb4F o3rSXxx3 p3RU/nj17fDQr4lw3Ip/UROsiSg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, 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 adds a new ksm (kernel samepage merging) test to evaluate the new smart scan feature. It allocates a page and fills it with 'a' characters. It captures the pages_skipped counter, waits for a few iterations and captures the pages_skipped counter again. The expectation is that over 50% of the page scans are skipped (There is only one page that has KSM enabled and it gets scanned during each iteration and it cannot be de-duplicated). Signed-off-by: Stefan Roesch --- runtest/mm | 1 + testcases/kernel/mem/.gitignore | 1 + testcases/kernel/mem/ksm/ksm07.c | 113 +++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 testcases/kernel/mem/ksm/ksm07.c diff --git a/runtest/mm b/runtest/mm index f288fed36..d859b331c 100644 --- a/runtest/mm +++ b/runtest/mm @@ -70,6 +70,7 @@ ksm05 ksm05 -I 10 ksm06 ksm06 ksm06_1 ksm06 -n 10 ksm06_2 ksm06 -n 8000 +ksm07 ksm07 cpuset01 cpuset01 diff --git a/testcases/kernel/mem/.gitignore b/testcases/kernel/mem/.gitignore index 7258489ed..c96fe8bfc 100644 --- a/testcases/kernel/mem/.gitignore +++ b/testcases/kernel/mem/.gitignore @@ -53,6 +53,7 @@ /ksm/ksm04 /ksm/ksm05 /ksm/ksm06 +/ksm/ksm07 /mem/mem02 /mmapstress/mmap-corruption01 /mmapstress/mmapstress01 diff --git a/testcases/kernel/mem/ksm/ksm07.c b/testcases/kernel/mem/ksm/ksm07.c new file mode 100644 index 000000000..1ae5e9cd8 --- /dev/null +++ b/testcases/kernel/mem/ksm/ksm07.c @@ -0,0 +1,113 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2010-2023 Red Hat, Inc. + */ +/*\ + * [Description] + * + * Kernel Samepage Merging (KSM) for smart scan feature + * + * Test allocates a page and fills it with 'a' characters. It captures the + * pages_skipped counter, waits for a few iterations and captures the + * pages_skipped counter again. The expectation is that over 50% of the page + * scans are skipped. (There is only one page that has KSM enabled and it gets + * scanned during each iteration and it cannot be de-duplicated.) + * + * Smart scan feature was added in kernel v6.7. + * + * [Prerequisites] + * + * ksm and ksmtuned daemons need to be disabled. Otherwise, it could + * distrub the testing as they also change some ksm tunables depends + * on current workloads. + */ +#include +#include "mem.h" + +/* This test allocates one page, fills the page with a's, captures the + * full_scan and pages_skipped counters. Then it makes sure at least 3 + * full scans have been performed and measures the above counters again. + * The expectation is that at least 50% of the pages are skipped. + * + * To wait for at least 3 scans it uses the wait_ksmd_full_scan() function. In + * reality, it will be a lot more scans as the wait_ksmd_full_scan() function + * sleeps for one second. + */ +static void verify_ksm(void) +{ + int full_scans_begin; + int full_scans_end; + int pages_skipped_begin; + int pages_skipped_end; + int diff_pages; + int diff_scans; + unsigned long page_size; + char *memory; + + /* Apply for the space for memory. */ + page_size = sysconf(_SC_PAGE_SIZE); + memory = SAFE_MALLOC(page_size); + memory = SAFE_MMAP(NULL, page_size, PROT_READ|PROT_WRITE, + MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); +#ifdef HAVE_DECL_MADV_MERGEABLE + if (madvise(memory, page_size, MADV_MERGEABLE) == -1) + tst_brk(TBROK|TERRNO, "madvise"); +#endif + memset(memory, 'a', page_size); + + tst_res(TINFO, "KSM merging"); + + if (access(PATH_KSM "max_page_sharing", F_OK) == 0) + SAFE_FILE_PRINTF(PATH_KSM "run", "2"); + + /* Set defalut ksm scan values. */ + SAFE_FILE_PRINTF(PATH_KSM "run", "1"); + SAFE_FILE_PRINTF(PATH_KSM "pages_to_scan", "%ld", 100l); + SAFE_FILE_PRINTF(PATH_KSM "sleep_millisecs", "0"); + + /* Measure pages skipped aka "smart scan". */ + SAFE_FILE_SCANF(PATH_KSM "full_scans", "%d", &full_scans_begin); + SAFE_FILE_SCANF(PATH_KSM "pages_skipped", "%d", &pages_skipped_begin); + wait_ksmd_full_scan(); + + tst_res(TINFO, "stop KSM"); + SAFE_FILE_PRINTF(PATH_KSM "run", "0"); + + SAFE_FILE_SCANF(PATH_KSM "full_scans", "%d", &full_scans_end); + SAFE_FILE_SCANF(PATH_KSM "pages_skipped", "%d", &pages_skipped_end); + diff_pages = pages_skipped_end - pages_skipped_begin; + diff_scans = full_scans_end - full_scans_begin; + + if (diff_pages < diff_scans * 50 / 100) { + tst_res(TINFO, "number of pages %d", diff_pages); + tst_res(TINFO, "number of scans %d", diff_scans); + tst_res(TFAIL, "not enough pages have been skipped by smart_scan."); + } else { + tst_res(TPASS, "smart_scan skipped more than 50%% of the pages."); + } + +#ifdef HAVE_DECL_MADV_MERGEABLE + if (madvise(memory, page_size, MADV_UNMERGEABLE) == -1) + tst_brk(TBROK|TERRNO, "madvise"); +#endif +} + +static struct tst_test test = { + .needs_root = 1, + .options = (struct tst_option[]) { + {} + }, + .save_restore = (const struct tst_path_val[]) { + {PATH_KSM "pages_skipped", NULL, TST_SR_TCONF}, + {PATH_KSM "run", NULL, TST_SR_TCONF}, + {PATH_KSM "sleep_millisecs", NULL, TST_SR_TCONF}, + {PATH_KSM "smart_scan", "1", + TST_SR_SKIP_MISSING | TST_SR_TCONF}, + {} + }, + .needs_kconfigs = (const char *const[]){ + "CONFIG_KSM=y", + NULL + }, + .test_all = verify_ksm, +};