Skip to contentSkip to conversation

alsa-kernel, make control.c suspend aware

RFC In review Giuliano Pochini 3 participants 8 messages Mar 1, 2006 – Mar 13, 2006

lore
Share & tools
lore
#0 lore raw

RFC 1/1 alsa-kernel, make control.c suspend aware

First attempt to add code that prevents someone to access the hardware via control interface when the card is suspended.

I put the code in snd_ctl_elem_read_user() instead of snd_ctl_elem_read() because /pci/hda/hda_codec.c:snd_hda_resume_ctls() calls the latter from the resume callback. I had no time to check other things, consider it mostly untested.

core/control.c +18 -2
diff -dup alsa-kernel__orig/core/control.c alsa-kernel/core/control.c
@@ -672,6 +672,7 @@ int snd_ctl_elem_read(struct snd_card *c
 	unsigned int index_offset;
 	int result, indirect;
 
+snd_printk("snd_ctl_elem_read\n");
 	down_read(&card->controls_rwsem);
 	kctl = snd_ctl_find_id(card, &control->id);
 	if (kctl == NULL) {
@@ -708,7 +709,13 @@ static int snd_ctl_elem_read_user(struct
 		kfree(control);
 		return -EFAULT;
 	}
-	result = snd_ctl_elem_read(card, control);
+snd_printk("snd_ctl_elem_read_user1\n");
+	snd_power_lock(card);
+	result = snd_power_wait(card, SNDRV_CTL_POWER_D0, NULL);
+	if (result >= 0)
+		result = snd_ctl_elem_read(card, control);
+	snd_power_unlock(card);
+snd_printk("snd_ctl_elem_read_user2\n");
 	if (result >= 0)
 		if (copy_to_user(_control, control, sizeof(*control)))
 			result = -EFAULT;
@@ -723,6 +730,7 @@ int snd_ctl_elem_write(struct snd_card *
 	struct snd_kcontrol_volatile *vd;
 	unsigned int index_offset;
 	int result, indirect;
+snd_printk("snd_ctl_elem_write\n");
 
 	down_read(&card->controls_rwsem);
 	kctl = snd_ctl_find_id(card, &control->id);
@@ -758,6 +766,7 @@ static int snd_ctl_elem_write_user(struc
 				   struct snd_ctl_elem_value __user *_control)
 {
 	struct snd_ctl_elem_value *control;
+	struct snd_card *card;
 	int result;
 
 	control = kmalloc(sizeof(*control), GFP_KERNEL);
@@ -767,7 +776,14 @@ static int snd_ctl_elem_write_user(struc
 		kfree(control);
 		return -EFAULT;
 	}
-	result = snd_ctl_elem_write(file->card, file, control);
+	card = file->card;
+snd_printk("snd_ctl_elem_write_user1\n");
+	snd_power_lock(card);
+	result = snd_power_wait(card, SNDRV_CTL_POWER_D0, NULL);
+	if (result >= 0)
+		result = snd_ctl_elem_write(card, file, control);
+	snd_power_unlock(card);
+snd_printk("snd_ctl_elem_write_user2\n");
 	if (result >= 0)
 		if (copy_to_user(_control, control, sizeof(*control)))
 			result = -EFAULT;

--
Giuliano.

-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language that extends applications into web and mobile media. Attend the live webcast and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642

#1 lore raw

At Thu, 2 Mar 2006 00:18:59 +0100,

Quoted 8 lines · Giuliano Pochini

First attempt to add code that prevents someone to access the hardware via control interface when the card is suspended.

I put the code in snd_ctl_elem_read_user() instead of snd_ctl_elem_read() because /pci/hda/hda_codec.c:snd_hda_resume_ctls() calls the latter from the resume callback. I had no time to check other things, consider it mostly untested.

Looks fine. We need also power_lock() around ioctls, too?

Takashi

Quoted 66 lines

diff -dup alsa-kernel__orig/core/control.c alsa-kernel/core/control.c

core/control.c +18 -3
@@ -672,6 +672,7 @@ int snd_ctl_elem_read(struct snd_card *c
 	unsigned int index_offset;
 	int result, indirect;
 
+snd_printk("snd_ctl_elem_read\n");
 	down_read(&card->controls_rwsem);
 	kctl = snd_ctl_find_id(card, &control->id);
 	if (kctl == NULL) {
@@ -708,7 +709,13 @@ static int snd_ctl_elem_read_user(struct
 		kfree(control);
 		return -EFAULT;
 	}
-	result = snd_ctl_elem_read(card, control);
+snd_printk("snd_ctl_elem_read_user1\n");
+	snd_power_lock(card);
+	result = snd_power_wait(card, SNDRV_CTL_POWER_D0, NULL);
+	if (result >= 0)
+		result = snd_ctl_elem_read(card, control);
+	snd_power_unlock(card);
+snd_printk("snd_ctl_elem_read_user2\n");
 	if (result >= 0)
 		if (copy_to_user(_control, control, sizeof(*control)))
 			result = -EFAULT;
@@ -723,6 +730,7 @@ int snd_ctl_elem_write(struct snd_card *
 	struct snd_kcontrol_volatile *vd;
 	unsigned int index_offset;
 	int result, indirect;
+snd_printk("snd_ctl_elem_write\n");
 
 	down_read(&card->controls_rwsem);
 	kctl = snd_ctl_find_id(card, &control->id);
@@ -758,6 +766,7 @@ static int snd_ctl_elem_write_user(struc
 				   struct snd_ctl_elem_value __user *_control)
 {
 	struct snd_ctl_elem_value *control;
+	struct snd_card *card;
 	int result;
 
 	control = kmalloc(sizeof(*control), GFP_KERNEL);
@@ -767,7 +776,14 @@ static int snd_ctl_elem_write_user(struc
 		kfree(control);
 		return -EFAULT;
 	}
-	result = snd_ctl_elem_write(file->card, file, control);
+	card = file->card;
+snd_printk("snd_ctl_elem_write_user1\n");
+	snd_power_lock(card);
+	result = snd_power_wait(card, SNDRV_CTL_POWER_D0, NULL);
+	if (result >= 0)
+		result = snd_ctl_elem_write(card, file, control);
+	snd_power_unlock(card);
+snd_printk("snd_ctl_elem_write_user2\n");
 	if (result >= 0)
 		if (copy_to_user(_control, control, sizeof(*control)))
 			result = -EFAULT;
 
 
--

Giuliano.

Quoted 10 lines

-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language that extends applications into web and mobile media. Attend the live webcast and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Alsa-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/alsa-devel

-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language that extends applications into web and mobile media. Attend the live webcast and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642

#2 lore raw
Quoted 6 lines · On Thu, 02 Mar 2006 11:58:39 +0100 Takashi Iwai <[email protected]> wrote:

At Thu, 2 Mar 2006 00:18:59 +0100,

Quoted 4 lines · Giuliano Pochini

First attempt to add code that prevents someone to access the hardware via control interface when the card is suspended.

Quoted 6 lines
Quoted 4 lines

I put the code in snd_ctl_elem_read_user() instead of snd_ctl_elem_read() because /pci/hda/hda_codec.c:snd_hda_resume_ctls() calls the latter from the resume callback. I had no time to check other things, consider it mostly untested.

Looks fine. We need also power_lock() around ioctls, too?

My driver doesn't use the ioctl interface, but if we want to make ti work always, I think so.

->put() and ->get() are called directly only from core/control.c:snd_ctl_elem_read() and core/control.c:snd_ctl_elem_write(). If we put snd_power_wait() in snd_ctl_elem_read/write() all their current and future callers are protected, but /hda/hda_codec.c deadlocks in the resume function (it's the only one). Otherwise, we can put snd_power_wait() in snd_ctl_elem_read/write_user() and in _snd_ioctl32_ctl_elem_value(). Which choice ? Do we duplicate power_wait or do we change hda_intel ?

--
Giuliano.

-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language that extends applications into web and mobile media. Attend the live webcast and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642

#3 lore raw

At Thu, 2 Mar 2006 23:07:20 +0100,

Quoted 20 lines · Giuliano Pochini
Quoted 6 lines · On Thu, 02 Mar 2006 11:58:39 +0100 Takashi Iwai <[email protected]> wrote:

At Thu, 2 Mar 2006 00:18:59 +0100,

Quoted 4 lines · Giuliano Pochini

First attempt to add code that prevents someone to access the hardware via control interface when the card is suspended.

Quoted 6 lines
Quoted 4 lines

I put the code in snd_ctl_elem_read_user() instead of snd_ctl_elem_read() because /pci/hda/hda_codec.c:snd_hda_resume_ctls() calls the latter from the resume callback. I had no time to check other things, consider it mostly untested.

Looks fine. We need also power_lock() around ioctls, too?

My driver doesn't use the ioctl interface, but if we want to make ti work always, I think so.

Quoted 7 lines

->put() and ->get() are called directly only from core/control.c:snd_ctl_elem_read() and core/control.c:snd_ctl_elem_write(). If we put snd_power_wait() in snd_ctl_elem_read/write() all their current and future callers are protected, but /hda/hda_codec.c deadlocks in the resume function (it's the only one). Otherwise, we can put snd_power_wait() in snd_ctl_elem_read/write_user() and in _snd_ioctl32_ctl_elem_value(). Which choice ? Do we duplicate power_wait or do we change hda_intel ?

I think it's better to set snd_power_lock() only for user-accessor.

Yes, get and put callbacks are only for read/write callbacks, but there is still info callback. So, we need to put power lock to snd_ctl_elem_user_info(), too.

Takashi

-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language that extends applications into web and mobile media. Attend the live webcast and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642

#4 lore raw
Quoted 8 lines · On Fri, 03 Mar 2006 18:45:28 +0100 Takashi Iwai <[email protected]> wrote:
Quoted 7 lines

->put() and ->get() are called directly only from core/control.c:snd_ctl_elem_read() and core/control.c:snd_ctl_elem_write(). If we put snd_power_wait() in snd_ctl_elem_read/write() all their current and future callers are protected, but /hda/hda_codec.c deadlocks in the resume function (it's the only one). Otherwise, we can put snd_power_wait() in snd_ctl_elem_read/write_user() and in _snd_ioctl32_ctl_elem_value(). Which choice ? Do we duplicate power_wait or do we change hda_intel ?

Quoted 5 lines

I think it's better to set snd_power_lock() only for user-accessor.

Yes, get and put callbacks are only for read/write callbacks, but there is still info callback. So, we need to put power lock to snd_ctl_elem_user_info(), too.

Ok, this patch changes all three snd_ctl_elem_user_*(). It seems to work fine here. It also patches alsa-kernel/core/control_compat.c, but that part has been compile-tested only. The last part fixes ioctl32/ioctl32_new.c and it is completely untested (how can I test it on ppc32 ?).

core/control.c +17 -3
diff -dup alsa-kernel__orig/core/control.c alsa-kernel/core/control.c
@@ -658,7 +658,11 @@ static int snd_ctl_elem_info_user(struct
 
 	if (copy_from_user(&info, _info, sizeof(info)))
 		return -EFAULT;
-	result = snd_ctl_elem_info(ctl, &info);
+	snd_power_lock(ctl->card);
+	result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0, NULL);
+	if (result >= 0)
+		result = snd_ctl_elem_info(ctl, &info);
+	snd_power_unlock(ctl->card);
 	if (result >= 0)
 		if (copy_to_user(_info, &info, sizeof(info)))
 			return -EFAULT;
@@ -708,7 +712,11 @@ static int snd_ctl_elem_read_user(struct
 		kfree(control);
 		return -EFAULT;
 	}
-	result = snd_ctl_elem_read(card, control);
+	snd_power_lock(card);
+	result = snd_power_wait(card, SNDRV_CTL_POWER_D0, NULL);
+	if (result >= 0)
+		result = snd_ctl_elem_read(card, control);
+	snd_power_unlock(card);
 	if (result >= 0)
 		if (copy_to_user(_control, control, sizeof(*control)))
 			result = -EFAULT;
@@ -758,6 +766,7 @@ static int snd_ctl_elem_write_user(struc
 				   struct snd_ctl_elem_value __user *_control)
 {
 	struct snd_ctl_elem_value *control;
+	struct snd_card *card;
 	int result;
 
 	control = kmalloc(sizeof(*control), GFP_KERNEL);
@@ -767,7 +776,12 @@ static int snd_ctl_elem_write_user(struc
 		kfree(control);
 		return -EFAULT;
 	}
-	result = snd_ctl_elem_write(file->card, file, control);
+	card = file->card;
+	snd_power_lock(card);
+	result = snd_power_wait(card, SNDRV_CTL_POWER_D0, NULL);
+	if (result >= 0)
+		result = snd_ctl_elem_write(card, file, control);
+	snd_power_unlock(card);
 	if (result >= 0)
 		if (copy_to_user(_control, control, sizeof(*control)))
 			result = -EFAULT;
core/control_compat.c +25 -8
diff -dup alsa-kernel__orig/core/control_compat.c alsa-kernel/core/control_compat.c
@@ -107,7 +107,13 @@ static int snd_ctl_elem_info_compat(stru
 	 */
 	if (get_user(data->value.enumerated.item, &data32->value.enumerated.item))
 		goto error;
-	err = snd_ctl_elem_info(ctl, data);
+
+	snd_power_lock(ctl->card);
+	err = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0, NULL);
+	if (err >= 0)
+		err = snd_ctl_elem_info(ctl, data);
+	snd_power_unlock(ctl->card);
+
 	if (err < 0)
 		goto error;
 	/* restore info to 32bit */
@@ -286,9 +292,14 @@ static int snd_ctl_elem_read_user_compat
 
 	if ((err = copy_ctl_value_from_user(card, data, data32, &type, &count)) < 0)
 		goto error;
-	if ((err = snd_ctl_elem_read(card, data)) < 0)
-		goto error;
-	err = copy_ctl_value_to_user(data32, data, type, count);
+
+	snd_power_lock(card);
+	err = snd_power_wait(card, SNDRV_CTL_POWER_D0, NULL);
+	if (err >= 0)
+		err = snd_ctl_elem_read(card, data);
+	snd_power_unlock(card);
+	if (err >= 0)
+		err = copy_ctl_value_to_user(data32, data, type, count);
  error:
 	kfree(data);
 	return err;
@@ -298,17 +309,23 @@ static int snd_ctl_elem_write_user_compa
 					  struct snd_ctl_elem_value32 __user *data32)
 {
 	struct snd_ctl_elem_value *data;
+	struct snd_card *card = file->card;
 	int err, type, count;
 
 	data = kzalloc(sizeof(*data), GFP_KERNEL);
 	if (data == NULL)
 		return -ENOMEM;
 
-	if ((err = copy_ctl_value_from_user(file->card, data, data32, &type, &count)) < 0)
-		goto error;
-	if ((err = snd_ctl_elem_write(file->card, file, data)) < 0)
+	if ((err = copy_ctl_value_from_user(card, data, data32, &type, &count)) < 0)
 		goto error;
-	err = copy_ctl_value_to_user(data32, data, type, count);
+
+	snd_power_lock(card);
+	err = snd_power_wait(card, SNDRV_CTL_POWER_D0, NULL);
+	if (err >= 0)
+		err = snd_ctl_elem_write(card, file, data);
+	snd_power_unlock(card);
+	if (err >= 0)
+		err = copy_ctl_value_to_user(data32, data, type, count);
  error:
 	kfree(data);
 	return err;
acore/ioctl32/ioctl32_new.c +17 -7
diff -dup alsa-driver__orig/acore/ioctl32/ioctl32_new.c alsa-driver/acore/ioctl32/ioctl32_new.c
@@ -243,7 +243,11 @@ static int get_ctl_type(struct snd_card 
 		return -ENXIO;
 	}
 	info.id = *id;
-	err = kctl->info(kctl, &info);
+	snd_power_lock(card);
+	err = snd_power_wait(card, SNDRV_CTL_POWER_D0, NULL);
+	if (err >= 0)
+		err = kctl->info(kctl, &info);
+	snd_power_unlock(card);
 	up_read(&card->controls_rwsem);
 	if (err >= 0)
 		err = info.type;
@@ -255,6 +259,7 @@ static inline int _snd_ioctl32_ctl_elem_
 	struct snd_ctl_elem_value *data;
 	struct snd_ctl_elem_value32 __user *data32;
 	struct snd_ctl_file *ctl;
+	struct snd_card *card;
 	int err, i, indirect;
 	int type;
 
@@ -285,7 +290,8 @@ static inline int _snd_ioctl32_ctl_elem_
 		err = -EINVAL;
 		goto __end;
 	}
-	type = get_ctl_type(ctl->card, &data->id);
+	card = ctl->card;
+	type = get_ctl_type(card, &data->id);
 	if (type < 0) {
 		err = type;
 		goto __end;
@@ -320,10 +326,15 @@ static inline int _snd_ioctl32_ctl_elem_
 		goto __end;
 	}
 
-	if (native_ctl == SNDRV_CTL_IOCTL_ELEM_READ)
-		err = snd_ctl_elem_read(ctl->card, data);
-	else
-		err = snd_ctl_elem_write(ctl->card, ctl, data);
+	snd_power_lock(card);
+	err = snd_power_wait(card, SNDRV_CTL_POWER_D0, NULL);
+	if (err >= 0) {
+		if (native_ctl == SNDRV_CTL_IOCTL_ELEM_READ)
+			err = snd_ctl_elem_read(card, data);
+		else
+			err = snd_ctl_elem_write(card, ctl, data);
+	}
+	snd_power_unlock(card);
 	if (err < 0)
 		goto __end;
 	/* restore info to 32bit */
@@ -347,7 +358,6 @@ static inline int _snd_ioctl32_ctl_elem_
 			goto __end;
 		}
 		break;
-		break;
 	}
 	err = 0;
       __end:

--
Giuliano.

-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language that extends applications into web and mobile media. Attend the live webcast and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642

#5 lore raw

At Tue, 7 Mar 2006 23:09:57 +0100,

Quoted 14 lines · Giuliano Pochini
Quoted 10 lines · On Fri, 03 Mar 2006 18:45:28 +0100 Takashi Iwai <[email protected]> wrote:
Quoted 7 lines

->put() and ->get() are called directly only from core/control.c:snd_ctl_elem_read() and core/control.c:snd_ctl_elem_write(). If we put snd_power_wait() in snd_ctl_elem_read/write() all their current and future callers are protected, but /hda/hda_codec.c deadlocks in the resume function (it's the only one). Otherwise, we can put snd_power_wait() in snd_ctl_elem_read/write_user() and in _snd_ioctl32_ctl_elem_value(). Which choice ? Do we duplicate power_wait or do we change hda_intel ?

I think it's better to set snd_power_lock() only for user-accessor.

Quoted 8 lines
Quoted 3 lines

Yes, get and put callbacks are only for read/write callbacks, but there is still info callback. So, we need to put power lock to snd_ctl_elem_user_info(), too.

Ok, this patch changes all three snd_ctl_elem_user_*(). It seems to work fine here. It also patches alsa-kernel/core/control_compat.c, but that part has been compile-tested only. The last part fixes ioctl32/ioctl32_new.c and it is completely untested (how can I test it on ppc32 ?).

Thanks, the patch looks fine to me.
Could you give a changelog and a signed-off-by?

Takashi

-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language that extends applications into web and mobile media. Attend the live webcast and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642

#6 lore raw
Quoted 7 lines · Takashi Iwai
Quoted 4 lines

Ok, this patch changes all three snd_ctl_elem_user_*(). It seems to work fine here. It also patches alsa-kernel/core/control_compat.c, but that part has been compile-tested only. The last part fixes ioctl32/ioctl32_new.c and it is completely untested (how can I test it on ppc32 ?).

Thanks, the patch looks fine to me.
Could you give a changelog and a signed-off-by?

This patch prevents user-space apps from accessing the hardware via control interface while the soundcard is suspended.

--
Giuliano.

-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language that extends applications into web and mobile media. Attend the live webcast and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642

#7 lore raw

At Wed, 8 Mar 2006 22:19:47 +0100 (CET),

Quoted 10 lines · Giuliano Pochini
Quoted 5 lines · Takashi Iwai
Quoted 4 lines

Ok, this patch changes all three snd_ctl_elem_user_*(). It seems to work fine here. It also patches alsa-kernel/core/control_compat.c, but that part has been compile-tested only. The last part fixes ioctl32/ioctl32_new.c and it is completely untested (how can I test it on ppc32 ?).

Quoted 8 lines
Quoted 2 lines

Thanks, the patch looks fine to me.
Could you give a changelog and a signed-off-by?

This patch prevents user-space apps from accessing the hardware via control interface while the soundcard is suspended.

Applied now to CVS. Thanks.

Takashi

-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language that extends applications into web and mobile media. Attend the live webcast and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642