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. atomic64_inc(&map->hits);
  356. return entry->val;
  357. }
  358. if (!test_key) {
  359. if (lookup_only)
  360. break;
  361. if (!cmpxchg(&entry->key, 0, key_hash)) {
  362. struct tracing_map_elt *elt;
  363. elt = get_free_elt(map);
  364. if (!elt) {
  365. atomic64_inc(&map->drops);
  366. entry->key = 0;
  367. break;
  368. }
  369. memcpy(elt->key, key, map->key_size);
  370. entry->val = elt;
  371. atomic64_inc(&map->hits);
  372. return entry->val;
  373. }
  374. }
  375. idx++;
  376. }
  377. return NULL;
  378. }
  379. /**
  380. * tracing_map_insert - Insert key and/or retrieve val from a tracing_map
  381. * @map: The tracing_map to insert into
  382. * @key: The key to insert
  383. *
  384. * Inserts a key into a tracing_map and creates and returns a new
  385. * tracing_map_elt for it, or if the key has already been inserted by
  386. * a previous call, returns the tracing_map_elt already associated
  387. * with it. When the map was created, the number of elements to be
  388. * allocated for the map was specified (internally maintained as
  389. * 'max_elts' in struct tracing_map), and that number of
  390. * tracing_map_elts was created by tracing_map_init(). This is the
  391. * pre-allocated pool of tracing_map_elts that tracing_map_insert()
  392. * will allocate from when adding new keys. Once that pool is
  393. * exhausted, tracing_map_insert() is useless and will return NULL to
  394. * signal that state. There are two user-visible tracing_map
  395. * variables, 'hits' and 'drops', which are updated by this function.
  396. * Every time an element is either successfully inserted or retrieved,
  397. * the 'hits' value is incrememented. Every time an element insertion
  398. * fails, the 'drops' value is incremented.
  399. *
  400. * This is a lock-free tracing map insertion function implementing a
  401. * modified form of Cliff Click's basic insertion algorithm. It
  402. * requires the table size be a power of two. To prevent any
  403. * possibility of an infinite loop we always make the internal table
  404. * size double the size of the requested table size (max_elts * 2).
  405. * Likewise, we never reuse a slot or resize or delete elements - when
  406. * we've reached max_elts entries, we simply return NULL once we've
  407. * run out of entries. Readers can at any point in time traverse the
  408. * tracing map and safely access the key/val pairs.
  409. *
  410. * Return: the tracing_map_elt pointer val associated with the key.
  411. * If this was a newly inserted key, the val will be a newly allocated
  412. * and associated tracing_map_elt pointer val. If the key wasn't
  413. * found and the pool of tracing_map_elts has been exhausted, NULL is
  414. * returned and no further insertions will succeed.
  415. */
  416. struct tracing_map_elt *tracing_map_insert(struct tracing_map *map, void *key)
  417. {
  418. return __tracing_map_insert(map, key, false);
  419. }
  420. /**
  421. * tracing_map_lookup - Retrieve val from a tracing_map
  422. * @map: The tracing_map to perform the lookup on
  423. * @key: The key to look up
  424. *
  425. * Looks up key in tracing_map and if found returns the matching
  426. * tracing_map_elt. This is a lock-free lookup; see
  427. * tracing_map_insert() for details on tracing_map and how it works.
  428. * Every time an element is retrieved, the 'hits' value is
  429. * incrememented. There is one user-visible tracing_map variable,
  430. * 'hits', which is updated by this function. Every time an element
  431. * is successfully retrieved, the 'hits' value is incrememented. The
  432. * 'drops' value is never updated by this function.
  433. *
  434. * Return: the tracing_map_elt pointer val associated with the key.
  435. * If the key wasn't found, NULL is returned.
  436. */
  437. struct tracing_map_elt *tracing_map_lookup(struct tracing_map *map, void *key)
  438. {
  439. return __tracing_map_insert(map, key, true);
  440. }
  441. /**
  442. * tracing_map_destroy - Destroy a tracing_map
  443. * @map: The tracing_map to destroy
  444. *
  445. * Frees a tracing_map along with its associated array of
  446. * tracing_map_elts.
  447. *
  448. * Callers should make sure there are no readers or writers actively
  449. * reading or inserting into the map before calling this.
  450. */
  451. void tracing_map_destroy(struct tracing_map *map)
  452. {
  453. if (!map)
  454. return;
  455. tracing_map_free_elts(map);
  456. tracing_map_array_free(map->map);
  457. kfree(map);
  458. }
  459. /**
  460. * tracing_map_clear - Clear a tracing_map
  461. * @map: The tracing_map to clear
  462. *
  463. * Resets the tracing map to a cleared or initial state. The
  464. * tracing_map_elts are all cleared, and the array of struct
  465. * tracing_map_entry is reset to an initialized state.
  466. *
  467. * Callers should make sure there are no writers actively inserting
  468. * into the map before calling this.
  469. */
  470. void tracing_map_clear(struct tracing_map *map)
  471. {
  472. unsigned int i;
  473. atomic_set(&map->next_elt, -1);
  474. atomic64_set(&map->hits, 0);
  475. atomic64_set(&map->drops, 0);
  476. tracing_map_array_clear(map->map);
  477. for (i = 0; i < map->max_elts; i++)
  478. tracing_map_elt_clear(*(TRACING_MAP_ELT(map->elts, i)));
  479. }
  480. static void set_sort_key(struct tracing_map *map,
  481. struct tracing_map_sort_key *sort_key)
  482. {
  483. map->sort_key = *sort_key;
  484. }
  485. /**
  486. * tracing_map_create - Create a lock-free map and element pool
  487. * @map_bits: The size of the map (2 ** map_bits)
  488. * @key_size: The size of the key for the map in bytes
  489. * @ops: Optional client-defined tracing_map_ops instance
  490. * @private_data: Client data associated with the map
  491. *
  492. * Creates and sets up a map to contain 2 ** map_bits number of
  493. * elements (internally maintained as 'max_elts' in struct
  494. * tracing_map). Before using, map fields should be added to the map
  495. * with tracing_map_add_sum_field() and tracing_map_add_key_field().
  496. * tracing_map_init() should then be called to allocate the array of
  497. * tracing_map_elts, in order to avoid allocating anything in the map
  498. * insertion path. The user-specified map size reflects the maximum
  499. * number of elements that can be contained in the table requested by
  500. * the user - internally we double that in order to keep the table
  501. * sparse and keep collisions manageable.
  502. *
  503. * A tracing_map is a special-purpose map designed to aggregate or
  504. * 'sum' one or more values associated with a specific object of type
  505. * tracing_map_elt, which is attached by the map to a given key.
  506. *
  507. * tracing_map_create() sets up the map itself, and provides
  508. * operations for inserting tracing_map_elts, but doesn't allocate the
  509. * tracing_map_elts themselves, or provide a means for describing the
  510. * keys or sums associated with the tracing_map_elts. All
  511. * tracing_map_elts for a given map have the same set of sums and
  512. * keys, which are defined by the client using the functions
  513. * tracing_map_add_key_field() and tracing_map_add_sum_field(). Once
  514. * the fields are defined, the pool of elements allocated for the map
  515. * can be created, which occurs when the client code calls
  516. * tracing_map_init().
  517. *
  518. * When tracing_map_init() returns, tracing_map_elt elements can be
  519. * inserted into the map using tracing_map_insert(). When called,
  520. * tracing_map_insert() grabs a free tracing_map_elt from the pool, or
  521. * finds an existing match in the map and in either case returns it.
  522. * The client can then use tracing_map_update_sum() and
  523. * tracing_map_read_sum() to update or read a given sum field for the
  524. * tracing_map_elt.
  525. *
  526. * The client can at any point retrieve and traverse the current set
  527. * of inserted tracing_map_elts in a tracing_map, via
  528. * tracing_map_sort_entries(). Sorting can be done on any field,
  529. * including keys.
  530. *
  531. * See tracing_map.h for a description of tracing_map_ops.
  532. *
  533. * Return: the tracing_map pointer if successful, ERR_PTR if not.
  534. */
  535. struct tracing_map *tracing_map_create(unsigned int map_bits,
  536. unsigned int key_size,
  537. const struct tracing_map_ops *ops,
  538. void *private_data)
  539. {
  540. struct tracing_map *map;
  541. unsigned int i;
  542. if (map_bits < TRACING_MAP_BITS_MIN ||
  543. map_bits > TRACING_MAP_BITS_MAX)
  544. return ERR_PTR(-EINVAL);
  545. map = kzalloc(sizeof(*map), GFP_KERNEL);
  546. if (!map)
  547. return ERR_PTR(-ENOMEM);
  548. map->map_bits = map_bits;
  549. map->max_elts = (1 << map_bits);
  550. atomic_set(&map->next_elt, -1);
  551. map->map_size = (1 << (map_bits + 1));
  552. map->ops = ops;
  553. map->private_data = private_data;
  554. map->map = tracing_map_array_alloc(map->map_size,
  555. sizeof(struct tracing_map_entry));
  556. if (!map->map)
  557. goto free;
  558. map->key_size = key_size;
  559. for (i = 0; i < TRACING_MAP_KEYS_MAX; i++)
  560. map->key_idx[i] = -1;
  561. out:
  562. return map;
  563. free:
  564. tracing_map_destroy(map);
  565. map = ERR_PTR(-ENOMEM);
  566. goto out;
  567. }
  568. /**
  569. * tracing_map_init - Allocate and clear a map's tracing_map_elts
  570. * @map: The tracing_map to initialize
  571. *
  572. * Allocates a clears a pool of tracing_map_elts equal to the
  573. * user-specified size of 2 ** map_bits (internally maintained as
  574. * 'max_elts' in struct tracing_map). Before using, the map fields
  575. * should be added to the map with tracing_map_add_sum_field() and
  576. * tracing_map_add_key_field(). tracing_map_init() should then be
  577. * called to allocate the array of tracing_map_elts, in order to avoid
  578. * allocating anything in the map insertion path. The user-specified
  579. * map size reflects the max number of elements requested by the user
  580. * - internally we double that in order to keep the table sparse and
  581. * keep collisions manageable.
  582. *
  583. * See tracing_map.h for a description of tracing_map_ops.
  584. *
  585. * Return: the tracing_map pointer if successful, ERR_PTR if not.
  586. */
  587. int tracing_map_init(struct tracing_map *map)
  588. {
  589. int err;
  590. if (map->n_fields < 2)
  591. return -EINVAL; /* need at least 1 key and 1 val */
  592. err = tracing_map_alloc_elts(map);
  593. if (err)
  594. return err;
  595. tracing_map_clear(map);
  596. return err;
  597. }
  598. static int cmp_entries_dup(const struct tracing_map_sort_entry **a,
  599. const struct tracing_map_sort_entry **b)
  600. {
  601. int ret = 0;
  602. if (memcmp((*a)->key, (*b)->key, (*a)->elt->map->key_size))
  603. ret = 1;
  604. return ret;
  605. }
  606. static int cmp_entries_sum(const struct tracing_map_sort_entry **a,
  607. const struct tracing_map_sort_entry **b)
  608. {
  609. const struct tracing_map_elt *elt_a, *elt_b;
  610. struct tracing_map_sort_key *sort_key;
  611. struct tracing_map_field *field;
  612. tracing_map_cmp_fn_t cmp_fn;
  613. void *val_a, *val_b;
  614. int ret = 0;
  615. elt_a = (*a)->elt;
  616. elt_b = (*b)->elt;
  617. sort_key = &elt_a->map->sort_key;
  618. field = &elt_a->fields[sort_key->field_idx];
  619. cmp_fn = field->cmp_fn;
  620. val_a = &elt_a->fields[sort_key->field_idx].sum;
  621. val_b = &elt_b->fields[sort_key->field_idx].sum;
  622. ret = cmp_fn(val_a, val_b);
  623. if (sort_key->descending)
  624. ret = -ret;
  625. return ret;
  626. }
  627. static int cmp_entries_key(const struct tracing_map_sort_entry **a,
  628. const struct tracing_map_sort_entry **b)
  629. {
  630. const struct tracing_map_elt *elt_a, *elt_b;
  631. struct tracing_map_sort_key *sort_key;
  632. struct tracing_map_field *field;
  633. tracing_map_cmp_fn_t cmp_fn;
  634. void *val_a, *val_b;
  635. int ret = 0;
  636. elt_a = (*a)->elt;
  637. elt_b = (*b)->elt;
  638. sort_key = &elt_a->map->sort_key;
  639. field = &elt_a->fields[sort_key->field_idx];
  640. cmp_fn = field->cmp_fn;
  641. val_a = elt_a->key + field->offset;
  642. val_b = elt_b->key + field->offset;
  643. ret = cmp_fn(val_a, val_b);
  644. if (sort_key->descending)
  645. ret = -ret;
  646. return ret;
  647. }
  648. static void destroy_sort_entry(struct tracing_map_sort_entry *entry)
  649. {
  650. if (!entry)
  651. return;
  652. if (entry->elt_copied)
  653. tracing_map_elt_free(entry->elt);
  654. kfree(entry);
  655. }
  656. /**
  657. * tracing_map_destroy_sort_entries - Destroy an array of sort entries
  658. * @entries: The entries to destroy
  659. * @n_entries: The number of entries in the array
  660. *
  661. * Destroy the elements returned by a tracing_map_sort_entries() call.
  662. */
  663. void tracing_map_destroy_sort_entries(struct tracing_map_sort_entry **entries,
  664. unsigned int n_entries)
  665. {
  666. unsigned int i;
  667. for (i = 0; i < n_entries; i++)
  668. destroy_sort_entry(entries[i]);
  669. vfree(entries);
  670. }
  671. static struct tracing_map_sort_entry *
  672. create_sort_entry(void *key, struct tracing_map_elt *elt)
  673. {
  674. struct tracing_map_sort_entry *sort_entry;
  675. sort_entry = kzalloc(sizeof(*sort_entry), GFP_KERNEL);
  676. if (!sort_entry)
  677. return NULL;
  678. sort_entry->key = key;
  679. sort_entry->elt = elt;
  680. return sort_entry;
  681. }
  682. static struct tracing_map_elt *copy_elt(struct tracing_map_elt *elt)
  683. {
  684. struct tracing_map_elt *dup_elt;
  685. unsigned int i;
  686. dup_elt = tracing_map_elt_alloc(elt->map);
  687. if (IS_ERR(dup_elt))
  688. return NULL;
  689. if (elt->map->ops && elt->map->ops->elt_copy)
  690. elt->map->ops->elt_copy(dup_elt, elt);
  691. dup_elt->private_data = elt->private_data;
  692. memcpy(dup_elt->key, elt->key, elt->map->key_size);
  693. for (i = 0; i < elt->map->n_fields; i++) {
  694. atomic64_set(&dup_elt->fields[i].sum,
  695. atomic64_read(&elt->fields[i].sum));
  696. dup_elt->fields[i].cmp_fn = elt->fields[i].cmp_fn;
  697. }
  698. return dup_elt;
  699. }
  700. static int merge_dup(struct tracing_map_sort_entry **sort_entries,
  701. unsigned int target, unsigned int dup)
  702. {
  703. struct tracing_map_elt *target_elt, *elt;
  704. bool first_dup = (target - dup) == 1;
  705. int i;
  706. if (first_dup) {
  707. elt = sort_entries[target]->elt;
  708. target_elt = copy_elt(elt);
  709. if (!target_elt)
  710. return -ENOMEM;
  711. sort_entries[target]->elt = target_elt;
  712. sort_entries[target]->elt_copied = true;
  713. } else
  714. target_elt = sort_entries[target]->elt;
  715. elt = sort_entries[dup]->elt;
  716. for (i = 0; i < elt->map->n_fields; i++)
  717. atomic64_add(atomic64_read(&elt->fields[i].sum),
  718. &target_elt->fields[i].sum);
  719. sort_entries[dup]->dup = true;
  720. return 0;
  721. }
  722. static int merge_dups(struct tracing_map_sort_entry **sort_entries,
  723. int n_entries, unsigned int key_size)
  724. {
  725. unsigned int dups = 0, total_dups = 0;
  726. int err, i, j;
  727. void *key;
  728. if (n_entries < 2)
  729. return total_dups;
  730. sort(sort_entries, n_entries, sizeof(struct tracing_map_sort_entry *),
  731. (int (*)(const void *, const void *))cmp_entries_dup, NULL);
  732. key = sort_entries[0]->key;
  733. for (i = 1; i < n_entries; i++) {
  734. if (!memcmp(sort_entries[i]->key, key, key_size)) {
  735. dups++; total_dups++;
  736. err = merge_dup(sort_entries, i - dups, i);
  737. if (err)
  738. return err;
  739. continue;
  740. }
  741. key = sort_entries[i]->key;
  742. dups = 0;
  743. }
  744. if (!total_dups)
  745. return total_dups;
  746. for (i = 0, j = 0; i < n_entries; i++) {
  747. if (!sort_entries[i]->dup) {
  748. sort_entries[j] = sort_entries[i];
  749. if (j++ != i)
  750. sort_entries[i] = NULL;
  751. } else {
  752. destroy_sort_entry(sort_entries[i]);
  753. sort_entries[i] = NULL;
  754. }
  755. }
  756. return total_dups;
  757. }
  758. static bool is_key(struct tracing_map *map, unsigned int field_idx)
  759. {
  760. unsigned int i;
  761. for (i = 0; i < map->n_keys; i++)
  762. if (map->key_idx[i] == field_idx)
  763. return true;
  764. return false;
  765. }
  766. static void sort_secondary(struct tracing_map *map,
  767. const struct tracing_map_sort_entry **entries,
  768. unsigned int n_entries,
  769. struct tracing_map_sort_key *primary_key,
  770. struct tracing_map_sort_key *secondary_key)
  771. {
  772. int (*primary_fn)(const struct tracing_map_sort_entry **,
  773. const struct tracing_map_sort_entry **);
  774. int (*secondary_fn)(const struct tracing_map_sort_entry **,
  775. const struct tracing_map_sort_entry **);
  776. unsigned i, start = 0, n_sub = 1;
  777. if (is_key(map, primary_key->field_idx))
  778. primary_fn = cmp_entries_key;
  779. else
  780. primary_fn = cmp_entries_sum;
  781. if (is_key(map, secondary_key->field_idx))
  782. secondary_fn = cmp_entries_key;
  783. else
  784. secondary_fn = cmp_entries_sum;
  785. for (i = 0; i < n_entries - 1; i++) {
  786. const struct tracing_map_sort_entry **a = &entries[i];
  787. const struct tracing_map_sort_entry **b = &entries[i + 1];
  788. if (primary_fn(a, b) == 0) {
  789. n_sub++;
  790. if (i < n_entries - 2)
  791. continue;
  792. }
  793. if (n_sub < 2) {
  794. start = i + 1;
  795. n_sub = 1;
  796. continue;
  797. }
  798. set_sort_key(map, secondary_key);
  799. sort(&entries[start], n_sub,
  800. sizeof(struct tracing_map_sort_entry *),
  801. (int (*)(const void *, const void *))secondary_fn, NULL);
  802. set_sort_key(map, primary_key);
  803. start = i + 1;
  804. n_sub = 1;
  805. }
  806. }
  807. /**
  808. * tracing_map_sort_entries - Sort the current set of tracing_map_elts in a map
  809. * @map: The tracing_map
  810. * @sort_key: The sort key to use for sorting
  811. * @sort_entries: outval: pointer to allocated and sorted array of entries
  812. *
  813. * tracing_map_sort_entries() sorts the current set of entries in the
  814. * map and returns the list of tracing_map_sort_entries containing
  815. * them to the client in the sort_entries param. The client can
  816. * access the struct tracing_map_elt element of interest directly as
  817. * the 'elt' field of a returned struct tracing_map_sort_entry object.
  818. *
  819. * The sort_key has only two fields: idx and descending. 'idx' refers
  820. * to the index of the field added via tracing_map_add_sum_field() or
  821. * tracing_map_add_key_field() when the tracing_map was initialized.
  822. * 'descending' is a flag that if set reverses the sort order, which
  823. * by default is ascending.
  824. *
  825. * The client should not hold on to the returned array but should use
  826. * it and call tracing_map_destroy_sort_entries() when done.
  827. *
  828. * Return: the number of sort_entries in the struct tracing_map_sort_entry
  829. * array, negative on error
  830. */
  831. int tracing_map_sort_entries(struct tracing_map *map,
  832. struct tracing_map_sort_key *sort_keys,
  833. unsigned int n_sort_keys,
  834. struct tracing_map_sort_entry ***sort_entries)
  835. {
  836. int (*cmp_entries_fn)(const struct tracing_map_sort_entry **,
  837. const struct tracing_map_sort_entry **);
  838. struct tracing_map_sort_entry *sort_entry, **entries;
  839. int i, n_entries, ret;
  840. entries = vmalloc(map->max_elts * sizeof(sort_entry));
  841. if (!entries)
  842. return -ENOMEM;
  843. for (i = 0, n_entries = 0; i < map->map_size; i++) {
  844. struct tracing_map_entry *entry;
  845. entry = TRACING_MAP_ENTRY(map->map, i);
  846. if (!entry->key || !entry->val)
  847. continue;
  848. entries[n_entries] = create_sort_entry(entry->val->key,
  849. entry->val);
  850. if (!entries[n_entries++]) {
  851. ret = -ENOMEM;
  852. goto free;
  853. }
  854. }
  855. if (n_entries == 0) {
  856. ret = 0;
  857. goto free;
  858. }
  859. if (n_entries == 1) {
  860. *sort_entries = entries;
  861. return 1;
  862. }
  863. ret = merge_dups(entries, n_entries, map->key_size);
  864. if (ret < 0)
  865. goto free;
  866. n_entries -= ret;
  867. if (is_key(map, sort_keys[0].field_idx))
  868. cmp_entries_fn = cmp_entries_key;
  869. else
  870. cmp_entries_fn = cmp_entries_sum;
  871. set_sort_key(map, &sort_keys[0]);
  872. sort(entries, n_entries, sizeof(struct tracing_map_sort_entry *),
  873. (int (*)(const void *, const void *))cmp_entries_fn, NULL);
  874. if (n_sort_keys > 1)
  875. sort_secondary(map,
  876. (const struct tracing_map_sort_entry **)entries,
  877. n_entries,
  878. &sort_keys[0],
  879. &sort_keys[1]);
  880. *sort_entries = entries;
  881. return n_entries;
  882. free:
  883. tracing_map_destroy_sort_entries(entries, n_entries);
  884. return ret;
  885. }