Merge tag 'for-6.4-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/pateldipen19...
[linux-block.git] / lib / once.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
46234253
HFS
2#include <linux/slab.h>
3#include <linux/spinlock.h>
4#include <linux/once.h>
5#include <linux/random.h>
1027b96e 6#include <linux/module.h>
46234253 7
c90aeb94 8struct once_work {
46234253 9 struct work_struct work;
cf4c950b 10 struct static_key_true *key;
1027b96e 11 struct module *module;
46234253
HFS
12};
13
c90aeb94 14static void once_deferred(struct work_struct *w)
46234253 15{
c90aeb94 16 struct once_work *work;
46234253 17
c90aeb94 18 work = container_of(w, struct once_work, work);
46234253 19 BUG_ON(!static_key_enabled(work->key));
cf4c950b 20 static_branch_disable(work->key);
1027b96e 21 module_put(work->module);
46234253
HFS
22 kfree(work);
23}
24
1027b96e 25static void once_disable_jump(struct static_key_true *key, struct module *mod)
46234253 26{
c90aeb94 27 struct once_work *w;
46234253
HFS
28
29 w = kmalloc(sizeof(*w), GFP_ATOMIC);
30 if (!w)
31 return;
32
c90aeb94 33 INIT_WORK(&w->work, once_deferred);
46234253 34 w->key = key;
1027b96e
KW
35 w->module = mod;
36 __module_get(mod);
46234253
HFS
37 schedule_work(&w->work);
38}
39
c90aeb94 40static DEFINE_SPINLOCK(once_lock);
46234253 41
c90aeb94
HFS
42bool __do_once_start(bool *done, unsigned long *flags)
43 __acquires(once_lock)
44{
45 spin_lock_irqsave(&once_lock, *flags);
46234253 46 if (*done) {
c90aeb94
HFS
47 spin_unlock_irqrestore(&once_lock, *flags);
48 /* Keep sparse happy by restoring an even lock count on
49 * this lock. In case we return here, we don't call into
50 * __do_once_done but return early in the DO_ONCE() macro.
51 */
52 __acquire(once_lock);
46234253
HFS
53 return false;
54 }
55
46234253
HFS
56 return true;
57}
c90aeb94
HFS
58EXPORT_SYMBOL(__do_once_start);
59
cf4c950b 60void __do_once_done(bool *done, struct static_key_true *once_key,
1027b96e 61 unsigned long *flags, struct module *mod)
c90aeb94
HFS
62 __releases(once_lock)
63{
64 *done = true;
65 spin_unlock_irqrestore(&once_lock, *flags);
1027b96e 66 once_disable_jump(once_key, mod);
c90aeb94
HFS
67}
68EXPORT_SYMBOL(__do_once_done);
62c07983
ED
69
70static DEFINE_MUTEX(once_mutex);
71
2a4187f4 72bool __do_once_sleepable_start(bool *done)
62c07983
ED
73 __acquires(once_mutex)
74{
75 mutex_lock(&once_mutex);
76 if (*done) {
77 mutex_unlock(&once_mutex);
78 /* Keep sparse happy by restoring an even lock count on
79 * this mutex. In case we return here, we don't call into
2a4187f4 80 * __do_once_done but return early in the DO_ONCE_SLEEPABLE() macro.
62c07983
ED
81 */
82 __acquire(once_mutex);
83 return false;
84 }
85
86 return true;
87}
2a4187f4 88EXPORT_SYMBOL(__do_once_sleepable_start);
62c07983 89
2a4187f4 90void __do_once_sleepable_done(bool *done, struct static_key_true *once_key,
62c07983
ED
91 struct module *mod)
92 __releases(once_mutex)
93{
94 *done = true;
95 mutex_unlock(&once_mutex);
96 once_disable_jump(once_key, mod);
97}
2a4187f4 98EXPORT_SYMBOL(__do_once_sleepable_done);