Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp737771imm; Thu, 13 Sep 2018 07:03:43 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbeOZYeiNOLUNqQR6/hB1fJeM30JyvxG3LLDYtfOVaZ4dahqDyUygornWrHO5+kRtFTIks3 X-Received: by 2002:a17:902:9a47:: with SMTP id x7-v6mr7526719plv.37.1536847423922; Thu, 13 Sep 2018 07:03:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536847423; cv=none; d=google.com; s=arc-20160816; b=CZcjegDLSLXK74ozeFvkK+xWDO+rJWbi6HmUmMeXQUogH/lv4jP5Uc28g+9tMKGic9 61PwHTil6duDWPOLZL6siaOerO/mefnvAB5xoADpOBHYcqkrWaPaoKUeNGwfXdWRX4jA fb+fjffvz3aA+32wqyUeDoQoFLGFxNc1/J0ITyLLThIPWOn+I5Lr6bueN6fIvU32vbUu g5qxgqLKh1VmI4gtBXsDcSI7YG8YmUWE7TD7IvIroOvxUnwNSvkD4dFbaF5ADknAvX3K xOzhAmoIzqwIVp02TUq55Em9pEpBFziY5UYu2g05BtJLlP+AhT7mHY7inzdvXmNQ3aJh vhcQ== 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; bh=1Y5TMAz9TowGt0+16dS2ie2y6RfBUjhaLxcEqxPQQIc=; b=0uuotMzcu90BtEKCfVB+T4uWCNnzin2j9bMptsajt8uhZwNq6OnF5aaswUksD2FONH 4j26uDAorO+DYfyUOLHurDqKnNeMPApT/tgA7DhmIfEQtRoTX4Ew+JZcCHmtJyLQ+257 CtGU1Gxvd79XICWR7BCPqr//N5Lw3sJ0OdagSJ/s9RYZgJTjKYO3B6Wrt2cz0kMacmii 0uYB9UqmNXaD/yyN4XWO+to0qlDYvtXpBbEV+ZURrDv2nRkqafLaJ25OZXj0F9ek61ZR ylJPJeHr1aXwGCiLjm6uNDZyu+9aIz6N6QddtylJ5AhO6WRVs31wFJTn/3PyxrBDdyd1 YzCw== ARC-Authentication-Results: i=1; mx.google.com; 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 t31-v6si3272172pga.167.2018.09.13.07.03.25; Thu, 13 Sep 2018 07:03:43 -0700 (PDT) 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; 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 S1731899AbeIMTLb (ORCPT + 99 others); Thu, 13 Sep 2018 15:11:31 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:35112 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731895AbeIMTLb (ORCPT ); Thu, 13 Sep 2018 15:11:31 -0400 Received: from localhost (ip-213-127-77-73.ip.prioritytelecom.net [213.127.77.73]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 81B8AD27; Thu, 13 Sep 2018 14:01:52 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Anand Jain , David Sterba , Sasha Levin Subject: [PATCH 4.18 145/197] btrfs: extend locked section when adding a new device in device_list_add Date: Thu, 13 Sep 2018 15:31:34 +0200 Message-Id: <20180913131847.356027895@linuxfoundation.org> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20180913131841.568116777@linuxfoundation.org> References: <20180913131841.568116777@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review 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 4.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Anand Jain [ Upstream commit 9c6d173ea6e4c8c939ae6c257c7fc18f7b320316 ] Make sure the device_list_lock is held the whole time: * when the device is being looked up * new device is initialized and put to the list * the list counters are updated (fs_devices::opened, fs_devices::total_devices) Signed-off-by: Anand Jain [ update changelog ] Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/volumes.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -765,21 +765,26 @@ static noinline struct btrfs_device *dev if (IS_ERR(fs_devices)) return ERR_CAST(fs_devices); + mutex_lock(&fs_devices->device_list_mutex); list_add(&fs_devices->fs_list, &fs_uuids); device = NULL; } else { + mutex_lock(&fs_devices->device_list_mutex); device = find_device(fs_devices, devid, disk_super->dev_item.uuid); } if (!device) { - if (fs_devices->opened) + if (fs_devices->opened) { + mutex_unlock(&fs_devices->device_list_mutex); return ERR_PTR(-EBUSY); + } device = btrfs_alloc_device(NULL, &devid, disk_super->dev_item.uuid); if (IS_ERR(device)) { + mutex_unlock(&fs_devices->device_list_mutex); /* we can safely leave the fs_devices entry around */ return device; } @@ -787,14 +792,13 @@ static noinline struct btrfs_device *dev name = rcu_string_strdup(path, GFP_NOFS); if (!name) { btrfs_free_device(device); + mutex_unlock(&fs_devices->device_list_mutex); return ERR_PTR(-ENOMEM); } rcu_assign_pointer(device->name, name); - mutex_lock(&fs_devices->device_list_mutex); list_add_rcu(&device->dev_list, &fs_devices->devices); fs_devices->num_devices++; - mutex_unlock(&fs_devices->device_list_mutex); device->fs_devices = fs_devices; *new_device_added = true; @@ -841,12 +845,15 @@ static noinline struct btrfs_device *dev * with larger generation number or the last-in if * generation are equal. */ + mutex_unlock(&fs_devices->device_list_mutex); return ERR_PTR(-EEXIST); } name = rcu_string_strdup(path, GFP_NOFS); - if (!name) + if (!name) { + mutex_unlock(&fs_devices->device_list_mutex); return ERR_PTR(-ENOMEM); + } rcu_string_free(device->name); rcu_assign_pointer(device->name, name); if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { @@ -866,6 +873,7 @@ static noinline struct btrfs_device *dev fs_devices->total_devices = btrfs_super_num_devices(disk_super); + mutex_unlock(&fs_devices->device_list_mutex); return device; }