Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp5613239img; Wed, 27 Mar 2019 11:43:15 -0700 (PDT) X-Google-Smtp-Source: APXvYqxoLqD97l/cwaJh4etNDJ/VrJbFdL0elnK4sWFyZhYmOUpd3CDuvvS5mnJM+Gb0Lhtgcyhw X-Received: by 2002:a62:469a:: with SMTP id o26mr37925849pfi.251.1553712195026; Wed, 27 Mar 2019 11:43:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553712195; cv=none; d=google.com; s=arc-20160816; b=t/MHoKxZH1Z7P/X8MIuisxa2CNqbksQqhcN3Iw6/3IIP6Cf/UujuIYpVQ1w4sKaXN1 88wObbIgcHGEsilyQyl1vCGoUPCuC8c7WrNHVbG7vWuo2tVaBPYlH+FDGlFk5InItZWl BAkbGUAqId2EXSKxMGKzWOvOJKREmS2/ppqUn+lqVVRW4SRveNzjUj/Q7y1DoNLB1H8k EiZQy/DdyUHJ6P0XyUI6fUIe7uHTJNTemJD7ujVHtjJeozW+4iwcYo9AnOT6PBAXWk5F ZgohXuvrP7Ynyq0qqvRk2argakK3nncGj+SHi+6Rf0SB/DbgEwGzRhOhDa/rReTlNsvw AzSA== 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=YgaQ+H3a8P40BV4GPgjkSoyhMuD3blPKp28kLGXmhV4=; b=uuKlZBTNsBooQfmfsxUXLzx6fSEtNREFOjvnCEkq0J5Gy2W7Ew+BG/LLuSHTt8/jxP LFAfUCKmTNZ8anNH8eYoidG8Fcb8zsxczCwxycfmTvGy/xq8vH9t/Glm2OiSG3V5Bvrx CWC+d7ATdZ+gxIQgEyuQpbSjZ0jmntTSq9qfv2tycjvnnH4LLHtCrLVJ0g66oShcUlOz DP1vvf70k3B34mURvQVCSAek9t7kYS2hsISJgogmxTwceVvFTLxoHhpaHksqPPDjwX16 r9/DZXD71Z5j65/L5/uH6m782w24jhMDEuwiOx+m5/xRNQPyXHhPb00zJ0jLcsNPMVAV 2CzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Ix59AXUW; 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 t22si19720699plo.79.2019.03.27.11.42.59; Wed, 27 Mar 2019 11:43:15 -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=Ix59AXUW; 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 S2403915AbfC0SlZ (ORCPT + 99 others); Wed, 27 Mar 2019 14:41:25 -0400 Received: from mail.kernel.org ([198.145.29.99]:40576 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390786AbfC0SW0 (ORCPT ); Wed, 27 Mar 2019 14:22:26 -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 EDFE12087C; Wed, 27 Mar 2019 18:22:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553710945; bh=jFfQX+Birf7ZZRjJAns+P1obvaM3uRrroI57ojy9gB0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ix59AXUWDWMH7Qfa3Va0z9quMDwJk9D1MsLM4xB+ntTPTtBU5ovvVj7U0SHjDbdWI NIxwD3fa/KH+NlgT4Yoo4DCUXASM52MuxLwIUtDr7NCk+XsHfoxe7PhSZ1o1CVj6DP cAA3Hf5kt1hZCa9yakS3l/oTgR2G3ZsljqSaMdUI= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Guenter Roeck , Jens Axboe , Sasha Levin Subject: [PATCH AUTOSEL 4.9 58/87] cdrom: Fix race condition in cdrom_sysctl_register Date: Wed, 27 Mar 2019 14:20:11 -0400 Message-Id: <20190327182040.17444-58-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190327182040.17444-1-sashal@kernel.org> References: <20190327182040.17444-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 ff4280800cd0..a46f188f679e 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 @@ -3683,9 +3684,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); @@ -3696,8 +3697,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