From patchwork Sun Feb 7 08:18:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhou Wang X-Patchwork-Id: 12072659 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.3 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B45EEC4332D for ; Sun, 7 Feb 2021 08:28:03 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 3F20964E72 for ; Sun, 7 Feb 2021 08:28:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3F20964E72 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=hisilicon.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-ID:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=0WuVrtBClnyxfOG1XrfnBCmC3KPEWx4uvy4GQeUPSWQ=; b=mrByQOVPM1yLoRwMKsgesxP+S IMI1wpXq0LHVEeNfB3NP8FHzEHsYvzM0lvNmNpIwyy0j0PHfnjbDVW2L6ZKZm5h/Dw6MpptR0SvR1 E2O2X0cdjuNdQrlzt02hF1tqGLVhVa7MBMxW4I9eBkMJjuXdpowS8o4xNBuhHagnm0Mj9oidTShh7 lkIYyvPsofKBwfo5Fo5br83nCujagSQJ4SpDXYGGM2QsJDV51VElhVXxFAMu+JoPJk/N0HKsPKyJV cIolYxeZ8PLuaOx7kOZF1rVypzhqsgnTuECH93SI3TSDFjvKgYzMUuDVF/6d4O+8++ax6JA7AQ+dh XcBTnufbQ==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1l8fPE-00007C-Qm; Sun, 07 Feb 2021 08:26:56 +0000 Received: from szxga05-in.huawei.com ([45.249.212.191]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1l8fOu-0008NY-UG for linux-arm-kernel@lists.infradead.org; Sun, 07 Feb 2021 08:26:49 +0000 Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.60]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4DYMf65ntqzjKdf; Sun, 7 Feb 2021 16:25:02 +0800 (CST) Received: from localhost.localdomain (10.69.192.58) by DGGEMS413-HUB.china.huawei.com (10.3.19.213) with Microsoft SMTP Server id 14.3.498.0; Sun, 7 Feb 2021 16:26:16 +0800 From: Zhou Wang To: , , , , , Andrew Morton , Alexander Viro Subject: [RFC PATCH v3 2/2] selftests/vm: add mempinfd test Date: Sun, 7 Feb 2021 16:18:04 +0800 Message-ID: <1612685884-19514-3-git-send-email-wangzhou1@hisilicon.com> X-Mailer: git-send-email 2.8.1 In-Reply-To: <1612685884-19514-1-git-send-email-wangzhou1@hisilicon.com> References: <1612685884-19514-1-git-send-email-wangzhou1@hisilicon.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.58] X-CFilter-Loop: Reflected X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210207_032637_488926_A163F8E1 X-CRM114-Status: GOOD ( 18.43 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: song.bao.hua@hisilicon.com, jean-philippe@linaro.org, kevin.tian@intel.com, jgg@ziepe.ca, gregkh@linuxfoundation.org, eric.auger@redhat.com, Zhou Wang , zhangfei.gao@linaro.org, liguozhu@hisilicon.com Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org This test gets a fd from new mempinfd syscall and creates multiple threads to do pin/unpin memory. Signed-off-by: Zhou Wang Suggested-by: Barry Song --- tools/testing/selftests/vm/Makefile | 1 + tools/testing/selftests/vm/mempinfd.c | 131 ++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 tools/testing/selftests/vm/mempinfd.c diff --git a/tools/testing/selftests/vm/Makefile b/tools/testing/selftests/vm/Makefile index d42115e..2d5b509 100644 --- a/tools/testing/selftests/vm/Makefile +++ b/tools/testing/selftests/vm/Makefile @@ -42,6 +42,7 @@ TEST_GEN_FILES += on-fault-limit TEST_GEN_FILES += thuge-gen TEST_GEN_FILES += transhuge-stress TEST_GEN_FILES += userfaultfd +TEST_GEN_FILES += mempinfd ifeq ($(MACHINE),x86_64) CAN_BUILD_I386 := $(shell ./../x86/check_cc.sh $(CC) ../x86/trivial_32bit_program.c -m32) diff --git a/tools/testing/selftests/vm/mempinfd.c b/tools/testing/selftests/vm/mempinfd.c new file mode 100644 index 0000000..51d5cbf --- /dev/null +++ b/tools/testing/selftests/vm/mempinfd.c @@ -0,0 +1,131 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2021 HiSilicon Limited. */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" + +#ifdef __NR_mempinfd + +#define DEF_PIN_SIZE (4096 * 1024) +#define MAX_THREAD_NUM 20 +#define DEF_THREAD_NUM 1 +#define DEF_TIMES 10000 + +struct test_data { + int fd; + unsigned long mem_size; + unsigned long times; +}; + +static void *do_pin_test(void *data) +{ + struct mem_pin_address addr; + struct test_data *d = data; + unsigned long times; + int ret, fd; + int i = 0; + void *p; + + p = malloc(d->mem_size); + if (!p) { + fprintf(stderr, "fail to allocate memory\n"); + return NULL; + } + + addr.addr = (__u64)p; + addr.size = d->mem_size; + times = d->mem_size; + fd = d->fd; + + while (i++ < times) { + ret = ioctl(fd, MEM_CMD_PIN, &addr); + if (ret) { + fprintf(stderr, "fail to pin memory\n"); + return NULL; + } + + usleep(1000); + + ret = ioctl(fd, MEM_CMD_UNPIN, &addr); + if (ret) { + fprintf(stderr, "fail to unpin memory\n"); + return NULL; + } + } + + free(p); + + return NULL; +} + +int main(int argc, char **argv) +{ + unsigned long thread_num = DEF_THREAD_NUM; + unsigned long mem_size = DEF_PIN_SIZE; + unsigned long times = DEF_TIMES; + pthread_t threads[MAX_THREAD_NUM]; + struct test_data data; + int fd, opt, i; + int ret = 0; + + while ((opt = getopt(argc, argv, "s:n:t:")) != -1) { + switch (opt) { + case 's': + mem_size = atoi(optarg); + break; + case 'n': + thread_num = atoi(optarg); + if (thread_num > MAX_THREAD_NUM) + return -1; + break; + case 't': + times = atoi(optarg); + break; + default: + return -1; + } + } + + fd = syscall(__NR_mempinfd); + if (fd < 0) { + fprintf(stderr, "mempinfd syscall not available in this kernel\n"); + return -1; + } + + data.fd = fd; + data.mem_size = mem_size; + data.times = times; + + for (i = 0; i < thread_num; i++) { + ret = pthread_create(&threads[i], NULL, do_pin_test, &data); + if (ret) { + fprintf(stderr, "fail to create thread %d: %d\n", + i, -errno); + return -1; + } + } + + for (i = 0; i < thread_num; i++) + pthread_join(threads[i], NULL); + + close(fd); + + return 0; +} + +#else /* __NR_mempinfd */ + +#warning "missing __NR_mempinfd definition" +int main(void) +{ + printf("skip: Skipping mempinfd test (missing __NR_mempinfd)\n"); + return KSFT_SKIP; +} + +#endif /* __NR_mempinfd */