From patchwork Thu Jun 28 07:54:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Vaittinen, Matti" X-Patchwork-Id: 10493245 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 3557B60230 for ; Thu, 28 Jun 2018 07:55:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1EFC228A34 for ; Thu, 28 Jun 2018 07:55:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 12BCD29F58; Thu, 28 Jun 2018 07:55:32 +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=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 7FAEE28A34 for ; Thu, 28 Jun 2018 07:55:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Subject:To:From :Date:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=5KnBBYxuGjSGN51o+pENuRkaexp0au0huVmwOtpL6s8=; b=evyL9mGEorx1bL pPKniIehUkDrsEkwKae3vR49zdx2si5YlFqsbugoV942Z6VvH9/kF5/t2w15fwEvDP1AkJZpzy+rl H2w0BEgqiqO+a6atFjaOsNnlrK5nER9wBsUh26lxZPvQjdZHDTH2q5rUrI5z7L9dgh87wYp+NgUDL sTyFH2jWXF2hHkA5F9Tt7hP41/mqTNXRyiRMTEiqvC/+ntsPL75OMOfzXTKqI5ClJa5YobLYh7IC8 4qb8LoRva0MJVHqD+NWjr/wEKHwV/ptqPeUU1rx47GCJ6H3nIzADf+/m/3uKqADyi9r/ccAfwq3TO E0bmvOJdCEpwain6/M9Q==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1fYRm0-0003YX-0c; Thu, 28 Jun 2018 07:55:24 +0000 Received: from mail-lj1-f194.google.com ([209.85.208.194]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fYRlv-0002gj-KO for linux-arm-kernel@lists.infradead.org; Thu, 28 Jun 2018 07:55:22 +0000 Received: by mail-lj1-f194.google.com with SMTP id t7-v6so3702719ljj.6 for ; Thu, 28 Jun 2018 00:55:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=bq0ri43tkI1v4pJyBVVvjGBURnL+75EpmDcRjTTfbEs=; b=DmbIoZUOJyuRhed/k9z3hCyAFeW6Y3yZnbF3v0rm6ofo8d4DRHXMH0MovkEo4h49aT 6HgvQI3aQUGpEg57AQtancmQCUkrojaZYuf/5fVN3SVKVICRme2w+TqCCWgk+nE8jSMA hN39BNssZvvgSveNHYvmbZ0f2PNdCmTzA8DT5VI/Vs+aAM0gCkgFWCCRKmNMx6Kipr/8 1Xus2mkzWSpwHYKd2k3iEQzymQ3sO/IzFBAPdIh+wsy1g+9/V74OmQtWZB02PdFogbAU vV+vLU9oQRSLlPx/3STJh1L9C7d0u3MtZbj639RfjXPnXddGPMEAnETQoVnUdTGBH4IO l+jg== X-Gm-Message-State: APt69E07ZXB7IbCUj/szqOaJ3hDSi44yI1YERVYBfm2rjCJvcOyiV4jp LFcqImRxaE3qeEsgKkrxWqU= X-Google-Smtp-Source: AAOMgpfzXLh+oekNRlle4rOfpxu9xIJAH+TQgh2Mk9QqxyeRT7StJ4fRFXZOkgcEp5bXW3fUwBVUuQ== X-Received: by 2002:a2e:99c7:: with SMTP id l7-v6mr6717833ljj.148.1530172506417; Thu, 28 Jun 2018 00:55:06 -0700 (PDT) Received: from localhost.localdomain ([213.255.186.46]) by smtp.gmail.com with ESMTPSA id y5-v6sm1165862lfy.94.2018.06.28.00.55.05 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 28 Jun 2018 00:55:05 -0700 (PDT) Date: Thu, 28 Jun 2018 10:54:53 +0300 From: Matti Vaittinen To: linux@armlinux.org.uk, sboyd@codeaurora.org, matti.vaittinen@fi.rohmeurope.com, mazziesaccount@gmail.com Subject: [PATCH] clk: clkdev - add managed versions of lookup registrations Message-ID: <20180628075453.GA7793@localhost.localdomain> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.9.2 (2017-12-15) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180628_005519_766883_5F3D7F1E X-CRM114-Status: GOOD ( 20.04 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-clk@vger.kernel.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Add devm_clk_hw_register_clkdev, devm_clk_register_clkdev and devm_clk_release_clkdev as a first styep to clean up drivers which are leaking clkdev lookups at driver remove. Signed-off-by: Matti Vaittinen --- While searching for example on how clk drivers release clkdev at exit I found that many of those don't. Simple grep for clkdev under drivers/clk suggest that bunch of drivers which call clk_register_clkdev or clk_hw_register_clkdev never call clkdev_drop. In order to help cleaning this up I decided to add devm versions of clk_register_clkdev and clk_hw_register_clkdev. I hope this would allow simply converting bunch of calls to clk_register_clkdev and clk_hw_register_clkdev into managed versions without building further clean up logic. Please review this carefully. I have only performed some simple tests with my BD71837 driver. And as I have only limited understanding on clkdev lookups I may have done mistakes. Thanks! drivers/clk/clkdev.c | 165 +++++++++++++++++++++++++++++++++++++++++-------- include/linux/clkdev.h | 8 +++ 2 files changed, 147 insertions(+), 26 deletions(-) diff --git a/drivers/clk/clkdev.c b/drivers/clk/clkdev.c index 7513411140b6..4752a0004a6c 100644 --- a/drivers/clk/clkdev.c +++ b/drivers/clk/clkdev.c @@ -390,7 +390,7 @@ void clkdev_drop(struct clk_lookup *cl) } EXPORT_SYMBOL(clkdev_drop); -static struct clk_lookup *__clk_register_clkdev(struct clk_hw *hw, +static struct clk_lookup *do_clk_register_clkdev(struct clk_hw *hw, const char *con_id, const char *dev_id, ...) { @@ -404,6 +404,24 @@ static struct clk_lookup *__clk_register_clkdev(struct clk_hw *hw, return cl; } +static struct clk_lookup *__clk_register_clkdev(struct clk_hw *hw, + const char *con_id, const char *dev_id) +{ + struct clk_lookup *cl; + + /* + * Since dev_id can be NULL, and NULL is handled specially, we must + * pass it as either a NULL format string, or with "%s". + */ + if (dev_id) + cl = do_clk_register_clkdev(hw, con_id, "%s", + dev_id); + else + cl = do_clk_register_clkdev(hw, con_id, NULL); + + return cl; +} + /** * clk_register_clkdev - register one clock lookup for a struct clk * @clk: struct clk to associate with all clk_lookups @@ -421,22 +439,18 @@ static struct clk_lookup *__clk_register_clkdev(struct clk_hw *hw, int clk_register_clkdev(struct clk *clk, const char *con_id, const char *dev_id) { - struct clk_lookup *cl; - - if (IS_ERR(clk)) - return PTR_ERR(clk); + int rval = 0; - /* - * Since dev_id can be NULL, and NULL is handled specially, we must - * pass it as either a NULL format string, or with "%s". - */ - if (dev_id) - cl = __clk_register_clkdev(__clk_get_hw(clk), con_id, "%s", - dev_id); - else - cl = __clk_register_clkdev(__clk_get_hw(clk), con_id, NULL); + if (IS_ERR(clk)) { + rval = PTR_ERR(clk); + } else { + struct clk_lookup *cl; - return cl ? 0 : -ENOMEM; + cl = __clk_register_clkdev(__clk_get_hw(clk), con_id, dev_id); + if (!cl) + rval = -ENOMEM; + } + return rval; } EXPORT_SYMBOL(clk_register_clkdev); @@ -456,21 +470,120 @@ EXPORT_SYMBOL(clk_register_clkdev); */ int clk_hw_register_clkdev(struct clk_hw *hw, const char *con_id, const char *dev_id) +{ + int rval = 0; + + if (IS_ERR(hw)) { + rval = PTR_ERR(hw); + } else { + struct clk_lookup *cl; + + cl = __clk_register_clkdev(hw, con_id, dev_id); + if (!cl) + rval = -ENOMEM; + } + + return rval; +} +EXPORT_SYMBOL(clk_hw_register_clkdev); + +static void devm_clkdev_release(struct device *dev, void *res) +{ + clkdev_drop(*(struct clk_lookup **)res); +} + +static int devm_clk_match_clkdev(struct device *dev, void *res, void *data) +{ + struct clk_lookup **l = res; + + if (!l || !*l) { + WARN_ON(!l || !*l); + return 0; + } + + return *l == data; +} + +/** + * devm_clk_release_clkdev - Resource managed clkdev lookup release + * @dev: device this lookup is bound + * @con_id: connection ID string on device + * @dev_id: format string describing device name + * + * Drop the clkdev lookup created with devm_clk_hw_register_clkdev or + * with devm_clk_register_clkdev. Normally this function will not need to be + * called and the resource management code will ensure that the resource is + * freed. + */ +void devm_clk_release_clkdev(struct device *dev, const char *con_id, + const char *dev_id) { struct clk_lookup *cl; + int rval; + + cl = clk_find(dev_id, con_id); + WARN_ON(!cl); + rval = devres_release(dev, devm_clkdev_release, + &devm_clk_match_clkdev, cl); + WARN_ON(rval); +} +EXPORT_SYMBOL(devm_clk_release_clkdev); + +/** + * devm_clk_hw_register_clkdev - managed clk lookup registration for clk_hw + * @dev: device this lookup is bound + * @hw: struct clk_hw to associate with all clk_lookups + * @con_id: connection ID string on device + * @dev_id: format string describing device name + * + * con_id or dev_id may be NULL as a wildcard, just as in the rest of + * clkdev. + * + * To make things easier for mass registration, we detect error clk_hws + * from a previous clk_hw_register_*() call, and return the error code for + * those. This is to permit this function to be called immediately + * after clk_hw_register_*(). + */ +int devm_clk_hw_register_clkdev(struct device *dev, struct clk_hw *hw, + const char *con_id, const char *dev_id) +{ + struct clk_lookup **cl = NULL; if (IS_ERR(hw)) return PTR_ERR(hw); + cl = devres_alloc(devm_clkdev_release, sizeof(*cl), GFP_KERNEL); + if (cl) { + *cl = __clk_register_clkdev(hw, con_id, dev_id); + if (*cl) + devres_add(dev, cl); + else + devres_free(cl); + } + return (cl && *cl) ? 0 : -ENOMEM; +} +EXPORT_SYMBOL(devm_clk_hw_register_clkdev); - /* - * Since dev_id can be NULL, and NULL is handled specially, we must - * pass it as either a NULL format string, or with "%s". - */ - if (dev_id) - cl = __clk_register_clkdev(hw, con_id, "%s", dev_id); - else - cl = __clk_register_clkdev(hw, con_id, NULL); - - return cl ? 0 : -ENOMEM; +/** + * devm_clk_register_clkdev - managed clk lookup registration for a struct clk + * @dev: device this lookup is bound + * @clk: struct clk to associate with all clk_lookups + * @con_id: connection ID string on device + * @dev_id: string describing device name + * + * con_id or dev_id may be NULL as a wildcard, just as in the rest of + * clkdev. + * + * To make things easier for mass registration, we detect error clks + * from a previous clk_register() call, and return the error code for + * those. This is to permit this function to be called immediately + * after clk_register(). + */ +int devm_clk_register_clkdev(struct device *dev, struct clk *clk, + const char *con_id, const char *dev_id) +{ + if (IS_ERR(clk)) + return PTR_ERR(clk); + return devm_clk_hw_register_clkdev(dev, __clk_get_hw(clk), con_id, + dev_id); } -EXPORT_SYMBOL(clk_hw_register_clkdev); +EXPORT_SYMBOL(devm_clk_register_clkdev); diff --git a/include/linux/clkdev.h b/include/linux/clkdev.h index 4890ff033220..27ebeae8ae26 100644 --- a/include/linux/clkdev.h +++ b/include/linux/clkdev.h @@ -52,4 +52,12 @@ int clk_add_alias(const char *, const char *, const char *, struct device *); int clk_register_clkdev(struct clk *, const char *, const char *); int clk_hw_register_clkdev(struct clk_hw *, const char *, const char *); +int devm_clk_register_clkdev(struct device *dev, struct clk *clk, + const char *con_id, const char *dev_id); +int devm_clk_hw_register_clkdev(struct device *dev, struct clk_hw *hw, + const char *con_id, const char *dev_id); +void devm_clk_release_clkdev(struct device *dev, const char *con_id, + const char *dev_id); + + #endif