From patchwork Wed Apr 12 16:41:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13209340 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 DCC2BC77B6E for ; Wed, 12 Apr 2023 16:41:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 54C7C280002; Wed, 12 Apr 2023 12:41:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4FC68280001; Wed, 12 Apr 2023 12:41:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 39D03280002; Wed, 12 Apr 2023 12:41:33 -0400 (EDT) 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 28C3E280001 for ; Wed, 12 Apr 2023 12:41:33 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id DD3FB1201AA for ; Wed, 12 Apr 2023 16:41:32 +0000 (UTC) X-FDA: 80673304824.07.B4CC854 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf02.hostedemail.com (Postfix) with ESMTP id CF6B680024 for ; Wed, 12 Apr 2023 16:41:30 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="KB+9/CEO"; spf=pass (imf02.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681317690; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=wXTqxDSTsrRiAkVZ6NC5Z+aMCLpRXkWfeIKb5BVB8IM=; b=kbNF3DjzoaAl2F8mQBhLTQJ0NumoZt7Xpeu3OZJGto/1X5/Ul4MNGsGCdDsQ8pwbJHgnau SJ1URei/9x2/ofeV1CZuHfGq1gzWP90FxPruvewqLFCgCwV3x09xMQhmIt8cxfe/roxAAM Gw4Yv/dK5pVpemOth17s4mTIE3gtpg8= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="KB+9/CEO"; spf=pass (imf02.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681317690; a=rsa-sha256; cv=none; b=2B71TKAqhdUSG8ssc03UP2gbpQNt40/3hoQm/ngQVu/knR8lYnxpSHZjY5y8QYbpCTXVk8 7qNrnDcvKQES/0hL8+5cjpvdmI51+tMEy62nzdT1U0yE1hMaatY0rxYVpeePYVP+ln4lHr 0t4CCvV/K376FpejWFKyMwWfOLGGsOk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681317690; h=from:from: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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wXTqxDSTsrRiAkVZ6NC5Z+aMCLpRXkWfeIKb5BVB8IM=; b=KB+9/CEOoHrCrP8imfN76Jj8Rdeh3DAQAD55o6408eRCFIvRh4atw2UaU24q7y6YxtuQjA uxdG5afDPRj8z55wNHEPIijQ8xeg0b3NaA+81F96X1B85pHoe+/8brceoUpHJS1PPHbsOx +52g5zdc73eGlrsUI0RpMz+QksaiEYA= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-262-WkMgNWWtN2-mxUmS4tsiUw-1; Wed, 12 Apr 2023 12:41:29 -0400 X-MC-Unique: WkMgNWWtN2-mxUmS4tsiUw-1 Received: by mail-qk1-f197.google.com with SMTP id af79cd13be357-74a90355636so62095885a.0 for ; Wed, 12 Apr 2023 09:41:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681317685; x=1683909685; 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=wXTqxDSTsrRiAkVZ6NC5Z+aMCLpRXkWfeIKb5BVB8IM=; b=WsFo/W0kCs7xAVp58Z0eHkQ2A2x1tyzR+W74ue45QAYq1OfHq5GmzyJ3aln3wmuCF5 OwyjYFOnXtD6O39jlNetu7QSpO/a72zy8MlquX0tm+51cL+RTafzqR444Zdtd/NSnuh4 NK53aq+b4n1odkJQUupn1/wT64fgjM6mmVDeUtl36MnfN3hDZ4d760EsANOYLdv7NX6a v623T0rdAdPSq9SIJZWMa5ihSTb9wi4GeaFR8vRc2/eWwJ2Qn49YxVQSHiZicsuPKczT /9156MDK8uaa6VSHpMOVRlBzBavXtPbRHtbAF/r4frjF0R/aUbeCYM4LmFI+EDsZPM1T 1Bpw== X-Gm-Message-State: AAQBX9dsQ+4eeb8ESvd2GBjqB7t815o4di6Hw/TbKa0KDMOIQ/V7e7DZ xruYPAhv7N1gWB0qtpbLuHkDuN2vuofo3f+GUSMRUPJh0QiM+RzpNOUIFBgpFMDiIwqfuWNnOG3 CGAJ19oQ9V0Q= X-Received: by 2002:ad4:5de6:0:b0:5da:b965:1efd with SMTP id jn6-20020ad45de6000000b005dab9651efdmr27656361qvb.2.1681317684872; Wed, 12 Apr 2023 09:41:24 -0700 (PDT) X-Google-Smtp-Source: AKy350ajUEBNrrqlo7NLbMBFZBxhABZNBWjNLqBWj6Qhq7WnBXzPQ9lMhNrGPdDfuPO/IRJuRxGCtQ== X-Received: by 2002:ad4:5de6:0:b0:5da:b965:1efd with SMTP id jn6-20020ad45de6000000b005dab9651efdmr27656333qvb.2.1681317684546; Wed, 12 Apr 2023 09:41:24 -0700 (PDT) Received: from x1n.redhat.com (bras-base-aurron9127w-grc-40-70-52-229-124.dsl.bell.ca. [70.52.229.124]) by smtp.gmail.com with ESMTPSA id m10-20020ad44b6a000000b005e1075e21b7sm5013110qvx.15.2023.04.12.09.41.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Apr 2023 09:41:23 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Axel Rasmussen , David Hildenbrand , Andrea Arcangeli , Mike Rapoport , Nadav Amit , peterx@redhat.com, Mike Kravetz , Andrew Morton , Leonardo Bras Soares Passos , Mike Rapoport Subject: [PATCH v2 04/31] selftests/mm: Merge util.h into vm_util.h Date: Wed, 12 Apr 2023 12:41:20 -0400 Message-Id: <20230412164120.327731-1-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230412163922.327282-1-peterx@redhat.com> References: <20230412163922.327282-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: CF6B680024 X-Stat-Signature: 9i4qxuk4nfku6smbfurmk4xfyi1ne6ac X-HE-Tag: 1681317690-152722 X-HE-Meta: U2FsdGVkX18Uk1E+XG81GpL+/3f2/nUD+7Zl6y6INdbJWNn5qZxX6lhUtUK3ckScRm2Mdkll82mxp5OtJUsTQKkEpAi0xMwe6ICtc8b4twDMtQO9KhIKcXFhzsQUnAaAVmEFdNyLPneK++coxB+/znXkYxtjwW4SwcDyNkdfkhLvLGVvxnlFnJK883UIC4KQNIjFjDzuE/yMBy1uCZ916tNQ+BrsSu9v3lZdz7b8IulEK8tYdlr4PKdKAXyRd2tRR8wNlc7uJZ/Th8fv7iFR8i8sdB+DEbhsMMTw4lYsND6SLFdvaCZmdlQMd6L7WCCXvw82XgPJ22mRr/Ncv01pfNhnDTjsftZlopG7liNugID6TzgIyq6ZlDupNUyH95kGJVgi8tDQmz86KtNIBXH/SAXgX6gjL4TAiO254hcnm0kzYT1M/Wd5A990VmqssjPAOpRkcBcKbpBeCAYxnSJ80DgsGhhFNgI5YaCS+Xf01WY2xmqXJ8y+/9nF/QZBkCoVQFT5QnCMd8p02tI+HC/EEHEtP0mHJBKq/b36LhBap6qjtL/EkzSOF8Ikp+cunNfMtm6wuOdrbKNnL5YDOpZrFQC2SxdyUpeqAlCwJeiM6gUEnfMhfHieUQWzXBcVdX6GQu3Vd0D+KITD3p3f7xci2ysCINTwAl3j3GxxQTMYyHDFzkCMqg0xSrVIpmZy4vVktHZsWi4tJzMfW6+A0MX82Py2yUnNEEuGXV7hesi54pFo9I6GLpXZoY+hxH/OcHR1aCipEj7rWusC8RguoNs+4I5hBHFLft+VSE7enxhD6py8OBahUanbcOA8i4UdREUK8Vr9eJ8otsin/Sda8h1CfhhbkGs9iNoqAaSc1cz0oTgCMwTD6oAkDBwIyJe1ov+awPlB3a4ypTox9Zk+B0lf4ZTHGJLH0HJesB/47itMgn3XuPMfQD5aqqRRIbzoi7fEmOKETGoHREs68GiB/aL qqC1+CWy cBKsgeLbzBXhIbCTPoQBTZ8vLOwisElVqrUB6/r//Ye7T9n7Zw7AkMlzkDGPFHzESvDHcY3IRPuQeS4F5DLWpbn3OQy02FZtdZdKli7U92YlfmAzxWy8ZFH2ArQgeI7ciNm8DjdjU1/0abbh2d4fZ7YbBFSyEOyQwcq1rTq4zpvOLv6eNtaYSqnnAY72mSEOXAMaKpk2BflbuX0NpuklQKNuKpo5Uv6BsoRL3DqdgDTf9XEic0mkkRr12+FzYZ2A7UyRfGN0+4tt6ThMno28xzM4N5wUgpuL2IwgwYqyEcUqT2JY8J2LatcdRWoJtNsV1zCXWOmgSz9uU/Yj8RZAKlBcm2vTQMj8+J2zLvULJthN132A/ERvaSMAjoERmJ73GRYFB6xj1ijYFuF/up38GjevH/2qoy6WuirsL1aXM7D/4l8B1wO7EYvW9G5fkOINmTHx5M4rVB0n5n1jivAl5FUxbf7khMdasPPRfBktRMYV6kaQzLvBzEpi4lncuIUbSjNFIqK05A2E0cv6HsaMtJ6h5VRUD+p4uJ9cMULBnEAcIgg6R32tcJKNOJWU9MjsYRo/SN1EiFBXi1SlHMutO/DhxXfhXTLv/piIF 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: There're two util headers under mm/ kselftest. Merge one with another. It turns out util.h is the easy one to move. When merging, drop PAGE_SIZE / PAGE_SHIFT because they're unnecessary wrappers to page_size() / page_shift(), meanwhile rename them to psize() and pshift() so as to not conflict with some existing definitions in some test files that includes vm_util.h. Reviewed-by: Axel Rasmussen Reviewed-by: David Hildenbrand Reviewed-by: Mike Rapoport (IBM) Signed-off-by: Peter Xu --- tools/testing/selftests/mm/Makefile | 4 ++ tools/testing/selftests/mm/gup_test.c | 5 +- tools/testing/selftests/mm/ksm_tests.c | 2 +- tools/testing/selftests/mm/mrelease_test.c | 11 ++- tools/testing/selftests/mm/transhuge-stress.c | 12 ++-- tools/testing/selftests/mm/util.h | 69 ------------------- tools/testing/selftests/mm/vm_util.c | 31 +++++++++ tools/testing/selftests/mm/vm_util.h | 31 +++++++++ 8 files changed, 80 insertions(+), 85 deletions(-) delete mode 100644 tools/testing/selftests/mm/util.h diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile index 8235dddbbbc6..9d9822b4bf24 100644 --- a/tools/testing/selftests/mm/Makefile +++ b/tools/testing/selftests/mm/Makefile @@ -113,6 +113,10 @@ $(OUTPUT)/mkdirty: vm_util.c $(OUTPUT)/soft-dirty: vm_util.c $(OUTPUT)/split_huge_page_test: vm_util.c $(OUTPUT)/userfaultfd: vm_util.c +$(OUTPUT)/gup_test: vm_util.c +$(OUTPUT)/mrelease_test: vm_util.c +$(OUTPUT)/transhuge-stress: vm_util.c +$(OUTPUT)/ksm_tests: vm_util.c ifeq ($(MACHINE),x86_64) BINARIES_32 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_32)) diff --git a/tools/testing/selftests/mm/gup_test.c b/tools/testing/selftests/mm/gup_test.c index e43879291dac..ec2229136384 100644 --- a/tools/testing/selftests/mm/gup_test.c +++ b/tools/testing/selftests/mm/gup_test.c @@ -12,8 +12,7 @@ #include #include #include "../kselftest.h" - -#include "util.h" +#include "vm_util.h" #define MB (1UL << 20) @@ -251,7 +250,7 @@ int main(int argc, char **argv) if (touch) { gup.gup_flags |= FOLL_TOUCH; } else { - for (; (unsigned long)p < gup.addr + size; p += PAGE_SIZE) + for (; (unsigned long)p < gup.addr + size; p += psize()) p[0] = 0; } diff --git a/tools/testing/selftests/mm/ksm_tests.c b/tools/testing/selftests/mm/ksm_tests.c index f9eb4d67e0dd..74281593a124 100644 --- a/tools/testing/selftests/mm/ksm_tests.c +++ b/tools/testing/selftests/mm/ksm_tests.c @@ -12,7 +12,7 @@ #include "../kselftest.h" #include -#include "util.h" +#include "vm_util.h" #define KSM_SYSFS_PATH "/sys/kernel/mm/ksm/" #define KSM_FP(s) (KSM_SYSFS_PATH s) diff --git a/tools/testing/selftests/mm/mrelease_test.c b/tools/testing/selftests/mm/mrelease_test.c index 6c62966ab5db..37b6d33b9e84 100644 --- a/tools/testing/selftests/mm/mrelease_test.c +++ b/tools/testing/selftests/mm/mrelease_test.c @@ -9,8 +9,7 @@ #include #include #include - -#include "util.h" +#include "vm_util.h" #include "../kselftest.h" @@ -32,7 +31,7 @@ static int alloc_noexit(unsigned long nr_pages, int pipefd) unsigned long i; char *buf; - buf = (char *)mmap(NULL, nr_pages * PAGE_SIZE, PROT_READ | PROT_WRITE, + buf = (char *)mmap(NULL, nr_pages * psize(), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, 0, 0); if (buf == MAP_FAILED) { perror("mmap failed, halting the test"); @@ -40,7 +39,7 @@ static int alloc_noexit(unsigned long nr_pages, int pipefd) } for (i = 0; i < nr_pages; i++) - *((unsigned long *)(buf + (i * PAGE_SIZE))) = i; + *((unsigned long *)(buf + (i * psize()))) = i; /* Signal the parent that the child is ready */ if (write(pipefd, "", 1) < 0) { @@ -54,7 +53,7 @@ static int alloc_noexit(unsigned long nr_pages, int pipefd) timeout--; } - munmap(buf, nr_pages * PAGE_SIZE); + munmap(buf, nr_pages * psize()); return (timeout > 0) ? KSFT_PASS : KSFT_FAIL; } @@ -87,7 +86,7 @@ static int child_main(int pipefd[], size_t size) /* Allocate and fault-in memory and wait to be killed */ close(pipefd[0]); - res = alloc_noexit(MB(size) / PAGE_SIZE, pipefd[1]); + res = alloc_noexit(MB(size) / psize(), pipefd[1]); close(pipefd[1]); return res; } diff --git a/tools/testing/selftests/mm/transhuge-stress.c b/tools/testing/selftests/mm/transhuge-stress.c index e3f00adb1b82..ba9d37ad3a89 100644 --- a/tools/testing/selftests/mm/transhuge-stress.c +++ b/tools/testing/selftests/mm/transhuge-stress.c @@ -15,7 +15,7 @@ #include #include #include -#include "util.h" +#include "vm_util.h" int backing_fd = -1; int mmap_flags = MAP_ANONYMOUS | MAP_NORESERVE | MAP_PRIVATE; @@ -34,10 +34,10 @@ int main(int argc, char **argv) int pagemap_fd; ram = sysconf(_SC_PHYS_PAGES); - if (ram > SIZE_MAX / sysconf(_SC_PAGESIZE) / 4) + if (ram > SIZE_MAX / psize() / 4) ram = SIZE_MAX / 4; else - ram *= sysconf(_SC_PAGESIZE); + ram *= psize(); len = ram; while (++i < argc) { @@ -58,7 +58,7 @@ int main(int argc, char **argv) warnx("allocate %zd transhuge pages, using %zd MiB virtual memory" " and %zd MiB of ram", len >> HPAGE_SHIFT, len >> 20, - ram >> (20 + HPAGE_SHIFT - PAGE_SHIFT - 1)); + ram >> (20 + HPAGE_SHIFT - pshift() - 1)); pagemap_fd = open("/proc/self/pagemap", O_RDONLY); if (pagemap_fd < 0) @@ -92,7 +92,7 @@ int main(int argc, char **argv) if (pfn < 0) { nr_failed++; } else { - size_t idx = pfn >> (HPAGE_SHIFT - PAGE_SHIFT); + size_t idx = pfn >> (HPAGE_SHIFT - pshift()); nr_succeed++; if (idx >= map_len) { @@ -108,7 +108,7 @@ int main(int argc, char **argv) } /* split transhuge page, keep last page */ - if (madvise(p, HPAGE_SIZE - PAGE_SIZE, MADV_DONTNEED)) + if (madvise(p, HPAGE_SIZE - psize(), MADV_DONTNEED)) err(2, "MADV_DONTNEED"); } clock_gettime(CLOCK_MONOTONIC, &b); diff --git a/tools/testing/selftests/mm/util.h b/tools/testing/selftests/mm/util.h deleted file mode 100644 index b27d26199334..000000000000 --- a/tools/testing/selftests/mm/util.h +++ /dev/null @@ -1,69 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ - -#ifndef __KSELFTEST_VM_UTIL_H -#define __KSELFTEST_VM_UTIL_H - -#include -#include -#include -#include /* ffsl() */ -#include /* _SC_PAGESIZE */ - -static unsigned int __page_size; -static unsigned int __page_shift; - -static inline unsigned int page_size(void) -{ - if (!__page_size) - __page_size = sysconf(_SC_PAGESIZE); - return __page_size; -} - -static inline unsigned int page_shift(void) -{ - if (!__page_shift) - __page_shift = (ffsl(page_size()) - 1); - return __page_shift; -} - -#define PAGE_SHIFT (page_shift()) -#define PAGE_SIZE (page_size()) -/* - * On ppc64 this will only work with radix 2M hugepage size - */ -#define HPAGE_SHIFT 21 -#define HPAGE_SIZE (1 << HPAGE_SHIFT) - -#define PAGEMAP_PRESENT(ent) (((ent) & (1ull << 63)) != 0) -#define PAGEMAP_PFN(ent) ((ent) & ((1ull << 55) - 1)) - - -static inline int64_t allocate_transhuge(void *ptr, int pagemap_fd) -{ - uint64_t ent[2]; - - /* drop pmd */ - if (mmap(ptr, HPAGE_SIZE, PROT_READ | PROT_WRITE, - MAP_FIXED | MAP_ANONYMOUS | - MAP_NORESERVE | MAP_PRIVATE, -1, 0) != ptr) - errx(2, "mmap transhuge"); - - if (madvise(ptr, HPAGE_SIZE, MADV_HUGEPAGE)) - err(2, "MADV_HUGEPAGE"); - - /* allocate transparent huge page */ - *(volatile void **)ptr = ptr; - - if (pread(pagemap_fd, ent, sizeof(ent), - (uintptr_t)ptr >> (PAGE_SHIFT - 3)) != sizeof(ent)) - err(2, "read pagemap"); - - if (PAGEMAP_PRESENT(ent[0]) && PAGEMAP_PRESENT(ent[1]) && - PAGEMAP_PFN(ent[0]) + 1 == PAGEMAP_PFN(ent[1]) && - !(PAGEMAP_PFN(ent[0]) & ((1 << (HPAGE_SHIFT - PAGE_SHIFT)) - 1))) - return PAGEMAP_PFN(ent[0]); - - return -1; -} - -#endif diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c index 8dc74dd022c2..85411ee7ff8b 100644 --- a/tools/testing/selftests/mm/vm_util.c +++ b/tools/testing/selftests/mm/vm_util.c @@ -8,6 +8,9 @@ #define SMAP_FILE_PATH "/proc/self/smaps" #define MAX_LINE_LENGTH 500 +unsigned int __page_size; +unsigned int __page_shift; + uint64_t pagemap_get_entry(int fd, char *start) { const unsigned long pfn = (unsigned long)start / getpagesize(); @@ -149,3 +152,31 @@ bool check_huge_shmem(void *addr, int nr_hpages, uint64_t hpage_size) { return __check_huge(addr, "ShmemPmdMapped:", nr_hpages, hpage_size); } + +int64_t allocate_transhuge(void *ptr, int pagemap_fd) +{ + uint64_t ent[2]; + + /* drop pmd */ + if (mmap(ptr, HPAGE_SIZE, PROT_READ | PROT_WRITE, + MAP_FIXED | MAP_ANONYMOUS | + MAP_NORESERVE | MAP_PRIVATE, -1, 0) != ptr) + errx(2, "mmap transhuge"); + + if (madvise(ptr, HPAGE_SIZE, MADV_HUGEPAGE)) + err(2, "MADV_HUGEPAGE"); + + /* allocate transparent huge page */ + *(volatile void **)ptr = ptr; + + if (pread(pagemap_fd, ent, sizeof(ent), + (uintptr_t)ptr >> (pshift() - 3)) != sizeof(ent)) + err(2, "read pagemap"); + + if (PAGEMAP_PRESENT(ent[0]) && PAGEMAP_PRESENT(ent[1]) && + PAGEMAP_PFN(ent[0]) + 1 == PAGEMAP_PFN(ent[1]) && + !(PAGEMAP_PFN(ent[0]) & ((1 << (HPAGE_SHIFT - pshift())) - 1))) + return PAGEMAP_PFN(ent[0]); + + return -1; +} diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h index 1995ee911ef2..6edeb531afc6 100644 --- a/tools/testing/selftests/mm/vm_util.h +++ b/tools/testing/selftests/mm/vm_util.h @@ -1,6 +1,27 @@ /* SPDX-License-Identifier: GPL-2.0 */ #include #include +#include +#include +#include /* ffsl() */ +#include /* _SC_PAGESIZE */ + +extern unsigned int __page_size; +extern unsigned int __page_shift; + +static inline unsigned int psize(void) +{ + if (!__page_size) + __page_size = sysconf(_SC_PAGESIZE); + return __page_size; +} + +static inline unsigned int pshift(void) +{ + if (!__page_shift) + __page_shift = (ffsl(psize()) - 1); + return __page_shift; +} uint64_t pagemap_get_entry(int fd, char *start); bool pagemap_is_softdirty(int fd, char *start); @@ -13,3 +34,13 @@ uint64_t read_pmd_pagesize(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); +int64_t allocate_transhuge(void *ptr, int pagemap_fd); + +/* + * On ppc64 this will only work with radix 2M hugepage size + */ +#define HPAGE_SHIFT 21 +#define HPAGE_SIZE (1 << HPAGE_SHIFT) + +#define PAGEMAP_PRESENT(ent) (((ent) & (1ull << 63)) != 0) +#define PAGEMAP_PFN(ent) ((ent) & ((1ull << 55) - 1))