Skip to content

Commit 5c19da3

Browse files
ujfalusibroonie
authored andcommitted
ASoC: SOF: Use guard()/scoped_guard() for mutex locks where it makes sense
Replace the manual mutex lock/unlock pairs with guard()/scoped_guard(). Only code refactoring, and no behavior change. Signed-off-by: Peter Ujfalusi <peter.ujfalusi@linux.intel.com> Reviewed-by: Daniel Baluta <daniel.baluta@nxp.com> Reviewed-by: Ranjani Sridharan <ranjani.sridharan@linux.intel.com> Link: https://patch.msgid.link/20260112101004.7648-2-peter.ujfalusi@linux.intel.com Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 0cd9bf6 commit 5c19da3

10 files changed

Lines changed: 51 additions & 104 deletions

File tree

sound/soc/sof/ipc.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,8 @@ void snd_sof_ipc_free(struct snd_sof_dev *sdev)
225225
return;
226226

227227
/* disable sending of ipc's */
228-
mutex_lock(&ipc->tx_mutex);
229-
ipc->disable_ipc_tx = true;
230-
mutex_unlock(&ipc->tx_mutex);
228+
scoped_guard(mutex, &ipc->tx_mutex)
229+
ipc->disable_ipc_tx = true;
231230

232231
if (ipc->ops->exit)
233232
ipc->ops->exit(sdev);

sound/soc/sof/ipc3-topology.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2427,9 +2427,9 @@ static int sof_ipc3_free_widgets_in_list(struct snd_sof_dev *sdev, bool include_
24272427
/* Do not free widgets for static pipelines with FW older than SOF2.2 */
24282428
if (!verify && !swidget->dynamic_pipeline_widget &&
24292429
SOF_FW_VER(v->major, v->minor, v->micro) < SOF_FW_VER(2, 2, 0)) {
2430-
mutex_lock(&swidget->setup_mutex);
2431-
swidget->use_count = 0;
2432-
mutex_unlock(&swidget->setup_mutex);
2430+
scoped_guard(mutex, &swidget->setup_mutex)
2431+
swidget->use_count = 0;
2432+
24332433
if (swidget->spipe)
24342434
swidget->spipe->complete = 0;
24352435
continue;

sound/soc/sof/ipc3.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ static int sof_ipc3_tx_msg(struct snd_sof_dev *sdev, void *msg_data, size_t msg_
378378
}
379379

380380
/* Serialise IPC TX */
381-
mutex_lock(&ipc->tx_mutex);
381+
guard(mutex)(&ipc->tx_mutex);
382382

383383
ret = ipc3_tx_msg_unlocked(ipc, msg_data, msg_bytes, reply_data, reply_bytes);
384384

@@ -405,8 +405,6 @@ static int sof_ipc3_tx_msg(struct snd_sof_dev *sdev, void *msg_data, size_t msg_
405405
}
406406
}
407407

408-
mutex_unlock(&ipc->tx_mutex);
409-
410408
return ret;
411409
}
412410

@@ -477,7 +475,7 @@ static int sof_ipc3_set_get_data(struct snd_sof_dev *sdev, void *data, size_t da
477475
memcpy(cdata_chunk, cdata, hdr_bytes);
478476

479477
/* Serialise IPC TX */
480-
mutex_lock(&sdev->ipc->tx_mutex);
478+
guard(mutex)(&ipc->tx_mutex);
481479

482480
/* copy the payload data in a loop */
483481
for (i = 0; i < num_msg; i++) {
@@ -511,8 +509,6 @@ static int sof_ipc3_set_get_data(struct snd_sof_dev *sdev, void *data, size_t da
511509
sof_ipc3_dump_payload(sdev, payload, data_bytes - header_bytes);
512510
}
513511

514-
mutex_unlock(&sdev->ipc->tx_mutex);
515-
516512
kfree(cdata_chunk);
517513

518514
return ret;

sound/soc/sof/ipc4-mtrace.c

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,22 +118,19 @@ static int sof_ipc4_mtrace_dfs_open(struct inode *inode, struct file *file)
118118
struct sof_mtrace_core_data *core_data = inode->i_private;
119119
int ret;
120120

121-
mutex_lock(&core_data->buffer_lock);
121+
guard(mutex)(&core_data->buffer_lock);
122122

123-
if (core_data->log_buffer) {
124-
ret = -EBUSY;
125-
goto out;
126-
}
123+
if (core_data->log_buffer)
124+
return -EBUSY;
127125

128126
ret = debugfs_file_get(file->f_path.dentry);
129127
if (unlikely(ret))
130-
goto out;
128+
return ret;
131129

132130
core_data->log_buffer = kmalloc(SOF_IPC4_DEBUG_SLOT_SIZE, GFP_KERNEL);
133131
if (!core_data->log_buffer) {
134132
debugfs_file_put(file->f_path.dentry);
135-
ret = -ENOMEM;
136-
goto out;
133+
return -ENOMEM;
137134
}
138135

139136
ret = simple_open(inode, file);
@@ -142,9 +139,6 @@ static int sof_ipc4_mtrace_dfs_open(struct inode *inode, struct file *file)
142139
debugfs_file_put(file->f_path.dentry);
143140
}
144141

145-
out:
146-
mutex_unlock(&core_data->buffer_lock);
147-
148142
return ret;
149143
}
150144

@@ -281,10 +275,10 @@ static int sof_ipc4_mtrace_dfs_release(struct inode *inode, struct file *file)
281275

282276
debugfs_file_put(file->f_path.dentry);
283277

284-
mutex_lock(&core_data->buffer_lock);
285-
kfree(core_data->log_buffer);
286-
core_data->log_buffer = NULL;
287-
mutex_unlock(&core_data->buffer_lock);
278+
scoped_guard(mutex, &core_data->buffer_lock) {
279+
kfree(core_data->log_buffer);
280+
core_data->log_buffer = NULL;
281+
}
288282

289283
return 0;
290284
}

sound/soc/sof/ipc4-pcm.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ static int sof_ipc4_trigger_pipelines(struct snd_soc_component *component,
487487
return -ENOMEM;
488488
}
489489

490-
mutex_lock(&ipc4_data->pipeline_state_mutex);
490+
guard(mutex)(&ipc4_data->pipeline_state_mutex);
491491

492492
/*
493493
* IPC4 requires pipelines to be triggered in order starting at the sink and
@@ -580,7 +580,6 @@ static int sof_ipc4_trigger_pipelines(struct snd_soc_component *component,
580580
}
581581

582582
free:
583-
mutex_unlock(&ipc4_data->pipeline_state_mutex);
584583
kfree(trigger_list);
585584
kfree(pipe_priority);
586585
return ret;

sound/soc/sof/ipc4-topology.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3150,7 +3150,7 @@ static int sof_ipc4_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget
31503150
struct sof_ipc4_fw_data *ipc4_data = sdev->private;
31513151
int ret = 0;
31523152

3153-
mutex_lock(&ipc4_data->pipeline_state_mutex);
3153+
guard(mutex)(&ipc4_data->pipeline_state_mutex);
31543154

31553155
/* freeing a pipeline frees all the widgets associated with it */
31563156
if (swidget->id == snd_soc_dapm_scheduler) {
@@ -3161,7 +3161,6 @@ static int sof_ipc4_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget
31613161
if (pipeline->use_chain_dma) {
31623162
dev_warn(sdev->dev, "use_chain_dma set for scheduler %s",
31633163
swidget->widget->name);
3164-
mutex_unlock(&ipc4_data->pipeline_state_mutex);
31653164
return 0;
31663165
}
31673166

@@ -3189,8 +3188,6 @@ static int sof_ipc4_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget
31893188
ida_free(&fw_module->m_ida, swidget->instance_id);
31903189
}
31913190

3192-
mutex_unlock(&ipc4_data->pipeline_state_mutex);
3193-
31943191
return ret;
31953192
}
31963193

sound/soc/sof/ipc4.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ static int sof_ipc4_tx_msg(struct snd_sof_dev *sdev, void *msg_data, size_t msg_
412412
}
413413

414414
/* Serialise IPC TX */
415-
mutex_lock(&ipc->tx_mutex);
415+
guard(mutex)(&ipc->tx_mutex);
416416

417417
ret = ipc4_tx_msg_unlocked(ipc, msg_data, msg_bytes, reply_data, reply_bytes);
418418

@@ -429,8 +429,6 @@ static int sof_ipc4_tx_msg(struct snd_sof_dev *sdev, void *msg_data, size_t msg_
429429
sof_ipc4_dump_payload(sdev, msg->data_ptr, msg->data_size);
430430
}
431431

432-
mutex_unlock(&ipc->tx_mutex);
433-
434432
return ret;
435433
}
436434

@@ -506,7 +504,7 @@ static int sof_ipc4_set_get_data(struct snd_sof_dev *sdev, void *data,
506504
}
507505

508506
/* Serialise IPC TX */
509-
mutex_lock(&sdev->ipc->tx_mutex);
507+
guard(mutex)(&sdev->ipc->tx_mutex);
510508

511509
do {
512510
size_t tx_size, rx_size;
@@ -590,8 +588,6 @@ static int sof_ipc4_set_get_data(struct snd_sof_dev *sdev, void *data,
590588
if (sof_debug_check_flag(SOF_DBG_DUMP_IPC_MESSAGE_PAYLOAD))
591589
sof_ipc4_dump_payload(sdev, ipc4_msg->data_ptr, ipc4_msg->data_size);
592590

593-
mutex_unlock(&sdev->ipc->tx_mutex);
594-
595591
kfree(tx_payload_for_get);
596592

597593
return ret;

sound/soc/sof/ops.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -287,16 +287,12 @@ static inline int
287287
snd_sof_dsp_set_power_state(struct snd_sof_dev *sdev,
288288
const struct sof_dsp_power_state *target_state)
289289
{
290-
int ret = 0;
291-
292-
mutex_lock(&sdev->power_state_access);
290+
guard(mutex)(&sdev->power_state_access);
293291

294292
if (sof_ops(sdev)->set_power_state)
295-
ret = sof_ops(sdev)->set_power_state(sdev, target_state);
296-
297-
mutex_unlock(&sdev->power_state_access);
293+
return sof_ops(sdev)->set_power_state(sdev, target_state);
298294

299-
return ret;
295+
return 0;
300296
}
301297

302298
/* debug */

sound/soc/sof/sof-audio.c

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,8 @@ static int sof_widget_free_unlocked(struct snd_sof_dev *sdev,
121121

122122
int sof_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
123123
{
124-
int ret;
125-
126-
mutex_lock(&swidget->setup_mutex);
127-
ret = sof_widget_free_unlocked(sdev, swidget);
128-
mutex_unlock(&swidget->setup_mutex);
129-
130-
return ret;
124+
guard(mutex)(&swidget->setup_mutex);
125+
return sof_widget_free_unlocked(sdev, swidget);
131126
}
132127
EXPORT_SYMBOL(sof_widget_free);
133128

@@ -240,13 +235,8 @@ static int sof_widget_setup_unlocked(struct snd_sof_dev *sdev,
240235

241236
int sof_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
242237
{
243-
int ret;
244-
245-
mutex_lock(&swidget->setup_mutex);
246-
ret = sof_widget_setup_unlocked(sdev, swidget);
247-
mutex_unlock(&swidget->setup_mutex);
248-
249-
return ret;
238+
guard(mutex)(&swidget->setup_mutex);
239+
return sof_widget_setup_unlocked(sdev, swidget);
250240
}
251241
EXPORT_SYMBOL(sof_widget_setup);
252242

@@ -377,24 +367,22 @@ static int sof_setup_pipeline_connections(struct snd_sof_dev *sdev,
377367
else
378368
swidget = sroute->src_widget;
379369

380-
mutex_lock(&swidget->setup_mutex);
381-
if (!swidget->use_count) {
382-
mutex_unlock(&swidget->setup_mutex);
383-
continue;
384-
}
370+
scoped_guard(mutex, &swidget->setup_mutex) {
371+
if (!swidget->use_count)
372+
continue;
385373

386-
if (tplg_ops && tplg_ops->route_setup) {
387-
/*
388-
* this route will get freed when either the source widget or the sink
389-
* widget is freed during hw_free
390-
*/
391-
ret = tplg_ops->route_setup(sdev, sroute);
392-
if (!ret)
393-
sroute->setup = true;
374+
if (tplg_ops && tplg_ops->route_setup) {
375+
/*
376+
* this route will get freed when either the
377+
* source widget or the sink widget is freed
378+
* during hw_free
379+
*/
380+
ret = tplg_ops->route_setup(sdev, sroute);
381+
if (!ret)
382+
sroute->setup = true;
383+
}
394384
}
395385

396-
mutex_unlock(&swidget->setup_mutex);
397-
398386
if (ret < 0)
399387
return ret;
400388
}

0 commit comments

Comments
 (0)