Skip to contentSkip to conversation

sound/core/seq : fix data-race in snd_seq_fifo_cell_out/snd_seq_fifo_poll_wait

PATCH In review [email protected] 2 participants 2 messages Sep 16, 2025 Sound

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sound/core/seq : fix data-race in snd_seq_fifo_cell_out/snd_seq_fifo_poll_wait

From: HariKrishna Sagala <[email protected]>

data race in both the functions, snd_seq_fifo_cell_out & snd_seq_fifo_poll_wait is protected with guards

---
 sound/core/seq/seq_fifo.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)
sound/core/seq/seq_fifo.c +13 -9
index f23c6b7ae240..65e28ebb0eb1 100644
@@ -138,16 +138,18 @@ static struct snd_seq_event_cell *fifo_cell_out(struct snd_seq_fifo *f)
 {
 	struct snd_seq_event_cell *cell;
 
-	cell = f->head;
-	if (cell) {
-		f->head = cell->next;
+	scoped_guard(spinlock_irqsave, &f->lock) {
+		cell = f->head;
+		if (cell) {
+			f->head = cell->next;
 
-		/* reset tail if this was the last element */
-		if (f->tail == cell)
-			f->tail = NULL;
+			/* reset tail if this was the last element */
+			if (f->tail == cell)
+				f->tail = NULL;
 
-		cell->next = NULL;
-		f->cells--;
+			cell->next = NULL;
+			f->cells--;
+		}
 	}
 
 	return cell;
@@ -210,7 +212,9 @@ int snd_seq_fifo_poll_wait(struct snd_seq_fifo *f, struct file *file,
 			   poll_table *wait)
 {
 	poll_wait(file, &f->input_sleep, wait);
-	return (f->cells > 0);
+	guard(spinlock_irqsave)(&f->lock);
+	int isNonzero = (f->cells > 0);
+	return isNonzero;
 }
 
 /* change the size of pool; all old events are removed */
Signature · 1 line
2.43.0
#1 lore raw
Quoted 5 lines · On Tue, 16 Sep 2025 12:45:48 +0200, [email protected] wrote:

From: HariKrishna Sagala <[email protected]>

data race in both the functions, snd_seq_fifo_cell_out & snd_seq_fifo_poll_wait is protected with guards

Please don't, this will lead obviously to a deadlock! fifo_cell_out() is already protected by f->lock spinlock. You can at most protect the f->cells reference in snd_seq_fifo_poll_wait(). Or change f->celss to atomic_t.

But, practically seen, it won't fix any real issue. The evaluation there can be racy in anyway.

Also the patch doesn't follow the coding style.
I sincerely hope you verify your patch whether it really works before submitting at the next time.

thanks,

Takashi

Quoted 53 lines
---
 sound/core/seq/seq_fifo.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)
sound/core/seq/seq_fifo.c +13 -9
index f23c6b7ae240..65e28ebb0eb1 100644
@@ -138,16 +138,18 @@ static struct snd_seq_event_cell *fifo_cell_out(struct snd_seq_fifo *f)
 {
 	struct snd_seq_event_cell *cell;
 
-	cell = f->head;
-	if (cell) {
-		f->head = cell->next;
+	scoped_guard(spinlock_irqsave, &f->lock) {
+		cell = f->head;
+		if (cell) {
+			f->head = cell->next;
 
-		/* reset tail if this was the last element */
-		if (f->tail == cell)
-			f->tail = NULL;
+			/* reset tail if this was the last element */
+			if (f->tail == cell)
+				f->tail = NULL;
 
-		cell->next = NULL;
-		f->cells--;
+			cell->next = NULL;
+			f->cells--;
+		}
 	}
 
 	return cell;
@@ -210,7 +212,9 @@ int snd_seq_fifo_poll_wait(struct snd_seq_fifo *f, struct file *file,
 			   poll_table *wait)
 {
 	poll_wait(file, &f->input_sleep, wait);
-	return (f->cells > 0);
+	guard(spinlock_irqsave)(&f->lock);
+	int isNonzero = (f->cells > 0);
+	return isNonzero;
 }
 
 /* change the size of pool; all old events are removed */
Signature · 1 line
2.43.0