Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp851550pxk; Thu, 17 Sep 2020 19:04:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy22pfHEZ1BT0Pg8eRJdGIg4lyNuB0zCFES1+0+FJNOjH3WL0qYexRU88nbPHXdtpzmD5Fe X-Received: by 2002:a17:906:d8cd:: with SMTP id re13mr32880459ejb.140.1600394647846; Thu, 17 Sep 2020 19:04:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600394647; cv=none; d=google.com; s=arc-20160816; b=CpbswRiSVxPHKyCmo5C3y9TvZcrzi6e+XHHwStcvH9zAY0Trl7YJh486as7UfUlnA7 aV3j9ozgnrIet2FLn6QD1shi2P18Cg2PMWHyyJYbO3GJb6bejcI5PoSjEgDhD2nHADUo RItge7+K6eHZWNizKM1O0idB3CTx8UYp5zjOETaecO4Mq53FIDC/Pm2/hdf3fTNrW6d2 MyABW/D0idY1xggLagZTO/pDqnOu7cu5NeXdo65vnMaHA2vActH9bGFqST0Us8u+6RsL OjvzuKK/ivp1120ikoKoMQ65CwgtnraIsM/rMMEKS7kmdJAOxZMkiijkM/biCECzlRqp YsfA== 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 :dkim-signature; bh=pFQKP4G1uv4qK/I3nXWW6NgCbX2hrR/SHI4Jkx6j+uA=; b=oL1UmOHKLS4jW0s58Y/kLjjqv/vOk+IBaod096l1S5EoWnUEAqqt5ihwYMkTSCvlGS J9iKfBbT1x/+n13/RBt3PsoMSEqeZPp/6KtFcWR4lTyVt8PUtYOscvl0CoNYefhWtBTf d07ZGxORBWS17SLM+pX+JUgj9galCOndfq6axlMBIv4tXHvXGBZvTShOH5RgVSOSAM5p zuXywGweohzLsrWdgTLvt2k8Ex4eC8+j6hAtqAbd7NMBCYM8EIcl0xlilzQGV5coSmIF CBUSaeZT1FC23j/XGcq+84W00IWAAi8wXzn/2m+5lhfKp4aIP03zKYv5dvgG2bouSnP0 XJcA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=JqGxW7uI; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id da1si1111682edb.102.2020.09.17.19.03.44; Thu, 17 Sep 2020 19:04:07 -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=JqGxW7uI; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726692AbgIRCCN (ORCPT + 99 others); Thu, 17 Sep 2020 22:02:13 -0400 Received: from mail.kernel.org ([198.145.29.99]:47000 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726611AbgIRCCF (ORCPT ); Thu, 17 Sep 2020 22:02:05 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2BBAA208DB; Fri, 18 Sep 2020 02:02:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600394524; bh=ANl3JrAbLgRoeObtJtIWUNJ2+H7AmOOwkQArMASHwBc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JqGxW7uIoAiPlEZSaQa8sy2VZGjgnNNX/p2PmRFEjFS5sUOiidiyh/ZhcG1xiomDl LVHb/6IqxO5g93rZNpXC2ZV6esekGJHuhjAhf82qglbDUhIokDyOoKXLttkmWemUQQ an25T6LMfn3tkT+UXzLJ++1Vc0liyRYSxVyR/nfo= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Guoju Fang , Coly Li , Jens Axboe , Sasha Levin , linux-bcache@vger.kernel.org, linux-raid@vger.kernel.org Subject: [PATCH AUTOSEL 5.4 045/330] bcache: fix a lost wake-up problem caused by mca_cannibalize_lock Date: Thu, 17 Sep 2020 21:56:25 -0400 Message-Id: <20200918020110.2063155-45-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200918020110.2063155-1-sashal@kernel.org> References: <20200918020110.2063155-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Guoju Fang [ Upstream commit 34cf78bf34d48dddddfeeadb44f9841d7864997a ] This patch fix a lost wake-up problem caused by the race between mca_cannibalize_lock and bch_cannibalize_unlock. Consider two processes, A and B. Process A is executing mca_cannibalize_lock, while process B takes c->btree_cache_alloc_lock and is executing bch_cannibalize_unlock. The problem happens that after process A executes cmpxchg and will execute prepare_to_wait. In this timeslice process B executes wake_up, but after that process A executes prepare_to_wait and set the state to TASK_INTERRUPTIBLE. Then process A goes to sleep but no one will wake up it. This problem may cause bcache device to dead. Signed-off-by: Guoju Fang Signed-off-by: Coly Li Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- drivers/md/bcache/bcache.h | 1 + drivers/md/bcache/btree.c | 12 ++++++++---- drivers/md/bcache/super.c | 1 + 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h index 217c838a1b405..859567ad3db4e 100644 --- a/drivers/md/bcache/bcache.h +++ b/drivers/md/bcache/bcache.h @@ -585,6 +585,7 @@ struct cache_set { */ wait_queue_head_t btree_cache_wait; struct task_struct *btree_cache_alloc_lock; + spinlock_t btree_cannibalize_lock; /* * When we free a btree node, we increment the gen of the bucket the diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c index 46556bde032e2..8d06105fc9ff5 100644 --- a/drivers/md/bcache/btree.c +++ b/drivers/md/bcache/btree.c @@ -886,15 +886,17 @@ out: static int mca_cannibalize_lock(struct cache_set *c, struct btree_op *op) { - struct task_struct *old; - - old = cmpxchg(&c->btree_cache_alloc_lock, NULL, current); - if (old && old != current) { + spin_lock(&c->btree_cannibalize_lock); + if (likely(c->btree_cache_alloc_lock == NULL)) { + c->btree_cache_alloc_lock = current; + } else if (c->btree_cache_alloc_lock != current) { if (op) prepare_to_wait(&c->btree_cache_wait, &op->wait, TASK_UNINTERRUPTIBLE); + spin_unlock(&c->btree_cannibalize_lock); return -EINTR; } + spin_unlock(&c->btree_cannibalize_lock); return 0; } @@ -929,10 +931,12 @@ static struct btree *mca_cannibalize(struct cache_set *c, struct btree_op *op, */ static void bch_cannibalize_unlock(struct cache_set *c) { + spin_lock(&c->btree_cannibalize_lock); if (c->btree_cache_alloc_lock == current) { c->btree_cache_alloc_lock = NULL; wake_up(&c->btree_cache_wait); } + spin_unlock(&c->btree_cannibalize_lock); } static struct btree *mca_alloc(struct cache_set *c, struct btree_op *op, diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c index 2cbfcd99b7ee7..63f5ce18311bb 100644 --- a/drivers/md/bcache/super.c +++ b/drivers/md/bcache/super.c @@ -1798,6 +1798,7 @@ struct cache_set *bch_cache_set_alloc(struct cache_sb *sb) sema_init(&c->sb_write_mutex, 1); mutex_init(&c->bucket_lock); init_waitqueue_head(&c->btree_cache_wait); + spin_lock_init(&c->btree_cannibalize_lock); init_waitqueue_head(&c->bucket_wait); init_waitqueue_head(&c->gc_wait); sema_init(&c->uuid_write_mutex, 1); -- 2.25.1