Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp2649389ybv; Fri, 21 Feb 2020 20:33:21 -0800 (PST) X-Google-Smtp-Source: APXvYqxBqhgPbgTFPEWrs2+q86qRhe62TJ3jiyE5WwD44OyisH99CVSReZT7KPPTWWz2ukRaYGdm X-Received: by 2002:aca:fd4c:: with SMTP id b73mr4967587oii.33.1582346001434; Fri, 21 Feb 2020 20:33:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582346001; cv=none; d=google.com; s=arc-20160816; b=LwgxiGz59Jz4lgoxEMLuCu/weaJSk6z48y/okzoas0dj60NyURas469K0NNpW7G/HS ZYDeDfrjqpSR8ZHAtez1DJ3b9DMQZfwcb9epZC4zIiEUuQ9UN/qw+rKa3zDv43Eaf2XO fXUGb0MSFOae7iblfma9qBolu16RaokUvAV2f1fLqnhbx8EoLnzd8y6TwlFfZNBqeIjV koZXrV7ZUTXHjjPjk6Q/CYaBAhCmK+eraOUkCC2texGnzvMFuV8IXGq5a6PioXVe56eL rjXObVuR18ZrDOrm2bd941SacaHGSfHCGmI14bceAO4beWTajbZzVj24eKrQ1d8lDwai dV/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=OPdiCspFUoLNdKrsNyt7aSs4T7N6gYtV5I8j4acuNpk=; b=fnZqEHevUguIWnJX1vYJZJIV03BQyisc4Z4GfvAVbnsSnDsAQNr3FlvRJ0clqymrMy rJ2aK/XcYInifY7HOnkyHUATfuhA4AyeZUEAxFyhdYWAXEvcRocL5yTLWhaI1CX4d/II 8eTY6o/uJDgWPelF32yjgAxWKxTQigylR7U3i40TaFpklxzlCRwP5vBUR/2Lgb/gKXGm lJmtb4vSpDZbVSGhMALm/1cSZXk8Qeob0WI9qYS/7cthMMPEJZgzq7GC76lTsAItIl9+ E6nw5ZW6RpQ26U1l8JCZ25BM6NV2F5aHxvadMZwUTkERsfooxZN9WqU6ryJObOSC50Jc gnfg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lca.pw header.s=google header.b=kCtRbLdy; spf=pass (google.com: best guess record for domain of linux-ext4-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m7si2740636otk.73.2020.02.21.20.33.10; Fri, 21 Feb 2020 20:33:21 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-ext4-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@lca.pw header.s=google header.b=kCtRbLdy; spf=pass (google.com: best guess record for domain of linux-ext4-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728130AbgBVEdE (ORCPT + 99 others); Fri, 21 Feb 2020 23:33:04 -0500 Received: from mail-qk1-f196.google.com ([209.85.222.196]:37504 "EHLO mail-qk1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727025AbgBVEdE (ORCPT ); Fri, 21 Feb 2020 23:33:04 -0500 Received: by mail-qk1-f196.google.com with SMTP id c188so3934786qkg.4 for ; Fri, 21 Feb 2020 20:33:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lca.pw; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=OPdiCspFUoLNdKrsNyt7aSs4T7N6gYtV5I8j4acuNpk=; b=kCtRbLdy7+38MLaQOqGvMn+7ARfqNP34p7Adh+Nw0vxvzdxkFuJqwccbrDTlSw/rIW a6TiZ4wDah27pbw5okdb81kVQ8H9gn3q2+7squeRbWaDfx7wqIFMrussM/adOQ1558qu vM/KRBsMBBX3s70MJmNT/XcO30s4LnsodDhHEE+vzXPnrx3DLSvMq0zWyXTE6OYBg+kR SXOXG8CMZTPSTIxw0M78unzC4qPvAhF7QREFjpU4G/rxpJe9H07eq2hd8hcP44arR5jQ D0J7NjGQuWpJdvcRRwkQ3qhAbrDKU4vfMhCtYX1fIZJH1SyJFDz9WAf349Fob4Ao1KEd 2/Wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=OPdiCspFUoLNdKrsNyt7aSs4T7N6gYtV5I8j4acuNpk=; b=e+6wRC0Z1uQ3jr5zsnh4PX0aVaeOf9WFulX+P+zCopM0krWRN47tG0L3lHRaTAVC8K W4VunSOeBKkucIZCB2rX+cWx9PpYLsNy9glAxysr7PqSGY69RSTvAdLL9lBYn7POLr/s yG/R7pJHWjWdkadlelxgwCuwOhBblCWGn3qlNLfKXAj1TiUpecz4S7IKXg1aDmNYyaf0 21/gRWt6hsvUHWhNH44WT6VaVdLRxwBEc9Dd5UaV7rGb4djm4ABlYGybMVhVE3PRtvaZ sPqdRPQnwU3ejLlc9cEyObhrK2znZXFA7szuotbzU45EL+zOMq+3Nzjanz6qKQk370Nx 3x/Q== X-Gm-Message-State: APjAAAU9/RCQtpHRY2j3AgYFKpjFLD7JUSJ9aZMxChe1N7QJK0kZoj6b DGaUWV2WqX8tRk+OhWDb24XeHw== X-Received: by 2002:a05:620a:15cf:: with SMTP id o15mr37586094qkm.140.1582345982183; Fri, 21 Feb 2020 20:33:02 -0800 (PST) Received: from ovpn-120-117.rdu2.redhat.com (pool-71-184-117-43.bstnma.fios.verizon.net. [71.184.117.43]) by smtp.gmail.com with ESMTPSA id j11sm2592733qkl.97.2020.02.21.20.33.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 21 Feb 2020 20:33:01 -0800 (PST) From: Qian Cai To: tytso@mit.edu Cc: adilger.kernel@dilger.ca, elver@google.com, linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org, Qian Cai Subject: [PATCH] ext4: fix a data race at inode->i_blocks Date: Fri, 21 Feb 2020 23:32:58 -0500 Message-Id: <20200222043258.2279-1-cai@lca.pw> X-Mailer: git-send-email 2.21.0 (Apple Git-122.2) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org inode->i_blocks could be accessed concurrently as noticed by KCSAN, BUG: KCSAN: data-race in ext4_do_update_inode [ext4] / inode_add_bytes write to 0xffff9a00d4b982d0 of 8 bytes by task 22100 on cpu 118: inode_add_bytes+0x65/0xf0 __inode_add_bytes at fs/stat.c:689 (inlined by) inode_add_bytes at fs/stat.c:702 ext4_mb_new_blocks+0x418/0xca0 [ext4] ext4_ext_map_blocks+0x1a6b/0x27b0 [ext4] ext4_map_blocks+0x1a9/0x950 [ext4] _ext4_get_block+0xfc/0x270 [ext4] ext4_get_block_unwritten+0x33/0x50 [ext4] __block_write_begin_int+0x22e/0xae0 __block_write_begin+0x39/0x50 ext4_write_begin+0x388/0xb50 [ext4] ext4_da_write_begin+0x35f/0x8f0 [ext4] generic_perform_write+0x15d/0x290 ext4_buffered_write_iter+0x11f/0x210 [ext4] ext4_file_write_iter+0xce/0x9e0 [ext4] new_sync_write+0x29c/0x3b0 __vfs_write+0x92/0xa0 vfs_write+0x103/0x260 ksys_write+0x9d/0x130 __x64_sys_write+0x4c/0x60 do_syscall_64+0x91/0xb05 entry_SYSCALL_64_after_hwframe+0x49/0xbe read to 0xffff9a00d4b982d0 of 8 bytes by task 8 on cpu 65: ext4_do_update_inode+0x4a0/0xf60 [ext4] ext4_inode_blocks_set at fs/ext4/inode.c:4815 ext4_mark_iloc_dirty+0xaf/0x160 [ext4] ext4_mark_inode_dirty+0x129/0x3e0 [ext4] ext4_convert_unwritten_extents+0x253/0x2d0 [ext4] ext4_convert_unwritten_io_end_vec+0xc5/0x150 [ext4] ext4_end_io_rsv_work+0x22c/0x350 [ext4] process_one_work+0x54f/0xb90 worker_thread+0x80/0x5f0 kthread+0x1cd/0x1f0 ret_from_fork+0x27/0x50 4 locks held by kworker/u256:0/8: #0: ffff9a025abc4328 ((wq_completion)ext4-rsv-conversion){+.+.}, at: process_one_work+0x443/0xb90 #1: ffffab5a862dbe20 ((work_completion)(&ei->i_rsv_conversion_work)){+.+.}, at: process_one_work+0x443/0xb90 #2: ffff9a025a9d0f58 (jbd2_handle){++++}, at: start_this_handle+0x1c1/0x9d0 [jbd2] #3: ffff9a00d4b985d8 (&(&ei->i_raw_lock)->rlock){+.+.}, at: ext4_do_update_inode+0xaa/0xf60 [ext4] irq event stamp: 3009267 hardirqs last enabled at (3009267): [] __find_get_block+0x107/0x790 hardirqs last disabled at (3009266): [] __find_get_block+0x49/0x790 softirqs last enabled at (3009230): [] __do_softirq+0x34c/0x57c softirqs last disabled at (3009223): [] irq_exit+0xa2/0xc0 Reported by Kernel Concurrency Sanitizer on: CPU: 65 PID: 8 Comm: kworker/u256:0 Tainted: G L 5.6.0-rc2-next-20200221+ #7 Hardware name: HPE ProLiant DL385 Gen10/ProLiant DL385 Gen10, BIOS A40 07/10/2019 Workqueue: ext4-rsv-conversion ext4_end_io_rsv_work [ext4] The plain read is outside of inode->i_lock critical section which results in a data race. Fix it by adding READ_ONCE() there. Signed-off-by: Qian Cai --- fs/ext4/inode.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index e60aca791d3f..98cadd111942 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -4812,7 +4812,7 @@ static int ext4_inode_blocks_set(handle_t *handle, struct ext4_inode_info *ei) { struct inode *inode = &(ei->vfs_inode); - u64 i_blocks = inode->i_blocks; + u64 i_blocks = READ_ONCE(inode->i_blocks); struct super_block *sb = inode->i_sb; if (i_blocks <= ~0U) { -- 2.21.0 (Apple Git-122.2)