From patchwork Mon Mar 12 22:51:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Hutchings X-Patchwork-Id: 10277627 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 A80B3602BD for ; Mon, 12 Mar 2018 22:51:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 993B028C69 for ; Mon, 12 Mar 2018 22:51:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8CE5328DA6; Mon, 12 Mar 2018 22:51:55 +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.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI 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 07BCC28C69 for ; Mon, 12 Mar 2018 22:51:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751451AbeCLWvy (ORCPT ); Mon, 12 Mar 2018 18:51:54 -0400 Received: from imap1.codethink.co.uk ([176.9.8.82]:56332 "EHLO imap1.codethink.co.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751327AbeCLWvx (ORCPT ); Mon, 12 Mar 2018 18:51:53 -0400 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126] helo=xylophone.i.decadent.org.uk) by imap1.codethink.co.uk with esmtpsa (Exim 4.84_2 #1 (Debian)) id 1evWIO-0000eR-7d for ; Mon, 12 Mar 2018 22:51:56 +0000 Date: Mon, 12 Mar 2018 22:51:48 +0000 From: Ben Hutchings To: linux-input@vger.kernel.org Subject: [PATCH] input: matrix_keypad: Prevent screaming interrupts on close or suspend Message-ID: <20180312225147.okm6w3gzaq7yt6ur@xylophone.i.decadent.org.uk> MIME-Version: 1.0 Content-Disposition: inline User-Agent: NeoMutt/20170113 (1.7.2) Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP matrix_keypad_stop() now serialises with matrix_keypad_interrupt() using spin_lock_irq() while setting the "stopped" flag, but it drops the lock before disabling the interrupt sources. If an interrupt is asserted at this point, matrix_keypad_interrupt() will ignore it because stopped is true. The interrupt will remain asserted and it may be called repeatedly, potentially leading to a hard lockup on a UP system. Instead, matrix_keypad_stop() should disable the interrupt sources (asynchronously) while still holding the lock. However, we have to take care because at this point they may already be disabled due to a pending scan. To avoid mismatched enable/disable calls, we should maintain the invariant that they are enabled if and only if !(scan_pending || stopped). Therefore: - Set the scan_pending flag in matrix_keypad_start(), since it does schedule a scan and doesn't enable interrupts - Only disable interrupts in matrix_keypad_stop() if scan_pending is false - Only enable interrupts in matrix_keypad_scan() if stopped is false Cc: stable@vger.kernel.org Signed-off-by: Ben Hutchings --- This is untested and completely based on code review. It's possible that the problem I identified can't really happen for reasons that aren't obvious to me. Ben. drivers/input/keyboard/matrix_keypad.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/drivers/input/keyboard/matrix_keypad.c b/drivers/input/keyboard/matrix_keypad.c index 41614c185918..e765950d3491 100644 --- a/drivers/input/keyboard/matrix_keypad.c +++ b/drivers/input/keyboard/matrix_keypad.c @@ -169,7 +169,8 @@ static void matrix_keypad_scan(struct work_struct *work) /* Enable IRQs again */ spin_lock_irq(&keypad->lock); keypad->scan_pending = false; - enable_row_irqs(keypad); + if (!keypad->stopped) + enable_row_irqs(keypad); spin_unlock_irq(&keypad->lock); } @@ -203,6 +204,7 @@ static int matrix_keypad_start(struct input_dev *dev) struct matrix_keypad *keypad = input_get_drvdata(dev); keypad->stopped = false; + keypad->scan_pending = true; mb(); /* @@ -220,14 +222,16 @@ static void matrix_keypad_stop(struct input_dev *dev) spin_lock_irq(&keypad->lock); keypad->stopped = true; + if (!keypad->scan_pending) { + /* + * matrix_keypad_scan() will leave IRQs enabled; + * we should disable them now. + */ + disable_row_irqs(keypad); + } spin_unlock_irq(&keypad->lock); flush_work(&keypad->work.work); - /* - * matrix_keypad_scan() will leave IRQs enabled; - * we should disable them now. - */ - disable_row_irqs(keypad); } #ifdef CONFIG_PM_SLEEP