From patchwork Wed Nov 16 13:07:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ChenXiaoSong X-Patchwork-Id: 21006 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp104185wru; Wed, 16 Nov 2022 04:11:52 -0800 (PST) X-Google-Smtp-Source: AA0mqf4KJo8iiJui8u/j6XlnM6WBuBb66nafz2hiMp56EUzSmjV4mko0qBXT++2/of/oEiPs5iNc X-Received: by 2002:aa7:ccd5:0:b0:458:211d:cbe4 with SMTP id y21-20020aa7ccd5000000b00458211dcbe4mr19195953edt.11.1668600712432; Wed, 16 Nov 2022 04:11:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668600712; cv=none; d=google.com; s=arc-20160816; b=IdevfzpQl6GBPnJ4eAFtOEmLj0ASct2ixhXTXWGif8c93WVYxx1lzvsmZyIve4D7Iq COcKG4kgpm4yVRBp+A3DGkK6H5HMOV2oMG0X6xRkd/xOxDlNqoqJHwVgjyE1LOIhk+gG J2SpDK1KWlTRtQldXzi9Ziw8Y3TK3BR2n+EA4QzFRKaIGd8uBs8EPGo4Z8gITFvFZxMM Oata8zVLC3UtxLqq/NvNYfE1wE83gxD9/2o+MI/GADnXq469vL669iDiuX960G23bj7Z OUxfMo5NW1DDhPpqVFQwTrB5YwYWadE1jQ6S+TMPLAsev1RQ8nxwhh/Wv0gQNhGxuic5 aUnQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=gDMFa+r5Sga12zgiOcSh/NDoeCEUkIAOuL9mUaOuyW8=; b=iRoeFJ/U8rTQ+cs6DeXrBD2X80eyuD7xI0o5JEfoeoZgjvS/hOZh861iGRsxG4+44X 9SNU81XU4m1IwLSbOdirlXgtZc/ZsteoxuNvu026fKYUFc/HYp78QDJpwdHjX5VTkpZ0 XFSxGCpCW4lJ4S0ogtliClGBM252/hFrpmwKBlcaA7Y29OOH8UzqU2rmt8CR3+0vJ2ec cCwLMjiFvEvlUvyelqLQ35PBxm+ZXwciPC5FPOb/sV9pWFvPrdYuK07rh8RPDIT5M8Cz V13Nhiw04V+wFxqo5eF8DVvaG4YGjgGj0Fu4/CKxMcpt4Gskog1gLuNdzAr+Uhe1mvfb gitA== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c11-20020a05640227cb00b0046151310d6dsi8028750ede.498.2022.11.16.04.11.28; Wed, 16 Nov 2022 04:11:52 -0800 (PST) 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; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232298AbiKPMKA (ORCPT + 99 others); Wed, 16 Nov 2022 07:10:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54340 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238495AbiKPMJB (ORCPT ); Wed, 16 Nov 2022 07:09:01 -0500 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D18A930F70; Wed, 16 Nov 2022 04:02:44 -0800 (PST) Received: from dggemv703-chm.china.huawei.com (unknown [172.30.72.53]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4NC1n20nNKzJnnc; Wed, 16 Nov 2022 19:59:34 +0800 (CST) Received: from kwepemm600015.china.huawei.com (7.193.23.52) by dggemv703-chm.china.huawei.com (10.3.19.46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 16 Nov 2022 20:02:42 +0800 Received: from huawei.com (10.175.101.6) by kwepemm600015.china.huawei.com (7.193.23.52) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 16 Nov 2022 20:02:41 +0800 From: ChenXiaoSong To: , , CC: , , , , , , Subject: [PATCH v5 1/2] btrfs: add might_sleep() to btrfs_search_slot() Date: Wed, 16 Nov 2022 21:07:15 +0800 Message-ID: <20221116130716.991901-2-chenxiaosong2@huawei.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20221116130716.991901-1-chenxiaosong2@huawei.com> References: <20221116130716.991901-1-chenxiaosong2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.101.6] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemm600015.china.huawei.com (7.193.23.52) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749654660712287099?= X-GMAIL-MSGID: =?utf-8?q?1749654660712287099?= At least 2 places might sleep in btrfs_search_slot(), as shown below: btrfs_search_slot setup_nodes_for_search reada_for_balance btrfs_readahead_node_child btrfs_readahead_tree_block btrfs_find_create_tree_block alloc_extent_buffer kmem_cache_zalloc /* allocate memory non-atomically, might sleep */ kmem_cache_alloc(GFP_NOFS|__GFP_NOFAIL|__GFP_ZERO) read_extent_buffer_pages submit_extent_page /* disk IO, might sleep */ submit_one_bio As the potential sleeping under spin lock is hard to spot, we should add might_sleep() to btrfs_search_slot(). Signed-off-by: ChenXiaoSong --- fs/btrfs/ctree.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c index a9543f01184c..809053e9cfde 100644 --- a/fs/btrfs/ctree.c +++ b/fs/btrfs/ctree.c @@ -1934,6 +1934,8 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root, int min_write_lock_level; int prev_cmp; + might_sleep(); + lowest_level = p->lowest_level; WARN_ON(lowest_level && ins_len > 0); WARN_ON(p->nodes[0] != NULL); From patchwork Wed Nov 16 13:07:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ChenXiaoSong X-Patchwork-Id: 21007 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp104225wru; Wed, 16 Nov 2022 04:11:56 -0800 (PST) X-Google-Smtp-Source: AA0mqf4aJl5E7f0Q5LUn4VsGq+cAKL5COyGcPCf7eDhX4F3RnMoKfAP77VJVXII6buHAOOPjXxXW X-Received: by 2002:a17:906:6a28:b0:7ad:8608:a0a1 with SMTP id qw40-20020a1709066a2800b007ad8608a0a1mr17227617ejc.110.1668600716026; Wed, 16 Nov 2022 04:11:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668600716; cv=none; d=google.com; s=arc-20160816; b=eyltjkpZR+ooWfN+GUFtFxt2WIdafd04b/TITp76WR+gr27fIN7BE1Q/vdz2U7YUp+ Mk2SKp3PMQnYKoASArEzw25s9DAw/0iKwzeZT2A/RH3TbIVKGza6MnV8gFa8q6558pne LZq3nv/r4g57TrneSoPLzFdZ56BEXi3Hfi5Pw2dyyl4n4Yd9HXd+1R9jJlp3+8/yqmYS CjflEOyNTWGWvBLStJTxd3gjLP1N3msy8XE3Kg0v2O9TvM21T3plAlno3bKXzwYaKhKx QJxGi9Zfld184siR73iqCLO7ao5kKNfiYRDqUW4/x2U5kRtPDYizLUBGR1guaJRDsm/Z KnRw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=CmGY/rMQY0rk6j0q1A1n0ApEDUefFx6EqE5+RBFzto4=; b=kWIQn/zwnKcKG3CN8zPALOmbDfwTNeTefIxiGmkZFQB+Hm4Xq8LX58rGbNi4/j5oBn kbx+voeRlc9I3bCpNvC8pyQ0FeQGBfGgwN72rqdnXgbc8vG4ZqNx90RuaVtQghefh3ls wxBhtAVTAed86iltoS12Icz2ixYKOhZRuhSQULWAcURAAdajkICEdGX6jY6vdt/7cJ3B cNEVghBbv049JZYrfGMKJffckCazFmCvKPDXldHGzKT7ajxj3PeHGVjN4MU1oSRC3mih Pj0FXqnSx8SSpA8pSQzv+qCLwcz220kJk8P73TSj9jfbfy7TEN9AG+2bpBQCWblV0QUM 4Dsw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id xd9-20020a170907078900b0078e267ea163si1848124ejb.557.2022.11.16.04.11.32; Wed, 16 Nov 2022 04:11:56 -0800 (PST) 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; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233818AbiKPMKF (ORCPT + 99 others); Wed, 16 Nov 2022 07:10:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54782 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232951AbiKPMJC (ORCPT ); Wed, 16 Nov 2022 07:09:02 -0500 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A7E992EF76; Wed, 16 Nov 2022 04:02:45 -0800 (PST) Received: from dggemv711-chm.china.huawei.com (unknown [172.30.72.55]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4NC1r46bqdzHvwH; Wed, 16 Nov 2022 20:02:12 +0800 (CST) Received: from kwepemm600015.china.huawei.com (7.193.23.52) by dggemv711-chm.china.huawei.com (10.1.198.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 16 Nov 2022 20:02:43 +0800 Received: from huawei.com (10.175.101.6) by kwepemm600015.china.huawei.com (7.193.23.52) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 16 Nov 2022 20:02:42 +0800 From: ChenXiaoSong To: , , CC: , , , , , , Subject: [PATCH v5 2/2] btrfs: qgroup: fix sleep from invalid context bug in update_qgroup_limit_item() Date: Wed, 16 Nov 2022 21:07:16 +0800 Message-ID: <20221116130716.991901-3-chenxiaosong2@huawei.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20221116130716.991901-1-chenxiaosong2@huawei.com> References: <20221116130716.991901-1-chenxiaosong2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.101.6] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemm600015.china.huawei.com (7.193.23.52) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749654664543760349?= X-GMAIL-MSGID: =?utf-8?q?1749654664543760349?= Syzkaller reported BUG as follows: BUG: sleeping function called from invalid context at include/linux/sched/mm.h:274 Call Trace: dump_stack_lvl+0xcd/0x134 __might_resched.cold+0x222/0x26b kmem_cache_alloc+0x2e7/0x3c0 update_qgroup_limit_item+0xe1/0x390 btrfs_qgroup_inherit+0x147b/0x1ee0 create_subvol+0x4eb/0x1710 btrfs_mksubvol+0xfe5/0x13f0 __btrfs_ioctl_snap_create+0x2b0/0x430 btrfs_ioctl_snap_create_v2+0x25a/0x520 btrfs_ioctl+0x2a1c/0x5ce0 __x64_sys_ioctl+0x193/0x200 do_syscall_64+0x35/0x80 Fix this by calling qgroup_dirty() on @dstqgroup, and update limit item in btrfs_run_qgroups() later. Signed-off-by: ChenXiaoSong --- fs/btrfs/qgroup.c | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/fs/btrfs/qgroup.c b/fs/btrfs/qgroup.c index 9334c3157c22..8f5c52e24430 100644 --- a/fs/btrfs/qgroup.c +++ b/fs/btrfs/qgroup.c @@ -2860,6 +2860,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, bool need_rescan = false; u32 level_size = 0; u64 nums; + bool dirty_dstgrp = false; /* * There are only two callers of this function. @@ -2941,7 +2942,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, dstgroup = add_qgroup_rb(fs_info, objectid); if (IS_ERR(dstgroup)) { ret = PTR_ERR(dstgroup); - goto unlock; + goto dirty; } if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) { @@ -2950,21 +2951,13 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, dstgroup->max_excl = inherit->lim.max_excl; dstgroup->rsv_rfer = inherit->lim.rsv_rfer; dstgroup->rsv_excl = inherit->lim.rsv_excl; - - ret = update_qgroup_limit_item(trans, dstgroup); - if (ret) { - qgroup_mark_inconsistent(fs_info); - btrfs_info(fs_info, - "unable to update quota limit for %llu", - dstgroup->qgroupid); - goto unlock; - } + dirty_dstgrp = true; } if (srcid) { srcgroup = find_qgroup_rb(fs_info, srcid); if (!srcgroup) - goto unlock; + goto dirty; /* * We call inherit after we clone the root in order to make sure @@ -2985,20 +2978,20 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, dstgroup->max_excl = srcgroup->max_excl; dstgroup->rsv_rfer = srcgroup->rsv_rfer; dstgroup->rsv_excl = srcgroup->rsv_excl; + dirty_dstgrp = true; - qgroup_dirty(fs_info, dstgroup); qgroup_dirty(fs_info, srcgroup); } if (!inherit) - goto unlock; + goto dirty; i_qgroups = (u64 *)(inherit + 1); for (i = 0; i < inherit->num_qgroups; ++i) { if (*i_qgroups) { ret = add_relation_rb(fs_info, objectid, *i_qgroups); if (ret) - goto unlock; + goto dirty; } ++i_qgroups; @@ -3022,7 +3015,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, if (!src || !dst) { ret = -EINVAL; - goto unlock; + goto dirty; } dst->rfer = src->rfer - level_size; @@ -3043,15 +3036,17 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, if (!src || !dst) { ret = -EINVAL; - goto unlock; + goto dirty; } dst->excl = src->excl + level_size; dst->excl_cmpr = src->excl_cmpr + level_size; need_rescan = true; } +dirty: + if (dirty_dstgrp) + qgroup_dirty(fs_info, dstgroup); -unlock: spin_unlock(&fs_info->qgroup_lock); if (!ret) ret = btrfs_sysfs_add_one_qgroup(fs_info, dstgroup);