@@ -579,9 +579,7 @@ void i915_gem_contexts_fini(struct drm_i915_private *i915)
579
579
580
580
static int context_idr_cleanup (int id , void * p , void * data )
581
581
{
582
- struct i915_gem_context * ctx = p ;
583
-
584
- context_close (ctx );
582
+ context_close (p );
585
583
return 0 ;
586
584
}
587
585
@@ -603,13 +601,15 @@ static int gem_context_register(struct i915_gem_context *ctx,
603
601
}
604
602
605
603
/* And finally expose ourselves to userspace via the idr */
604
+ mutex_lock (& fpriv -> context_idr_lock );
606
605
ret = idr_alloc (& fpriv -> context_idr , ctx ,
607
606
DEFAULT_CONTEXT_HANDLE , 0 , GFP_KERNEL );
607
+ if (ret >= 0 )
608
+ ctx -> user_handle = ret ;
609
+ mutex_unlock (& fpriv -> context_idr_lock );
608
610
if (ret < 0 )
609
611
goto err_name ;
610
612
611
- ctx -> user_handle = ret ;
612
-
613
613
return 0 ;
614
614
615
615
err_name :
@@ -627,10 +627,11 @@ int i915_gem_context_open(struct drm_i915_private *i915,
627
627
int err ;
628
628
629
629
idr_init (& file_priv -> context_idr );
630
+ mutex_init (& file_priv -> context_idr_lock );
630
631
631
632
mutex_lock (& i915 -> drm .struct_mutex );
632
-
633
633
ctx = i915_gem_create_context (i915 );
634
+ mutex_unlock (& i915 -> drm .struct_mutex );
634
635
if (IS_ERR (ctx )) {
635
636
err = PTR_ERR (ctx );
636
637
goto err ;
@@ -643,14 +644,14 @@ int i915_gem_context_open(struct drm_i915_private *i915,
643
644
GEM_BUG_ON (ctx -> user_handle != DEFAULT_CONTEXT_HANDLE );
644
645
GEM_BUG_ON (i915_gem_context_is_kernel (ctx ));
645
646
646
- mutex_unlock (& i915 -> drm .struct_mutex );
647
-
648
647
return 0 ;
649
648
650
649
err_ctx :
650
+ mutex_lock (& i915 -> drm .struct_mutex );
651
651
context_close (ctx );
652
- err :
653
652
mutex_unlock (& i915 -> drm .struct_mutex );
653
+ err :
654
+ mutex_destroy (& file_priv -> context_idr_lock );
654
655
idr_destroy (& file_priv -> context_idr );
655
656
return PTR_ERR (ctx );
656
657
}
@@ -663,6 +664,7 @@ void i915_gem_context_close(struct drm_file *file)
663
664
664
665
idr_for_each (& file_priv -> context_idr , context_idr_cleanup , NULL );
665
666
idr_destroy (& file_priv -> context_idr );
667
+ mutex_destroy (& file_priv -> context_idr_lock );
666
668
}
667
669
668
670
static struct i915_request *
@@ -845,25 +847,22 @@ int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
845
847
return ret ;
846
848
847
849
ctx = i915_gem_create_context (i915 );
848
- if (IS_ERR (ctx )) {
849
- ret = PTR_ERR (ctx );
850
- goto err_unlock ;
851
- }
850
+ mutex_unlock (& dev -> struct_mutex );
851
+ if (IS_ERR (ctx ))
852
+ return PTR_ERR (ctx );
852
853
853
854
ret = gem_context_register (ctx , file_priv );
854
855
if (ret )
855
856
goto err_ctx ;
856
857
857
- mutex_unlock (& dev -> struct_mutex );
858
-
859
858
args -> ctx_id = ctx -> user_handle ;
860
859
DRM_DEBUG ("HW context %d created\n" , args -> ctx_id );
861
860
862
861
return 0 ;
863
862
864
863
err_ctx :
864
+ mutex_lock (& dev -> struct_mutex );
865
865
context_close (ctx );
866
- err_unlock :
867
866
mutex_unlock (& dev -> struct_mutex );
868
867
return ret ;
869
868
}
@@ -874,29 +873,25 @@ int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
874
873
struct drm_i915_gem_context_destroy * args = data ;
875
874
struct drm_i915_file_private * file_priv = file -> driver_priv ;
876
875
struct i915_gem_context * ctx ;
877
- int ret ;
878
876
879
877
if (args -> pad != 0 )
880
878
return - EINVAL ;
881
879
882
880
if (args -> ctx_id == DEFAULT_CONTEXT_HANDLE )
883
881
return - ENOENT ;
884
882
885
- ctx = i915_gem_context_lookup (file_priv , args -> ctx_id );
883
+ if (mutex_lock_interruptible (& file_priv -> context_idr_lock ))
884
+ return - EINTR ;
885
+
886
+ ctx = idr_remove (& file_priv -> context_idr , args -> ctx_id );
887
+ mutex_unlock (& file_priv -> context_idr_lock );
886
888
if (!ctx )
887
889
return - ENOENT ;
888
890
889
- ret = mutex_lock_interruptible (& dev -> struct_mutex );
890
- if (ret )
891
- goto out ;
892
-
893
- idr_remove (& file_priv -> context_idr , ctx -> user_handle );
891
+ mutex_lock (& dev -> struct_mutex );
894
892
context_close (ctx );
895
-
896
893
mutex_unlock (& dev -> struct_mutex );
897
894
898
- out :
899
- i915_gem_context_put (ctx );
900
895
return 0 ;
901
896
}
902
897
0 commit comments