Skip to contentSkip to conversation

ALSA: seq: virmidi: Offload the output event processing

PATCH New Takashi Iwai 1 participant 2 messages Jul 30, 2018 Sound

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 ALSA: seq: virmidi: Offload the output event processing

The virmidi sequencer stuff tries to translate the rawmidi bytes to sequencer events and deliver the packets at trigger callback. The amount of the whole process of these translations and deliveries depends on the incoming rawmidi bytes, and we have no limit for that; this was the cause of a CPU soft lockup that had been reported and fixed recently.

Although we've fixed the soft lockup by putting the temporary unlock and cond_resched(), it's rather a quick band aid. In this patch, meanwhile, the event parsing and delivery process is offloaded to a dedicated work, and the trigger callback just kicks it off. It has three merits, at least:

- The processing is always done in a sleepable context, which can assure the event delivery with non-atomic flag without hackish is_atomic() usage.

- Other relevant codes can be simplified, reducing the lines

- It makes me happier

---
 include/sound/seq_virmidi.h  |   1 +
 sound/core/seq/seq_virmidi.c | 100 ++++++++++++++++-------------------
 2 files changed, 47 insertions(+), 54 deletions(-)
include/sound/seq_virmidi.h +1 -0
index 695257ae64ac..d488dcfa3a4e 100644
@@ -41,6 +41,7 @@ struct snd_virmidi {
 	struct snd_seq_event event;
 	struct snd_virmidi_dev *rdev;
 	struct snd_rawmidi_substream *substream;
+	struct work_struct output_work;
 };
 
 #define SNDRV_VIRMIDI_SUBSCRIBE		(1<<0)
sound/core/seq/seq_virmidi.c +46 -54

128 lines not on this page · raw message

Signature · 1 line
2.18.0
#1 lore raw

PATCH 2/2 ALSA: seq: virmidi: Use READ_ONCE/WRITE_ONCE() macros

The trigger flag in vmidi object can be referred in different contexts concurrently, hence it's better to be put with READ_ONCE() and WRITE_ONCE() macros to assure the accesses.

---
 include/sound/seq_virmidi.h  |  2 +-
 sound/core/seq/seq_virmidi.c | 14 +++++---------
 2 files changed, 6 insertions(+), 10 deletions(-)
include/sound/seq_virmidi.h +1 -1
index d488dcfa3a4e..796ce7772213 100644
@@ -36,7 +36,7 @@ struct snd_virmidi {
 	int seq_mode;
 	int client;
 	int port;
-	unsigned int trigger: 1;
+	bool trigger;
 	struct snd_midi_event *parser;
 	struct snd_seq_event event;
 	struct snd_virmidi_dev *rdev;
sound/core/seq/seq_virmidi.c +5 -9
index 67ea5d62cebc..03ac5e72dbe6 100644
@@ -89,7 +89,7 @@ static int snd_virmidi_dev_receive_event(struct snd_virmidi_dev *rdev,
 	else
 		down_read(&rdev->filelist_sem);
 	list_for_each_entry(vmidi, &rdev->filelist, list) {
-		if (!vmidi->trigger)
+		if (!READ_ONCE(vmidi->trigger))
 			continue;
 		if (ev->type == SNDRV_SEQ_EVENT_SYSEX) {
 			if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
@@ -147,11 +147,7 @@ static void snd_virmidi_input_trigger(struct snd_rawmidi_substream *substream, i
 {
 	struct snd_virmidi *vmidi = substream->runtime->private_data;
 
-	if (up) {
-		vmidi->trigger = 1;
-	} else {
-		vmidi->trigger = 0;
-	}
+	WRITE_ONCE(vmidi->trigger, !!up);
 }
 
 /* process rawmidi bytes and send events;
@@ -175,7 +171,7 @@ static void snd_vmidi_output_work(struct work_struct *work)
 		return;
 	}
 
-	while (vmidi->trigger) {
+	while (READ_ONCE(vmidi->trigger)) {
 		if (snd_rawmidi_transmit(substream, &input, 1) != 1)
 			break;
 		if (snd_midi_event_encode_byte(vmidi->parser, input,
@@ -201,7 +197,7 @@ static void snd_virmidi_output_trigger(struct snd_rawmidi_substream *substream,
 {
 	struct snd_virmidi *vmidi = substream->runtime->private_data;
 
-	vmidi->trigger = !!up;
+	WRITE_ONCE(vmidi->trigger, !!up);
 	if (up)
 		queue_work(system_highpri_wq, &vmidi->output_work);
 }
@@ -289,7 +285,7 @@ static int snd_virmidi_output_close(struct snd_rawmidi_substream *substream)
 {
 	struct snd_virmidi *vmidi = substream->runtime->private_data;
 
-	vmidi->trigger = 0; /* to be sure */
+	WRITE_ONCE(vmidi->trigger, false); /* to be sure */
 	cancel_work_sync(&vmidi->output_work);
 	snd_midi_event_free(vmidi->parser);
 	substream->runtime->private_data = NULL;
Signature · 1 line
2.18.0