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: 39471 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp233516wrt; Thu, 5 Jan 2023 02:39:26 -0800 (PST) X-Google-Smtp-Source: AMrXdXv5cXImIprqKqqZTMJpcsqi+za8p+1R0Lg1N7LnHwXw/C8Ty0bBD8fngfeTz3HTEri9k6Ro X-Received: by 2002:a05:6a20:13a8:b0:b0:a35:b763 with SMTP id w40-20020a056a2013a800b000b00a35b763mr81567219pzh.5.1672915165711; Thu, 05 Jan 2023 02:39:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672915165; cv=none; d=google.com; s=arc-20160816; b=n31gYKrfqn4Fr0BcRp2wpyGoBcXG/spGrUz4cxZTnn2AORZFHmavnIjbZbNUc2m9C7 ngRwQVx+GCIb2R0Z3H0t5+Vhu0V8bxszvtiIWdKDXCfeT8Uxfa3oB3pMkMK/JLUHqY/L vXYpRCDENbWMPowst7u7g6QW1PAPY14HaGIx0KJrt/CWj6syLJH3jAN7WFGCpvvL45Bc s5x+Whn3N0E6D0trirgct+M8Yb+VG7+oKcnaj9rhZq2nsngg6jsB4/KUcLbm45iT6Ryb 8rUlRvJ4WtsD60HqLYd5hFOtV7TEJN1oYiFQR/2G8rPlYyl+GE6D3sYztyt5YC41gNyw W4JA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=rtdl4oKQo+XEbgxtU4YTxEZN3Xtv3pieSBor01lKxpM=; b=cSnww1TfW/sLVjuwvPMzkrRYgmtNMCCWCPJpXmpYxTGH4l+q//yfqt89oxsC+CxV79 Mt/P/1T4X7f1gLrf7elAY9fTLXS914XBJjk25cLXuO7Mxlz04Dppu/Jg5CWvWicY5+xT 9z71NIE7LuH1ckzOa1FgugP0kXwqs7Y7/dhxxTy2Mk1QY14/N1WFWJOBybgnBGfZ5Wuc ULthNwC6QPuFcd+2yqi1Yj5nLcRlfTXJIwWj7g1JZ7jtkCI0eIsE5hdWt418gdWgdNcm BzpgOipjIUShr0oAncCDIJi75kFmo0affWkNhL6tAsUDpWmCxNUOO5R9J9fW/pz3RPkO wwTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=JoK7U3yU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a22-20020a656416000000b004783935de72si40138337pgv.45.2023.01.05.02.39.13; Thu, 05 Jan 2023 02:39:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=JoK7U3yU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232942AbjAEKXl (ORCPT + 99 others); Thu, 5 Jan 2023 05:23:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233100AbjAEKV4 (ORCPT ); Thu, 5 Jan 2023 05:21:56 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE72550E6B for ; Thu, 5 Jan 2023 02:20:01 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id v9-20020a259d89000000b007b515f139e0so1270719ybp.17 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=CKFsRSYXjKk7ZMF12qnZxr7w33qTpLv3YyMsd+ttfA9/6lNamU/cHR2u4eQNW2atDT eq8kY6+gfP/hhlN8NkOVnomIDseZWIzR03CjuDwDeECo/Zo20zEnYuMD4Vn8EVHlgIlC RuRoC0eLmea8m4XBFOt7ls8Y3dy7/L/eV02lBSyaK32OTuA96wLg4SEYYVvHKA41MLb3 /0IoD+naU3H7gE+ImDzjCCNllNXwEg0vxjxzaTfWaNa+ZyrcbA9n8RU8QXfdCcEX7C/4 QJeBYm7+mFHS7RlSB0e7iOWqH97I0nLS5PEZleSQSoDexz/+mo9wCIL/2qVtvmdMDgXX BaYA== X-Gm-Message-State: AFqh2krwlXrxhChOjLQnbnAJsyVPsnLqdv/pHXy5zvnAzeika3tnCUF6 VPiIoIOGYVvyizFLF+Fk475yOpMpulcy8Uh1 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-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754178692336343364?= X-GMAIL-MSGID: =?utf-8?q?1754178692336343364?= 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