Skip to content

Commit 5e46765

Browse files
committed
printk: re-organize log_output() to be more legible
Avoid some duplicate logic now that we can return early, and update the comments for the new LOG_CONT world order. This also stops the continuation flushing from just using random record flags for the flushing action, instead taking the flags from the proper original line and updating them as we add continuations to it. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent c362c7f commit 5e46765

1 file changed

Lines changed: 27 additions & 35 deletions

File tree

‎kernel/printk/printk.c‎

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,35 +1640,33 @@ static struct cont {
16401640
bool flushed:1; /* buffer sealed and committed */
16411641
} cont;
16421642

1643-
static void cont_flush(enum log_flags flags)
1643+
static void cont_flush(void)
16441644
{
16451645
if (cont.flushed)
16461646
return;
16471647
if (cont.len == 0)
16481648
return;
1649-
16501649
if (cont.cons) {
16511650
/*
16521651
* If a fragment of this line was directly flushed to the
16531652
* console; wait for the console to pick up the rest of the
16541653
* line. LOG_NOCONS suppresses a duplicated output.
16551654
*/
1656-
log_store(cont.facility, cont.level, flags | LOG_NOCONS,
1655+
log_store(cont.facility, cont.level, cont.flags | LOG_NOCONS,
16571656
cont.ts_nsec, NULL, 0, cont.buf, cont.len);
1658-
cont.flags = flags;
16591657
cont.flushed = true;
16601658
} else {
16611659
/*
16621660
* If no fragment of this line ever reached the console,
16631661
* just submit it to the store and free the buffer.
16641662
*/
1665-
log_store(cont.facility, cont.level, flags, 0,
1663+
log_store(cont.facility, cont.level, cont.flags, 0,
16661664
NULL, 0, cont.buf, cont.len);
16671665
cont.len = 0;
16681666
}
16691667
}
16701668

1671-
static bool cont_add(int facility, int level, const char *text, size_t len)
1669+
static bool cont_add(int facility, int level, enum log_flags flags, const char *text, size_t len)
16721670
{
16731671
if (cont.len && cont.flushed)
16741672
return false;
@@ -1679,7 +1677,7 @@ static bool cont_add(int facility, int level, const char *text, size_t len)
16791677
* the line gets too long, split it up in separate records.
16801678
*/
16811679
if (nr_ext_console_drivers || cont.len + len > sizeof(cont.buf)) {
1682-
cont_flush(LOG_CONT);
1680+
cont_flush();
16831681
return false;
16841682
}
16851683

@@ -1688,16 +1686,23 @@ static bool cont_add(int facility, int level, const char *text, size_t len)
16881686
cont.level = level;
16891687
cont.owner = current;
16901688
cont.ts_nsec = local_clock();
1691-
cont.flags = 0;
1689+
cont.flags = flags;
16921690
cont.cons = 0;
16931691
cont.flushed = false;
16941692
}
16951693

16961694
memcpy(cont.buf + cont.len, text, len);
16971695
cont.len += len;
16981696

1697+
// The original flags come from the first line,
1698+
// but later continuations can add a newline.
1699+
if (flags & LOG_NEWLINE) {
1700+
cont.flags |= LOG_NEWLINE;
1701+
cont_flush();
1702+
}
1703+
16991704
if (cont.len > (sizeof(cont.buf) * 80) / 100)
1700-
cont_flush(LOG_CONT);
1705+
cont_flush();
17011706

17021707
return true;
17031708
}
@@ -1732,39 +1737,26 @@ static size_t cont_print_text(char *text, size_t size)
17321737

17331738
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)
17341739
{
1735-
if (!(lflags & LOG_NEWLINE)) {
1736-
/*
1737-
* Flush the conflicting buffer. An earlier newline was missing,
1738-
* or another task also prints continuation lines.
1739-
*/
1740-
if (cont.len && (!(lflags & LOG_CONT) || cont.owner != current))
1741-
cont_flush(LOG_NEWLINE);
1742-
1743-
/* buffer line if possible, otherwise store it right away */
1744-
if (cont_add(facility, level, text, text_len))
1745-
return text_len;
1746-
1747-
return log_store(facility, level, lflags | LOG_CONT, 0, dict, dictlen, text, text_len);
1748-
}
1749-
17501740
/*
1751-
* If an earlier newline was missing and it was the same task,
1752-
* either merge it with the current buffer and flush, or if
1753-
* there was a race with interrupts (prefix == true) then just
1754-
* flush it out and store this line separately.
1755-
* If the preceding printk was from a different task and missed
1756-
* a newline, flush and append the newline.
1741+
* If an earlier line was buffered, and we're a continuation
1742+
* write from the same process, try to add it to the buffer.
17571743
*/
17581744
if (cont.len) {
1759-
bool stored = false;
1745+
if (cont.owner == current && (lflags & LOG_CONT)) {
1746+
if (cont_add(facility, level, lflags, text, text_len))
1747+
return text_len;
1748+
}
1749+
/* Otherwise, make sure it's flushed */
1750+
cont_flush();
1751+
}
17601752

1761-
if (cont.owner == current && (lflags & LOG_CONT))
1762-
stored = cont_add(facility, level, text, text_len);
1763-
cont_flush(LOG_NEWLINE);
1764-
if (stored)
1753+
/* If it doesn't end in a newline, try to buffer the current line */
1754+
if (!(lflags & LOG_NEWLINE)) {
1755+
if (cont_add(facility, level, lflags, text, text_len))
17651756
return text_len;
17661757
}
17671758

1759+
/* Store it in the record log */
17681760
return log_store(facility, level, lflags, 0, dict, dictlen, text, text_len);
17691761
}
17701762

0 commit comments

Comments
 (0)