ALSA: PCM state reference optimization
Subsystems (5)
cover ALSA: PCM state reference optimization
Hi,
this is a patch set for simplifying the reference to the current PCM state by having the local copy in runtime instead of relying on runtime->status indirection. This also hardens against the attack by modifying the mmapped status record.
The first patch does the basic job in the core PCM side, and the second patch flips the PCM status mmap to read-only for hardening, while the remaining patches are for drivers to follow the core change.
The conversions are straightforward. In most places, it's just replacing runtime->status->state with runtime->state.
Takashi
===
Takashi Iwai (11):
ALSA: pcm: Avoid reference to status->state
ALSA: pcm: Make mmap status read-only
ALSA: aloop: Replace runtime->status->state reference to
runtime->state
ALSA: firewire: Replace runtime->status->state reference to
runtime->state
ALSA: hda: Replace runtime->status->state reference to runtime->state
ALSA: asihpi: Replace runtime->status->state reference to
runtime->state
ALSA: usb-audio: Replace runtime->status->state reference to
runtime->state
ALSA: usx2y: Replace runtime->status->state reference to
runtime->state
ASoC: intel: Replace runtime->status->state reference to
runtime->state
ASoC: sh: Replace runtime->status->state reference to runtime->state
usb: gadget: Replace runtime->status->state reference to
runtime->statedrivers/usb/gadget/function/u_uac1_legacy.c | 4 +- include/sound/pcm.h | 20 ++- sound/core/oss/pcm_oss.c | 42 +++---- sound/core/pcm.c | 9 +- sound/core/pcm_compat.c | 4 +- sound/core/pcm_lib.c | 16 +-- sound/core/pcm_native.c | 128 ++++++++++---------- sound/drivers/aloop.c | 4 +- sound/firewire/bebob/bebob_pcm.c | 4 +- sound/firewire/dice/dice-pcm.c | 4 +- sound/firewire/digi00x/digi00x-pcm.c | 4 +- sound/firewire/fireface/ff-pcm.c | 4 +- sound/firewire/fireworks/fireworks_pcm.c | 4 +- sound/firewire/motu/motu-pcm.c | 4 +- sound/firewire/oxfw/oxfw-pcm.c | 8 +- sound/firewire/tascam/tascam-pcm.c | 4 +- sound/hda/hdmi_chmap.c | 2 +- sound/pci/asihpi/asihpi.c | 2 +- sound/soc/intel/skylake/skl-pcm.c | 4 +- sound/soc/sh/rz-ssi.c | 2 +- sound/usb/pcm.c | 4 +- sound/usb/usx2y/usbusx2yaudio.c | 3 +- sound/usb/usx2y/usx2yhwdeppcm.c | 3 +- 23 files changed, 150 insertions(+), 133 deletions(-)
===
Signature · 1 line
2.35.3
PATCH 1/11 ALSA: pcm: Avoid reference to status->state
In the PCM core and driver code, there are lots place referring to the current PCM state via runtime->status->state. This patch introduced a local PCM state in runtime itself and replaces those references with runtime->state. It has improvements in two aspects:
- The reduction of a indirect access leads to more code optimization
- It avoids a possible (unexpected) modification of the state via mmap of the status record
The status->state is updated together with runtime->state, so that user-space can still read the current state via mmap like before, too.
This patch touches only the ALSA core code. The changes in each driver will follow in later patches.
--- include/sound/pcm.h | 20 +++++- sound/core/oss/pcm_oss.c | 42 ++++++------- sound/core/pcm.c | 9 +-- sound/core/pcm_compat.c | 4 +- sound/core/pcm_lib.c | 16 ++--- sound/core/pcm_native.c | 127 ++++++++++++++++++++------------------- 6 files changed, 118 insertions(+), 100 deletions(-)
include/sound/pcm.h +18 -2
@@ -346,6 +346,8 @@ static inline void snd_pcm_pack_audio_tstamp_report(__u32 *data, __u32 *accuracystruct snd_pcm_runtime {/* -- Status -- */ +snd_pcm_state_t state;/* stream state */ +snd_pcm_state_t suspended_state;/* suspended stream state */ struct snd_pcm_substream *trigger_master;struct timespec64 trigger_tstamp;/* trigger timestamp */ bool trigger_tstamp_latched;/* trigger timestamp latched in low-level driver/hardware */
@@ -678,11 +680,25 @@ void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,*/ static inline int snd_pcm_running (struct snd_pcm_substream *substream) { -return (substream->runtime->status->state ==SNDRV_PCM_STATE_RUNNING || - (substream->runtime->status->state ==SNDRV_PCM_STATE_DRAINING && +return (substream->runtime->state ==SNDRV_PCM_STATE_RUNNING || + (substream->runtime->state ==SNDRV_PCM_STATE_DRAINING && substream->stream ==SNDRV_PCM_STREAM_PLAYBACK )); } +/** +* __snd_pcm_set_state - Change the current PCM state +* @runtime: PCM runtime to set +* @state: the current state to set +* +* Call within the stream lock +*/ +static inline void __snd_pcm_set_state (struct snd_pcm_runtime *runtime, +snd_pcm_state_t state) +{ + runtime->state = state; + runtime->status->state = state;/* copy for mmap */ +} +/** * bytes_to_samples - Unit conversion of the size from bytes to samples * @runtime: PCM runtime instance
sound/core/oss/pcm_oss.c +21 -21
124 lines not on this page · raw message
sound/core/pcm.c +5 -4
@@ -387,7 +387,7 @@ static void snd_pcm_substream_proc_hw_params_read(struct snd_info_entry *entry,snd_iprintf (buffer,"closed\n" ); goto unlock; } -if (runtime->status->state ==SNDRV_PCM_STATE_OPEN ) { +if (runtime->state ==SNDRV_PCM_STATE_OPEN ) {snd_iprintf (buffer,"no setup\n" ); goto unlock; }
@@ -424,7 +424,7 @@ static void snd_pcm_substream_proc_sw_params_read(struct snd_info_entry *entry,snd_iprintf (buffer,"closed\n" ); goto unlock; } -if (runtime->status->state ==SNDRV_PCM_STATE_OPEN ) { +if (runtime->state ==SNDRV_PCM_STATE_OPEN ) {snd_iprintf (buffer,"no setup\n" ); goto unlock; }
@@ -970,7 +970,7 @@ int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,init_waitqueue_head (&runtime->sleep);init_waitqueue_head (&runtime->tsleep); - runtime->status->state =SNDRV_PCM_STATE_OPEN ; +__snd_pcm_set_state (runtime,SNDRV_PCM_STATE_OPEN );mutex_init (&runtime->buffer_mutex);atomic_set (&runtime->buffer_accessing,0 );
@@ -1112,7 +1112,8 @@ static int snd_pcm_dev_disconnect(struct snd_device *device)if (snd_pcm_running (substream))snd_pcm_stop (substream,SNDRV_PCM_STATE_DISCONNECTED );/* to be sure, set the state unconditionally */ - substream->runtime->status->state =SNDRV_PCM_STATE_DISCONNECTED ; +__snd_pcm_set_state (substream->runtime, +SNDRV_PCM_STATE_DISCONNECTED );wake_up (&substream->runtime->sleep);wake_up (&substream->runtime->tsleep); }
sound/core/pcm_compat.c +2 -2
@@ -295,7 +295,7 @@ static int snd_pcm_ioctl_xferi_compat(struct snd_pcm_substream *substream,return -ENOTTY ;if (substream->stream != dir)return -EINVAL ; -if (substream->runtime->status->state ==SNDRV_PCM_STATE_OPEN ) +if (substream->runtime->state ==SNDRV_PCM_STATE_OPEN )return -EBADFD ; if (get_user(buf, &data32->buf) ||
@@ -341,7 +341,7 @@ static int snd_pcm_ioctl_xfern_compat(struct snd_pcm_substream *substream,return -ENOTTY ;if (substream->stream != dir)return -EINVAL ; -if (substream->runtime->status->state ==SNDRV_PCM_STATE_OPEN ) +if (substream->runtime->state ==SNDRV_PCM_STATE_OPEN )return -EBADFD ; ch = substream->runtime->channels;
sound/core/pcm_lib.c +8 -8
@@ -186,7 +186,7 @@ int snd_pcm_update_state(struct snd_pcm_substream *substream, avail =snd_pcm_avail (substream);if (avail > runtime->avail_max) runtime->avail_max = avail; -if (runtime->status->state ==SNDRV_PCM_STATE_DRAINING ) { +if (runtime->state ==SNDRV_PCM_STATE_DRAINING ) {if (avail >= runtime->buffer_size) {snd_pcm_drain_done (substream);return -EPIPE ;
@@ -1911,7 +1911,7 @@ static int wait_for_avail(struct snd_pcm_substream *substream,snd_pcm_stream_lock_irq (substream);set_current_state (TASK_INTERRUPTIBLE ); -switch (runtime->status->state) { +switch (runtime->state) {case SNDRV_PCM_STATE_SUSPENDED : err = -ESTRPIPE ; goto _endloop;
@@ -2099,14 +2099,14 @@ static int pcm_sanity_check(struct snd_pcm_substream *substream) runtime = substream->runtime;if (snd_BUG_ON (!substream->ops->copy_user && !runtime->dma_area))return -EINVAL ; -if (runtime->status->state ==SNDRV_PCM_STATE_OPEN ) +if (runtime->state ==SNDRV_PCM_STATE_OPEN )return -EBADFD ;return 0 ; }static int pcm_accessible_state (struct snd_pcm_runtime *runtime) { -switch (runtime->status->state) { +switch (runtime->state) {case SNDRV_PCM_STATE_PREPARED :case SNDRV_PCM_STATE_RUNNING :case SNDRV_PCM_STATE_PAUSED :
@@ -2225,7 +2225,7 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream, goto _end_unlock; runtime->twake = runtime->control->avail_min ? :1 ; -if (runtime->status->state ==SNDRV_PCM_STATE_RUNNING ) +if (runtime->state ==SNDRV_PCM_STATE_RUNNING )snd_pcm_update_hw_ptr (substream);/*
@@ -2233,7 +2233,7 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,* thread may start capture */ if (!is_playback && - runtime->status->state ==SNDRV_PCM_STATE_PREPARED && + runtime->state ==SNDRV_PCM_STATE_PREPARED && size >= runtime->start_threshold) { err =snd_pcm_start (substream);if (err <0 )
@@ -2247,7 +2247,7 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,snd_pcm_uframes_t cont;if (!avail) {if (!is_playback && - runtime->status->state ==SNDRV_PCM_STATE_DRAINING ) { + runtime->state ==SNDRV_PCM_STATE_DRAINING ) {snd_pcm_stop (substream,SNDRV_PCM_STATE_SETUP ); goto _end_unlock; }
@@ -2303,7 +2303,7 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream, xfer += frames; avail -= frames;if (is_playback && - runtime->status->state ==SNDRV_PCM_STATE_PREPARED && + runtime->state ==SNDRV_PCM_STATE_PREPARED &&snd_pcm_playback_hw_avail (runtime) >= (snd_pcm_sframes_t )runtime->start_threshold) { err =snd_pcm_start (substream);if (err <0 )
sound/core/pcm_native.c +64 -63
418 lines not on this page · raw message
Signature · 1 line
2.35.3
PATCH 2/11 ALSA: pcm: Make mmap status read-only
In reply to Takashi IwaiThe mmap status record should be read-only. Modifying it from user-space may screw up things unexpectedly, so let's clear the write bits at exposing it.
--- sound/core/pcm_native.c | 1 + 1 file changed, 1 insertion(+)
sound/core/pcm_native.c +1 -0
@@ -3668,6 +3668,7 @@ static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file area->vm_ops = &snd_pcm_vm_ops_status; area->vm_private_data = substream; area->vm_flags |=VM_DONTEXPAND |VM_DONTDUMP ; + area->vm_flags &= ~(VM_WRITE |VM_MAYWRITE );return 0 ; }
Signature · 1 line
2.35.3
PATCH 3/11 ALSA: aloop: Replace runtime->status->state reference to runtime->state
In reply to Takashi IwaiThe recent change in ALSA core allows drivers to get the current PCM state directly from runtime object. Replace the calls accordingly.
--- sound/drivers/aloop.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
sound/drivers/aloop.c +2 -2
@@ -535,7 +535,7 @@ static void copy_play_buf(struct loopback_pcm *play,/* check if playback is draining, trim the capture copy size * when our pointer is at the end of playback ring buffer */ -if (runtime->status->state ==SNDRV_PCM_STATE_DRAINING && +if (runtime->state ==SNDRV_PCM_STATE_DRAINING &&snd_pcm_playback_hw_avail (runtime) < runtime->buffer_size) {snd_pcm_uframes_t appl_ptr, appl_ptr1, diff; appl_ptr = appl_ptr1 = runtime->control->appl_ptr;
@@ -730,7 +730,7 @@ static void loopback_snd_timer_period_elapsed(struct loopback_cable *cable,if (event ==SNDRV_TIMER_EVENT_MSTOP ) {if (!dpcm_play || - dpcm_play->substream->runtime->status->state != + dpcm_play->substream->runtime->state !=SNDRV_PCM_STATE_DRAINING ) {spin_unlock_irqrestore (&cable->lock, flags);return ;
Signature · 1 line
2.35.3
PATCH 4/11 ALSA: firewire: Replace runtime->status->state reference to runtime->state
In reply to Takashi IwaiThe recent change in ALSA core allows drivers to get the current PCM state directly from runtime object. Replace the calls accordingly.
--- sound/firewire/bebob/bebob_pcm.c | 4 ++-- sound/firewire/dice/dice-pcm.c | 4 ++-- sound/firewire/digi00x/digi00x-pcm.c | 4 ++-- sound/firewire/fireface/ff-pcm.c | 4 ++-- sound/firewire/fireworks/fireworks_pcm.c | 4 ++-- sound/firewire/motu/motu-pcm.c | 4 ++-- sound/firewire/oxfw/oxfw-pcm.c | 8 ++++---- sound/firewire/tascam/tascam-pcm.c | 4 ++-- 8 files changed, 18 insertions(+), 18 deletions(-)
sound/firewire/bebob/bebob_pcm.c +2 -2
@@ -214,7 +214,7 @@ static int pcm_hw_params(struct snd_pcm_substream *substream,struct snd_bebob *bebob = substream->private_data;int err =0 ; -if (substream->runtime->status->state ==SNDRV_PCM_STATE_OPEN ) { +if (substream->runtime->state ==SNDRV_PCM_STATE_OPEN ) {unsigned int rate =params_rate (hw_params);unsigned int frames_per_period =params_period_size (hw_params);unsigned int frames_per_buffer =params_buffer_size (hw_params);
@@ -236,7 +236,7 @@ static int pcm_hw_free(struct snd_pcm_substream *substream)mutex_lock (&bebob->mutex); -if (substream->runtime->status->state !=SNDRV_PCM_STATE_OPEN ) +if (substream->runtime->state !=SNDRV_PCM_STATE_OPEN ) bebob->substreams_counter--;snd_bebob_stream_stop_duplex (bebob);
sound/firewire/dice/dice-pcm.c +2 -2
@@ -266,7 +266,7 @@ static int pcm_hw_params(struct snd_pcm_substream *substream,struct snd_dice *dice = substream->private_data;int err =0 ; -if (substream->runtime->status->state ==SNDRV_PCM_STATE_OPEN ) { +if (substream->runtime->state ==SNDRV_PCM_STATE_OPEN ) {unsigned int rate =params_rate (hw_params);unsigned int events_per_period =params_period_size (hw_params);unsigned int events_per_buffer =params_buffer_size (hw_params);
@@ -293,7 +293,7 @@ static int pcm_hw_free(struct snd_pcm_substream *substream)mutex_lock (&dice->mutex); -if (substream->runtime->status->state !=SNDRV_PCM_STATE_OPEN ) +if (substream->runtime->state !=SNDRV_PCM_STATE_OPEN ) --dice->substreams_counter;snd_dice_stream_stop_duplex (dice);
sound/firewire/digi00x/digi00x-pcm.c +2 -2
@@ -190,7 +190,7 @@ static int pcm_hw_params(struct snd_pcm_substream *substream,struct snd_dg00x *dg00x = substream->private_data;int err =0 ; -if (substream->runtime->status->state ==SNDRV_PCM_STATE_OPEN ) { +if (substream->runtime->state ==SNDRV_PCM_STATE_OPEN ) {unsigned int rate =params_rate (hw_params);unsigned int frames_per_period =params_period_size (hw_params);unsigned int frames_per_buffer =params_buffer_size (hw_params);
@@ -212,7 +212,7 @@ static int pcm_hw_free(struct snd_pcm_substream *substream)mutex_lock (&dg00x->mutex); -if (substream->runtime->status->state !=SNDRV_PCM_STATE_OPEN ) +if (substream->runtime->state !=SNDRV_PCM_STATE_OPEN ) --dg00x->substreams_counter;snd_dg00x_stream_stop_duplex (dg00x);
sound/firewire/fireface/ff-pcm.c +2 -2
@@ -230,7 +230,7 @@ static int pcm_hw_params(struct snd_pcm_substream *substream,struct snd_ff *ff = substream->private_data;int err =0 ; -if (substream->runtime->status->state ==SNDRV_PCM_STATE_OPEN ) { +if (substream->runtime->state ==SNDRV_PCM_STATE_OPEN ) {unsigned int rate =params_rate (hw_params);unsigned int frames_per_period =params_period_size (hw_params);unsigned int frames_per_buffer =params_buffer_size (hw_params);
@@ -252,7 +252,7 @@ static int pcm_hw_free(struct snd_pcm_substream *substream)mutex_lock (&ff->mutex); -if (substream->runtime->status->state !=SNDRV_PCM_STATE_OPEN ) +if (substream->runtime->state !=SNDRV_PCM_STATE_OPEN ) --ff->substreams_counter;snd_ff_stream_stop_duplex (ff);
sound/firewire/fireworks/fireworks_pcm.c +2 -2
@@ -250,7 +250,7 @@ static int pcm_hw_params(struct snd_pcm_substream *substream,struct snd_efw *efw = substream->private_data;int err =0 ; -if (substream->runtime->status->state ==SNDRV_PCM_STATE_OPEN ) { +if (substream->runtime->state ==SNDRV_PCM_STATE_OPEN ) {unsigned int rate =params_rate (hw_params);unsigned int frames_per_period =params_period_size (hw_params);unsigned int frames_per_buffer =params_buffer_size (hw_params);
@@ -272,7 +272,7 @@ static int pcm_hw_free(struct snd_pcm_substream *substream)mutex_lock (&efw->mutex); -if (substream->runtime->status->state !=SNDRV_PCM_STATE_OPEN ) +if (substream->runtime->state !=SNDRV_PCM_STATE_OPEN ) --efw->substreams_counter;snd_efw_stream_stop_duplex (efw);
sound/firewire/motu/motu-pcm.c +2 -2
@@ -210,7 +210,7 @@ static int pcm_hw_params(struct snd_pcm_substream *substream,struct snd_motu *motu = substream->private_data;int err =0 ; -if (substream->runtime->status->state ==SNDRV_PCM_STATE_OPEN ) { +if (substream->runtime->state ==SNDRV_PCM_STATE_OPEN ) {unsigned int rate =params_rate (hw_params);unsigned int frames_per_period =params_period_size (hw_params);unsigned int frames_per_buffer =params_buffer_size (hw_params);
@@ -232,7 +232,7 @@ static int pcm_hw_free(struct snd_pcm_substream *substream)mutex_lock (&motu->mutex); -if (substream->runtime->status->state !=SNDRV_PCM_STATE_OPEN ) +if (substream->runtime->state !=SNDRV_PCM_STATE_OPEN ) --motu->substreams_counter;snd_motu_stream_stop_duplex (motu);
sound/firewire/oxfw/oxfw-pcm.c +4 -4
@@ -239,7 +239,7 @@ static int pcm_capture_hw_params(struct snd_pcm_substream *substream,struct snd_oxfw *oxfw = substream->private_data;int err =0 ; -if (substream->runtime->status->state ==SNDRV_PCM_STATE_OPEN ) { +if (substream->runtime->state ==SNDRV_PCM_STATE_OPEN ) {unsigned int rate =params_rate (hw_params);unsigned int channels =params_channels (hw_params);unsigned int frames_per_period =params_period_size (hw_params);
@@ -262,7 +262,7 @@ static int pcm_playback_hw_params(struct snd_pcm_substream *substream,struct snd_oxfw *oxfw = substream->private_data;int err =0 ; -if (substream->runtime->status->state ==SNDRV_PCM_STATE_OPEN ) { +if (substream->runtime->state ==SNDRV_PCM_STATE_OPEN ) {unsigned int rate =params_rate (hw_params);unsigned int channels =params_channels (hw_params);unsigned int frames_per_period =params_period_size (hw_params);
@@ -286,7 +286,7 @@ static int pcm_capture_hw_free(struct snd_pcm_substream *substream)mutex_lock (&oxfw->mutex); -if (substream->runtime->status->state !=SNDRV_PCM_STATE_OPEN ) +if (substream->runtime->state !=SNDRV_PCM_STATE_OPEN ) --oxfw->substreams_count;snd_oxfw_stream_stop_duplex (oxfw);
@@ -301,7 +301,7 @@ static int pcm_playback_hw_free(struct snd_pcm_substream *substream)mutex_lock (&oxfw->mutex); -if (substream->runtime->status->state !=SNDRV_PCM_STATE_OPEN ) +if (substream->runtime->state !=SNDRV_PCM_STATE_OPEN ) --oxfw->substreams_count;snd_oxfw_stream_stop_duplex (oxfw);
sound/firewire/tascam/tascam-pcm.c +2 -2
@@ -119,7 +119,7 @@ static int pcm_hw_params(struct snd_pcm_substream *substream,struct snd_tscm *tscm = substream->private_data;int err =0 ; -if (substream->runtime->status->state ==SNDRV_PCM_STATE_OPEN ) { +if (substream->runtime->state ==SNDRV_PCM_STATE_OPEN ) {unsigned int rate =params_rate (hw_params);unsigned int frames_per_period =params_period_size (hw_params);unsigned int frames_per_buffer =params_buffer_size (hw_params);
@@ -141,7 +141,7 @@ static int pcm_hw_free(struct snd_pcm_substream *substream)mutex_lock (&tscm->mutex); -if (substream->runtime->status->state !=SNDRV_PCM_STATE_OPEN ) +if (substream->runtime->state !=SNDRV_PCM_STATE_OPEN ) --tscm->substreams_counter;snd_tscm_stream_stop_duplex (tscm);
Signature · 1 line
2.35.3
PATCH 5/11 ALSA: hda: Replace runtime->status->state reference to runtime->state
In reply to Takashi IwaiThe recent change in ALSA core allows drivers to get the current PCM state directly from runtime object. Replace the calls accordingly.
--- sound/hda/hdmi_chmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
sound/hda/hdmi_chmap.c +1 -1
@@ -774,7 +774,7 @@ static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol, substream =snd_pcm_chmap_substream (info, ctl_idx);if (!substream || !substream->runtime)return 0 ;/* just for avoiding error from alsactl restore */ -switch (substream->runtime->status->state) { +switch (substream->runtime->state) {case SNDRV_PCM_STATE_OPEN :case SNDRV_PCM_STATE_SETUP :break ;
Signature · 1 line
2.35.3
PATCH 6/11 ALSA: asihpi: Replace runtime->status->state reference to runtime->state
In reply to Takashi IwaiThe recent change in ALSA core allows drivers to get the current PCM state directly from runtime object. Replace the calls accordingly.
--- sound/pci/asihpi/asihpi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
sound/pci/asihpi/asihpi.c +1 -1
@@ -632,7 +632,7 @@ static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,/*? workaround linked streams don't transition to SETUP 20070706*/ - s->runtime->status->state =SNDRV_PCM_STATE_SETUP ; +__snd_pcm_set_state (s->runtime,SNDRV_PCM_STATE_SETUP );if (card->support_grouping) {snd_printdd ("%d group\n" , s->number);
Signature · 1 line
2.35.3
PATCH 7/11 ALSA: usb-audio: Replace runtime->status->state reference to runtime->state
In reply to Takashi IwaiThe recent change in ALSA core allows drivers to get the current PCM state directly from runtime object. Replace the calls accordingly.
--- sound/usb/pcm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
sound/usb/pcm.c +2 -2
@@ -1395,7 +1395,7 @@ static int prepare_playback_urb(struct snd_usb_substream *subs, transfer_done = subs->transfer_done;if (subs->lowlatency_playback && - runtime->status->state !=SNDRV_PCM_STATE_DRAINING ) { + runtime->state !=SNDRV_PCM_STATE_DRAINING ) {unsigned int hwptr = subs->hwptr_done / stride;/* calculate the byte offset-in-buffer of the appl_ptr */
@@ -1583,7 +1583,7 @@ static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substreareturn 0 ;case SNDRV_PCM_TRIGGER_SUSPEND :case SNDRV_PCM_TRIGGER_STOP : -stop_endpoints (subs, substream->runtime->status->state ==SNDRV_PCM_STATE_DRAINING ); +stop_endpoints (subs, substream->runtime->state ==SNDRV_PCM_STATE_DRAINING );snd_usb_endpoint_set_callback (subs->data_endpoint,NULL ,NULL ,NULL ); subs->running =0 ;
Signature · 1 line
2.35.3
PATCH 8/11 ALSA: usx2y: Replace runtime->status->state reference to runtime->state
In reply to Takashi IwaiThe recent change in ALSA core allows drivers to get the current PCM state directly from runtime object. Replace the calls accordingly.
--- sound/usb/usx2y/usbusx2yaudio.c | 3 +-- sound/usb/usx2y/usx2yhwdeppcm.c | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-)
sound/usb/usx2y/usbusx2yaudio.c +1 -2
@@ -822,8 +822,7 @@ static int snd_usx2y_pcm_hw_free(struct snd_pcm_substream *substream)usx2y_urbs_release (subs);if (!cap_subs->pcm_substream || !cap_subs->pcm_substream->runtime || - !cap_subs->pcm_substream->runtime->status || - cap_subs->pcm_substream->runtime->status->state <SNDRV_PCM_STATE_PREPARED ) { + cap_subs->pcm_substream->runtime->state <SNDRV_PCM_STATE_PREPARED ) {atomic_set (&cap_subs->state,STATE_STOPPED );usx2y_urbs_release (cap_subs); }
sound/usb/usx2y/usx2yhwdeppcm.c +1 -2
@@ -374,8 +374,7 @@ static int snd_usx2y_usbpcm_hw_free(struct snd_pcm_substream *substream)usx2y_usbpcm_urbs_release (subs);if (!cap_subs->pcm_substream || !cap_subs->pcm_substream->runtime || - !cap_subs->pcm_substream->runtime->status || - cap_subs->pcm_substream->runtime->status->state <SNDRV_PCM_STATE_PREPARED ) { + cap_subs->pcm_substream->runtime->state <SNDRV_PCM_STATE_PREPARED ) {atomic_set (&cap_subs->state,STATE_STOPPED );if (cap_subs2)atomic_set (&cap_subs2->state,STATE_STOPPED );
Signature · 1 line
2.35.3
PATCH 9/11 ASoC: intel: Replace runtime->status->state reference to runtime->state
In reply to Takashi IwaiThe recent change in ALSA core allows drivers to get the current PCM state directly from runtime object. Replace the calls accordingly.
---
I'm going to merge this through my sound git tree as it requires ALSA core changes.
sound/soc/intel/skylake/skl-pcm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
sound/soc/intel/skylake/skl-pcm.c +2 -2
@@ -275,7 +275,7 @@ static int skl_pcm_prepare(struct snd_pcm_substream *substream,* calls prepare another time, reset the FW pipe to clean state */ if (mconfig && - (substream->runtime->status->state ==SNDRV_PCM_STATE_XRUN || + (substream->runtime->state ==SNDRV_PCM_STATE_XRUN || mconfig->pipe->state ==SKL_PIPE_CREATED || mconfig->pipe->state ==SKL_PIPE_PAUSED )) {
@@ -593,7 +593,7 @@ static int skl_link_pcm_prepare(struct snd_pcm_substream *substream,/* In case of XRUN recovery, reset the FW pipe to clean state */ mconfig =skl_tplg_be_get_cpr_module (dai, substream->stream);if (mconfig && !mconfig->pipe->passthru && - (substream->runtime->status->state ==SNDRV_PCM_STATE_XRUN )) + (substream->runtime->state ==SNDRV_PCM_STATE_XRUN ))skl_reset_pipe (skl, mconfig->pipe);return 0 ;
Signature · 1 line
2.35.3
Re: [PATCH 09/11] ASoC: intel: Replace runtime->status->state reference to runtime->state
Quoted 2 lines · Takashi Iwai
The recent change in ALSA core allows drivers to get the current PCM state directly from runtime object. Replace the calls accordingly.
PATCH 10/11 ASoC: sh: Replace runtime->status->state reference to runtime->state
In reply to Takashi IwaiThe recent change in ALSA core allows drivers to get the current PCM state directly from runtime object. Replace the calls accordingly.
---
I'm going to merge this through my sound git tree as it requires ALSA core changes.
sound/soc/sh/rz-ssi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
sound/soc/sh/rz-ssi.c +1 -1
@@ -598,7 +598,7 @@ static int rz_ssi_dma_transfer(struct rz_ssi_priv *ssi,return -EINVAL ; runtime = substream->runtime; -if (runtime->status->state ==SNDRV_PCM_STATE_DRAINING ) +if (runtime->state ==SNDRV_PCM_STATE_DRAINING )/* * Stream is ending, so do not queue up any more DMA * transfers otherwise we play partial sound clips
Signature · 1 line
2.35.3
Re: [PATCH 10/11] ASoC: sh: Replace runtime->status->state reference to runtime->state
Quoted 2 lines · Takashi Iwai
The recent change in ALSA core allows drivers to get the current PCM state directly from runtime object. Replace the calls accordingly.
PATCH 11/11 usb: gadget: Replace runtime->status->state reference to runtime->state
In reply to Takashi IwaiThe recent change in ALSA core allows drivers to get the current PCM state directly from runtime object. Replace the calls accordingly.
---
I'm going to merge this through sound git tree as it requires ALSA core changes.
drivers/usb/gadget/function/u_uac1_legacy.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/gadget/function/u_uac1_legacy.c +2 -2
@@ -158,8 +158,8 @@ size_t u_audio_playback(struct gaudio *card, void *buf, size_t count)snd_pcm_sframes_t frames; try_again: -if (runtime->status->state ==SNDRV_PCM_STATE_XRUN || - runtime->status->state ==SNDRV_PCM_STATE_SUSPENDED ) { +if (runtime->state ==SNDRV_PCM_STATE_XRUN || + runtime->state ==SNDRV_PCM_STATE_SUSPENDED ) { result =snd_pcm_kernel_ioctl (substream,SNDRV_PCM_IOCTL_PREPARE ,NULL );if (result <0 ) {
Signature · 1 line
2.35.3
Re: [PATCH 11/11] usb: gadget: Replace runtime->status->state reference to runtime->state
Quoted 10 lines · Takashi Iwai
The recent change in ALSA core allows drivers to get the current PCM state directly from runtime object. Replace the calls accordingly.
Cc Felipe Balbi <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Signed-off-by Takashi Iwai <[email protected]>---
I'm going to merge this through sound git tree as it requires ALSA core changes.
Looks good to me:
Quoted 24 lines · Takashi Iwai
Hi,
this is a patch set for simplifying the reference to the current PCM state by having the local copy in runtime instead of relying on
runtime->statusindirection. This also hardens against the attack by modifying the mmapped status record.The first patch does the basic job in the core PCM side, and the second patch flips the PCM status mmap to read-only for hardening, while the remaining patches are for drivers to follow the core change.
The conversions are straightforward. In most places, it's just replacing
runtime->status->statewithruntime->state.Takashi
===
Takashi Iwai (11):
ALSA: pcm: Avoid reference tostatus->state
ALSA: pcm: Make mmap status read-only
ALSA: aloop: Replaceruntime->status->statereference to
Quoted 15 lines
runtime->state
ALSA: firewire: Replace runtime->status->state reference to
runtime->state
ALSA: hda: Replace runtime->status->state reference to runtime->state
ALSA: asihpi: Replace runtime->status->state reference to
runtime->state
ALSA: usb-audio: Replace runtime->status->state reference to
runtime->state
ALSA: usx2y: Replace runtime->status->state reference to
runtime->state
ASoC: intel: Replace runtime->status->state reference to
runtime->state
ASoC: sh: Replace runtime->status->state reference to runtime->state
usb: gadget: Replace runtime->status->state reference to
runtime->stateNice cleanup. Perhaps, you may add a note to the second patch that the status is already mmaped as read-only in alsa-lib for ages. So no regressions are expected.
Signature · 2 lines
Jaroslav Kysela <[email protected]> Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
Quoted 42 lines · Jaroslav Kysela
Quoted 24 lines · Takashi Iwai
Hi,
this is a patch set for simplifying the reference to the current PCM state by having the local copy in runtime instead of relying on
runtime->statusindirection. This also hardens against the attack by modifying the mmapped status record.The first patch does the basic job in the core PCM side, and the second patch flips the PCM status mmap to read-only for hardening, while the remaining patches are for drivers to follow the core change.
The conversions are straightforward. In most places, it's just replacing
runtime->status->statewithruntime->state.Takashi
===
Takashi Iwai (11):
ALSA: pcm: Avoid reference tostatus->state
ALSA: pcm: Make mmap status read-only
ALSA: aloop: Replaceruntime->status->statereference toQuoted 15 lines
runtime->state ALSA: firewire: Replace runtime->status->state reference to runtime->state ALSA: hda: Replace runtime->status->state reference to runtime->state ALSA: asihpi: Replace runtime->status->state reference to runtime->state ALSA: usb-audio: Replace runtime->status->state reference to runtime->state ALSA: usx2y: Replace runtime->status->state reference to runtime->state ASoC: intel: Replace runtime->status->state reference to runtime->state ASoC: sh: Replace runtime->status->state reference to runtime->state usb: gadget: Replace runtime->status->state reference to runtime->state
Quoted 3 lines
Nice cleanup. Perhaps, you may add a note to the second patch that the status is already mmaped as read-only in alsa-lib for ages. So no regressions are expected.
Makes sense. Will add some text.
thanks,
Takashi
Takashi Sakamoto
maintainerHi,
Quoted 6 lines · Takashi Iwai
Hi,
this is a patch set for simplifying the reference to the current PCM state by having the local copy in runtime instead of relying on
runtime->statusindirection. This also hardens against the attack by modifying the mmapped status record.
The overall patches looks good to me and I have no objections, while I have some slight opinions to them in a place of sound driver developer.
Quoted 1 line
The first patch does the basic job in the core PCM side,
The main concern is indirect accessing to state field via some pointer hops. I think addition of helper macro at first step eases centre of your work, like:
```
diff --git a/include/sound/pcm.h b/include/sound/pcm.h
index 8c48a5bce88c..f6a160cb8135 100644
--- a/include/sound/pcm.h
+++ b/include/sound/pcm.h
@@ -669,6 +669,20 @@ void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
stream <= SNDRV_PCM_STREAM_LAST; \
stream++)
+/**
+ * snd_pcm_stream_state - Return state in runtime of the PCM substream.
+ * @substream: substream to check. runtime should be attached.
+ *
+ * Return state in runtime of the PCM substream. The substream should exists and
+ * runtime should be attached to it.
+ */
+static inline snd_pcm_state_t snd_pcm_stream_state(const snd_pcm_substream *substream)
+{
+ snd_BUG_ON(!(sub) || !(sub)->runtime);
+
+ return substream->runtime->status->state;
+}
```As we can see, sound driver programmer sometimes checks state of runtime in their code, thus the macro could helps them as well as centre of your change.
Quoted 2 lines
and the
second patch flips the PCM status mmap to read-only for hardening,
The patch looks good to me as well as the patch for asihpi driver, and should be in your tree independent from the others.
Quoted 5 lines
while the remaining patches are for drivers to follow the core change.
The conversions are straightforward. In most places, it's just replacing
runtime->status->statewithruntime->state.
The usage of mentioned macro can control the concerned access. Now addition of new field, state to runtime structure can be done easily.
Quoted 51 lines
Takashi
===
Takashi Iwai (11):
ALSA: pcm: Avoid reference tostatus->state
ALSA: pcm: Make mmap status read-only
ALSA: aloop: Replaceruntime->status->statereference toruntime->state ALSA: firewire: Replace runtime->status->state reference to runtime->state ALSA: hda: Replace runtime->status->state reference to runtime->state ALSA: asihpi: Replace runtime->status->state reference to runtime->state ALSA: usb-audio: Replace runtime->status->state reference to runtime->state ALSA: usx2y: Replace runtime->status->state reference to runtime->state ASoC: intel: Replace runtime->status->state reference to runtime->state ASoC: sh: Replace runtime->status->state reference to runtime->state usb: gadget: Replace runtime->status->state reference to runtime->statedrivers/usb/gadget/function/u_uac1_legacy.c | 4 +- include/sound/pcm.h | 20 ++- sound/core/oss/pcm_oss.c | 42 +++---- sound/core/pcm.c | 9 +- sound/core/pcm_compat.c | 4 +- sound/core/pcm_lib.c | 16 +-- sound/core/pcm_native.c | 128 ++++++++++---------- sound/drivers/aloop.c | 4 +- sound/firewire/bebob/bebob_pcm.c | 4 +- sound/firewire/dice/dice-pcm.c | 4 +- sound/firewire/digi00x/digi00x-pcm.c | 4 +- sound/firewire/fireface/ff-pcm.c | 4 +- sound/firewire/fireworks/fireworks_pcm.c | 4 +- sound/firewire/motu/motu-pcm.c | 4 +- sound/firewire/oxfw/oxfw-pcm.c | 8 +- sound/firewire/tascam/tascam-pcm.c | 4 +- sound/hda/hdmi_chmap.c | 2 +- sound/pci/asihpi/asihpi.c | 2 +- sound/soc/intel/skylake/skl-pcm.c | 4 +- sound/soc/sh/rz-ssi.c | 2 +- sound/usb/pcm.c | 4 +- sound/usb/usx2y/usbusx2yaudio.c | 3 +- sound/usb/usx2y/usx2yhwdeppcm.c | 3 +- 23 files changed, 150 insertions(+), 133 deletions(-)===
Quoted 14 lines
Cc Bard Liao <[email protected]>Cc Cezary Rojewski <[email protected]>Cc Felipe Balbi <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Cc Kai Vehmanen <[email protected]>Cc Liam Girdwood <[email protected]>Cc Mark Brown <[email protected]>Cc Peter Ujfalusi <[email protected]>Cc Pierre-Louis Bossart <[email protected]>Cc Ranjani Sridharan <[email protected]>Cc Takashi Sakamoto <[email protected]>Signature · 1 line
2.35.3
Regards
Takashi Sakamoto
Quoted 44 lines · Takashi Sakamoto
Hi,
Quoted 6 lines · Takashi Iwai
Hi,
this is a patch set for simplifying the reference to the current PCM state by having the local copy in runtime instead of relying on
runtime->statusindirection. This also hardens against the attack by modifying the mmapped status record.The overall patches looks good to me and I have no objections, while I have some slight opinions to them in a place of sound driver developer.
Quoted 1 line
The first patch does the basic job in the core PCM side,
The main concern is indirect accessing to state field via some pointer hops. I think addition of helper macro at first step eases centre of your work, like:
```
include/sound/pcm.h +13
-0@@ -669,6 +669,20 @@ void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream, stream <=SNDRV_PCM_STREAM_LAST ; \ stream++) +/** +* snd_pcm_stream_state - Return state in runtime of the PCM substream. +* @substream: substream to check. runtime should be attached. +* +* Return state in runtime of the PCM substream. The substream should exists and +* runtime should be attached to it. +*/ +static inline snd_pcm_state_t snd_pcm_stream_state (const snd_pcm_substream *substream) +{ +snd_BUG_ON (!(sub) || !(sub)->runtime); + +return substream->runtime->status->state; +}```
Quoted 3 lines
As we can see, sound driver programmer sometimes checks state of runtime in their code, thus the macro could helps them as well as centre of your change.
This might help if we may need any change in future again.
But the NULL check is superfluous in most places, and the state check is done often in a hot path, it's better to rip off. So it's only about the read of runtime->state, and then it's doubtful whether it brings much value at this moment; that's the reason I didn't introduce such a macro for now.
Takashi
Takashi Sakamoto
maintainerQuoted 52 lines · Takashi Iwai
Quoted 44 lines · Takashi Sakamoto
Hi,
Quoted 6 lines · Takashi Iwai
Hi,
this is a patch set for simplifying the reference to the current PCM state by having the local copy in runtime instead of relying on
runtime->statusindirection. This also hardens against the attack by modifying the mmapped status record.The overall patches looks good to me and I have no objections, while I have some slight opinions to them in a place of sound driver developer.
Quoted 1 line
The first patch does the basic job in the core PCM side,
The main concern is indirect accessing to state field via some pointer hops. I think addition of helper macro at first step eases centre of your work, like:
```
include/sound/pcm.h +13
-0@@ -669,6 +669,20 @@ void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream, stream <=SNDRV_PCM_STREAM_LAST ; \ stream++) +/** +* snd_pcm_stream_state - Return state in runtime of the PCM substream. +* @substream: substream to check. runtime should be attached. +* +* Return state in runtime of the PCM substream. The substream should exists and +* runtime should be attached to it. +*/ +static inline snd_pcm_state_t snd_pcm_stream_state (const snd_pcm_substream *substream) +{ +snd_BUG_ON (!(sub) || !(sub)->runtime); + +return substream->runtime->status->state; +}```
Quoted 3 lines
As we can see, sound driver programmer sometimes checks state of runtime in their code, thus the macro could helps them as well as centre of your change.
This might help if we may need any change in future again.
Quoted 5 lines
But the NULL check is superfluous in most places, and the state check is done often in a hot path, it's better to rip off. So it's only about the read of
runtime->state, and then it's doubtful whether it brings much value at this moment; that's the reason I didn't introduce such a macro for now.
Yes, so I have no strong suggestion to introduce the macro and no objections to apply the patches as is.
I'm too conservative developer and consider about usage of memory barrier always when seeing such state check since we should pay enough attension to instruction reordering and concurrent accesses across processors for such case. Fortunately we have no reports for such issue yet. When posting the last message, it was on my mind and it's still.
Regards
Takashi Sakamoto