Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp2647903ybv; Fri, 21 Feb 2020 20:31:39 -0800 (PST) X-Google-Smtp-Source: APXvYqynA1fv3i7mL00gO5kH0rO0rV7c0Uh70QVfGKyPZxeYi2iOZ5uKUmDmGt6uB79hiC9Gvo34 X-Received: by 2002:a05:6808:209:: with SMTP id l9mr4921231oie.112.1582345899760; Fri, 21 Feb 2020 20:31:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582345899; cv=none; d=google.com; s=arc-20160816; b=cWuyjJJQnHZbnHo9yEyH0AnzT5XwFX/IbhiwUwt1VZhV6AzjDKEUpOM7IDOBExApEv XgNmRSrjhzZnQy4LGH0QyErLCj5CO3D0rg6oqR9W+Wl0QJKKeW3DtxTmtHbXG6FuYZ3g Lvphy+vEc4+jZdBb3HDd9443MtIR5+WsNgSmFKnV4XBV0KJ3XO5N36+No170NZGT/qob kCWm51Ko2//h8F5Qmv+rVXhfJsowXQyPt7r7/ChXobGYsCEgYDOFQZkJjoyCRbfEnqsk OvsymJ7I/34LwLYqBLSuqw96cd+hVI2PIoGWZGeXsk/0WogXJKGLqtMAgqX4mVajgU69 +8cg== 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=CX8IlyO2NfaoYedky7RgY9iFI5Qm9KXskMaolJPwzcE=; b=ltDzBBxqVweZtVS8JVmsjKgkW6BFnyxP922pciXNxgzpUF3W+Q8DPo9UaZr8a4AKUE wmT8yIPTEJLw+/GXaRtc0U0RJeM8Uh2uoq1seD8UJD/mknjcCaH6JFC/oM0IDrC+Mpm9 ldAJdFm/HHpHjK4ADzur+aypXuWB3GTxcKRcKCyBP5tAQtJIKDT3y49quwy86MDXCnrL UIAgt8ZRkqfSUhZUUFt5tvsCnsvFhw1xp90kHjhOGqQ1Ti+7xoTeITRDwbfXaYcUTKtF 1A046nxoZ8Tlg1lMVatezQ+jhovH5dJK3oMsJPWEKYwKyQUSveAunyxWpx58394IbaZi LeFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lca.pw header.s=google header.b=HURxGwm6; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-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 w7si2567041otq.250.2020.02.21.20.31.27; Fri, 21 Feb 2020 20:31:39 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-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=HURxGwm6; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728073AbgBVEbT (ORCPT + 99 others); Fri, 21 Feb 2020 23:31:19 -0500 Received: from mail-qk1-f194.google.com ([209.85.222.194]:37368 "EHLO mail-qk1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726472AbgBVEbS (ORCPT ); Fri, 21 Feb 2020 23:31:18 -0500 Received: by mail-qk1-f194.google.com with SMTP id c188so3932677qkg.4 for ; Fri, 21 Feb 2020 20:31:17 -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=CX8IlyO2NfaoYedky7RgY9iFI5Qm9KXskMaolJPwzcE=; b=HURxGwm6wXjTFFfHTTe3ARqif8yoUISPdDmgETee13HaBdibZts47iyQcECyw2+uX2 Jq+CrCoiw3KLZfhUWHsC39rSkR+puexd4NSHZFiMWpyR56vYkGVyR3uDKDPLA3TaDfqz NcFARAMh31DnnivAwrLoqs3hoWW9UWRV/4DYyywZUhg9JvvwDCj7BSYwQxrV0Xa31TGZ 4LNUwq+uLEYNsNg2ENSJL4TMUJrI3JFyQiIWZLJwiH6auWnTZyJvfYVzonEOnAIFqSju nfbjkM6ZYqKDmX17//S2is7PQzogFZeBtY4Re2HAcIpaleWEREJXhMxbM+FUwwVMqBFO c1BA== 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=CX8IlyO2NfaoYedky7RgY9iFI5Qm9KXskMaolJPwzcE=; b=IDnjRr/mdlbOKVA8oeHGRz3JU0vgryNo1XcEgWEG5N9XzS6ivWBqjWS2bXlOexRw0A SBcFuIdgqPqZq6KU4UPdagSJwrkDtDSXbLLsB/b2KsfeK2ha8qN7VT8ZR3Knbbuv7J7I JFRkMJQAFiBeBJAGCq974jefOZu6h0prWILlfgTUCJkREWFJZp11VGO2vDLaHQvC7NBX mIr+SYh3C2WEvHydaK2D3HKyqQvI10f9IsxgqtGhggVkhJjoc771yZ6nPTQeocsnneWr ac8tFysX6lyOIlJM7NqAayTExZh9ylRu3ufsYXB000NsriTPa5jQRoSNITEsBmkIGGoj 213g== X-Gm-Message-State: APjAAAVFYEaES5YJLgF3IsSwnkZTN/dGKdQVPjUv6qOnvW9hFFFummuj rcFHKjpLT9eyPUjFA0CZksEKDQ== X-Received: by 2002:a05:620a:150a:: with SMTP id i10mr13947361qkk.407.1582345877273; Fri, 21 Feb 2020 20:31:17 -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 184sm2578786qki.92.2020.02.21.20.31.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 21 Feb 2020 20:31:16 -0800 (PST) From: Qian Cai To: tytso@mit.edu, jack@suse.com Cc: linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org, Qian Cai Subject: [PATCH] fs/jbd2: fix data races at struct journal_head Date: Fri, 21 Feb 2020 23:31:11 -0500 Message-Id: <20200222043111.2227-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-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org journal_head::b_transaction and journal_head::b_next_transaction could be accessed concurrently as noticed by KCSAN, LTP: starting fsync04 /dev/zero: Can't open blockdev EXT4-fs (loop0): mounting ext3 file system using the ext4 subsystem EXT4-fs (loop0): mounted filesystem with ordered data mode. Opts: (null) ================================================================== BUG: KCSAN: data-race in __jbd2_journal_refile_buffer [jbd2] / jbd2_write_access_granted [jbd2] write to 0xffff99f9b1bd0e30 of 8 bytes by task 25721 on cpu 70: __jbd2_journal_refile_buffer+0xdd/0x210 [jbd2] __jbd2_journal_refile_buffer at fs/jbd2/transaction.c:2569 jbd2_journal_commit_transaction+0x2d15/0x3f20 [jbd2] (inlined by) jbd2_journal_commit_transaction at fs/jbd2/commit.c:1034 kjournald2+0x13b/0x450 [jbd2] kthread+0x1cd/0x1f0 ret_from_fork+0x27/0x50 read to 0xffff99f9b1bd0e30 of 8 bytes by task 25724 on cpu 68: jbd2_write_access_granted+0x1b2/0x250 [jbd2] jbd2_write_access_granted at fs/jbd2/transaction.c:1155 jbd2_journal_get_write_access+0x2c/0x60 [jbd2] __ext4_journal_get_write_access+0x50/0x90 [ext4] ext4_mb_mark_diskspace_used+0x158/0x620 [ext4] ext4_mb_new_blocks+0x54f/0xca0 [ext4] ext4_ind_map_blocks+0xc79/0x1b40 [ext4] ext4_map_blocks+0x3b4/0x950 [ext4] _ext4_get_block+0xfc/0x270 [ext4] ext4_get_block+0x3b/0x50 [ext4] __block_write_begin_int+0x22e/0xae0 __block_write_begin+0x39/0x50 ext4_write_begin+0x388/0xb50 [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 5 locks held by fsync04/25724: #0: ffff99f9911093f8 (sb_writers#13){.+.+}, at: vfs_write+0x21c/0x260 #1: ffff99f9db4c0348 (&sb->s_type->i_mutex_key#15){+.+.}, at: ext4_buffered_write_iter+0x65/0x210 [ext4] #2: ffff99f5e7dfcf58 (jbd2_handle){++++}, at: start_this_handle+0x1c1/0x9d0 [jbd2] #3: ffff99f9db4c0168 (&ei->i_data_sem){++++}, at: ext4_map_blocks+0x176/0x950 [ext4] #4: ffffffff99086b40 (rcu_read_lock){....}, at: jbd2_write_access_granted+0x4e/0x250 [jbd2] irq event stamp: 1407125 hardirqs last enabled at (1407125): [] __find_get_block+0x107/0x790 hardirqs last disabled at (1407124): [] __find_get_block+0x49/0x790 softirqs last enabled at (1405528): [] __do_softirq+0x34c/0x57c softirqs last disabled at (1405521): [] irq_exit+0xa2/0xc0 Reported by Kernel Concurrency Sanitizer on: CPU: 68 PID: 25724 Comm: fsync04 Tainted: G L 5.6.0-rc2-next-20200221+ #7 Hardware name: HPE ProLiant DL385 Gen10/ProLiant DL385 Gen10, BIOS A40 07/10/2019 The plain reads are outside of jh->b_state_lock critical section which result in data races. Fix them by adding pairs of READ|WRITE_ONCE(). Signed-off-by: Qian Cai --- fs/jbd2/transaction.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fs/jbd2/transaction.c b/fs/jbd2/transaction.c index 2dd848a743ed..c5f7f6d0f33b 100644 --- a/fs/jbd2/transaction.c +++ b/fs/jbd2/transaction.c @@ -1152,8 +1152,8 @@ static bool jbd2_write_access_granted(handle_t *handle, struct buffer_head *bh, /* For undo access buffer must have data copied */ if (undo && !jh->b_committed_data) goto out; - if (jh->b_transaction != handle->h_transaction && - jh->b_next_transaction != handle->h_transaction) + if (READ_ONCE(jh->b_transaction) != handle->h_transaction && + READ_ONCE(jh->b_next_transaction) != handle->h_transaction) goto out; /* * There are two reasons for the barrier here: @@ -2565,8 +2565,8 @@ bool __jbd2_journal_refile_buffer(struct journal_head *jh) * our jh reference and thus __jbd2_journal_file_buffer() must not * take a new one. */ - jh->b_transaction = jh->b_next_transaction; - jh->b_next_transaction = NULL; + WRITE_ONCE(jh->b_transaction, jh->b_next_transaction); + WRITE_ONCE(jh->b_next_transaction, NULL); if (buffer_freed(bh)) jlist = BJ_Forget; else if (jh->b_modified) -- 2.21.0 (Apple Git-122.2)