@@ -79,8 +79,8 @@ static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma);
79
79
80
80
static u64 kvirt_to_phys (void * addr );
81
81
static int assign_ctxt (struct hfi1_filedata * fd , struct hfi1_user_info * uinfo );
82
- static int init_subctxts (struct hfi1_ctxtdata * uctxt ,
83
- const struct hfi1_user_info * uinfo );
82
+ static void init_subctxts (struct hfi1_ctxtdata * uctxt ,
83
+ const struct hfi1_user_info * uinfo );
84
84
static int init_user_ctxt (struct hfi1_filedata * fd ,
85
85
struct hfi1_ctxtdata * uctxt );
86
86
static void user_init (struct hfi1_ctxtdata * uctxt );
@@ -758,7 +758,6 @@ static int hfi1_file_close(struct inode *inode, struct file *fp)
758
758
goto done ;
759
759
760
760
hfi1_cdbg (PROC , "freeing ctxt %u:%u" , uctxt -> ctxt , fdata -> subctxt );
761
- mutex_lock (& hfi1_mutex );
762
761
763
762
flush_wc ();
764
763
/* drain user sdma queue */
@@ -778,6 +777,7 @@ static int hfi1_file_close(struct inode *inode, struct file *fp)
778
777
HFI1_MAX_SHARED_CTXTS ) + fdata -> subctxt ;
779
778
* ev = 0 ;
780
779
780
+ mutex_lock (& hfi1_mutex );
781
781
__clear_bit (fdata -> subctxt , uctxt -> in_use_ctxts );
782
782
fdata -> uctxt = NULL ;
783
783
hfi1_rcd_put (uctxt ); /* fdata reference */
@@ -844,6 +844,38 @@ static u64 kvirt_to_phys(void *addr)
844
844
return paddr ;
845
845
}
846
846
847
+ static int complete_subctxt (struct hfi1_filedata * fd )
848
+ {
849
+ int ret ;
850
+
851
+ /*
852
+ * sub-context info can only be set up after the base context
853
+ * has been completed.
854
+ */
855
+ ret = wait_event_interruptible (
856
+ fd -> uctxt -> wait ,
857
+ !test_bit (HFI1_CTXT_BASE_UNINIT , & fd -> uctxt -> event_flags ));
858
+
859
+ if (test_bit (HFI1_CTXT_BASE_FAILED , & fd -> uctxt -> event_flags ))
860
+ ret = - ENOMEM ;
861
+
862
+ /* The only thing a sub context needs is the user_xxx stuff */
863
+ if (!ret ) {
864
+ fd -> rec_cpu_num = hfi1_get_proc_affinity (fd -> uctxt -> numa_id );
865
+ ret = init_user_ctxt (fd , fd -> uctxt );
866
+ }
867
+
868
+ if (ret ) {
869
+ hfi1_rcd_put (fd -> uctxt );
870
+ fd -> uctxt = NULL ;
871
+ mutex_lock (& hfi1_mutex );
872
+ __clear_bit (fd -> subctxt , fd -> uctxt -> in_use_ctxts );
873
+ mutex_unlock (& hfi1_mutex );
874
+ }
875
+
876
+ return ret ;
877
+ }
878
+
847
879
static int assign_ctxt (struct hfi1_filedata * fd , struct hfi1_user_info * uinfo )
848
880
{
849
881
int ret ;
@@ -854,79 +886,57 @@ static int assign_ctxt(struct hfi1_filedata *fd, struct hfi1_user_info *uinfo)
854
886
if (swmajor != HFI1_USER_SWMAJOR )
855
887
return - ENODEV ;
856
888
889
+ if (uinfo -> subctxt_cnt > HFI1_MAX_SHARED_CTXTS )
890
+ return - EINVAL ;
891
+
857
892
swminor = uinfo -> userversion & 0xffff ;
858
893
894
+ /*
895
+ * Acquire the mutex to protect against multiple creations of what
896
+ * could be a shared base context.
897
+ */
859
898
mutex_lock (& hfi1_mutex );
860
899
/*
861
- * Get a sub context if necessary .
900
+ * Get a sub context if available (fd->uctxt will be set) .
862
901
* ret < 0 error, 0 no context, 1 sub-context found
863
902
*/
864
- ret = 0 ;
865
- if (uinfo -> subctxt_cnt ) {
866
- ret = find_sub_ctxt (fd , uinfo );
867
- if (ret > 0 )
868
- fd -> rec_cpu_num =
869
- hfi1_get_proc_affinity (fd -> uctxt -> numa_id );
870
- }
903
+ ret = find_sub_ctxt (fd , uinfo );
871
904
872
905
/*
873
- * Allocate a base context if context sharing is not required or we
874
- * couldn't find a sub context.
906
+ * Allocate a base context if context sharing is not required or a
907
+ * sub context wasn't found .
875
908
*/
876
909
if (!ret )
877
910
ret = allocate_ctxt (fd , fd -> dd , uinfo , & uctxt );
878
911
879
912
mutex_unlock (& hfi1_mutex );
880
913
881
914
/* Depending on the context type, do the appropriate init */
882
- if (ret > 0 ) {
883
- /*
884
- * sub-context info can only be set up after the base
885
- * context has been completed.
886
- */
887
- ret = wait_event_interruptible (fd -> uctxt -> wait , !test_bit (
888
- HFI1_CTXT_BASE_UNINIT ,
889
- & fd -> uctxt -> event_flags ));
890
- if (test_bit (HFI1_CTXT_BASE_FAILED , & fd -> uctxt -> event_flags ))
891
- ret = - ENOMEM ;
892
-
893
- /* The only thing a sub context needs is the user_xxx stuff */
894
- if (!ret )
895
- ret = init_user_ctxt (fd , fd -> uctxt );
896
-
897
- if (ret )
898
- clear_bit (fd -> subctxt , fd -> uctxt -> in_use_ctxts );
899
-
900
- } else if (!ret ) {
915
+ switch (ret ) {
916
+ case 0 :
901
917
ret = setup_base_ctxt (fd , uctxt );
902
918
if (uctxt -> subctxt_cnt ) {
903
- /* If there is an error, set the failed bit. */
904
- if (ret )
905
- set_bit (HFI1_CTXT_BASE_FAILED ,
906
- & uctxt -> event_flags );
907
919
/*
908
920
* Base context is done, notify anybody using a
909
921
* sub-context that is waiting for this completion
910
922
*/
911
923
clear_bit (HFI1_CTXT_BASE_UNINIT , & uctxt -> event_flags );
912
924
wake_up (& uctxt -> wait );
913
925
}
914
- if (ret )
915
- deallocate_ctxt (uctxt );
916
- }
917
-
918
- /* If an error occurred, clear the reference */
919
- if (ret && fd -> uctxt ) {
920
- hfi1_rcd_put (fd -> uctxt );
921
- fd -> uctxt = NULL ;
926
+ break ;
927
+ case 1 :
928
+ ret = complete_subctxt (fd );
929
+ break ;
930
+ default :
931
+ break ;
922
932
}
923
933
924
934
return ret ;
925
935
}
926
936
927
937
/*
928
938
* The hfi1_mutex must be held when this function is called. It is
929
- * necessary to ensure serialized access to the bitmask in_use_ctxts .
939
+ * necessary to ensure serialized creation of shared contexts .
930
940
*/
931
941
static int find_sub_ctxt (struct hfi1_filedata * fd ,
932
942
const struct hfi1_user_info * uinfo )
@@ -935,6 +945,9 @@ static int find_sub_ctxt(struct hfi1_filedata *fd,
935
945
struct hfi1_devdata * dd = fd -> dd ;
936
946
u16 subctxt ;
937
947
948
+ if (!uinfo -> subctxt_cnt )
949
+ return 0 ;
950
+
938
951
for (i = dd -> first_dyn_alloc_ctxt ; i < dd -> num_rcv_contexts ; i ++ ) {
939
952
struct hfi1_ctxtdata * uctxt = dd -> rcd [i ];
940
953
@@ -983,7 +996,6 @@ static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
983
996
struct hfi1_ctxtdata * * cd )
984
997
{
985
998
struct hfi1_ctxtdata * uctxt ;
986
- u16 ctxt ;
987
999
int ret , numa ;
988
1000
989
1001
if (dd -> flags & HFI1_FROZEN ) {
@@ -997,22 +1009,9 @@ static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
997
1009
return - EIO ;
998
1010
}
999
1011
1000
- /*
1001
- * This check is sort of redundant to the next EBUSY error. It would
1002
- * also indicate an inconsistancy in the driver if this value was
1003
- * zero, but there were still contexts available.
1004
- */
1005
1012
if (!dd -> freectxts )
1006
1013
return - EBUSY ;
1007
1014
1008
- for (ctxt = dd -> first_dyn_alloc_ctxt ;
1009
- ctxt < dd -> num_rcv_contexts ; ctxt ++ )
1010
- if (!dd -> rcd [ctxt ])
1011
- break ;
1012
-
1013
- if (ctxt == dd -> num_rcv_contexts )
1014
- return - EBUSY ;
1015
-
1016
1015
/*
1017
1016
* If we don't have a NUMA node requested, preference is towards
1018
1017
* device NUMA node.
@@ -1022,11 +1021,10 @@ static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
1022
1021
numa = cpu_to_node (fd -> rec_cpu_num );
1023
1022
else
1024
1023
numa = numa_node_id ();
1025
- uctxt = hfi1_create_ctxtdata (dd -> pport , ctxt , numa );
1026
- if (!uctxt ) {
1027
- dd_dev_err (dd ,
1028
- "Unable to allocate ctxtdata memory, failing open\n" );
1029
- return - ENOMEM ;
1024
+ ret = hfi1_create_ctxtdata (dd -> pport , numa , & uctxt );
1025
+ if (ret < 0 ) {
1026
+ dd_dev_err (dd , "user ctxtdata allocation failed\n" );
1027
+ return ret ;
1030
1028
}
1031
1029
hfi1_cdbg (PROC , "[%u:%u] pid %u assigned to CPU %d (NUMA %u)" ,
1032
1030
uctxt -> ctxt , fd -> subctxt , current -> pid , fd -> rec_cpu_num ,
@@ -1035,8 +1033,7 @@ static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
1035
1033
/*
1036
1034
* Allocate and enable a PIO send context.
1037
1035
*/
1038
- uctxt -> sc = sc_alloc (dd , SC_USER , uctxt -> rcvhdrqentsize ,
1039
- uctxt -> dd -> node );
1036
+ uctxt -> sc = sc_alloc (dd , SC_USER , uctxt -> rcvhdrqentsize , dd -> node );
1040
1037
if (!uctxt -> sc ) {
1041
1038
ret = - ENOMEM ;
1042
1039
goto ctxdata_free ;
@@ -1048,20 +1045,13 @@ static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
1048
1045
goto ctxdata_free ;
1049
1046
1050
1047
/*
1051
- * Setup sub context resources if the user-level has requested
1048
+ * Setup sub context information if the user-level has requested
1052
1049
* sub contexts.
1053
1050
* This has to be done here so the rest of the sub-contexts find the
1054
- * proper master .
1051
+ * proper base context .
1055
1052
*/
1056
- if (uinfo -> subctxt_cnt ) {
1057
- ret = init_subctxts (uctxt , uinfo );
1058
- /*
1059
- * On error, we don't need to disable and de-allocate the
1060
- * send context because it will be done during file close
1061
- */
1062
- if (ret )
1063
- goto ctxdata_free ;
1064
- }
1053
+ if (uinfo -> subctxt_cnt )
1054
+ init_subctxts (uctxt , uinfo );
1065
1055
uctxt -> userversion = uinfo -> userversion ;
1066
1056
uctxt -> flags = hfi1_cap_mask ; /* save current flag state */
1067
1057
init_waitqueue_head (& uctxt -> wait );
@@ -1081,9 +1071,7 @@ static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
1081
1071
return 0 ;
1082
1072
1083
1073
ctxdata_free :
1084
- * cd = NULL ;
1085
- dd -> rcd [ctxt ] = NULL ;
1086
- hfi1_rcd_put (uctxt );
1074
+ hfi1_free_ctxt (dd , uctxt );
1087
1075
return ret ;
1088
1076
}
1089
1077
@@ -1093,28 +1081,17 @@ static void deallocate_ctxt(struct hfi1_ctxtdata *uctxt)
1093
1081
hfi1_stats .sps_ctxts -- ;
1094
1082
if (++ uctxt -> dd -> freectxts == uctxt -> dd -> num_user_contexts )
1095
1083
aspm_enable_all (uctxt -> dd );
1096
-
1097
- /* _rcd_put() should be done after releasing mutex */
1098
- uctxt -> dd -> rcd [uctxt -> ctxt ] = NULL ;
1099
1084
mutex_unlock (& hfi1_mutex );
1100
- hfi1_rcd_put (uctxt ); /* dd reference */
1085
+
1086
+ hfi1_free_ctxt (uctxt -> dd , uctxt );
1101
1087
}
1102
1088
1103
- static int init_subctxts (struct hfi1_ctxtdata * uctxt ,
1104
- const struct hfi1_user_info * uinfo )
1089
+ static void init_subctxts (struct hfi1_ctxtdata * uctxt ,
1090
+ const struct hfi1_user_info * uinfo )
1105
1091
{
1106
- u16 num_subctxts ;
1107
-
1108
- num_subctxts = uinfo -> subctxt_cnt ;
1109
- if (num_subctxts > HFI1_MAX_SHARED_CTXTS )
1110
- return - EINVAL ;
1111
-
1112
1092
uctxt -> subctxt_cnt = uinfo -> subctxt_cnt ;
1113
1093
uctxt -> subctxt_id = uinfo -> subctxt_id ;
1114
- uctxt -> redirect_seq_cnt = 1 ;
1115
1094
set_bit (HFI1_CTXT_BASE_UNINIT , & uctxt -> event_flags );
1116
-
1117
- return 0 ;
1118
1095
}
1119
1096
1120
1097
static int setup_subctxt (struct hfi1_ctxtdata * uctxt )
@@ -1302,8 +1279,8 @@ static int setup_base_ctxt(struct hfi1_filedata *fd,
1302
1279
return 0 ;
1303
1280
1304
1281
setup_failed :
1305
- /* Call _free_ctxtdata, not _rcd_put(). We still need the context. */
1306
- hfi1_free_ctxtdata ( dd , uctxt );
1282
+ set_bit ( HFI1_CTXT_BASE_FAILED , & uctxt -> event_flags );
1283
+ deallocate_ctxt ( uctxt );
1307
1284
return ret ;
1308
1285
}
1309
1286
0 commit comments