Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

crush: sync up with userspace

.. up to ceph.git commit 1db1abc8328d ("crush: eliminate ad hoc diff
between kernel and userspace"). This fixes a bunch of recently pulled
coding style issues and makes includes a bit cleaner.

A patch "crush:Make the function crush_ln static" from Nicholas Krause
<xerofoify@gmail.com> is folded in as crush_ln() has been made static
in userspace as well.

Signed-off-by: Ilya Dryomov <idryomov@gmail.com>

+160 -78
+38 -2
include/linux/crush/crush.h
··· 1 1 #ifndef CEPH_CRUSH_CRUSH_H 2 2 #define CEPH_CRUSH_CRUSH_H 3 3 4 - #include <linux/types.h> 4 + #ifdef __KERNEL__ 5 + # include <linux/types.h> 6 + #else 7 + # include "crush_compat.h" 8 + #endif 5 9 6 10 /* 7 11 * CRUSH is a pseudo-random data distribution algorithm that ··· 24 20 #define CRUSH_MAGIC 0x00010000ul /* for detecting algorithm revisions */ 25 21 26 22 #define CRUSH_MAX_DEPTH 10 /* max crush hierarchy depth */ 23 + #define CRUSH_MAX_RULESET (1<<8) /* max crush ruleset number */ 24 + #define CRUSH_MAX_RULES CRUSH_MAX_RULESET /* should be the same as max rulesets */ 27 25 26 + #define CRUSH_MAX_DEVICE_WEIGHT (100u * 0x10000u) 27 + #define CRUSH_MAX_BUCKET_WEIGHT (65535u * 0x10000u) 28 28 29 29 #define CRUSH_ITEM_UNDEF 0x7ffffffe /* undefined result (internal use only) */ 30 30 #define CRUSH_ITEM_NONE 0x7fffffff /* no result */ ··· 116 108 }; 117 109 extern const char *crush_bucket_alg_name(int alg); 118 110 111 + /* 112 + * although tree was a legacy algorithm, it has been buggy, so 113 + * exclude it. 114 + */ 115 + #define CRUSH_LEGACY_ALLOWED_BUCKET_ALGS ( \ 116 + (1 << CRUSH_BUCKET_UNIFORM) | \ 117 + (1 << CRUSH_BUCKET_LIST) | \ 118 + (1 << CRUSH_BUCKET_STRAW)) 119 + 119 120 struct crush_bucket { 120 121 __s32 id; /* this'll be negative */ 121 122 __u16 type; /* non-zero; type=0 is reserved for devices */ ··· 191 174 /* choose local attempts using a fallback permutation before 192 175 * re-descent */ 193 176 __u32 choose_local_fallback_tries; 194 - /* choose attempts before giving up */ 177 + /* choose attempts before giving up */ 195 178 __u32 choose_total_tries; 196 179 /* attempt chooseleaf inner descent once for firstn mode; on 197 180 * reject retry outer descent. Note that this does *not* ··· 204 187 * that want to limit reshuffling, a value of 3 or 4 will make the 205 188 * mappings line up a bit better with previous mappings. */ 206 189 __u8 chooseleaf_vary_r; 190 + 191 + #ifndef __KERNEL__ 192 + /* 193 + * version 0 (original) of straw_calc has various flaws. version 1 194 + * fixes a few of them. 195 + */ 196 + __u8 straw_calc_version; 197 + 198 + /* 199 + * allowed bucket algs is a bitmask, here the bit positions 200 + * are CRUSH_BUCKET_*. note that these are *bits* and 201 + * CRUSH_BUCKET_* values are not, so we need to or together (1 202 + * << CRUSH_BUCKET_WHATEVER). The 0th bit is not used to 203 + * minimize confusion (bucket type values start at 1). 204 + */ 205 + __u32 allowed_bucket_algs; 206 + 207 + __u32 *choose_tries; 208 + #endif 207 209 }; 208 210 209 211
+6
include/linux/crush/hash.h
··· 1 1 #ifndef CEPH_CRUSH_HASH_H 2 2 #define CEPH_CRUSH_HASH_H 3 3 4 + #ifdef __KERNEL__ 5 + # include <linux/types.h> 6 + #else 7 + # include "crush_compat.h" 8 + #endif 9 + 4 10 #define CRUSH_HASH_RJENKINS1 0 5 11 6 12 #define CRUSH_HASH_DEFAULT CRUSH_HASH_RJENKINS1
+1 -1
include/linux/crush/mapper.h
··· 8 8 * LGPL2 9 9 */ 10 10 11 - #include <linux/crush/crush.h> 11 + #include "crush.h" 12 12 13 13 extern int crush_find_rule(const struct crush_map *map, int ruleset, int type, int size); 14 14 extern int crush_do_rule(const struct crush_map *map,
+6 -7
net/ceph/crush/crush.c
··· 1 - 2 1 #ifdef __KERNEL__ 3 2 # include <linux/slab.h> 3 + # include <linux/crush/crush.h> 4 4 #else 5 - # include <stdlib.h> 6 - # include <assert.h> 7 - # define kfree(x) do { if (x) free(x); } while (0) 8 - # define BUG_ON(x) assert(!(x)) 5 + # include "crush_compat.h" 6 + # include "crush.h" 9 7 #endif 10 - 11 - #include <linux/crush/crush.h> 12 8 13 9 const char *crush_bucket_alg_name(int alg) 14 10 { ··· 130 134 kfree(map->rules); 131 135 } 132 136 137 + #ifndef __KERNEL__ 138 + kfree(map->choose_tries); 139 + #endif 133 140 kfree(map); 134 141 } 135 142
+15 -17
net/ceph/crush/crush_ln_table.h
··· 10 10 * 11 11 */ 12 12 13 - #if defined(__linux__) 14 - #include <linux/types.h> 15 - #elif defined(__FreeBSD__) 16 - #include <sys/types.h> 17 - #endif 18 - 19 13 #ifndef CEPH_CRUSH_LN_H 20 14 #define CEPH_CRUSH_LN_H 21 15 16 + #ifdef __KERNEL__ 17 + # include <linux/types.h> 18 + #else 19 + # include "crush_compat.h" 20 + #endif 22 21 23 - // RH_LH_tbl[2*k] = 2^48/(1.0+k/128.0) 24 - // RH_LH_tbl[2*k+1] = 2^48*log2(1.0+k/128.0) 25 - 26 - static int64_t __RH_LH_tbl[128*2+2] = { 22 + /* 23 + * RH_LH_tbl[2*k] = 2^48/(1.0+k/128.0) 24 + * RH_LH_tbl[2*k+1] = 2^48*log2(1.0+k/128.0) 25 + */ 26 + static __s64 __RH_LH_tbl[128*2+2] = { 27 27 0x0001000000000000ll, 0x0000000000000000ll, 0x0000fe03f80fe040ll, 0x000002dfca16dde1ll, 28 28 0x0000fc0fc0fc0fc1ll, 0x000005b9e5a170b4ll, 0x0000fa232cf25214ll, 0x0000088e68ea899all, 29 29 0x0000f83e0f83e0f9ll, 0x00000b5d69bac77ell, 0x0000f6603d980f67ll, 0x00000e26fd5c8555ll, ··· 89 89 0x0000820820820821ll, 0x0000fa2f045e7832ll, 0x000081848da8faf1ll, 0x0000fba577877d7dll, 90 90 0x0000810204081021ll, 0x0000fd1a708bbe11ll, 0x0000808080808081ll, 0x0000fe8df263f957ll, 91 91 0x0000800000000000ll, 0x0000ffff00000000ll, 92 - }; 92 + }; 93 93 94 - 95 - // LL_tbl[k] = 2^48*log2(1.0+k/2^15); 96 - static int64_t __LL_tbl[256] = { 94 + /* 95 + * LL_tbl[k] = 2^48*log2(1.0+k/2^15) 96 + */ 97 + static __s64 __LL_tbl[256] = { 97 98 0x0000000000000000ull, 0x00000002e2a60a00ull, 0x000000070cb64ec5ull, 0x00000009ef50ce67ull, 98 99 0x0000000cd1e588fdull, 0x0000000fb4747e9cull, 0x0000001296fdaf5eull, 0x0000001579811b58ull, 99 100 0x000000185bfec2a1ull, 0x0000001b3e76a552ull, 0x0000001e20e8c380ull, 0x0000002103551d43ull, ··· 160 159 0x000002c8e1e891f6ull, 0x000002cbbf023fc2ull, 0x000002ce9c163e6eull, 0x000002d179248e13ull, 161 160 0x000002d4562d2ec6ull, 0x000002d73330209dull, 0x000002da102d63b0ull, 0x000002dced24f814ull, 162 161 }; 163 - 164 - 165 - 166 162 167 163 #endif
+5 -3
net/ceph/crush/hash.c
··· 1 - 2 - #include <linux/types.h> 3 - #include <linux/crush/hash.h> 1 + #ifdef __KERNEL__ 2 + # include <linux/crush/hash.h> 3 + #else 4 + # include "hash.h" 5 + #endif 4 6 5 7 /* 6 8 * Robert Jenkins' function for mixing 32-bit values
+89 -48
net/ceph/crush/mapper.c
··· 1 + /* 2 + * Ceph - scalable distributed file system 3 + * 4 + * Copyright (C) 2015 Intel Corporation All Rights Reserved 5 + * 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. 10 + * 11 + */ 1 12 2 13 #ifdef __KERNEL__ 3 14 # include <linux/string.h> 4 15 # include <linux/slab.h> 5 16 # include <linux/bug.h> 6 17 # include <linux/kernel.h> 7 - # ifndef dprintk 8 - # define dprintk(args...) 9 - # endif 18 + # include <linux/crush/crush.h> 19 + # include <linux/crush/hash.h> 10 20 #else 11 - # include <string.h> 12 - # include <stdio.h> 13 - # include <stdlib.h> 14 - # include <assert.h> 15 - # define BUG_ON(x) assert(!(x)) 16 - # define dprintk(args...) /* printf(args) */ 17 - # define kmalloc(x, f) malloc(x) 18 - # define kfree(x) free(x) 21 + # include "crush_compat.h" 22 + # include "crush.h" 23 + # include "hash.h" 19 24 #endif 20 - 21 - #include <linux/crush/crush.h> 22 - #include <linux/crush/hash.h> 23 25 #include "crush_ln_table.h" 26 + 27 + #define dprintk(args...) /* printf(args) */ 24 28 25 29 /* 26 30 * Implement the core CRUSH mapping algorithm. ··· 143 139 int i; 144 140 145 141 for (i = bucket->h.size-1; i >= 0; i--) { 146 - __u64 w = crush_hash32_4(bucket->h.hash,x, bucket->h.items[i], 142 + __u64 w = crush_hash32_4(bucket->h.hash, x, bucket->h.items[i], 147 143 r, bucket->h.id); 148 144 w &= 0xffff; 149 145 dprintk("list_choose i=%d x=%d r=%d item %d weight %x " ··· 242 238 return bucket->h.items[high]; 243 239 } 244 240 245 - // compute 2^44*log2(input+1) 246 - uint64_t crush_ln(unsigned xin) 241 + /* compute 2^44*log2(input+1) */ 242 + static __u64 crush_ln(unsigned int xin) 247 243 { 248 - unsigned x=xin, x1; 249 - int iexpon, index1, index2; 250 - uint64_t RH, LH, LL, xl64, result; 244 + unsigned int x = xin, x1; 245 + int iexpon, index1, index2; 246 + __u64 RH, LH, LL, xl64, result; 251 247 252 - x++; 248 + x++; 253 249 254 - // normalize input 255 - iexpon = 15; 256 - while(!(x&0x18000)) { x<<=1; iexpon--; } 250 + /* normalize input */ 251 + iexpon = 15; 252 + while (!(x & 0x18000)) { 253 + x <<= 1; 254 + iexpon--; 255 + } 257 256 258 - index1 = (x>>8)<<1; 259 - // RH ~ 2^56/index1 260 - RH = __RH_LH_tbl[index1 - 256]; 261 - // LH ~ 2^48 * log2(index1/256) 262 - LH = __RH_LH_tbl[index1 + 1 - 256]; 257 + index1 = (x >> 8) << 1; 258 + /* RH ~ 2^56/index1 */ 259 + RH = __RH_LH_tbl[index1 - 256]; 260 + /* LH ~ 2^48 * log2(index1/256) */ 261 + LH = __RH_LH_tbl[index1 + 1 - 256]; 263 262 264 - // RH*x ~ 2^48 * (2^15 + xf), xf<2^8 265 - xl64 = (int64_t)x * RH; 266 - xl64 >>= 48; 267 - x1 = xl64; 263 + /* RH*x ~ 2^48 * (2^15 + xf), xf<2^8 */ 264 + xl64 = (__s64)x * RH; 265 + xl64 >>= 48; 266 + x1 = xl64; 268 267 269 - result = iexpon; 270 - result <<= (12 + 32); 268 + result = iexpon; 269 + result <<= (12 + 32); 271 270 272 - index2 = x1 & 0xff; 273 - // LL ~ 2^48*log2(1.0+index2/2^15) 274 - LL = __LL_tbl[index2]; 271 + index2 = x1 & 0xff; 272 + /* LL ~ 2^48*log2(1.0+index2/2^15) */ 273 + LL = __LL_tbl[index2]; 275 274 276 - LH = LH + LL; 275 + LH = LH + LL; 277 276 278 - LH >>= (48-12 - 32); 279 - result += LH; 277 + LH >>= (48 - 12 - 32); 278 + result += LH; 280 279 281 - return result; 280 + return result; 282 281 } 283 282 284 283 ··· 297 290 static int bucket_straw2_choose(struct crush_bucket_straw2 *bucket, 298 291 int x, int r) 299 292 { 300 - unsigned i, high = 0; 301 - unsigned u; 302 - unsigned w; 293 + unsigned int i, high = 0; 294 + unsigned int u; 295 + unsigned int w; 303 296 __s64 ln, draw, high_draw = 0; 304 297 305 298 for (i = 0; i < bucket->h.size; i++) { ··· 574 567 out[outpos] = item; 575 568 outpos++; 576 569 count--; 570 + #ifndef __KERNEL__ 571 + if (map->choose_tries && ftotal <= map->choose_total_tries) 572 + map->choose_tries[ftotal]++; 573 + #endif 577 574 } 578 575 579 576 dprintk("CHOOSE returns %d\n", outpos); ··· 621 610 } 622 611 623 612 for (ftotal = 0; left > 0 && ftotal < tries; ftotal++) { 613 + #ifdef DEBUG_INDEP 614 + if (out2 && ftotal) { 615 + dprintk("%u %d a: ", ftotal, left); 616 + for (rep = outpos; rep < endpos; rep++) { 617 + dprintk(" %d", out[rep]); 618 + } 619 + dprintk("\n"); 620 + dprintk("%u %d b: ", ftotal, left); 621 + for (rep = outpos; rep < endpos; rep++) { 622 + dprintk(" %d", out2[rep]); 623 + } 624 + dprintk("\n"); 625 + } 626 + #endif 624 627 for (rep = outpos; rep < endpos; rep++) { 625 628 if (out[rep] != CRUSH_ITEM_UNDEF) 626 629 continue; ··· 751 726 out2[rep] = CRUSH_ITEM_NONE; 752 727 } 753 728 } 729 + #ifndef __KERNEL__ 730 + if (map->choose_tries && ftotal <= map->choose_total_tries) 731 + map->choose_tries[ftotal]++; 732 + #endif 733 + #ifdef DEBUG_INDEP 734 + if (out2) { 735 + dprintk("%u %d a: ", ftotal, left); 736 + for (rep = outpos; rep < endpos; rep++) { 737 + dprintk(" %d", out[rep]); 738 + } 739 + dprintk("\n"); 740 + dprintk("%u %d b: ", ftotal, left); 741 + for (rep = outpos; rep < endpos; rep++) { 742 + dprintk(" %d", out2[rep]); 743 + } 744 + dprintk("\n"); 745 + } 746 + #endif 754 747 } 755 748 756 749 /** ··· 927 884 0); 928 885 } else { 929 886 out_size = ((numrep < (result_max-osize)) ? 930 - numrep : (result_max-osize)); 887 + numrep : (result_max-osize)); 931 888 crush_choose_indep( 932 889 map, 933 890 map->buckets[-1-w[i]], ··· 973 930 } 974 931 return result_len; 975 932 } 976 - 977 -