From patchwork Tue Jun 23 15:49:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takashi Iwai X-Patchwork-Id: 11620943 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1CFD0618 for ; Tue, 23 Jun 2020 15:53:01 +0000 (UTC) Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id A84662078A for ; Tue, 23 Jun 2020 15:53:00 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alsa-project.org header.i=@alsa-project.org header.b="jtSWDLXF" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A84662078A Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=alsa-devel-bounces@alsa-project.org Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id 28947179C; Tue, 23 Jun 2020 17:52:11 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 28947179C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1592927579; bh=eVAQyZQd4hc/19sTDMa1ggVsaLEtjvcJ0GRDAhMrmVE=; h=From:To:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=jtSWDLXFHa2PmL8jYsRu1jkqWpFOSICRA/xqm2KhvjZ5HPM/GzQgiSQwmC5SYK34P hAZ/bV45LT0QWUl3AvAvuS5wTpI4nLB5OsUyKzdHf3CJFkB+HFd3W8TnJi17W/kEyW sb2IUKGl+iim9oJIqutTa0aKj8eLGbE3KhweSjyY= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id 36226F802C4; Tue, 23 Jun 2020 17:49:47 +0200 (CEST) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa1.perex.cz (Postfix, from userid 50401) id 6E512F802BD; Tue, 23 Jun 2020 17:49:45 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on alsa1.perex.cz X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 67FC8F8023E for ; Tue, 23 Jun 2020 17:49:33 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 67FC8F8023E X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 5991CB039 for ; Tue, 23 Jun 2020 15:49:32 +0000 (UTC) From: Takashi Iwai To: alsa-devel@alsa-project.org Subject: [PATCH alsa-lib v2 1/2] pcm: dmix: make lockless operation optional Date: Tue, 23 Jun 2020 17:49:30 +0200 Message-Id: <20200623154931.27755-2-tiwai@suse.de> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200623154931.27755-1-tiwai@suse.de> References: <20200623154931.27755-1-tiwai@suse.de> X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" The recently reported (but a long-standing) bug about the unconditional semaphore usage in the dmix implies that basically we've had no problem with the locking in the practical usages over years. Although the lockless operation has a clear merit, it's a much higher CPU usage (especially on some uncached pages), and it might lead to a potential deadlock in theory (which is hard to reproduce at will, though). This patch introduces a new configure option "--enable-lockless-dmix" or "--disable-lockless-dmix" to let user choose the default dmix operation mode. The usage of the lockless mixing has been already conditionally enabled via asoundrc and card config "direct_memory_access", so we just need to set the default value based on it. In this patch, the default is set off to the lockless mixing, i.e. the generic mixing is chosen. It makes more sense from the performance POV. For any users who still require the lockless operation, it can be enabled either via configure option or the asoundrc. The magic number used in the shmem is also changed depending on the operation mode. It's just for safety, not to conflict both operation modes with each other. Signed-off-by: Takashi Iwai --- configure.ac | 13 +++++++++++++ src/pcm/pcm_direct.c | 16 +++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index 4577c417037a..75d037d5509a 100644 --- a/configure.ac +++ b/configure.ac @@ -629,6 +629,19 @@ if test "$build_pcm_mmap_emul" = "yes"; then AC_DEFINE([BUILD_PCM_PLUGIN_MMAP_EMUL], "1", [Build PCM mmap-emul plugin]) fi +if test "$build_pcm_dmix" = "yes"; then +AC_MSG_CHECKING(for default lockless dmix) +AC_ARG_ENABLE(lockless-dmix, + AS_HELP_STRING([--enable-lockless-dmix], + [use lockless dmix as default on x86]), + lockless_dmix="$enableval", lockless_dmix="no") +if test "$lockless_dmix" = "yes"; then + AC_MSG_RESULT(yes) + AC_DEFINE([LOCKLESS_DMIX_DEFAULT], "1", [Lockless dmix as default]) +else + AC_MSG_RESULT(no) +fi +fi dnl Create PCM plugin symbol list for static library rm -f "$srcdir"/src/pcm/pcm_symbols_list.c diff --git a/src/pcm/pcm_direct.c b/src/pcm/pcm_direct.c index 665340954cf3..19c5a811262f 100644 --- a/src/pcm/pcm_direct.c +++ b/src/pcm/pcm_direct.c @@ -82,7 +82,13 @@ int snd_pcm_direct_semaphore_create_or_connect(snd_pcm_direct_t *dmix) return 0; } -#define SND_PCM_DIRECT_MAGIC (0xa15ad300 + sizeof(snd_pcm_direct_share_t)) +static unsigned int snd_pcm_direct_magic(snd_pcm_direct_t *dmix) +{ + if (!dmix->direct_memory_access) + return 0xa15ad300 + sizeof(snd_pcm_direct_share_t); + else + return 0xb15ad300 + sizeof(snd_pcm_direct_share_t); +} /* * global shared memory area @@ -132,10 +138,10 @@ retryget: buf.shm_perm.gid = dmix->ipc_gid; shmctl(dmix->shmid, IPC_SET, &buf); } - dmix->shmptr->magic = SND_PCM_DIRECT_MAGIC; + dmix->shmptr->magic = snd_pcm_direct_magic(dmix); return 1; } else { - if (dmix->shmptr->magic != SND_PCM_DIRECT_MAGIC) { + if (dmix->shmptr->magic != snd_pcm_direct_magic(dmix)) { snd_pcm_direct_shm_discard(dmix); return -EINVAL; } @@ -1892,7 +1898,11 @@ int snd_pcm_direct_parse_open_conf(snd_config_t *root, snd_config_t *conf, rec->slowptr = 1; rec->max_periods = 0; rec->var_periodsize = 0; +#ifdef LOCKLESS_DMIX_DEFAULT rec->direct_memory_access = 1; +#else + rec->direct_memory_access = 0; +#endif rec->hw_ptr_alignment = SND_PCM_HW_PTR_ALIGNMENT_AUTO; rec->tstamp_type = -1; From patchwork Tue Jun 23 15:49:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takashi Iwai X-Patchwork-Id: 11620939 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7D248618 for ; Tue, 23 Jun 2020 15:51:24 +0000 (UTC) Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 146182076E for ; Tue, 23 Jun 2020 15:51:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=alsa-project.org header.i=@alsa-project.org header.b="N2DgIQ2g" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 146182076E Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=alsa-devel-bounces@alsa-project.org Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id A01C317A2; Tue, 23 Jun 2020 17:50:34 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz A01C317A2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1592927482; bh=dS4rt7CJcy8P1xBC8m2qBteO1gXgiIT94M6HPBtbY9o=; h=From:To:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=N2DgIQ2gq9mfttUeFZGaAAW70g4wjHRR2NRYTYKYaefKzj6rvarerGWQeogP++qXj 0VXXQYp24VU9u2HFzBU7RltRiFklU1/rjVrQ7pGpJ8cjsy7DUn1L4/qPOnayw/vb0h g88Tjis0D/2Yjlsb2yPhSSpN2q0v0WJxbN53oSIM= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id 8BD72F80291; Tue, 23 Jun 2020 17:49:42 +0200 (CEST) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa1.perex.cz (Postfix, from userid 50401) id 33FD9F80249; Tue, 23 Jun 2020 17:49:40 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on alsa1.perex.cz X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 6158BF800B2 for ; Tue, 23 Jun 2020 17:49:33 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 6158BF800B2 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 5C8FFB03A for ; Tue, 23 Jun 2020 15:49:32 +0000 (UTC) From: Takashi Iwai To: alsa-devel@alsa-project.org Subject: [PATCH alsa-lib v2 2/2] pcm: dmix: Fix semaphore usage with lockless operation Date: Tue, 23 Jun 2020 17:49:31 +0200 Message-Id: <20200623154931.27755-3-tiwai@suse.de> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200623154931.27755-1-tiwai@suse.de> References: <20200623154931.27755-1-tiwai@suse.de> X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" As Maarten Baert recently reported, the current dmix code applies the semaphore unnecessarily around mixing streams even when the lockless mix operation is used on x86. This was rather introduced mistakenly at the commit 267d7c728196 ("Add support of little-endian on i386/x86_64 dmix") where the generic dmix code was included on x86, too. For achieving the original performance back, this patch changes the semaphore handling to be checked at run time instead of statically at compile time. Signed-off-by: Takashi Iwai --- src/pcm/pcm_direct.h | 1 + src/pcm/pcm_dmix.c | 18 +++++++++++------- src/pcm/pcm_dmix_generic.c | 2 +- src/pcm/pcm_dmix_i386.c | 1 + src/pcm/pcm_dmix_x86_64.c | 1 + 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/pcm/pcm_direct.h b/src/pcm/pcm_direct.h index 8a236970a3a1..2150bce15449 100644 --- a/src/pcm/pcm_direct.h +++ b/src/pcm/pcm_direct.h @@ -186,6 +186,7 @@ struct snd_pcm_direct { mix_areas_32_t *remix_areas_32; mix_areas_24_t *remix_areas_24; mix_areas_u8_t *remix_areas_u8; + unsigned int use_sem; } dmix; struct { unsigned long long chn_mask; diff --git a/src/pcm/pcm_dmix.c b/src/pcm/pcm_dmix.c index 843fa3168756..e9343b19a536 100644 --- a/src/pcm/pcm_dmix.c +++ b/src/pcm/pcm_dmix.c @@ -292,13 +292,17 @@ static void remix_areas(snd_pcm_direct_t *dmix, * the area via semaphore */ #ifndef DOC_HIDDEN -#ifdef NO_CONCURRENT_ACCESS -#define dmix_down_sem(dmix) snd_pcm_direct_semaphore_down(dmix, DIRECT_IPC_SEM_CLIENT) -#define dmix_up_sem(dmix) snd_pcm_direct_semaphore_up(dmix, DIRECT_IPC_SEM_CLIENT) -#else -#define dmix_down_sem(dmix) -#define dmix_up_sem(dmix) -#endif +static void dmix_down_sem(snd_pcm_direct_t *dmix) +{ + if (dmix->u.dmix.use_sem) + snd_pcm_direct_semaphore_down(dmix, DIRECT_IPC_SEM_CLIENT); +} + +static void dmix_up_sem(snd_pcm_direct_t *dmix) +{ + if (dmix->u.dmix.use_sem) + snd_pcm_direct_semaphore_up(dmix, DIRECT_IPC_SEM_CLIENT); +} #endif /* diff --git a/src/pcm/pcm_dmix_generic.c b/src/pcm/pcm_dmix_generic.c index 40c08747a74a..8a5b6f148556 100644 --- a/src/pcm/pcm_dmix_generic.c +++ b/src/pcm/pcm_dmix_generic.c @@ -43,7 +43,6 @@ static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old, #ifndef ARCH_ADD #define ARCH_ADD(p,a) (*(p) += (a)) #define ARCH_CMPXCHG(p,a,b) (*(p)) /* fake */ -#define NO_CONCURRENT_ACCESS /* use semaphore to avoid race */ #define IS_CONCURRENT 0 /* no race check */ #endif @@ -530,6 +529,7 @@ static void generic_mix_select_callbacks(snd_pcm_direct_t *dmix) dmix->u.dmix.mix_areas_u8 = generic_mix_areas_u8; dmix->u.dmix.remix_areas_24 = generic_remix_areas_24; dmix->u.dmix.remix_areas_u8 = generic_remix_areas_u8; + dmix->u.dmix.use_sem = 1; } #endif diff --git a/src/pcm/pcm_dmix_i386.c b/src/pcm/pcm_dmix_i386.c index 1ab983a8a373..82a91c5c2897 100644 --- a/src/pcm/pcm_dmix_i386.c +++ b/src/pcm/pcm_dmix_i386.c @@ -135,4 +135,5 @@ static void mix_select_callbacks(snd_pcm_direct_t *dmix) dmix->u.dmix.mix_areas_24 = smp > 1 ? mix_areas_24_smp: mix_areas_24; dmix->u.dmix.remix_areas_24 = smp > 1 ? remix_areas_24_smp: remix_areas_24; } + dmix->u.dmix.use_sem = 0; } diff --git a/src/pcm/pcm_dmix_x86_64.c b/src/pcm/pcm_dmix_x86_64.c index 34c40d4e9d1d..4d882bfd01bf 100644 --- a/src/pcm/pcm_dmix_x86_64.c +++ b/src/pcm/pcm_dmix_x86_64.c @@ -102,4 +102,5 @@ static void mix_select_callbacks(snd_pcm_direct_t *dmix) dmix->u.dmix.remix_areas_32 = smp > 1 ? remix_areas_32_smp : remix_areas_32; dmix->u.dmix.mix_areas_24 = smp > 1 ? mix_areas_24_smp : mix_areas_24; dmix->u.dmix.remix_areas_24 = smp > 1 ? remix_areas_24_smp : remix_areas_24; + dmix->u.dmix.use_sem = 0; }