From: Takashi Iwai <tiwai@suse.de>
To: IceBsi <bsiice@msn.com>
Cc: Qing Cai <caiqing@neusoft.com>, alsa-devel@alsa-project.org
Subject: Re: [PATCH - pcm 1/1] pcm: fix "unable to create IPC shm instance" in some case
Date: Thu, 03 Mar 2016 14:53:00 +0100 [thread overview]
Message-ID: <s5hh9gn6537.wl-tiwai@suse.de> (raw)
In-Reply-To: <BLU437-SMTP418A25BEF034BD65DA159BD2BD0@phx.gbl>
On Thu, 03 Mar 2016 13:40:42 +0100,
IceBsi wrote:
>
> From: Qing Cai <caiqing@neusoft.com>
>
> As stated in manpage SHMCTL(2), shm_nattch is "No. of current attaches"
> (i.e., number of processes attached to the shared memeory). If an
> application uses alsa-lib and invokes fork() at some point, there should
> be the following execution sequence:
> 1. execute the following statement:
> pcm_direct.c:110: dmix->shmptr = shmat(dmix->shmid, 0, 0)
> (shm_nattch becomes 1)
> 2. invoke fork() in some thread.
> (shm_nattch becomes 2)
> 3. execute the following statement:
> pcm_direct.c:122: if (buf.shm_nattch == 1)
> 4. execute the following statement:
> pcm_direct.c:131: if (dmix->shmptr->magic != SND_PCM_DIRECT_MAGIC)
> (As stated in manpage SHMGET(2), "When a new shared memory segment
> is created, its contents are initialized to zero values", so
> dmix->shmptr->magic is 0)
> 5. execute the following statements:
> pcm_direct.c:132: snd_pcm_direct_shm_discard(dmix)
> pcm_direct.c:133: return -EINVAL
> The above execution sequence will cause the following error:
> unable to create IPC shm instance
> This error causes multimedia application has no sound. This error rarely
> occurs, probability is about 1%.
> Because the first user of the shared memory will get that
> dmix->shmptr->magic is 0, check dmix->shmptr->magic's value to determine
> if "we're the first user" is OK.
> Tests have been made 400+ times after this fix, and the issue no longer
> exists.
I think this is still racy. Multiple users can grab the shmem at the
very same time. Maybe it looks as if working just because both users
behavior as the first user and do clear and initialize.
The check of bus.shm_nattach=1 should be fine, per se. The problem is
the magic key check of the secondary. In the current code, as you
pointed out, this may happen before the first client finishes the
initialization.
One option would be to use some lock like pthread mutex. Another
option would be to spin for a while until it gets a non-zero value.
I guess the latter is more suitable for dmix & co.
Thoughts?
thanks,
Takashi
>
> Signed-off-by: Qing Cai <bsiice@msn.com>
> Signed-off-by: Qing Cai <caiqing@neusoft.com>
>
> diff --git a/src/pcm/pcm_direct.c b/src/pcm/pcm_direct.c
> index fd3877c..93d6f3c 100644
> --- a/src/pcm/pcm_direct.c
> +++ b/src/pcm/pcm_direct.c
> @@ -119,7 +119,7 @@ retryget:
> snd_pcm_direct_shm_discard(dmix);
> return err;
> }
> - if (buf.shm_nattch == 1) { /* we're the first user, clear the segment */
> + if (dmix->shmptr->magic != SND_PCM_DIRECT_MAGIC) { /* we're the first user, clear the segment */
> memset(dmix->shmptr, 0, sizeof(snd_pcm_direct_share_t));
> if (dmix->ipc_gid >= 0) {
> buf.shm_perm.gid = dmix->ipc_gid;
> --
> 2.7.2
>
next prev parent reply other threads:[~2016-03-03 13:53 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-03-03 12:40 [PATCH - pcm 1/1] pcm: fix "unable to create IPC shm instance" in some case IceBsi
2016-03-03 13:53 ` Takashi Iwai [this message]
2016-03-04 16:22 ` bsiice
2016-03-04 16:37 ` Takashi Iwai
2016-03-04 17:36 ` bsiice
2016-03-04 18:08 ` Takashi Iwai
2016-03-05 6:37 ` bsiice
2016-03-05 7:36 ` Takashi Iwai
2016-03-05 8:35 ` bsiice
2016-03-05 10:07 ` Takashi Iwai
2016-03-05 14:21 ` bsiice
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=s5hh9gn6537.wl-tiwai@suse.de \
--to=tiwai@suse.de \
--cc=alsa-devel@alsa-project.org \
--cc=bsiice@msn.com \
--cc=caiqing@neusoft.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox