From patchwork Thu Mar 30 16:06:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13194487 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 ED7ECC77B60 for ; Thu, 30 Mar 2023 16:07:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 877B96B007D; Thu, 30 Mar 2023 12:07:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 827BD900002; Thu, 30 Mar 2023 12:07:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6A2BE6B0080; Thu, 30 Mar 2023 12:07:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 576166B007D for ; Thu, 30 Mar 2023 12:07:12 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 0AA85160128 for ; Thu, 30 Mar 2023 16:07:12 +0000 (UTC) X-FDA: 80626043904.05.1672357 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf28.hostedemail.com (Postfix) with ESMTP id 60CD4C0013 for ; Thu, 30 Mar 2023 16:07:09 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=K9vNu+h0; spf=pass (imf28.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=1680192429; 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=7NTd9ykbxp4UXzRNrTfarBboXMMqnuQ6sOTF5uMlUjU=; b=gGE3ir17nbd4+3u23m4dX4BrFl+nK9pTJrOme9+2o175VbCRp8A/J1iBzokU2KJ88h3SmH i+IO3zYWsWN1/gQ4fsWrr70F2XLWTg+Idb0+T/hqtZ0PcYpg3oDp7ZXuw0v6QR5ozkgYmR LQJYDKYL3w6ybHwkURmNfd0miKXUyWk= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=K9vNu+h0; spf=pass (imf28.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=1680192429; a=rsa-sha256; cv=none; b=SGML90ym78LhX0jPtQrbOFrhU7TjDsdsrppL0l6XHwxCgSHzdLQVjuw2AhdawXI8uAty8u kXYn/PBtTO/wZNi6+OjlFFhB6hUlSqxwb57W452Kz4ptAzz56V+v3x6ctdrGMJzQEGF0+R KGuU96Ba6KxorxgfnEGlBpzBlU2GGDY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680192428; 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=7NTd9ykbxp4UXzRNrTfarBboXMMqnuQ6sOTF5uMlUjU=; b=K9vNu+h0weu4wtxycoRDm1FbMDNBg+4xliABLrvD/q+SdoAilw9cVWmIlrTiNX5umV/9iX kB6O3wRFavfel6HpaaO2qaf79AXVqklf7tXgy6XBnfbsGUq6SzsNW4iWQ5fho3GV6fRgkS KHGEMoObqNUgRqSnK9bjEzBtf1JfdrQ= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-308-q_aoFJLANye-7myhghhrCQ-1; Thu, 30 Mar 2023 12:07:06 -0400 X-MC-Unique: q_aoFJLANye-7myhghhrCQ-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-57c67ea348eso12975936d6.1 for ; Thu, 30 Mar 2023 09:06:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680192414; 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=7NTd9ykbxp4UXzRNrTfarBboXMMqnuQ6sOTF5uMlUjU=; b=Yej8YHJO9kZZsR+Mif7rnFOtD0UbhFWp9Qt+KVzD+yyPtsZ4u+fhYtOfG7xUHoV60o 9WFl6WYcB0ROafddQaltojCn4e17JTyQ9R+jgkwhwkrtpMpEM8dOsfrnpuDZ9ZhLp1Dy pwXlkC60sVI8updiBEz/mcCp/L6OFIZGnQlUvX7LiJq6B2IrixQge9zLaXqoKBE+oZJD mSpspG2rfkmsbUy2jKc5jOTY6vuHQKkjvSWDj04ENVp04r5+yaEIy+vGOohgJtpbYBnp sXDJCx3Ek82gOUckWx0U/QBCFNj9YBiUUwkoz8LFo2mQch998OZt1ojTbYrMr6kHvqt/ Z+CA== X-Gm-Message-State: AAQBX9d3ZKsFV9Suv5YvUy2YroPKcGblk04yljQpiSo4MkVyyq2MVxOW KVi+NNAH0814XUv6pbMHDPOwXuYvkodPqxmMQqOdjbEJ5sWwYZm3O3umevj0iEwhOqQESVSTocx xsP70AZEVa3g5QwTqEAuWXNU8ric33gWLvtyVwYEsT+kcWMGKJn0LjS72lo7I+ypSSAlP X-Received: by 2002:a05:6214:d0c:b0:5df:a693:39e with SMTP id 12-20020a0562140d0c00b005dfa693039emr3564921qvh.5.1680192414136; Thu, 30 Mar 2023 09:06:54 -0700 (PDT) X-Google-Smtp-Source: AKy350ay9foZ+7sLH0l9upG28YZMcH3wwEPMDOy35oTEC9bGWqO6YTuX4ly8yK4M9vqcaISxkOHyLQ== X-Received: by 2002:a05:6214:d0c:b0:5df:a693:39e with SMTP id 12-20020a0562140d0c00b005dfa693039emr3564872qvh.5.1680192413714; Thu, 30 Mar 2023 09:06:53 -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 jy3-20020a0562142b4300b005dd8b934595sm5541041qvb.45.2023.03.30.09.06.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Mar 2023 09:06:52 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: David Hildenbrand , Andrew Morton , Andrea Arcangeli , peterx@redhat.com, Axel Rasmussen , Mike Kravetz , Leonardo Bras Soares Passos , Mike Rapoport , Nadav Amit Subject: [PATCH 04/29] selftests/mm: Merge util.h into vm_util.h Date: Thu, 30 Mar 2023 12:06:49 -0400 Message-Id: <20230330160649.3106916-1-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230330155707.3106228-1-peterx@redhat.com> References: <20230330155707.3106228-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 60CD4C0013 X-Stat-Signature: zskwqhxuqh5axpsnjixf57tmck7b3da4 X-Rspam-User: X-HE-Tag: 1680192429-304519 X-HE-Meta: U2FsdGVkX1/SvwWzHEfNTgFQfFCRF8YL28Yle2TpOHHY7hhuf9E3eoLxHLWDeeD0823H717ltEnv7ML/Ai/EkCaVRB0Xfj7z/vvxa+i3T/xfbP4ETyWMbD2XPa0FXIlWdgPXT/Nq3Oy/JXlbB8vcpmkHdUCK8k0z34MJ2U07iAh/sisQBtrMrHJ01v02YyCQDmpn9IvY71bR2jjMvrQX601xnEJHQ48vjgJP5XPhZGUPT6JUIxo+G3HI3kmxBVHbXS3rkzqrgHrIP23ZUonhftfJIYsB2vhdnDyZRkCjX5wtt4eOoFm2tuDkI8ZOjoarMJ6NlDX7Hog+7talel6UKLvdUyt0E0P58i/+NzGmvC2WEaSKPlMq5C4VQxvMX2Dikrz7CmNL1mbqpUb9pOcgonxk4S2I4xysLypctYcghmnSndZd4NTlyvGaFZYMQ4HiiDkQmKJTQTZLfQc1wsFPZRKr9s+vpo5qFhyPmH5cmLQmsSsOzQP82QtHkqBVjMwKrxcDKfRPdrjzhHbFRGNmZMR4Basawq9k/tK1udynGSSoSIcYE/SVuVejL2hJGUKLMttr2Ab2xfbwr40EipfLPopN5a8IUQP8y/c9dvqMY9gwwpcUPIbM5V2RMmTDNiS5hq0kb7dm6CRPv4s6A7azi1fv5EzTrTQfE9/zT6d3kShmIoBdkedBhSWeDRDjkHkBCKwg1+Teyzv+/07iJ3NAFpCNPyFHgQ2De3yuzCOeNuq2GLkPtxoTm87JI3yL9ULn7aPedUzVMlr2QjwZBKEZCvimPjuq+QKd8nE6gjlydiyT3bi+wFTcboMaxWY7x9lu6WXyQjXjL4dGJFH4mSjQEMcAgFXQX+vCN7TzjyxzOf1VdQjzh4X9wPfim/KLONOhSqPNZ3cuoKfp4W6B1Gwmw6INOxEd3jLROxiCYmPNwbAB4eXfqb6HO9YoyDV5oD3/wJQq5qHImKYDx/MPSt6 lhAyCudi Z/13UuRALnD4WnlzwcfdwTUcfjHQq/0bdWh+qxtDf8a4rHZH6cxLaLohD6pomB4lJCZcPj6GiGvc8nZANeSVsjeepEMD3ulmYzIceoLrzZzNCUoP/0xR06Ia8TRT/p1echg93DwqKKhHwnHoMJrYEYwHfcPyLCwxopGhDfiBMtO31r+4K2GTi7v85lSynqMpwcEBp5S4Wm5mwq3TJNu79QPaUOso6mOsEEPyUrlNsbg7TvqYhD9IAjn6dUizU0GMlhwhet+eB3hGQRtnrLG4nxTFNonlUn44s03cIZOGoxGWtHRuN9jNMpcEUMmwDAawtBkeR4WKsKQIRhs6UglwU+YJWF7bFByTM/GeJWZvtiq0ZD77qTv0WABad39t9hPpTMZgJS9Hf/3SGK4YTOsTj89z+qXcKN/pz6mbiQor2pi41G3VXLhX0tQ+UwieL7zmXPi/pcqjlc6D9caAx/dRXypBFOCMGHXkR8De7cmBh9QFgP4sExOVChZ0phPY2Lq7ABU0trTX4C5mDPU0= 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. Signed-off-by: Peter Xu Reviewed-by: Axel Rasmussen Reviewed-by: David Hildenbrand Reviewed-by: Mike Rapoport (IBM) --- 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 fbf5646b1072..4188435967ed 100644 --- a/tools/testing/selftests/mm/Makefile +++ b/tools/testing/selftests/mm/Makefile @@ -111,6 +111,10 @@ $(OUTPUT)/madv_populate: 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 9fb21b982dc9..85a49aea3ab8 100644 --- a/tools/testing/selftests/mm/ksm_tests.c +++ b/tools/testing/selftests/mm/ksm_tests.c @@ -14,7 +14,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 40e795624ff3..0204c469be43 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))