From patchwork Mon Nov 6 21:07:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 162195 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp2928244vqu; Mon, 6 Nov 2023 13:08:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IEnIY5UoC3ZDqwr0E7HrZKude5l/j5xCbFS4e+brxRTkxLnGXIhcw7zqocvAYGHFRqJdiX/ X-Received: by 2002:a17:902:bd05:b0:1ca:dee1:4d1a with SMTP id p5-20020a170902bd0500b001cadee14d1amr24345585pls.25.1699304901176; Mon, 06 Nov 2023 13:08:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699304901; cv=none; d=google.com; s=arc-20160816; b=JjPqvV7K8xGCB3K1fMumxQe/Q6v4zMrrgVhBB3EzW0rnyzTwAvwwxodfgJu3Uq1NVr SNyvzw7hNA31N5U5eXJap1cbUkGqvW/3PzcqS8eHFu2FEWDmAg8C4/ZPhTJFO7l2Dg8f hItUTWH8KRyvblReVQ5WdHU3jFJCfwDnbz42kq1Qd7gSdBM6drHHQ83WXuYKibCPIsZP 3R4P/z1LXspEbwQwY85pI8K6IAk62YWbv06MZj3KKhZVxRxEjKI9fFFtPrwptKZY5vXl bNkR0VeMsyXRyaV4qmtizJwtRpjui4y/qIhxDqo69D75a9nsC3zuHLecGilluWdgBf3d zAkA== 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=z3f4HQf7ma/ip4YULZcgH7RuMtHihS3FcdUbZrma7Uk=; fh=SH94mmwiRuycNuXnyBv4dc1kk0FqoQ65LgesulXvwvo=; b=mmnjGoPd1bmOcrzxmx5Pg5vGxnRXapMZoaUOWxnxqDfY53h7WggsWV5Er6YIb+gYKA m+eWbnS6Wq0jhUA+LFx1Xo6kAAYocxSzrDcGW0jFkZruvk0wiHcZFnFzorBS2fD6FoV3 1t/hlrb7ffcWWeIhQ4To+VcrDzd4SsiuikgBNN22pAb5cx0IQG8e5iUdfljgxdyYjwPv MuPv28TKens42zQWEKbWUfm3qUJpePtMxne5TdLiLos08VSTyY3/iuWJq4QGxXQPle9j vmv1pZFEXG8yNTpEHYywo/HAqMsaXQsPCn5FesvQDkrChBfctYJFRD+0R07K0s2SO++Z RidQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Ka1zOgQR; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=1c9aXZum; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 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 fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id w7-20020a170902e88700b001b8a67f1c10si9411533plg.468.2023.11.06.13.08.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 13:08:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Ka1zOgQR; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=1c9aXZum; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 5D65B80C6DEA; Mon, 6 Nov 2023 13:08:08 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233040AbjKFVHl (ORCPT + 34 others); Mon, 6 Nov 2023 16:07:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233014AbjKFVHi (ORCPT ); Mon, 6 Nov 2023 16:07:38 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE242D51 for ; Mon, 6 Nov 2023 13:07:34 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1699304853; 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=z3f4HQf7ma/ip4YULZcgH7RuMtHihS3FcdUbZrma7Uk=; b=Ka1zOgQRVwOC98UHvxmZ5RPErohmKs1H9jqjbO7+JzTjJeJqk7Md97RUUPigivGsEB481W oPlaHboW50PHEI3jAd8tARikwPbeXPSg12R2JjR0lJdqwr6PioNEHgRO37S3g8d4ElHquM fq+4TvhRAA2TDg7hrK8mYHFYMZkr1we6FfOsh+CnyL0VMD5FBSX3+ed9qjPQj8uBE04fnl e4VMyeBG4M0Afa5PwMJRi3tUVEavK73KAoWvrZQ7x1xfRAqG5yjoWt+MiOVjqZvihlsoFl mK4jH2PofoWJeOTPZ8EafGy75YOfZQblvEaaWbnTlvCQbwx6JP2NJvekIq0a3A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1699304853; 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=z3f4HQf7ma/ip4YULZcgH7RuMtHihS3FcdUbZrma7Uk=; b=1c9aXZumpnPAe5mtBK859fA03kvAfrkbOsMQkwd4RKKpZDEGwC4iFIy27xrg6l01nOCBp6 s6F2tGPdLd3fHXCA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Mukesh Ojha Subject: [PATCH printk v2 1/9] printk: ringbuffer: Do not skip non-finalized records with prb_next_seq() Date: Mon, 6 Nov 2023 22:13:22 +0106 Message-Id: <20231106210730.115192-2-john.ogness@linutronix.de> In-Reply-To: <20231106210730.115192-1-john.ogness@linutronix.de> References: <20231106210730.115192-1-john.ogness@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INVALID_DATE_TZ_ABSURD, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Mon, 06 Nov 2023 13:08:08 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781850336439357273 X-GMAIL-MSGID: 1781850336439357273 Commit f244b4dc53e5 ("printk: ringbuffer: Improve prb_next_seq() performance") introduced an optimization for prb_next_seq() by using best-effort to track recently finalized records. However, the order of finalization does not necessarily match the order of the records. The optimization changed prb_next_seq() to return inconsistent results, possibly yielding sequence numbers that are not available to readers because they are preceded by non-finalized records or they are not yet visible to the reader CPU. Rather than simply best-effort tracking recently finalized records, force the committing writer to read records and increment the last "contiguous block" of finalized records. In order to do this, the sequence number instead of ID must be stored because ID's cannot be directly compared. A new memory barrier pair is introduced to guarantee that a reader can always read the records up until the sequence number returned by prb_next_seq() (unless the records have since been overwritten in the ringbuffer). This restores the original functionality of prb_next_seq() while also keeping the optimization. For 32bit systems, only the lower 32 bits of the sequence number are stored. When reading the value, it is expanded to the full 64bit sequence number by folding in the value returned by prb_first_seq(). Fixes: f244b4dc53e5 ("printk: ringbuffer: Improve prb_next_seq() performance") Signed-off-by: John Ogness --- kernel/printk/printk_ringbuffer.c | 189 ++++++++++++++++++++++++------ kernel/printk/printk_ringbuffer.h | 4 +- 2 files changed, 153 insertions(+), 40 deletions(-) diff --git a/kernel/printk/printk_ringbuffer.c b/kernel/printk/printk_ringbuffer.c index fde338606ce8..94eede5356ac 100644 --- a/kernel/printk/printk_ringbuffer.c +++ b/kernel/printk/printk_ringbuffer.c @@ -6,6 +6,7 @@ #include #include #include "printk_ringbuffer.h" +#include "internal.h" /** * DOC: printk_ringbuffer overview @@ -303,6 +304,9 @@ * * desc_push_tail:B / desc_reserve:D * set descriptor reusable (state), then push descriptor tail (id) + * + * desc_update_last_finalized:A / desc_last_finalized_seq:A + * store finalized record, then set new highest finalized sequence number */ #define DATA_SIZE(data_ring) _DATA_SIZE((data_ring)->size_bits) @@ -1441,20 +1445,144 @@ bool prb_reserve_in_last(struct prb_reserved_entry *e, struct printk_ringbuffer return false; } +#ifdef CONFIG_64BIT + +#define __u64seq_to_ulseq(u64seq) (u64seq) +#define __ulseq_to_u64seq(ulseq) (ulseq) + +#else /* CONFIG_64BIT */ + +static u64 prb_first_seq(struct printk_ringbuffer *rb); + +#define __u64seq_to_ulseq(u64seq) ((u32)u64seq) +static inline u64 __ulseq_to_u64seq(u32 ulseq) +{ + u64 rb_first_seq = prb_first_seq(prb); + u64 seq; + + /* + * The provided sequence is only the lower 32 bits of the ringbuffer + * sequence. It needs to be expanded to 64bit. Get the first sequence + * number from the ringbuffer and fold it. + */ + seq = rb_first_seq - ((u32)rb_first_seq - ulseq); + + return seq; +} + +#endif /* CONFIG_64BIT */ + +/* + * @last_finalized_seq value guarantees that all records up to and including + * this sequence number are finalized and can be read. The only exception are + * too old records which have already been overwritten. + * + * It is also guaranteed that @last_finalized_seq only increases. + * + * Be aware that finalized records following non-finalized records are not + * reported because they are not yet available to the reader. For example, + * a new record stored via printk() will not be available to a printer if + * it follows a record that has not been finalized yet. However, once that + * non-finalized record becomes finalized, @last_finalized_seq will be + * appropriately updated and the full set of finalized records will be + * available to the printer. And since each printk() caller will either + * directly print or trigger deferred printing of all available unprinted + * records, all printk() messages will get printed. + */ +static u64 desc_last_finalized_seq(struct prb_desc_ring *desc_ring) +{ + unsigned long ulseq; + + /* + * Guarantee the sequence number is loaded before loading the + * associated record in order to guarantee that the record can be + * seen by this CPU. This pairs with desc_update_last_finalized:A. + */ + ulseq = atomic_long_read_acquire(&desc_ring->last_finalized_seq + ); /* LMM(desc_last_finalized_seq:A) */ + + return __ulseq_to_u64seq(ulseq); +} + +static bool _prb_read_valid(struct printk_ringbuffer *rb, u64 *seq, + struct printk_record *r, unsigned int *line_count); + +/* + * Check if there are records directly following @last_finalized_seq that are + * finalized. If so, update @last_finalized_seq to the latest of these + * records. It is not allowed to skip over records that are not yet finalized. + */ +static void desc_update_last_finalized(struct printk_ringbuffer *rb) +{ + struct prb_desc_ring *desc_ring = &rb->desc_ring; + u64 old_seq = desc_last_finalized_seq(desc_ring); + unsigned long oldval; + unsigned long newval; + u64 finalized_seq; + u64 try_seq; + +try_again: + finalized_seq = old_seq; + try_seq = finalized_seq + 1; + + /* Try to find later finalized records. */ + while (_prb_read_valid(rb, &try_seq, NULL, NULL)) { + finalized_seq = try_seq; + try_seq++; + } + + /* No update needed if no later finalized record was found. */ + if (finalized_seq == old_seq) + return; + + oldval = __u64seq_to_ulseq(old_seq); + newval = __u64seq_to_ulseq(finalized_seq); + + /* + * Set the sequence number of a later finalized record that has been + * seen. + * + * Guarantee the record data is visible to other CPUs before storing + * its sequence number. This pairs with desc_last_finalized_seq:A. + * + * Memory barrier involvement: + * + * If desc_last_finalized_seq:A reads from + * desc_update_last_finalized:A, then desc_read:A reads from + * _prb_commit:B. + * + * Relies on: + * + * RELEASE from _prb_commit:B to desc_update_last_finalized:A + * matching + * ACQUIRE from desc_last_finalized_seq:A to desc_read:A + * + * Note: _prb_commit:B and desc_update_last_finalized:A can be + * different CPUs. However, the desc_update_last_finalized:A + * CPU (which performs the release) must have previously seen + * _prb_commit:B. + */ + if (!atomic_long_try_cmpxchg_release(&desc_ring->last_finalized_seq, + &oldval, newval)) { /* LMM(desc_update_last_finalized:A) */ + old_seq = __ulseq_to_u64seq(oldval); + goto try_again; + } +} + /* * Attempt to finalize a specified descriptor. If this fails, the descriptor * is either already final or it will finalize itself when the writer commits. */ -static void desc_make_final(struct prb_desc_ring *desc_ring, unsigned long id) +static void desc_make_final(struct printk_ringbuffer *rb, unsigned long id) { + struct prb_desc_ring *desc_ring = &rb->desc_ring; unsigned long prev_state_val = DESC_SV(id, desc_committed); struct prb_desc *d = to_desc(desc_ring, id); - atomic_long_cmpxchg_relaxed(&d->state_var, prev_state_val, - DESC_SV(id, desc_finalized)); /* LMM(desc_make_final:A) */ - - /* Best effort to remember the last finalized @id. */ - atomic_long_set(&desc_ring->last_finalized_id, id); + if (atomic_long_try_cmpxchg_relaxed(&d->state_var, &prev_state_val, + DESC_SV(id, desc_finalized))) { /* LMM(desc_make_final:A) */ + desc_update_last_finalized(rb); + } } /** @@ -1550,7 +1678,7 @@ bool prb_reserve(struct prb_reserved_entry *e, struct printk_ringbuffer *rb, * readers. (For seq==0 there is no previous descriptor.) */ if (info->seq > 0) - desc_make_final(desc_ring, DESC_ID(id - 1)); + desc_make_final(rb, DESC_ID(id - 1)); r->text_buf = data_alloc(rb, r->text_buf_size, &d->text_blk_lpos, id); /* If text data allocation fails, a data-less record is committed. */ @@ -1643,7 +1771,7 @@ void prb_commit(struct prb_reserved_entry *e) */ head_id = atomic_long_read(&desc_ring->head_id); /* LMM(prb_commit:A) */ if (head_id != e->id) - desc_make_final(desc_ring, e->id); + desc_make_final(e->rb, e->id); } /** @@ -1663,12 +1791,9 @@ void prb_commit(struct prb_reserved_entry *e) */ void prb_final_commit(struct prb_reserved_entry *e) { - struct prb_desc_ring *desc_ring = &e->rb->desc_ring; - _prb_commit(e, desc_finalized); - /* Best effort to remember the last finalized @id. */ - atomic_long_set(&desc_ring->last_finalized_id, e->id); + desc_update_last_finalized(e->rb); } /* @@ -2008,7 +2133,9 @@ u64 prb_first_valid_seq(struct printk_ringbuffer *rb) * newest sequence number available to readers will be. * * This provides readers a sequence number to jump to if all currently - * available records should be skipped. + * available records should be skipped. It is guaranteed that all records + * previous to the returned value have been finalized and are (or were) + * available to the reader. * * Context: Any context. * Return: The sequence number of the next newest (not yet available) record @@ -2017,33 +2144,19 @@ u64 prb_first_valid_seq(struct printk_ringbuffer *rb) u64 prb_next_seq(struct printk_ringbuffer *rb) { struct prb_desc_ring *desc_ring = &rb->desc_ring; - enum desc_state d_state; - unsigned long id; u64 seq; - /* Check if the cached @id still points to a valid @seq. */ - id = atomic_long_read(&desc_ring->last_finalized_id); - d_state = desc_read(desc_ring, id, NULL, &seq, NULL); + seq = desc_last_finalized_seq(desc_ring); - if (d_state == desc_finalized || d_state == desc_reusable) { - /* - * Begin searching after the last finalized record. - * - * On 0, the search must begin at 0 because of hack#2 - * of the bootstrapping phase it is not known if a - * record at index 0 exists. - */ - if (seq != 0) - seq++; - } else { - /* - * The information about the last finalized sequence number - * has gone. It should happen only when there is a flood of - * new messages and the ringbuffer is rapidly recycled. - * Give up and start from the beginning. - */ - seq = 0; - } + /* + * Begin searching after the last finalized record. + * + * On 0, the search must begin at 0 because of hack#2 + * of the bootstrapping phase it is not known if a + * record at index 0 exists. + */ + if (seq != 0) + seq++; /* * The information about the last finalized @seq might be inaccurate. @@ -2085,7 +2198,7 @@ void prb_init(struct printk_ringbuffer *rb, rb->desc_ring.infos = infos; atomic_long_set(&rb->desc_ring.head_id, DESC0_ID(descbits)); atomic_long_set(&rb->desc_ring.tail_id, DESC0_ID(descbits)); - atomic_long_set(&rb->desc_ring.last_finalized_id, DESC0_ID(descbits)); + atomic_long_set(&rb->desc_ring.last_finalized_seq, 0); rb->text_data_ring.size_bits = textbits; rb->text_data_ring.data = text_buf; diff --git a/kernel/printk/printk_ringbuffer.h b/kernel/printk/printk_ringbuffer.h index 18cd25e489b8..3374a5a3303e 100644 --- a/kernel/printk/printk_ringbuffer.h +++ b/kernel/printk/printk_ringbuffer.h @@ -75,7 +75,7 @@ struct prb_desc_ring { struct printk_info *infos; atomic_long_t head_id; atomic_long_t tail_id; - atomic_long_t last_finalized_id; + atomic_long_t last_finalized_seq; }; /* @@ -259,7 +259,7 @@ static struct printk_ringbuffer name = { \ .infos = &_##name##_infos[0], \ .head_id = ATOMIC_INIT(DESC0_ID(descbits)), \ .tail_id = ATOMIC_INIT(DESC0_ID(descbits)), \ - .last_finalized_id = ATOMIC_INIT(DESC0_ID(descbits)), \ + .last_finalized_seq = ATOMIC_INIT(0), \ }, \ .text_data_ring = { \ .size_bits = (avgtextbits) + (descbits), \ From patchwork Mon Nov 6 21:07:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 162193 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp2928131vqu; Mon, 6 Nov 2023 13:08:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IGGIdJyZWp2uuUNxd4lDVgDT0/uVRbDfcET7xSbIlRLcL6SOKMnvidU73+GAoM6IN8394Nh X-Received: by 2002:a05:6358:7e91:b0:16b:4f8c:93c4 with SMTP id o17-20020a0563587e9100b0016b4f8c93c4mr9750813rwn.27.1699304890707; Mon, 06 Nov 2023 13:08:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699304890; cv=none; d=google.com; s=arc-20160816; b=nS9Zstgc4ExLIQ4XHQlNvWSDinIkTY2yHrLeuffOkwebdJuzOIV/RMfCjoYaFA1jsF YRbqXKhG4znJCDVjNlITyrv7c0ddmSesGbK2J/xMXNO0MwXnjYcBtbkuEesaKXGqeres uwhQ/2USuQB68ymlLt9mdr6Qp6K0n5Xbn4eyqXkzSn6EsF92ZYI/3uQGnEZ44lWnj0Dl a2y0QYVy/LuyJmYZmgZJFC3JijxfPCfYEhYiOXAkfuSJNz2VjVBAkKn8WRShreCrk813 4vL/jMir8bBDiL5wIfjVXukorymodHXBUyE7OB4PAvRGbUFHmy1LSOF7Xy+1Fntbmjgc vI4A== 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=zy0aCNS0BNbBR2t/Vy2pLfxqa358VYw0pvvR05rDg/o=; fh=pKnPKUsir0uEGSsor+4Zc2vgbu+g+ayvUgsdzkuXaoA=; b=vNy5FGAOT/opGaV8d+A9PNx+0Qzdo4Q7yxOzWBbnVQfMOww8LiZhRlMIufozX8LYQs kdXs1nRsidcEHIYI5vKW90rLmTk5yXrPovMt+gZg+kJpsJWSpe5eKJgkQnkiF0kp4Ey7 bhgqlnnBoK7n3qQihlA+4teyyd/eTvYyOv8K1Z2z+Jk97AK7VtjBF8Ofs5krBGZEbjaU 0vXSYM7rVSbi3o+ycavKxdkWY635Hw7qobrxpu0XvBFtRVlVEOLPVUC1Vg2TGHygfeyY rGxwa+yt6BL5trLJVh0qiTyABF1gpbayIyDzmYrtlfws6boTNnlezHqSvxByhqZsnG4V C6SQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=fB6LLMeP; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id cb13-20020a056a02070d00b005ac8d44bad4si593727pgb.592.2023.11.06.13.08.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 13:08:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=fB6LLMeP; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 4127380320DA; Mon, 6 Nov 2023 13:08:04 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233137AbjKFVHp (ORCPT + 34 others); Mon, 6 Nov 2023 16:07:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233081AbjKFVHi (ORCPT ); Mon, 6 Nov 2023 16:07:38 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 379CDD75 for ; Mon, 6 Nov 2023 13:07:35 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1699304853; 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=zy0aCNS0BNbBR2t/Vy2pLfxqa358VYw0pvvR05rDg/o=; b=fB6LLMePAfBS6UsMHNPaGN1Jp491bueVhdT9MO5Poa+tLrJXPOEbHESi9XjdXfPX/nq8zj Fy5eumkd+lH2QB7p1YFdETnX/II5LCSnAhWuUVcUYv7/qIdiCOtb7HjraMrTj8Onso/pFF 8V6YPGa9VHUK4pQ7Z8bTiw/rrkZDsDboZE5aHm9OAHBj6hEONNlfwJmLTQOEuO5tHZ4Ibd 2s7hOSONf4fJKU6NKS/5aKD3l74gjI7t+EO4KqcJGlM4vno/UySNkR8p03mQiQU+G++tBt Jp5KQMn8Zh5hLvxc83e7nKW1IBu9K1liHOM3+eSpb8DCl6UIFszPphqlTi8Z8A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1699304853; 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=zy0aCNS0BNbBR2t/Vy2pLfxqa358VYw0pvvR05rDg/o=; b=HbiGHTBfFOaOEt2JUlfzI0MJAMvgqI0KgYMKg369AG+/tYwBccrxc0M4/aXxi7/3czo/64 Krv9JHZCFJytrADA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v2 2/9] printk: ringbuffer: Clarify special lpos values Date: Mon, 6 Nov 2023 22:13:23 +0106 Message-Id: <20231106210730.115192-3-john.ogness@linutronix.de> In-Reply-To: <20231106210730.115192-1-john.ogness@linutronix.de> References: <20231106210730.115192-1-john.ogness@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INVALID_DATE_TZ_ABSURD, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Mon, 06 Nov 2023 13:08:04 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781850325222251691 X-GMAIL-MSGID: 1781850325222251691 For empty line records, no data blocks are created. Instead, these valid records are identified by special logical position values (in fields of @prb_desc.text_blk_lpos). Currently the macro NO_LPOS is used for empty line records. This name is confusing because it does not imply _why_ there is no data block. Rename NO_LPOS to EMPTY_LINE_LPOS so that it is clear why there is no data block. Also add comments explaining the use of EMPTY_LINE_LPOS as well as clarification to the values used to represent data-less blocks. Signed-off-by: John Ogness --- kernel/printk/printk_ringbuffer.c | 20 ++++++++++++++++---- kernel/printk/printk_ringbuffer.h | 16 +++++++++++++++- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/kernel/printk/printk_ringbuffer.c b/kernel/printk/printk_ringbuffer.c index 94eede5356ac..43d77db435c5 100644 --- a/kernel/printk/printk_ringbuffer.c +++ b/kernel/printk/printk_ringbuffer.c @@ -1034,9 +1034,13 @@ static char *data_alloc(struct printk_ringbuffer *rb, unsigned int size, unsigned long next_lpos; if (size == 0) { - /* Specify a data-less block. */ - blk_lpos->begin = NO_LPOS; - blk_lpos->next = NO_LPOS; + /* + * Data blocks are not created for empty lines. Instead, the + * reader will recognize these special lpos values and handle + * it appropriately. + */ + blk_lpos->begin = EMPTY_LINE_LPOS; + blk_lpos->next = EMPTY_LINE_LPOS; return NULL; } @@ -1214,10 +1218,18 @@ static const char *get_data(struct prb_data_ring *data_ring, /* Data-less data block description. */ if (BLK_DATALESS(blk_lpos)) { - if (blk_lpos->begin == NO_LPOS && blk_lpos->next == NO_LPOS) { + /* + * Records that are just empty lines are also valid, even + * though they do not have a data block. For such records + * explicitly return empty string data to signify success. + */ + if (blk_lpos->begin == EMPTY_LINE_LPOS && + blk_lpos->next == EMPTY_LINE_LPOS) { *data_size = 0; return ""; } + + /* Data lost, invalid, or otherwise unavailable. */ return NULL; } diff --git a/kernel/printk/printk_ringbuffer.h b/kernel/printk/printk_ringbuffer.h index 3374a5a3303e..5c09061433f3 100644 --- a/kernel/printk/printk_ringbuffer.h +++ b/kernel/printk/printk_ringbuffer.h @@ -127,8 +127,22 @@ enum desc_state { #define DESC_SV(id, state) (((unsigned long)state << DESC_FLAGS_SHIFT) | id) #define DESC_ID_MASK (~DESC_FLAGS_MASK) #define DESC_ID(sv) ((sv) & DESC_ID_MASK) + +/* + * Special data block logical position values (for fields of + * @prb_desc.text_blk_lpos). + * + * - Bit0 is used to identify if the record has no data block. (Implemented in + * the LPOS_DATALESS() macro.) + * + * - Bit1 specifies the reason for not having a data block. + * + * These special values could never be real lpos values because of the + * meta data and alignment padding of data blocks. (See to_blk_size() for + * details.) + */ #define FAILED_LPOS 0x1 -#define NO_LPOS 0x3 +#define EMPTY_LINE_LPOS 0x3 #define FAILED_BLK_LPOS \ { \ From patchwork Mon Nov 6 21:07:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 162190 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp2928028vqu; Mon, 6 Nov 2023 13:07:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IGl+mVqaJAjUIySjIdgf5frcYXFyEofVOzkj9tExL+yUihSVEwaTaUj+Ou660JYVZ58b2Pr X-Received: by 2002:a17:90b:81:b0:27d:57d:dcb8 with SMTP id bb1-20020a17090b008100b0027d057ddcb8mr24961509pjb.47.1699304879740; Mon, 06 Nov 2023 13:07:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699304879; cv=none; d=google.com; s=arc-20160816; b=O7OjQ7g6BNi5emcSgVVpsJUTujUc6W/fVWQYrHU2zwC5EIXxxoJizMbCqU6L/KzSXz Cwaa8eej2e0uGFjDyHWsDmXFMSGN5jUbvTp2yVb/v733Z4adgGjT0SRq+zppOpkPjsHD ZAGpyvsN3TE2XST21hw+cqGqiKV57yjihYt1DY5f2FhMRFUdD2wQPPaWGMmjvj9wFYCq HIiPKnJtaX1JTbJ7tCbJ3a8ILeZLUb480XFscaCW+a+0fdd2jbUeL7sWe51TcMOsF52W WNPocWT44MZm9zOb4/n7XlMYOUGc5jUeqgi4smb8v05WXE+V4NN28FvYkj5jjJKTwMtS tX/g== 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=krH2QkxdJALvXRu9Bq+Fw3gs7PqsPIKkUheQqZKkVK0=; fh=pKnPKUsir0uEGSsor+4Zc2vgbu+g+ayvUgsdzkuXaoA=; b=Ek5wdszqUTTyQGlJ7dqtDsTvENaKxmO6zoQot0/92CQeUX11kpd3ZrQro8q7gL06Js WMP2g0/jGvltrKaM6dLSugnO+pgGKrHrQfMoOu6Vrj+PPZsuhj/gHnfQfVplaUw7tbbg GeSH4S+JYIuweBup/QJK88Zv7ZOfItp2iEbxwR5u/yKlpdrVQa1zJbt+5IMnz97eJgi2 XmveHvJW7uWLZwfEbuSLLKaqXjosGlASg9qaWfIEwN89ojec7KztqEfBmF4GEjFcvHtc eZqSzEO2R1HtntihHFxzh0CbMZqG9fr+kr0FNsa/LWeySEHTVkZNv/a7Yn6vcdPUdp0/ 8Haw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=QMdwCTBC; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b="mlP0P/xI"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id p6-20020a17090a868600b00280979dbb7asi8933659pjn.103.2023.11.06.13.07.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 13:07:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=QMdwCTBC; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b="mlP0P/xI"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id E326D801B387; Mon, 6 Nov 2023 13:07:58 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233144AbjKFVHr (ORCPT + 34 others); Mon, 6 Nov 2023 16:07:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233080AbjKFVHi (ORCPT ); Mon, 6 Nov 2023 16:07:38 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0FBB7D76 for ; Mon, 6 Nov 2023 13:07:35 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1699304854; 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=krH2QkxdJALvXRu9Bq+Fw3gs7PqsPIKkUheQqZKkVK0=; b=QMdwCTBCqjbEWs3BE4hUb1Y9Hym3aJ4RR2/J7tmD2m4kRWoI3l0B2B2psbP22JClESL9J7 ZgTv1JSFlm1LMLVKTaFpw7IaDmsVLLf+0+uSwWIoOPLJJXsxF7EQhdtPbqLKfbedcoSYOK oHLZo/QR8v6WlnUWfFhhUVBD8elhrD+EvlaYd84rX8j0QT+FjLqTl1eClLY2I6+tVxji0L 4NQ7PndmDJtqPTPCPnCe7RdXMERG5rqkuhtN6rNzxXPsr2mvRbMxQ5cDtAdiyWDs0DE2e2 yWxsRwU8mUaCACmX/UKN1m+uk8AwUbGpAK220XDYe7nr8JyAO6G6a2g9XpTAAQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1699304854; 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=krH2QkxdJALvXRu9Bq+Fw3gs7PqsPIKkUheQqZKkVK0=; b=mlP0P/xItlPB8rJI+B2aSLZNJdPxPRLp0Ee10WmEDPUHH+5vxuKYEC22SmOHkCwAKvaP7M JLdzZZH1TEVxRECw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v2 3/9] printk: For @suppress_panic_printk check for other CPU in panic Date: Mon, 6 Nov 2023 22:13:24 +0106 Message-Id: <20231106210730.115192-4-john.ogness@linutronix.de> In-Reply-To: <20231106210730.115192-1-john.ogness@linutronix.de> References: <20231106210730.115192-1-john.ogness@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-1.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,INVALID_DATE_TZ_ABSURD, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 06 Nov 2023 13:07:58 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781850313886371303 X-GMAIL-MSGID: 1781850313886371303 Currently @suppress_panic_printk is checked along with non-matching @panic_cpu and current CPU. This works because @suppress_panic_printk is only set when panic_in_progress() is true. Rather than relying on the @suppress_panic_printk semantics, use the concise helper function other_cpu_in_progress(). The helper function exists to avoid open coding such tests. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index be95a6851164..22bb45d8f2f2 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2270,8 +2270,7 @@ asmlinkage int vprintk_emit(int facility, int level, if (unlikely(suppress_printk)) return 0; - if (unlikely(suppress_panic_printk) && - atomic_read(&panic_cpu) != raw_smp_processor_id()) + if (unlikely(suppress_panic_printk) && other_cpu_in_panic()) return 0; if (level == LOGLEVEL_SCHED) { From patchwork Mon Nov 6 21:07:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 162189 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp2927976vqu; Mon, 6 Nov 2023 13:07:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IHBoGBhdIe4DM2Z9d/Lfe3p8CuxIJrrcplWt8n0TYBweVlTLnk6jxskqjgYNLTEbHBn3f1g X-Received: by 2002:a17:902:d401:b0:1cc:2f90:6291 with SMTP id b1-20020a170902d40100b001cc2f906291mr23801857ple.54.1699304873361; Mon, 06 Nov 2023 13:07:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699304873; cv=none; d=google.com; s=arc-20160816; b=L2UgCKDSmts1oQc9qpdjuS9iTk5rxGmjcp1auBNkIpsLYFDrwOHF8u2V9v2HMq8/G+ 2NPf94sGKu0gWsIVCzY5YXCq4GzKclIi6F8CzlLOMWRiyYuvtLEAeZ4c+FORoKO8q7Xx fnzycz0kEQjT1oFb9YnNYVaaRGVnPRIFL/Nf6MVHM7PY6K/d96LFP7lu4iRHxi5f9zRS dT2caLvtCDPoz/P3ktbWKwMgLR4qaj6HaGmEwQl8sn1tN5BVmeR8fyVOJjwon+xUeFZJ eBQdv5LndkBjpf5pUJXSmJro/yCbdf9MaZZs05nseBtgdfL4RUa71GjXZMApNiMrPLSB wLsA== 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=EjNlI5JonKv0m/kINdXCywSbWYOfTvFxrSFxNOQW5Ko=; fh=pKnPKUsir0uEGSsor+4Zc2vgbu+g+ayvUgsdzkuXaoA=; b=G8NCYCzgvZjVOH2i8y/38vw6sSNYRKCEj8PwnvzTkCZ0Q5Sfti+isnT5ZJNEujY0aS LLvN50S2gz1IlcCYaGRuGwSnQAA3Mc76qXebsgGQ0y4GY4ceWrSc+lSiRLJZbTmfwLR9 BtUjlftwsVp/irFdxCS2QkaLGzVM81tj4zX0duxFCswaUQSq/9yK0xT8xaSwLPhU5Ks5 wACje9eG0Lzfs+Q2kWMoPsPL8pUuXMk+vqsvNrIOwTpx+9RnK9x1fdQi2rVADCrJ3cjh 5QtfylzxcLFHbfV9F/T8aUXZUyI11rZ8aAph3lid/ex1p6j37hycBHlH3tNOmTeDbfdV orug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=MJVAR+sg; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id jb9-20020a170903258900b001c7345bc01csi8376514plb.450.2023.11.06.13.07.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 13:07:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=MJVAR+sg; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 61D56801B3D2; Mon, 6 Nov 2023 13:07:52 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233128AbjKFVHn (ORCPT + 34 others); Mon, 6 Nov 2023 16:07:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57730 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233093AbjKFVHi (ORCPT ); Mon, 6 Nov 2023 16:07:38 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6F4C103 for ; Mon, 6 Nov 2023 13:07:35 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1699304854; 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=EjNlI5JonKv0m/kINdXCywSbWYOfTvFxrSFxNOQW5Ko=; b=MJVAR+sgDJZ8TL8ljWTnvRYcn0y6yHtJzqUbEQ1e08Ii5zXYVTcFfCo+HM+F2/58vSrvt8 EROu+OomB2vxIDFyqIF2UTt13/UFWVxvHcwBgtziEhu4iXKMvgFDdEgFc77rLT8H+sO9ty 3U6iH2DrYM/PwKtpj508qEy+IUrJug5tljmMnPg/N4zQWNiSoxyk6pEDgjCYL/m1iXd/sB KfsNmQVvxP+S+XbGcI+HrkLUQn2BTjvNDWOSesTg2KVF2HrtTRtzQKIeDXn6CpErnA3WBD 6lV/LSeGIK0g3Hvm7yE9wr20Vmx93ZNrfmVWVFvMByZgIub8HG2pQFLQnSb51A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1699304854; 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=EjNlI5JonKv0m/kINdXCywSbWYOfTvFxrSFxNOQW5Ko=; b=RyhUWvptryHr7Sz4ofjXpuWKZwkpDAcmLBQ1/uuyrbLijg192A/cny+0wnHW0xwQ/7zmoj PfrSVwuLOHUZAOCQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v2 4/9] printk: Add this_cpu_in_panic() Date: Mon, 6 Nov 2023 22:13:25 +0106 Message-Id: <20231106210730.115192-5-john.ogness@linutronix.de> In-Reply-To: <20231106210730.115192-1-john.ogness@linutronix.de> References: <20231106210730.115192-1-john.ogness@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-1.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,INVALID_DATE_TZ_ABSURD, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 06 Nov 2023 13:07:52 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781850306793176801 X-GMAIL-MSGID: 1781850306793176801 There is already panic_in_progress() and other_cpu_in_panic(), but checking if the current CPU is the panic CPU must still be open coded. Add this_cpu_in_panic() to complete the set. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 1 + kernel/printk/printk.c | 43 +++++++++++++++++++++------------------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 6c2afee5ef62..ac2d9750e5f8 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -130,6 +130,7 @@ struct printk_message { }; bool other_cpu_in_panic(void); +bool this_cpu_in_panic(void); bool printk_get_next_message(struct printk_message *pmsg, u64 seq, bool is_extended, bool may_supress); diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 22bb45d8f2f2..82dc2c7949b7 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -347,6 +347,29 @@ static bool panic_in_progress(void) return unlikely(atomic_read(&panic_cpu) != PANIC_CPU_INVALID); } +/* Return true if a panic is in progress on the current CPU. */ +bool this_cpu_in_panic(void) +{ + /* + * We can use raw_smp_processor_id() here because it is impossible for + * the task to be migrated to the panic_cpu, or away from it. If + * panic_cpu has already been set, and we're not currently executing on + * that CPU, then we never will be. + */ + return unlikely(atomic_read(&panic_cpu) == raw_smp_processor_id()); +} + +/* + * Return true if a panic is in progress on a remote CPU. + * + * On true, the local CPU should immediately release any printing resources + * that may be needed by the panic CPU. + */ +bool other_cpu_in_panic(void) +{ + return (panic_in_progress() && !this_cpu_in_panic()); +} + /* * This is used for debugging the mess that is the VT code by * keeping track if we have the console semaphore held. It's @@ -2592,26 +2615,6 @@ static int console_cpu_notify(unsigned int cpu) return 0; } -/* - * Return true if a panic is in progress on a remote CPU. - * - * On true, the local CPU should immediately release any printing resources - * that may be needed by the panic CPU. - */ -bool other_cpu_in_panic(void) -{ - if (!panic_in_progress()) - return false; - - /* - * We can use raw_smp_processor_id() here because it is impossible for - * the task to be migrated to the panic_cpu, or away from it. If - * panic_cpu has already been set, and we're not currently executing on - * that CPU, then we never will be. - */ - return atomic_read(&panic_cpu) != raw_smp_processor_id(); -} - /** * console_lock - block the console subsystem from printing * From patchwork Mon Nov 6 21:07:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 162191 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp2928051vqu; Mon, 6 Nov 2023 13:08:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IEiwRbg6QMaUfs7AoULpV9MlpLk5eh6Ivfys1yp19Nysmt0DMeJ/Oo8/2uJ0cPRsXMjAgbF X-Received: by 2002:a17:902:ce83:b0:1cc:6ec2:d24e with SMTP id f3-20020a170902ce8300b001cc6ec2d24emr22402366plg.53.1699304882226; Mon, 06 Nov 2023 13:08:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699304882; cv=none; d=google.com; s=arc-20160816; b=UflyT6jnZJsA4vJd2CRGifLZ212B/q4COZX7aZfeBTzU03jhtDZDywlsnB3t5+4OKo MqxzYaatDst7kLHpTQnFF19c6A9NT07Z6aQxXTtdi5oqX2q7o8D1/AvOW66s/6Q+N1VG gTmPrq0ujG28GqIED+6OQlnl5cfZtQbKYb/JbcfJUV8SJNtzZaA3EOuiddsm2ROTxnH1 XPRyLSYWkTyB1KussnRg073gl4YGXybk5y5XU0cyv8319TDKP8EW5Vx2laKCTX111JLW TUV/FxzkdVBZ7oCyGCUZvmicrdM043/E+BwM/Yt2h28uJ5KBwjNCZJasmNQDJfYZwhZm isiw== 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=kBti+euuqh1/uRUq/4/BqmS276Luuh3FRgstJhjJXpI=; fh=pKnPKUsir0uEGSsor+4Zc2vgbu+g+ayvUgsdzkuXaoA=; b=stR6rxBeGR/hJGRZT2d1O6wz6AXPCOmj4pI3FCL0I0VQt7T1JVUospWANQtBzXgua9 KcuZrm5VxeN72jsyxkr6uFHZJcRKD8EIY7fkrWPqw6g/20FdA2QO902h8Ls5R5EptybP yyeyn9xZZxOXO++j3xDkCXnV46B/yoYaw3egzPSWgtQKRRbFzb4pZu//98vVaLmc4YKY TXb64mneinhafuO7jhMUtaCpoB3ACqpdyFbDEfpZQGh7Dtt2lOHoM4AzzZX9uZrt5gL5 DtCzzC5SdGw1XaIj0y4dw8NsgIf84EtE9S9LLn1AOmUW0/FgFRnq3zuDi6prx15qieIV ivDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=rO+IZ5z6; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id jc15-20020a17090325cf00b001ca110d482csi8438650plb.573.2023.11.06.13.08.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 13:08:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=rO+IZ5z6; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 5FF82801B3EE; Mon, 6 Nov 2023 13:08:01 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233157AbjKFVHt (ORCPT + 34 others); Mon, 6 Nov 2023 16:07:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57758 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229755AbjKFVHk (ORCPT ); Mon, 6 Nov 2023 16:07:40 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A425AA3 for ; Mon, 6 Nov 2023 13:07:37 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1699304854; 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=kBti+euuqh1/uRUq/4/BqmS276Luuh3FRgstJhjJXpI=; b=rO+IZ5z60LDnK6bOmPFT51j9NOkdxNHT9YTE9QZdCaYLYBFDqIUeiq+GgPdCUHqyT21iCg MNKL4XsVsrcxD4gaI2D9beZ0cyOFmcGNp4wtfyRR2Xn1oVw01OcTZYOgDA4jB7RYWi0YST 2iBEb5G9AT0tWn/Ko6goqu+SFXk1aUSusJZt0Rem+6xg6irc7xJTUqpnbLJOznuFV2sfpr H3MVdibz2/l0My1YTcu1+cBVZPrWBXP/YsVkhCMq029iDuS8LdgABtTtc97m/5VegKpz7C xBjYR+UEjT2UIyg2LBvh1lAB2CQWOQnOHWvTd+wwjUDmnPWGf0ifEPYnQWbv+Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1699304854; 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=kBti+euuqh1/uRUq/4/BqmS276Luuh3FRgstJhjJXpI=; b=Si1gJVCx5MZqrcjxIsCfq5cD8gX/FtEBGyffHlxR4+OyKVjZ4IMcEJIxH6J4bM8k2BVAei bWXpXrJUvIyjc7CA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v2 5/9] printk: ringbuffer: Cleanup reader terminology Date: Mon, 6 Nov 2023 22:13:26 +0106 Message-Id: <20231106210730.115192-6-john.ogness@linutronix.de> In-Reply-To: <20231106210730.115192-1-john.ogness@linutronix.de> References: <20231106210730.115192-1-john.ogness@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-1.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,INVALID_DATE_TZ_ABSURD, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 06 Nov 2023 13:08:01 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781850316220197431 X-GMAIL-MSGID: 1781850316220197431 With the lockless ringbuffer, it is allowed that multiple CPUs/contexts write simultaneously into the buffer. This creates an ambiguity as some writers will finalize sooner. The documentation for the prb_read functions is not clear as it refers to "not yet written" and "no data available". Clarify the return values and language to be in terms of the reader: records available for reading. Signed-off-by: John Ogness --- kernel/printk/printk_ringbuffer.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/kernel/printk/printk_ringbuffer.c b/kernel/printk/printk_ringbuffer.c index 43d77db435c5..7c121fd68330 100644 --- a/kernel/printk/printk_ringbuffer.c +++ b/kernel/printk/printk_ringbuffer.c @@ -2013,11 +2013,13 @@ static u64 prb_first_seq(struct printk_ringbuffer *rb) } /* - * Non-blocking read of a record. Updates @seq to the last finalized record - * (which may have no data available). + * Non-blocking read of a record. * - * See the description of prb_read_valid() and prb_read_valid_info() - * for details. + * On success @seq is updated to the record that was read and (if provided) + * @r and @line_count will contain the read/calculated data. + * + * On failure @seq is updated to a record that is not yet available to the + * reader, but it will be the next record available to the reader. */ static bool _prb_read_valid(struct printk_ringbuffer *rb, u64 *seq, struct printk_record *r, unsigned int *line_count) @@ -2036,7 +2038,7 @@ static bool _prb_read_valid(struct printk_ringbuffer *rb, u64 *seq, *seq = tail_seq; } else if (err == -ENOENT) { - /* Record exists, but no data available. Skip. */ + /* Record exists, but the data was lost. Skip. */ (*seq)++; } else { @@ -2069,7 +2071,7 @@ static bool _prb_read_valid(struct printk_ringbuffer *rb, u64 *seq, * On success, the reader must check r->info.seq to see which record was * actually read. This allows the reader to detect dropped records. * - * Failure means @seq refers to a not yet written record. + * Failure means @seq refers to a record not yet available to the reader. */ bool prb_read_valid(struct printk_ringbuffer *rb, u64 seq, struct printk_record *r) @@ -2099,7 +2101,7 @@ bool prb_read_valid(struct printk_ringbuffer *rb, u64 seq, * On success, the reader must check info->seq to see which record meta data * was actually read. This allows the reader to detect dropped records. * - * Failure means @seq refers to a not yet written record. + * Failure means @seq refers to a record not yet available to the reader. */ bool prb_read_valid_info(struct printk_ringbuffer *rb, u64 seq, struct printk_info *info, unsigned int *line_count) From patchwork Mon Nov 6 21:07:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 162196 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp2928343vqu; Mon, 6 Nov 2023 13:08:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IEXrZdbbfOzTICBriOV2DCUQylgAmFIUuDbnkTywU67LuzNgI34F4YffsFU1+oqb1S0002+ X-Received: by 2002:a17:90a:db92:b0:27d:4f1f:47f5 with SMTP id h18-20020a17090adb9200b0027d4f1f47f5mr25370742pjv.23.1699304910103; Mon, 06 Nov 2023 13:08:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699304910; cv=none; d=google.com; s=arc-20160816; b=gIiJEe7MP4tHrrfivPvNgKl0dBmJEIsxmra7ytmQD4ImidwjkQ4B8ePcDeOc/lBdTV Cn8wbLFTRH63btbZcUZylrmZ/JF546Bh/XQfk3yBz5EWlvF5IVE+mQSYF5SakzEsNHlY Ir61Wue5ENjQq3tyje45vIXyj29lkx9xRswhWw7vpdsaimofmJkY3cb3sIuBqejEz8/9 u9mcx6mRJLg8SMFlV7oGmIlwhQT50xXErfmbqSCKBPs4lY4eAb7uhqKbcaPJd2UIsNmi DhuvmHw9YztfY8v9Ae8VuiMv0uF31F+KUHSGJfP9MnwYCYWWkfWTCVtu0pVDOywBPKLz b+3g== 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=kyZ2jAekG9IbA8OHPwAlE8+iwKYLcZ73K8RQgyikDwc=; fh=pKnPKUsir0uEGSsor+4Zc2vgbu+g+ayvUgsdzkuXaoA=; b=obS4dInMvJIFxFW9+icZgpQuTRhkKLVYvWUeWcpJRIuSOoi1TGkGnbzYObSCLmZ5xX ZRraJbMlpppTV8VIl4jgtPUlqwptqErziprY49Tb0lCjZohnAkzrcPomGgqz9OgPSLKd k/4ulG/Qdj7Ja27HAQM/hMx6QhywLQVJbfHOQMYvynFeye1zJ5VBuASvaZXNjcM3ye2o yiop7xFM2jrOlh2Xd5jk3ojEnRvuucvetDlRYbT4cluTPnoB+UbAMMwmBgUG3ZBPFjTI pmHmNiaC0TGDIeD07sXCpvxxhFZePX5sfOgSXuUWcsxABI5jFCAjrFYZ3NWD2NvF6zdq rWEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=hoDow6cm; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id i10-20020a17090a974a00b0027db9371d09si8659860pjw.107.2023.11.06.13.08.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 13:08:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=hoDow6cm; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 89E87803207C; Mon, 6 Nov 2023 13:08:21 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231805AbjKFVHv (ORCPT + 34 others); Mon, 6 Nov 2023 16:07:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57764 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233102AbjKFVHk (ORCPT ); Mon, 6 Nov 2023 16:07:40 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A4DE7103 for ; Mon, 6 Nov 2023 13:07:37 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1699304855; 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=kyZ2jAekG9IbA8OHPwAlE8+iwKYLcZ73K8RQgyikDwc=; b=hoDow6cmR6fMaIE4IEz8T8ZOa9WAsRAIZ1laV7umbzSbt5N8y2x2sO+SEOaC0qXJgBq0cN HEy9osDwNbibWwsvVQvTwmQxALb/YuUKRlWjPncmw6OPDe7hHkUmhNI7smOxwew9qPX0Pq 5EB4HTobjMmvA3On+SoaH6IIwXB9P+1b4ZDYfb/vHAmXFZHB1yfpAL5sGW30R8TlCgzZym Xj2TupQPiAbEdqrVVXILcrBfu1cjcPkcWvW2P1kPCLRMPS341VzeJp3sFWt1uVLmiI7MKZ k1e8LB08vDy7bVHANZ8tiyNOjMH8g634lXyeERfrncWjMnDybu5U62N8OagTXw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1699304855; 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=kyZ2jAekG9IbA8OHPwAlE8+iwKYLcZ73K8RQgyikDwc=; b=NeUa92hCHMV8isuc4k3GcjK6dGlvkA0s8ZlE8vv05kzT4Cr+cL2RHKKjGmyq2k7xGzaAZS D2KlUcMMEzB78VCw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v2 6/9] printk: Wait for all reserved records with pr_flush() Date: Mon, 6 Nov 2023 22:13:27 +0106 Message-Id: <20231106210730.115192-7-john.ogness@linutronix.de> In-Reply-To: <20231106210730.115192-1-john.ogness@linutronix.de> References: <20231106210730.115192-1-john.ogness@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INVALID_DATE_TZ_ABSURD, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Mon, 06 Nov 2023 13:08:21 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781850345477766830 X-GMAIL-MSGID: 1781850345477766830 Currently pr_flush() will only wait for records that were available to readers at the time of the call. But there may be more records (non-finalized) with following finalized records. pr_flush() should wait for these to print as well. Particularly because any trailing finalized records may be the messages that the calling context wants to ensure are printed. Fixes: 3b604ca81202 ("printk: add pr_flush()") Signed-off-by: John Ogness --- kernel/printk/printk.c | 2 +- kernel/printk/printk_ringbuffer.c | 96 +++++++++++++++++++++++++++++++ kernel/printk/printk_ringbuffer.h | 1 + 3 files changed, 98 insertions(+), 1 deletion(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 82dc2c7949b7..f3a7f5a6f6f8 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3755,7 +3755,7 @@ static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progre might_sleep(); - seq = prb_next_seq(prb); + seq = prb_next_reserve_seq(prb); /* Flush the consoles so that records up to @seq are printed. */ console_lock(); diff --git a/kernel/printk/printk_ringbuffer.c b/kernel/printk/printk_ringbuffer.c index 7c121fd68330..dc83569d3a3a 100644 --- a/kernel/printk/printk_ringbuffer.c +++ b/kernel/printk/printk_ringbuffer.c @@ -2012,6 +2012,102 @@ static u64 prb_first_seq(struct printk_ringbuffer *rb) return seq; } +/** + * prb_next_reserve_seq() - Get the sequence number after the most recently + * reserved record. + * + * @rb: The ringbuffer to get the sequence number from. + * + * This is the public function available to readers to see what sequence + * number will be assigned to the next reserved record. + * + * Note that depending on the situation, this value can be equal to or + * higher than the sequence number returned by prb_next_seq(). + * + * Context: Any context. + * Return: The sequence number that will be assigned to the next record + * reserved. + */ +u64 prb_next_reserve_seq(struct printk_ringbuffer *rb) +{ + struct prb_desc_ring *desc_ring = &rb->desc_ring; + unsigned long last_finalized_id; + atomic_long_t *state_var; + u64 last_finalized_seq; + unsigned long head_id; + struct prb_desc desc; + unsigned long diff; + struct prb_desc *d; + int err; + + /* + * It may not be possible to read a sequence number for @head_id. + * So the ID of @last_finailzed_seq is used to calculate what the + * sequence number of @head_id will be. + */ + +try_again: + last_finalized_seq = desc_last_finalized_seq(desc_ring); + + /* + * @head_id is loaded after @last_finalized_seq to ensure that it is + * at or beyond @last_finalized_seq. + * + * Memory barrier involvement: + * + * If desc_last_finalized_seq:A reads from + * desc_update_last_finalized:A, then + * prb_next_reserve_seq:A reads from desc_reserve:D. + * + * Relies on: + * + * RELEASE from desc_reserve:D to desc_update_last_finalized:A + * matching + * ACQUIRE from desc_last_finalized_seq:A to prb_next_reserve_seq:A + * + * Note: desc_reserve:D and desc_update_last_finalized:A can be + * different CPUs. However, the desc_update_last_finalized:A CPU + * (which performs the release) must have previously seen + * desc_read:C, which implies desc_reserve:D can be seen. + */ + head_id = atomic_long_read(&desc_ring->head_id); /* LMM(prb_next_reserve_seq:A) */ + + d = to_desc(desc_ring, last_finalized_seq); + state_var = &d->state_var; + + /* Extract the ID, used to specify the descriptor to read. */ + last_finalized_id = DESC_ID(atomic_long_read(state_var)); + + /* Ensure @last_finalized_id is correct. */ + err = desc_read_finalized_seq(desc_ring, last_finalized_id, last_finalized_seq, &desc); + + if (err == -EINVAL) { + if (last_finalized_seq == 0) { + /* + * @last_finalized_seq still contains its initial + * value. Probably no record has been finalized yet. + * This means the ringbuffer is not yet full and the + * @head_id value can be used directly (subtracting + * off its initial value). + * + * Because of hack#2 of the bootstrapping phase, the + * @head_id initial value must be handled separately. + */ + if (head_id == DESC0_ID(desc_ring->count_bits)) + return 0; + + last_finalized_id = DESC0_ID(desc_ring->count_bits) + 1; + } else { + /* Record must have been overwritten. Try again. */ + goto try_again; + } + } + + diff = head_id - last_finalized_id; + + return (last_finalized_seq + diff); +} + /* * Non-blocking read of a record. * diff --git a/kernel/printk/printk_ringbuffer.h b/kernel/printk/printk_ringbuffer.h index 5c09061433f3..b48b44ecbe6d 100644 --- a/kernel/printk/printk_ringbuffer.h +++ b/kernel/printk/printk_ringbuffer.h @@ -394,5 +394,6 @@ bool prb_read_valid_info(struct printk_ringbuffer *rb, u64 seq, u64 prb_first_valid_seq(struct printk_ringbuffer *rb); u64 prb_next_seq(struct printk_ringbuffer *rb); +u64 prb_next_reserve_seq(struct printk_ringbuffer *rb); #endif /* _KERNEL_PRINTK_RINGBUFFER_H */ From patchwork Mon Nov 6 21:07:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 162197 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp2928414vqu; Mon, 6 Nov 2023 13:08:37 -0800 (PST) X-Google-Smtp-Source: AGHT+IEkztvXW4zx4VwIiNYsSF1P3ssRR6RApSYIjPXnNVFwvMCuGjFr+S//KF2PyqmNhKCN9/Gq X-Received: by 2002:a54:4e8c:0:b0:3b2:e60d:27e3 with SMTP id c12-20020a544e8c000000b003b2e60d27e3mr29342934oiy.30.1699304917261; Mon, 06 Nov 2023 13:08:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699304917; cv=none; d=google.com; s=arc-20160816; b=AyjE/SeYUoCgqAS60VRvY3Ot+h78AULuKnXS9CcP0KR5sdvBNsz3O4asXbV0oRrSN3 H6FBEkTz+fiV1FytUS48Am+SMElzxFQypcTCAubuIqzp+rk68ACrna34CJHWVAqQgJOo qEWTexVNEFXtKmeOKFQQqsMEQDZzCW1MdhnFCvc45k7EgVmouqxUl2MER2EvdNHtor/3 SqUI+k3q24bSFlQ258zvkgE2uuFy9LDsGILZqidio4pfhHRb2mTETdOq59VFU5wUEXlB GXOvuvhx7sq0VyL2tn8nRXE1alnUIFoeV32M9kO9Oa4POkDLmXHDiwTsq5Zm2HABCs2d STZQ== 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=gPyZTcWZWhXXvYtKbCYSzlUC2oh3LgQ1z4Pk6pXNprY=; fh=pKnPKUsir0uEGSsor+4Zc2vgbu+g+ayvUgsdzkuXaoA=; b=r0jb2jU5mjIvYqC8nuSxPWeW1+q0zX1S8Yj44RQRtD5CnU9Nwv9FddenAC5FQ51Apk 5MGr2HMJxJemDyDdmCHy0HtBk5sRzds4/723eAh8T0nAbfnCnhI0nlRPy9cAbhPAWmI5 pMbcKZ2NzIN/TwFcmTynydXnX+8m2R8JoURa/sjaQT7hXvdezF3lO8qbRgvl5GLeGDSp 2a9cvofYs4cobFxhH4WcQqJRDswhO9VpF57RisvEx8To/T/Hysh/ORiruTOpsikSiXnX tIWdXDfoB22yQzuxiNRoOhwKaDO7WkxH7BpkQOczcyUCI9seQU+Kl+9LYKB9DNCiSrY8 YZJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=JiSkeIhK; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id fa15-20020a056a002d0f00b006935df3019esi8978792pfb.235.2023.11.06.13.08.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 13:08:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=JiSkeIhK; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 631288048E7B; Mon, 6 Nov 2023 13:08:25 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233191AbjKFVH5 (ORCPT + 34 others); Mon, 6 Nov 2023 16:07:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57782 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233110AbjKFVHk (ORCPT ); Mon, 6 Nov 2023 16:07:40 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3DAFAD75 for ; Mon, 6 Nov 2023 13:07:38 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1699304855; 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=gPyZTcWZWhXXvYtKbCYSzlUC2oh3LgQ1z4Pk6pXNprY=; b=JiSkeIhKiFvecS2FXzSEnPYPxGF/LN+OykA0vVJaX1Q93NZlyyXg/o98rXaFU+8hcdtPZM 7EonEjJzEiMLX+gD9ZBIB+cNXvR0yRgSTc4ctPifNmogqolc8y7pyMW/WxCrXAJt/1JT/B EDS8mYvTBp6DoBgri33uPStc3KKUaSjqvXFWValpM9Vcs/SK79Yg4W1XN10UE2ALuE6zwV upeHCakfbHiKt2rF4V1c/tuxH83VzGSX/Z2hRy3mdfCzBl47TMpAKdSjX3mQqI8ORYXLhg ERVNPeJDQk1R2ORZcaqyFdVWl+hE7D4StmRdsSmksY1BBjnXvKlp85R2JSoM8g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1699304855; 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=gPyZTcWZWhXXvYtKbCYSzlUC2oh3LgQ1z4Pk6pXNprY=; b=yFI+fvbcrINRbTXEB4WeODLJn6Vn4DYrIh3V7Z7kHof5vRGsQCN1/t6qrayYoKMVVkGGzv 6OwczLGJ8aIDuqCg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v2 7/9] printk: Skip non-finalized records in panic Date: Mon, 6 Nov 2023 22:13:28 +0106 Message-Id: <20231106210730.115192-8-john.ogness@linutronix.de> In-Reply-To: <20231106210730.115192-1-john.ogness@linutronix.de> References: <20231106210730.115192-1-john.ogness@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INVALID_DATE_TZ_ABSURD, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Mon, 06 Nov 2023 13:08:25 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781850352812205811 X-GMAIL-MSGID: 1781850352812205811 Normally a reader will stop once reaching a non-finalized record. However, when a panic happens, writers from other CPUs (or an interrupted context on the panic CPU) may have been writing a record and were unable to finalize it. The panic CPU will reserve/commit/finalize its panic records, but these will be located after the non-finalized records. This results in panic() not flushing the panic messages. Extend _prb_read_valid() to skip over non-finalized records if on the panic CPU. Fixes: 896fbe20b4e2 ("printk: use the lockless ringbuffer") Signed-off-by: John Ogness --- kernel/printk/printk_ringbuffer.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/kernel/printk/printk_ringbuffer.c b/kernel/printk/printk_ringbuffer.c index dc83569d3a3a..584d2b213876 100644 --- a/kernel/printk/printk_ringbuffer.c +++ b/kernel/printk/printk_ringbuffer.c @@ -2116,6 +2116,10 @@ u64 prb_next_reserve_seq(struct printk_ringbuffer *rb) * * On failure @seq is updated to a record that is not yet available to the * reader, but it will be the next record available to the reader. + * + * Note: When the current CPU is in panic, this function will skip over any + * non-existent/non-finalized records in order to allow the panic CPU + * to print any and all records that have been finalized. */ static bool _prb_read_valid(struct printk_ringbuffer *rb, u64 *seq, struct printk_record *r, unsigned int *line_count) @@ -2138,8 +2142,22 @@ static bool _prb_read_valid(struct printk_ringbuffer *rb, u64 *seq, (*seq)++; } else { - /* Non-existent/non-finalized record. Must stop. */ - return false; + /* + * Non-existent/non-finalized record. Must stop. + * + * For panic situations it cannot be expected that + * non-finalized records will become finalized. But + * there may be other finalized records beyond that + * need to be printed for a panic situation. If this + * is the panic CPU, skip this + * non-existent/non-finalized record unless it is + * at or beyond the head, in which case it is not + * possible to continue. + */ + if (this_cpu_in_panic() && ((*seq + 1) < prb_next_reserve_seq(rb))) + (*seq)++; + else + return false; } } From patchwork Mon Nov 6 21:07:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 162192 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp2928104vqu; Mon, 6 Nov 2023 13:08:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IE6dlBdGRJeKZRPKUPA/KD/dJiTrTN+f+v+p7qx4k5R0wTfl9xoqyjX6b3ainTjApQWry4S X-Received: by 2002:a05:6a20:4328:b0:15c:b7ba:6a4d with SMTP id h40-20020a056a20432800b0015cb7ba6a4dmr33691978pzk.50.1699304886042; Mon, 06 Nov 2023 13:08:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699304886; cv=none; d=google.com; s=arc-20160816; b=iKMgvNxeZLm2yWfe1fD44pKsvyDhPFkqRsFXNVoXkmaWEf4yp5oseOJWCcysahapQ+ wCWkXYYcr0jqNdSDeovC+gjVQ7ddwJXs2NVrWOqMf0L+FqRrqpnAiV7LhwZDxD/Su/bD p5tRQRJgLSs7KbW3+oD49SLz0ah5d3fIRjW3Tb6ZaCLU0q95AhjU0YqShdHeaoJ6A5vY s3ebnaxm2xM/PVSWlcjrYh6IK7nGQhFuhJNh2uUKThkl/Z1Kpuqox882SqQkLBi+Ap8q gfamo1OEOGDjJgOKbX2NPycIR/X+5V0zd0SinzIy47kcHs08utsaGTvswx0ItMjFDyAB HFtw== 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=W3IXvUuXJ9pnznvJZ8clgdIjVGMNk75/C1eUuuTRsrs=; fh=pKnPKUsir0uEGSsor+4Zc2vgbu+g+ayvUgsdzkuXaoA=; b=QjC+nLn3eBxCTlbqGa6Ghl1VtvoPcA5qU7pSH4CBM/ytibggaITl0ZV4CLIvUvpO88 NnST25FnednnWS1IpHiGMdRHVRjOmRysKpyLTiqECGSzPhx96bi6dsyn4LzhgSwouqJU CHkh/s3etlWC11FIKdC2yWxbIQMXOZ18BnwvjXqY+LIDeMzqk0YrdTgx8t2wZT3Id96V wTPb3g9UKIwjTmkte5HiYs3iG+/K+FsOwShtIWa6uFgkETbM61lOiWUAy62a+Ds/RbqR 7ad+WovCB0u9o4a1pJXXhWf9q0lqANOrG8Khd6Dmli7/cyuLCOthpoB6tiLcB643Oyx8 IqfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=hudJ+tTq; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id b3-20020a056a000cc300b006c34752a6e8si9197156pfv.81.2023.11.06.13.08.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 13:08:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=hudJ+tTq; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 232FA801B387; Mon, 6 Nov 2023 13:08:05 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233181AbjKFVHy (ORCPT + 34 others); Mon, 6 Nov 2023 16:07:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233111AbjKFVHk (ORCPT ); Mon, 6 Nov 2023 16:07:40 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 36C09D51 for ; Mon, 6 Nov 2023 13:07:38 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1699304855; 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=W3IXvUuXJ9pnznvJZ8clgdIjVGMNk75/C1eUuuTRsrs=; b=hudJ+tTqn5YPwaMErxVwIfT/yrE7Dn2MSwLeYnJsOMmetVf6LDrr8WCcqaxk6UkKagtcbe aCMNzerz9ohd70fVZAY4jftufPp+4B5fwQllqUxfmMPPVwBCuKwwRpm/57s7uCevGCX5T9 K+y4KZQvcbucKwB6WSgmwI5GNnSngkSVAbkBlRTNZnVQ8swSLVEYYLnzNZUHHq3NhBbRVO 1918m9QZsvgZg9F8l/S9DUnpx9nMBVdaD0h/ORFw6WIKqyxpG0qZ+4x5SgqSjGDpxe1kD3 UEOQeXkEhefuiuyRLtZiRdPplRdp+EXQzGO7XPoRfWt3LRMAhygWL9hqkcM7cA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1699304855; 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=W3IXvUuXJ9pnznvJZ8clgdIjVGMNk75/C1eUuuTRsrs=; b=LXqdXzlKnXhpsVg5KAQF+V3ELg6J3Er0v/dgs1pFYn6MvYNwFoi5VvAh/p2ZD8+mm1jBgA hN8+QAI+pn1msFBA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v2 8/9] printk: Disable passing console lock owner completely during panic() Date: Mon, 6 Nov 2023 22:13:29 +0106 Message-Id: <20231106210730.115192-9-john.ogness@linutronix.de> In-Reply-To: <20231106210730.115192-1-john.ogness@linutronix.de> References: <20231106210730.115192-1-john.ogness@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-1.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,INVALID_DATE_TZ_ABSURD, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 06 Nov 2023 13:08:05 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781850319932470423 X-GMAIL-MSGID: 1781850319932470423 From: Petr Mladek The commit d51507098ff91 ("printk: disable optimistic spin during panic") added checks to avoid becoming a console waiter if a panic is in progress. However, the transition to panic can occur while there is already a waiter. The current owner should not pass the lock to the waiter because it might get stopped or blocked anytime. Also the panic context might pass the console lock owner to an already stopped waiter by mistake. It might happen when console_flush_on_panic() ignores the current lock owner, for example: CPU0 CPU1 ---- ---- console_lock_spinning_enable() console_trylock_spinning() [CPU1 now console waiter] NMI: panic() panic_other_cpus_shutdown() [stopped as console waiter] console_flush_on_panic() console_lock_spinning_enable() [print 1 record] console_lock_spinning_disable_and_check() [handover to stopped CPU1] This results in panic() not flushing the panic messages. Fix these problems by disabling all spinning operations completely during panic(). Another advantage is that it prevents possible deadlocks caused by "console_owner_lock". The panic() context does not need to take it any longer. The lockless checks are safe because the functions become NOPs when they see the panic in progress. All operations manipulating the state are still synchronized by the lock even when non-panic CPUs would notice the panic synchronously. The current owner might stay spinning. But non-panic() CPUs would get stopped anyway and the panic context will never start spinning. Fixes: dbdda842fe96 ("printk: Add console owner and waiter logic to load balance console writes") Signed-off-by: Petr Mladek Reviewed-by: John Ogness --- kernel/printk/printk.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index f3a7f5a6f6f8..cb99c854a648 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -1869,10 +1869,23 @@ static bool console_waiter; */ static void console_lock_spinning_enable(void) { + /* + * Do not use spinning in panic(). The panic CPU wants to keep the lock. + * Non-panic CPUs abandon the flush anyway. + * + * Just keep the lockdep annotation. The panic-CPU should avoid + * taking console_owner_lock because it might cause a deadlock. + * This looks like the easiest way how to prevent false lockdep + * reports without handling races a lockless way. + */ + if (panic_in_progress()) + goto lockdep; + raw_spin_lock(&console_owner_lock); console_owner = current; raw_spin_unlock(&console_owner_lock); +lockdep: /* The waiter may spin on us after setting console_owner */ spin_acquire(&console_owner_dep_map, 0, 0, _THIS_IP_); } @@ -1897,6 +1910,22 @@ static int console_lock_spinning_disable_and_check(int cookie) { int waiter; + /* + * Ignore spinning waiters during panic() because they might get stopped + * or blocked at any time, + * + * It is safe because nobody is allowed to start spinning during panic + * in the first place. If there has been a waiter then non panic CPUs + * might stay spinning. They would get stopped anyway. The panic context + * will never start spinning and an interrupted spin on panic CPU will + * never continue. + */ + if (panic_in_progress()) { + /* Keep lockdep happy. */ + spin_release(&console_owner_dep_map, _THIS_IP_); + return 0; + } + raw_spin_lock(&console_owner_lock); waiter = READ_ONCE(console_waiter); console_owner = NULL; From patchwork Mon Nov 6 21:07:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 162194 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp2928148vqu; Mon, 6 Nov 2023 13:08:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IHPAcwzV5g2kWwdBTwA8xixk/Q1GaZpNukrNYymaa996dv0APuY5pQAsKQhHGKvyN1phA+2 X-Received: by 2002:a17:902:e1cd:b0:1b8:b382:f6c3 with SMTP id t13-20020a170902e1cd00b001b8b382f6c3mr21536490pla.13.1699304892589; Mon, 06 Nov 2023 13:08:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699304892; cv=none; d=google.com; s=arc-20160816; b=a9HmrxwOs0ApWT+KzuAY/gMXftnYcne5VhHIEYekGeC2rHDVLLlTwQno3MrjP3outN F3gUvMQl+zV9h2fh7587AJjneMpN9EQSVqVGwpqvi5TjI4aOtX49xCpA77fANxJLB5r+ x7IpfbhtkY7qThueYWrTsV8ws8nETMp3vQiJldrOV+BehRTitCkzByNWUU14CU08puER VlrYh+Ave5Bfi4m1AXby8JsJvH/pyNhFgfp8Xf0igmirDZJ2gmLwTQrpza85/B8m8rxV LakY7gl55Z8VaqujfmPIlMxZ6hUJFlmpU8kZrOjjByJ3bdLdwUeLBGIPfo9WjSweG1QH VouQ== 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=vxMWmZFtd/CI8sGJ7jbK846Sk0z3M0dHtiMNHUYVwDo=; fh=pKnPKUsir0uEGSsor+4Zc2vgbu+g+ayvUgsdzkuXaoA=; b=evSPc+m5picV3xysc8Eol+E7yzjaTru3I5sX/ma7NEfMT9VlL0Uj6qVH4x8+HgrgUc OmoSFyHuUZlVliO1SFVh5i5VkImzv8aVRuNJw/LnxRBZbTvN6DPHlPTEtiwOB0AE/JPV W9TtS5UF70Wdc5pN2nhzROXF5RcdTXYo1jMA2E/E3yiAHhCM3MTQAhsqPi7MzpksN/v8 XkLuLHKpfL7AmHBhQMU0sgWMFsrlsOjpQLzF9HFS8m8YArD9umA5OiUZmJoadc/LV8TF xZDqh+b2mgewLm6KmNy6Dcsk4pZFMxoouT2zNd0MMpJshV4bvuSnT/nq2i25vRJXJSVa BV4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=F0nq+Nls; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=iIknmtgf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id l19-20020a170902e2d300b001cc4aca5f5dsi8156892plc.636.2023.11.06.13.08.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 13:08:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=F0nq+Nls; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=iIknmtgf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id ACEB2801B308; Mon, 6 Nov 2023 13:08:11 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233197AbjKFVIA (ORCPT + 34 others); Mon, 6 Nov 2023 16:08:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233099AbjKFVHl (ORCPT ); Mon, 6 Nov 2023 16:07:41 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 50586D76 for ; Mon, 6 Nov 2023 13:07:38 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1699304856; 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=vxMWmZFtd/CI8sGJ7jbK846Sk0z3M0dHtiMNHUYVwDo=; b=F0nq+NlshhqBwgbmYAQfswiQz1RQAekjo/Mj/fxavpN1DopPZNz/2sjwVY+hqrvI9r1JCk +np2lvriNUf+PawEEI0BpgJmRpqxTGFldaw4+HdZPFHLhiHrDWaTn5xvr2xFwzcQEDTwoc AhNz/uO0JWi24Ms1qCAp8bkn7Yl/c/yP03rOGNVJBJmPsRd4p2OlE3PMzswZ7Rytz0NUSK m9hQGkFvrCwZ5WjTMjKd/4QOpmp27u8ZRmWAd9B58+vu0oWwSVpu8BzFTf1SuOYyJpTTgN eC4c4BiDYXkfrY+d3N97JKLFSxcEbi3MtJ8izxxdpix+zVUHLZfPI2btd3eUDg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1699304856; 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=vxMWmZFtd/CI8sGJ7jbK846Sk0z3M0dHtiMNHUYVwDo=; b=iIknmtgfSg2FSpw6KP51q6sFYxkf9/F1c/JoIp+Uru1hpwxLGCMX8xzDQ6iLDQTjOTKDJu ciQ+0OUj30ky+6CQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v2 9/9] printk: Avoid non-panic CPUs flooding ringbuffer Date: Mon, 6 Nov 2023 22:13:30 +0106 Message-Id: <20231106210730.115192-10-john.ogness@linutronix.de> In-Reply-To: <20231106210730.115192-1-john.ogness@linutronix.de> References: <20231106210730.115192-1-john.ogness@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-1.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,INVALID_DATE_TZ_ABSURD, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 06 Nov 2023 13:08:11 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781850327182292050 X-GMAIL-MSGID: 1781850327182292050 Commit 13fb0f74d702 ("printk: Avoid livelock with heavy printk during panic") introduced a mechanism to silence non-panic CPUs if too many messages are being dropped. Aside from trying to workaround the livelock bugs of legacy consoles, it was also intended to avoid losing panic messages. However, if non-panic CPUs are flooding the ringbuffer, then reacting to dropped messages is too late. To avoid losing panic CPU messages, the tracking needs to occur when non-panic CPUs are storing messages. If non-panic CPUs have filled approximately 1/4 the ringbuffer, they need to be silenced to ensure the ringbuffer has ample space available for the panic CPU messages. Rather than trying to come up with an accurate heuristic to measure the size used by non-panic CPUs, simply restrict them to 1/4 the possible ringbuffer descriptors. In practice this will end up being around 1/3 the ringbuffer size, which still leaves ample space for the panic CPU messages. Signed-off-by: John Ogness --- kernel/printk/printk.c | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index cb99c854a648..9ac7d50c2f18 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2315,6 +2315,8 @@ asmlinkage int vprintk_emit(int facility, int level, const struct dev_printk_info *dev_info, const char *fmt, va_list args) { + static atomic_t panic_noise_count = ATOMIC_INIT(0); + int printed_len; bool in_sched = false; @@ -2322,8 +2324,22 @@ asmlinkage int vprintk_emit(int facility, int level, if (unlikely(suppress_printk)) return 0; - if (unlikely(suppress_panic_printk) && other_cpu_in_panic()) - return 0; + if (other_cpu_in_panic()) { + if (unlikely(suppress_panic_printk)) + return 0; + + /* + * The messages on the panic CPU are the most important. If + * non-panic CPUs are generating many messages, the panic + * messages could get lost. Limit the number of non-panic + * messages to approximately 1/4 of the ringbuffer. + */ + if (atomic_inc_return_relaxed(&panic_noise_count) > + (1 << (prb->desc_ring.count_bits - 2))) { + suppress_panic_printk = 1; + return 0; + } + } if (level == LOGLEVEL_SCHED) { level = LOGLEVEL_DEFAULT; @@ -2799,8 +2815,6 @@ void console_prepend_dropped(struct printk_message *pmsg, unsigned long dropped) bool printk_get_next_message(struct printk_message *pmsg, u64 seq, bool is_extended, bool may_suppress) { - static int panic_console_dropped; - struct printk_buffers *pbufs = pmsg->pbufs; const size_t scratchbuf_sz = sizeof(pbufs->scratchbuf); const size_t outbuf_sz = sizeof(pbufs->outbuf); @@ -2828,17 +2842,6 @@ bool printk_get_next_message(struct printk_message *pmsg, u64 seq, pmsg->seq = r.info->seq; pmsg->dropped = r.info->seq - seq; - /* - * Check for dropped messages in panic here so that printk - * suppression can occur as early as possible if necessary. - */ - if (pmsg->dropped && - panic_in_progress() && - panic_console_dropped++ > 10) { - suppress_panic_printk = 1; - pr_warn_once("Too many dropped messages. Suppress messages on non-panic CPUs to prevent livelock.\n"); - } - /* Skip record that has level above the console loglevel. */ if (may_suppress && suppress_message_printing(r.info->level)) goto out;