From patchwork Thu Apr 13 05:58:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 82813 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp817107vqo; Wed, 12 Apr 2023 23:10:35 -0700 (PDT) X-Google-Smtp-Source: AKy350a2sLmod5yVSyQJIAYciP+w6EInXh1B5aqsFIetqX5rq6a9dhbMslH1kESGSvypn8tDil0W X-Received: by 2002:a17:906:f8f:b0:8f4:ec13:d599 with SMTP id q15-20020a1709060f8f00b008f4ec13d599mr1682326ejj.27.1681366234827; Wed, 12 Apr 2023 23:10:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681366234; cv=none; d=google.com; s=arc-20160816; b=uixDTXHkBK2UUnqirLf3DJ3EzjNDPZUjz5R6pc0hSCxdcGDmA5w9S6ZDzeoirk8/yn SZ6dTWv4cwqBwzXPtctnPZvrnaol1l7lFUwFuyo7rqSRXbdYjVtu3m7TmiIrkViMlsPe M5axmoq1c8Zi4jTunFIfCoac0+S+eG5ro1KS+JaeR9ry9pGlYnxKMPNaQC4/Z3dQTE5G 2/zTtc1pp7JuNqdhM5DY3FRQ9i0vvS5rtgbIdbKyyy0YUqTHZ3R54QmLAVduDAS5PITs K+sqf1/I/VW//OObodSBgjAeYbJR2e5UAJOqhwB75EmJm1/Lu8UgMloZczzomO92Gv9i HPxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=pE7Wd0uExrD/jZCzy8tdXI088r8kHoluLhMZ3e6Ztyk=; b=P30jYoGka/JQQN0weYxuclwW9RC2lkcEkDZ6mxWVIdwGdTiWzlV/ns7KQWU3WYDyy6 kk2zf3cZy02M97bV1MCwL8ZRZuVU7iAFvQL2N1uqrmNrF2sfEBoQ0eteCYbHN12H2UI7 PNr1I2wUDiMDofXgKWuCdbvzcUJlh6K2E4aQmlF6o/EyM7c6tjxbrzk8yIPZUcfrLqmF UFyn+cEeqeoDUNcHetjFj7nQsuL5YNjrGjTravcPmUg0qtuzsbxLT1Jv1Yf3mSEvFh0y odSMqEDomNqeCeMoT170KwrVVEZhOMr3MSCloUbZJ/IJlewUO2UyKQVzzyVMz9OjJOLp /9AA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=zte.com.cn Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id vf5-20020a170907238500b00931475425a4si1052238ejb.495.2023.04.12.23.10.11; Wed, 12 Apr 2023 23:10:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=zte.com.cn Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229820AbjDMF6m (ORCPT + 99 others); Thu, 13 Apr 2023 01:58:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57304 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229793AbjDMF6l (ORCPT ); Thu, 13 Apr 2023 01:58:41 -0400 Received: from ubuntu20 (unknown [193.203.214.57]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A8CA7692 for ; Wed, 12 Apr 2023 22:58:39 -0700 (PDT) Received: by ubuntu20 (Postfix, from userid 1003) id E2947E1A39; Thu, 13 Apr 2023 05:58:37 +0000 (UTC) From: Yang Yang To: akpm@linux-foundation.org, david@redhat.com Cc: yang.yang29@zte.com.cn, imbrenda@linux.ibm.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, ran.xiaokai@zte.com.cn, xu.xin.sc@gmail.com, xu.xin16@zte.com.cn, Xuexin Jiang Subject: [PATCH v7 6/6] selftest: add a testcase of ksm zero pages Date: Thu, 13 Apr 2023 13:58:36 +0800 Message-Id: <20230413055836.181259-1-yang.yang29@zte.com.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <202304131346489021903@zte.com.cn> References: <202304131346489021903@zte.com.cn> MIME-Version: 1.0 X-Spam-Status: No, score=3.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,FSL_HELO_NON_FQDN_1, HEADER_FROM_DIFFERENT_DOMAINS,HELO_NO_DOMAIN,NO_DNS_FOR_FROM, RCVD_IN_PBL,RDNS_NONE,SPF_SOFTFAIL,SPOOFED_FREEMAIL_NO_RDNS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: *** X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763040280845117967?= X-GMAIL-MSGID: =?utf-8?q?1763040280845117967?= From: xu xin Add a function test_unmerge_zero_page() to test the functionality on unsharing and counting ksm-placed zero pages and counting of this patch series. test_unmerge_zero_page() actually contains three subjct test objects: (1) whether the count of ksm zero pages can update correctly after merging; (2) whether the count of ksm zero pages can update correctly after unmerging; (3) whether ksm zero pages are really unmerged. Signed-off-by: xu xin Cc: Claudio Imbrenda Cc: David Hildenbrand Cc: Xuexin Jiang Reviewed-by: Xiaokai Ran Reviewed-by: Yang Yang --- tools/testing/selftests/mm/ksm_functional_tests.c | 75 +++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c index d8b5b4930412..11f8e4726607 100644 --- a/tools/testing/selftests/mm/ksm_functional_tests.c +++ b/tools/testing/selftests/mm/ksm_functional_tests.c @@ -27,6 +27,8 @@ static int ksm_fd; static int ksm_full_scans_fd; +static int ksm_zero_pages_fd; +static int ksm_use_zero_pages_fd; static int pagemap_fd; static size_t pagesize; @@ -57,6 +59,21 @@ static bool range_maps_duplicates(char *addr, unsigned long size) return false; } +static long get_ksm_zero_pages(void) +{ + char buf[20]; + ssize_t read_size; + unsigned long ksm_zero_pages; + + read_size = pread(ksm_zero_pages_fd, buf, sizeof(buf) - 1, 0); + if (read_size < 0) + return -errno; + buf[read_size] = 0; + ksm_zero_pages = strtol(buf, NULL, 10); + + return ksm_zero_pages; +} + static long ksm_get_full_scans(void) { char buf[10]; @@ -146,6 +163,61 @@ static void test_unmerge(void) munmap(map, size); } +static inline unsigned long expected_ksm_pages(unsigned long mergeable_size) +{ + return mergeable_size / pagesize; +} + +static void test_unmerge_zero_pages(void) +{ + const unsigned int size = 2 * MiB; + char *map; + unsigned long pages_expected; + + ksft_print_msg("[RUN] %s\n", __func__); + + /* Confirm the interfaces*/ + if (ksm_zero_pages_fd < 0) { + ksft_test_result_skip("open(\"/sys/kernel/mm/ksm/ksm_zero_pages\") failed\n"); + return; + } + if (ksm_use_zero_pages_fd < 0) { + ksft_test_result_skip("open \"/sys/kernel/mm/ksm/use_zero_pages\" failed\n"); + return; + } + if (write(ksm_use_zero_pages_fd, "1", 1) != 1) { + ksft_test_result_skip("write \"/sys/kernel/mm/ksm/use_zero_pages\" failed\n"); + return; + } + + /* Mmap zero pages*/ + map = mmap_and_merge_range(0x00, size); + if (map == MAP_FAILED) + return; + + /* Check if ksm_zero_pages can be update correctly after merging */ + pages_expected = expected_ksm_pages(size); + ksft_test_result(pages_expected == get_ksm_zero_pages(), + "The count zero_page_sharing was updated after merging\n"); + + /* try to unmerge half of the region */ + if (madvise(map, size / 2, MADV_UNMERGEABLE)) { + ksft_test_result_fail("MADV_UNMERGEABLE failed\n"); + goto unmap; + } + + /* Check if ksm_zero_pages can be update correctly after unmerging */ + pages_expected = expected_ksm_pages(size / 2); + ksft_test_result(pages_expected == get_ksm_zero_pages(), + "The count zero_page_sharing was updated after unmerging\n"); + + /* Check if ksm zero pages are really unmerged */ + ksft_test_result(!range_maps_duplicates(map, size / 2), + "KSM zero pages were unmerged\n"); +unmap: + munmap(map, size); +} + static void test_unmerge_discarded(void) { const unsigned int size = 2 * MiB; @@ -264,8 +336,11 @@ int main(int argc, char **argv) pagemap_fd = open("/proc/self/pagemap", O_RDONLY); if (pagemap_fd < 0) ksft_exit_skip("open(\"/proc/self/pagemap\") failed\n"); + ksm_zero_pages_fd = open("/sys/kernel/mm/ksm/ksm_zero_pages", O_RDONLY); + ksm_use_zero_pages_fd = open("/sys/kernel/mm/ksm/use_zero_pages", O_RDWR); test_unmerge(); + test_unmerge_zero_pages(); test_unmerge_discarded(); #ifdef __NR_userfaultfd test_unmerge_uffd_wp();