Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp3687606ybv; Mon, 10 Feb 2020 04:43:29 -0800 (PST) X-Google-Smtp-Source: APXvYqzu3pcQ2iQlGvbLjmrQzPC9cMjMHWR9ivsdOxUpg91uA/y8Wg/XRSrrJ5bMIlMcXbUPuEu1 X-Received: by 2002:a05:6830:1608:: with SMTP id g8mr857160otr.169.1581338609542; Mon, 10 Feb 2020 04:43:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581338609; cv=none; d=google.com; s=arc-20160816; b=FiFNRLJfr6TJg7VF4hpXMoUBg++c4XoX46+tmE+Es1AfZrKhhjRxmMrSou0uc9+GJQ 7XZk4bck3T1obBfDjCfKRZcX3DNyiScMsFDTkzuMLuYq45nd6NaqU475ikdY8/0zHXNJ TrTyh/dkdKgK+Hft8jNT+k7NsU5cjv/h2i94vP5EtPHPlHPXPXX6e7odhHrD5MeO9oA/ qIFKLetp96o29jNalPqCY9oZ2c+TiXHmd5pS9ovczKWsySQtxOgqArP8RGV5D6rB9OBK vyuXKw407XZw7Z15QEqTuPPChevLlFS/Vg2NsNkullzmEedmfGUKDfDxhNHIwczNyd3/ lvuA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=HvzG9ozj60FWHEMouPMPOlkkcY7UGJlpulCW/hdVSoA=; b=vcf3cLdjSpLbxDl6ny56Un6G+N532kIeKYy3iwmPIsCqol0QU5T/UbMUTihktOyHHj FQcH9Z3dIyqFJ1F7itlaIuMxeTVrP1kbCgul+FvOhJQXV9tX4z7gkNf9m00zDln+iPh8 VJkaLAVmgQ364qn6Jyoj7hn9mz1m33LHydWwAqJhaD7+UYN8V2YpohTvK8H/0yzHRCuG 98msVyQBfv7wWLAUzlmNb5rag3FlXkSiZ/mdE3Rxig6kceRkBuG+5NUXOixFNWwWfJEW AAT62xjOwdk/CNoEPW9D4pqtmboPcPRH+p2chE0NiM4qismpfDbeqB2X4+/nJeSZb23b lH2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Eh93tTZr; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r8si134834ota.288.2020.02.10.04.43.17; Mon, 10 Feb 2020 04:43:29 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Eh93tTZr; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730394AbgBJMnI (ORCPT + 99 others); Mon, 10 Feb 2020 07:43:08 -0500 Received: from mail.kernel.org ([198.145.29.99]:38356 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728940AbgBJMjq (ORCPT ); Mon, 10 Feb 2020 07:39:46 -0500 Received: from localhost (unknown [209.37.97.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 8572E2051A; Mon, 10 Feb 2020 12:39:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1581338386; bh=dQft7Jsp+PifajobanTiCRRBQKL883ifoUu3o6aGT+w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Eh93tTZrC3FAckTwswLuDrmnFKhOekDShYRQex/AiUMQ9kQZtm2YoqUdIauv+ps9z IOHTKP3HMqftbvAd+wrSBf6kKTcSw27YOeDTeiMEO34bjMo9Gb5sxvWrRjlijVXOsa Zv5ay5np1HMWdVN+DAx9j54HSTD2hGI0a5TRWw9Y= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+30edd0f34bfcdc548ac4@syzkaller.appspotmail.com, Takashi Iwai Subject: [PATCH 5.5 030/367] ALSA: pcm: Fix memory leak at closing a stream without hw_free Date: Mon, 10 Feb 2020 04:29:03 -0800 Message-Id: <20200210122426.682758235@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200210122423.695146547@linuxfoundation.org> References: <20200210122423.695146547@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Takashi Iwai commit 66f2d19f8116e16898f8d82e28573a384ddc430d upstream. ALSA PCM core recently introduced a new managed PCM buffer allocation mode that does allocate / free automatically at hw_params and hw_free. However, it overlooked the code path directly calling hw_free PCM ops at releasing the PCM substream, and it may result in a memory leak as spotted by syzkaller when no buffer preallocation is used (e.g. vmalloc buffer). This patch papers over it with a slight refactoring. The hw_free ops call and relevant tasks are unified in a new helper function, and call it from both places. Fixes: 0dba808eae26 ("ALSA: pcm: Introduce managed buffer allocation mode") Reported-by: syzbot+30edd0f34bfcdc548ac4@syzkaller.appspotmail.com Cc: Link: https://lore.kernel.org/r/20200129195907.12197-1-tiwai@suse.de Signed-off-by: Takashi Iwai Signed-off-by: Greg Kroah-Hartman --- sound/core/pcm_native.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) --- a/sound/core/pcm_native.c +++ b/sound/core/pcm_native.c @@ -785,10 +785,22 @@ end: return err; } +static int do_hw_free(struct snd_pcm_substream *substream) +{ + int result = 0; + + snd_pcm_sync_stop(substream); + if (substream->ops->hw_free) + result = substream->ops->hw_free(substream); + if (substream->managed_buffer_alloc) + snd_pcm_lib_free_pages(substream); + return result; +} + static int snd_pcm_hw_free(struct snd_pcm_substream *substream) { struct snd_pcm_runtime *runtime; - int result = 0; + int result; if (PCM_RUNTIME_CHECK(substream)) return -ENXIO; @@ -805,11 +817,7 @@ static int snd_pcm_hw_free(struct snd_pc snd_pcm_stream_unlock_irq(substream); if (atomic_read(&substream->mmap_count)) return -EBADFD; - snd_pcm_sync_stop(substream); - if (substream->ops->hw_free) - result = substream->ops->hw_free(substream); - if (substream->managed_buffer_alloc) - snd_pcm_lib_free_pages(substream); + result = do_hw_free(substream); snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN); pm_qos_remove_request(&substream->latency_pm_qos_req); return result; @@ -2466,9 +2474,7 @@ void snd_pcm_release_substream(struct sn snd_pcm_drop(substream); if (substream->hw_opened) { - if (substream->ops->hw_free && - substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) - substream->ops->hw_free(substream); + do_hw_free(substream); substream->ops->close(substream); substream->hw_opened = 0; }