Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp3918031imj; Tue, 12 Feb 2019 06:55:27 -0800 (PST) X-Google-Smtp-Source: AHgI3IZQ+9oiSLwfeKnzrKhTLWN7h/uYBv1xODBGRpRuSLyKN0Pn8XQvOBjfP8xIggrZa5VdOXOU X-Received: by 2002:a63:3541:: with SMTP id c62mr3988912pga.191.1549983327822; Tue, 12 Feb 2019 06:55:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549983327; cv=none; d=google.com; s=arc-20160816; b=MCE9Isv5xjR199oX0SDk+REhLWnRgPZshrkO9uuWlxzmBwKDfx8hZZ63d9dKiy3pl+ pl/n0ihGFfiVk1GSa55IJUHern18ZZs7QoHRFqvRkx/JUepvnbCEeiWgsdzA7HVW9pwW +D+06mEXzNrmjIiqT8kXMMAMm1+1T6/Xq9mjhntSqZtYbm0mVB7+hKtqcNuR+7DV0Tyt wIjksuHAy/ghOZ6qA67QE+plcRX4miKXOqlegEQw+F/fAEw6zyI3x0Ch+l4iwGGrKNgY 4eadBh/LoFgnvv4qGvjm/M1hE9zBqMZQx+mNE83q4RZyJnSF/P2WlrEc0oo7m/weT6dD unEQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=rLAKDI9/XI5JVfqI456qOCPIQXjZO/rY1RWOPOoCv70=; b=jGbHL3SoDNc101zWGuLjkUnDzA3Majz3XMDNZcBaKQDlscHHC4Xz3kBj/ZPqWRoLoy pvRKGPKHyWRTp1V0EfoEwz2NEKNy6NANe1fRyhGKHdQ4CAXBC9/XMu1a8QIqU+Vci8c7 xQs56tTDbomPTrAFAWOkOdCRpW8B8eg5M/KDz0j1iPlkDO5SbuslNDE1ndGcowrJBUuR yuJflZKxXuT64+yJCC54Kh4dsNs/+KveyrcTqIkraE90H3XU6OkR0Iv0Y/tNZYgJX5cX UKrvDXw0iGquoBaEAKmcp4VO00n18qSf/VYnqIOPk+fXq8Focdkam6EUwe8hnBTOu+Jb NnvA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d15si2617777pgl.575.2019.02.12.06.55.10; Tue, 12 Feb 2019 06:55:27 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729565AbfBLOck (ORCPT + 99 others); Tue, 12 Feb 2019 09:32:40 -0500 Received: from Galois.linutronix.de ([146.0.238.70]:43723 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729496AbfBLOad (ORCPT ); Tue, 12 Feb 2019 09:30:33 -0500 Received: from [5.158.153.53] (helo=linux.lab.linutronix.de.) by Galois.linutronix.de with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1gtZ4j-0005Af-9R; Tue, 12 Feb 2019 15:30:17 +0100 From: John Ogness To: linux-kernel@vger.kernel.org Cc: Peter Zijlstra , Petr Mladek , Sergey Senozhatsky , Steven Rostedt , Daniel Wang , Andrew Morton , Linus Torvalds , Greg Kroah-Hartman , Alan Cox , Jiri Slaby , Peter Feiner , linux-serial@vger.kernel.org, Sergey Senozhatsky Subject: [RFC PATCH v1 10/25] printk: redirect emit/store to new ringbuffer Date: Tue, 12 Feb 2019 15:29:48 +0100 Message-Id: <20190212143003.48446-11-john.ogness@linutronix.de> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190212143003.48446-1-john.ogness@linutronix.de> References: <20190212143003.48446-1-john.ogness@linutronix.de> X-Linutronix-Spam-Score: -1.0 X-Linutronix-Spam-Level: - X-Linutronix-Spam-Status: No , -1.0 points, 5.0 required, ALL_TRUSTED=-1,SHORTCIRCUIT=-0.0001 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org vprintk_emit and vprintk_store are the main functions that all printk variants eventually go through. Change these to store the message in the new printk ring buffer that the printk kthread is reading. Remove functions no longer in use because of the changes to vprintk_emit and vprintk_store. In order to handle interrupts and NMIs, a second per-cpu ring buffer (sprint_rb) is added. This ring buffer is used for NMI-safe memory allocation in order to format the printk messages. NOTE: LOG_CONT is ignored for now and handled as individual messages. LOG_CONT functions are masked behind "#if 0" blocks until their functionality can be restored Signed-off-by: John Ogness --- kernel/printk/printk.c | 319 ++++++++----------------------------------------- 1 file changed, 51 insertions(+), 268 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 5a5a685bb128..b6a6f1002741 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -493,90 +493,6 @@ static u32 log_next(u32 idx) return idx + msg->len; } -/* - * Check whether there is enough free space for the given message. - * - * The same values of first_idx and next_idx mean that the buffer - * is either empty or full. - * - * If the buffer is empty, we must respect the position of the indexes. - * They cannot be reset to the beginning of the buffer. - */ -static int logbuf_has_space(u32 msg_size, bool empty) -{ - u32 free; - - if (log_next_idx > log_first_idx || empty) - free = max(log_buf_len - log_next_idx, log_first_idx); - else - free = log_first_idx - log_next_idx; - - /* - * We need space also for an empty header that signalizes wrapping - * of the buffer. - */ - return free >= msg_size + sizeof(struct printk_log); -} - -static int log_make_free_space(u32 msg_size) -{ - while (log_first_seq < log_next_seq && - !logbuf_has_space(msg_size, false)) { - /* drop old messages until we have enough contiguous space */ - log_first_idx = log_next(log_first_idx); - log_first_seq++; - } - - if (clear_seq < log_first_seq) { - clear_seq = log_first_seq; - clear_idx = log_first_idx; - } - - /* sequence numbers are equal, so the log buffer is empty */ - if (logbuf_has_space(msg_size, log_first_seq == log_next_seq)) - return 0; - - return -ENOMEM; -} - -/* compute the message size including the padding bytes */ -static u32 msg_used_size(u16 text_len, u16 dict_len, u32 *pad_len) -{ - u32 size; - - size = sizeof(struct printk_log) + text_len + dict_len; - *pad_len = (-size) & (LOG_ALIGN - 1); - size += *pad_len; - - return size; -} - -/* - * Define how much of the log buffer we could take at maximum. The value - * must be greater than two. Note that only half of the buffer is available - * when the index points to the middle. - */ -#define MAX_LOG_TAKE_PART 4 -static const char trunc_msg[] = ""; - -static u32 truncate_msg(u16 *text_len, u16 *trunc_msg_len, - u16 *dict_len, u32 *pad_len) -{ - /* - * The message should not take the whole buffer. Otherwise, it might - * get removed too soon. - */ - u32 max_text_len = log_buf_len / MAX_LOG_TAKE_PART; - if (*text_len > max_text_len) - *text_len = max_text_len; - /* enable the warning message */ - *trunc_msg_len = strlen(trunc_msg); - /* disable the "dict" completely */ - *dict_len = 0; - /* compute the size again, count also the warning message */ - return msg_used_size(*text_len + *trunc_msg_len, 0, pad_len); -} - /* insert record into the buffer, discard old ones, update heads */ static int log_store(int facility, int level, enum log_flags flags, u64 ts_nsec, @@ -584,54 +500,36 @@ static int log_store(int facility, int level, const char *text, u16 text_len) { struct printk_log *msg; - u32 size, pad_len; - u16 trunc_msg_len = 0; - - /* number of '\0' padding bytes to next message */ - size = msg_used_size(text_len, dict_len, &pad_len); - - if (log_make_free_space(size)) { - /* truncate the message if it is too long for empty buffer */ - size = truncate_msg(&text_len, &trunc_msg_len, - &dict_len, &pad_len); - /* survive when the log buffer is too small for trunc_msg */ - if (log_make_free_space(size)) - return 0; - } + struct prb_handle h; + char *rbuf; + u32 size; - if (log_next_idx + size + sizeof(struct printk_log) > log_buf_len) { + size = sizeof(*msg) + text_len + dict_len; + + rbuf = prb_reserve(&h, &printk_rb, size); + if (!rbuf) { /* - * This message + an additional empty header does not fit - * at the end of the buffer. Add an empty header with len == 0 - * to signify a wrap around. + * An emergency message would have been printed, but + * it cannot be stored in the log. */ - memset(log_buf + log_next_idx, 0, sizeof(struct printk_log)); - log_next_idx = 0; + prb_inc_lost(&printk_rb); + return 0; } /* fill message */ - msg = (struct printk_log *)(log_buf + log_next_idx); + msg = (struct printk_log *)rbuf; memcpy(log_text(msg), text, text_len); msg->text_len = text_len; - if (trunc_msg_len) { - memcpy(log_text(msg) + text_len, trunc_msg, trunc_msg_len); - msg->text_len += trunc_msg_len; - } memcpy(log_dict(msg), dict, dict_len); msg->dict_len = dict_len; msg->facility = facility; msg->level = level & 7; msg->flags = flags & 0x1f; - if (ts_nsec > 0) - msg->ts_nsec = ts_nsec; - else - msg->ts_nsec = local_clock(); - memset(log_dict(msg) + dict_len, 0, pad_len); + msg->ts_nsec = ts_nsec; msg->len = size; /* insert message */ - log_next_idx += msg->len; - log_next_seq++; + prb_commit(&h); return msg->text_len; } @@ -1675,70 +1573,6 @@ static int console_lock_spinning_disable_and_check(void) return 1; } -/** - * console_trylock_spinning - try to get console_lock by busy waiting - * - * This allows to busy wait for the console_lock when the current - * owner is running in specially marked sections. It means that - * the current owner is running and cannot reschedule until it - * is ready to lose the lock. - * - * Return: 1 if we got the lock, 0 othrewise - */ -static int console_trylock_spinning(void) -{ - struct task_struct *owner = NULL; - bool waiter; - bool spin = false; - unsigned long flags; - - if (console_trylock()) - return 1; - - printk_safe_enter_irqsave(flags); - - raw_spin_lock(&console_owner_lock); - owner = READ_ONCE(console_owner); - waiter = READ_ONCE(console_waiter); - if (!waiter && owner && owner != current) { - WRITE_ONCE(console_waiter, true); - spin = true; - } - raw_spin_unlock(&console_owner_lock); - - /* - * If there is an active printk() writing to the - * consoles, instead of having it write our data too, - * see if we can offload that load from the active - * printer, and do some printing ourselves. - * Go into a spin only if there isn't already a waiter - * spinning, and there is an active printer, and - * that active printer isn't us (recursive printk?). - */ - if (!spin) { - printk_safe_exit_irqrestore(flags); - return 0; - } - - /* We spin waiting for the owner to release us */ - spin_acquire(&console_owner_dep_map, 0, 0, _THIS_IP_); - /* Owner will clear console_waiter on hand off */ - while (READ_ONCE(console_waiter)) - cpu_relax(); - spin_release(&console_owner_dep_map, 1, _THIS_IP_); - - printk_safe_exit_irqrestore(flags); - /* - * The owner passed the console lock to us. - * Since we did not spin on console lock, annotate - * this as a trylock. Otherwise lockdep will - * complain. - */ - mutex_acquire(&console_lock_dep_map, 0, 1, _THIS_IP_); - - return 1; -} - /* * Call the console drivers, asking them to write out * log_buf[start] to log_buf[end - 1]. @@ -1759,7 +1593,7 @@ static void call_console_drivers(const char *ext_text, size_t ext_len, continue; if (!con->write) continue; - if (!cpu_online(smp_processor_id()) && + if (!cpu_online(raw_smp_processor_id()) && !(con->flags & CON_ANYTIME)) continue; if (con->flags & CON_EXTENDED) @@ -1783,6 +1617,8 @@ static inline void printk_delay(void) } } +/* FIXME: no support for LOG_CONT */ +#if 0 /* * Continuation lines are buffered, and not committed to the record buffer * until the line is complete, or a race forces it. The line fragments @@ -1837,53 +1673,44 @@ static bool cont_add(int facility, int level, enum log_flags flags, const char * return true; } +#endif /* 0 */ -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) -{ - /* - * If an earlier line was buffered, and we're a continuation - * write from the same process, try to add it to the buffer. - */ - if (cont.len) { - if (cont.owner == current && (lflags & LOG_CONT)) { - if (cont_add(facility, level, lflags, text, text_len)) - return text_len; - } - /* Otherwise, make sure it's flushed */ - cont_flush(); - } - - /* Skip empty continuation lines that couldn't be added - they just flush */ - if (!text_len && (lflags & LOG_CONT)) - return 0; - - /* If it doesn't end in a newline, try to buffer the current line */ - if (!(lflags & LOG_NEWLINE)) { - if (cont_add(facility, level, lflags, text, text_len)) - return text_len; - } - - /* Store it in the record log */ - return log_store(facility, level, lflags, 0, dict, dictlen, text, text_len); -} - -/* Must be called under logbuf_lock. */ int vprintk_store(int facility, int level, const char *dict, size_t dictlen, const char *fmt, va_list args) { - static char textbuf[LOG_LINE_MAX]; - char *text = textbuf; - size_t text_len; + return vprintk_emit(facility, level, dict, dictlen, fmt, args); +} + +/* ring buffer used as memory allocator for temporary sprint buffers */ +DECLARE_STATIC_PRINTKRB(sprint_rb, + ilog2(PRINTK_RECORD_MAX + sizeof(struct prb_entry) + + sizeof(long)) + 2, &printk_cpulock); + +asmlinkage int vprintk_emit(int facility, int level, + const char *dict, size_t dictlen, + const char *fmt, va_list args) +{ enum log_flags lflags = 0; + int printed_len = 0; + struct prb_handle h; + size_t text_len; + u64 ts_nsec; + char *text; + char *rbuf; - /* - * The printf needs to come first; we need the syslog - * prefix which might be passed-in as a parameter. - */ - text_len = vscnprintf(text, sizeof(textbuf), fmt, args); + ts_nsec = local_clock(); - /* mark and strip a trailing newline */ + rbuf = prb_reserve(&h, &sprint_rb, PRINTK_SPRINT_MAX); + if (!rbuf) { + prb_inc_lost(&printk_rb); + return printed_len; + } + + text = rbuf; + text_len = vscnprintf(text, PRINTK_SPRINT_MAX, fmt, args); + + /* strip and flag a trailing newline */ if (text_len && text[text_len-1] == '\n') { text_len--; lflags |= LOG_NEWLINE; @@ -1917,54 +1744,10 @@ int vprintk_store(int facility, int level, if (dict) lflags |= LOG_PREFIX|LOG_NEWLINE; - return log_output(facility, level, lflags, - dict, dictlen, text, text_len); -} - -asmlinkage int vprintk_emit(int facility, int level, - const char *dict, size_t dictlen, - const char *fmt, va_list args) -{ - int printed_len; - bool in_sched = false, pending_output; - unsigned long flags; - u64 curr_log_seq; - - if (level == LOGLEVEL_SCHED) { - level = LOGLEVEL_DEFAULT; - in_sched = true; - } - - boot_delay_msec(level); - printk_delay(); - - /* This stops the holder of console_sem just where we want him */ - logbuf_lock_irqsave(flags); - curr_log_seq = log_next_seq; - printed_len = vprintk_store(facility, level, dict, dictlen, fmt, args); - pending_output = (curr_log_seq != log_next_seq); - logbuf_unlock_irqrestore(flags); - - /* If called from the scheduler, we can not call up(). */ - if (!in_sched && pending_output) { - /* - * Disable preemption to avoid being preempted while holding - * console_sem which would prevent anyone from printing to - * console - */ - preempt_disable(); - /* - * Try to acquire and then immediately release the console - * semaphore. The release will print out buffers and wake up - * /dev/kmsg and syslog() users. - */ - if (console_trylock_spinning()) - console_unlock(); - preempt_enable(); - } + printed_len = log_store(facility, level, lflags, ts_nsec, + dict, dictlen, text, text_len); - if (pending_output) - wake_up_klogd(); + prb_commit(&h); return printed_len; } EXPORT_SYMBOL(vprintk_emit); @@ -2429,7 +2212,7 @@ void console_unlock(void) console_lock_spinning_enable(); stop_critical_timings(); /* don't trace print latency */ - call_console_drivers(ext_text, ext_len, text, len); + //call_console_drivers(ext_text, ext_len, text, len); start_critical_timings(); if (console_lock_spinning_disable_and_check()) { -- 2.11.0