tracing_map.c 28 KB

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