pmdomain: core: Wait for device link removals before dropping genpd->dev
Subsystems (3)
PATCH 1/1 pmdomain: core: Wait for device link removals before dropping genpd->dev
From: Jan Sebastian Götte <[email protected]>
genpd->dev is embedded in struct generic_pm_domain and its release function is empty, so providers free the containing genpd with a plain kfree() once of_genpd_remove_last() returns, ignoring its refcount.
Since genpd->dev is registered on the genpd provider bus, fw_devlink creates device links to it, and those are torn down asynchronously. Nothing made genpd_remove() wait for those teardowns, so the provider could free the memory backing genpd->dev while the queued workers still used it.
This is reachable at boot on qrb2210, where the firmware rejects PC mode and psci_cpuidle_domain_probe() removes all the CPU PM domains before returning -EPROBE_DEFER. The bug is asymptomatic on defconfig, but shows up when enabling KASAN or INIT_ON_FREE_DEFAULT_ON. In some builds, it causes the kernel to crash a few hundred ms into the boot.
Call device_link_wait_removal() before dropping the final reference. All link removal work is queued from device_del(), via device_links_driver_cleanup() and device_links_purge(), which precedes genpd_free_data(), and flush_workqueue() waits for it to complete.
Note: This patch was LLM-assisted. I reproduced the issue and tested this patch on hardware, and I did my best to verify it by hand. However, I'm far from an expert in pmdomain, so YMMV.
--- drivers/pmdomain/core.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/pmdomain/core.c +3 -0
@@ -2348,6 +2348,9 @@ static int genpd_alloc_data(struct generic_pm_domain *genpd)static void genpd_free_data (struct generic_pm_domain *genpd) { +/* Pending device link removals still reference genpd->dev. */ +device_link_wait_removal (); +put_device (&genpd->dev);if (genpd->device_id != -ENXIO )ida_free (&genpd_ida, genpd->device_id);
Signature · 1 line
2.53.0
Quoted 18 lines · jaseg
From: Jan Sebastian Götte <[email protected]>
genpd->devis embedded in structgeneric_pm_domainand its release function is empty, so providers free the containing genpd with a plainkfree()onceof_genpd_remove_last()returns, ignoring its refcount.Since
genpd->devis registered on the genpd provider bus,fw_devlinkcreates device links to it, and those are torn down asynchronously. Nothing madegenpd_remove()wait for those teardowns, so the provider could free the memory backinggenpd->devwhile the queued workers still used it.This is reachable at boot on qrb2210, where the firmware rejects PC mode and
psci_cpuidle_domain_probe()removes all the CPU PM domains before returning -EPROBE_DEFER. The bug is asymptomatic on defconfig, but shows up when enabling KASAN orINIT_ON_FREE_DEFAULT_ON. In some builds, it causes the kernel to crash a few hundred ms into the boot.
Quoted 8 lines
Call
device_link_wait_removal()before dropping the final reference. All link removal work is queued fromdevice_del(), viadevice_links_driver_cleanup()anddevice_links_purge(), which precedesgenpd_free_data(), andflush_workqueue()waits for it to complete.Note: This patch was LLM-assisted. I reproduced the issue and tested this patch on hardware, and I did my best to verify it by hand. However, I'm far from an expert in pmdomain, so YMMV.
Thanks for the submission!
I'm not an expert in pmdomain either, but it seems to make sense
For future contributions, it's best to keep things that you wouldn't necessarily want to preserve in the git log under the --- line (so that it still shows up in the patch but it's removed during git am)
Also, your sender identity doesn't match the patch author (i.e. your full name shows up in the From: header of the patch but not in the email), but that's just cosmetic
Konrad
Quoted 23 lines · jaseg
From: Jan Sebastian Götte <[email protected]>
genpd->devis embedded in structgeneric_pm_domainand its release function is empty, so providers free the containing genpd with a plainkfree()onceof_genpd_remove_last()returns, ignoring its refcount.Since
genpd->devis registered on the genpd provider bus,fw_devlinkcreates device links to it, and those are torn down asynchronously. Nothing madegenpd_remove()wait for those teardowns, so the provider could free the memory backinggenpd->devwhile the queued workers still used it.This is reachable at boot on qrb2210, where the firmware rejects PC mode and
psci_cpuidle_domain_probe()removes all the CPU PM domains before returning -EPROBE_DEFER. The bug is asymptomatic on defconfig, but shows up when enabling KASAN orINIT_ON_FREE_DEFAULT_ON. In some builds, it causes the kernel to crash a few hundred ms into the boot.Call
device_link_wait_removal()before dropping the final reference. All link removal work is queued fromdevice_del(), viadevice_links_driver_cleanup()anddevice_links_purge(), which precedesgenpd_free_data(), andflush_workqueue()waits for it to complete.
Quoted 8 lines
Note: This patch was LLM-assisted. I reproduced the issue and tested this patch on hardware, and I did my best to verify it by hand. However, I'm far from an expert in pmdomain, so YMMV.
Assisted-by Claude:claude-5-opusFixes 18a3a510ecfd ("pmdomain: core: Add the genpd->dev to the genpd provider bus")Signed-off-by Jan Sebastian Götte <[email protected]>
Konrad beat me to it. But if you drop the last paragraph from commit message, I think the rest looks OK, so:
PATCH v2 1/1 pmdomain: core: Wait for device link removals before dropping genpd->dev
genpd->dev is embedded in struct generic_pm_domain and its release function is empty, so providers free the containing genpd with a plain kfree() once of_genpd_remove_last() returns, ignoring its refcount.
Since genpd->dev is registered on the genpd provider bus, fw_devlink creates device links to it, and those are torn down asynchronously. Nothing made genpd_remove() wait for those teardowns, so the provider could free the memory backing genpd->dev while the queued workers still used it.
This is reachable at boot on qrb2210, where the firmware rejects PC mode and psci_cpuidle_domain_probe() removes all the CPU PM domains before returning -EPROBE_DEFER. The bug is asymptomatic on defconfig, but shows up when enabling KASAN or INIT_ON_FREE_DEFAULT_ON. In some builds, it causes the kernel to crash a few hundred ms into the boot.
Call device_link_wait_removal() before dropping the final reference. All link removal work is queued from device_del(), via device_links_driver_cleanup() and device_links_purge(), which precedes genpd_free_data(), and flush_workqueue() waits for it to complete.
---
v2: remove this note from the commit text.
Note: This patch was LLM-assisted. I reproduced the issue and tested this patch on hardware, and I did my best to verify it by hand. However, I'm far from an expert in pmdomain, so YMMV.
drivers/pmdomain/core.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/pmdomain/core.c +3 -0
@@ -2348,6 +2348,9 @@ static int genpd_alloc_data(struct generic_pm_domain *genpd)static void genpd_free_data (struct generic_pm_domain *genpd) { +/* Pending device link removals still reference genpd->dev. */ +device_link_wait_removal (); +put_device (&genpd->dev);if (genpd->device_id != -ENXIO )ida_free (&genpd_ida, genpd->device_id);
Signature · 1 line
2.53.0
+ Saravana, Rafael, Danilo
Quoted 6 lines · jaseg
From: Jan Sebastian Götte <[email protected]>
genpd->devis embedded in structgeneric_pm_domainand its release function is empty, so providers free the containing genpd with a plainkfree()onceof_genpd_remove_last()returns, ignoring its refcount.
Yes, there is certainly room for improvements in regards to reference counting and freeing data in genpd. It's moving slowly in the right direction though.
Anyway, the release function is actually set to genpd_provider_release(), which ideally should free the data in the long run, but we haven't fully reached that point yet.
That said, I guess you actually are referring to the ->remove() callback for the genpd_provider_drv, right?
Quoted 35 lines
Since
genpd->devis registered on the genpd provider bus,fw_devlinkcreates device links to it, and those are torn down asynchronously. Nothing madegenpd_remove()wait for those teardowns, so the provider could free the memory backinggenpd->devwhile the queued workers still used it.This is reachable at boot on qrb2210, where the firmware rejects PC mode and
psci_cpuidle_domain_probe()removes all the CPU PM domains before returning -EPROBE_DEFER. The bug is asymptomatic on defconfig, but shows up when enabling KASAN orINIT_ON_FREE_DEFAULT_ON. In some builds, it causes the kernel to crash a few hundred ms into the boot.Call
device_link_wait_removal()before dropping the final reference. All link removal work is queued fromdevice_del(), viadevice_links_driver_cleanup()anddevice_links_purge(), which precedesgenpd_free_data(), andflush_workqueue()waits for it to complete.Note: This patch was LLM-assisted. I reproduced the issue and tested this patch on hardware, and I did my best to verify it by hand. However, I'm far from an expert in pmdomain, so YMMV.
Assisted-by Claude:claude-5-opusFixes 18a3a510ecfd ("pmdomain: core: Add the genpd->dev to the genpd provider bus")Signed-off-by Jan Sebastian Götte <[email protected]>--- drivers/pmdomain/core.c | 3 +++ 1 file changed, 3 insertions(+)drivers/pmdomain/core.c +0
-0
drivers/pmdomain/core.c static int genpd_alloc_data(struct generic_pm_domain *genpd) · line 2353
Quoted 6 lines
drivers/pmdomain/core.c +3
-0@@ -2348,6 +2348,9 @@ static int genpd_alloc_data(struct generic_pm_domain *genpd)static void genpd_free_data (struct generic_pm_domain *genpd) { +/* Pending device link removals still reference genpd->dev. */ +device_link_wait_removal (); +
Even if this fixes the issue, it looks wrong to me that drivers should have to deal with this themselves at device removal.
In other words, I was expecting the driver core to deal with this for everyone, but that seems not to be the case. Hmm.
Note that, the device removal should typically happen when the genpd provider calls of_genpd_del_provider() and it calls device_del().
Quoted 6 lines
drivers/pmdomain/core.c +0
-1put_device (&genpd->dev);if (genpd->device_id != -ENXIO )ida_free (&genpd_ida, genpd->device_id); --2.53.0
I will have a closer look at the driver core for this. In the meantime, maybe Saravana, Rafael or Danilo have some ideas already?
Kind regards
Uffe
Quoted 68 lines · Ulf Hansson
Quoted 2 lines
+ Saravana, Rafael, Danilo
Quoted 6 lines · jaseg
From: Jan Sebastian Götte <[email protected]>
genpd->devis embedded in structgeneric_pm_domainand its release function is empty, so providers free the containing genpd with a plainkfree()onceof_genpd_remove_last()returns, ignoring its refcount.Yes, there is certainly room for improvements in regards to reference counting and freeing data in genpd. It's moving slowly in the right direction though.
Anyway, the release function is actually set to
genpd_provider_release(), which ideally should free the data in the long run, but we haven't fully reached that point yet.That said, I guess you actually are referring to the ->remove() callback for the
genpd_provider_drv, right?Quoted 35 lines
Since
genpd->devis registered on the genpd provider bus,fw_devlinkcreates device links to it, and those are torn down asynchronously. Nothing madegenpd_remove()wait for those teardowns, so the provider could free the memory backinggenpd->devwhile the queued workers still used it.This is reachable at boot on qrb2210, where the firmware rejects PC mode and
psci_cpuidle_domain_probe()removes all the CPU PM domains before returning -EPROBE_DEFER. The bug is asymptomatic on defconfig, but shows up when enabling KASAN orINIT_ON_FREE_DEFAULT_ON. In some builds, it causes the kernel to crash a few hundred ms into the boot.Call
device_link_wait_removal()before dropping the final reference. All link removal work is queued fromdevice_del(), viadevice_links_driver_cleanup()anddevice_links_purge(), which precedesgenpd_free_data(), andflush_workqueue()waits for it to complete.Note: This patch was LLM-assisted. I reproduced the issue and tested this patch on hardware, and I did my best to verify it by hand. However, I'm far from an expert in pmdomain, so YMMV.
Assisted-by Claude:claude-5-opusFixes 18a3a510ecfd ("pmdomain: core: Add the genpd->dev to the genpd provider bus")Signed-off-by Jan Sebastian Götte <[email protected]>--- drivers/pmdomain/core.c | 3 +++ 1 file changed, 3 insertions(+)drivers/pmdomain/core.c +0
-0drivers/pmdomain/core.c static int genpd_alloc_data(struct generic_pm_domain *genpd) · line 2353
Quoted 6 lines
drivers/pmdomain/core.c +3
-0@@ -2348,6 +2348,9 @@ static int genpd_alloc_data(struct generic_pm_domain *genpd)static void genpd_free_data (struct generic_pm_domain *genpd) { +/* Pending device link removals still reference genpd->dev. */ +device_link_wait_removal (); +Even if this fixes the issue, it looks wrong to me that drivers should have to deal with this themselves at device removal.
Quoted 2 lines
In other words, I was expecting the driver core to deal with this for everyone, but that seems not to be the case. Hmm.
I agree.
I'm not even sure if device_link_wait_removal() should have been
added. The point of refcounting is that we don't have to worry about who releases in what order and the last one to release will have a resource freed. Needing both ref counting and a say to sync feels wrong. Let me take a closer look.
-Saravana
Quoted 16 lines
Note that, the device removal should typically happen when the genpd provider calls
of_genpd_del_provider()and it callsdevice_del().Quoted 6 lines
drivers/pmdomain/core.c +0
-1put_device (&genpd->dev);if (genpd->device_id != -ENXIO )ida_free (&genpd_ida, genpd->device_id); --2.53.0
I will have a closer look at the driver core for this. In the meantime, maybe Saravana, Rafael or Danilo have some ideas already?
Kind regards
Uffe
Quoted 75 lines · Saravana Kannan
Quoted 68 lines · Ulf Hansson
Quoted 2 lines
+ Saravana, Rafael, Danilo
Quoted 6 lines · jaseg
From: Jan Sebastian Götte <[email protected]>
genpd->devis embedded in structgeneric_pm_domainand its release function is empty, so providers free the containing genpd with a plainkfree()onceof_genpd_remove_last()returns, ignoring its refcount.Yes, there is certainly room for improvements in regards to reference counting and freeing data in genpd. It's moving slowly in the right direction though.
Anyway, the release function is actually set to
genpd_provider_release(), which ideally should free the data in the long run, but we haven't fully reached that point yet.That said, I guess you actually are referring to the ->remove() callback for the
genpd_provider_drv, right?Quoted 35 lines
Since
genpd->devis registered on the genpd provider bus,fw_devlinkcreates device links to it, and those are torn down asynchronously. Nothing madegenpd_remove()wait for those teardowns, so the provider could free the memory backinggenpd->devwhile the queued workers still used it.This is reachable at boot on qrb2210, where the firmware rejects PC mode and
psci_cpuidle_domain_probe()removes all the CPU PM domains before returning -EPROBE_DEFER. The bug is asymptomatic on defconfig, but shows up when enabling KASAN orINIT_ON_FREE_DEFAULT_ON. In some builds, it causes the kernel to crash a few hundred ms into the boot.Call
device_link_wait_removal()before dropping the final reference. All link removal work is queued fromdevice_del(), viadevice_links_driver_cleanup()anddevice_links_purge(), which precedesgenpd_free_data(), andflush_workqueue()waits for it to complete.Note: This patch was LLM-assisted. I reproduced the issue and tested this patch on hardware, and I did my best to verify it by hand. However, I'm far from an expert in pmdomain, so YMMV.
Assisted-by Claude:claude-5-opusFixes 18a3a510ecfd ("pmdomain: core: Add the genpd->dev to the genpd provider bus")Signed-off-by Jan Sebastian Götte <[email protected]>--- drivers/pmdomain/core.c | 3 +++ 1 file changed, 3 insertions(+)drivers/pmdomain/core.c +0
-0drivers/pmdomain/core.c static int genpd_alloc_data(struct generic_pm_domain *genpd) · line 2353
Quoted 6 lines
drivers/pmdomain/core.c +3
-0@@ -2348,6 +2348,9 @@ static int genpd_alloc_data(struct generic_pm_domain *genpd)static void genpd_free_data (struct generic_pm_domain *genpd) { +/* Pending device link removals still reference genpd->dev. */ +device_link_wait_removal (); +Even if this fixes the issue, it looks wrong to me that drivers should have to deal with this themselves at device removal.
Quoted 2 lines
In other words, I was expecting the driver core to deal with this for everyone, but that seems not to be the case. Hmm.
I agree.
Quoted 21 lines
I'm not even sure if
device_link_wait_removal()should have been
added. The point of refcounting is that we don't have to worry about who releases in what order and the last one to release will have a resource freed. Needing both ref counting and a say to sync feels wrong. Let me take a closer look.-Saravana
Quoted 13 lines
Note that, the device removal should typically happen when the genpd provider calls
of_genpd_del_provider()and it callsdevice_del().Quoted 6 lines
drivers/pmdomain/core.c +0
-1put_device (&genpd->dev);if (genpd->device_id != -ENXIO )ida_free (&genpd_ida, genpd->device_id); --2.53.0
I will have a closer look at the driver core for this. In the meantime, maybe Saravana, Rafael or Danilo have some ideas already?
I took a closer look.
I think the problem is that the genpd framework's internal structure (struct generic_pm_domain) is allocated by the provider driver and each one of them allocates it in a different way. For example:
* Part of a container struct's allocation. Eg: struct scpsys_domain
* Using a kzalloc(). Eg: dt_idle_pd_alloc()
And the genpd framework has the struct device as a member of struct generic_pm_domain and device_add()s it. But the problem is that the framework now has no control on when the struct device is freed AND the provider driver has no way of knowing when it's safe to release this specific genpd->dev and any containers of it.
I think the right solution is for struct generic_pm_domain to change the "dev" field to a pointer and then allocate the device inside genpd_alloc_data() and free it in the device's release function (which is currently a no-op). There are a few places genpd->dev is used outside of the framework, but those can be fixed up easily.
$ git grep -c 'genpd->dev' -- drivers/ drivers/pmdomain/arm/scmi_perf_domain.c:1 drivers/pmdomain/core.c:59 drivers/pmdomain/governor.c:2 drivers/soc/tegra/pmc.c:3
$ git grep -c '&genpd->dev' -- drivers/ drivers/pmdomain/arm/scmi_perf_domain.c:1 drivers/pmdomain/core.c:40 drivers/pmdomain/governor.c:2 drivers/soc/tegra/pmc.c:3
Hope this helps.
-Saravana
+ Loic
Quoted 22 lines · Jan Sebastian Götte
genpd->devis embedded in structgeneric_pm_domainand its release function is empty, so providers free the containing genpd with a plainkfree()onceof_genpd_remove_last()returns, ignoring its refcount.Since
genpd->devis registered on the genpd provider bus,fw_devlinkcreates device links to it, and those are torn down asynchronously. Nothing madegenpd_remove()wait for those teardowns, so the provider could free the memory backinggenpd->devwhile the queued workers still used it.This is reachable at boot on qrb2210, where the firmware rejects PC mode and
psci_cpuidle_domain_probe()removes all the CPU PM domains before returning -EPROBE_DEFER. The bug is asymptomatic on defconfig, but shows up when enabling KASAN orINIT_ON_FREE_DEFAULT_ON. In some builds, it causes the kernel to crash a few hundred ms into the boot.Call
device_link_wait_removal()before dropping the final reference. All link removal work is queued fromdevice_del(), viadevice_links_driver_cleanup()anddevice_links_purge(), which precedesgenpd_free_data(), andflush_workqueue()waits for it to complete.
Quoted 4 lines
Assisted-by Claude:claude-5-opusFixes 18a3a510ecfd ("pmdomain: core: Add the genpd->dev to the genpd provider bus")Signed-off-by Jan Sebastian Götte <[email protected]>
I decided to apply this patch as is, for fixes! Apologize for the delay!
Let's try to come back to this and see if there is a better long term solution, along the lines of what Saravana suggested.
@Loic, if you have time, can you please have a go and see if $subject patch solves the problem you are seeing as well [1]?
Kind regards
Uffe
[1]
https://lore.kernel.org/all/[email protected]/
Quoted 27 lines
---
v2: remove this note from the commit text.Note: This patch was LLM-assisted. I reproduced the issue and tested this patch on hardware, and I did my best to verify it by hand. However, I'm far from an expert in pmdomain, so YMMV.
drivers/pmdomain/core.c | 3 +++ 1 file changed, 3 insertions(+)drivers/pmdomain/core.c +3
-1@@ -2348,6 +2348,9 @@ static int genpd_alloc_data(struct generic_pm_domain *genpd)static void genpd_free_data (struct generic_pm_domain *genpd) { +/* Pending device link removals still reference genpd->dev. */ +device_link_wait_removal (); +put_device (&genpd->dev);if (genpd->device_id != -ENXIO )ida_free (&genpd_ida, genpd->device_id); --2.53.0
Quoted 9 lines · Ulf Hansson
Quoted 3 lines · Jan Sebastian Götte
[...]>> Fixes:
18a3a510ecfd("pmdomain: core: Add thegenpd->devto the genpd provider bus")Signed-off-by Jan Sebastian Götte <[email protected]>I decided to apply this patch as is, for fixes! Apologize for the delay!
Let's try to come back to this and see if there is a better long term solution, along the lines of what Saravana suggested.
Please feel free to CC me when you find the time to write a more long term fix, I'm happy to test over here.