Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp5645900img; Wed, 27 Mar 2019 12:21:14 -0700 (PDT) X-Google-Smtp-Source: APXvYqzK2a4mfW6VGqkewOZwB7XsXkC508MEg6LUqeXiWOFcanHGll7/q0IUTyFc3vB70LyPvhL5 X-Received: by 2002:a63:943:: with SMTP id 64mr17483954pgj.448.1553714474368; Wed, 27 Mar 2019 12:21:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553714474; cv=none; d=google.com; s=arc-20160816; b=mai2j6JKE3vqn2ep00zik6Fv869X1InC542FP9P4mCkvo5hvAZzE4EwXYJKe/tAdO8 4p/bl5tExA0omZcTo6TqsBdoBOi9A3AJIITZAw7G0dSue/Pc9tiFw6gPXECX/nOIdzOY tcoSnKixkb7VIUl8ZVpGfROC7RjfnmBlKWwbpi0AYMaZ3MN3Gw9nO0ASE6WYHIppjs+H ssdjhWrPVcNwrXerTauk5jrpyfcKUerR9aEaUkhVwgEKWtIINLwC9tzaVGuzvhF6JN7p IWipgOcicDBUD0KqiBejmomiNKGvi4gQSsiL57w/iynmzd9v6cZgwnZwOOM+H2CyRd3Y LOdA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=E1CRgxKn/Ac4q0BhOS+1VYn7dwa14mMFc11YAJW9S58=; b=yjDkE/qsGSHzjtH5UKJthX1K0lJYQXyGEOOsSmK/z1N/9llxY98MGhkCn7CI3D3lK0 pLWXyPM7XY9a5jy8DhEJq/XmZu/XsO4jXJ1Y2plRyrxyNbbXolsxc4ZoIB3IkAxEU93N RVc34hMwew2HdqvdkCnJCmwNDT6AUFdMFpXjnpBNPJ4bmU4zwYZg94FeUq9ZueBIgSBg mkPjuidf4XzSxBAeOJWB1XG1jM7jvypn6dwLRoQteepTltDGn7VmpiaJO53oTX+xYeiv 2Si/L8rD7paHObk6KwK3pUm8VQiXa8HmPoX9Hr2khsQqPkqzMafK7yaqZzvXeLmW6KJb AoeQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=CWJKVbHt; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j16si19070060pfe.152.2019.03.27.12.20.59; Wed, 27 Mar 2019 12:21:14 -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=CWJKVbHt; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388392AbfC0TUV (ORCPT + 99 others); Wed, 27 Mar 2019 15:20:21 -0400 Received: from mail.kernel.org ([198.145.29.99]:49164 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731740AbfC0SHV (ORCPT ); Wed, 27 Mar 2019 14:07:21 -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 4620E21734; Wed, 27 Mar 2019 18:07:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553710039; bh=ILWUV+WkoJGEKH1nu4EKtUh0n5zRY5W6K6rX3S1TNZ0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CWJKVbHt3lQzMr9HUNzg7g2x9gBu26ykHDEdyFYqVDe7GWoz0iRKgdslUErvUtA3v mSqMXw9Efby6Faus0TAGTI4dsAXUsMNE7Abx4e0f1ft+Fcq8b91LgFglXuNqEtwJQS vLCuBQEkuFEkRyU0KnnzA2uQ30q5Cd+zW8pA1ASs= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Guenter Roeck , Jens Axboe , Sasha Levin Subject: [PATCH AUTOSEL 5.0 169/262] cdrom: Fix race condition in cdrom_sysctl_register Date: Wed, 27 Mar 2019 14:00:24 -0400 Message-Id: <20190327180158.10245-169-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190327180158.10245-1-sashal@kernel.org> References: <20190327180158.10245-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Guenter Roeck [ Upstream commit f25191bb322dec8fa2979ecb8235643aa42470e1 ] The following traceback is sometimes seen when booting an image in qemu: [ 54.608293] cdrom: Uniform CD-ROM driver Revision: 3.20 [ 54.611085] Fusion MPT base driver 3.04.20 [ 54.611877] Copyright (c) 1999-2008 LSI Corporation [ 54.616234] Fusion MPT SAS Host driver 3.04.20 [ 54.635139] sysctl duplicate entry: /dev/cdrom//info [ 54.639578] CPU: 0 PID: 266 Comm: kworker/u4:5 Not tainted 5.0.0-rc5 #1 [ 54.639578] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015 [ 54.641273] Workqueue: events_unbound async_run_entry_fn [ 54.641273] Call Trace: [ 54.641273] dump_stack+0x67/0x90 [ 54.641273] __register_sysctl_table+0x50b/0x570 [ 54.641273] ? rcu_read_lock_sched_held+0x6f/0x80 [ 54.641273] ? kmem_cache_alloc_trace+0x1c7/0x1f0 [ 54.646814] __register_sysctl_paths+0x1c8/0x1f0 [ 54.646814] cdrom_sysctl_register.part.7+0xc/0x5f [ 54.646814] register_cdrom.cold.24+0x2a/0x33 [ 54.646814] sr_probe+0x4bd/0x580 [ 54.646814] ? __driver_attach+0xd0/0xd0 [ 54.646814] really_probe+0xd6/0x260 [ 54.646814] ? __driver_attach+0xd0/0xd0 [ 54.646814] driver_probe_device+0x4a/0xb0 [ 54.646814] ? __driver_attach+0xd0/0xd0 [ 54.646814] bus_for_each_drv+0x73/0xc0 [ 54.646814] __device_attach+0xd6/0x130 [ 54.646814] bus_probe_device+0x9a/0xb0 [ 54.646814] device_add+0x40c/0x670 [ 54.646814] ? __pm_runtime_resume+0x4f/0x80 [ 54.646814] scsi_sysfs_add_sdev+0x81/0x290 [ 54.646814] scsi_probe_and_add_lun+0x888/0xc00 [ 54.646814] ? scsi_autopm_get_host+0x21/0x40 [ 54.646814] __scsi_add_device+0x116/0x130 [ 54.646814] ata_scsi_scan_host+0x93/0x1c0 [ 54.646814] async_run_entry_fn+0x34/0x100 [ 54.646814] process_one_work+0x237/0x5e0 [ 54.646814] worker_thread+0x37/0x380 [ 54.646814] ? rescuer_thread+0x360/0x360 [ 54.646814] kthread+0x118/0x130 [ 54.646814] ? kthread_create_on_node+0x60/0x60 [ 54.646814] ret_from_fork+0x3a/0x50 The only sensible explanation is that cdrom_sysctl_register() is called twice, once from the module init function and once from register_cdrom(). cdrom_sysctl_register() is not mutex protected and may happily execute twice if the second call is made before the first call is complete. Use a static atomic to ensure that the function is executed exactly once. Signed-off-by: Guenter Roeck Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- drivers/cdrom/cdrom.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/drivers/cdrom/cdrom.c b/drivers/cdrom/cdrom.c index 614ecdbb4ab7..933268b8d6a5 100644 --- a/drivers/cdrom/cdrom.c +++ b/drivers/cdrom/cdrom.c @@ -265,6 +265,7 @@ /* #define ERRLOGMASK (CD_WARNING|CD_OPEN|CD_COUNT_TRACKS|CD_CLOSE) */ /* #define ERRLOGMASK (CD_WARNING|CD_REG_UNREG|CD_DO_IOCTL|CD_OPEN|CD_CLOSE|CD_COUNT_TRACKS) */ +#include #include #include #include @@ -3692,9 +3693,9 @@ static struct ctl_table_header *cdrom_sysctl_header; static void cdrom_sysctl_register(void) { - static int initialized; + static atomic_t initialized = ATOMIC_INIT(0); - if (initialized == 1) + if (!atomic_add_unless(&initialized, 1, 1)) return; cdrom_sysctl_header = register_sysctl_table(cdrom_root_table); @@ -3705,8 +3706,6 @@ static void cdrom_sysctl_register(void) cdrom_sysctl_settings.debug = debug; cdrom_sysctl_settings.lock = lockdoor; cdrom_sysctl_settings.check = check_media_type; - - initialized = 1; } static void cdrom_sysctl_unregister(void) -- 2.19.1