Files

99 lines
2.2 KiB
C
Raw Permalink Normal View History

// SPDX-License-Identifier: GPL-2.0
2015-10-08 01:20:35 +02:00
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/once.h>
#include <linux/random.h>
2021-08-06 16:21:24 +08:00
#include <linux/module.h>
2015-10-08 01:20:35 +02:00
struct once_work {
2015-10-08 01:20:35 +02:00
struct work_struct work;
2017-10-09 14:30:52 -07:00
struct static_key_true *key;
2021-08-06 16:21:24 +08:00
struct module *module;
2015-10-08 01:20:35 +02:00
};
static void once_deferred(struct work_struct *w)
2015-10-08 01:20:35 +02:00
{
struct once_work *work;
2015-10-08 01:20:35 +02:00
work = container_of(w, struct once_work, work);
2015-10-08 01:20:35 +02:00
BUG_ON(!static_key_enabled(work->key));
2017-10-09 14:30:52 -07:00
static_branch_disable(work->key);
2021-08-06 16:21:24 +08:00
module_put(work->module);
2015-10-08 01:20:35 +02:00
kfree(work);
}
2021-08-06 16:21:24 +08:00
static void once_disable_jump(struct static_key_true *key, struct module *mod)
2015-10-08 01:20:35 +02:00
{
struct once_work *w;
2015-10-08 01:20:35 +02:00
w = kmalloc_obj(*w, GFP_ATOMIC);
2015-10-08 01:20:35 +02:00
if (!w)
return;
INIT_WORK(&w->work, once_deferred);
2015-10-08 01:20:35 +02:00
w->key = key;
2021-08-06 16:21:24 +08:00
w->module = mod;
__module_get(mod);
2015-10-08 01:20:35 +02:00
schedule_work(&w->work);
}
static DEFINE_SPINLOCK(once_lock);
2015-10-08 01:20:35 +02:00
bool __do_once_start(bool *done, unsigned long *flags)
__acquires(once_lock)
{
spin_lock_irqsave(&once_lock, *flags);
2015-10-08 01:20:35 +02:00
if (*done) {
spin_unlock_irqrestore(&once_lock, *flags);
/* Keep sparse happy by restoring an even lock count on
* this lock. In case we return here, we don't call into
* __do_once_done but return early in the DO_ONCE() macro.
*/
__acquire(once_lock);
2015-10-08 01:20:35 +02:00
return false;
}
return true;
}
EXPORT_SYMBOL(__do_once_start);
2017-10-09 14:30:52 -07:00
void __do_once_done(bool *done, struct static_key_true *once_key,
2021-08-06 16:21:24 +08:00
unsigned long *flags, struct module *mod)
__releases(once_lock)
{
*done = true;
spin_unlock_irqrestore(&once_lock, *flags);
2021-08-06 16:21:24 +08:00
once_disable_jump(once_key, mod);
}
EXPORT_SYMBOL(__do_once_done);
static DEFINE_MUTEX(once_mutex);
2022-10-03 20:14:13 +02:00
bool __do_once_sleepable_start(bool *done)
__acquires(once_mutex)
{
mutex_lock(&once_mutex);
if (*done) {
mutex_unlock(&once_mutex);
/* Keep sparse happy by restoring an even lock count on
* this mutex. In case we return here, we don't call into
2022-10-03 20:14:13 +02:00
* __do_once_done but return early in the DO_ONCE_SLEEPABLE() macro.
*/
__acquire(once_mutex);
return false;
}
return true;
}
2022-10-03 20:14:13 +02:00
EXPORT_SYMBOL(__do_once_sleepable_start);
2022-10-03 20:14:13 +02:00
void __do_once_sleepable_done(bool *done, struct static_key_true *once_key,
struct module *mod)
__releases(once_mutex)
{
*done = true;
mutex_unlock(&once_mutex);
static_branch_disable(once_key);
}
2022-10-03 20:14:13 +02:00
EXPORT_SYMBOL(__do_once_sleepable_done);