Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH] ALSA: Fix dynamic major number assignment
@ 2026-11-25  7:30 Sreeraj S Kurup
  2026-11-25  7:41 ` Takashi Iwai
  0 siblings, 1 reply; 7+ messages in thread
From: Sreeraj S Kurup @ 2026-11-25  7:30 UTC (permalink / raw)
  To: perex, tiwai; +Cc: linux-kernel, linux-sound, Sreeraj S Kurup

When major is set to 0, register_chrdev() allocates
a major number and returns it.

Store the allocated major number and update snd_major
after successful registration.

This ensures ALSA uses the correct major number for
device registration and unregistration.

Signed-off-by: Sreeraj S Kurup <sreekuttan2156239@gmail.com>
---
 sound/core/sound.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/sound/core/sound.c b/sound/core/sound.c
index 24f40763a20c..eaead62cf814 100644
--- a/sound/core/sound.c
+++ b/sound/core/sound.c
@@ -396,7 +396,6 @@ static int __init alsa_sound_init(void)
 {
 	int err;
 
-	snd_major = major;
 	snd_ecards_limit = cards_limit;
 
 	err = register_chrdev(major, "alsa", &snd_fops);
@@ -405,6 +404,10 @@ static int __init alsa_sound_init(void)
 		return err;
 	}
 
+	if (major == 0)
+		major = err;
+	snd_major = major;
+
 	err = snd_info_init();
 	if (err < 0) {
 		unregister_chrdev(major, "alsa");
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] ALSA: Fix dynamic major number assignment
  2026-11-25  7:30 [PATCH] ALSA: Fix dynamic major number assignment Sreeraj S Kurup
@ 2026-11-25  7:41 ` Takashi Iwai
  2026-11-25  8:24   ` Sreeraj S Kurup
                     ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Takashi Iwai @ 2026-11-25  7:41 UTC (permalink / raw)
  To: Sreeraj S Kurup; +Cc: perex, tiwai, linux-kernel, linux-sound

On Wed, 25 Nov 2026 08:30:09 +0100,
Sreeraj S Kurup wrote:
> 
> When major is set to 0, register_chrdev() allocates
> a major number and returns it.
> 
> Store the allocated major number and update snd_major
> after successful registration.
> 
> This ensures ALSA uses the correct major number for
> device registration and unregistration.
> 
> Signed-off-by: Sreeraj S Kurup <sreekuttan2156239@gmail.com>

We never thought of the dynamic major assignment there.
So major=0 should be treated simply as an error before passing to
register_chrdev().

Moreover, the current code was written before module_param_named() is
used widely, and that's why it has unnecessary assignments.
That is, we can drop major and cards_limit variables, and directly use
snd_major and snd_ecards_limit with initialization and
module_param_named().


thanks,

Takashi

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] ALSA: Fix dynamic major number assignment
  2026-11-25  7:41 ` Takashi Iwai
@ 2026-11-25  8:24   ` Sreeraj S Kurup
  2026-11-25  8:30   ` [PATCH v2] ALSA: core: Reject zero major number Sreeraj S Kurup
  2026-11-25  8:43   ` [PATCH v3] " Sreeraj S Kurup
  2 siblings, 0 replies; 7+ messages in thread
From: Sreeraj S Kurup @ 2026-11-25  8:24 UTC (permalink / raw)
  To: tiwai; +Cc: linux-kernel, linux-sound, perex, sreekuttan2156239

Hi Takashi,

Thanks for your review.

I've updated the patch according to your suggestions. It now rejects major=0 before 
calling register_chrdev() and removes the redundant major and cards_limit variables, using 
snd_major and snd_ecards_limit directly with module_param_named().

Please review the updated version.

Best regards,
Sreeraj

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2] ALSA: core: Reject zero major number
  2026-11-25  7:41 ` Takashi Iwai
  2026-11-25  8:24   ` Sreeraj S Kurup
@ 2026-11-25  8:30   ` Sreeraj S Kurup
  2026-11-25  8:38     ` sashiko-bot
  2026-11-25  8:43   ` [PATCH v3] " Sreeraj S Kurup
  2 siblings, 1 reply; 7+ messages in thread
From: Sreeraj S Kurup @ 2026-11-25  8:30 UTC (permalink / raw)
  To: tiwai; +Cc: linux-kernel, linux-sound, perex, sreekuttan2156239

Reject a zero major number before calling register_chrdev(),
as ALSA expects a fixed major number.

Remove the redundant major and cards_limit variables and
use snd_major and snd_ecards_limit directly with
module_param_named().

Update device number creation and character device
registration and unregistration to use snd_major.

Signed-off-by: Sreeraj S Kurup <sreekuttan2156239@gmail.com>
---
v2:
 - Reject major=0 before calling register_chrdev().
 - Remove redundant major and cards_limit variables.
 - Use snd_major and snd_ecards_limit directly with module_param_named().
 - Update device number creation, registration and unregistration to use snd_major.

 sound/core/sound.c | 36 +++++++++++++++++-------------------
 1 file changed, 17 insertions(+), 19 deletions(-)

diff --git a/sound/core/sound.c b/sound/core/sound.c
index 24f40763a20c..8ac3cee19e66 100644
--- a/sound/core/sound.c
+++ b/sound/core/sound.c
@@ -18,28 +18,25 @@
 #include <linux/kmod.h>
 #include <linux/mutex.h>
 
-static int major = CONFIG_SND_MAJOR;
-int snd_major;
+int snd_major = CONFIG_SND_MAJOR;
 EXPORT_SYMBOL(snd_major);
 
-static int cards_limit = 1;
+/* this one holds the actual max. card number currently available.
+ * as default, it's identical with cards_limit option.  when more
+ * modules are loaded manually, this limit number increases, too.
+ */
+int snd_ecards_limit = 1;
+EXPORT_SYMBOL(snd_ecards_limit);
 
 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
 MODULE_DESCRIPTION("Advanced Linux Sound Architecture driver for soundcards.");
 MODULE_LICENSE("GPL");
-module_param(major, int, 0444);
+module_param_named(major, snd_major, int, 0444);
 MODULE_PARM_DESC(major, "Major # for sound driver.");
-module_param(cards_limit, int, 0444);
+module_param_named(cards_limit, snd_ecards_limit, int, 0444);
 MODULE_PARM_DESC(cards_limit, "Count of auto-loadable soundcards.");
 MODULE_ALIAS_CHARDEV_MAJOR(CONFIG_SND_MAJOR);
 
-/* this one holds the actual max. card number currently available.
- * as default, it's identical with cards_limit option.  when more
- * modules are loaded manually, this limit number increases, too.
- */
-int snd_ecards_limit;
-EXPORT_SYMBOL(snd_ecards_limit);
-
 #ifdef CONFIG_SND_DEBUG
 struct dentry *sound_debugfs_root;
 EXPORT_SYMBOL_GPL(sound_debugfs_root);
@@ -61,7 +58,7 @@ void snd_request_card(int card)
 {
 	if (snd_card_locked(card))
 		return;
-	if (card < 0 || card >= cards_limit)
+	if (card < 0 || card >= snd_ecards_limit)
 		return;
 	request_module("snd-card-%i", card);
 }
@@ -280,7 +277,7 @@ int snd_register_device(int type, struct snd_card *card, int dev,
 	}
 
 	preg->dev = device;
-	device->devt = MKDEV(major, minor);
+	device->devt = MKDEV(snd_major, minor);
 	err = device_add(device);
 	if (err < 0)
 		goto error;
@@ -396,18 +393,19 @@ static int __init alsa_sound_init(void)
 {
 	int err;
 
-	snd_major = major;
-	snd_ecards_limit = cards_limit;
+	if (!snd_major)
+		return -EINVAL;
 
-	err = register_chrdev(major, "alsa", &snd_fops);
+	err = register_chrdev(snd_major, "alsa", &snd_fops);
 	if (err < 0) {
-		pr_err("ALSA core: unable to register native major device number %d\n", major);
+		pr_err("ALSA core: unable to register native major device number %d\n",
+		       snd_major);
 		return err;
 	}
 
 	err = snd_info_init();
 	if (err < 0) {
-		unregister_chrdev(major, "alsa");
+		unregister_chrdev(snd_major, "alsa");
 		return err;
 	}
 
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] ALSA: core: Reject zero major number
  2026-11-25  8:30   ` [PATCH v2] ALSA: core: Reject zero major number Sreeraj S Kurup
@ 2026-11-25  8:38     ` sashiko-bot
  0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-25  8:38 UTC (permalink / raw)
  To: Sreeraj S Kurup; +Cc: linux-sound

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] ALSA: core: undeclared identifier major in alsa_sound_exit()
- [Medium] ALSA: core: merging cards_limit with snd_ecards_limit breaks
  autoload limit
--

commit 9a3f490adfcc49e86e9645ef8d568109e4bac5c8
Author: Sreeraj S Kurup <sreekuttan2156239@gmail.com>

ALSA: core: Reject zero major number

This commit rejects a zero major number in alsa_sound_init() before calling
register_chrdev(), removes the static major and cards_limit variables, and
binds the module parameters directly to snd_major and snd_ecards_limit.

> diff --git a/sound/core/sound.c b/sound/core/sound.c
> index ba226aa7affd3..ff459d8f8aa78 100644
> --- a/sound/core/sound.c
> +++ b/sound/core/sound.c
> @@ -18,28 +18,25 @@
>  #include <linux/kmod.h>
>  #include <linux/mutex.h>
>  
> -static int major = CONFIG_SND_MAJOR;
> -int snd_major;
> +int snd_major = CONFIG_SND_MAJOR;
>  EXPORT_SYMBOL(snd_major);
>  
> -static int cards_limit = 1;
> +/* this one holds the actual max. card number currently available.
> + * as default, it's identical with cards_limit option.  when more
> + * modules are loaded manually, this limit number increases, too.
> + */
> +int snd_ecards_limit = 1;
> +EXPORT_SYMBOL(snd_ecards_limit);
>  
>  MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
>  MODULE_DESCRIPTION("Advanced Linux Sound Architecture driver for soundcards.");
>  MODULE_LICENSE("GPL");
> -module_param(major, int, 0444);
> +module_param_named(major, snd_major, int, 0444);
>  MODULE_PARM_DESC(major, "Major # for sound driver.");
> -module_param(cards_limit, int, 0444);
> +module_param_named(cards_limit, snd_ecards_limit, int, 0444);
>  MODULE_PARM_DESC(cards_limit, "Count of auto-loadable soundcards.");
>  MODULE_ALIAS_CHARDEV_MAJOR(CONFIG_SND_MAJOR);
>  
> -/* this one holds the actual max. card number currently available.
> - * as default, it's identical with cards_limit option.  when more
> - * modules are loaded manually, this limit number increases, too.
> - */
> -int snd_ecards_limit;
> -EXPORT_SYMBOL(snd_ecards_limit);
> -
>  #ifdef CONFIG_SND_DEBUG
>  struct dentry *sound_debugfs_root;
>  EXPORT_SYMBOL_GPL(sound_debugfs_root);
> @@ -61,7 +58,7 @@ void snd_request_card(int card)
>  {
>  	if (snd_card_locked(card))
>  		return;
> -	if (card < 0 || card >= cards_limit)
> +	if (card < 0 || card >= snd_ecards_limit)
>  		return;
>  	request_module("snd-card-%i", card);
>  }

[Severity: Medium]
Does replacing cards_limit with snd_ecards_limit break the auto-load card
limit?

As noted in the comment above snd_ecards_limit, snd_card_init() raises
snd_ecards_limit whenever a card is registered at an index >=
snd_ecards_limit:

sound/core/init.c:snd_card_init() {
    ...
		if (!err) {
			set_bit(idx, snd_cards_lock);		/* lock it */
			if (idx >= snd_ecards_limit)
				snd_ecards_limit = idx + 1; /* increase the limit */
		}
    ...
}

Once a card is registered at an index above the configured cards_limit,
snd_ecards_limit grows to idx + 1.  snd_request_card() will then call
request_module("snd-card-%i", card) for any free or unloaded slot up to the
new snd_ecards_limit instead of stopping at the configured cards_limit, and
/sys/module/snd/parameters/cards_limit will report the mutated runtime
value.

[ ... ]

> @@ -442,18 +439,19 @@ static int __init alsa_sound_init(void)
>  {
>  	int err;
>  
> -	snd_major = major;
> -	snd_ecards_limit = cards_limit;
> +	if (!snd_major)
> +		return -EINVAL;
>  
> -	err = register_chrdev(major, "alsa", &snd_fops);
> +	err = register_chrdev(snd_major, "alsa", &snd_fops);
>  	if (err < 0) {
> -		pr_err("ALSA core: unable to register native major device number %d\n", major);
> +		pr_err("ALSA core: unable to register native major device number %d\n",
> +		       snd_major);
>  		return err;
>  	}
>  
>  	err = snd_info_init();
>  	if (err < 0) {
> -		unregister_chrdev(major, "alsa");
> +		unregister_chrdev(snd_major, "alsa");
>  		return err;
>  	}

[Severity: High]
Does alsa_sound_exit() fail to compile now that major is removed?

sound/core/sound.c:alsa_sound_exit() {
    ...
	snd_info_done();
	unregister_chrdev(major, "alsa");
}

alsa_sound_exit() still passes major instead of snd_major to
unregister_chrdev(), causing an undeclared identifier build error.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125083053.3917-1-sreekuttan2156239@gmail.com?part=1

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v3] ALSA: core: Reject zero major number
  2026-11-25  7:41 ` Takashi Iwai
  2026-11-25  8:24   ` Sreeraj S Kurup
  2026-11-25  8:30   ` [PATCH v2] ALSA: core: Reject zero major number Sreeraj S Kurup
@ 2026-11-25  8:43   ` Sreeraj S Kurup
  2026-11-25  8:52     ` sashiko-bot
  2 siblings, 1 reply; 7+ messages in thread
From: Sreeraj S Kurup @ 2026-11-25  8:43 UTC (permalink / raw)
  To: tiwai; +Cc: linux-kernel, linux-sound, perex, sreekuttan2156239

Reject a zero major number before calling register_chrdev(),
as ALSA expects a fixed major number.

Remove the redundant major and cards_limit variables and
use snd_major and snd_ecards_limit directly with
module_param_named().

Update device number creation and character device
registration and unregistration to use snd_major.

Signed-off-by: Sreeraj S Kurup <sreekuttan2156239@gmail.com>
---
v3:
 - Update the unregister_chrdev() call in alsa_sound_exit() to use snd_major.
v2:
 - Reject major=0 before calling register_chrdev().
 - Remove redundant major and cards_limit variables.
 - Use snd_major and snd_ecards_limit directly with module_param_named().
 - Update device number creation, registration and unregistration to use snd_major.

 sound/core/sound.c | 38 ++++++++++++++++++--------------------
 1 file changed, 18 insertions(+), 20 deletions(-)

diff --git a/sound/core/sound.c b/sound/core/sound.c
index 24f40763a20c..4c71f2bf440a 100644
--- a/sound/core/sound.c
+++ b/sound/core/sound.c
@@ -18,28 +18,25 @@
 #include <linux/kmod.h>
 #include <linux/mutex.h>
 
-static int major = CONFIG_SND_MAJOR;
-int snd_major;
+int snd_major = CONFIG_SND_MAJOR;
 EXPORT_SYMBOL(snd_major);
 
-static int cards_limit = 1;
+/* this one holds the actual max. card number currently available.
+ * as default, it's identical with cards_limit option.  when more
+ * modules are loaded manually, this limit number increases, too.
+ */
+int snd_ecards_limit = 1;
+EXPORT_SYMBOL(snd_ecards_limit);
 
 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
 MODULE_DESCRIPTION("Advanced Linux Sound Architecture driver for soundcards.");
 MODULE_LICENSE("GPL");
-module_param(major, int, 0444);
+module_param_named(major, snd_major, int, 0444);
 MODULE_PARM_DESC(major, "Major # for sound driver.");
-module_param(cards_limit, int, 0444);
+module_param_named(cards_limit, snd_ecards_limit, int, 0444);
 MODULE_PARM_DESC(cards_limit, "Count of auto-loadable soundcards.");
 MODULE_ALIAS_CHARDEV_MAJOR(CONFIG_SND_MAJOR);
 
-/* this one holds the actual max. card number currently available.
- * as default, it's identical with cards_limit option.  when more
- * modules are loaded manually, this limit number increases, too.
- */
-int snd_ecards_limit;
-EXPORT_SYMBOL(snd_ecards_limit);
-
 #ifdef CONFIG_SND_DEBUG
 struct dentry *sound_debugfs_root;
 EXPORT_SYMBOL_GPL(sound_debugfs_root);
@@ -61,7 +58,7 @@ void snd_request_card(int card)
 {
 	if (snd_card_locked(card))
 		return;
-	if (card < 0 || card >= cards_limit)
+	if (card < 0 || card >= snd_ecards_limit)
 		return;
 	request_module("snd-card-%i", card);
 }
@@ -280,7 +277,7 @@ int snd_register_device(int type, struct snd_card *card, int dev,
 	}
 
 	preg->dev = device;
-	device->devt = MKDEV(major, minor);
+	device->devt = MKDEV(snd_major, minor);
 	err = device_add(device);
 	if (err < 0)
 		goto error;
@@ -396,18 +393,19 @@ static int __init alsa_sound_init(void)
 {
 	int err;
 
-	snd_major = major;
-	snd_ecards_limit = cards_limit;
+	if (!snd_major)
+		return -EINVAL;
 
-	err = register_chrdev(major, "alsa", &snd_fops);
+	err = register_chrdev(snd_major, "alsa", &snd_fops);
 	if (err < 0) {
-		pr_err("ALSA core: unable to register native major device number %d\n", major);
+		pr_err("ALSA core: unable to register native major device number %d\n",
+		       snd_major);
 		return err;
 	}
 
 	err = snd_info_init();
 	if (err < 0) {
-		unregister_chrdev(major, "alsa");
+		unregister_chrdev(snd_major, "alsa");
 		return err;
 	}
 
@@ -426,7 +424,7 @@ static void __exit alsa_sound_exit(void)
 	debugfs_remove(sound_debugfs_root);
 #endif
 	snd_info_done();
-	unregister_chrdev(major, "alsa");
+	unregister_chrdev(snd_major, "alsa");
 }
 
 subsys_initcall(alsa_sound_init);
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v3] ALSA: core: Reject zero major number
  2026-11-25  8:43   ` [PATCH v3] " Sreeraj S Kurup
@ 2026-11-25  8:52     ` sashiko-bot
  0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-25  8:52 UTC (permalink / raw)
  To: Sreeraj S Kurup; +Cc: linux-sound

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] ALSA: core: using snd_ecards_limit for cards_limit breaks
  snd_request_card()
--

commit 09efa67fd5336064a9c9600571b9d55c3d75ea0a
Author: Sreeraj S Kurup <sreekuttan2156239@gmail.com>

ALSA: core: Reject zero major number

This commit rejects a zero major number in alsa_sound_init() and replaces
the static major and cards_limit variables with snd_major and
snd_ecards_limit via module_param_named().

> diff --git a/sound/core/sound.c b/sound/core/sound.c
> index ba226aa7affd3..746a5e254cbb8 100644
> --- a/sound/core/sound.c
> +++ b/sound/core/sound.c
> @@ -18,28 +18,25 @@
>  #include <linux/kmod.h>
>  #include <linux/mutex.h>
>  
> -static int major = CONFIG_SND_MAJOR;
> -int snd_major;
> +int snd_major = CONFIG_SND_MAJOR;
>  EXPORT_SYMBOL(snd_major);
>  
> -static int cards_limit = 1;
> +/* this one holds the actual max. card number currently available.
> + * as default, it's identical with cards_limit option.  when more
> + * modules are loaded manually, this limit number increases, too.
> + */
> +int snd_ecards_limit = 1;
> +EXPORT_SYMBOL(snd_ecards_limit);
>  
>  MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
>  MODULE_DESCRIPTION("Advanced Linux Sound Architecture driver for soundcards.");
>  MODULE_LICENSE("GPL");
> -module_param(major, int, 0444);
> +module_param_named(major, snd_major, int, 0444);
>  MODULE_PARM_DESC(major, "Major # for sound driver.");
> -module_param(cards_limit, int, 0444);
> +module_param_named(cards_limit, snd_ecards_limit, int, 0444);
>  MODULE_PARM_DESC(cards_limit, "Count of auto-loadable soundcards.");
>  MODULE_ALIAS_CHARDEV_MAJOR(CONFIG_SND_MAJOR);
>  
> -/* this one holds the actual max. card number currently available.
> - * as default, it's identical with cards_limit option.  when more
> - * modules are loaded manually, this limit number increases, too.
> - */
> -int snd_ecards_limit;
> -EXPORT_SYMBOL(snd_ecards_limit);
> -
>  #ifdef CONFIG_SND_DEBUG
>  struct dentry *sound_debugfs_root;
>  EXPORT_SYMBOL_GPL(sound_debugfs_root);
> @@ -61,7 +58,7 @@ void snd_request_card(int card)
>  {
>  	if (snd_card_locked(card))
>  		return;
> -	if (card < 0 || card >= cards_limit)
> +	if (card < 0 || card >= snd_ecards_limit)
>  		return;
>  	request_module("snd-card-%i", card);
>  }

[Severity: Medium]
Does replacing cards_limit with snd_ecards_limit raise the auto-load limit
at runtime and mutate the cards_limit module parameter?

snd_card_init() increases snd_ecards_limit whenever a card is registered
at an index at or above it:

sound/core/init.c:snd_card_init() {
	...
			if (idx >= snd_ecards_limit)
				snd_ecards_limit = idx + 1; /* increase the limit */
	...
}

Once a second card or a card with an explicit index >= cards_limit is
registered, snd_ecards_limit grows to idx + 1 and is never lowered on
disconnect.  snd_request_card() will then invoke
request_module("snd-card-%i", card) for unoccupied or disconnected card
slots above the configured cards_limit, and
/sys/module/snd/parameters/cards_limit will report the mutated runtime
high-water mark instead of the configured limit.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125084324.4151-1-sreekuttan2156239@gmail.com?part=1

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-25  8:52 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25  7:30 [PATCH] ALSA: Fix dynamic major number assignment Sreeraj S Kurup
2026-11-25  7:41 ` Takashi Iwai
2026-11-25  8:24   ` Sreeraj S Kurup
2026-11-25  8:30   ` [PATCH v2] ALSA: core: Reject zero major number Sreeraj S Kurup
2026-11-25  8:38     ` sashiko-bot
2026-11-25  8:43   ` [PATCH v3] " Sreeraj S Kurup
2026-11-25  8:52     ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox