tracing_map.c 28 KB

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