From patchwork Wed Sep 7 14:45:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zach O'Keefe X-Patchwork-Id: 12969077 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 E6397C38145 for ; Wed, 7 Sep 2022 14:45:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EBBC76B0080; Wed, 7 Sep 2022 10:45:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E504F6B0081; Wed, 7 Sep 2022 10:45:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CBD098D0001; Wed, 7 Sep 2022 10:45:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id BF01B6B0080 for ; Wed, 7 Sep 2022 10:45:50 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 2369B161952 for ; Wed, 7 Sep 2022 14:45:50 +0000 (UTC) X-FDA: 79885563660.12.DFA4712 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) by imf07.hostedemail.com (Postfix) with ESMTP id BF2AB40064 for ; Wed, 7 Sep 2022 14:45:49 +0000 (UTC) Received: by mail-pg1-f201.google.com with SMTP id j3-20020a634a43000000b00429f2cb4a43so7575026pgl.0 for ; Wed, 07 Sep 2022 07:45:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date; bh=eptMqZsXfthc4Fo3nDS8AoWTbJT6CSImKGcX5IRPq98=; b=TTssvBqf26eSJ/3HRBZ7OIpS4VLeSLyfUOmqi+jI4iXPNHD8PZ9Z+NRWzCX+P/3jCO NrJpBd6jgC0D8arkaOmU4/P3ReDlD0qVYnDQqsa0pA4utInPlpmkqQfBLX4pvtHwLrR/ PFr15plil+THMXoA5H0Wy7HOAP/rkDUaKEeivNJerHL7Lskxs58nl47f9b9SSnoJhdhV Kh4NPp5dLgEFJCV9MFwhtbrwBhY7kSgVBtsq+Mi0D+umwk642dhwnxi+OkkHxiQWg+CD nfkWkea6IuJTBZKEVnLfjO6YCPhEY677uqPBhsmPAoD8/OvyldbJT/qGUGPA+dq7+Cin xxog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date; bh=eptMqZsXfthc4Fo3nDS8AoWTbJT6CSImKGcX5IRPq98=; b=qFy6QNZun5aK8TfApUktVqsyUYW6qWIOclDU9HaMzcYKTCgTNRq8ujKv19wJWNvk5k DArEeNNh2BSB1vxgp9SJOLM3JFDIZlYquoUqAabx9J+BX2wB86cYCdDGF2wanDa3XDGs fy3Kb7iFSMVP3EFTpQwCbJpIM+RzI3VWXS5EtOiP1UXKDsxv/CxrK/YX261C4LnMhOEd EliOXoiSbnVneVAR/ZjS0sYE3a7R94YOFFWfauh9cIJDXL//TJS0NllSAta8ZBMDbAs+ OJ3T7Pt3NTisI4Q47qDz8mJUSO3XRmDIQB7TXSbLgyqYqC1LL/F2Wsqju0THtj5G7UWY HTcg== X-Gm-Message-State: ACgBeo0g5YIEDJ9B9TZvUOZe0oGyms4cYju+l5Q/EBDY5NOfD8+LzocW J6HPlt4oyNFxwZkSNBUCpJhsOwQgQ6RseARrn8FOSOsbBFyZN1qJ3jIMO6FOZE7Au/nuOcmRhOI 2ASeLbrrTeKtVfftq1apGBraI5deAaOyMrgKz/7SGjgblB21NRkMUoT9+bho= X-Google-Smtp-Source: AA6agR5PUIpjR65xaT9rRH0ZEW/PyWeAVTl4DMB7WjwcrnUcJyP+cZuGbCU/W8amXtmaFZ89TEMrqxw5WRWj X-Received: from zokeefe3.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1b6]) (user=zokeefe job=sendgmr) by 2002:a17:90a:249:b0:1e0:a8a3:3c6c with SMTP id t9-20020a17090a024900b001e0a8a33c6cmr19770pje.0.1662561947963; Wed, 07 Sep 2022 07:45:47 -0700 (PDT) Date: Wed, 7 Sep 2022 07:45:19 -0700 In-Reply-To: <20220907144521.3115321-1-zokeefe@google.com> Mime-Version: 1.0 References: <20220907144521.3115321-1-zokeefe@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220907144521.3115321-9-zokeefe@google.com> Subject: [PATCH mm-unstable v3 08/10] selftests/vm: add thp collapse shmem testing From: "Zach O'Keefe" To: linux-mm@kvack.org Cc: Andrew Morton , linux-api@vger.kernel.org, Axel Rasmussen , James Houghton , Hugh Dickins , Yang Shi , Miaohe Lin , David Hildenbrand , David Rientjes , Matthew Wilcox , Pasha Tatashin , Peter Xu , Rongwei Wang , SeongJae Park , Song Liu , Vlastimil Babka , Chris Kennelly , "Kirill A. Shutemov" , Minchan Kim , Patrick Xia , "Zach O'Keefe" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1662561949; a=rsa-sha256; cv=none; b=Za+MPEjLRMSqNkvKKPDgkU6UkrKKHHVOH5kZlg7c85sB+Jc1oZpzY7nRPHnO0gqOMTtomy VseCz+ayPpOUYdHtA5/hA9gf0vDLoD72BbL2j+vU21hFIGXYqOgj2burctNjuaur1KSmE8 l1+3XCUojpaGAdXn/JJ8gd2P8mB9sp8= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=TTssvBqf; spf=pass (imf07.hostedemail.com: domain of 3m64YYwcKCHoxmiccdcemmejc.amkjglsv-kkitYai.mpe@flex--zokeefe.bounces.google.com designates 209.85.215.201 as permitted sender) smtp.mailfrom=3m64YYwcKCHoxmiccdcemmejc.amkjglsv-kkitYai.mpe@flex--zokeefe.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1662561949; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=eptMqZsXfthc4Fo3nDS8AoWTbJT6CSImKGcX5IRPq98=; b=Zy7W1msL0U12Og+/OuGnok7yCsfoOPewdTXbTQ1Wqqp6cBvSs3g7BP9eXUidBGOPJSOAqc GJyCh7yvmuRP7VbS18MSwGw5qb3F3HzzmUymjQoVpHwPLgMJuLE6M9rIyC1/fOHBlCwC8b cAL0WC3BsMa/xPTO48nlyFuwJRnqrTg= X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: BF2AB40064 X-Rspam-User: Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=TTssvBqf; spf=pass (imf07.hostedemail.com: domain of 3m64YYwcKCHoxmiccdcemmejc.amkjglsv-kkitYai.mpe@flex--zokeefe.bounces.google.com designates 209.85.215.201 as permitted sender) smtp.mailfrom=3m64YYwcKCHoxmiccdcemmejc.amkjglsv-kkitYai.mpe@flex--zokeefe.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Stat-Signature: gddachxm78topirr1th6im15p7unm77r X-HE-Tag: 1662561949-926830 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: Add memory operations for shmem (memfd) memory, and reuse existing tests with the new memory operations. Shmem tests can be called with "shmem" mem_type, and shmem tests are ran with "all" mem_type as well. Signed-off-by: Zach O'Keefe --- tools/testing/selftests/vm/khugepaged.c | 57 ++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/vm/khugepaged.c b/tools/testing/selftests/vm/khugepaged.c index 59f56a329f43..05d9945daa48 100644 --- a/tools/testing/selftests/vm/khugepaged.c +++ b/tools/testing/selftests/vm/khugepaged.c @@ -58,6 +58,7 @@ struct mem_ops { static struct mem_ops *file_ops; static struct mem_ops *anon_ops; +static struct mem_ops *shmem_ops; struct collapse_context { void (*collapse)(const char *msg, char *p, int nr_hpages, @@ -708,6 +709,40 @@ static bool file_check_huge(void *addr, int nr_hpages) } } +static void *shmem_setup_area(int nr_hpages) +{ + void *p; + unsigned long size = nr_hpages * hpage_pmd_size; + + finfo.fd = memfd_create("khugepaged-selftest-collapse-shmem", 0); + if (finfo.fd < 0) { + perror("memfd_create()"); + exit(EXIT_FAILURE); + } + if (ftruncate(finfo.fd, size)) { + perror("ftruncate()"); + exit(EXIT_FAILURE); + } + p = mmap(BASE_ADDR, size, PROT_READ | PROT_WRITE, MAP_SHARED, finfo.fd, + 0); + if (p != BASE_ADDR) { + perror("mmap()"); + exit(EXIT_FAILURE); + } + return p; +} + +static void shmem_cleanup_area(void *p, unsigned long size) +{ + munmap(p, size); + close(finfo.fd); +} + +static bool shmem_check_huge(void *addr, int nr_hpages) +{ + return check_huge_shmem(addr, nr_hpages, hpage_pmd_size); +} + static struct mem_ops __anon_ops = { .setup_area = &anon_setup_area, .cleanup_area = &anon_cleanup_area, @@ -724,6 +759,14 @@ static struct mem_ops __file_ops = { .name = "file", }; +static struct mem_ops __shmem_ops = { + .setup_area = &shmem_setup_area, + .cleanup_area = &shmem_cleanup_area, + .fault = &anon_fault, + .check_huge = &shmem_check_huge, + .name = "shmem", +}; + static void __madvise_collapse(const char *msg, char *p, int nr_hpages, struct mem_ops *ops, bool expect) { @@ -1285,7 +1328,7 @@ static void usage(void) fprintf(stderr, "\nUsage: ./khugepaged [dir]\n\n"); fprintf(stderr, "\t\t: :\n"); fprintf(stderr, "\t\t: [all|khugepaged|madvise]\n"); - fprintf(stderr, "\t\t: [all|anon|file]\n"); + fprintf(stderr, "\t\t: [all|anon|file|shmem]\n"); fprintf(stderr, "\n\t\"file,all\" mem_type requires [dir] argument\n"); fprintf(stderr, "\n\t\"file,all\" mem_type requires kernel built with\n"); fprintf(stderr, "\tCONFIG_READ_ONLY_THP_FOR_FS=y\n"); @@ -1327,10 +1370,13 @@ static void parse_test_type(int argc, const char **argv) if (!strcmp(buf, "all")) { file_ops = &__file_ops; anon_ops = &__anon_ops; + shmem_ops = &__shmem_ops; } else if (!strcmp(buf, "anon")) { anon_ops = &__anon_ops; } else if (!strcmp(buf, "file")) { file_ops = &__file_ops; + } else if (!strcmp(buf, "shmem")) { + shmem_ops = &__shmem_ops; } else { usage(); } @@ -1347,7 +1393,7 @@ int main(int argc, const char **argv) struct settings default_settings = { .thp_enabled = THP_MADVISE, .thp_defrag = THP_DEFRAG_ALWAYS, - .shmem_enabled = SHMEM_NEVER, + .shmem_enabled = SHMEM_ADVISE, .use_zero_page = 0, .khugepaged = { .defrag = 1, @@ -1394,16 +1440,20 @@ int main(int argc, const char **argv) TEST(collapse_full, khugepaged_context, anon_ops); TEST(collapse_full, khugepaged_context, file_ops); + TEST(collapse_full, khugepaged_context, shmem_ops); TEST(collapse_full, madvise_context, anon_ops); TEST(collapse_full, madvise_context, file_ops); + TEST(collapse_full, madvise_context, shmem_ops); TEST(collapse_empty, khugepaged_context, anon_ops); TEST(collapse_empty, madvise_context, anon_ops); TEST(collapse_single_pte_entry, khugepaged_context, anon_ops); TEST(collapse_single_pte_entry, khugepaged_context, file_ops); + TEST(collapse_single_pte_entry, khugepaged_context, shmem_ops); TEST(collapse_single_pte_entry, madvise_context, anon_ops); TEST(collapse_single_pte_entry, madvise_context, file_ops); + TEST(collapse_single_pte_entry, madvise_context, shmem_ops); TEST(collapse_max_ptes_none, khugepaged_context, anon_ops); TEST(collapse_max_ptes_none, khugepaged_context, file_ops); @@ -1417,8 +1467,10 @@ int main(int argc, const char **argv) TEST(collapse_full_of_compound, khugepaged_context, anon_ops); TEST(collapse_full_of_compound, khugepaged_context, file_ops); + TEST(collapse_full_of_compound, khugepaged_context, shmem_ops); TEST(collapse_full_of_compound, madvise_context, anon_ops); TEST(collapse_full_of_compound, madvise_context, file_ops); + TEST(collapse_full_of_compound, madvise_context, shmem_ops); TEST(collapse_compound_extreme, khugepaged_context, anon_ops); TEST(collapse_compound_extreme, madvise_context, anon_ops); @@ -1440,6 +1492,7 @@ int main(int argc, const char **argv) TEST(madvise_collapse_existing_thps, madvise_context, anon_ops); TEST(madvise_collapse_existing_thps, madvise_context, file_ops); + TEST(madvise_collapse_existing_thps, madvise_context, shmem_ops); restore_settings(0); }