Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp644577lqo; Fri, 10 May 2024 10:08:25 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW3DNe8+Werdt1zdZTLV+CEmdFz58emoeGt4CL3+JGhXOoAkDOvVZM/82rSyaXzujkjfSHxrQppYp0GGwV6Od8QgAzfjgo5MLudhmRd7A== X-Google-Smtp-Source: AGHT+IEs3KlVex8qFnBxdnQeM9e8zj8Dd3nhBBpaXGcgZvbwD6xVNKUMIO/qOusd+pIwFUIN/45m X-Received: by 2002:a17:907:552:b0:a59:a6b2:b898 with SMTP id a640c23a62f3a-a5a2d57b00dmr200330066b.27.1715360905726; Fri, 10 May 2024 10:08:25 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715360905; cv=pass; d=google.com; s=arc-20160816; b=fZCtiULYXuKKnJidBvyyr7PcOZ6lJsedny05my4tNj+NxifScGg2vvWQ3LnLTlOZ5P oSY1KIDXgncbxDOW+XaW2Evx/+9G6X7m5ZcYGTSL9anml6NSjjpWo9TOnrTx2sPVF1O+ +IEXbBy2/UbA5myczyZ6QTL1z/sCXMJ1LZPPv59sKYBiCV2VS7dZS88j57TSzCktJizG PqK3FlcpZB9zyn6IlJcZFOwNZt6amrfkICxV4G8Nea/CCY4KCngBmmRZV7S3z05PdbhP JeUAFnzjv/MWvqVkG8P2+mzcyb34AkxH9GsztizDEuf9KmnBypmvit+lEA+3Wcfcxm4R WX1g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=U03qeE2FT7DlLpOgj/sRDZR0pWeBHd7ufNCAOskScfY=; fh=7DmST56HsxvP98uw537w4U/fRJeJRCLY0PjmNSOZV9o=; b=IQHVp1tSJzzni0HIkhYnB+bkT1TkbuTte9Og3pAfzUSx2D+wrKRO646XyC8M3y4y4/ InczSKL/CmVRnNX5DHWDP3gcHrYIivtc+NUPPV7quAUzP7f1b9RANa8Qga3gsdwGoCN0 eOC87cxz7VCi4mdQbreeQVpxPubCgyky71+NKFJ3RVQY0xb7qifnojD5uKe9ovRlpnf+ uRVGnKM9Zy45/MEUUA1R93xDSwFmFo21Q3S3H6+qVT2M8u4xZ7sEYGAjamXOJSWf3XLs BACi0IJr83k603y44bavqWy1C2+VnvKCflIDSnZOvZfoGTEk7SOO9iSBEZLFnB4gWwKY /8nQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=DEM0zyJe; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-176037-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-176037-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a640c23a62f3a-a5a17bafeccsi218759666b.683.2024.05.10.10.08.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 May 2024 10:08:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-176037-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=DEM0zyJe; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-176037-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-176037-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 76D4F1F23A41 for ; Fri, 10 May 2024 17:08:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3BE2617C6A; Fri, 10 May 2024 17:08:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DEM0zyJe" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5C3F8168BD; Fri, 10 May 2024 17:08:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715360897; cv=none; b=uj3zB9LCtPuQdS2l4Vi/0oqNEiWmXn/+l62cWSloeEDIiQB3HdCRFM9uTdQszqlFu78RHDYciBfLLcIRAoOXI/J/ZtoS/61bxAFgsWBDYbtd1UfYnZFznzhpFXrOgdH9Rt2VDAZZVgnJA5X+2mbqZORcaxGpY75YJb8QoefE0iM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715360897; c=relaxed/simple; bh=FeldaRg6tINYOeoeFmz/DOcrzlUptv3ygea75vJ6kSY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=qKnHpv9PoC5yVorbAS+T/ILOmJRokB08no39ce2e7QqpD1OcGjR2caDYkSldJKWtGyEXpf2k/7Oq/LCcuraKR28Bv8jL5yHzzwYuWyDvVSaGIdhwU7emXmDbm8Oc97HyjQEB9v63oeM53AFn+kpeulhosBNUetSkkY0RzDOn5Fo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DEM0zyJe; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id E99BFC113CC; Fri, 10 May 2024 17:08:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715360897; bh=FeldaRg6tINYOeoeFmz/DOcrzlUptv3ygea75vJ6kSY=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=DEM0zyJe/j8AUmcUc1mKG2HoRzTBnZ3MRLZhztcGakB5xxz+/iYXoVzymrn8tmTHi Jnclldj4TFmUqzNZoZdouLKBq92Ul5hz5sXIQI5pNRzVtnHLjA+5vmGape1e6EFaOe B7Xh8/aV3u9kkkjLD6RdOsjAM0EHOMO1G/RGblzTV6QcfW7KBFasH0/rc0dxDTkvYl Fy3zgzYZOh7Td5qrP85zptpVf+aFPH4cj9xQuubw5pfpQV3efIeXdaxnviJFhqqkcf Fm4G78nkjQY1T2db0ffZO+59EXsY1q0aJCLBzywR57Ed8AO23x4wF5wKJd86y109h2 w8uPMiIqN7lAw== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 8DC36CE094C; Fri, 10 May 2024 10:08:16 -0700 (PDT) Date: Fri, 10 May 2024 10:08:16 -0700 From: "Paul E. McKenney" To: Bart Van Assche Cc: Breno Leitao , Jens Axboe , "open list:BLOCK LAYER" , open list Subject: Re: [PATCH] block: Annotate a racy read in blk_do_io_stat() Message-ID: Reply-To: paulmck@kernel.org References: <20240510141921.883231-1-leitao@debian.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Fri, May 10, 2024 at 09:20:58AM -0700, Bart Van Assche wrote: > On 5/10/24 8:41 AM, Paul E. McKenney wrote: > > On Fri, May 10, 2024 at 07:28:41AM -0700, Bart Van Assche wrote: > > > On 5/10/24 07:19, Breno Leitao wrote: > > > > diff --git a/block/blk.h b/block/blk.h > > > > index d9f584984bc4..57a1d73a0718 100644 > > > > --- a/block/blk.h > > > > +++ b/block/blk.h > > > > @@ -353,7 +353,8 @@ int blk_dev_init(void); > > > > */ > > > > static inline bool blk_do_io_stat(struct request *rq) > > > > { > > > > - return (rq->rq_flags & RQF_IO_STAT) && !blk_rq_is_passthrough(rq); > > > > + /* Disk stats reading isn’t critical, let it race */ > > > > + return (data_race(rq->rq_flags) & RQF_IO_STAT) && !blk_rq_is_passthrough(rq); > > > > } > > > > void update_io_ticks(struct block_device *part, unsigned long now, bool end); > > > > > > Why to annotate this race with data_race() instead of READ_ONCE()? Are > > > there any cases in which it is better to use data_race() than > > > READ_ONCE()? > > > > We use this pattern quite a bit in RCU. For example, suppose that we > > have a variable that is accessed only under a given lock, except that it > > is also locklessly accessed for diagnostics or statistics. Then having > > unmarked (normal C language) accesses under the lock and data_race() > > for that statistics enables KCSAN to flag other (buggy) lockless accesses. > > Can using data_race() instead of READ_ONCE() result in incorrect code > generation, e.g. the compiler emitting a read twice and reading two > different values? It could. And if that was a big enough problem, you might want READ_ONCE() there. The cases in Linux-kernel RCU involve quantities that rarely change, so even if the compiler does something counterproductive, the odds of output being affected are low. So why not just always use READ_ONCE() for debugging/statistical accesses? To see that, consider a variable that is supposed to be accessed only under a lock (aside from the debugging/statistical access). Under RCU's KCSAN rules, marking those debugging/statistical accesses with READ_ONCE() would require all the updates to be marked with WRITE_ONCE(). Which would prevent KCSAN from noticing a buggy lockless WRITE_ONCE() update of that variable. In contrast, if we use data_race() for the debugging/statistical accesses and leave the normal lock-protected accesses unmarked (as normal C-language accesses), then KCSAN will complain about buggy lockless accesses, even if they are marked with READ_ONCE() or WRITE_ONCE(). Does that help, or am I missing your point? Thanx, Paul