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 <[email protected]>
Cc: Eric Anholt <[email protected]>
Cc: Stefan Wahren <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>
Cc: Florian Fainelli <[email protected]>
Cc: Ray Jui <[email protected]>
Cc: Scott Branden <[email protected]>
Cc: Andy Shevchenko <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---
.../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 <sound/pcm-indirect.h>
#include <linux/workqueue.h>
+#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,
--
2.13.6
Hi Kirill,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on staging/staging-testing]
[also build test WARNING on v4.17-rc1]
[cannot apply to anholt/for-next]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Kirill-Marinushkin/staging-bcm2835-audio-Disconnect-and-free-vchi_instance-on-module_exit/20180417-193147
config: arm-allmodconfig
compiler: arm-linux-gnueabi-gcc (Debian 7.2.0-11) 7.2.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
make.cross ARCH=arm allmodconfig
make.cross ARCH=arm
Note: it may well be a FALSE warning. FWIW you are at least aware of it now.
http://gcc.gnu.org/wiki/Better_Uninitialized_Warnings
All warnings (new ones prefixed by >>):
vim +/err +307 drivers/staging/vc04_services/bcm2835-audio/bcm2835.c
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 274
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 275 static int snd_add_child_device(struct device *device,
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 276 struct bcm2835_audio_driver *audio_driver,
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 277 u32 numchans)
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 278 {
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 279 struct snd_card *card;
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 280 struct device *child;
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 281 struct bcm2835_chip *chip;
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 282 int err, i;
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 283
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 284 child = snd_create_device(device, &audio_driver->driver,
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 285 audio_driver->driver.name);
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 286 if (IS_ERR(child)) {
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 287 dev_err(device,
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 288 "Unable to create child device %p, error %ld",
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 289 audio_driver->driver.name,
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 290 PTR_ERR(child));
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 291 return PTR_ERR(child);
23b028c8 drivers/staging/bcm2835-audio/bcm2835.c Michael Zoran 2017-01-25 292 }
23b028c8 drivers/staging/bcm2835-audio/bcm2835.c Michael Zoran 2017-01-25 293
626118b4 drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Kirill Marinushkin 2018-03-23 294 card = snd_bcm2835_card_new(child);
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 295 if (IS_ERR(card)) {
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 296 dev_err(child, "Failed to create card");
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 297 return PTR_ERR(card);
23b028c8 drivers/staging/bcm2835-audio/bcm2835.c Michael Zoran 2017-01-25 298 }
23b028c8 drivers/staging/bcm2835-audio/bcm2835.c Michael Zoran 2017-01-25 299
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 300 snd_card_set_dev(card, child);
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 301 strcpy(card->driver, audio_driver->driver.name);
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 302 strcpy(card->shortname, audio_driver->shortname);
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 303 strcpy(card->longname, audio_driver->longname);
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 304
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 305 err = snd_bcm2835_create(card, &chip);
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 306 if (err) {
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 @307 dev_err(child, "Failed to create chip, error %d\n", err);
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 308 return err;
23b028c8 drivers/staging/bcm2835-audio/bcm2835.c Michael Zoran 2017-01-25 309 }
23b028c8 drivers/staging/bcm2835-audio/bcm2835.c Michael Zoran 2017-01-25 310
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 311 chip->dev = child;
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 312
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 313 err = audio_driver->newpcm(chip, audio_driver->shortname,
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 314 audio_driver->route,
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 315 numchans);
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 316 if (err) {
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 317 dev_err(child, "Failed to create pcm, error %d\n", err);
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 318 return err;
23b028c8 drivers/staging/bcm2835-audio/bcm2835.c Michael Zoran 2017-01-25 319 }
23b028c8 drivers/staging/bcm2835-audio/bcm2835.c Michael Zoran 2017-01-25 320
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 321 err = audio_driver->newctl(chip);
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 322 if (err) {
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 323 dev_err(child, "Failed to create controls, error %d\n", err);
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 324 return err;
23b028c8 drivers/staging/bcm2835-audio/bcm2835.c Michael Zoran 2017-01-25 325 }
23b028c8 drivers/staging/bcm2835-audio/bcm2835.c Michael Zoran 2017-01-25 326
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 327 for (i = 0; i < numchans; i++)
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 328 chip->avail_substreams |= (1 << i);
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 329
23b028c8 drivers/staging/bcm2835-audio/bcm2835.c Michael Zoran 2017-01-25 330 err = snd_card_register(card);
23b028c8 drivers/staging/bcm2835-audio/bcm2835.c Michael Zoran 2017-01-25 331 if (err) {
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 332 dev_err(child, "Failed to register card, error %d\n", err);
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 333 return err;
23b028c8 drivers/staging/bcm2835-audio/bcm2835.c Michael Zoran 2017-01-25 334 }
23b028c8 drivers/staging/bcm2835-audio/bcm2835.c Michael Zoran 2017-01-25 335
626118b4 drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Kirill Marinushkin 2018-03-23 336 dev_set_drvdata(child, chip);
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 337 dev_info(child, "card created with %d channels\n", numchans);
23b028c8 drivers/staging/bcm2835-audio/bcm2835.c Michael Zoran 2017-01-25 338
23b028c8 drivers/staging/bcm2835-audio/bcm2835.c Michael Zoran 2017-01-25 339 return 0;
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 340 }
325b5b6c drivers/staging/vc04_services/bcm2835-audio/bcm2835.c Michael Zoran 2017-03-14 341
:::::: The code at line 307 was first introduced by commit
:::::: 325b5b6c96a863989078df402d1670d061f52d88 staging: bcm2835-audio: Add support for simultanous HDMI and Headphone audio
:::::: TO: Michael Zoran <[email protected]>
:::::: CC: Greg Kroah-Hartman <[email protected]>
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
On Tue, Apr 17, 2018 at 07:00:28AM +0200, 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:
<snip>
Patch dropped due to kbuild complaints.
Please fix up and resend.
thanks,
greg k-h
On 04/23/18 15:50, Greg Kroah-Hartman wrote:
> On Tue, Apr 17, 2018 at 07:00:28AM +0200, 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:
> <snip>
>
> Patch dropped due to kbuild complaints.
>
> Please fix up and resend.
>
> thanks,
>
> greg k-h
Hello Greg,
Due to which complains of kbuild is the patch dropped?
I would like to fix the complains, but I don't see any errors or warnings on my
side.
Are you sure that the kbuild test robot did it's job correctly? The log says:
>> it may well be a FALSE warning
Please show me what is wrong in my patch.
The log from kbuild test robot says:
>> [auto build test WARNING on staging/staging-testing]
>> [also build test WARNING on v4.17-rc1]
>> [cannot apply to anholt/for-next]
>> [if your patch is applied to the wrong git tree, please drop us a note to
help improve the system]
>>
>> url:
https://github.com/0day-ci/linux/commits/Kirill-Marinushkin/staging-bcm2835-audio-Disconnect-and-free-vchi_instance-on-module_exit/20180417-193147
>> config: arm-allmodconfig
>> compiler: arm-linux-gnueabi-gcc (Debian 7.2.0-11) 7.2.0
>> reproduce:
>> wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross
-O ~/bin/make.cross
>> chmod +x ~/bin/make.cross
>> make.cross ARCH=arm allmodconfig
>> make.cross ARCH=arm
>>
>> Note: it may well be a FALSE warning. FWIW you are at least aware of it now.
>> http://gcc.gnu.org/wiki/Better_Uninitialized_Warnings
I cannot clone from the given url, because it does not exist.
I cannot use the compiler from the log, because it is built for x86_64, and my
host machine is i686.
Below are the steps how I tried to reproduce the issue, but errors or warnings
didn't happen.
I applied the patch on top of the following upstream tag:
60cc43fc8884 (tag: v4.17-rc1) Linux 4.17-rc1
And executed:
make CROSS_COMPILE=arm-linux-gnueabihf- --jobs=4 ARCH=arm allmodconfig
make CROSS_COMPILE=arm-linux-gnueabihf- --jobs=4 ARCH=arm
But the compilation succeed without any errors or warnings.
My compiler version is:
arm-linux-gnueabihf-gcc (crosstool-NG linaro-1.13.1-4.8-2014.01 - Linaro GCC
2013.11) 4.8.3 20140106 (prerelease)
Best Regards,
Kirill
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 <[email protected]>
Cc: Eric Anholt <[email protected]>
Cc: Stefan Wahren <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>
Cc: Florian Fainelli <[email protected]>
Cc: Ray Jui <[email protected]>
Cc: Scott Branden <[email protected]>
Cc: Andy Shevchenko <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---
.../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 <sound/pcm-indirect.h>
#include <linux/workqueue.h>
+#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,
--
2.13.6
Hi Kirill,
Am 24.04.2018 um 02:35 schrieb Kirill Marinushkin:
> On 04/23/18 15:50, Greg Kroah-Hartman wrote:
>> On Tue, Apr 17, 2018 at 07:00:28AM +0200, 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:
>> <snip>
>>
>> Patch dropped due to kbuild complaints.
>>
>> Please fix up and resend.
>>
>> thanks,
>>
>> greg k-h
> Hello Greg,
>
> Due to which complains of kbuild is the patch dropped?
> I would like to fix the complains, but I don't see any errors or warnings on my
> side.
> Are you sure that the kbuild test robot did it's job correctly? The log says:
>
>>> it may well be a FALSE warning
> Please show me what is wrong in my patch.
>
> The log from kbuild test robot says:
>
>>> [auto build test WARNING on staging/staging-testing]
>>> [also build test WARNING on v4.17-rc1]
>>> [cannot apply to anholt/for-next]
>>> [if your patch is applied to the wrong git tree, please drop us a note to
> help improve the system]
>>> url:
> https://github.com/0day-ci/linux/commits/Kirill-Marinushkin/staging-bcm2835-audio-Disconnect-and-free-vchi_instance-on-module_exit/20180417-193147
>>> config: arm-allmodconfig
>>> compiler: arm-linux-gnueabi-gcc (Debian 7.2.0-11) 7.2.0
>>> reproduce:
>>> wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross
> -O ~/bin/make.cross
>>> chmod +x ~/bin/make.cross
>>> make.cross ARCH=arm allmodconfig
>>> make.cross ARCH=arm
>>>
>>> Note: it may well be a FALSE warning. FWIW you are at least aware of it now.
>>> http://gcc.gnu.org/wiki/Better_Uninitialized_Warnings
> I cannot clone from the given url, because it does not exist.
> I cannot use the compiler from the log, because it is built for x86_64, and my
> host machine is i686.
> Below are the steps how I tried to reproduce the issue, but errors or warnings
> didn't happen.
>
> I applied the patch on top of the following upstream tag:
> 60cc43fc8884 (tag: v4.17-rc1) Linux 4.17-rc1
>
> And executed:
> make CROSS_COMPILE=arm-linux-gnueabihf- --jobs=4 ARCH=arm allmodconfig
> make CROSS_COMPILE=arm-linux-gnueabihf- --jobs=4 ARCH=arm
>
> But the compilation succeed without any errors or warnings.
>
> My compiler version is:
> arm-linux-gnueabihf-gcc (crosstool-NG linaro-1.13.1-4.8-2014.01 - Linaro GCC
> 2013.11) 4.8.3 20140106 (prerelease)
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/
Best regards
Stefan
>
> Best Regards,
> Kirill
>
On Tue, Apr 24, 2018 at 02:35:50AM +0200, Kirill Marinushkin wrote:
> On 04/23/18 15:50, Greg Kroah-Hartman wrote:
> > On Tue, Apr 17, 2018 at 07:00:28AM +0200, 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:
> > <snip>
> >
> > Patch dropped due to kbuild complaints.
> >
> > Please fix up and resend.
> >
> > thanks,
> >
> > greg k-h
>
> Hello Greg,
>
> Due to which complains of kbuild is the patch dropped?
> I would like to fix the complains, but I don't see any errors or warnings on my
> side.
> Are you sure that the kbuild test robot did it's job correctly? The log says:
>
> >> it may well be a FALSE warning
>
> Please show me what is wrong in my patch.
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.
thanks,
greg k-h
On Tue, Apr 24, 2018 at 09:44:59AM +0200, 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 <[email protected]>
> Cc: Eric Anholt <[email protected]>
> Cc: Stefan Wahren <[email protected]>
> Cc: Greg Kroah-Hartman <[email protected]>
> Cc: Florian Fainelli <[email protected]>
> Cc: Ray Jui <[email protected]>
> Cc: Scott Branden <[email protected]>
> Cc: Andy Shevchenko <[email protected]>
> Cc: [email protected]
> Cc: [email protected]
> Cc: [email protected]
> Cc: [email protected]
> Cc: [email protected]
> ---
> .../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;
kbuild is complaining that "err" is uninitialized here but for some
reason the line numbers are off. It's a real bug.
regards,
dan carpenter
On Tue, Apr 24, 2018 at 10:44 AM, Kirill Marinushkin
<[email protected]> 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.
> Signed-off-by: Kirill Marinushkin <[email protected]>
> Cc: Eric Anholt <[email protected]>
> Cc: Stefan Wahren <[email protected]>
> Cc: Greg Kroah-Hartman <[email protected]>
> Cc: Florian Fainelli <[email protected]>
> Cc: Ray Jui <[email protected]>
> Cc: Scott Branden <[email protected]>
> Cc: Andy Shevchenko <[email protected]>
> Cc: [email protected]
> Cc: [email protected]
> Cc: [email protected]
> Cc: [email protected]
> Cc: [email protected]
AFAIR I gave you a tag and you again missed it.
Before sending anything just check twice if all prerequisites are fulfilled.
And yes, kbuild bot is right. You need to return known value.
--
With Best Regards,
Andy Shevchenko
@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 <[email protected]>
> Cc: Eric Anholt <[email protected]>
> Cc: Stefan Wahren <[email protected]>
> Cc: Greg Kroah-Hartman <[email protected]>
> Cc: Florian Fainelli <[email protected]>
> Cc: Ray Jui <[email protected]>
> Cc: Scott Branden <[email protected]>
> Cc: Andy Shevchenko <[email protected]>
> Cc: [email protected]
> Cc: [email protected]
> Cc: [email protected]
> Cc: [email protected]
> Cc: [email protected]
> ---
> .../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 <sound/pcm-indirect.h>
> #include <linux/workqueue.h>
>
> +#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,
On Tue, Apr 24, 2018 at 9:27 PM, Kirill Marinushkin
<[email protected]> wrote:
> @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.
Ah, okay, send new version as a separate thread and if I have time I
would review it.
--
With Best Regards,
Andy Shevchenko
On 04/24/18 20:35, Andy Shevchenko wrote:
> On Tue, Apr 24, 2018 at 9:27 PM, Kirill Marinushkin
> <[email protected]> wrote:
>
>> @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.
> Ah, okay, send new version as a separate thread and if I have time I
> would review it.
>
Thank you!
But it will be in the same thread. From my understanding, keeping the thread
when sending new versions of a patch helps to keep tracking of the conversation
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 <[email protected]>
Cc: Eric Anholt <[email protected]>
Cc: Stefan Wahren <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>
Cc: Florian Fainelli <[email protected]>
Cc: Ray Jui <[email protected]>
Cc: Scott Branden <[email protected]>
Cc: Andy Shevchenko <[email protected]>
Cc: Dan Carpenter <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---
.../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..662e05bd8f05 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 -ENODEV;
+ }
+
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 <sound/pcm-indirect.h>
#include <linux/workqueue.h>
+#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,
--
2.13.6
On Tue, Apr 24, 2018 at 09:57:29PM +0200, 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 <[email protected]>
> Cc: Eric Anholt <[email protected]>
> Cc: Stefan Wahren <[email protected]>
> Cc: Greg Kroah-Hartman <[email protected]>
> Cc: Florian Fainelli <[email protected]>
> Cc: Ray Jui <[email protected]>
> Cc: Scott Branden <[email protected]>
> Cc: Andy Shevchenko <[email protected]>
> Cc: Dan Carpenter <[email protected]>
> Cc: [email protected]
> Cc: [email protected]
> Cc: [email protected]
> Cc: [email protected]
> Cc: [email protected]
> ---
> .../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(-)
What changed from v1? Always put that below the --- line as the
documentation says to do so.
v3? :)
thanks,
greg k-h
On Tue, Apr 24, 2018 at 08:51:34PM +0200, Kirill Marinushkin wrote:
> On 04/24/18 20:35, Andy Shevchenko wrote:
> > On Tue, Apr 24, 2018 at 9:27 PM, Kirill Marinushkin
> > <[email protected]> wrote:
> >
> >> @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.
> > Ah, okay, send new version as a separate thread and if I have time I
> > would review it.
> >
>
> Thank you!
> But it will be in the same thread. From my understanding, keeping the thread
> when sending new versions of a patch helps to keep tracking of the conversation
It doesn't really matter either way.
thanks,
greg k-h
On 04/25/18 08:16, Greg Kroah-Hartman wrote:
> On Tue, Apr 24, 2018 at 09:57:29PM +0200, 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 <[email protected]>
>> Cc: Eric Anholt <[email protected]>
>> Cc: Stefan Wahren <[email protected]>
>> Cc: Greg Kroah-Hartman <[email protected]>
>> Cc: Florian Fainelli <[email protected]>
>> Cc: Ray Jui <[email protected]>
>> Cc: Scott Branden <[email protected]>
>> Cc: Andy Shevchenko <[email protected]>
>> Cc: Dan Carpenter <[email protected]>
>> Cc: [email protected]
>> Cc: [email protected]
>> Cc: [email protected]
>> Cc: [email protected]
>> Cc: [email protected]
>> ---
>> .../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(-)
> What changed from v1? Always put that below the --- line as the
> documentation says to do so.
>
> v3? :)
>
> thanks,
>
> greg k-h
:)
Below is the git diff v1..v2
~~~~
diff --git a/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c
b/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c
index 009c972d93d6..662e05bd8f05 100644
--- a/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c
+++ b/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c
@@ -165,7 +165,7 @@ static int snd_bcm2835_create(struct snd_card *card,
bcm2835_devm_free_vchi_ctx, NULL, NULL);
if (!chip->vchi_ctx) {
kfree(chip);
- return err;
+ return -ENODEV;
}
err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
~~~~
Best Regards,
Kirill
Greg deleted your patch already...
regards,
dan carpenter
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 <[email protected]>
Cc: Eric Anholt <[email protected]>
Cc: Stefan Wahren <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>
Cc: Florian Fainelli <[email protected]>
Cc: Ray Jui <[email protected]>
Cc: Scott Branden <[email protected]>
Cc: Andy Shevchenko <[email protected]>
Cc: Dan Carpenter <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---
Chagelog
v1: Initial patch
v2: Fixed the compiler warning
@drivers/staging/vc04_services/bcm2835-audio/bcm2835.c:168
if (!chip->vchi_ctx) {
kfree(chip);
- return err;
+ return -ENODEV;
}
v3: Appended this changelog
.../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..662e05bd8f05 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 -ENODEV;
+ }
+
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 <sound/pcm-indirect.h>
#include <linux/workqueue.h>
+#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,
--
2.13.6