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

perf probe: Avoid calling freeing routine multiple times for same pointer

When perf_add_probe_events() we call cleanup_perf_probe_events() for the
pev pointer it receives, then, as part of handling this failure the main
'perf probe' goes on and calls cleanup_params() and that will again call
cleanup_perf_probe_events()for the same pointer, so just set nevents to
zero when handling the failure of perf_add_probe_events() to avoid the
double free.

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: https://lkml.kernel.org/n/tip-x8qgma4g813z96dvtw9w219q@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

+10
+10
tools/perf/builtin-probe.c
··· 698 698 699 699 ret = perf_add_probe_events(params.events, params.nevents); 700 700 if (ret < 0) { 701 + 702 + /* 703 + * When perf_add_probe_events() fails it calls 704 + * cleanup_perf_probe_events(pevs, npevs), i.e. 705 + * cleanup_perf_probe_events(params.events, params.nevents), which 706 + * will call clear_perf_probe_event(), so set nevents to zero 707 + * to avoid cleanup_params() to call clear_perf_probe_event() again 708 + * on the same pevs. 709 + */ 710 + params.nevents = 0; 701 711 pr_err_with_code(" Error: Failed to add events.", ret); 702 712 return ret; 703 713 }