try_module_get(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ¦‹ 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:

  1. 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 🫢
  2. 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 use try_module_get on 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 πŸ”₯


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