* [PATCH] tracing: Fix overflow in get_free_elt()
@ 2024-07-10 9:19 Tze-nan Wu
2024-07-22 5:55 ` Tze-nan Wu (吳澤南)
0 siblings, 1 reply; 2+ messages in thread
From: Tze-nan Wu @ 2024-07-10 9:19 UTC (permalink / raw)
To: rostedt
Cc: cheng-jui.wang, bobule.chang, eric-yk.wu, wsd_upstream,
Tze-nan Wu, Mathieu Desnoyers, linux-kernel, linux-trace-kernel,
linux-arm-kernel, linux-mediatek
"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".
Co-developed-by: Cheng-Jui Wang <cheng-jui.wang@mediatek.com>
Signed-off-by: Cheng-Jui Wang <cheng-jui.wang@mediatek.com>
Signed-off-by: Tze-nan Wu <Tze-nan.Wu@mediatek.com>
---
kernel/trace/tracing_map.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/kernel/trace/tracing_map.c b/kernel/trace/tracing_map.c
index a4dcf0f24352..3a56e7c8aa4f 100644
--- a/kernel/trace/tracing_map.c
+++ b/kernel/trace/tracing_map.c
@@ -454,7 +454,7 @@ static struct tracing_map_elt *get_free_elt(struct tracing_map *map)
struct tracing_map_elt *elt = NULL;
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) {
elt = *(TRACING_MAP_ELT(map->elts, idx));
if (map->ops && map->ops->elt_init)
@@ -699,7 +699,7 @@ void tracing_map_clear(struct tracing_map *map)
{
unsigned int i;
- atomic_set(&map->next_elt, -1);
+ atomic_set(&map->next_elt, 0);
atomic64_set(&map->hits, 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->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->ops = ops;
--
2.18.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] tracing: Fix overflow in get_free_elt()
2024-07-10 9:19 [PATCH] tracing: Fix overflow in get_free_elt() Tze-nan Wu
@ 2024-07-22 5:55 ` Tze-nan Wu (吳澤南)
0 siblings, 0 replies; 2+ messages in thread
From: Tze-nan Wu (吳澤南) @ 2024-07-22 5:55 UTC (permalink / raw)
To: rostedt@goodmis.org
Cc: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
linux-mediatek@lists.infradead.org,
Cheng-Jui Wang (王正睿),
Eric-YK Wu (吳育葵), wsd_upstream,
Bobule Chang (張弘義),
linux-arm-kernel@lists.infradead.org,
mathieu.desnoyers@efficios.com
On Wed, 2024-07-10 at 17:19 +0800, Tze-nan Wu wrote:
> "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".
>
> Co-developed-by: Cheng-Jui Wang <cheng-jui.wang@mediatek.com>
> Signed-off-by: Cheng-Jui Wang <cheng-jui.wang@mediatek.com>
> Signed-off-by: Tze-nan Wu <Tze-nan.Wu@mediatek.com>
> ---
> kernel/trace/tracing_map.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
Just a gentle ping. Any comments on this patch will be appreciated.
Actually we have encountered this issue internally after enabling the
throttle_rss_stat feature in Perfetto for an extended duration, during
which the rss_stat tracepoint was invoked over 2^32 times.
Then the CPU could hang in function "__tracing_map_insert()" after the
tracing_map left no empty entry.
throttle_rss_stat is literally:
1. $echo "rss_stat_throttled unsigned int mm_id unsigned int curr int
member long size" >> /sys/kernel/tracing/synthetic_events
2. $echo
'hist:keys=mm_id,member:bucket=size/0x80000:onchange($bucket).rss_stat_
throttled(mm_id,curr,member,size)' >
/sys/kernel/tracing/events/kmem/rss_stat/trigger
> diff --git a/kernel/trace/tracing_map.c b/kernel/trace/tracing_map.c
> index a4dcf0f24352..3a56e7c8aa4f 100644
> --- a/kernel/trace/tracing_map.c
> +++ b/kernel/trace/tracing_map.c
> @@ -454,7 +454,7 @@ static struct tracing_map_elt
> *get_free_elt(struct tracing_map *map)
> struct tracing_map_elt *elt = NULL;
> 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) {
> elt = *(TRACING_MAP_ELT(map->elts, idx));
> if (map->ops && map->ops->elt_init)
> @@ -699,7 +699,7 @@ void tracing_map_clear(struct tracing_map *map)
> {
> unsigned int i;
>
> - atomic_set(&map->next_elt, -1);
> + atomic_set(&map->next_elt, 0);
> atomic64_set(&map->hits, 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->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->ops = ops;
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-07-22 5:55 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-10 9:19 [PATCH] tracing: Fix overflow in get_free_elt() Tze-nan Wu
2024-07-22 5:55 ` Tze-nan Wu (吳澤南)
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).