Linux Audio

Check our new training course

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