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: stable@vger.kernel.org
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Fixes: 08d43a5fa0 ("tracing: Add lock-free tracing_map")
Co-developed-by: Cheng-Jui Wang <cheng-jui.wang@mediatek.com>
Link: https://lore.kernel.org/20240805055922.6277-1-Tze-nan.Wu@mediatek.com
Signed-off-by: Cheng-Jui Wang <cheng-jui.wang@mediatek.com>
Signed-off-by: Tze-nan Wu <Tze-nan.Wu@mediatek.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
This commit is contained in:
Tze-nan Wu 2024-08-05 13:59:22 +08:00 committed by Steven Rostedt (Google)
parent 604b72b325
commit bcf86c01ca

View File

@ -454,7 +454,7 @@ static struct tracing_map_elt *get_free_elt(struct tracing_map *map)
struct tracing_map_elt *elt = NULL; struct tracing_map_elt *elt = NULL;
int idx; int idx;
idx = atomic_inc_return(&map->next_elt); idx = atomic_fetch_add_unless(&map->next_elt, 1, map->max_elts);
if (idx < map->max_elts) { if (idx < map->max_elts) {
elt = *(TRACING_MAP_ELT(map->elts, idx)); elt = *(TRACING_MAP_ELT(map->elts, idx));
if (map->ops && map->ops->elt_init) if (map->ops && map->ops->elt_init)
@ -699,7 +699,7 @@ void tracing_map_clear(struct tracing_map *map)
{ {
unsigned int i; unsigned int i;
atomic_set(&map->next_elt, -1); atomic_set(&map->next_elt, 0);
atomic64_set(&map->hits, 0); atomic64_set(&map->hits, 0);
atomic64_set(&map->drops, 0); atomic64_set(&map->drops, 0);
@ -783,7 +783,7 @@ struct tracing_map *tracing_map_create(unsigned int map_bits,
map->map_bits = map_bits; map->map_bits = map_bits;
map->max_elts = (1 << map_bits); map->max_elts = (1 << map_bits);
atomic_set(&map->next_elt, -1); atomic_set(&map->next_elt, 0);
map->map_size = (1 << (map_bits + 1)); map->map_size = (1 << (map_bits + 1));
map->ops = ops; map->ops = ops;