Skip to content

Commit e5cde57

Browse files
tzanussigregkh
authored andcommitted
tracing: Use strncpy instead of memcpy for string keys in hist triggers
commit 9f0bbf3 upstream. Because there may be random garbage beyond a string's null terminator, it's not correct to copy the the complete character array for use as a hist trigger key. This results in multiple histogram entries for the 'same' string key. So, in the case of a string key, use strncpy instead of memcpy to avoid copying in the extra bytes. Before, using the gdbus entries in the following hist trigger as an example: # echo 'hist:key=comm' > /sys/kernel/debug/tracing/events/sched/sched_waking/trigger # cat /sys/kernel/debug/tracing/events/sched/sched_waking/hist ... { comm: ImgDecoder hardkernel#4 } hitcount: 203 { comm: gmain } hitcount: 213 { comm: gmain } hitcount: 216 { comm: StreamTrans hardkernel#73 } hitcount: 221 { comm: mozStorage hardkernel#3 } hitcount: 230 { comm: gdbus } hitcount: 233 { comm: StyleThread#5 } hitcount: 253 { comm: gdbus } hitcount: 256 { comm: gdbus } hitcount: 260 { comm: StyleThread#4 } hitcount: 271 ... # cat /sys/kernel/debug/tracing/events/sched/sched_waking/hist | egrep gdbus | wc -l 51 After: # cat /sys/kernel/debug/tracing/events/sched/sched_waking/hist | egrep gdbus | wc -l 1 Link: http://lkml.kernel.org/r/50c35ae1267d64eee975b8125e151e600071d4dc.1549309756.git.tom.zanussi@linux.intel.com Cc: Namhyung Kim <[email protected]> Cc: [email protected] Fixes: 79e577c ("tracing: Support string type key properly") Signed-off-by: Tom Zanussi <[email protected]> Signed-off-by: Steven Rostedt (VMware) <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent c892f4e commit e5cde57

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

kernel/trace/trace_events_hist.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4695,9 +4695,10 @@ static inline void add_to_key(char *compound_key, void *key,
46954695
/* ensure NULL-termination */
46964696
if (size > key_field->size - 1)
46974697
size = key_field->size - 1;
4698-
}
46994698

4700-
memcpy(compound_key + key_field->offset, key, size);
4699+
strncpy(compound_key + key_field->offset, (char *)key, size);
4700+
} else
4701+
memcpy(compound_key + key_field->offset, key, size);
47014702
}
47024703

47034704
static void

0 commit comments

Comments
 (0)