iommu.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
From: Zhou Wang <wangzhou1@hisilicon.com>
To: <linux-kernel@vger.kernel.org>,
	<iommu@lists.linux-foundation.org>, <linux-mm@kvack.org>,
	<linux-arm-kernel@lists.infradead.org>,
	<linux-api@vger.kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Alexander Viro <viro@zeniv.linux.org.uk>
Cc: jean-philippe@linaro.org, kevin.tian@intel.com, jgg@ziepe.ca,
	gregkh@linuxfoundation.org, zhangfei.gao@linaro.org,
	liguozhu@hisilicon.com
Subject: [RFC PATCH v3 2/2] selftests/vm: add mempinfd test
Date: Sun, 7 Feb 2021 16:18:04 +0800	[thread overview]
Message-ID: <1612685884-19514-3-git-send-email-wangzhou1@hisilicon.com> (raw)
In-Reply-To: <1612685884-19514-1-git-send-email-wangzhou1@hisilicon.com>

This test gets a fd from new mempinfd syscall and creates multiple threads
to do pin/unpin memory.

Signed-off-by: Zhou Wang <wangzhou1@hisilicon.com>
Suggested-by: Barry Song <song.bao.hua@hisilicon.com>
---
 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 <linux/mempinfd.h>
+#include <malloc.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <sys/ioctl.h>
+#include <sys/syscall.h>
+
+#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 */
-- 
2.8.1

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

      parent reply	other threads:[~2021-02-07  8:26 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-07  8:18 [RFC PATCH v3 0/2] mempinfd: Add new syscall to provide memory pin Zhou Wang
2021-02-07  8:18 ` [RFC PATCH v3 1/2] " Zhou Wang
2021-02-07 21:34   ` Matthew Wilcox
2021-02-07 22:24     ` Song Bao Hua (Barry Song)
2021-02-08  1:30       ` Matthew Wilcox
2021-02-08  2:27         ` Song Bao Hua (Barry Song)
2021-02-08  8:21           ` David Hildenbrand
2021-02-08 10:13             ` Song Bao Hua (Barry Song)
2021-02-08 10:37               ` David Hildenbrand
2021-02-08 20:52                 ` Song Bao Hua (Barry Song)
2021-02-08  2:18       ` David Rientjes via iommu
2021-02-08  5:34         ` Song Bao Hua (Barry Song)
2021-02-07 21:51   ` Arnd Bergmann
2021-02-09  9:27     ` Zhou Wang
2021-02-07 22:02   ` Andy Lutomirski
2021-02-09  9:17     ` Zhou Wang
2021-02-09  9:37       ` Greg KH
2021-02-09 11:58         ` Zhou Wang
2021-02-09 12:01           ` Greg KH
2021-02-09 12:20             ` Zhou Wang
2021-02-10 18:50               ` Matthew Wilcox
2021-02-08  8:14   ` David Hildenbrand
2021-02-08 18:33     ` Jason Gunthorpe
2021-02-08 20:35       ` Song Bao Hua (Barry Song)
2021-02-08 21:30         ` Jason Gunthorpe
2021-02-09  3:01           ` Song Bao Hua (Barry Song)
2021-02-09 13:53             ` Jason Gunthorpe
2021-02-09 22:22               ` Song Bao Hua (Barry Song)
2021-02-10 18:04                 ` Jason Gunthorpe
2021-02-10 21:39                   ` Song Bao Hua (Barry Song)
2021-02-11 10:28                     ` David Hildenbrand
2021-02-07  8:18 ` Zhou Wang [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1612685884-19514-3-git-send-email-wangzhou1@hisilicon.com \
    --to=wangzhou1@hisilicon.com \
    --cc=akpm@linux-foundation.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=iommu@lists.linux-foundation.org \
    --cc=jean-philippe@linaro.org \
    --cc=jgg@ziepe.ca \
    --cc=kevin.tian@intel.com \
    --cc=liguozhu@hisilicon.com \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=zhangfei.gao@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).