@@ -253,8 +253,8 @@ static int virtsnd_kctl_tlv_op(struct snd_kcontrol *kcontrol, int op_flag,
tlv = kzalloc(size, GFP_KERNEL);
if (!tlv) {
- virtsnd_ctl_msg_unref(msg);
- return -ENOMEM;
+ rc = -ENOMEM;
+ goto on_cleanup;
}
sg_init_one(&sg, tlv, size);
@@ -266,6 +266,11 @@ static int virtsnd_kctl_tlv_op(struct snd_kcontrol *kcontrol, int op_flag,
case SNDRV_CTL_TLV_OP_READ:
hdr->hdr.code = cpu_to_le32(VIRTIO_SND_R_CTL_TLV_READ);
+ /* Since virtsnd_ctl_msg_send() drops the reference, we increase
+ * the counter to be consistent with the on_cleanup path.
+ */
+ virtsnd_ctl_msg_ref(msg);
+
rc = virtsnd_ctl_msg_send(snd, msg, NULL, &sg, false);
if (!rc) {
if (copy_to_user(utlv, tlv, size))
@@ -281,14 +286,25 @@ static int virtsnd_kctl_tlv_op(struct snd_kcontrol *kcontrol, int op_flag,
hdr->hdr.code =
cpu_to_le32(VIRTIO_SND_R_CTL_TLV_COMMAND);
- if (copy_from_user(tlv, utlv, size))
+ if (copy_from_user(tlv, utlv, size)) {
rc = -EFAULT;
- else
+ } else {
+ /* Same as the comment above */
+ virtsnd_ctl_msg_ref(msg);
+
rc = virtsnd_ctl_msg_send(snd, msg, &sg, NULL, false);
+ }
+
+ break;
+ default:
+ rc = -EINVAL;
break;
}
+on_cleanup:
+ virtsnd_ctl_msg_unref(msg);
+
kfree(tlv);
return rc;