Skip to contentSkip to conversation

drm/i915/gt: Fix request use-after-free in heartbeat()

PATCH In review Shuangpeng Bai 6 participants 8 messages Aug 8 – Aug 31

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/i915/gt: Fix request use-after-free in heartbeat()

heartbeat() detaches engine->heartbeat.systole with xchg() before checking whether the request has completed. If the request is complete, dropping the detached systole reference may free it. Concurrently, idle_pulse() can observe the empty slot and publish a newer request.

The worker then sees a non-NULL engine->heartbeat.systole, but continues to dereference its stale local rq. The newer request can therefore make the shared-slot check succeed after the old request has been freed, causing use-after-free accesses to emitted_jiffies, submit, and sched.

If the detached request is incomplete, heartbeat() can also overwrite a newer request published by idle_pulse() when it unconditionally restores the old pointer.

Use cmpxchg() both when idle_pulse() publishes a request and when heartbeat() restores the detached request. Drop the detached reference when restoration loses the race, and reload rq from the shared slot before dereferencing it.

A KASAN-enabled i915 mock selftest reproduces the use-after-free before this change and completes without a report after the fix.

Fixes bf9bd6a5128a ("drm/i915/gt: Track the most recent pulse for the heartbeat")
Cc [email protected] # v5.11+
---
 .../gpu/drm/i915/gt/intel_engine_heartbeat.c  | 22 ++++++++++++++-----
 1 file changed, 16 insertions(+), 6 deletions(-)
drivers/gpu/drm/i915/gt/intel_engine_heartbeat.c +16 -6
index 6424ecce8bcb..fc6c53c93cf5 100644
@@ -81,10 +81,18 @@ heartbeat_create(struct intel_context *ce, gfp_t gfp)
 
 static void idle_pulse(struct intel_engine_cs *engine, struct i915_request *rq)
 {
+	struct i915_request *systole;
+
 	engine->wakeref_serial = READ_ONCE(engine->serial) + 1;
 	i915_request_add_active_barriers(rq);
-	if (!engine->heartbeat.systole && intel_engine_has_heartbeat(engine))
-		engine->heartbeat.systole = i915_request_get(rq);
+	if (READ_ONCE(engine->heartbeat.systole) ||
+	    !intel_engine_has_heartbeat(engine))
+		return;
+
+	systole = i915_request_get(rq);
+	/* The worker may have restored its detached systole in the meantime. */
+	if (cmpxchg(&engine->heartbeat.systole, NULL, systole))
+		i915_request_put(systole);
 }
 
 static void heartbeat_commit(struct i915_request *rq,
@@ -152,9 +160,11 @@ static void heartbeat(struct work_struct *wrk)
 	if (rq) {
 		if (i915_request_completed(rq))
 			i915_request_put(rq);
-		else
-			engine->heartbeat.systole = rq;
+		/* Keep a newer pulse that raced with the detached systole. */
+		else if (cmpxchg(&engine->heartbeat.systole, NULL, rq))
+			i915_request_put(rq);
 	}
+	rq = READ_ONCE(engine->heartbeat.systole);
 
 	if (!intel_engine_pm_get_if_awake(engine))
 		return;
@@ -163,11 +173,11 @@ static void heartbeat(struct work_struct *wrk)
 		goto out;
 
 	if (i915_sched_engine_disabled(engine->sched_engine)) {
-		reset_engine(engine, engine->heartbeat.systole);
+		reset_engine(engine, rq);
 		goto out;
 	}
 
-	if (engine->heartbeat.systole) {
+	if (rq) {
 		long delay = READ_ONCE(engine->props.heartbeat_interval_ms);
 
 		/* Safeguard against too-fast worker invocations */

base-commit: a59f57e2aa127c5354168d2ec4bac920df1be4f4

Signature · 1 line
2.43.0
#1 lore raw

✗ LGCI.VerificationFailed: failure for drm/i915/gt: Fix request use-after-free in heartbeat()

== Series Details ==

== Series Details ==

Series: drm/i915/gt: Fix request use-after-free in heartbeat()
URL : https://patchwork.freedesktop.org/series/171942/
State : failure

== Summary ==

Series author address '[email protected]' is not on the allowlist, which prevents CI from being automatically triggered. If you want CI to run for this series, ask Patchwork project owners to click 'retest' on the series in Patchwork. Exception occurred during validation, bailing out!
Build URL: http://gfx-ci.igk.intel.com:8080/job/CI_PW_kernel/183928/ (on built-in)

#2 lore raw
In reply to Shuangpeng Bai

Hi Shuangpeng,

Quoted 14 lines · Shuangpeng Bai

heartbeat() detaches engine->heartbeat.systole with xchg() before checking whether the request has completed. If the request is complete, dropping the detached systole reference may free it. Concurrently, idle_pulse() can observe the empty slot and publish a newer request.

The worker then sees a non-NULL engine->heartbeat.systole, but continues to dereference its stale local rq. The newer request can therefore make the shared-slot check succeed after the old request has been freed, causing use-after-free accesses to emitted_jiffies, submit, and sched.

If the detached request is incomplete, heartbeat() can also overwrite a newer request published by idle_pulse() when it unconditionally restores the old pointer.

Quoted 7 lines

Use cmpxchg() both when idle_pulse() publishes a request and when heartbeat() restores the detached request. Drop the detached reference when restoration loses the race, and reload rq from the shared slot before dereferencing it.

A KASAN-enabled i915 mock selftest reproduces the use-after-free before this change and completes without a report after the fix.

What is this mock selftest? Can I see it somewhere and confirm if it works myself? A link to a public issue would be cool too, something that gives more context as to how this was discovered.

Quoted 62 lines
Fixes bf9bd6a5128a ("drm/i915/gt: Track the most recent pulse for the heartbeat")
Cc [email protected] # v5.11+
---
 .../gpu/drm/i915/gt/intel_engine_heartbeat.c  | 22 ++++++++++++++-----
 1 file changed, 16 insertions(+), 6 deletions(-)
drivers/gpu/drm/i915/gt/intel_engine_heartbeat.c +16 -6
index 6424ecce8bcb..fc6c53c93cf5 100644
@@ -81,10 +81,18 @@ heartbeat_create(struct intel_context *ce, gfp_t gfp)
 
 static void idle_pulse(struct intel_engine_cs *engine, struct i915_request *rq)
 {
+	struct i915_request *systole;
+
 	engine->wakeref_serial = READ_ONCE(engine->serial) + 1;
 	i915_request_add_active_barriers(rq);
-	if (!engine->heartbeat.systole && intel_engine_has_heartbeat(engine))
-		engine->heartbeat.systole = i915_request_get(rq);
+	if (READ_ONCE(engine->heartbeat.systole) ||
+	    !intel_engine_has_heartbeat(engine))
+		return;
+
+	systole = i915_request_get(rq);
+	/* The worker may have restored its detached systole in the meantime. */
+	if (cmpxchg(&engine->heartbeat.systole, NULL, systole))
+		i915_request_put(systole);
 }
 
 static void heartbeat_commit(struct i915_request *rq,
@@ -152,9 +160,11 @@ static void heartbeat(struct work_struct *wrk)
 	if (rq) {
 		if (i915_request_completed(rq))
 			i915_request_put(rq);
-		else
-			engine->heartbeat.systole = rq;
+		/* Keep a newer pulse that raced with the detached systole. */
+		else if (cmpxchg(&engine->heartbeat.systole, NULL, rq))
+			i915_request_put(rq);
 	}
+	rq = READ_ONCE(engine->heartbeat.systole);
 
 	if (!intel_engine_pm_get_if_awake(engine))
 		return;
@@ -163,11 +173,11 @@ static void heartbeat(struct work_struct *wrk)
 		goto out;
 
 	if (i915_sched_engine_disabled(engine->sched_engine)) {
-		reset_engine(engine, engine->heartbeat.systole);
+		reset_engine(engine, rq);
 		goto out;
 	}
 
-	if (engine->heartbeat.systole) {
+	if (rq) {
 		long delay = READ_ONCE(engine->props.heartbeat_interval_ms);
 
 		/* Safeguard against too-fast worker invocations */
Quoted 4 lines

base-commit: a59f57e2aa127c5354168d2ec4bac920df1be4f4

Signature · 1 line
2.43.0

The fix looks sane at first glance to me, but I'd like to see how it was spotted before diving in further.

Signature · 2 lines
Best Regards,
Krzysztof
author
#3 lore raw
Quoted 24 lines · Krzysztof Karas

Hi Shuangpeng,

Quoted 19 lines · Shuangpeng Bai

heartbeat() detaches engine->heartbeat.systole with xchg() before checking whether the request has completed. If the request is complete, dropping the detached systole reference may free it. Concurrently, idle_pulse() can observe the empty slot and publish a newer request.

The worker then sees a non-NULL engine->heartbeat.systole, but continues to dereference its stale local rq. The newer request can therefore make the shared-slot check succeed after the old request has been freed, causing use-after-free accesses to emitted_jiffies, submit, and sched.

If the detached request is incomplete, heartbeat() can also overwrite a newer request published by idle_pulse() when it unconditionally restores the old pointer.

Use cmpxchg() both when idle_pulse() publishes a request and when heartbeat() restores the detached request. Drop the detached reference when restoration loses the race, and reload rq from the shared slot before dereferencing it.

Quoted 6 lines
Quoted 2 lines

A KASAN-enabled i915 mock selftest reproduces the use-after-free before this change and completes without a report after the fix.

What is this mock selftest? Can I see it somewhere and confirm if it works myself? A link to a public issue would be cool too, something that gives more context as to how this was discovered.

Hi Krzysztof,

I've put the reproducer here:

https://gist.github.com/shuangpengbai/1410664bc909e8c834311576d6242103

Tested on commit a59f57e2aa127c5354168d2ec4bac920df1be4f4 (2026-08-07).

It uses i915's mock selftest path, so no Intel GPU hardware is required. Please apply 1_repro.patch, build with 2_config, and boot with:
i915.mock_selftests=1 i915.igt__36__mock_heartbeat_uaf=1

Expected result:

BUG: KASAN: slab-use-after-free in heartbeat

Best,
Shuangpeng

Quoted 73 lines
Quoted 62 lines
Fixes bf9bd6a5128a ("drm/i915/gt: Track the most recent pulse for the heartbeat")
Cc [email protected] # v5.11+

---
.../gpu/drm/i915/gt/intel_engine_heartbeat.c | 22 ++++++++++++++-----

1 file changed, 16 insertions(+), 6 deletions(-)
drivers/gpu/drm/i915/gt/intel_engine_heartbeat.c +16 -6
index 6424ecce8bcb..fc6c53c93cf5 100644
@@ -81,10 +81,18 @@ heartbeat_create(struct intel_context *ce, gfp_t gfp)
 
 static void idle_pulse(struct intel_engine_cs *engine, struct i915_request *rq)
 {
+ struct i915_request *systole;
+
 engine->wakeref_serial = READ_ONCE(engine->serial) + 1;
 i915_request_add_active_barriers(rq);
- if (!engine->heartbeat.systole && intel_engine_has_heartbeat(engine))
- engine->heartbeat.systole = i915_request_get(rq);
+ if (READ_ONCE(engine->heartbeat.systole) ||
+     !intel_engine_has_heartbeat(engine))
+ return;
+
+ systole = i915_request_get(rq);
+ /* The worker may have restored its detached systole in the meantime. */
+ if (cmpxchg(&engine->heartbeat.systole, NULL, systole))
+ i915_request_put(systole);
 }
 
 static void heartbeat_commit(struct i915_request *rq,
@@ -152,9 +160,11 @@ static void heartbeat(struct work_struct *wrk)
 if (rq) {
 if (i915_request_completed(rq))
 i915_request_put(rq);
- else
- engine->heartbeat.systole = rq;
+ /* Keep a newer pulse that raced with the detached systole. */
+ else if (cmpxchg(&engine->heartbeat.systole, NULL, rq))
+ i915_request_put(rq);
 }
+ rq = READ_ONCE(engine->heartbeat.systole);
 
 if (!intel_engine_pm_get_if_awake(engine))
 return;
@@ -163,11 +173,11 @@ static void heartbeat(struct work_struct *wrk)
 goto out;
 
 if (i915_sched_engine_disabled(engine->sched_engine)) {
- reset_engine(engine, engine->heartbeat.systole);
+ reset_engine(engine, rq);
 goto out;
 }
 
- if (engine->heartbeat.systole) {
+ if (rq) {
 long delay = READ_ONCE(engine->props.heartbeat_interval_ms);
 
 /* Safeguard against too-fast worker invocations */
Quoted 4 lines

base-commit: a59f57e2aa127c5354168d2ec4bac920df1be4f4

Signature · 1 line
2.43.0

The fix looks sane at first glance to me, but I'd like to see how it was spotted before diving in further.

Signature · 2 lines
Best Regards,
Krzysztof
Reviewed-by on v1 1/1
#4 lore raw
In reply to Shuangpeng Bai

Hi Shuangpeng,

Quoted 19 lines · Shuangpeng Bai

heartbeat() detaches engine->heartbeat.systole with xchg() before checking whether the request has completed. If the request is complete, dropping the detached systole reference may free it. Concurrently, idle_pulse() can observe the empty slot and publish a newer request.

The worker then sees a non-NULL engine->heartbeat.systole, but continues to dereference its stale local rq. The newer request can therefore make the shared-slot check succeed after the old request has been freed, causing use-after-free accesses to emitted_jiffies, submit, and sched.

If the detached request is incomplete, heartbeat() can also overwrite a newer request published by idle_pulse() when it unconditionally restores the old pointer.

Use cmpxchg() both when idle_pulse() publishes a request and when heartbeat() restores the detached request. Drop the detached reference when restoration loses the race, and reload rq from the shared slot before dereferencing it.

Quoted 5 lines

A KASAN-enabled i915 mock selftest reproduces the use-after-free before this change and completes without a report after the fix.

Fixes bf9bd6a5128a ("drm/i915/gt: Track the most recent pulse for the heartbeat")
Cc [email protected] # v5.11+

Your patch looks correct, although I don't think the Fixes tag is. I believe the actual fix is:

Fixes 4c71fd099513 ("drm/i915/gt: fix refcount underflow in intel_engine_park_heartbeat")
Cc <[email protected]> # v7.0+

That commit introduced the early xchg() which hides the old request from idle_pulse() and replaces it with a new one.

I'm going to resend the patch so that it can be picked up by our CI.

Thanks,
Andi

author
#5 lore raw

Hi Andi,

Quoted 33 lines · Andi Shyti

Hi Shuangpeng,

Quoted 19 lines · Shuangpeng Bai

heartbeat() detaches engine->heartbeat.systole with xchg() before checking whether the request has completed. If the request is complete, dropping the detached systole reference may free it. Concurrently, idle_pulse() can observe the empty slot and publish a newer request.

The worker then sees a non-NULL engine->heartbeat.systole, but continues to dereference its stale local rq. The newer request can therefore make the shared-slot check succeed after the old request has been freed, causing use-after-free accesses to emitted_jiffies, submit, and sched.

If the detached request is incomplete, heartbeat() can also overwrite a newer request published by idle_pulse() when it unconditionally restores the old pointer.

Use cmpxchg() both when idle_pulse() publishes a request and when heartbeat() restores the detached request. Drop the detached reference when restoration loses the race, and reload rq from the shared slot before dereferencing it.

Quoted 5 lines

A KASAN-enabled i915 mock selftest reproduces the use-after-free before this change and completes without a report after the fix.

Fixes bf9bd6a5128a ("drm/i915/gt: Track the most recent pulse for the heartbeat")
Cc [email protected] # v5.11+
Quoted 9 lines

Your patch looks correct, although I don't think the Fixes tag is. I believe the actual fix is:

Fixes 4c71fd099513 ("drm/i915/gt: fix refcount underflow in intel_engine_park_heartbeat")
Cc <[email protected]> # v7.0+

That commit introduced the early xchg() which hides the old request from idle_pulse() and replaces it with a new one.

Thanks for the review and for correcting the Fixes tag.

Quoted 3 lines

I'm going to resend the patch so that it can be picked up by our CI.

Thanks for resending it.

Quoted 2 lines

Thanks,
Andi

Best,
Shuangpeng

maintainer
#6 lore raw

Quoting Shuangpeng (2026-08-31 03:13:28)

Quoted 29 lines

Hi Andi,

Quoted 26 lines · Andi Shyti

Hi Shuangpeng,

Quoted 21 lines · Shuangpeng Bai

heartbeat() detaches engine->heartbeat.systole with xchg() before checking whether the request has completed. If the request is complete, dropping the detached systole reference may free it. Concurrently, idle_pulse() can observe the empty slot and publish a newer request.

The worker then sees a non-NULL engine->heartbeat.systole, but continues to dereference its stale local rq. The newer request can therefore make the shared-slot check succeed after the old request has been freed, causing use-after-free accesses to emitted_jiffies, submit, and sched.

If the detached request is incomplete, heartbeat() can also overwrite a newer request published by idle_pulse() when it unconditionally restores the old pointer.

Use cmpxchg() both when idle_pulse() publishes a request and when heartbeat() restores the detached request. Drop the detached reference when restoration loses the race, and reload rq from the shared slot before dereferencing it.

A KASAN-enabled i915 mock selftest reproduces the use-after-free before this change and completes without a report after the fix.

The test should be properly submitted and reviewed first before we even consider merging this, especially with such bad commit message. The commit message doesn't mention a word about real world usage scenario that could lead to this.

There are probably hundreds of ways to cause kernel to crash by crafting selftests, and if they don't map to any real real hardware usage behavior, that's just noise.

Regards, Joonas

maintainer
#7 lore raw
In reply to Andi Shyti
Quoted 1 line · Andi Shyti

I'm going to resend the patch so that it can be picked up by our CI.

Nowadays, there's no need to do that. It's possible to request retest in patchwork.

BR,
Jani.

Signature · 1 line
Jani Nikula, Intel