From bd7fae9e3ea63b3a702767f672c0cc237953a12e Mon Sep 17 00:00:00 2001 From: Greg Kroah-Hartman Date: Thu, 31 Aug 2023 13:44:36 +0000 Subject: [PATCH] Revert "ALSA: pcm: Fix potential data race at PCM memory allocation helpers" This reverts commit 7e1d1456c8db9949459c5a24e8845cfe92430b0f which is commit bd55842ed998a622ba6611fe59b3358c9f76773d upstream. It breaks the Android abi. If it is required in the future, it can come back in an abi-safe way. Bug: 161946584 Change-Id: I1ef898f3ecf95e31f03d37785cebb5d773e0f577 Signed-off-by: Greg Kroah-Hartman --- sound/core/pcm_memory.c | 44 ++++++++--------------------------------- 1 file changed, 8 insertions(+), 36 deletions(-) diff --git a/sound/core/pcm_memory.c b/sound/core/pcm_memory.c index beca39f7c8f3..97b471d7b32e 100644 --- a/sound/core/pcm_memory.c +++ b/sound/core/pcm_memory.c @@ -31,40 +31,14 @@ static unsigned long max_alloc_per_card = 32UL * 1024UL * 1024UL; module_param(max_alloc_per_card, ulong, 0644); MODULE_PARM_DESC(max_alloc_per_card, "Max total allocation bytes per card."); -static void __update_allocated_size(struct snd_card *card, ssize_t bytes) -{ - card->total_pcm_alloc_bytes += bytes; -} - -static void update_allocated_size(struct snd_card *card, ssize_t bytes) -{ - mutex_lock(&card->memory_mutex); - __update_allocated_size(card, bytes); - mutex_unlock(&card->memory_mutex); -} - -static void decrease_allocated_size(struct snd_card *card, size_t bytes) -{ - mutex_lock(&card->memory_mutex); - WARN_ON(card->total_pcm_alloc_bytes < bytes); - __update_allocated_size(card, -(ssize_t)bytes); - mutex_unlock(&card->memory_mutex); -} - static int do_alloc_pages(struct snd_card *card, int type, struct device *dev, size_t size, struct snd_dma_buffer *dmab) { int err; - /* check and reserve the requested size */ - mutex_lock(&card->memory_mutex); if (max_alloc_per_card && - card->total_pcm_alloc_bytes + size > max_alloc_per_card) { - mutex_unlock(&card->memory_mutex); + card->total_pcm_alloc_bytes + size > max_alloc_per_card) return -ENOMEM; - } - __update_allocated_size(card, size); - mutex_unlock(&card->memory_mutex); if (IS_ENABLED(CONFIG_SND_DMA_SGBUF) && (type == SNDRV_DMA_TYPE_DEV_SG || type == SNDRV_DMA_TYPE_DEV_UC_SG) && @@ -79,14 +53,9 @@ static int do_alloc_pages(struct snd_card *card, int type, struct device *dev, err = snd_dma_alloc_pages(type, dev, size, dmab); if (!err) { - /* the actual allocation size might be bigger than requested, - * and we need to correct the account - */ - if (dmab->bytes != size) - update_allocated_size(card, dmab->bytes - size); - } else { - /* take back on allocation failure */ - decrease_allocated_size(card, size); + mutex_lock(&card->memory_mutex); + card->total_pcm_alloc_bytes += dmab->bytes; + mutex_unlock(&card->memory_mutex); } return err; } @@ -95,7 +64,10 @@ static void do_free_pages(struct snd_card *card, struct snd_dma_buffer *dmab) { if (!dmab->area) return; - decrease_allocated_size(card, dmab->bytes); + mutex_lock(&card->memory_mutex); + WARN_ON(card->total_pcm_alloc_bytes < dmab->bytes); + card->total_pcm_alloc_bytes -= dmab->bytes; + mutex_unlock(&card->memory_mutex); snd_dma_free_pages(dmab); dmab->area = NULL; }