Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp2013675lqo; Mon, 13 May 2024 05:41:46 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWTfZlOKrPB9yQVm7g2tARqKcJgqgMuQbO+ARdpf6HxCQOU4FxttlkUct+9wr9RC10N7VUTVjLxjeuzgzHVdOIWjhPBwYCl5oe9HzgiSQ== X-Google-Smtp-Source: AGHT+IHCWEMHkrFcGo65mzu5ANSxwLUXSBLbY4Zr9nTB+ZZYHmE7m70KVH490pJZfFL60MqZkioZ X-Received: by 2002:a05:6e02:19cf:b0:36c:c619:41b7 with SMTP id e9e14a558f8ab-36cc619424dmr119603725ab.20.1715604106163; Mon, 13 May 2024 05:41:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715604106; cv=pass; d=google.com; s=arc-20160816; b=Zn4SMiOYfPvnOv7bAxoe84rVOQ4qwE1mbvvsU43vM7s24QLMgWqUM99INAh76TuoTL bHxdohPYgc14zldk3vmneVl1FDOqfG6sqGmCAdI2JrSTyxMdf4aLh6+l/ir/COXxjpJi RsM9ZSlUL9ir4/pqicZgUVdW49NwV2MYeErqnIq9lrCdiodjRAHCz8f3Rv44e6C/s5oa DmLotGPxrJ2BhRnl0VpGEtuxonPxjODRQ9pKq/1h6471N21vfwSt0Y0K/psYZgvKIbqa whVmOi3G70OzZWkkqA+eEuLRVDpM9AP1Z0RPqsiXRjhlPT5Rv0wXiocIISQHf4QyXv3C gFMg== 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:message-id:subject:cc :to:from:date; bh=OZp92t/tgjMWQaWxzSO1yWcazbBOvy/GSBsy+000DoM=; fh=uWPkIMx67MN9yqM981GsU3OD2bbQfvAWE3GWJKJ9mLw=; b=FR4WkwWnOHqaMv2mFm+7Sa6veHSR2p0WK2POwOVeyGWWcIc6gog/yWjEcs5m1wwfdF xH7rNG6RT5KDOobqdkq6gPutHlpIFmEdqYYOsNEYyf10YmQOnEqM1RmegQekwNeKrjAM O5ZB0vGdEEybLtKLiOThkO1aQw/FqRA5pyzQyDnp204M/5eWyPZmebSbGBp/SeNNWdkc jAxIsURVmf8OwwRW2WriDqnJVgMni0/4Sb1K5BbSb0RSyDPL+xduM+dzIvuM7pbCqev1 mWzJZF7ZleFup6dpVCR6Ho9o57+NyxeQWgZ4H4wUN/pT/mjxYxzR/3Dsflj4xuiWa7sE uUtw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-177569-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-177569-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id 41be03b00d2f7-63411530b78si8713109a12.558.2024.05.13.05.41.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 May 2024 05:41:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-177569-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-177569-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-177569-linux.lists.archive=gmail.com@vger.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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 8851C281D8C for ; Mon, 13 May 2024 12:41:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E529814F9CF; Mon, 13 May 2024 12:41:36 +0000 (UTC) Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2DF47A3F; Mon, 13 May 2024 12:41:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715604096; cv=none; b=PUZ9JNYl+SDWsxvRmybicCyh+xlw82mrwlWxkfCooFK7jenIgbMPPbsDHeNx2ESqzi2FvGEHS9PfxbUakR95k+UyaUJWwkhiS3tVuTH2JBlziRB1NrqRS438SpMJH47m/OkpxSf2mhZHXQyTf9qujeAWB6zam17z2kWc3CPPVow= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715604096; c=relaxed/simple; bh=iyglkIQB6KKTyXDEKBd19IqLBit9NFZJ94NAOwpbG6M=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=cJCnDWW8OYyj6Nxlbem/glX2j+2DjxgxkASD48lCPgtr51n8Xs9Av5aAGw2GdWoY6TbpCl+u+NASQc7OoU/HRJ/FasEGp5p1nfV7yv7HlLFQmGVemWkbOIKXF1ka7F+6qT/iHF92ArBzQqyoe+gBG4QTO6ep+vlNkfMEUIssAqA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=debian.org; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.214.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=debian.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-1ed96772f92so34888895ad.0; Mon, 13 May 2024 05:41:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715604094; x=1716208894; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=OZp92t/tgjMWQaWxzSO1yWcazbBOvy/GSBsy+000DoM=; b=o+4gqFsHXRthNNW2yWxpBI+vE6b73PSv3LEBaqoMzU7L0dm/jkqe6v1ajPbtIee5rU rrQcOIO1p+2OytQknvFtlDPjsN7jF4+PVnSmJ2Xo6AYvh2w5CKlmZ4JRS4l2juOvUTdD ZqaGFexrTK5qCECwciTj+TXG0onAcYTUevt0zfLAkx5891meOBriPEmTTBiFLmNxtzUA LRtAYEcUITkWCBe8Vtwx/+pGIdDSdBQQAubekxhuaQpzRPVkWMA8zq8b2Be3zpjLHSdU ShhLO9awz7hv7XncVbpsGCBLdK1iyG3kNdR9LqBG1GxRsRe0Q4E5O439G3xjRHMQrqwS l+Pg== X-Forwarded-Encrypted: i=1; AJvYcCVgGxD/6NZXPlm4eMH+O4E3c0mpeFdmD1WkBI3W0pEzCihH+ssLEUGciw2KAyjOn1ylbI66GxQ1N7OpoRXnbNwwnc8ILAPb0iJgqRIv2Yq9FX6H9HX55oDncqpYamfyulu5V/afDDLpWovNGA== X-Gm-Message-State: AOJu0YzWEwekx6gRHr+T/QOy8JCMDbpIYPt0PNyxXK1UDylVm6avqxhh NUWXVBdweM9b+4JUG8+Um4D0NSoJm1Z4nkMWWQ9G8FJnY5RjsDKP+ir73Q== X-Received: by 2002:a17:903:2301:b0:1eb:14e3:5d6e with SMTP id d9443c01a7336-1ef43d2ecbfmr116229015ad.31.1715604094367; Mon, 13 May 2024 05:41:34 -0700 (PDT) Received: from gmail.com (c-71-195-205-98.hsd1.ut.comcast.net. [71.195.205.98]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0c037520sm78361715ad.194.2024.05.13.05.41.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 May 2024 05:41:33 -0700 (PDT) Date: Mon, 13 May 2024 13:41:32 +0100 From: Breno Leitao To: Miklos Szeredi Cc: paulmck@kernel.org, "open list:FUSE: FILESYSTEM IN USERSPACE" , open list Subject: Re: [PATCH] fuse: annotate potential data-race in num_background Message-ID: References: <20240509125716.1268016-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=us-ascii Content-Disposition: inline In-Reply-To: Hello Miklos, On Fri, May 10, 2024 at 11:21:19AM +0200, Miklos Szeredi wrote: > On Thu, 9 May 2024 at 14:57, Breno Leitao wrote: > > > Annotated the reader with READ_ONCE() and the writer with WRITE_ONCE() > > to avoid such complaint from KCSAN. > > I'm not sure the write side part is really needed, since the lock is > properly protecting against concurrent readers/writers within the > locked region. I understand that num_background is read from an unlocked region (fuse_readahead()). > Does KCSAN still complain if you just add the READ_ONCE() to fuse_readahead()? I haven't checked, but, looking at the documentation it says that both part needs to be marked. Here is an example very similar to ours here, from tools/memory-model/Documentation/access-marking.txt Lock-Protected Writes With Lockless Reads ----------------------------------------- For another example, suppose a shared variable "foo" is updated only while holding a spinlock, but is read locklessly. The code might look as follows: int foo; DEFINE_SPINLOCK(foo_lock); void update_foo(int newval) { spin_lock(&foo_lock); WRITE_ONCE(foo, newval); ASSERT_EXCLUSIVE_WRITER(foo); do_something(newval); spin_unlock(&foo_wlock); } int read_foo(void) { do_something_else(); return READ_ONCE(foo); } Because foo is read locklessly, all accesses are marked. From my understanding, we need a WRITE_ONCE() inside the lock, because the bg_lock lock in fuse_request_end() is invisible for fuse_readahead(), and fuse_readahead() might read num_backgroud that was writen non-atomically/corrupted (if there is no WRITE_ONCE()). That said, if the reader (fuse_readahead()) can handle possible corrupted data, we can mark is with data_race() annotation. Then I understand we don't need to mark the write with WRITE_ONCE(). Here is what access-marking.txt says about this case: Here are some situations where data_race() should be used instead of READ_ONCE() and WRITE_ONCE(): 1. Data-racy loads from shared variables whose values are used only for diagnostic purposes. 2. Data-racy reads whose values are checked against marked reload. 3. Reads whose values feed into error-tolerant heuristics. 4. Writes setting values that feed into error-tolerant heuristics. Anyway, I am more than happy to test with only a READ_ONLY() in the reader side, if that the approach you prefer. Thanks!