From patchwork Tue Dec 12 23:43:43 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Lechner X-Patchwork-Id: 10108437 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 EC0C36032B for ; Tue, 12 Dec 2017 23:44:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DCB4328853 for ; Tue, 12 Dec 2017 23:44:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D0DF128C81; Tue, 12 Dec 2017 23:44:06 +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_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable 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 851E528853 for ; Tue, 12 Dec 2017 23:44:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752863AbdLLXn4 (ORCPT ); Tue, 12 Dec 2017 18:43:56 -0500 Received: from vern.gendns.com ([206.190.152.46]:51447 "EHLO vern.gendns.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752766AbdLLXnu (ORCPT ); Tue, 12 Dec 2017 18:43:50 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lechnology.com; s=default; h=Message-Id:Date:Subject:Cc:To:From:Sender: Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=441UUyyozidtxd53POCY6E/RvlLiDVNpZoQLmYirkj8=; b=dkANDp12uC7NCg+H1FvdrEnxCa IFxvFpsNx/ncZuOY1XEt86H4Zkx7Btonk3cohiGilzAyRHln6zIfJRNr0DA1D2I6MTm8QO1CMXz8i ApOAB5OgOy4JIIpxY/DLyzsu5s0y65Lfg7z8rxDan0hiwaAPleNHDmp61u64PEss2LrIKZLt552yA qQRqhMU21CERkzhBUcE3NHWHlOutPltF5JEb+wncpS8RUthMni1AwC8UHULrx8MtUboMu4sxFS/eH lpkvh9cJWmZaO/9eRYl85fMy4StGdhI14ezmqMAAtTlyRI9JdleF1t/MbgMOvk6JRi2wFYI2g2KIG zuC943ZA==; Received: from 108-198-5-147.lightspeed.okcbok.sbcglobal.net ([108.198.5.147]:48362 helo=freyr.lechnology.com) by vern.gendns.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES128-SHA256:128) (Exim 4.89) (envelope-from ) id 1eOuBU-001pIK-LU; Tue, 12 Dec 2017 18:42:00 -0500 From: David Lechner To: linux-clk@vger.kernel.org Cc: David Lechner , Michael Turquette , Stephen Boyd , linux-kernel@vger.kernel.org Subject: [PATCH] clk: fix spin_lock/unlock imbalance on bad clk_enable() reentrancy Date: Tue, 12 Dec 2017 17:43:43 -0600 Message-Id: <1513122223-14932-1-git-send-email-david@lechnology.com> X-Mailer: git-send-email 2.7.4 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - vern.gendns.com X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - lechnology.com X-Get-Message-Sender-Via: vern.gendns.com: authenticated_id: davidmain+lechnology.com/only user confirmed/virtual account not confirmed X-Authenticated-Sender: vern.gendns.com: davidmain@lechnology.com X-Source: X-Source-Args: X-Source-Dir: Sender: linux-clk-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-clk@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP If clk_enable() is called in reentrant way and spin_trylock_irqsave() is not working as expected, it is possible to get a negative enable_refcnt which results in a missed call to spin_unlock_irqrestore(). It works like this: 1. clk_enable() is called. 2. clk_enable_unlock() calls spin_trylock_irqsave() and sets enable_refcnt = 1. 3. Another clk_enable() is called before the first has returned (reentrant), but somehow spin_trylock_irqsave() is returning true. (I'm not sure how/why this is happening yet, but it is happening to me with arch/arm/mach-davinci clocks that I am working on). 4. Because spin_trylock_irqsave() returned true, enable_lock has been locked twice without being unlocked and enable_refcnt = 1 is called instead of enable_refcnt++. 5. After the inner clock is enabled clk_enable_unlock() is called which decrements enable_refnct to 0 and calls spin_unlock_irqrestore() 6. The inner clk_enable() function returns. 7. clk_enable_unlock() is called again for the outer clock. enable_refcnt is decremented to -1 and spin_unlock_irqrestore() is *not* called. 8. The outer clk_enable() function returns. 9. Unrelated code called later issues a BUG warning about sleeping in an atomic context because of the unbalanced calls for the spin lock. This patch fixes the problem of unbalanced calls by calling spin_unlock_irqrestore() if enable_refnct <= 0 instead of just checking if it is == 0. The BUG warning about sleeping in an atomic context in the unrelated code is eliminated with this patch, but there are still warnings printed from clk_enable_unlock() and clk_enable_unlock() because of the reference counting problems. Signed-off-by: David Lechner --- drivers/clk/clk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c index 647d056..bb1b1f9 100644 --- a/drivers/clk/clk.c +++ b/drivers/clk/clk.c @@ -162,7 +162,7 @@ static void clk_enable_unlock(unsigned long flags) WARN_ON_ONCE(enable_owner != current); WARN_ON_ONCE(enable_refcnt == 0); - if (--enable_refcnt) { + if (--enable_refcnt > 0) { __release(enable_lock); return; }