Skip to content

Commit 4b08d9e

Browse files
KAGA-KOKOpmladek
authored andcommitted
printk: nbcon: Add ownership state functions
Provide functions that are related to the safe handover mechanism and allow console drivers to dynamically specify unsafe regions: - nbcon_context_can_proceed() Invoked by a console owner to check whether a handover request is pending or whether the console has been taken over by another context. If a handover request is pending, this function will also perform the handover, thus cancelling its own ownership. - nbcon_context_enter_unsafe()/nbcon_context_exit_unsafe() Invoked by a console owner to denote that the driver is about to enter or leave a critical region where a take over is unsafe. The exit variant is the point where the current owner releases the lock for a higher priority context which asked for the friendly handover. The unsafe state is stored in the console state and allows a new context to make informed decisions whether to attempt a takeover of such a console. The unsafe state is also available to the driver so that it can make informed decisions about the required actions and possibly take a special emergency path. Co-developed-by: John Ogness <john.ogness@linutronix.de> Signed-off-by: John Ogness <john.ogness@linutronix.de> Signed-off-by: Thomas Gleixner (Intel) <tglx@linutronix.de> Reviewed-by: Petr Mladek <pmladek@suse.com> Signed-off-by: Petr Mladek <pmladek@suse.com> Link: https://lore.kernel.org/r/20230916192007.608398-6-john.ogness@linutronix.de
1 parent 5634c90 commit 4b08d9e

File tree

1 file changed

+122
-1
lines changed

1 file changed

+122
-1
lines changed

kernel/printk/nbcon.c

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,6 @@ static bool nbcon_owner_matches(struct nbcon_state *cur, int expected_cpu,
537537
* nbcon_context_release - Release the console
538538
* @ctxt: The nbcon context from nbcon_context_try_acquire()
539539
*/
540-
__maybe_unused
541540
static void nbcon_context_release(struct nbcon_context *ctxt)
542541
{
543542
unsigned int cpu = smp_processor_id();
@@ -565,6 +564,128 @@ static void nbcon_context_release(struct nbcon_context *ctxt)
565564
ctxt->pbufs = NULL;
566565
}
567566

567+
/**
568+
* nbcon_context_can_proceed - Check whether ownership can proceed
569+
* @ctxt: The nbcon context from nbcon_context_try_acquire()
570+
* @cur: The current console state
571+
*
572+
* Return: True if this context still owns the console. False if
573+
* ownership was handed over or taken.
574+
*
575+
* Must be invoked when entering the unsafe state to make sure that it still
576+
* owns the lock. Also must be invoked when exiting the unsafe context
577+
* to eventually free the lock for a higher priority context which asked
578+
* for the friendly handover.
579+
*
580+
* It can be called inside an unsafe section when the console is just
581+
* temporary in safe state instead of exiting and entering the unsafe
582+
* state.
583+
*
584+
* Also it can be called in the safe context before doing an expensive
585+
* safe operation. It does not make sense to do the operation when
586+
* a higher priority context took the lock.
587+
*
588+
* When this function returns false then the calling context no longer owns
589+
* the console and is no longer allowed to go forward. In this case it must
590+
* back out immediately and carefully. The buffer content is also no longer
591+
* trusted since it no longer belongs to the calling context.
592+
*/
593+
static bool nbcon_context_can_proceed(struct nbcon_context *ctxt, struct nbcon_state *cur)
594+
{
595+
unsigned int cpu = smp_processor_id();
596+
597+
/* Make sure this context still owns the console. */
598+
if (!nbcon_owner_matches(cur, cpu, ctxt->prio))
599+
return false;
600+
601+
/* The console owner can proceed if there is no waiter. */
602+
if (cur->req_prio == NBCON_PRIO_NONE)
603+
return true;
604+
605+
/*
606+
* A console owner within an unsafe region is always allowed to
607+
* proceed, even if there are waiters. It can perform a handover
608+
* when exiting the unsafe region. Otherwise the waiter will
609+
* need to perform an unsafe hostile takeover.
610+
*/
611+
if (cur->unsafe)
612+
return true;
613+
614+
/* Waiters always have higher priorities than owners. */
615+
WARN_ON_ONCE(cur->req_prio <= cur->prio);
616+
617+
/*
618+
* Having a safe point for take over and eventually a few
619+
* duplicated characters or a full line is way better than a
620+
* hostile takeover. Post processing can take care of the garbage.
621+
* Release and hand over.
622+
*/
623+
nbcon_context_release(ctxt);
624+
625+
/*
626+
* It is not clear whether the waiter really took over ownership. The
627+
* outermost callsite must make the final decision whether console
628+
* ownership is needed for it to proceed. If yes, it must reacquire
629+
* ownership (possibly hostile) before carefully proceeding.
630+
*
631+
* The calling context no longer owns the console so go back all the
632+
* way instead of trying to implement reacquire heuristics in tons of
633+
* places.
634+
*/
635+
return false;
636+
}
637+
638+
#define nbcon_context_enter_unsafe(c) __nbcon_context_update_unsafe(c, true)
639+
#define nbcon_context_exit_unsafe(c) __nbcon_context_update_unsafe(c, false)
640+
641+
/**
642+
* __nbcon_context_update_unsafe - Update the unsafe bit in @con->nbcon_state
643+
* @ctxt: The nbcon context from nbcon_context_try_acquire()
644+
* @unsafe: The new value for the unsafe bit
645+
*
646+
* Return: True if the unsafe state was updated and this context still
647+
* owns the console. Otherwise false if ownership was handed
648+
* over or taken.
649+
*
650+
* This function allows console owners to modify the unsafe status of the
651+
* console.
652+
*
653+
* When this function returns false then the calling context no longer owns
654+
* the console and is no longer allowed to go forward. In this case it must
655+
* back out immediately and carefully. The buffer content is also no longer
656+
* trusted since it no longer belongs to the calling context.
657+
*
658+
* Internal helper to avoid duplicated code.
659+
*/
660+
__maybe_unused
661+
static bool __nbcon_context_update_unsafe(struct nbcon_context *ctxt, bool unsafe)
662+
{
663+
struct console *con = ctxt->console;
664+
struct nbcon_state cur;
665+
struct nbcon_state new;
666+
667+
nbcon_state_read(con, &cur);
668+
669+
do {
670+
/*
671+
* The unsafe bit must not be cleared if an
672+
* unsafe hostile takeover has occurred.
673+
*/
674+
if (!unsafe && cur.unsafe_takeover)
675+
goto out;
676+
677+
if (!nbcon_context_can_proceed(ctxt, &cur))
678+
return false;
679+
680+
new.atom = cur.atom;
681+
new.unsafe = unsafe;
682+
} while (!nbcon_state_try_cmpxchg(con, &cur, &new));
683+
684+
cur.atom = new.atom;
685+
out:
686+
return nbcon_context_can_proceed(ctxt, &cur);
687+
}
688+
568689
/**
569690
* nbcon_alloc - Allocate buffers needed by the nbcon console
570691
* @con: Console to allocate buffers for

0 commit comments

Comments
 (0)