From patchwork Tue Jul 22 14:01:57 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Elder X-Patchwork-Id: 34092 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-pd0-f198.google.com (mail-pd0-f198.google.com [209.85.192.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 6730F20540 for ; Tue, 22 Jul 2014 14:02:13 +0000 (UTC) Received: by mail-pd0-f198.google.com with SMTP id fp1sf60638731pdb.1 for ; Tue, 22 Jul 2014 07:02:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:sender:precedence:list-id :x-original-sender:x-original-authentication-results:mailing-list :list-post:list-help:list-archive:list-unsubscribe; bh=9CmwTpDOTxE1AzV/wIMY/WmDwYIjHqtCrNCB4+MABWc=; b=nI6K5RgE3Grl/xfmcAZrbNVbSLHb9oqzPNJVwYnSeiauQm4Jui0/5VPWWdf5HFl02k TRiszzwWQBqFSgZuzBvTC7Zfvps1EreTlWUlD6Azzy0zvSK2Qv1/mMrIRZYWngCSGiVx Q2rRP2aLYfJuU5WkLd89/LFS89bUXYr9DsNgCiJFifs/7dgzX/Fl1Ddn0xiRo+ZDZ1fa zoXnZmyoa9PiHQPiBtGRpP0gsRgyOfWFuEPg8lDnUDpY0rNFNoBUzQt7AKQY461sCAwM 9gA+0TQDVUBm7a6xC3Iv50yudenAVJrl/3ePEtIQKDmoLwakCneOfxrojYMPCOl6NC1p Tx8g== X-Gm-Message-State: ALoCoQm2WFHi/rsUgnNUNXumR019Mc4aaQejRrysU1x+AU8fvylx9atzuM//bf1QPQJJaVJmyDq0 X-Received: by 10.66.182.130 with SMTP id ee2mr16967565pac.37.1406037732561; Tue, 22 Jul 2014 07:02:12 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.50.176.132 with SMTP id ci4ls2173438igc.4.canary; Tue, 22 Jul 2014 07:02:12 -0700 (PDT) X-Received: by 10.70.109.196 with SMTP id hu4mr1895369pdb.58.1406037732322; Tue, 22 Jul 2014 07:02:12 -0700 (PDT) Received: from mail-vc0-f172.google.com (mail-vc0-f172.google.com [209.85.220.172]) by mx.google.com with ESMTPS id cx1si852073vec.38.2014.07.22.07.02.05 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 22 Jul 2014 07:02:05 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.172 as permitted sender) client-ip=209.85.220.172; Received: by mail-vc0-f172.google.com with SMTP id im17so15200635vcb.3 for ; Tue, 22 Jul 2014 07:02:05 -0700 (PDT) X-Received: by 10.53.5.230 with SMTP id cp6mr437794vdd.25.1406037725551; Tue, 22 Jul 2014 07:02:05 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.221.37.5 with SMTP id tc5csp198694vcb; Tue, 22 Jul 2014 07:02:04 -0700 (PDT) X-Received: by 10.66.119.132 with SMTP id ku4mr12447642pab.121.1406037724337; Tue, 22 Jul 2014 07:02:04 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id qd5si520454pbb.211.2014.07.22.07.02.02 for ; Tue, 22 Jul 2014 07:02:02 -0700 (PDT) Received-SPF: none (google.com: linux-kernel-owner@vger.kernel.org does not designate permitted sender hosts) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754700AbaGVOB5 (ORCPT + 14 others); Tue, 22 Jul 2014 10:01:57 -0400 Received: from mail-ie0-f173.google.com ([209.85.223.173]:64336 "EHLO mail-ie0-f173.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750893AbaGVOBz (ORCPT ); Tue, 22 Jul 2014 10:01:55 -0400 Received: by mail-ie0-f173.google.com with SMTP id tr6so8174510ieb.18 for ; Tue, 22 Jul 2014 07:01:55 -0700 (PDT) X-Received: by 10.42.202.14 with SMTP id fc14mr37208570icb.8.1406037715205; Tue, 22 Jul 2014 07:01:55 -0700 (PDT) Received: from localhost.localdomain (c-24-245-18-91.hsd1.mn.comcast.net. [24.245.18.91]) by mx.google.com with ESMTPSA id ga11sm2569229igd.8.2014.07.22.07.01.54 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 22 Jul 2014 07:01:54 -0700 (PDT) From: Alex Elder To: akpm@linux-foundation.org Cc: kay@vrfy.org, pmladek@suse.cz, bp@suse.de, john.stultz@linaro.org, jack@suse.cz, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/2] printk: improve some commentary; tidy up Date: Tue, 22 Jul 2014 09:01:57 -0500 Message-Id: <1406037717-9670-3-git-send-email-elder@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1406037717-9670-1-git-send-email-elder@linaro.org> References: <1406037717-9670-1-git-send-email-elder@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: elder@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.172 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Add some comments to explain how the log flags are used to control how records get formatted. Also add and refine some comments in vprintk_emit(). Now that we're done fixing up log record flags, simplify how they're used in computing some local variable values in msg_print_text(). Use a local variable "flush_cont" in vprintk_emit() to factor out a common expression used later in that function. Signed-off-by: Alex Elder --- kernel/printk/printk.c | 63 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 22 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 49c9238..cecdc1b 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -141,6 +141,12 @@ EXPORT_SYMBOL(console_set_on_cmdline); static int console_may_schedule; /* + * Each call to printk() fills a record in a circular log buffer. + * The contents of the log buffer are read by various subsystems + * (including the console subsystem), each of which formats the + * content of log buffers for human consumption. Flags in each + * log record are used to track formatting-related state. + * * The printk log buffer consists of a chain of concatenated variable * length records. Every record starts with a record header, containing * the overall length of the record. @@ -150,7 +156,7 @@ static int console_may_schedule; * are stored.. * * If the heads indicate available messages, the length in the header - * tells the start next message. A length == 0 for the next message + * tells the start of the next message. A length == 0 for the next message * indicates a wrap-around to the beginning of the buffer. * * Every record carries the monotonic timestamp in microseconds, as well as @@ -192,6 +198,16 @@ static int console_may_schedule; * 67 "g" * 0032 00 00 00 padding to next message header * + * If a printk() call contains no newline, its content is saved in a + * special "cont" buffer rather than being written directly into the + * log. One or more follow-in printk() calls from the same source + * can then be combined into a single newline-terminated message (if + * possible) before the combined result is saved into a log record. + * Occasionally a buffered/partial message needs to be flushed to + * the log before the logically next printk() call is seen. When + * this occurs, the incomplete record (with no LOG_NEWLINE) will + * be followed by a new record marked LOG_PREFIX. + * * The 'struct printk_log' buffer header must never be directly exported to * userspace, it is a kernel-private implementation detail that might * need to be changed in the future, when the requirements change. @@ -1005,17 +1021,14 @@ static size_t msg_print_text(const struct printk_log *msg, enum log_flags prev, { const char *text = log_text(msg); size_t text_size = msg->text_len; - bool prefix = true; - bool newline = true; size_t len = 0; + bool prefix; + bool newline; - if (!(prev & LOG_NEWLINE) && !(msg->flags & LOG_PREFIX)) - prefix = false; - - if (!(msg->flags & LOG_NEWLINE)) - newline = false; - - if (!(prev & LOG_NEWLINE) && (msg->flags & LOG_PREFIX) && len < size) { + prefix = (prev & LOG_NEWLINE) || (msg->flags & LOG_PREFIX); + newline = !!(msg->flags & LOG_NEWLINE); + /* Insert a newline if we're terminating the previous line early */ + if (prefix && !(prev & LOG_NEWLINE) && len < size) { if (buf) buf[len++] = '\n'; else @@ -1600,6 +1613,7 @@ asmlinkage int vprintk_emit(int facility, int level, int this_cpu; int printed_len = 0; bool in_sched = false; + bool flush_cont = false; /* cpu currently holding logbuf_lock in this function */ static volatile unsigned int logbuf_cpu = UINT_MAX; @@ -1698,12 +1712,16 @@ asmlinkage int vprintk_emit(int facility, int level, if (dict) lflags = LOG_PREFIX|LOG_NEWLINE; + /* + * If the previous printk() call had no newline, it will be buffered. + * If the buffered message was produced by someone else, or if this + * call is forcing a new record, we will need to flush the buffer + * rather than merge this message into it. + */ + flush_cont = (cont.owner != current) || (lflags & LOG_PREFIX); if (!(lflags & LOG_NEWLINE)) { - /* - * Flush the conflicting buffer. An earlier newline was missing, - * or another task also prints continuation lines. - */ - if (cont.len && (lflags & LOG_PREFIX || cont.owner != current)) + /* If the buffered record conflicts, flush it first. */ + if (cont.len && flush_cont) cont_flush(LOG_NEWLINE); /* buffer line if possible, otherwise store it right away */ @@ -1716,20 +1734,21 @@ asmlinkage int vprintk_emit(int facility, int level, bool stored = false; /* - * If an earlier newline was missing and it was the same task, - * either merge it with the current buffer and flush, or if - * there was a race with interrupts (prefix == true) then just - * flush it out and store this line separately. - * If the preceding printk was from a different task and missed - * a newline, flush and append the newline. + * If there's a buffered message, try to merge with + * it, then flush whatever's buffered to the log. */ if (cont.len) { - if (cont.owner == current && !(lflags & LOG_PREFIX)) + if (!flush_cont) stored = cont_add(facility, level, text, text_len); cont_flush(LOG_NEWLINE); } + /* + * Record how much we just formatted. If cont_add() didn't + * combine this message with the buffered one we still have + * to store this one to the log. + */ if (stored) printed_len += text_len; else