public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Tvrtko Ursulin <tursulin@ursulin.net>
To: Intel-gfx@lists.freedesktop.org
Cc: tursulin@ursulin.net, Tvrtko Ursulin <tvrtko.ursulin@intel.com>,
	Chris Wilson <chris@chris-wilson.co.uk>,
	linux-kernel@vger.kernel.org
Subject: [PATCH 5/5] tools/testing/scatterlist: Test new __sg_alloc_table_from_pages
Date: Mon, 31 Jul 2017 19:55:12 +0100	[thread overview]
Message-ID: <20170731185512.20010-5-tvrtko.ursulin@linux.intel.com> (raw)
In-Reply-To: <20170731185512.20010-1-tvrtko.ursulin@linux.intel.com>

From: Tvrtko Ursulin <tvrtko.ursulin@intel.com>

Exercise the new __sg_alloc_table_from_pages API (and through
it also the old sg_alloc_table_from_pages), checking that the
created table has the expected number of segments depending on
the sequence of input pages and other conditions.

Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
Cc: Chris Wilson <chris@chris-wilson.co.uk>
Cc: linux-kernel@vger.kernel.org
---
 tools/testing/scatterlist/Makefile   |  30 +++++++++
 tools/testing/scatterlist/linux/mm.h | 125 +++++++++++++++++++++++++++++++++++
 tools/testing/scatterlist/main.c     | 112 +++++++++++++++++++++++++++++++
 3 files changed, 267 insertions(+)
 create mode 100644 tools/testing/scatterlist/Makefile
 create mode 100644 tools/testing/scatterlist/linux/mm.h
 create mode 100644 tools/testing/scatterlist/main.c

diff --git a/tools/testing/scatterlist/Makefile b/tools/testing/scatterlist/Makefile
new file mode 100644
index 000000000000..0867e0ef32d6
--- /dev/null
+++ b/tools/testing/scatterlist/Makefile
@@ -0,0 +1,30 @@
+CFLAGS += -I. -I../../include -g -O2 -Wall -fsanitize=address
+LDFLAGS += -fsanitize=address
+TARGETS = main
+OFILES = main.o scatterlist.o
+
+ifeq ($(BUILD), 32)
+        CFLAGS += -m32
+        LDFLAGS += -m32
+endif
+
+targets: include $(TARGETS)
+
+main: $(OFILES)
+
+clean:
+	$(RM) $(TARGETS) $(OFILES) scatterlist.c linux/scatterlist.h linux/highmem.h linux/kmemleak.h asm/io.h
+	@rmdir asm
+
+scatterlist.c: ../../../lib/scatterlist.c
+	@sed -e 's/^static //' -e 's/__always_inline //' -e 's/inline //' < $< > $@
+
+.PHONY: include
+
+include: ../../../include/linux/scatterlist.h
+	@mkdir -p linux
+	@mkdir -p asm
+	@touch asm/io.h
+	@touch linux/highmem.h
+	@touch linux/kmemleak.h
+	@cp $< linux/scatterlist.h
diff --git a/tools/testing/scatterlist/linux/mm.h b/tools/testing/scatterlist/linux/mm.h
new file mode 100644
index 000000000000..ccbb248ebdc1
--- /dev/null
+++ b/tools/testing/scatterlist/linux/mm.h
@@ -0,0 +1,125 @@
+#ifndef _LINUX_MM_H
+#define _LINUX_MM_H
+
+#include <assert.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdio.h>
+
+typedef unsigned long dma_addr_t;
+
+#define unlikely
+
+#define BUG_ON(x) assert(!(x))
+
+#define WARN_ON(condition) ({                                           \
+        int __ret_warn_on = !!(condition);                              \
+        unlikely(__ret_warn_on);                                        \
+})
+
+#define WARN_ON_ONCE(condition) ({                              \
+        int __ret_warn_on = !!(condition);                      \
+        if (unlikely(__ret_warn_on))                            \
+                assert(0);                                      \
+        unlikely(__ret_warn_on);                                \
+})
+
+#define PAGE_SIZE	(4096)
+#define PAGE_SHIFT	(12)
+#define PAGE_MASK	(~(PAGE_SIZE-1))
+
+#define __ALIGN_KERNEL(x, a)            __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
+#define __ALIGN_KERNEL_MASK(x, mask)    (((x) + (mask)) & ~(mask))
+#define ALIGN(x, a)             __ALIGN_KERNEL((x), (a))
+
+#define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE)
+
+#define offset_in_page(p)	((unsigned long)(p) & ~PAGE_MASK)
+
+#define virt_to_page(x)	((void *)x)
+#define page_address(x)	((void *)x)
+
+static inline unsigned long page_to_phys(struct page *page)
+{
+	assert(0);
+
+	return 0;
+}
+
+#define page_to_pfn(page) ((unsigned long)(page) / PAGE_SIZE)
+#define pfn_to_page(pfn) (void *)((pfn) * PAGE_SIZE)
+#define nth_page(page,n) pfn_to_page(page_to_pfn((page)) + (n))
+
+#define __min(t1, t2, min1, min2, x, y) ({              \
+        t1 min1 = (x);                                  \
+        t2 min2 = (y);                                  \
+        (void) (&min1 == &min2);                        \
+        min1 < min2 ? min1 : min2; })
+
+#define ___PASTE(a,b) a##b
+#define __PASTE(a,b) ___PASTE(a,b)
+
+#define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
+
+#define min(x, y)                                       \
+        __min(typeof(x), typeof(y),                     \
+              __UNIQUE_ID(min1_), __UNIQUE_ID(min2_),   \
+              x, y)
+
+#define min_t(type, x, y)                               \
+        __min(type, type,                               \
+              __UNIQUE_ID(min1_), __UNIQUE_ID(min2_),   \
+              x, y)
+
+#define preemptible() (1)
+
+static inline void *kmap(struct page *page)
+{
+	assert(0);
+
+	return NULL;
+}
+
+static inline void *kmap_atomic(struct page *page)
+{
+	assert(0);
+
+	return NULL;
+}
+
+static inline void kunmap(void *addr)
+{
+	assert(0);
+}
+
+static inline void kunmap_atomic(void *addr)
+{
+	assert(0);
+}
+
+static inline unsigned long __get_free_page(unsigned int flags)
+{
+	return (unsigned long)malloc(PAGE_SIZE);
+}
+
+static inline void free_page(unsigned long page)
+{
+	free((void *)page);
+}
+
+static inline void *kmalloc(unsigned int size, unsigned int flags)
+{
+	return malloc(size);
+}
+
+#define kfree(x) free(x)
+
+#define kmemleak_alloc(a, b, c, d)
+#define kmemleak_free(a)
+
+#define PageSlab(p) (0)
+#define flush_kernel_dcache_page(p)
+
+#endif
diff --git a/tools/testing/scatterlist/main.c b/tools/testing/scatterlist/main.c
new file mode 100644
index 000000000000..edf9b2960734
--- /dev/null
+++ b/tools/testing/scatterlist/main.c
@@ -0,0 +1,112 @@
+#include <stdio.h>
+#include <assert.h>
+
+#include <linux/scatterlist.h>
+
+#define MAX_PAGES (64)
+
+static unsigned
+_set_pages(struct page **pages, const unsigned *array, unsigned num)
+{
+	unsigned int i;
+
+	assert(num < MAX_PAGES);
+
+	for (i = 0; i < num; i++)
+		pages[i] = (struct page *)(unsigned long)
+			   ((1 + array[i]) * PAGE_SIZE);
+
+	return num;
+}
+
+#define set_pages(p, a) _set_pages((p), (a), sizeof(a) / sizeof(a[0]))
+
+#define check_and_free(_st, _ret, _nents) \
+{ \
+	assert((_ret) == 0); \
+	assert((_st)->nents == _nents); \
+	assert((_st)->orig_nents == _nents); \
+	sg_free_table(_st); \
+}
+
+static int
+alloc_tbl(struct sg_table *st, struct page **pages, unsigned nr_pages,
+	  unsigned offset, unsigned size, unsigned max)
+{
+	return __sg_alloc_table_from_pages(st, pages, nr_pages, offset, size,
+					   max, GFP_KERNEL);
+}
+
+int main(void)
+{
+	const unsigned int sgmax = SCATTERLIST_MAX_SEGMENT;
+	struct page *pages[MAX_PAGES];
+	struct sg_table st;
+	int ret;
+
+	ret = set_pages(pages, ((unsigned []){ 0 }));
+	ret = alloc_tbl(&st, pages, ret, 0, ret * PAGE_SIZE, PAGE_SIZE + 1);
+	assert(ret == -EINVAL);
+
+	ret = set_pages(pages, ((unsigned []){ 0 }));
+	ret = alloc_tbl(&st, pages, ret, 0, ret * PAGE_SIZE, 0);
+	assert(ret == -EINVAL);
+
+	ret = set_pages(pages, ((unsigned []){ 0 }));
+	ret = alloc_tbl(&st, pages, ret, 0, ret * PAGE_SIZE, sgmax);
+	check_and_free(&st, ret, 1);
+
+	ret = set_pages(pages, ((unsigned []){ 0 }));
+	ret = alloc_tbl(&st, pages, ret, 0, ret, sgmax);
+	check_and_free(&st, ret, 1);
+
+	ret = set_pages(pages, ((unsigned []){ 0, 1 }));
+	ret = alloc_tbl(&st, pages, ret, 0, ret * PAGE_SIZE, sgmax);
+	check_and_free(&st, ret, 1);
+
+	ret = set_pages(pages, ((unsigned []){ 0, 2 }));
+	ret = alloc_tbl(&st, pages, ret, 0, ret * PAGE_SIZE, sgmax);
+	check_and_free(&st, ret, 2);
+
+	ret = set_pages(pages, ((unsigned []){ 0, 1, 3 }));
+	ret = alloc_tbl(&st, pages, ret, 0, ret * PAGE_SIZE, sgmax);
+	check_and_free(&st, ret, 2);
+
+	ret = set_pages(pages, ((unsigned []){ 0, 1, 3, 4 }));
+	ret = alloc_tbl(&st, pages, ret, 0, ret * PAGE_SIZE, sgmax);
+	check_and_free(&st, ret, 2);
+
+	ret = set_pages(pages, ((unsigned []){ 0, 1, 3, 4, 5 }));
+	ret = alloc_tbl(&st, pages, ret, 0, ret * PAGE_SIZE, sgmax);
+	check_and_free(&st, ret, 2);
+
+	ret = set_pages(pages, ((unsigned []){ 0, 1, 3, 4, 6 }));
+	ret = alloc_tbl(&st, pages, ret, 0, ret * PAGE_SIZE, sgmax);
+	check_and_free(&st, ret, 3);
+
+	ret = set_pages(pages, ((unsigned []){ 0, 2, 4, 6, 8 }));
+	ret = alloc_tbl(&st, pages, ret, 0, ret * PAGE_SIZE, sgmax);
+	check_and_free(&st, ret, 5);
+
+	ret = set_pages(pages, ((unsigned []){ 0, 1, 2, 3, 4 }));
+	ret = alloc_tbl(&st, pages, ret, 0, ret * PAGE_SIZE, sgmax);
+	check_and_free(&st, ret, 1);
+
+	ret = set_pages(pages, ((unsigned []){ 0, 1, 2, 3, 4 }));
+	ret = alloc_tbl(&st, pages, ret, 0, ret * PAGE_SIZE, 2 * PAGE_SIZE);
+	check_and_free(&st, ret, 3);
+
+	ret = set_pages(pages, ((unsigned []){ 0, 1, 2, 3, 4, 5}));
+	ret = alloc_tbl(&st, pages, ret, 0, ret * PAGE_SIZE, 2 * PAGE_SIZE);
+	check_and_free(&st, ret, 3);
+
+	ret = set_pages(pages, ((unsigned []){ 0, 2, 3, 4, 5, 6}));
+	ret = alloc_tbl(&st, pages, ret, 0, ret * PAGE_SIZE, 2 * PAGE_SIZE);
+	check_and_free(&st, ret, 4);
+
+	ret = set_pages(pages, ((unsigned []){ 0, 1, 3, 4, 5, 6}));
+	ret = alloc_tbl(&st, pages, ret, 0, ret * PAGE_SIZE, 2 * PAGE_SIZE);
+	check_and_free(&st, ret, 3);
+
+	return 0;
+}
-- 
2.9.4

  parent reply	other threads:[~2017-07-31 18:55 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-31 18:55 [PATCH 1/5] lib/scatterlist: Fix offset type in sg_alloc_table_from_pages Tvrtko Ursulin
2017-07-31 18:55 ` [PATCH 2/5] lib/scatterlist: Avoid potential scatterlist entry overflow Tvrtko Ursulin
2017-08-03  9:13   ` [PATCH v6 " Tvrtko Ursulin
2017-07-31 18:55 ` [PATCH 3/5] lib/scatterlist: Introduce and export __sg_alloc_table_from_pages Tvrtko Ursulin
2017-08-03  9:13   ` [PATCH v7 " Tvrtko Ursulin
2017-07-31 18:55 ` [PATCH 4/5] drm/i915: Use __sg_alloc_table_from_pages for userptr allocations Tvrtko Ursulin
2017-08-03  9:14   ` [PATCH v8 " Tvrtko Ursulin
2017-07-31 18:55 ` Tvrtko Ursulin [this message]
2017-09-05 10:24   ` [PATCH v2 5/5] tools/testing/scatterlist: Test new __sg_alloc_table_from_pages Tvrtko Ursulin
2017-09-06 10:48     ` Chris Wilson
2017-09-06 12:10       ` [Intel-gfx] " Tvrtko Ursulin
2017-09-06 12:39         ` Chris Wilson
2017-09-06 14:55           ` [PATCH v3 " Tvrtko Ursulin

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=20170731185512.20010-5-tvrtko.ursulin@linux.intel.com \
    --to=tursulin@ursulin.net \
    --cc=Intel-gfx@lists.freedesktop.org \
    --cc=chris@chris-wilson.co.uk \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tvrtko.ursulin@intel.com \
    /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