b24413180f560 (Greg Kroah-Hartman 2017-11-01 15:07:57 +0100 1) // SPDX-License-Identifier: GPL-2.0
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 2) #include <linux/slab.h>
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 3) #include <linux/spinlock.h>
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 4) #include <linux/once.h>
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 5) #include <linux/random.h>
b8eaf1e595fef (Kefeng Wang 2021-08-06 16:21:24 +0800 6) #include <linux/module.h>
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 7)
c90aeb948222a (Hannes Frederic Sowa 2015-10-08 01:20:36 +0200 8) struct once_work {
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 9) struct work_struct work;
cf4c950b87ee2 (Eric Biggers 2017-10-09 14:30:52 -0700 10) struct static_key_true *key;
b8eaf1e595fef (Kefeng Wang 2021-08-06 16:21:24 +0800 11) struct module *module;
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 12) };
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 13)
c90aeb948222a (Hannes Frederic Sowa 2015-10-08 01:20:36 +0200 14) static void once_deferred(struct work_struct *w)
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 15) {
c90aeb948222a (Hannes Frederic Sowa 2015-10-08 01:20:36 +0200 16) struct once_work *work;
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 17)
c90aeb948222a (Hannes Frederic Sowa 2015-10-08 01:20:36 +0200 18) work = container_of(w, struct once_work, work);
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 19) BUG_ON(!static_key_enabled(work->key));
cf4c950b87ee2 (Eric Biggers 2017-10-09 14:30:52 -0700 20) static_branch_disable(work->key);
b8eaf1e595fef (Kefeng Wang 2021-08-06 16:21:24 +0800 21) module_put(work->module);
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 22) kfree(work);
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 23) }
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 24)
b8eaf1e595fef (Kefeng Wang 2021-08-06 16:21:24 +0800 25) static void once_disable_jump(struct static_key_true *key, struct module *mod)
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 26) {
c90aeb948222a (Hannes Frederic Sowa 2015-10-08 01:20:36 +0200 27) struct once_work *w;
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 28)
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 29) w = kmalloc(sizeof(*w), GFP_ATOMIC);
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 30) if (!w)
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 31) return;
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 32)
c90aeb948222a (Hannes Frederic Sowa 2015-10-08 01:20:36 +0200 33) INIT_WORK(&w->work, once_deferred);
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 34) w->key = key;
b8eaf1e595fef (Kefeng Wang 2021-08-06 16:21:24 +0800 35) w->module = mod;
b8eaf1e595fef (Kefeng Wang 2021-08-06 16:21:24 +0800 36) __module_get(mod);
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 37) schedule_work(&w->work);
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 38) }
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 39)
c90aeb948222a (Hannes Frederic Sowa 2015-10-08 01:20:36 +0200 40) static DEFINE_SPINLOCK(once_lock);
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 41)
c90aeb948222a (Hannes Frederic Sowa 2015-10-08 01:20:36 +0200 42) bool __do_once_start(bool *done, unsigned long *flags)
c90aeb948222a (Hannes Frederic Sowa 2015-10-08 01:20:36 +0200 43) __acquires(once_lock)
c90aeb948222a (Hannes Frederic Sowa 2015-10-08 01:20:36 +0200 44) {
c90aeb948222a (Hannes Frederic Sowa 2015-10-08 01:20:36 +0200 45) spin_lock_irqsave(&once_lock, *flags);
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 46) if (*done) {
c90aeb948222a (Hannes Frederic Sowa 2015-10-08 01:20:36 +0200 47) spin_unlock_irqrestore(&once_lock, *flags);
c90aeb948222a (Hannes Frederic Sowa 2015-10-08 01:20:36 +0200 48) /* Keep sparse happy by restoring an even lock count on
c90aeb948222a (Hannes Frederic Sowa 2015-10-08 01:20:36 +0200 49) * this lock. In case we return here, we don't call into
c90aeb948222a (Hannes Frederic Sowa 2015-10-08 01:20:36 +0200 50) * __do_once_done but return early in the DO_ONCE() macro.
c90aeb948222a (Hannes Frederic Sowa 2015-10-08 01:20:36 +0200 51) */
c90aeb948222a (Hannes Frederic Sowa 2015-10-08 01:20:36 +0200 52) __acquire(once_lock);
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 53) return false;
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 54) }
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 55)
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 56) return true;
46234253b9363 (Hannes Frederic Sowa 2015-10-08 01:20:35 +0200 57) }
c90aeb948222a (Hannes Frederic Sowa 2015-10-08 01:20:36 +0200 58) EXPORT_SYMBOL(__do_once_start);
c90aeb948222a (Hannes Frederic Sowa 2015-10-08 01:20:36 +0200 59)
cf4c950b87ee2 (Eric Biggers 2017-10-09 14:30:52 -0700 60) void __do_once_done(bool *done, struct static_key_true *once_key,
b8eaf1e595fef (Kefeng Wang 2021-08-06 16:21:24 +0800 61) unsigned long *flags, struct module *mod)
c90aeb948222a (Hannes Frederic Sowa 2015-10-08 01:20:36 +0200 62) __releases(once_lock)
c90aeb948222a (Hannes Frederic Sowa 2015-10-08 01:20:36 +0200 63) {
c90aeb948222a (Hannes Frederic Sowa 2015-10-08 01:20:36 +0200 64) *done = true;
c90aeb948222a (Hannes Frederic Sowa 2015-10-08 01:20:36 +0200 65) spin_unlock_irqrestore(&once_lock, *flags);
b8eaf1e595fef (Kefeng Wang 2021-08-06 16:21:24 +0800 66) once_disable_jump(once_key, mod);
c90aeb948222a (Hannes Frederic Sowa 2015-10-08 01:20:36 +0200 67) }
c90aeb948222a (Hannes Frederic Sowa 2015-10-08 01:20:36 +0200 68) EXPORT_SYMBOL(__do_once_done);