lib/vsprintf.c
Source file repositories/reference/linux-study-clean/lib/vsprintf.c
File Facts
- System
- Linux kernel
- Corpus path
lib/vsprintf.c- Extension
.c- Size
- 92345 bytes
- Lines
- 3747
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/stdarg.hlinux/build_bug.hlinux/clk.hlinux/clk-provider.hlinux/errname.hlinux/module.hlinux/types.hlinux/string.hlinux/ctype.hlinux/hex.hlinux/kernel.hlinux/kallsyms.hlinux/math64.hlinux/uaccess.hlinux/ioport.hlinux/dcache.hlinux/cred.hlinux/rtc.hlinux/sprintf.hlinux/time.hlinux/uuid.hlinux/of.hnet/addrconf.hlinux/siphash.hlinux/compiler.hlinux/property.hlinux/notifier.hlinux/blkdev.h../mm/internal.hasm/page.hasm/byteorder.hlinux/unaligned.h
Detected Declarations
struct printf_specstruct page_flags_fieldsstruct fmtenum hash_pointers_policyenum format_statefunction simple_strntoullfunction simple_strtoullfunction simple_strtoulfunction simple_strtolfunction simple_strntollfunction simple_strtollfunction skip_atoifunction put_dec_full4function put_dec_helper4function num_to_strfunction move_rightfunction check_pointerfunction debug_boot_weak_hash_enablefunction fill_ptr_keyfunction vsprintf_init_hashvalfunction __ptr_to_hashvalfunction ptr_to_hashvalfunction for_each_set_bitrangefunction of_property_for_each_stringfunction hash_pointers_finalizefunction hash_pointers_mode_parsefunction no_hash_pointers_enablefunction documentationfunction spec_flagfunction set_field_widthfunction set_precisionfunction readfunction convert_num_specfunction pointerfunction vsnprintffunction vsnprintffunction scnprintffunction vsnprintffunction vsnprintffunction vbin_printffunction bstr_printffunction vsscanffunction sscanfmodule init vsprintf_init_hashvalexport simple_strtoullexport simple_strtoulexport simple_strtolexport simple_strtoll
Annotated Snippet
subsys_initcall(vsprintf_init_hashval)
/* Maps a pointer to a 32 bit unique identifier. */
static inline int __ptr_to_hashval(const void *ptr, unsigned long *hashval_out)
{
unsigned long hashval;
if (!READ_ONCE(filled_random_ptr_key))
return -EBUSY;
/* Pairs with smp_wmb() after writing ptr_key. */
smp_rmb();
#ifdef CONFIG_64BIT
hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
/*
* Mask off the first 32 bits, this makes explicit that we have
* modified the address (and 32 bits is plenty for a unique ID).
*/
hashval = hashval & 0xffffffff;
#else
hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
#endif
*hashval_out = hashval;
return 0;
}
int ptr_to_hashval(const void *ptr, unsigned long *hashval_out)
{
return __ptr_to_hashval(ptr, hashval_out);
}
static char *ptr_to_id(char *buf, char *end, const void *ptr,
struct printf_spec spec)
{
const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
unsigned long hashval;
int ret;
/*
* Print the real pointer value for NULL and error pointers,
* as they are not actual addresses.
*/
if (IS_ERR_OR_NULL(ptr))
return pointer_string(buf, end, ptr, spec);
/* When debugging early boot use non-cryptographically secure hash. */
if (unlikely(debug_boot_weak_hash)) {
hashval = hash_long((unsigned long)ptr, 32);
return pointer_string(buf, end, (const void *)hashval, spec);
}
ret = __ptr_to_hashval(ptr, &hashval);
if (ret) {
spec.field_width = 2 * sizeof(ptr);
/* string length must be less than default_width */
return error_string(buf, end, str, spec);
}
return pointer_string(buf, end, (const void *)hashval, spec);
}
static char *default_pointer(char *buf, char *end, const void *ptr,
struct printf_spec spec)
{
/*
* default is to _not_ leak addresses, so hash before printing,
* unless no_hash_pointers is specified on the command line.
*/
if (unlikely(no_hash_pointers))
return pointer_string(buf, end, ptr, spec);
return ptr_to_id(buf, end, ptr, spec);
}
int kptr_restrict __read_mostly;
EXPORT_SYMBOL_FOR_MODULES(kptr_restrict, "printf_kunit");
static noinline_for_stack
char *restricted_pointer(char *buf, char *end, const void *ptr,
struct printf_spec spec)
{
switch (kptr_restrict) {
case 0:
/* Handle as %p, hash and do _not_ leak addresses. */
return default_pointer(buf, end, ptr, spec);
case 1: {
const struct cred *cred;
/*
Annotation
- Immediate include surface: `linux/stdarg.h`, `linux/build_bug.h`, `linux/clk.h`, `linux/clk-provider.h`, `linux/errname.h`, `linux/module.h`, `linux/types.h`, `linux/string.h`.
- Detected declarations: `struct printf_spec`, `struct page_flags_fields`, `struct fmt`, `enum hash_pointers_policy`, `enum format_state`, `function simple_strntoull`, `function simple_strtoull`, `function simple_strtoul`, `function simple_strtol`, `function simple_strntoll`.
- Atlas domain: Kernel Services / lib.
- Implementation status: integration implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.