Skip to content

Commit bcf86c0

Browse files
Tze-nan Wurostedt
authored andcommitted
tracing: Fix overflow in get_free_elt()
"tracing_map->next_elt" in get_free_elt() is at risk of overflowing. Once it overflows, new elements can still be inserted into the tracing_map even though the maximum number of elements (`max_elts`) has been reached. Continuing to insert elements after the overflow could result in the tracing_map containing "tracing_map->max_size" elements, leaving no empty entries. If any attempt is made to insert an element into a full tracing_map using `__tracing_map_insert()`, it will cause an infinite loop with preemption disabled, leading to a CPU hang problem. Fix this by preventing any further increments to "tracing_map->next_elt" once it reaches "tracing_map->max_elt". Cc: [email protected] Cc: Masami Hiramatsu <[email protected]> Fixes: 08d43a5 ("tracing: Add lock-free tracing_map") Co-developed-by: Cheng-Jui Wang <[email protected]> Link: https://lore.kernel.org/[email protected] Signed-off-by: Cheng-Jui Wang <[email protected]> Signed-off-by: Tze-nan Wu <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent 604b72b commit bcf86c0

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

kernel/trace/tracing_map.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ static struct tracing_map_elt *get_free_elt(struct tracing_map *map)
454454
struct tracing_map_elt *elt = NULL;
455455
int idx;
456456

457-
idx = atomic_inc_return(&map->next_elt);
457+
idx = atomic_fetch_add_unless(&map->next_elt, 1, map->max_elts);
458458
if (idx < map->max_elts) {
459459
elt = *(TRACING_MAP_ELT(map->elts, idx));
460460
if (map->ops && map->ops->elt_init)
@@ -699,7 +699,7 @@ void tracing_map_clear(struct tracing_map *map)
699699
{
700700
unsigned int i;
701701

702-
atomic_set(&map->next_elt, -1);
702+
atomic_set(&map->next_elt, 0);
703703
atomic64_set(&map->hits, 0);
704704
atomic64_set(&map->drops, 0);
705705

@@ -783,7 +783,7 @@ struct tracing_map *tracing_map_create(unsigned int map_bits,
783783

784784
map->map_bits = map_bits;
785785
map->max_elts = (1 << map_bits);
786-
atomic_set(&map->next_elt, -1);
786+
atomic_set(&map->next_elt, 0);
787787

788788
map->map_size = (1 << (map_bits + 1));
789789
map->ops = ops;

0 commit comments

Comments
 (0)