@@ -491,52 +491,6 @@ static void truncate_msg(u16 *text_len, u16 *trunc_msg_len)
491491 * trunc_msg_len = 0 ;
492492}
493493
494- /* insert record into the buffer, discard old ones, update heads */
495- static int log_store (u32 caller_id , int facility , int level ,
496- enum log_flags flags , u64 ts_nsec ,
497- const struct dev_printk_info * dev_info ,
498- const char * text , u16 text_len )
499- {
500- struct prb_reserved_entry e ;
501- struct printk_record r ;
502- u16 trunc_msg_len = 0 ;
503-
504- prb_rec_init_wr (& r , text_len );
505-
506- if (!prb_reserve (& e , prb , & r )) {
507- /* truncate the message if it is too long for empty buffer */
508- truncate_msg (& text_len , & trunc_msg_len );
509- prb_rec_init_wr (& r , text_len + trunc_msg_len );
510- /* survive when the log buffer is too small for trunc_msg */
511- if (!prb_reserve (& e , prb , & r ))
512- return 0 ;
513- }
514-
515- /* fill message */
516- memcpy (& r .text_buf [0 ], text , text_len );
517- if (trunc_msg_len )
518- memcpy (& r .text_buf [text_len ], trunc_msg , trunc_msg_len );
519- r .info -> text_len = text_len + trunc_msg_len ;
520- r .info -> facility = facility ;
521- r .info -> level = level & 7 ;
522- r .info -> flags = flags & 0x1f ;
523- if (ts_nsec > 0 )
524- r .info -> ts_nsec = ts_nsec ;
525- else
526- r .info -> ts_nsec = local_clock ();
527- r .info -> caller_id = caller_id ;
528- if (dev_info )
529- memcpy (& r .info -> dev_info , dev_info , sizeof (r .info -> dev_info ));
530-
531- /* A message without a trailing newline can be continued. */
532- if (!(flags & LOG_NEWLINE ))
533- prb_commit (& e );
534- else
535- prb_final_commit (& e );
536-
537- return (text_len + trunc_msg_len );
538- }
539-
540494int dmesg_restrict = IS_ENABLED (CONFIG_SECURITY_DMESG_RESTRICT );
541495
542496static int syslog_action_restricted (int type )
@@ -1907,44 +1861,28 @@ static inline u32 printk_caller_id(void)
19071861 0x80000000 + raw_smp_processor_id ();
19081862}
19091863
1910- static size_t log_output (int facility , int level , enum log_flags lflags ,
1911- const struct dev_printk_info * dev_info ,
1912- char * text , size_t text_len )
1913- {
1914- const u32 caller_id = printk_caller_id ();
1915-
1916- if (lflags & LOG_CONT ) {
1917- struct prb_reserved_entry e ;
1918- struct printk_record r ;
1919-
1920- prb_rec_init_wr (& r , text_len );
1921- if (prb_reserve_in_last (& e , prb , & r , caller_id , LOG_LINE_MAX )) {
1922- memcpy (& r .text_buf [r .info -> text_len ], text , text_len );
1923- r .info -> text_len += text_len ;
1924- if (lflags & LOG_NEWLINE ) {
1925- r .info -> flags |= LOG_NEWLINE ;
1926- prb_final_commit (& e );
1927- } else {
1928- prb_commit (& e );
1929- }
1930- return text_len ;
1931- }
1932- }
1933-
1934- /* Store it in the record log */
1935- return log_store (caller_id , facility , level , lflags , 0 ,
1936- dev_info , text , text_len );
1937- }
1938-
19391864/* Must be called under logbuf_lock. */
19401865int vprintk_store (int facility , int level ,
19411866 const struct dev_printk_info * dev_info ,
19421867 const char * fmt , va_list args )
19431868{
1869+ const u32 caller_id = printk_caller_id ();
19441870 static char textbuf [LOG_LINE_MAX ];
1945- char * text = textbuf ;
1946- size_t text_len ;
1871+ struct prb_reserved_entry e ;
19471872 enum log_flags lflags = 0 ;
1873+ struct printk_record r ;
1874+ u16 trunc_msg_len = 0 ;
1875+ char * text = textbuf ;
1876+ u16 text_len ;
1877+ u64 ts_nsec ;
1878+
1879+ /*
1880+ * Since the duration of printk() can vary depending on the message
1881+ * and state of the ringbuffer, grab the timestamp now so that it is
1882+ * close to the call of printk(). This provides a more deterministic
1883+ * timestamp with respect to the caller.
1884+ */
1885+ ts_nsec = local_clock ();
19481886
19491887 /*
19501888 * The printf needs to come first; we need the syslog
@@ -1983,7 +1921,58 @@ int vprintk_store(int facility, int level,
19831921 if (dev_info )
19841922 lflags |= LOG_NEWLINE ;
19851923
1986- return log_output (facility , level , lflags , dev_info , text , text_len );
1924+ if (lflags & LOG_CONT ) {
1925+ prb_rec_init_wr (& r , text_len );
1926+ 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 );
1928+ r .info -> text_len += text_len ;
1929+
1930+ if (lflags & LOG_NEWLINE ) {
1931+ r .info -> flags |= LOG_NEWLINE ;
1932+ prb_final_commit (& e );
1933+ } else {
1934+ prb_commit (& e );
1935+ }
1936+
1937+ return text_len ;
1938+ }
1939+ }
1940+
1941+ /*
1942+ * Explicitly initialize the record before every prb_reserve() call.
1943+ * prb_reserve_in_last() and prb_reserve() purposely invalidate the
1944+ * structure when they fail.
1945+ */
1946+ prb_rec_init_wr (& r , text_len );
1947+ if (!prb_reserve (& e , prb , & r )) {
1948+ /* truncate the message if it is too long for empty buffer */
1949+ truncate_msg (& text_len , & trunc_msg_len );
1950+
1951+ prb_rec_init_wr (& r , text_len + trunc_msg_len );
1952+ if (!prb_reserve (& e , prb , & r ))
1953+ return 0 ;
1954+ }
1955+
1956+ /* fill message */
1957+ memcpy (& r .text_buf [0 ], text , text_len );
1958+ if (trunc_msg_len )
1959+ memcpy (& r .text_buf [text_len ], trunc_msg , trunc_msg_len );
1960+ r .info -> text_len = text_len + trunc_msg_len ;
1961+ r .info -> facility = facility ;
1962+ r .info -> level = level & 7 ;
1963+ r .info -> flags = lflags & 0x1f ;
1964+ r .info -> ts_nsec = ts_nsec ;
1965+ r .info -> caller_id = caller_id ;
1966+ if (dev_info )
1967+ memcpy (& r .info -> dev_info , dev_info , sizeof (r .info -> dev_info ));
1968+
1969+ /* A message without a trailing newline can be continued. */
1970+ if (!(lflags & LOG_NEWLINE ))
1971+ prb_commit (& e );
1972+ else
1973+ prb_final_commit (& e );
1974+
1975+ return (text_len + trunc_msg_len );
19871976}
19881977
19891978asmlinkage int vprintk_emit (int facility , int level ,
0 commit comments