kernel/printk/nbcon.c
Source file repositories/reference/linux-study-clean/kernel/printk/nbcon.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/printk/nbcon.c- Extension
.c- Size
- 61838 bytes
- Lines
- 2003
- Domain
- Core OS
- Bucket
- Scheduler, Processes, Timers, Sync, And Syscalls
- Inferred role
- Core OS: exported/initcall integration point
- Status
- integration 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.
- 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.
- 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/atomic.hlinux/bug.hlinux/console.hlinux/delay.hlinux/errno.hlinux/export.hlinux/init.hlinux/irqflags.hlinux/kdb.hlinux/kthread.hlinux/minmax.hlinux/panic.hlinux/percpu.hlinux/preempt.hlinux/slab.hlinux/smp.hlinux/stddef.hlinux/string.hlinux/types.hinternal.hprintk_ringbuffer.h
Detected Declarations
function nbcon_state_setfunction nbcon_state_readfunction nbcon_state_try_cmpxchgfunction nbcon_seq_readfunction initfunction nbcon_seq_forcefunction nbcon_context_try_acquire_directfunction reacquirefunction nbcon_waiter_matchesfunction nbcon_context_try_acquire_handoverfunction nbcon_context_try_acquire_directfunction panicfunction nbcon_context_try_acquirefunction nbcon_owner_matchesfunction nbcon_context_releasefunction nbcon_context_can_proceedfunction nbcon_enter_unsafefunction __nbcon_context_update_unsafefunction nbcon_write_context_set_buffunction nbcon_enter_unsafefunction nbcon_exit_unsafefunction nbcon_reacquire_nobuffunction wctxt_load_execution_ctxfunction wctxt_load_execution_ctxfunction write_threadfunction nbcon_emit_next_recordfunction nbcon_kthread_should_wakeupfunction nbcon_kthread_funcfunction nbcon_irq_workfunction rcuwait_has_sleeperfunction nbcon_kthreads_wakefunction nbcon_kthread_stopfunction nbcon_kthread_createfunction nbcon_cpu_emergency_enterfunction nbcon_get_default_priofunction console_is_usablefunction write_threadfunction write_atomicfunction write_atomicfunction prb_read_validfunction write_atomicfunction write_atomicfunction write_atomicfunction nbcon_cpu_emergency_enterfunction nbcon_cpu_emergency_exitfunction nbcon_freefunction nbcon_freefunction activities
Annotated Snippet
__u64seq_to_ulseq(new_seq))) {
ctxt->seq = new_seq;
} else {
ctxt->seq = nbcon_seq_read(con);
}
}
/**
* nbcon_context_try_acquire_direct - Try to acquire directly
* @ctxt: The context of the caller
* @cur: The current console state
* @is_reacquire: This acquire is a reacquire
*
* Acquire the console when it is released. Also acquire the console when
* the current owner has a lower priority and the console is in a safe state.
*
* Return: 0 on success. Otherwise, an error code on failure. Also @cur
* is updated to the latest state when failed to modify it.
*
* Errors:
*
* -EPERM: A panic is in progress and this is neither the panic
* CPU nor is this a reacquire. Or the current owner or
* waiter has the same or higher priority. No acquire
* method can be successful in these cases.
*
* -EBUSY: The current owner has a lower priority but the console
* in an unsafe state. The caller should try using
* the handover acquire method.
*/
static int nbcon_context_try_acquire_direct(struct nbcon_context *ctxt,
struct nbcon_state *cur, bool is_reacquire)
{
unsigned int cpu = smp_processor_id();
struct console *con = ctxt->console;
struct nbcon_state new;
do {
/*
* Panic does not imply that the console is owned. However,
* since all non-panic CPUs are stopped during panic(), it
* is safer to have them avoid gaining console ownership.
*
* One exception is when kdb has locked for printing on this CPU.
*
* Second exception is a reacquire (and an unsafe takeover
* has not previously occurred) then it is allowed to attempt
* a direct acquire in panic. This gives console drivers an
* opportunity to perform any necessary cleanup if they were
* interrupted by the panic CPU while printing.
*/
if (panic_on_other_cpu() &&
!kdb_printf_on_this_cpu() &&
(!is_reacquire || cur->unsafe_takeover)) {
return -EPERM;
}
if (ctxt->prio <= cur->prio || ctxt->prio <= cur->req_prio)
return -EPERM;
if (cur->unsafe)
return -EBUSY;
/*
* The console should never be safe for a direct acquire
* if an unsafe hostile takeover has ever happened.
*/
WARN_ON_ONCE(cur->unsafe_takeover);
new.atom = cur->atom;
new.prio = ctxt->prio;
new.req_prio = NBCON_PRIO_NONE;
new.unsafe = cur->unsafe_takeover;
new.cpu = cpu;
} while (!nbcon_state_try_cmpxchg(con, cur, &new));
return 0;
}
static bool nbcon_waiter_matches(struct nbcon_state *cur, int expected_prio)
{
/*
* The request context is well defined by the @req_prio because:
*
* - Only a context with a priority higher than the owner can become
* a waiter.
* - Only a context with a priority higher than the waiter can
* directly take over the request.
* - There are only three priorities.
Annotation
- Immediate include surface: `linux/atomic.h`, `linux/bug.h`, `linux/console.h`, `linux/delay.h`, `linux/errno.h`, `linux/export.h`, `linux/init.h`, `linux/irqflags.h`.
- Detected declarations: `function nbcon_state_set`, `function nbcon_state_read`, `function nbcon_state_try_cmpxchg`, `function nbcon_seq_read`, `function init`, `function nbcon_seq_force`, `function nbcon_context_try_acquire_direct`, `function reacquire`, `function nbcon_waiter_matches`, `function nbcon_context_try_acquire_handover`.
- Atlas domain: Core OS / Scheduler, Processes, Timers, Sync, And Syscalls.
- 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.