Re: About THIS_MODULE

Petr Pavlu <[email protected]> Thu, 9 Jul 2026 16:31:41 +0200
Newsgroups org.kernel.vger.linux-modules
Message-ID <[email protected]>
On 7/3/26 5:33 PM, John Garry wrote:
> Hi all,
> 
> I have a query which I hope someone can advise on.
> 
> I am adding a library API which requires a driver to pass the driver module pointer to the API.
> 
> So we use THIS_MODULE for that purpose.
> 
> However, adding a sanity check in the library to ensure that pointer is set is a challenge. Normally we would check that the module pointer is non-NULL. However, for a built-in driver module, THIS_MODULE is NULL, so rely on the non-NULL check.
> 
> Any idea how to deal with this?
Right, a NULL module pointer is ambiguous if you want to distinguish
between these two cases. I can't think of a neat way to handle this in
a library API.

One option would be to rework THIS_MODULE and introduce a reduced module
struct also for vmlinux and built-in modules. However, this isn't
a trivial change and I can't recall a case in the last few years where
this has caused a specific issue. Additionally, if someone forgets to
pass a module pointer somewhere, I would expect it to be fairly easy to
detect and fix.

-- 
Cheers,
Petr