Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp1277433rwr; Wed, 3 May 2023 12:42:23 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6v//B0UDqSofEDjE8N7h688wIPhACNItD9Lq89fXoO/zIxEirvWMYhPgE+7Tzw1TSwJ3HH X-Received: by 2002:a17:903:1103:b0:1aa:f6c3:ba24 with SMTP id n3-20020a170903110300b001aaf6c3ba24mr1652168plh.4.1683142942372; Wed, 03 May 2023 12:42:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683142942; cv=none; d=google.com; s=arc-20160816; b=EN7r48qBGJc6YdcMN5C8PIyGZ0kXbY27uPfXIWkH5pvpsVROBWY+ZfPxWAVIGSzfAQ vgHxMTmACd15DDGVj0dPW8deZDvsRA4kiwd8YsoKf00fkyQ7Y4vOlmu3pRkDPoH5SbsF sxRhIb2pD1ZXCXymw/FZKzUbjFt7aHa6NWeyy+2NPNQWLrvLtruZ4/W+N3MpdY6D03jF ZixSkyuo//w3WmN4ncq1vZJhUZQP4AxyYJiH/yba6ZFP2hFHbyzvuZmApU9UgzgHkEHp ZxQ0iy39f1rjp0zhrHIkBl9uwKXT61TjtLpX//7JSe0ysd8/GoISD83OYjh3jX7LI4rX 2VAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:subject :from:references:cc:to:content-language:user-agent:mime-version:date :message-id:dkim-signature:dkim-filter; bh=134enEbGMk+lh5qLFrd6rCqMfaAzngLEWDoCerDp574=; b=k/uGKZtYtbSd9mQYc5toytEJKgnFUMkW80x1+qiCp/Q6bkHvwHkh86pv2EcVJdQo2D 5wuc7q0tQ2DzKv+av4G8SW+xnSMbY+lCZbvk50IL5ulcl8FGeJZqje7JMejVKvufeqdV EkMADZbXfegLR9NYVagALv+KLYe503AMRehYbC+suPWmyz831mtXhWMdxNoQ/gdZlKIl y23gPKKwbygfR6rWmXiulk1ksSbW2vYxrocBvAKTLexMG44S5GBr/2HFe6smoolDnvQj 8+0zKTcZbLY4QhkMCdtUQd75iWdPoGsZkrKDMPNzLpl2vEIi/89e6wTd9nut4pHnqeAT v9UQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@perex.cz header.s=default header.b=mH1R4hNd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=perex.cz Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e6-20020a17090301c600b001a644fba6d8si35559798plh.86.2023.05.03.12.42.09; Wed, 03 May 2023 12:42:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@perex.cz header.s=default header.b=mH1R4hNd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=perex.cz Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229661AbjECTjd (ORCPT + 99 others); Wed, 3 May 2023 15:39:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229481AbjECTjb (ORCPT ); Wed, 3 May 2023 15:39:31 -0400 X-Greylist: delayed 436 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Wed, 03 May 2023 12:39:29 PDT Received: from mail1.perex.cz (mail1.perex.cz [77.48.224.245]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2DFB2619E for ; Wed, 3 May 2023 12:39:29 -0700 (PDT) Received: from mail1.perex.cz (localhost [127.0.0.1]) by smtp1.perex.cz (Perex's E-mail Delivery System) with ESMTP id 668F011E6; Wed, 3 May 2023 21:32:09 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.perex.cz 668F011E6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=perex.cz; s=default; t=1683142329; bh=134enEbGMk+lh5qLFrd6rCqMfaAzngLEWDoCerDp574=; h=Date:To:Cc:References:From:Subject:In-Reply-To:From; b=mH1R4hNdkw8mc5omaYOxBvqB+s5qmt34wH6OTqYXyqhtLd+v5/ly3hUNwGdvhf8wb arX4muFNRvDyhnoLlqXydwS43CBhirbD6HeSl1N2/cX8uT2TWV/mx1mDndmqsx/yiP ss92gGVOymJwisBmUZ0Hw/HYgbRRiVgNGOinb5KE= Received: from [192.168.100.98] (unknown [192.168.100.98]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: perex) by mail1.perex.cz (Perex's E-mail Delivery System) with ESMTPSA; Wed, 3 May 2023 21:32:03 +0200 (CEST) Message-ID: <7b80ef1e-23dd-c523-0663-4bf311c1823a@perex.cz> Date: Wed, 3 May 2023 21:32:02 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.1 Content-Language: en-US To: Takashi Iwai , Oswald Buddenhagen Cc: Jeff Chua , lkml , Bagas Sanjaya , ALSA development References: <87wn1pmm4d.wl-tiwai@suse.de> From: Jaroslav Kysela Subject: Re: linux-6.4 alsa sound broken In-Reply-To: <87wn1pmm4d.wl-tiwai@suse.de> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-8.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_MED, SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 03. 05. 23 18:10, Takashi Iwai wrote: > On Mon, 01 May 2023 09:17:20 +0200, > Oswald Buddenhagen wrote: >> >> On Mon, May 01, 2023 at 11:59:12AM +0800, Jeff Chua wrote: >>> Latest git pull from Linus's tree ... playing a simple sound file will >>> resulted in a lot of echo. >>> >> how _exactly_ does it sound? >> have you recorded a file through loopback for us to investigate? best >> would be a short sample of a clean wave (sine or sawtooth) with some >> leading and trailing silence. >> >>> Running on Lenovo X1 with .. >>> 00:1f.3 Audio device: Intel Corporation Alder Lake PCH-P High >>> Definition Audio Controller (rev 01) >>> >>> I've bisected and reverted the following patch fixed the problem. >>> >> this seems weird. so my first thought is: are you _sure_ that your >> bisect isn't "contaminated" somehow? is the effect consistent across >> several reboots with the same build? does re-applying my patch >> immediately re-introduce the problem? >> >> - this code is about silencing. getting dropouts or no playback at all >> would be plausible, while echo (that is, repetition) seems surprising. >> theoretically, the driver may be setting a bad fill_silence() >> callback which copies some garbage instead of zeroing, but the HDA >> driver doesn't set one at all (i.e., uses the default one). >> - this code must be explicitly enabled, which for all i know is done >> by almost nothing. what players did you try? did you get consistent >> results? did you try taking out audio servers from the equation? >> - the affected hardware belongs to the extremely widely used HDA >> family, which at the layer the patch is even remotely connected with >> is completely standardized. so _a lot_ of people should be affected, >> and we should be getting reports like yours by the dozen. are we? >> >> of course i can't exclude the possibility that my patch is affected by >> an uninitialized variable or memory corruption (or in the worst case >> causes it), which would of course have very hard to predict >> effects. but that should be investigated properly instead of just >> reverting, lest we might be papering over a much more serious problem. > > Oswald, this looks like a real regression by the patch. > Specially, this happens with dmix, and the issue doesn't seem specific > to the driver. It happens also with USB-audio, not only with > HD-audio. Just aplay /usr/share/sounds/alsa/Side_Left.wav or whatever > there with the dmix config showed the problem. > > The dmix uses the silence_size=boundary as a fill-all operation, and > it's a free-wheel mode, so supposedly something was overlooked in your > code refactoring. > > Could you check it and address quickly? I'd like to fix it before > 6.4-rc1 release, so if no fix comes up in a couple of days, I'll have > to revert the change for 6.4-rc1. I would revert this patch. It seems that this "do silence right after the playback is finished" mechanism is not handled in the updated code (and I overlooked that, too): - ofs = runtime->status->hw_ptr; - frames = new_hw_ptr - ofs; - if ((snd_pcm_sframes_t)frames < 0) - frames += runtime->boundary; - runtime->silence_filled -= frames; - if ((snd_pcm_sframes_t)runtime->silence_filled < 0) { - runtime->silence_filled = 0; - runtime->silence_start = new_hw_ptr; - } else { - runtime->silence_start = ofs; - } It requires to track the old and new hw_ptr, so the removal of the new_hw_ptr argument is not valid. I don't see any easy way to fix this. I would probably fix the snd_pcm_playback_hw_avail() call with the old hw_ptr which seems like the only one issue with the original code, because it makes the threshold inaccurate (it is expected to fill more silent samples). Another issue is wrong silence_start for the incremental silence calls. The patch to fix the original code may look like: diff --git a/sound/core/pcm_lib.c b/sound/core/pcm_lib.c index af1eb136feb0..70795a83e50a 100644 --- a/sound/core/pcm_lib.c +++ b/sound/core/pcm_lib.c @@ -45,7 +45,7 @@ static int fill_silence_frames(struct snd_pcm_substream *substream, void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr) { struct snd_pcm_runtime *runtime = substream->runtime; - snd_pcm_uframes_t frames, ofs, transfer; + snd_pcm_uframes_t start, frames, ofs, transfer; int err; if (runtime->silence_size < runtime->boundary) { @@ -63,12 +63,17 @@ void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_ufram } if (runtime->silence_filled >= runtime->buffer_size) return; + /* use appl_ptr as a temporary variable */ + appl_ptr = runtime->status->hw_ptr; + runtime->status->hw_ptr = new_hw_ptr; noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled; + runtime->status->hw_ptr = appl_ptr; if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold) return; frames = runtime->silence_threshold - noise_dist; if (frames > runtime->silence_size) frames = runtime->silence_size; + start = (runtime->silence_start + runtime->silence_filled) % runtime->boundary; } else { if (new_hw_ptr == ULONG_MAX) { /* initialization */ snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime); @@ -92,12 +97,13 @@ void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_ufram } } frames = runtime->buffer_size - runtime->silence_filled; + start = runtime->silence_start; } if (snd_BUG_ON(frames > runtime->buffer_size)) return; if (frames == 0) return; - ofs = runtime->silence_start % runtime->buffer_size; + ofs = start % runtime->buffer_size; while (frames > 0) { transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames; err = fill_silence_frames(substream, ofs, transfer); I'll post a complete patch when we agree on this solution. The runtime->status->hw_ptr may not be even preserved, because it is no used in the rest of code in snd_pcm_update_hw_ptr0(), but the code looks more sane. Jaroslav -- Jaroslav Kysela Linux Sound Maintainer; ALSA Project; Red Hat, Inc.