From patchwork Fri Feb 11 06:33:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Aneesh Kumar K.V" X-Patchwork-Id: 12742904 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 B3804C433EF for ; Fri, 11 Feb 2022 06:33:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EE1B66B0073; Fri, 11 Feb 2022 01:33:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E911B6B0074; Fri, 11 Feb 2022 01:33:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D59F66B0075; Fri, 11 Feb 2022 01:33:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0062.hostedemail.com [216.40.44.62]) by kanga.kvack.org (Postfix) with ESMTP id C7CD46B0073 for ; Fri, 11 Feb 2022 01:33:57 -0500 (EST) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 86A8E180949DB for ; Fri, 11 Feb 2022 06:33:57 +0000 (UTC) X-FDA: 79129533714.24.A8C5846 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by imf27.hostedemail.com (Postfix) with ESMTP id CFBD140005 for ; Fri, 11 Feb 2022 06:33:56 +0000 (UTC) Received: from pps.filterd (m0098409.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 21B4SKpA014983; Fri, 11 Feb 2022 06:33:45 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding; s=pp1; bh=nOp21wqH4SYh3homqaEaiEvpvy0uUYsdNREK04SxuTs=; b=MDep5ljAWb8ldrgZ0oTgHsjeczxnZJA0FibqE6LqKPwWMY5vruUCNGHWlE0tg4183PuL r44XYfwtJfgkwtG4yWZtHMM5WZLXipuLUNMNtuwjPeu2emWkE8FllxeeoFOh/b5NVqOA b0Xd2tMK4wxXAXVmRr+lQn/t1Tzncf3UowSESYvtDZE04NOWxOREgJyC24zw3B4hxz9y ot2uApkTa8+CEcEUEaP6kqnNmt8IMUzdf3BEUhbne58YC/arwBYdmwuO93Sr7irwWyt2 5i67ucvUK1Yp8pEDGXhikdLyRBbKIihZJzd/NggMtTRiFsDlWX8iOYizlu9xehNIvnpz sg== Received: from ppma02dal.us.ibm.com (a.bd.3ea9.ip4.static.sl-reverse.com [169.62.189.10]) by mx0a-001b2d01.pphosted.com with ESMTP id 3e5gt81tds-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 11 Feb 2022 06:33:45 +0000 Received: from pps.filterd (ppma02dal.us.ibm.com [127.0.0.1]) by ppma02dal.us.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 21B6XQnv014417; Fri, 11 Feb 2022 06:33:44 GMT Received: from b03cxnp07027.gho.boulder.ibm.com (b03cxnp07027.gho.boulder.ibm.com [9.17.130.14]) by ppma02dal.us.ibm.com with ESMTP id 3e3gq1nqa2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 11 Feb 2022 06:33:44 +0000 Received: from b03ledav002.gho.boulder.ibm.com (b03ledav002.gho.boulder.ibm.com [9.17.130.233]) by b03cxnp07027.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 21B6XhST27132278 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 11 Feb 2022 06:33:43 GMT Received: from b03ledav002.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7237613605D; Fri, 11 Feb 2022 06:33:43 +0000 (GMT) Received: from b03ledav002.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 91CE8136051; Fri, 11 Feb 2022 06:33:40 +0000 (GMT) Received: from skywalker.ibmuc.com (unknown [9.43.41.36]) by b03ledav002.gho.boulder.ibm.com (Postfix) with ESMTP; Fri, 11 Feb 2022 06:33:40 +0000 (GMT) From: "Aneesh Kumar K.V" To: linux-mm@kvack.org, akpm@linux-foundation.org Cc: mpe@ellerman.id.au, linuxppc-dev@lists.ozlabs.org, "Aneesh Kumar K.V" , Shuah Khan , linux-kselftest@vger.kernel.org Subject: [PATCH v2 1/2] selftest/vm: Use correct PAGE_SHIFT value for ppc64 Date: Fri, 11 Feb 2022 12:03:28 +0530 Message-Id: <20220211063330.99648-1-aneesh.kumar@linux.ibm.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: eMie-krhdorMG-hQI764di1Bfr0l3ebj X-Proofpoint-ORIG-GUID: eMie-krhdorMG-hQI764di1Bfr0l3ebj X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.816,Hydra:6.0.425,FMLib:17.11.62.513 definitions=2022-02-11_02,2022-02-09_01,2021-12-02_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 impostorscore=0 mlxscore=0 spamscore=0 priorityscore=1501 mlxlogscore=924 bulkscore=0 clxscore=1011 lowpriorityscore=0 suspectscore=0 malwarescore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2202110036 X-Rspamd-Queue-Id: CFBD140005 X-Rspam-User: Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=ibm.com header.s=pp1 header.b=MDep5ljA; spf=pass (imf27.hostedemail.com: domain of aneesh.kumar@linux.ibm.com designates 148.163.156.1 as permitted sender) smtp.mailfrom=aneesh.kumar@linux.ibm.com; dmarc=pass (policy=none) header.from=ibm.com X-Stat-Signature: pd4wqrihnm9g3kbhbiyaqw6t8nrdjrji X-Rspamd-Server: rspam11 X-HE-Tag: 1644561236-706903 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: Keep it simple by using a #define and limiting hugepage size to 2M. This keeps the test simpler instead of dynamically finding the page size and huge page size. Without this tests are broken w.r.t reading /proc/self/pagemap if (pread(pagemap_fd, ent, sizeof(ent), (uintptr_t)ptr >> (PAGE_SHIFT - 3)) != sizeof(ent)) err(2, "read pagemap"); Cc: Shuah Khan Cc: linux-kselftest@vger.kernel.org Signed-off-by: Aneesh Kumar K.V Signed-off-by: Mike Rapoport --- tools/testing/selftests/vm/ksm_tests.c | 9 ++++++++- tools/testing/selftests/vm/transhuge-stress.c | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/vm/ksm_tests.c b/tools/testing/selftests/vm/ksm_tests.c index 1436e1a9a3d3..cae72872152b 100644 --- a/tools/testing/selftests/vm/ksm_tests.c +++ b/tools/testing/selftests/vm/ksm_tests.c @@ -22,7 +22,14 @@ #define KSM_MERGE_ACROSS_NODES_DEFAULT true #define MB (1ul << 20) -#define PAGE_SHIFT 12 +#ifdef __powerpc64__ +#define PAGE_SHIFT 16 +#else +#define PAGE_SHIFT 12 +#endif +/* + * On ppc64 this will only work with radix 2M hugepage size + */ #define HPAGE_SHIFT 21 #define PAGE_SIZE (1 << PAGE_SHIFT) diff --git a/tools/testing/selftests/vm/transhuge-stress.c b/tools/testing/selftests/vm/transhuge-stress.c index 5e4c036f6ad3..b1f8d98355c5 100644 --- a/tools/testing/selftests/vm/transhuge-stress.c +++ b/tools/testing/selftests/vm/transhuge-stress.c @@ -16,7 +16,14 @@ #include #include -#define PAGE_SHIFT 12 +#ifdef __powerpc64__ +#define PAGE_SHIFT 16 +#else +#define PAGE_SHIFT 12 +#endif +/* + * On ppc64 this will only work with radix 2M hugepage size + */ #define HPAGE_SHIFT 21 #define PAGE_SIZE (1 << PAGE_SHIFT) From patchwork Fri Feb 11 06:33:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Aneesh Kumar K.V" X-Patchwork-Id: 12742905 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 CE14AC433F5 for ; Fri, 11 Feb 2022 06:35:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2D2AB6B0073; Fri, 11 Feb 2022 01:35:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 282986B0074; Fri, 11 Feb 2022 01:35:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 14A536B0075; Fri, 11 Feb 2022 01:35:14 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0060.hostedemail.com [216.40.44.60]) by kanga.kvack.org (Postfix) with ESMTP id 076046B0073 for ; Fri, 11 Feb 2022 01:35:14 -0500 (EST) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id B37948DF66 for ; Fri, 11 Feb 2022 06:35:13 +0000 (UTC) X-FDA: 79129536906.26.DCF4F17 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by imf17.hostedemail.com (Postfix) with ESMTP id 353A24000B for ; Fri, 11 Feb 2022 06:35:13 +0000 (UTC) Received: from pps.filterd (m0098421.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 21B6PJJD011241; Fri, 11 Feb 2022 06:35:02 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=SWc9HmSHc793CMFIC2l4bwVO7HD12ma7vyanF9gnhcM=; b=pQKc8/LOC3p2DkWRpXR2OhsWuoiT4HzgcOQQIRjVUEp03TRGJP/q4ZNxJBcXm7mMjWpU WgsE4CH06nG2GfJm+3dI3jhwvWpy3NK5yTPCxQnx01VIbTkyhuy8Bc6vPOzNxRhuOR49 o8+YLKv5K10yjFVUMFoBFpcE05FNNGrcTzDuV/k9NxCTUPBbuJNCA3ktOYJpAB7vXudH TuyHAKfr5biXLA0j+54amjgA0FwytQcbumpefSqccnVGp0zEcrXSMITxBAwt/Spppzbt DTd1o9ifxrtIfL31wk9YtRElocjx1WfaUeBtL+tM43+c1kcERAFT55G2pGJtHPBgbZ7K kg== Received: from ppma02dal.us.ibm.com (a.bd.3ea9.ip4.static.sl-reverse.com [169.62.189.10]) by mx0a-001b2d01.pphosted.com with ESMTP id 3e5b38q8ca-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 11 Feb 2022 06:35:01 +0000 Received: from pps.filterd (ppma02dal.us.ibm.com [127.0.0.1]) by ppma02dal.us.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 21B6XQpT014417; Fri, 11 Feb 2022 06:35:01 GMT Received: from b03cxnp07027.gho.boulder.ibm.com (b03cxnp07027.gho.boulder.ibm.com [9.17.130.14]) by ppma02dal.us.ibm.com with ESMTP id 3e3gq1nqyn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 11 Feb 2022 06:35:01 +0000 Received: from b03ledav002.gho.boulder.ibm.com (b03ledav002.gho.boulder.ibm.com [9.17.130.233]) by b03cxnp07027.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 21B6Z0nq32964884 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 11 Feb 2022 06:35:00 GMT Received: from b03ledav002.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id ED01C13605E; Fri, 11 Feb 2022 06:34:59 +0000 (GMT) Received: from b03ledav002.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7D558136053; Fri, 11 Feb 2022 06:34:57 +0000 (GMT) Received: from skywalker.ibmuc.com (unknown [9.43.41.36]) by b03ledav002.gho.boulder.ibm.com (Postfix) with ESMTP; Fri, 11 Feb 2022 06:34:57 +0000 (GMT) From: "Aneesh Kumar K.V" To: linux-mm@kvack.org, akpm@linux-foundation.org Cc: mpe@ellerman.id.au, linuxppc-dev@lists.ozlabs.org, "Aneesh Kumar K.V" , Shuah Khan , linux-kselftest@vger.kernel.org Subject: [PATCH v2 2/2] selftest/vm: Add util.h and and move helper functions there Date: Fri, 11 Feb 2022 12:03:29 +0530 Message-Id: <20220211063330.99648-2-aneesh.kumar@linux.ibm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220211063330.99648-1-aneesh.kumar@linux.ibm.com> References: <20220211063330.99648-1-aneesh.kumar@linux.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: H6QsVuT_qpeFL_jvK7a5VVGJZPAAHqNW X-Proofpoint-ORIG-GUID: H6QsVuT_qpeFL_jvK7a5VVGJZPAAHqNW X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.816,Hydra:6.0.425,FMLib:17.11.62.513 definitions=2022-02-11_02,2022-02-09_01,2021-12-02_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 phishscore=0 bulkscore=0 spamscore=0 suspectscore=0 impostorscore=0 clxscore=1015 priorityscore=1501 mlxscore=0 malwarescore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2202110036 X-Rspam-User: X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 353A24000B X-Stat-Signature: 7p9f1e63whsb91zfho5bsfji8181tf6g Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=ibm.com header.s=pp1 header.b="pQKc8/LO"; spf=pass (imf17.hostedemail.com: domain of aneesh.kumar@linux.ibm.com designates 148.163.158.5 as permitted sender) smtp.mailfrom=aneesh.kumar@linux.ibm.com; dmarc=pass (policy=none) header.from=ibm.com X-HE-Tag: 1644561313-826596 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: Avoid code duplication by adding util.h Cc: Shuah Khan Cc: linux-kselftest@vger.kernel.org Signed-off-by: Aneesh Kumar K.V --- tools/testing/selftests/vm/ksm_tests.c | 45 +--------------- tools/testing/selftests/vm/transhuge-stress.c | 49 ++--------------- tools/testing/selftests/vm/util.h | 54 +++++++++++++++++++ 3 files changed, 58 insertions(+), 90 deletions(-) create mode 100644 tools/testing/selftests/vm/util.h diff --git a/tools/testing/selftests/vm/ksm_tests.c b/tools/testing/selftests/vm/ksm_tests.c index cae72872152b..fd85f15869d1 100644 --- a/tools/testing/selftests/vm/ksm_tests.c +++ b/tools/testing/selftests/vm/ksm_tests.c @@ -12,6 +12,7 @@ #include "../kselftest.h" #include "../../../../include/vdso/time64.h" +#include "util.h" #define KSM_SYSFS_PATH "/sys/kernel/mm/ksm/" #define KSM_FP(s) (KSM_SYSFS_PATH s) @@ -22,22 +23,6 @@ #define KSM_MERGE_ACROSS_NODES_DEFAULT true #define MB (1ul << 20) -#ifdef __powerpc64__ -#define PAGE_SHIFT 16 -#else -#define PAGE_SHIFT 12 -#endif -/* - * On ppc64 this will only work with radix 2M hugepage size - */ -#define HPAGE_SHIFT 21 - -#define PAGE_SIZE (1 << PAGE_SHIFT) -#define HPAGE_SIZE (1 << HPAGE_SHIFT) - -#define PAGEMAP_PRESENT(ent) (((ent) & (1ull << 63)) != 0) -#define PAGEMAP_PFN(ent) ((ent) & ((1ull << 55) - 1)) - struct ksm_sysfs { unsigned long max_page_sharing; unsigned long merge_across_nodes; @@ -463,34 +448,6 @@ static int check_ksm_numa_merge(int mapping, int prot, int timeout, bool merge_a return KSFT_FAIL; } -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; -} - static int ksm_merge_hugepages_time(int mapping, int prot, int timeout, size_t map_size) { void *map_ptr, *map_ptr_orig; diff --git a/tools/testing/selftests/vm/transhuge-stress.c b/tools/testing/selftests/vm/transhuge-stress.c index b1f8d98355c5..0da4aa10746a 100644 --- a/tools/testing/selftests/vm/transhuge-stress.c +++ b/tools/testing/selftests/vm/transhuge-stress.c @@ -16,51 +16,7 @@ #include #include -#ifdef __powerpc64__ -#define PAGE_SHIFT 16 -#else -#define PAGE_SHIFT 12 -#endif -/* - * On ppc64 this will only work with radix 2M hugepage size - */ -#define HPAGE_SHIFT 21 - -#define PAGE_SIZE (1 << PAGE_SHIFT) -#define HPAGE_SIZE (1 << HPAGE_SHIFT) - -#define PAGEMAP_PRESENT(ent) (((ent) & (1ull << 63)) != 0) -#define PAGEMAP_PFN(ent) ((ent) & ((1ull << 55) - 1)) - -int pagemap_fd; - -int64_t allocate_transhuge(void *ptr) -{ - 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; -} +#include "util.h" int main(int argc, char **argv) { @@ -70,6 +26,7 @@ int main(int argc, char **argv) double s; uint8_t *map; size_t map_len; + int pagemap_fd; ram = sysconf(_SC_PHYS_PAGES); if (ram > SIZE_MAX / sysconf(_SC_PAGESIZE) / 4) @@ -116,7 +73,7 @@ int main(int argc, char **argv) for (p = ptr; p < ptr + len; p += HPAGE_SIZE) { int64_t pfn; - pfn = allocate_transhuge(p); + pfn = allocate_transhuge(p, pagemap_fd); if (pfn < 0) { nr_failed++; diff --git a/tools/testing/selftests/vm/util.h b/tools/testing/selftests/vm/util.h new file mode 100644 index 000000000000..8ec94940371f --- /dev/null +++ b/tools/testing/selftests/vm/util.h @@ -0,0 +1,54 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __KSELFTEST_VM_UTIL_H +#define __KSELFTEST_VM_UTIL_H + +#include +#include + +#ifdef __powerpc64__ +#define PAGE_SHIFT 16 +#else +#define PAGE_SHIFT 12 +#endif +/* + * On ppc64 this will only work with radix 2M hugepage size + */ +#define HPAGE_SHIFT 21 + +#define PAGE_SIZE (1 << PAGE_SHIFT) +#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