]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - lib/once.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-kernel.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>
6
c90aeb94 7struct once_work {
46234253 8 struct work_struct work;
cf4c950b 9 struct static_key_true *key;
46234253
HFS
10};
11
c90aeb94 12static void once_deferred(struct work_struct *w)
46234253 13{
c90aeb94 14 struct once_work *work;
46234253 15
c90aeb94 16 work = container_of(w, struct once_work, work);
46234253 17 BUG_ON(!static_key_enabled(work->key));
cf4c950b 18 static_branch_disable(work->key);
46234253
HFS
19 kfree(work);
20}
21
cf4c950b 22static void once_disable_jump(struct static_key_true *key)
46234253 23{
c90aeb94 24 struct once_work *w;
46234253
HFS
25
26 w = kmalloc(sizeof(*w), GFP_ATOMIC);
27 if (!w)
28 return;
29
c90aeb94 30 INIT_WORK(&w->work, once_deferred);
46234253
HFS
31 w->key = key;
32 schedule_work(&w->work);
33}
34
c90aeb94 35static DEFINE_SPINLOCK(once_lock);
46234253 36
c90aeb94
HFS
37bool __do_once_start(bool *done, unsigned long *flags)
38 __acquires(once_lock)
39{
40 spin_lock_irqsave(&once_lock, *flags);
46234253 41 if (*done) {
c90aeb94
HFS
42 spin_unlock_irqrestore(&once_lock, *flags);
43 /* Keep sparse happy by restoring an even lock count on
44 * this lock. In case we return here, we don't call into
45 * __do_once_done but return early in the DO_ONCE() macro.
46 */
47 __acquire(once_lock);
46234253
HFS
48 return false;
49 }
50
46234253
HFS
51 return true;
52}
c90aeb94
HFS
53EXPORT_SYMBOL(__do_once_start);
54
cf4c950b 55void __do_once_done(bool *done, struct static_key_true *once_key,
c90aeb94
HFS
56 unsigned long *flags)
57 __releases(once_lock)
58{
59 *done = true;
60 spin_unlock_irqrestore(&once_lock, *flags);
61 once_disable_jump(once_key);
62}
63EXPORT_SYMBOL(__do_once_done);