Skip to contentSkip to conversation

drm/i915: Fix phys pwrite for struct_mutex-less operation

PATCH In review Chris Wilson 3 participants 10 messages Jan 6, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 drm/i915: Fix phys pwrite for struct_mutex-less operation

Since commit fe115628d567 ("drm/i915: Implement pwrite without struct-mutex") the lowlevel pwrite calls are now called without the protection of struct_mutex, but pwrite_phys was still asserting that it held the struct_mutex and later tried to drop and relock it.

Fixes fe115628d567 ("drm/i915: Implement pwrite without struct-mutex")
Cc Joonas Lahtinen <[email protected]>
---
 drivers/gpu/drm/i915/i915_gem.c | 34 ++++------------------------------
 1 file changed, 4 insertions(+), 30 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +4 -30
index 91072ebdbdcb..94958437252a 100644
@@ -597,47 +597,21 @@ i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
 		     struct drm_i915_gem_pwrite *args,
 		     struct drm_file *file)
 {
-	struct drm_device *dev = obj->base.dev;
 	void *vaddr = obj->phys_handle->vaddr + args->offset;
 	char __user *user_data = u64_to_user_ptr(args->data_ptr);
-	int ret;
 
 	/* We manually control the domain here and pretend that it
 	 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
 	 */
-	lockdep_assert_held(&obj->base.dev->struct_mutex);
-	ret = i915_gem_object_wait(obj,
-				   I915_WAIT_INTERRUPTIBLE |
-				   I915_WAIT_LOCKED |
-				   I915_WAIT_ALL,
-				   MAX_SCHEDULE_TIMEOUT,
-				   to_rps_client(file));
-	if (ret)
-		return ret;
-
 	intel_fb_obj_invalidate(obj, ORIGIN_CPU);
-	if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
-		unsigned long unwritten;
-
-		/* The physical object once assigned is fixed for the lifetime
-		 * of the obj, so we can safely drop the lock and continue
-		 * to access vaddr.
-		 */
-		mutex_unlock(&dev->struct_mutex);
-		unwritten = copy_from_user(vaddr, user_data, args->size);
-		mutex_lock(&dev->struct_mutex);
-		if (unwritten) {
-			ret = -EFAULT;
-			goto out;
-		}
-	}
+	if (copy_from_user(vaddr, user_data, args->size))
+		return -EFAULT;
 
 	drm_clflush_virt_range(vaddr, args->size);
-	i915_gem_chipset_flush(to_i915(dev));
+	i915_gem_chipset_flush(to_i915(obj->base.dev));
 
-out:
 	intel_fb_obj_flush(obj, false, ORIGIN_CPU);
-	return ret;
+	return 0;
 }
 
 void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
Signature · 6 lines
2.11.0

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

PATCH 2/3 drm/i915: Purge loose pages if we run out of DMA remap space

If the DMA remap fails, one cause can be that we have too many objects pinned in a small remapping table, such as swiotlb. (DMA remapping does not trigger the shrinker by itself on its normal failure paths.) So try purging all other objects (using i915_gem_shrink_all(), sparing our own pages as we have yet to assign them to the obj->pages) and try again. If there are no pages to reclaim (and consequently no pages to unmap), the shrinker will report 0 and we fail with -ENOSPC as before.

---
 drivers/gpu/drm/i915/i915_gem_gtt.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
drivers/gpu/drm/i915/i915_gem_gtt.c +18 -4
index 00520f27bea6..f698006fe883 100644
@@ -2361,10 +2361,24 @@ void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
 int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
 			       struct sg_table *pages)
 {
-	if (dma_map_sg(&obj->base.dev->pdev->dev,
-		       pages->sgl, pages->nents,
-		       PCI_DMA_BIDIRECTIONAL))
-		return 0;
+	do {
+		if (dma_map_sg(&obj->base.dev->pdev->dev,
+			       pages->sgl, pages->nents,
+			       PCI_DMA_BIDIRECTIONAL))
+			return 0;
+
+		/* If the DMA remap fails, one cause can be that we have
+		 * too many objects pinned in a small remapping table,
+		 * such as swiotlb. Incrementally purge all other objects and
+		 * try again - if there are no more pages to remove from
+		 * the DMA remapper, i915_gem_shrink will return 0.
+		 */
+		GEM_BUG_ON(obj->mm.pages == pages);
+	} while (i915_gem_shrink(to_i915(obj->base.dev),
+				 obj->base.size >> PAGE_SHIFT,
+				 I915_SHRINK_BOUND |
+				 I915_SHRINK_UNBOUND |
+				 I915_SHRINK_ACTIVE));
 
 	return -ENOSPC;
 }
Signature · 6 lines
2.11.0

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

PATCH 3/3 drm/i915: Drain freed objects for mmap space exhaustion

In reply to Chris Wilson

As we now use a deferred free queue for objects, simply retiring the active objects is not enough to immediately free them and recover their mmap space - we must now also drain the freed object list.

Fixes fbbd37b36fa5 ("drm/i915: Move object release to a freelist + worker"
Cc Joonas Lahtinen <[email protected]>
---
 drivers/gpu/drm/i915/i915_gem.c | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +11 -13
index 94958437252a..f6f4ec894a7f 100644
@@ -2090,23 +2090,21 @@ static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
 	int err;
 
 	err = drm_gem_create_mmap_offset(&obj->base);
-	if (!err)
+	if (likely(!err))
 		return 0;
 
-	/* We can idle the GPU locklessly to flush stale objects, but in order
-	 * to claim that space for ourselves, we need to take the big
-	 * struct_mutex to free the requests+objects and allocate our slot.
-	 */
-	err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
-	if (err)
-		return err;
+	/* Attempt to reap some mmap space from dead objects */
+	do {
+		err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
+		if (err)
+			break;
 
-	err = i915_mutex_lock_interruptible(&dev_priv->drm);
-	if (!err) {
-		i915_gem_retire_requests(dev_priv);
+		i915_gem_drain_freed_objects(dev_priv);
 		err = drm_gem_create_mmap_offset(&obj->base);
-		mutex_unlock(&dev_priv->drm.struct_mutex);
-	}
+		if (!err)
+			break;
+
+	} while (flush_delayed_work(&dev_priv->gt.retire_work));
 
 	return err;
 }
Signature · 6 lines
2.11.0

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

Re: [PATCH 3/3] drm/i915: Drain freed objects for mmap space exhaustion

Quoted 38 lines · Chris Wilson

As we now use a deferred free queue for objects, simply retiring the active objects is not enough to immediately free them and recover their mmap space - we must now also drain the freed object list.

Fixes fbbd37b36fa5 ("drm/i915: Move object release to a freelist + worker"
Cc Joonas Lahtinen <[email protected]>
---
 drivers/gpu/drm/i915/i915_gem.c | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +6 -9
index 94958437252a..f6f4ec894a7f 100644
@@ -2090,23 +2090,21 @@ static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
 	int err;
 
 	err = drm_gem_create_mmap_offset(&obj->base);
-	if (!err)
+	if (likely(!err))
 		return 0;
 
-	/* We can idle the GPU locklessly to flush stale objects, but in order
-	 * to claim that space for ourselves, we need to take the big
-	 * struct_mutex to free the requests+objects and allocate our slot.
-	 */
-	err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
-	if (err)
-		return err;
+	/* Attempt to reap some mmap space from dead objects */
+	do {
+		err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
+		if (err)
+			break;
 
-	err = i915_mutex_lock_interruptible(&dev_priv->drm);

drivers/gpu/drm/i915/i915_gem.c static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj) · line 2107

Quoted 10 lines
drivers/gpu/drm/i915/i915_gem.c +5 -4
-	if (!err) {
-		i915_gem_retire_requests(dev_priv);
+		i915_gem_drain_freed_objects(dev_priv);
 		err = drm_gem_create_mmap_offset(&obj->base);
-		mutex_unlock(&dev_priv->drm.struct_mutex);
-	}
+		if (!err)
+			break;
+
+	} while (flush_delayed_work(&dev_priv->gt.retire_work));

Would it be better to keep a direct retire in here, since while it waits for the retire worker to run, someone might grab the mutex and then the retire worker won't do anything? Unless flush_delayed_work would not return in case work re-queues itself?

Regards,

Tvrtko

drivers/gpu/drm/i915/i915_gem.c static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)

Quoted 4 lines
drivers/gpu/drm/i915/i915_gem.c +0 -0
 	return err;
 }
 

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

#4 lore raw

Re: [PATCH 3/3] drm/i915: Drain freed objects for mmap space exhaustion

Quoted 39 lines · Tvrtko Ursulin
Quoted 37 lines · Chris Wilson

As we now use a deferred free queue for objects, simply retiring the active objects is not enough to immediately free them and recover their mmap space - we must now also drain the freed object list.

Fixes fbbd37b36fa5 ("drm/i915: Move object release to a freelist + worker"
Cc Joonas Lahtinen <[email protected]>

---
drivers/gpu/drm/i915/i915_gem.c | 24 +++++++++++-------------

1 file changed, 11 insertions(+), 13 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +6 -8
index 94958437252a..f6f4ec894a7f 100644
@@ -2090,23 +2090,21 @@ static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
 	int err;
 
 	err = drm_gem_create_mmap_offset(&obj->base);
-	if (!err)
+	if (likely(!err))
 		return 0;
 
-	/* We can idle the GPU locklessly to flush stale objects, but in order
-	 * to claim that space for ourselves, we need to take the big
-	 * struct_mutex to free the requests+objects and allocate our slot.
-	 */
-	err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
-	if (err)
-		return err;
+	/* Attempt to reap some mmap space from dead objects */
+	do {
+		err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
+		if (err)
+			break;
 
Quoted 16 lines

drivers/gpu/drm/i915/i915_gem.c static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj) · line 2107

Quoted 11 lines
drivers/gpu/drm/i915/i915_gem.c +5 -5
-	err = i915_mutex_lock_interruptible(&dev_priv->drm);
-	if (!err) {
-		i915_gem_retire_requests(dev_priv);
+		i915_gem_drain_freed_objects(dev_priv);
 		err = drm_gem_create_mmap_offset(&obj->base);
-		mutex_unlock(&dev_priv->drm.struct_mutex);
-	}
+		if (!err)
+			break;
+
+	} while (flush_delayed_work(&dev_priv->gt.retire_work));

Would it be better to keep a direct retire in here, since while it waits for the retire worker to run, someone might grab the mutex and then the retire worker won't do anything? Unless flush_delayed_work would not return in case work re-queues itself?

It's a wait either way. I favoured avoiding the struct_mutex here (one less to worry about) and waiting on active requests as that's secondary to the freed work which is how we expect to make forward progress. The loop on retire_work makes sure that any active dead objects will be reaped as soon as possible, and we keep on checking until there are no more dead objects available atm.
-Chris

Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Reviewed-by
#5 lore raw

Re: [PATCH 3/3] drm/i915: Drain freed objects for mmap space exhaustion

Quoted 57 lines · Chris Wilson
Quoted 39 lines · Tvrtko Ursulin
Quoted 37 lines · Chris Wilson

As we now use a deferred free queue for objects, simply retiring the active objects is not enough to immediately free them and recover their mmap space - we must now also drain the freed object list.

Fixes fbbd37b36fa5 ("drm/i915: Move object release to a freelist + worker"
Cc Joonas Lahtinen <[email protected]>

---
drivers/gpu/drm/i915/i915_gem.c | 24 +++++++++++-------------

1 file changed, 11 insertions(+), 13 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +6 -8
index 94958437252a..f6f4ec894a7f 100644
@@ -2090,23 +2090,21 @@ static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
 	int err;
 
 	err = drm_gem_create_mmap_offset(&obj->base);
-	if (!err)
+	if (likely(!err))
 		return 0;
 
-	/* We can idle the GPU locklessly to flush stale objects, but in order
-	 * to claim that space for ourselves, we need to take the big
-	 * struct_mutex to free the requests+objects and allocate our slot.
-	 */
-	err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
-	if (err)
-		return err;
+	/* Attempt to reap some mmap space from dead objects */
+	do {
+		err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
+		if (err)
+			break;
 
Quoted 16 lines

drivers/gpu/drm/i915/i915_gem.c static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj) · line 2107

Quoted 11 lines
drivers/gpu/drm/i915/i915_gem.c +5 -5
-	err = i915_mutex_lock_interruptible(&dev_priv->drm);
-	if (!err) {
-		i915_gem_retire_requests(dev_priv);
+		i915_gem_drain_freed_objects(dev_priv);
 		err = drm_gem_create_mmap_offset(&obj->base);
-		mutex_unlock(&dev_priv->drm.struct_mutex);
-	}
+		if (!err)
+			break;
+
+	} while (flush_delayed_work(&dev_priv->gt.retire_work));

Would it be better to keep a direct retire in here, since while it waits for the retire worker to run, someone might grab the mutex and then the retire worker won't do anything? Unless flush_delayed_work would not return in case work re-queues itself?

Quoted 6 lines

It's a wait either way. I favoured avoiding the struct_mutex here (one less to worry about) and waiting on active requests as that's secondary to the freed work which is how we expect to make forward progress. The loop on retire_work makes sure that any active dead objects will be reaped as soon as possible, and we keep on checking until there are no more dead objects available atm.

Ok I think I understand it now. I missed that i915_gem_wait_for_idle is getting repeat in every iteration so it could indeed wait for the retire worker multiple times.

Regards,

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

#6 lore raw
In reply to Chris Wilson
Quoted 60 lines · Chris Wilson

Since commit fe115628d567 ("drm/i915: Implement pwrite without struct-mutex") the lowlevel pwrite calls are now called without the protection of struct_mutex, but pwrite_phys was still asserting that it held the struct_mutex and later tried to drop and relock it.

Fixes fe115628d567 ("drm/i915: Implement pwrite without struct-mutex")
Cc Joonas Lahtinen <[email protected]>
---
 drivers/gpu/drm/i915/i915_gem.c | 34 ++++------------------------------
 1 file changed, 4 insertions(+), 30 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +2 -27
index 91072ebdbdcb..94958437252a 100644
@@ -597,47 +597,21 @@ i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
 		     struct drm_i915_gem_pwrite *args,
 		     struct drm_file *file)
 {
-	struct drm_device *dev = obj->base.dev;
 	void *vaddr = obj->phys_handle->vaddr + args->offset;
 	char __user *user_data = u64_to_user_ptr(args->data_ptr);
-	int ret;
 
 	/* We manually control the domain here and pretend that it
 	 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
 	 */
-	lockdep_assert_held(&obj->base.dev->struct_mutex);
-	ret = i915_gem_object_wait(obj,
-				   I915_WAIT_INTERRUPTIBLE |
-				   I915_WAIT_LOCKED |
-				   I915_WAIT_ALL,
-				   MAX_SCHEDULE_TIMEOUT,
-				   to_rps_client(file));
-	if (ret)
-		return ret;
-
 	intel_fb_obj_invalidate(obj, ORIGIN_CPU);
-	if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
-		unsigned long unwritten;
-
-		/* The physical object once assigned is fixed for the lifetime
-		 * of the obj, so we can safely drop the lock and continue
-		 * to access vaddr.
-		 */
-		mutex_unlock(&dev->struct_mutex);
-		unwritten = copy_from_user(vaddr, user_data, args->size);
-		mutex_lock(&dev->struct_mutex);
-		if (unwritten) {
-			ret = -EFAULT;
-			goto out;
-		}
-	}
+	if (copy_from_user(vaddr, user_data, args->size))
+		return -EFAULT;
 
 	drm_clflush_virt_range(vaddr, args->size);

drivers/gpu/drm/i915/i915_gem.c i915_gem_phys_pwrite(struct drm_i915_gem_object *obj, · line 617

Quoted 10 lines
drivers/gpu/drm/i915/i915_gem.c +2 -3
-	i915_gem_chipset_flush(to_i915(dev));
+	i915_gem_chipset_flush(to_i915(obj->base.dev));
 
-out:
 	intel_fb_obj_flush(obj, false, ORIGIN_CPU);
-	return ret;
+	return 0;
 }
 
 void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)

First time round I missed how obviously broken the copy was and that wait is already done one level up. What I missed from the original work is, now it is quite possible for userspace to see/create corruption if it doesn't serialize rendering and pread/pwrite itself. Is that OK?

Regards.

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

#7 lore raw
Quoted 70 lines · Tvrtko Ursulin
Quoted 68 lines · Chris Wilson

Since commit fe115628d567 ("drm/i915: Implement pwrite without struct-mutex") the lowlevel pwrite calls are now called without the protection of struct_mutex, but pwrite_phys was still asserting that it held the struct_mutex and later tried to drop and relock it.

Fixes fe115628d567 ("drm/i915: Implement pwrite without struct-mutex")
Cc Joonas Lahtinen <[email protected]>

---
drivers/gpu/drm/i915/i915_gem.c | 34 ++++------------------------------

1 file changed, 4 insertions(+), 30 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +4 -30
index 91072ebdbdcb..94958437252a 100644
@@ -597,47 +597,21 @@ i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
 		     struct drm_i915_gem_pwrite *args,
 		     struct drm_file *file)
 {
-	struct drm_device *dev = obj->base.dev;
 	void *vaddr = obj->phys_handle->vaddr + args->offset;
 	char __user *user_data = u64_to_user_ptr(args->data_ptr);
-	int ret;
 
 	/* We manually control the domain here and pretend that it
 	 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
 	 */
-	lockdep_assert_held(&obj->base.dev->struct_mutex);
-	ret = i915_gem_object_wait(obj,
-				   I915_WAIT_INTERRUPTIBLE |
-				   I915_WAIT_LOCKED |
-				   I915_WAIT_ALL,
-				   MAX_SCHEDULE_TIMEOUT,
-				   to_rps_client(file));
-	if (ret)
-		return ret;
-
 	intel_fb_obj_invalidate(obj, ORIGIN_CPU);
-	if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
-		unsigned long unwritten;
-
-		/* The physical object once assigned is fixed for the lifetime
-		 * of the obj, so we can safely drop the lock and continue
-		 * to access vaddr.
-		 */
-		mutex_unlock(&dev->struct_mutex);
-		unwritten = copy_from_user(vaddr, user_data, args->size);
-		mutex_lock(&dev->struct_mutex);
-		if (unwritten) {
-			ret = -EFAULT;
-			goto out;
-		}
-	}
+	if (copy_from_user(vaddr, user_data, args->size))
+		return -EFAULT;
 
 	drm_clflush_virt_range(vaddr, args->size);
-	i915_gem_chipset_flush(to_i915(dev));
+	i915_gem_chipset_flush(to_i915(obj->base.dev));
 
-out:
 	intel_fb_obj_flush(obj, false, ORIGIN_CPU);
-	return ret;
+	return 0;
 }
 
Quoted 8 lines

drivers/gpu/drm/i915/i915_gem.c i915_gem_phys_pwrite(struct drm_i915_gem_object *obj, · line 617

Quoted 2 lines
drivers/gpu/drm/i915/i915_gem.c +0 -0
 void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)

First time round I missed how obviously broken the copy was and that wait is already done one level up. What I missed from the original work is, now it is quite possible for userspace to see/create corruption if it doesn't serialize rendering and pread/pwrite itself. Is that OK?

Yes. We take the general view that we cannot prevent userspace from racing with itself. Consider not just concurrent pwrites, but vs GTT, CPU, wC mmaps and execbuf.

Starting from scratch, we would fence everything (including CPU access) and have no implicit syncs and rely on userspace using the fences correctly (if they wish to avoid seeing corruption).
-Chris

Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Reviewed-by
#8 lore raw
Quoted 80 lines · Chris Wilson
Quoted 70 lines · Tvrtko Ursulin
Quoted 68 lines · Chris Wilson

Since commit fe115628d567 ("drm/i915: Implement pwrite without struct-mutex") the lowlevel pwrite calls are now called without the protection of struct_mutex, but pwrite_phys was still asserting that it held the struct_mutex and later tried to drop and relock it.

Fixes fe115628d567 ("drm/i915: Implement pwrite without struct-mutex")
Cc Joonas Lahtinen <[email protected]>

---
drivers/gpu/drm/i915/i915_gem.c | 34 ++++------------------------------

1 file changed, 4 insertions(+), 30 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +4 -30
index 91072ebdbdcb..94958437252a 100644
@@ -597,47 +597,21 @@ i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
 		     struct drm_i915_gem_pwrite *args,
 		     struct drm_file *file)
 {
-	struct drm_device *dev = obj->base.dev;
 	void *vaddr = obj->phys_handle->vaddr + args->offset;
 	char __user *user_data = u64_to_user_ptr(args->data_ptr);
-	int ret;
 
 	/* We manually control the domain here and pretend that it
 	 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
 	 */
-	lockdep_assert_held(&obj->base.dev->struct_mutex);
-	ret = i915_gem_object_wait(obj,
-				   I915_WAIT_INTERRUPTIBLE |
-				   I915_WAIT_LOCKED |
-				   I915_WAIT_ALL,
-				   MAX_SCHEDULE_TIMEOUT,
-				   to_rps_client(file));
-	if (ret)
-		return ret;
-
 	intel_fb_obj_invalidate(obj, ORIGIN_CPU);
-	if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
-		unsigned long unwritten;
-
-		/* The physical object once assigned is fixed for the lifetime
-		 * of the obj, so we can safely drop the lock and continue
-		 * to access vaddr.
-		 */
-		mutex_unlock(&dev->struct_mutex);
-		unwritten = copy_from_user(vaddr, user_data, args->size);
-		mutex_lock(&dev->struct_mutex);
-		if (unwritten) {
-			ret = -EFAULT;
-			goto out;
-		}
-	}
+	if (copy_from_user(vaddr, user_data, args->size))
+		return -EFAULT;
 
 	drm_clflush_virt_range(vaddr, args->size);
-	i915_gem_chipset_flush(to_i915(dev));
+	i915_gem_chipset_flush(to_i915(obj->base.dev));
 
-out:
 	intel_fb_obj_flush(obj, false, ORIGIN_CPU);
-	return ret;
+	return 0;
 }
 
Quoted 8 lines

drivers/gpu/drm/i915/i915_gem.c i915_gem_phys_pwrite(struct drm_i915_gem_object *obj, · line 617

Quoted 2 lines
drivers/gpu/drm/i915/i915_gem.c +0 -0
 void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)

First time round I missed how obviously broken the copy was and that wait is already done one level up. What I missed from the original work is, now it is quite possible for userspace to see/create corruption if it doesn't serialize rendering and pread/pwrite itself. Is that OK?

Quoted 7 lines

Yes. We take the general view that we cannot prevent userspace from racing with itself. Consider not just concurrent pwrites, but vs GTT, CPU, wC mmaps and execbuf.

Starting from scratch, we would fence everything (including CPU access) and have no implicit syncs and rely on userspace using the fences correctly (if they wish to avoid seeing corruption).

Good, just double checking I got everything right.

Regards,

Tvrtko

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

#9 lore raw

✓ Fi.CI.BAT: success for series starting with [1/3] drm/i915: Fix phys pwrite for struct_mutex-less operation

In reply to Chris Wilson
== Series Details ==

== Series Details ==

Series: series starting with [1/3] drm/i915: Fix phys pwrite for struct_mutex-less operation
URL : https://patchwork.freedesktop.org/series/17601/
State : success

== Summary ==

Series 17601v1 Series without cover letter
https://patchwork.freedesktop.org/api/1.0/series/17601/revisions/1/mbox/

fi-bdw-5557u     total:246  pass:232  dwarn:0   dfail:0   fail:0   skip:14 
fi-bsw-n3050     total:246  pass:207  dwarn:0   dfail:0   fail:0   skip:39 
fi-bxt-j4205     total:246  pass:224  dwarn:0   dfail:0   fail:0   skip:22 
fi-byt-j1900     total:246  pass:219  dwarn:0   dfail:0   fail:0   skip:27 
fi-hsw-4770      total:246  pass:227  dwarn:0   dfail:0   fail:0   skip:19 
fi-hsw-4770r     total:246  pass:227  dwarn:0   dfail:0   fail:0   skip:19 
fi-ivb-3520m     total:246  pass:225  dwarn:0   dfail:0   fail:0   skip:21 
fi-ivb-3770      total:246  pass:225  dwarn:0   dfail:0   fail:0   skip:21 
fi-kbl-7500u     total:246  pass:225  dwarn:0   dfail:0   fail:0   skip:21 
fi-skl-6260u     total:246  pass:233  dwarn:0   dfail:0   fail:0   skip:13 
fi-skl-6700hq    total:246  pass:226  dwarn:0   dfail:0   fail:0   skip:20 
fi-skl-6700k     total:246  pass:222  dwarn:3   dfail:0   fail:0   skip:21 
fi-snb-2520m     total:246  pass:215  dwarn:0   dfail:0   fail:0   skip:31 
fi-snb-2600      total:246  pass:214  dwarn:0   dfail:0   fail:0   skip:32 

fi-bxt-t5700 failed to collect. IGT log at Patchwork_3447/fi-bxt-t5700/igt.log fi-byt-n2820 failed to collect. IGT log at Patchwork_3447/fi-byt-n2820/igt.log fi-skl-6770hq failed to collect. IGT log at Patchwork_3447/fi-skl-6770hq/igt.log

88c0b7de9157d1f7824dee04d4ad04dbd9c802fb drm-tip: 2017y-01m-06d-10h-07m-28s UTC integration manifest 8083dce drm/i915: Drain freed objects for mmap space exhaustion
12441ba drm/i915: Purge loose pages if we run out of DMA remap space
fd7a882 drm/i915: Fix phys pwrite for struct_mutex-less operation

== Logs ==

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