staging: axis-fifo: fix sleep while holding mutex
PATCH v2 1/1 staging: axis-fifo: fix sleep while holding mutex
The driver calls wait_event_interruptible_timeout() while holding a mutex. This can lead to a deadlock or poor performance as the mutex is held while the process is sleeping, preventing other processes from accessing the device. This patch moves the wait_event call outside of the mutex lock in both read and write operations.
--- drivers/staging/axis-fifo/axis-fifo.c | 30 +++++++++++++-------------- 1 file changed, 15 insertions(+), 15 deletions(-)
drivers/staging/axis-fifo/axis-fifo.c +15 -15
@@ -197,11 +197,10 @@ static ssize_t axis_fifo_read(struct file *f, char __user *buf, goto end_unlock; } }else { -/* opened in blocking mode -* wait for a packet available interrupt (or timeout) -* if nothing is currently available +/* +* Opened in blocking mode. Wait for a packet available +* interrupt (or timeout) before acquiring the lock. */ -mutex_lock (&fifo->read_lock); ret =wait_event_interruptible_timeout (fifo->read_queue,ioread32 (fifo->base_addr +XLLF_RDFO_OFFSET ), read_timeout);
@@ -214,8 +213,10 @@ static ssize_t axis_fifo_read(struct file *f, char __user *buf, ret); } - goto end_unlock; +return ret; } + +mutex_lock (&fifo->read_lock); } bytes_available =ioread32 (fifo->base_addr +XLLF_RLR_OFFSET );
@@ -340,27 +341,26 @@ static ssize_t axis_fifo_write(struct file *f, const char __user *buf, goto end_unlock; } }else { -/* opened in blocking mode */ - -/* wait for an interrupt (or timeout) if there isn't -* currently enough room in the fifo +/* +* Opened in blocking mode. Wait for enough room in the FIFO +* (or timeout) before acquiring the lock. */ -mutex_lock (&fifo->write_lock); ret =wait_event_interruptible_timeout (fifo->write_queue,ioread32 (fifo->base_addr +XLLF_TDFV_OFFSET ) - >= words_to_write, + >= words_to_write, write_timeout);if (ret <=0 ) { -if (ret ==0 ) { +if (ret ==0 ) ret = -EAGAIN ; - }else if (ret != -ERESTARTSYS ) { +else if (ret != -ERESTARTSYS )dev_err (fifo->dt_device,"wait_event_interruptible_timeout() error in write (ret=%i)\n" , ret); - } - goto end_unlock; +return ret; } + +mutex_lock (&fifo->write_lock); } txbuf =vmemdup_user (buf, len);
Signature · 1 line
2.39.5 (Apple Git-154)
Greg KH
maintainerQuoted 79 lines · Baran Arda
The driver calls
wait_event_interruptible_timeout()while holding a mutex. This can lead to a deadlock or poor performance as the mutex is held while the process is sleeping, preventing other processes from accessing the device. This patch moves thewait_eventcall outside of the mutex lock in both read and write operations.Signed-off-by Baran Arda <[email protected]>--- drivers/staging/axis-fifo/axis-fifo.c | 30 +++++++++++++-------------- 1 file changed, 15 insertions(+), 15 deletions(-)drivers/staging/axis-fifo/axis-fifo.c +15
-15@@ -197,11 +197,10 @@ static ssize_t axis_fifo_read(struct file *f, char __user *buf, goto end_unlock; } }else { -/* opened in blocking mode -* wait for a packet available interrupt (or timeout) -* if nothing is currently available +/* +* Opened in blocking mode. Wait for a packet available +* interrupt (or timeout) before acquiring the lock. */ -mutex_lock (&fifo->read_lock); ret =wait_event_interruptible_timeout (fifo->read_queue,ioread32 (fifo->base_addr +XLLF_RDFO_OFFSET ), read_timeout);@@ -214,8 +213,10 @@ static ssize_t axis_fifo_read(struct file *f, char __user *buf, ret); } - goto end_unlock; +return ret; } + +mutex_lock (&fifo->read_lock); } bytes_available =ioread32 (fifo->base_addr +XLLF_RLR_OFFSET );@@ -340,27 +341,26 @@ static ssize_t axis_fifo_write(struct file *f, const char __user *buf, goto end_unlock; } }else { -/* opened in blocking mode */ - -/* wait for an interrupt (or timeout) if there isn't -* currently enough room in the fifo +/* +* Opened in blocking mode. Wait for enough room in the FIFO +* (or timeout) before acquiring the lock. */ -mutex_lock (&fifo->write_lock); ret =wait_event_interruptible_timeout (fifo->write_queue,ioread32 (fifo->base_addr +XLLF_TDFV_OFFSET ) - >= words_to_write, + >= words_to_write, write_timeout);if (ret <=0 ) { -if (ret ==0 ) { +if (ret ==0 ) ret = -EAGAIN ; - }else if (ret != -ERESTARTSYS ) { +else if (ret != -ERESTARTSYS )dev_err (fifo->dt_device,"wait_event_interruptible_timeout() error in write (ret=%i)\n" , ret); - } - goto end_unlock; +return ret; } + +mutex_lock (&fifo->write_lock); } txbuf =vmemdup_user (buf, len);Signature · 1 line
2.39.5 (Apple Git-154)
Hi,
This is the friendly patch-bot of Greg Kroah-Hartman. You have sent him a patch that has triggered this response. He used to manually respond to these common problems, but in order to save his sanity (he kept writing the same thing over and over, yet to different people), I was created. Hopefully you will not take offence and will fix the problem in your patch and resubmit it so that it can be accepted into the Linux kernel tree.
You are receiving this message because of the following common error(s) as indicated below:
- Your patch contains warnings and/or errors noticed by the scripts/checkpatch.pl tool.
- This looks like a new version of a previously submitted patch, but you did not list below the --- line any changes from the previous version. Please read the section entitled "The canonical patch format" in the kernel file, Documentation/process/submitting-patches.rst for what needs to be done here to properly describe this.
If you wish to discuss this problem further, or you have questions about how to resolve this issue, please feel free to respond to this email and Greg will reply once he has dug out from the pending patches received from other developers.
thanks,
greg k-h's patch email bot
Greg KH
maintainerQuoted 1 line · Baran Arda
The driver calls
wait_event_interruptible_timeout()while holding a mutex. This can lead to a deadlock or poor performance as the mutex is held while the process is sleeping, preventing other processes from accessing the device. This patch moves thewait_eventcall outside of the mutex lock in both read and write operations.
What kind of tool was used to "find and fix" this "issue"?
And how was this tested?
And are you _sure_ it is correct? (hint, I think you just broke the driver's logic, please look closely at your patch...)
And what caused you to want to make this change to this driver? Do you have access to this hardware?
thanks,
greg k-h
Hi Greg,
I didnt think there was an issue, I thought I identified a pattern and wanted to improve speed with this update.
This was just compile tested. I didnt realize it would create a _race condition_.
I occasionally review staging drivers for cleanup opportunities for learning linux kernel. I do not have access to this driver.
best regards,
Baran Arda
Greg KH <[email protected]>, 3 Şub 2026 Sal, 15:02 tarihinde şunu yazdı:
Quoted 17 lines · Baran Arda
Quoted 1 line
The driver calls
wait_event_interruptible_timeout()while holding a mutex. This can lead to a deadlock or poor performance as the mutex is held while the process is sleeping, preventing other processes from accessing the device. This patch moves thewait_eventcall outside of the mutex lock in both read and write operations.What kind of tool was used to "find and fix" this "issue"?
And how was this tested?
And are you _sure_ it is correct? (hint, I think you just broke the driver's logic, please look closely at your patch...)
And what caused you to want to make this change to this driver? Do you have access to this hardware?
thanks,
greg k-h