Alsa-Devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
To: "Yang, Libin" <libin.yang@intel.com>, Takashi Iwai <tiwai@suse.de>
Cc: "alsa-devel@alsa-project.org" <alsa-devel@alsa-project.org>,
	"broonie@kernel.org" <broonie@kernel.org>
Subject: Re: [PATCH v2 12/12] ASoC: SOF: Intel: hda-codec: fix memory allocation
Date: Mon, 3 Jun 2019 08:45:26 -0500	[thread overview]
Message-ID: <475384fa-718f-14f4-7f5f-8257240163f6@linux.intel.com> (raw)
In-Reply-To: <96A12704CE18D347B625EE2D4A099D195285B18B@SHSMSX103.ccr.corp.intel.com>

Hi Libin,

>>>>> Please let me describe the issue here.
>>>>>
>>>>> The test case is:
>>>>> 1) Unload module with script "sudo ./sof_remove.sh" ,
>>>>> 2) reload module with script "sudo ./sof_insert.sh"
>>>>>
>>>>> After several rounds of removing and inserting kernel modules,
>>>>> system will complain like below:
>>>>> "BUG: unable to handle kernel paging request at 000000292a282031"
>>>>
>>>> Did you try some kernel debug options?  It might show what went wrong.
>>>
>>> No, I haven't. I'm not sure which options I can use for this case.
>>> Could you please give me some suggestions?
>>
>> You can enable CONFIG_DEBUG_DEVRES and adjust the devres.log option for
>> showing each devres allocation and removal.  And I'd try
>> CONFIG_DEBUG_SLAB and CONFIG_DEBUG_KMEMLEAK or whatever
>> interesting in CONFIG_DEBUG section, too.
> 
> Thanks for your suggestion. After more than 1 week debug, I think maybe
> I have root caused this issue from the devres.log message.
> 
> Below is my finding.
> 1. When initialing the codecs, snd_hdac_ext_bus_device_init() will be called,
> and it will set hdev->dev.release = default_release.
> However, for analog codec (not hdac_hdmi codec), hdac_hda_codec_probe()
> will be called later. And it will call snd_hda_codec_device_new(), which will
> reset codec->code.dev.release = snd_hda_codec_dev_release;
> This means hdac_hdmi's hdev dev release is default_release() defined in
> hdac_ext_bus.c, and other hda codec's hdev dev release is
> snd_hda_codec_dev_release().
> 
> Both default_release() and snd_hda_codec_dev_release() will call kfree()
> to free the hdac_device (or its container) in the current code.
> 
> 2. When we run rmmod sof_pci_dev, it will free the sdev. If we use
> Struct hdac_device *hdev = devm_kzalloc(sdev->dev...). This means
> hdev will also be freed automatically.
> 
> In the removal, snd_hdac_ext_bus_device_remove() will be called
> to remove the hdev (in this function it is struct hdac_device *codec.
> The name is not aligned in different places).
> However for hdac_hdmi, the hdev->dev is used by other code.

what other code? Can you elaborate on why the release is delayed?

> So calling device.release() (the function default_release()) will
> be postponed. After after sdev is freed, the device.release() will
> be called. But for devm_xxx, hdev will also be freed when sdev is
> freed. This means hdev.dev after sdev is freed is invalid now as
> hdev has already freed. It will access invalid memory. This will cause the bug.

This is very hard to follow. 4 lines above you wrote the release is 
postponed but the way you describe is looks completely sequential.

> 
> So I think we should not use devm_xxx, and let's free the hdev manually.
> 
> At the end of this topic, I still found 2 suspicious code in the current code.
> 1. in sound/soc/intel/skylate/skl.c
> it calls hdev = devm_kazlloc() or hda_codec = devm_kzalloc().
> As we will call kfree() in the current code, should we replace it with
> kzalloc()? Maybe we need cavs drivers owner's help on it.

maybe you should send a diff suggestion to help everyone understand the 
changes you are referring to?

> 
> 2. in snd_hdac_ext_bus_device_remove()
> It will call snd_hdac_device_unregister() to unregister the hdac_devices
> and put_device(&codec->dev) to release the dev.
> For analog codec, snd_hdac_device_unregister()  will free the codec->dev's
> kobject. And snd_hda_codec_dev_release() will be called to free the
> hdac_device.
> So it is invalid to call put_device(&codec->dev). If you print
> refcound_read(&(codec->dev.kobj.kref.refcount)) for analog codec before
> put_device(), you will find the refcount has already been 0.

Isn't it a different problem though? Does this cause a freeze or is this 
just a bad refcount?

  reply	other threads:[~2019-06-03 13:45 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-22 16:21 [PATCH v2 00/12] ASoC: SOF: stability fixes Pierre-Louis Bossart
2019-05-22 16:21 ` [PATCH v2 01/12] ASoC: SOF: core: remove DSP after unregistering machine driver Pierre-Louis Bossart
2019-05-22 16:21 ` [PATCH v2 02/12] ASoC: SOF: core: remove snd_soc_unregister_component in case of error Pierre-Louis Bossart
2019-05-22 16:21 ` [PATCH v2 03/12] ASoC: SOF: core: fix error handling with the probe workqueue Pierre-Louis Bossart
2019-05-22 16:21 ` [PATCH v2 04/12] ASoC: SOF: pcm: remove runtime PM calls during pcm open/close Pierre-Louis Bossart
2019-05-22 16:21 ` [PATCH v2 05/12] ASoC: SOF: pcm: clear hw_params_upon_resume flag correctly Pierre-Louis Bossart
2019-05-22 16:21 ` [PATCH v2 06/12] ASoC: SOF: pcm: remove warning - initialize workqueue on open Pierre-Louis Bossart
2019-05-22 16:21 ` [PATCH v2 07/12] ASoC: SOF: control: correct the copy size for bytes kcontrol put Pierre-Louis Bossart
2019-05-22 16:21 ` [PATCH v2 08/12] ASoC: SOF: ipc: fix a race, leading to IPC timeouts Pierre-Louis Bossart
2019-05-23  6:34   ` [PATCH v3 " Guennadi Liakhovetski
2019-06-05 11:34     ` Mark Brown
2019-05-22 16:21 ` [PATCH v2 09/12] ASoC: SOF: Intel: hda: fix the hda init chip Pierre-Louis Bossart
2019-05-22 16:21 ` [PATCH v2 10/12] ASoC: SOF: Intel: hda: use the defined ppcap functions Pierre-Louis Bossart
2019-06-06 21:27   ` Applied "ASoC: SOF: Intel: hda: use the defined ppcap functions" to the asoc tree Mark Brown
2019-05-22 16:21 ` [PATCH v2 11/12] ALSA: hdac: fix memory release for SST and SOF drivers Pierre-Louis Bossart
2019-05-22 16:21 ` [PATCH v2 12/12] ASoC: SOF: Intel: hda-codec: fix memory allocation Pierre-Louis Bossart
2019-05-23  4:10   ` Yang, Libin
2019-05-23  8:03   ` Yang, Libin
2019-05-23  8:15     ` Takashi Iwai
2019-05-23  8:21       ` Yang, Libin
2019-05-23  8:27         ` Takashi Iwai
2019-05-23  8:34           ` Yang, Libin
2019-06-03  9:10           ` Yang, Libin
2019-06-03 13:45             ` Pierre-Louis Bossart [this message]
2019-06-04  1:38               ` Yang, Libin
2019-06-04  1:21             ` Yang, Libin
2019-06-17  7:30             ` Yang, Libin
2019-05-23  8:24       ` Yang, Libin
2019-05-23 11:35         ` Pierre-Louis Bossart
2019-05-24  1:10           ` Yang, Libin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=475384fa-718f-14f4-7f5f-8257240163f6@linux.intel.com \
    --to=pierre-louis.bossart@linux.intel.com \
    --cc=alsa-devel@alsa-project.org \
    --cc=broonie@kernel.org \
    --cc=libin.yang@intel.com \
    --cc=tiwai@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox