Intel-GFX Archive on lore.kernel.org
 help / color / Atom feed
From: Chris Wilson <chris@chris-wilson.co.uk>
To: intel-gfx@lists.freedesktop.org
Cc: Chris Wilson <chris@chris-wilson.co.uk>
Subject: [Intel-gfx] [PATCH xf86-video-intel] sna: Probe userptr storage on construction
Date: Fri, 15 Jan 2021 22:07:44 +0000
Message-ID: <20210115220744.1402642-1-chris@chris-wilson.co.uk> (raw)

Not all user address ranges are valid for userptr, as we can only
utilize struct pages (and not indirect pfnmaps). Currently we use a
set-domain call after userptr to validate the range by populating the
userptr, but this has the drawback of forcing the population ever for
userptr that are never used. I915_USERPTR_PROBE introduces a method for
doing a simple vma lookup to check that the userptr is valid at the time
of construction, leaving the more costly page lookup for lazy execution.

See https://patchwork.freedesktop.org/series/33449/

Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
---
 src/sna/kgem.c | 49 ++++++++++++++++++++++++++++++++++++++++++++-----
 src/sna/kgem.h |  1 +
 2 files changed, 45 insertions(+), 5 deletions(-)

diff --git a/src/sna/kgem.c b/src/sna/kgem.c
index 7b645da8d..7b143dd34 100644
--- a/src/sna/kgem.c
+++ b/src/sna/kgem.c
@@ -170,6 +170,8 @@ struct local_i915_gem_userptr {
 	uint64_t user_size;
 	uint32_t flags;
 #define I915_USERPTR_READ_ONLY		0x1
+#define I915_USERPTR_PROBE		0x2
+#define I915_USERPTR_POPULATE		0x4
 #define I915_USERPTR_UNSYNCHRONIZED	0x80000000
 	uint32_t handle;
 };
@@ -520,16 +522,14 @@ static bool gem_set_caching(int fd, uint32_t handle, int caching)
 	return do_ioctl(fd, LOCAL_IOCTL_I915_GEM_SET_CACHING, &arg) == 0;
 }
 
-static uint32_t gem_userptr(int fd, void *ptr, size_t size, int read_only)
+static uint32_t gem_userptr(int fd, void *ptr, size_t size, unsigned int flags)
 {
 	struct local_i915_gem_userptr arg;
 
 	VG_CLEAR(arg);
 	arg.user_ptr = (uintptr_t)ptr;
 	arg.user_size = size;
-	arg.flags = I915_USERPTR_UNSYNCHRONIZED;
-	if (read_only)
-		arg.flags |= I915_USERPTR_READ_ONLY;
+	arg.flags = flags | I915_USERPTR_UNSYNCHRONIZED;
 
 	if (DBG_NO_UNSYNCHRONIZED_USERPTR ||
 	    do_ioctl(fd, LOCAL_IOCTL_I915_GEM_USERPTR, &arg)) {
@@ -1390,6 +1390,29 @@ static bool test_has_userptr(struct kgem *kgem)
 	return arg.handle != 0;
 }
 
+static bool test_has_userptr_probe(struct kgem *kgem)
+{
+	struct local_i915_gem_userptr arg;
+	void *ptr;
+
+	if (DBG_NO_USERPTR)
+		return false;
+
+	if (posix_memalign(&ptr, PAGE_SIZE, PAGE_SIZE))
+		return false;
+
+	VG_CLEAR(arg);
+	arg.user_ptr = (uintptr_t)ptr;
+	arg.user_size = PAGE_SIZE;
+	arg.flags = I915_USERPTR_PROBE;
+
+	if (do_ioctl(kgem->fd, LOCAL_IOCTL_I915_GEM_USERPTR, &arg) == 0)
+		gem_close(kgem->fd, arg.handle);
+
+	free(ptr);
+	return arg.handle != 0;
+}
+
 static bool test_has_create2(struct kgem *kgem)
 {
 #if defined(USE_CREATE2)
@@ -2034,6 +2057,12 @@ void kgem_init(struct kgem *kgem, int fd, struct pci_device *dev, unsigned gen)
 	DBG(("%s: has userptr? %d\n", __FUNCTION__,
 	     kgem->has_userptr));
 
+	kgem->has_userptr_probe = false;
+	if (kgem->has_userptr)
+		kgem->has_userptr_probe = test_has_userptr_probe(kgem);
+	DBG(("%s: has userptr probe? %d\n", __FUNCTION__,
+	     kgem->has_userptr_probe));
+
 	kgem->has_create2 = test_has_create2(kgem);
 	DBG(("%s: has create2? %d\n", __FUNCTION__,
 	     kgem->has_create2));
@@ -7038,15 +7067,25 @@ static bool probe(struct kgem *kgem, uint32_t handle)
 		.read_domains = I915_GEM_DOMAIN_CPU,
 	};
 
+	if (kgem->has_userptr_probe)
+		return true;
+
 	return do_ioctl(kgem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &arg) == 0;
 }
 
 static uint32_t probe_userptr(struct kgem *kgem,
 			      void *ptr, size_t size, int read_only)
 {
+	unsigned int flags;
 	uint32_t handle;
 
-	handle = gem_userptr(kgem->fd, ptr, size, read_only);
+	flags = 0;
+	if (read_only)
+		flags |= I915_USERPTR_READ_ONLY;
+	if (kgem->has_userptr_probe)
+		flags |= I915_USERPTR_PROBE;
+
+	handle = gem_userptr(kgem->fd, ptr, size, flags);
 	if (handle && !probe(kgem, handle)) {
 		gem_close(kgem->fd, handle);
 		handle = 0;
diff --git a/src/sna/kgem.h b/src/sna/kgem.h
index 6a087a574..b62f956c6 100644
--- a/src/sna/kgem.h
+++ b/src/sna/kgem.h
@@ -183,6 +183,7 @@ struct kgem {
 
 	uint32_t has_create2 :1;
 	uint32_t has_userptr :1;
+	uint32_t has_userptr_probe :1;
 	uint32_t has_blt :1;
 	uint32_t has_relaxed_fencing :1;
 	uint32_t has_relaxed_delta :1;
-- 
2.30.0

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

             reply index

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-15 22:07 Chris Wilson [this message]
2021-01-15 23:42 ` [Intel-gfx] ✗ Fi.CI.BUILD: failure for " Patchwork

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=20210115220744.1402642-1-chris@chris-wilson.co.uk \
    --to=chris@chris-wilson.co.uk \
    --cc=intel-gfx@lists.freedesktop.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

Intel-GFX Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/intel-gfx/0 intel-gfx/git/0.git
	git clone --mirror https://lore.kernel.org/intel-gfx/1 intel-gfx/git/1.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 intel-gfx intel-gfx/ https://lore.kernel.org/intel-gfx \
		intel-gfx@lists.freedesktop.org
	public-inbox-index intel-gfx

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.freedesktop.lists.intel-gfx


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git