Skip to content

Commit 8c5c48b

Browse files
Sai Prakash Ranjangregkh
authored andcommitted
coresight: tmc-etf: Fix NULL ptr dereference in tmc_enable_etf_sink_perf()
commit 868663d upstream. There was a report of NULL pointer dereference in ETF enable path for perf CS mode with PID monitoring. It is almost 100% reproducible when the process to monitor is something very active such as chrome and with ETF as the sink and not ETR. Currently in a bid to find the pid, the owner is dereferenced via task_pid_nr() call in tmc_enable_etf_sink_perf() and with owner being NULL, we get a NULL pointer dereference. Looking at the ETR and other places in the kernel, ETF and the ETB are the only places trying to dereference the task(owner) in tmc_enable_etf_sink_perf() which is also called from the sched_in path as in the call trace. Owner(task) is NULL even in the case of ETR in tmc_enable_etr_sink_perf(), but since we cache the PID in alloc_buffer() callback and it is done as part of etm_setup_aux() when allocating buffer for ETR sink, we never dereference this NULL pointer and we are safe. So lets do the same thing with ETF and cache the PID to which the cs_buffer belongs in tmc_alloc_etf_buffer() as done for ETR. This will also remove the unnecessary function calls(task_pid_nr()) since we are caching the PID. Easily reproducible running below: perf record -e cs_etm/@tmc_etf0/ -N -p <pid> Unable to handle kernel NULL pointer dereference at virtual address 0000000000000548 Mem abort info: ESR = 0x96000006 EC = 0x25: DABT (current EL), IL = 32 bits SET = 0, FnV = 0 EA = 0, S1PTW = 0 Data abort info: ISV = 0, ISS = 0x00000006 CM = 0, WnR = 0 <snip>... Call trace: tmc_enable_etf_sink+0xe4/0x280 coresight_enable_path+0x168/0x1fc etm_event_start+0x8c/0xf8 etm_event_add+0x38/0x54 event_sched_in+0x194/0x2ac group_sched_in+0x54/0x12c flexible_sched_in+0xd8/0x120 visit_groups_merge+0x100/0x16c ctx_flexible_sched_in+0x50/0x74 ctx_sched_in+0xa4/0xa8 perf_event_sched_in+0x60/0x6c perf_event_context_sched_in+0x98/0xe0 __perf_event_task_sched_in+0x5c/0xd8 finish_task_switch+0x184/0x1cc schedule_tail+0x20/0xec ret_from_fork+0x4/0x18 Fixes: 880af78 ("coresight: tmc-etf: Add support for CPU-wide trace scenarios") Cc: stable@vger.kernel.org Signed-off-by: Sai Prakash Ranjan <saiprakash.ranjan@codeaurora.org> Signed-off-by: Mathieu Poirier <mathieu.poirier@linaro.org> Link: https://lore.kernel.org/r/20201127175256.1092685-10-mathieu.poirier@linaro.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 61bed86 commit 8c5c48b

2 files changed

Lines changed: 5 additions & 1 deletion

File tree

‎drivers/hwtracing/coresight/coresight-priv.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ enum cs_mode {
8787
* struct cs_buffer - keep track of a recording session' specifics
8888
* @cur: index of the current buffer
8989
* @nr_pages: max number of pages granted to us
90+
* @pid: PID this cs_buffer belongs to
9091
* @offset: offset within the current buffer
9192
* @data_size: how much we collected in this run
9293
* @snapshot: is this run in snapshot mode
@@ -95,6 +96,7 @@ enum cs_mode {
9596
struct cs_buffers {
9697
unsigned int cur;
9798
unsigned int nr_pages;
99+
pid_t pid;
98100
unsigned long offset;
99101
local_t data_size;
100102
bool snapshot;

‎drivers/hwtracing/coresight/coresight-tmc-etf.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ static int tmc_enable_etf_sink_perf(struct coresight_device *csdev, void *data)
227227
unsigned long flags;
228228
struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
229229
struct perf_output_handle *handle = data;
230+
struct cs_buffers *buf = etm_perf_sink_config(handle);
230231

231232
spin_lock_irqsave(&drvdata->spinlock, flags);
232233
do {
@@ -243,7 +244,7 @@ static int tmc_enable_etf_sink_perf(struct coresight_device *csdev, void *data)
243244
}
244245

245246
/* Get a handle on the pid of the process to monitor */
246-
pid = task_pid_nr(handle->event->owner);
247+
pid = buf->pid;
247248

248249
if (drvdata->pid != -1 && drvdata->pid != pid) {
249250
ret = -EBUSY;
@@ -399,6 +400,7 @@ static void *tmc_alloc_etf_buffer(struct coresight_device *csdev,
399400
if (!buf)
400401
return NULL;
401402

403+
buf->pid = task_pid_nr(event->owner);
402404
buf->snapshot = overwrite;
403405
buf->nr_pages = nr_pages;
404406
buf->data_pages = pages;

0 commit comments

Comments
 (0)