On Tue, Oct 20, 2026 at 05:22:18PM +0200, Petr Pavlu wrote:
> The trace_module_put() tracepoint is used only for debugging. What
> tooling are you referring to that could be confused by changing the
> recorded refcnt value?

Fair point. Please ignore. I have another idea to avoid a reference count
that is off by one.

> Normally, module_put() is not invoked concurrently with free_module()
> and doesn't need to synchronize with it, so an RCU read-side critical
> section only adds overhead.
> 
> The preempt_disable() call in module_put() was previously needed because
> of the complex module_ref scheme, but that was replaced in commit
> 2f35c41f58a9 ("module: Replace module_ref with atomic_t refcnt"). Commit
> cb0b50b813f6 ("module: Remove preempt_disable() from module reference
> counting.") subsequently removed the no longer needed synchronization.

Agreed.

> The fix is valid, but I don't see a strong argument to introduce an RCU
> read-side critical section in module_put(), especially just for
> a tracepoint that will be disabled 99% of the time.

How about the following to ensure the fast path incurs zero RCU overhead
when tracing is disabled, gate the RCU critical section behind
trace_module_put_enabled():

diff --git a/kernel/module/main.c b/kernel/module/main.c
index 34e0204ea37c..14bad822e18f 100644
--- a/kernel/module/main.c
+++ b/kernel/module/main.c
@@ -960,15 +960,27 @@ bool try_module_get(struct module *module)
 }
 EXPORT_SYMBOL(try_module_get);
 
-void module_put(struct module *module)
+static __always_inline void __module_put(struct module *module)
 {
        int ret;
 
-       if (module) {
-               ret = atomic_dec_if_positive(&module->refcnt);
-               WARN_ON(ret < 0);       /* Failed to put refcount */
+       ret = atomic_dec_if_positive(&module->refcnt);
+       WARN_ON(ret < 0);    /* Failed to put refcount */
+}
+
+void module_put(struct module *module)
+{
+       if (!module)
+               return;
+
+       if (trace_module_put_enabled()) {
+               guard(rcu)();
+               __module_put(module);
                trace_module_put(module, _RET_IP_);
+               return;
        }
+
+       __module_put(module);
 }
 EXPORT_SYMBOL(module_put);
 

-- 
Aaron Tomlin

Reply via email to