From patchwork Sun Jul 31 11:56:15 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heiner Kallweit X-Patchwork-Id: 9253363 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 0210F6077C for ; Sun, 31 Jul 2016 11:56:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DB57728448 for ; Sun, 31 Jul 2016 11:56:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CFD122845D; Sun, 31 Jul 2016 11:56:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5130D28448 for ; Sun, 31 Jul 2016 11:56:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751980AbcGaL4Y (ORCPT ); Sun, 31 Jul 2016 07:56:24 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:36175 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750748AbcGaL4X (ORCPT ); Sun, 31 Jul 2016 07:56:23 -0400 Received: by mail-wm0-f65.google.com with SMTP id x83so22171318wma.3 for ; Sun, 31 Jul 2016 04:56:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:message-id:date:user-agent:mime-version :content-transfer-encoding; bh=QsCcG2CQvRNrFd/vYFg+I3b0BNL9tQR/Ae9M1MXTtDY=; b=E08IqyXczBV9AQ97DROv0xj4mjeafwlD2K5SuSiX5wUW1e1YASnHx6KDTib1ighzLF DavQXPidZ9jiod0PHmShiKRa5TzOA9sXXQoWwQ4oQFK1FnumsIVZ3jL9xoZqgpMA3LCz IpA6vfvIjPu4nnoI+8I3WUWsS8dIKb8AbmVHg7ffHvSEn7PCHv4LMPnuuLkSh2wEd3ud Qlm5xOmQCNyFtUtaddmPwoyBZnHvev90JsSzdLSO6x1XoEz5NY1WoO3WzbaPSboIOhNf DiXZu57Dx3ON9tB+ZQgpRzgVkDQw4X9mYj8HUtbWaZBrfpuZ3VfV8maZPqsF51PsaZZq oAMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:message-id:date:user-agent :mime-version:content-transfer-encoding; bh=QsCcG2CQvRNrFd/vYFg+I3b0BNL9tQR/Ae9M1MXTtDY=; b=a/r3xTdsRXMtW+GfnVt1oc3weCzNVDDWlG6h3l0hU5UsWuILNpXjytMAt1z8CnFrjp Owm1Zc0Wcj+dIxiT2zvf06M0/R5Pq5dc2ZaWGx7Iz5iPKxZiuwCsw1yax1OuC8zmeEiB 5J+duw5NT0PVhc0xdiOG8LX1mIXqTSW2/UZ5fU5SRE10s3SbytXGwvgG0XDZ1sBbY+17 Xz3+6/NjqxM5jeo1vzwGYomiG+CcXpwGxw724So4BE0e1IRBf9b7ZCa0WH3DRCU56KiO erjFv2UOFPefYR+9DHGg42B2Jy+YEqkt3C5Tce5vcepye5a8aYu4u54NN5Y44AERkueH nPKw== X-Gm-Message-State: AEkoouuPqgqqnwf2PDZ0bUbf8g4SJuGZBjSagtTk1xaKdkrCj4TCP3phkVpzl98A2Vm8Cg== X-Received: by 10.194.21.200 with SMTP id x8mr51425530wje.129.1469966180963; Sun, 31 Jul 2016 04:56:20 -0700 (PDT) Received: from ?IPv6:2003:62:5f01:4400:f9aa:5089:9399:c8e1? (p200300625F014400F9AA50899399C8E1.dip0.t-ipconnect.de. [2003:62:5f01:4400:f9aa:5089:9399:c8e1]) by smtp.googlemail.com with ESMTPSA id va3sm25365387wjb.18.2016.07.31.04.56.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 31 Jul 2016 04:56:20 -0700 (PDT) From: Heiner Kallweit To: Mauro Carvalho Chehab Cc: linux-media@vger.kernel.org Subject: [PATCH] media: rc: fix deadlock when module ir_lirc_codec is removed Message-ID: <376e679b-1506-e437-8945-9161dc309150@gmail.com> Date: Sun, 31 Jul 2016 13:56:15 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 MIME-Version: 1.0 Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When removing module ir_lirc_codec I got this deadlock warning. Fix this by introducing a separate mutex to protect access to available_protocols instead of using ir_raw_handler_lock for this purpose. ====================================================== [ INFO: possible circular locking dependency detected ] 4.7.0-next-20160729 #1 Not tainted ------------------------------------------------------- rmmod/2542 is trying to acquire lock: (&dev->lock){+.+.+.}, at: [] ir_raw_handler_unregister+0x77/0xd0 [rc_core] but task is already holding lock: (ir_raw_handler_lock){+.+.+.}, at: [] ir_raw_handler_unregister+0x22/0xd0 [rc_core] which lock already depends on the new lock. the existing dependency chain (in reverse order) is: -> #1 (ir_raw_handler_lock){+.+.+.}: [] lock_acquire+0xb2/0x1e0 [] mutex_lock_nested+0x5f/0x360 [] ir_raw_get_allowed_protocols+0x13/0x30 [rc_core] [] store_protocols+0x2fa/0x480 [rc_core] [] dev_attr_store+0x13/0x20 [] sysfs_kf_write+0x40/0x50 [] kernfs_fop_write+0x150/0x1e0 [] __vfs_write+0x23/0x120 [] vfs_write+0xb0/0x190 [] SyS_write+0x44/0xa0 [] entry_SYSCALL_64_fastpath+0x18/0xa8 -> #0 (&dev->lock){+.+.+.}: [] __lock_acquire+0x10fc/0x1270 [] lock_acquire+0xb2/0x1e0 [] mutex_lock_nested+0x5f/0x360 [] ir_raw_handler_unregister+0x77/0xd0 [rc_core] [] ir_lirc_codec_exit+0x10/0x12 [ir_lirc_codec] [] SyS_delete_module+0x168/0x220 [] entry_SYSCALL_64_fastpath+0x18/0xa8 other info that might help us debug this: Possible unsafe locking scenario: CPU0 CPU1 ---- ---- lock(ir_raw_handler_lock); lock(&dev->lock); lock(ir_raw_handler_lock); lock(&dev->lock); *** DEADLOCK *** 1 lock held by rmmod/2542: #0: (ir_raw_handler_lock){+.+.+.}, at: [] ir_raw_handler_unregister+0x22/0xd0 [rc_core] stack backtrace: CPU: 0 PID: 2542 Comm: rmmod Not tainted 4.7.0-next-20160729 #1 Hardware name: ZOTAC ZBOX-CI321NANO/ZBOX-CI321NANO, BIOS B246P105 06/01/2015 0000000000000000 ffff88006e607cc0 ffffffff812715f5 ffffffff8232b230 ffffffff8232b230 ffff88006e607d00 ffffffff810a846e 00000000790107f0 ffff880079010818 ffff8800790107f0 1efeb9f4f0dd2e6f ffff880079010000 Call Trace: [] dump_stack+0x68/0x93 [] print_circular_bug+0x1be/0x210 [] __lock_acquire+0x10fc/0x1270 [] ? debug_lockdep_rcu_enabled+0x1d/0x20 [] lock_acquire+0xb2/0x1e0 [] ? ir_raw_handler_unregister+0x77/0xd0 [rc_core] [] mutex_lock_nested+0x5f/0x360 [] ? ir_raw_handler_unregister+0x77/0xd0 [rc_core] [] ? trace_hardirqs_on_caller+0xee/0x1b0 [] ir_raw_handler_unregister+0x77/0xd0 [rc_core] [] ir_lirc_codec_exit+0x10/0x12 [ir_lirc_codec] [] SyS_delete_module+0x168/0x220 [] entry_SYSCALL_64_fastpath+0x18/0xa8 Signed-off-by: Heiner Kallweit --- drivers/media/rc/rc-ir-raw.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/drivers/media/rc/rc-ir-raw.c b/drivers/media/rc/rc-ir-raw.c index 144304c..205ecc6 100644 --- a/drivers/media/rc/rc-ir-raw.c +++ b/drivers/media/rc/rc-ir-raw.c @@ -26,6 +26,7 @@ static LIST_HEAD(ir_raw_client_list); /* Used to handle IR raw handler extensions */ static DEFINE_MUTEX(ir_raw_handler_lock); static LIST_HEAD(ir_raw_handler_list); +static DEFINE_MUTEX(available_protocols_lock); static u64 available_protocols; static int ir_raw_event_thread(void *data) @@ -234,9 +235,9 @@ u64 ir_raw_get_allowed_protocols(void) { u64 protocols; - mutex_lock(&ir_raw_handler_lock); + mutex_lock(&available_protocols_lock); protocols = available_protocols; - mutex_unlock(&ir_raw_handler_lock); + mutex_unlock(&available_protocols_lock); return protocols; } @@ -330,7 +331,9 @@ int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler) if (ir_raw_handler->raw_register) list_for_each_entry(raw, &ir_raw_client_list, list) ir_raw_handler->raw_register(raw->dev); + mutex_lock(&available_protocols_lock); available_protocols |= ir_raw_handler->protocols; + mutex_unlock(&available_protocols_lock); mutex_unlock(&ir_raw_handler_lock); return 0; @@ -349,7 +352,9 @@ void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler) if (ir_raw_handler->raw_unregister) ir_raw_handler->raw_unregister(raw->dev); } + mutex_lock(&available_protocols_lock); available_protocols &= ~protocols; + mutex_unlock(&available_protocols_lock); mutex_unlock(&ir_raw_handler_lock); } EXPORT_SYMBOL(ir_raw_handler_unregister);