1 // SPDX-License-Identifier: GPL-2.0
3 * tracing_map - lock-free map for tracing
5 * Copyright (C) 2015 Tom Zanussi <tom.zanussi@linux.intel.com>
7 * tracing_map implementation inspired by lock-free map algorithms
8 * originated by Dr. Cliff Click:
10 * http://www.azulsystems.com/blog/cliff/2007-03-26-non-blocking-hashtable
11 * http://www.azulsystems.com/events/javaone_2007/2007_LockFreeHash.pdf
14 #include <linux/vmalloc.h>
15 #include <linux/jhash.h>
16 #include <linux/slab.h>
17 #include <linux/sort.h>
19 #include "tracing_map.h"
23 * NOTE: For a detailed description of the data structures used by
24 * these functions (such as tracing_map_elt) please see the overview
25 * of tracing_map data structures at the beginning of tracing_map.h.
29 * tracing_map_update_sum - Add a value to a tracing_map_elt's sum field
30 * @elt: The tracing_map_elt
31 * @i: The index of the given sum associated with the tracing_map_elt
32 * @n: The value to add to the sum
34 * Add n to sum i associated with the specified tracing_map_elt
35 * instance. The index i is the index returned by the call to
36 * tracing_map_add_sum_field() when the tracing map was set up.
38 void tracing_map_update_sum(struct tracing_map_elt *elt, unsigned int i, u64 n)
40 atomic64_add(n, &elt->fields[i].sum);
44 * tracing_map_read_sum - Return the value of a tracing_map_elt's sum field
45 * @elt: The tracing_map_elt
46 * @i: The index of the given sum associated with the tracing_map_elt
48 * Retrieve the value of the sum i associated with the specified
49 * tracing_map_elt instance. The index i is the index returned by the
50 * call to tracing_map_add_sum_field() when the tracing map was set
53 * Return: The sum associated with field i for elt.
55 u64 tracing_map_read_sum(struct tracing_map_elt *elt, unsigned int i)
57 return (u64)atomic64_read(&elt->fields[i].sum);
61 * tracing_map_set_var - Assign a tracing_map_elt's variable field
62 * @elt: The tracing_map_elt
63 * @i: The index of the given variable associated with the tracing_map_elt
64 * @n: The value to assign
66 * Assign n to variable i associated with the specified tracing_map_elt
67 * instance. The index i is the index returned by the call to
68 * tracing_map_add_var() when the tracing map was set up.
70 void tracing_map_set_var(struct tracing_map_elt *elt, unsigned int i, u64 n)
72 atomic64_set(&elt->vars[i], n);
73 elt->var_set[i] = true;
77 * tracing_map_var_set - Return whether or not a variable has been set
78 * @elt: The tracing_map_elt
79 * @i: The index of the given variable associated with the tracing_map_elt
81 * Return true if the variable has been set, false otherwise. The
82 * index i is the index returned by the call to tracing_map_add_var()
83 * when the tracing map was set up.
85 bool tracing_map_var_set(struct tracing_map_elt *elt, unsigned int i)
87 return elt->var_set[i];
91 * tracing_map_read_var - Return the value of a tracing_map_elt's variable field
92 * @elt: The tracing_map_elt
93 * @i: The index of the given variable associated with the tracing_map_elt
95 * Retrieve the value of the variable i associated with the specified
96 * tracing_map_elt instance. The index i is the index returned by the
97 * call to tracing_map_add_var() when the tracing map was set
100 * Return: The variable value associated with field i for elt.
102 u64 tracing_map_read_var(struct tracing_map_elt *elt, unsigned int i)
104 return (u64)atomic64_read(&elt->vars[i]);
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
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.
119 * Return: The variable value associated with field i for elt.
121 u64 tracing_map_read_var_once(struct tracing_map_elt *elt, unsigned int i)
123 elt->var_set[i] = false;
124 return (u64)atomic64_read(&elt->vars[i]);
127 int tracing_map_cmp_string(void *val_a, void *val_b)
135 int tracing_map_cmp_none(void *val_a, void *val_b)
140 static int tracing_map_cmp_atomic64(void *val_a, void *val_b)
142 u64 a = atomic64_read((atomic64_t *)val_a);
143 u64 b = atomic64_read((atomic64_t *)val_b);
145 return (a > b) ? 1 : ((a < b) ? -1 : 0);
148 #define DEFINE_TRACING_MAP_CMP_FN(type) \
149 static int tracing_map_cmp_##type(void *val_a, void *val_b) \
151 type a = (type)(*(u64 *)val_a); \
152 type b = (type)(*(u64 *)val_b); \
154 return (a > b) ? 1 : ((a < b) ? -1 : 0); \
157 DEFINE_TRACING_MAP_CMP_FN(s64);
158 DEFINE_TRACING_MAP_CMP_FN(u64);
159 DEFINE_TRACING_MAP_CMP_FN(s32);
160 DEFINE_TRACING_MAP_CMP_FN(u32);
161 DEFINE_TRACING_MAP_CMP_FN(s16);
162 DEFINE_TRACING_MAP_CMP_FN(u16);
163 DEFINE_TRACING_MAP_CMP_FN(s8);
164 DEFINE_TRACING_MAP_CMP_FN(u8);
166 tracing_map_cmp_fn_t tracing_map_cmp_num(int field_size,
169 tracing_map_cmp_fn_t fn = tracing_map_cmp_none;
171 switch (field_size) {
174 fn = tracing_map_cmp_s64;
176 fn = tracing_map_cmp_u64;
180 fn = tracing_map_cmp_s32;
182 fn = tracing_map_cmp_u32;
186 fn = tracing_map_cmp_s16;
188 fn = tracing_map_cmp_u16;
192 fn = tracing_map_cmp_s8;
194 fn = tracing_map_cmp_u8;
201 static int tracing_map_add_field(struct tracing_map *map,
202 tracing_map_cmp_fn_t cmp_fn)
206 if (map->n_fields < TRACING_MAP_FIELDS_MAX) {
208 map->fields[map->n_fields++].cmp_fn = cmp_fn;
215 * tracing_map_add_sum_field - Add a field describing a tracing_map sum
216 * @map: The tracing_map
218 * Add a sum field to the key and return the index identifying it in
219 * the map and associated tracing_map_elts. This is the index used
220 * for instance to update a sum for a particular tracing_map_elt using
221 * tracing_map_update_sum() or reading it via tracing_map_read_sum().
223 * Return: The index identifying the field in the map and associated
224 * tracing_map_elts, or -EINVAL on error.
226 int tracing_map_add_sum_field(struct tracing_map *map)
228 return tracing_map_add_field(map, tracing_map_cmp_atomic64);
232 * tracing_map_add_var - Add a field describing a tracing_map var
233 * @map: The tracing_map
235 * Add a var to the map and return the index identifying it in the map
236 * and associated tracing_map_elts. This is the index used for
237 * instance to update a var for a particular tracing_map_elt using
238 * tracing_map_update_var() or reading it via tracing_map_read_var().
240 * Return: The index identifying the var in the map and associated
241 * tracing_map_elts, or -EINVAL on error.
243 int tracing_map_add_var(struct tracing_map *map)
247 if (map->n_vars < TRACING_MAP_VARS_MAX)
254 * tracing_map_add_key_field - Add a field describing a tracing_map key
255 * @map: The tracing_map
256 * @offset: The offset within the key
257 * @cmp_fn: The comparison function that will be used to sort on the key
259 * Let the map know there is a key and that if it's used as a sort key
262 * A key can be a subset of a compound key; for that purpose, the
263 * offset param is used to describe where within the compound key
264 * the key referenced by this key field resides.
266 * Return: The index identifying the field in the map and associated
267 * tracing_map_elts, or -EINVAL on error.
269 int tracing_map_add_key_field(struct tracing_map *map,
271 tracing_map_cmp_fn_t cmp_fn)
274 int idx = tracing_map_add_field(map, cmp_fn);
279 map->fields[idx].offset = offset;
281 map->key_idx[map->n_keys++] = idx;
286 static void tracing_map_array_clear(struct tracing_map_array *a)
293 for (i = 0; i < a->n_pages; i++)
294 memset(a->pages[i], 0, PAGE_SIZE);
297 static void tracing_map_array_free(struct tracing_map_array *a)
307 for (i = 0; i < a->n_pages; i++) {
310 free_page((unsigned long)a->pages[i]);
319 static struct tracing_map_array *tracing_map_array_alloc(unsigned int n_elts,
320 unsigned int entry_size)
322 struct tracing_map_array *a;
325 a = kzalloc(sizeof(*a), GFP_KERNEL);
329 a->entry_size_shift = fls(roundup_pow_of_two(entry_size) - 1);
330 a->entries_per_page = PAGE_SIZE / (1 << a->entry_size_shift);
331 a->n_pages = n_elts / a->entries_per_page;
334 a->entry_shift = fls(a->entries_per_page) - 1;
335 a->entry_mask = (1 << a->entry_shift) - 1;
337 a->pages = kcalloc(a->n_pages, sizeof(void *), GFP_KERNEL);
341 for (i = 0; i < a->n_pages; i++) {
342 a->pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
349 tracing_map_array_free(a);
355 static void tracing_map_elt_clear(struct tracing_map_elt *elt)
359 for (i = 0; i < elt->map->n_fields; i++)
360 if (elt->fields[i].cmp_fn == tracing_map_cmp_atomic64)
361 atomic64_set(&elt->fields[i].sum, 0);
363 for (i = 0; i < elt->map->n_vars; i++) {
364 atomic64_set(&elt->vars[i], 0);
365 elt->var_set[i] = false;
368 if (elt->map->ops && elt->map->ops->elt_clear)
369 elt->map->ops->elt_clear(elt);
372 static void tracing_map_elt_init_fields(struct tracing_map_elt *elt)
376 tracing_map_elt_clear(elt);
378 for (i = 0; i < elt->map->n_fields; i++) {
379 elt->fields[i].cmp_fn = elt->map->fields[i].cmp_fn;
381 if (elt->fields[i].cmp_fn != tracing_map_cmp_atomic64)
382 elt->fields[i].offset = elt->map->fields[i].offset;
386 static void tracing_map_elt_free(struct tracing_map_elt *elt)
391 if (elt->map->ops && elt->map->ops->elt_free)
392 elt->map->ops->elt_free(elt);
400 static struct tracing_map_elt *tracing_map_elt_alloc(struct tracing_map *map)
402 struct tracing_map_elt *elt;
405 elt = kzalloc(sizeof(*elt), GFP_KERNEL);
407 return ERR_PTR(-ENOMEM);
411 elt->key = kzalloc(map->key_size, GFP_KERNEL);
417 elt->fields = kcalloc(map->n_fields, sizeof(*elt->fields), GFP_KERNEL);
423 elt->vars = kcalloc(map->n_vars, sizeof(*elt->vars), GFP_KERNEL);
429 elt->var_set = kcalloc(map->n_vars, sizeof(*elt->var_set), GFP_KERNEL);
435 tracing_map_elt_init_fields(elt);
437 if (map->ops && map->ops->elt_alloc) {
438 err = map->ops->elt_alloc(elt);
444 tracing_map_elt_free(elt);
449 static struct tracing_map_elt *get_free_elt(struct tracing_map *map)
451 struct tracing_map_elt *elt = NULL;
454 idx = atomic_inc_return(&map->next_elt);
455 if (idx < map->max_elts) {
456 elt = *(TRACING_MAP_ELT(map->elts, idx));
457 if (map->ops && map->ops->elt_init)
458 map->ops->elt_init(elt);
464 static void tracing_map_free_elts(struct tracing_map *map)
471 for (i = 0; i < map->max_elts; i++) {
472 tracing_map_elt_free(*(TRACING_MAP_ELT(map->elts, i)));
473 *(TRACING_MAP_ELT(map->elts, i)) = NULL;
476 tracing_map_array_free(map->elts);
480 static int tracing_map_alloc_elts(struct tracing_map *map)
484 map->elts = tracing_map_array_alloc(map->max_elts,
485 sizeof(struct tracing_map_elt *));
489 for (i = 0; i < map->max_elts; i++) {
490 *(TRACING_MAP_ELT(map->elts, i)) = tracing_map_elt_alloc(map);
491 if (IS_ERR(*(TRACING_MAP_ELT(map->elts, i)))) {
492 *(TRACING_MAP_ELT(map->elts, i)) = NULL;
493 tracing_map_free_elts(map);
502 static inline bool keys_match(void *key, void *test_key, unsigned key_size)
506 if (memcmp(key, test_key, key_size))
512 static inline struct tracing_map_elt *
513 __tracing_map_insert(struct tracing_map *map, void *key, bool lookup_only)
515 u32 idx, key_hash, test_key;
517 struct tracing_map_entry *entry;
518 struct tracing_map_elt *val;
520 key_hash = jhash(key, map->key_size, 0);
523 idx = key_hash >> (32 - (map->map_bits + 1));
526 idx &= (map->map_size - 1);
527 entry = TRACING_MAP_ENTRY(map->map, idx);
528 test_key = entry->key;
530 if (test_key && test_key == key_hash) {
531 val = READ_ONCE(entry->val);
533 keys_match(key, val->key, map->key_size)) {
535 atomic64_inc(&map->hits);
537 } else if (unlikely(!val)) {
539 * The key is present. But, val (pointer to elt
540 * struct) is still NULL. which means some other
541 * thread is in the process of inserting an
544 * On top of that, it's key_hash is same as the
545 * one being inserted right now. So, it's
546 * possible that the element has the same
551 if (dup_try > map->map_size) {
552 atomic64_inc(&map->drops);
563 if (!cmpxchg(&entry->key, 0, key_hash)) {
564 struct tracing_map_elt *elt;
566 elt = get_free_elt(map);
568 atomic64_inc(&map->drops);
573 memcpy(elt->key, key, map->key_size);
575 atomic64_inc(&map->hits);
580 * cmpxchg() failed. Loop around once
581 * more to check what key was inserted.
595 * tracing_map_insert - Insert key and/or retrieve val from a tracing_map
596 * @map: The tracing_map to insert into
597 * @key: The key to insert
599 * Inserts a key into a tracing_map and creates and returns a new
600 * tracing_map_elt for it, or if the key has already been inserted by
601 * a previous call, returns the tracing_map_elt already associated
602 * with it. When the map was created, the number of elements to be
603 * allocated for the map was specified (internally maintained as
604 * 'max_elts' in struct tracing_map), and that number of
605 * tracing_map_elts was created by tracing_map_init(). This is the
606 * pre-allocated pool of tracing_map_elts that tracing_map_insert()
607 * will allocate from when adding new keys. Once that pool is
608 * exhausted, tracing_map_insert() is useless and will return NULL to
609 * signal that state. There are two user-visible tracing_map
610 * variables, 'hits' and 'drops', which are updated by this function.
611 * Every time an element is either successfully inserted or retrieved,
612 * the 'hits' value is incremented. Every time an element insertion
613 * fails, the 'drops' value is incremented.
615 * This is a lock-free tracing map insertion function implementing a
616 * modified form of Cliff Click's basic insertion algorithm. It
617 * requires the table size be a power of two. To prevent any
618 * possibility of an infinite loop we always make the internal table
619 * size double the size of the requested table size (max_elts * 2).
620 * Likewise, we never reuse a slot or resize or delete elements - when
621 * we've reached max_elts entries, we simply return NULL once we've
622 * run out of entries. Readers can at any point in time traverse the
623 * tracing map and safely access the key/val pairs.
625 * Return: the tracing_map_elt pointer val associated with the key.
626 * If this was a newly inserted key, the val will be a newly allocated
627 * and associated tracing_map_elt pointer val. If the key wasn't
628 * found and the pool of tracing_map_elts has been exhausted, NULL is
629 * returned and no further insertions will succeed.
631 struct tracing_map_elt *tracing_map_insert(struct tracing_map *map, void *key)
633 return __tracing_map_insert(map, key, false);
637 * tracing_map_lookup - Retrieve val from a tracing_map
638 * @map: The tracing_map to perform the lookup on
639 * @key: The key to look up
641 * Looks up key in tracing_map and if found returns the matching
642 * tracing_map_elt. This is a lock-free lookup; see
643 * tracing_map_insert() for details on tracing_map and how it works.
644 * Every time an element is retrieved, the 'hits' value is
645 * incremented. There is one user-visible tracing_map variable,
646 * 'hits', which is updated by this function. Every time an element
647 * is successfully retrieved, the 'hits' value is incremented. The
648 * 'drops' value is never updated by this function.
650 * Return: the tracing_map_elt pointer val associated with the key.
651 * If the key wasn't found, NULL is returned.
653 struct tracing_map_elt *tracing_map_lookup(struct tracing_map *map, void *key)
655 return __tracing_map_insert(map, key, true);
659 * tracing_map_destroy - Destroy a tracing_map
660 * @map: The tracing_map to destroy
662 * Frees a tracing_map along with its associated array of
665 * Callers should make sure there are no readers or writers actively
666 * reading or inserting into the map before calling this.
668 void tracing_map_destroy(struct tracing_map *map)
673 tracing_map_free_elts(map);
675 tracing_map_array_free(map->map);
680 * tracing_map_clear - Clear a tracing_map
681 * @map: The tracing_map to clear
683 * Resets the tracing map to a cleared or initial state. The
684 * tracing_map_elts are all cleared, and the array of struct
685 * tracing_map_entry is reset to an initialized state.
687 * Callers should make sure there are no writers actively inserting
688 * into the map before calling this.
690 void tracing_map_clear(struct tracing_map *map)
694 atomic_set(&map->next_elt, -1);
695 atomic64_set(&map->hits, 0);
696 atomic64_set(&map->drops, 0);
698 tracing_map_array_clear(map->map);
700 for (i = 0; i < map->max_elts; i++)
701 tracing_map_elt_clear(*(TRACING_MAP_ELT(map->elts, i)));
704 static void set_sort_key(struct tracing_map *map,
705 struct tracing_map_sort_key *sort_key)
707 map->sort_key = *sort_key;
711 * tracing_map_create - Create a lock-free map and element pool
712 * @map_bits: The size of the map (2 ** map_bits)
713 * @key_size: The size of the key for the map in bytes
714 * @ops: Optional client-defined tracing_map_ops instance
715 * @private_data: Client data associated with the map
717 * Creates and sets up a map to contain 2 ** map_bits number of
718 * elements (internally maintained as 'max_elts' in struct
719 * tracing_map). Before using, map fields should be added to the map
720 * with tracing_map_add_sum_field() and tracing_map_add_key_field().
721 * tracing_map_init() should then be called to allocate the array of
722 * tracing_map_elts, in order to avoid allocating anything in the map
723 * insertion path. The user-specified map size reflects the maximum
724 * number of elements that can be contained in the table requested by
725 * the user - internally we double that in order to keep the table
726 * sparse and keep collisions manageable.
728 * A tracing_map is a special-purpose map designed to aggregate or
729 * 'sum' one or more values associated with a specific object of type
730 * tracing_map_elt, which is attached by the map to a given key.
732 * tracing_map_create() sets up the map itself, and provides
733 * operations for inserting tracing_map_elts, but doesn't allocate the
734 * tracing_map_elts themselves, or provide a means for describing the
735 * keys or sums associated with the tracing_map_elts. All
736 * tracing_map_elts for a given map have the same set of sums and
737 * keys, which are defined by the client using the functions
738 * tracing_map_add_key_field() and tracing_map_add_sum_field(). Once
739 * the fields are defined, the pool of elements allocated for the map
740 * can be created, which occurs when the client code calls
741 * tracing_map_init().
743 * When tracing_map_init() returns, tracing_map_elt elements can be
744 * inserted into the map using tracing_map_insert(). When called,
745 * tracing_map_insert() grabs a free tracing_map_elt from the pool, or
746 * finds an existing match in the map and in either case returns it.
747 * The client can then use tracing_map_update_sum() and
748 * tracing_map_read_sum() to update or read a given sum field for the
751 * The client can at any point retrieve and traverse the current set
752 * of inserted tracing_map_elts in a tracing_map, via
753 * tracing_map_sort_entries(). Sorting can be done on any field,
756 * See tracing_map.h for a description of tracing_map_ops.
758 * Return: the tracing_map pointer if successful, ERR_PTR if not.
760 struct tracing_map *tracing_map_create(unsigned int map_bits,
761 unsigned int key_size,
762 const struct tracing_map_ops *ops,
765 struct tracing_map *map;
768 if (map_bits < TRACING_MAP_BITS_MIN ||
769 map_bits > TRACING_MAP_BITS_MAX)
770 return ERR_PTR(-EINVAL);
772 map = kzalloc(sizeof(*map), GFP_KERNEL);
774 return ERR_PTR(-ENOMEM);
776 map->map_bits = map_bits;
777 map->max_elts = (1 << map_bits);
778 atomic_set(&map->next_elt, -1);
780 map->map_size = (1 << (map_bits + 1));
783 map->private_data = private_data;
785 map->map = tracing_map_array_alloc(map->map_size,
786 sizeof(struct tracing_map_entry));
790 map->key_size = key_size;
791 for (i = 0; i < TRACING_MAP_KEYS_MAX; i++)
792 map->key_idx[i] = -1;
796 tracing_map_destroy(map);
797 map = ERR_PTR(-ENOMEM);
803 * tracing_map_init - Allocate and clear a map's tracing_map_elts
804 * @map: The tracing_map to initialize
806 * Allocates a clears a pool of tracing_map_elts equal to the
807 * user-specified size of 2 ** map_bits (internally maintained as
808 * 'max_elts' in struct tracing_map). Before using, the map fields
809 * should be added to the map with tracing_map_add_sum_field() and
810 * tracing_map_add_key_field(). tracing_map_init() should then be
811 * called to allocate the array of tracing_map_elts, in order to avoid
812 * allocating anything in the map insertion path. The user-specified
813 * map size reflects the max number of elements requested by the user
814 * - internally we double that in order to keep the table sparse and
815 * keep collisions manageable.
817 * See tracing_map.h for a description of tracing_map_ops.
819 * Return: the tracing_map pointer if successful, ERR_PTR if not.
821 int tracing_map_init(struct tracing_map *map)
825 if (map->n_fields < 2)
826 return -EINVAL; /* need at least 1 key and 1 val */
828 err = tracing_map_alloc_elts(map);
832 tracing_map_clear(map);
837 static int cmp_entries_dup(const struct tracing_map_sort_entry **a,
838 const struct tracing_map_sort_entry **b)
842 if (memcmp((*a)->key, (*b)->key, (*a)->elt->map->key_size))
848 static int cmp_entries_sum(const struct tracing_map_sort_entry **a,
849 const struct tracing_map_sort_entry **b)
851 const struct tracing_map_elt *elt_a, *elt_b;
852 struct tracing_map_sort_key *sort_key;
853 struct tracing_map_field *field;
854 tracing_map_cmp_fn_t cmp_fn;
861 sort_key = &elt_a->map->sort_key;
863 field = &elt_a->fields[sort_key->field_idx];
864 cmp_fn = field->cmp_fn;
866 val_a = &elt_a->fields[sort_key->field_idx].sum;
867 val_b = &elt_b->fields[sort_key->field_idx].sum;
869 ret = cmp_fn(val_a, val_b);
870 if (sort_key->descending)
876 static int cmp_entries_key(const struct tracing_map_sort_entry **a,
877 const struct tracing_map_sort_entry **b)
879 const struct tracing_map_elt *elt_a, *elt_b;
880 struct tracing_map_sort_key *sort_key;
881 struct tracing_map_field *field;
882 tracing_map_cmp_fn_t cmp_fn;
889 sort_key = &elt_a->map->sort_key;
891 field = &elt_a->fields[sort_key->field_idx];
893 cmp_fn = field->cmp_fn;
895 val_a = elt_a->key + field->offset;
896 val_b = elt_b->key + field->offset;
898 ret = cmp_fn(val_a, val_b);
899 if (sort_key->descending)
905 static void destroy_sort_entry(struct tracing_map_sort_entry *entry)
910 if (entry->elt_copied)
911 tracing_map_elt_free(entry->elt);
917 * tracing_map_destroy_sort_entries - Destroy an array of sort entries
918 * @entries: The entries to destroy
919 * @n_entries: The number of entries in the array
921 * Destroy the elements returned by a tracing_map_sort_entries() call.
923 void tracing_map_destroy_sort_entries(struct tracing_map_sort_entry **entries,
924 unsigned int n_entries)
928 for (i = 0; i < n_entries; i++)
929 destroy_sort_entry(entries[i]);
934 static struct tracing_map_sort_entry *
935 create_sort_entry(void *key, struct tracing_map_elt *elt)
937 struct tracing_map_sort_entry *sort_entry;
939 sort_entry = kzalloc(sizeof(*sort_entry), GFP_KERNEL);
943 sort_entry->key = key;
944 sort_entry->elt = elt;
949 static void detect_dups(struct tracing_map_sort_entry **sort_entries,
950 int n_entries, unsigned int key_size)
952 unsigned int dups = 0, total_dups = 0;
959 sort(sort_entries, n_entries, sizeof(struct tracing_map_sort_entry *),
960 (int (*)(const void *, const void *))cmp_entries_dup, NULL);
962 key = sort_entries[0]->key;
963 for (i = 1; i < n_entries; i++) {
964 if (!memcmp(sort_entries[i]->key, key, key_size)) {
965 dups++; total_dups++;
968 key = sort_entries[i]->key;
972 WARN_ONCE(total_dups > 0,
973 "Duplicates detected: %d\n", total_dups);
976 static bool is_key(struct tracing_map *map, unsigned int field_idx)
980 for (i = 0; i < map->n_keys; i++)
981 if (map->key_idx[i] == field_idx)
986 static void sort_secondary(struct tracing_map *map,
987 const struct tracing_map_sort_entry **entries,
988 unsigned int n_entries,
989 struct tracing_map_sort_key *primary_key,
990 struct tracing_map_sort_key *secondary_key)
992 int (*primary_fn)(const struct tracing_map_sort_entry **,
993 const struct tracing_map_sort_entry **);
994 int (*secondary_fn)(const struct tracing_map_sort_entry **,
995 const struct tracing_map_sort_entry **);
996 unsigned i, start = 0, n_sub = 1;
998 if (is_key(map, primary_key->field_idx))
999 primary_fn = cmp_entries_key;
1001 primary_fn = cmp_entries_sum;
1003 if (is_key(map, secondary_key->field_idx))
1004 secondary_fn = cmp_entries_key;
1006 secondary_fn = cmp_entries_sum;
1008 for (i = 0; i < n_entries - 1; i++) {
1009 const struct tracing_map_sort_entry **a = &entries[i];
1010 const struct tracing_map_sort_entry **b = &entries[i + 1];
1012 if (primary_fn(a, b) == 0) {
1014 if (i < n_entries - 2)
1024 set_sort_key(map, secondary_key);
1025 sort(&entries[start], n_sub,
1026 sizeof(struct tracing_map_sort_entry *),
1027 (int (*)(const void *, const void *))secondary_fn, NULL);
1028 set_sort_key(map, primary_key);
1036 * tracing_map_sort_entries - Sort the current set of tracing_map_elts in a map
1037 * @map: The tracing_map
1038 * @sort_key: The sort key to use for sorting
1039 * @sort_entries: outval: pointer to allocated and sorted array of entries
1041 * tracing_map_sort_entries() sorts the current set of entries in the
1042 * map and returns the list of tracing_map_sort_entries containing
1043 * them to the client in the sort_entries param. The client can
1044 * access the struct tracing_map_elt element of interest directly as
1045 * the 'elt' field of a returned struct tracing_map_sort_entry object.
1047 * The sort_key has only two fields: idx and descending. 'idx' refers
1048 * to the index of the field added via tracing_map_add_sum_field() or
1049 * tracing_map_add_key_field() when the tracing_map was initialized.
1050 * 'descending' is a flag that if set reverses the sort order, which
1051 * by default is ascending.
1053 * The client should not hold on to the returned array but should use
1054 * it and call tracing_map_destroy_sort_entries() when done.
1056 * Return: the number of sort_entries in the struct tracing_map_sort_entry
1057 * array, negative on error
1059 int tracing_map_sort_entries(struct tracing_map *map,
1060 struct tracing_map_sort_key *sort_keys,
1061 unsigned int n_sort_keys,
1062 struct tracing_map_sort_entry ***sort_entries)
1064 int (*cmp_entries_fn)(const struct tracing_map_sort_entry **,
1065 const struct tracing_map_sort_entry **);
1066 struct tracing_map_sort_entry *sort_entry, **entries;
1067 int i, n_entries, ret;
1069 entries = vmalloc(array_size(sizeof(sort_entry), map->max_elts));
1073 for (i = 0, n_entries = 0; i < map->map_size; i++) {
1074 struct tracing_map_entry *entry;
1076 entry = TRACING_MAP_ENTRY(map->map, i);
1078 if (!entry->key || !entry->val)
1081 entries[n_entries] = create_sort_entry(entry->val->key,
1083 if (!entries[n_entries++]) {
1089 if (n_entries == 0) {
1094 if (n_entries == 1) {
1095 *sort_entries = entries;
1099 detect_dups(entries, n_entries, map->key_size);
1101 if (is_key(map, sort_keys[0].field_idx))
1102 cmp_entries_fn = cmp_entries_key;
1104 cmp_entries_fn = cmp_entries_sum;
1106 set_sort_key(map, &sort_keys[0]);
1108 sort(entries, n_entries, sizeof(struct tracing_map_sort_entry *),
1109 (int (*)(const void *, const void *))cmp_entries_fn, NULL);
1111 if (n_sort_keys > 1)
1113 (const struct tracing_map_sort_entry **)entries,
1118 *sort_entries = entries;
1122 tracing_map_destroy_sort_entries(entries, n_entries);