From patchwork Mon Aug 19 02:30:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Usama Arif X-Patchwork-Id: 13767729 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 A71F6C531DF for ; Mon, 19 Aug 2024 02:31:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 11E9A6B0085; Sun, 18 Aug 2024 22:31:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0A7076B0088; Sun, 18 Aug 2024 22:31:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E3B356B0089; Sun, 18 Aug 2024 22:31:57 -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 BC37D6B0085 for ; Sun, 18 Aug 2024 22:31:57 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 75710A869D for ; Mon, 19 Aug 2024 02:31:57 +0000 (UTC) X-FDA: 82467419874.29.141C473 Received: from mail-yb1-f170.google.com (mail-yb1-f170.google.com [209.85.219.170]) by imf09.hostedemail.com (Postfix) with ESMTP id AC8F1140017 for ; Mon, 19 Aug 2024 02:31:55 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=itE3oYs5; spf=pass (imf09.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.219.170 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=1724034638; 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=Owo/SJvtMXykW8EjYMAoXbuTcNtghag9XOhQfHQGUKxeWPpDDcLkix0Sy2BImEXwlTiEaz gC8XIXfgur12rIl4lRSL0Q7q+wRE6ekqzt1EK+JJToUgGZlC6DOnEjK7ImaB31Q/3jmETY lENJfyonc9wwRHAXTgqBXUbJj6He3+4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724034638; a=rsa-sha256; cv=none; b=YRf+2A/ezS2x07Wed6khwvZFuPlyMz4CXMrxdQpUvUHnXfrtrShONsRKd3ktJyl7HbYCba wh39hM6hvKSUHpNTI6wZtI5f2H8AyMQO4Vrun+d76NhN9fm0eT41+Q/7dx6qwn/U/K1BWG xfFcQcqNGmWZzIZzZTBaDPvNgmHg7l0= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=itE3oYs5; spf=pass (imf09.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.219.170 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-yb1-f170.google.com with SMTP id 3f1490d57ef6-e117059666eso3506231276.3 for ; Sun, 18 Aug 2024 19:31:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724034715; x=1724639515; 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=itE3oYs5X2ewpcCPBcc+N3MzJ1CNzXnV6hxDyUh1Qj8PlnQHoylqc9S4Xg2l0DvDx2 Tt+ar1U1ISK/0jkxiOZL1DiznwVun5JSmkHoOc+dB2ddL9WhV0Is1ax3/qk8rMQT9ZC4 NnyakZd3LzNB+h6j+sny0jc9gdBxJDL/mDHHY6OQa6sm07mYeWRUq2jrbNMwbTe9jrlT BNnAQWaNXWVoPHFBkYd1gRMrVSBvJm3tmfeWTTu86pHYR/0WGXxXkaxcS4ScBt7AAn17 ksYSdss+FFdW5I/esZdMd2fpmqVnA7jIct7Hi9slQAAoAHKzmykAkR30OUBikXq2cc0e hVWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724034715; x=1724639515; 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=gCw+/b/+bEVYBI3dQfzcqHEr9fT/PlQvtFL4wUD5mqEiABGHGOPPk03XS0FLBLI16A qRV2NpsNCqkKsB30YbnjvzwGimo4DMInLYWw1ZYBfBfO9NV+Sr+5BIpqed3tgv/jgt+1 UydSbhmga/la8WYmBtVjh4L/oB4bznPDzgHff2hFWB0ponr5v/0t+q41IChVzTs97tJF AlosaqF2ONU3oAGWFZ4nU7D9XJKkkmCswVPEWJ2MdzheWXn1J6W1Na5eSN3Q7ZHuGGhD D9ewNARYud26JjwmDe/9yPMEOVLVfwtBl39RxxGjOI8DAOxOz6xg8AKLfYv4aq49eEJj 738Q== X-Forwarded-Encrypted: i=1; AJvYcCXlZhZhxSnANfIRBx6FUGv69DRyOCFRtZc+gZpJ8g+FGVtQ9L7kAJpz1Y4u1grMA8Q+i/9RzcmVbWHfTmWL1hIWzNU= X-Gm-Message-State: AOJu0Yzj6oRJVn93PlcyiOLP9e+TRPBk/8tE0cmhhof/GY9+GpWGbcFs ctFDP9B/OoTZ+ZYwgy2x/3Xfppo7m//7cfvrmlD2FMBSpxQvXVOk X-Google-Smtp-Source: AGHT+IGftm6KdS613kRYfd4LezgLxWlUfzFZGyqU4Es+6S/AqWgBVnIYntbuL1qMdd7RJe0UQP4CYA== X-Received: by 2002:a05:6902:260c:b0:e13:eaf4:884f with SMTP id 3f1490d57ef6-e13eaf4b68emr3300790276.13.1724034714608; Sun, 18 Aug 2024 19:31:54 -0700 (PDT) Received: from localhost (fwdproxy-ash-008.fbsv.net. [2a03:2880:20ff:8::face:b00c]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-4536a04e638sm36843281cf.71.2024.08.18.19.31.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Aug 2024 19:31:53 -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, 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 v4 3/6] mm: selftest to verify zero-filled pages are mapped to zeropage Date: Mon, 19 Aug 2024 03:30:56 +0100 Message-ID: <20240819023145.2415299-4-usamaarif642@gmail.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240819023145.2415299-1-usamaarif642@gmail.com> References: <20240819023145.2415299-1-usamaarif642@gmail.com> MIME-Version: 1.0 X-Stat-Signature: xbfwgr41udzsyjc5pziu5er3ye4su5ug X-Rspamd-Queue-Id: AC8F1140017 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1724034715-592141 X-HE-Meta: U2FsdGVkX18kPViwtYVoxwvKkcKxEmHbg1J09gQiBoz4aOF73Vk149KbSDciq+Yk3dMpaBoIMlYzrL0Ya0S4mGVrQ7uCFNxVAUB3kJz+43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ZjU44auprRLsdsf+xPISRy2w65bQJs9C1x7OV31uTtrddX4VIDoL6u8j9eAmrFx1Dy9ZZ2fPKVHZTgzkPobEZfRtPoPZldaoFRz7yrovjUUMJgV4sLvh0pSzUfZXZrJTRyUu6sKQ/HWa5hcCKNUPsAv1kNtLUHYCctFIlVk3gGXuB5ifyiOroWPakitnmwPdy8/hTzt4epQF5QY1Is5mMVMkj7p0aTDBX08jO+m8mRpc9kmiJLZEX5cc0+3pqvkRoz9M= 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 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);