From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.9 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 593CFC43218 for ; Fri, 26 Apr 2019 05:33:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1B23E21479 for ; Fri, 26 Apr 2019 05:33:17 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="u66RsWG0" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726255AbfDZFdQ (ORCPT ); Fri, 26 Apr 2019 01:33:16 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:45418 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725854AbfDZFdP (ORCPT ); Fri, 26 Apr 2019 01:33:15 -0400 Received: by mail-pg1-f196.google.com with SMTP id y27so302905pgl.12 for ; Thu, 25 Apr 2019 22:33:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=B3a4/fKTHTXdtHCuFcpfY4dukjeDm34PKGaxXB15JjI=; b=u66RsWG0SJXgerjqCUxP/SlDZMpQWKbdkwH2vJ0VajMN512FbIXnnFnY+AvHlr/46l XvvgUQw/kEO1oZ6soeGAebaVIwwxDXL/Yd+BJ5IiTdKtemcfcX8tiI3gB6JPnL0H3P2+ z5SLC42Srwb8VcnAPYEgFO0t8lI1vOr8ktd5Ib3B3mPGgdL8V4Xo5rG5ufh7KG7iD5/c jHO2qrIylCD2r8uxYB8MYBKgMmXfVXK+tH33RKkMjpXvqpWwmI1a2k+UddGSfQ5k4QcN cmWsH+39iE0W8ckrYvxC1uE4WjeohGIU1YEKddNmBJikZXKYFYMNN0vz82mn83uoAufn E1Ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=B3a4/fKTHTXdtHCuFcpfY4dukjeDm34PKGaxXB15JjI=; b=og7fMwBxMA+1pSJv5ECr0rqs9Vhfc/t9Rj0sOzr3mAJl8zdsi2444cbhvGuOm8udsO Mi9LrxV8WkL4rB3QRP7D5+DveE8NFsQbh+eotYIVA03YIBbEeF3+nTt/VfzjdNpKQwaw XqF1j9CjCEov5MjeJAYrjwmEhD4Eq1QpU173bbRUvmp8jppYFPSqDvFyTWbyas7kzK6/ UesXZPIivWaFqZ/bKl59uq8oce2f37/vQrZYVQ3PZS/DDCljGZwlEDGfWaeMws47SClr AdfT+FYxoUopcYewFCS7H7ml5adB1iKFheUeGC6gpBRbEb5ssHRcWwSmXMxn4oyzOQoq j9Zw== X-Gm-Message-State: APjAAAWBrkIDRYC3+HS+0rX/ZKG/h38ICzVVICq+kMyK1f6f7QZH3SFt OEtfIF4ZleoWrbN9fCJ+b20= X-Google-Smtp-Source: APXvYqxfgH4rGzl2v/wo4+6r6/qlvjLX9KJ/O5y85O0HQ15kQ7Ll0kFgwwPXnUezIhv0k/X2pu1jFA== X-Received: by 2002:a63:2943:: with SMTP id p64mr38892102pgp.151.1556256794752; Thu, 25 Apr 2019 22:33:14 -0700 (PDT) Received: from localhost.localdomain ([110.70.55.171]) by smtp.gmail.com with ESMTPSA id j5sm5256532pfg.186.2019.04.25.22.33.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 25 Apr 2019 22:33:13 -0700 (PDT) From: Sergey Senozhatsky X-Google-Original-From: Sergey Senozhatsky To: Petr Mladek , Steven Rostedt Cc: Andrew Morton , linux-kernel@vger.kernel.org, Sergey Senozhatsky , Sergey Senozhatsky Subject: [PATCHv2 1/4] printk: factor out __unregister_console() code Date: Fri, 26 Apr 2019 14:32:59 +0900 Message-Id: <20190426053302.4332-2-sergey.senozhatsky@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190426053302.4332-1-sergey.senozhatsky@gmail.com> References: <20190426053302.4332-1-sergey.senozhatsky@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following pattern in register_console() is not completely safe: for_each_console(bcon) if (bcon->flags & CON_BOOT) unregister_console(bcon); Because, in theory, console drivers list and console drivers can be modified concurrently from another CPU. We need to grab console_sem lock, which protects console drivers list and console drivers, before we start iterating console drivers list. Factor out __unregister_console(), which will be called from unregister_console() and register_console(), in both cases under console_sem lock. Signed-off-by: Sergey Senozhatsky --- kernel/printk/printk.c | 98 ++++++++++++++++++++++++------------------ 1 file changed, 56 insertions(+), 42 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 17102fd4c136..b0e361ca1bea 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2605,6 +2605,48 @@ static int __init keep_bootcon_setup(char *str) early_param("keep_bootcon", keep_bootcon_setup); +static int __unregister_console(struct console *console) +{ + struct console *a, *b; + int res; + + pr_info("%sconsole [%s%d] disabled\n", + (console->flags & CON_BOOT) ? "boot" : "", + console->name, console->index); + + res = _braille_unregister_console(console); + if (res) + return res; + + res = 1; + if (console_drivers == console) { + console_drivers = console->next; + res = 0; + } else if (console_drivers) { + for (a = console_drivers->next, b = console_drivers; + a; b = a, a = b->next) { + if (a == console) { + b->next = a->next; + res = 0; + break; + } + } + } + + if (!res && (console->flags & CON_EXTENDED)) + nr_ext_console_drivers--; + + /* + * If this isn't the last console and it has CON_CONSDEV set, we + * need to set it on the next preferred console. + */ + if (console_drivers != NULL && console->flags & CON_CONSDEV) + console_drivers->flags |= CON_CONSDEV; + + console->flags &= ~CON_ENABLED; + return res; +} + /* * The console driver calls this routine during kernel initialization * to register the console printing procedure with printk() and to @@ -2777,62 +2819,34 @@ void register_console(struct console *newcon) pr_info("%sconsole [%s%d] enabled\n", (newcon->flags & CON_BOOT) ? "boot" : "" , newcon->name, newcon->index); - if (bcon && - ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) && - !keep_bootcon) { - /* We need to iterate through all boot consoles, to make + + if (keep_bootcon) + return; + + if (bcon && (newcon->flags & (CON_CONSDEV|CON_BOOT)) == CON_CONSDEV) { + console_lock(); + /* + * We need to iterate through all boot consoles, to make * sure we print everything out, before we unregister them. */ for_each_console(bcon) if (bcon->flags & CON_BOOT) - unregister_console(bcon); + __unregister_console(bcon); + console_unlock(); + console_sysfs_notify(); } } EXPORT_SYMBOL(register_console); int unregister_console(struct console *console) { - struct console *a, *b; - int res; - - pr_info("%sconsole [%s%d] disabled\n", - (console->flags & CON_BOOT) ? "boot" : "" , - console->name, console->index); - - res = _braille_unregister_console(console); - if (res) - return res; + int ret; - res = 1; console_lock(); - if (console_drivers == console) { - console_drivers=console->next; - res = 0; - } else if (console_drivers) { - for (a=console_drivers->next, b=console_drivers ; - a; b=a, a=b->next) { - if (a == console) { - b->next = a->next; - res = 0; - break; - } - } - } - - if (!res && (console->flags & CON_EXTENDED)) - nr_ext_console_drivers--; - - /* - * If this isn't the last console and it has CON_CONSDEV set, we - * need to set it on the next preferred console. - */ - if (console_drivers != NULL && console->flags & CON_CONSDEV) - console_drivers->flags |= CON_CONSDEV; - - console->flags &= ~CON_ENABLED; + ret = __unregister_console(console); console_unlock(); console_sysfs_notify(); - return res; + return ret; } EXPORT_SYMBOL(unregister_console); -- 2.21.0