kernel_optimize_test/lib/once.c
Eric Biggers cf4c950b87 once: switch to new jump label API
Switch the DO_ONCE() macro from the deprecated jump label API to the new
one.  The new one is more readable, and for DO_ONCE() it also makes the
generated code more icache-friendly: now the one-time initialization
code is placed out-of-line at the jump target, rather than at the inline
fallthrough case.

Acked-by: Hannes Frederic Sowa <hannes@stressinduktion.org>
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-10-09 20:26:23 -07:00

63 lines
1.3 KiB
C

#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/once.h>
#include <linux/random.h>
struct once_work {
struct work_struct work;
struct static_key_true *key;
};
static void once_deferred(struct work_struct *w)
{
struct once_work *work;
work = container_of(w, struct once_work, work);
BUG_ON(!static_key_enabled(work->key));
static_branch_disable(work->key);
kfree(work);
}
static void once_disable_jump(struct static_key_true *key)
{
struct once_work *w;
w = kmalloc(sizeof(*w), GFP_ATOMIC);
if (!w)
return;
INIT_WORK(&w->work, once_deferred);
w->key = key;
schedule_work(&w->work);
}
static DEFINE_SPINLOCK(once_lock);
bool __do_once_start(bool *done, unsigned long *flags)
__acquires(once_lock)
{
spin_lock_irqsave(&once_lock, *flags);
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);
return false;
}
return true;
}
EXPORT_SYMBOL(__do_once_start);
void __do_once_done(bool *done, struct static_key_true *once_key,
unsigned long *flags)
__releases(once_lock)
{
*done = true;
spin_unlock_irqrestore(&once_lock, *flags);
once_disable_jump(once_key);
}
EXPORT_SYMBOL(__do_once_done);