From patchwork Sat Feb 18 00:28:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13145411 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 D8433C6379F for ; Sat, 18 Feb 2023 00:30:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0B0806B0081; Fri, 17 Feb 2023 19:29:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 064346B0082; Fri, 17 Feb 2023 19:29:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E1CFC6B0083; Fri, 17 Feb 2023 19:29:48 -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 CFBB46B0081 for ; Fri, 17 Feb 2023 19:29:48 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id AC7B540B47 for ; Sat, 18 Feb 2023 00:29:48 +0000 (UTC) X-FDA: 80478529656.18.3C94318 Received: from mail-vk1-f201.google.com (mail-vk1-f201.google.com [209.85.221.201]) by imf25.hostedemail.com (Postfix) with ESMTP id EACFAA000D for ; Sat, 18 Feb 2023 00:29:46 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=FNS8OXzQ; spf=pass (imf25.hostedemail.com: domain of 3-hvwYwoKCBk8I6DJ56IDC5DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--jthoughton.bounces.google.com designates 209.85.221.201 as permitted sender) smtp.mailfrom=3-hvwYwoKCBk8I6DJ56IDC5DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--jthoughton.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=1676680186; 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=IfKq0bBaVq4CjPIDar2fHBWF2wKUUWJpsDr5c4wwSS8=; b=R/RslYEW8ZL5uMWgY5m+1G+/TY22JcpdIUWBkFPInEQDgHDHiGA21J7LnKd0z98Co2uqQN xK/+w9VVUszegZ0OWaWY6KjOc3D/kgOMsBYBtZgaj9DaJBrmRmEasLlg4NgKpFtXHhl8WL qKaYVEhvBoru/uyyozzm6PUC3VkUl14= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=FNS8OXzQ; spf=pass (imf25.hostedemail.com: domain of 3-hvwYwoKCBk8I6DJ56IDC5DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--jthoughton.bounces.google.com designates 209.85.221.201 as permitted sender) smtp.mailfrom=3-hvwYwoKCBk8I6DJ56IDC5DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--jthoughton.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676680187; a=rsa-sha256; cv=none; b=vHAXewwg64KieGMMW+qHDpdTcwJxJAFKB6AZF027zYpxX6jc+w6kq1HXlZrw1sC74+d6by W7S9TE1UTsPq7ha4T9NaqkSt0otoPUTWB9Vbva6B0BggLdGQeHsKiA7GvOlfONzwD3j+8J qtu4tRKrKjhGYa7wOlwCheQCJarRsGM= Received: by mail-vk1-f201.google.com with SMTP id o31-20020a056122179f00b003e20d9fec6dso957017vkf.12 for ; Fri, 17 Feb 2023 16:29:46 -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=IfKq0bBaVq4CjPIDar2fHBWF2wKUUWJpsDr5c4wwSS8=; b=FNS8OXzQNbEkpLDpjB3nAEwYdiqjzDyLdWfy2qMCsFYgDJSKBhPFK3yDxyIWpeySgl maQUSikgn8RHfHnveHKP4KewomfuBHkrzkOdNxKAEwOz4ERjMdSgdS+IkmHhDevj0Dlo 8SYMrUU5w0fXhm10675DXBEz1qa82TExwLIbs6xG0LABcq+O89FT45Z9grk1Qq9uWQnL VEl/w45AklX6i6AYyOmin2NKbHJDvd0cIf5fk1zu+qjIn8o5y4g9O4lMFPFSQVgZ0QZ9 4eExXJ6WU1Q2oLrhiuNiGB+aLlTM+fJuXr+noURddq0gbHVbzoVDxDtmh6wk9zHk9oKN 9jig== 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=IfKq0bBaVq4CjPIDar2fHBWF2wKUUWJpsDr5c4wwSS8=; b=k1mdnSNG1LwD+ygne9lZdsLG3eOlJsGy90a2EZoeFfZBt1Uo/FxuDThVkRMe6i0eYi T7K6sRLij6jIX982pLh0THiDckCXEcrqvXakePB9qiFsUSlA1uOfgq2IvtBZIp/U4c2F XP6s9OCNgJ2S8pvE9iFy/11/QyfbcxUo0fwxT1Ta2Jy3wgYaFhb4LJOp51VeWrpA2sxe 2icEKWiQAQCScl+ttUmTZJdEWvUBMZ3joAApDCmPJXdLoFE6TSRejPFW/ZCekIqu1FN/ aHJlldFUMxS9THcQbLo9jLtcnOYzpy2LKPlOWKV6HnRoTpyOwteHAlZI1FnV5JEirdy9 jYCg== X-Gm-Message-State: AO0yUKWbjD+xT4kb1HI4Jw81bFNy5maba4F5wwa84CJCyZqohJ1zR+FP 6BM7qLhXzL6XUUR0VpqegQr3K4v3cZ0LbgDl X-Google-Smtp-Source: AK7set+/8h4Ti6SaOCrSw2JWCwa+A4XjEDrEdPurb5cORBytv6OxLdCN0/KOEI5ep27FSzg7p5WKdfqWfRYxseKZ X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:ab0:53d2:0:b0:68b:923a:d6f4 with SMTP id l18-20020ab053d2000000b0068b923ad6f4mr47364uaa.2.1676680186208; Fri, 17 Feb 2023 16:29:46 -0800 (PST) Date: Sat, 18 Feb 2023 00:28:18 +0000 In-Reply-To: <20230218002819.1486479-1-jthoughton@google.com> Mime-Version: 1.0 References: <20230218002819.1486479-1-jthoughton@google.com> X-Mailer: git-send-email 2.39.2.637.g21b0678d19-goog Message-ID: <20230218002819.1486479-46-jthoughton@google.com> Subject: [PATCH v2 45/46] selftests/mm: add hugetlb HGM test to migration selftest From: James Houghton To: Mike Kravetz , Muchun Song , Peter Xu , Andrew Morton 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 , Frank van der Linden , Jiaqi Yan , linux-mm@kvack.org, linux-kernel@vger.kernel.org, James Houghton X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: EACFAA000D X-Rspam-User: X-Stat-Signature: t8nck9x14xsa9ostw9wxnhc7ohaq7kdi X-HE-Tag: 1676680186-286491 X-HE-Meta: U2FsdGVkX19xgfjnZheospz6h1N751sKTjjhipzV3GSkOVFtbH1yHuuakalOMYHnCkHCGhQ9jsSRfjC1rwgKi5k9yoNymYKclTIrqys8EPVDJGBCq3yTQJ0dYPtYjZb+qaEYuZwalgqyJRXkdJsnTCoosbaXAhLyc2SoIfNtZtj2pwuNjIYLghA9dIWgMxPwxLO0ztGoNTyKPI52Nay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lyiFQoKw+rZsmIIVR7h8l1KdOVPiCw6NPiSycLZ25X81gNCOIQ3IbABWAUTlzx3Lc823UnEJmK/MdZvEbEFsK+Lj34kOa8vbTyWB/Kc12DRceY7VS7SRM9c7MeTvKY4l3EVzv9Ynh8k/o0TFNX6mgUmkpvikXInWoRGwGPAESWrs7IJcIQVUxSvHQtyu5h/TkgVW2bwiCbA9JfGyI9Amm0STdMMZLmtIO+XvcdJv3XhyN2L9+rP3n2v4kWmohjNuPaMbgUkeEcS20ef9zZNLKBvxRGpODEW/YVQrWViWPH8LE1kKepBZLYx5ouH2xb8qEFvhlqTcLbJPMdxj5KsUVZRulTU6VPcMYJVJE5lhHwTg8avY9F/La9zZh2OkkKKLG 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 diff --git a/tools/testing/selftests/mm/migration.c b/tools/testing/selftests/mm/migration.c index 21577a84d7e4..1fb3607accab 100644 --- a/tools/testing/selftests/mm/migration.c +++ b/tools/testing/selftests/mm/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