Skip to content

Commit 9073dac

Browse files
geertupmladek
authored andcommitted
lib/vsprintf: Prepare for more general use of ptr_to_id()
Move the function and its dependencies up so it can be called from special pointer type formatting routines. Link: http://lkml.kernel.org/r/20181011084249.4520-2-geert+renesas@glider.be To: "Tobin C . Harding" <me@tobin.cc> To: Andrew Morton <akpm@linux-foundation.org> To: Jonathan Corbet <corbet@lwn.net> Cc: linux-doc@vger.kernel.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> [pmladek@suse.com: Split into separate patch] Signed-off-by: Petr Mladek <pmladek@suse.com>
1 parent f31b224 commit 9073dac

File tree

1 file changed

+103
-103
lines changed

1 file changed

+103
-103
lines changed

lib/vsprintf.c

Lines changed: 103 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,109 @@ char *string(char *buf, char *end, const char *s, struct printf_spec spec)
612612
return widen_string(buf, len, end, spec);
613613
}
614614

615+
static noinline_for_stack
616+
char *pointer_string(char *buf, char *end, const void *ptr,
617+
struct printf_spec spec)
618+
{
619+
spec.base = 16;
620+
spec.flags |= SMALL;
621+
if (spec.field_width == -1) {
622+
spec.field_width = 2 * sizeof(ptr);
623+
spec.flags |= ZEROPAD;
624+
}
625+
626+
return number(buf, end, (unsigned long int)ptr, spec);
627+
}
628+
629+
/* Make pointers available for printing early in the boot sequence. */
630+
static int debug_boot_weak_hash __ro_after_init;
631+
632+
static int __init debug_boot_weak_hash_enable(char *str)
633+
{
634+
debug_boot_weak_hash = 1;
635+
pr_info("debug_boot_weak_hash enabled\n");
636+
return 0;
637+
}
638+
early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
639+
640+
static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
641+
static siphash_key_t ptr_key __read_mostly;
642+
643+
static void enable_ptr_key_workfn(struct work_struct *work)
644+
{
645+
get_random_bytes(&ptr_key, sizeof(ptr_key));
646+
/* Needs to run from preemptible context */
647+
static_branch_disable(&not_filled_random_ptr_key);
648+
}
649+
650+
static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
651+
652+
static void fill_random_ptr_key(struct random_ready_callback *unused)
653+
{
654+
/* This may be in an interrupt handler. */
655+
queue_work(system_unbound_wq, &enable_ptr_key_work);
656+
}
657+
658+
static struct random_ready_callback random_ready = {
659+
.func = fill_random_ptr_key
660+
};
661+
662+
static int __init initialize_ptr_random(void)
663+
{
664+
int key_size = sizeof(ptr_key);
665+
int ret;
666+
667+
/* Use hw RNG if available. */
668+
if (get_random_bytes_arch(&ptr_key, key_size) == key_size) {
669+
static_branch_disable(&not_filled_random_ptr_key);
670+
return 0;
671+
}
672+
673+
ret = add_random_ready_callback(&random_ready);
674+
if (!ret) {
675+
return 0;
676+
} else if (ret == -EALREADY) {
677+
/* This is in preemptible context */
678+
enable_ptr_key_workfn(&enable_ptr_key_work);
679+
return 0;
680+
}
681+
682+
return ret;
683+
}
684+
early_initcall(initialize_ptr_random);
685+
686+
/* Maps a pointer to a 32 bit unique identifier. */
687+
static char *ptr_to_id(char *buf, char *end, const void *ptr,
688+
struct printf_spec spec)
689+
{
690+
const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
691+
unsigned long hashval;
692+
693+
/* When debugging early boot use non-cryptographically secure hash. */
694+
if (unlikely(debug_boot_weak_hash)) {
695+
hashval = hash_long((unsigned long)ptr, 32);
696+
return pointer_string(buf, end, (const void *)hashval, spec);
697+
}
698+
699+
if (static_branch_unlikely(&not_filled_random_ptr_key)) {
700+
spec.field_width = 2 * sizeof(ptr);
701+
/* string length must be less than default_width */
702+
return string(buf, end, str, spec);
703+
}
704+
705+
#ifdef CONFIG_64BIT
706+
hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
707+
/*
708+
* Mask off the first 32 bits, this makes explicit that we have
709+
* modified the address (and 32 bits is plenty for a unique ID).
710+
*/
711+
hashval = hashval & 0xffffffff;
712+
#else
713+
hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
714+
#endif
715+
return pointer_string(buf, end, (const void *)hashval, spec);
716+
}
717+
615718
static noinline_for_stack
616719
char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
617720
const char *fmt)
@@ -1357,20 +1460,6 @@ char *uuid_string(char *buf, char *end, const u8 *addr,
13571460
return string(buf, end, uuid, spec);
13581461
}
13591462

1360-
static noinline_for_stack
1361-
char *pointer_string(char *buf, char *end, const void *ptr,
1362-
struct printf_spec spec)
1363-
{
1364-
spec.base = 16;
1365-
spec.flags |= SMALL;
1366-
if (spec.field_width == -1) {
1367-
spec.field_width = 2 * sizeof(ptr);
1368-
spec.flags |= ZEROPAD;
1369-
}
1370-
1371-
return number(buf, end, (unsigned long int)ptr, spec);
1372-
}
1373-
13741463
int kptr_restrict __read_mostly;
13751464

13761465
static noinline_for_stack
@@ -1651,95 +1740,6 @@ char *device_node_string(char *buf, char *end, struct device_node *dn,
16511740
return widen_string(buf, buf - buf_start, end, spec);
16521741
}
16531742

1654-
/* Make pointers available for printing early in the boot sequence. */
1655-
static int debug_boot_weak_hash __ro_after_init;
1656-
1657-
static int __init debug_boot_weak_hash_enable(char *str)
1658-
{
1659-
debug_boot_weak_hash = 1;
1660-
pr_info("debug_boot_weak_hash enabled\n");
1661-
return 0;
1662-
}
1663-
early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
1664-
1665-
static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
1666-
static siphash_key_t ptr_key __read_mostly;
1667-
1668-
static void enable_ptr_key_workfn(struct work_struct *work)
1669-
{
1670-
get_random_bytes(&ptr_key, sizeof(ptr_key));
1671-
/* Needs to run from preemptible context */
1672-
static_branch_disable(&not_filled_random_ptr_key);
1673-
}
1674-
1675-
static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
1676-
1677-
static void fill_random_ptr_key(struct random_ready_callback *unused)
1678-
{
1679-
/* This may be in an interrupt handler. */
1680-
queue_work(system_unbound_wq, &enable_ptr_key_work);
1681-
}
1682-
1683-
static struct random_ready_callback random_ready = {
1684-
.func = fill_random_ptr_key
1685-
};
1686-
1687-
static int __init initialize_ptr_random(void)
1688-
{
1689-
int key_size = sizeof(ptr_key);
1690-
int ret;
1691-
1692-
/* Use hw RNG if available. */
1693-
if (get_random_bytes_arch(&ptr_key, key_size) == key_size) {
1694-
static_branch_disable(&not_filled_random_ptr_key);
1695-
return 0;
1696-
}
1697-
1698-
ret = add_random_ready_callback(&random_ready);
1699-
if (!ret) {
1700-
return 0;
1701-
} else if (ret == -EALREADY) {
1702-
/* This is in preemptible context */
1703-
enable_ptr_key_workfn(&enable_ptr_key_work);
1704-
return 0;
1705-
}
1706-
1707-
return ret;
1708-
}
1709-
early_initcall(initialize_ptr_random);
1710-
1711-
/* Maps a pointer to a 32 bit unique identifier. */
1712-
static char *ptr_to_id(char *buf, char *end, const void *ptr,
1713-
struct printf_spec spec)
1714-
{
1715-
const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
1716-
unsigned long hashval;
1717-
1718-
/* When debugging early boot use non-cryptographically secure hash. */
1719-
if (unlikely(debug_boot_weak_hash)) {
1720-
hashval = hash_long((unsigned long)ptr, 32);
1721-
return pointer_string(buf, end, (const void *)hashval, spec);
1722-
}
1723-
1724-
if (static_branch_unlikely(&not_filled_random_ptr_key)) {
1725-
spec.field_width = 2 * sizeof(ptr);
1726-
/* string length must be less than default_width */
1727-
return string(buf, end, str, spec);
1728-
}
1729-
1730-
#ifdef CONFIG_64BIT
1731-
hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
1732-
/*
1733-
* Mask off the first 32 bits, this makes explicit that we have
1734-
* modified the address (and 32 bits is plenty for a unique ID).
1735-
*/
1736-
hashval = hashval & 0xffffffff;
1737-
#else
1738-
hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
1739-
#endif
1740-
return pointer_string(buf, end, (const void *)hashval, spec);
1741-
}
1742-
17431743
/*
17441744
* Show a '%p' thing. A kernel extension is that the '%p' is followed
17451745
* by an extra set of alphanumeric characters that are extended format

0 commit comments

Comments
 (0)