ALSA: seq: oss: Fix data-race for max_midi_devs access
PATCH 1/2 ALSA: seq: oss: Fix data-race for max_midi_devs access
ALSA OSS sequencer refers to a global variable max_midi_devs at creating a new port, storing it to its own field. Meanwhile this variable may be changed by other sequencer events at snd_seq_oss_midi_check_exit_port() in parallel, which may cause a data race.
OTOH, this data race itself is almost harmless, as the access to the MIDI device is done via get_mdev() and it's protected with a refcount, hence its presence is guaranteed.
Though, it's sill better to address the data-race from the code sanity POV, and this patch adds the proper spinlock for the protection.
--- sound/core/seq/oss/seq_oss_midi.c | 2 ++ 1 file changed, 2 insertions(+)
sound/core/seq/oss/seq_oss_midi.c +2 -0
@@ -270,7 +270,9 @@ snd_seq_oss_midi_clear_all(void)void snd_seq_oss_midi_setup (struct seq_oss_devinfo *dp) { +spin_lock_irq (®ister_lock); dp->max_mididev = max_midi_devs; +spin_unlock_irq (®ister_lock); }/*
Signature · 1 line
2.35.3
PATCH 2/2 ALSA: seq: Fix data-race at module auto-loading
It's been reported that there is a possible data-race accessing to the global card_requested[] array at ALSA sequencer core, which is used for determining whether to call request_module() for the card or not. This data race itself is almost harmless, as it might end up with one extra request_module() call for the already loaded module at most. But it's still better to fix.
This patch addresses the possible data race of card_requested[] and client_requested[] arrays by replacing them with bitmask. It's an atomic operation and can work without locks.
--- sound/core/seq/seq_clientmgr.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)
sound/core/seq/seq_clientmgr.c +5 -7
@@ -121,13 +121,13 @@ struct snd_seq_client *snd_seq_client_use_ptr(int clientid)spin_unlock_irqrestore (&clients_lock, flags);#ifdef CONFIG_MODULES if (!in_interrupt ()) { -static char client_requested[SNDRV_SEQ_GLOBAL_CLIENTS ]; -static char card_requested[SNDRV_CARDS ]; + static DECLARE_BITMAP(client_requested, SNDRV_SEQ_GLOBAL_CLIENTS); + static DECLARE_BITMAP(card_requested, SNDRV_CARDS); +if (clientid <SNDRV_SEQ_GLOBAL_CLIENTS ) {int idx; -if (!client_requested[clientid]) { - client_requested[clientid] =1 ; +if (!test_and_set_bit (clientid, client_requested)) {for (idx =0 ; idx <15 ; idx++) {if (seq_client_load[idx] <0 )break ;
@@ -142,10 +142,8 @@ struct snd_seq_client *snd_seq_client_use_ptr(int clientid)int card = (clientid -SNDRV_SEQ_GLOBAL_CLIENTS ) /SNDRV_SEQ_CLIENTS_PER_CARD ;if (card < snd_ecards_limit) { -if (! card_requested[card]) { - card_requested[card] =1 ; +if (!test_and_set_bit (card_requested, card))snd_request_card (card); - }snd_seq_device_load_drivers (); } }
Signature · 1 line
2.35.3
Re: [PATCH 2/2] ALSA: seq: Fix data-race at module auto-loading
We have tested this patch and confirm it eliminates the race we initially reported.
Best,
Gabe
Quoted 65 lines · Takashi Iwai
It's been reported that there is a possible data-race accessing to the global card_requested[] array at ALSA sequencer core, which is used for determining whether to call
request_module()for the card or not. This data race itself is almost harmless, as it might end up with one extrarequest_module()call for the already loaded module at most. But it's still better to fix.This patch addresses the possible data race of card_requested[] and client_requested[] arrays by replacing them with bitmask. It's an atomic operation and can work without locks.
Reported-by Abhishek Shah <[email protected]>Cc <[email protected]>Signed-off-by Takashi Iwai <[email protected]>--- sound/core/seq/seq_clientmgr.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)sound/core/seq/seq_clientmgr.c +4
-4@@ -121,13 +121,13 @@ struct snd_seq_client *snd_seq_client_use_ptr(int clientid) spin_unlock_irqrestore(&clients_lock, flags);#ifdef CONFIG_MODULES if (!in_interrupt ()) { -static char client_requested[SNDRV_SEQ_GLOBAL_CLIENTS ]; -static char card_requested[SNDRV_CARDS ]; + static DECLARE_BITMAP(client_requested, SNDRV_SEQ_GLOBAL_CLIENTS); + static DECLARE_BITMAP(card_requested, SNDRV_CARDS); +if (clientid <SNDRV_SEQ_GLOBAL_CLIENTS ) {int idx; -if (!client_requested[clientid]) { - client_requested[clientid] =1 ; +if (!test_and_set_bit (clientid, client_requested)) {for (idx = 0; idx < 15; idx++) { if (seq_client_load[idx] < 0) break;sound/core/seq/seq_clientmgr.c +1
-3@@ -142,10 +142,8 @@ struct snd_seq_client *snd_seq_client_use_ptr(int clientid)int card = (clientid -SNDRV_SEQ_GLOBAL_CLIENTS ) /SNDRV_SEQ_CLIENTS_PER_CARD ;if (card < snd_ecards_limit) { -if (! card_requested[card]) { - card_requested[card] =1 ; +if (!test_and_set_bit (card_requested, card))snd_request_card (card); - }snd_seq_device_load_drivers ();} }--
2.35.3
Re: [PATCH 2/2] ALSA: seq: Fix data-race at module auto-loading
In reply to Takashi IwaiQuoted 8 lines · Takashi Iwai
It's been reported that there is a possible data-race accessing to the global card_requested[] array at ALSA sequencer core, which is used for determining whether to call
request_module()for the card or not. This data race itself is almost harmless, as it might end up with one extrarequest_module()call for the already loaded module at most. But it's still better to fix.
Quoted 8 lines
This patch addresses the possible data race of card_requested[] and client_requested[] arrays by replacing them with bitmask. It's an atomic operation and can work without locks.
Gah, there was an obvious type in the patch. The correct version is below, and I merged it now.
thanks,
Takashi
-- 8< --
From: Takashi Iwai <[email protected]>
Subject: [PATCH v2] ALSA: seq: Fix data-race at module auto-loading
It's been reported that there is a possible data-race accessing to the global card_requested[] array at ALSA sequencer core, which is used for determining whether to call request_module() for the card or not. This data race itself is almost harmless, as it might end up with one extra request_module() call for the already loaded module at most. But it's still better to fix.
This patch addresses the possible data race of card_requested[] and client_requested[] arrays by replacing them with bitmask. It's an atomic operation and can work without locks.
---v1->v2: fix compile error
sound/core/seq/seq_clientmgr.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)
sound/core/seq/seq_clientmgr.c +5 -7
@@ -121,13 +121,13 @@ struct snd_seq_client *snd_seq_client_use_ptr(int clientid)spin_unlock_irqrestore (&clients_lock, flags);#ifdef CONFIG_MODULES if (!in_interrupt ()) { -static char client_requested[SNDRV_SEQ_GLOBAL_CLIENTS ]; -static char card_requested[SNDRV_CARDS ]; + static DECLARE_BITMAP(client_requested, SNDRV_SEQ_GLOBAL_CLIENTS); + static DECLARE_BITMAP(card_requested, SNDRV_CARDS); +if (clientid <SNDRV_SEQ_GLOBAL_CLIENTS ) {int idx; -if (!client_requested[clientid]) { - client_requested[clientid] =1 ; +if (!test_and_set_bit (clientid, client_requested)) {for (idx =0 ; idx <15 ; idx++) {if (seq_client_load[idx] <0 )break ;
@@ -142,10 +142,8 @@ struct snd_seq_client *snd_seq_client_use_ptr(int clientid)int card = (clientid -SNDRV_SEQ_GLOBAL_CLIENTS ) /SNDRV_SEQ_CLIENTS_PER_CARD ;if (card < snd_ecards_limit) { -if (! card_requested[card]) { - card_requested[card] =1 ; +if (!test_and_set_bit (card, card_requested))snd_request_card (card); - }snd_seq_device_load_drivers (); } }
Signature · 1 line
2.35.3
Re: [PATCH 2/2] ALSA: seq: Fix data-race at module auto-loading
Apologies, I just realized the previous patch raised incompatible integer conversion warnings on that line during compilation. In the future I'll keep a closer eye out for relevant warnings when testing.
This patch still eliminates the race we initially reported.
Best,
Gabe
Quoted 91 lines · Takashi Iwai
Quoted 8 lines · Takashi Iwai
It's been reported that there is a possible data-race accessing to the global card_requested[] array at ALSA sequencer core, which is used for determining whether to call
request_module()for the card or not. This data race itself is almost harmless, as it might end up with one extrarequest_module()call for the already loaded module at most. But it's still better to fix.Quoted 8 lines
This patch addresses the possible data race of card_requested[] and client_requested[] arrays by replacing them with bitmask. It's an atomic operation and can work without locks.
Reported-by Abhishek Shah <[email protected]>Cc <[email protected]>Signed-off-by Takashi Iwai <[email protected]>Gah, there was an obvious type in the patch. The correct version is below, and I merged it now.
thanks,
Takashi
-- 8< --
From: Takashi Iwai <[email protected]>
Subject: [PATCH v2] ALSA: seq: Fix data-race at module auto-loadingIt's been reported that there is a possible data-race accessing to the global card_requested[] array at ALSA sequencer core, which is used for determining whether to call
request_module()for the card or not. This data race itself is almost harmless, as it might end up with one extrarequest_module()call for the already loaded module at most. But it's still better to fix.This patch addresses the possible data race of card_requested[] and client_requested[] arrays by replacing them with bitmask. It's an atomic operation and can work without locks.
Reported-by Abhishek Shah <[email protected]>Cc <[email protected]>Signed-off-by Takashi Iwai <[email protected]>---
v1->v2: fix compile errorsound/core/seq/seq_clientmgr.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)sound/core/seq/seq_clientmgr.c +5
-8@@ -121,13 +121,13 @@ struct snd_seq_client *snd_seq_client_use_ptr(int clientid)spin_unlock_irqrestore (&clients_lock, flags);#ifdef CONFIG_MODULES if (!in_interrupt ()) { -static char client_requested[SNDRV_SEQ_GLOBAL_CLIENTS ]; -static char card_requested[SNDRV_CARDS ]; + static DECLARE_BITMAP(client_requested, SNDRV_SEQ_GLOBAL_CLIENTS); + static DECLARE_BITMAP(card_requested, SNDRV_CARDS); +if (clientid <SNDRV_SEQ_GLOBAL_CLIENTS ) {int idx; -if (!client_requested[clientid]) { - client_requested[clientid] =1 ; +if (!test_and_set_bit (clientid, client_requested)) {for (idx =0 ; idx <15 ; idx++) {if (seq_client_load[idx] <0 )break ;@@ -142,10 +142,8 @@ struct snd_seq_client *snd_seq_client_use_ptr(int clientid)int card = (clientid -SNDRV_SEQ_GLOBAL_CLIENTS ) /SNDRV_SEQ_CLIENTS_PER_CARD ;if (card < snd_ecards_limit) { -if (! card_requested[card]) { - card_requested[card] =1 ; +if (!test_and_set_bit (card, card_requested))snd_request_card (card); - }snd_seq_device_load_drivers (); } } --2.35.3
Signature · 2 lines
Gabriel Ryan PhD Candidate at Columbia University
We have tested this patch and confirm it eliminates the race we observed on max_mididev.
Best,
Gabe
Quoted 42 lines · Takashi Iwai
ALSA OSS sequencer refers to a global variable
max_midi_devsat creating a new port, storing it to its own field. Meanwhile this variable may be changed by other sequencer events atsnd_seq_oss_midi_check_exit_port()in parallel, which may cause a data race.OTOH, this data race itself is almost harmless, as the access to the MIDI device is done via
get_mdev()and it's protected with a refcount, hence its presence is guaranteed.Though, it's sill better to address the data-race from the code sanity POV, and this patch adds the proper spinlock for the protection.
Reported-by Abhishek Shah <[email protected]>Cc <[email protected]>Signed-off-by Takashi Iwai <[email protected]>--- sound/core/seq/oss/seq_oss_midi.c | 2 ++ 1 file changed, 2 insertions(+)sound/core/seq/oss/seq_oss_midi.c +2
-1@@ -270,7 +270,9 @@ snd_seq_oss_midi_clear_all(void)void snd_seq_oss_midi_setup (struct seq_oss_devinfo *dp) { +spin_lock_irq (®ister_lock); dp->max_mididev = max_midi_devs; +spin_unlock_irq (®ister_lock); }/* -- 2.35.3