Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf record: Fix bug mismatch with -c option definition

The -c option defines the user requested sampling period. It was implemented
using an unsigned int variable but the type of the option was OPT_LONG. Thus,
the option parser was overwriting memory belonging to other variables, namely
the mmap_pages leading to a zero page sampling buffer. The bug was exposed only
when compiling at -O0, probably because the compiler was padding variables at
higher optimization levels.

This patch fixes this problem by declaring user_interval as u64. This also
avoids wrap-around issues for large period on 32-bit systems.

Commiter note:

Made it use OPT_U64(user_interval) after implementing OPT_U64 in the
previous patch.

Cc: David S. Miller <davem@davemloft.net>
Cc: Frédéric Weisbecker <fweisbec@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <peterz@infradead.org>
LKML-Reference: <4bf11ae9.e88cd80a.06b0.ffffa8e3@mx.google.com>
Signed-off-by: Stephane Eranian <eranian@google.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Stephane Eranian and committed by
Arnaldo Carvalho de Melo
3de29cab 6ba85cea

+5 -6
+5 -6
tools/perf/builtin-record.c
··· 33 33 34 34 static int *fd[MAX_NR_CPUS][MAX_COUNTERS]; 35 35 36 - static unsigned int user_interval = UINT_MAX; 37 - static long default_interval = 0; 36 + static u64 user_interval = ULLONG_MAX; 37 + static u64 default_interval = 0; 38 38 39 39 static int nr_cpus = 0; 40 40 static unsigned int page_size; ··· 268 268 * it a weak assumption overridable by the user. 269 269 */ 270 270 if (!attr->sample_period || (user_freq != UINT_MAX && 271 - user_interval != UINT_MAX)) { 271 + user_interval != ULLONG_MAX)) { 272 272 if (freq) { 273 273 attr->sample_type |= PERF_SAMPLE_PERIOD; 274 274 attr->freq = 1; ··· 817 817 "CPU to profile on"), 818 818 OPT_BOOLEAN('f', "force", &force, 819 819 "overwrite existing data file (deprecated)"), 820 - OPT_LONG('c', "count", &user_interval, 821 - "event period to sample"), 820 + OPT_U64('c', "count", &user_interval, "event period to sample"), 822 821 OPT_STRING('o', "output", &output_name, "file", 823 822 "output file name"), 824 823 OPT_BOOLEAN('i', "no-inherit", &no_inherit, ··· 900 901 if (!event_array) 901 902 return -ENOMEM; 902 903 903 - if (user_interval != UINT_MAX) 904 + if (user_interval != ULLONG_MAX) 904 905 default_interval = user_interval; 905 906 if (user_freq != UINT_MAX) 906 907 freq = user_freq;