tracing_map.c 27 KB

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