Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

printk: nbcon: Introduce KDB helpers

These helpers will be used when calling console->write_atomic on
KDB code in the next patch. It's basically the same implementation
as nbcon_device_try_acquire, but using NBCON_PRIO_EMERGENCY when
acquiring the context.

If the acquire succeeds, the message and message length are assigned to
nbcon_write_context so ->write_atomic can print the message.

After release try to flush the console since there may be a backlog of
messages in the ringbuffer. The kthread console printers do not get a
chance to run while kdb is active.

Reviewed-by: Petr Mladek <pmladek@suse.com>
Reviewed-by: John Ogness <john.ogness@linutronix.de>
Signed-off-by: Marcos Paulo de Souza <mpdesouza@suse.com>
Link: https://patch.msgid.link/20251016-nbcon-kgdboc-v6-2-866aac60a80e@suse.com
Signed-off-by: Petr Mladek <pmladek@suse.com>

authored by

Marcos Paulo de Souza and committed by
Petr Mladek
49f7d305 4da42aaa

+67
+6
include/linux/console.h
··· 606 606 extern bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt); 607 607 extern bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt); 608 608 extern void nbcon_reacquire_nobuf(struct nbcon_write_context *wctxt); 609 + extern bool nbcon_kdb_try_acquire(struct console *con, 610 + struct nbcon_write_context *wctxt); 611 + extern void nbcon_kdb_release(struct nbcon_write_context *wctxt); 609 612 610 613 /* 611 614 * Check if the given console is currently capable and allowed to print ··· 658 655 static inline bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt) { return false; } 659 656 static inline bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt) { return false; } 660 657 static inline void nbcon_reacquire_nobuf(struct nbcon_write_context *wctxt) { } 658 + static inline bool nbcon_kdb_try_acquire(struct console *con, 659 + struct nbcon_write_context *wctxt) { return false; } 660 + static inline void nbcon_kdb_release(struct console *con) { } 661 661 static inline bool console_is_usable(struct console *con, short flags, 662 662 bool use_atomic) { return false; } 663 663 #endif
+61
kernel/printk/nbcon.c
··· 1855 1855 console_srcu_read_unlock(cookie); 1856 1856 } 1857 1857 EXPORT_SYMBOL_GPL(nbcon_device_release); 1858 + 1859 + /** 1860 + * nbcon_kdb_try_acquire - Try to acquire nbcon console and enter unsafe 1861 + * section 1862 + * @con: The nbcon console to acquire 1863 + * @wctxt: The nbcon write context to be used on success 1864 + * 1865 + * Context: Under console_srcu_read_lock() for emitting a single kdb message 1866 + * using the given con->write_atomic() callback. Can be called 1867 + * only when the console is usable at the moment. 1868 + * 1869 + * Return: True if the console was acquired. False otherwise. 1870 + * 1871 + * kdb emits messages on consoles registered for printk() without 1872 + * storing them into the ring buffer. It has to acquire the console 1873 + * ownerhip so that it could call con->write_atomic() callback a safe way. 1874 + * 1875 + * This function acquires the nbcon console using priority NBCON_PRIO_EMERGENCY 1876 + * and marks it unsafe for handover/takeover. 1877 + */ 1878 + bool nbcon_kdb_try_acquire(struct console *con, 1879 + struct nbcon_write_context *wctxt) 1880 + { 1881 + struct nbcon_context *ctxt = &ACCESS_PRIVATE(wctxt, ctxt); 1882 + 1883 + memset(ctxt, 0, sizeof(*ctxt)); 1884 + ctxt->console = con; 1885 + ctxt->prio = NBCON_PRIO_EMERGENCY; 1886 + 1887 + if (!nbcon_context_try_acquire(ctxt, false)) 1888 + return false; 1889 + 1890 + if (!nbcon_context_enter_unsafe(ctxt)) 1891 + return false; 1892 + 1893 + return true; 1894 + } 1895 + 1896 + /** 1897 + * nbcon_kdb_release - Exit unsafe section and release the nbcon console 1898 + * 1899 + * @wctxt: The nbcon write context initialized by a successful 1900 + * nbcon_kdb_try_acquire() 1901 + */ 1902 + void nbcon_kdb_release(struct nbcon_write_context *wctxt) 1903 + { 1904 + struct nbcon_context *ctxt = &ACCESS_PRIVATE(wctxt, ctxt); 1905 + 1906 + if (!nbcon_context_exit_unsafe(ctxt)) 1907 + return; 1908 + 1909 + nbcon_context_release(ctxt); 1910 + 1911 + /* 1912 + * Flush any new printk() messages added when the console was blocked. 1913 + * Only the console used by the given write context was blocked. 1914 + * The console was locked only when the write_atomic() callback 1915 + * was usable. 1916 + */ 1917 + __nbcon_atomic_flush_pending_con(ctxt->console, prb_next_reserve_seq(prb), false); 1918 + }