Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp8573ybt; Tue, 23 Jun 2020 13:53:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxmrlKAv5ypuOw8Y0sexxh9jua4qINTsglFd0mBjVC1bNEF1e1Q+bwnvqGfplh8BDb6mhzh X-Received: by 2002:a50:fe07:: with SMTP id f7mr24016059edt.315.1592945580051; Tue, 23 Jun 2020 13:53:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592945580; cv=none; d=google.com; s=arc-20160816; b=gnij6SRBaJwXgt7HOwmWM7QYk6SVSncTSCjeKQ7D9yTXyVMZmRPkaHO0cTIft7TveJ qlI3RWH7SBCpMNZvwb0A6DTWNW93IEz7rN5gi+GJmGyQ/xfDMnsEQNmuxfrMqUCw/tRK enQGRgjX0uzhWUrLfxHuOqaDJQTHK0i0TYN/ECYHzcWVL5tRB6Tx1mR+EUlzICraf+C5 y34ZuqCdWWvQuw/oPnWJsOnK13cQFlOrTiPWb5jiwHpMyJr1BMvxxoipMZ1a6VGgLfxY yg3KvPH0Gn/C5lSzliMmQ4Bh0pQvDZMfcp86JS7D6WEBA6wCwJpEt5iyTGajb2QSgtFt AGJA== 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=99ifDHchf5YPfFHXRHZ8Ib80ORQMGUOMcyujI13xvyg=; b=bF3zcsrutAHDa6GNh6t5r2wOdTqwPJPFCc6ZZI25UTItnHC3rRC/CYVUPZ+T6KDdCf wHiKwvbCO7Sn5I14j1mhUc1mY5SbaYLsiLWMjbS1mlKynDKqk1Gvkzn9RTM89ownIwiY 3RNalpiXDkvuUSm9W42IVNxQSbkLjRzITQvW+hE2QsHfy7Kj2ZapIA0g3bpEEJfojbQO 55WPK5YdAvmo9ZPBNkb+mck+uaRaoX00M7rtISVoW/ucKl9TpuRbodrM1sNURkOT/YE2 DWnYEFK4cYFu4ivfT3dassj0M9qe/pbIFE9FrCU1Q7T5NMJHwrFPKFcKxY9JAa0c9tG0 Ak9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="YbEmdYw/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c6si1080441edv.311.2020.06.23.13.52.36; Tue, 23 Jun 2020 13:53:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="YbEmdYw/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2392773AbgFWUtO (ORCPT + 99 others); Tue, 23 Jun 2020 16:49:14 -0400 Received: from mail.kernel.org ([198.145.29.99]:48564 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389240AbgFWUtJ (ORCPT ); Tue, 23 Jun 2020 16:49:09 -0400 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 A0E7921548; Tue, 23 Jun 2020 20:49:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1592945349; bh=wYNo/0POcBUsahM54OawsFDuz6ZmxzPbMlN9WqiBGK0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YbEmdYw/h774wnn1Urq6FR6Lfao0OOyJ+HOPQ6rqYpEJ3HUb4q2rp3E9jyGRQ6Lhk WDNIrpfZoBZ6rS4EGB1NXQYWlSN3eKMHZsAzU52A6k0rbhiZe/6z4GCZ6kFnx98NIq mNQQ1Y3LI+DzIopmz0yqAJ0fABPkPYe0lYX3t/Vc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Zhiqiang Liu , Coly Li , Jens Axboe , Sasha Levin Subject: [PATCH 4.14 107/136] bcache: fix potential deadlock problem in btree_gc_coalesce Date: Tue, 23 Jun 2020 21:59:23 +0200 Message-Id: <20200623195309.060217822@linuxfoundation.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200623195303.601828702@linuxfoundation.org> References: <20200623195303.601828702@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: Zhiqiang Liu [ Upstream commit be23e837333a914df3f24bf0b32e87b0331ab8d1 ] coccicheck reports: drivers/md//bcache/btree.c:1538:1-7: preceding lock on line 1417 In btree_gc_coalesce func, if the coalescing process fails, we will goto to out_nocoalesce tag directly without releasing new_nodes[i]->write_lock. Then, it will cause a deadlock when trying to acquire new_nodes[i]-> write_lock for freeing new_nodes[i] before return. btree_gc_coalesce func details as follows: if alloc new_nodes[i] fails: goto out_nocoalesce; // obtain new_nodes[i]->write_lock mutex_lock(&new_nodes[i]->write_lock) // main coalescing process for (i = nodes - 1; i > 0; --i) [snipped] if coalescing process fails: // Here, directly goto out_nocoalesce // tag will cause a deadlock goto out_nocoalesce; [snipped] // release new_nodes[i]->write_lock mutex_unlock(&new_nodes[i]->write_lock) // coalesing succ, return return; out_nocoalesce: btree_node_free(new_nodes[i]) // free new_nodes[i] // obtain new_nodes[i]->write_lock mutex_lock(&new_nodes[i]->write_lock); // set flag for reuse clear_bit(BTREE_NODE_dirty, &ew_nodes[i]->flags); // release new_nodes[i]->write_lock mutex_unlock(&new_nodes[i]->write_lock); To fix the problem, we add a new tag 'out_unlock_nocoalesce' for releasing new_nodes[i]->write_lock before out_nocoalesce tag. If coalescing process fails, we will go to out_unlock_nocoalesce tag for releasing new_nodes[i]->write_lock before free new_nodes[i] in out_nocoalesce tag. (Coly Li helps to clean up commit log format.) Fixes: 2a285686c109816 ("bcache: btree locking rework") Signed-off-by: Zhiqiang Liu Signed-off-by: Coly Li Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- drivers/md/bcache/btree.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c index 96a6583e7b522..66c764491a830 100644 --- a/drivers/md/bcache/btree.c +++ b/drivers/md/bcache/btree.c @@ -1374,7 +1374,7 @@ static int btree_gc_coalesce(struct btree *b, struct btree_op *op, if (__set_blocks(n1, n1->keys + n2->keys, block_bytes(b->c)) > btree_blocks(new_nodes[i])) - goto out_nocoalesce; + goto out_unlock_nocoalesce; keys = n2->keys; /* Take the key of the node we're getting rid of */ @@ -1403,7 +1403,7 @@ static int btree_gc_coalesce(struct btree *b, struct btree_op *op, if (__bch_keylist_realloc(&keylist, bkey_u64s(&new_nodes[i]->key))) - goto out_nocoalesce; + goto out_unlock_nocoalesce; bch_btree_node_write(new_nodes[i], &cl); bch_keylist_add(&keylist, &new_nodes[i]->key); @@ -1449,6 +1449,10 @@ static int btree_gc_coalesce(struct btree *b, struct btree_op *op, /* Invalidated our iterator */ return -EINTR; +out_unlock_nocoalesce: + for (i = 0; i < nodes; i++) + mutex_unlock(&new_nodes[i]->write_lock); + out_nocoalesce: closure_sync(&cl); bch_keylist_free(&keylist); -- 2.25.1