Skip to contentSkip to conversation

ALSA: Allow NULL passed to snd_ctl_remove()

PATCH In review Takashi Iwai 1 participant 7 messages Jun 17, 2024 – Jun 18, 2024 Sound

lore
Share & tools
lore
#0 lore raw

cover ALSA: Allow NULL passed to snd_ctl_remove()

This is a small series of cleanup patches for the call patterns with snd_ctl_remove() and NULL kcontrol.
Now snd_ctl_remove() accepts the NULL argument just like free() or kfree(), so that the callers can be cleaned up without NULL checks.

No functional changes should be introduced by this patch set.

Takashi

===

Takashi Iwai (5):
ALSA: control: Allow NULL passed to snd_ctl_remove()
ALSA: sb: Drop NULL check for snd_ctl_remove()
ALSA: emu10k1: Drop NULL check for snd_ctl_remove()
ALSA: hda: Drop NULL check for snd_ctl_remove()
ALSA: spi: Drop NULL check for snd_ctl_remove()

 sound/core/control.c            |  3 +++
 sound/isa/sb/emu8000.c          |  6 ++----
 sound/isa/sb/sb16_csp.c         | 12 ++++--------
 sound/pci/emu10k1/emufx.c       |  5 +----
 sound/pci/hda/hda_cs_dsp_ctl.c  |  2 +-
 sound/pci/hda/tas2781_hda_i2c.c | 13 ++++---------
 sound/spi/at73c213.c            |  8 ++------
 7 files changed, 17 insertions(+), 32 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/5 ALSA: control: Allow NULL passed to snd_ctl_remove()

There are lots of code checking NULL for kcontrol passed to snd_ctl_remove() in the caller side. Let's make snd_ctl_remove() accepting the NULL kcontrol instead a la free(), so that we can clean up the caller side.

---
 sound/core/control.c | 3 +++
 1 file changed, 3 insertions(+)
sound/core/control.c +3 -0
index fb0c60044f7b..7f0999d1671e 100644
@@ -604,6 +604,7 @@ static inline int snd_ctl_remove_locked(struct snd_card *card,
  *
  * Removes the control from the card and then releases the instance.
  * You don't need to call snd_ctl_free_one().
+ * Passing NULL to @kcontrol argument is allowed as noop.
  *
  * Return: 0 if successful, or a negative error code on failure.
  *
@@ -611,6 +612,8 @@ static inline int snd_ctl_remove_locked(struct snd_card *card,
  */
 int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
 {
+	if (!kcontrol)
+		return 0;
 	guard(rwsem_write)(&card->controls_rwsem);
 	return snd_ctl_remove_locked(card, kcontrol);
 }
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/5 ALSA: sb: Drop NULL check for snd_ctl_remove()

In reply to Takashi Iwai

Since snd_ctl_remove() accepts the NULL kcontrol argument now, we can drop the check in the caller side.

---
 sound/isa/sb/emu8000.c  |  6 ++----
 sound/isa/sb/sb16_csp.c | 12 ++++--------
 2 files changed, 6 insertions(+), 12 deletions(-)
sound/isa/sb/emu8000.c +2 -4
index a6405772d537..af478c36ce5b 100644
@@ -1039,10 +1039,8 @@ snd_emu8000_create_mixer(struct snd_card *card, struct snd_emu8000 *emu)
 	return 0;
 
 __error:
-	for (i = 0; i < EMU8000_NUM_CONTROLS; i++) {
-		if (emu->controls[i])
-			snd_ctl_remove(card, emu->controls[i]);
-	}
+	for (i = 0; i < EMU8000_NUM_CONTROLS; i++)
+		snd_ctl_remove(card, emu->controls[i]);
 	return err;
 }
 
sound/isa/sb/sb16_csp.c +4 -8
index 8d8357019719..fdb992733bde 100644
@@ -1080,14 +1080,10 @@ static void snd_sb_qsound_destroy(struct snd_sb_csp * p)
 
 	card = p->chip->card;	
 	
-	if (p->qsound_switch) {
-		snd_ctl_remove(card, p->qsound_switch);
-		p->qsound_switch = NULL;
-	}
-	if (p->qsound_space) {
-		snd_ctl_remove(card, p->qsound_space);
-		p->qsound_space = NULL;
-	}
+	snd_ctl_remove(card, p->qsound_switch);
+	p->qsound_switch = NULL;
+	snd_ctl_remove(card, p->qsound_space);
+	p->qsound_space = NULL;
 
 	/* cancel pending transfer of QSound parameters */
 	spin_lock_irqsave (&p->q_lock, flags);
Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/5 ALSA: emu10k1: Drop NULL check for snd_ctl_remove()

In reply to Takashi Iwai

Since snd_ctl_remove() accepts the NULL kcontrol argument now, we can drop the check in the caller side.

---
 sound/pci/emu10k1/emufx.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
sound/pci/emu10k1/emufx.c +1 -4
index 03efc317e05f..34f385b5cd46 100644
@@ -959,7 +959,6 @@ static int snd_emu10k1_del_controls(struct snd_emu10k1 *emu,
 {
 	unsigned int i;
 	struct emu10k1_ctl_elem_id id;
-	struct snd_emu10k1_fx8010_ctl *ctl;
 	struct snd_card *card = emu->card;
 	int err;
 	
@@ -968,9 +967,7 @@ static int snd_emu10k1_del_controls(struct snd_emu10k1 *emu,
 				       in_kernel);
 		if (err < 0)
 			return err;
-		ctl = snd_emu10k1_look_for_ctl(emu, &id);
-		if (ctl)
-			snd_ctl_remove(card, ctl->kcontrol);
+		snd_ctl_remove(card, snd_emu10k1_look_for_ctl(emu, &id));
 	}
 	return 0;
 }
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH 3/5] ALSA: emu10k1: Drop NULL check for snd_ctl_remove()

Quoted 5 lines · Takashi Iwai

Since snd_ctl_remove() accepts the NULL kcontrol argument now, we can drop the check in the caller side.

Grrr, this is utterly broken. Scratch this one.
Sorry for the noise.

Takashi

Quoted 30 lines
---
 sound/pci/emu10k1/emufx.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
sound/pci/emu10k1/emufx.c +1 -4
index 03efc317e05f..34f385b5cd46 100644
@@ -959,7 +959,6 @@ static int snd_emu10k1_del_controls(struct snd_emu10k1 *emu,
 {
 	unsigned int i;
 	struct emu10k1_ctl_elem_id id;
-	struct snd_emu10k1_fx8010_ctl *ctl;
 	struct snd_card *card = emu->card;
 	int err;
 
@@ -968,9 +967,7 @@ static int snd_emu10k1_del_controls(struct snd_emu10k1 *emu,
 				       in_kernel);
 		if (err < 0)
 			return err;
-		ctl = snd_emu10k1_look_for_ctl(emu, &id);
-		if (ctl)
-			snd_ctl_remove(card, ctl->kcontrol);
+		snd_ctl_remove(card, snd_emu10k1_look_for_ctl(emu, &id));
 	}
 	return 0;
 }
Signature · 1 line
2.43.0
#5 lore raw

PATCH 4/5 ALSA: hda: Drop NULL check for snd_ctl_remove()

In reply to Takashi Iwai

Since snd_ctl_remove() accepts the NULL kcontrol argument now, we can drop the check in the caller side.

---
 sound/pci/hda/hda_cs_dsp_ctl.c  |  2 +-
 sound/pci/hda/tas2781_hda_i2c.c | 13 ++++---------
 2 files changed, 5 insertions(+), 10 deletions(-)
sound/pci/hda/hda_cs_dsp_ctl.c +1 -1
index e6e876998e71..deb74c247082 100644
@@ -207,7 +207,7 @@ void hda_cs_dsp_control_remove(struct cs_dsp_coeff_ctl *cs_ctl)
 	struct hda_cs_dsp_coeff_ctl *ctl = cs_ctl->priv;
 
 	/* ctl and kctl may already have been removed by ALSA private_free */
-	if (ctl && ctl->kctl)
+	if (ctl)
 		snd_ctl_remove(ctl->card, ctl->kctl);
 }
 EXPORT_SYMBOL_NS_GPL(hda_cs_dsp_control_remove, SND_HDA_CS_DSP_CONTROLS);
sound/pci/hda/tas2781_hda_i2c.c +4 -9
index 75f7674c66ee..c878967cb66a 100644
@@ -597,18 +597,13 @@ static void tas2781_hda_remove_controls(struct tas2781_hda *tas_hda)
 {
 	struct hda_codec *codec = tas_hda->priv->codec;
 
-	if (tas_hda->dsp_prog_ctl)
-		snd_ctl_remove(codec->card, tas_hda->dsp_prog_ctl);
-
-	if (tas_hda->dsp_conf_ctl)
-		snd_ctl_remove(codec->card, tas_hda->dsp_conf_ctl);
+	snd_ctl_remove(codec->card, tas_hda->dsp_prog_ctl);
+	snd_ctl_remove(codec->card, tas_hda->dsp_conf_ctl);
 
 	for (int i = ARRAY_SIZE(tas_hda->snd_ctls) - 1; i >= 0; i--)
-		if (tas_hda->snd_ctls[i])
-			snd_ctl_remove(codec->card, tas_hda->snd_ctls[i]);
+		snd_ctl_remove(codec->card, tas_hda->snd_ctls[i]);
 
-	if (tas_hda->prof_ctl)
-		snd_ctl_remove(codec->card, tas_hda->prof_ctl);
+	snd_ctl_remove(codec->card, tas_hda->prof_ctl);
 }
 
 static void tasdev_fw_ready(const struct firmware *fmw, void *context)
Signature · 1 line
2.43.0
#6 lore raw

PATCH 5/5 ALSA: spi: Drop NULL check for snd_ctl_remove()

In reply to Takashi Iwai

Since snd_ctl_remove() accepts the NULL kcontrol argument now, we can drop the check in the caller side.

---
 sound/spi/at73c213.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
sound/spi/at73c213.c +2 -6
index 5648d744aa79..8f6929ced2c8 100644
@@ -726,12 +726,8 @@ static int snd_at73c213_mixer(struct snd_at73c213 *chip)
 	return 0;
 
 cleanup:
-	for (idx = 1; idx < ARRAY_SIZE(snd_at73c213_controls) + 1; idx++) {
-		struct snd_kcontrol *kctl;
-		kctl = snd_ctl_find_numid(card, idx);
-		if (kctl)
-			snd_ctl_remove(card, kctl);
-	}
+	for (idx = 1; idx < ARRAY_SIZE(snd_at73c213_controls) + 1; idx++)
+		snd_ctl_remove(card, snd_ctl_find_numid(card, idx));
 	return errval;
 }
 
Signature · 1 line
2.43.0