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=1796299203; b=zxE5r51SvQiMsJvl0KibIV2PzmMxy1dci2ZNQnlOJQsCriukGemZUEF4zTTmv3Z9OvtG M8X0b5kkDw0nvBRion64VKxdjdlSnZf71LDf8WrK9Z/xzGE1IiLkqA5nlFY1NAFU6/3F+ O+dyHHq/ZtbNf4lpqN5vwYrvEXr12TSYM1iTXtd9DQZed7aSaWbru6nepb2blfqoQhHa6 8K3URyKuJqx6oh2eoViLiY8Xtg6jLG+eQPJQxK7NoNZ9/C/r+7Pb0j3idEld4fa9y/vcR 5AWnSZ6W8kLsyAsCnATd8EB0wrV4+SjH7wb+cdj0T8Hl6yJhLqZyOxB40y/Mtk/+xPg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796299203; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=obVMhe5UR6pIN8u6WRvbA+nB3AUh5YwHkw/MhnLY2dM=; b=AhK0hkwoI/KvgGnbi6/K3gpT55mHIsOiLNjRn6MlTTAHS39gvR90ByYn1OynjmWB00FR zLjaGQ2v5yVhABEkPQBHTjPvgi5tLzj01Owb9nWtp3XpyBHRSAYnjtHDPeqe2ImrO2RKs x7TAYDXFtih9tfGwFzMw6EPLPGGD8NNhKt4JRvXeeZwKJxs+23vickE1na3eefeNaDg2N EbiMh5eEUYnQ2mVqzdByxSpLpc3LmSCeaT6I2RQY1xhKrtvKYXTy0Ur2SRf4cVhmX5iu5 TT9D5jZmb4HysONmUn0Il8fp5qK4BJ/t94329BwjNtY98Q9hZtp8esu9XleBK4/TCQg== 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=b+BpIHU3; 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 5F3DB606A33F for ; Thu, 3 Dec 2026 12:00:03 +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=b+BpIHU3 Received: by smtp.kernel.org (Postfix) with ESMTPSA id D41101F00893; Thu, 3 Dec 2026 12:00:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796299202; bh=obVMhe5UR6pIN8u6WRvbA+nB3AUh5YwHkw/MhnLY2dM=; h=From:Subject:Reply-To:To:In-Reply-To:References:Date; b=b+BpIHU380wiaXfupuGu0zhX3za2ulFuecCpnnP3L1tentCN18VELIJpL56aoLrBX kKfTqDgiobXSeffLebMHPaFdS4elbIIGxx9gyRC/EpoAlVerxix3UyFi5u8hq8rqHm Ql+FOo8YItHr/XqFeQzubcMSJxFRDxIQ2HHmM/G2D/8AHngzwOjZVaNbXy54VXXUif gdf/bnSHTn/n273JC+S8gwE2XVyfigDHIuJEsY0aZABodACgvlyjGr5MYmgNdKHedP h2IK0c3ig/r+TAxkiC0Cujzc17OvUoYv08f/51HiC2QQxZrmGlTmf1Zi1OcPVUpPTO aRK8qZHg6GgrQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v3 3/3] 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: <20261203113424.1222171-4-tiwai@suse.de> References: <20261203113424.1222171-1-tiwai@suse.de> <20261203113424.1222171-4-tiwai@suse.de> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Thu, 03 Dec 2026 12:00:02 +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: pcm: mmap_count check in snd_pcm_lib_malloc_pages() breaks OSS mmap - [Medium] ALSA: cs46xx: unchecked snd_pcm_lib_free_pages() leaks DMA buffer on mmap -- commit 1cac93d127922c61e69e21736ae3127c9af966f3 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 while the substream buffer is 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 break OSS mmap hw_params updates on drivers that do not use managed_buffer_alloc? snd_pcm_hw_params() still calls ops->hw_params() for post-mmap OSS updates (such as SNDCTL_DSP_SPEED) when format, channels, period_size, and buffer_size are unchanged. Non-managed drivers like emu10k1, cs46xx, and mt8365-afe-pcm call snd_pcm_lib_malloc_pages() there. Previously, those calls reused runtime->dma_buffer_p without reallocating. Failing with -EBADFD before that check leaves earlier ops->hw_params() changes unrolled back (snd_pcm_hw_params() skips hw_free during mmap) and keeps OSS ioctls returning -EBADFD until munmap(). Would checking mmap_count only when reallocation is needed (after the runtime->dma_buffer_p->bytes >=3D size check, without shrinking runtime->dma_bytes while mmapped) avoid this regression? > @@ -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 leak runtime->dma_buffer_p and its DMA pages in snd_cs46xx_capture_hw_params()? sound/pci/cs46xx/cs46xx_lib.c:snd_cs46xx_capture_hw_params() { ... if (runtime->periods =3D=3D CS46XX_FRAGS) { if (runtime->dma_area !=3D chip->capt.hw_buf.area) snd_pcm_lib_free_pages(substream); snd_pcm_set_runtime_buffer(substream, &chip->capt.hw_buf); substream->ops =3D &snd_cs46xx_capture_ops; } else { if (runtime->dma_area =3D=3D chip->capt.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; substream->ops =3D &snd_cs46xx_capture_indirect_ops; } ... } Because snd_cs46xx_capture_hw_params() checks stale runtime->periods, an initial setup with periods =3D=3D CS46XX_FRAGS allocates in the else branch. A later post-mmap OSS hw_params update takes the CS46XX_FRAGS branch, ignores -EBADFD from snd_pcm_lib_free_pages(), and overwrites the buffer. That swaps runtime->dma_area under an active mmap and, for a dynamically allocated buffer (such as preallocate_dma=3D0), permanently leaks struct snd_dma_buffer, its DMA pages, and card->total_pcm_alloc_bytes. In the else branch, if runtime->dma_area =3D=3D chip->capt.hw_buf.area, snd_pcm_set_runtime_buffer(substream, NULL) also clears the buffer before snd_pcm_lib_malloc_pages() fails with -EBADFD, leaving a live mmapped stream with NULL runtime->dma_area and zero runtime->dma_bytes. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261203113424.1222= 171-1-tiwai@suse.de?part=3D3