^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/once.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) struct once_work {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) struct work_struct work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) struct static_key_true *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) static void once_deferred(struct work_struct *w)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct once_work *work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) work = container_of(w, struct once_work, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) BUG_ON(!static_key_enabled(work->key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static_branch_disable(work->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) kfree(work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static void once_disable_jump(struct static_key_true *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct once_work *w;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) w = kmalloc(sizeof(*w), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) if (!w)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) INIT_WORK(&w->work, once_deferred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) w->key = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) schedule_work(&w->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static DEFINE_SPINLOCK(once_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) bool __do_once_start(bool *done, unsigned long *flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) __acquires(once_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) spin_lock_irqsave(&once_lock, *flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (*done) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) spin_unlock_irqrestore(&once_lock, *flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /* Keep sparse happy by restoring an even lock count on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * this lock. In case we return here, we don't call into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * __do_once_done but return early in the DO_ONCE() macro.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) __acquire(once_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) EXPORT_SYMBOL(__do_once_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) void __do_once_done(bool *done, struct static_key_true *once_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) unsigned long *flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) __releases(once_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) *done = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) spin_unlock_irqrestore(&once_lock, *flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) once_disable_jump(once_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) EXPORT_SYMBOL(__do_once_done);