Skip to content

Commit 9c64a0a

Browse files
ujfalusibardliao
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>
1 parent 402ed86 commit 9c64a0a

10 files changed

Lines changed: 50 additions & 103 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
@@ -94,22 +94,19 @@ static int sof_ipc4_mtrace_dfs_open(struct inode *inode, struct file *file)
9494
struct sof_mtrace_core_data *core_data = inode->i_private;
9595
int ret;
9696

97-
mutex_lock(&core_data->buffer_lock);
97+
guard(mutex)(&core_data->buffer_lock);
9898

99-
if (core_data->log_buffer) {
100-
ret = -EBUSY;
101-
goto out;
102-
}
99+
if (core_data->log_buffer)
100+
return -EBUSY;
103101

104102
ret = debugfs_file_get(file->f_path.dentry);
105103
if (unlikely(ret))
106-
goto out;
104+
return ret;
107105

108106
core_data->log_buffer = kmalloc(SOF_IPC4_DEBUG_SLOT_SIZE, GFP_KERNEL);
109107
if (!core_data->log_buffer) {
110108
debugfs_file_put(file->f_path.dentry);
111-
ret = -ENOMEM;
112-
goto out;
109+
return -ENOMEM;
113110
}
114111

115112
ret = simple_open(inode, file);
@@ -118,9 +115,6 @@ static int sof_ipc4_mtrace_dfs_open(struct inode *inode, struct file *file)
118115
debugfs_file_put(file->f_path.dentry);
119116
}
120117

121-
out:
122-
mutex_unlock(&core_data->buffer_lock);
123-
124118
return ret;
125119
}
126120

@@ -257,10 +251,10 @@ static int sof_ipc4_mtrace_dfs_release(struct inode *inode, struct file *file)
257251

258252
debugfs_file_put(file->f_path.dentry);
259253

260-
mutex_lock(&core_data->buffer_lock);
261-
kfree(core_data->log_buffer);
262-
core_data->log_buffer = NULL;
263-
mutex_unlock(&core_data->buffer_lock);
254+
scoped_guard(mutex, &core_data->buffer_lock) {
255+
kfree(core_data->log_buffer);
256+
core_data->log_buffer = NULL;
257+
}
264258

265259
return 0;
266260
}

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
@@ -3260,7 +3260,7 @@ static int sof_ipc4_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget
32603260
struct sof_ipc4_fw_data *ipc4_data = sdev->private;
32613261
int ret = 0;
32623262

3263-
mutex_lock(&ipc4_data->pipeline_state_mutex);
3263+
guard(mutex)(&ipc4_data->pipeline_state_mutex);
32643264

32653265
/* freeing a pipeline frees all the widgets associated with it */
32663266
if (swidget->id == snd_soc_dapm_scheduler) {
@@ -3271,7 +3271,6 @@ static int sof_ipc4_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget
32713271
if (pipeline->use_chain_dma) {
32723272
dev_warn(sdev->dev, "use_chain_dma set for scheduler %s",
32733273
swidget->widget->name);
3274-
mutex_unlock(&ipc4_data->pipeline_state_mutex);
32753274
return 0;
32763275
}
32773276

@@ -3299,8 +3298,6 @@ static int sof_ipc4_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget
32993298
ida_free(&fw_module->m_ida, swidget->instance_id);
33003299
}
33013300

3302-
mutex_unlock(&ipc4_data->pipeline_state_mutex);
3303-
33043301
return ret;
33053302
}
33063303

sound/soc/sof/ipc4.c

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

415415
/* Serialise IPC TX */
416-
mutex_lock(&ipc->tx_mutex);
416+
guard(mutex)(&ipc->tx_mutex);
417417

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

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

433-
mutex_unlock(&ipc->tx_mutex);
434-
435433
return ret;
436434
}
437435

@@ -507,7 +505,7 @@ static int sof_ipc4_set_get_data(struct snd_sof_dev *sdev, void *data,
507505
}
508506

509507
/* Serialise IPC TX */
510-
mutex_lock(&sdev->ipc->tx_mutex);
508+
guard(mutex)(&sdev->ipc->tx_mutex);
511509

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

594-
mutex_unlock(&sdev->ipc->tx_mutex);
595-
596592
kfree(tx_payload_for_get);
597593

598594
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)