Skip to content

Commit ad8cd1d

Browse files
ozbenhpmladek
authored andcommitted
printk: Move console matching logic into a separate function
This moves the loop that tries to match a newly registered console with the command line or add_preferred_console list into a separate helper, in order to be able to call it multiple times in subsequent patches. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Benjamin Herrenschmidt <[email protected]> Reviewed-by: Sergey Senozhatsky <[email protected]> Reviewed-by: Petr Mladek <[email protected]> Signed-off-by: Petr Mladek <[email protected]>
1 parent a4fe2b4 commit ad8cd1d

File tree

1 file changed

+65
-40
lines changed

1 file changed

+65
-40
lines changed

kernel/printk/printk.c

Lines changed: 65 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ static struct console *exclusive_console;
280280
static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
281281

282282
static int preferred_console = -1;
283+
static bool has_preferred_console;
283284
int console_set_on_cmdline;
284285
EXPORT_SYMBOL(console_set_on_cmdline);
285286

@@ -2626,6 +2627,60 @@ static int __init keep_bootcon_setup(char *str)
26262627

26272628
early_param("keep_bootcon", keep_bootcon_setup);
26282629

2630+
/*
2631+
* This is called by register_console() to try to match
2632+
* the newly registered console with any of the ones selected
2633+
* by either the command line or add_preferred_console() and
2634+
* setup/enable it.
2635+
*
2636+
* Care need to be taken with consoles that are statically
2637+
* enabled such as netconsole
2638+
*/
2639+
static int try_enable_new_console(struct console *newcon)
2640+
{
2641+
struct console_cmdline *c;
2642+
int i;
2643+
2644+
for (i = 0, c = console_cmdline;
2645+
i < MAX_CMDLINECONSOLES && c->name[0];
2646+
i++, c++) {
2647+
if (!newcon->match ||
2648+
newcon->match(newcon, c->name, c->index, c->options) != 0) {
2649+
/* default matching */
2650+
BUILD_BUG_ON(sizeof(c->name) != sizeof(newcon->name));
2651+
if (strcmp(c->name, newcon->name) != 0)
2652+
continue;
2653+
if (newcon->index >= 0 &&
2654+
newcon->index != c->index)
2655+
continue;
2656+
if (newcon->index < 0)
2657+
newcon->index = c->index;
2658+
2659+
if (_braille_register_console(newcon, c))
2660+
return 0;
2661+
2662+
if (newcon->setup &&
2663+
newcon->setup(newcon, c->options) != 0)
2664+
return -EIO;
2665+
}
2666+
newcon->flags |= CON_ENABLED;
2667+
if (i == preferred_console) {
2668+
newcon->flags |= CON_CONSDEV;
2669+
has_preferred_console = true;
2670+
}
2671+
return 0;
2672+
}
2673+
2674+
/*
2675+
* Some consoles, such as pstore and netconsole, can be enabled even
2676+
* without matching.
2677+
*/
2678+
if (newcon->flags & CON_ENABLED)
2679+
return 0;
2680+
2681+
return -ENOENT;
2682+
}
2683+
26292684
/*
26302685
* The console driver calls this routine during kernel initialization
26312686
* to register the console printing procedure with printk() and to
@@ -2647,11 +2702,9 @@ early_param("keep_bootcon", keep_bootcon_setup);
26472702
*/
26482703
void register_console(struct console *newcon)
26492704
{
2650-
int i;
26512705
unsigned long flags;
26522706
struct console *bcon = NULL;
2653-
struct console_cmdline *c;
2654-
static bool has_preferred;
2707+
int err;
26552708

26562709
if (console_drivers)
26572710
for_each_console(bcon)
@@ -2678,63 +2731,35 @@ void register_console(struct console *newcon)
26782731
if (console_drivers && console_drivers->flags & CON_BOOT)
26792732
bcon = console_drivers;
26802733

2681-
if (!has_preferred || bcon || !console_drivers)
2682-
has_preferred = preferred_console >= 0;
2734+
if (!has_preferred_console || bcon || !console_drivers)
2735+
has_preferred_console = preferred_console >= 0;
26832736

26842737
/*
26852738
* See if we want to use this console driver. If we
26862739
* didn't select a console we take the first one
26872740
* that registers here.
26882741
*/
2689-
if (!has_preferred) {
2742+
if (!has_preferred_console) {
26902743
if (newcon->index < 0)
26912744
newcon->index = 0;
26922745
if (newcon->setup == NULL ||
26932746
newcon->setup(newcon, NULL) == 0) {
26942747
newcon->flags |= CON_ENABLED;
26952748
if (newcon->device) {
26962749
newcon->flags |= CON_CONSDEV;
2697-
has_preferred = true;
2750+
has_preferred_console = true;
26982751
}
26992752
}
27002753
}
27012754

27022755
/*
2703-
* See if this console matches one we selected on
2704-
* the command line.
2756+
* See if this console matches one we selected on
2757+
* the command line or if it was statically enabled
27052758
*/
2706-
for (i = 0, c = console_cmdline;
2707-
i < MAX_CMDLINECONSOLES && c->name[0];
2708-
i++, c++) {
2709-
if (!newcon->match ||
2710-
newcon->match(newcon, c->name, c->index, c->options) != 0) {
2711-
/* default matching */
2712-
BUILD_BUG_ON(sizeof(c->name) != sizeof(newcon->name));
2713-
if (strcmp(c->name, newcon->name) != 0)
2714-
continue;
2715-
if (newcon->index >= 0 &&
2716-
newcon->index != c->index)
2717-
continue;
2718-
if (newcon->index < 0)
2719-
newcon->index = c->index;
2720-
2721-
if (_braille_register_console(newcon, c))
2722-
return;
2723-
2724-
if (newcon->setup &&
2725-
newcon->setup(newcon, c->options) != 0)
2726-
break;
2727-
}
2728-
2729-
newcon->flags |= CON_ENABLED;
2730-
if (i == preferred_console) {
2731-
newcon->flags |= CON_CONSDEV;
2732-
has_preferred = true;
2733-
}
2734-
break;
2735-
}
2759+
err = try_enable_new_console(newcon);
27362760

2737-
if (!(newcon->flags & CON_ENABLED))
2761+
/* printk() messages are not printed to the Braille console. */
2762+
if (err || newcon->flags & CON_BRL)
27382763
return;
27392764

27402765
/*

0 commit comments

Comments
 (0)