2 * Ceph - scalable distributed file system
4 * Copyright (C) 2015 Intel Corporation All Rights Reserved
6 * This is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License version 2.1, as published by the Free Software
9 * Foundation. See file COPYING.
14 # include <linux/string.h>
15 # include <linux/slab.h>
16 # include <linux/bug.h>
17 # include <linux/kernel.h>
18 # include <linux/crush/crush.h>
19 # include <linux/crush/hash.h>
20 # include <linux/crush/mapper.h>
22 # include "crush_compat.h"
27 #include "crush_ln_table.h"
29 #define dprintk(args...) /* printf(args) */
32 * Implement the core CRUSH mapping algorithm.
36 * crush_find_rule - find a crush_rule id for a given ruleset, type, and size.
38 * @ruleset: the storage ruleset id (user defined)
39 * @type: storage ruleset type (user defined)
40 * @size: output set size
42 int crush_find_rule(const struct crush_map *map, int ruleset, int type, int size)
46 for (i = 0; i < map->max_rules; i++) {
48 map->rules[i]->mask.ruleset == ruleset &&
49 map->rules[i]->mask.type == type &&
50 map->rules[i]->mask.min_size <= size &&
51 map->rules[i]->mask.max_size >= size)
58 * bucket choose methods
60 * For each bucket algorithm, we have a "choose" method that, given a
61 * crush input @x and replica position (usually, position in output set) @r,
62 * will produce an item in the bucket.
66 * Choose based on a random permutation of the bucket.
68 * We used to use some prime number arithmetic to do this, but it
69 * wasn't very random, and had some other bad behaviors. Instead, we
70 * calculate an actual random permutation of the bucket members.
71 * Since this is expensive, we optimize for the r=0 case, which
72 * captures the vast majority of calls.
74 static int bucket_perm_choose(const struct crush_bucket *bucket,
75 struct crush_work_bucket *work,
78 unsigned int pr = r % bucket->size;
81 /* start a new permutation if @x has changed */
82 if (work->perm_x != (__u32)x || work->perm_n == 0) {
83 dprintk("bucket %d new x=%d\n", bucket->id, x);
86 /* optimize common r=0 case */
88 s = crush_hash32_3(bucket->hash, x, bucket->id, 0) %
91 work->perm_n = 0xffff; /* magic value, see below */
95 for (i = 0; i < bucket->size; i++)
98 } else if (work->perm_n == 0xffff) {
99 /* clean up after the r=0 case above */
100 for (i = 1; i < bucket->size; i++)
102 work->perm[work->perm[0]] = 0;
106 /* calculate permutation up to pr */
107 for (i = 0; i < work->perm_n; i++)
108 dprintk(" perm_choose have %d: %d\n", i, work->perm[i]);
109 while (work->perm_n <= pr) {
110 unsigned int p = work->perm_n;
111 /* no point in swapping the final entry */
112 if (p < bucket->size - 1) {
113 i = crush_hash32_3(bucket->hash, x, bucket->id, p) %
116 unsigned int t = work->perm[p + i];
117 work->perm[p + i] = work->perm[p];
120 dprintk(" perm_choose swap %d with %d\n", p, p+i);
124 for (i = 0; i < bucket->size; i++)
125 dprintk(" perm_choose %d: %d\n", i, work->perm[i]);
129 dprintk(" perm_choose %d sz=%d x=%d r=%d (%d) s=%d\n", bucket->id,
130 bucket->size, x, r, pr, s);
131 return bucket->items[s];
135 static int bucket_uniform_choose(const struct crush_bucket_uniform *bucket,
136 struct crush_work_bucket *work, int x, int r)
138 return bucket_perm_choose(&bucket->h, work, x, r);
142 static int bucket_list_choose(const struct crush_bucket_list *bucket,
147 for (i = bucket->h.size-1; i >= 0; i--) {
148 __u64 w = crush_hash32_4(bucket->h.hash, x, bucket->h.items[i],
151 dprintk("list_choose i=%d x=%d r=%d item %d weight %x "
153 i, x, r, bucket->h.items[i], bucket->item_weights[i],
154 bucket->sum_weights[i], w);
155 w *= bucket->sum_weights[i];
157 /*dprintk(" scaled %llx\n", w);*/
158 if (w < bucket->item_weights[i]) {
159 return bucket->h.items[i];
163 dprintk("bad list sums for bucket %d\n", bucket->h.id);
164 return bucket->h.items[0];
169 static int height(int n)
172 while ((n & 1) == 0) {
179 static int left(int x)
182 return x - (1 << (h-1));
185 static int right(int x)
188 return x + (1 << (h-1));
191 static int terminal(int x)
196 static int bucket_tree_choose(const struct crush_bucket_tree *bucket,
204 n = bucket->num_nodes >> 1;
206 while (!terminal(n)) {
208 /* pick point in [0, w) */
209 w = bucket->node_weights[n];
210 t = (__u64)crush_hash32_4(bucket->h.hash, x, n, r,
211 bucket->h.id) * (__u64)w;
214 /* descend to the left or right? */
216 if (t < bucket->node_weights[l])
222 return bucket->h.items[n >> 1];
228 static int bucket_straw_choose(const struct crush_bucket_straw *bucket,
236 for (i = 0; i < bucket->h.size; i++) {
237 draw = crush_hash32_3(bucket->h.hash, x, bucket->h.items[i], r);
239 draw *= bucket->straws[i];
240 if (i == 0 || draw > high_draw) {
245 return bucket->h.items[high];
248 /* compute 2^44*log2(input+1) */
249 static __u64 crush_ln(unsigned int xin)
251 unsigned int x = xin;
252 int iexpon, index1, index2;
253 __u64 RH, LH, LL, xl64, result;
257 /* normalize input */
261 * figure out number of bits we need to shift and
262 * do it in one step instead of iteratively
264 if (!(x & 0x18000)) {
265 int bits = __builtin_clz(x & 0x1FFFF) - 16;
270 index1 = (x >> 8) << 1;
271 /* RH ~ 2^56/index1 */
272 RH = __RH_LH_tbl[index1 - 256];
273 /* LH ~ 2^48 * log2(index1/256) */
274 LH = __RH_LH_tbl[index1 + 1 - 256];
276 /* RH*x ~ 2^48 * (2^15 + xf), xf<2^8 */
277 xl64 = (__s64)x * RH;
281 result <<= (12 + 32);
283 index2 = xl64 & 0xff;
284 /* LL ~ 2^48*log2(1.0+index2/2^15) */
285 LL = __LL_tbl[index2];
289 LH >>= (48 - 12 - 32);
299 * for reference, see:
301 * https://en.wikipedia.org/wiki/Exponential_distribution#Distribution_of_the_minimum_of_exponential_random_variables
305 static __u32 *get_choose_arg_weights(const struct crush_bucket_straw2 *bucket,
306 const struct crush_choose_arg *arg,
309 if (!arg || !arg->weight_set)
310 return bucket->item_weights;
312 if (position >= arg->weight_set_size)
313 position = arg->weight_set_size - 1;
314 return arg->weight_set[position].weights;
317 static __s32 *get_choose_arg_ids(const struct crush_bucket_straw2 *bucket,
318 const struct crush_choose_arg *arg)
320 if (!arg || !arg->ids)
321 return bucket->h.items;
326 static int bucket_straw2_choose(const struct crush_bucket_straw2 *bucket,
328 const struct crush_choose_arg *arg,
331 unsigned int i, high = 0;
333 __s64 ln, draw, high_draw = 0;
334 __u32 *weights = get_choose_arg_weights(bucket, arg, position);
335 __s32 *ids = get_choose_arg_ids(bucket, arg);
337 for (i = 0; i < bucket->h.size; i++) {
338 dprintk("weight 0x%x item %d\n", weights[i], ids[i]);
340 u = crush_hash32_3(bucket->h.hash, x, ids[i], r);
344 * for some reason slightly less than 0x10000 produces
345 * a slightly more accurate distribution... probably a
348 * the natural log lookup table maps [0,0xffff]
349 * (corresponding to real numbers [1/0x10000, 1] to
350 * [0, 0xffffffffffff] (corresponding to real numbers
353 ln = crush_ln(u) - 0x1000000000000ll;
356 * divide by 16.16 fixed-point weight. note
357 * that the ln value is negative, so a larger
358 * weight means a larger (less negative) value
361 draw = div64_s64(ln, weights[i]);
366 if (i == 0 || draw > high_draw) {
372 return bucket->h.items[high];
376 static int crush_bucket_choose(const struct crush_bucket *in,
377 struct crush_work_bucket *work,
379 const struct crush_choose_arg *arg,
382 dprintk(" crush_bucket_choose %d x=%d r=%d\n", in->id, x, r);
383 BUG_ON(in->size == 0);
385 case CRUSH_BUCKET_UNIFORM:
386 return bucket_uniform_choose(
387 (const struct crush_bucket_uniform *)in,
389 case CRUSH_BUCKET_LIST:
390 return bucket_list_choose((const struct crush_bucket_list *)in,
392 case CRUSH_BUCKET_TREE:
393 return bucket_tree_choose((const struct crush_bucket_tree *)in,
395 case CRUSH_BUCKET_STRAW:
396 return bucket_straw_choose(
397 (const struct crush_bucket_straw *)in,
399 case CRUSH_BUCKET_STRAW2:
400 return bucket_straw2_choose(
401 (const struct crush_bucket_straw2 *)in,
402 x, r, arg, position);
404 dprintk("unknown bucket %d alg %d\n", in->id, in->alg);
410 * true if device is marked "out" (failed, fully offloaded)
413 static int is_out(const struct crush_map *map,
414 const __u32 *weight, int weight_max,
417 if (item >= weight_max)
419 if (weight[item] >= 0x10000)
421 if (weight[item] == 0)
423 if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff)
430 * crush_choose_firstn - choose numrep distinct items of given type
431 * @map: the crush_map
432 * @bucket: the bucket we are choose an item from
433 * @x: crush input value
434 * @numrep: the number of items to choose
435 * @type: the type of item to choose
436 * @out: pointer to output vector
437 * @outpos: our position in that vector
438 * @out_size: size of the out vector
439 * @tries: number of attempts to make
440 * @recurse_tries: number of attempts to have recursive chooseleaf make
441 * @local_retries: localized retries
442 * @local_fallback_retries: localized fallback retries
443 * @recurse_to_leaf: true if we want one device under each item of given type (chooseleaf instead of choose)
444 * @stable: stable mode starts rep=0 in the recursive call for all replicas
445 * @vary_r: pass r to recursive calls
446 * @out2: second output vector for leaf items (if @recurse_to_leaf)
447 * @parent_r: r value passed from the parent
449 static int crush_choose_firstn(const struct crush_map *map,
450 struct crush_work *work,
451 const struct crush_bucket *bucket,
452 const __u32 *weight, int weight_max,
453 int x, int numrep, int type,
454 int *out, int outpos,
457 unsigned int recurse_tries,
458 unsigned int local_retries,
459 unsigned int local_fallback_retries,
465 const struct crush_choose_arg *choose_args)
468 unsigned int ftotal, flocal;
469 int retry_descent, retry_bucket, skip_rep;
470 const struct crush_bucket *in = bucket;
476 int count = out_size;
478 dprintk("CHOOSE%s bucket %d x %d outpos %d numrep %d tries %d recurse_tries %d local_retries %d local_fallback_retries %d parent_r %d stable %d\n",
479 recurse_to_leaf ? "_LEAF" : "",
480 bucket->id, x, outpos, numrep,
481 tries, recurse_tries, local_retries, local_fallback_retries,
484 for (rep = stable ? 0 : outpos; rep < numrep && count > 0 ; rep++) {
485 /* keep trying until we get a non-out, non-colliding item */
490 in = bucket; /* initial bucket */
492 /* choose through intervening buckets */
498 /* r' = r + f_total */
506 if (local_fallback_retries > 0 &&
507 flocal >= (in->size>>1) &&
508 flocal > local_fallback_retries)
509 item = bucket_perm_choose(
510 in, work->work[-1-in->id],
513 item = crush_bucket_choose(
514 in, work->work[-1-in->id],
517 &choose_args[-1-in->id] : NULL),
519 if (item >= map->max_devices) {
520 dprintk(" bad item %d\n", item);
527 itemtype = map->buckets[-1-item]->type;
530 dprintk(" item %d type %d\n", item, itemtype);
533 if (itemtype != type) {
535 (-1-item) >= map->max_buckets) {
536 dprintk(" bad item type %d\n", type);
540 in = map->buckets[-1-item];
546 for (i = 0; i < outpos; i++) {
547 if (out[i] == item) {
554 if (!collide && recurse_to_leaf) {
558 sub_r = r >> (vary_r-1);
561 if (crush_choose_firstn(
564 map->buckets[-1-item],
566 x, stable ? 1 : outpos+1, 0,
570 local_fallback_retries,
576 choose_args) <= outpos)
577 /* didn't get leaf */
580 /* we already have a leaf! */
585 if (!reject && !collide) {
588 reject = is_out(map, weight,
594 if (reject || collide) {
598 if (collide && flocal <= local_retries)
599 /* retry locally a few times */
601 else if (local_fallback_retries > 0 &&
602 flocal <= in->size + local_fallback_retries)
603 /* exhaustive bucket search */
605 else if (ftotal < tries)
606 /* then retry descent */
611 dprintk(" reject %d collide %d "
612 "ftotal %u flocal %u\n",
613 reject, collide, ftotal,
616 } while (retry_bucket);
617 } while (retry_descent);
620 dprintk("skip rep\n");
624 dprintk("CHOOSE got %d\n", item);
629 if (map->choose_tries && ftotal <= map->choose_total_tries)
630 map->choose_tries[ftotal]++;
634 dprintk("CHOOSE returns %d\n", outpos);
640 * crush_choose_indep: alternative breadth-first positionally stable mapping
643 static void crush_choose_indep(const struct crush_map *map,
644 struct crush_work *work,
645 const struct crush_bucket *bucket,
646 const __u32 *weight, int weight_max,
647 int x, int left, int numrep, int type,
648 int *out, int outpos,
650 unsigned int recurse_tries,
654 const struct crush_choose_arg *choose_args)
656 const struct crush_bucket *in = bucket;
657 int endpos = outpos + left;
666 dprintk("CHOOSE%s INDEP bucket %d x %d outpos %d numrep %d\n", recurse_to_leaf ? "_LEAF" : "",
667 bucket->id, x, outpos, numrep);
669 /* initially my result is undefined */
670 for (rep = outpos; rep < endpos; rep++) {
671 out[rep] = CRUSH_ITEM_UNDEF;
673 out2[rep] = CRUSH_ITEM_UNDEF;
676 for (ftotal = 0; left > 0 && ftotal < tries; ftotal++) {
678 if (out2 && ftotal) {
679 dprintk("%u %d a: ", ftotal, left);
680 for (rep = outpos; rep < endpos; rep++) {
681 dprintk(" %d", out[rep]);
684 dprintk("%u %d b: ", ftotal, left);
685 for (rep = outpos; rep < endpos; rep++) {
686 dprintk(" %d", out2[rep]);
691 for (rep = outpos; rep < endpos; rep++) {
692 if (out[rep] != CRUSH_ITEM_UNDEF)
695 in = bucket; /* initial bucket */
697 /* choose through intervening buckets */
699 /* note: we base the choice on the position
700 * even in the nested call. that means that
701 * if the first layer chooses the same bucket
702 * in a different position, we will tend to
703 * choose a different item in that bucket.
704 * this will involve more devices in data
705 * movement and tend to distribute the load.
710 if (in->alg == CRUSH_BUCKET_UNIFORM &&
711 in->size % numrep == 0)
712 /* r'=r+(n+1)*f_total */
713 r += (numrep+1) * ftotal;
715 /* r' = r + n*f_total */
716 r += numrep * ftotal;
720 dprintk(" empty bucket\n");
724 item = crush_bucket_choose(
725 in, work->work[-1-in->id],
728 &choose_args[-1-in->id] : NULL),
730 if (item >= map->max_devices) {
731 dprintk(" bad item %d\n", item);
732 out[rep] = CRUSH_ITEM_NONE;
734 out2[rep] = CRUSH_ITEM_NONE;
741 itemtype = map->buckets[-1-item]->type;
744 dprintk(" item %d type %d\n", item, itemtype);
747 if (itemtype != type) {
749 (-1-item) >= map->max_buckets) {
750 dprintk(" bad item type %d\n", type);
751 out[rep] = CRUSH_ITEM_NONE;
758 in = map->buckets[-1-item];
764 for (i = outpos; i < endpos; i++) {
765 if (out[i] == item) {
773 if (recurse_to_leaf) {
778 map->buckets[-1-item],
785 if (out2[rep] == CRUSH_ITEM_NONE) {
786 /* placed nothing; no leaf */
790 /* we already have a leaf! */
797 is_out(map, weight, weight_max, item, x))
807 for (rep = outpos; rep < endpos; rep++) {
808 if (out[rep] == CRUSH_ITEM_UNDEF) {
809 out[rep] = CRUSH_ITEM_NONE;
811 if (out2 && out2[rep] == CRUSH_ITEM_UNDEF) {
812 out2[rep] = CRUSH_ITEM_NONE;
816 if (map->choose_tries && ftotal <= map->choose_total_tries)
817 map->choose_tries[ftotal]++;
821 dprintk("%u %d a: ", ftotal, left);
822 for (rep = outpos; rep < endpos; rep++) {
823 dprintk(" %d", out[rep]);
826 dprintk("%u %d b: ", ftotal, left);
827 for (rep = outpos; rep < endpos; rep++) {
828 dprintk(" %d", out2[rep]);
837 * This takes a chunk of memory and sets it up to be a shiny new
838 * working area for a CRUSH placement computation. It must be called
839 * on any newly allocated memory before passing it in to
840 * crush_do_rule. It may be used repeatedly after that, so long as the
841 * map has not changed. If the map /has/ changed, you must make sure
842 * the working size is no smaller than what was allocated and re-run
843 * crush_init_workspace.
845 * If you do retain the working space between calls to crush, make it
848 void crush_init_workspace(const struct crush_map *map, void *v)
850 struct crush_work *w = v;
854 * We work by moving through the available space and setting
855 * values and pointers as we go.
857 * It's a bit like Forth's use of the 'allot' word since we
858 * set the pointer first and then reserve the space for it to
859 * point to by incrementing the point.
861 v += sizeof(struct crush_work);
863 v += map->max_buckets * sizeof(struct crush_work_bucket *);
864 for (b = 0; b < map->max_buckets; ++b) {
865 if (!map->buckets[b])
869 switch (map->buckets[b]->alg) {
871 v += sizeof(struct crush_work_bucket);
874 w->work[b]->perm_x = 0;
875 w->work[b]->perm_n = 0;
876 w->work[b]->perm = v;
877 v += map->buckets[b]->size * sizeof(__u32);
879 BUG_ON(v - (void *)w != map->working_size);
883 * crush_do_rule - calculate a mapping with the given input and rule
884 * @map: the crush_map
885 * @ruleno: the rule id
887 * @result: pointer to result vector
888 * @result_max: maximum result size
889 * @weight: weight vector (for map leaves)
890 * @weight_max: size of weight vector
891 * @cwin: pointer to at least crush_work_size() bytes of memory
892 * @choose_args: weights and ids for each known bucket
894 int crush_do_rule(const struct crush_map *map,
895 int ruleno, int x, int *result, int result_max,
896 const __u32 *weight, int weight_max,
897 void *cwin, const struct crush_choose_arg *choose_args)
900 struct crush_work *cw = cwin;
901 int *a = cwin + map->working_size;
902 int *b = a + result_max;
903 int *c = b + result_max;
910 const struct crush_rule *rule;
916 * the original choose_total_tries value was off by one (it
917 * counted "retries" and not "tries"). add one.
919 int choose_tries = map->choose_total_tries + 1;
920 int choose_leaf_tries = 0;
922 * the local tries values were counted as "retries", though,
923 * and need no adjustment
925 int choose_local_retries = map->choose_local_tries;
926 int choose_local_fallback_retries = map->choose_local_fallback_tries;
928 int vary_r = map->chooseleaf_vary_r;
929 int stable = map->chooseleaf_stable;
931 if ((__u32)ruleno >= map->max_rules) {
932 dprintk(" bad ruleno %d\n", ruleno);
936 rule = map->rules[ruleno];
939 for (step = 0; step < rule->len; step++) {
941 const struct crush_rule_step *curstep = &rule->steps[step];
943 switch (curstep->op) {
944 case CRUSH_RULE_TAKE:
945 if ((curstep->arg1 >= 0 &&
946 curstep->arg1 < map->max_devices) ||
947 (-1-curstep->arg1 >= 0 &&
948 -1-curstep->arg1 < map->max_buckets &&
949 map->buckets[-1-curstep->arg1])) {
950 w[0] = curstep->arg1;
953 dprintk(" bad take value %d\n", curstep->arg1);
957 case CRUSH_RULE_SET_CHOOSE_TRIES:
958 if (curstep->arg1 > 0)
959 choose_tries = curstep->arg1;
962 case CRUSH_RULE_SET_CHOOSELEAF_TRIES:
963 if (curstep->arg1 > 0)
964 choose_leaf_tries = curstep->arg1;
967 case CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES:
968 if (curstep->arg1 >= 0)
969 choose_local_retries = curstep->arg1;
972 case CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES:
973 if (curstep->arg1 >= 0)
974 choose_local_fallback_retries = curstep->arg1;
977 case CRUSH_RULE_SET_CHOOSELEAF_VARY_R:
978 if (curstep->arg1 >= 0)
979 vary_r = curstep->arg1;
982 case CRUSH_RULE_SET_CHOOSELEAF_STABLE:
983 if (curstep->arg1 >= 0)
984 stable = curstep->arg1;
987 case CRUSH_RULE_CHOOSELEAF_FIRSTN:
988 case CRUSH_RULE_CHOOSE_FIRSTN:
991 case CRUSH_RULE_CHOOSELEAF_INDEP:
992 case CRUSH_RULE_CHOOSE_INDEP:
998 CRUSH_RULE_CHOOSELEAF_FIRSTN ||
1000 CRUSH_RULE_CHOOSELEAF_INDEP;
1005 for (i = 0; i < wsize; i++) {
1007 numrep = curstep->arg1;
1009 numrep += result_max;
1014 /* make sure bucket id is valid */
1016 if (bno < 0 || bno >= map->max_buckets) {
1017 /* w[i] is probably CRUSH_ITEM_NONE */
1018 dprintk(" bad w[i] %d\n", w[i]);
1023 if (choose_leaf_tries)
1026 else if (map->chooseleaf_descend_once)
1029 recurse_tries = choose_tries;
1030 osize += crush_choose_firstn(
1041 choose_local_retries,
1042 choose_local_fallback_retries,
1050 out_size = ((numrep < (result_max-osize)) ?
1051 numrep : (result_max-osize));
1057 x, out_size, numrep,
1062 choose_leaf_tries : 1,
1071 if (recurse_to_leaf)
1072 /* copy final _leaf_ values to output set */
1073 memcpy(o, c, osize*sizeof(*o));
1075 /* swap o and w arrays */
1083 case CRUSH_RULE_EMIT:
1084 for (i = 0; i < wsize && result_len < result_max; i++) {
1085 result[result_len] = w[i];
1092 dprintk(" unknown op %d at step %d\n",