tracing_map.c 30 KB

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