From patchwork Mon Nov 14 16:29:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 19893 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2238260wru; Mon, 14 Nov 2022 08:31:38 -0800 (PST) X-Google-Smtp-Source: AA0mqf5hDPXnNzVwAdL76aTX85sVNDXhb3hU35YrJlviGreXQF0yIxnIXSe2bKVwFMzXQxxExCcs X-Received: by 2002:a17:90a:8a15:b0:211:b2b0:25d8 with SMTP id w21-20020a17090a8a1500b00211b2b025d8mr14116625pjn.54.1668443497708; Mon, 14 Nov 2022 08:31:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668443497; cv=none; d=google.com; s=arc-20160816; b=ZI513Yuog9WLprNG7oJoRiAs4Ir7vLmlq0uDcSdDcyp3dDJH1iQVHi0U47EBOxfpje I8UwO/2R68h4yBooUPEY33ZXIcecvjwWxX6TyKGUD8ReS+93WjtWdEbgsjZLO+ic3oHE ki6Gx2oCydalh56ngv3hXmNI9LZAXJZBaV/WFEl8ZASdHPMobyvd122KZhjyDkDxEBKo H0GezFQXXsp7yhXxswoE73Cqb/tYnpWz3h0ObGkBPnQ0Y+ihNT1p+iQLhfN4DL4ArKnC 3OJ2iLuvBDkCxmDgbj+VQVFiBfku9j+ATR5CCQLa+IsjerGrg18VIZTSEse9wP3tCORb wquw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=L7LcLWC5s4F/RyK0sKOkJZmEcWHo6be8Q4Ot+G43/3M=; b=PDfCXUZ8PbN1PEkoZkycEbJymnWf8HlrqdAAXHK9dypvNm4PkzWJV0VRODYjJs9rVO TLDgkGr2VW9W9kg5LYPaYfsbxUfCNyrpcluiyoeN+hGfHeKvL/TUcmlXpxCRmmgWK0Iz Gods+lbyZHP1vfJBvtfViZiYPtogu7w7mmneu5KpUCDJ7pNm35xQGVs1cZcKCQSAEjI4 99F0tkowSpPGoCS3j0YBf9q7auSVWDXKW37LoQDhgG7fh/BA0eoBFeu5Ft0w/qMEj89u QrxFlpfqdDTzJ2HHAft4dBJlJrWpJVgplNPlfpUeeYKShV9A72eRJp47paOaZ5Qs0o4e DdqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=OFAAu1wQ; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hk11-20020a17090b224b00b001fd7d02c976si17361333pjb.87.2022.11.14.08.30.58; Mon, 14 Nov 2022 08:31:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=OFAAu1wQ; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237663AbiKNQad (ORCPT + 99 others); Mon, 14 Nov 2022 11:30:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35760 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237556AbiKNQ3q (ORCPT ); Mon, 14 Nov 2022 11:29:46 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 351252AC3 for ; Mon, 14 Nov 2022 08:29:42 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1668443380; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=L7LcLWC5s4F/RyK0sKOkJZmEcWHo6be8Q4Ot+G43/3M=; b=OFAAu1wQb8JE+XQ07aM7bHXpc19dnsNC9yP9c5ZzXx0bfjLiMGQMjE6XrwxsAuBePR1K/M pyNi/LcjvAOsZj/jSUTUV+xS+OQ4ZZxMr1DMwMwK4D3yNi4UIQ6TMKVXuaK6BFH9e6t/iQ EQ8OY/nZsNth+EEUYNm4rhKMDSO7OP61uITfT7JsR4MRgnwGEd9vaYKcQFi4tCFnwFcHGW ydrrAsYmcRcg6e/Z5lis67e33vbUoqquYc3Nl6e1ksQUn0pQBooYox8BTjEiUIwB55DkEf rh8Q0CHfk5ZG19dJYpL4EW2UlQfkjmtfiFlmiraf/6esqJmj4W4S0Ef/rRr/dA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1668443380; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=L7LcLWC5s4F/RyK0sKOkJZmEcWHo6be8Q4Ot+G43/3M=; b=7sIkrDBZIW1O7yy5M+v1Q9mn5c0wOFxQxxNvWh3c0Ti2f1yIPuMOd+HPBIX0rDpXWU6XpO JI5l6JIzCl/kT3Cw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 14/39] printk: console_flush_all: use srcu console list iterator Date: Mon, 14 Nov 2022 17:35:07 +0106 Message-Id: <20221114162932.141883-15-john.ogness@linutronix.de> In-Reply-To: <20221114162932.141883-1-john.ogness@linutronix.de> References: <20221114162932.141883-1-john.ogness@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,INVALID_DATE_TZ_ABSURD, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749489808888716658?= X-GMAIL-MSGID: =?utf-8?q?1749489808888716658?= Guarantee safe iteration of the console list by using SRCU. Note that in the case of a handover, the SRCU read lock is also released. This is documented in the function description and as comments in the code. It is a bit tricky, but this preserves the lockdep lock ordering for the context handing over the console_lock: console_lock() | mutex_acquire(&console_lock_dep_map) <-- console lock | console_unlock() | console_flush_all() | | srcu_read_lock(&console_srcu) <-- srcu lock | | console_emit_next_record() | | | console_lock_spinning_disable_and_check() | | | | srcu_read_unlock(&console_srcu) <-- srcu unlock | | | | mutex_release(&console_lock_dep_map) <-- console unlock Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 50 +++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 41c90b768c1c..b37ca16abbd0 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -1893,13 +1893,13 @@ static void console_lock_spinning_enable(void) * safe to start busy waiting for the lock. Second, it checks if * there is a busy waiter and passes the lock rights to her. * - * Important: Callers lose the lock if there was a busy waiter. - * They must not touch items synchronized by console_lock - * in this case. + * Important: Callers lose both the console_lock and the SRCU read lock if + * there was a busy waiter. They must not touch items synchronized by + * console_lock or SRCU read lock in this case. * * Return: 1 if the lock rights were passed, 0 otherwise. */ -static int console_lock_spinning_disable_and_check(void) +static int console_lock_spinning_disable_and_check(int cookie) { int waiter; @@ -1918,6 +1918,12 @@ static int console_lock_spinning_disable_and_check(void) spin_release(&console_owner_dep_map, _THIS_IP_); + /* + * Preserve lockdep lock ordering. Release the SRCU read lock before + * releasing the console_lock. + */ + console_srcu_read_unlock(cookie); + /* * Hand off console_lock to waiter. The waiter will perform * the up(). After this, the waiter is the console_lock owner. @@ -2401,7 +2407,7 @@ static ssize_t msg_print_ext_body(char *buf, size_t size, char *text, size_t text_len, struct dev_printk_info *dev_info) { return 0; } static void console_lock_spinning_enable(void) { } -static int console_lock_spinning_disable_and_check(void) { return 0; } +static int console_lock_spinning_disable_and_check(int cookie) { return 0; } static void call_console_driver(struct console *con, const char *text, size_t len, char *dropped_text) { @@ -2743,16 +2749,18 @@ static void __console_unlock(void) * DROPPED_TEXT_MAX. Otherwise @dropped_text must be NULL. * * @handover will be set to true if a printk waiter has taken over the - * console_lock, in which case the caller is no longer holding the - * console_lock. Otherwise it is set to false. + * console_lock, in which case the caller is no longer holding both the + * console_lock and the SRCU read lock. Otherwise it is set to false. + * + * @cookie is the cookie from the SRCU read lock. * * Returns false if the given console has no next record to print, otherwise * true. * - * Requires the console_lock. + * Requires the console_lock and the SRCU read lock. */ static bool console_emit_next_record(struct console *con, char *text, char *ext_text, - char *dropped_text, bool *handover) + char *dropped_text, bool *handover, int cookie) { static int panic_console_dropped; struct printk_info info; @@ -2812,7 +2820,7 @@ static bool console_emit_next_record(struct console *con, char *text, char *ext_ con->seq++; - *handover = console_lock_spinning_disable_and_check(); + *handover = console_lock_spinning_disable_and_check(cookie); printk_safe_exit_irqrestore(flags); skip: return true; @@ -2849,6 +2857,7 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove bool any_usable = false; struct console *con; bool any_progress; + int cookie; *next_seq = 0; *handover = false; @@ -2856,23 +2865,29 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove do { any_progress = false; - for_each_console(con) { + cookie = console_srcu_read_lock(); + for_each_console_srcu(con) { bool progress; if (!console_is_usable(con)) continue; any_usable = true; - if (con->flags & CON_EXTENDED) { + if (console_srcu_read_flags(con) & CON_EXTENDED) { /* Extended consoles do not print "dropped messages". */ progress = console_emit_next_record(con, &text[0], &ext_text[0], NULL, - handover); + handover, cookie); } else { progress = console_emit_next_record(con, &text[0], NULL, &dropped_text[0], - handover); + handover, cookie); } + + /* + * If a handover has occurred, the SRCU read lock + * is already released. + */ if (*handover) return false; @@ -2886,14 +2901,19 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove /* Allow panic_cpu to take over the consoles safely. */ if (abandon_console_lock_in_panic()) - return false; + goto abandon; if (do_cond_resched) cond_resched(); } + console_srcu_read_unlock(cookie); } while (any_progress); return any_usable; + +abandon: + console_srcu_read_unlock(cookie); + return false; } /**