Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp998428yba; Thu, 4 Apr 2019 02:05:07 -0700 (PDT) X-Google-Smtp-Source: APXvYqzdhI2beC7bF91e9hCHLR5FJrvat6I6p+gDRULNBChxipe4TOq9z0JBfruRe35PwzsUHODi X-Received: by 2002:a63:5c53:: with SMTP id n19mr4494161pgm.193.1554368707480; Thu, 04 Apr 2019 02:05:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554368707; cv=none; d=google.com; s=arc-20160816; b=KT6XEQI1zgKxA8D2HarvxDC95cVvpe9LP4aMw7GqOE3yn99JJZ3XpRJxGz+61Zv1cg 4CLaYHzO0mLjeOZ0RsRaCgH6ZWmhUrnB9WRE29YkiKwMFDY9jh1HZrbDwsix19MbFZRt 58RXCrcNbZUawrKEyLvlREfVDCw5YKQK/iA09qGWJVcKAQ2nVOvkKws8VQylEht++l0r nW0ewf6Kte6vVANf9H99RLlTQmlF9cPBUzcVakaPLaC0IczwITo+BbMdnD75YpCYnRQ4 H66uZapBNFUMvXBqCTvua1sE7us74fOLy/WA1e5MK63/VUZRjwB8yFhVV8ScZ+0JdPaP XBPw== 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=Y2jhaV5AuakWdFZX7LBCBTdIwNc4bbvornCokhwDJXI=; b=BBGeVSM2Knl8cntkmRF/1xrXj8xrr0eEmHsP3IRYHJ7tI4ELK3wnnU6YWZvRHUXekf OSByH9SqejM4eMh8lna/fZMR8cfRnDsF5x80Rpe6WPOYX1jXcpFl8Bt48EfPvyt8QBQC dTp9kLLblJCLZI6r0gA69Ebja3CPtB1fSwlS8RqV4vlvuXBM7Uo8I3lzz23MdqyyL75j GUH+QhpaBe0hAFRA0Ek5mSPALkPKsaPwuLJAVnR5rnyEaJyGpYzF5RSrPVFSWRhallOp vpwyTfopeNc8LCAzrDJy6G7979C/xntcaiIU5uckLXt37nXzKzz6nqU53ec5+qoX8JL7 H7Yg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=2fJmJu3k; 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 f9si15396026pgq.347.2019.04.04.02.04.52; Thu, 04 Apr 2019 02:05:07 -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=2fJmJu3k; 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 S1731942AbfDDJEU (ORCPT + 99 others); Thu, 4 Apr 2019 05:04:20 -0400 Received: from mail.kernel.org ([198.145.29.99]:42048 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731930AbfDDJER (ORCPT ); Thu, 4 Apr 2019 05:04:17 -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 80F25214AF; Thu, 4 Apr 2019 09:04:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1554368657; bh=qlCaWlHWdO+5IwDamWz+jFzOBdpr7Mmnmvq9loXbQ/I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2fJmJu3kANu04fbwI1iN08qBJUCnInSm7bGmJ8LL7Ebrinde7NERv5GAS+ao2JqVZ d6JjgTrb10iQIiEnEC6vaU2SIxGYQiw8+BKoHYj5RR+OVx7gua+BYZguH0UHrvk0x/ YxEV/m8ldXG3zZToaX3e5u266pieHpuV8VdFjGpw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Guenter Roeck , Jens Axboe , Sasha Levin Subject: [PATCH 4.19 120/187] cdrom: Fix race condition in cdrom_sysctl_register Date: Thu, 4 Apr 2019 10:47:37 +0200 Message-Id: <20190404084608.894949775@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190404084603.119654039@linuxfoundation.org> References: <20190404084603.119654039@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore 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 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ [ 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 10802d1fc554..27a82a559ab9 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 @@ -3693,9 +3694,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); @@ -3706,8 +3707,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