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

perf thread: Fixes to thread__new() related to initializing comm

Freeing the thread on failure won't work with reference count checking,
use thread__delete().

Don't allocate the comm_str, use a stack allocation instead.

Fixes: f6005cafebab72f8 ("perf thread: Add reference count checking")
Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linux.dev>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240508035301.1554434-5-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
3536c257 45b4f402

+5 -9
+5 -9
tools/perf/util/thread.c
··· 39 39 40 40 struct thread *thread__new(pid_t pid, pid_t tid) 41 41 { 42 - char *comm_str; 43 - struct comm *comm; 44 42 RC_STRUCT(thread) *_thread = zalloc(sizeof(*_thread)); 45 43 struct thread *thread; 46 44 47 45 if (ADD_RC_CHK(thread, _thread) != NULL) { 46 + struct comm *comm; 47 + char comm_str[32]; 48 + 48 49 thread__set_pid(thread, pid); 49 50 thread__set_tid(thread, tid); 50 51 thread__set_ppid(thread, -1); ··· 57 56 init_rwsem(thread__namespaces_lock(thread)); 58 57 init_rwsem(thread__comm_lock(thread)); 59 58 60 - comm_str = malloc(32); 61 - if (!comm_str) 62 - goto err_thread; 63 - 64 - snprintf(comm_str, 32, ":%d", tid); 59 + snprintf(comm_str, sizeof(comm_str), ":%d", tid); 65 60 comm = comm__new(comm_str, 0, false); 66 - free(comm_str); 67 61 if (!comm) 68 62 goto err_thread; 69 63 ··· 72 76 return thread; 73 77 74 78 err_thread: 75 - free(thread); 79 + thread__delete(thread); 76 80 return NULL; 77 81 } 78 82