From patchwork Fri Aug 30 10:03:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Usama Arif X-Patchwork-Id: 13784869 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 5E288CA0EFC for ; Fri, 30 Aug 2024 10:04:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1AA6D6B00F7; Fri, 30 Aug 2024 06:04:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 164DC6B00F8; Fri, 30 Aug 2024 06:04:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EEA826B00F9; Fri, 30 Aug 2024 06:04:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id CC0936B00F7 for ; Fri, 30 Aug 2024 06:04:49 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 853591416C4 for ; Fri, 30 Aug 2024 10:04:49 +0000 (UTC) X-FDA: 82508477898.14.452FDD5 Received: from mail-oa1-f50.google.com (mail-oa1-f50.google.com [209.85.160.50]) by imf29.hostedemail.com (Postfix) with ESMTP id A37CA120009 for ; Fri, 30 Aug 2024 10:04:47 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=KJ9y3ZHx; spf=pass (imf29.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.160.50 as permitted sender) smtp.mailfrom=usamaarif642@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=1725012168; 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=ozbFo2ch+MkbZ+krZSCzDhOFvoowl/UCaYy4gtivTp4=; b=tTCOYSlv207DB1FiR6YqL2RYD6W30ufuGLl4oivuVlRtMBP0AiIyruDYhv4gLRvaQ137ek 4kuoCaMrjCMJHLsLi29N2suOpfT/ltQhb/SIS3E16w4EWlcvnwBKpqPdOftevqkZpIxXTk EUA0+IvrEK0P4ag6Sea68QlHKo9niyU= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=KJ9y3ZHx; spf=pass (imf29.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.160.50 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725012168; a=rsa-sha256; cv=none; b=s42l1zfoPZ+AuRbhvc478IZhuGXbDJYQ7DxKiJfXvLFEkn53IRjlSE3QmLluHCVNiwV9q3 okt1DLcG98z8WI2v2z0cPccA0hhoVtkOUwAiUcZN/IK3kUAB5Y05HBmogGo8KJoA3HlOMo ccO71BRX1bmqzG8Xv1V85UwadGbRHEE= Received: by mail-oa1-f50.google.com with SMTP id 586e51a60fabf-27045e54272so798660fac.0 for ; Fri, 30 Aug 2024 03:04:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725012286; x=1725617086; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ozbFo2ch+MkbZ+krZSCzDhOFvoowl/UCaYy4gtivTp4=; b=KJ9y3ZHxqwqXWYfZOg2r5ZrCyvQnrK92QySYInBWlfAjfJqvvNKPOJDmcmNl9wZhvQ Ikql+8P8aw/Bp+33ScQBssnAc74961Te0fPt4oKEKTe6OngMgLTS67gpoA2f/zn5gGqj BptkkmS2KFz+oE9h7e9w4IzQF4P86gpoaEp/wjoV08KEWZnA59eYm9gP8aiJiuXyjz7R GHwxVmIuG0oFKKRFc7k87BiszxPp8b09ykdAXuLlGSID2S9HV1Wn06JVSFGajprbtUY2 ZOMbfr7n5gPwPifyUoalnwJyEVygPOKWmlaJ7NsVGdiQd0NYtf++IVj7yQNea61rwYIT iFIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725012286; x=1725617086; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ozbFo2ch+MkbZ+krZSCzDhOFvoowl/UCaYy4gtivTp4=; b=v1ffM62kkxzvenL83UjtaR0GHUMcn3z5TpTY/IXuPlN+G/Ec7/78EBq0/bO/6WJOjj rsoUoCQaRwE8r1PuG2Cod/1TnbOLc/IXy/0640nzhfeo51N9d9xvn0ahjPVw9dx0ICUE sZj6L2f0YKBr/4cLhb3U6z518q38Njm9QDBtpCEbep5pDTVEQcpaM3oles+Lxvtal8SC 645zzBSCNL2KHB2HOqiyAU7PBlz7VPqHbAHIsz1tmx1CE5hxOYRq0J6WR6ojA8N2a45Z j1zZ1qs+s8UdepTKJXsAYct7PkJF2EWiOpQ3Cs+lLXYyIEZZb7GoTvPaXlq+koMn1DfM YBDg== X-Forwarded-Encrypted: i=1; AJvYcCUC5p2xMJw4BuR5oSfIVI4DcQnGjRJDZQDzsBl5pfCQpvwkgqanMJJAX3R8b24HrUMAtonROSkUCQ==@kvack.org X-Gm-Message-State: AOJu0YwxLltwTwxbwsA/NUgZfslDq/55pWejIxWXGMzOzRYCIUPw4xwF vVOxdYw37DPHFahfq86wTs4WHedQ0NSDiicOOgGwVgkyGnzYxr6s X-Google-Smtp-Source: AGHT+IFoXn0hlKtDOWPBmBf5kINkjOtVZEqP5VHvyK8WGYNnu5Y+WFxUha8zAkckLlImFFhEi3Na6w== X-Received: by 2002:a05:6870:2046:b0:270:1dab:64a9 with SMTP id 586e51a60fabf-277900c10d2mr6589971fac.14.1725012286247; Fri, 30 Aug 2024 03:04:46 -0700 (PDT) Received: from localhost (fwdproxy-ash-003.fbsv.net. [2a03:2880:20ff:3::face:b00c]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7a806c4a6e6sm129878685a.64.2024.08.30.03.04.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Aug 2024 03:04:45 -0700 (PDT) From: Usama Arif To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: hannes@cmpxchg.org, riel@surriel.com, shakeel.butt@linux.dev, roman.gushchin@linux.dev, yuzhao@google.com, david@redhat.com, npache@redhat.com, baohua@kernel.org, ryan.roberts@arm.com, rppt@kernel.org, willy@infradead.org, cerasuolodomenico@gmail.com, ryncsn@gmail.com, corbet@lwn.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kernel-team@meta.com, Alexander Zhu , Usama Arif Subject: [PATCH v5 3/6] mm: selftest to verify zero-filled pages are mapped to zeropage Date: Fri, 30 Aug 2024 11:03:37 +0100 Message-ID: <20240830100438.3623486-4-usamaarif642@gmail.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240830100438.3623486-1-usamaarif642@gmail.com> References: <20240830100438.3623486-1-usamaarif642@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: A37CA120009 X-Stat-Signature: 4adwo1x9nbiqjgr8c41qm3toamjaqexb X-Rspam-User: X-HE-Tag: 1725012287-95709 X-HE-Meta: U2FsdGVkX1+FKBS8lh7ZzbKOf96lRjcACOgcoY0BELDTX8FF5UJqChOhrC5HyXrrIkrM4T+0ihjffWYu/R0iHf8xwJC4Kp0OIT2313UIcAlpCIuK7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7Qv0yD7PV53zeeBsQxJ9p4Ewj62lFh5MvH7BYRDhGeCPdo48O7cIrGWluN+H98fG2tX2SUJW2OYn6ktkTILKk4xjfpZpfYRS5x3td7SZySO5dp72DfDl39W8MtgYPnJ/tLszzdoVQh/lXD97A2DZLtQ6ECGRwPAx7WmjLAQc0Tv0rZi5RBCiW48q1xif5ld9JFMsQXc05nNhxXcgNEKUTGJuBeSNFiPBYcMXfW5a1rXpCPS2EiBArS1gEB4V1pv10ttwq1W4ZGfU3bx0LA== 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: From: Alexander Zhu When a THP is split, any subpage that is zero-filled will be mapped to the shared zeropage, hence saving memory. Add selftest to verify this by allocating zero-filled THP and comparing RssAnon before and after split. Signed-off-by: Alexander Zhu Acked-by: Rik van Riel Signed-off-by: Usama Arif --- .../selftests/mm/split_huge_page_test.c | 71 +++++++++++++++++++ tools/testing/selftests/mm/vm_util.c | 22 ++++++ tools/testing/selftests/mm/vm_util.h | 1 + 3 files changed, 94 insertions(+) diff --git a/tools/testing/selftests/mm/split_huge_page_test.c b/tools/testing/selftests/mm/split_huge_page_test.c index e5e8dafc9d94..eb6d1b9fc362 100644 --- a/tools/testing/selftests/mm/split_huge_page_test.c +++ b/tools/testing/selftests/mm/split_huge_page_test.c @@ -84,6 +84,76 @@ static void write_debugfs(const char *fmt, ...) write_file(SPLIT_DEBUGFS, input, ret + 1); } +static char *allocate_zero_filled_hugepage(size_t len) +{ + char *result; + size_t i; + + result = memalign(pmd_pagesize, len); + if (!result) { + printf("Fail to allocate memory\n"); + exit(EXIT_FAILURE); + } + + madvise(result, len, MADV_HUGEPAGE); + + for (i = 0; i < len; i++) + result[i] = (char)0; + + return result; +} + +static void verify_rss_anon_split_huge_page_all_zeroes(char *one_page, int nr_hpages, size_t len) +{ + unsigned long rss_anon_before, rss_anon_after; + size_t i; + + if (!check_huge_anon(one_page, 4, pmd_pagesize)) { + printf("No THP is allocated\n"); + exit(EXIT_FAILURE); + } + + rss_anon_before = rss_anon(); + if (!rss_anon_before) { + printf("No RssAnon is allocated before split\n"); + exit(EXIT_FAILURE); + } + + /* split all THPs */ + write_debugfs(PID_FMT, getpid(), (uint64_t)one_page, + (uint64_t)one_page + len, 0); + + for (i = 0; i < len; i++) + if (one_page[i] != (char)0) { + printf("%ld byte corrupted\n", i); + exit(EXIT_FAILURE); + } + + if (!check_huge_anon(one_page, 0, pmd_pagesize)) { + printf("Still AnonHugePages not split\n"); + exit(EXIT_FAILURE); + } + + rss_anon_after = rss_anon(); + if (rss_anon_after >= rss_anon_before) { + printf("Incorrect RssAnon value. Before: %ld After: %ld\n", + rss_anon_before, rss_anon_after); + exit(EXIT_FAILURE); + } +} + +void split_pmd_zero_pages(void) +{ + char *one_page; + int nr_hpages = 4; + size_t len = nr_hpages * pmd_pagesize; + + one_page = allocate_zero_filled_hugepage(len); + verify_rss_anon_split_huge_page_all_zeroes(one_page, nr_hpages, len); + printf("Split zero filled huge pages successful\n"); + free(one_page); +} + void split_pmd_thp(void) { char *one_page; @@ -431,6 +501,7 @@ int main(int argc, char **argv) fd_size = 2 * pmd_pagesize; + split_pmd_zero_pages(); split_pmd_thp(); split_pte_mapped_thp(); split_file_backed_thp(); diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c index 5a62530da3b5..d8d0cf04bb57 100644 --- a/tools/testing/selftests/mm/vm_util.c +++ b/tools/testing/selftests/mm/vm_util.c @@ -12,6 +12,7 @@ #define PMD_SIZE_FILE_PATH "/sys/kernel/mm/transparent_hugepage/hpage_pmd_size" #define SMAP_FILE_PATH "/proc/self/smaps" +#define STATUS_FILE_PATH "/proc/self/status" #define MAX_LINE_LENGTH 500 unsigned int __page_size; @@ -171,6 +172,27 @@ uint64_t read_pmd_pagesize(void) return strtoul(buf, NULL, 10); } +unsigned long rss_anon(void) +{ + unsigned long rss_anon = 0; + FILE *fp; + char buffer[MAX_LINE_LENGTH]; + + fp = fopen(STATUS_FILE_PATH, "r"); + if (!fp) + ksft_exit_fail_msg("%s: Failed to open file %s\n", __func__, STATUS_FILE_PATH); + + if (!check_for_pattern(fp, "RssAnon:", buffer, sizeof(buffer))) + goto err_out; + + if (sscanf(buffer, "RssAnon:%10lu kB", &rss_anon) != 1) + ksft_exit_fail_msg("Reading status error\n"); + +err_out: + fclose(fp); + return rss_anon; +} + bool __check_huge(void *addr, char *pattern, int nr_hpages, uint64_t hpage_size) { diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h index 9007c420d52c..2eaed8209925 100644 --- a/tools/testing/selftests/mm/vm_util.h +++ b/tools/testing/selftests/mm/vm_util.h @@ -39,6 +39,7 @@ unsigned long pagemap_get_pfn(int fd, char *start); void clear_softdirty(void); bool check_for_pattern(FILE *fp, const char *pattern, char *buf, size_t len); uint64_t read_pmd_pagesize(void); +unsigned long rss_anon(void); bool check_huge_anon(void *addr, int nr_hpages, uint64_t hpage_size); bool check_huge_file(void *addr, int nr_hpages, uint64_t hpage_size); bool check_huge_shmem(void *addr, int nr_hpages, uint64_t hpage_size);