Skip to content

Commit cd9eae5

Browse files
committed
ASoC: SOF: ipc3-dtrace: Add helper function to update the sdev->host_offset
We are using the READ_ONCE() on the debugfs read path for accessing sdev->host_offset, but the set is not atomic or protected in any way. Add a small helper to do the host_offset update and be really paranoid about the a possible race in update Signed-off-by: Peter Ujfalusi <peter.ujfalusi@linux.intel.com>
1 parent 32531ea commit cd9eae5

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

sound/soc/sof/ipc3-dtrace.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,21 @@ static int debugfs_create_trace_filter(struct snd_sof_dev *sdev)
251251
return 0;
252252
}
253253

254+
static bool sof_dtrace_set_host_offset(struct sof_dtrace_priv *priv, u32 new_offset)
255+
{
256+
u32 host_offset = READ_ONCE(priv->host_offset);
257+
258+
if (host_offset != new_offset) {
259+
/* This is a bit paranoid and unlikely that it is needed */
260+
u32 ret = cmpxchg(&priv->host_offset, host_offset, new_offset);
261+
262+
if (ret == host_offset)
263+
return true;
264+
}
265+
266+
return false;
267+
}
268+
254269
static size_t sof_dtrace_avail(struct snd_sof_dev *sdev,
255270
loff_t pos, size_t buffer_size)
256271
{
@@ -367,7 +382,7 @@ static int dfsentry_dtrace_release(struct inode *inode, struct file *file)
367382

368383
/* avoid duplicate traces at next open */
369384
if (priv->dtrace_state != SOF_DTRACE_ENABLED)
370-
priv->host_offset = 0;
385+
sof_dtrace_set_host_offset(priv, 0);
371386

372387
return 0;
373388
}
@@ -443,7 +458,7 @@ static int ipc3_dtrace_enable(struct snd_sof_dev *sdev)
443458
params.buffer.pages = priv->dma_trace_pages;
444459
params.stream_tag = 0;
445460

446-
priv->host_offset = 0;
461+
sof_dtrace_set_host_offset(priv, 0);
447462
priv->dtrace_draining = false;
448463

449464
ret = sof_dtrace_host_init(sdev, &priv->dmatb, &params);
@@ -558,10 +573,8 @@ int ipc3_dtrace_posn_update(struct snd_sof_dev *sdev,
558573
return 0;
559574

560575
if (trace_pos_update_expected(priv) &&
561-
priv->host_offset != posn->host_offset) {
562-
priv->host_offset = posn->host_offset;
576+
sof_dtrace_set_host_offset(priv, posn->host_offset))
563577
wake_up(&priv->trace_sleep);
564-
}
565578

566579
if (posn->overflow != 0)
567580
dev_err(sdev->dev,

0 commit comments

Comments
 (0)