Received: by 2002:a05:7412:a9a2:b0:e2:908c:2ebd with SMTP id o34csp261604rdh; Thu, 26 Oct 2023 01:07:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHspbotImHY0tG94sIqLvTMZMk5/JUuElvDS3BgVlkJPp8hLZ3OVL2gzaTikXKrVx50W8Px X-Received: by 2002:a0d:e684:0:b0:5a7:ad67:2b67 with SMTP id p126-20020a0de684000000b005a7ad672b67mr17093958ywe.5.1698307652366; Thu, 26 Oct 2023 01:07:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698307652; cv=none; d=google.com; s=arc-20160816; b=mXl8OO/MVOpgFwcErmC1hFyFeNxJHxW1fip+893XVAxbFGa4evF0Hme3EcSKBojjO6 j/TOGg6uE/50nNJSA3/ozAjjCxAEwIGNdON6RbaZnopG60FCSGZ8YwlL62x4m0ZaKfis TJU7cZh9/1MewkADorospGBGsP0Pu3jAyMMPGXS8iNQPIQBbUhGMs4zCEogFMfVMeYC6 DoSbFi4egDFeNkFRnedOI345aOI3DXiSQIC7jlpOLTkeF+C0hiVDR2MUT1hdsZOdKCgi JjIZGd0fh2CTxRLDAHJSTyoGn3zmlxpj9BOGT8fIHzua9HVMEOrP9NZycZQvY97k8Dz5 6/3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=dOqlmHOwqicAv8RHA6ux1qvA9alTwy+R9UbbAgzwIA0=; fh=+6k46dcAtPfhgsUUmykne56od42/izkYcLVAPAF7Gb0=; b=yW6pO8EyN3frACbp3t4vwv+Z73of27jL5zfw3XJB87ufK8rp7s8rsNLO2S3GV705MQ gzKTLaEbyv4aC8ACru03FBenG1UmlRotzPl9wUKXTrKucZF4aor/a31Yfssd+0P/QtDJ ZJhhzPcnKobHDIi0MigV8/HFig2gHiH3u7crZkO0FuJ+pa/tRvmZwLYGOb4VYaPxWU1T cQoeJKfdYRtRL4rsDfqMxcw8wlcDVKzmIKliJdNJEtYmrOyirAUprNNMK6mwj9ytNBDl NmdYV/kZw+7cGAY/7V+22sI7HUupbmsJTSXBCV3b2P9TxVa4jxkCV/Dpn8m76sh29kP+ 65Mw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id t127-20020a815f85000000b005a8a753979esi15097628ywb.245.2023.10.26.01.07.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Oct 2023 01:07:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 5D5B681143CF; Thu, 26 Oct 2023 01:07:29 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230283AbjJZIHN (ORCPT + 99 others); Thu, 26 Oct 2023 04:07:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229705AbjJZIHM (ORCPT ); Thu, 26 Oct 2023 04:07:12 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DD0DAB8 for ; Thu, 26 Oct 2023 01:07:09 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 7AAB421B3F; Thu, 26 Oct 2023 08:07:08 +0000 (UTC) Received: from suse.cz (pmladek.udp.ovpn2.prg.suse.de [10.100.201.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id BE9052C7AE; Thu, 26 Oct 2023 08:07:07 +0000 (UTC) Date: Thu, 26 Oct 2023 10:07:07 +0200 From: Petr Mladek To: John Ogness Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Mukesh Ojha , Chunlei Wang Subject: Re: [RFC PATCH printk v1] printk: ringbuffer: Do not skip non-finalized with prb_next_seq() Message-ID: References: <20231019132545.1190490-1-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spamd-Bar: +++++++++++++++ Authentication-Results: smtp-out1.suse.de; dkim=none; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=suse.com (policy=quarantine); spf=fail (smtp-out1.suse.de: domain of pmladek@suse.com does not designate 149.44.160.134 as permitted sender) smtp.mailfrom=pmladek@suse.com X-Rspamd-Server: rspamd2 X-Spamd-Result: default: False [15.00 / 50.00]; ARC_NA(0.00)[]; R_SPF_FAIL(1.00)[-all]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-3.00)[-1.000]; MIME_GOOD(-0.10)[text/plain]; RWL_MAILSPIKE_GOOD(0.00)[149.44.160.134:from]; DMARC_POLICY_QUARANTINE(1.50)[suse.com : No valid SPF, No valid DKIM,quarantine]; VIOLATED_DIRECT_SPF(3.50)[]; MX_GOOD(-0.01)[]; NEURAL_HAM_SHORT(-1.00)[-1.000]; RCPT_COUNT_SEVEN(0.00)[7]; RCVD_NO_TLS_LAST(0.10)[]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.20)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; MID_RHS_NOT_FQDN(0.50)[]; BAYES_HAM(-3.00)[100.00%] X-Spam-Score: 15.00 X-Rspamd-Queue-Id: 7AAB421B3F X-Spam: Yes X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.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 (howler.vger.email [0.0.0.0]); Thu, 26 Oct 2023 01:07:29 -0700 (PDT) On Wed 2023-10-25 17:16:09, Petr Mladek wrote: > On Thu 2023-10-19 15:31:45, John Ogness wrote: > > --- a/kernel/printk/printk_ringbuffer.c > > +++ b/kernel/printk/printk_ringbuffer.c > > +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. > > I would add some details about what expect from this value. Something > like: > > * @last_finalized_seq value guarantees that all records up to this > * sequence number are finalized and might be read. The only exception > * are too old records which have already been overwritten. > * > * Also it is guaranteed that the value can only grow. > * > * But it is just a best effort. There is _no_ synchronization between > * writers finalizing records and @last_finalized_seq updates. The result > * might be a lower value because updaters might not see finalized > * records from other CPUs. > * > * There is _no_ easy way to serialize these two operations. It would > * require to remember two values which might be called handled in: > * > * @last_finalized_id in desc_make_final() > * @last_readable_seq in desc_update_last_readable_seq() > * > * and these two values would need to be updated atomically together > * so that only the last updater of the @id part would be allowed > * to update the @seq part. Also it would require a barrier so > * that each writer would see the finalized state from other > * writers whom updated the @id part earlier. > * > * Summary: > * > * This value might be used by readers only to check the last > * readable seq number at the given time. They must count with > * the fact that new records might appear at any time. > * > * Beware that this value is not increased with every finalized > * record. It stays the same when there are not-yet-finalized > * records with lower sequence number. > * > * In particular, it does not guarantee that readers woken > * via wake_up_klogd would be able to flush all messages > * up to the one just stored by vprintk_store(). For example, > * it does not guarantee that console_unlock() would flush > * all pending messages. > */ The last paragraph sounds scary. But it is a pretty old problem. I was curious why nobody noticed it. IMHO, we are mostly safe in practice because: 1. Every printk() either tries to flush the consoles or queues irq_work to do a deferred flush. 2. Every printk() calls wake_up_klogd() to wake user space log daemons. 3. console_unlock() checks prb_next_seq() after releasing the semaphore. It goes back and flushes any message finalized in parallel or the parallel writer is able to take the console semaphore. By other words, even when one flush is not able to flush everything there always should be scheduled someone who would flush the rest in a near future. The only problem might be a missing barrier when some CPU sees a finalized record and others do not see it. But it is probably very hard to hit in practice. Anyway, I haven't been aware about this prb_next_seq() limitation until last week. We should keep it in mind or improve it. But it seems that it is not that critical in the end. Best Regards, Petr