@@ -964,6 +964,143 @@ static int decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
964964 return - EINVAL ;
965965}
966966
967+ /*
968+ * CRUSH workspaces
969+ *
970+ * workspace_manager framework borrowed from fs/btrfs/compression.c.
971+ * Two simplifications: there is only one type of workspace and there
972+ * is always at least one workspace.
973+ */
974+ static struct crush_work * alloc_workspace (const struct crush_map * c )
975+ {
976+ struct crush_work * work ;
977+ size_t work_size ;
978+
979+ WARN_ON (!c -> working_size );
980+ work_size = crush_work_size (c , CEPH_PG_MAX_SIZE );
981+ dout ("%s work_size %zu bytes\n" , __func__ , work_size );
982+
983+ work = ceph_kvmalloc (work_size , GFP_NOIO );
984+ if (!work )
985+ return NULL ;
986+
987+ INIT_LIST_HEAD (& work -> item );
988+ crush_init_workspace (c , work );
989+ return work ;
990+ }
991+
992+ static void free_workspace (struct crush_work * work )
993+ {
994+ WARN_ON (!list_empty (& work -> item ));
995+ kvfree (work );
996+ }
997+
998+ static void init_workspace_manager (struct workspace_manager * wsm )
999+ {
1000+ INIT_LIST_HEAD (& wsm -> idle_ws );
1001+ spin_lock_init (& wsm -> ws_lock );
1002+ atomic_set (& wsm -> total_ws , 0 );
1003+ wsm -> free_ws = 0 ;
1004+ init_waitqueue_head (& wsm -> ws_wait );
1005+ }
1006+
1007+ static void add_initial_workspace (struct workspace_manager * wsm ,
1008+ struct crush_work * work )
1009+ {
1010+ WARN_ON (!list_empty (& wsm -> idle_ws ));
1011+
1012+ list_add (& work -> item , & wsm -> idle_ws );
1013+ atomic_set (& wsm -> total_ws , 1 );
1014+ wsm -> free_ws = 1 ;
1015+ }
1016+
1017+ static void cleanup_workspace_manager (struct workspace_manager * wsm )
1018+ {
1019+ struct crush_work * work ;
1020+
1021+ while (!list_empty (& wsm -> idle_ws )) {
1022+ work = list_first_entry (& wsm -> idle_ws , struct crush_work ,
1023+ item );
1024+ list_del_init (& work -> item );
1025+ free_workspace (work );
1026+ }
1027+ atomic_set (& wsm -> total_ws , 0 );
1028+ wsm -> free_ws = 0 ;
1029+ }
1030+
1031+ /*
1032+ * Finds an available workspace or allocates a new one. If it's not
1033+ * possible to allocate a new one, waits until there is one.
1034+ */
1035+ static struct crush_work * get_workspace (struct workspace_manager * wsm ,
1036+ const struct crush_map * c )
1037+ {
1038+ struct crush_work * work ;
1039+ int cpus = num_online_cpus ();
1040+
1041+ again :
1042+ spin_lock (& wsm -> ws_lock );
1043+ if (!list_empty (& wsm -> idle_ws )) {
1044+ work = list_first_entry (& wsm -> idle_ws , struct crush_work ,
1045+ item );
1046+ list_del_init (& work -> item );
1047+ wsm -> free_ws -- ;
1048+ spin_unlock (& wsm -> ws_lock );
1049+ return work ;
1050+
1051+ }
1052+ if (atomic_read (& wsm -> total_ws ) > cpus ) {
1053+ DEFINE_WAIT (wait );
1054+
1055+ spin_unlock (& wsm -> ws_lock );
1056+ prepare_to_wait (& wsm -> ws_wait , & wait , TASK_UNINTERRUPTIBLE );
1057+ if (atomic_read (& wsm -> total_ws ) > cpus && !wsm -> free_ws )
1058+ schedule ();
1059+ finish_wait (& wsm -> ws_wait , & wait );
1060+ goto again ;
1061+ }
1062+ atomic_inc (& wsm -> total_ws );
1063+ spin_unlock (& wsm -> ws_lock );
1064+
1065+ work = alloc_workspace (c );
1066+ if (!work ) {
1067+ atomic_dec (& wsm -> total_ws );
1068+ wake_up (& wsm -> ws_wait );
1069+
1070+ /*
1071+ * Do not return the error but go back to waiting. We
1072+ * have the inital workspace and the CRUSH computation
1073+ * time is bounded so we will get it eventually.
1074+ */
1075+ WARN_ON (atomic_read (& wsm -> total_ws ) < 1 );
1076+ goto again ;
1077+ }
1078+ return work ;
1079+ }
1080+
1081+ /*
1082+ * Puts a workspace back on the list or frees it if we have enough
1083+ * idle ones sitting around.
1084+ */
1085+ static void put_workspace (struct workspace_manager * wsm ,
1086+ struct crush_work * work )
1087+ {
1088+ spin_lock (& wsm -> ws_lock );
1089+ if (wsm -> free_ws <= num_online_cpus ()) {
1090+ list_add (& work -> item , & wsm -> idle_ws );
1091+ wsm -> free_ws ++ ;
1092+ spin_unlock (& wsm -> ws_lock );
1093+ goto wake ;
1094+ }
1095+ spin_unlock (& wsm -> ws_lock );
1096+
1097+ free_workspace (work );
1098+ atomic_dec (& wsm -> total_ws );
1099+ wake :
1100+ if (wq_has_sleeper (& wsm -> ws_wait ))
1101+ wake_up (& wsm -> ws_wait );
1102+ }
1103+
9671104/*
9681105 * osd map
9691106 */
@@ -981,16 +1118,20 @@ struct ceph_osdmap *ceph_osdmap_alloc(void)
9811118 map -> primary_temp = RB_ROOT ;
9821119 map -> pg_upmap = RB_ROOT ;
9831120 map -> pg_upmap_items = RB_ROOT ;
984- mutex_init (& map -> crush_workspace_mutex );
1121+
1122+ init_workspace_manager (& map -> crush_wsm );
9851123
9861124 return map ;
9871125}
9881126
9891127void ceph_osdmap_destroy (struct ceph_osdmap * map )
9901128{
9911129 dout ("osdmap_destroy %p\n" , map );
1130+
9921131 if (map -> crush )
9931132 crush_destroy (map -> crush );
1133+ cleanup_workspace_manager (& map -> crush_wsm );
1134+
9941135 while (!RB_EMPTY_ROOT (& map -> pg_temp )) {
9951136 struct ceph_pg_mapping * pg =
9961137 rb_entry (rb_first (& map -> pg_temp ),
@@ -1029,7 +1170,6 @@ void ceph_osdmap_destroy(struct ceph_osdmap *map)
10291170 kvfree (map -> osd_weight );
10301171 kvfree (map -> osd_addr );
10311172 kvfree (map -> osd_primary_affinity );
1032- kvfree (map -> crush_workspace );
10331173 kfree (map );
10341174}
10351175
@@ -1104,26 +1244,22 @@ static int osdmap_set_max_osd(struct ceph_osdmap *map, u32 max)
11041244
11051245static int osdmap_set_crush (struct ceph_osdmap * map , struct crush_map * crush )
11061246{
1107- void * workspace ;
1108- size_t work_size ;
1247+ struct crush_work * work ;
11091248
11101249 if (IS_ERR (crush ))
11111250 return PTR_ERR (crush );
11121251
1113- work_size = crush_work_size (crush , CEPH_PG_MAX_SIZE );
1114- dout ("%s work_size %zu bytes\n" , __func__ , work_size );
1115- workspace = ceph_kvmalloc (work_size , GFP_NOIO );
1116- if (!workspace ) {
1252+ work = alloc_workspace (crush );
1253+ if (!work ) {
11171254 crush_destroy (crush );
11181255 return - ENOMEM ;
11191256 }
1120- crush_init_workspace (crush , workspace );
11211257
11221258 if (map -> crush )
11231259 crush_destroy (map -> crush );
1124- kvfree ( map -> crush_workspace );
1260+ cleanup_workspace_manager ( & map -> crush_wsm );
11251261 map -> crush = crush ;
1126- map -> crush_workspace = workspace ;
1262+ add_initial_workspace ( & map -> crush_wsm , work ) ;
11271263 return 0 ;
11281264}
11291265
@@ -2322,6 +2458,7 @@ static int do_crush(struct ceph_osdmap *map, int ruleno, int x,
23222458 s64 choose_args_index )
23232459{
23242460 struct crush_choose_arg_map * arg_map ;
2461+ struct crush_work * work ;
23252462 int r ;
23262463
23272464 BUG_ON (result_max > CEPH_PG_MAX_SIZE );
@@ -2332,12 +2469,11 @@ static int do_crush(struct ceph_osdmap *map, int ruleno, int x,
23322469 arg_map = lookup_choose_arg_map (& map -> crush -> choose_args ,
23332470 CEPH_DEFAULT_CHOOSE_ARGS );
23342471
2335- mutex_lock (& map -> crush_workspace_mutex );
2472+ work = get_workspace (& map -> crush_wsm , map -> crush );
23362473 r = crush_do_rule (map -> crush , ruleno , x , result , result_max ,
2337- weight , weight_max , map -> crush_workspace ,
2474+ weight , weight_max , work ,
23382475 arg_map ? arg_map -> args : NULL );
2339- mutex_unlock (& map -> crush_workspace_mutex );
2340-
2476+ put_workspace (& map -> crush_wsm , work );
23412477 return r ;
23422478}
23432479
0 commit comments