Skip to content

Commit b0af4bc

Browse files
KAGA-KOKOgregkh
authored andcommitted
serial: core: Provide port lock wrappers
When a serial port is used for kernel console output, then all modifications to the UART registers which are done from other contexts, e.g. getty, termios, are interference points for the kernel console. So far this has been ignored and the printk output is based on the principle of hope. The rework of the console infrastructure which aims to support threaded and atomic consoles, requires to mark sections which modify the UART registers as unsafe. This allows the atomic write function to make informed decisions and eventually to restore operational state. It also allows to prevent the regular UART code from modifying UART registers while printk output is in progress. All modifications of UART registers are guarded by the UART port lock, which provides an obvious synchronization point with the console infrastructure. Provide wrapper functions for spin_[un]lock*(port->lock) invocations so that the console mechanics can be applied later on at a single place and does not require to copy the same logic all over the drivers. Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Ilpo Järvinen <[email protected]> Signed-off-by: John Ogness <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 11e7f27 commit b0af4bc

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

include/linux/serial_core.h

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,85 @@ struct uart_port {
588588
void *private_data; /* generic platform data pointer */
589589
};
590590

591+
/**
592+
* uart_port_lock - Lock the UART port
593+
* @up: Pointer to UART port structure
594+
*/
595+
static inline void uart_port_lock(struct uart_port *up)
596+
{
597+
spin_lock(&up->lock);
598+
}
599+
600+
/**
601+
* uart_port_lock_irq - Lock the UART port and disable interrupts
602+
* @up: Pointer to UART port structure
603+
*/
604+
static inline void uart_port_lock_irq(struct uart_port *up)
605+
{
606+
spin_lock_irq(&up->lock);
607+
}
608+
609+
/**
610+
* uart_port_lock_irqsave - Lock the UART port, save and disable interrupts
611+
* @up: Pointer to UART port structure
612+
* @flags: Pointer to interrupt flags storage
613+
*/
614+
static inline void uart_port_lock_irqsave(struct uart_port *up, unsigned long *flags)
615+
{
616+
spin_lock_irqsave(&up->lock, *flags);
617+
}
618+
619+
/**
620+
* uart_port_trylock - Try to lock the UART port
621+
* @up: Pointer to UART port structure
622+
*
623+
* Returns: True if lock was acquired, false otherwise
624+
*/
625+
static inline bool uart_port_trylock(struct uart_port *up)
626+
{
627+
return spin_trylock(&up->lock);
628+
}
629+
630+
/**
631+
* uart_port_trylock_irqsave - Try to lock the UART port, save and disable interrupts
632+
* @up: Pointer to UART port structure
633+
* @flags: Pointer to interrupt flags storage
634+
*
635+
* Returns: True if lock was acquired, false otherwise
636+
*/
637+
static inline bool uart_port_trylock_irqsave(struct uart_port *up, unsigned long *flags)
638+
{
639+
return spin_trylock_irqsave(&up->lock, *flags);
640+
}
641+
642+
/**
643+
* uart_port_unlock - Unlock the UART port
644+
* @up: Pointer to UART port structure
645+
*/
646+
static inline void uart_port_unlock(struct uart_port *up)
647+
{
648+
spin_unlock(&up->lock);
649+
}
650+
651+
/**
652+
* uart_port_unlock_irq - Unlock the UART port and re-enable interrupts
653+
* @up: Pointer to UART port structure
654+
*/
655+
static inline void uart_port_unlock_irq(struct uart_port *up)
656+
{
657+
spin_unlock_irq(&up->lock);
658+
}
659+
660+
/**
661+
* uart_port_lock_irqrestore - Unlock the UART port, restore interrupts
662+
* @up: Pointer to UART port structure
663+
* @flags: The saved interrupt flags for restore
664+
*/
665+
static inline void uart_port_unlock_irqrestore(struct uart_port *up, unsigned long flags)
666+
{
667+
spin_unlock_irqrestore(&up->lock, flags);
668+
}
669+
591670
static inline int serial_port_in(struct uart_port *up, int offset)
592671
{
593672
return up->serial_in(up, offset);

0 commit comments

Comments
 (0)