tracing_map.c 28 KB

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