linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Keith Busch <kbusch@meta.com>
To: <linux-mm@kvack.org>, <linux-kernel@vger.kernel.org>,
	Matthew Wilcox <willy@infradead.org>
Cc: Tony Battersby <tonyb@cybernetics.com>,
	Kernel Team <kernel-team@meta.com>,
	Keith Busch <kbusch@kernel.org>
Subject: [PATCHv2 01/11] dmapool: add alloc/free performance test
Date: Fri, 16 Dec 2022 12:16:15 -0800	[thread overview]
Message-ID: <20221216201625.2362737-2-kbusch@meta.com> (raw)
In-Reply-To: <20221216201625.2362737-1-kbusch@meta.com>

From: Keith Busch <kbusch@kernel.org>

Provide a module that allocates and frees many blocks of various sizes
and report how long it takes. This is intended to provide a consistent
way to measure how changes to the dma_pool_alloc/free routines affect
timing.

Signed-off-by: Keith Busch <kbusch@kernel.org>
---
v1->v2:

  Used appropriate dma_ops accessor, fixing:

   https://lore.kernel.org/all/202212061052.hMChRFTf-lkp@intel.com/ 

  Added ability to test arbitrary alignments and boundaries and added an
  entry to test the previously failing test case:

   https://lore.kernel.org/oe-lkp/202212112301.ad0819f7-oliver.sang@intel.com/

 mm/Kconfig        |   9 +++
 mm/Makefile       |   1 +
 mm/dmapool_test.c | 146 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 156 insertions(+)
 create mode 100644 mm/dmapool_test.c

diff --git a/mm/Kconfig b/mm/Kconfig
index 34d36958b8ac9..3d71acc29c10f 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -1070,6 +1070,15 @@ config GUP_TEST
 comment "GUP_TEST needs to have DEBUG_FS enabled"
 	depends on !GUP_TEST && !DEBUG_FS
 
+config DMAPOOL_TEST
+	tristate "Enable a module to run time tests on dma_pool"
+	depends on HAS_DMA
+	help
+	  Provides a module that will allocate and free many blocks of various
+	  sizes and report how long it takes. This is intended to provide a
+	  consistent way to measure how changes to the dma_pool_alloc/free
+	  routines affect performance.
+
 config GUP_GET_PTE_LOW_HIGH
 	bool
 
diff --git a/mm/Makefile b/mm/Makefile
index 8e105e5b3e293..3a08f5d7b1782 100644
--- a/mm/Makefile
+++ b/mm/Makefile
@@ -103,6 +103,7 @@ obj-$(CONFIG_MEMCG) += swap_cgroup.o
 endif
 obj-$(CONFIG_CGROUP_HUGETLB) += hugetlb_cgroup.o
 obj-$(CONFIG_GUP_TEST) += gup_test.o
+obj-$(CONFIG_DMAPOOL_TEST) += dmapool_test.o
 obj-$(CONFIG_MEMORY_FAILURE) += memory-failure.o
 obj-$(CONFIG_HWPOISON_INJECT) += hwpoison-inject.o
 obj-$(CONFIG_DEBUG_KMEMLEAK) += kmemleak.o
diff --git a/mm/dmapool_test.c b/mm/dmapool_test.c
new file mode 100644
index 0000000000000..c4292dfda3690
--- /dev/null
+++ b/mm/dmapool_test.c
@@ -0,0 +1,146 @@
+#include <linux/device.h>
+#include <linux/dma-map-ops.h>
+#include <linux/dma-mapping.h>
+#include <linux/dmapool.h>
+#include <linux/kernel.h>
+#include <linux/ktime.h>
+#include <linux/module.h>
+
+#define NR_TESTS (100)
+
+struct dma_pool_pair {
+	dma_addr_t dma;
+	void *v;
+};
+
+struct dmapool_parms {
+	size_t size;
+	size_t align;
+	size_t boundary;
+};
+
+static const struct dmapool_parms pool_parms[] = {
+	{ .size = 16, .align = 16, .boundary = 0 },
+	{ .size = 64, .align = 64, .boundary = 0 },
+	{ .size = 256, .align = 256, .boundary = 0 },
+	{ .size = 1024, .align = 1024, .boundary = 0 },
+	{ .size = 4096, .align = 4096, .boundary = 0 },
+	{ .size = 68, .align = 32, .boundary = 4096 },
+};
+
+static struct dma_pool *pool;
+static struct device test_dev;
+static u64 dma_mask;
+
+static inline int nr_blocks(int size)
+{
+	return clamp_t(int, (PAGE_SIZE / size) * 512, 1024, 8192);
+}
+
+static int dmapool_test_alloc(struct dma_pool_pair *p, int blocks)
+{
+	int i;
+
+	for (i = 0; i < blocks; i++) {
+		p[i].v = dma_pool_alloc(pool, GFP_KERNEL,
+					&p[i].dma);
+		if (!p[i].v)
+			goto pool_fail;
+	}
+
+	for (i = 0; i < blocks; i++)
+		dma_pool_free(pool, p[i].v, p[i].dma);
+
+	return 0;
+
+pool_fail:
+	for (--i; i >= 0; i--)
+		dma_pool_free(pool, p[i].v, p[i].dma);
+	return -ENOMEM;
+}
+
+static int dmapool_test_block(const struct dmapool_parms *parms)
+{
+	int blocks = nr_blocks(parms->size);
+	ktime_t start_time, end_time;
+	struct dma_pool_pair *p;
+	int i, ret;
+
+	p = kcalloc(blocks, sizeof(*p), GFP_KERNEL);
+	if (!p)
+		return -ENOMEM;
+
+	pool = dma_pool_create("test pool", &test_dev, parms->size,
+			       parms->align, parms->boundary);
+	if (!pool) {
+		ret = -ENOMEM;
+		goto free_pairs;
+	}
+
+	start_time = ktime_get();
+	for (i = 0; i < NR_TESTS; i++) {
+		ret = dmapool_test_alloc(p, blocks);
+		if (ret)
+			goto free_pool;
+		if (need_resched())
+			cond_resched();
+	}
+	end_time = ktime_get();
+
+	printk("dmapool test: size:%-4ld blocks:%-6d time:%llu\n", parms->size,
+	       blocks, ktime_us_delta(end_time, start_time));
+
+free_pool:
+	dma_pool_destroy(pool);
+free_pairs:
+	kfree(p);
+	return ret;
+}
+
+static void dmapool_test_release(struct device *dev)
+{
+}
+
+static int dmapool_checks(void)
+{
+	int i, ret;
+
+	ret = dev_set_name(&test_dev, "dmapool-test");
+	if (ret)
+		return ret;
+
+	ret = device_register(&test_dev);
+	if (ret) {
+		printk("%s: register failed:%d\n", __func__, ret);
+		goto put_device;
+	}
+
+	test_dev.release = dmapool_test_release;
+	set_dma_ops(&test_dev, NULL);
+	test_dev.dma_mask = &dma_mask;
+	ret = dma_set_mask_and_coherent(&test_dev, DMA_BIT_MASK(64));
+	if (ret) {
+		printk("%s: mask failed:%d\n", __func__, ret);
+		goto del_device;
+	}
+
+	for (i = 0; i < ARRAY_SIZE(pool_parms); i++) {
+		ret = dmapool_test_block(&pool_parms[i]);
+		if (ret)
+			break;
+	}
+
+del_device:
+	device_del(&test_dev);
+put_device:
+	put_device(&test_dev);
+	return ret;
+}
+
+static void dmapool_exit(void)
+{
+}
+
+module_init(dmapool_checks);
+module_exit(dmapool_exit);
+MODULE_LICENSE("GPL");
-- 
2.30.2



  reply	other threads:[~2022-12-16 20:17 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-16 20:16 [PATCHv2 00/11] dmapool enhancements Keith Busch
2022-12-16 20:16 ` Keith Busch [this message]
2022-12-16 20:16 ` [PATCHv2 02/11] dmapool: remove checks for dev == NULL Keith Busch
2022-12-23 16:27   ` Christoph Hellwig
2022-12-16 20:16 ` [PATCHv2 03/11] dmapool: use sysfs_emit() instead of scnprintf() Keith Busch
2022-12-23 16:28   ` Christoph Hellwig
2022-12-16 20:16 ` [PATCHv2 04/11] dmapool: cleanup integer types Keith Busch
2022-12-23 16:29   ` Christoph Hellwig
2022-12-16 20:16 ` [PATCHv2 05/11] dmapool: speedup DMAPOOL_DEBUG with init_on_alloc Keith Busch
2022-12-23 16:29   ` Christoph Hellwig
2022-12-16 20:16 ` [PATCHv2 06/11] dmapool: move debug code to own functions Keith Busch
2022-12-23 16:31   ` Christoph Hellwig
2022-12-16 20:16 ` [PATCHv2 07/11] dmapool: rearrange page alloc failure handling Keith Busch
2022-12-23 16:31   ` Christoph Hellwig
2022-12-16 20:16 ` [PATCHv2 08/11] dmapool: consolidate page initialization Keith Busch
2022-12-23 16:35   ` Christoph Hellwig
2022-12-16 20:16 ` [PATCHv2 09/11] dmapool: simplify freeing Keith Busch
2022-12-23 16:38   ` Christoph Hellwig
2022-12-27 20:21     ` Keith Busch
2022-12-16 20:16 ` [PATCHv2 10/11] dmapool: don't memset on free twice Keith Busch
2022-12-23 16:39   ` Christoph Hellwig
2022-12-16 20:16 ` [PATCHv2 11/11] dmapool: link blocks across pages Keith Busch
2022-12-17  2:39   ` kernel test robot
2022-12-17  3:49   ` kernel test robot
2022-12-23 16:58   ` Christoph Hellwig
2022-12-23 17:08     ` Tony Battersby
2022-12-23 17:15       ` Christoph Hellwig
2022-12-24 14:55         ` Keith Busch

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=20221216201625.2362737-2-kbusch@meta.com \
    --to=kbusch@meta.com \
    --cc=kbusch@kernel.org \
    --cc=kernel-team@meta.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=tonyb@cybernetics.com \
    --cc=willy@infradead.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