Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp1186021ybf; Thu, 27 Feb 2020 06:30:19 -0800 (PST) X-Google-Smtp-Source: APXvYqxsqO9pVeCqAePVHRXWCbpLdwMAu4Xu6VxZMDxauUB7jpJSCgUFJylxy998atNwuFvieYiU X-Received: by 2002:a54:4106:: with SMTP id l6mr3476078oic.76.1582813819294; Thu, 27 Feb 2020 06:30:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582813819; cv=none; d=google.com; s=arc-20160816; b=k6J7wRu5onRNQ5q+iDRVCRmLsRBnWSRnlFNMUxGGKZ3MZMURDHo7USSW4ypoVk9SQs WiC3RcFzJrRhRFLjLO+nIqr1kUHtkvuzwkxl8woxneX7e9A19uK4RKqHIbD5kLdrLFYa S/ELAN/dWDJt5MNXMwR5HakHTQedRd91dOV4DbMXb6Pah/8tEE7TQfzk3Wtk+GlGSdbL WUpfnGOi0qmrumfvgsoJ15PNfv0eVbFEb4mNYp72aexMX9D/V9xHqXpkPt58mBBk2sFQ zf8Z56FDyTpDgZamUb67q9O/9SOKVHIxb1trwco+y5hN/q5PtZJTfTGDr5uICkC7p9uC lkjA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=3CGX3vUgKmjouSVqM7NunoYnZKuF1APo9OxpxxjhL4w=; b=gp2BmSY/8GWWOVbGUpOHx1eK1RadM4+O34kmoznvX67MTheRrc+BaOwwiwfnT/7P4n rpeilf6tiLRxEPIRgYczMxCEUuWQGAj3sxsO4DvJFzgtqnDpU3n70T8cylxD6d9uPwt3 aWuO18FC029/svMVTW7Y6rUlc7Gp8x9MpLh4hCA1wxtUyZVkw7WQW6gjBRnOxJynvcbi sL9u4JBYi2MMsM+3REDlC+lko4CE52eLOnOZvavUzy+cQ0CxMg0azOSSY1N1xd/Mwbtc QWQOe+CyyA3OLNo3tUFBp3KBW/tBYZPm4ZwmX3a+UsBOWsgKkWVwSVqwyIU9Vm6/yq12 TMpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=BbEhvUHC; 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 y24si1470631oih.24.2020.02.27.06.30.06; Thu, 27 Feb 2020 06:30:19 -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=@kernel.org header.s=default header.b=BbEhvUHC; 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 S2388207AbgB0OaE (ORCPT + 99 others); Thu, 27 Feb 2020 09:30:04 -0500 Received: from mail.kernel.org ([198.145.29.99]:41954 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387757AbgB0OFU (ORCPT ); Thu, 27 Feb 2020 09:05:20 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 1C93424691; Thu, 27 Feb 2020 14:05:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1582812319; bh=9Rm7FboJR5u6DSKxWNcSjEtbnTqTU1SriE20FSZRexk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BbEhvUHC0oKkCGK/uhkYMSgnBzgHtimZXlFpOiRBIf/n7Inff5fWhHYCPWsBRFLhH ozU/IPy/GTOrny7R5XGecxE2Ocj76yGi6j4BbW/6Ho4/PUOruOv7VjpFBGa1SMPvNz IUIk4NAqOna2vidzOIOgGvpazHYmdmzhW8odSHSQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+2202a584a00fffd19fbf@syzkaller.appspotmail.com, Eric Biggers , Theodore Tso , Jan Kara , stable@kernel.org Subject: [PATCH 4.19 70/97] ext4: fix race between writepages and enabling EXT4_EXTENTS_FL Date: Thu, 27 Feb 2020 14:37:18 +0100 Message-Id: <20200227132225.913901998@linuxfoundation.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200227132214.553656188@linuxfoundation.org> References: <20200227132214.553656188@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eric Biggers commit cb85f4d23f794e24127f3e562cb3b54b0803f456 upstream. If EXT4_EXTENTS_FL is set on an inode while ext4_writepages() is running on it, the following warning in ext4_add_complete_io() can be hit: WARNING: CPU: 1 PID: 0 at fs/ext4/page-io.c:234 ext4_put_io_end_defer+0xf0/0x120 Here's a minimal reproducer (not 100% reliable) (root isn't required): while true; do sync done & while true; do rm -f file touch file chattr -e file echo X >> file chattr +e file done The problem is that in ext4_writepages(), ext4_should_dioread_nolock() (which only returns true on extent-based files) is checked once to set the number of reserved journal credits, and also again later to select the flags for ext4_map_blocks() and copy the reserved journal handle to ext4_io_end::handle. But if EXT4_EXTENTS_FL is being concurrently set, the first check can see dioread_nolock disabled while the later one can see it enabled, causing the reserved handle to unexpectedly be NULL. Since changing EXT4_EXTENTS_FL is uncommon, and there may be other races related to doing so as well, fix this by synchronizing changing EXT4_EXTENTS_FL with ext4_writepages() via the existing s_writepages_rwsem (previously called s_journal_flag_rwsem). This was originally reported by syzbot without a reproducer at https://syzkaller.appspot.com/bug?extid=2202a584a00fffd19fbf, but now that dioread_nolock is the default I also started seeing this when running syzkaller locally. Link: https://lore.kernel.org/r/20200219183047.47417-3-ebiggers@kernel.org Reported-by: syzbot+2202a584a00fffd19fbf@syzkaller.appspotmail.com Fixes: 6b523df4fb5a ("ext4: use transaction reservation for extent conversion in ext4_end_io") Signed-off-by: Eric Biggers Signed-off-by: Theodore Ts'o Reviewed-by: Jan Kara Cc: stable@kernel.org Signed-off-by: Greg Kroah-Hartman --- fs/ext4/ext4.h | 5 ++++- fs/ext4/migrate.c | 27 +++++++++++++++++++-------- 2 files changed, 23 insertions(+), 9 deletions(-) --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -1520,7 +1520,10 @@ struct ext4_sb_info { struct ratelimit_state s_warning_ratelimit_state; struct ratelimit_state s_msg_ratelimit_state; - /* Barrier between changing inodes' journal flags and writepages ops. */ + /* + * Barrier between writepages ops and changing any inode's JOURNAL_DATA + * or EXTENTS flag. + */ struct percpu_rw_semaphore s_writepages_rwsem; struct dax_device *s_daxdev; }; --- a/fs/ext4/migrate.c +++ b/fs/ext4/migrate.c @@ -427,6 +427,7 @@ static int free_ext_block(handle_t *hand int ext4_ext_migrate(struct inode *inode) { + struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); handle_t *handle; int retval = 0, i; __le32 *i_data; @@ -451,6 +452,8 @@ int ext4_ext_migrate(struct inode *inode */ return retval; + percpu_down_write(&sbi->s_writepages_rwsem); + /* * Worst case we can touch the allocation bitmaps, a bgd * block, and a block to link in the orphan list. We do need @@ -461,7 +464,7 @@ int ext4_ext_migrate(struct inode *inode if (IS_ERR(handle)) { retval = PTR_ERR(handle); - return retval; + goto out_unlock; } goal = (((inode->i_ino - 1) / EXT4_INODES_PER_GROUP(inode->i_sb)) * EXT4_INODES_PER_GROUP(inode->i_sb)) + 1; @@ -472,7 +475,7 @@ int ext4_ext_migrate(struct inode *inode if (IS_ERR(tmp_inode)) { retval = PTR_ERR(tmp_inode); ext4_journal_stop(handle); - return retval; + goto out_unlock; } i_size_write(tmp_inode, i_size_read(inode)); /* @@ -514,7 +517,7 @@ int ext4_ext_migrate(struct inode *inode */ ext4_orphan_del(NULL, tmp_inode); retval = PTR_ERR(handle); - goto out; + goto out_tmp_inode; } ei = EXT4_I(inode); @@ -595,10 +598,11 @@ err_out: /* Reset the extent details */ ext4_ext_tree_init(handle, tmp_inode); ext4_journal_stop(handle); -out: +out_tmp_inode: unlock_new_inode(tmp_inode); iput(tmp_inode); - +out_unlock: + percpu_up_write(&sbi->s_writepages_rwsem); return retval; } @@ -608,7 +612,8 @@ out: int ext4_ind_migrate(struct inode *inode) { struct ext4_extent_header *eh; - struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es; + struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); + struct ext4_super_block *es = sbi->s_es; struct ext4_inode_info *ei = EXT4_I(inode); struct ext4_extent *ex; unsigned int i, len; @@ -632,9 +637,13 @@ int ext4_ind_migrate(struct inode *inode if (test_opt(inode->i_sb, DELALLOC)) ext4_alloc_da_blocks(inode); + percpu_down_write(&sbi->s_writepages_rwsem); + handle = ext4_journal_start(inode, EXT4_HT_MIGRATE, 1); - if (IS_ERR(handle)) - return PTR_ERR(handle); + if (IS_ERR(handle)) { + ret = PTR_ERR(handle); + goto out_unlock; + } down_write(&EXT4_I(inode)->i_data_sem); ret = ext4_ext_check_inode(inode); @@ -669,5 +678,7 @@ int ext4_ind_migrate(struct inode *inode errout: ext4_journal_stop(handle); up_write(&EXT4_I(inode)->i_data_sem); +out_unlock: + percpu_up_write(&sbi->s_writepages_rwsem); return ret; }