Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp792561lqo; Fri, 10 May 2024 15:35:45 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXnr0cKcByGaxGUBjva4jSCXtyFSNF8OgAFvE+DXotvbq8WJo75qi9I6EaYAya6AwLm56K3Ee7ZhbgqX8yWLot+LjtxrVDO6RDU3n4WRg== X-Google-Smtp-Source: AGHT+IHFCYDk2/o+NHdf4dPxmbN+7fXKcAGrmurNHZJtf75kSH1/R5pebvYbRo+CyXUojfrxwDa3 X-Received: by 2002:a05:6a20:5653:b0:1af:a4b4:53eb with SMTP id adf61e73a8af0-1afde1b7e93mr3908168637.42.1715380545510; Fri, 10 May 2024 15:35:45 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715380545; cv=pass; d=google.com; s=arc-20160816; b=K1xGJcdRA8JElw0ilEYzXDm3x4ranAz6Jem8sYCC7ctkXClaJBrlmseTBS80lAMrmp YZ898mAMhi+2G1wPVNTP1nuE5+8ZlCVw+9+lSV3PdG6yOAijok/zWMazKTxYgLjxov73 6yjfpwfYSusw9N0VEzCbDp03+ReccfGQe41OITOBzKJKEdDGaVWzJiVSY4OZVjFnZcri SBQ9Vn4Jw0Gzxjs4DpLwkPpkdoSTGw/D6L6r4dkwTK9xAlhy105MIIR5ytu5dOutWJSh BNNGBvK88L1Dv+znEgbVMnWRKnO4dn8mh/WEGEa7emUgMASR7aAWrK+272m5K9/xfpyk W86g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:reply-to:message-id :subject:cc:to:from:date:dkim-signature; bh=PlTeXBARCa3AFCvVYYXJRJMiVY/tpguzE3AklV8SvUM=; fh=vYVgPCnUodSTCEVjh4OuCJUL93JWUs8huFBwVsrzMUQ=; b=VDh67lmXXov8S6JwQNm3yhwrSKGtcjm4x7xcHMlMPbPIk7P1yUFa281kex/Fgqk0G6 qqb9z/sfrpvvsbr5OSV9NOXurPA/ieVvKFSJKspxCffgSuOV6gQ/2KzGbMggkiu41kjb nASNUn8DfuC5zZdeU1m3xjUBkX2OPPmCKeQZGmz3gkhe7kkyU5/oyp2OqSgWkJRtwE7z EbSjMhPZGw8zCqM2n4lIvJ+FNeaF0GryizJ7RUoCcyVutNzuTESM2Ptbh53qLR/hoMjH Sj06/F1jFfoEZyHoVFC5HFFiOkRcf3Q2z0fqFEHEGFxJxIOnBIjKtr8qQnYEa9ibmPml tXMg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=I8IMZvav; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-176276-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-176276-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id 98e67ed59e1d1-2b62886678fsi6631505a91.7.2024.05.10.15.35.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 May 2024 15:35:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-176276-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=I8IMZvav; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-176276-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-176276-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id DE758B23BC2 for ; Fri, 10 May 2024 22:35:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A9E6F17108B; Fri, 10 May 2024 22:35:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="I8IMZvav" 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 C9ED3635; Fri, 10 May 2024 22:35:31 +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=1715380531; cv=none; b=NEkxEDvZpZuIvzc2pjMKwLh6xVljV7h8FkjKFjPCv16Azi+I0y1zV2K7kH2/XwyTVJOYrIg9w3Sj722wtRY8iF5XSdgky8Lb1pWP8wMe/yX2yCI5P3ejfgqnYrJJak16yDJK/Akm6kw7HzMHkXSH179yXdnXNcz4Kr3CoqvjBUE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715380531; c=relaxed/simple; bh=RsSwNAFVJJDe067v5AIvDeUDB4gAsGvhvKhNsvBq+b8=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=T3qVZT2imONdSFAadmx9Qnnn11mLW6ShQ9O3ZESwKvF8jvgpdAA9RC7DDZ69lvI1yknzIk1h4MuiT8MuC1p3GpKFyM1vdzZbqVnYQ5YfIMX2GafxHvk+A7i0/Zal1g+4QkaR2ooexohtvscpHlCjKTDv8LJexCDePkEYMIqyCGA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=I8IMZvav; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3D408C113CC; Fri, 10 May 2024 22:35:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715380531; bh=RsSwNAFVJJDe067v5AIvDeUDB4gAsGvhvKhNsvBq+b8=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=I8IMZvavCQAC9NFiDsV64W9bdaIP+RM0aCYkfR/kIvdkVMoQFs2Eby+IlPTjnH2zg 6sjoUfTgk6eUTSLEyUIE/2VvBJNvsWF5ja5lxeHLThozbu5wp+Xv6DZGOLuBvZvr/Z ykXPojWBixHQdSRVzVZ2KOPRCdzLxFjArZO/eQernL5JuT8uTl5uPGvuI5HCc7k2jP FXu52bDFyoxNuowaNBL12ECakn80Fq4WCFEBQxSVsO8+hnlsAfEx8t8E2ttFj8snIo Mb90BHvkLO6ULqskrLSNNfQq0VtEXV7y1C52Vr997hFmDf65JJEJrQ1vFE2Ikva3AH MfCCwP7YsWiww== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id D2968CE0F90; Fri, 10 May 2024 15:35:30 -0700 (PDT) Date: Fri, 10 May 2024 15:35:30 -0700 From: "Paul E. McKenney" To: Bart Van Assche Cc: Breno Leitao , Jens Axboe , "open list:BLOCK LAYER" , open list , Marco Elver Subject: Re: [PATCH] block: Annotate a racy read in blk_do_io_stat() Message-ID: <447ad732-3ff8-40bf-bd82-f7be66899cee@paulmck-laptop> Reply-To: paulmck@kernel.org References: <20240510141921.883231-1-leitao@debian.org> <4d230bac-bdb0-4a01-8006-e95156965aa8@acm.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=us-ascii Content-Disposition: inline In-Reply-To: <4d230bac-bdb0-4a01-8006-e95156965aa8@acm.org> On Fri, May 10, 2024 at 01:30:03PM -0700, Bart Van Assche wrote: > On 5/10/24 10:08 AM, Paul E. McKenney wrote: > > 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? > > Thanks, that's very helpful. Has it been considered to add this > explanation as a comment above the data_race() macro definition? > There may be other kernel developers who are wondering about when > to use data_race() and when to use READ_ONCE(). Well, sometimes you need to use both! Does the prototype patch below help? (Also adding Marco on CC for his thoughts.) Thanx, Paul ------------------------------------------------------------------------ diff --git a/include/linux/compiler.h b/include/linux/compiler.h index c00cc6c0878a1..78593b40fe7e9 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -194,9 +194,18 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val, * This data_race() macro is useful for situations in which data races * should be forgiven. One example is diagnostic code that accesses * shared variables but is not a part of the core synchronization design. + * For example, if accesses to a given variable are protected by a lock, + * except for diagnostic code, then the accesses under the lock should + * be plain C-language accesses and those in the diagnostic code should + * use data_race(). This way, KCSAN will complain if buggy lockless + * accesses to that variable are introduced, even if the buggy accesses + * are protected by READ_ONCE() or WRITE_ONCE(). + * + * This macro *does not* affect normal code generation, but is a hint to + * tooling that data races here are to be ignored. If code generation must + * be protected *and* KCSAN should ignore the access, use both data_race() + * and READ_ONCE(), for example, data_race(READ_ONCE(x)). * - * This macro *does not* affect normal code generation, but is a hint - * to tooling that data races here are to be ignored. */ #define data_race(expr) \ ({ \