tracing_map.c 30 KB

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