kernel/printk/printk.c
Source file repositories/reference/linux-study-clean/kernel/printk/printk.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/printk/printk.c- Extension
.c- Size
- 137645 bytes
- Lines
- 5185
- Domain
- Core OS
- Bucket
- Scheduler, Processes, Timers, Sync, And Syscalls
- Inferred role
- Core OS: syscall or user/kernel boundary
- Status
- core implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Defines or participates in a user/kernel boundary; inspect argument validation, copy_from_user/copy_to_user, credentials, and dispatch target.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/kernel.hlinux/mm.hlinux/tty.hlinux/tty_driver.hlinux/console.hlinux/init.hlinux/jiffies.hlinux/nmi.hlinux/module.hlinux/moduleparam.hlinux/delay.hlinux/smp.hlinux/security.hlinux/memblock.hlinux/syscalls.hlinux/syscore_ops.hlinux/vmcore_info.hlinux/ratelimit.hlinux/kmsg_dump.hlinux/syslog.hlinux/cpu.hlinux/rculist.hlinux/poll.hlinux/irq_work.hlinux/ctype.hlinux/uio.hlinux/sched/clock.hlinux/sched/debug.hlinux/sched/task_stack.hlinux/panic.hlinux/uaccess.hasm/sections.h
Detected Declarations
syscall syslogstruct latched_seqstruct devkmsg_userenum devkmsg_log_bitsenum devkmsg_log_masksenum con_msg_format_flagsfunction lockdep_assert_console_list_lock_heldfunction console_srcu_read_lock_is_heldfunction __control_devkmsgfunction control_devkmsgfunction devkmsg_sysctl_set_loglvlfunction console_list_lockfunction console_list_lockfunction for_each_console_srcufunction console_srcu_read_lockfunction __down_trylock_console_semfunction __up_console_semfunction printk_percpu_data_readyfunction latched_seq_writefunction latched_seq_read_nolockfunction log_buf_len_getfunction truncate_msgfunction syslog_action_restrictedfunction check_syslog_permissionsfunction append_charfunction info_print_ext_headerfunction msg_add_ext_textfunction msg_add_dict_textfunction msg_print_ext_bodyfunction __printffunction devkmsg_writefunction devkmsg_readfunction messagesfunction devkmsg_pollfunction devkmsg_openfunction devkmsg_releasefunction log_buf_vmcoreinfo_setupfunction log_buf_len_updatefunction log_buf_len_setupfunction log_buf_add_cpufunction log_buf_add_cpufunction add_to_rbfunction print_log_buf_usage_statsfunction setup_log_buffunction ignore_loglevel_setupfunction suppress_message_printingfunction boot_delay_setupfunction boot_delay_msec
Annotated Snippet
SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
{
return do_syslog(type, buf, len, SYSLOG_FROM_READER);
}
/*
* Special console_lock variants that help to reduce the risk of soft-lockups.
* They allow to pass console_lock to another printk() call using a busy wait.
*/
#ifdef CONFIG_LOCKDEP
static struct lockdep_map console_owner_dep_map = {
.name = "console_owner"
};
#endif
static DEFINE_RAW_SPINLOCK(console_owner_lock);
static struct task_struct *console_owner;
static bool console_waiter;
/**
* console_lock_spinning_enable - mark beginning of code where another
* thread might safely busy wait
*
* This basically converts console_lock into a spinlock. This marks
* the section where the console_lock owner can not sleep, because
* there may be a waiter spinning (like a spinlock). Also it must be
* ready to hand over the lock at the end of the section.
*/
void console_lock_spinning_enable(void)
{
/*
* Do not use spinning in panic(). The panic CPU wants to keep the lock.
* Non-panic CPUs abandon the flush anyway.
*
* Just keep the lockdep annotation. The panic-CPU should avoid
* taking console_owner_lock because it might cause a deadlock.
* This looks like the easiest way how to prevent false lockdep
* reports without handling races a lockless way.
*/
if (panic_in_progress())
goto lockdep;
raw_spin_lock(&console_owner_lock);
console_owner = current;
raw_spin_unlock(&console_owner_lock);
lockdep:
/* The waiter may spin on us after setting console_owner */
spin_acquire(&console_owner_dep_map, 0, 0, _THIS_IP_);
}
/**
* console_lock_spinning_disable_and_check - mark end of code where another
* thread was able to busy wait and check if there is a waiter
* @cookie: cookie returned from console_srcu_read_lock()
*
* This is called at the end of the section where spinning is allowed.
* It has two functions. First, it is a signal that it is no longer
* safe to start busy waiting for the lock. Second, it checks if
* there is a busy waiter and passes the lock rights to her.
*
* Important: Callers lose both the console_lock and the SRCU read lock if
* there was a busy waiter. They must not touch items synchronized by
* console_lock or SRCU read lock in this case.
*
* Return: 1 if the lock rights were passed, 0 otherwise.
*/
int console_lock_spinning_disable_and_check(int cookie)
{
int waiter;
/*
* Ignore spinning waiters during panic() because they might get stopped
* or blocked at any time,
*
* It is safe because nobody is allowed to start spinning during panic
* in the first place. If there has been a waiter then non panic CPUs
* might stay spinning. They would get stopped anyway. The panic context
* will never start spinning and an interrupted spin on panic CPU will
* never continue.
*/
if (panic_in_progress()) {
/* Keep lockdep happy. */
spin_release(&console_owner_dep_map, _THIS_IP_);
return 0;
}
raw_spin_lock(&console_owner_lock);
waiter = READ_ONCE(console_waiter);
Annotation
- Immediate include surface: `linux/kernel.h`, `linux/mm.h`, `linux/tty.h`, `linux/tty_driver.h`, `linux/console.h`, `linux/init.h`, `linux/jiffies.h`, `linux/nmi.h`.
- Detected declarations: `syscall syslog`, `struct latched_seq`, `struct devkmsg_user`, `enum devkmsg_log_bits`, `enum devkmsg_log_masks`, `enum con_msg_format_flags`, `function lockdep_assert_console_list_lock_held`, `function console_srcu_read_lock_is_held`, `function __control_devkmsg`, `function control_devkmsg`.
- Atlas domain: Core OS / Scheduler, Processes, Timers, Sync, And Syscalls.
- Implementation status: core implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- 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.