From patchwork Fri Dec 15 16:29:56 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: David Lechner X-Patchwork-Id: 10115483 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 31D15602C2 for ; Fri, 15 Dec 2017 16:29:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 23A4029145 for ; Fri, 15 Dec 2017 16:29:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 16F8629169; Fri, 15 Dec 2017 16:29: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.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, 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 8A90329145 for ; Fri, 15 Dec 2017 16:29:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756733AbdLOQ3y (ORCPT ); Fri, 15 Dec 2017 11:29:54 -0500 Received: from vern.gendns.com ([206.190.152.46]:40549 "EHLO vern.gendns.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756697AbdLOQ3x (ORCPT ); Fri, 15 Dec 2017 11:29:53 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lechnology.com; s=default; h=Content-Transfer-Encoding:Content-Type: In-Reply-To:MIME-Version:Date:Message-ID:References:Cc:To:From:Subject:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=EXrmVnzyoLBtpYmT4dW6ZAAjXj2Jyfq/gWGgROEM3qI=; b=GZPmuMjiwfF7GlSo4kUWmwf7aN i6Lc3luc93xEUl8h6k6PwWxuEQqAJwwfy7Ne6tWargjqA6ms6ymdz5q02Ubpiq42DpbjScQ9nr7tM CD7orXiGqHBN5M3W6+NeLGNVBAffUx9JpMNQ9A9CJsd1I2/kA3AgD93hawflDuT6qUfKCZ9KnoDE8 O5LqX2TDbYmYVd+ei1QdkW6HV8ZMmCw9ciiU5B6whLk7nIrC/rDDmuxkdKStXRtmoQQfkFnU4riL/ Lj4T9hNsppysW/qWSEJRj2FCK/EOvC2UrDPTUEvn1Xkvx66+zt3glizVzEJ0A7yV7yNLAIi0qxHIF 0mOxntgQ==; Received: from 108-198-5-147.lightspeed.okcbok.sbcglobal.net ([108.198.5.147]:52990 helo=[192.168.0.134]) by vern.gendns.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.89) (envelope-from ) id 1ePsq8-002SkV-3u; Fri, 15 Dec 2017 11:28:00 -0500 Subject: Re: [PATCH] clk: fix spin_lock/unlock imbalance on bad clk_enable() reentrancy From: David Lechner To: linux-clk@vger.kernel.org Cc: Michael Turquette , Stephen Boyd , linux-kernel@vger.kernel.org, Jerome Brunet References: <1513122223-14932-1-git-send-email-david@lechnology.com> <32ca585d-c51d-1c85-42ba-85f0b1df0a60@lechnology.com> Message-ID: Date: Fri, 15 Dec 2017 10:29:56 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.0 MIME-Version: 1.0 In-Reply-To: <32ca585d-c51d-1c85-42ba-85f0b1df0a60@lechnology.com> Content-Language: en-US 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 On 12/12/2017 10:14 PM, David Lechner wrote: > On 12/12/2017 05:43 PM, David Lechner wrote: >> 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). > > I think I have figured out that since CONFIG_SMP=n and > CONFIG_DEBUG_SPINLOCK=n on my kernel that > > #define arch_spin_trylock(lock)({ barrier(); (void)(lock); 1; }) > > in include/linux/spinlock_up.h is causing the problem. > > So, basically, reentrancy of clk_enable() is broken for non-SMP systems, > but I'm not sure I know how to fix it. > > Here is what I came up with for a fix. If it looks reasonable, I will resend as a proper patch. enable_refcnt++; __acquire(enable_lock); --- To unsubscribe from this list: send the line "unsubscribe linux-clk" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c index bb1b1f9..53fad5f 100644 --- a/drivers/clk/clk.c +++ b/drivers/clk/clk.c @@ -136,12 +136,23 @@ static void clk_prepare_unlock(void) mutex_unlock(&prepare_lock); } +#ifdef CONFIG_SMP +#define NO_SMP 0 +#else +#define NO_SMP 1 +#endif + static unsigned long clk_enable_lock(void) __acquires(enable_lock) { - unsigned long flags; + unsigned long flags = 0; - if (!spin_trylock_irqsave(&enable_lock, flags)) { + /* + * spin_trylock_irqsave() always returns true on non-SMP system (unless + * spin lock debugging is enabled) so don't call spin_trylock_irqsave() + * unless we are on an SMP system. + */ + if (NO_SMP || !spin_trylock_irqsave(&enable_lock, flags)) { if (enable_owner == current) {