]>
Commit | Line | Data |
---|---|---|
46234253 HFS |
1 | #include <linux/slab.h> |
2 | #include <linux/spinlock.h> | |
3 | #include <linux/once.h> | |
4 | #include <linux/random.h> | |
5 | ||
6 | struct __random_once_work { | |
7 | struct work_struct work; | |
8 | struct static_key *key; | |
9 | }; | |
10 | ||
11 | static void __random_once_deferred(struct work_struct *w) | |
12 | { | |
13 | struct __random_once_work *work; | |
14 | ||
15 | work = container_of(w, struct __random_once_work, work); | |
16 | BUG_ON(!static_key_enabled(work->key)); | |
17 | static_key_slow_dec(work->key); | |
18 | kfree(work); | |
19 | } | |
20 | ||
21 | static void __random_once_disable_jump(struct static_key *key) | |
22 | { | |
23 | struct __random_once_work *w; | |
24 | ||
25 | w = kmalloc(sizeof(*w), GFP_ATOMIC); | |
26 | if (!w) | |
27 | return; | |
28 | ||
29 | INIT_WORK(&w->work, __random_once_deferred); | |
30 | w->key = key; | |
31 | schedule_work(&w->work); | |
32 | } | |
33 | ||
34 | bool __get_random_once(void *buf, int nbytes, bool *done, | |
35 | struct static_key *once_key) | |
36 | { | |
37 | static DEFINE_SPINLOCK(lock); | |
38 | unsigned long flags; | |
39 | ||
40 | spin_lock_irqsave(&lock, flags); | |
41 | if (*done) { | |
42 | spin_unlock_irqrestore(&lock, flags); | |
43 | return false; | |
44 | } | |
45 | ||
46 | get_random_bytes(buf, nbytes); | |
47 | *done = true; | |
48 | spin_unlock_irqrestore(&lock, flags); | |
49 | ||
50 | __random_once_disable_jump(once_key); | |
51 | ||
52 | return true; | |
53 | } | |
54 | EXPORT_SYMBOL(__get_random_once); |