xref: /openbmc/linux/lib/once.c (revision 2a4187f4)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
246234253SHannes Frederic Sowa #include <linux/slab.h>
346234253SHannes Frederic Sowa #include <linux/spinlock.h>
446234253SHannes Frederic Sowa #include <linux/once.h>
546234253SHannes Frederic Sowa #include <linux/random.h>
61027b96eSKefeng Wang #include <linux/module.h>
746234253SHannes Frederic Sowa 
8c90aeb94SHannes Frederic Sowa struct once_work {
946234253SHannes Frederic Sowa 	struct work_struct work;
10cf4c950bSEric Biggers 	struct static_key_true *key;
111027b96eSKefeng Wang 	struct module *module;
1246234253SHannes Frederic Sowa };
1346234253SHannes Frederic Sowa 
once_deferred(struct work_struct * w)14c90aeb94SHannes Frederic Sowa static void once_deferred(struct work_struct *w)
1546234253SHannes Frederic Sowa {
16c90aeb94SHannes Frederic Sowa 	struct once_work *work;
1746234253SHannes Frederic Sowa 
18c90aeb94SHannes Frederic Sowa 	work = container_of(w, struct once_work, work);
1946234253SHannes Frederic Sowa 	BUG_ON(!static_key_enabled(work->key));
20cf4c950bSEric Biggers 	static_branch_disable(work->key);
211027b96eSKefeng Wang 	module_put(work->module);
2246234253SHannes Frederic Sowa 	kfree(work);
2346234253SHannes Frederic Sowa }
2446234253SHannes Frederic Sowa 
once_disable_jump(struct static_key_true * key,struct module * mod)251027b96eSKefeng Wang static void once_disable_jump(struct static_key_true *key, struct module *mod)
2646234253SHannes Frederic Sowa {
27c90aeb94SHannes Frederic Sowa 	struct once_work *w;
2846234253SHannes Frederic Sowa 
2946234253SHannes Frederic Sowa 	w = kmalloc(sizeof(*w), GFP_ATOMIC);
3046234253SHannes Frederic Sowa 	if (!w)
3146234253SHannes Frederic Sowa 		return;
3246234253SHannes Frederic Sowa 
33c90aeb94SHannes Frederic Sowa 	INIT_WORK(&w->work, once_deferred);
3446234253SHannes Frederic Sowa 	w->key = key;
351027b96eSKefeng Wang 	w->module = mod;
361027b96eSKefeng Wang 	__module_get(mod);
3746234253SHannes Frederic Sowa 	schedule_work(&w->work);
3846234253SHannes Frederic Sowa }
3946234253SHannes Frederic Sowa 
40c90aeb94SHannes Frederic Sowa static DEFINE_SPINLOCK(once_lock);
4146234253SHannes Frederic Sowa 
__do_once_start(bool * done,unsigned long * flags)42c90aeb94SHannes Frederic Sowa bool __do_once_start(bool *done, unsigned long *flags)
43c90aeb94SHannes Frederic Sowa 	__acquires(once_lock)
44c90aeb94SHannes Frederic Sowa {
45c90aeb94SHannes Frederic Sowa 	spin_lock_irqsave(&once_lock, *flags);
4646234253SHannes Frederic Sowa 	if (*done) {
47c90aeb94SHannes Frederic Sowa 		spin_unlock_irqrestore(&once_lock, *flags);
48c90aeb94SHannes Frederic Sowa 		/* Keep sparse happy by restoring an even lock count on
49c90aeb94SHannes Frederic Sowa 		 * this lock. In case we return here, we don't call into
50c90aeb94SHannes Frederic Sowa 		 * __do_once_done but return early in the DO_ONCE() macro.
51c90aeb94SHannes Frederic Sowa 		 */
52c90aeb94SHannes Frederic Sowa 		__acquire(once_lock);
5346234253SHannes Frederic Sowa 		return false;
5446234253SHannes Frederic Sowa 	}
5546234253SHannes Frederic Sowa 
5646234253SHannes Frederic Sowa 	return true;
5746234253SHannes Frederic Sowa }
58c90aeb94SHannes Frederic Sowa EXPORT_SYMBOL(__do_once_start);
59c90aeb94SHannes Frederic Sowa 
__do_once_done(bool * done,struct static_key_true * once_key,unsigned long * flags,struct module * mod)60cf4c950bSEric Biggers void __do_once_done(bool *done, struct static_key_true *once_key,
611027b96eSKefeng Wang 		    unsigned long *flags, struct module *mod)
62c90aeb94SHannes Frederic Sowa 	__releases(once_lock)
63c90aeb94SHannes Frederic Sowa {
64c90aeb94SHannes Frederic Sowa 	*done = true;
65c90aeb94SHannes Frederic Sowa 	spin_unlock_irqrestore(&once_lock, *flags);
661027b96eSKefeng Wang 	once_disable_jump(once_key, mod);
67c90aeb94SHannes Frederic Sowa }
68c90aeb94SHannes Frederic Sowa EXPORT_SYMBOL(__do_once_done);
6962c07983SEric Dumazet 
7062c07983SEric Dumazet static DEFINE_MUTEX(once_mutex);
7162c07983SEric Dumazet 
__do_once_sleepable_start(bool * done)72*2a4187f4SJason A. Donenfeld bool __do_once_sleepable_start(bool *done)
7362c07983SEric Dumazet 	__acquires(once_mutex)
7462c07983SEric Dumazet {
7562c07983SEric Dumazet 	mutex_lock(&once_mutex);
7662c07983SEric Dumazet 	if (*done) {
7762c07983SEric Dumazet 		mutex_unlock(&once_mutex);
7862c07983SEric Dumazet 		/* Keep sparse happy by restoring an even lock count on
7962c07983SEric Dumazet 		 * this mutex. In case we return here, we don't call into
80*2a4187f4SJason A. Donenfeld 		 * __do_once_done but return early in the DO_ONCE_SLEEPABLE() macro.
8162c07983SEric Dumazet 		 */
8262c07983SEric Dumazet 		__acquire(once_mutex);
8362c07983SEric Dumazet 		return false;
8462c07983SEric Dumazet 	}
8562c07983SEric Dumazet 
8662c07983SEric Dumazet 	return true;
8762c07983SEric Dumazet }
88*2a4187f4SJason A. Donenfeld EXPORT_SYMBOL(__do_once_sleepable_start);
8962c07983SEric Dumazet 
__do_once_sleepable_done(bool * done,struct static_key_true * once_key,struct module * mod)90*2a4187f4SJason A. Donenfeld void __do_once_sleepable_done(bool *done, struct static_key_true *once_key,
9162c07983SEric Dumazet 			 struct module *mod)
9262c07983SEric Dumazet 	__releases(once_mutex)
9362c07983SEric Dumazet {
9462c07983SEric Dumazet 	*done = true;
9562c07983SEric Dumazet 	mutex_unlock(&once_mutex);
9662c07983SEric Dumazet 	once_disable_jump(once_key, mod);
9762c07983SEric Dumazet }
98*2a4187f4SJason A. Donenfeld EXPORT_SYMBOL(__do_once_sleepable_done);
99