[Intel-gfx] [PATCH xf86-video-intel] sna: Probe userptr storage on construction

Chris Wilson chris at chris-wilson.co.uk
Fri Jan 15 22:07:44 UTC 2021


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 at 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



More information about the Intel-gfx mailing list