Skip to content

Commit

Permalink
tracing: Fix cmp_entries_dup() to respect sort() comparison rules
Browse files Browse the repository at this point in the history
The cmp_entries_dup() function used as the comparator for sort()
violated the symmetry and transitivity properties required by the
sorting algorithm. Specifically, it returned 1 whenever memcmp() was
non-zero, which broke the following expectations:

* Symmetry: If x < y, then y > x.
* Transitivity: If x < y and y < z, then x < z.

These violations could lead to incorrect sorting and failure to
correctly identify duplicate elements.

Fix the issue by directly returning the result of memcmp(), which
adheres to the required comparison properties.

Cc: stable@vger.kernel.org
Fixes: 08d43a5 ("tracing: Add lock-free tracing_map")
Link: https://lore.kernel.org/20241203202228.1274403-1-visitorckw@gmail.com
Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
  • Loading branch information
Kuan-Wei Chiu authored and Steven Rostedt (Google) committed Dec 4, 2024
1 parent 40384c8 commit e63fbd5
Showing 1 changed file with 1 addition and 5 deletions.
6 changes: 1 addition & 5 deletions kernel/trace/tracing_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -845,15 +845,11 @@ int tracing_map_init(struct tracing_map *map)
static int cmp_entries_dup(const void *A, const void *B)
{
const struct tracing_map_sort_entry *a, *b;
int ret = 0;

a = *(const struct tracing_map_sort_entry **)A;
b = *(const struct tracing_map_sort_entry **)B;

if (memcmp(a->key, b->key, a->elt->map->key_size))
ret = 1;

return ret;
return memcmp(a->key, b->key, a->elt->map->key_size);
}

static int cmp_entries_sum(const void *A, const void *B)
Expand Down

0 comments on commit e63fbd5

Please sign in to comment.