Skip to content

Commit fcc62b1

Browse files
committed
ALSA: control: Take power_ref lock primarily
The code path for kcontrol accesses have often nested locks of both card's controls_rwsem and power_ref, and applies in that order. However, what could take much longer is the latter, power_ref; it waits for the power state of the device, and it pretty much depends on the user's action. This patch swaps the locking order of those locks to a more natural way, namely, power_ref -> controls_rwsem, in order to shorten the time of possible nested locks. For consistency, power_ref is taken always in the top-level caller side (that is, *_user() functions and the ioctl handler itself). Link: https://patch.msgid.link/[email protected] Signed-off-by: Takashi Iwai <[email protected]>
1 parent 3bb6682 commit fcc62b1

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

sound/core/control.c

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,9 +1167,7 @@ static int __snd_ctl_elem_info(struct snd_card *card,
11671167
#ifdef CONFIG_SND_DEBUG
11681168
info->access = 0;
11691169
#endif
1170-
result = snd_power_ref_and_wait(card);
1171-
if (!result)
1172-
result = kctl->info(kctl, info);
1170+
result = kctl->info(kctl, info);
11731171
snd_power_unref(card);
11741172
if (result >= 0) {
11751173
snd_BUG_ON(info->access);
@@ -1208,12 +1206,17 @@ static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
12081206
static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
12091207
struct snd_ctl_elem_info __user *_info)
12101208
{
1209+
struct snd_card *card = ctl->card;
12111210
struct snd_ctl_elem_info info;
12121211
int result;
12131212

12141213
if (copy_from_user(&info, _info, sizeof(info)))
12151214
return -EFAULT;
1215+
result = snd_power_ref_and_wait(card);
1216+
if (result)
1217+
return result;
12161218
result = snd_ctl_elem_info(ctl, &info);
1219+
snd_power_unref(card);
12171220
if (result < 0)
12181221
return result;
12191222
/* drop internal access flags */
@@ -1257,10 +1260,7 @@ static int snd_ctl_elem_read(struct snd_card *card,
12571260

12581261
if (!snd_ctl_skip_validation(&info))
12591262
fill_remaining_elem_value(control, &info, pattern);
1260-
ret = snd_power_ref_and_wait(card);
1261-
if (!ret)
1262-
ret = kctl->get(kctl, control);
1263-
snd_power_unref(card);
1263+
ret = kctl->get(kctl, control);
12641264
if (ret < 0)
12651265
return ret;
12661266
if (!snd_ctl_skip_validation(&info) &&
@@ -1285,7 +1285,11 @@ static int snd_ctl_elem_read_user(struct snd_card *card,
12851285
if (IS_ERR(control))
12861286
return PTR_ERR(no_free_ptr(control));
12871287

1288+
result = snd_power_ref_and_wait(card);
1289+
if (result)
1290+
return result;
12881291
result = snd_ctl_elem_read(card, control);
1292+
snd_power_unref(card);
12891293
if (result < 0)
12901294
return result;
12911295

@@ -1300,7 +1304,7 @@ static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
13001304
struct snd_kcontrol *kctl;
13011305
struct snd_kcontrol_volatile *vd;
13021306
unsigned int index_offset;
1303-
int result;
1307+
int result = 0;
13041308

13051309
down_write(&card->controls_rwsem);
13061310
kctl = snd_ctl_find_id_locked(card, &control->id);
@@ -1318,9 +1322,8 @@ static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
13181322
}
13191323

13201324
snd_ctl_build_ioff(&control->id, kctl, index_offset);
1321-
result = snd_power_ref_and_wait(card);
13221325
/* validate input values */
1323-
if (IS_ENABLED(CONFIG_SND_CTL_INPUT_VALIDATION) && !result) {
1326+
if (IS_ENABLED(CONFIG_SND_CTL_INPUT_VALIDATION)) {
13241327
struct snd_ctl_elem_info info;
13251328

13261329
memset(&info, 0, sizeof(info));
@@ -1332,7 +1335,6 @@ static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
13321335
}
13331336
if (!result)
13341337
result = kctl->put(kctl, control);
1335-
snd_power_unref(card);
13361338
if (result < 0) {
13371339
up_write(&card->controls_rwsem);
13381340
return result;
@@ -1361,7 +1363,11 @@ static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
13611363
return PTR_ERR(no_free_ptr(control));
13621364

13631365
card = file->card;
1366+
result = snd_power_ref_and_wait(card);
1367+
if (result < 0)
1368+
return result;
13641369
result = snd_ctl_elem_write(card, file, control);
1370+
snd_power_unref(card);
13651371
if (result < 0)
13661372
return result;
13671373

@@ -1830,7 +1836,7 @@ static int call_tlv_handler(struct snd_ctl_file *file, int op_flag,
18301836
{SNDRV_CTL_TLV_OP_CMD, SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND},
18311837
};
18321838
struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff(kctl, id)];
1833-
int i, ret;
1839+
int i;
18341840

18351841
/* Check support of the request for this element. */
18361842
for (i = 0; i < ARRAY_SIZE(pairs); ++i) {
@@ -1848,11 +1854,7 @@ static int call_tlv_handler(struct snd_ctl_file *file, int op_flag,
18481854
vd->owner != NULL && vd->owner != file)
18491855
return -EPERM;
18501856

1851-
ret = snd_power_ref_and_wait(file->card);
1852-
if (!ret)
1853-
ret = kctl->tlv.c(kctl, op_flag, size, buf);
1854-
snd_power_unref(file->card);
1855-
return ret;
1857+
return kctl->tlv.c(kctl, op_flag, size, buf);
18561858
}
18571859

18581860
static int read_tlv_buf(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id,
@@ -1965,16 +1967,28 @@ static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg
19651967
case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
19661968
return snd_ctl_subscribe_events(ctl, ip);
19671969
case SNDRV_CTL_IOCTL_TLV_READ:
1968-
scoped_guard(rwsem_read, &ctl->card->controls_rwsem)
1970+
err = snd_power_ref_and_wait(card);
1971+
if (err < 0)
1972+
return err;
1973+
scoped_guard(rwsem_read, &card->controls_rwsem)
19691974
err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_READ);
1975+
snd_power_unref(card);
19701976
return err;
19711977
case SNDRV_CTL_IOCTL_TLV_WRITE:
1972-
scoped_guard(rwsem_write, &ctl->card->controls_rwsem)
1978+
err = snd_power_ref_and_wait(card);
1979+
if (err < 0)
1980+
return err;
1981+
scoped_guard(rwsem_write, &card->controls_rwsem)
19731982
err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_WRITE);
1983+
snd_power_unref(card);
19741984
return err;
19751985
case SNDRV_CTL_IOCTL_TLV_COMMAND:
1976-
scoped_guard(rwsem_write, &ctl->card->controls_rwsem)
1986+
err = snd_power_ref_and_wait(card);
1987+
if (err < 0)
1988+
return err;
1989+
scoped_guard(rwsem_write, &card->controls_rwsem)
19771990
err = snd_ctl_tlv_ioctl(ctl, argp, SNDRV_CTL_TLV_OP_CMD);
1991+
snd_power_unref(card);
19781992
return err;
19791993
case SNDRV_CTL_IOCTL_POWER:
19801994
return -ENOPROTOOPT;

0 commit comments

Comments
 (0)