From patchwork Fri Dec 22 18:42:50 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Lechner X-Patchwork-Id: 10130999 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 4CBEB6038F for ; Fri, 22 Dec 2017 18:42:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 43D2B282EC for ; Fri, 22 Dec 2017 18:42:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3896729989; Fri, 22 Dec 2017 18:42:45 +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 7D05C282EC for ; Fri, 22 Dec 2017 18:42:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755940AbdLVSmo (ORCPT ); Fri, 22 Dec 2017 13:42:44 -0500 Received: from vern.gendns.com ([206.190.152.46]:52029 "EHLO vern.gendns.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755458AbdLVSmn (ORCPT ); Fri, 22 Dec 2017 13:42:43 -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:From:References:Cc:To: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=T2YJzfIGh8o5GBCy0s16I3Bu9I7PMNSWRiJzjD2tjUo=; b=dTYL1vwVI6X0obu1WBP/J8dL8/ jH7a0JWDMnARbT5r2DbKUPbaqaOzklTPC4vq08zfkY5whYMOC+/qULLx/rAgx+0M+4dbYH7n/muMd vlCU2Kaw54ZvtJq9PNtTdInG0i6bFIk8IXxcHpYjkvDMbCjewI+i8GU6NeKHh4azPR5mn8njJSfR2 u3gRMMlbJUwIg3zQ2lHUZZDJijRbuZI4o5kJ6gGu6Nebj0Vuz9x831lyvjSDmV8pQAGKCu8ReQrO+ yjTM+zpguoUhJmsFbcabQTnsN/rFQ6ZMaPWwje+O6MKLmBnuojJ4tr+1VjFU6umB1gRftaxibH80Q v69m2teg==; Received: from 108-198-5-147.lightspeed.okcbok.sbcglobal.net ([108.198.5.147]:43638 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 1eSSFJ-001YU9-L0; Fri, 22 Dec 2017 13:40:37 -0500 Subject: Re: [PATCH] clk: fix spin_lock/unlock imbalance on bad clk_enable() reentrancy To: Stephen Boyd Cc: Michael Turquette , linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org, Jerome Brunet References: <1513122223-14932-1-git-send-email-david@lechnology.com> <32ca585d-c51d-1c85-42ba-85f0b1df0a60@lechnology.com> <151372258747.45969.10121622799666696251@resonance> <3ad110cf-95ba-0d0b-53ee-f1fa2d37d7fa@lechnology.com> <151379785862.37313.908268542576551305@resonance> <749e4759-3511-92d4-a19a-0f72c31b1ee6@lechnology.com> <20171220230009.GI7997@codeaurora.org> <20171222013915.GC7997@codeaurora.org> From: David Lechner Message-ID: <88c526ed-5f85-1a91-2a1d-59f9ac06559c@lechnology.com> Date: Fri, 22 Dec 2017 12:42:50 -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: <20171222013915.GC7997@codeaurora.org> 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/21/2017 07:39 PM, Stephen Boyd wrote: > On 12/20, Stephen Boyd wrote: >> On 12/20, David Lechner wrote: >>> On 12/20/2017 02:33 PM, David Lechner wrote: >>> >>> >>> So, the question I have is: what is the actual "correct" behavior of >>> spin_trylock_irqsave()? Is it really supposed to always return true >>> when CONFIG_DEBUG_SPINLOCK=n and CONFIG_SMP=n or is this a bug? >> >> Thanks for doing the analysis in this thread. >> >> When CONFIG_DEBUG_SPINLOCK=n and CONFIG_SMP=n, spinlocks are >> compiler barriers, that's it. So even if it is a bug to always >> return true, I fail to see how we can detect that a spinlock is >> already held in this configuration and return true or false. >> >> I suppose the best option is to make clk_enable_lock() and >> clk_enable_unlock() into nops or pure owner/refcount/barrier >> updates when CONFIG_SMP=n. We pretty much just need the barrier >> semantics when there's only a single CPU. >> > > How about this patch? It should make the trylock go away on UP > configs and then we keep everything else for refcount and > ownership. We would test enable_owner outside of any > irqs/preemption disabled section though. That needs a think. > > ---8<---- > diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c > index 3526bc068f30..b6f61367aa8d 100644 > --- a/drivers/clk/clk.c > +++ b/drivers/clk/clk.c > @@ -143,7 +143,8 @@ static unsigned long clk_enable_lock(void) > { > unsigned long flags; > > - if (!spin_trylock_irqsave(&enable_lock, flags)) { > + if (!IS_ENABLED(CONFIG_SMP) || > + !spin_trylock_irqsave(&enable_lock, flags)) { > if (enable_owner == current) { > enable_refcnt++; > __acquire(enable_lock); > > After sleeping on it, this is what I came up with. This keeps enable_owner and enable_refcnt protected and basically does the same thing that spin_lock_irqsave()/spin_unlock_irqrestore() would do on a UP system anyway, just more explicitly. --- -- 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..adbace3 100644 --- a/drivers/clk/clk.c +++ b/drivers/clk/clk.c @@ -136,6 +136,8 @@ static void clk_prepare_unlock(void) mutex_unlock(&prepare_lock); } +#ifdef CONFIG_SMP + static unsigned long clk_enable_lock(void) __acquires(enable_lock) { @@ -170,6 +172,43 @@ static void clk_enable_unlock(unsigned long flags) spin_unlock_irqrestore(&enable_lock, flags); } +#else + +static unsigned long clk_enable_lock(void) + __acquires(enable_lock) +{ + unsigned long flags; + + __acquire(enable_lock); + local_irq_save(flags); + preempt_disable(); + + if (enable_refcnt++ == 0) { + WARN_ON_ONCE(enable_owner != NULL); + enable_owner = current; + } else { + WARN_ON_ONCE(enable_owner != current); + } + + return flags; +} + +static void clk_enable_unlock(unsigned long flags) + __releases(enable_lock) +{ + WARN_ON_ONCE(enable_owner != current); + WARN_ON_ONCE(enable_refcnt == 0); + + if (--enable_refcnt == 0) + enable_owner = NULL; + + __release(enable_lock); + local_irq_restore(flags); + preempt_enable(); +} + +#endif + static bool clk_core_is_prepared(struct clk_core *core) { bool ret = false;