@@ -1126,7 +1126,7 @@ void __init setup_log_buf(int early)
1126
1126
new_descs , ilog2 (new_descs_count ),
1127
1127
new_infos );
1128
1128
1129
- logbuf_lock_irqsave (flags );
1129
+ printk_safe_enter_irqsave (flags );
1130
1130
1131
1131
log_buf_len = new_log_buf_len ;
1132
1132
log_buf = new_log_buf ;
@@ -1143,7 +1143,7 @@ void __init setup_log_buf(int early)
1143
1143
*/
1144
1144
prb = & printk_rb_dynamic ;
1145
1145
1146
- logbuf_unlock_irqrestore (flags );
1146
+ printk_safe_exit_irqrestore (flags );
1147
1147
1148
1148
if (seq != prb_next_seq (& printk_rb_static )) {
1149
1149
pr_err ("dropped %llu messages\n" ,
@@ -1861,18 +1861,90 @@ static inline u32 printk_caller_id(void)
1861
1861
0x80000000 + raw_smp_processor_id ();
1862
1862
}
1863
1863
1864
- /* Must be called under logbuf_lock. */
1864
+ /**
1865
+ * parse_prefix - Parse level and control flags.
1866
+ *
1867
+ * @text: The terminated text message.
1868
+ * @level: A pointer to the current level value, will be updated.
1869
+ * @lflags: A pointer to the current log flags, will be updated.
1870
+ *
1871
+ * @level may be NULL if the caller is not interested in the parsed value.
1872
+ * Otherwise the variable pointed to by @level must be set to
1873
+ * LOGLEVEL_DEFAULT in order to be updated with the parsed value.
1874
+ *
1875
+ * @lflags may be NULL if the caller is not interested in the parsed value.
1876
+ * Otherwise the variable pointed to by @lflags will be OR'd with the parsed
1877
+ * value.
1878
+ *
1879
+ * Return: The length of the parsed level and control flags.
1880
+ */
1881
+ static u16 parse_prefix (char * text , int * level , enum log_flags * lflags )
1882
+ {
1883
+ u16 prefix_len = 0 ;
1884
+ int kern_level ;
1885
+
1886
+ while (* text ) {
1887
+ kern_level = printk_get_level (text );
1888
+ if (!kern_level )
1889
+ break ;
1890
+
1891
+ switch (kern_level ) {
1892
+ case '0' ... '7' :
1893
+ if (level && * level == LOGLEVEL_DEFAULT )
1894
+ * level = kern_level - '0' ;
1895
+ break ;
1896
+ case 'c' : /* KERN_CONT */
1897
+ if (lflags )
1898
+ * lflags |= LOG_CONT ;
1899
+ }
1900
+
1901
+ prefix_len += 2 ;
1902
+ text += 2 ;
1903
+ }
1904
+
1905
+ return prefix_len ;
1906
+ }
1907
+
1908
+ static u16 printk_sprint (char * text , u16 size , int facility , enum log_flags * lflags ,
1909
+ const char * fmt , va_list args )
1910
+ {
1911
+ u16 text_len ;
1912
+
1913
+ text_len = vscnprintf (text , size , fmt , args );
1914
+
1915
+ /* Mark and strip a trailing newline. */
1916
+ if (text_len && text [text_len - 1 ] == '\n' ) {
1917
+ text_len -- ;
1918
+ * lflags |= LOG_NEWLINE ;
1919
+ }
1920
+
1921
+ /* Strip log level and control flags. */
1922
+ if (facility == 0 ) {
1923
+ u16 prefix_len ;
1924
+
1925
+ prefix_len = parse_prefix (text , NULL , NULL );
1926
+ if (prefix_len ) {
1927
+ text_len -= prefix_len ;
1928
+ memmove (text , text + prefix_len , text_len );
1929
+ }
1930
+ }
1931
+
1932
+ return text_len ;
1933
+ }
1934
+
1935
+ __printf (4 , 0 )
1865
1936
int vprintk_store (int facility , int level ,
1866
1937
const struct dev_printk_info * dev_info ,
1867
1938
const char * fmt , va_list args )
1868
1939
{
1869
1940
const u32 caller_id = printk_caller_id ();
1870
- static char textbuf [LOG_LINE_MAX ];
1871
1941
struct prb_reserved_entry e ;
1872
1942
enum log_flags lflags = 0 ;
1873
1943
struct printk_record r ;
1874
1944
u16 trunc_msg_len = 0 ;
1875
- char * text = textbuf ;
1945
+ char prefix_buf [8 ];
1946
+ u16 reserve_size ;
1947
+ va_list args2 ;
1876
1948
u16 text_len ;
1877
1949
u64 ts_nsec ;
1878
1950
@@ -1885,35 +1957,21 @@ int vprintk_store(int facility, int level,
1885
1957
ts_nsec = local_clock ();
1886
1958
1887
1959
/*
1888
- * The printf needs to come first; we need the syslog
1889
- * prefix which might be passed-in as a parameter.
1960
+ * The sprintf needs to come first since the syslog prefix might be
1961
+ * passed in as a parameter. An extra byte must be reserved so that
1962
+ * later the vscnprintf() into the reserved buffer has room for the
1963
+ * terminating '\0', which is not counted by vsnprintf().
1890
1964
*/
1891
- text_len = vscnprintf (text , sizeof (textbuf ), fmt , args );
1892
-
1893
- /* mark and strip a trailing newline */
1894
- if (text_len && text [text_len - 1 ] == '\n' ) {
1895
- text_len -- ;
1896
- lflags |= LOG_NEWLINE ;
1897
- }
1898
-
1899
- /* strip kernel syslog prefix and extract log level or control flags */
1900
- if (facility == 0 ) {
1901
- int kern_level ;
1965
+ va_copy (args2 , args );
1966
+ reserve_size = vsnprintf (& prefix_buf [0 ], sizeof (prefix_buf ), fmt , args2 ) + 1 ;
1967
+ va_end (args2 );
1902
1968
1903
- while ((kern_level = printk_get_level (text )) != 0 ) {
1904
- switch (kern_level ) {
1905
- case '0' ... '7' :
1906
- if (level == LOGLEVEL_DEFAULT )
1907
- level = kern_level - '0' ;
1908
- break ;
1909
- case 'c' : /* KERN_CONT */
1910
- lflags |= LOG_CONT ;
1911
- }
1969
+ if (reserve_size > LOG_LINE_MAX )
1970
+ reserve_size = LOG_LINE_MAX ;
1912
1971
1913
- text_len -= 2 ;
1914
- text += 2 ;
1915
- }
1916
- }
1972
+ /* Extract log level or control flags. */
1973
+ if (facility == 0 )
1974
+ parse_prefix (& prefix_buf [0 ], & level , & lflags );
1917
1975
1918
1976
if (level == LOGLEVEL_DEFAULT )
1919
1977
level = default_message_loglevel ;
@@ -1922,9 +1980,10 @@ int vprintk_store(int facility, int level,
1922
1980
lflags |= LOG_NEWLINE ;
1923
1981
1924
1982
if (lflags & LOG_CONT ) {
1925
- prb_rec_init_wr (& r , text_len );
1983
+ prb_rec_init_wr (& r , reserve_size );
1926
1984
if (prb_reserve_in_last (& e , prb , & r , caller_id , LOG_LINE_MAX )) {
1927
- memcpy (& r .text_buf [r .info -> text_len ], text , text_len );
1985
+ text_len = printk_sprint (& r .text_buf [r .info -> text_len ], reserve_size ,
1986
+ facility , & lflags , fmt , args );
1928
1987
r .info -> text_len += text_len ;
1929
1988
1930
1989
if (lflags & LOG_NEWLINE ) {
@@ -1943,18 +2002,18 @@ int vprintk_store(int facility, int level,
1943
2002
* prb_reserve_in_last() and prb_reserve() purposely invalidate the
1944
2003
* structure when they fail.
1945
2004
*/
1946
- prb_rec_init_wr (& r , text_len );
2005
+ prb_rec_init_wr (& r , reserve_size );
1947
2006
if (!prb_reserve (& e , prb , & r )) {
1948
2007
/* truncate the message if it is too long for empty buffer */
1949
- truncate_msg (& text_len , & trunc_msg_len );
2008
+ truncate_msg (& reserve_size , & trunc_msg_len );
1950
2009
1951
- prb_rec_init_wr (& r , text_len + trunc_msg_len );
2010
+ prb_rec_init_wr (& r , reserve_size + trunc_msg_len );
1952
2011
if (!prb_reserve (& e , prb , & r ))
1953
2012
return 0 ;
1954
2013
}
1955
2014
1956
2015
/* fill message */
1957
- memcpy (& r .text_buf [0 ], text , text_len );
2016
+ text_len = printk_sprint (& r .text_buf [0 ], reserve_size , facility , & lflags , fmt , args );
1958
2017
if (trunc_msg_len )
1959
2018
memcpy (& r .text_buf [text_len ], trunc_msg , trunc_msg_len );
1960
2019
r .info -> text_len = text_len + trunc_msg_len ;
@@ -1995,10 +2054,9 @@ asmlinkage int vprintk_emit(int facility, int level,
1995
2054
boot_delay_msec (level );
1996
2055
printk_delay ();
1997
2056
1998
- /* This stops the holder of console_sem just where we want him */
1999
- logbuf_lock_irqsave (flags );
2057
+ printk_safe_enter_irqsave (flags );
2000
2058
printed_len = vprintk_store (facility , level , dev_info , fmt , args );
2001
- logbuf_unlock_irqrestore (flags );
2059
+ printk_safe_exit_irqrestore (flags );
2002
2060
2003
2061
/* If called from the scheduler, we can not call up(). */
2004
2062
if (!in_sched ) {
0 commit comments