Skip to content

Commit 85f4f12

Browse files
committed
vsprintf: Replace memory barrier with static_key for random_ptr_key update
Reviewing Tobin's patches for getting pointers out early before entropy has been established, I noticed that there's a lone smp_mb() in the code. As with most lone memory barriers, this one appears to be incorrectly used. We currently basically have this: get_random_bytes(&ptr_key, sizeof(ptr_key)); /* * have_filled_random_ptr_key==true is dependent on get_random_bytes(). * ptr_to_id() needs to see have_filled_random_ptr_key==true * after get_random_bytes() returns. */ smp_mb(); WRITE_ONCE(have_filled_random_ptr_key, true); And later we have: if (unlikely(!have_filled_random_ptr_key)) return string(buf, end, "(ptrval)", spec); /* Missing memory barrier here. */ hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key); As the CPU can perform speculative loads, we could have a situation with the following: CPU0 CPU1 ---- ---- load ptr_key = 0 store ptr_key = random smp_mb() store have_filled_random_ptr_key load have_filled_random_ptr_key = true BAD BAD BAD! (you're so bad!) Because nothing prevents CPU1 from loading ptr_key before loading have_filled_random_ptr_key. But this race is very unlikely, but we can't keep an incorrect smp_mb() in place. Instead, replace the have_filled_random_ptr_key with a static_branch not_filled_random_ptr_key, that is initialized to true and changed to false when we get enough entropy. If the update happens in early boot, the static_key is updated immediately, otherwise it will have to wait till entropy is filled and this happens in an interrupt handler which can't enable a static_key, as that requires a preemptible context. In that case, a work_queue is used to enable it, as entropy already took too long to establish in the first place waiting a little more shouldn't hurt anything. The benefit of using the static key is that the unlikely branch in vsprintf() now becomes a nop. Link: http://lkml.kernel.org/r/[email protected] Cc: [email protected] Fixes: ad67b74 ("printk: hash addresses printed with %p") Acked-by: Linus Torvalds <[email protected]> Signed-off-by: Steven Rostedt (VMware) <[email protected]>
1 parent 67b8d5c commit 85f4f12

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

lib/vsprintf.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,19 +1669,22 @@ char *pointer_string(char *buf, char *end, const void *ptr,
16691669
return number(buf, end, (unsigned long int)ptr, spec);
16701670
}
16711671

1672-
static bool have_filled_random_ptr_key __read_mostly;
1672+
static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
16731673
static siphash_key_t ptr_key __read_mostly;
16741674

1675-
static void fill_random_ptr_key(struct random_ready_callback *unused)
1675+
static void enable_ptr_key_workfn(struct work_struct *work)
16761676
{
16771677
get_random_bytes(&ptr_key, sizeof(ptr_key));
1678-
/*
1679-
* have_filled_random_ptr_key==true is dependent on get_random_bytes().
1680-
* ptr_to_id() needs to see have_filled_random_ptr_key==true
1681-
* after get_random_bytes() returns.
1682-
*/
1683-
smp_mb();
1684-
WRITE_ONCE(have_filled_random_ptr_key, true);
1678+
/* Needs to run from preemptible context */
1679+
static_branch_disable(&not_filled_random_ptr_key);
1680+
}
1681+
1682+
static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
1683+
1684+
static void fill_random_ptr_key(struct random_ready_callback *unused)
1685+
{
1686+
/* This may be in an interrupt handler. */
1687+
queue_work(system_unbound_wq, &enable_ptr_key_work);
16851688
}
16861689

16871690
static struct random_ready_callback random_ready = {
@@ -1695,7 +1698,8 @@ static int __init initialize_ptr_random(void)
16951698
if (!ret) {
16961699
return 0;
16971700
} else if (ret == -EALREADY) {
1698-
fill_random_ptr_key(&random_ready);
1701+
/* This is in preemptible context */
1702+
enable_ptr_key_workfn(&enable_ptr_key_work);
16991703
return 0;
17001704
}
17011705

@@ -1709,7 +1713,7 @@ static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)
17091713
unsigned long hashval;
17101714
const int default_width = 2 * sizeof(ptr);
17111715

1712-
if (unlikely(!have_filled_random_ptr_key)) {
1716+
if (static_branch_unlikely(&not_filled_random_ptr_key)) {
17131717
spec.field_width = default_width;
17141718
/* string length must be less than default_width */
17151719
return string(buf, end, "(ptrval)", spec);

0 commit comments

Comments
 (0)