Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp716186ybg; Wed, 10 Jun 2020 11:44:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxRGrdxqN03nkXAn1+2wycOw0abCHFo94frsEyG/WrpvQ70c+m4C4u5dUG5bRDnFuL1h6lP X-Received: by 2002:a17:907:217a:: with SMTP id rl26mr4607209ejb.209.1591814656538; Wed, 10 Jun 2020 11:44:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591814656; cv=none; d=google.com; s=arc-20160816; b=n8NYoK/wRMftmXIX2jJc2rt+Jr2ZeiUnXGoahSVNS72x1vG9/9xkN+rBsun1OT/b2K Q0XY7zvlNOWgQIvW9JWKINdzrOwcbaxM62rOkYs3ztrC4bdYt6Mqb71WnxUtj1NJNgLP dvjukOyKzGXbcZpFF5xyqCbGaQUe8bJhx/xWvNipofG6gRUg/zqPlzMTxuXIKpQNaLj/ WH8sJyWeA0aiilpE4rQ4M7Zy9GkRJEyvqhp7a1C/V0I3OewVoCw21ts0HlNK1r93p79M jW9RPYRcXITcqhlqsIrwXiubfbZcpKjfi4FRgjrVNEUXjltJC8xaABlHFHsblGeygAwm OiMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:message-id :in-reply-to:date:references:subject:cc:to:from; bh=ZNc6x8jUrbEG2WJ2ws9epcZ48FLQ4v3VGRJPK4x4vu4=; b=ndrGBdajhQOpvFzF84FNzauIcqrj5bLws8KhKG2UfdTrHn0WLWQq54YRcN9KhN5pKd PQz1fsYvRyfVsyDMnTVVpjJCrDyXeq0Uq7r006/Fl60p/+RJza8G5sYvNZcgp7p+iE9a HSsrhM9KZsHk7+dqPtYdnDDL3UwL71vbS1CKpVRWZucI2GBGNXREw3hCpfId/kBbJWJl NXbQsH1RBSRNOA7cOWz0cEdgDBmGu0Rqw6kXYtqgB5i759lL3tN1mTBmOYhnq1dbHOYU Xcs+2Br72M1zeAFbdf8MnJTZxeHPKZZrdaeAvL/N/1x8T0e6LOnSxTGOfqPc+HePiyxX R9lA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id jz17si530883ejb.243.2020.06.10.11.43.54; Wed, 10 Jun 2020 11:44:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729579AbgFJNzH (ORCPT + 99 others); Wed, 10 Jun 2020 09:55:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58322 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726449AbgFJNzH (ORCPT ); Wed, 10 Jun 2020 09:55:07 -0400 Received: from Galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CCCADC03E96B for ; Wed, 10 Jun 2020 06:55:06 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=vostro) by Galois.linutronix.de with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1jj1C1-0005zZ-Ex; Wed, 10 Jun 2020 15:55:01 +0200 From: John Ogness To: Petr Mladek Cc: Peter Zijlstra , Sergey Senozhatsky , Sergey Senozhatsky , Steven Rostedt , Linus Torvalds , Greg Kroah-Hartman , Andrea Parri , Thomas Gleixner , kexec@lists.infradead.org, linux-kernel@vger.kernel.org, Paul McKenney Subject: Re: blk->id read race: was: [PATCH v2 2/3] printk: add lockless buffer References: <20200501094010.17694-1-john.ogness@linutronix.de> <20200501094010.17694-3-john.ogness@linutronix.de> <20200609071030.GA23752@linux-b0ei> <87tuzkuxtw.fsf@vostro.fn.ogness.net> <20200610084248.GA4311@linux-b0ei> Date: Wed, 10 Jun 2020 15:55:00 +0200 In-Reply-To: <20200610084248.GA4311@linux-b0ei> (Petr Mladek's message of "Wed, 10 Jun 2020 10:42:48 +0200") Message-ID: <87k10fowjv.fsf@vostro.fn.ogness.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2020-06-10, Petr Mladek wrote: >>>> --- /dev/null >>>> +++ b/kernel/printk/printk_ringbuffer.c >>>> +/* >>>> + * Given a data ring (text or dict), put the associated descriptor of each >>>> + * data block from @lpos_begin until @lpos_end into the reusable state. >>>> + * >>>> + * If there is any problem making the associated descriptor reusable, either >>>> + * the descriptor has not yet been committed or another writer task has >>>> + * already pushed the tail lpos past the problematic data block. Regardless, >>>> + * on error the caller can re-load the tail lpos to determine the situation. >>>> + */ >>>> +static bool data_make_reusable(struct printk_ringbuffer *rb, >>>> + struct prb_data_ring *data_ring, >>>> + unsigned long lpos_begin, >>>> + unsigned long lpos_end, >>>> + unsigned long *lpos_out) >>>> +{ >>>> + struct prb_desc_ring *desc_ring = &rb->desc_ring; >>>> + struct prb_data_blk_lpos *blk_lpos; >>>> + struct prb_data_block *blk; >>>> + unsigned long tail_lpos; >>>> + enum desc_state d_state; >>>> + struct prb_desc desc; >>>> + unsigned long id; >>>> + >>>> + /* >>>> + * Using the provided @data_ring, point @blk_lpos to the correct >>>> + * blk_lpos within the local copy of the descriptor. >>>> + */ >>>> + if (data_ring == &rb->text_data_ring) >>>> + blk_lpos = &desc.text_blk_lpos; >>>> + else >>>> + blk_lpos = &desc.dict_blk_lpos; >>>> + >>>> + /* Loop until @lpos_begin has advanced to or beyond @lpos_end. */ >>>> + while ((lpos_end - lpos_begin) - 1 < DATA_SIZE(data_ring)) { >>>> + blk = to_block(data_ring, lpos_begin); >>>> + id = READ_ONCE(blk->id); /* LMM(data_make_reusable:A) */ >>> >>> This would deserve some comment: > > I wonder if the comment might look like: > > /* > * No barrier is needed between reading tail_lpos and the related > * blk->id. Only CPU that modifies tail_lpos via cmpxchg is allowed > * to modify the related blk->id. CPUs that see the moved tail_lpos > * are looking at another block related to the new tail_lpos. > * It does not mater when the previous winner modifies the previous > * block. > */ Sorry, but this comment does not make sense for me. The tail is pushed _before_ the block ID is modified. So any kind of barrier here (where we read the tail, then the block ID, i.e. the same order) would be inappropriate anyway. Also, this comment only talks about when a new value is seen, but not about when the old value is seen. IMO it is seeing the old value that is worthy of a comment since that is the only case with a data race. In preparation for my next version I have added the following comment: blk = to_block(data_ring, lpos_begin); /* * When going from lpos to block pointer, the wrap around * feature of the lpos value is lost. Since another CPU could * invalidate this data area at any time, the data tail must * be re-checked after the block ID has been read. */ id = blk->id; /* LMM(data_make_reusable:A) */ I think this comment also helps to further clarify "why" the following data tail check occurs. John Ogness