Page MenuHomeFreeBSD

Fix busdma resource leak on usb device detach

Authored by ian on Sep 28 2021, 4:20 PM.
Referenced Files
Unknown Object (File)
Thu, Feb 8, 4:11 AM
Unknown Object (File)
Dec 20 2023, 3:57 AM
Unknown Object (File)
Dec 14 2023, 8:44 PM
Unknown Object (File)
Oct 16 2023, 9:25 AM
Unknown Object (File)
Oct 10 2023, 12:42 AM
Unknown Object (File)
Aug 28 2023, 10:53 PM
Unknown Object (File)
Aug 17 2023, 2:07 PM
Unknown Object (File)
Aug 16 2023, 10:11 AM



When a usb device is detached, usb_pc_dmamap_destroy() called bus_dmamap_destroy() while the map was still loaded. That's harmless on x86 architectures, but on all other platforms it causes bus_dmamap_destroy() to return EBUSY and leak away any memory resources (including bounce buffers) associated with the mapping, as well as any allocated map structure itself.

This change introduces a new is_loaded flag to the usb_page_cache struct to track whether a map is loaded or not. If the map is loaded, bus_dmamap_unload() is called before bus_dmamap_destroy() to avoid leaking away resources.

Test Plan

On a non-x86 platform such as arm, sysctl vm.kvm_free can be used to track the resource leaks. Before this change, you can see kvm_free go down with each usb device attach/detach. (Using usbconfig reset in a shell while 1 loop will eventually run the kernel out of memory and hang or panic the system.) After this change, the sysctl shows no change in kvm_free across multiple attach/detach cycles.

Diff Detail

rG FreeBSD src repository
Lint Not Applicable
Tests Not Applicable