tpm: flush timeout work outside buffer_mutex to avoid deadlock
Subsystems (3)
PATCH v2 1/1 tpm: flush timeout work outside buffer_mutex to avoid deadlock
From: Hyunjung Ko <[email protected]>
tpm_common_read() flushes priv->timeout_work while holding priv->buffer_mutex:
mutex_lock(&priv->buffer_mutex);
...
if (!priv->response_length) {
*off = 0;
timer_delete_sync(&priv->user_read_timer);
flush_work(&priv->timeout_work);
}
mutex_unlock(&priv->buffer_mutex);The flushed handler, tpm_timeout_work(), takes the same buffer_mutex at its start. user_reader_timeout() schedules timeout_work from the user_read_timer, which is armed while a response is pending. If that timer fires and timeout_work starts running while a reader holds buffer_mutex, flush_work() waits for timeout_work to finish while the reader holds the mutex, and timeout_work blocks acquiring that same mutex -> deadlock. lockdep would report this as a circular locking dependency between buffer_mutex and the work.
Before commit 9488585b21be ("tpm: add support for partial reads") the timer deletion and the work flush ran before buffer_mutex was taken; that commit moved them into the mutex-held section, introducing the flush-under-lock cycle.
Flush timeout_work after dropping buffer_mutex. The timer is still deleted under the lock, so no new timeout work can be queued once the mutex is released, and the flush no longer runs under the lock the work needs.
Found by static analysis (a checker for cancel/flush-sync of a work performed under a lock that the work's handler also takes) and confirmed by code inspection. Not reproduced at runtime: it requires the deprecated user-space read timeout to fire while a read holds buffer_mutex, so the deadlock is established by inspection rather than an observed lockdep splat.
---
v2: Rename the local bool flush_timeout -> flush, per Jarkko's review.
No functional change.
drivers/char/tpm/tpm-dev-common.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/char/tpm/tpm-dev-common.c +12 -1
@@ -132,6 +132,7 @@ ssize_t tpm_common_read(struct file *file, char __user *buf, {struct file_priv *priv = file->private_data;ssize_t ret_size =0 ; +bool flush = false;int rc;if (!size)
@@ -164,9 +165,19 @@ out:if (!priv->response_length) { *off =0 ;timer_delete_sync (&priv->user_read_timer); -flush_work (&priv->timeout_work); + flush = true; }mutex_unlock (&priv->buffer_mutex); + +/* +* tpm_timeout_work() acquires buffer_mutex, so flushing it while that +* mutex is held deadlocks if the timeout work is already running and +* waiting for the lock. Flush after dropping the mutex; the timer is +* deleted above under the lock, so no new timeout work can be queued. +*/ +if (flush) +flush_work (&priv->timeout_work); +return ret_size; }
Signature · 1 line
2.43.0
Jarkko Sakkinen
maintainerQuoted 28 lines · hyunjungg
From: Hyunjung Ko <[email protected]>
tpm_common_read()flushespriv->timeout_workwhile holdingpriv->buffer_mutex:mutex_lock(&priv->buffer_mutex); ... if (!priv->response_length) { *off = 0; timer_delete_sync(&priv->user_read_timer); flush_work(&priv->timeout_work); } mutex_unlock(&priv->buffer_mutex);The flushed handler,
tpm_timeout_work(), takes the samebuffer_mutexat its start.user_reader_timeout()schedulestimeout_workfrom theuser_read_timer, which is armed while a response is pending. If that timer fires andtimeout_workstarts running while a reader holdsbuffer_mutex,flush_work()waits fortimeout_workto finish while the reader holds the mutex, andtimeout_workblocks acquiring that same mutex -> deadlock. lockdep would report this as a circular locking dependency betweenbuffer_mutexand the work.Before commit
9488585b21be("tpm: add support for partial reads") the timer deletion and the work flush ran beforebuffer_mutexwas taken; that commit moved them into the mutex-held section, introducing the flush-under-lock cycle.
Quoted 4 lines
Flush
timeout_workafter droppingbuffer_mutex. The timer is still deleted under the lock, so no new timeout work can be queued once the mutex is released, and the flush no longer runs under the lock the work needs.
I 100% understand the bug fix but why we need to write an article to describe a trivial bug? It's long enough that I lost the plot before finishing it up. Have you *ever* tried to read it yourself?
Quoted 7 lines
Found by static analysis (a checker for cancel/flush-sync of a work performed under a lock that the work's handler also takes) and confirmed by code inspection. Not reproduced at runtime: it requires the deprecated user-space read timeout to fire while a read holds
buffer_mutex, so the deadlock is established by inspection rather than an observed lockdep splat.
I think the last paragraph should be deleted.
Quoted 27 lines
Fixes 9488585b21be ("tpm: add support for partial reads")Signed-off-by Hyunjung Ko <[email protected]>---
v2: Rename the local boolflush_timeout-> flush, per Jarkko's review.
No functional change.drivers/char/tpm/tpm-dev-common.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)drivers/char/tpm/tpm-dev-common.c +1
-1@@ -132,6 +132,7 @@ ssize_t tpm_common_read(struct file *file, char __user *buf, {struct file_priv *priv = file->private_data;ssize_t ret_size =0 ; +bool flush = false;int rc;if (!size)@@ -164,9 +165,19 @@ out:if (!priv->response_length) { *off =0 ;timer_delete_sync (&priv->user_read_timer); -flush_work (&priv->timeout_work);
drivers/char/tpm/tpm-dev-common.c out: · line 177
Quoted 10 lines
drivers/char/tpm/tpm-dev-common.c +8
-0+ flush = true; }mutex_unlock (&priv->buffer_mutex); + +/* +* tpm_timeout_work() acquires buffer_mutex, so flushing it while that +* mutex is held deadlocks if the timeout work is already running and +* waiting for the lock. Flush after dropping the mutex; the timer is +* deleted above under the lock, so no new timeout work can be queued. +*/
I missed this one but I don't think we need this remark for a trivial bug fix. It's 50% of the whole patch. Please, delete it.
Quoted 9 lines
drivers/char/tpm/tpm-dev-common.c +3
-0+if (flush) +flush_work (&priv->timeout_work); +return ret_size; }Signature · 1 line
2.43.0
The comment had the pretty much had the relevant information. Please next time don't submit RAW LLM output and make sure that you are able to at least the commit message. This is horrible underperforming in my books.
Br, Jarkko