Received: by 2002:a25:86ce:0:0:0:0:0 with SMTP id y14csp11658ybm; Mon, 20 May 2019 10:59:21 -0700 (PDT) X-Google-Smtp-Source: APXvYqzxF7Ce5DcbrFb640VM0E7kRF5hMvPukNJJk/A3I+rZxr5wcUMd0w8GAJUln+TfQqHvvv2Y X-Received: by 2002:a17:902:aa0a:: with SMTP id be10mr13694962plb.293.1558375161886; Mon, 20 May 2019 10:59:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558375161; cv=none; d=google.com; s=arc-20160816; b=aHWAnCJ+AxLX90S6dy1ZPZCRxyhpOBSdt4yJ/svfcFbVLcir2Dzfyliub2aspTMpP9 GbdbHg2ECRn7Kxlp9uwpbEYzj6kJr02hkmQB1J2ChCT4SQfCabRc0k80yiEEaL8h3iw+ KYlpcfmNPKv+KK1Rc8krcHBE3e9/UyApxt8cCXMlidgSg15SQdHURS9kLSJEgIb26leD M978opEKo9WaWaRwbxztuZGQHwkio26hfTZcmApfLDhYD2TDSbvAycYZnSlVC+Zl9jQe Biw7BVRR0uC4XMBsoEeOIMRgJOruPpLPQM/Fih1nNCaSRabzUDw/dO8o9ayhX5moqjg/ /Hpg== 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=QuRZhuKEqhgyvl553kCm+RQUdeXhnz3wt80UZP2fBOg=; b=OkkVxk3WP1G4hyRcHs5uH3EGyj9HyVg+DL/5p5umwqZiLo6WM7TmDls0ZBaw0BjDI0 xcpFQ2qZllGEAPiOB3PF7hcg3ZoHF34uHuisgofpB3LSiDihS3EYEuvNmhJWY2rgsxK8 xvUqPbg5ecLwNVHvDu9V7HvrHZsfmo7cdurLmMKYxIDcu09LvlY+piCMvxcXpkh73+OJ x+wn2F/zx05LXcjGdHs/bgmmYnIAtUBiPUUUo8F+cM2z4rNHNRN4jhIquH1S+I2tMt/1 G2mkVP5qgSkwNnlW75lsrAS74EDxuKb5uugLKfDQCTEEAO2Nv6Vx8VdYgfI4sju+aa7I 73hA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=dauN3iQg; 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 g4si20091702plm.324.2019.05.20.10.59.07; Mon, 20 May 2019 10:59:21 -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; dkim=pass header.i=@kernel.org header.s=default header.b=dauN3iQg; 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 S2388788AbfETMvJ (ORCPT + 99 others); Mon, 20 May 2019 08:51:09 -0400 Received: from mail.kernel.org ([198.145.29.99]:33326 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387909AbfETMUA (ORCPT ); Mon, 20 May 2019 08:20:00 -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 AC593216E3; Mon, 20 May 2019 12:19:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1558354800; bh=FShAbIvuo5Dxjc1FjFgVxxINTXvXvWkM+qxwB8esTp4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dauN3iQgqUk+7N/BxZRcHyQyh+1wM8ZSA2uJ6qxfT65oPb6XV9GMtO4r4BSrfz6/q mTe/kf5EB499NJzGjCpaP8wQSyTlI6/fCvZ9m9GWekC2yZdXmSHdwQYtPgiwK84I8E MGuZhMFpYOQ/mDxhkor6304BD9jFpLMp94jrBnns= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Liang Chen , Coly Li , Jens Axboe Subject: [PATCH 4.14 46/63] bcache: fix a race between cache register and cacheset unregister Date: Mon, 20 May 2019 14:14:25 +0200 Message-Id: <20190520115236.148797379@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190520115231.137981521@linuxfoundation.org> References: <20190520115231.137981521@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: Liang Chen commit a4b732a248d12cbdb46999daf0bf288c011335eb upstream. There is a race between cache device register and cache set unregister. For an already registered cache device, register_bcache will call bch_is_open to iterate through all cachesets and check every cache there. The race occurs if cache_set_free executes at the same time and clears the caches right before ca is dereferenced in bch_is_open_cache. To close the race, let's make sure the clean up work is protected by the bch_register_lock as well. This issue can be reproduced as follows, while true; do echo /dev/XXX> /sys/fs/bcache/register ; done& while true; do echo 1> /sys/block/XXX/bcache/set/unregister ; done & and results in the following oops, [ +0.000053] BUG: unable to handle kernel NULL pointer dereference at 0000000000000998 [ +0.000457] #PF error: [normal kernel read fault] [ +0.000464] PGD 800000003ca9d067 P4D 800000003ca9d067 PUD 3ca9c067 PMD 0 [ +0.000388] Oops: 0000 [#1] SMP PTI [ +0.000269] CPU: 1 PID: 3266 Comm: bash Not tainted 5.0.0+ #6 [ +0.000346] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.11.0-2.fc28 04/01/2014 [ +0.000472] RIP: 0010:register_bcache+0x1829/0x1990 [bcache] [ +0.000344] Code: b0 48 83 e8 50 48 81 fa e0 e1 10 c0 0f 84 a9 00 00 00 48 89 c6 48 89 ca 0f b7 ba 54 04 00 00 4c 8b 82 60 0c 00 00 85 ff 74 2f <49> 3b a8 98 09 00 00 74 4e 44 8d 47 ff 31 ff 49 c1 e0 03 eb 0d [ +0.000839] RSP: 0018:ffff92ee804cbd88 EFLAGS: 00010202 [ +0.000328] RAX: ffffffffc010e190 RBX: ffff918b5c6b5000 RCX: ffff918b7d8e0000 [ +0.000399] RDX: ffff918b7d8e0000 RSI: ffffffffc010e190 RDI: 0000000000000001 [ +0.000398] RBP: ffff918b7d318340 R08: 0000000000000000 R09: ffffffffb9bd2d7a [ +0.000385] R10: ffff918b7eb253c0 R11: ffffb95980f51200 R12: ffffffffc010e1a0 [ +0.000411] R13: fffffffffffffff2 R14: 000000000000000b R15: ffff918b7e232620 [ +0.000384] FS: 00007f955bec2740(0000) GS:ffff918b7eb00000(0000) knlGS:0000000000000000 [ +0.000420] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ +0.000801] CR2: 0000000000000998 CR3: 000000003cad6000 CR4: 00000000001406e0 [ +0.000837] Call Trace: [ +0.000682] ? _cond_resched+0x10/0x20 [ +0.000691] ? __kmalloc+0x131/0x1b0 [ +0.000710] kernfs_fop_write+0xfa/0x170 [ +0.000733] __vfs_write+0x2e/0x190 [ +0.000688] ? inode_security+0x10/0x30 [ +0.000698] ? selinux_file_permission+0xd2/0x120 [ +0.000752] ? security_file_permission+0x2b/0x100 [ +0.000753] vfs_write+0xa8/0x1a0 [ +0.000676] ksys_write+0x4d/0xb0 [ +0.000699] do_syscall_64+0x3a/0xf0 [ +0.000692] entry_SYSCALL_64_after_hwframe+0x44/0xa9 Signed-off-by: Liang Chen Cc: stable@vger.kernel.org Signed-off-by: Coly Li Signed-off-by: Jens Axboe Signed-off-by: Greg Kroah-Hartman --- drivers/md/bcache/super.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) --- a/drivers/md/bcache/super.c +++ b/drivers/md/bcache/super.c @@ -1357,6 +1357,7 @@ static void cache_set_free(struct closur bch_btree_cache_free(c); bch_journal_free(c); + mutex_lock(&bch_register_lock); for_each_cache(ca, c, i) if (ca) { ca->set = NULL; @@ -1379,7 +1380,6 @@ static void cache_set_free(struct closur mempool_destroy(c->search); kfree(c->devices); - mutex_lock(&bch_register_lock); list_del(&c->list); mutex_unlock(&bch_register_lock);