Received: by 2002:ab2:6991:0:b0:1f7:f6c3:9cb1 with SMTP id v17csp1134259lqo; Thu, 9 May 2024 05:57:36 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWePs0aBYgsNzQ+Y75l+zdrT4a7oarNZZX1DuwD4ABUpwVM123cN1mggYbq22QqysagMPEleXq4pyBHGW/MflkRNQRTQPthQdiO/4Tg7g== X-Google-Smtp-Source: AGHT+IGiGmZwuhdFf6Qqa0poo9ykNF/QmDjZlsod8mcqXxtVOcOI3HWvNDdQzcErA8QEoAwISvnJ X-Received: by 2002:a17:906:b156:b0:a59:ac10:9be5 with SMTP id a640c23a62f3a-a5a1177fe41mr200249366b.27.1715259456298; Thu, 09 May 2024 05:57:36 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715259456; cv=pass; d=google.com; s=arc-20160816; b=RjEyEOKoaQXqsSnKgwWFJm2bBr6OA42is00P0b6KgFiHPNloEdApoKk2VmA6iCYS0p fuU3TmBUfjFWZNgYwuPM6lnzg9OQiNoswff/YiFsOPTyaiNElaBGzefceBTXi85mlUO3 K1VeraQ5fVDqWWx+daJkehQqJRR0LMskJQF/k7R+iw4wiLoeXAVQD177qb4o8MllrQLC tWLnVwh+zfNGUAqeHGJPZC4uhARcLWdkd9dS1czGH6xdacDovVCodFCS4eD8AeJMNFBV 3fco49DEgtMQn27AjyiP47IbMA7txDE8Soz0TaxRqL2pIvOC5j+m19qGY+6XVIMj4LTC FV1A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from; bh=8tj5n/47UwDomI75HyIwdPYAf/haThSqq9uY0sQZVmo=; fh=UoxdPh/vAJMHXCTpyTPk5wnVshtQmf7VMeb3hRveYe8=; b=A+GfR01qbHYmYGpXsUUonexBfDevf/NVTbUq58YYhp7ELtCEhosuSS8yRsBxWcTCXp Jl8vP47lzP/9qcL9hCTA+Ej4tEq3GgscJArtH7L/vBkhH3PrclUoBSqcP2qBdXT0+/IV xgVKfU+dVT1NAkRoZYFX7Q26U6bW5GGDKTvSb6tqsA51xQhopQtCLy++lvT9vHHCcwYo z9ukiNYvsr6Vh609cTTbyP6x3pSSfiMkinImIdy4+ha8GO6sd8+LbautzQ/JjExPhPUV ewNW4ltRPdhJ3KCFpEAjDcm+LGH/ZqGcdA8RI1EHVRut0C44wYB4R4WUVHFfxkkG3seh d0vQ==; 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-174523-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-174523-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a5a17946841si84650166b.155.2024.05.09.05.57.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 05:57:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-174523-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-174523-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-174523-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 054841F23537 for ; Thu, 9 May 2024 12:57:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 06EB514EC4A; Thu, 9 May 2024 12:57:27 +0000 (UTC) Received: from mail-ed1-f41.google.com (mail-ed1-f41.google.com [209.85.208.41]) (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 D793D147C72; Thu, 9 May 2024 12:57:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715259446; cv=none; b=cUerYtyR6K9d22ttYGdiYDMmlSG4J/IrW531XpWdsl7ZRUP4zSH+IBtqGcYYvQM3GZ74H5SsGlA900HoQB5pBbdlC6LZWGFugIfqrBRjq5L1MqArxvtZ77LHcHkfbBA+WRBWw7r/7iQJ7QXYoDLbUcui2D35VEqljhgqE3s81mA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715259446; c=relaxed/simple; bh=mYMjMEAUKh44Zl6t2hYm68iMO3BZjSBLKzOpqis9l8s=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=Gl9VQBe/ciIpzl5YesKNmN7a9TR++OcF/WI6D777896dPTs7HWICihz6CbojSY3XMakrxD+67ma0LsnZ07Pd9lN9BRyYMH4LcTIZE7iRpo7dPwrOfTKo9H+fqy6Fpd9CtUfIWEhVWN5c/wIc5UTBNHT5LuTO0PqBswUN8inm/LE= 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.208.41 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-ed1-f41.google.com with SMTP id 4fb4d7f45d1cf-572c65cea55so1538116a12.0; Thu, 09 May 2024 05:57:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715259443; x=1715864243; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=8tj5n/47UwDomI75HyIwdPYAf/haThSqq9uY0sQZVmo=; b=WL/msjGFt7tIsnOiVfgbh8fmawJWXJQBVDj/u9EoN0pZpkyj8fJie3ruO9SanMpG6s wptmjNptEdqx8ARVQn6CSCIBf7g5/eedOypirIGVxz/DLNbLGJDPVq9GTyncXtuFJIoS NgWRbO82HaEQBW1JaUMgQZuVYguLF4tVrFMSkit/plHDFgx2WvOFpPywgMeRJvNO+gvd 1de0EI+JE1pWsrrbOuT3riWCk0hXXQcNEg2AXO62cBJ/wRl86QlLDHonOhkX4OA5Ezo5 ZnRQFJyAmwWYYWQQJ0lQ9dM4u3w/9PPw0W6NhE3iGHM603JnXhDWWnmCEoaY3x+otosh SNfw== X-Forwarded-Encrypted: i=1; AJvYcCVXEu6fUHTNTgdpBZNv83XN5X+WZ4ydItdtlnCBQ1ExLaWhaFO6my2WCaZOH0Nb5PP73kX/mL5DtvcOrbfbxUGBpWTVXa57jWqax4XpEkHpqwI+hmCQGBUUHKcw3e8R7EVUGjgazSC1iQk4qg== X-Gm-Message-State: AOJu0Ywj4mFmjzcHnWeMoK0AL4/yBVCR2B0I34xZC+78RKCzQYpjjAc/ 0AEQmfkULG833qAWYxdcXvo9WbTBZeRKh3jP2imALi24UzD1YwN+ X-Received: by 2002:a05:6402:1487:b0:572:99fa:1095 with SMTP id 4fb4d7f45d1cf-57332949554mr2155993a12.18.1715259443034; Thu, 09 May 2024 05:57:23 -0700 (PDT) Received: from localhost (fwdproxy-lla-009.fbsv.net. [2a03:2880:30ff:9::face:b00c]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5733becfb83sm672852a12.46.2024.05.09.05.57.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 05:57:22 -0700 (PDT) From: Breno Leitao To: Miklos Szeredi Cc: paulmck@kernel.org, linux-fsdevel@vger.kernel.org (open list:FUSE: FILESYSTEM IN USERSPACE), linux-kernel@vger.kernel.org (open list) Subject: [PATCH] fuse: annotate potential data-race in num_background Date: Thu, 9 May 2024 05:57:15 -0700 Message-ID: <20240509125716.1268016-1-leitao@debian.org> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit A data race occurs when two concurrent data paths potentially access fuse_conn->num_background simultaneously. Specifically, fuse_request_end() accesses and modifies ->num_background while holding the bg_lock, whereas fuse_readahead() reads ->num_background without acquiring any lock beforehand. This potential data race is flagged by KCSAN: BUG: KCSAN: data-race in fuse_readahead [fuse] / fuse_request_end [fuse] read-write to 0xffff8883a6666598 of 4 bytes by task 113809 on cpu 39: fuse_request_end (fs/fuse/dev.c:318) fuse fuse_dev_do_write (fs/fuse/dev.c:?) fuse fuse_dev_write (fs/fuse/dev.c:?) fuse ... read to 0xffff8883a6666598 of 4 bytes by task 113787 on cpu 8: fuse_readahead (fs/fuse/file.c:1005) fuse read_pages (mm/readahead.c:166) page_cache_ra_unbounded (mm/readahead.c:?) ... value changed: 0x00000001 -> 0x00000000 Annotated the reader with READ_ONCE() and the writer with WRITE_ONCE() to avoid such complaint from KCSAN. Suggested-by: Miklos Szeredi Signed-off-by: Breno Leitao --- fs/fuse/dev.c | 6 ++++-- fs/fuse/file.c | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/fs/fuse/dev.c b/fs/fuse/dev.c index 3ec8bb5e68ff..8e63dba49eff 100644 --- a/fs/fuse/dev.c +++ b/fs/fuse/dev.c @@ -282,6 +282,7 @@ void fuse_request_end(struct fuse_req *req) struct fuse_mount *fm = req->fm; struct fuse_conn *fc = fm->fc; struct fuse_iqueue *fiq = &fc->iq; + unsigned int num_background; if (test_and_set_bit(FR_FINISHED, &req->flags)) goto put_request; @@ -301,7 +302,8 @@ void fuse_request_end(struct fuse_req *req) if (test_bit(FR_BACKGROUND, &req->flags)) { spin_lock(&fc->bg_lock); clear_bit(FR_BACKGROUND, &req->flags); - if (fc->num_background == fc->max_background) { + num_background = READ_ONCE(fc->num_background); + if (num_background == fc->max_background) { fc->blocked = 0; wake_up(&fc->blocked_waitq); } else if (!fc->blocked) { @@ -315,7 +317,7 @@ void fuse_request_end(struct fuse_req *req) wake_up(&fc->blocked_waitq); } - fc->num_background--; + WRITE_ONCE(fc->num_background, num_background - 1); fc->active_background--; flush_bg_queue(fc); spin_unlock(&fc->bg_lock); diff --git a/fs/fuse/file.c b/fs/fuse/file.c index b57ce4157640..07331889bbf3 100644 --- a/fs/fuse/file.c +++ b/fs/fuse/file.c @@ -1002,7 +1002,7 @@ static void fuse_readahead(struct readahead_control *rac) struct fuse_io_args *ia; struct fuse_args_pages *ap; - if (fc->num_background >= fc->congestion_threshold && + if (READ_ONCE(fc->num_background) >= fc->congestion_threshold && rac->ra->async_size >= readahead_count(rac)) /* * Congested and only async pages left, so skip the -- 2.43.0