Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp580715rdf; Fri, 3 Nov 2023 08:58:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE0IXsDWCy5Oh9FAwGoKSznLsPcP0Emksu/2BDGN1vw15WZaViJ10Qfm+s9GvlmKi2Af0o/ X-Received: by 2002:a05:6a20:4303:b0:13e:fb5e:b460 with SMTP id h3-20020a056a20430300b0013efb5eb460mr4096283pzk.0.1699027098907; Fri, 03 Nov 2023 08:58:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1699027098; cv=none; d=google.com; s=arc-20160816; b=Is0pt0gvltFe+4xNcoSMSOTkvd5h5KOcS/HpyaPQWZj+XT5VJ8TEyIHis5BotLSpV4 QCtxjwYMW0jD8LACrqFdlxq0fe6ZKObigWhWyLN6U8/zdgAMhLh9hacus5zi9+iybPGr Kh7DhMzK5OykvQLBcx6fnisbNRjMhHd5fBo6FbalDATKykXa93uMktmr5NwdNqupNYrd 4euvMRfbvFh0eRXuijxEKeUheRmXc7+ABTR4KOxT6dL9bRxbl3E3eixtqZQ5mXBYTEFD awZz6PjahHc8q9O+jOoZE+4NwNvqZ3TQejgVPcNCbtWE329ZtQhJ4uEjTzLZf9OfTBq5 8E6w== 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:dkim-signature; bh=vFnigUx+7qpYC+4Dey3mI9qEiFvu9YLcz7iE7OLqZ90=; fh=+6k46dcAtPfhgsUUmykne56od42/izkYcLVAPAF7Gb0=; b=zxbnxdv8ZWmcYdbVTaqAEyDgGkXUcvBr0uyhU4r68X8CaORebwdmqznTql2E58uoGN oZKihcAE0KwQ3vr3Rx7gHU8wTvqsuD3Idmz+XUoT/F4fhMRySYzbF+4O0+MK3EGV6dtN +wPixAtGXCvDEyM9BFsJnAazpAnv7Tf9EIh4ggf3a1uYqiWHGLkIBv3k2Q1qE44dcxrF S/qcK24MZPsCNml6+zhBySmNCLJpKaJSVVo6Gr1NcSNXxKbXN0Q9o4F0kY2x0SdLw+9g mWMQNZvrlpwwlMcquBmYCsUEcTlEQX0+bczAFG2l4aQWPeZ3vY6Jzg6ff6TMPrHjXJNs YXkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=VofaeDom; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id z130-20020a633388000000b005898e10f9b9si1685613pgz.213.2023.11.03.08.58.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 08:58:18 -0700 (PDT) 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=@suse.com header.s=susede1 header.b=VofaeDom; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 1B32683C1E01; Fri, 3 Nov 2023 08:58:18 -0700 (PDT) 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 S1344176AbjKCP6O (ORCPT + 99 others); Fri, 3 Nov 2023 11:58:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49660 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344730AbjKCP6L (ORCPT ); Fri, 3 Nov 2023 11:58:11 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D15C2D52 for ; Fri, 3 Nov 2023 08:58:03 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 8671B1F45F; Fri, 3 Nov 2023 15:58:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1699027082; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=vFnigUx+7qpYC+4Dey3mI9qEiFvu9YLcz7iE7OLqZ90=; b=VofaeDom1pVNONfjHJKV2C4nen1wdPd5tNWU8B4lP67L1CAT0gxyWpPAAqwpd7CGiMJBKV ylf/2V97EZvUCkrWSnB+JZhHcmX4P1VSBrayb4HOI5DlE4HVxEX903Il9KNb2Tej0V3Bbu OfmrRuOaTAzOTl3ltYyIB7wO7JxcoDo= 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 CD51A2C38D; Fri, 3 Nov 2023 15:58:01 +0000 (UTC) Date: Fri, 3 Nov 2023 16:58:01 +0100 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: <87zfzwp8pk.fsf@jogness.linutronix.de> <8734xnj74k.fsf@jogness.linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <8734xnj74k.fsf@jogness.linutronix.de> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Fri, 03 Nov 2023 08:58:18 -0700 (PDT) On Fri 2023-11-03 14:37:23, John Ogness wrote: > On 2023-11-03, Petr Mladek wrote: > >> Generally we have not concerned ourselves with readers. But I agree we > >> should make the optimization coherent with what a reader can actually > >> read. It might save some CPU cycles for polling tasks. > > > > I wanted to agree. But then I found this scenario: > > > > CPU0 CPU1 > > > > console_unlock() > > console_flush_all() > > > > printk() > > vprintk_store() > > return; > > prb_final_commit; > > > > console_trylock(); # failed __console_unlock(); while (prb_read_valid() || console_trylock(); I added the __console_unlock() and console_trylock() to have the full picture. > > > > Now, the race: > > > > + console_flush_all() did not flush the message from CPU1 because > > it was not finalized in time. > > > > + CPU1 failed to get console_lock() => CPU0 is responsible for > > flushing > > > > + prb_read_valid() failed on CPU0 because it did not see > > the prb_desc finalized (missing barrier). > > For semaphores, up() and down_trylock() successfully take and release a > raw spin lock. That provides the necessary barriers so that CPU0 sees > the record that CPU1 finalized. I see. Hmm, we should document this. The spinlock is an implementaion detail. IMHO, up()/down()/down_trylock() are quite special. I think that the theory is that lock() and unlock() are one-way barriers. And trylock() is one-way barrier only when it succeds. By other words, _trylock() on CPU1 normally would not guarantee that CPU0 would see the finalized record after unlock(). [*] Maybe, we could rely on the existing behavior but we should at least document it. Note that I thouht many times about using spin_trylock() in down_trylock(). It would make more sense because trylock() should not be be blocking operations. But I see now, that it might break users depending on the full barrier. Note: It is possible that I get it wrong. It is so easy to get lost in barriers. > >> Writing and reading of @last_finalized_seq will provide the necessary > >> boundaries to guarantee this: > >> > >> ...finalize record... > >> atomic_long_try_cmpxchg_release(&desc_ring->last_finalized_seq, ...); > >> > >> and > >> > >> atomic_long_read_acquire(&desc_ring->last_finalized_seq); > >> ...read record... > > > > Yup. something like this. > > > > Well, it is suspicious that there is no _release() counter part. > > Take a closer look above. The cmpxchg (on the writer side) does the > release. I have the litmus tests to verify that is correct and > sufficient for what we want: to guarantee that for any read > @last_finalized_seq value, the CPU can also read the associated record. The barrier is only in _prb_commit() which sets the committed state. desc_make_final() uses cmpxchg_relaxed() so that it is not guaranteed that other CPUs would see the "finalized" state. > I am finalizing a new version of the "fix console flushing on panic" > series [0] that will also include the prb_next_seq() fix. If needed, we > can continue this discussion based on the new code. OK, maybe it would look different in the new version. And it might be better to continue the discussion on the new code. Best Regards, Petr