Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3764221pxk; Tue, 29 Sep 2020 05:50:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy8eq0tq2Q2Al/qz3dHfhbP4EltPBe/vb77rkib+y7K//27gVHaleNHw4bhnH8G30VDE/Wh X-Received: by 2002:a17:907:2506:: with SMTP id y6mr3455513ejl.265.1601383805890; Tue, 29 Sep 2020 05:50:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601383805; cv=none; d=google.com; s=arc-20160816; b=uv5RxdZ48xl/ZsA2Ax5J97SXU8wiaL/3XeU/CVUfr95xOfVU4QXbWNR0Jpywu3ef3Q zeU5gs7Falv0wmogs9FQIt0YwRdt9QOWwyovUMKKngpGBEh+iZ5NRqeUjIVWEgXmafWg ti5kX/McR8fJ3Fvxc5Mnio+ciP1AKihZ/f/ZOpYlYYfL2gZue64rYWGlligtn2I8hzrP PBM231n5Ndb6q1QOUmpgx91JGzyKOwI5vMhoL3wGiS4tczqEEJqe4ULCo/GQ5ncEhAI+ oYSVGZqfATDR84tbxrcsiHr6xncyVkqTBJpkevDs6aqzGXZH7TgzsYOcRtmavOceBa5x wSWw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=uKCA+5hdWJTF/uoq/ybG0sESsp4CwkYctFZbp3W4yqc=; b=Xd+Nz+niuRynY11zJCte6wG5oTy5lZWB1DsDW3XxMxXWMXIKF2oAzpnhKLK+KAK6Jm iOJACXGR9FxYFRpS6qteCahdElkmH2vOMV/yMoNkaHI3HLLUPuiyWAWIcZaK+R0cYhkm mxHr5pxefttBoKY1w+ByrGJqCbgX9I+FPKnSekEbKPBHt3/tsZfAEAv1SqzLa7GzCswz fHQWuo5Xeu2orp0PKWHLR0YjVNvkfuKpFoXTTY3oqgrNvHJOtJFD4Oe9xn+AxqXksfTZ GmZchnWEkmBIyCnDtJ/G0XGD8PzDWPi0UkRvej9ARTdnVBEDg2QFaXSgNmT2UdN7PdOM yWvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=mZtH8bYL; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p8si2619080edi.48.2020.09.29.05.49.43; Tue, 29 Sep 2020 05:50:05 -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=mZtH8bYL; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728368AbgI2LDU (ORCPT + 99 others); Tue, 29 Sep 2020 07:03:20 -0400 Received: from mail.kernel.org ([198.145.29.99]:38882 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728388AbgI2LDQ (ORCPT ); Tue, 29 Sep 2020 07:03:16 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (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 DEB2121941; Tue, 29 Sep 2020 11:03:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1601377395; bh=5urpLU8oeS6gDR7Jjp/j4LpHwZGRGcr0hFAeFKjIv6g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mZtH8bYL2+dJLqD17PeSSChYa8pu5tC2AW+k6GvcgWhWbNMRtylgmyjMpRH7qz9tz nQ2ja8oD4ZOCRCF+cvExawgX2k4oQKlsusaHOfcsC0VV9nPZkYWpmhvKVrCOW1dtN3 Ubmz+hIDC1iVIM7Kh5x1NH0o4GmLGzHBY1zcKiO0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Guoju Fang , Coly Li , Jens Axboe , Sasha Levin Subject: [PATCH 4.4 20/85] bcache: fix a lost wake-up problem caused by mca_cannibalize_lock Date: Tue, 29 Sep 2020 12:59:47 +0200 Message-Id: <20200929105929.231342533@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200929105928.198942536@linuxfoundation.org> References: <20200929105928.198942536@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 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 7fe7df56fa334..f0939fc1cfe55 100644 --- a/drivers/md/bcache/bcache.h +++ b/drivers/md/bcache/bcache.h @@ -547,6 +547,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 122d975220945..bdf6071c1b184 100644 --- a/drivers/md/bcache/btree.c +++ b/drivers/md/bcache/btree.c @@ -841,15 +841,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; } @@ -884,10 +886,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 f7f8fb079d2a9..d73f9ea776861 100644 --- a/drivers/md/bcache/super.c +++ b/drivers/md/bcache/super.c @@ -1511,6 +1511,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