Bug 270270 - mii: memory leak when mii_attach() fails
Summary: mii: memory leak when mii_attach() fails
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Many People
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-03-16 14:45 UTC by Jiahao LI
Modified: 2024-11-26 06:42 UTC (History)
2 users (show)

See Also:


Attachments
Fix of memory leak when mii_attach() fails (465 bytes, patch)
2023-03-16 14:45 UTC, Jiahao LI
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jiahao LI 2023-03-16 14:45:10 UTC
Created attachment 240901 [details]
Fix of memory leak when mii_attach() fails

When the PHY attach fails, the allocated miibus->softc from mii_attach() -> device_set_driver() is not free. There is a memory leak for the "bus-sc" memory.

A proposed fix is attached.
Comment 1 Mina Galić freebsd_triage 2023-03-16 16:18:14 UTC
this looks like a trivial enough patch to submit via GitHub:
https://docs.freebsd.org/en/articles/contributing/
Comment 2 Mina Galić freebsd_triage 2023-03-17 03:41:18 UTC
from IRC:

16:31 <kevans91_> meeena: mii_attach doesn't appear to allocate the softc except by device_* API that does it implicitly, why isn't this cleaned up with device_delete_child(), I wonder?
16:33 <kevans91_> it doesn't seem to claim the softc to prevent it from being cleaned up elsewhere
Comment 3 Jiahao LI 2023-03-17 20:05:48 UTC
(In reply to Mina Galić from comment #2)
Thanks for the suggestions.

I agree that it would be better to clean up the softc in device_delete_child(). Since changing the device_delete_child() would have an impact on many devices and the softc will be allocated if the "miibus" input parameter of mii_attach() is null, I only change it in the mii_attach() for minimal impact of other components. It would be better that Freebsd can re-investigate the memory allocation issues using device_*() APIs.