Skip to content

Commit d7081cb

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 e890361 commit d7081cb

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

sound/soc/sof/ipc3-dtrace.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,21 @@ static int debugfs_create_trace_filter(struct snd_sof_dev *sdev)
241241
return 0;
242242
}
243243

244+
static bool sof_dtrace_set_host_offset(struct sof_dtrace_priv *priv, u32 new_offset)
245+
{
246+
u32 host_offset = READ_ONCE(priv->host_offset);
247+
248+
if (host_offset != new_offset) {
249+
/* This is a bit paranoid and unlikely that it is needed */
250+
u32 ret = cmpxchg(&priv->host_offset, host_offset, new_offset);
251+
252+
if (ret == host_offset)
253+
return true;
254+
}
255+
256+
return false;
257+
}
258+
244259
static size_t sof_dtrace_avail(struct snd_sof_dev *sdev,
245260
loff_t pos, size_t buffer_size)
246261
{
@@ -357,7 +372,7 @@ static int dfsentry_dtrace_release(struct inode *inode, struct file *file)
357372

358373
/* avoid duplicate traces at next open */
359374
if (priv->dtrace_state != SOF_DTRACE_ENABLED)
360-
priv->host_offset = 0;
375+
sof_dtrace_set_host_offset(priv, 0);
361376

362377
return 0;
363378
}
@@ -433,7 +448,7 @@ static int ipc3_dtrace_enable(struct snd_sof_dev *sdev)
433448
params.buffer.pages = priv->dma_trace_pages;
434449
params.stream_tag = 0;
435450

436-
priv->host_offset = 0;
451+
sof_dtrace_set_host_offset(priv, 0);
437452
priv->dtrace_draining = false;
438453

439454
ret = sof_dtrace_host_init(sdev, &priv->dmatb, &params);
@@ -545,10 +560,9 @@ int ipc3_dtrace_posn_update(struct snd_sof_dev *sdev,
545560
if (!sdev->fw_trace_is_supported)
546561
return 0;
547562

548-
if (priv->dtrace_state == SOF_DTRACE_ENABLED &&
549-
priv->host_offset != posn->host_offset) {
550-
priv->host_offset = posn->host_offset;
551-
wake_up(&priv->trace_sleep);
563+
if (priv->dtrace_state == SOF_DTRACE_ENABLED) {
564+
if (sof_dtrace_set_host_offset(priv, posn->host_offset))
565+
wake_up(&priv->trace_sleep);
552566
}
553567

554568
if (posn->overflow != 0)

0 commit comments

Comments
 (0)