Skip to contentSkip to conversation

arm_pmu: acpi: fix reference leak on failed device registration

PATCH In review Guangshuo Li 5 participants 11 messages Apr 15 – Apr 20

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm_pmu: acpi: fix reference leak on failed device registration

When platform_device_register() fails in arm_acpi_register_pmu_device(), the embedded struct device in pdev has already been initialized by device_initialize(), but the failure path only unregisters the GSI and does not drop the device reference for the current platform device:

arm_acpi_register_pmu_device()

    -> platform_device_register(pdev)
       -> device_initialize(&pdev->dev)
       -> setup_pdev_dma_masks(pdev)
       -> platform_device_add(pdev)

This leads to a reference leak when platform_device_register() fails. Fix this by calling platform_device_put() after unregistering the GSI.

The issue was identified by a static analysis tool I developed and confirmed by manual review.

Fixes 81e5ee4716098 ("arm_pmu: acpi: Refactor arm_spe_acpi_register_device()")
---
 drivers/perf/arm_pmu_acpi.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/perf/arm_pmu_acpi.c +3 -1
index e80f76d95e68..5ce382661e34 100644
@@ -119,8 +119,10 @@ arm_acpi_register_pmu_device(struct platform_device *pdev, u8 len,
 
 	pdev->resource[0].start = irq;
 	ret = platform_device_register(pdev);
-	if (ret)
+	if (ret) {
 		acpi_unregister_gsi(gsi);
+		platform_device_put(pdev);
+	}
 
 	return ret;
 }
Signature · 1 line
2.43.0
maintainer
#1 lore raw

Hi,

Thanks for the patch, but from a quick skim, I don't think this is the right fix.

Greg, I think we might want to rework the core API here; question for you at the end.

Quoted 5 lines · Guangshuo Li

When platform_device_register() fails in arm_acpi_register_pmu_device(), the embedded struct device in pdev has already been initialized by device_initialize(), but the failure path only unregisters the GSI and does not drop the device reference for the current platform device:

Quoted 7 lines

arm_acpi_register_pmu_device()

    -> platform_device_register(pdev)
       -> device_initialize(&pdev->dev)
       -> setup_pdev_dma_masks(pdev)
       -> platform_device_add(pdev)

This leads to a reference leak when platform_device_register() fails.

AFAICT you're saying that the reference was taken *within* platform_device_register(), and then platform_device_register() itself has failed. I think it's surprising that platform_device_register() doesn't clean that up itself in the case of an error.

There are *tonnes* of calls to platform_device_register() throughout the kernel that don't even bother to check the return value, and many that just pass the return onto a caller that can't possibly know to call platform_device_put().

Code in the same file as platform_device_register() expects it to clean up after itself, e.g.

| int platform_add_devices(struct platform_device **devs, int num) 
| {
|         int i, ret = 0; 
| 
|         for (i = 0; i < num; i++) {
|                 ret = platform_device_register(devs[i]);
|                 if (ret) {
|                         while (--i >= 0)
|                                 platform_device_unregister(devs[i]);
|                         break;
|                 }    
|         }    
| 
|         return ret; 
| }

That's been there since the initial git commit, and back then, platform_device_register() didn't mention that callers needed to perform any cleanup.

I see a comment was added to platform_device_register() in commit:

67e532a42cf4 ("driver core: platform: document registration-failure requirement")

... and that copied the commend added for device_register() in commit:

5739411acbaa ("Driver core: Clarify device cleanup.")

... but the potential brokenness is so widespread, and the behaviour is so surprising, that I'd argue the real but is that device_register() doesn't clean up in case of error. I don't think it's worth changing this single instance given the prevalance and churn fixing all of that would involve.

I think it would be far better to fix the core driver API such that when those functions return an error, they've already cleaned up for themselves.

Greg, am I missing some functional reason why we can't rework device_register() and friends to handle cleanup themselves? I appreciate that'll involve churn for some callers, but AFAICT the majority of callers don't have the required cleanup.

Mark.

Quoted 31 lines

Fix this by calling platform_device_put() after unregistering the GSI.

The issue was identified by a static analysis tool I developed and confirmed by manual review.

Fixes 81e5ee4716098 ("arm_pmu: acpi: Refactor arm_spe_acpi_register_device()")
---
 drivers/perf/arm_pmu_acpi.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/perf/arm_pmu_acpi.c +3 -1
index e80f76d95e68..5ce382661e34 100644
@@ -119,8 +119,10 @@ arm_acpi_register_pmu_device(struct platform_device *pdev, u8 len,
 
 	pdev->resource[0].start = irq;
 	ret = platform_device_register(pdev);
-	if (ret)
+	if (ret) {
 		acpi_unregister_gsi(gsi);
+		platform_device_put(pdev);
+	}
 
 	return ret;
 }
Signature · 1 line
2.43.0
#2 lore raw
Quoted 69 lines · Mark Rutland

Hi,

Thanks for the patch, but from a quick skim, I don't think this is the right fix.

Greg, I think we might want to rework the core API here; question for you at the end.

Quoted 5 lines · Guangshuo Li

When platform_device_register() fails in arm_acpi_register_pmu_device(), the embedded struct device in pdev has already been initialized by device_initialize(), but the failure path only unregisters the GSI and does not drop the device reference for the current platform device:

Quoted 7 lines

arm_acpi_register_pmu_device()

    -> platform_device_register(pdev)
       -> device_initialize(&pdev->dev)
       -> setup_pdev_dma_masks(pdev)
       -> platform_device_add(pdev)

This leads to a reference leak when platform_device_register() fails.

AFAICT you're saying that the reference was taken *within* platform_device_register(), and then platform_device_register() itself has failed. I think it's surprising that platform_device_register() doesn't clean that up itself in the case of an error.

There are *tonnes* of calls to platform_device_register() throughout the kernel that don't even bother to check the return value, and many that just pass the return onto a caller that can't possibly know to call platform_device_put().

Code in the same file as platform_device_register() expects it to clean up after itself, e.g.

| int platform_add_devices(struct platform_device **devs, int num) 
| {
|         int i, ret = 0; 
| 
|         for (i = 0; i < num; i++) {
|                 ret = platform_device_register(devs[i]);
|                 if (ret) {
|                         while (--i >= 0)
|                                 platform_device_unregister(devs[i]);
|                         break;
|                 }    
|         }    
| 
|         return ret; 
| }

That's been there since the initial git commit, and back then, platform_device_register() didn't mention that callers needed to perform any cleanup.

I see a comment was added to platform_device_register() in commit:

67e532a42cf4 ("driver core: platform: document registration-failure requirement")

... and that copied the commend added for device_register() in commit:

5739411acbaa ("Driver core: Clarify device cleanup.")

... but the potential brokenness is so widespread, and the behaviour is so surprising, that I'd argue the real but is that device_register() doesn't clean up in case of error. I don't think it's worth changing this single instance given the prevalance and churn fixing all of that would involve.

Quoted 8 lines

I think it would be far better to fix the core driver API such that when those functions return an error, they've already cleaned up for themselves.

Greg, am I missing some functional reason why we can't rework device_register() and friends to handle cleanup themselves? I appreciate that'll involve churn for some callers, but AFAICT the majority of callers don't have the required cleanup.

Yes, we should fix the platform core code here, this should not be required to do everywhere as obviously we all got it wrong.

Guangshuo, can you submit a patch to do that instead and ask for all of your other patches to not be applied as well?

thanks,

greg k-h

#3 lore raw

Hi Mark, Greg,

Thanks for the feedback.

Quoted 83 lines · Greg Kroah-Hartman
Quoted 69 lines · Mark Rutland

Hi,

Thanks for the patch, but from a quick skim, I don't think this is the right fix.

Greg, I think we might want to rework the core API here; question for you at the end.

Quoted 5 lines · Guangshuo Li

When platform_device_register() fails in arm_acpi_register_pmu_device(), the embedded struct device in pdev has already been initialized by device_initialize(), but the failure path only unregisters the GSI and does not drop the device reference for the current platform device:

Quoted 7 lines

arm_acpi_register_pmu_device()

    -> platform_device_register(pdev)
       -> device_initialize(&pdev->dev)
       -> setup_pdev_dma_masks(pdev)
       -> platform_device_add(pdev)

This leads to a reference leak when platform_device_register() fails.

AFAICT you're saying that the reference was taken *within* platform_device_register(), and then platform_device_register() itself has failed. I think it's surprising that platform_device_register() doesn't clean that up itself in the case of an error.

There are *tonnes* of calls to platform_device_register() throughout the kernel that don't even bother to check the return value, and many that just pass the return onto a caller that can't possibly know to call platform_device_put().

Code in the same file as platform_device_register() expects it to clean up after itself, e.g.

| int platform_add_devices(struct platform_device **devs, int num)
| {
|         int i, ret = 0;
|
|         for (i = 0; i < num; i++) {
|                 ret = platform_device_register(devs[i]);
|                 if (ret) {
|                         while (--i >= 0)
|                                 platform_device_unregister(devs[i]);
|                         break;
|                 }
|         }
|
|         return ret;
| }

That's been there since the initial git commit, and back then, platform_device_register() didn't mention that callers needed to perform any cleanup.

I see a comment was added to platform_device_register() in commit:

67e532a42cf4 ("driver core: platform: document registration-failure requirement")

... and that copied the commend added for device_register() in commit:

5739411acbaa ("Driver core: Clarify device cleanup.")

... but the potential brokenness is so widespread, and the behaviour is so surprising, that I'd argue the real but is that device_register() doesn't clean up in case of error. I don't think it's worth changing this single instance given the prevalance and churn fixing all of that would involve.

Quoted 8 lines

I think it would be far better to fix the core driver API such that when those functions return an error, they've already cleaned up for themselves.

Greg, am I missing some functional reason why we can't rework device_register() and friends to handle cleanup themselves? I appreciate that'll involve churn for some callers, but AFAICT the majority of callers don't have the required cleanup.

Yes, we should fix the platform core code here, this should not be required to do everywhere as obviously we all got it wrong.

Quoted 6 lines

Guangshuo, can you submit a patch to do that instead and ask for all of your other patches to not be applied as well?

thanks,

greg k-h

I agree that fixing this in the platform core makes more sense than handling it in individual callers.

I'll look into the core code and send a patch for that instead. I'll also ask for my other related patches not to be applied.

Thanks,
Guangshuo

#4 lore raw
In reply to Greg Kroah-Hartman
Quoted 53 lines · Greg Kroah-Hartman
Quoted 51 lines · Mark Rutland

AFAICT you're saying that the reference was taken *within* platform_device_register(), and then platform_device_register() itself has failed. I think it's surprising that platform_device_register() doesn't clean that up itself in the case of an error.

There are *tonnes* of calls to platform_device_register() throughout the kernel that don't even bother to check the return value, and many that just pass the return onto a caller that can't possibly know to call platform_device_put().

Code in the same file as platform_device_register() expects it to clean up after itself, e.g.

| int platform_add_devices(struct platform_device **devs, int num) 
| {
|         int i, ret = 0; 
| 
|         for (i = 0; i < num; i++) {
|                 ret = platform_device_register(devs[i]);
|                 if (ret) {
|                         while (--i >= 0)
|                                 platform_device_unregister(devs[i]);
|                         break;
|                 }    
|         }    
| 
|         return ret; 
| }

That's been there since the initial git commit, and back then, platform_device_register() didn't mention that callers needed to perform any cleanup.

I see a comment was added to platform_device_register() in commit:

67e532a42cf4 ("driver core: platform: document registration-failure requirement")

... and that copied the commend added for device_register() in commit:

5739411acbaa ("Driver core: Clarify device cleanup.")

... but the potential brokenness is so widespread, and the behaviour is so surprising, that I'd argue the real but is that device_register() doesn't clean up in case of error. I don't think it's worth changing this single instance given the prevalance and churn fixing all of that would involve.

I think it would be far better to fix the core driver API such that when those functions return an error, they've already cleaned up for themselves.

Quoted 7 lines
Quoted 4 lines

Greg, am I missing some functional reason why we can't rework device_register() and friends to handle cleanup themselves? I appreciate that'll involve churn for some callers, but AFAICT the majority of callers don't have the required cleanup.

Yes, we should fix the platform core code here, this should not be required to do everywhere as obviously we all got it wrong.

It's not just the platform code as this directly reflects the behaviour of device_register() as Mark pointed out.

It is indeed an unfortunate quirk of the driver model, but one can argue that having a registration function that frees its argument on errors would be even worse. And even more so when many (or most) users get this right.

So if we want to change this, I think we would need to deprecate device_register() in favour of explicit device_initialize() and device_add().

That said, most users of platform_device_register() appear to operate on static platform devices which don't even have a release function and would trigger a WARN() if we ever drop the reference (which is arguably worse than leaking a tiny bit of memory).

So leaving things as-is is also an option.

Johan

#5 lore raw

Hi Greg, Mark, Johan,

Thanks for the further comments.

Quoted 75 lines · Johan Hovold
Quoted 53 lines · Greg Kroah-Hartman
Quoted 51 lines · Mark Rutland

AFAICT you're saying that the reference was taken *within* platform_device_register(), and then platform_device_register() itself has failed. I think it's surprising that platform_device_register() doesn't clean that up itself in the case of an error.

There are *tonnes* of calls to platform_device_register() throughout the kernel that don't even bother to check the return value, and many that just pass the return onto a caller that can't possibly know to call platform_device_put().

Code in the same file as platform_device_register() expects it to clean up after itself, e.g.

| int platform_add_devices(struct platform_device **devs, int num)
| {
|         int i, ret = 0;
|
|         for (i = 0; i < num; i++) {
|                 ret = platform_device_register(devs[i]);
|                 if (ret) {
|                         while (--i >= 0)
|                                 platform_device_unregister(devs[i]);
|                         break;
|                 }
|         }
|
|         return ret;
| }

That's been there since the initial git commit, and back then, platform_device_register() didn't mention that callers needed to perform any cleanup.

I see a comment was added to platform_device_register() in commit:

67e532a42cf4 ("driver core: platform: document registration-failure requirement")

... and that copied the commend added for device_register() in commit:

5739411acbaa ("Driver core: Clarify device cleanup.")

... but the potential brokenness is so widespread, and the behaviour is so surprising, that I'd argue the real but is that device_register() doesn't clean up in case of error. I don't think it's worth changing this single instance given the prevalance and churn fixing all of that would involve.

I think it would be far better to fix the core driver API such that when those functions return an error, they've already cleaned up for themselves.

Quoted 7 lines
Quoted 4 lines

Greg, am I missing some functional reason why we can't rework device_register() and friends to handle cleanup themselves? I appreciate that'll involve churn for some callers, but AFAICT the majority of callers don't have the required cleanup.

Yes, we should fix the platform core code here, this should not be required to do everywhere as obviously we all got it wrong.

It's not just the platform code as this directly reflects the behaviour of device_register() as Mark pointed out.

It is indeed an unfortunate quirk of the driver model, but one can argue that having a registration function that frees its argument on errors would be even worse. And even more so when many (or most) users get this right.

So if we want to change this, I think we would need to deprecate device_register() in favour of explicit device_initialize() and device_add().

Quoted 8 lines

That said, most users of platform_device_register() appear to operate on static platform devices which don't even have a release function and would trigger a WARN() if we ever drop the reference (which is arguably worse than leaking a tiny bit of memory).

So leaving things as-is is also an option.

Johan

I did some more investigation, and it looks like directly changing the semantics of the existing API would break code that is already correct today.

In particular, there seem to be at least two different kinds of callers:

Callers that already handle the failure path explicitly after platform_device_register() fails. For these users, changing platform_device_register() itself to drop the reference internally would lead to double put / use-after-free issues.

Callers that operate on static struct platform_device objects. Many of these do not have a release callback, so blindly dropping the reference on failure would trigger a WARN.

Because of this, changing platform_device_register() itself to always clean up on failure does not look safe.

One possible direction may be to leave platform_device_register() unchanged, and instead add new helper APIs for the different cases.

For case (1), I was thinking of a helper like:

platform_device_register_and_put()

The implementation would simply call platform_device_register(), and if that fails, call platform_device_put(). Callers converted to this helper would then no longer perform their own put on the failure path.

For case (2), I was thinking of a helper like:

platform_device_register_static()

The implementation would first install a no-op release callback when pdev->dev.release is not set, and then call platform_device_register_and_put(). This would make the failure path well-defined for static platform_device users, avoiding the reference leak without triggering a WARN.

If this direction sounds reasonable, I would be happy to work on it and send a patch, and I would also be very willing to help with the related API conversion work for existing callers.

Thanks,
Guangshuo

maintainer
#6 lore raw
Quoted 29 lines · Guangshuo Li
Quoted 28 lines · Johan Hovold
Quoted 9 lines · Greg Kroah-Hartman
Quoted 4 lines · Mark Rutland

Greg, am I missing some functional reason why we can't rework device_register() and friends to handle cleanup themselves? I appreciate that'll involve churn for some callers, but AFAICT the majority of callers don't have the required cleanup.

Yes, we should fix the platform core code here, this should not be required to do everywhere as obviously we all got it wrong.

It's not just the platform code as this directly reflects the behaviour of device_register() as Mark pointed out.

It is indeed an unfortunate quirk of the driver model, but one can argue that having a registration function that frees its argument on errors would be even worse. And even more so when many (or most) users get this right.

So if we want to change this, I think we would need to deprecate device_register() in favour of explicit device_initialize() and device_add().

That said, most users of platform_device_register() appear to operate on static platform devices which don't even have a release function and would trigger a WARN() if we ever drop the reference (which is arguably worse than leaking a tiny bit of memory).

Quoted 7 lines
Quoted 3 lines

So leaving things as-is is also an option.

Johan

I did some more investigation, and it looks like directly changing the semantics of the existing API would break code that is already correct today.

Evidently this wasn't entirely clear, but when I suggested changing the semantic, I had implicitly meant that we'd also go and fix up callers to handle the new semantic.

I agree that whatever we do, we'll have to change some callers, given that existing callers have inconsistent expectations.

Quoted 6 lines

In particular, there seem to be at least two different kinds of callers:

Callers that already handle the failure path explicitly after platform_device_register() fails. For these users, changing platform_device_register() itself to drop the reference internally would lead to double put / use-after-free issues.

Yes; for those we could drop the explicit cleanup.

As an alternative (as Johan mentioned above), if we deprecated *_register() in favour of separate *_initialize() and *_add() calls, then we could require that callers had explicit cleanup. As that cleanup would more obviously pair with the *_initialize() step, it would be less surprising than cleaning up for a function that returned an error.

As I mentioned in my other reply to Johan, that might also give options for how to handle the static platform_device case, e.g. with an *_uninitialize() function.

Quoted 6 lines

Callers that operate on static struct platform_device objects. Many of these do not have a release callback, so blindly dropping the reference on failure would trigger a WARN.

Because of this, changing platform_device_register() itself to always clean up on failure does not look safe.

I agree that we probably can't have _*register() do all the necessary cleanup, since callers want different things.

As per Johan's suggestion, and my reply, I suspect the best option for a consistent API would be to deprecate *_register() in favour of separate *_initialize() and *_add() calls.

Quoted 10 lines

One possible direction may be to leave platform_device_register() unchanged, and instead add new helper APIs for the different cases.

For case (1), I was thinking of a helper like:

platform_device_register_and_put()

The implementation would simply call platform_device_register(), and if that fails, call platform_device_put(). Callers converted to this helper would then no longer perform their own put on the failure path.

I think that's going to be a source of confusion, because there's no clear way to name that function. A '_and_put' suffix makes it sound like it does a put unconditionally, rather than when the *_add() step fails.

Otherwise, I agree that would work for those callers.

Quoted 9 lines

For case (2), I was thinking of a helper like:

platform_device_register_static()

The implementation would first install a no-op release callback when pdev->dev.release is not set, and then call platform_device_register_and_put(). This would make the failure path well-defined for static platform_device users, avoiding the reference leak without triggering a WARN.

Something like that might work.

As above, I think my preference would be to have separate init/add/uninit calls, as that way each of the functions succeeds or fails atomically, which is more aligned with general conventions.

Quoted 3 lines

If this direction sounds reasonable, I would be happy to work on it and send a patch, and I would also be very willing to help with the related API conversion work for existing callers.

Fantastic!

I think we should hear what Greg thinks of the options before we start on that, but it's great to hear that you're willing!

Mark.

maintainer
#7 lore raw
In reply to Johan Hovold
Quoted 62 lines · Johan Hovold
Quoted 53 lines · Greg Kroah-Hartman
Quoted 51 lines · Mark Rutland

AFAICT you're saying that the reference was taken *within* platform_device_register(), and then platform_device_register() itself has failed. I think it's surprising that platform_device_register() doesn't clean that up itself in the case of an error.

There are *tonnes* of calls to platform_device_register() throughout the kernel that don't even bother to check the return value, and many that just pass the return onto a caller that can't possibly know to call platform_device_put().

Code in the same file as platform_device_register() expects it to clean up after itself, e.g.

| int platform_add_devices(struct platform_device **devs, int num) 
| {
|         int i, ret = 0; 
| 
|         for (i = 0; i < num; i++) {
|                 ret = platform_device_register(devs[i]);
|                 if (ret) {
|                         while (--i >= 0)
|                                 platform_device_unregister(devs[i]);
|                         break;
|                 }    
|         }    
| 
|         return ret; 
| }

That's been there since the initial git commit, and back then, platform_device_register() didn't mention that callers needed to perform any cleanup.

I see a comment was added to platform_device_register() in commit:

67e532a42cf4 ("driver core: platform: document registration-failure requirement")

... and that copied the commend added for device_register() in commit:

5739411acbaa ("Driver core: Clarify device cleanup.")

... but the potential brokenness is so widespread, and the behaviour is so surprising, that I'd argue the real but is that device_register() doesn't clean up in case of error. I don't think it's worth changing this single instance given the prevalance and churn fixing all of that would involve.

I think it would be far better to fix the core driver API such that when those functions return an error, they've already cleaned up for themselves.

Quoted 7 lines
Quoted 4 lines

Greg, am I missing some functional reason why we can't rework device_register() and friends to handle cleanup themselves? I appreciate that'll involve churn for some callers, but AFAICT the majority of callers don't have the required cleanup.

Yes, we should fix the platform core code here, this should not be required to do everywhere as obviously we all got it wrong.

Quoted 7 lines

It's not just the platform code as this directly reflects the behaviour of device_register() as Mark pointed out.

It is indeed an unfortunate quirk of the driver model, but one can argue that having a registration function that frees its argument on errors would be even worse. And even more so when many (or most) users get this right.

Ah, sorry; I had missed that the _put() step would actually free the object (and as you explain below, how that won't work for many callers).

Quoted 3 lines

So if we want to change this, I think we would need to deprecate device_register() in favour of explicit device_initialize() and device_add().

Is is possible to have {platfom_,}device_uninitialize() functions that does everything except the ->release() call? If we had that, then we'd be able to have a flow along the lines of:

	int some_init_function(void)
	{
		int err;
	
		platform_device_init(&static_pdev);
	
		err = platform_device_add(&static_pdev))
		if (err)
			goto out_uninit;
	
		return 0;
	
	out_uninit:
		platform_device_uninit(&static_pdev);
		return err;
	}

... which I think would align with what people generally expect to have to do.

Those would have to check that only a single reference was held (from the corresponding _initialize()), and could WARN/fail if more were held.

Quoted 6 lines

That said, most users of platform_device_register() appear to operate on static platform devices which don't even have a release function and would trigger a WARN() if we ever drop the reference (which is arguably worse than leaking a tiny bit of memory).

So leaving things as-is is also an option.

I suspect that might be the best option for now.

Mark.

#8 lore raw
Quoted 38 lines · Mark Rutland
Quoted 7 lines · Johan Hovold

It's not just the platform code as this directly reflects the behaviour of device_register() as Mark pointed out.

It is indeed an unfortunate quirk of the driver model, but one can argue that having a registration function that frees its argument on errors would be even worse. And even more so when many (or most) users get this right.

Ah, sorry; I had missed that the _put() step would actually free the object (and as you explain below, how that won't work for many callers).

Quoted 3 lines

So if we want to change this, I think we would need to deprecate device_register() in favour of explicit device_initialize() and device_add().

Is is possible to have {platfom_,}device_uninitialize() functions that does everything except the ->release() call? If we had that, then we'd be able to have a flow along the lines of:

	int some_init_function(void)
	{
		int err;
	
		platform_device_init(&static_pdev);
	
		err = platform_device_add(&static_pdev))
		if (err)
			goto out_uninit;
	
		return 0;
	
	out_uninit:
		platform_device_uninit(&static_pdev);
		return err;
	}
Quoted 2 lines

... which I think would align with what people generally expect to have to do.

The issue here is that platform_device_add() allocates a device name and such resources are not released until the last reference is dropped.

It's been this way since 2008, but some of the static platform devices predates that and they both lack a release callback (explicitly required since 2003) and are not cleaned up on registration failure.

Since registration would essentially only fail during development (e.g. due to name collision or fault injection), this is hardly something to worry about, but we could consider moving towards dynamic objects to address both issues.

We have a few functions for allocating *and* registering platform devices that could be used in many of these cases (and they already clean up after themselves on errors):

platform_device_register_simple()
platform_device_register_data()
platform_device_register_resndata()
platform_device_register_full()

and where those do not fit (and cannot be extended) we have the underlying:

platform_device_alloc()
platform_device_add_resources()
platform_device_add_data()
plaform_device_add()

But there are some 800 static platform devices left, mostly in legacy platform code and board files that I assume few people care about.

Johan

#9 lore raw
Quoted 45 lines · Johan Hovold
Quoted 38 lines · Mark Rutland
Quoted 7 lines · Johan Hovold

It's not just the platform code as this directly reflects the behaviour of device_register() as Mark pointed out.

It is indeed an unfortunate quirk of the driver model, but one can argue that having a registration function that frees its argument on errors would be even worse. And even more so when many (or most) users get this right.

Ah, sorry; I had missed that the _put() step would actually free the object (and as you explain below, how that won't work for many callers).

Quoted 3 lines

So if we want to change this, I think we would need to deprecate device_register() in favour of explicit device_initialize() and device_add().

Is is possible to have {platfom_,}device_uninitialize() functions that does everything except the ->release() call? If we had that, then we'd be able to have a flow along the lines of:

	int some_init_function(void)
	{
		int err;
	
		platform_device_init(&static_pdev);
	
		err = platform_device_add(&static_pdev))
		if (err)
			goto out_uninit;
	
		return 0;
	
	out_uninit:
		platform_device_uninit(&static_pdev);
		return err;
	}
Quoted 2 lines

... which I think would align with what people generally expect to have to do.

The issue here is that platform_device_add() allocates a device name and such resources are not released until the last reference is dropped.

Quoted 8 lines

It's been this way since 2008, but some of the static platform devices predates that and they both lack a release callback (explicitly required since 2003) and are not cleaned up on registration failure.

Since registration would essentially only fail during development (e.g. due to name collision or fault injection), this is hardly something to worry about, but we could consider moving towards dynamic objects to address both issues.

Agreed, this whole thing, including the error handling, is all just theoretical as no real user ever hits this, which is why it has been _way_ down my priority list.

Quoted 12 lines

We have a few functions for allocating *and* registering platform devices that could be used in many of these cases (and they already clean up after themselves on errors):

platform_device_register_simple()
platform_device_register_data()
platform_device_register_resndata()
platform_device_register_full()

and where those do not fit (and cannot be extended) we have the underlying:

Quoted 7 lines

platform_device_alloc()
platform_device_add_resources()
platform_device_add_data()
plaform_device_add()

But there are some 800 static platform devices left, mostly in legacy platform code and board files that I assume few people care about.

Yes, I agree that we do have all of the needed apis here already, we should just work at converting existing drivers to the new apis OR just not caring at all as again, no one will ever hit these code paths :)

thanks,

greg k-h

#10 lore raw
In reply to Guangshuo Li
Sashiko AI review found 1 potential issue(s): - [High] Calling platform_device_put() on statically allocated platform devices triggers a kernel WARN. --

Sashiko AI review found 1 potential issue(s):
- [High] Calling platform_device_put() on statically allocated platform devices triggers a kernel WARN. --

commit 3d6b3748275060c9fa53f46ef32bdbc93f8522c7
Author: Guangshuo Li <[email protected]>

arm_pmu: acpi: fix reference leak on failed device registration

This commit attempts to fix a reference leak when platform_device_register() fails in arm_acpi_register_pmu_device() by calling platform_device_put() on the failed path.

Quoted 4 lines
drivers/perf/arm_pmu_acpi.c +0 -0
index e80f76d95e68b..5ce382661e344 100644

[ ... ]

drivers/perf/arm_pmu_acpi.c arm_acpi_register_pmu_device(struct platform_device *pdev, u8 len, · line 124

Quoted 8 lines
drivers/perf/arm_pmu_acpi.c +2 -1
@@ -119,8 +119,10 @@ arm_acpi_register_pmu_device(struct platform_device *pdev, u8 len,
 
 	pdev->resource[0].start = irq;
 	ret = platform_device_register(pdev);
-	if (ret)
+	if (ret) {
 		acpi_unregister_gsi(gsi);
+		platform_device_put(pdev);

Does this cause a kernel warning when called on statically allocated platform devices?

The arm_acpi_register_pmu_device() function is called with statically allocated platform devices:

arm_spe_acpi_register_device()

    arm_acpi_register_pmu_device(&spe_dev, ...)

arm_trbe_acpi_register_device()

    arm_acpi_register_pmu_device(&trbe_dev, ...)

Because these devices are statically allocated and not created via platform_device_alloc(), they do not have a .dev.release callback configured.

When platform_device_put() drops the reference count to 0, the driver core's device_release() function will see the missing release callback and trigger a warning splat.

On systems with panic_on_warn enabled, could this turn a benign device registration failure into a kernel panic?

Quoted 4 lines
drivers/perf/arm_pmu_acpi.c +1 -0
+	}
 
 	return ret;
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1