tracing_map.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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. }