Skip to content

Commit e4279b5

Browse files
Sebastian Andrzej Siewiorpmladek
authored andcommitted
lib/vsprintf: Remove static_branch_likely() from __ptr_to_hashval().
Using static_branch_likely() to signal that ptr_key has been filled is a bit much given that it is not a fast path. Replace static_branch_likely() with bool for condition and a memory barrier for ptr_key. Suggested-by: Petr Mladek <pmladek@suse.com> Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Reviewed-by: Petr Mladek <pmladek@suse.com> Signed-off-by: Petr Mladek <pmladek@suse.com> Link: https://lore.kernel.org/r/20220927104912.622645-2-bigeasy@linutronix.de
1 parent a1b0275 commit e4279b5

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

lib/vsprintf.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -750,37 +750,34 @@ static int __init debug_boot_weak_hash_enable(char *str)
750750
}
751751
early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
752752

753-
static DEFINE_STATIC_KEY_FALSE(filled_random_ptr_key);
754-
755-
static void enable_ptr_key_workfn(struct work_struct *work)
756-
{
757-
static_branch_enable(&filled_random_ptr_key);
758-
}
753+
static bool filled_random_ptr_key __read_mostly;
759754

760755
/* Maps a pointer to a 32 bit unique identifier. */
761756
static inline int __ptr_to_hashval(const void *ptr, unsigned long *hashval_out)
762757
{
763758
static siphash_key_t ptr_key __read_mostly;
764759
unsigned long hashval;
765760

766-
if (!static_branch_likely(&filled_random_ptr_key)) {
761+
if (!READ_ONCE(filled_random_ptr_key)) {
767762
static bool filled = false;
768763
static DEFINE_SPINLOCK(filling);
769-
static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
770764
unsigned long flags;
771765

772-
if (!system_unbound_wq || !rng_is_initialized() ||
766+
if (!rng_is_initialized() ||
773767
!spin_trylock_irqsave(&filling, flags))
774768
return -EAGAIN;
775769

776770
if (!filled) {
777771
get_random_bytes(&ptr_key, sizeof(ptr_key));
778-
queue_work(system_unbound_wq, &enable_ptr_key_work);
772+
/* Pairs with smp_rmb() before reading ptr_key. */
773+
smp_wmb();
774+
WRITE_ONCE(filled_random_ptr_key, true);
779775
filled = true;
780776
}
781777
spin_unlock_irqrestore(&filling, flags);
782778
}
783-
779+
/* Pairs with smp_wmb() after writing ptr_key. */
780+
smp_rmb();
784781

785782
#ifdef CONFIG_64BIT
786783
hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);

0 commit comments

Comments
 (0)