Skip to content

Commit f4fa968

Browse files
committed
ALSA: core: Don't refer to snd_cards array directly
The snd_cards[] array holds the card pointers that have been currently registered, and it's exported for the external modules that may need to refer a card object. But accessing to this array can be racy against the driver probe or removal, as the card registration or free may happen concurrently. This patch gets rid of the direct access to snd_cards[] array and provides a helper function to give the card object from the index number with a refcount management. Then the caller can access to the given card object safely, and releases it via snd_card_unref(). While we're at it, add a proper comment to snd_card_unref() and make it an inlined function for type-safety, too. Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent c989954 commit f4fa968

5 files changed

Lines changed: 58 additions & 12 deletions

File tree

‎include/sound/core.h‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ int copy_from_user_toio(volatile void __iomem *dst, const void __user *src, size
226226

227227
/* init.c */
228228

229-
extern struct snd_card *snd_cards[SNDRV_CARDS];
230229
int snd_card_locked(int card);
231230
#if IS_ENABLED(CONFIG_SND_MIXER_OSS)
232231
#define SND_MIXER_OSS_NOTIFY_REGISTER 0
@@ -251,7 +250,20 @@ int snd_card_add_dev_attr(struct snd_card *card,
251250
int snd_component_add(struct snd_card *card, const char *component);
252251
int snd_card_file_add(struct snd_card *card, struct file *file);
253252
int snd_card_file_remove(struct snd_card *card, struct file *file);
254-
#define snd_card_unref(card) put_device(&(card)->card_dev)
253+
254+
struct snd_card *snd_card_ref(int card);
255+
256+
/**
257+
* snd_card_unref - Unreference the card object
258+
* @card: the card object to unreference
259+
*
260+
* Call this function for the card object that was obtained via snd_card_ref()
261+
* or snd_lookup_minor_data().
262+
*/
263+
static inline void snd_card_unref(struct snd_card *card)
264+
{
265+
put_device(&card->card_dev);
266+
}
255267

256268
#define snd_card_set_dev(card, devptr) ((card)->dev = (devptr))
257269

‎sound/core/init.c‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ static const struct file_operations snd_shutdown_f_ops;
4949

5050
/* locked for registering/using */
5151
static DECLARE_BITMAP(snd_cards_lock, SNDRV_CARDS);
52-
struct snd_card *snd_cards[SNDRV_CARDS];
53-
EXPORT_SYMBOL(snd_cards);
52+
static struct snd_card *snd_cards[SNDRV_CARDS];
5453

5554
static DEFINE_MUTEX(snd_card_mutex);
5655

@@ -268,6 +267,26 @@ int snd_card_new(struct device *parent, int idx, const char *xid,
268267
}
269268
EXPORT_SYMBOL(snd_card_new);
270269

270+
/**
271+
* snd_card_ref - Get the card object from the index
272+
* @idx: the card index
273+
*
274+
* Returns a card object corresponding to the given index or NULL if not found.
275+
* Release the object via snd_card_unref().
276+
*/
277+
struct snd_card *snd_card_ref(int idx)
278+
{
279+
struct snd_card *card;
280+
281+
mutex_lock(&snd_card_mutex);
282+
card = snd_cards[idx];
283+
if (card)
284+
get_device(&card->card_dev);
285+
mutex_unlock(&snd_card_mutex);
286+
return card;
287+
}
288+
EXPORT_SYMBOL_GPL(snd_card_ref);
289+
271290
/* return non-zero if a card is already locked */
272291
int snd_card_locked(int card)
273292
{

‎sound/core/oss/mixer_oss.c‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,24 +1403,32 @@ static int snd_mixer_oss_notify_handler(struct snd_card *card, int cmd)
14031403

14041404
static int __init alsa_mixer_oss_init(void)
14051405
{
1406+
struct snd_card *card;
14061407
int idx;
14071408

14081409
snd_mixer_oss_notify_callback = snd_mixer_oss_notify_handler;
14091410
for (idx = 0; idx < SNDRV_CARDS; idx++) {
1410-
if (snd_cards[idx])
1411-
snd_mixer_oss_notify_handler(snd_cards[idx], SND_MIXER_OSS_NOTIFY_REGISTER);
1411+
card = snd_card_ref(idx);
1412+
if (card) {
1413+
snd_mixer_oss_notify_handler(card, SND_MIXER_OSS_NOTIFY_REGISTER);
1414+
snd_card_unref(card);
1415+
}
14121416
}
14131417
return 0;
14141418
}
14151419

14161420
static void __exit alsa_mixer_oss_exit(void)
14171421
{
1422+
struct snd_card *card;
14181423
int idx;
14191424

14201425
snd_mixer_oss_notify_callback = NULL;
14211426
for (idx = 0; idx < SNDRV_CARDS; idx++) {
1422-
if (snd_cards[idx])
1423-
snd_mixer_oss_notify_handler(snd_cards[idx], SND_MIXER_OSS_NOTIFY_FREE);
1427+
card = snd_card_ref(idx);
1428+
if (card) {
1429+
snd_mixer_oss_notify_handler(card, SND_MIXER_OSS_NOTIFY_FREE);
1430+
snd_card_unref(card);
1431+
}
14241432
}
14251433
}
14261434

‎sound/core/sound.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,11 @@ static struct snd_minor *autoload_device(unsigned int minor)
134134
if (dev == SNDRV_MINOR_CONTROL) {
135135
/* /dev/aloadC? */
136136
int card = SNDRV_MINOR_CARD(minor);
137-
if (snd_cards[card] == NULL)
137+
struct snd_card *ref = snd_card_ref(card);
138+
if (!ref)
138139
snd_request_card(card);
140+
else
141+
snd_card_unref(ref);
139142
} else if (dev == SNDRV_MINOR_GLOBAL) {
140143
/* /dev/aloadSEQ */
141144
snd_request_other(minor);

‎sound/last.c‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@
2424

2525
static int __init alsa_sound_last_init(void)
2626
{
27+
struct snd_card *card;
2728
int idx, ok = 0;
2829

2930
printk(KERN_INFO "ALSA device list:\n");
30-
for (idx = 0; idx < SNDRV_CARDS; idx++)
31-
if (snd_cards[idx] != NULL) {
32-
printk(KERN_INFO " #%i: %s\n", idx, snd_cards[idx]->longname);
31+
for (idx = 0; idx < SNDRV_CARDS; idx++) {
32+
card = snd_card_ref(idx);
33+
if (card) {
34+
printk(KERN_INFO " #%i: %s\n", idx, card->longname);
35+
snd_card_unref(card);
3336
ok++;
3437
}
38+
}
3539
if (ok == 0)
3640
printk(KERN_INFO " No soundcards found.\n");
3741
return 0;

0 commit comments

Comments
 (0)