Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Hannes Frederic Sowa | 4623425 | 2015-10-08 01:20:35 +0200 | [diff] [blame] | 2 | #include <linux/slab.h> |
| 3 | #include <linux/spinlock.h> |
| 4 | #include <linux/once.h> |
| 5 | #include <linux/random.h> |
Kefeng Wang | 1027b96 | 2021-08-06 16:21:24 +0800 | [diff] [blame] | 6 | #include <linux/module.h> |
Hannes Frederic Sowa | 4623425 | 2015-10-08 01:20:35 +0200 | [diff] [blame] | 7 | |
Hannes Frederic Sowa | c90aeb9 | 2015-10-08 01:20:36 +0200 | [diff] [blame] | 8 | struct once_work { |
Hannes Frederic Sowa | 4623425 | 2015-10-08 01:20:35 +0200 | [diff] [blame] | 9 | struct work_struct work; |
Eric Biggers | cf4c950 | 2017-10-09 14:30:52 -0700 | [diff] [blame] | 10 | struct static_key_true *key; |
Kefeng Wang | 1027b96 | 2021-08-06 16:21:24 +0800 | [diff] [blame] | 11 | struct module *module; |
Hannes Frederic Sowa | 4623425 | 2015-10-08 01:20:35 +0200 | [diff] [blame] | 12 | }; |
| 13 | |
Hannes Frederic Sowa | c90aeb9 | 2015-10-08 01:20:36 +0200 | [diff] [blame] | 14 | static void once_deferred(struct work_struct *w) |
Hannes Frederic Sowa | 4623425 | 2015-10-08 01:20:35 +0200 | [diff] [blame] | 15 | { |
Hannes Frederic Sowa | c90aeb9 | 2015-10-08 01:20:36 +0200 | [diff] [blame] | 16 | struct once_work *work; |
Hannes Frederic Sowa | 4623425 | 2015-10-08 01:20:35 +0200 | [diff] [blame] | 17 | |
Hannes Frederic Sowa | c90aeb9 | 2015-10-08 01:20:36 +0200 | [diff] [blame] | 18 | work = container_of(w, struct once_work, work); |
Hannes Frederic Sowa | 4623425 | 2015-10-08 01:20:35 +0200 | [diff] [blame] | 19 | BUG_ON(!static_key_enabled(work->key)); |
Eric Biggers | cf4c950 | 2017-10-09 14:30:52 -0700 | [diff] [blame] | 20 | static_branch_disable(work->key); |
Kefeng Wang | 1027b96 | 2021-08-06 16:21:24 +0800 | [diff] [blame] | 21 | module_put(work->module); |
Hannes Frederic Sowa | 4623425 | 2015-10-08 01:20:35 +0200 | [diff] [blame] | 22 | kfree(work); |
| 23 | } |
| 24 | |
Kefeng Wang | 1027b96 | 2021-08-06 16:21:24 +0800 | [diff] [blame] | 25 | static void once_disable_jump(struct static_key_true *key, struct module *mod) |
Hannes Frederic Sowa | 4623425 | 2015-10-08 01:20:35 +0200 | [diff] [blame] | 26 | { |
Hannes Frederic Sowa | c90aeb9 | 2015-10-08 01:20:36 +0200 | [diff] [blame] | 27 | struct once_work *w; |
Hannes Frederic Sowa | 4623425 | 2015-10-08 01:20:35 +0200 | [diff] [blame] | 28 | |
| 29 | w = kmalloc(sizeof(*w), GFP_ATOMIC); |
| 30 | if (!w) |
| 31 | return; |
| 32 | |
Hannes Frederic Sowa | c90aeb9 | 2015-10-08 01:20:36 +0200 | [diff] [blame] | 33 | INIT_WORK(&w->work, once_deferred); |
Hannes Frederic Sowa | 4623425 | 2015-10-08 01:20:35 +0200 | [diff] [blame] | 34 | w->key = key; |
Kefeng Wang | 1027b96 | 2021-08-06 16:21:24 +0800 | [diff] [blame] | 35 | w->module = mod; |
| 36 | __module_get(mod); |
Hannes Frederic Sowa | 4623425 | 2015-10-08 01:20:35 +0200 | [diff] [blame] | 37 | schedule_work(&w->work); |
| 38 | } |
| 39 | |
Hannes Frederic Sowa | c90aeb9 | 2015-10-08 01:20:36 +0200 | [diff] [blame] | 40 | static DEFINE_SPINLOCK(once_lock); |
Hannes Frederic Sowa | 4623425 | 2015-10-08 01:20:35 +0200 | [diff] [blame] | 41 | |
Hannes Frederic Sowa | c90aeb9 | 2015-10-08 01:20:36 +0200 | [diff] [blame] | 42 | bool __do_once_start(bool *done, unsigned long *flags) |
| 43 | __acquires(once_lock) |
| 44 | { |
| 45 | spin_lock_irqsave(&once_lock, *flags); |
Hannes Frederic Sowa | 4623425 | 2015-10-08 01:20:35 +0200 | [diff] [blame] | 46 | if (*done) { |
Hannes Frederic Sowa | c90aeb9 | 2015-10-08 01:20:36 +0200 | [diff] [blame] | 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); |
Hannes Frederic Sowa | 4623425 | 2015-10-08 01:20:35 +0200 | [diff] [blame] | 53 | return false; |
| 54 | } |
| 55 | |
Hannes Frederic Sowa | 4623425 | 2015-10-08 01:20:35 +0200 | [diff] [blame] | 56 | return true; |
| 57 | } |
Hannes Frederic Sowa | c90aeb9 | 2015-10-08 01:20:36 +0200 | [diff] [blame] | 58 | EXPORT_SYMBOL(__do_once_start); |
| 59 | |
Eric Biggers | cf4c950 | 2017-10-09 14:30:52 -0700 | [diff] [blame] | 60 | void __do_once_done(bool *done, struct static_key_true *once_key, |
Kefeng Wang | 1027b96 | 2021-08-06 16:21:24 +0800 | [diff] [blame] | 61 | unsigned long *flags, struct module *mod) |
Hannes Frederic Sowa | c90aeb9 | 2015-10-08 01:20:36 +0200 | [diff] [blame] | 62 | __releases(once_lock) |
| 63 | { |
| 64 | *done = true; |
| 65 | spin_unlock_irqrestore(&once_lock, *flags); |
Kefeng Wang | 1027b96 | 2021-08-06 16:21:24 +0800 | [diff] [blame] | 66 | once_disable_jump(once_key, mod); |
Hannes Frederic Sowa | c90aeb9 | 2015-10-08 01:20:36 +0200 | [diff] [blame] | 67 | } |
| 68 | EXPORT_SYMBOL(__do_once_done); |