Skip to contentSkip to conversation

btrfs: qgroup: flush reservations during quota disable

bug report Dan Carpenter 1 participant 1 message Sep 27, 2023 Btrfs file system

lore
Share & tools
lore
#0 lore raw

Hello Boris Burkov,

The patch 5e99a45f1f0f: "btrfs: qgroup: flush reservations during quota disable" from Jun 28, 2023 (linux-next), leads to the following Smatch static checker warning:

	fs/btrfs/qgroup.c:1437 btrfs_quota_disable()
	error: double unlocked '&fs_info->qgroup_ioctl_lock' (orig line 1366)

fs/btrfs/qgroup.c

    1332 int btrfs_quota_disable(struct btrfs_fs_info *fs_info)
    1333 {
    1334         struct btrfs_root *quota_root;
    1335         struct btrfs_trans_handle *trans = NULL;
    1336         int ret = 0;
    1337 
    1338         /*
    1339          * We need to have subvol_sem write locked to prevent races with
    1340          * snapshot creation.
    1341          */
    1342         lockdep_assert_held_write(&fs_info->subvol_sem);
    1343 
    1344         /*
    1345          * Lock the cleaner mutex to prevent races with concurrent relocation,
    1346          * because relocation may be building backrefs for blocks of the quota
    1347          * root while we are deleting the root. This is like dropping fs roots
    1348          * of deleted snapshots/subvolumes, we need the same protection.
    1349          *
    1350          * This also prevents races between concurrent tasks trying to disable
    1351          * quotas, because we will unlock and relock qgroup_ioctl_lock across
    1352          * BTRFS_FS_QUOTA_ENABLED changes.
    1353          */
    1354         mutex_lock(&fs_info->cleaner_mutex);
    1355 
    1356         mutex_lock(&fs_info->qgroup_ioctl_lock);
    1357         if (!fs_info->quota_root)
    1358                 goto out;
    1359 
    1360         /*
    1361          * Unlock the qgroup_ioctl_lock mutex before waiting for the rescan worker to
    1362          * complete. Otherwise we can deadlock because btrfs_remove_qgroup() needs
    1363          * to lock that mutex while holding a transaction handle and the rescan
    1364          * worker needs to commit a transaction.
    1365          */
    1366         mutex_unlock(&fs_info->qgroup_ioctl_lock);

We drop the lock.

    1367 
    1368         /*
    1369          * Request qgroup rescan worker to complete and wait for it. This wait
    1370          * must be done before transaction start for quota disable since it may
    1371          * deadlock with transaction by the qgroup rescan worker.
    1372          */
    1373         clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
    1374         btrfs_qgroup_wait_for_completion(fs_info, false);
    1375 
    1376         ret = flush_reservations(fs_info);
    1377         if (ret)
    1378                 goto out;

The patch introduces a new goto.

    1379 
    1380         /*
    1381          * 1 For the root item
    1382          *
    1383          * We should also reserve enough items for the quota tree deletion in
    1384          * btrfs_clean_quota_tree but this is not done.
    1385          *
    1386          * Also, we must always start a transaction without holding the mutex
    1387          * qgroup_ioctl_lock, see btrfs_quota_enable().
    1388          */
    1389         trans = btrfs_start_transaction(fs_info->tree_root, 1);
    1390 
    1391         mutex_lock(&fs_info->qgroup_ioctl_lock);
    1392         if (IS_ERR(trans)) {
    1393                 ret = PTR_ERR(trans);
    1394                 trans = NULL;
    1395                 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
    1396                 goto out;
    1397         }
    1398 
    1399         if (!fs_info->quota_root)
    1400                 goto out;
    1401 
    1402         spin_lock(&fs_info->qgroup_lock);
    1403         quota_root = fs_info->quota_root;
    1404         fs_info->quota_root = NULL;
    1405         fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
    1406         fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_SIMPLE_MODE;
    1407         fs_info->qgroup_drop_subtree_thres = BTRFS_MAX_LEVEL;
    1408         spin_unlock(&fs_info->qgroup_lock);
    1409 
    1410         btrfs_free_qgroup_config(fs_info);
    1411 
    1412         ret = btrfs_clean_quota_tree(trans, quota_root);
    1413         if (ret) {
    1414                 btrfs_abort_transaction(trans, ret);
    1415                 goto out;
    1416         }
    1417 
    1418         ret = btrfs_del_root(trans, &quota_root->root_key);
    1419         if (ret) {
    1420                 btrfs_abort_transaction(trans, ret);
    1421                 goto out;
    1422         }
    1423 
    1424         spin_lock(&fs_info->trans_lock);
    1425         list_del(&quota_root->dirty_list);
    1426         spin_unlock(&fs_info->trans_lock);
    1427 
    1428         btrfs_tree_lock(quota_root->node);
    1429         btrfs_clear_buffer_dirty(trans, quota_root->node);
    1430         btrfs_tree_unlock(quota_root->node);
    1431         btrfs_free_tree_block(trans, btrfs_root_id(quota_root),
    1432                               quota_root->node, 0, 1);
    1433 
    1434         btrfs_put_root(quota_root);
    1435 
    1436 out:

--> 1437 mutex_unlock(&fs_info->qgroup_ioctl_lock);

Double unlock.

    1438         if (ret && trans)
    1439                 btrfs_end_transaction(trans);
    1440         else if (trans)
    1441                 ret = btrfs_commit_transaction(trans);
    1442         mutex_unlock(&fs_info->cleaner_mutex);
    1443 
    1444         return ret;
    1445 }

regards,
dan carpenter