All of lore.kernel.org
 help / color / mirror / Atom feed
From: Takashi Iwai <tiwai@suse.de>
To: Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
Cc: Linux-ALSA <alsa-devel@alsa-project.org>,
	Mark Brown <broonie@kernel.org>, Simon <horms@verge.net.au>,
	Takashi Sakamoto <o-takashi@sakamocchi.jp>
Subject: Re: [PATCH 0/6] ALSA: pcm: check parameter on snd_pcm_running()
Date: Tue, 28 Nov 2017 07:42:48 +0100	[thread overview]
Message-ID: <s5ho9nmlx93.wl-tiwai@suse.de> (raw)
In-Reply-To: <87a7z6udif.wl%kuninori.morimoto.gx@renesas.com>

On Tue, 28 Nov 2017 07:33:24 +0100,
Kuninori Morimoto wrote:
> 
> 
> Hi Mark, Takashi-san
> 
> I wonder are these patches acceptable ? or not ?
> If acceptable, should I resend ?

Since the use-case to pass NULL is limited, I'd rather avoid the
change.  If there were more than handful users, maybe I would rethink,
but currently not convincing enough.

And, the NULL check is needed only when the caller is outside the
stream lock.  Such a situation already smells like a buggy code.
We don't need to make it easier :)


thanks,

Takashi

> > Takashi Sakamoto wrote:
> > > 
> > > On Nov 9 2017 11:11, Kuninori Morimoto wrote:
> > > >
> > > > Hi Takashi-san, Mark
> > > >
> > > > snd_pcm_running() is using "substream" and "substream->runtime"
> > > > pointer, no check.
> > > > These patches adds its check in function,
> > > > and removes duplicate checks from each drivers.
> > > >
> > > > Not super important, but can be cleanup
> > > >
> > > > Kuninori Morimoto (6):
> > > >    ALSA: pcm: check parameter on snd_pcm_running()
> > > >    ALSA: pdaudiocf: remove unneeded check for snd_pcm_running()
> > > >    ASoC: dwc: remove unneeded check for snd_pcm_running()
> > > >    ASoC: omap-hdmi-audio: remove unneeded check for snd_pcm_running()
> > > >    ASoC: xtfpga-i2s: remove unneeded check for snd_pcm_running()
> > > >    ASoC: rsnd: remove unneeded check for snd_pcm_running()
> > > >
> > > >   include/sound/pcm.h                    | 3 +++
> > > >   sound/pcmcia/pdaudiocf/pdaudiocf_irq.c | 2 +-
> > > >   sound/soc/dwc/dwc-pcm.c                | 2 +-
> > > >   sound/soc/omap/omap-hdmi-audio.c       | 3 +--
> > > >   sound/soc/sh/rcar/core.c               | 5 +----
> > > >   sound/soc/xtensa/xtfpga-i2s.c          | 4 ++--
> > > >   6 files changed, 9 insertions(+), 10 deletions(-)
> > > 
> > > This is a bad direction. I exactly oppose to your idea.
> > > 
> > > >  include/sound/pcm.h | 3 +++
> > > >  1 file changed, 3 insertions(+)
> > > >
> > > > diff --git a/include/sound/pcm.h b/include/sound/pcm.h
> > > > index 24febf9..a8e49f5 100644
> > > > --- a/include/sound/pcm.h
> > > > +++ b/include/sound/pcm.h
> > > > @@ -664,6 +664,9 @@ void snd_pcm_stream_unlock_irqrestore(struct 
> > > snd_pcm_substream *substream,
> > > >   */
> > > >  static inline int snd_pcm_running(struct snd_pcm_substream *substream)
> > > >  {
> > > > +     if (!substream || !substream->runtime)
> > > > +             return 0;
> > > > +
> > > >       return (substream->runtime->status->state == 
> > > SNDRV_PCM_STATE_RUNNING ||
> > > >               (substream->runtime->status->state == 
> > > SNDRV_PCM_STATE_DRAINING &&
> > > >                substream->stream == SNDRV_PCM_STREAM_PLAYBACK));
> > > 
> > > In a view of 'design by contract', this function has a pre-condition
> > > that a given argument should not be NULL. Callers _should_ guarantee
> > > it to keep semantics of this function.
> > 
> > Generally I agree, but note that it depends on the exposure of the API
> > function itself.  If the API function is supposed to be an interface
> > directly communicated with the outside, it's not seldom to allow NULL
> > there.  An implicit NULL-check would be handy and often makes coding
> > easier (see the case of free()).
> > 
> > > Your idea appends the duty of callers to this function. This causes a
> > > semantical contradiction. If it were something to bring kernel
> > > corruption such as BUG_ON(), the original design would be kept. When
> > > substream is NULL, it's a bug of drivers in adding PCM
> > > components. When runtime is NULL, it's a bug of ALSA PCM core in
> > > handling open system call.
> > 
> > When you call snd_pcm_running(), basically you're evaluating the PCM
> > stream status, and likely a state machine.  It often assumes that PCM
> > state is consistent during the following action, and it implies the
> > PCM stream lock was acquired.  And, of course, PCM stream lock
> > requires the non-NULL substream.
> > 
> > That said, if the code has a proper protection for the PCM stream
> > consistency, the substream NULL check had to be done far before that
> > point due to a stream lock.
> > 
> > Though, most codes aren't super-critical about the state change and
> > the direct snd_pcm_running() works in most cases.  But in the perfect
> > world, stream locking is preferred around the state evaluation and the
> > action according to it.
> > 
> > 
> > thanks,
> > 
> > Takashi
> 
> 
> Best regards
> ---
> Kuninori Morimoto
> 

  reply	other threads:[~2017-11-28  6:42 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-09  2:11 [PATCH 0/6] ALSA: pcm: check parameter on snd_pcm_running() Kuninori Morimoto
2017-11-09  2:11 ` [PATCH 1/6] " Kuninori Morimoto
2017-11-09  2:12 ` [PATCH 2/6] ALSA: pdaudiocf: remove unneeded check for snd_pcm_running() Kuninori Morimoto
2017-11-09  2:12 ` [PATCH 3/6] ASoC: dwc: " Kuninori Morimoto
2017-11-09  2:12 ` [PATCH 4/6] ASoC: omap-hdmi-audio: " Kuninori Morimoto
2017-11-09  2:14 ` [PATCH 5/6] ASoC: xtfpga-i2s: " Kuninori Morimoto
2017-11-09  2:15 ` [PATCH 6/6] ASoC: rsnd: " Kuninori Morimoto
2017-11-09  2:40 ` [PATCH 0/6] ALSA: pcm: check parameter on snd_pcm_running() Takashi Sakamoto
2017-11-09  7:41   ` Takashi Iwai
2017-11-28  6:33     ` Kuninori Morimoto
2017-11-28  6:42       ` Takashi Iwai [this message]
2017-11-28  6:53         ` Kuninori Morimoto

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=s5ho9nmlx93.wl-tiwai@suse.de \
    --to=tiwai@suse.de \
    --cc=alsa-devel@alsa-project.org \
    --cc=broonie@kernel.org \
    --cc=horms@verge.net.au \
    --cc=kuninori.morimoto.gx@renesas.com \
    --cc=o-takashi@sakamocchi.jp \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.