From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796289033; b=cipEyUaiJGm6h/SGTQ27ufxQ3FKbwF4xd7bS3gf1svEAGeEu41PoSMPBWtZH9svNp8u6 Ab32F7IX9pB531mzq167r9KYoFs8TgpJzHLIbvDEPREsopITSi5YQW0ZIAktsA2p+uV+u CkDJ3UAy1kcczeAivod3K+tc89m3O131oSO3rRw7ZKcU2RBoMbh5zVu/59zlTQabmVuKF bli7SGX3GpkTVzeWl9p/mnaJcD0NaUvZIxx2sanUCiG2I6vxL6c/6CyNotpp4BpC0pBWS 4XB+++f3CvYqo9cPw5A8MLSl7Env/tgUsOVvl3k3JvyfUyxsw0ZA4NTzJQ9v8zclZOA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796289033; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=2x7xj91/Gs263Xm5laWY3MpLWl6IgwcPB5A1pIRK1WQ=; b=Q8A9h8dKq8YBX7gecjddmf0Xt8m6LnbsYSb9UaH7riwCxVexPuUnjAQnizbGK4eV7N+M dKOjqc/h6hTODM8dec7D3BQSIVlvxJXD8tsraQ8kbZaiwlIB/VfeI9UFT32hCBxCBFC2L 6UnBbTm4jf8JtUMVMv4yp0Fxo7jYnJMD/T/eG8eKksHNcZlA5KPJMDusqOeyosT2EAdua ljw+YLJ2aMTNAiMhqq9oiCajxKpVkWRKeSOF8shN5bE05dbRoRvpsXEIjRZHp6L855YZo h+BUkTlU4KXZolyrhff6azsP2j7UO84qTG/UKn0llnxavSZwXnO9pLcJjcVsTZ38iUw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=BmBlOGk3; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B7148606A322 for ; Thu, 3 Dec 2026 09:10:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=BmBlOGk3 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7D8D11F00893; Thu, 3 Dec 2026 09:10:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796289033; bh=2x7xj91/Gs263Xm5laWY3MpLWl6IgwcPB5A1pIRK1WQ=; h=From:Subject:Reply-To:To:In-Reply-To:References:Date; b=BmBlOGk3Ra6NohQaMtL8LIJGHVXTigzUm8qQ5C4RaBRBpcb3lRI8xkN1GkuJCwDHT yIJOXNyvor25tV+fp2eq787tDqnMf8yDMWo4hIAnuFT2GXqqHqSIsRwBMJugay3+93 oxnYirXYmbvL2ztK2QBA6S7Uj9p/VMWtEOqWziPS/CYGcqU/uuThJ4KboDFuCLNsC5 X+fLM3OMKksdNKKCEFKBQSfUezbVxeNDa0drINvFrDmIr7Ytpy+eLyAxQJYpZMQ88T YD+Xni9uZJKZSmdEexF1dXnvvmogQDN/m/1kLb1D9+I4njFSjm8ZdQJX3DoMksjdo5 WMUHhcaX8PE1w== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v2 3/4] ALSA: pcm: Prohibit buffer (re-)allocation/free during mmap Reply-To: sashiko-reviews@lists.linux.dev To: "Takashi Iwai" , linux-sound@vger.kernel.org In-Reply-To: <20261203084041.914026-4-tiwai@suse.de> References: <20261203084041.914026-1-tiwai@suse.de> <20261203084041.914026-4-tiwai@suse.de> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Thu, 03 Dec 2026 09:10:31 +0000 X-Mailing-List: linux-sound@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] ALSA: cs46xx: unchecked snd_pcm_lib_free_pages() leaks DMA pages on mmap - [Medium] ALSA: pcm: mmap_count check breaks emu10k1 iommu_workaround hw_params -- commit e8658b5ebd7a644a0d7a3fb31efe7b8d7e460650 Author: Takashi Iwai ALSA: pcm: Prohibit buffer (re-)allocation/free during mmap This commit adds mmap_count checks to snd_pcm_lib_malloc_pages() and snd_pcm_lib_free_pages() so both return -EBADFD when the PCM buffer is currently mapped. Link: https://lore.kernel.org/20261202140117.1164633-1-ahmedabdelmoumen05@g= mail.com > diff --git a/sound/core/pcm_memory.c b/sound/core/pcm_memory.c > index cfe9f10e7359d..7aaa45f3bac14 100644 > --- a/sound/core/pcm_memory.c > +++ b/sound/core/pcm_memory.c > @@ -431,6 +431,9 @@ int snd_pcm_lib_malloc_pages(struct snd_pcm_substream= *substream, size_t size) > runtime =3D substream->runtime; > card =3D substream->pcm->card; > =20 > + if (atomic_read(&substream->mmap_count)) > + return -EBADFD; > + > if (runtime->dma_buffer_p) { > /* perphaps, we might free the large DMA memory region > to save some space here, but the actual solution [Severity: Medium] Can this mmap_count check break same-size OSS hw_params updates on emu10k1 playback streams when emu->iommu_workaround is enabled? When an emu10k1 device is behind a non-passthrough IOMMU, snd_emu10k1_playback_hw_params() adds EMUPAGESIZE before calling snd_pcm_lib_malloc_pages() and subtracts it from runtime->dma_bytes afterward: sound/pci/emu10k1/emupcm.c:snd_emu10k1_playback_hw_params() { ... alloc_size =3D params_buffer_bytes(hw_params); if (emu->iommu_workaround) alloc_size +=3D EMUPAGESIZE; err =3D snd_pcm_lib_malloc_pages(substream, alloc_size); if (err < 0) return err; if (emu->iommu_workaround && runtime->dma_bytes >=3D EMUPAGESIZE) runtime->dma_bytes -=3D EMUPAGESIZE; ... } On a subsequent same-size OSS hw_params call during mmap (such as changing rate or channels), alloc_size is runtime->dma_bytes + EMUPAGESIZE. Even with commit "ALSA: pcm: Don't reallocate for the same sized buffers" checking runtime->dma_bytes =3D=3D size ahead of this check, execution still hits -EBADFD instead of reusing runtime->dma_buffer_p. Because snd_emu10k1_pcm_channel_alloc() has already reallocated epcm->voices and snd_pcm_hw_params() skips hw_free cleanup while mmap_count is non-zero, the stream is left with mismatched voice allocations and runtime->oss.params stuck set until munmap(). > @@ -488,6 +491,8 @@ int snd_pcm_lib_free_pages(struct snd_pcm_substream *= substream) > runtime =3D substream->runtime; > if (runtime->dma_area =3D=3D NULL) > return 0; > + if (atomic_read(&substream->mmap_count)) > + return -EBADFD; > if (runtime->dma_buffer_p !=3D &substream->dma_buffer) { > struct snd_card *card =3D substream->pcm->card; > =20 [Severity: Medium] Can returning -EBADFD here cause cs46xx to permanently leak dynamically allocated DMA buffers when an OSS client changes fragment counts while mmapped? snd_cs46xx_playback_hw_params() ignores the return value of snd_pcm_lib_free_pages() before overwriting runtime->dma_buffer_p with &cpcm->hw_buf, and clears the runtime buffer before calling snd_pcm_lib_malloc_pages() on the reverse path: sound/pci/cs46xx/cs46xx_lib.c:snd_cs46xx_playback_hw_params() { ... if (params_periods(hw_params) =3D=3D CS46XX_FRAGS) { if (runtime->dma_area !=3D cpcm->hw_buf.area) snd_pcm_lib_free_pages(substream); snd_pcm_set_runtime_buffer(substream, &cpcm->hw_buf); ... } else { if (runtime->dma_area =3D=3D cpcm->hw_buf.area) snd_pcm_set_runtime_buffer(substream, NULL); err =3D snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params= )); if (err < 0) return err; ... } When an OSS client keeps buffer_bytes unchanged and switches to CS46XX_FRAGS periods during mmap, snd_pcm_lib_free_pages() fails without freeing runtime->dma_buffer_p. Overwriting it leaks the snd_dma_buffer, its DMA pages, and card->total_pcm_alloc_bytes accounting. On the reverse transition away from hw_buf, clearing the runtime buffer makes snd_pcm_lib_malloc_pages() return -EBADFD even with commit "ALSA: pcm: Don't reallocate for the same sized buffers", leaving the mmapped stream with NULL dma_area and zero dma_bytes. snd_cs46xx_capture_hw_params() has the same two issues. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261203084041.9140= 26-1-tiwai@suse.de?part=3D3