Page MenuHomeFreeBSD

Fix busdma resource leak on usb device detach
ClosedPublic

Authored by ian on Sep 28 2021, 4:20 PM.
Tags
None
Referenced Files
Unknown Object (File)
Fri, May 15, 8:50 AM
Unknown Object (File)
Fri, May 15, 8:33 AM
Unknown Object (File)
Fri, May 15, 7:23 AM
Unknown Object (File)
Tue, May 12, 12:53 AM
Unknown Object (File)
Mon, May 11, 6:14 AM
Unknown Object (File)
Thu, Apr 30, 3:20 PM
Unknown Object (File)
Mon, Apr 27, 4:05 PM
Unknown Object (File)
Sat, Apr 25, 12:21 AM
Subscribers

Details

Summary

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

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable