try_module_get(9)
π¦ try_module_get(9) β the "can I pls have a ref, but like, only if you're not dying" situation
okay bestie, so this is a page from the Kernel API Manual, and it's about try_module_get(9) β genuinely one of those little functions that saves your whole entire life if you use it right π
β¨ NAME β¨
try_module_get β take module refcount unless module is being removed
πΈ SYNOPSIS πΈ
bool try_module_get (struct module *module );
π ARGUMENTS π
moduleβ the module we should check for
π DESCRIPTION π
So here's the vibe: only try to get a module reference count if the module is not being removed. Like, this call will fail if the module is in the process of being removed. Periodt. π
And ngl, you also have to be careful to make sure the module actually exists and is alive prior to using this call. You can guarantee that through two means:
- Direct protection β you know an earlier caller must have increased the module reference through
__module_get. Typically you get this by having another entity other than the module itself increment the module reference count. iykyk π«Ά - Implied protection β there's an implied protection against module removal. Example: the implied protection used by kernfs / sysfs. The sysfs store / read file operations are guaranteed to exist through the use of kernfs's active reference (see
kernfs_active), and a sysfs / kernfs file removal cannot happen unless the same file is not active. So therefore, if a sysfs file is being read or written to, the module which created it must still exist. Which means it is therefore safe to usetry_module_geton module sysfs store / read ops. slay β¨
One of the real values to try_module_get is the module_is_live check, which ensures that the caller of try_module_get can yield to userspace module removal requests and gracefully fail if the module is on its way out. Like, it fails pretty instead of exploding π
Returns true if the reference count was successfully incremented. π
π₯ SEE ALSO π₯
- Kernel file
./include/linux/module.h module_init(9),module_exit(9),struct klp_modinfo(9),module_put(9)
and that's the whole page bestie!! it lives in ./include/linux, dated October 2026 β go forth and increment responsibly, you absolute icon ππ
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms