Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp534154ybg; Wed, 10 Jun 2020 07:13:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyvCRorlMjTU4AMcL+CqlTI19NSWhbY/V+VpGvgT8VClxtqPCbR/d1c5t+AyCbOL04r58V+ X-Received: by 2002:a50:8425:: with SMTP id 34mr2674395edp.314.1591798391204; Wed, 10 Jun 2020 07:13:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591798391; cv=none; d=google.com; s=arc-20160816; b=kQO4DXKFVYWmw7SX5zs7Nvtjlax6ItgctvUxYRmM/mXh+4l0DkcN9D0GfYrwk9ew7Z RQr63DzO8ZbmIveit9d0WaBpqsG+k3LChQXlEDfokgUyPWLDWDsJEg44PTRPpssDP/7e ujNR/NRJGJMYNxvzyvFCA19RMOiK30K4hkUgGZgyjd65LsnCL8+0lUpIyKeC8Wn0BQWc bNDtb1hUK4swfZOO7pFbw2wFFarFKwsvaVSbSXf/LQ0PsyqIO0S0Nb20noJmjVYI99Fh fu53j8prpe0jJ4SjEEbOfVGGE+FB2qMJ50QguXzPf5LStxtsmvhp1R9Ujc3Cnk3NSSj0 u7JA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=b0QvdDviHYZ9AvyTll2uQ/xnsBZaxv0KLVmyUC6UuBw=; b=ypQL/kvKNWd3FLWd4L3dNazGWmKRfA/Zv7u6tztFdCeiP6bvRDkdSk5XbfoKSNxyC3 c/6ATuL4R1QNJL/A+0CTpOwBuQQSrsOCASeZXPiN7qIxXry8UxU3VHuyWH3ffjb+hXNc bNlO+0ChXXTpLM4Xf8sUPzsnxgiiy+w5nBYMWtbGmY6oQdYrY0NTmtvcyG/y8WFtD2eI Wa0GxI0Key5xs6jo5e/cRQcxcmgIuZ0HZNUStoIIEXK6XVVUJmnFJZl+Yon/gIPpyZ+2 HjcM5gZN+7TqvQgZBc0eiLMdj9GmlCYwRRfNc8fY4bvIAky8PDq8D7oLKBkHJkKJgM+D CS1A== 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 zb3si36918ejb.610.2020.06.10.07.12.47; Wed, 10 Jun 2020 07:13:11 -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 S1727801AbgFJJik (ORCPT + 99 others); Wed, 10 Jun 2020 05:38:40 -0400 Received: from mx2.suse.de ([195.135.220.15]:40034 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727007AbgFJJij (ORCPT ); Wed, 10 Jun 2020 05:38:39 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id AB83EAAC6; Wed, 10 Jun 2020 09:38:40 +0000 (UTC) Date: Wed, 10 Jun 2020 11:38:35 +0200 From: Petr Mladek To: John Ogness 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: redundant check in make_data_reusable(): was [PATCH v2 2/3] printk: add lockless buffer Message-ID: <20200610093835.GB4311@linux-b0ei> References: <20200501094010.17694-1-john.ogness@linutronix.de> <20200501094010.17694-3-john.ogness@linutronix.de> <20200609093103.GB23752@linux-b0ei> <87lfkwuwg1.fsf@vostro.fn.ogness.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <87lfkwuwg1.fsf@vostro.fn.ogness.net> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue 2020-06-09 16:48:30, John Ogness wrote: > On 2020-06-09, 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) */ > >> + > >> + /* > >> + * Guarantee the block ID is loaded before checking the tail > >> + * lpos. The loaded block ID can only be considered valid if > >> + * the tail lpos has not overtaken @lpos_begin. This pairs > >> + * with data_alloc:A. > >> + * > >> + * Memory barrier involvement: > >> + * > >> + * If data_make_reusable:A reads from data_alloc:B, then > >> + * data_make_reusable:C reads from data_push_tail:D. > >> + * > >> + * Relies on: > >> + * > >> + * MB from data_push_tail:D to data_alloc:B > >> + * matching > >> + * RMB from data_make_reusable:A to data_make_reusable:C > >> + * > >> + * Note: data_push_tail:D and data_alloc:B can be different > >> + * CPUs. However, the data_alloc:B CPU (which performs > >> + * the full memory barrier) must have previously seen > >> + * data_push_tail:D. > >> + */ > >> + smp_rmb(); /* LMM(data_make_reusable:B) */ > >> + > >> + tail_lpos = atomic_long_read(&data_ring->tail_lpos > >> + ); /* LMM(data_make_reusable:C) */ > >> + > >> + /* > >> + * If @lpos_begin has fallen behind the tail lpos, the read > >> + * block ID cannot be trusted. Fast forward @lpos_begin to the > >> + * tail lpos and try again. > >> + */ > >> + if (lpos_begin - tail_lpos >= DATA_SIZE(data_ring)) { > >> + lpos_begin = tail_lpos; > >> + continue; > >> + } > > > > I am sorry if we have had this discussion already in past. > > We have [0]. (Search for "Ouch.") I see. Thanks a lot for the pointer. > > Well, it would just prove that it really needs a comment why this > > check is necessary. > > The comment says why it is necessary. The previous read of the block ID > cannot be trusted if the the tail has been pushed beyond it. Not really. The comment describes what the check does. But it does not explain why it is needed. The reason might be described be something like: * Make sure that the id read from tail_lpos is really * pointing to this lpos. The block might have been * reused in the meantime. Make sure to do not make * the new owner reusable. But wait! This situation should get caught by the two existing descriptor checks: > case desc_committed: > /* > * This data block is invalid if the descriptor > * does not point back to it. > */ > if (blk_lpos->begin != lpos_begin) > return false; > desc_make_reusable(desc_ring, id); > break; > case desc_reusable: > /* > * This data block is invalid if the descriptor > * does not point back to it. > */ > if (blk_lpos->begin != lpos_begin) > return false; > break; Here again the comments describe what the check does but not why. I would write something like: /* * The block might have already been * reused. Make sure that the descriptor really * points back to the checked lpos. It covers * both situations. Random data might point to * a valid descriptor just by chance. Or the block * has been already reused by another descriptor. */ BTW: In theory, it might happen that the ringbuffer is reused many times in the meantime and (blk_lpos->begin == lpos_begin) again. But then lpos_begin would be valid lpos again and even the range check would not catch this as well. So, I think that the lpos range check is still redundant. We might describe it as an extra paranoid check but I am not sure if it is worth it. But I would remove it and keep the code "simple" and design "clear". Well, I do not resist on it. Best Regards, Petr