Skip to contentSkip to conversation

drm/i915: Allow i915 to manage the vma offset nodes instead of drm core

PATCH Superseded Abdiel Janulgue 4 participants 25 messages Aug 26, 2019 – Oct 7, 2019 1 message not in the archive

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 drm/i915: Allow i915 to manage the vma offset nodes instead of drm core

Have i915 replace the core drm_gem_mmap implementation to overcome its limitation in having only a single mmap offset node per gem object. The change allows us to have multiple mmap offsets per object. This enables a mmapping instance to use unique fault-handlers per user vma.

This enables us to store extra data within vma->vm_private_data and assign the pagefault ops for each mmap instance allowing objects to use multiple fault handlers depending on its backing storage.

Cc Joonas Lahtinen <[email protected]>
Cc Chris Wilson <[email protected]>
---
 drivers/gpu/drm/i915/gem/i915_gem_mman.c      | 193 ++++++++++++++++--
 drivers/gpu/drm/i915/gem/i915_gem_object.c    |  19 ++
 drivers/gpu/drm/i915/gem/i915_gem_object.h    |   7 +-
 .../gpu/drm/i915/gem/i915_gem_object_types.h  |  18 ++
 .../drm/i915/gem/selftests/i915_gem_mman.c    |  12 +-
 drivers/gpu/drm/i915/gt/intel_reset.c         |  11 +-
 drivers/gpu/drm/i915/i915_drv.c               |   9 +-
 drivers/gpu/drm/i915/i915_drv.h               |   1 +
 drivers/gpu/drm/i915/i915_vma.c               |  20 +-
 9 files changed, 254 insertions(+), 36 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_mman.c +175 -18
index 595539a09e38..fb7e39f115d7 100644
@@ -218,7 +218,8 @@ vm_fault_t i915_gem_fault(struct vm_fault *vmf)
 {
 #define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT)
 	struct vm_area_struct *area = vmf->vma;
-	struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
+	struct i915_mmap_offset *priv = area->vm_private_data;
+	struct drm_i915_gem_object *obj = priv->obj;
 	struct drm_device *dev = obj->base.dev;
 	struct drm_i915_private *i915 = to_i915(dev);
 	struct intel_runtime_pm *rpm = &i915->runtime_pm;
@@ -372,13 +373,20 @@ vm_fault_t i915_gem_fault(struct vm_fault *vmf)
 void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
 {
 	struct i915_vma *vma;
+	struct i915_mmap_offset *mmo;
 
 	GEM_BUG_ON(!obj->userfault_count);
 
 	obj->userfault_count = 0;
 	list_del(&obj->userfault_link);
-	drm_vma_node_unmap(&obj->base.vma_node,
-			   obj->base.dev->anon_inode->i_mapping);
+
+	mutex_lock(&obj->mmo_lock);
+	list_for_each_entry(mmo, &obj->mmap_offsets, offset) {
+		if (mmo->mmap_type == I915_MMAP_TYPE_GTT)
+			drm_vma_node_unmap(&mmo->vma_node,
+					   obj->base.dev->anon_inode->i_mapping);
+	}
+	mutex_unlock(&obj->mmo_lock);
 
 	for_each_ggtt_vma(vma, obj)
 		i915_vma_unset_userfault(vma);
@@ -433,14 +441,33 @@ void i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
 	intel_runtime_pm_put(&i915->runtime_pm, wakeref);
 }
 
-static int create_mmap_offset(struct drm_i915_gem_object *obj)
+static void init_mmap_offset(struct drm_i915_gem_object *obj,
+			     struct i915_mmap_offset *mmo)
+{
+	mutex_lock(&obj->mmo_lock);
+	kref_init(&mmo->ref);
+	list_add(&mmo->offset, &obj->mmap_offsets);
+	mutex_unlock(&obj->mmo_lock);
+
+	mmo->obj = obj;
+}
+
+static int create_mmap_offset(struct drm_i915_gem_object *obj,
+			      struct i915_mmap_offset *mmo)
 {
 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
+	struct drm_device *dev = obj->base.dev;
 	int err;
 
-	err = drm_gem_create_mmap_offset(&obj->base);
-	if (likely(!err))
+	drm_vma_node_reset(&mmo->vma_node);
+	if (mmo->file)
+		drm_vma_node_allow(&mmo->vma_node, mmo->file);
+	err = drm_vma_offset_add(dev->vma_offset_manager, &mmo->vma_node,
+				 obj->base.size / PAGE_SIZE);
+	if (likely(!err)) {
+		init_mmap_offset(obj, mmo);
 		return 0;
+	}
 
 	/* Attempt to reap some mmap space from dead objects */
 	do {
@@ -451,32 +478,48 @@ static int create_mmap_offset(struct drm_i915_gem_object *obj)
 			break;
 
 		i915_gem_drain_freed_objects(i915);
-		err = drm_gem_create_mmap_offset(&obj->base);
-		if (!err)
+		err = drm_vma_offset_add(dev->vma_offset_manager, &mmo->vma_node,
+					 obj->base.size / PAGE_SIZE);
+		if (!err) {
+			init_mmap_offset(obj, mmo);
 			break;
+		}
 
 	} while (flush_delayed_work(&i915->gem.retire_work));
 
 	return err;
 }
 
-int
-i915_gem_mmap_gtt(struct drm_file *file,
-		  struct drm_device *dev,
-		  u32 handle,
-		  u64 *offset)
+static int
+__assign_gem_object_mmap_data(struct drm_file *file,
+			      u32 handle,
+			      enum i915_mmap_type mmap_type,
+			      u64 *offset)
 {
 	struct drm_i915_gem_object *obj;
+	struct i915_mmap_offset *mmo;
 	int ret;
 
 	obj = i915_gem_object_lookup(file, handle);
 	if (!obj)
 		return -ENOENT;
 
-	ret = create_mmap_offset(obj);
-	if (ret == 0)
-		*offset = drm_vma_node_offset_addr(&obj->base.vma_node);
+	mmo = kzalloc(sizeof(*mmo), GFP_KERNEL);
+	if (!mmo) {
+		ret = -ENOMEM;
+		goto err;
+	}
+
+	mmo->file = file;
+	ret = create_mmap_offset(obj, mmo);
+	if (ret) {
+		kfree(mmo);
+		goto err;
+	}
 
+	mmo->mmap_type = mmap_type;
+	*offset = drm_vma_node_offset_addr(&mmo->vma_node);
+err:
 	i915_gem_object_put(obj);
 	return ret;
 }
@@ -500,9 +543,123 @@ int
 i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
 			struct drm_file *file)
 {
-	struct drm_i915_gem_mmap_gtt *args = data;
+	struct drm_i915_gem_mmap_offset *args = data;
+
+	return __assign_gem_object_mmap_data(file, args->handle,
+					     I915_MMAP_TYPE_GTT,
+					     &args->offset);
+}
+
+void i915_mmap_offset_object_release(struct kref *ref)
+{
+	struct i915_mmap_offset *mmo = container_of(ref,
+						    struct i915_mmap_offset,
+						    ref);
+	struct drm_i915_gem_object *obj = mmo->obj;
+	struct drm_device *dev = obj->base.dev;
+
+	lockdep_assert_held(&obj->mmo_lock);
+
+	if (mmo->file)
+		drm_vma_node_revoke(&mmo->vma_node, mmo->file);
+	drm_vma_offset_remove(dev->vma_offset_manager, &mmo->vma_node);
+	list_del(&mmo->offset);
 
-	return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
+	kfree(mmo);
+}
+
+static void i915_gem_vm_open(struct vm_area_struct *vma)
+{
+	struct i915_mmap_offset *priv = vma->vm_private_data;
+	struct drm_i915_gem_object *obj = priv->obj;
+
+	i915_gem_object_get(obj);
+	kref_get(&priv->ref);
+}
+
+static void i915_gem_vm_close(struct vm_area_struct *vma)
+{
+	struct i915_mmap_offset *priv = vma->vm_private_data;
+	struct drm_i915_gem_object *obj = priv->obj;
+
+	mutex_lock(&obj->mmo_lock);
+	kref_put(&priv->ref, i915_mmap_offset_object_release);
+	mutex_unlock(&obj->mmo_lock);
+
+	i915_gem_object_put(obj);
+}
+
+static const struct vm_operations_struct i915_gem_gtt_vm_ops = {
+	.fault = i915_gem_fault,
+	.open = i915_gem_vm_open,
+	.close = i915_gem_vm_close,
+};
+
+/* This overcomes the limitation in drm_gem_mmap's assignment of a
+ * drm_gem_object as the vma->vm_private_data. Since we need to
+ * be able to resolve multiple mmap offsets which could be tied
+ * to a single gem object.
+ */
+int i915_gem_mmap(struct file *filp, struct vm_area_struct *vma)
+{
+	struct drm_vma_offset_node *node;
+	struct drm_file *priv = filp->private_data;
+	struct drm_device *dev = priv->minor->dev;
+	struct i915_mmap_offset *mmo = NULL;
+	struct drm_gem_object *obj = NULL;
+
+	if (drm_dev_is_unplugged(dev))
+		return -ENODEV;
+
+	drm_vma_offset_lock_lookup(dev->vma_offset_manager);
+	node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
+						  vma->vm_pgoff,
+						  vma_pages(vma));
+	if (likely(node)) {
+		mmo = container_of(node, struct i915_mmap_offset,
+				   vma_node);
+		/*
+		 * Skip 0-refcnted objects as it is in the process of being
+		 * destroyed and will be invalid when the vma manager lock
+		 * is released.
+		 */
+		obj = &mmo->obj->base;
+		if (!kref_get_unless_zero(&obj->refcount))
+			obj = NULL;
+	}
+	drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
+
+	if (!obj)
+		return -EINVAL;
+
+	if (!drm_vma_node_is_allowed(node, priv)) {
+		drm_gem_object_put_unlocked(obj);
+		return -EACCES;
+	}
+
+	if (to_intel_bo(obj)->readonly) {
+		if (vma->vm_flags & VM_WRITE) {
+			drm_gem_object_put_unlocked(obj);
+			return -EINVAL;
+		}
+
+		vma->vm_flags &= ~VM_MAYWRITE;
+	}
+
+	vma->vm_flags |= VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
+	vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
+	vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
+	vma->vm_private_data = mmo;
+
+	vma->vm_ops = &i915_gem_gtt_vm_ops;
+
+	/*
+	 * Take a ref for our mmap_offset object. The reference is cleaned
+	 * up when the vma is closed.
+	 */
+	kref_get(&mmo->ref);
+
+	return 0;
 }
 
 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
drivers/gpu/drm/i915/gem/i915_gem_object.c +19 -0
index d7855dc5a5c5..eb3d7f3528f3 100644
@@ -58,6 +58,9 @@ void i915_gem_object_init(struct drm_i915_gem_object *obj,
 
 	INIT_LIST_HEAD(&obj->lut_list);
 
+	mutex_init(&obj->mmo_lock);
+	INIT_LIST_HEAD(&obj->mmap_offsets);
+
 	init_rcu_head(&obj->rcu);
 
 	obj->ops = ops;
@@ -94,6 +97,7 @@ void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
 	struct drm_i915_gem_object *obj = to_intel_bo(gem);
 	struct drm_i915_file_private *fpriv = file->driver_priv;
 	struct i915_lut_handle *lut, *ln;
+	struct i915_mmap_offset *mmo, *on;
 	LIST_HEAD(close);
 
 	i915_gem_object_lock(obj);
@@ -108,6 +112,13 @@ void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
 	}
 	i915_gem_object_unlock(obj);
 
+	mutex_lock(&obj->mmo_lock);
+	list_for_each_entry_safe(mmo, on, &obj->mmap_offsets, offset) {
+		if (mmo->file == file)
+			kref_put(&mmo->ref, i915_mmap_offset_object_release);
+	}
+	mutex_unlock(&obj->mmo_lock);
+
 	list_for_each_entry_safe(lut, ln, &close, obj_link) {
 		struct i915_gem_context *ctx = lut->ctx;
 		struct i915_vma *vma;
@@ -156,6 +167,7 @@ static void __i915_gem_free_objects(struct drm_i915_private *i915,
 	wakeref = intel_runtime_pm_get(&i915->runtime_pm);
 	llist_for_each_entry_safe(obj, on, freed, freed) {
 		struct i915_vma *vma, *vn;
+		struct i915_mmap_offset *mmo, *on;
 
 		trace_i915_gem_object_destroy(obj);
 
@@ -169,6 +181,13 @@ static void __i915_gem_free_objects(struct drm_i915_private *i915,
 		GEM_BUG_ON(!list_empty(&obj->vma.list));
 		GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma.tree));
 
+		i915_gem_object_release_mmap(obj);
+
+		mutex_lock(&obj->mmo_lock);
+		list_for_each_entry_safe(mmo, on, &obj->mmap_offsets, offset)
+			kref_put(&mmo->ref, i915_mmap_offset_object_release);
+		mutex_unlock(&obj->mmo_lock);
+
 		mutex_unlock(&i915->drm.struct_mutex);
 
 		GEM_BUG_ON(atomic_read(&obj->bind_count));
drivers/gpu/drm/i915/gem/i915_gem_object.h +5 -2
index 5efb9936e05b..d667ed8bb0a6 100644
@@ -125,13 +125,13 @@ void i915_gem_object_unlock_fence(struct drm_i915_gem_object *obj,
 static inline void
 i915_gem_object_set_readonly(struct drm_i915_gem_object *obj)
 {
-	obj->base.vma_node.readonly = true;
+	obj->readonly = true;
 }
 
 static inline bool
 i915_gem_object_is_readonly(const struct drm_i915_gem_object *obj)
 {
-	return obj->base.vma_node.readonly;
+	return obj->readonly;
 }
 
 static inline bool
@@ -423,6 +423,9 @@ int i915_gem_object_wait(struct drm_i915_gem_object *obj,
 int i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
 				  unsigned int flags,
 				  const struct i915_sched_attr *attr);
+
+void i915_mmap_offset_object_release(struct kref *ref);
+
 #define I915_PRIORITY_DISPLAY I915_USER_PRIORITY(I915_PRIORITY_MAX)
 
 #endif
drivers/gpu/drm/i915/gem/i915_gem_object_types.h +18 -0
index ede0eb4218a8..d74ddb479318 100644
@@ -60,6 +60,19 @@ struct drm_i915_gem_object_ops {
 	void (*release)(struct drm_i915_gem_object *obj);
 };
 
+enum i915_mmap_type {
+	I915_MMAP_TYPE_GTT = 0,
+};
+
+struct i915_mmap_offset {
+	struct drm_vma_offset_node vma_node;
+	struct drm_i915_gem_object *obj;
+	struct drm_file *file;
+	enum i915_mmap_type mmap_type;
+	struct kref ref;
+	struct list_head offset;
+};
+
 struct drm_i915_gem_object {
 	struct drm_gem_object base;
 
@@ -115,6 +128,11 @@ struct drm_i915_gem_object {
 	unsigned int userfault_count;
 	struct list_head userfault_link;
 
+	/* Protects access to mmap offsets */
+	struct mutex mmo_lock;
+	struct list_head mmap_offsets;
+	bool readonly:1;
+
 	I915_SELFTEST_DECLARE(struct list_head st_link);
 
 	/*
drivers/gpu/drm/i915/gem/selftests/i915_gem_mman.c +10 -2
index 1d27babff0ce..a7513421b1db 100644
@@ -371,15 +371,20 @@ static bool assert_mmap_offset(struct drm_i915_private *i915,
 			       int expected)
 {
 	struct drm_i915_gem_object *obj;
+	/* refcounted in create_mmap_offset */
+	struct i915_mmap_offset *mmo = kzalloc(sizeof(*mmo), GFP_KERNEL);
 	int err;
 
 	obj = i915_gem_object_create_internal(i915, size);
 	if (IS_ERR(obj))
 		return PTR_ERR(obj);
 
-	err = create_mmap_offset(obj);
+	err = create_mmap_offset(obj, mmo);
+	if (err)
+		kfree(mmo);
 	i915_gem_object_put(obj);
 
+
 	return err == expected;
 }
 
@@ -422,6 +427,8 @@ static int igt_mmap_offset_exhaustion(void *arg)
 	struct drm_mm *mm = &i915->drm.vma_offset_manager->vm_addr_space_mm;
 	struct drm_i915_gem_object *obj;
 	struct drm_mm_node resv, *hole;
+	/* refcounted in create_mmap_offset */
+	struct i915_mmap_offset *mmo = kzalloc(sizeof(*mmo), GFP_KERNEL);
 	u64 hole_start, hole_end;
 	int loop, err;
 
@@ -465,9 +472,10 @@ static int igt_mmap_offset_exhaustion(void *arg)
 		goto out;
 	}
 
-	err = create_mmap_offset(obj);
+	err = create_mmap_offset(obj, mmo);
 	if (err) {
 		pr_err("Unable to insert object into reclaimed hole\n");
+		kfree(mmo);
 		goto err_obj;
 	}
 
drivers/gpu/drm/i915/gt/intel_reset.c +10 -1
index b9d84d52e986..7b82ef888b96 100644
@@ -628,6 +628,7 @@ static void revoke_mmaps(struct intel_gt *gt)
 
 	for (i = 0; i < gt->ggtt->num_fences; i++) {
 		struct drm_vma_offset_node *node;
+		struct i915_mmap_offset *mmo;
 		struct i915_vma *vma;
 		u64 vma_offset;
 
@@ -641,10 +642,18 @@ static void revoke_mmaps(struct intel_gt *gt)
 		GEM_BUG_ON(vma->fence != &gt->ggtt->fence_regs[i]);
 		node = &vma->obj->base.vma_node;
 		vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT;
-		unmap_mapping_range(gt->i915->drm.anon_inode->i_mapping,
+
+		list_for_each_entry(mmo, &vma->obj->mmap_offsets, offset) {
+			node = &mmo->vma_node;
+			if (!drm_mm_node_allocated(&node->vm_node) ||
+			    mmo->mmap_type != I915_MMAP_TYPE_GTT)
+				continue;
+
+			unmap_mapping_range(gt->i915->drm.anon_inode->i_mapping,
 				    drm_vma_node_offset_addr(node) + vma_offset,
 				    vma->size,
 				    1);
+		}
 	}
 }
 
drivers/gpu/drm/i915/i915_drv.c +1 -8
index 1c4576a4a5e9..bdd597200984 100644
@@ -2722,18 +2722,12 @@ const struct dev_pm_ops i915_pm_ops = {
 	.runtime_resume = intel_runtime_resume,
 };
 
-static const struct vm_operations_struct i915_gem_vm_ops = {
-	.fault = i915_gem_fault,
-	.open = drm_gem_vm_open,
-	.close = drm_gem_vm_close,
-};
-
 static const struct file_operations i915_driver_fops = {
 	.owner = THIS_MODULE,
 	.open = drm_open,
 	.release = drm_release,
 	.unlocked_ioctl = drm_ioctl,
-	.mmap = drm_gem_mmap,
+	.mmap = i915_gem_mmap,
 	.poll = drm_poll,
 	.read = drm_read,
 	.compat_ioctl = i915_compat_ioctl,
@@ -2822,7 +2816,6 @@ static struct drm_driver driver = {
 
 	.gem_close_object = i915_gem_close_object,
 	.gem_free_object_unlocked = i915_gem_free_object,
-	.gem_vm_ops = &i915_gem_vm_ops,
 
 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
drivers/gpu/drm/i915/i915_drv.h +1 -0
index b42651a387d9..bf2927e88e14 100644
@@ -2327,6 +2327,7 @@ int i915_gem_wait_for_idle(struct drm_i915_private *dev_priv,
 void i915_gem_suspend(struct drm_i915_private *dev_priv);
 void i915_gem_suspend_late(struct drm_i915_private *dev_priv);
 void i915_gem_resume(struct drm_i915_private *dev_priv);
+int i915_gem_mmap(struct file *filp, struct vm_area_struct *vma);
 vm_fault_t i915_gem_fault(struct vm_fault *vmf);
 
 int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file);
drivers/gpu/drm/i915/i915_vma.c +15 -5
index e0e677b2a3a9..677323644319 100644
@@ -861,7 +861,8 @@ static void __i915_vma_iounmap(struct i915_vma *vma)
 
 void i915_vma_revoke_mmap(struct i915_vma *vma)
 {
-	struct drm_vma_offset_node *node = &vma->obj->base.vma_node;
+	struct drm_vma_offset_node *node;
+	struct i915_mmap_offset *mmo;
 	u64 vma_offset;
 
 	lockdep_assert_held(&vma->vm->mutex);
@@ -873,10 +874,19 @@ void i915_vma_revoke_mmap(struct i915_vma *vma)
 	GEM_BUG_ON(!vma->obj->userfault_count);
 
 	vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT;
-	unmap_mapping_range(vma->vm->i915->drm.anon_inode->i_mapping,
-			    drm_vma_node_offset_addr(node) + vma_offset,
-			    vma->size,
-			    1);
+
+	list_for_each_entry(mmo, &vma->obj->mmap_offsets, offset) {
+		node = &mmo->vma_node;
+		/* Only gtt-mmaps for this vma should be unmapped */
+		if (!drm_mm_node_allocated(&node->vm_node) ||
+		    mmo->mmap_type != I915_MMAP_TYPE_GTT)
+			continue;
+
+		unmap_mapping_range(vma->vm->i915->drm.anon_inode->i_mapping,
+				    drm_vma_node_offset_addr(node) + vma_offset,
+				    vma->size,
+				    1);
+	}
 
 	i915_vma_unset_userfault(vma);
 	if (!--vma->obj->userfault_count)
Signature · 6 lines
2.23.0

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#1 lore raw

PATCH 2/5 drm/i915: Introduce DRM_I915_GEM_MMAP_OFFSET

This is really just an alias of mmap_gtt. Add a new CPU mmap implementation that allows multiple fault handlers that depends on the object's backing pages.

Note that we multiplex mmap_gtt and mmap_offset through the same ioctl, and use the zero extending behaviour of drm to differentiate between them, when we inspect the flags.

---
 drivers/gpu/drm/i915/gem/i915_gem_mman.c      | 34 ++++++++++++++++++-
 .../gpu/drm/i915/gem/i915_gem_object_types.h  |  3 ++
 drivers/gpu/drm/i915/i915_drv.c               |  2 +-
 include/uapi/drm/i915_drm.h                   | 28 +++++++++++++++
 4 files changed, 65 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_mman.c +33 -1
index fb7e39f115d7..87690aa64814 100644
@@ -143,6 +143,9 @@ static unsigned int tile_row_pages(const struct drm_i915_gem_object *obj)
  * 3 - Remove implicit set-domain(GTT) and synchronisation on initial
  *     pagefault; swapin remains transparent.
  *
+ * 4 - Support multiple fault handlers per object depending on object's
+ *     backing storage (a.k.a. MMAP_OFFSET).
+ *
  * Restrictions:
  *
  *  * snoopable objects cannot be accessed via the GTT. It can cause machine
@@ -170,7 +173,7 @@ static unsigned int tile_row_pages(const struct drm_i915_gem_object *obj)
  */
 int i915_gem_mmap_gtt_version(void)
 {
-	return 3;
+	return 4;
 }
 
 static inline struct i915_ggtt_view
@@ -524,6 +527,27 @@ __assign_gem_object_mmap_data(struct drm_file *file,
 	return ret;
 }
 
+static int gem_mmap_offset(struct drm_device *dev, void *data,
+			   struct drm_file *file)
+{
+	struct drm_i915_gem_mmap_offset *args = data;
+	enum i915_mmap_type type;
+
+	if ((args->flags & (I915_MMAP_OFFSET_WC | I915_MMAP_OFFSET_WB)) &&
+	    !boot_cpu_has(X86_FEATURE_PAT))
+		return -ENODEV;
+
+	if (args->flags & I915_MMAP_OFFSET_WC)
+		type = I915_MMAP_TYPE_OFFSET_WC;
+	else if (args->flags & I915_MMAP_OFFSET_WB)
+		type = I915_MMAP_TYPE_OFFSET_WB;
+	else if (args->flags & I915_MMAP_OFFSET_UC)
+		type = I915_MMAP_TYPE_OFFSET_UC;
+
+	return __assign_gem_object_mmap_data(file, args->handle, type,
+					     &args->offset);
+}
+
 /**
  * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  * @dev: DRM device
@@ -544,6 +568,14 @@ i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
 			struct drm_file *file)
 {
 	struct drm_i915_gem_mmap_offset *args = data;
+	struct drm_i915_private *i915 = to_i915(dev);
+
+	if (args->flags & I915_MMAP_OFFSET_FLAGS)
+		return gem_mmap_offset(dev, data, file);
+
+	if (!HAS_MAPPABLE_APERTURE(i915))
+		/* No aperture, cannot mmap via legacy GTT */
+		return -ENODEV;
 
 	return __assign_gem_object_mmap_data(file, args->handle,
 					     I915_MMAP_TYPE_GTT,
drivers/gpu/drm/i915/gem/i915_gem_object_types.h +3 -0
index d74ddb479318..cf7712aea2d9 100644
@@ -62,6 +62,9 @@ struct drm_i915_gem_object_ops {
 
 enum i915_mmap_type {
 	I915_MMAP_TYPE_GTT = 0,
+	I915_MMAP_TYPE_OFFSET_WC,
+	I915_MMAP_TYPE_OFFSET_WB,
+	I915_MMAP_TYPE_OFFSET_UC,
 };
 
 struct i915_mmap_offset {
drivers/gpu/drm/i915/i915_drv.c +1 -1
index bdd597200984..68ed4499fa52 100644
@@ -2774,7 +2774,7 @@ static const struct drm_ioctl_desc i915_ioctls[] = {
 	DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_RENDER_ALLOW),
-	DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_OFFSET, i915_gem_mmap_gtt_ioctl, DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling_ioctl, DRM_RENDER_ALLOW),
include/uapi/drm/i915_drm.h +28 -0
index 469dc512cca3..75fc9277e9ab 100644
@@ -359,6 +359,7 @@ typedef struct _drm_i915_sarea {
 #define DRM_I915_QUERY			0x39
 #define DRM_I915_GEM_VM_CREATE		0x3a
 #define DRM_I915_GEM_VM_DESTROY		0x3b
+#define DRM_I915_GEM_MMAP_OFFSET	DRM_I915_GEM_MMAP_GTT
 /* Must be kept compact -- no holes */
 
 #define DRM_IOCTL_I915_INIT		DRM_IOW( DRM_COMMAND_BASE + DRM_I915_INIT, drm_i915_init_t)
@@ -421,6 +422,7 @@ typedef struct _drm_i915_sarea {
 #define DRM_IOCTL_I915_QUERY			DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_QUERY, struct drm_i915_query)
 #define DRM_IOCTL_I915_GEM_VM_CREATE	DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_VM_CREATE, struct drm_i915_gem_vm_control)
 #define DRM_IOCTL_I915_GEM_VM_DESTROY	DRM_IOW (DRM_COMMAND_BASE + DRM_I915_GEM_VM_DESTROY, struct drm_i915_gem_vm_control)
+#define DRM_IOCTL_I915_GEM_MMAP_OFFSET		DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_MMAP_OFFSET, struct drm_i915_gem_mmap_offset)
 
 /* Allow drivers to submit batchbuffers directly to hardware, relying
  * on the security mechanisms provided by hardware.
@@ -611,6 +613,7 @@ typedef struct drm_i915_irq_wait {
  * See I915_EXEC_FENCE_OUT and I915_EXEC_FENCE_SUBMIT.
  */
 #define I915_PARAM_HAS_EXEC_SUBMIT_FENCE 53
+
 /* Must be kept compact -- no holes and well documented */
 
 typedef struct drm_i915_getparam {
@@ -786,6 +789,31 @@ struct drm_i915_gem_mmap_gtt {
 	__u64 offset;
 };
 
+struct drm_i915_gem_mmap_offset {
+	/** Handle for the object being mapped. */
+	__u32 handle;
+	__u32 pad;
+	/**
+	 * Fake offset to use for subsequent mmap call
+	 *
+	 * This is a fixed-size type for 32/64 compatibility.
+	 */
+	__u64 offset;
+
+	/**
+	 * Flags for extended behaviour.
+	 *
+	 * It is mandatory that either one of the _WC/_WB flags
+	 * should be passed here.
+	 */
+	__u64 flags;
+#define I915_MMAP_OFFSET_WC (1 << 0)
+#define I915_MMAP_OFFSET_WB (1 << 1)
+#define I915_MMAP_OFFSET_UC (1 << 2)
+#define I915_MMAP_OFFSET_FLAGS \
+	(I915_MMAP_OFFSET_WC | I915_MMAP_OFFSET_WB | I915_MMAP_OFFSET_UC)
+};
+
 struct drm_i915_gem_set_domain {
 	/** Handle for the object */
 	__u32 handle;
Signature · 6 lines
2.23.0

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#2 lore raw

PATCH 3/5 drm/i915: Add cpu fault handler for mmap_offset

In reply to Abdiel Janulgue

Fault handler to handle missing pages for shmem-backed objects. Handle also logic needed to refault pages depending on fault handler usage.

---
 drivers/gpu/drm/i915/gem/i915_gem_domain.c |   2 +-
 drivers/gpu/drm/i915/gem/i915_gem_mman.c   | 189 ++++++++++++++++-----
 drivers/gpu/drm/i915/gem/i915_gem_object.h |   2 +-
 drivers/gpu/drm/i915/i915_gem.c            |   2 +-
 4 files changed, 149 insertions(+), 46 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_domain.c +1 -1
index 9c58e8fac1d9..517e73c3aa31 100644
@@ -254,7 +254,7 @@ int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
 			}
 
 			if (obj->userfault_count)
-				__i915_gem_object_release_mmap(obj);
+				__i915_gem_object_release_mmap_gtt(obj);
 
 			/*
 			 * As we no longer need a fence for GTT access,
drivers/gpu/drm/i915/gem/i915_gem_mman.c +146 -43

247 lines not on this page · raw message

drivers/gpu/drm/i915/gem/i915_gem_object.h +1 -1
index d667ed8bb0a6..012e5d1d99cc 100644
@@ -338,7 +338,7 @@ static inline void i915_gem_object_unpin_map(struct drm_i915_gem_object *obj)
 	i915_gem_object_unpin_pages(obj);
 }
 
-void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj);
+void __i915_gem_object_release_mmap_gtt(struct drm_i915_gem_object *obj);
 void i915_gem_object_release_mmap(struct drm_i915_gem_object *obj);
 
 void
drivers/gpu/drm/i915/i915_gem.c +1 -1
index 95e7c52cf8ed..363b35ba559d 100644
@@ -857,7 +857,7 @@ void i915_gem_runtime_suspend(struct drm_i915_private *i915)
 
 	list_for_each_entry_safe(obj, on,
 				 &i915->ggtt.userfault_list, userfault_link)
-		__i915_gem_object_release_mmap(obj);
+		__i915_gem_object_release_mmap_gtt(obj);
 
 	/*
 	 * The fence will be lost when the device powers down. If any were
Signature · 6 lines
2.23.0

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#3 lore raw

Re: [PATCH 3/5] drm/i915: Add cpu fault handler for mmap_offset

Quoting Abdiel Janulgue (2019-08-26 13:21:00)

Quoted 51 lines

Fault handler to handle missing pages for shmem-backed objects. Handle also logic needed to refault pages depending on fault handler usage.

---
 drivers/gpu/drm/i915/gem/i915_gem_domain.c |   2 +-
 drivers/gpu/drm/i915/gem/i915_gem_mman.c   | 189 ++++++++++++++++-----
 drivers/gpu/drm/i915/gem/i915_gem_object.h |   2 +-
 drivers/gpu/drm/i915/i915_gem.c            |   2 +-
 4 files changed, 149 insertions(+), 46 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_domain.c +1 -1
index 9c58e8fac1d9..517e73c3aa31 100644
@@ -254,7 +254,7 @@ int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
                        }
 
                        if (obj->userfault_count)
-                               __i915_gem_object_release_mmap(obj);
+                               __i915_gem_object_release_mmap_gtt(obj);
 
                        /*
                         * As we no longer need a fence for GTT access,
drivers/gpu/drm/i915/gem/i915_gem_mman.c +10 -0
index 87690aa64814..d0a653a9a4cc 100644
@@ -5,6 +5,7 @@
  */
 
 #include <linux/mman.h>
+#include <linux/pfn_t.h>
 #include <linux/sizes.h>
 
 #include "gt/intel_gt.h"
@@ -199,6 +200,45 @@ compute_partial_view(const struct drm_i915_gem_object *obj,
        return view;
 }
 
+static vm_fault_t i915_error_to_vmf_fault(int err, struct intel_gt *gt)
+{
+       switch (err) {
+       case -EIO:
+               /*
+                * We eat errors when the gpu is terminally wedged to avoid
+                * userspace unduly crashing (gl has no provisions for mmaps to
+                * fail). But any other -EIO isn't ours (e.g. swap in failure)
+                * and so needs to be reported.

drivers/gpu/drm/i915/gem/i915_gem_mman.c compute_partial_view(const struct drm_i915_gem_object *obj, · line 221

Quoted 10 lines
drivers/gpu/drm/i915/gem/i915_gem_mman.c +10 -0
+                */
+               if (!intel_gt_is_wedged(gt))
+                       return VM_FAULT_SIGBUS;
+               /* else, fall through */
+       case -EAGAIN:
+               /*
+                * EAGAIN means the gpu is hung and we'll wait for the error
+                * handler to reset everything when re-faulting in
+                * i915_mutex_lock_interruptible.
+                */

These errors don't exist anymore and should not be cargo-culted onto paths that could not possibly generate them.
-Chris
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#4 lore raw

Re: [PATCH 3/5] drm/i915: Add cpu fault handler for mmap_offset

In reply to Abdiel Janulgue

Quoting Abdiel Janulgue (2019-08-26 13:21:00)

drivers/gpu/drm/i915/gem/i915_gem_mman.c vm_fault_t i915_gem_fault(struct vm_fault *vmf) · line 410

Quoted 9 lines
drivers/gpu/drm/i915/gem/i915_gem_mman.c +9 -0
+       for (i = 0; i < size >> PAGE_SHIFT; i++) {
+               struct page *page = i915_gem_object_get_page(obj, i);
+
+               vmf_ret = vmf_insert_pfn(area,
+                                        (unsigned long)area->vm_start + i * PAGE_SIZE,
+                                        page_to_pfn(page));
+               if (vmf_ret & VM_FAULT_ERROR) {
+                       vmf_ret = i915_error_to_vmf_fault(vm_fault_to_errno(vmf_ret, 0),
+                                                         &dev_priv->gt);

Sigh. You take a mm/, run it through the internal error convertor that *only* applies to i915 errors, and then return the original fault code. -Chris
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#5 lore raw

PATCH 4/5 drm/i915: define HAS_MAPPABLE_APERTURE

In reply to Abdiel Janulgue

From: Daniele Ceraolo Spurio <[email protected]>

The following patches in the series will use it to avoid certain operations when aperture is not available in HW.

---
 drivers/gpu/drm/i915/i915_drv.h | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/i915/i915_drv.h +2 -0
index bf2927e88e14..4e1d2e0fcc86 100644
@@ -2110,6 +2110,8 @@ IS_SUBPLATFORM(const struct drm_i915_private *i915,
 #define OVERLAY_NEEDS_PHYSICAL(dev_priv) \
 		(INTEL_INFO(dev_priv)->display.overlay_needs_physical)
 
+#define HAS_MAPPABLE_APERTURE(dev_priv) (dev_priv->ggtt.mappable_end > 0)
+
 /* Early gen2 have a totally busted CS tlb and require pinned batches. */
 #define HAS_BROKEN_CS_TLB(dev_priv)	(IS_I830(dev_priv) || IS_I845G(dev_priv))
 
Signature · 6 lines
2.23.0

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#6 lore raw

PATCH 5/5 drm/i915: cpu-map based dumb buffers

In reply to Abdiel Janulgue

If there is no aperture we can't use map_gtt to map dumb buffers, so we need a cpu-map based path to do it. We prefer map_gtt on platforms that do have aperture.

Cc Daniele Ceraolo Spurio <[email protected]>
Cc Tvrtko Ursulin <[email protected]>
Cc Matthew Auld <[email protected]>
---
 drivers/gpu/drm/i915/gem/i915_gem_mman.c | 13 +++++++++++++
 drivers/gpu/drm/i915/i915_drv.c          |  2 +-
 drivers/gpu/drm/i915/i915_drv.h          |  2 +-
 3 files changed, 15 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_mman.c +13 -0
index d0a653a9a4cc..ff35b0517e59 100644
@@ -615,6 +615,19 @@ static int gem_mmap_offset(struct drm_device *dev, void *data,
 					     &args->offset);
 }
 
+int
+i915_gem_mmap_dumb(struct drm_file *file,
+		  struct drm_device *dev,
+		  u32 handle,
+		  u64 *offset)
+{
+	struct drm_i915_private *i915 = dev->dev_private;
+	enum i915_mmap_type mmap_type = HAS_MAPPABLE_APERTURE(i915) ?
+		I915_MMAP_TYPE_GTT : I915_MMAP_TYPE_OFFSET_WC;
+
+	return __assign_gem_object_mmap_data(file, handle, mmap_type, offset);
+}
+
 /**
  * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  * @dev: DRM device
drivers/gpu/drm/i915/i915_drv.c +1 -1
index 68ed4499fa52..40ccadc9ef29 100644
@@ -2826,7 +2826,7 @@ static struct drm_driver driver = {
 	.get_scanout_position = i915_get_crtc_scanoutpos,
 
 	.dumb_create = i915_gem_dumb_create,
-	.dumb_map_offset = i915_gem_mmap_gtt,
+	.dumb_map_offset = i915_gem_mmap_dumb,
 	.ioctls = i915_ioctls,
 	.num_ioctls = ARRAY_SIZE(i915_ioctls),
 	.fops = &i915_driver_fops,
drivers/gpu/drm/i915/i915_drv.h +1 -1
index 4e1d2e0fcc86..d3514184efef 100644
@@ -2300,7 +2300,7 @@ i915_mutex_lock_interruptible(struct drm_device *dev)
 int i915_gem_dumb_create(struct drm_file *file_priv,
 			 struct drm_device *dev,
 			 struct drm_mode_create_dumb *args);
-int i915_gem_mmap_gtt(struct drm_file *file_priv, struct drm_device *dev,
+int i915_gem_mmap_dumb(struct drm_file *file_priv, struct drm_device *dev,
 		      u32 handle, u64 *offset);
 int i915_gem_mmap_gtt_version(void);
 
Signature · 6 lines
2.23.0

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#7 lore raw

Re: [PATCH 5/5] drm/i915: cpu-map based dumb buffers

Quoting Abdiel Janulgue (2019-08-26 13:21:02)

drivers/gpu/drm/i915/gem/i915_gem_mman.c static int gem_mmap_offset(struct drm_device *dev, void *data, · line 626

Quoted 9 lines
drivers/gpu/drm/i915/gem/i915_gem_mman.c +9 -0
+int
+i915_gem_mmap_dumb(struct drm_file *file,
+                 struct drm_device *dev,
+                 u32 handle,
+                 u64 *offset)
+{
+       struct drm_i915_private *i915 = dev->dev_private;
+       enum i915_mmap_type mmap_type = HAS_MAPPABLE_APERTURE(i915) ?
+               I915_MMAP_TYPE_GTT : I915_MMAP_TYPE_OFFSET_WC;

Is there any reason for using GTT if we support WC? The buffer is meant to be linear.
-Chris
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#8 lore raw

Re: [PATCH 5/5] drm/i915: cpu-map based dumb buffers

Quoted 15 lines · Chris Wilson

Quoting Abdiel Janulgue (2019-08-26 13:21:02)

drivers/gpu/drm/i915/gem/i915_gem_mman.c static int gem_mmap_offset(struct drm_device *dev, void *data, · line 626

Quoted 9 lines
drivers/gpu/drm/i915/gem/i915_gem_mman.c +9 -0
+int
+i915_gem_mmap_dumb(struct drm_file *file,
+                 struct drm_device *dev,
+                 u32 handle,
+                 u64 *offset)
+{
+       struct drm_i915_private *i915 = dev->dev_private;
+       enum i915_mmap_type mmap_type = HAS_MAPPABLE_APERTURE(i915) ?
+               I915_MMAP_TYPE_GTT : I915_MMAP_TYPE_OFFSET_WC;

Is there any reason for using GTT if we support WC? The buffer is meant to be linear.
-Chris

Actually there seems no reason at all. Yeah dumb BO mmaps should probably be done via mmap_offset CPU WC mmaps.

-Abdiel
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#9 lore raw
In reply to Abdiel Janulgue

Quoting Abdiel Janulgue (2019-08-26 13:20:58)

drivers/gpu/drm/i915/gem/i915_gem_mman.c void i915_gem_object_release_mmap(struct drm_i915_gem_object *obj) · line 447

Quoted 5 lines
drivers/gpu/drm/i915/gem/i915_gem_mman.c +4 -1
-static int create_mmap_offset(struct drm_i915_gem_object *obj)
+static void init_mmap_offset(struct drm_i915_gem_object *obj,
+                            struct i915_mmap_offset *mmo)
+{
+       mutex_lock(&obj->mmo_lock);

This lock should only be guarding obj->mmap_offsets. You don't need to take it around every kref, just be careful to remove the link on close.

Quoted 28 lines
drivers/gpu/drm/i915/gem/i915_gem_mman.c +27 -0
+       kref_init(&mmo->ref);
+       list_add(&mmo->offset, &obj->mmap_offsets);
+       mutex_unlock(&obj->mmo_lock);
+
+       mmo->obj = obj;
+}
 
+/* This overcomes the limitation in drm_gem_mmap's assignment of a
+ * drm_gem_object as the vma->vm_private_data. Since we need to
+ * be able to resolve multiple mmap offsets which could be tied
+ * to a single gem object.
+ */
+int i915_gem_mmap(struct file *filp, struct vm_area_struct *vma)
+{
+       struct drm_vma_offset_node *node;
+       struct drm_file *priv = filp->private_data;
+       struct drm_device *dev = priv->minor->dev;
+       struct i915_mmap_offset *mmo = NULL;
+       struct drm_gem_object *obj = NULL;
+
+       if (drm_dev_is_unplugged(dev))
+               return -ENODEV;
+
+       drm_vma_offset_lock_lookup(dev->vma_offset_manager);
+       node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
+                                                 vma->vm_pgoff,
+                                                 vma_pages(vma));
+       if (likely(node)) {

drivers/gpu/drm/i915/gem/i915_gem_mman.c int · line 628

Quoted 10 lines
drivers/gpu/drm/i915/gem/i915_gem_mman.c +10 -0
+               mmo = container_of(node, struct i915_mmap_offset,
+                                  vma_node);
+               /*
+                * Skip 0-refcnted objects as it is in the process of being
+                * destroyed and will be invalid when the vma manager lock
+                * is released.
+                */
+               obj = &mmo->obj->base;
+               if (!kref_get_unless_zero(&obj->refcount))
+                       obj = NULL;

Hmm, references are still weird. This doesn't seem like it protects against

Thread A Thread B
mmap(fd, offset_of_A); gem_close(fd, A);

Time for a gem_mmap_gtt/close-race.
-Chris
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#10 lore raw

✗ Fi.CI.CHECKPATCH: warning for series starting with [1/5] drm/i915: Allow i915 to manage the vma offset nodes instead of drm core

In reply to Abdiel Janulgue
== Series Details ==

== Series Details ==

Series: series starting with [1/5] drm/i915: Allow i915 to manage the vma offset nodes instead of drm core
URL : https://patchwork.freedesktop.org/series/65793/
State : warning

== Summary ==

$ dim checkpatch origin/drm-tip
f5bf965f51a8 drm/i915: Allow i915 to manage the vma offset nodes instead of drm core
-:425: CHECK:LINE_SPACING: Please don't use multiple blank lines
#425: FILE: drivers/gpu/drm/i915/gem/selftests/i915_gem_mman.c:387:

+

-:475: CHECK:PARENTHESIS_ALIGNMENT: Alignment should match open parenthesis #475: FILE: drivers/gpu/drm/i915/gt/intel_reset.c:653:
+ unmap_mapping_range(gt->i915->drm.anon_inode->i_mapping,
drm_vma_node_offset_addr(node) + vma_offset,

total: 0 errors, 0 warnings, 2 checks, 484 lines checked b1e0a0f8d0d9 drm/i915: Introduce DRM_I915_GEM_MMAP_OFFSET -:127: WARNING:LONG_LINE: line over 100 characters #127: FILE: include/uapi/drm/i915_drm.h:425:
+#define DRM_IOCTL_I915_GEM_MMAP_OFFSET DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_MMAP_OFFSET, struct drm_i915_gem_mmap_offset)

total: 0 errors, 1 warnings, 0 checks, 127 lines checked
2824d8871cc6 drm/i915: Add cpu fault handler for mmap_offset
a15b2877b235 drm/i915: define HAS_MAPPABLE_APERTURE
-:20: CHECK:MACRO_ARG_PRECEDENCE: Macro argument 'dev_priv' may be better as '(dev_priv)' to avoid precedence issues #20: FILE: drivers/gpu/drm/i915/i915_drv.h:2113:
+#define HAS_MAPPABLE_APERTURE(dev_priv) (dev_priv->ggtt.mappable_end > 0)

total: 0 errors, 0 warnings, 1 checks, 8 lines checked db6a41bc2840 drm/i915: cpu-map based dumb buffers
-:25: CHECK:PARENTHESIS_ALIGNMENT: Alignment should match open parenthesis #25: FILE: drivers/gpu/drm/i915/gem/i915_gem_mman.c:620:
+i915_gem_mmap_dumb(struct drm_file *file,
+ struct drm_device *dev,

-:62: CHECK:PARENTHESIS_ALIGNMENT: Alignment should match open parenthesis #62: FILE: drivers/gpu/drm/i915/i915_drv.h:2304:
+int i915_gem_mmap_dumb(struct drm_file *file_priv, struct drm_device *dev,
u32 handle, u64 *offset);

total: 0 errors, 0 warnings, 2 checks, 35 lines checked

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#11 lore raw

✗ Fi.CI.SPARSE: warning for series starting with [1/5] drm/i915: Allow i915 to manage the vma offset nodes instead of drm core

In reply to Abdiel Janulgue
== Series Details ==

== Series Details ==

Series: series starting with [1/5] drm/i915: Allow i915 to manage the vma offset nodes instead of drm core
URL : https://patchwork.freedesktop.org/series/65793/
State : warning

== Summary ==

$ dim sparse origin/drm-tip
Sparse version: v0.6.0

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#12 lore raw
In reply to Abdiel Janulgue

Quoting Abdiel Janulgue (2019-08-26 13:20:58)

drivers/gpu/drm/i915/gt/intel_reset.c static void revoke_mmaps(struct intel_gt *gt) · line 650

Quoted 11 lines
drivers/gpu/drm/i915/gt/intel_reset.c +6 -1
@@ -641,10 +642,18 @@ static void revoke_mmaps(struct intel_gt *gt)
                GEM_BUG_ON(vma->fence != &gt->ggtt->fence_regs[i]);
                node = &vma->obj->base.vma_node;
                vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT;
-               unmap_mapping_range(gt->i915->drm.anon_inode->i_mapping,
+
+               list_for_each_entry(mmo, &vma->obj->mmap_offsets, offset) {
+                       node = &mmo->vma_node;
+                       if (!drm_mm_node_allocated(&node->vm_node) ||
+                           mmo->mmap_type != I915_MMAP_TYPE_GTT)
+                               continue;

That list needs locking as is not protected by the reset srcu (and you are not allowed your own locking in here, unless you have a jolly good reason and can prove it will never block a reset).

One thing to observe is is that you only ever need the mmo associated with a fence for revocation upon reset, and you could use a local list for tracking the active mmo.
-Chris
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#13 lore raw

✓ Fi.CI.BAT: success for series starting with [1/5] drm/i915: Allow i915 to manage the vma offset nodes instead of drm core

In reply to Abdiel Janulgue
== Series Details ==

== Series Details ==

Series: series starting with [1/5] drm/i915: Allow i915 to manage the vma offset nodes instead of drm core
URL : https://patchwork.freedesktop.org/series/65793/
State : success

== Summary ==

CI Bug Log - changes from CI_DRM_6784 -> Patchwork_14188
====================================================

Summary
-------

**SUCCESS**

No regressions found.

External URL: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/

Known issues
------------

Here are the changes found in Patchwork_14188 that come from known issues:

### IGT changes ###

#### Issues hit ####

* igt@kms_flip@basic-flip-vs-dpms:
- fi-hsw-4770r: [PASS][1] -> [DMESG-WARN][2] ([fdo#105602])
[1]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/fi-hsw-4770r/igt@[email protected]
[2]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/fi-hsw-4770r/igt@[email protected]

* igt@kms_frontbuffer_tracking@basic:
- fi-icl-u3: [PASS][3] -> [FAIL][4] ([fdo#103167])
[3]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/fi-icl-u3/igt@[email protected]
[4]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/fi-icl-u3/igt@[email protected]
- fi-icl-u2: [PASS][5] -> [FAIL][6] ([fdo#103167])
[5]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/fi-icl-u2/igt@[email protected]
[6]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/fi-icl-u2/igt@[email protected]

{name}: This element is suppressed. This means it is ignored when computing
the status of the difference (SUCCESS, WARNING, or FAILURE).

[fdo#103167]: https://bugs.freedesktop.org/show_bug.cgi?id=103167
[fdo#105602]: https://bugs.freedesktop.org/show_bug.cgi?id=105602
[fdo#111045]: https://bugs.freedesktop.org/show_bug.cgi?id=111045

Participating hosts (54 -> 43)
------------------------------

Missing (11): fi-kbl-soraka fi-ilk-m540 fi-bsw-n3050 fi-byt-j1900 fi-hsw-4200u fi-byt-squawks fi-bsw-cyan fi-icl-y fi-blb-e6850 fi-byt-clapper fi-bdw-samus

Build changes
-------------

* CI: CI-20190529 -> None
* Linux: CI_DRM_6784 -> Patchwork_14188

CI-20190529: 20190529
CI_DRM_6784: 38e1a03497a6e7d1dd271f3f0cd140127181053d @ git://anongit.freedesktop.org/gfx-ci/linux
IGT_5149: 6756ede680ee12745393360d7cc87cc0eb733ff6 @ git://anongit.freedesktop.org/xorg/app/intel-gpu-tools
Patchwork_14188: db6a41bc284019cc6835513c15c96d4e7c4a7c8f @ git://anongit.freedesktop.org/gfx-ci/linux

== Linux commits ==

db6a41bc2840 drm/i915: cpu-map based dumb buffers
a15b2877b235 drm/i915: define HAS_MAPPABLE_APERTURE
2824d8871cc6 drm/i915: Add cpu fault handler for mmap_offset b1e0a0f8d0d9 drm/i915: Introduce DRM_I915_GEM_MMAP_OFFSET
f5bf965f51a8 drm/i915: Allow i915 to manage the vma offset nodes instead of drm core

== Logs ==

For more details see: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#14 lore raw

✗ Fi.CI.IGT: failure for series starting with [1/5] drm/i915: Allow i915 to manage the vma offset nodes instead of drm core

In reply to Abdiel Janulgue
== Series Details ==

== Series Details ==

Series: series starting with [1/5] drm/i915: Allow i915 to manage the vma offset nodes instead of drm core
URL : https://patchwork.freedesktop.org/series/65793/
State : failure

== Summary ==

CI Bug Log - changes from CI_DRM_6784_full -> Patchwork_14188_full
====================================================

Summary
-------

**FAILURE**

Serious unknown changes coming with Patchwork_14188_full absolutely need to be verified manually.

If you think the reported changes have nothing to do with the changes introduced in Patchwork_14188_full, please notify your bug team to allow them to document this new failure mode, which will reduce false positives in CI.

Possible new issues
-------------------

Here are the unknown changes that may have been introduced in Patchwork_14188_full:

### IGT changes ###

#### Possible regressions ####

* igt@gem_persistent_relocs@forked-interruptible-faulting-reloc-thrashing:
- shard-hsw: [PASS][1] -> [TIMEOUT][2]
[1]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-hsw2/igt@gem_persistent_relocs@forked-interruptible-faulting-reloc-thrashing.html
[2]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-hsw2/igt@gem_persistent_relocs@forked-interruptible-faulting-reloc-thrashing.html

Known issues
------------

Here are the changes found in Patchwork_14188_full that come from known issues:

### IGT changes ###

#### Issues hit ####

* igt@gem_exec_schedule@reorder-wide-bsd:
- shard-iclb: [PASS][3] -> [SKIP][4] ([fdo#111325]) +7 similar issues
[3]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-iclb8/igt@[email protected]
[4]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-iclb2/igt@[email protected]

* igt@gem_tiled_swapping@non-threaded:
- shard-apl: [PASS][5] -> [DMESG-WARN][6] ([fdo#108686])
[5]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-apl7/igt@[email protected]
[6]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-apl7/igt@[email protected]
- shard-glk: [PASS][7] -> [DMESG-WARN][8] ([fdo#108686])
[7]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-glk2/igt@[email protected]
[8]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-glk2/igt@[email protected]

* igt@gem_workarounds@suspend-resume-context:
- shard-apl: [PASS][9] -> [DMESG-WARN][10] ([fdo#108566]) +4 similar issues
[9]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-apl6/igt@[email protected]
[10]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-apl3/igt@[email protected]

* igt@kms_flip@flip-vs-expired-vblank-interruptible:
- shard-glk: [PASS][11] -> [FAIL][12] ([fdo#105363]) +1 similar issue
[11]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-glk7/igt@[email protected]
[12]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-glk6/igt@[email protected]

* igt@kms_frontbuffer_tracking@fbcpsr-1p-primscrn-spr-indfb-draw-render:
- shard-iclb: [PASS][13] -> [FAIL][14] ([fdo#103167]) +2 similar issues
[13]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-iclb6/igt@kms_frontbuffer_tracking@fbcpsr-1p-primscrn-spr-indfb-draw-render.html
[14]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-iclb2/igt@kms_frontbuffer_tracking@fbcpsr-1p-primscrn-spr-indfb-draw-render.html

* igt@kms_plane_alpha_blend@pipe-c-coverage-7efc:
- shard-skl: [PASS][15] -> [FAIL][16] ([fdo#108145] / [fdo#110403])
[15]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-skl7/igt@[email protected]
[16]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-skl4/igt@[email protected]

* igt@kms_psr@psr2_cursor_plane_onoff:
- shard-iclb: [PASS][17] -> [SKIP][18] ([fdo#109441]) +1 similar issue
[17]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-iclb2/igt@kms_psr@psr2_cursor_plane_onoff.html
[18]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-iclb7/igt@kms_psr@psr2_cursor_plane_onoff.html

* igt@prime_busy@hang-bsd2:
- shard-iclb: [PASS][19] -> [SKIP][20] ([fdo#109276]) +23 similar issues
[19]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-iclb2/igt@[email protected]
[20]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-iclb8/igt@[email protected]

#### Possible fixes ####

* igt@gem_ctx_shared@exec-single-timeline-bsd:
- shard-iclb: [SKIP][21] ([fdo#110841]) -> [PASS][22]
[21]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-iclb2/igt@[email protected]
[22]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-iclb8/igt@[email protected]

* igt@gem_exec_schedule@wide-bsd:
- shard-iclb: [SKIP][23] ([fdo#111325]) -> [PASS][24] +4 similar issues
[23]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-iclb4/igt@[email protected]
[24]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-iclb3/igt@[email protected]

* igt@gem_exec_suspend@basic-s3:
- shard-kbl: [DMESG-WARN][25] ([fdo#108566]) -> [PASS][26]
[25]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-kbl3/igt@[email protected]
[26]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-kbl6/igt@[email protected]

* igt@gem_softpin@noreloc-s3:
- shard-skl: [INCOMPLETE][27] ([fdo#104108]) -> [PASS][28]
[27]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-skl6/igt@[email protected]
[28]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-skl6/igt@[email protected]

* igt@i915_suspend@sysfs-reader:
- shard-apl: [DMESG-WARN][29] ([fdo#108566]) -> [PASS][30] +3 similar issues
[29]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-apl4/igt@[email protected]
[30]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-apl5/igt@[email protected]

* igt@kms_cursor_legacy@2x-long-cursor-vs-flip-atomic:
- shard-hsw: [FAIL][31] ([fdo#105767]) -> [PASS][32]
[31]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-hsw1/igt@[email protected]
[32]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-hsw7/igt@[email protected]

* igt@kms_frontbuffer_tracking@fbc-1p-primscrn-indfb-pgflip-blt:
- shard-iclb: [FAIL][33] ([fdo#103167]) -> [PASS][34] +3 similar issues
[33]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-iclb2/igt@[email protected]
[34]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-iclb4/igt@[email protected]

* igt@kms_psr@psr2_sprite_plane_move:
- shard-iclb: [SKIP][35] ([fdo#109441]) -> [PASS][36] +2 similar issues
[35]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-iclb3/igt@kms_psr@psr2_sprite_plane_move.html
[36]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-iclb2/igt@kms_psr@psr2_sprite_plane_move.html

* igt@kms_setmode@basic:
- shard-apl: [FAIL][37] ([fdo#99912]) -> [PASS][38]
[37]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-apl5/igt@[email protected]
[38]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-apl6/igt@[email protected]

* igt@kms_vblank@pipe-b-query-busy:
- shard-apl: [INCOMPLETE][39] ([fdo#103927]) -> [PASS][40] +3 similar issues
[39]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-apl6/igt@[email protected]
[40]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-apl7/igt@[email protected]

* igt@perf@polling:
- shard-skl: [FAIL][41] ([fdo#110728]) -> [PASS][42]
[41]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-skl3/igt@[email protected]
[42]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-skl10/igt@[email protected]

* igt@prime_vgem@fence-wait-bsd2:
- shard-iclb: [SKIP][43] ([fdo#109276]) -> [PASS][44] +15 similar issues
[43]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-iclb6/igt@[email protected]
[44]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-iclb2/igt@[email protected]

#### Warnings ####

* igt@gem_ctx_isolation@vcs1-nonpriv:
- shard-iclb: [FAIL][45] ([fdo#111329]) -> [SKIP][46] ([fdo#109276])
[45]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-iclb2/igt@[email protected]
[46]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-iclb8/igt@[email protected]

* igt@gem_mocs_settings@mocs-reset-bsd2:
- shard-iclb: [FAIL][47] ([fdo#111330]) -> [SKIP][48] ([fdo#109276])
[47]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-iclb4/igt@[email protected]
[48]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-iclb6/igt@[email protected]

* igt@gem_mocs_settings@mocs-settings-bsd2:
- shard-iclb: [SKIP][49] ([fdo#109276]) -> [FAIL][50] ([fdo#111330]) +1 similar issue
[49]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-iclb8/igt@[email protected]
[50]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-iclb2/igt@[email protected]

* igt@kms_frontbuffer_tracking@fbc-badstride:
- shard-skl: [FAIL][51] ([fdo#108040]) -> [FAIL][52] ([fdo#103167])
[51]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-skl5/igt@[email protected]
[52]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-skl6/igt@[email protected]

* igt@kms_frontbuffer_tracking@fbcpsr-1p-primscrn-spr-indfb-fullscreen:
- shard-skl: [FAIL][53] ([fdo#103167]) -> [FAIL][54] ([fdo#108040])
[53]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_6784/shard-skl5/igt@kms_frontbuffer_tracking@fbcpsr-1p-primscrn-spr-indfb-fullscreen.html
[54]: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/shard-skl6/igt@kms_frontbuffer_tracking@fbcpsr-1p-primscrn-spr-indfb-fullscreen.html

{name}: This element is suppressed. This means it is ignored when computing
the status of the difference (SUCCESS, WARNING, or FAILURE).

[fdo#103167]: https://bugs.freedesktop.org/show_bug.cgi?id=103167
[fdo#103927]: https://bugs.freedesktop.org/show_bug.cgi?id=103927
[fdo#104108]: https://bugs.freedesktop.org/show_bug.cgi?id=104108
[fdo#105363]: https://bugs.freedesktop.org/show_bug.cgi?id=105363
[fdo#105767]: https://bugs.freedesktop.org/show_bug.cgi?id=105767
[fdo#108040]: https://bugs.freedesktop.org/show_bug.cgi?id=108040
[fdo#108145]: https://bugs.freedesktop.org/show_bug.cgi?id=108145
[fdo#108566]: https://bugs.freedesktop.org/show_bug.cgi?id=108566
[fdo#108686]: https://bugs.freedesktop.org/show_bug.cgi?id=108686
[fdo#109276]: https://bugs.freedesktop.org/show_bug.cgi?id=109276
[fdo#109441]: https://bugs.freedesktop.org/show_bug.cgi?id=109441
[fdo#110403]: https://bugs.freedesktop.org/show_bug.cgi?id=110403
[fdo#110728]: https://bugs.freedesktop.org/show_bug.cgi?id=110728
[fdo#110841]: https://bugs.freedesktop.org/show_bug.cgi?id=110841
[fdo#111325]: https://bugs.freedesktop.org/show_bug.cgi?id=111325
[fdo#111329]: https://bugs.freedesktop.org/show_bug.cgi?id=111329
[fdo#111330]: https://bugs.freedesktop.org/show_bug.cgi?id=111330
[fdo#99912]: https://bugs.freedesktop.org/show_bug.cgi?id=99912

Participating hosts (9 -> 9)
------------------------------

No changes in participating hosts

Build changes
-------------

* CI: CI-20190529 -> None
* Linux: CI_DRM_6784 -> Patchwork_14188

CI-20190529: 20190529
CI_DRM_6784: 38e1a03497a6e7d1dd271f3f0cd140127181053d @ git://anongit.freedesktop.org/gfx-ci/linux
IGT_5149: 6756ede680ee12745393360d7cc87cc0eb733ff6 @ git://anongit.freedesktop.org/xorg/app/intel-gpu-tools
Patchwork_14188: db6a41bc284019cc6835513c15c96d4e7c4a7c8f @ git://anongit.freedesktop.org/gfx-ci/linux
piglit_4509: fdc5a4ca11124ab8413c7988896eec4c97336694 @ git://anongit.freedesktop.org/piglit

== Logs ==

For more details see: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_14188/
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#15 lore raw
In reply to Abdiel Janulgue
Quoted 130 lines · Abdiel Janulgue

Have i915 replace the core drm_gem_mmap implementation to overcome its limitation in having only a single mmap offset node per gem object. The change allows us to have multiple mmap offsets per object. This enables a mmapping instance to use unique fault-handlers per user vma.

This enables us to store extra data within vma->vm_private_data and assign the pagefault ops for each mmap instance allowing objects to use multiple fault handlers depending on its backing storage.

Cc Joonas Lahtinen <[email protected]>
Cc Chris Wilson <[email protected]>
---
 drivers/gpu/drm/i915/gem/i915_gem_mman.c      | 193 ++++++++++++++++--
 drivers/gpu/drm/i915/gem/i915_gem_object.c    |  19 ++
 drivers/gpu/drm/i915/gem/i915_gem_object.h    |   7 +-
 .../gpu/drm/i915/gem/i915_gem_object_types.h  |  18 ++
 .../drm/i915/gem/selftests/i915_gem_mman.c    |  12 +-
 drivers/gpu/drm/i915/gt/intel_reset.c         |  11 +-
 drivers/gpu/drm/i915/i915_drv.c               |   9 +-
 drivers/gpu/drm/i915/i915_drv.h               |   1 +
 drivers/gpu/drm/i915/i915_vma.c               |  20 +-
 9 files changed, 254 insertions(+), 36 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_mman.c +44 -13
index 595539a09e38..fb7e39f115d7 100644
@@ -218,7 +218,8 @@ vm_fault_t i915_gem_fault(struct vm_fault *vmf)
 {
 #define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT)
        struct vm_area_struct *area = vmf->vma;
-       struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
+       struct i915_mmap_offset *priv = area->vm_private_data;
+       struct drm_i915_gem_object *obj = priv->obj;
        struct drm_device *dev = obj->base.dev;
        struct drm_i915_private *i915 = to_i915(dev);
        struct intel_runtime_pm *rpm = &i915->runtime_pm;
@@ -372,13 +373,20 @@ vm_fault_t i915_gem_fault(struct vm_fault *vmf)
 void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
 {
        struct i915_vma *vma;
+       struct i915_mmap_offset *mmo;
 
        GEM_BUG_ON(!obj->userfault_count);
 
        obj->userfault_count = 0;
        list_del(&obj->userfault_link);
-       drm_vma_node_unmap(&obj->base.vma_node,
-                          obj->base.dev->anon_inode->i_mapping);
+
+       mutex_lock(&obj->mmo_lock);
+       list_for_each_entry(mmo, &obj->mmap_offsets, offset) {
+               if (mmo->mmap_type == I915_MMAP_TYPE_GTT)
+                       drm_vma_node_unmap(&mmo->vma_node,
+                                          obj->base.dev->anon_inode->i_mapping);
+       }
+       mutex_unlock(&obj->mmo_lock);
 
        for_each_ggtt_vma(vma, obj)
                i915_vma_unset_userfault(vma);
@@ -433,14 +441,33 @@ void i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
        intel_runtime_pm_put(&i915->runtime_pm, wakeref);
 }
 
-static int create_mmap_offset(struct drm_i915_gem_object *obj)
+static void init_mmap_offset(struct drm_i915_gem_object *obj,
+                            struct i915_mmap_offset *mmo)
+{
+       mutex_lock(&obj->mmo_lock);
+       kref_init(&mmo->ref);
+       list_add(&mmo->offset, &obj->mmap_offsets);
+       mutex_unlock(&obj->mmo_lock);
+
+       mmo->obj = obj;
+}
+
+static int create_mmap_offset(struct drm_i915_gem_object *obj,
+                             struct i915_mmap_offset *mmo)
 {
        struct drm_i915_private *i915 = to_i915(obj->base.dev);
+       struct drm_device *dev = obj->base.dev;
        int err;
 
-       err = drm_gem_create_mmap_offset(&obj->base);
-       if (likely(!err))
+       drm_vma_node_reset(&mmo->vma_node);
+       if (mmo->file)
+               drm_vma_node_allow(&mmo->vma_node, mmo->file);
+       err = drm_vma_offset_add(dev->vma_offset_manager, &mmo->vma_node,
+                                obj->base.size / PAGE_SIZE);
+       if (likely(!err)) {
+               init_mmap_offset(obj, mmo);
                return 0;
+       }
 
        /* Attempt to reap some mmap space from dead objects */
        do {
@@ -451,32 +478,48 @@ static int create_mmap_offset(struct drm_i915_gem_object *obj)
                        break;
 
                i915_gem_drain_freed_objects(i915);
-               err = drm_gem_create_mmap_offset(&obj->base);
-               if (!err)
+               err = drm_vma_offset_add(dev->vma_offset_manager, &mmo->vma_node,
+                                        obj->base.size / PAGE_SIZE);
+               if (!err) {
+                       init_mmap_offset(obj, mmo);
                        break;
+               }
 
        } while (flush_delayed_work(&i915->gem.retire_work));
 
        return err;
 }
 
-int
-i915_gem_mmap_gtt(struct drm_file *file,
-                 struct drm_device *dev,
-                 u32 handle,
-                 u64 *offset)
+static int
+__assign_gem_object_mmap_data(struct drm_file *file,
+                             u32 handle,
+                             enum i915_mmap_type mmap_type,
+                             u64 *offset)
 {
        struct drm_i915_gem_object *obj;
+       struct i915_mmap_offset *mmo;

drivers/gpu/drm/i915/gem/i915_gem_mman.c static int create_mmap_offset(struct drm_i915_gem_object *obj) · line 507

Quoted 10 lines
drivers/gpu/drm/i915/gem/i915_gem_mman.c +1 -3
        int ret;
 
        obj = i915_gem_object_lookup(file, handle);
        if (!obj)
                return -ENOENT;
 
-       ret = create_mmap_offset(obj);
-       if (ret == 0)
-               *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
+       mmo = kzalloc(sizeof(*mmo), GFP_KERNEL);

I got thrown off a bunch of times here reading the code, but I think I got this right now.

Why exactly do we want multiple vma offsets? Yes this makes it a drop-in replacement for the old cpu mmap ioctl, which was a bit dubious design. But if we go all new here, I really wonder about why this is necessary. No other discrete driver needs this, they all fix the mmap mode for the lifetime of an object, because flushing stuff is as expensive as just reallocating (or at least close enough).

I think us going once again our separate route here needs a lot more justification than just "we've accidentally ended up with uapi like this 10 years ago".
-Daniel

Quoted 436 lines
drivers/gpu/drm/i915/gem/i915_gem_mman.c +130 -2
+       if (!mmo) {
+               ret = -ENOMEM;
+               goto err;
+       }
+
+       mmo->file = file;
+       ret = create_mmap_offset(obj, mmo);
+       if (ret) {
+               kfree(mmo);
+               goto err;
+       }
 
+       mmo->mmap_type = mmap_type;
+       *offset = drm_vma_node_offset_addr(&mmo->vma_node);
+err:
        i915_gem_object_put(obj);
        return ret;
 }
@@ -500,9 +543,123 @@ int
 i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
                        struct drm_file *file)
 {
-       struct drm_i915_gem_mmap_gtt *args = data;
+       struct drm_i915_gem_mmap_offset *args = data;
+
+       return __assign_gem_object_mmap_data(file, args->handle,
+                                            I915_MMAP_TYPE_GTT,
+                                            &args->offset);
+}
+
+void i915_mmap_offset_object_release(struct kref *ref)
+{
+       struct i915_mmap_offset *mmo = container_of(ref,
+                                                   struct i915_mmap_offset,
+                                                   ref);
+       struct drm_i915_gem_object *obj = mmo->obj;
+       struct drm_device *dev = obj->base.dev;
+
+       lockdep_assert_held(&obj->mmo_lock);
+
+       if (mmo->file)
+               drm_vma_node_revoke(&mmo->vma_node, mmo->file);
+       drm_vma_offset_remove(dev->vma_offset_manager, &mmo->vma_node);
+       list_del(&mmo->offset);
 
-       return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
+       kfree(mmo);
+}
+
+static void i915_gem_vm_open(struct vm_area_struct *vma)
+{
+       struct i915_mmap_offset *priv = vma->vm_private_data;
+       struct drm_i915_gem_object *obj = priv->obj;
+
+       i915_gem_object_get(obj);
+       kref_get(&priv->ref);
+}
+
+static void i915_gem_vm_close(struct vm_area_struct *vma)
+{
+       struct i915_mmap_offset *priv = vma->vm_private_data;
+       struct drm_i915_gem_object *obj = priv->obj;
+
+       mutex_lock(&obj->mmo_lock);
+       kref_put(&priv->ref, i915_mmap_offset_object_release);
+       mutex_unlock(&obj->mmo_lock);
+
+       i915_gem_object_put(obj);
+}
+
+static const struct vm_operations_struct i915_gem_gtt_vm_ops = {
+       .fault = i915_gem_fault,
+       .open = i915_gem_vm_open,
+       .close = i915_gem_vm_close,
+};
+
+/* This overcomes the limitation in drm_gem_mmap's assignment of a
+ * drm_gem_object as the vma->vm_private_data. Since we need to
+ * be able to resolve multiple mmap offsets which could be tied
+ * to a single gem object.
+ */
+int i915_gem_mmap(struct file *filp, struct vm_area_struct *vma)
+{
+       struct drm_vma_offset_node *node;
+       struct drm_file *priv = filp->private_data;
+       struct drm_device *dev = priv->minor->dev;
+       struct i915_mmap_offset *mmo = NULL;
+       struct drm_gem_object *obj = NULL;
+
+       if (drm_dev_is_unplugged(dev))
+               return -ENODEV;
+
+       drm_vma_offset_lock_lookup(dev->vma_offset_manager);
+       node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
+                                                 vma->vm_pgoff,
+                                                 vma_pages(vma));
+       if (likely(node)) {
+               mmo = container_of(node, struct i915_mmap_offset,
+                                  vma_node);
+               /*
+                * Skip 0-refcnted objects as it is in the process of being
+                * destroyed and will be invalid when the vma manager lock
+                * is released.
+                */
+               obj = &mmo->obj->base;
+               if (!kref_get_unless_zero(&obj->refcount))
+                       obj = NULL;
+       }
+       drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
+
+       if (!obj)
+               return -EINVAL;
+
+       if (!drm_vma_node_is_allowed(node, priv)) {
+               drm_gem_object_put_unlocked(obj);
+               return -EACCES;
+       }
+
+       if (to_intel_bo(obj)->readonly) {
+               if (vma->vm_flags & VM_WRITE) {
+                       drm_gem_object_put_unlocked(obj);
+                       return -EINVAL;
+               }
+
+               vma->vm_flags &= ~VM_MAYWRITE;
+       }
+
+       vma->vm_flags |= VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
+       vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
+       vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
+       vma->vm_private_data = mmo;
+
+       vma->vm_ops = &i915_gem_gtt_vm_ops;
+
+       /*
+        * Take a ref for our mmap_offset object. The reference is cleaned
+        * up when the vma is closed.
+        */
+       kref_get(&mmo->ref);
+
+       return 0;
 }
 
 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
drivers/gpu/drm/i915/gem/i915_gem_object.c +19 -0
index d7855dc5a5c5..eb3d7f3528f3 100644
@@ -58,6 +58,9 @@ void i915_gem_object_init(struct drm_i915_gem_object *obj,
 
        INIT_LIST_HEAD(&obj->lut_list);
 
+       mutex_init(&obj->mmo_lock);
+       INIT_LIST_HEAD(&obj->mmap_offsets);
+
        init_rcu_head(&obj->rcu);
 
        obj->ops = ops;
@@ -94,6 +97,7 @@ void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
        struct drm_i915_gem_object *obj = to_intel_bo(gem);
        struct drm_i915_file_private *fpriv = file->driver_priv;
        struct i915_lut_handle *lut, *ln;
+       struct i915_mmap_offset *mmo, *on;
        LIST_HEAD(close);
 
        i915_gem_object_lock(obj);
@@ -108,6 +112,13 @@ void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
        }
        i915_gem_object_unlock(obj);
 
+       mutex_lock(&obj->mmo_lock);
+       list_for_each_entry_safe(mmo, on, &obj->mmap_offsets, offset) {
+               if (mmo->file == file)
+                       kref_put(&mmo->ref, i915_mmap_offset_object_release);
+       }
+       mutex_unlock(&obj->mmo_lock);
+
        list_for_each_entry_safe(lut, ln, &close, obj_link) {
                struct i915_gem_context *ctx = lut->ctx;
                struct i915_vma *vma;
@@ -156,6 +167,7 @@ static void __i915_gem_free_objects(struct drm_i915_private *i915,
        wakeref = intel_runtime_pm_get(&i915->runtime_pm);
        llist_for_each_entry_safe(obj, on, freed, freed) {
                struct i915_vma *vma, *vn;
+               struct i915_mmap_offset *mmo, *on;
 
                trace_i915_gem_object_destroy(obj);
 
@@ -169,6 +181,13 @@ static void __i915_gem_free_objects(struct drm_i915_private *i915,
                GEM_BUG_ON(!list_empty(&obj->vma.list));
                GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma.tree));
 
+               i915_gem_object_release_mmap(obj);
+
+               mutex_lock(&obj->mmo_lock);
+               list_for_each_entry_safe(mmo, on, &obj->mmap_offsets, offset)
+                       kref_put(&mmo->ref, i915_mmap_offset_object_release);
+               mutex_unlock(&obj->mmo_lock);
+
                mutex_unlock(&i915->drm.struct_mutex);
 
                GEM_BUG_ON(atomic_read(&obj->bind_count));
drivers/gpu/drm/i915/gem/i915_gem_object.h +5 -2
index 5efb9936e05b..d667ed8bb0a6 100644
@@ -125,13 +125,13 @@ void i915_gem_object_unlock_fence(struct drm_i915_gem_object *obj,
 static inline void
 i915_gem_object_set_readonly(struct drm_i915_gem_object *obj)
 {
-       obj->base.vma_node.readonly = true;
+       obj->readonly = true;
 }
 
 static inline bool
 i915_gem_object_is_readonly(const struct drm_i915_gem_object *obj)
 {
-       return obj->base.vma_node.readonly;
+       return obj->readonly;
 }
 
 static inline bool
@@ -423,6 +423,9 @@ int i915_gem_object_wait(struct drm_i915_gem_object *obj,
 int i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
                                  unsigned int flags,
                                  const struct i915_sched_attr *attr);
+
+void i915_mmap_offset_object_release(struct kref *ref);
+
 #define I915_PRIORITY_DISPLAY I915_USER_PRIORITY(I915_PRIORITY_MAX)
 
 #endif
drivers/gpu/drm/i915/gem/i915_gem_object_types.h +18 -0
index ede0eb4218a8..d74ddb479318 100644
@@ -60,6 +60,19 @@ struct drm_i915_gem_object_ops {
        void (*release)(struct drm_i915_gem_object *obj);
 };
 
+enum i915_mmap_type {
+       I915_MMAP_TYPE_GTT = 0,
+};
+
+struct i915_mmap_offset {
+       struct drm_vma_offset_node vma_node;
+       struct drm_i915_gem_object *obj;
+       struct drm_file *file;
+       enum i915_mmap_type mmap_type;
+       struct kref ref;
+       struct list_head offset;
+};
+
 struct drm_i915_gem_object {
        struct drm_gem_object base;
 
@@ -115,6 +128,11 @@ struct drm_i915_gem_object {
        unsigned int userfault_count;
        struct list_head userfault_link;
 
+       /* Protects access to mmap offsets */
+       struct mutex mmo_lock;
+       struct list_head mmap_offsets;
+       bool readonly:1;
+
        I915_SELFTEST_DECLARE(struct list_head st_link);
 
        /*
drivers/gpu/drm/i915/gem/selftests/i915_gem_mman.c +10 -2
index 1d27babff0ce..a7513421b1db 100644
@@ -371,15 +371,20 @@ static bool assert_mmap_offset(struct drm_i915_private *i915,
                               int expected)
 {
        struct drm_i915_gem_object *obj;
+       /* refcounted in create_mmap_offset */
+       struct i915_mmap_offset *mmo = kzalloc(sizeof(*mmo), GFP_KERNEL);
        int err;
 
        obj = i915_gem_object_create_internal(i915, size);
        if (IS_ERR(obj))
                return PTR_ERR(obj);
 
-       err = create_mmap_offset(obj);
+       err = create_mmap_offset(obj, mmo);
+       if (err)
+               kfree(mmo);
        i915_gem_object_put(obj);
 
+
        return err == expected;
 }
 
@@ -422,6 +427,8 @@ static int igt_mmap_offset_exhaustion(void *arg)
        struct drm_mm *mm = &i915->drm.vma_offset_manager->vm_addr_space_mm;
        struct drm_i915_gem_object *obj;
        struct drm_mm_node resv, *hole;
+       /* refcounted in create_mmap_offset */
+       struct i915_mmap_offset *mmo = kzalloc(sizeof(*mmo), GFP_KERNEL);
        u64 hole_start, hole_end;
        int loop, err;
 
@@ -465,9 +472,10 @@ static int igt_mmap_offset_exhaustion(void *arg)
                goto out;
        }
 
-       err = create_mmap_offset(obj);
+       err = create_mmap_offset(obj, mmo);
        if (err) {
                pr_err("Unable to insert object into reclaimed hole\n");
+               kfree(mmo);
                goto err_obj;
        }
 
drivers/gpu/drm/i915/gt/intel_reset.c +10 -1
index b9d84d52e986..7b82ef888b96 100644
@@ -628,6 +628,7 @@ static void revoke_mmaps(struct intel_gt *gt)
 
        for (i = 0; i < gt->ggtt->num_fences; i++) {
                struct drm_vma_offset_node *node;
+               struct i915_mmap_offset *mmo;
                struct i915_vma *vma;
                u64 vma_offset;
 
@@ -641,10 +642,18 @@ static void revoke_mmaps(struct intel_gt *gt)
                GEM_BUG_ON(vma->fence != &gt->ggtt->fence_regs[i]);
                node = &vma->obj->base.vma_node;
                vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT;
-               unmap_mapping_range(gt->i915->drm.anon_inode->i_mapping,
+
+               list_for_each_entry(mmo, &vma->obj->mmap_offsets, offset) {
+                       node = &mmo->vma_node;
+                       if (!drm_mm_node_allocated(&node->vm_node) ||
+                           mmo->mmap_type != I915_MMAP_TYPE_GTT)
+                               continue;
+
+                       unmap_mapping_range(gt->i915->drm.anon_inode->i_mapping,
                                    drm_vma_node_offset_addr(node) + vma_offset,
                                    vma->size,
                                    1);
+               }
        }
 }
 
drivers/gpu/drm/i915/i915_drv.c +1 -8
index 1c4576a4a5e9..bdd597200984 100644
@@ -2722,18 +2722,12 @@ const struct dev_pm_ops i915_pm_ops = {
        .runtime_resume = intel_runtime_resume,
 };
 
-static const struct vm_operations_struct i915_gem_vm_ops = {
-       .fault = i915_gem_fault,
-       .open = drm_gem_vm_open,
-       .close = drm_gem_vm_close,
-};
-
 static const struct file_operations i915_driver_fops = {
        .owner = THIS_MODULE,
        .open = drm_open,
        .release = drm_release,
        .unlocked_ioctl = drm_ioctl,
-       .mmap = drm_gem_mmap,
+       .mmap = i915_gem_mmap,
        .poll = drm_poll,
        .read = drm_read,
        .compat_ioctl = i915_compat_ioctl,
@@ -2822,7 +2816,6 @@ static struct drm_driver driver = {
 
        .gem_close_object = i915_gem_close_object,
        .gem_free_object_unlocked = i915_gem_free_object,
-       .gem_vm_ops = &i915_gem_vm_ops,
 
        .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
        .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
drivers/gpu/drm/i915/i915_drv.h +1 -0
index b42651a387d9..bf2927e88e14 100644
@@ -2327,6 +2327,7 @@ int i915_gem_wait_for_idle(struct drm_i915_private *dev_priv,
 void i915_gem_suspend(struct drm_i915_private *dev_priv);
 void i915_gem_suspend_late(struct drm_i915_private *dev_priv);
 void i915_gem_resume(struct drm_i915_private *dev_priv);
+int i915_gem_mmap(struct file *filp, struct vm_area_struct *vma);
 vm_fault_t i915_gem_fault(struct vm_fault *vmf);
 
 int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file);
drivers/gpu/drm/i915/i915_vma.c +15 -6
index e0e677b2a3a9..677323644319 100644
@@ -861,7 +861,8 @@ static void __i915_vma_iounmap(struct i915_vma *vma)
 
 void i915_vma_revoke_mmap(struct i915_vma *vma)
 {
-       struct drm_vma_offset_node *node = &vma->obj->base.vma_node;
+       struct drm_vma_offset_node *node;
+       struct i915_mmap_offset *mmo;
        u64 vma_offset;
 
        lockdep_assert_held(&vma->vm->mutex);
@@ -873,10 +874,19 @@ void i915_vma_revoke_mmap(struct i915_vma *vma)
        GEM_BUG_ON(!vma->obj->userfault_count);
 
        vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT;
-       unmap_mapping_range(vma->vm->i915->drm.anon_inode->i_mapping,
-                           drm_vma_node_offset_addr(node) + vma_offset,
-                           vma->size,
-                           1);
+
+       list_for_each_entry(mmo, &vma->obj->mmap_offsets, offset) {
+               node = &mmo->vma_node;
+               /* Only gtt-mmaps for this vma should be unmapped */
+               if (!drm_mm_node_allocated(&node->vm_node) ||
+                   mmo->mmap_type != I915_MMAP_TYPE_GTT)
+                       continue;
+
+               unmap_mapping_range(vma->vm->i915->drm.anon_inode->i_mapping,
+                                   drm_vma_node_offset_addr(node) + vma_offset,
+                                   vma->size,
+                                   1);
+       }
 
        i915_vma_unset_userfault(vma);
        if (!--vma->obj->userfault_count)
--

2.23.0

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

A message that is not in the archive (sent off-list or not yet received): [email protected]

#17 lore raw

Quoting Balestrieri, Francesco (2019-09-11 13:03:25)

Quoted 18 lines

On 04/09/2019, 13.33, "Intel-gfx on behalf of Daniel Vetter" <[email protected] on behalf of [email protected]> wrote:

On Mon, Aug 26, 2019 at 2:21 PM Abdiel Janulgue
> - ret = create_mmap_offset(obj);
> - if (ret == 0)
> - *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
> + mmo = kzalloc(sizeof(*mmo), GFP_KERNEL);

I got thrown off a bunch of times here reading the code, but I think I got this right now.

Why exactly do we want multiple vma offsets? Yes this makes it a drop-in replacement for the old cpu mmap ioctl, which was a bit dubious design. But if we go all new here, I really wonder about why this is necessary. No other discrete driver needs this, they all fix the mmap mode for the lifetime of an object, because flushing stuff is as expensive as just reallocating (or at least close enough).

Quoted 3 lines

I think us going once again our separate route here needs a lot more justification than just "we've accidentally ended up with uapi like this 10 years ago".

That's exactly the whole point, to replace the uapi we accidentally ended up with 10 years ago with the api that doesn't cause valgrind to complain, is easily extensible and supports all legacy usecases which should be a very good position to be in to support unknown future usecases as well. Letting userspace control their mmapings is very powerful, and we definitely do not want to be limiting their flexibility.

That no other driver even seems to allow multiple mmaps, and so has not developed a desire to manage multiple vma per object does not seem to be a reason to limit ourselves. The infrastructure all supports it; the only thing that is at odds is the desire to force the lowest common denominator as the defacto standard.
-Chris
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#18 lore raw
Quoted 31 lines · Chris Wilson

Quoting Balestrieri, Francesco (2019-09-11 13:03:25)

Quoted 18 lines

On 04/09/2019, 13.33, "Intel-gfx on behalf of Daniel Vetter" <[email protected] on behalf of [email protected]> wrote:

On Mon, Aug 26, 2019 at 2:21 PM Abdiel Janulgue
> - ret = create_mmap_offset(obj);
> - if (ret == 0)
> - *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
> + mmo = kzalloc(sizeof(*mmo), GFP_KERNEL);

I got thrown off a bunch of times here reading the code, but I think I got this right now.

Why exactly do we want multiple vma offsets? Yes this makes it a drop-in replacement for the old cpu mmap ioctl, which was a bit dubious design. But if we go all new here, I really wonder about why this is necessary. No other discrete driver needs this, they all fix the mmap mode for the lifetime of an object, because flushing stuff is as expensive as just reallocating (or at least close enough).

Quoted 3 lines

I think us going once again our separate route here needs a lot more justification than just "we've accidentally ended up with uapi like this 10 years ago".

That's exactly the whole point, to replace the uapi we accidentally ended up with 10 years ago with the api that doesn't cause valgrind to complain, is easily extensible and supports all legacy usecases which should be a very good position to be in to support unknown future usecases as well. Letting userspace control their mmapings is very powerful, and we definitely do not want to be limiting their flexibility.

Quoted 5 lines

That no other driver even seems to allow multiple mmaps, and so has not developed a desire to manage multiple vma per object does not seem to be a reason to limit ourselves. The infrastructure all supports it; the only thing that is at odds is the desire to force the lowest common denominator as the defacto standard.

Just because something is possible (or looks possible at first) doesn't make it good uapi. That's how we get stuff like gtt mmap on userptr bo, and then regrets. This entire thing here gets sold as "uapi cleanup for lmem". Which makes sense, except most of the uapi isn't really cleaned up at all:

- We still have relocations (and we even made them more powerful by pipelining them, at a time when all our mesa drivers finally managed to move to softpin). We could ditch all the reloc stuff for gen12+ and lmem instead. Both amdgpu and nv50+ have done that.

- We're still adding more uapi that's all adding variable state. Meanwhile everything else (gallium/iris, vk) move to invariant state object models, where you create stuff once with the right properties and call it a day. lmem does add quite a few new state bits here, would be a lot simpler if we could make them as invariant as possible. For placement we might need to allow changes to the priority, but not to the placement list itself. For mmap, you just select the mmap mode that you want (because once you fixing caching mode, placement list and tiling bits, there's really not a choice anymore), and that's the one you get with the single mmap offset.

Instead we just hang onto all the accumulated complexity, add more, and the only cleanup I'm seeing is that we bake in the multi-mmap model to save one single line in userspace for the valgrind annotation. Which everyone has already anyway, that is not really going to go away.

I think using lmem to clean up the uapi makes tons of sense, but if we bother with that it should be a real cleanup. Not just cosmetics for the one valgrind annotation line in userspace we have right now. -Daniel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#19 lore raw
Quoted 67 lines · Daniel Vetter
Quoted 31 lines · Chris Wilson

Quoting Balestrieri, Francesco (2019-09-11 13:03:25)

Quoted 18 lines

On 04/09/2019, 13.33, "Intel-gfx on behalf of Daniel Vetter" <[email protected] on behalf of [email protected]> wrote:

On Mon, Aug 26, 2019 at 2:21 PM Abdiel Janulgue
> - ret = create_mmap_offset(obj);
> - if (ret == 0)
> - *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
> + mmo = kzalloc(sizeof(*mmo), GFP_KERNEL);

I got thrown off a bunch of times here reading the code, but I think I got this right now.

Why exactly do we want multiple vma offsets? Yes this makes it a drop-in replacement for the old cpu mmap ioctl, which was a bit dubious design. But if we go all new here, I really wonder about why this is necessary. No other discrete driver needs this, they all fix the mmap mode for the lifetime of an object, because flushing stuff is as expensive as just reallocating (or at least close enough).

Quoted 3 lines

I think us going once again our separate route here needs a lot more justification than just "we've accidentally ended up with uapi like this 10 years ago".

That's exactly the whole point, to replace the uapi we accidentally ended up with 10 years ago with the api that doesn't cause valgrind to complain, is easily extensible and supports all legacy usecases which should be a very good position to be in to support unknown future usecases as well. Letting userspace control their mmapings is very powerful, and we definitely do not want to be limiting their flexibility.

Quoted 5 lines

That no other driver even seems to allow multiple mmaps, and so has not developed a desire to manage multiple vma per object does not seem to be a reason to limit ourselves. The infrastructure all supports it; the only thing that is at odds is the desire to force the lowest common denominator as the defacto standard.

Just because something is possible (or looks possible at first) doesn't make it good uapi. That's how we get stuff like gtt mmap on userptr bo, and then regrets. This entire thing here gets sold as "uapi cleanup for lmem". Which makes sense, except most of the uapi isn't really cleaned up at all:

- We still have relocations (and we even made them more powerful by pipelining them, at a time when all our mesa drivers finally managed to move to softpin). We could ditch all the reloc stuff for gen12+ and lmem instead. Both amdgpu and nv50+ have done that.

- We're still adding more uapi that's all adding variable state. Meanwhile everything else (gallium/iris, vk) move to invariant state object models, where you create stuff once with the right properties and call it a day. lmem does add quite a few new state bits here, would be a lot simpler if we could make them as invariant as possible. For placement we might need to allow changes to the priority, but not to the placement list itself. For mmap, you just select the mmap mode that you want (because once you fixing caching mode, placement list and tiling bits, there's really not a choice anymore), and that's the one you get with the single mmap offset.

Instead we just hang onto all the accumulated complexity, add more, and the only cleanup I'm seeing is that we bake in the multi-mmap model to save one single line in userspace for the valgrind annotation. Which everyone has already anyway, that is not really going to go away.

Quoted 3 lines

I think using lmem to clean up the uapi makes tons of sense, but if we bother with that it should be a real cleanup. Not just cosmetics for the one valgrind annotation line in userspace we have right now.

Chatted with Chris, and one useful thing that multi-mmap gives us (and which I ignored) is much easier testing of coherency between all the different views. And we need that because the kernel needs that anyway (at least cpu wc view against all others is needed for swap-out), and we need to be able to test it well because our hw is historically rather buggy in this area. So bummer and lots of sighs :-/ -Daniel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#20 lore raw

PATCH v2 1/5 drm/i915: Allow i915 to manage the vma offset nodes instead of drm core

In reply to Abdiel Janulgue

Have i915 replace the core drm_gem_mmap implementation to overcome its limitation in having only a single mmap offset node per gem object. This change allows us to have multiple mmap offsets per object and enables a mmapping instance to use unique fault-handlers per user vma.

This allows i915 to store extra data within vma->vm_private_data and assign the pagefault ops for each mmap instance allowing objects to use multiple fault handlers depending on its backing storage.

v2:
- Fix race condition exposed by gem_mmap_gtt@close-race. Simplify lifetime management of the mmap offset objects be ensuring it is owned by the parent gem object instead of refcounting.
- Track mmo used by fencing to Avoid locking when revoking mmaps during GPU reset.
- Rebase

Cc Matthew Auld <[email protected]>
Cc Joonas Lahtinen <[email protected]>
Cc Chris Wilson <[email protected]>
---
 drivers/gpu/drm/i915/gem/i915_gem_domain.c    |   2 +-
 drivers/gpu/drm/i915/gem/i915_gem_mman.c      | 245 ++++++++++++++++--
 drivers/gpu/drm/i915/gem/i915_gem_object.c    |  13 +
 drivers/gpu/drm/i915/gem/i915_gem_object.h    |   9 +-
 .../gpu/drm/i915/gem/i915_gem_object_types.h  |  23 ++
 .../drm/i915/gem/selftests/i915_gem_mman.c    |  12 +-
 drivers/gpu/drm/i915/gt/intel_reset.c         |   7 +-
 drivers/gpu/drm/i915/i915_drv.c               |  10 +-
 drivers/gpu/drm/i915/i915_drv.h               |   3 +-
 drivers/gpu/drm/i915/i915_gem.c               |   2 +-
 drivers/gpu/drm/i915/i915_vma.c               |  24 +-
 drivers/gpu/drm/i915/i915_vma.h               |   4 +
 12 files changed, 305 insertions(+), 49 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_domain.c +1 -1
index 9937b4c341f1..40792d2017a7 100644
@@ -254,7 +254,7 @@ int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
 			}
 
 			if (obj->userfault_count)
-				__i915_gem_object_release_mmap(obj);
+				__i915_gem_object_release_mmap_gtt(obj);
 
 			/*
 			 * As we no longer need a fence for GTT access,
drivers/gpu/drm/i915/gem/i915_gem_mman.c +220 -25

323 lines not on this page · raw message

drivers/gpu/drm/i915/gem/i915_gem_object.c +13 -0
index dbf9be9a79f4..8abcd5202374 100644
@@ -58,6 +58,9 @@ void i915_gem_object_init(struct drm_i915_gem_object *obj,
 
 	INIT_LIST_HEAD(&obj->lut_list);
 
+	mutex_init(&obj->mmo_lock);
+	INIT_LIST_HEAD(&obj->mmap_offsets);
+
 	init_rcu_head(&obj->rcu);
 
 	obj->ops = ops;
@@ -155,6 +158,8 @@ static void __i915_gem_free_objects(struct drm_i915_private *i915,
 
 	wakeref = intel_runtime_pm_get(&i915->runtime_pm);
 	llist_for_each_entry_safe(obj, on, freed, freed) {
+		struct i915_mmap_offset *mmo, *mn;
+
 		trace_i915_gem_object_destroy(obj);
 
 		if (!list_empty(&obj->vma.list)) {
@@ -180,6 +185,14 @@ static void __i915_gem_free_objects(struct drm_i915_private *i915,
 			spin_unlock(&obj->vma.lock);
 		}
 
+		i915_gem_object_release_mmap(obj);
+
+		list_for_each_entry_safe(mmo, mn, &obj->mmap_offsets, offset) {
+			mmo->obj = NULL;
+			i915_mmap_offset_destroy(mmo, &obj->mmo_lock);
+		}
+
+		GEM_BUG_ON(!list_empty(&obj->mmap_offsets));
 		GEM_BUG_ON(atomic_read(&obj->bind_count));
 		GEM_BUG_ON(obj->userfault_count);
 		GEM_BUG_ON(!list_empty(&obj->lut_list));
drivers/gpu/drm/i915/gem/i915_gem_object.h +6 -3
index 086a9bf5adcc..6473a87730ce 100644
@@ -130,13 +130,13 @@ void i915_gem_object_unlock_fence(struct drm_i915_gem_object *obj,
 static inline void
 i915_gem_object_set_readonly(struct drm_i915_gem_object *obj)
 {
-	obj->base.vma_node.readonly = true;
+	obj->readonly = true;
 }
 
 static inline bool
 i915_gem_object_is_readonly(const struct drm_i915_gem_object *obj)
 {
-	return obj->base.vma_node.readonly;
+	return obj->readonly;
 }
 
 static inline bool
@@ -356,7 +356,7 @@ static inline void i915_gem_object_unpin_map(struct drm_i915_gem_object *obj)
 	i915_gem_object_unpin_pages(obj);
 }
 
-void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj);
+void __i915_gem_object_release_mmap_gtt(struct drm_i915_gem_object *obj);
 void i915_gem_object_release_mmap(struct drm_i915_gem_object *obj);
 
 void
@@ -442,6 +442,9 @@ int i915_gem_object_wait(struct drm_i915_gem_object *obj,
 int i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
 				  unsigned int flags,
 				  const struct i915_sched_attr *attr);
+
+void i915_mmap_offset_destroy(struct i915_mmap_offset *mmo, struct mutex *mutex);
+
 #define I915_PRIORITY_DISPLAY I915_USER_PRIORITY(I915_PRIORITY_MAX)
 
 #endif
drivers/gpu/drm/i915/gem/i915_gem_object_types.h +23 -0
index c00b4f077f9e..74544eca5e2c 100644
@@ -62,6 +62,24 @@ struct drm_i915_gem_object_ops {
 	void (*release)(struct drm_i915_gem_object *obj);
 };
 
+enum i915_mmap_type {
+	I915_MMAP_TYPE_GTT = 0,
+};
+
+struct i915_mmap_offset {
+	struct drm_device *dev;
+	struct drm_vma_offset_node vma_node;
+	struct drm_i915_gem_object *obj;
+	struct drm_file *file;
+	enum i915_mmap_type mmap_type;
+
+	struct list_head offset;
+	/* list of active VMA using this mmo for fencing */
+	struct list_head vma_list;
+	/* protects vma list */
+	struct mutex vma_lock;
+};
+
 struct drm_i915_gem_object {
 	struct drm_gem_object base;
 
@@ -117,6 +135,11 @@ struct drm_i915_gem_object {
 	unsigned int userfault_count;
 	struct list_head userfault_link;
 
+	/* Protects access to mmap offsets */
+	struct mutex mmo_lock;
+	struct list_head mmap_offsets;
+	bool readonly:1;
+
 	I915_SELFTEST_DECLARE(struct list_head st_link);
 
 	/*
drivers/gpu/drm/i915/gem/selftests/i915_gem_mman.c +10 -2
index cfa52c525691..23c2363dbb2d 100644
@@ -557,15 +557,20 @@ static bool assert_mmap_offset(struct drm_i915_private *i915,
 			       int expected)
 {
 	struct drm_i915_gem_object *obj;
+	/* Ownership transferred to parent gem object in create_mmap_offset */
+	struct i915_mmap_offset *mmo = kzalloc(sizeof(*mmo), GFP_KERNEL);
 	int err;
 
 	obj = i915_gem_object_create_internal(i915, size);
 	if (IS_ERR(obj))
 		return PTR_ERR(obj);
 
-	err = create_mmap_offset(obj);
+	err = create_mmap_offset(obj, mmo);
+	if (err)
+		kfree(mmo);
 	i915_gem_object_put(obj);
 
+
 	return err == expected;
 }
 
@@ -601,6 +606,8 @@ static int igt_mmap_offset_exhaustion(void *arg)
 	struct drm_mm *mm = &i915->drm.vma_offset_manager->vm_addr_space_mm;
 	struct drm_i915_gem_object *obj;
 	struct drm_mm_node resv, *hole;
+	/* Ownership transferred to parent gem object in create_mmap_offset */
+	struct i915_mmap_offset *mmo = kzalloc(sizeof(*mmo), GFP_KERNEL);
 	u64 hole_start, hole_end;
 	int loop, err;
 
@@ -644,9 +651,10 @@ static int igt_mmap_offset_exhaustion(void *arg)
 		goto out;
 	}
 
-	err = create_mmap_offset(obj);
+	err = create_mmap_offset(obj, mmo);
 	if (err) {
 		pr_err("Unable to insert object into reclaimed hole\n");
+		kfree(mmo);
 		goto err_obj;
 	}
 
drivers/gpu/drm/i915/gt/intel_reset.c +6 -1
index 7b3d9d4517a0..d1090fcb6814 100644
@@ -667,8 +667,13 @@ static void revoke_mmaps(struct intel_gt *gt)
 			continue;
 
 		GEM_BUG_ON(vma->fence != &gt->ggtt->fence_regs[i]);
-		node = &vma->obj->base.vma_node;
+
+		if (!vma->mmo)
+			continue;
+
+		node = &vma->mmo->vma_node;
 		vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT;
+
 		unmap_mapping_range(gt->i915->drm.anon_inode->i_mapping,
 				    drm_vma_node_offset_addr(node) + vma_offset,
 				    vma->size,
drivers/gpu/drm/i915/i915_drv.c +1 -9
index 9354924576c4..cc70aba6ac26 100644
@@ -2644,18 +2644,12 @@ const struct dev_pm_ops i915_pm_ops = {
 	.runtime_resume = intel_runtime_resume,
 };
 
-static const struct vm_operations_struct i915_gem_vm_ops = {
-	.fault = i915_gem_fault,
-	.open = drm_gem_vm_open,
-	.close = drm_gem_vm_close,
-};
-
 static const struct file_operations i915_driver_fops = {
 	.owner = THIS_MODULE,
 	.open = drm_open,
 	.release = drm_release,
 	.unlocked_ioctl = drm_ioctl,
-	.mmap = drm_gem_mmap,
+	.mmap = i915_gem_mmap,
 	.poll = drm_poll,
 	.read = drm_read,
 	.compat_ioctl = i915_compat_ioctl,
@@ -2744,7 +2738,6 @@ static struct drm_driver driver = {
 
 	.gem_close_object = i915_gem_close_object,
 	.gem_free_object_unlocked = i915_gem_free_object,
-	.gem_vm_ops = &i915_gem_vm_ops,
 
 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
@@ -2755,7 +2748,6 @@ static struct drm_driver driver = {
 	.get_scanout_position = i915_get_crtc_scanoutpos,
 
 	.dumb_create = i915_gem_dumb_create,
-	.dumb_map_offset = i915_gem_mmap_gtt,
 	.ioctls = i915_ioctls,
 	.num_ioctls = ARRAY_SIZE(i915_ioctls),
 	.fops = &i915_driver_fops,
drivers/gpu/drm/i915/i915_drv.h +1 -2
index b367467b03c2..fa6dd301a738 100644
@@ -2276,8 +2276,6 @@ i915_mutex_lock_interruptible(struct drm_device *dev)
 int i915_gem_dumb_create(struct drm_file *file_priv,
 			 struct drm_device *dev,
 			 struct drm_mode_create_dumb *args);
-int i915_gem_mmap_gtt(struct drm_file *file_priv, struct drm_device *dev,
-		      u32 handle, u64 *offset);
 int i915_gem_mmap_gtt_version(void);
 
 int __must_check i915_gem_set_global_seqno(struct drm_device *dev, u32 seqno);
@@ -2302,6 +2300,7 @@ void i915_gem_driver_release(struct drm_i915_private *dev_priv);
 void i915_gem_suspend(struct drm_i915_private *dev_priv);
 void i915_gem_suspend_late(struct drm_i915_private *dev_priv);
 void i915_gem_resume(struct drm_i915_private *dev_priv);
+int i915_gem_mmap(struct file *filp, struct vm_area_struct *vma);
 vm_fault_t i915_gem_fault(struct vm_fault *vmf);
 
 int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file);
drivers/gpu/drm/i915/i915_gem.c +1 -1
index 0ddbd3a5fb8d..e60ffae4b105 100644
@@ -853,7 +853,7 @@ void i915_gem_runtime_suspend(struct drm_i915_private *i915)
 
 	list_for_each_entry_safe(obj, on,
 				 &i915->ggtt.userfault_list, userfault_link)
-		__i915_gem_object_release_mmap(obj);
+		__i915_gem_object_release_mmap_gtt(obj);
 
 	/*
 	 * The fence will be lost when the device powers down. If any were
drivers/gpu/drm/i915/i915_vma.c +19 -5
index 9fdcd4e2c799..a95b2b79d806 100644
@@ -130,6 +130,7 @@ vma_create(struct drm_i915_gem_object *obj,
 	}
 
 	INIT_LIST_HEAD(&vma->closed_link);
+	INIT_LIST_HEAD(&vma->mmo_link);
 
 	if (view && view->type != I915_GGTT_VIEW_NORMAL) {
 		vma->ggtt_view = *view;
@@ -1002,6 +1003,14 @@ void i915_vma_destroy(struct i915_vma *vma)
 		spin_unlock(&obj->vma.lock);
 	}
 
+	if (vma->mmo) {
+		struct i915_mmap_offset *mmo = vma->mmo;
+
+		mutex_lock(&mmo->vma_lock);
+		list_del(&vma->mmo_link);
+		mutex_unlock(&mmo->vma_lock);
+	}
+
 	__i915_vma_remove_closed(vma);
 	i915_vm_put(vma->vm);
 
@@ -1058,7 +1067,7 @@ static void __i915_vma_iounmap(struct i915_vma *vma)
 
 void i915_vma_revoke_mmap(struct i915_vma *vma)
 {
-	struct drm_vma_offset_node *node = &vma->obj->base.vma_node;
+	struct drm_vma_offset_node *node;
 	u64 vma_offset;
 
 	lockdep_assert_held(&vma->vm->mutex);
@@ -1070,10 +1079,15 @@ void i915_vma_revoke_mmap(struct i915_vma *vma)
 	GEM_BUG_ON(!vma->obj->userfault_count);
 
 	vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT;
-	unmap_mapping_range(vma->vm->i915->drm.anon_inode->i_mapping,
-			    drm_vma_node_offset_addr(node) + vma_offset,
-			    vma->size,
-			    1);
+
+	if (vma->mmo) {
+		node = &vma->mmo->vma_node;
+
+		unmap_mapping_range(vma->vm->i915->drm.anon_inode->i_mapping,
+				    drm_vma_node_offset_addr(node) + vma_offset,
+				    vma->size,
+				    1);
+	}
 
 	i915_vma_unset_userfault(vma);
 	if (!--vma->obj->userfault_count)
drivers/gpu/drm/i915/i915_vma.h +4 -0
index 858908e3d1cc..34afd1a59ea5 100644
@@ -63,6 +63,10 @@ struct i915_vma {
 	u64 display_alignment;
 	struct i915_page_sizes page_sizes;
 
+	/* mmap-offset associated with fencing for this vma */
+	struct i915_mmap_offset	*mmo;
+	struct list_head mmo_link;
+
 	u32 fence_size;
 	u32 fence_alignment;
 
Signature · 6 lines
2.23.0

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#21 lore raw

PATCH v2 2/5 drm/i915: define HAS_MAPPABLE_APERTURE

From: Daniele Ceraolo Spurio <[email protected]>

The following patches in the series will use it to avoid certain operations when aperture is not available in HW.

---
 drivers/gpu/drm/i915/i915_drv.h | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/i915/i915_drv.h +2 -0
index fa6dd301a738..659319f03f18 100644
@@ -2080,6 +2080,8 @@ IS_SUBPLATFORM(const struct drm_i915_private *i915,
 #define OVERLAY_NEEDS_PHYSICAL(dev_priv) \
 		(INTEL_INFO(dev_priv)->display.overlay_needs_physical)
 
+#define HAS_MAPPABLE_APERTURE(dev_priv) (dev_priv->ggtt.mappable_end > 0)
+
 /* Early gen2 have a totally busted CS tlb and require pinned batches. */
 #define HAS_BROKEN_CS_TLB(dev_priv)	(IS_I830(dev_priv) || IS_I845G(dev_priv))
 
Signature · 6 lines
2.23.0

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#22 lore raw

PATCH v2 3/5 drm/i915: Introduce DRM_I915_GEM_MMAP_OFFSET

In reply to Abdiel Janulgue

This is really just an alias of mmap_gtt. Add a new CPU mmap implementation that allows multiple fault handlers that depends on the object's backing pages.

Note that we multiplex mmap_gtt and mmap_offset through the same ioctl, and use the zero extending behaviour of drm to differentiate between them, when we inspect the flags.

---
 drivers/gpu/drm/i915/gem/i915_gem_mman.c      | 36 +++++++++++++++++--
 .../gpu/drm/i915/gem/i915_gem_object_types.h  |  3 ++
 drivers/gpu/drm/i915/i915_drv.c               |  2 +-
 include/uapi/drm/i915_drm.h                   | 28 +++++++++++++++
 4 files changed, 66 insertions(+), 3 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_mman.c +34 -2
index af4377a80502..ae1e687408da 100644
@@ -144,6 +144,9 @@ static unsigned int tile_row_pages(const struct drm_i915_gem_object *obj)
  * 3 - Remove implicit set-domain(GTT) and synchronisation on initial
  *     pagefault; swapin remains transparent.
  *
+ * 4 - Support multiple fault handlers per object depending on object's
+ *     backing storage (a.k.a. MMAP_OFFSET).
+ *
  * Restrictions:
  *
  *  * snoopable objects cannot be accessed via the GTT. It can cause machine
@@ -171,7 +174,7 @@ static unsigned int tile_row_pages(const struct drm_i915_gem_object *obj)
  */
 int i915_gem_mmap_gtt_version(void)
 {
-	return 3;
+	return 4;
 }
 
 static inline struct i915_ggtt_view
@@ -545,6 +548,27 @@ __assign_gem_object_mmap_data(struct drm_file *file,
 	return ret;
 }
 
+static int gem_mmap_offset(struct drm_device *dev, void *data,
+			   struct drm_file *file)
+{
+	struct drm_i915_gem_mmap_offset *args = data;
+	enum i915_mmap_type type;
+
+	if ((args->flags & (I915_MMAP_OFFSET_WC | I915_MMAP_OFFSET_WB)) &&
+	    !boot_cpu_has(X86_FEATURE_PAT))
+		return -ENODEV;
+
+	if (args->flags & I915_MMAP_OFFSET_WC)
+		type = I915_MMAP_TYPE_OFFSET_WC;
+	else if (args->flags & I915_MMAP_OFFSET_WB)
+		type = I915_MMAP_TYPE_OFFSET_WB;
+	else if (args->flags & I915_MMAP_OFFSET_UC)
+		type = I915_MMAP_TYPE_OFFSET_UC;
+
+	return __assign_gem_object_mmap_data(file, args->handle, type,
+					     &args->offset);
+}
+
 /**
  * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  * @dev: DRM device
@@ -564,7 +588,15 @@ int
 i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
 			struct drm_file *file)
 {
-	struct drm_i915_gem_mmap_gtt *args = data;
+	struct drm_i915_gem_mmap_offset *args = data;
+	struct drm_i915_private *i915 = to_i915(dev);
+
+	if (args->flags & I915_MMAP_OFFSET_FLAGS)
+		return gem_mmap_offset(dev, data, file);
+
+	if (!HAS_MAPPABLE_APERTURE(i915))
+		/* No aperture, cannot mmap via legacy GTT */
+		return -ENODEV;
 
 	return __assign_gem_object_mmap_data(file, args->handle,
 					     I915_MMAP_TYPE_GTT,
drivers/gpu/drm/i915/gem/i915_gem_object_types.h +3 -0
index 74544eca5e2c..18e891f4e9d3 100644
@@ -64,6 +64,9 @@ struct drm_i915_gem_object_ops {
 
 enum i915_mmap_type {
 	I915_MMAP_TYPE_GTT = 0,
+	I915_MMAP_TYPE_OFFSET_WC,
+	I915_MMAP_TYPE_OFFSET_WB,
+	I915_MMAP_TYPE_OFFSET_UC,
 };
 
 struct i915_mmap_offset {
drivers/gpu/drm/i915/i915_drv.c +1 -1
index cc70aba6ac26..9182da57182b 100644
@@ -2696,7 +2696,7 @@ static const struct drm_ioctl_desc i915_ioctls[] = {
 	DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_RENDER_ALLOW),
-	DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_OFFSET, i915_gem_mmap_gtt_ioctl, DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling_ioctl, DRM_RENDER_ALLOW),
include/uapi/drm/i915_drm.h +28 -0
index 30c542144016..bc85656ab7fd 100644
@@ -359,6 +359,7 @@ typedef struct _drm_i915_sarea {
 #define DRM_I915_QUERY			0x39
 #define DRM_I915_GEM_VM_CREATE		0x3a
 #define DRM_I915_GEM_VM_DESTROY		0x3b
+#define DRM_I915_GEM_MMAP_OFFSET	DRM_I915_GEM_MMAP_GTT
 /* Must be kept compact -- no holes */
 
 #define DRM_IOCTL_I915_INIT		DRM_IOW( DRM_COMMAND_BASE + DRM_I915_INIT, drm_i915_init_t)
@@ -421,6 +422,7 @@ typedef struct _drm_i915_sarea {
 #define DRM_IOCTL_I915_QUERY			DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_QUERY, struct drm_i915_query)
 #define DRM_IOCTL_I915_GEM_VM_CREATE	DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_VM_CREATE, struct drm_i915_gem_vm_control)
 #define DRM_IOCTL_I915_GEM_VM_DESTROY	DRM_IOW (DRM_COMMAND_BASE + DRM_I915_GEM_VM_DESTROY, struct drm_i915_gem_vm_control)
+#define DRM_IOCTL_I915_GEM_MMAP_OFFSET		DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_MMAP_OFFSET, struct drm_i915_gem_mmap_offset)
 
 /* Allow drivers to submit batchbuffers directly to hardware, relying
  * on the security mechanisms provided by hardware.
@@ -611,6 +613,7 @@ typedef struct drm_i915_irq_wait {
  * See I915_EXEC_FENCE_OUT and I915_EXEC_FENCE_SUBMIT.
  */
 #define I915_PARAM_HAS_EXEC_SUBMIT_FENCE 53
+
 /* Must be kept compact -- no holes and well documented */
 
 typedef struct drm_i915_getparam {
@@ -786,6 +789,31 @@ struct drm_i915_gem_mmap_gtt {
 	__u64 offset;
 };
 
+struct drm_i915_gem_mmap_offset {
+	/** Handle for the object being mapped. */
+	__u32 handle;
+	__u32 pad;
+	/**
+	 * Fake offset to use for subsequent mmap call
+	 *
+	 * This is a fixed-size type for 32/64 compatibility.
+	 */
+	__u64 offset;
+
+	/**
+	 * Flags for extended behaviour.
+	 *
+	 * It is mandatory that either one of the _WC/_WB flags
+	 * should be passed here.
+	 */
+	__u64 flags;
+#define I915_MMAP_OFFSET_WC (1 << 0)
+#define I915_MMAP_OFFSET_WB (1 << 1)
+#define I915_MMAP_OFFSET_UC (1 << 2)
+#define I915_MMAP_OFFSET_FLAGS \
+	(I915_MMAP_OFFSET_WC | I915_MMAP_OFFSET_WB | I915_MMAP_OFFSET_UC)
+};
+
 struct drm_i915_gem_set_domain {
 	/** Handle for the object */
 	__u32 handle;
Signature · 6 lines
2.23.0

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#23 lore raw

PATCH v2 4/5 drm/i915: cpu-map based dumb buffers

In reply to Abdiel Janulgue

If there is no aperture we can't use map_gtt to map dumb buffers, so we need a cpu-map based path to do it. We prefer map_gtt on platforms that do have aperture.

Cc Daniele Ceraolo Spurio <[email protected]>
Cc Tvrtko Ursulin <[email protected]>
Cc Matthew Auld <[email protected]>
---
 drivers/gpu/drm/i915/gem/i915_gem_mman.c | 13 +++++++++++++
 drivers/gpu/drm/i915/i915_drv.c          |  1 +
 drivers/gpu/drm/i915/i915_drv.h          |  2 ++
 3 files changed, 16 insertions(+)
drivers/gpu/drm/i915/gem/i915_gem_mman.c +13 -0
index ae1e687408da..a9604d0db606 100644
@@ -569,6 +569,19 @@ static int gem_mmap_offset(struct drm_device *dev, void *data,
 					     &args->offset);
 }
 
+int
+i915_gem_mmap_dumb(struct drm_file *file,
+		  struct drm_device *dev,
+		  u32 handle,
+		  u64 *offset)
+{
+	struct drm_i915_private *i915 = dev->dev_private;
+	enum i915_mmap_type mmap_type = HAS_MAPPABLE_APERTURE(i915) ?
+		I915_MMAP_TYPE_GTT : I915_MMAP_TYPE_OFFSET_WC;
+
+	return __assign_gem_object_mmap_data(file, handle, mmap_type, offset);
+}
+
 /**
  * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  * @dev: DRM device
drivers/gpu/drm/i915/i915_drv.c +1 -0
index 9182da57182b..0fbbe03befb0 100644
@@ -2748,6 +2748,7 @@ static struct drm_driver driver = {
 	.get_scanout_position = i915_get_crtc_scanoutpos,
 
 	.dumb_create = i915_gem_dumb_create,
+	.dumb_map_offset = i915_gem_mmap_dumb,
 	.ioctls = i915_ioctls,
 	.num_ioctls = ARRAY_SIZE(i915_ioctls),
 	.fops = &i915_driver_fops,
drivers/gpu/drm/i915/i915_drv.h +2 -0
index 659319f03f18..47487beaf0e3 100644
@@ -2278,6 +2278,8 @@ i915_mutex_lock_interruptible(struct drm_device *dev)
 int i915_gem_dumb_create(struct drm_file *file_priv,
 			 struct drm_device *dev,
 			 struct drm_mode_create_dumb *args);
+int i915_gem_mmap_dumb(struct drm_file *file_priv, struct drm_device *dev,
+		      u32 handle, u64 *offset);
 int i915_gem_mmap_gtt_version(void);
 
 int __must_check i915_gem_set_global_seqno(struct drm_device *dev, u32 seqno);
Signature · 6 lines
2.23.0

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#24 lore raw

PATCH v2 5/5 drm/i915: Add cpu fault handler for mmap_offset

In reply to Abdiel Janulgue

Fault handler to handle missing pages for shmem-backed objects.

---
 drivers/gpu/drm/i915/gem/i915_gem_mman.c | 125 ++++++++++++++++++-----
 1 file changed, 100 insertions(+), 25 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_mman.c +100 -25
index a9604d0db606..02afdae812ba 100644
@@ -5,6 +5,7 @@
  */
 
 #include <linux/mman.h>
+#include <linux/pfn_t.h>
 #include <linux/sizes.h>
 
 #include "gt/intel_gt.h"
@@ -200,6 +201,67 @@ compute_partial_view(const struct drm_i915_gem_object *obj,
 	return view;
 }
 
+static vm_fault_t i915_error_to_vmf_fault(int err)
+{
+	switch (err) {
+	default:
+		WARN_ONCE(err, "unhandled error in %s: %i\n", __func__, err);
+		/* fallthrough */
+	case -EIO: /* shmemfs failure from swap device */
+	case -EFAULT: /* purged object */
+	case -ENODEV: /* bad object, how did you get here! */
+		return VM_FAULT_SIGBUS;
+
+	case -ENOSPC: /* shmemfs allocation failure */
+	case -ENOMEM: /* our allocation failure */
+		return VM_FAULT_OOM;
+
+	case 0:
+	case -EAGAIN:
+	case -ERESTARTSYS:
+	case -EINTR:
+	case -EBUSY:
+		/*
+		 * EBUSY is ok: this just means that another thread
+		 * already did the job.
+		 */
+		return VM_FAULT_NOPAGE;
+	}
+}
+
+static vm_fault_t i915_gem_fault_cpu(struct vm_fault *vmf)
+{
+	struct vm_area_struct *area = vmf->vma;
+	struct i915_mmap_offset *priv = area->vm_private_data;
+	struct drm_i915_gem_object *obj = priv->obj;
+	vm_fault_t vmf_ret;
+	unsigned long size = area->vm_end - area->vm_start;
+	bool write = area->vm_flags & VM_WRITE;
+	int i, ret;
+
+	/* Sanity check that we allow writing into this object */
+	if (i915_gem_object_is_readonly(obj) && write)
+		return VM_FAULT_SIGBUS;
+
+	ret = i915_gem_object_pin_pages(obj);
+	if (ret)
+		return i915_error_to_vmf_fault(ret);
+
+	for (i = 0; i < size >> PAGE_SHIFT; i++) {
+		struct page *page = i915_gem_object_get_page(obj, i);
+
+		vmf_ret = vmf_insert_pfn(area,
+					 (unsigned long)area->vm_start + i * PAGE_SIZE,
+					 page_to_pfn(page));
+		if (vmf_ret & VM_FAULT_ERROR)
+			break;
+	}
+
+	i915_gem_object_unpin_pages(obj);
+
+	return vmf_ret;
+}
+
 /**
  * i915_gem_fault - fault a page into the GTT
  * @vmf: fault info
@@ -342,30 +404,7 @@ vm_fault_t i915_gem_fault(struct vm_fault *vmf)
 	intel_runtime_pm_put(rpm, wakeref);
 	i915_gem_object_unpin_pages(obj);
 err:
-	switch (ret) {
-	default:
-		WARN_ONCE(ret, "unhandled error in %s: %i\n", __func__, ret);
-		/* fallthrough */
-	case -EIO: /* shmemfs failure from swap device */
-	case -EFAULT: /* purged object */
-	case -ENODEV: /* bad object, how did you get here! */
-		return VM_FAULT_SIGBUS;
-
-	case -ENOSPC: /* shmemfs allocation failure */
-	case -ENOMEM: /* our allocation failure */
-		return VM_FAULT_OOM;
-
-	case 0:
-	case -EAGAIN:
-	case -ERESTARTSYS:
-	case -EINTR:
-	case -EBUSY:
-		/*
-		 * EBUSY is ok: this just means that another thread
-		 * already did the job.
-		 */
-		return VM_FAULT_NOPAGE;
-	}
+	return i915_error_to_vmf_fault(ret);
 }
 
 void __i915_gem_object_release_mmap_gtt(struct drm_i915_gem_object *obj)
@@ -660,6 +699,33 @@ static const struct vm_operations_struct i915_gem_gtt_vm_ops = {
 	.close = i915_gem_vm_close,
 };
 
+static const struct vm_operations_struct i915_gem_cpu_vm_ops = {
+	.fault = i915_gem_fault_cpu,
+	.open = i915_gem_vm_open,
+	.close = i915_gem_vm_close,
+};
+
+static void set_vmdata_mmap_offset(struct i915_mmap_offset *mmo, struct vm_area_struct *vma)
+{
+	switch (mmo->mmap_type) {
+	case I915_MMAP_TYPE_OFFSET_WC:
+		vma->vm_page_prot =
+			pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
+		break;
+	case I915_MMAP_TYPE_OFFSET_WB:
+		vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
+		break;
+	case I915_MMAP_TYPE_OFFSET_UC:
+		vma->vm_page_prot =
+			pgprot_noncached(vm_get_page_prot(vma->vm_flags));
+		break;
+	default:
+		break;
+	}
+
+	vma->vm_ops = &i915_gem_cpu_vm_ops;
+}
+
 /* This overcomes the limitation in drm_gem_mmap's assignment of a
  * drm_gem_object as the vma->vm_private_data. Since we need to
  * be able to resolve multiple mmap offsets which could be tied
@@ -727,7 +793,16 @@ int i915_gem_mmap(struct file *filp, struct vm_area_struct *vma)
 	vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
 	vma->vm_private_data = mmo;
 
-	vma->vm_ops = &i915_gem_gtt_vm_ops;
+	switch (mmo->mmap_type) {
+	case I915_MMAP_TYPE_OFFSET_WC:
+	case I915_MMAP_TYPE_OFFSET_WB:
+	case I915_MMAP_TYPE_OFFSET_UC:
+		set_vmdata_mmap_offset(mmo, vma);
+		break;
+	case I915_MMAP_TYPE_GTT:
+		vma->vm_ops = &i915_gem_gtt_vm_ops;
+		break;
+	}
 
 	return 0;
 }
Signature · 6 lines
2.23.0

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#25 lore raw

✗ Fi.CI.BUILD: failure for series starting with [v2,5/5] drm/i915: Add cpu fault handler for mmap_offset (rev5)

In reply to Abdiel Janulgue
== Series Details ==

== Series Details ==

Series: series starting with [v2,5/5] drm/i915: Add cpu fault handler for mmap_offset (rev5)
URL : https://patchwork.freedesktop.org/series/65793/
State : failure

== Summary ==

Applying: drm/i915: Add cpu fault handler for mmap_offset
error: sha1 information is lacking or useless (drivers/gpu/drm/i915/gem/i915_gem_mman.c).
error: could not build fake ancestor
hint: Use 'git am --show-current-patch' to see the failed patch
Patch failed at 0001 drm/i915: Add cpu fault handler for mmap_offset
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx