1 #include <linux/slab.h>
2 #include <linux/spinlock.h>
3 #include <linux/once.h>
4 #include <linux/random.h>
6 struct __random_once_work {
7 struct work_struct work;
8 struct static_key *key;
11 static void __random_once_deferred(struct work_struct *w)
13 struct __random_once_work *work;
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);
21 static void __random_once_disable_jump(struct static_key *key)
23 struct __random_once_work *w;
25 w = kmalloc(sizeof(*w), GFP_ATOMIC);
29 INIT_WORK(&w->work, __random_once_deferred);
31 schedule_work(&w->work);
34 bool __get_random_once(void *buf, int nbytes, bool *done,
35 struct static_key *once_key)
37 static DEFINE_SPINLOCK(lock);
40 spin_lock_irqsave(&lock, flags);
42 spin_unlock_irqrestore(&lock, flags);
46 get_random_bytes(buf, nbytes);
48 spin_unlock_irqrestore(&lock, flags);
50 __random_once_disable_jump(once_key);
54 EXPORT_SYMBOL(__get_random_once);