Commit be72615b authored by Chris Wilson's avatar Chris Wilson Committed by Eric Anholt

drm/i915: Repeat unbinding during free if interrupted (v6)

If during the freeing of an object the unbind is interrupted by a system
call, which is quite possible if we have outstanding GPU writes that
must be flushed, the unbind is silently aborted. This still leaves the
AGP region and backing pages allocated, and perhaps more importantly,
the object remains upon the various lists exposing us to memory
corruption.

I think this is the cause behind the use-after-free, such as

  Bug 15664 - Graphics hang and kernel backtrace when starting Azureus
              with Compiz enabled
  https://bugzilla.kernel.org/show_bug.cgi?id=15664

v2: Daniel Vetter reminded me that kernel space programming is never easy.
We cannot simply spin to clear the pending signal and so must deferred
the freeing of the object until later.
v3: Run from the top level retire requests.
v4: Tested with P(return -ERESTARTSYS)=.5 from i915_gem_do_wait_request()
v5: Rebase against Eric's for-linus tree.
v6: Refactor, split and add a comment about avoiding unbounded recursion.
Signed-off-by: default avatarChris Wilson <chris@chris-wilson.co.uk>
Cc: Daniel Vetter <daniel@ffwll.ch>
Signed-off-by: default avatarEric Anholt <eric@anholt.net>
parent b09a1fec
...@@ -551,6 +551,14 @@ typedef struct drm_i915_private { ...@@ -551,6 +551,14 @@ typedef struct drm_i915_private {
/** LRU list of objects with fence regs on them. */ /** LRU list of objects with fence regs on them. */
struct list_head fence_list; struct list_head fence_list;
/**
* List of objects currently pending being freed.
*
* These objects are no longer in use, but due to a signal
* we were prevented from freeing them at the appointed time.
*/
struct list_head deferred_free_list;
/** /**
* We leave the user IRQ off as much as possible, * We leave the user IRQ off as much as possible,
* but this means that requests will finish and never * but this means that requests will finish and never
......
...@@ -53,6 +53,7 @@ static int i915_gem_evict_from_inactive_list(struct drm_device *dev); ...@@ -53,6 +53,7 @@ static int i915_gem_evict_from_inactive_list(struct drm_device *dev);
static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj, static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
struct drm_i915_gem_pwrite *args, struct drm_i915_gem_pwrite *args,
struct drm_file *file_priv); struct drm_file *file_priv);
static void i915_gem_free_object_tail(struct drm_gem_object *obj);
static LIST_HEAD(shrink_list); static LIST_HEAD(shrink_list);
static DEFINE_SPINLOCK(shrink_list_lock); static DEFINE_SPINLOCK(shrink_list_lock);
...@@ -1755,6 +1756,20 @@ i915_gem_retire_requests(struct drm_device *dev) ...@@ -1755,6 +1756,20 @@ i915_gem_retire_requests(struct drm_device *dev)
{ {
drm_i915_private_t *dev_priv = dev->dev_private; drm_i915_private_t *dev_priv = dev->dev_private;
if (!list_empty(&dev_priv->mm.deferred_free_list)) {
struct drm_i915_gem_object *obj_priv, *tmp;
/* We must be careful that during unbind() we do not
* accidentally infinitely recurse into retire requests.
* Currently:
* retire -> free -> unbind -> wait -> retire_ring
*/
list_for_each_entry_safe(obj_priv, tmp,
&dev_priv->mm.deferred_free_list,
list)
i915_gem_free_object_tail(&obj_priv->base);
}
i915_gem_retire_requests_ring(dev, &dev_priv->render_ring); i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
if (HAS_BSD(dev)) if (HAS_BSD(dev))
i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring); i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
...@@ -4458,20 +4473,19 @@ int i915_gem_init_object(struct drm_gem_object *obj) ...@@ -4458,20 +4473,19 @@ int i915_gem_init_object(struct drm_gem_object *obj)
return 0; return 0;
} }
void i915_gem_free_object(struct drm_gem_object *obj) static void i915_gem_free_object_tail(struct drm_gem_object *obj)
{ {
struct drm_device *dev = obj->dev; struct drm_device *dev = obj->dev;
drm_i915_private_t *dev_priv = dev->dev_private;
struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
int ret;
trace_i915_gem_object_destroy(obj); ret = i915_gem_object_unbind(obj);
if (ret == -ERESTARTSYS) {
while (obj_priv->pin_count > 0) list_move(&obj_priv->list,
i915_gem_object_unpin(obj); &dev_priv->mm.deferred_free_list);
return;
if (obj_priv->phys_obj) }
i915_gem_detach_phys_object(dev, obj);
i915_gem_object_unbind(obj);
if (obj_priv->mmap_offset) if (obj_priv->mmap_offset)
i915_gem_free_mmap_offset(obj); i915_gem_free_mmap_offset(obj);
...@@ -4483,6 +4497,22 @@ void i915_gem_free_object(struct drm_gem_object *obj) ...@@ -4483,6 +4497,22 @@ void i915_gem_free_object(struct drm_gem_object *obj)
kfree(obj_priv); kfree(obj_priv);
} }
void i915_gem_free_object(struct drm_gem_object *obj)
{
struct drm_device *dev = obj->dev;
struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
trace_i915_gem_object_destroy(obj);
while (obj_priv->pin_count > 0)
i915_gem_object_unpin(obj);
if (obj_priv->phys_obj)
i915_gem_detach_phys_object(dev, obj);
i915_gem_free_object_tail(obj);
}
/** Unbinds all inactive objects. */ /** Unbinds all inactive objects. */
static int static int
i915_gem_evict_from_inactive_list(struct drm_device *dev) i915_gem_evict_from_inactive_list(struct drm_device *dev)
...@@ -4756,6 +4786,7 @@ i915_gem_load(struct drm_device *dev) ...@@ -4756,6 +4786,7 @@ i915_gem_load(struct drm_device *dev)
INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list); INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
INIT_LIST_HEAD(&dev_priv->mm.inactive_list); INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
INIT_LIST_HEAD(&dev_priv->mm.fence_list); INIT_LIST_HEAD(&dev_priv->mm.fence_list);
INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
INIT_LIST_HEAD(&dev_priv->render_ring.active_list); INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
INIT_LIST_HEAD(&dev_priv->render_ring.request_list); INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
if (HAS_BSD(dev)) { if (HAS_BSD(dev)) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment