@@ -666,11 +666,8 @@ static ssize_t msg_print_ext_header(char *buf, size_t size,
666666 * better readable output. 'c' in the record flags mark the first
667667 * fragment of a line, '+' the following.
668668 */
669- if (msg -> flags & LOG_CONT && !(prev_flags & LOG_CONT ))
670- cont = 'c' ;
671- else if ((msg -> flags & LOG_CONT ) ||
672- ((prev_flags & LOG_CONT ) && !(msg -> flags & LOG_PREFIX )))
673- cont = '+' ;
669+ if (msg -> flags & LOG_CONT )
670+ cont = (prev_flags & LOG_CONT ) ? '+' : 'c' ;
674671
675672 return scnprintf (buf , size , "%u,%llu,%llu,%c;" ,
676673 (msg -> facility << 3 ) | msg -> level , seq , ts_usec , cont );
@@ -797,6 +794,8 @@ static ssize_t devkmsg_write(struct kiocb *iocb, struct iov_iter *from)
797794 return ret ;
798795}
799796
797+ static void cont_flush (void );
798+
800799static ssize_t devkmsg_read (struct file * file , char __user * buf ,
801800 size_t count , loff_t * ppos )
802801{
@@ -812,6 +811,7 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
812811 if (ret )
813812 return ret ;
814813 raw_spin_lock_irq (& logbuf_lock );
814+ cont_flush ();
815815 while (user -> seq == log_next_seq ) {
816816 if (file -> f_flags & O_NONBLOCK ) {
817817 ret = - EAGAIN ;
@@ -874,6 +874,7 @@ static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)
874874 return - ESPIPE ;
875875
876876 raw_spin_lock_irq (& logbuf_lock );
877+ cont_flush ();
877878 switch (whence ) {
878879 case SEEK_SET :
879880 /* the first record */
@@ -912,6 +913,7 @@ static unsigned int devkmsg_poll(struct file *file, poll_table *wait)
912913 poll_wait (file , & log_wait , wait );
913914
914915 raw_spin_lock_irq (& logbuf_lock );
916+ cont_flush ();
915917 if (user -> seq < log_next_seq ) {
916918 /* return error when data has vanished underneath us */
917919 if (user -> seq < log_first_seq )
@@ -1298,6 +1300,7 @@ static int syslog_print(char __user *buf, int size)
12981300 size_t skip ;
12991301
13001302 raw_spin_lock_irq (& logbuf_lock );
1303+ cont_flush ();
13011304 if (syslog_seq < log_first_seq ) {
13021305 /* messages are gone, move to first one */
13031306 syslog_seq = log_first_seq ;
@@ -1357,6 +1360,7 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
13571360 return - ENOMEM ;
13581361
13591362 raw_spin_lock_irq (& logbuf_lock );
1363+ cont_flush ();
13601364 if (buf ) {
13611365 u64 next_seq ;
13621366 u64 seq ;
@@ -1518,6 +1522,7 @@ int do_syslog(int type, char __user *buf, int len, int source)
15181522 /* Number of chars in the log buffer */
15191523 case SYSLOG_ACTION_SIZE_UNREAD :
15201524 raw_spin_lock_irq (& logbuf_lock );
1525+ cont_flush ();
15211526 if (syslog_seq < log_first_seq ) {
15221527 /* messages are gone, move to first one */
15231528 syslog_seq = log_first_seq ;
@@ -1654,35 +1659,33 @@ static struct cont {
16541659 bool flushed :1 ; /* buffer sealed and committed */
16551660} cont ;
16561661
1657- static void cont_flush (enum log_flags flags )
1662+ static void cont_flush (void )
16581663{
16591664 if (cont .flushed )
16601665 return ;
16611666 if (cont .len == 0 )
16621667 return ;
1663-
16641668 if (cont .cons ) {
16651669 /*
16661670 * If a fragment of this line was directly flushed to the
16671671 * console; wait for the console to pick up the rest of the
16681672 * line. LOG_NOCONS suppresses a duplicated output.
16691673 */
1670- log_store (cont .facility , cont .level , flags | LOG_NOCONS ,
1674+ log_store (cont .facility , cont .level , cont . flags | LOG_NOCONS ,
16711675 cont .ts_nsec , NULL , 0 , cont .buf , cont .len );
1672- cont .flags = flags ;
16731676 cont .flushed = true;
16741677 } else {
16751678 /*
16761679 * If no fragment of this line ever reached the console,
16771680 * just submit it to the store and free the buffer.
16781681 */
1679- log_store (cont .facility , cont .level , flags , 0 ,
1682+ log_store (cont .facility , cont .level , cont . flags , 0 ,
16801683 NULL , 0 , cont .buf , cont .len );
16811684 cont .len = 0 ;
16821685 }
16831686}
16841687
1685- static bool cont_add (int facility , int level , const char * text , size_t len )
1688+ static bool cont_add (int facility , int level , enum log_flags flags , const char * text , size_t len )
16861689{
16871690 if (cont .len && cont .flushed )
16881691 return false;
@@ -1693,7 +1696,7 @@ static bool cont_add(int facility, int level, const char *text, size_t len)
16931696 * the line gets too long, split it up in separate records.
16941697 */
16951698 if (nr_ext_console_drivers || cont .len + len > sizeof (cont .buf )) {
1696- cont_flush (LOG_CONT );
1699+ cont_flush ();
16971700 return false;
16981701 }
16991702
@@ -1702,16 +1705,23 @@ static bool cont_add(int facility, int level, const char *text, size_t len)
17021705 cont .level = level ;
17031706 cont .owner = current ;
17041707 cont .ts_nsec = local_clock ();
1705- cont .flags = 0 ;
1708+ cont .flags = flags ;
17061709 cont .cons = 0 ;
17071710 cont .flushed = false;
17081711 }
17091712
17101713 memcpy (cont .buf + cont .len , text , len );
17111714 cont .len += len ;
17121715
1716+ // The original flags come from the first line,
1717+ // but later continuations can add a newline.
1718+ if (flags & LOG_NEWLINE ) {
1719+ cont .flags |= LOG_NEWLINE ;
1720+ cont_flush ();
1721+ }
1722+
17131723 if (cont .len > (sizeof (cont .buf ) * 80 ) / 100 )
1714- cont_flush (LOG_CONT );
1724+ cont_flush ();
17151725
17161726 return true;
17171727}
@@ -1744,6 +1754,31 @@ static size_t cont_print_text(char *text, size_t size)
17441754 return textlen ;
17451755}
17461756
1757+ static size_t log_output (int facility , int level , enum log_flags lflags , const char * dict , size_t dictlen , char * text , size_t text_len )
1758+ {
1759+ /*
1760+ * If an earlier line was buffered, and we're a continuation
1761+ * write from the same process, try to add it to the buffer.
1762+ */
1763+ if (cont .len ) {
1764+ if (cont .owner == current && (lflags & LOG_CONT )) {
1765+ if (cont_add (facility , level , lflags , text , text_len ))
1766+ return text_len ;
1767+ }
1768+ /* Otherwise, make sure it's flushed */
1769+ cont_flush ();
1770+ }
1771+
1772+ /* If it doesn't end in a newline, try to buffer the current line */
1773+ if (!(lflags & LOG_NEWLINE )) {
1774+ if (cont_add (facility , level , lflags , text , text_len ))
1775+ return text_len ;
1776+ }
1777+
1778+ /* Store it in the record log */
1779+ return log_store (facility , level , lflags , 0 , dict , dictlen , text , text_len );
1780+ }
1781+
17471782asmlinkage int vprintk_emit (int facility , int level ,
17481783 const char * dict , size_t dictlen ,
17491784 const char * fmt , va_list args )
@@ -1830,25 +1865,23 @@ asmlinkage int vprintk_emit(int facility, int level,
18301865
18311866 /* strip kernel syslog prefix and extract log level or control flags */
18321867 if (facility == 0 ) {
1833- int kern_level = printk_get_level ( text ) ;
1868+ int kern_level ;
18341869
1835- if (kern_level ) {
1836- const char * end_of_header = printk_skip_level (text );
1870+ while ((kern_level = printk_get_level (text )) != 0 ) {
18371871 switch (kern_level ) {
18381872 case '0' ... '7' :
18391873 if (level == LOGLEVEL_DEFAULT )
18401874 level = kern_level - '0' ;
18411875 /* fallthrough */
18421876 case 'd' : /* KERN_DEFAULT */
18431877 lflags |= LOG_PREFIX ;
1878+ break ;
1879+ case 'c' : /* KERN_CONT */
1880+ lflags |= LOG_CONT ;
18441881 }
1845- /*
1846- * No need to check length here because vscnprintf
1847- * put '\0' at the end of the string. Only valid and
1848- * newly printed level is detected.
1849- */
1850- text_len -= end_of_header - text ;
1851- text = (char * )end_of_header ;
1882+
1883+ text_len -= 2 ;
1884+ text += 2 ;
18521885 }
18531886 }
18541887
@@ -1858,45 +1891,7 @@ asmlinkage int vprintk_emit(int facility, int level,
18581891 if (dict )
18591892 lflags |= LOG_PREFIX |LOG_NEWLINE ;
18601893
1861- if (!(lflags & LOG_NEWLINE )) {
1862- /*
1863- * Flush the conflicting buffer. An earlier newline was missing,
1864- * or another task also prints continuation lines.
1865- */
1866- if (cont .len && (lflags & LOG_PREFIX || cont .owner != current ))
1867- cont_flush (LOG_NEWLINE );
1868-
1869- /* buffer line if possible, otherwise store it right away */
1870- if (cont_add (facility , level , text , text_len ))
1871- printed_len += text_len ;
1872- else
1873- printed_len += log_store (facility , level ,
1874- lflags | LOG_CONT , 0 ,
1875- dict , dictlen , text , text_len );
1876- } else {
1877- bool stored = false;
1878-
1879- /*
1880- * If an earlier newline was missing and it was the same task,
1881- * either merge it with the current buffer and flush, or if
1882- * there was a race with interrupts (prefix == true) then just
1883- * flush it out and store this line separately.
1884- * If the preceding printk was from a different task and missed
1885- * a newline, flush and append the newline.
1886- */
1887- if (cont .len ) {
1888- if (cont .owner == current && !(lflags & LOG_PREFIX ))
1889- stored = cont_add (facility , level , text ,
1890- text_len );
1891- cont_flush (LOG_NEWLINE );
1892- }
1893-
1894- if (stored )
1895- printed_len += text_len ;
1896- else
1897- printed_len += log_store (facility , level , lflags , 0 ,
1898- dict , dictlen , text , text_len );
1899- }
1894+ printed_len += log_output (facility , level , lflags , dict , dictlen , text , text_len );
19001895
19011896 logbuf_cpu = UINT_MAX ;
19021897 raw_spin_unlock (& logbuf_lock );
@@ -3040,6 +3035,7 @@ void kmsg_dump(enum kmsg_dump_reason reason)
30403035 dumper -> active = true;
30413036
30423037 raw_spin_lock_irqsave (& logbuf_lock , flags );
3038+ cont_flush ();
30433039 dumper -> cur_seq = clear_seq ;
30443040 dumper -> cur_idx = clear_idx ;
30453041 dumper -> next_seq = log_next_seq ;
@@ -3130,6 +3126,7 @@ bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog,
31303126 bool ret ;
31313127
31323128 raw_spin_lock_irqsave (& logbuf_lock , flags );
3129+ cont_flush ();
31333130 ret = kmsg_dump_get_line_nolock (dumper , syslog , line , size , len );
31343131 raw_spin_unlock_irqrestore (& logbuf_lock , flags );
31353132
@@ -3172,6 +3169,7 @@ bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
31723169 goto out ;
31733170
31743171 raw_spin_lock_irqsave (& logbuf_lock , flags );
3172+ cont_flush ();
31753173 if (dumper -> cur_seq < log_first_seq ) {
31763174 /* messages are gone, move to first available one */
31773175 dumper -> cur_seq = log_first_seq ;
0 commit comments