tracing_map.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * tracing_map - lock-free map for tracing
  4. *
  5. * Copyright (C) 2015 Tom Zanussi <[email protected]>
  6. *
  7. * tracing_map implementation inspired by lock-free map algorithms
  8. * originated by Dr. Cliff Click:
  9. *
  10. * http://www.azulsystems.com/blog/cliff/2007-03-26-non-blocking-hashtable
  11. * http://www.azulsystems.com/events/javaone_2007/2007_LockFreeHash.pdf
  12. */
  13. #include <linux/vmalloc.h>
  14. #include <linux/jhash.h>
  15. #include <linux/slab.h>
  16. #include <linux/sort.h>
  17. #include <linux/kmemleak.h>
  18. #include "tracing_map.h"
  19. #include "trace.h"
  20. /*
  21. * NOTE: For a detailed description of the data structures used by
  22. * these functions (such as tracing_map_elt) please see the overview
  23. * of tracing_map data structures at the beginning of tracing_map.h.
  24. */
  25. /**
  26. * tracing_map_update_sum - Add a value to a tracing_map_elt's sum field
  27. * @elt: The tracing_map_elt
  28. * @i: The index of the given sum associated with the tracing_map_elt
  29. * @n: The value to add to the sum
  30. *
  31. * Add n to sum i associated with the specified tracing_map_elt
  32. * instance. The index i is the index returned by the call to
  33. * tracing_map_add_sum_field() when the tracing map was set up.
  34. */
  35. void tracing_map_update_sum(struct tracing_map_elt *elt, unsigned int i, u64 n)
  36. {
  37. atomic64_add(n, &elt->fields[i].sum);
  38. }
  39. /**
  40. * tracing_map_read_sum - Return the value of a tracing_map_elt's sum field
  41. * @elt: The tracing_map_elt
  42. * @i: The index of the given sum associated with the tracing_map_elt
  43. *
  44. * Retrieve the value of the sum i associated with the specified
  45. * tracing_map_elt instance. The index i is the index returned by the
  46. * call to tracing_map_add_sum_field() when the tracing map was set
  47. * up.
  48. *
  49. * Return: The sum associated with field i for elt.
  50. */
  51. u64 tracing_map_read_sum(struct tracing_map_elt *elt, unsigned int i)
  52. {
  53. return (u64)atomic64_read(&elt->fields[i].sum);
  54. }
  55. /**
  56. * tracing_map_set_var - Assign a tracing_map_elt's variable field
  57. * @elt: The tracing_map_elt
  58. * @i: The index of the given variable associated with the tracing_map_elt
  59. * @n: The value to assign
  60. *
  61. * Assign n to variable i associated with the specified tracing_map_elt
  62. * instance. The index i is the index returned by the call to
  63. * tracing_map_add_var() when the tracing map was set up.
  64. */
  65. void tracing_map_set_var(struct tracing_map_elt *elt, unsigned int i, u64 n)
  66. {
  67. atomic64_set(&elt->vars[i], n);
  68. elt->var_set[i] = true;
  69. }
  70. /**
  71. * tracing_map_var_set - Return whether or not a variable has been set
  72. * @elt: The tracing_map_elt
  73. * @i: The index of the given variable associated with the tracing_map_elt
  74. *
  75. * Return true if the variable has been set, false otherwise. The
  76. * index i is the index returned by the call to tracing_map_add_var()
  77. * when the tracing map was set up.
  78. */
  79. bool tracing_map_var_set(struct tracing_map_elt *elt, unsigned int i)
  80. {
  81. return elt->var_set[i];
  82. }
  83. /**
  84. * tracing_map_read_var - Return the value of a tracing_map_elt's variable field
  85. * @elt: The tracing_map_elt
  86. * @i: The index of the given variable associated with the tracing_map_elt
  87. *
  88. * Retrieve the value of the variable i associated with the specified
  89. * tracing_map_elt instance. The index i is the index returned by the
  90. * call to tracing_map_add_var() when the tracing map was set
  91. * up.
  92. *
  93. * Return: The variable value associated with field i for elt.
  94. */
  95. u64 tracing_map_read_var(struct tracing_map_elt *elt, unsigned int i)
  96. {
  97. return (u64)atomic64_read(&elt->vars[i]);
  98. }
  99. /**
  100. * tracing_map_read_var_once - Return and reset a tracing_map_elt's variable field
  101. * @elt: The tracing_map_elt
  102. * @i: The index of the given variable associated with the tracing_map_elt
  103. *
  104. * Retrieve the value of the variable i associated with the specified
  105. * tracing_map_elt instance, and reset the variable to the 'not set'
  106. * state. The index i is the index returned by the call to
  107. * tracing_map_add_var() when the tracing map was set up. The reset
  108. * essentially makes the variable a read-once variable if it's only
  109. * accessed using this function.
  110. *
  111. * Return: The variable value associated with field i for elt.
  112. */
  113. u64 tracing_map_read_var_once(struct tracing_map_elt *elt, unsigned int i)
  114. {
  115. elt->var_set[i] = false;
  116. return (u64)atomic64_read(&elt->vars[i]);
  117. }
  118. int tracing_map_cmp_string(void *val_a, void *val_b)
  119. {
  120. char *a = val_a;
  121. char *b = val_b;
  122. return strcmp(a, b);
  123. }
  124. int tracing_map_cmp_none(void *val_a, void *val_b)
  125. {
  126. return 0;
  127. }
  128. static int tracing_map_cmp_atomic64(void *val_a, void *val_b)
  129. {
  130. u64 a = atomic64_read((atomic64_t *)val_a);
  131. u64 b = atomic64_read((atomic64_t *)val_b);
  132. return (a > b) ? 1 : ((a < b) ? -1 : 0);
  133. }
  134. #define DEFINE_TRACING_MAP_CMP_FN(type) \
  135. static int tracing_map_cmp_##type(void *val_a, void *val_b) \
  136. { \
  137. type a = (type)(*(u64 *)val_a); \
  138. type b = (type)(*(u64 *)val_b); \
  139. \
  140. return (a > b) ? 1 : ((a < b) ? -1 : 0); \
  141. }
  142. DEFINE_TRACING_MAP_CMP_FN(s64);
  143. DEFINE_TRACING_MAP_CMP_FN(u64);
  144. DEFINE_TRACING_MAP_CMP_FN(s32);
  145. DEFINE_TRACING_MAP_CMP_FN(u32);
  146. DEFINE_TRACING_MAP_CMP_FN(s16);
  147. DEFINE_TRACING_MAP_CMP_FN(u16);
  148. DEFINE_TRACING_MAP_CMP_FN(s8);
  149. DEFINE_TRACING_MAP_CMP_FN(u8);
  150. tracing_map_cmp_fn_t tracing_map_cmp_num(int field_size,
  151. int field_is_signed)
  152. {
  153. tracing_map_cmp_fn_t fn = tracing_map_cmp_none;
  154. switch (field_size) {
  155. case 8:
  156. if (field_is_signed)
  157. fn = tracing_map_cmp_s64;
  158. else
  159. fn = tracing_map_cmp_u64;
  160. break;
  161. case 4:
  162. if (field_is_signed)
  163. fn = tracing_map_cmp_s32;
  164. else
  165. fn = tracing_map_cmp_u32;
  166. break;
  167. case 2:
  168. if (field_is_signed)
  169. fn = tracing_map_cmp_s16;
  170. else
  171. fn = tracing_map_cmp_u16;
  172. break;
  173. case 1:
  174. if (field_is_signed)
  175. fn = tracing_map_cmp_s8;
  176. else
  177. fn = tracing_map_cmp_u8;
  178. break;
  179. }
  180. return fn;
  181. }
  182. static int tracing_map_add_field(struct tracing_map *map,
  183. tracing_map_cmp_fn_t cmp_fn)
  184. {
  185. int ret = -EINVAL;
  186. if (map->n_fields < TRACING_MAP_FIELDS_MAX) {
  187. ret = map->n_fields;
  188. map->fields[map->n_fields++].cmp_fn = cmp_fn;
  189. }
  190. return ret;
  191. }
  192. /**
  193. * tracing_map_add_sum_field - Add a field describing a tracing_map sum
  194. * @map: The tracing_map
  195. *
  196. * Add a sum field to the key and return the index identifying it in
  197. * the map and associated tracing_map_elts. This is the index used
  198. * for instance to update a sum for a particular tracing_map_elt using
  199. * tracing_map_update_sum() or reading it via tracing_map_read_sum().
  200. *
  201. * Return: The index identifying the field in the map and associated
  202. * tracing_map_elts, or -EINVAL on error.
  203. */
  204. int tracing_map_add_sum_field(struct tracing_map *map)
  205. {
  206. return tracing_map_add_field(map, tracing_map_cmp_atomic64);
  207. }
  208. /**
  209. * tracing_map_add_var - Add a field describing a tracing_map var
  210. * @map: The tracing_map
  211. *
  212. * Add a var to the map and return the index identifying it in the map
  213. * and associated tracing_map_elts. This is the index used for
  214. * instance to update a var for a particular tracing_map_elt using
  215. * tracing_map_update_var() or reading it via tracing_map_read_var().
  216. *
  217. * Return: The index identifying the var in the map and associated
  218. * tracing_map_elts, or -EINVAL on error.
  219. */
  220. int tracing_map_add_var(struct tracing_map *map)
  221. {
  222. int ret = -EINVAL;
  223. if (map->n_vars < TRACING_MAP_VARS_MAX)
  224. ret = map->n_vars++;
  225. return ret;
  226. }
  227. /**
  228. * tracing_map_add_key_field - Add a field describing a tracing_map key
  229. * @map: The tracing_map
  230. * @offset: The offset within the key
  231. * @cmp_fn: The comparison function that will be used to sort on the key
  232. *
  233. * Let the map know there is a key and that if it's used as a sort key
  234. * to use cmp_fn.
  235. *
  236. * A key can be a subset of a compound key; for that purpose, the
  237. * offset param is used to describe where within the compound key
  238. * the key referenced by this key field resides.
  239. *
  240. * Return: The index identifying the field in the map and associated
  241. * tracing_map_elts, or -EINVAL on error.
  242. */
  243. int tracing_map_add_key_field(struct tracing_map *map,
  244. unsigned int offset,
  245. tracing_map_cmp_fn_t cmp_fn)
  246. {
  247. int idx = tracing_map_add_field(map, cmp_fn);
  248. if (idx < 0)
  249. return idx;
  250. map->fields[idx].offset = offset;
  251. map->key_idx[map->n_keys++] = idx;
  252. return idx;
  253. }
  254. static void tracing_map_array_clear(struct tracing_map_array *a)
  255. {
  256. unsigned int i;
  257. if (!a->pages)
  258. return;
  259. for (i = 0; i < a->n_pages; i++)
  260. memset(a->pages[i], 0, PAGE_SIZE);
  261. }
  262. static void tracing_map_array_free(struct tracing_map_array *a)
  263. {
  264. unsigned int i;
  265. if (!a)
  266. return;
  267. if (!a->pages)
  268. goto free;
  269. for (i = 0; i < a->n_pages; i++) {
  270. if (!a->pages[i])
  271. break;
  272. kmemleak_free(a->pages[i]);
  273. free_page((unsigned long)a->pages[i]);
  274. }
  275. kfree(a->pages);
  276. free:
  277. kfree(a);
  278. }
  279. static struct tracing_map_array *tracing_map_array_alloc(unsigned int n_elts,
  280. unsigned int entry_size)
  281. {
  282. struct tracing_map_array *a;
  283. unsigned int i;
  284. a = kzalloc(sizeof(*a), GFP_KERNEL);
  285. if (!a)
  286. return NULL;
  287. a->entry_size_shift = fls(roundup_pow_of_two(entry_size) - 1);
  288. a->entries_per_page = PAGE_SIZE / (1 << a->entry_size_shift);
  289. a->n_pages = n_elts / a->entries_per_page;
  290. if (!a->n_pages)
  291. a->n_pages = 1;
  292. a->entry_shift = fls(a->entries_per_page) - 1;
  293. a->entry_mask = (1 << a->entry_shift) - 1;
  294. a->pages = kcalloc(a->n_pages, sizeof(void *), GFP_KERNEL);
  295. if (!a->pages)
  296. goto free;
  297. for (i = 0; i < a->n_pages; i++) {
  298. a->pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
  299. if (!a->pages[i])
  300. goto free;
  301. kmemleak_alloc(a->pages[i], PAGE_SIZE, 1, GFP_KERNEL);
  302. }
  303. out:
  304. return a;
  305. free:
  306. tracing_map_array_free(a);
  307. a = NULL;
  308. goto out;
  309. }
  310. static void tracing_map_elt_clear(struct tracing_map_elt *elt)
  311. {
  312. unsigned i;
  313. for (i = 0; i < elt->map->n_fields; i++)
  314. if (elt->fields[i].cmp_fn == tracing_map_cmp_atomic64)
  315. atomic64_set(&elt->fields[i].sum, 0);
  316. for (i = 0; i < elt->map->n_vars; i++) {
  317. atomic64_set(&elt->vars[i], 0);
  318. elt->var_set[i] = false;
  319. }
  320. if (elt->map->ops && elt->map->ops->elt_clear)
  321. elt->map->ops->elt_clear(elt);
  322. }
  323. static void tracing_map_elt_init_fields(struct tracing_map_elt *elt)
  324. {
  325. unsigned int i;
  326. tracing_map_elt_clear(elt);
  327. for (i = 0; i < elt->map->n_fields; i++) {
  328. elt->fields[i].cmp_fn = elt->map->fields[i].cmp_fn;
  329. if (elt->fields[i].cmp_fn != tracing_map_cmp_atomic64)
  330. elt->fields[i].offset = elt->map->fields[i].offset;
  331. }
  332. }
  333. static void tracing_map_elt_free(struct tracing_map_elt *elt)
  334. {
  335. if (!elt)
  336. return;
  337. if (elt->map->ops && elt->map->ops->elt_free)
  338. elt->map->ops->elt_free(elt);
  339. kfree(elt->fields);
  340. kfree(elt->vars);
  341. kfree(elt->var_set);
  342. kfree(elt->key);
  343. kfree(elt);
  344. }
  345. static struct tracing_map_elt *tracing_map_elt_alloc(struct tracing_map *map)
  346. {
  347. struct tracing_map_elt *elt;
  348. int err = 0;
  349. elt = kzalloc(sizeof(*elt), GFP_KERNEL);
  350. if (!elt)
  351. return ERR_PTR(-ENOMEM);
  352. elt->map = map;
  353. elt->key = kzalloc(map->key_size, GFP_KERNEL);
  354. if (!elt->key) {
  355. err = -ENOMEM;
  356. goto free;
  357. }
  358. elt->fields = kcalloc(map->n_fields, sizeof(*elt->fields), GFP_KERNEL);
  359. if (!elt->fields) {
  360. err = -ENOMEM;
  361. goto free;
  362. }
  363. elt->vars = kcalloc(map->n_vars, sizeof(*elt->vars), GFP_KERNEL);
  364. if (!elt->vars) {
  365. err = -ENOMEM;
  366. goto free;
  367. }
  368. elt->var_set = kcalloc(map->n_vars, sizeof(*elt->var_set), GFP_KERNEL);
  369. if (!elt->var_set) {
  370. err = -ENOMEM;
  371. goto free;
  372. }
  373. tracing_map_elt_init_fields(elt);
  374. if (map->ops && map->ops->elt_alloc) {
  375. err = map->ops->elt_alloc(elt);
  376. if (err)
  377. goto free;
  378. }
  379. return elt;
  380. free:
  381. tracing_map_elt_free(elt);
  382. return ERR_PTR(err);
  383. }
  384. static struct tracing_map_elt *get_free_elt(struct tracing_map *map)
  385. {
  386. struct tracing_map_elt *elt = NULL;
  387. int idx;
  388. idx = atomic_inc_return(&map->next_elt);
  389. if (idx < map->max_elts) {
  390. elt = *(TRACING_MAP_ELT(map->elts, idx));
  391. if (map->ops && map->ops->elt_init)
  392. map->ops->elt_init(elt);
  393. }
  394. return elt;
  395. }
  396. static void tracing_map_free_elts(struct tracing_map *map)
  397. {
  398. unsigned int i;
  399. if (!map->elts)
  400. return;
  401. for (i = 0; i < map->max_elts; i++) {
  402. tracing_map_elt_free(*(TRACING_MAP_ELT(map->elts, i)));
  403. *(TRACING_MAP_ELT(map->elts, i)) = NULL;
  404. }
  405. tracing_map_array_free(map->elts);
  406. map->elts = NULL;
  407. }
  408. static int tracing_map_alloc_elts(struct tracing_map *map)
  409. {
  410. unsigned int i;
  411. map->elts = tracing_map_array_alloc(map->max_elts,
  412. sizeof(struct tracing_map_elt *));
  413. if (!map->elts)
  414. return -ENOMEM;
  415. for (i = 0; i < map->max_elts; i++) {
  416. *(TRACING_MAP_ELT(map->elts, i)) = tracing_map_elt_alloc(map);
  417. if (IS_ERR(*(TRACING_MAP_ELT(map->elts, i)))) {
  418. *(TRACING_MAP_ELT(map->elts, i)) = NULL;
  419. tracing_map_free_elts(map);
  420. return -ENOMEM;
  421. }
  422. }
  423. return 0;
  424. }
  425. static inline bool keys_match(void *key, void *test_key, unsigned key_size)
  426. {
  427. bool match = true;
  428. if (memcmp(key, test_key, key_size))
  429. match = false;
  430. return match;
  431. }
  432. static inline struct tracing_map_elt *
  433. __tracing_map_insert(struct tracing_map *map, void *key, bool lookup_only)
  434. {
  435. u32 idx, key_hash, test_key;
  436. int dup_try = 0;
  437. struct tracing_map_entry *entry;
  438. struct tracing_map_elt *val;
  439. key_hash = jhash(key, map->key_size, 0);
  440. if (key_hash == 0)
  441. key_hash = 1;
  442. idx = key_hash >> (32 - (map->map_bits + 1));
  443. while (1) {
  444. idx &= (map->map_size - 1);
  445. entry = TRACING_MAP_ENTRY(map->map, idx);
  446. test_key = entry->key;
  447. if (test_key && test_key == key_hash) {
  448. val = READ_ONCE(entry->val);
  449. if (val &&
  450. keys_match(key, val->key, map->key_size)) {
  451. if (!lookup_only)
  452. atomic64_inc(&map->hits);
  453. return val;
  454. } else if (unlikely(!val)) {
  455. /*
  456. * The key is present. But, val (pointer to elt
  457. * struct) is still NULL. which means some other
  458. * thread is in the process of inserting an
  459. * element.
  460. *
  461. * On top of that, it's key_hash is same as the
  462. * one being inserted right now. So, it's
  463. * possible that the element has the same
  464. * key as well.
  465. */
  466. dup_try++;
  467. if (dup_try > map->map_size) {
  468. atomic64_inc(&map->drops);
  469. break;
  470. }
  471. continue;
  472. }
  473. }
  474. if (!test_key) {
  475. if (lookup_only)
  476. break;
  477. if (!cmpxchg(&entry->key, 0, key_hash)) {
  478. struct tracing_map_elt *elt;
  479. elt = get_free_elt(map);
  480. if (!elt) {
  481. atomic64_inc(&map->drops);
  482. entry->key = 0;
  483. break;
  484. }
  485. memcpy(elt->key, key, map->key_size);
  486. entry->val = elt;
  487. atomic64_inc(&map->hits);
  488. return entry->val;
  489. } else {
  490. /*
  491. * cmpxchg() failed. Loop around once
  492. * more to check what key was inserted.
  493. */
  494. dup_try++;
  495. continue;
  496. }
  497. }
  498. idx++;
  499. }
  500. return NULL;
  501. }
  502. /**
  503. * tracing_map_insert - Insert key and/or retrieve val from a tracing_map
  504. * @map: The tracing_map to insert into
  505. * @key: The key to insert
  506. *
  507. * Inserts a key into a tracing_map and creates and returns a new
  508. * tracing_map_elt for it, or if the key has already been inserted by
  509. * a previous call, returns the tracing_map_elt already associated
  510. * with it. When the map was created, the number of elements to be
  511. * allocated for the map was specified (internally maintained as
  512. * 'max_elts' in struct tracing_map), and that number of
  513. * tracing_map_elts was created by tracing_map_init(). This is the
  514. * pre-allocated pool of tracing_map_elts that tracing_map_insert()
  515. * will allocate from when adding new keys. Once that pool is
  516. * exhausted, tracing_map_insert() is useless and will return NULL to
  517. * signal that state. There are two user-visible tracing_map
  518. * variables, 'hits' and 'drops', which are updated by this function.
  519. * Every time an element is either successfully inserted or retrieved,
  520. * the 'hits' value is incremented. Every time an element insertion
  521. * fails, the 'drops' value is incremented.
  522. *
  523. * This is a lock-free tracing map insertion function implementing a
  524. * modified form of Cliff Click's basic insertion algorithm. It
  525. * requires the table size be a power of two. To prevent any
  526. * possibility of an infinite loop we always make the internal table
  527. * size double the size of the requested table size (max_elts * 2).
  528. * Likewise, we never reuse a slot or resize or delete elements - when
  529. * we've reached max_elts entries, we simply return NULL once we've
  530. * run out of entries. Readers can at any point in time traverse the
  531. * tracing map and safely access the key/val pairs.
  532. *
  533. * Return: the tracing_map_elt pointer val associated with the key.
  534. * If this was a newly inserted key, the val will be a newly allocated
  535. * and associated tracing_map_elt pointer val. If the key wasn't
  536. * found and the pool of tracing_map_elts has been exhausted, NULL is
  537. * returned and no further insertions will succeed.
  538. */
  539. struct tracing_map_elt *tracing_map_insert(struct tracing_map *map, void *key)
  540. {
  541. return __tracing_map_insert(map, key, false);
  542. }
  543. /**
  544. * tracing_map_lookup - Retrieve val from a tracing_map
  545. * @map: The tracing_map to perform the lookup on
  546. * @key: The key to look up
  547. *
  548. * Looks up key in tracing_map and if found returns the matching
  549. * tracing_map_elt. This is a lock-free lookup; see
  550. * tracing_map_insert() for details on tracing_map and how it works.
  551. * Every time an element is retrieved, the 'hits' value is
  552. * incremented. There is one user-visible tracing_map variable,
  553. * 'hits', which is updated by this function. Every time an element
  554. * is successfully retrieved, the 'hits' value is incremented. The
  555. * 'drops' value is never updated by this function.
  556. *
  557. * Return: the tracing_map_elt pointer val associated with the key.
  558. * If the key wasn't found, NULL is returned.
  559. */
  560. struct tracing_map_elt *tracing_map_lookup(struct tracing_map *map, void *key)
  561. {
  562. return __tracing_map_insert(map, key, true);
  563. }
  564. /**
  565. * tracing_map_destroy - Destroy a tracing_map
  566. * @map: The tracing_map to destroy
  567. *
  568. * Frees a tracing_map along with its associated array of
  569. * tracing_map_elts.
  570. *
  571. * Callers should make sure there are no readers or writers actively
  572. * reading or inserting into the map before calling this.
  573. */
  574. void tracing_map_destroy(struct tracing_map *map)
  575. {
  576. if (!map)
  577. return;
  578. tracing_map_free_elts(map);
  579. tracing_map_array_free(map->map);
  580. kfree(map);
  581. }
  582. /**
  583. * tracing_map_clear - Clear a tracing_map
  584. * @map: The tracing_map to clear
  585. *
  586. * Resets the tracing map to a cleared or initial state. The
  587. * tracing_map_elts are all cleared, and the array of struct
  588. * tracing_map_entry is reset to an initialized state.
  589. *
  590. * Callers should make sure there are no writers actively inserting
  591. * into the map before calling this.
  592. */
  593. void tracing_map_clear(struct tracing_map *map)
  594. {
  595. unsigned int i;
  596. atomic_set(&map->next_elt, -1);
  597. atomic64_set(&map->hits, 0);
  598. atomic64_set(&map->drops, 0);
  599. tracing_map_array_clear(map->map);
  600. for (i = 0; i < map->max_elts; i++)
  601. tracing_map_elt_clear(*(TRACING_MAP_ELT(map->elts, i)));
  602. }
  603. static void set_sort_key(struct tracing_map *map,
  604. struct tracing_map_sort_key *sort_key)
  605. {
  606. map->sort_key = *sort_key;
  607. }
  608. /**
  609. * tracing_map_create - Create a lock-free map and element pool
  610. * @map_bits: The size of the map (2 ** map_bits)
  611. * @key_size: The size of the key for the map in bytes
  612. * @ops: Optional client-defined tracing_map_ops instance
  613. * @private_data: Client data associated with the map
  614. *
  615. * Creates and sets up a map to contain 2 ** map_bits number of
  616. * elements (internally maintained as 'max_elts' in struct
  617. * tracing_map). Before using, map fields should be added to the map
  618. * with tracing_map_add_sum_field() and tracing_map_add_key_field().
  619. * tracing_map_init() should then be called to allocate the array of
  620. * tracing_map_elts, in order to avoid allocating anything in the map
  621. * insertion path. The user-specified map size reflects the maximum
  622. * number of elements that can be contained in the table requested by
  623. * the user - internally we double that in order to keep the table
  624. * sparse and keep collisions manageable.
  625. *
  626. * A tracing_map is a special-purpose map designed to aggregate or
  627. * 'sum' one or more values associated with a specific object of type
  628. * tracing_map_elt, which is attached by the map to a given key.
  629. *
  630. * tracing_map_create() sets up the map itself, and provides
  631. * operations for inserting tracing_map_elts, but doesn't allocate the
  632. * tracing_map_elts themselves, or provide a means for describing the
  633. * keys or sums associated with the tracing_map_elts. All
  634. * tracing_map_elts for a given map have the same set of sums and
  635. * keys, which are defined by the client using the functions
  636. * tracing_map_add_key_field() and tracing_map_add_sum_field(). Once
  637. * the fields are defined, the pool of elements allocated for the map
  638. * can be created, which occurs when the client code calls
  639. * tracing_map_init().
  640. *
  641. * When tracing_map_init() returns, tracing_map_elt elements can be
  642. * inserted into the map using tracing_map_insert(). When called,
  643. * tracing_map_insert() grabs a free tracing_map_elt from the pool, or
  644. * finds an existing match in the map and in either case returns it.
  645. * The client can then use tracing_map_update_sum() and
  646. * tracing_map_read_sum() to update or read a given sum field for the
  647. * tracing_map_elt.
  648. *
  649. * The client can at any point retrieve and traverse the current set
  650. * of inserted tracing_map_elts in a tracing_map, via
  651. * tracing_map_sort_entries(). Sorting can be done on any field,
  652. * including keys.
  653. *
  654. * See tracing_map.h for a description of tracing_map_ops.
  655. *
  656. * Return: the tracing_map pointer if successful, ERR_PTR if not.
  657. */
  658. struct tracing_map *tracing_map_create(unsigned int map_bits,
  659. unsigned int key_size,
  660. const struct tracing_map_ops *ops,
  661. void *private_data)
  662. {
  663. struct tracing_map *map;
  664. unsigned int i;
  665. if (map_bits < TRACING_MAP_BITS_MIN ||
  666. map_bits > TRACING_MAP_BITS_MAX)
  667. return ERR_PTR(-EINVAL);
  668. map = kzalloc(sizeof(*map), GFP_KERNEL);
  669. if (!map)
  670. return ERR_PTR(-ENOMEM);
  671. map->map_bits = map_bits;
  672. map->max_elts = (1 << map_bits);
  673. atomic_set(&map->next_elt, -1);
  674. map->map_size = (1 << (map_bits + 1));
  675. map->ops = ops;
  676. map->private_data = private_data;
  677. map->map = tracing_map_array_alloc(map->map_size,
  678. sizeof(struct tracing_map_entry));
  679. if (!map->map)
  680. goto free;
  681. map->key_size = key_size;
  682. for (i = 0; i < TRACING_MAP_KEYS_MAX; i++)
  683. map->key_idx[i] = -1;
  684. out:
  685. return map;
  686. free:
  687. tracing_map_destroy(map);
  688. map = ERR_PTR(-ENOMEM);
  689. goto out;
  690. }
  691. /**
  692. * tracing_map_init - Allocate and clear a map's tracing_map_elts
  693. * @map: The tracing_map to initialize
  694. *
  695. * Allocates a clears a pool of tracing_map_elts equal to the
  696. * user-specified size of 2 ** map_bits (internally maintained as
  697. * 'max_elts' in struct tracing_map). Before using, the map fields
  698. * should be added to the map with tracing_map_add_sum_field() and
  699. * tracing_map_add_key_field(). tracing_map_init() should then be
  700. * called to allocate the array of tracing_map_elts, in order to avoid
  701. * allocating anything in the map insertion path. The user-specified
  702. * map size reflects the max number of elements requested by the user
  703. * - internally we double that in order to keep the table sparse and
  704. * keep collisions manageable.
  705. *
  706. * See tracing_map.h for a description of tracing_map_ops.
  707. *
  708. * Return: the tracing_map pointer if successful, ERR_PTR if not.
  709. */
  710. int tracing_map_init(struct tracing_map *map)
  711. {
  712. int err;
  713. if (map->n_fields < 2)
  714. return -EINVAL; /* need at least 1 key and 1 val */
  715. err = tracing_map_alloc_elts(map);
  716. if (err)
  717. return err;
  718. tracing_map_clear(map);
  719. return err;
  720. }
  721. static int cmp_entries_dup(const void *A, const void *B)
  722. {
  723. const struct tracing_map_sort_entry *a, *b;
  724. int ret = 0;
  725. a = *(const struct tracing_map_sort_entry **)A;
  726. b = *(const struct tracing_map_sort_entry **)B;
  727. if (memcmp(a->key, b->key, a->elt->map->key_size))
  728. ret = 1;
  729. return ret;
  730. }
  731. static int cmp_entries_sum(const void *A, const void *B)
  732. {
  733. const struct tracing_map_elt *elt_a, *elt_b;
  734. const struct tracing_map_sort_entry *a, *b;
  735. struct tracing_map_sort_key *sort_key;
  736. struct tracing_map_field *field;
  737. tracing_map_cmp_fn_t cmp_fn;
  738. void *val_a, *val_b;
  739. int ret = 0;
  740. a = *(const struct tracing_map_sort_entry **)A;
  741. b = *(const struct tracing_map_sort_entry **)B;
  742. elt_a = a->elt;
  743. elt_b = b->elt;
  744. sort_key = &elt_a->map->sort_key;
  745. field = &elt_a->fields[sort_key->field_idx];
  746. cmp_fn = field->cmp_fn;
  747. val_a = &elt_a->fields[sort_key->field_idx].sum;
  748. val_b = &elt_b->fields[sort_key->field_idx].sum;
  749. ret = cmp_fn(val_a, val_b);
  750. if (sort_key->descending)
  751. ret = -ret;
  752. return ret;
  753. }
  754. static int cmp_entries_key(const void *A, const void *B)
  755. {
  756. const struct tracing_map_elt *elt_a, *elt_b;
  757. const struct tracing_map_sort_entry *a, *b;
  758. struct tracing_map_sort_key *sort_key;
  759. struct tracing_map_field *field;
  760. tracing_map_cmp_fn_t cmp_fn;
  761. void *val_a, *val_b;
  762. int ret = 0;
  763. a = *(const struct tracing_map_sort_entry **)A;
  764. b = *(const struct tracing_map_sort_entry **)B;
  765. elt_a = a->elt;
  766. elt_b = b->elt;
  767. sort_key = &elt_a->map->sort_key;
  768. field = &elt_a->fields[sort_key->field_idx];
  769. cmp_fn = field->cmp_fn;
  770. val_a = elt_a->key + field->offset;
  771. val_b = elt_b->key + field->offset;
  772. ret = cmp_fn(val_a, val_b);
  773. if (sort_key->descending)
  774. ret = -ret;
  775. return ret;
  776. }
  777. static void destroy_sort_entry(struct tracing_map_sort_entry *entry)
  778. {
  779. if (!entry)
  780. return;
  781. if (entry->elt_copied)
  782. tracing_map_elt_free(entry->elt);
  783. kfree(entry);
  784. }
  785. /**
  786. * tracing_map_destroy_sort_entries - Destroy an array of sort entries
  787. * @entries: The entries to destroy
  788. * @n_entries: The number of entries in the array
  789. *
  790. * Destroy the elements returned by a tracing_map_sort_entries() call.
  791. */
  792. void tracing_map_destroy_sort_entries(struct tracing_map_sort_entry **entries,
  793. unsigned int n_entries)
  794. {
  795. unsigned int i;
  796. for (i = 0; i < n_entries; i++)
  797. destroy_sort_entry(entries[i]);
  798. vfree(entries);
  799. }
  800. static struct tracing_map_sort_entry *
  801. create_sort_entry(void *key, struct tracing_map_elt *elt)
  802. {
  803. struct tracing_map_sort_entry *sort_entry;
  804. sort_entry = kzalloc(sizeof(*sort_entry), GFP_KERNEL);
  805. if (!sort_entry)
  806. return NULL;
  807. sort_entry->key = key;
  808. sort_entry->elt = elt;
  809. return sort_entry;
  810. }
  811. static void detect_dups(struct tracing_map_sort_entry **sort_entries,
  812. int n_entries, unsigned int key_size)
  813. {
  814. unsigned int total_dups = 0;
  815. int i;
  816. void *key;
  817. if (n_entries < 2)
  818. return;
  819. sort(sort_entries, n_entries, sizeof(struct tracing_map_sort_entry *),
  820. (int (*)(const void *, const void *))cmp_entries_dup, NULL);
  821. key = sort_entries[0]->key;
  822. for (i = 1; i < n_entries; i++) {
  823. if (!memcmp(sort_entries[i]->key, key, key_size)) {
  824. total_dups++;
  825. continue;
  826. }
  827. key = sort_entries[i]->key;
  828. }
  829. WARN_ONCE(total_dups > 0,
  830. "Duplicates detected: %d\n", total_dups);
  831. }
  832. static bool is_key(struct tracing_map *map, unsigned int field_idx)
  833. {
  834. unsigned int i;
  835. for (i = 0; i < map->n_keys; i++)
  836. if (map->key_idx[i] == field_idx)
  837. return true;
  838. return false;
  839. }
  840. static void sort_secondary(struct tracing_map *map,
  841. const struct tracing_map_sort_entry **entries,
  842. unsigned int n_entries,
  843. struct tracing_map_sort_key *primary_key,
  844. struct tracing_map_sort_key *secondary_key)
  845. {
  846. int (*primary_fn)(const void *, const void *);
  847. int (*secondary_fn)(const void *, const void *);
  848. unsigned i, start = 0, n_sub = 1;
  849. if (is_key(map, primary_key->field_idx))
  850. primary_fn = cmp_entries_key;
  851. else
  852. primary_fn = cmp_entries_sum;
  853. if (is_key(map, secondary_key->field_idx))
  854. secondary_fn = cmp_entries_key;
  855. else
  856. secondary_fn = cmp_entries_sum;
  857. for (i = 0; i < n_entries - 1; i++) {
  858. const struct tracing_map_sort_entry **a = &entries[i];
  859. const struct tracing_map_sort_entry **b = &entries[i + 1];
  860. if (primary_fn(a, b) == 0) {
  861. n_sub++;
  862. if (i < n_entries - 2)
  863. continue;
  864. }
  865. if (n_sub < 2) {
  866. start = i + 1;
  867. n_sub = 1;
  868. continue;
  869. }
  870. set_sort_key(map, secondary_key);
  871. sort(&entries[start], n_sub,
  872. sizeof(struct tracing_map_sort_entry *),
  873. (int (*)(const void *, const void *))secondary_fn, NULL);
  874. set_sort_key(map, primary_key);
  875. start = i + 1;
  876. n_sub = 1;
  877. }
  878. }
  879. /**
  880. * tracing_map_sort_entries - Sort the current set of tracing_map_elts in a map
  881. * @map: The tracing_map
  882. * @sort_keys: The sort key to use for sorting
  883. * @n_sort_keys: hitcount, always have at least one
  884. * @sort_entries: outval: pointer to allocated and sorted array of entries
  885. *
  886. * tracing_map_sort_entries() sorts the current set of entries in the
  887. * map and returns the list of tracing_map_sort_entries containing
  888. * them to the client in the sort_entries param. The client can
  889. * access the struct tracing_map_elt element of interest directly as
  890. * the 'elt' field of a returned struct tracing_map_sort_entry object.
  891. *
  892. * The sort_key has only two fields: idx and descending. 'idx' refers
  893. * to the index of the field added via tracing_map_add_sum_field() or
  894. * tracing_map_add_key_field() when the tracing_map was initialized.
  895. * 'descending' is a flag that if set reverses the sort order, which
  896. * by default is ascending.
  897. *
  898. * The client should not hold on to the returned array but should use
  899. * it and call tracing_map_destroy_sort_entries() when done.
  900. *
  901. * Return: the number of sort_entries in the struct tracing_map_sort_entry
  902. * array, negative on error
  903. */
  904. int tracing_map_sort_entries(struct tracing_map *map,
  905. struct tracing_map_sort_key *sort_keys,
  906. unsigned int n_sort_keys,
  907. struct tracing_map_sort_entry ***sort_entries)
  908. {
  909. int (*cmp_entries_fn)(const void *, const void *);
  910. struct tracing_map_sort_entry *sort_entry, **entries;
  911. int i, n_entries, ret;
  912. entries = vmalloc(array_size(sizeof(sort_entry), map->max_elts));
  913. if (!entries)
  914. return -ENOMEM;
  915. for (i = 0, n_entries = 0; i < map->map_size; i++) {
  916. struct tracing_map_entry *entry;
  917. entry = TRACING_MAP_ENTRY(map->map, i);
  918. if (!entry->key || !entry->val)
  919. continue;
  920. entries[n_entries] = create_sort_entry(entry->val->key,
  921. entry->val);
  922. if (!entries[n_entries++]) {
  923. ret = -ENOMEM;
  924. goto free;
  925. }
  926. }
  927. if (n_entries == 0) {
  928. ret = 0;
  929. goto free;
  930. }
  931. if (n_entries == 1) {
  932. *sort_entries = entries;
  933. return 1;
  934. }
  935. detect_dups(entries, n_entries, map->key_size);
  936. if (is_key(map, sort_keys[0].field_idx))
  937. cmp_entries_fn = cmp_entries_key;
  938. else
  939. cmp_entries_fn = cmp_entries_sum;
  940. set_sort_key(map, &sort_keys[0]);
  941. sort(entries, n_entries, sizeof(struct tracing_map_sort_entry *),
  942. (int (*)(const void *, const void *))cmp_entries_fn, NULL);
  943. if (n_sort_keys > 1)
  944. sort_secondary(map,
  945. (const struct tracing_map_sort_entry **)entries,
  946. n_entries,
  947. &sort_keys[0],
  948. &sort_keys[1]);
  949. *sort_entries = entries;
  950. return n_entries;
  951. free:
  952. tracing_map_destroy_sort_entries(entries, n_entries);
  953. return ret;
  954. }