Skip to contentSkip to conversation

coresight: etm-perf: Fix reference count leak in etm_setup_aux

PATCH Superseded Ma Ke 1 participant 1 message May 19

Subsystems (3)
lore
Share & tools
lore

Ma Ke

author
#0 lore raw

PATCH v3 1/1 coresight: etm-perf: Fix reference count leak in etm_setup_aux

bus_find_device() returns a device with its reference count incremented. When a user-selected sink is obtained through coresight_get_sink_by_id(), etm_setup_aux() keeps using the returned sink while building the path and allocating the sink buffer.

Therefore the lookup reference must remain valid while etm_setup_aux() is still using the sink, otherwise the sink could be removed under the caller. Drop the lookup reference on the common exit path, after etm_setup_aux() no longer directly uses the user-selected sink.

The CoreSight path code takes the references it needs for built paths, so the initial lookup reference from coresight_get_sink_by_id() is no longer needed after setup_aux finishes.

Found by code review.

Fixes 0e6c20517596 ("coresight: etm-perf: Allow an event to use different sinks")

---
Changes in v3:
- do not drop the lookup reference in coresight_get_sink_by_id(), as that would return a sink pointer without keeping the device reference while etm_setup_aux() is still using it.
- dropped the lookup reference in etm_setup_aux on the common exit path, as suggested by Suzuki.
- updated the commit message to describe why the reference is kept until etm_setup_aux() finishes using the sink.
Changes in v2:
- modified the patch as suggestions.

---
 drivers/hwtracing/coresight/coresight-etm-perf.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/hwtracing/coresight/coresight-etm-perf.c +5 -0
index f85dedf89a3f..d5116177c1b9 100644
@@ -456,6 +456,11 @@ static void *etm_setup_aux(struct perf_event *event, void **pages,
 		goto err;
 
 out:
+	if (user_sink) {
+		put_device(&user_sink->dev);
+		user_sink = NULL;
+	}
+
 	return event_data;
 
 err:
Signature · 1 line
2.43.0