Bug 248050 - Keeps handle to loaded kernel module twice?
Summary: Keeps handle to loaded kernel module twice?
Status: Closed Not A Bug
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: 12.1-STABLE
Hardware: amd64 Any
: --- Affects Only Me
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-07-17 17:44 UTC by dmilith
Modified: 2022-03-24 14:48 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description dmilith 2020-07-17 17:44:16 UTC
Basically:

kldstat | grep -c dtrace # returns "0", no kernel modules loaded yet
kldload dtrace # ok
kldload dtraceall # ok
kldstat | grep -c dtrace # returns "2" - two matching kernel modules loaded - all ok.


And here's the issue:

kldunload dtrace # ok - shouldn't be ok. dtrace module is still loaded at this point but command returns 0 - success
kldstat | grep -c dtrace # returns "2" again.

kldunload dtrace # causes (expected) fail:

kldunload: can't unload file: Device busy
Comment 1 dmilith 2020-07-17 17:51:07 UTC
It's more stuff like that after further digging:

kldunload opensolaris # returns 0 (nothing happens)
kldunload opensolaris # fails (zfs requirement so no go)

kldunload kernel # returns 0 (nothing happens)
kldunload kernel # returns: kldunload: can't unload file: Device bus
Comment 2 Mark Johnston freebsd_committer freebsd_triage 2020-07-23 15:09:46 UTC
This is by design, though the behaviour is admittedly not intuitive.  When you kldload dtrace, dtrace.ko gets a reference; loading dtraceall.ko adds a second reference to dtrace.ko.

The first attempt to unload dtrace releases the initial reference, but dtraceall.ko's reference ensures that dtrace.ko remains loaded.  The second attempt to unload fails because dtraceall.ko still depends on dtrace.ko.  Unloading dtraceall.ko should cause dtrace.ko to be unloaded automatically in that case.
Comment 3 Ed Maste freebsd_committer freebsd_triage 2022-03-24 14:48:03 UTC
Perhaps we could make a note of this in the kldunload man page.