Received: by 10.192.165.148 with SMTP id m20csp4973965imm; Tue, 24 Apr 2018 11:27:41 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+cVeTKSCE46hwmPajtH5Kd8Gs8DGjZ+wYXwq1OyZaea7AuG2LcpbLyrJv+FbYgny2we0yn X-Received: by 10.98.200.152 with SMTP id i24mr25054057pfk.35.1524594461276; Tue, 24 Apr 2018 11:27:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524594461; cv=none; d=google.com; s=arc-20160816; b=PA4JRfI2TzTv5H/cCbwQTomV9NiGD24tBK7mXLlK2zxWkiqsN7eFqGCU1cUR+kPSdG Rs7dBslXFZo4UsbnkjU/KtjXWmL4eIvELqkKeNIYVxesiER7uYeXpNSBQaDa0UjpNa1f 8ELhY+keVYRoRFFFeYkuVyv+zQM1G5E782r6jthgXcyMDJPXf3/mxOiUBw0UpCpQ98DE +5yyS1rgqQsivUs38uvy4cXONLcoMNujMTL7WhpVahkcPza+w/TsfucDuDDisaBFkjLd EvQHbWwr7QrQaG1UwqOAaOg0rpw8O0DzkphrVDR39P6jL6SswWWdabS6JELuWGYkFgHM qWQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature :arc-authentication-results; bh=hJhXH4NmvItYwmFr7ARdSRGmm6Y+PoHw07gWRMSasys=; b=yb98dnnjboSvjlni8sKv7iopUsdPOYGZHnJzTe6+OLrzwu23yhqlI+gFCY6YBoZWJT lUdkgw/CY7YP8u6boh7H7JmEZj2b3sBDAYBZOFF6Kt6Mr6ZLhW/v0KNqfL/j8hVqf+Hp sUvl3JaPmvFcHxwj4xR4dbRdYC1JCPJ812BnHNGOPnnyyjyr251Tkq0JQUWsT5+bDt+E CLA78yzMK7i/z/0LLPxQYKArt62PJjzpfocrhvJz9yEnuBs1OuTGqM5nFeoOF6J9oe9R Ghh2X3XYiCKQ7LUf6MzUu5MnAGjdUgsh8PyuDd0C+9U/YzJByhila5OnMTBSHjtwVzCp i5uA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Fjb+f7v1; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f5si11980880pga.595.2018.04.24.11.27.26; Tue, 24 Apr 2018 11:27:41 -0700 (PDT) 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=@gmail.com header.s=20161025 header.b=Fjb+f7v1; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752583AbeDXS0T (ORCPT + 99 others); Tue, 24 Apr 2018 14:26:19 -0400 Received: from mail-wm0-f67.google.com ([74.125.82.67]:32858 "EHLO mail-wm0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751213AbeDXS0R (ORCPT ); Tue, 24 Apr 2018 14:26:17 -0400 Received: by mail-wm0-f67.google.com with SMTP id x12so1609345wmc.0 for ; Tue, 24 Apr 2018 11:26:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding:content-language; bh=hJhXH4NmvItYwmFr7ARdSRGmm6Y+PoHw07gWRMSasys=; b=Fjb+f7v1EJvRy4+11or8WFjyS522NrB0HQLUvw325BPrxmEhgqH3qty049rCXcIEPc S1wlsGUUi7fU1enegKfHOa/BQ3GLiZSLAzLmii24y7FWFLP8vCKuOMyndimE80D0RraM +BPD8tKqHnFhm1iH6uMmovTHfetsdonmMFdHrzMMoiEFEZ3dvqfMe0rSQQzy7HHQnmf6 dSsjFdi2u7TYcOLCM9ejNp/IzRq9Q4cDAY74JNJ7zTC7CTt4fztH9XIAGXJZhA686q6p qZ58y8wsJFQrr9NBTtOLdt0vSR82yaJUHal2b0k1P5MLxWXtfn6tHoROcwqRF8I896lO iHGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=hJhXH4NmvItYwmFr7ARdSRGmm6Y+PoHw07gWRMSasys=; b=Ebcct5mNZy1yGUcGaZefDJ5BvpTnyP5ILAgrIsK7B9ug07Cy1J2mxlRDcn1FqWvEec FsgD83JYv70Nj2sEkDfovNZond/8ug5AybizNUiDcEuIUFyMFxgn27E1aICNLxydaEHb 94f+vaa4Q8XV1RLFKVfZxgB1ouWsXXfdg1RaWyWXSYwcFf7ilB/IrObJ4QN0dOLRbTvY 2qhoHBVbmBdjw4M56CDaawqubnZCIVsNEeVxePSeDGskUIwdtS052x5TpQsto3Z9qCQ/ N6qNpj7UOdHblpBXD6gljbE2BDu2Od6vWLq1/qhoSV7CLUD1uUklSt85483Dc8fvQB+j vzXQ== X-Gm-Message-State: ALQs6tBJmZn3HiMicfx13RYQxgFSmLCyMlYdA6oAF6m1Q67E3EJhiViW nSFYzzCxInvVTURRiD4w9ZqOEXb0pBA= X-Received: by 10.28.140.204 with SMTP id o195mr13428738wmd.35.1524594376058; Tue, 24 Apr 2018 11:26:16 -0700 (PDT) Received: from [192.168.1.3] (x55b277b7.dyn.telefonica.de. [85.178.119.183]) by smtp.gmail.com with ESMTPSA id o2sm9449085wmo.12.2018.04.24.11.26.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 24 Apr 2018 11:26:15 -0700 (PDT) Subject: Re: [RESEND PATCH] staging: bcm2835-audio: Disconnect and free vchi_instance on module_exit() To: Greg Kroah-Hartman , Andy Shevchenko , Eric Anholt , Stefan Wahren , Florian Fainelli , Ray Jui , Scott Branden , Dan Carpenter Cc: bcm-kernel-feedback-list@broadcom.com, linux-rpi-kernel@lists.infradead.org, linux-arm-kernel@lists.infradead.org, devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org References: <20180424071618.GB17825@kroah.com> <20180424074459.7215-1-k.marinushkin@gmail.com> From: Kirill Marinushkin Message-ID: <9e4fc4a0-370b-9101-2809-4040de58553c@gmail.com> Date: Tue, 24 Apr 2018 20:27:06 +0200 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: <20180424074459.7215-1-k.marinushkin@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Content-Language: en-US Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org @Greg > I have no idea as the patch is long gone from my queue now. If you > think all is fine, please resend it and I will be glad to review it > again. Stefan, Dan and Andy explained me, what caused the compiler warning in my patch, and why I couldn't reproduce it. I will fix the root cause, and send the patch v2. @Stefan > this is an old toolchain. You can download newer ones here (even for i686): > https://releases.linaro.org/components/toolchain/binaries/7.2-2017.11/arm-linux-gnueabihf/ Thank you for the hint. Looks like I will be able to catch this compiler warning with the newer toolchain. @Dan > kbuild is complaining that "err" is uninitialized here but for some > reason the line numbers are off. It's a real bug. Thank you for the review. Yes, indeed, this is not correct. I will fix it. @Andy > AFAIR I gave you a tag and you again missed it. > Before sending anything just check twice if all prerequisites are fulfilled. I think you mix it up. This is a new patch, you didn't review it before. > And yes, kbuild bot is right. You need to return known value. Yes, that's right. I will fix it. Best Regards, Kirill On 04/24/18 09:44, Kirill Marinushkin wrote: > In the current implementation, vchi_instance is inited during the first > call of bcm2835_audio_open_connection(), and is never freed. It causes a > memory leak when the module `snd_bcm2835` is removed. > > Here is how this commit fixes it: > > * the VCHI context (including vchi_instance) is created once in the > platform's devres > * the VCHI context is allocated and connected once during module_init() > * all created bcm2835_chips have a pointer to this VCHI context > * bcm2835_audio_open_connection() can access the VCHI context through the > associated bcm2835_chip > * the VCHI context is disconnected and freed once during module_exit() > > After this commit is applied, I don't see other issues with the module's > init/exit, so I also remove the associated TODO task. > > Steps to reproduce the memory leak before this commit: > > ~~~~ > root@raspberrypi:/home/pi# aplay test0.wav > Playing WAVE 'test0.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Ster > ^CAborted by signal Interrupt... > root@raspberrypi:/home/pi# rmmod snd_bcm2835 > root@raspberrypi:/home/pi# modprobe snd_bcm2835 > root@raspberrypi:/home/pi# aplay test0.wav > Playing WAVE 'test0.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Ster > ^CAborted by signal Interrupt... > root@raspberrypi:/home/pi# echo scan > /sys/kernel/debug/kmemleak > root@raspberrypi:/home/pi# cat /sys/kernel/debug/kmemleak > unreferenced object 0xb6794c00 (size 128): > comm "aplay", pid 406, jiffies 36870 (age 116.650s) > hex dump (first 32 bytes): > 08 a5 82 81 01 00 00 00 08 4c 79 b6 08 4c 79 b6 .........Ly..Ly. > 00 00 00 00 00 00 00 00 ad 4e ad de ff ff ff ff .........N...... > backtrace: > [<802af5e0>] kmem_cache_alloc_trace+0x294/0x3d0 > [<806ce620>] vchiq_initialise+0x98/0x1b0 > [<806d0b34>] vchi_initialise+0x24/0x34 > [<7f1311ec>] 0x7f1311ec > [<7f1303bc>] 0x7f1303bc > [<7f130590>] 0x7f130590 > [<7f111fd8>] snd_pcm_open_substream+0x68/0xc4 [snd_pcm] > [<7f112108>] snd_pcm_open+0xd4/0x248 [snd_pcm] > [<7f112334>] snd_pcm_playback_open+0x4c/0x6c [snd_pcm] > [<7f0e250c>] snd_open+0xa8/0x14c [snd] > [<802ce590>] chrdev_open+0xac/0x188 > [<802c57b4>] do_dentry_open+0x10c/0x314 > [<802c6ba8>] vfs_open+0x5c/0x88 > [<802d9a68>] path_openat+0x368/0x944 > [<802dacd4>] do_filp_open+0x70/0xc4 > [<802c6f70>] do_sys_open+0x110/0x1d4 > ~~~~ > > Signed-off-by: Kirill Marinushkin > Cc: Eric Anholt > Cc: Stefan Wahren > Cc: Greg Kroah-Hartman > Cc: Florian Fainelli > Cc: Ray Jui > Cc: Scott Branden > Cc: Andy Shevchenko > Cc: bcm-kernel-feedback-list@broadcom.com > Cc: linux-rpi-kernel@lists.infradead.org > Cc: linux-arm-kernel@lists.infradead.org > Cc: devel@driverdev.osuosl.org > Cc: linux-kernel@vger.kernel.org > --- > .../vc04_services/bcm2835-audio/bcm2835-vchiq.c | 64 +++++++++++++--------- > .../staging/vc04_services/bcm2835-audio/bcm2835.c | 43 ++++++++++++++- > .../staging/vc04_services/bcm2835-audio/bcm2835.h | 12 ++++ > 3 files changed, 91 insertions(+), 28 deletions(-) > > diff --git a/drivers/staging/vc04_services/bcm2835-audio/bcm2835-vchiq.c b/drivers/staging/vc04_services/bcm2835-audio/bcm2835-vchiq.c > index 3c6f1d91d22d..389a18f9350a 100644 > --- a/drivers/staging/vc04_services/bcm2835-audio/bcm2835-vchiq.c > +++ b/drivers/staging/vc04_services/bcm2835-audio/bcm2835-vchiq.c > @@ -33,7 +33,6 @@ > > /* ---- Include Files -------------------------------------------------------- */ > > -#include "interface/vchi/vchi.h" > #include "vc_vchi_audioserv_defs.h" > > /* ---- Private Constants and Types ------------------------------------------ */ > @@ -371,14 +370,46 @@ static int vc_vchi_audio_deinit(struct bcm2835_audio_instance *instance) > return 0; > } > > +int bcm2835_new_vchi_ctx(struct bcm2835_vchi_ctx *vchi_ctx) > +{ > + int ret; > + > + /* Initialize and create a VCHI connection */ > + ret = vchi_initialise(&vchi_ctx->vchi_instance); > + if (ret) { > + LOG_ERR("%s: failed to initialise VCHI instance (ret=%d)\n", > + __func__, ret); > + > + return -EIO; > + } > + > + ret = vchi_connect(NULL, 0, vchi_ctx->vchi_instance); > + if (ret) { > + LOG_ERR("%s: failed to connect VCHI instance (ret=%d)\n", > + __func__, ret); > + > + kfree(vchi_ctx->vchi_instance); > + vchi_ctx->vchi_instance = NULL; > + > + return -EIO; > + } > + > + return 0; > +} > + > +void bcm2835_free_vchi_ctx(struct bcm2835_vchi_ctx *vchi_ctx) > +{ > + /* Close the VCHI connection - it will also free vchi_instance */ > + WARN_ON(vchi_disconnect(vchi_ctx->vchi_instance)); > + > + vchi_ctx->vchi_instance = NULL; > +} > + > static int bcm2835_audio_open_connection(struct bcm2835_alsa_stream *alsa_stream) > { > - static VCHI_INSTANCE_T vchi_instance; > - static VCHI_CONNECTION_T *vchi_connection; > - static int initted; > struct bcm2835_audio_instance *instance = > (struct bcm2835_audio_instance *)alsa_stream->instance; > - int ret; > + struct bcm2835_vchi_ctx *vhci_ctx = alsa_stream->chip->vchi_ctx; > > LOG_INFO("%s: start\n", __func__); > BUG_ON(instance); > @@ -390,28 +421,9 @@ static int bcm2835_audio_open_connection(struct bcm2835_alsa_stream *alsa_stream > return 0; > } > > - /* Initialize and create a VCHI connection */ > - if (!initted) { > - ret = vchi_initialise(&vchi_instance); > - if (ret) { > - LOG_ERR("%s: failed to initialise VCHI instance (ret=%d)\n", > - __func__, ret); > - > - return -EIO; > - } > - ret = vchi_connect(NULL, 0, vchi_instance); > - if (ret) { > - LOG_ERR("%s: failed to connect VCHI instance (ret=%d)\n", > - __func__, ret); > - > - kfree(vchi_instance); > - return -EIO; > - } > - initted = 1; > - } > - > /* Initialize an instance of the audio service */ > - instance = vc_vchi_audio_init(vchi_instance, &vchi_connection, 1); > + instance = vc_vchi_audio_init(vhci_ctx->vchi_instance, > + &vhci_ctx->vchi_connection, 1); > > if (IS_ERR(instance)) { > LOG_ERR("%s: failed to initialize audio service\n", __func__); > diff --git a/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c b/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c > index 9030d71a3d0b..009c972d93d6 100644 > --- a/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c > +++ b/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c > @@ -65,6 +65,36 @@ static int snd_devm_add_child(struct device *dev, struct device *child) > return 0; > } > > +static void bcm2835_devm_free_vchi_ctx(struct device *dev, void *res) > +{ > + struct bcm2835_vchi_ctx *vchi_ctx = res; > + > + bcm2835_free_vchi_ctx(vchi_ctx); > +} > + > +static int bcm2835_devm_add_vchi_ctx(struct device *dev) > +{ > + struct bcm2835_vchi_ctx *vchi_ctx; > + int ret; > + > + vchi_ctx = devres_alloc(bcm2835_devm_free_vchi_ctx, sizeof(*vchi_ctx), > + GFP_KERNEL); > + if (!vchi_ctx) > + return -ENOMEM; > + > + memset(vchi_ctx, 0, sizeof(*vchi_ctx)); > + > + ret = bcm2835_new_vchi_ctx(vchi_ctx); > + if (ret) { > + devres_free(vchi_ctx); > + return ret; > + } > + > + devres_add(dev, vchi_ctx); > + > + return 0; > +} > + > static void snd_bcm2835_release(struct device *dev) > { > struct bcm2835_chip *chip = dev_get_drvdata(dev); > @@ -106,8 +136,6 @@ static int snd_bcm2835_dev_free(struct snd_device *device) > struct bcm2835_chip *chip = device->device_data; > struct snd_card *card = chip->card; > > - /* TODO: free pcm, ctl */ > - > snd_device_free(card, chip); > > return 0; > @@ -133,6 +161,13 @@ static int snd_bcm2835_create(struct snd_card *card, > > chip->card = card; > > + chip->vchi_ctx = devres_find(card->dev->parent, > + bcm2835_devm_free_vchi_ctx, NULL, NULL); > + if (!chip->vchi_ctx) { > + kfree(chip); > + return err; > + } > + > err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops); > if (err) { > kfree(chip); > @@ -403,6 +438,10 @@ static int snd_bcm2835_alsa_probe_dt(struct platform_device *pdev) > numchans); > } > > + err = bcm2835_devm_add_vchi_ctx(dev); > + if (err) > + return err; > + > err = snd_add_child_devices(dev, numchans); > if (err) > return err; > diff --git a/drivers/staging/vc04_services/bcm2835-audio/bcm2835.h b/drivers/staging/vc04_services/bcm2835-audio/bcm2835.h > index f1e43e45fd67..1c82c2ee47dc 100644 > --- a/drivers/staging/vc04_services/bcm2835-audio/bcm2835.h > +++ b/drivers/staging/vc04_services/bcm2835-audio/bcm2835.h > @@ -26,6 +26,8 @@ > #include > #include > > +#include "interface/vchi/vchi.h" > + > /* > * #define AUDIO_DEBUG_ENABLE > * #define AUDIO_VERBOSE_DEBUG_ENABLE > @@ -97,6 +99,11 @@ enum snd_bcm2835_ctrl { > PCM_PLAYBACK_DEVICE, > }; > > +struct bcm2835_vchi_ctx { > + VCHI_INSTANCE_T vchi_instance; > + VCHI_CONNECTION_T *vchi_connection; > +}; > + > /* definition of the chip-specific record */ > struct bcm2835_chip { > struct snd_card *card; > @@ -115,6 +122,8 @@ struct bcm2835_chip { > unsigned int opened; > unsigned int spdif_status; > struct mutex audio_mutex; > + > + struct bcm2835_vchi_ctx *vchi_ctx; > }; > > struct bcm2835_alsa_stream { > @@ -153,6 +162,9 @@ int snd_bcm2835_new_simple_pcm(struct bcm2835_chip *chip, > int snd_bcm2835_new_hdmi_ctl(struct bcm2835_chip *chip); > int snd_bcm2835_new_headphones_ctl(struct bcm2835_chip *chip); > > +int bcm2835_new_vchi_ctx(struct bcm2835_vchi_ctx *vchi_ctx); > +void bcm2835_free_vchi_ctx(struct bcm2835_vchi_ctx *vchi_ctx); > + > int bcm2835_audio_open(struct bcm2835_alsa_stream *alsa_stream); > int bcm2835_audio_close(struct bcm2835_alsa_stream *alsa_stream); > int bcm2835_audio_set_params(struct bcm2835_alsa_stream *alsa_stream,