Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp4372887imw; Tue, 19 Jul 2022 05:33:35 -0700 (PDT) X-Google-Smtp-Source: AGRyM1shUGrLfB4ke/A0+M/EHiQEw1V1D+8G6Tq7INY8MfySmo+P2hqAqZ12WNLnWr/Xbqzm2Uf0 X-Received: by 2002:a05:6402:2753:b0:43a:d6f2:9839 with SMTP id z19-20020a056402275300b0043ad6f29839mr42368825edd.73.1658234015451; Tue, 19 Jul 2022 05:33:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658234015; cv=none; d=google.com; s=arc-20160816; b=Q17UYGbNOYja560U98pEYXlW3R2Qz+hWThT1+fX8jRdwQLJ/uBXqDtYvYDzDqpDfQe ZdlJwcEZE84AonvdnDoH0T8AZi674IZwy29RmELikucTS3kUdE94aG/OMVFxKfyRpNG4 u/iWMaOoO5NvRY9aoLbo154m1raJSt2YDVleJRd78X0Ik2lxKZzFoaliF6Z/S/i3DBqc C8WfeldUf6PKIuIhr2lj9bK4dvFpr8OEtlR6jJ+Mo0I8OKlhaHF8eRIc6KV+13Dfbcpt x90OJmnBx0CKeJTu0Mj3K7AQ1aCacDdWmLtK7QBfssqy8qWVgcO4iRX9LD9QKoZ82kWR toiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=i3FMlKy2ZAqhOlYjOe8VrDevheXesJYjaytLT3VjUnE=; b=c2vBZ4lUeSjnHzc8IDOSmAIWs0SNsDB1sagBkfDC50D9HiOFtAhNOc0IHJ8mXxsUiv rKhGfhC9Hzu8PipgDpMfT02vTOEk7dMCEnNBRTyH9IsDaVMyGEA7fPRVF0LZYJC2vsZH mp9B5Ql1VxQCotE3ZqmYsJ9MsXwN5t5FG1j4KnmlI9FNBqeCXVwyLR84tM8FjZV5frn8 juWDY/TcxHoJQt8zF1uI/mdsxfAZEC6dImuv23jFVVklB45rIsbm0gDhtOH3oVGETpcm +ahoIqxFVSvDBR9uQ1ciLYC9bDTWy3IpIb4cYCCVh3ZB/BqK4GRbl+d+FNMIEz2qkj4B PPJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=QWwc899m; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h10-20020aa7cdca000000b0043a96be1dc0si16951507edw.111.2022.07.19.05.33.11; Tue, 19 Jul 2022 05:33:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=QWwc899m; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239587AbiGSMXp (ORCPT + 99 others); Tue, 19 Jul 2022 08:23:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239584AbiGSMX2 (ORCPT ); Tue, 19 Jul 2022 08:23:28 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B4D095FADC; Tue, 19 Jul 2022 05:08:36 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 5A53E61632; Tue, 19 Jul 2022 12:07:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 33D67C341C6; Tue, 19 Jul 2022 12:07:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1658232449; bh=71bLrnFNtXJkYlYN/aEIRhT0yTNHFTHvQbAHuGCm6F0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QWwc899mLhK4aaOiododgdlIrsLGvr4KNalM0HJUdUvOQFjWMv41OyxqSw1vpGuqO fUwOHvFlqd9StBXsSmxNwN926+9seVC6h9/UaVDWCM4MkNQl/mIz1gv8Qfb6nktjq4 L0iZdKvzbdk4UD/XW63qeXTUr/YF9hKhhjmE4uEc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Hulk Robot , Baokun Li , Jan Kara , Theodore Tso , Tadeusz Struk Subject: [PATCH 5.10 024/112] ext4: fix race condition between ext4_write and ext4_convert_inline_data Date: Tue, 19 Jul 2022 13:53:17 +0200 Message-Id: <20220719114628.353501114@linuxfoundation.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220719114626.156073229@linuxfoundation.org> References: <20220719114626.156073229@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Baokun Li commit f87c7a4b084afc13190cbb263538e444cb2b392a upstream. Hulk Robot reported a BUG_ON: ================================================================== EXT4-fs error (device loop3): ext4_mb_generate_buddy:805: group 0, block bitmap and bg descriptor inconsistent: 25 vs 31513 free clusters kernel BUG at fs/ext4/ext4_jbd2.c:53! invalid opcode: 0000 [#1] SMP KASAN PTI CPU: 0 PID: 25371 Comm: syz-executor.3 Not tainted 5.10.0+ #1 RIP: 0010:ext4_put_nojournal fs/ext4/ext4_jbd2.c:53 [inline] RIP: 0010:__ext4_journal_stop+0x10e/0x110 fs/ext4/ext4_jbd2.c:116 [...] Call Trace: ext4_write_inline_data_end+0x59a/0x730 fs/ext4/inline.c:795 generic_perform_write+0x279/0x3c0 mm/filemap.c:3344 ext4_buffered_write_iter+0x2e3/0x3d0 fs/ext4/file.c:270 ext4_file_write_iter+0x30a/0x11c0 fs/ext4/file.c:520 do_iter_readv_writev+0x339/0x3c0 fs/read_write.c:732 do_iter_write+0x107/0x430 fs/read_write.c:861 vfs_writev fs/read_write.c:934 [inline] do_pwritev+0x1e5/0x380 fs/read_write.c:1031 [...] ================================================================== Above issue may happen as follows: cpu1 cpu2 __________________________|__________________________ do_pwritev vfs_writev do_iter_write ext4_file_write_iter ext4_buffered_write_iter generic_perform_write ext4_da_write_begin vfs_fallocate ext4_fallocate ext4_convert_inline_data ext4_convert_inline_data_nolock ext4_destroy_inline_data_nolock clear EXT4_STATE_MAY_INLINE_DATA ext4_map_blocks ext4_ext_map_blocks ext4_mb_new_blocks ext4_mb_regular_allocator ext4_mb_good_group_nolock ext4_mb_init_group ext4_mb_init_cache ext4_mb_generate_buddy --> error ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) ext4_restore_inline_data set EXT4_STATE_MAY_INLINE_DATA ext4_block_write_begin ext4_da_write_end ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) ext4_write_inline_data_end handle=NULL ext4_journal_stop(handle) __ext4_journal_stop ext4_put_nojournal(handle) ref_cnt = (unsigned long)handle BUG_ON(ref_cnt == 0) ---> BUG_ON The lock held by ext4_convert_inline_data is xattr_sem, but the lock held by generic_perform_write is i_rwsem. Therefore, the two locks can be concurrent. To solve above issue, we add inode_lock() for ext4_convert_inline_data(). At the same time, move ext4_convert_inline_data() in front of ext4_punch_hole(), remove similar handling from ext4_punch_hole(). Fixes: 0c8d414f163f ("ext4: let fallocate handle inline data correctly") Cc: stable@vger.kernel.org Reported-by: Hulk Robot Signed-off-by: Baokun Li Reviewed-by: Jan Kara Link: https://lore.kernel.org/r/20220428134031.4153381-1-libaokun1@huawei.com Signed-off-by: Theodore Ts'o Signed-off-by: Tadeusz Struk Signed-off-by: Greg Kroah-Hartman --- fs/ext4/extents.c | 9 +++++---- fs/ext4/inode.c | 9 --------- 2 files changed, 5 insertions(+), 13 deletions(-) --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c @@ -4691,16 +4691,17 @@ long ext4_fallocate(struct file *file, i return -EOPNOTSUPP; ext4_fc_start_update(inode); + inode_lock(inode); + ret = ext4_convert_inline_data(inode); + inode_unlock(inode); + if (ret) + goto exit; if (mode & FALLOC_FL_PUNCH_HOLE) { ret = ext4_punch_hole(file, offset, len); goto exit; } - ret = ext4_convert_inline_data(inode); - if (ret) - goto exit; - if (mode & FALLOC_FL_COLLAPSE_RANGE) { ret = ext4_collapse_range(file, offset, len); goto exit; --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -4042,15 +4042,6 @@ int ext4_punch_hole(struct file *file, l trace_ext4_punch_hole(inode, offset, length, 0); - ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); - if (ext4_has_inline_data(inode)) { - down_write(&EXT4_I(inode)->i_mmap_sem); - ret = ext4_convert_inline_data(inode); - up_write(&EXT4_I(inode)->i_mmap_sem); - if (ret) - return ret; - } - /* * Write out all dirty pages to avoid race conditions * Then release them.