Skip to contentSkip to conversation

drm/exynos: queue the IPP cleanup work before waking the caller

RFC PATCH New Yaohan Li 2 participants 3 messages Nov 19

Subsystems (3)
lore
Share & tools
lore

Yaohan Li

author
#0 lore raw

RFC PATCH 1/1 drm/exynos: queue the IPP cleanup work before waking the caller

exynos_drm_ipp_task_done() marks the task done, wakes the waiters and only then decides whether the task has to be released by the cleanup work. A commit ioctl that waits for the task is woken by that wake_up() and frees the task itself as soon as it sees the DONE flag, so the task->flags read that follows touches memory that may already be gone.

Read the flag while the task is still guaranteed to be alive, together with the other task state under ipp->lock, and queue the cleanup work before the wakeup. A task that is handed to the work is then always queued while it still exists.

---
Prepared against v7.4-rc1-1969-g666539eecdf4.

Compile tested only: arm64 with aarch64-linux-gnu-gcc 11.4
and CONFIG_DRM_EXYNOS=m with the IPP drivers. The affected hardware was not available, so the teardown path was not exercised and no fault or race injection was run.

AI assistance: the analysis, the change and its review notes were prepared with Claude (Anthropic) as a coding assistant. The author reviewed the reasoning and takes responsibility for the content.

---
 drivers/gpu/drm/exynos/exynos_drm_ipp.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/gpu/drm/exynos/exynos_drm_ipp.c +10 -2
index 43bc4f63bb84..49ae8ce9b95a 100644
@@ -764,6 +764,7 @@ void exynos_drm_ipp_task_done(struct exynos_drm_ipp_task *task, int ret)
 {
 	struct exynos_drm_ipp *ipp = task->ipp;
 	unsigned long flags;
+	bool async;
 
 	DRM_DEV_DEBUG_DRIVER(task->dev, "ipp: %d, task %p done: %d\n",
 			     ipp->id, task, ret);
@@ -773,15 +774,22 @@ void exynos_drm_ipp_task_done(struct exynos_drm_ipp_task *task, int ret)
 		ipp->task = NULL;
 	task->flags |= DRM_EXYNOS_IPP_TASK_DONE;
 	task->ret = ret;
+	async = task->flags & DRM_EXYNOS_IPP_TASK_ASYNC;
 	spin_unlock_irqrestore(&ipp->lock, flags);
 
 	exynos_drm_ipp_next_task(ipp);
-	wake_up(&ipp->done_wq);
 
-	if (task->flags & DRM_EXYNOS_IPP_TASK_ASYNC) {
+	/*
+	 * Hand the task over to the cleanup work before a synchronous caller
+	 * is woken up: once wake_up() returns, that caller may have released
+	 * the task already.
+	 */
+	if (async) {
 		INIT_WORK(&task->cleanup_work, exynos_drm_ipp_cleanup_work);
 		schedule_work(&task->cleanup_work);
 	}
+
+	wake_up(&ipp->done_wq);
 }
 
 static void exynos_drm_ipp_next_task(struct exynos_drm_ipp *ipp)
Signature · 1 line
2.34.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] drm/exynos: early DONE flag in exynos_drm_ipp_task_done() races with waiter - [Low] drm/exynos…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] drm/exynos: early DONE flag in exynos_drm_ipp_task_done() races with waiter
- [Low] drm/exynos: inaccurate comment and changelog in exynos_drm_ipp_task_done()
- [Low] drm/exynos: missing Fixes trailer for exynos_drm_ipp_task_done() UAF fix
--

commit 358647e37f3b75ba1e0c967389989dcad2ba8a0e
Author: Yaohan Li <[email protected]>

Quoted 8 lines

drm/exynos: queue the IPP cleanup work before waking the caller

exynos_drm_ipp_task_done() marks the task done, wakes the waiters and only then decides whether the task has to be released by the cleanup work. A commit ioctl that waits for the task is woken by that wake_up() and frees the task itself as soon as it sees the DONE flag, so the task->flags read that follows touches memory that may already be gone.

Quoted 6 lines

Read the flag while the task is still guaranteed to be alive, together with the other task state under ipp->lock, and queue the cleanup work before the wakeup. A task that is handed to the work is then always queued while it still exists.

[Severity: Low]
Should this bug fix include a Fixes tag?

Fixes 9913f74fe157 ("drm/exynos: ipp: Add IPP v2 framework")
Quoted 13 lines
drivers/gpu/drm/exynos/exynos_drm_ipp.c +1 -0
index 43bc4f63bb845..49ae8ce9b95a6 100644
@@ -764,6 +764,7 @@ void exynos_drm_ipp_task_done(struct exynos_drm_ipp_task *task, int ret)
 {
 	struct exynos_drm_ipp *ipp = task->ipp;
 	unsigned long flags;
+	bool async;
 
 	DRM_DEV_DEBUG_DRIVER(task->dev, "ipp: %d, task %p done: %d\n",
 			     ipp->id, task, ret);

drivers/gpu/drm/exynos/exynos_drm_ipp.c void exynos_drm_ipp_task_done(struct exynos_drm_ipp_task *task, int ret) · line 778

Quoted 5 lines
drivers/gpu/drm/exynos/exynos_drm_ipp.c +1 -0
@@ -773,15 +774,22 @@ void exynos_drm_ipp_task_done(struct exynos_drm_ipp_task *task, int ret)
 		ipp->task = NULL;
 	task->flags |= DRM_EXYNOS_IPP_TASK_DONE;
 	task->ret = ret;
+	async = task->flags & DRM_EXYNOS_IPP_TASK_ASYNC;
 	spin_unlock_irqrestore(&ipp->lock, flags);

[Severity: High]
Can a synchronous waiter in exynos_drm_ipp_commit_ioctl() observe DRM_EXYNOS_IPP_TASK_DONE and free task before exynos_drm_ipp_task_done() finishes writing task->ret and reading DRM_EXYNOS_IPP_TASK_ASYNC?

Neither wait_event_interruptible() nor exynos_drm_ipp_task_cleanup() takes ipp->lock:

drivers/gpu/drm/exynos/exynos_drm_ipp.c:exynos_drm_ipp_commit_ioctl() {
    ...
		exynos_drm_ipp_schedule_task(ipp, task);
		ret = wait_event_interruptible(ipp->done_wq,
					task->flags & DRM_EXYNOS_IPP_TASK_DONE);
		if (ret)
			exynos_drm_ipp_task_abort(ipp, task);
		else
			ret = exynos_drm_ipp_task_cleanup(task);
    ...
}

Because wait_event_interruptible() checks task->flags locklessly before sleeping (or when woken by an earlier task on the shared ipp->done_wq), setting DRM_EXYNOS_IPP_TASK_DONE first allows the waiter to run exynos_drm_ipp_task_cleanup() concurrently:

CPU0 CPU1
exynos_drm_ipp_task_done() exynos_drm_ipp_commit_ioctl()
spin_lock_irqsave(&ipp->lock)

  task->flags |= ..._TASK_DONE;
                                    wait_event_interruptible()
                                      sees DRM_EXYNOS_IPP_TASK_DONE
                                    exynos_drm_ipp_task_cleanup(task)
                                      ret = task->ret;  <-- stale 0
                                      exynos_drm_ipp_task_free()
                                        kfree(task)

task->ret = ret;
async = task->flags & ...; <-- use-after-free

Writing task->ret and reading DRM_EXYNOS_IPP_TASK_ASYNC before setting DRM_EXYNOS_IPP_TASK_DONE (with release/acquire ordering or under ipp->lock on the reader side) would avoid this race.

Quoted 6 lines
drivers/gpu/drm/exynos/exynos_drm_ipp.c +2 -2
 	exynos_drm_ipp_next_task(ipp);
-	wake_up(&ipp->done_wq);
 
-	if (task->flags & DRM_EXYNOS_IPP_TASK_ASYNC) {
+	/*
+	 * Hand the task over to the cleanup work before a synchronous caller

drivers/gpu/drm/exynos/exynos_drm_ipp.c void exynos_drm_ipp_task_done(struct exynos_drm_ipp_task *task, int ret) · line 793

Quoted 10 lines
drivers/gpu/drm/exynos/exynos_drm_ipp.c +6 -0
+	 * is woken up: once wake_up() returns, that caller may have released
+	 * the task already.
+	 */
+	if (async) {
 		INIT_WORK(&task->cleanup_work, exynos_drm_ipp_cleanup_work);
 		schedule_work(&task->cleanup_work);
 	}
+
+	wake_up(&ipp->done_wq);
 }

[Severity: Low]
Is this comment (and the corresponding explanation in the commit message) accurate about why async is checked before wake_up()?

DRM_EXYNOS_IPP_TASK_ASYNC is only set for non-blocking commits (which do not wait on ipp->done_wq) or in exynos_drm_ipp_task_abort() after wait_event_interruptible() was already interrupted by a signal and handed ownership of the running task to cleanup_work. When async is true, no synchronous caller will free the task upon wake_up(); when a synchronous caller is waiting to free the task, async is false. What prevents the use-after-free is reading DRM_EXYNOS_IPP_TASK_ASYNC before the waiter can observe task completion, rather than queuing cleanup_work before wake_up().

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#2 lore raw

RFC PATCH v2 1/1 drm/exynos: publish the IPP task completion after the last task access

In reply to Yaohan Li

exynos_drm_ipp_task_done() sets DRM_EXYNOS_IPP_TASK_DONE before it writes task->ret and reads DRM_EXYNOS_IPP_TASK_ASYNC. A commit ioctl that waits for the task evaluates the DONE flag in wait_event_interruptible() without taking ipp->lock, and exynos_drm_ipp_task_cleanup() frees the task as soon as it observes it, so the completion path can still be writing and reading a task that the waiter has already released.

Settle task->ret and the asynchronous ownership first, and publish DONE last with release semantics; read the flag with acquire semantics in the waiter. The flag is the publication point: a waiter that observes it on another CPU also observes everything written before it.

The completion path does not touch the task after the flag is visible any more, except for the hand-over to the cleanup work, which only runs for tasks no synchronous caller can release: DRM_EXYNOS_IPP_TASK_ASYNC was sampled before the publication and is set either for a non-blocking commit, whose ioctl does not wait, or by exynos_drm_ipp_task_abort() under ipp->lock.

The previous comment claimed that queueing the cleanup work before the wakeup is what makes the wakeup safe. That is not the ordering that
matters: wait_event_interruptible() evaluates its condition without this wakeup as well, for instance when another task on the shared ipp->done_wq wakes the waiter, so what orders the two sides is the publication of DONE.

Fixes 9913f74fe157 ("drm/exynos: ipp: Add IPP v2 framework")

---
Changes in v2:
- Publish DRM_EXYNOS_IPP_TASK_DONE last, after task->ret is written and after DRM_EXYNOS_IPP_TASK_ASYNC has been sampled, and do it with smp_store_release(); the waiter reads the flag with smp_load_acquire(). A synchronous waiter evaluates the condition without ipp->lock, so moving the wakeup alone does not order the two sides. Reported by Sashiko.
- Rewrite the comment and the commit message: what orders the waiters is the publication of DONE, not the wake_up() call.
- Add the Fixes: tag for the IPP v2 framework.
- Exercise the publication order and the signal-abort path with a controlled test; the outcome is in the note below.
Prepared against v7.4-rc1-1969-g666539eecdf4.

Compile tested: arm64 with aarch64-linux-gnu-gcc 11.4 and CONFIG_DRM_EXYNOS=m with the IPP drivers.

The publication order and its interaction with the abort path were exercised by a controlled test on a KASAN kernel built from this tree (x86_64, QEMU/KVM; 12 checks, all passing). The test transcribes exynos_drm_ipp_task_done() before and after the change and the wait/abort part of the commit ioctl, and runs them on the real workqueue, wait and signal machinery:

- pre-patch order: the woken waiter releases the task and the completion path then reads task->flags - a KASAN slab-use-after-free;
- patched order, same window: the completion path does not touch the task after the wakeup, and the waiter reads the result it stored;
- a wait interrupted by a signal (-ERESTARTSYS) enters the abort path, the task is handed to the cleanup work and released exactly once;
- a signal that arrives after the completion does not turn the wait into an abort: the waiter takes the success path and releases the task inline, exactly once.

The test does not compile the driver itself, which would need the whole DRM device, so no Exynos hardware and no DRM teardown path was exercised, and the store-store half of the ordering can only be observed on a weakly ordered architecture.

AI assistance: the analysis, the change and its review notes were prepared with Claude (Anthropic) as a coding assistant. The author reviewed the reasoning and takes responsibility for the content.

---
 drivers/gpu/drm/exynos/exynos_drm_ipp.c | 30 +++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)
drivers/gpu/drm/exynos/exynos_drm_ipp.c +26 -4
index 43bc4f63bb84..20d3c10872d8 100644
@@ -764,6 +764,7 @@ void exynos_drm_ipp_task_done(struct exynos_drm_ipp_task *task, int ret)
 {
 	struct exynos_drm_ipp *ipp = task->ipp;
 	unsigned long flags;
+	bool async;
 
 	DRM_DEV_DEBUG_DRIVER(task->dev, "ipp: %d, task %p done: %d\n",
 			     ipp->id, task, ret);
@@ -771,17 +772,36 @@ void exynos_drm_ipp_task_done(struct exynos_drm_ipp_task *task, int ret)
 	spin_lock_irqsave(&ipp->lock, flags);
 	if (ipp->task == task)
 		ipp->task = NULL;
-	task->flags |= DRM_EXYNOS_IPP_TASK_DONE;
+
+	/*
+	 * A synchronous waiter evaluates DRM_EXYNOS_IPP_TASK_DONE without
+	 * taking @ipp->lock and frees the task as soon as it observes the
+	 * flag, so @ret and the decision whether the cleanup work takes the
+	 * task over have to be settled before it becomes visible.
+	 */
 	task->ret = ret;
+	async = task->flags & DRM_EXYNOS_IPP_TASK_ASYNC;
+
+	/* Publish the completion last; the waiter pairs with an acquire. */
+	smp_store_release(&task->flags,
+			  task->flags | DRM_EXYNOS_IPP_TASK_DONE);
 	spin_unlock_irqrestore(&ipp->lock, flags);
 
 	exynos_drm_ipp_next_task(ipp);
-	wake_up(&ipp->done_wq);
 
-	if (task->flags & DRM_EXYNOS_IPP_TASK_ASYNC) {
+	/*
+	 * The task is not touched after the flag became visible, except for
+	 * this hand-over, which only runs for tasks no synchronous caller can
+	 * release: @async was sampled before the publication, and it is set
+	 * either for a non-blocking commit, whose ioctl does not wait, or by
+	 * exynos_drm_ipp_task_abort() under @ipp->lock.
+	 */
+	if (async) {
 		INIT_WORK(&task->cleanup_work, exynos_drm_ipp_cleanup_work);
 		schedule_work(&task->cleanup_work);
 	}
+
+	wake_up(&ipp->done_wq);
 }
 
 static void exynos_drm_ipp_next_task(struct exynos_drm_ipp *ipp)
@@ -929,7 +949,9 @@ int exynos_drm_ipp_commit_ioctl(struct drm_device *dev, void *data,
 				     ipp->id, task);
 		exynos_drm_ipp_schedule_task(ipp, task);
 		ret = wait_event_interruptible(ipp->done_wq,
-					task->flags & DRM_EXYNOS_IPP_TASK_DONE);
+					/* pairs with the release above */
+					smp_load_acquire(&task->flags) &
+					DRM_EXYNOS_IPP_TASK_DONE);
 		if (ret)
 			exynos_drm_ipp_task_abort(ipp, task);
 		else
Signature · 1 line
2.34.1