Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp237862yba; Mon, 20 May 2019 07:56:45 -0700 (PDT) X-Google-Smtp-Source: APXvYqwAZ+mDHiMKpyFuZmB3Jt5zwz7qx0LkL1zPnlLtjr2+2naUgL3h7Sw76eZs/mL8RgD+YakG X-Received: by 2002:a63:1c16:: with SMTP id c22mr15185531pgc.333.1558364205547; Mon, 20 May 2019 07:56:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558364205; cv=none; d=google.com; s=arc-20160816; b=kB280A01TahFkGusOTuvMiL+9WdkG5rcnO5ZiFnAnaK20236bZ9wqjIPHacrNgqOAn N/hpHl35PQuGTQcQ4U4CYEO7rv68JiqtLD8NRoi1yUmIIqjYZtMrX1YmXMc7kkg+Dryf 1n95wA+5ZVFpUOaf5Tnjh92OA2BSFVTHbYMAqIJQtHFIVn97XyIr67r5jyICSpS45AE4 y3XlH/tkqVnz1VDf6VryvlNtJ+HyV+D21J7GQBI3/ZpWIiwbRA0yANcTutiVQrcspIcR 1tiSUQM/5OC6at3lysnN8ufyMlNmU1CnvuZCY0i2m94Jlj0mFTmKjqBy85VuOLbLOnGF hhNA== 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=Shk1ibS4Z6q8aLj3AU/h1TDnXvK7chgRKPwcGh50I5txO9pOYAH/KJeAYh5t9IKlcn oNpU0VZLMRGhPJeJnalJxOa62URK0CUtxL7mA2QeABI8LKJcomqstCCOm9HfalpecOIf x7X5umCCL4VxFms6snLFmBFwbrECHFAHoA9k6NENh7MDA4E+3PlW60cpCe8ueEWUnw9C 7G6yI65+fITO1iUTyhaRUHIbERvRnVm82+ERotk8NCgY45fbeUVC/3gU+vfprK96lvo1 kOw02H70oR+rL5MLuvZTRhVJtVXk34095020LC0Vsxj7XoXpa29YSbY0tzN3HnXamicw +huw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Xf4nYM3y; 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 15si12087229pfh.174.2019.05.20.07.56.30; Mon, 20 May 2019 07:56:45 -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=Xf4nYM3y; 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 S2387496AbfETMRQ (ORCPT + 99 others); Mon, 20 May 2019 08:17:16 -0400 Received: from mail.kernel.org ([198.145.29.99]:58102 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387480AbfETMRN (ORCPT ); Mon, 20 May 2019 08:17:13 -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 DC8B020815; Mon, 20 May 2019 12:17:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1558354632; bh=FShAbIvuo5Dxjc1FjFgVxxINTXvXvWkM+qxwB8esTp4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Xf4nYM3yXTtvRrmsaZ9E8pPtzcVF4WMG3ukllu4nPfPf0dyRqgBuuQjggYH79PDSF mZgk+5WEU4XjyeW8n40gCruYrCL5osm8We/m4Cc9gWlEkKePmN6+x3oEz7m9nVcAXj A8XTVh37SlLkj1i6cUTV/1/nHLcVBt3+qOes0AsQ= 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.9 31/44] bcache: fix a race between cache register and cacheset unregister Date: Mon, 20 May 2019 14:14:20 +0200 Message-Id: <20190520115234.879079375@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190520115230.720347034@linuxfoundation.org> References: <20190520115230.720347034@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);