From patchwork Tue Aug 13 12:02:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Usama Arif X-Patchwork-Id: 13761888 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 F23CAC52D7B for ; Tue, 13 Aug 2024 12:03:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DA4386B00A1; Tue, 13 Aug 2024 08:03:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D4CF66B00A3; Tue, 13 Aug 2024 08:03:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A66B96B00A5; Tue, 13 Aug 2024 08:03:51 -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 61A3A6B00A1 for ; Tue, 13 Aug 2024 08:03:51 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id E25C214078F for ; Tue, 13 Aug 2024 12:03:50 +0000 (UTC) X-FDA: 82447088220.06.B95A190 Received: from mail-qv1-f42.google.com (mail-qv1-f42.google.com [209.85.219.42]) by imf16.hostedemail.com (Postfix) with ESMTP id AF9D118000D for ; Tue, 13 Aug 2024 12:03:47 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=NL68yJXV; spf=pass (imf16.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.219.42 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=1723550556; 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=zqJ9Y6RR0iCOONporxiAWd0rvcRWYXEUM+u9cAIorwk=; b=DLCtg+DtEdXavh/ri8d89IZ0FHTlOSAczxW94oxUL4IIlhf4Af1Jl36AIibuvaxgIGeKHH yjQZAG+Pau4kETHmLUaoDau58auw78glpm7pcyks/JKXgLpfjj5tyFkVa5SpAGA7Mfk1bU OiOmMDZuhHClUODW5lTL8AXhkyuU8HU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723550556; a=rsa-sha256; cv=none; b=PkjGdnVcboatBzvTDzNzqIP5Ly2LIM5Nd2r1wAcwvX8SCkRFDDtglOhBDJ5ZEas66Oaw5d uBxzZSKCbDoXSka5SGzNfbweni1O57zWypX0eCN/0tWjyRZuPKVyG/In44PLsMPEYdrqGN fQaIs0Lpc6qAXz/ZYq4RWZssnkt83Cs= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=NL68yJXV; spf=pass (imf16.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.219.42 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-qv1-f42.google.com with SMTP id 6a1803df08f44-6b79b93a4c9so45030986d6.1 for ; Tue, 13 Aug 2024 05:03:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723550627; x=1724155427; 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=zqJ9Y6RR0iCOONporxiAWd0rvcRWYXEUM+u9cAIorwk=; b=NL68yJXVV593ASiA82+QymJHrmS2wvQupdxUbZTn6Yboem2HDVPeLKvT/wxpEEAlLM fErtPoGOtJk7/jIgrrTS+NFYeC8znlljUSd/jvqb5QLIRlyreYWfmjF7V/w5ibHrn1Kx DUvwq4zTL9thlJIbQUjrjTa9iRlAEqTg98m+fMoumbdqk+5BDzQk2jBtfOVFbpvStxD3 ZptOlGGQdu8xrOm7I6nJmB/MSC0lpUkEWJySR9yIUQTBqjjvNuKfYYVA1tM2cSggR6Y5 3NAgVbauIQYaudMNe1c/fua5+cRCqWn0DNWY5HXClu86s6yv4Xu1KjA2qUXJr6OJ7wW0 WqRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723550627; x=1724155427; 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=zqJ9Y6RR0iCOONporxiAWd0rvcRWYXEUM+u9cAIorwk=; b=sSNTMleuSyu6NOkfs0UNcp++zz6uyOuVJdICbtHYhJ94NfS35+30dGUYjfiUHSPMAG inuZx+bHW9+xY5vFNjV3o96JVZ/2AeCZNUZlR10YowtU1dVHPifcPTjTO4Pd+Qk+4xcY q9hPtYnC4DPYApsI33WBuagba8+K3oD19laeE5rQz7G/k6r5K7Zo+UcU8kW8fiOIAfjz OPr7/OOIQqpXVaN3DPeZITovYMUCyOsZnQYrSPUsF1v9dfU853gG0wDlJSeenjMhTtSU q1+Qu7iHonLF+YOLc+aLL55ngdm++33+y40olFRkCVMlmufd/WEw9dDUlptQfNXuTZUl U5lQ== X-Forwarded-Encrypted: i=1; AJvYcCXlOIh1ZcmMh/jisYprmBi2cvDH76PB6+uVzhdLvlaRsrA3mlDAi37R38ou8R3OhNaac3uhC6DoGm/BUziq7VNzIoo= X-Gm-Message-State: AOJu0YzG00CxkDITH7v2ypoMhHNEz5dZzp/G7vJqsb38Ye2dJ8SkE2mM OY4srgMQiAZJ/jizrcDMnPhKsIvh4y0RB4kGXOj91pm9gLUOCOT3 X-Google-Smtp-Source: AGHT+IFJ5i6wBfUkrpsXhs0Tc15ioa/fxXOSNWt+VVOIv2sQLq9LGVXdX8ciULa9jF+2pjwR9W6Tjw== X-Received: by 2002:a05:6214:2aab:b0:6bb:8b7b:c2df with SMTP id 6a1803df08f44-6bf50c6a12emr48629266d6.25.1723550626609; Tue, 13 Aug 2024 05:03:46 -0700 (PDT) Received: from localhost (fwdproxy-ash-112.fbsv.net. [2a03:2880:20ff:70::face:b00c]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6bd82e35785sm33439146d6.81.2024.08.13.05.03.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Aug 2024 05:03:46 -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, baohua@kernel.org, ryan.roberts@arm.com, rppt@kernel.org, willy@infradead.org, cerasuolodomenico@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 v3 3/6] mm: selftest to verify zero-filled pages are mapped to zeropage Date: Tue, 13 Aug 2024 13:02:46 +0100 Message-ID: <20240813120328.1275952-4-usamaarif642@gmail.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240813120328.1275952-1-usamaarif642@gmail.com> References: <20240813120328.1275952-1-usamaarif642@gmail.com> MIME-Version: 1.0 X-Stat-Signature: 5e467899ee9zoj7oqm4u579egqmy1k45 X-Rspamd-Queue-Id: AF9D118000D X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1723550627-332066 X-HE-Meta: U2FsdGVkX1/bAN7auqbFVSH8aKzWL+eUglhAfuk7wjXuBiH14ygP1/GjMqSKYnk61CAleHu+hS5zu5fMB9fVsUFys/y22qcskRZ/D7Jj23+DXaZB8VkthuIpgF7la1iBug9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5OYOj7NpioqE5hCizTxQnHwhJiaDf7B24xwIz4i0sTE4nakFzHTcX09DRrVWSzH/Itm8UgB34yRU0aHV4yQ8xnKX+2nq7g48uE3b+2IZyTd7+ByGSJS3JY/Ds1cShQbI+GxkK/AgONEt5dK4dQGDOUmgLGUSSFayJFIF28yQW/fKLlqx20Q7chY7/GWUlE4sn+QRimnxWrq/dfjt3yoPMt3JB0ufyy3NxR1fQFMntr8fJf0tTrfN0zj0HBBg51Qfs= 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..71b75429f4a5 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); +uint64_t 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);