Received: by 2002:ac0:950c:0:0:0:0:0 with SMTP id f12csp1030679imc; Mon, 11 Mar 2019 05:05:36 -0700 (PDT) X-Google-Smtp-Source: APXvYqxNzyWq20KSrvVaA3SEa9omamuxy18k+rEBuQjpgeiniM2JDs1Fmm6f7+8MaM0o57jFHe/k X-Received: by 2002:a17:902:27a8:: with SMTP id d37mr33484011plb.164.1552305936530; Mon, 11 Mar 2019 05:05:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552305936; cv=none; d=google.com; s=arc-20160816; b=esmIJtY/OXWZ7lOawlNGV3m+saFsq2xGp61LtK9wJKg2izMLmafTQMNQqSWpESWu/a D5PRj7iZzQs0+imD6fCZHAGOC3/S+kW/rB6B1bo7jxnDN50bssSsi8BXlKi2hXazMd9N 54/Dm5SYY0RjRTLfX/EfLIhA91RH7zStsqlD98Wu8nMj5aCCwWL1T0KE8TeUBTeXQ/zJ wXdld8/paYE0+3ksQg/uBMgW1wvBRE3nfVdKpR8p/o5Ika0bTJ7AeKjJ25+uWRN9py7P 0NIuZvZ5+dbpn3YtDkBABCDS6sLWLHsP0xQ3QrFA+r2WKInZSYSpzFQeoCHcMKdIQbW4 L1BA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:message-id :in-reply-to:date:references:subject:cc:to:from; bh=oo+b4jAQMl/vRghfEjWShtgd1q5hM1jYsRDK/5EKoEg=; b=0OE6E3NnQKSQhnkaz1mBWxomYNNdn/f5ROxohhFLXqJ60rkyLFhX0hsDlwWBuNEKd9 O5s9fd0TUnYRNv6AbWSu25+L3dRTrY0HhJxvAizb2wRtdleIn3U7pdmpq5UKC/gG5meb 1ewipy248EuxNy9tGkuwvES+gWDcMwoO1NfpSDgqA8L+YUag6KB2P71qo5RtGrIKVJmM 6H7SVUdFLhhRD7hok3O9uy+5grmsAtt02yyjZCWP/tVuqE4tH5T/QWTBoTagecXvb8r0 fC3ZNi4JYtttY5Ua2P37u6gqXqDvFSnGnTJKL627ojhVUSJqvfvIg3UhQ1khmrKQ9iaK HdPg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i69si4842027pge.103.2019.03.11.05.05.20; Mon, 11 Mar 2019 05:05:36 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727361AbfCKMEo (ORCPT + 99 others); Mon, 11 Mar 2019 08:04:44 -0400 Received: from Galois.linutronix.de ([146.0.238.70]:38216 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726592AbfCKMEn (ORCPT ); Mon, 11 Mar 2019 08:04:43 -0400 Received: from localhost ([127.0.0.1] helo=vostro.local) by Galois.linutronix.de with esmtp (Exim 4.80) (envelope-from ) id 1h3JfS-0003L7-Ag; Mon, 11 Mar 2019 13:04:30 +0100 From: John Ogness To: Petr Mladek Cc: Sergey Senozhatsky , linux-kernel@vger.kernel.org, Peter Zijlstra , Steven Rostedt , Daniel Wang , Andrew Morton , Linus Torvalds , Greg Kroah-Hartman , Alan Cox , Jiri Slaby , Peter Feiner , linux-serial@vger.kernel.org, Sergey Senozhatsky Subject: Re: [RFC PATCH v1 19/25] printk: introduce emergency messages References: <20190212143003.48446-1-john.ogness@linutronix.de> <20190212143003.48446-20-john.ogness@linutronix.de> <20190307073029.GA489@jagdpanzerIV> <20190308103127.txsgv3d6lqlf6pad@pathway.suse.cz> Date: Mon, 11 Mar 2019 13:04:28 +0100 In-Reply-To: <20190308103127.txsgv3d6lqlf6pad@pathway.suse.cz> (Petr Mladek's message of "Fri, 8 Mar 2019 11:31:27 +0100") Message-ID: <87k1h5zkfn.fsf@linutronix.de> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2019-03-08, Petr Mladek wrote: >>> +static bool console_can_emergency(int level) >>> +{ >>> + struct console *con; >>> + >>> + for_each_console(con) { >>> + if (!(con->flags & CON_ENABLED)) >>> + continue; >>> + if (con->write_atomic && level < emergency_console_loglevel) >>> + return true; >>> + if (con->write && (con->flags & CON_BOOT)) >>> + return true; >>> + } >>> + return false; >>> +} >>> + >>> +static void call_emergency_console_drivers(int level, const char *text, >>> + size_t text_len) >>> +{ >>> + struct console *con; >>> + >>> + for_each_console(con) { >>> + if (!(con->flags & CON_ENABLED)) >>> + continue; >>> + if (con->write_atomic && level < emergency_console_loglevel) { >>> + con->write_atomic(con, text, text_len); >>> + continue; >>> + } >>> + if (con->write && (con->flags & CON_BOOT)) { >>> + con->write(con, text, text_len); >>> + continue; >>> + } >>> + } >>> +} >>> + >>> +static void printk_emergency(char *buffer, int level, u64 ts_nsec, u16 cpu, >>> + char *text, u16 text_len) >>> +{ >>> + struct printk_log msg; >>> + size_t prefix_len; >>> + >>> + if (!console_can_emergency(level)) >>> + return; >>> + >>> + msg.level = level; >>> + msg.ts_nsec = ts_nsec; >>> + msg.cpu = cpu; >>> + msg.facility = 0; >>> + >>> + /* "text" must have PREFIX_MAX preceding bytes available */ >>> + >>> + prefix_len = print_prefix(&msg, >>> + console_msg_format & MSG_FORMAT_SYSLOG, >>> + printk_time, buffer); >>> + /* move the prefix forward to the beginning of the message text */ >>> + text -= prefix_len; >>> + memmove(text, buffer, prefix_len); >>> + text_len += prefix_len; >>> + >>> + text[text_len++] = '\n'; >>> + >>> + call_emergency_console_drivers(level, text, text_len); >> >> So this iterates the console list and calls consoles' callbacks, but >> what prevents console driver to be rmmod-ed under us? >> >> CPU0 CPU1 >> >> printk_emergency() rmmod netcon >> call_emergency_console_drivers() >> con_foo->flags & CON_ENABLED == 1 >> unregister_console(con_foo) >> con_foo->flags &= ~CON_ENABLED >> __exit // con_foo gone ? >> con_foo->write() >> >> We use console_lock()/console_trylock() in order to protect the list >> and console drivers; but this brings scheduler to the picture, with >> all its locks. > > Great catch! Yes, thanks! > I think that it is doable to guard the list using RCU. I think it would be enough to take the prb_cpulock when modifying the console linked list. That will keep printk_emergency() out until the list has been updated. (registering/unregistering consoles is not something that happens often.) John Ogness