From patchwork Thu Jan 5 10:18:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13089678 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 7B656C3DA7D for ; Thu, 5 Jan 2023 10:20:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1A24F900004; Thu, 5 Jan 2023 05:20:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 15160900002; Thu, 5 Jan 2023 05:20:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F348A900004; Thu, 5 Jan 2023 05:20:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id E50E9900002 for ; Thu, 5 Jan 2023 05:20:03 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 94565A0D29 for ; Thu, 5 Jan 2023 10:20:03 +0000 (UTC) X-FDA: 80320349886.02.17C7E2A Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf30.hostedemail.com (Postfix) with ESMTP id 08A9F8000D for ; Thu, 5 Jan 2023 10:20:01 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=JoK7U3yU; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf30.hostedemail.com: domain of 3UaS2YwoKCJgBL9GM89LGF8GG8D6.4GEDAFMP-EECN24C.GJ8@flex--jthoughton.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3UaS2YwoKCJgBL9GM89LGF8GG8D6.4GEDAFMP-EECN24C.GJ8@flex--jthoughton.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1672914002; 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=rtdl4oKQo+XEbgxtU4YTxEZN3Xtv3pieSBor01lKxpM=; b=EbLXDEZaqRqx5n4iMxZgwR6b02D40obIifZnr9KNiURt6/BlSLRgaZ3npb7bVUgW9tcOmX gt2FFeE9PTKSf1MzS+vVCVXgf6VcOkqvzdcXUlUTfKq963KDnnJSd3NpAS6Ev6e9sThif/ bEuvBPLTrHOfB6czOPDZ3ApCuW7HApE= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=JoK7U3yU; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf30.hostedemail.com: domain of 3UaS2YwoKCJgBL9GM89LGF8GG8D6.4GEDAFMP-EECN24C.GJ8@flex--jthoughton.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3UaS2YwoKCJgBL9GM89LGF8GG8D6.4GEDAFMP-EECN24C.GJ8@flex--jthoughton.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1672914002; a=rsa-sha256; cv=none; b=5g585GKXeChnRJrmRdv1FEIMB53EpNEKCSvFh7NjkGzPSm0N3NLWdLDJozgzl3yATzhkfd mGCvvJW0ZzU0HE+Z4MqNCAdtEMziTs5LnmPfewUEtGGOoFOIUbbTgx34T3IsYYQEu0xtAO 3YgICIp+Aq86lSljgkPu32AgVcuPN+E= Received: by mail-yb1-f201.google.com with SMTP id h66-20020a252145000000b0071a7340eea9so36101005ybh.6 for ; Thu, 05 Jan 2023 02:20:01 -0800 (PST) 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:message-id:reply-to; bh=rtdl4oKQo+XEbgxtU4YTxEZN3Xtv3pieSBor01lKxpM=; b=JoK7U3yUK3xMp7jj3ED65QQRxyxFdtP2VVXxjeaagA1yOQ1VDOWmerpsJ93SBkq9zh 1+sCrcsTNTUzGHfBCN3DVG0Ub0PaSUoyZmam0r/dYWFPfLKvvL3DvvjbWnpAFCJmqtmQ 9dL77taavQp85y0beQ4G5rqDpS08FPS6eH96z44yL0EIzphbGcRadmhuAWN3kkhTwcyi Z6tLvspR8aGzxFL9AdtTmhEJWc3alCOwAyuQe0Fe4aSztOMMuenUMqwRmV5J/FhoFHxX pOzfJfehMxt0Fd8Q97JtyGiSLNhamOIcoa298fqunC0dT5N8onLPpGnxOJexx6VIdt++ pOSw== 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:message-id:reply-to; bh=rtdl4oKQo+XEbgxtU4YTxEZN3Xtv3pieSBor01lKxpM=; b=5Gg9naikzLQk35WzLZH+H9QOLIw9ESmRKpwuBJG/FV2ULGj5vc8qfW0uhC7EemCxeq vB9j10BolB4zuBCHye5i8Km/t0wOWWg/hbKNvD93Mxh3J3QYBG/BXY6H2TbMDVJqvAMP 6fStV6tp2DKIpVi5PQP6+mv54lpgNfp+NVHipGyu3GzuKY1ELwWFSLsVHtkhs/CYHwSU o3F5VfLGdtye+SrhwqlArydgJuwdAqIQRKak2ZgQ+j1+9AhR3Jp+se617C/aACU89pft sTZBjVe2qNIi7wE7hDrCqm6sToASIBrN08UKI6b1a4G8t1ARoo6GRGoRWk418Ve9He6l O/sA== X-Gm-Message-State: AFqh2kqYOpgmB9DHvPp6PAb0IY2YheYgK2FatCbSSwq5bwfZ2I5OYP4e a2lxRKYHQw8HTNn4f1PWf3IuGLvU9jaxiL+w X-Google-Smtp-Source: AMrXdXv5OilDl2+kIyWuY+cCyv/mwU3aGk8FszvI2rVdH9/dLOCIgt0mfQQiMmbp6/zudM0adl4O7gCLJPTvbpXO X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a25:8149:0:b0:7b6:5baa:c97c with SMTP id j9-20020a258149000000b007b65baac97cmr13349ybm.515.1672914001263; Thu, 05 Jan 2023 02:20:01 -0800 (PST) Date: Thu, 5 Jan 2023 10:18:43 +0000 In-Reply-To: <20230105101844.1893104-1-jthoughton@google.com> Mime-Version: 1.0 References: <20230105101844.1893104-1-jthoughton@google.com> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog Message-ID: <20230105101844.1893104-46-jthoughton@google.com> Subject: [PATCH 45/46] selftests/vm: add hugetlb HGM test to migration selftest From: James Houghton To: Mike Kravetz , Muchun Song , Peter Xu Cc: David Hildenbrand , David Rientjes , Axel Rasmussen , Mina Almasry , "Zach O'Keefe" , Manish Mishra , Naoya Horiguchi , "Dr . David Alan Gilbert" , "Matthew Wilcox (Oracle)" , Vlastimil Babka , Baolin Wang , Miaohe Lin , Yang Shi , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, James Houghton X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 08A9F8000D X-Stat-Signature: taxgizhk71p9pbj1opys7ucejmhwhxtc X-HE-Tag: 1672914001-201394 X-HE-Meta: U2FsdGVkX1/1NPh1NUfbMbV9DjJDS9b12P9ilY6PxMegnnVcBuUXKY0gDWIGuFh9drcH/K7Cz0z/BfzZciTgQxkGlEwIg7YHD5MuutzSp24Cy4Iyx7LUnBR4aGoEYEHbMJan8zSJ8Z+WOjtLjLKqgK6KO93TUhrD9cWE76AZmmHGAjj92Gh/snDBKf6Uig6QhDnNE0fV1IGXNGBh5HCHNZQb4P9y5c1Fx21TcgJMm5Jcu+KNkLZQBzXY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/UUXCbbt/U7P3A7ZpyU/YKaLeTuFSajKBWqr93taaJqtGta9n8VftfD3jCleo2kymU2nHx6g== 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: This is mostly the same as the shared HugeTLB case, but instead of mapping the page with a regular page fault, we map it with lots of UFFDIO_CONTINUE operations. We also verify that the contents haven't changed after the migration, which would be the case if the post-migration PTEs pointed to the wrong page. Signed-off-by: James Houghton --- tools/testing/selftests/vm/migration.c | 146 +++++++++++++++++++++++++ 1 file changed, 146 insertions(+) diff --git a/tools/testing/selftests/vm/migration.c b/tools/testing/selftests/vm/migration.c index 21577a84d7e4..1fb3607accab 100644 --- a/tools/testing/selftests/vm/migration.c +++ b/tools/testing/selftests/vm/migration.c @@ -14,12 +14,21 @@ #include #include #include +#include +#include +#include +#include +#include #define TWOMEG (2<<20) #define RUNTIME (60) #define ALIGN(x, a) (((x) + (a - 1)) & (~((a) - 1))) +#ifndef MADV_SPLIT +#define MADV_SPLIT 26 +#endif + FIXTURE(migration) { pthread_t *threads; @@ -265,4 +274,141 @@ TEST_F_TIMEOUT(migration, shared_hugetlb, 2*RUNTIME) close(fd); } +#ifdef __NR_userfaultfd +static int map_at_high_granularity(char *mem, size_t length) +{ + int i; + int ret; + int uffd = syscall(__NR_userfaultfd, 0); + struct uffdio_api api; + struct uffdio_register reg; + int pagesize = getpagesize(); + + if (uffd < 0) { + perror("couldn't create uffd"); + return uffd; + } + + api.api = UFFD_API; + api.features = 0; + + ret = ioctl(uffd, UFFDIO_API, &api); + if (ret || api.api != UFFD_API) { + perror("UFFDIO_API failed"); + goto out; + } + + if (madvise(mem, length, MADV_SPLIT) == -1) { + perror("MADV_SPLIT failed"); + goto out; + } + + reg.range.start = (unsigned long)mem; + reg.range.len = length; + + reg.mode = UFFDIO_REGISTER_MODE_MISSING | UFFDIO_REGISTER_MODE_MINOR; + + ret = ioctl(uffd, UFFDIO_REGISTER, ®); + if (ret) { + perror("UFFDIO_REGISTER failed"); + goto out; + } + + /* UFFDIO_CONTINUE each 4K segment of the 2M page. */ + for (i = 0; i < length/pagesize; ++i) { + struct uffdio_continue cont; + + cont.range.start = (unsigned long long)mem + i * pagesize; + cont.range.len = pagesize; + cont.mode = 0; + ret = ioctl(uffd, UFFDIO_CONTINUE, &cont); + if (ret) { + fprintf(stderr, "UFFDIO_CONTINUE failed " + "for %llx -> %llx: %d\n", + cont.range.start, + cont.range.start + cont.range.len, + errno); + goto out; + } + } + ret = 0; +out: + close(uffd); + return ret; +} +#else +static int map_at_high_granularity(char *mem, size_t length) +{ + fprintf(stderr, "Userfaultfd missing\n"); + return -1; +} +#endif /* __NR_userfaultfd */ + +/* + * Tests the high-granularity hugetlb migration entry paths. + */ +TEST_F_TIMEOUT(migration, shared_hugetlb_hgm, 2*RUNTIME) +{ + uint64_t *ptr; + int i; + int fd; + unsigned long sz; + struct statfs filestat; + + if (self->nthreads < 2 || self->n1 < 0 || self->n2 < 0) + SKIP(return, "Not enough threads or NUMA nodes available"); + + fd = memfd_create("tmp_hugetlb", MFD_HUGETLB); + if (fd < 0) + SKIP(return, "Couldn't create hugetlb memfd"); + + if (fstatfs(fd, &filestat) < 0) + SKIP(return, "Couldn't fstatfs hugetlb file"); + + sz = filestat.f_bsize; + + if (ftruncate(fd, sz)) + SKIP(return, "Couldn't allocate hugetlb pages"); + + if (fallocate(fd, 0, 0, sz) < 0) { + perror("fallocate failed"); + SKIP(return, "fallocate failed"); + } + + ptr = mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (ptr == MAP_FAILED) + SKIP(return, "Could not allocate hugetlb pages"); + + /* + * We have to map_at_high_granularity before we memset, otherwise + * memset will map everything at the hugepage size. + */ + if (map_at_high_granularity((char *)ptr, sz) < 0) + SKIP(return, "Could not map HugeTLB range at high granularity"); + + /* Populate the page we're migrating. */ + for (i = 0; i < sz/sizeof(*ptr); ++i) + ptr[i] = i; + + for (i = 0; i < self->nthreads - 1; i++) + if (pthread_create(&self->threads[i], NULL, access_mem, ptr)) + perror("Couldn't create thread"); + + ASSERT_EQ(migrate(ptr, self->n1, self->n2, 10), 0); + for (i = 0; i < self->nthreads - 1; i++) { + ASSERT_EQ(pthread_cancel(self->threads[i]), 0); + pthread_join(self->threads[i], NULL); + } + + /* Check that the contents didnt' change. */ + for (i = 0; i < sz/sizeof(*ptr); ++i) { + ASSERT_EQ(ptr[i], i); + if (ptr[i] != i) + break; + } + + ftruncate(fd, 0); + close(fd); +} + TEST_HARNESS_MAIN