@@ -730,32 +730,69 @@ static int eb_reserve(struct i915_execbuffer *eb)
730730 bool unpinned ;
731731
732732 /*
733- * Attempt to pin all of the buffers into the GTT.
734- * This is done in 2 phases:
733+ * We have one more buffers that we couldn't bind, which could be due to
734+ * various reasons. To resolve this we have 4 passes, with every next
735+ * level turning the screws tighter:
735736 *
736- * 1. Unbind all objects that do not match the GTT constraints for
737- * the execbuffer (fenceable, mappable, alignment etc).
738- * 2. Bind new objects.
737+ * 0. Unbind all objects that do not match the GTT constraints for the
738+ * execbuffer (fenceable, mappable, alignment etc). Bind all new
739+ * objects. This avoids unnecessary unbinding of later objects in order
740+ * to make room for the earlier objects *unless* we need to defragment.
739741 *
740- * This avoid unnecessary unbinding of later objects in order to make
741- * room for the earlier objects *unless* we need to defragment.
742+ * 1. Reorder the buffers, where objects with the most restrictive
743+ * placement requirements go first (ignoring fixed location buffers for
744+ * now). For example, objects needing the mappable aperture (the first
745+ * 256M of GTT), should go first vs objects that can be placed just
746+ * about anywhere. Repeat the previous pass.
742747 *
743- * Defragmenting is skipped if all objects are pinned at a fixed location.
748+ * 2. Consider buffers that are pinned at a fixed location. Also try to
749+ * evict the entire VM this time, leaving only objects that we were
750+ * unable to lock. Try again to bind the buffers. (still using the new
751+ * buffer order).
752+ *
753+ * 3. We likely have object lock contention for one or more stubborn
754+ * objects in the VM, for which we need to evict to make forward
755+ * progress (perhaps we are fighting the shrinker?). When evicting the
756+ * VM this time around, anything that we can't lock we now track using
757+ * the busy_bo, using the full lock (after dropping the vm->mutex to
758+ * prevent deadlocks), instead of trylock. We then continue to evict the
759+ * VM, this time with the stubborn object locked, which we can now
760+ * hopefully unbind (if still bound in the VM). Repeat until the VM is
761+ * evicted. Finally we should be able bind everything.
744762 */
745- for (pass = 0 ; pass <= 2 ; pass ++ ) {
763+ for (pass = 0 ; pass <= 3 ; pass ++ ) {
746764 int pin_flags = PIN_USER | PIN_VALIDATE ;
747765
748766 if (pass == 0 )
749767 pin_flags |= PIN_NONBLOCK ;
750768
751769 if (pass >= 1 )
752- unpinned = eb_unbind (eb , pass = = 2 );
770+ unpinned = eb_unbind (eb , pass > = 2 );
753771
754772 if (pass == 2 ) {
755773 err = mutex_lock_interruptible (& eb -> context -> vm -> mutex );
756774 if (!err ) {
757- err = i915_gem_evict_vm (eb -> context -> vm , & eb -> ww );
775+ err = i915_gem_evict_vm (eb -> context -> vm , & eb -> ww , NULL );
776+ mutex_unlock (& eb -> context -> vm -> mutex );
777+ }
778+ if (err )
779+ return err ;
780+ }
781+
782+ if (pass == 3 ) {
783+ retry :
784+ err = mutex_lock_interruptible (& eb -> context -> vm -> mutex );
785+ if (!err ) {
786+ struct drm_i915_gem_object * busy_bo = NULL ;
787+
788+ err = i915_gem_evict_vm (eb -> context -> vm , & eb -> ww , & busy_bo );
758789 mutex_unlock (& eb -> context -> vm -> mutex );
790+ if (err && busy_bo ) {
791+ err = i915_gem_object_lock (busy_bo , & eb -> ww );
792+ i915_gem_object_put (busy_bo );
793+ if (!err )
794+ goto retry ;
795+ }
759796 }
760797 if (err )
761798 return err ;
0 commit comments