From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S964836AbcJ0Qfe (ORCPT ); Thu, 27 Oct 2016 12:35:34 -0400 Received: from smtprelay0175.hostedemail.com ([216.40.44.175]:38089 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S932109AbcJ0Qfb (ORCPT ); Thu, 27 Oct 2016 12:35:31 -0400 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::::::::::::::::::,RULES_HIT:41:69:355:379:421:541:599:800:960:973:988:989:1260:1277:1311:1313:1314:1345:1359:1373:1437:1515:1516:1518:1535:1544:1593:1594:1605:1711:1730:1747:1777:1792:2198:2199:2393:2538:2559:2562:2691:2828:2898:2914:3138:3139:3140:3141:3142:3622:3742:3865:3866:3867:3868:3870:3871:3872:3874:4250:4321:4470:5007:6119:7875:7903:7904:8603:9007:10004:10848:11026:11232:11473:11658:11914:12043:12291:12296:12438:12555:12683:12740:12760:13095:13146:13230:13439:14181:14659:14721:21063:21080:21433:21451:30012:30054:30064:30070:30091,0,RBL:none,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:1,LUA_SUMMARY:none X-HE-Tag: house04_1274f2110b528 X-Filterd-Recvd-Size: 5994 Message-ID: <1477586125.30917.3.camel@perches.com> Subject: Re: [PATCH 1/4] printk/NMI: Handle continuous lines and missing newline From: Joe Perches To: Petr Mladek , Linus Torvalds Cc: Andrew Morton , Sergey Senozhatsky , Steven Rostedt , Jason Wessel , Jaroslav Kysela , Takashi Iwai , Chris Mason , Josef Bacik , David Sterba , linux-kernel@vger.kernel.org Date: Thu, 27 Oct 2016 09:35:25 -0700 In-Reply-To: <1477583574-30988-2-git-send-email-pmladek@suse.com> References: <1477583574-30988-1-git-send-email-pmladek@suse.com> <1477583574-30988-2-git-send-email-pmladek@suse.com> Content-Type: text/plain; charset="ISO-8859-1" X-Mailer: Evolution 3.22.1-0ubuntu2 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 2016-10-27 at 17:52 +0200, Petr Mladek wrote: > The commit 4bcc595ccd80decb4245 ("printk: reinstate KERN_CONT for printing > continuation lines") added back KERN_CONT message header. As a result > it might appear in the middle of the line when the parts are squashed > via the temporary NMI buffer. > > A reasonable solution seems to be to split the text in the NNI temporary > not only by newlines but also by the message headers. > > Another solution would be to filter out KERN_CONT when writing to > the temporary buffer. But this would complicate the lockless handling. > Also it would not solve problems with a missing newline that was there > even before the KERN_CONT stuff. I believe the proper solution there is add the missing EOL/newlines where appropriate. There aren't many treewide. Maybe a couple dozen vs the 250,000 messages with newlines. > This patch moves the temporary buffer handling into separate function. > I played with it and it seems that using the char pointers make the > code easier to read. > > Also it moves handling of the s->len overflow into the paranoid check. > And allows to recover from the disaster. > > Signed-off-by: Petr Mladek > --- > kernel/printk/nmi.c | 78 +++++++++++++++++++++++++++++++++-------------------- > 1 file changed, 49 insertions(+), 29 deletions(-) > > diff --git a/kernel/printk/nmi.c b/kernel/printk/nmi.c > index 16bab471c7e2..740c90efc65d 100644 > --- a/kernel/printk/nmi.c > +++ b/kernel/printk/nmi.c > @@ -113,16 +113,49 @@ static void printk_nmi_flush_line(const char *text, int len) > > } > > -/* > - * printk one line from the temporary buffer from @start index until > - * and including the @end index. > - */ > -static void printk_nmi_flush_seq_line(struct nmi_seq_buf *s, > - int start, int end) > +/* printk part of the temporary buffer line by line */ > +static int printk_nmi_flush_buffer(unsigned char *start, size_t len) const unsigned char *? > { > - const char *buf = s->buffer + start; > + unsigned char *c, *end; > + bool header; > + > + c = start; > + end = start + len; > + header = true; > + > + /* Print line by line. */ > + while (c < end) { > + if (*c == '\n') { > + printk_nmi_flush_line(start, c - start + 1); > + start = ++c; > + header = true; > + continue; > + } > > - printk_nmi_flush_line(buf, (end - start) + 1); > + /* Handle continuous lines or missing new line. */ > + if ((c + 1 < end) && printk_get_level(c)) { > + if (header) { > + c += 2; printk_skip_level > + continue; > + } > + > + printk_nmi_flush_line(start, c - start); > + start = c++; > + header = true; > + continue; > + } > + > + header = false; > + c++; > + } > + > + /* Check if there was a partial line. Ignore pure header. */ > + if (start < end && !header) { > + printk_nmi_flush_line(start, end - start); > + printk_nmi_flush_line("\n", strlen("\n")); > + } > + > + return len; > } > > /* > @@ -135,8 +168,8 @@ static void __printk_nmi_flush(struct irq_work *work) > __RAW_SPIN_LOCK_INITIALIZER(read_lock); > struct nmi_seq_buf *s = container_of(work, struct nmi_seq_buf, work); > unsigned long flags; > - size_t len, size; > - int i, last_i; > + size_t len; > + int i; > > /* > * The lock has two functions. First, one reader has to flush all > @@ -154,35 +187,22 @@ static void __printk_nmi_flush(struct irq_work *work) > /* > * This is just a paranoid check that nobody has manipulated > * the buffer an unexpected way. If we printed something then > - * @len must only increase. > + * @len must only increase. Also it should never overflow the > + * buffer size. > */ > - if (i && i >= len) { > + if ((i && i >= len) || len > sizeof(s->buffer)) { > const char *msg = "printk_nmi_flush: internal error\n"; > > printk_nmi_flush_line(msg, strlen(msg)); > + len = 0; > } > > if (!len) > goto out; /* Someone else has already flushed the buffer. */ > > - /* Make sure that data has been written up to the @len */ > + /* Make sure the data has been written up to the @len */ > smp_rmb(); > - > - size = min(len, sizeof(s->buffer)); > - last_i = i; > - > - /* Print line by line. */ > - for (; i < size; i++) { > - if (s->buffer[i] == '\n') { > - printk_nmi_flush_seq_line(s, last_i, i); > - last_i = i + 1; > - } > - } > - /* Check if there was a partial line. */ > - if (last_i < size) { > - printk_nmi_flush_seq_line(s, last_i, size - 1); > - printk_nmi_flush_line("\n", strlen("\n")); > - } > + i += printk_nmi_flush_buffer(s->buffer + i, len - i); > > /* > * Check that nothing has got added in the meantime and truncate