From patchwork Thu Jun 23 12:18:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Nuno Sa X-Patchwork-Id: 12892310 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E3F26C433EF for ; Thu, 23 Jun 2022 12:19:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230047AbiFWMTX (ORCPT ); Thu, 23 Jun 2022 08:19:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37304 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229967AbiFWMTW (ORCPT ); Thu, 23 Jun 2022 08:19:22 -0400 Received: from mx0a-00128a01.pphosted.com (mx0a-00128a01.pphosted.com [148.163.135.77]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E0CC31A6 for ; Thu, 23 Jun 2022 05:19:21 -0700 (PDT) Received: from pps.filterd (m0167089.ppops.net [127.0.0.1]) by mx0a-00128a01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 25N8x2s7024175; Thu, 23 Jun 2022 08:19:13 -0400 Received: from nwd2mta3.analog.com ([137.71.173.56]) by mx0a-00128a01.pphosted.com (PPS) with ESMTPS id 3gvemnb4tk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 23 Jun 2022 08:19:13 -0400 Received: from ASHBMBX8.ad.analog.com (ASHBMBX8.ad.analog.com [10.64.17.5]) by nwd2mta3.analog.com (8.14.7/8.14.7) with ESMTP id 25NCJCoR023750 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Thu, 23 Jun 2022 08:19:12 -0400 Received: from ASHBCASHYB5.ad.analog.com (10.64.17.133) by ASHBMBX8.ad.analog.com (10.64.17.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.14; Thu, 23 Jun 2022 08:19:11 -0400 Received: from ASHBMBX8.ad.analog.com (10.64.17.5) by ASHBCASHYB5.ad.analog.com (10.64.17.133) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.14; Thu, 23 Jun 2022 08:19:11 -0400 Received: from zeus.spd.analog.com (10.66.68.11) by ashbmbx8.ad.analog.com (10.64.17.5) with Microsoft SMTP Server id 15.2.986.14 via Frontend Transport; Thu, 23 Jun 2022 08:19:11 -0400 Received: from NSA-L01.ad.analog.com ([10.32.224.71]) by zeus.spd.analog.com (8.15.1/8.15.1) with ESMTP id 25NCJ2ap026429; Thu, 23 Jun 2022 08:19:08 -0400 From: =?utf-8?q?Nuno_S=C3=A1?= To: CC: Michael Turquette , Stephen Boyd Subject: [RESEND RFC PATCH v2 1/4] clk: clk-conf: properly release of nodes Date: Thu, 23 Jun 2022 14:18:54 +0200 Message-ID: <20220623121857.886-2-nuno.sa@analog.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220623121857.886-1-nuno.sa@analog.com> References: <20220623121857.886-1-nuno.sa@analog.com> MIME-Version: 1.0 X-ADIRuleOP-NewSCL: Rule Triggered X-Proofpoint-ORIG-GUID: R24LtQuwP9lIbvQsh97tFAJ75IXQKCBw X-Proofpoint-GUID: R24LtQuwP9lIbvQsh97tFAJ75IXQKCBw X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.883,Hydra:6.0.517,FMLib:17.11.122.1 definitions=2022-06-23_05,2022-06-23_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 mlxlogscore=627 malwarescore=0 impostorscore=0 lowpriorityscore=0 bulkscore=0 phishscore=0 adultscore=0 suspectscore=0 mlxscore=0 spamscore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2204290000 definitions=main-2206230050 Precedence: bulk List-ID: X-Mailing-List: linux-clk@vger.kernel.org We need to call 'of_node_put()' when we are done with the node or on error paths. Otherwise this can leak memory in DYNAMIC_OF setups. In order to make things easier to follow, an helper function was added to set each parent in it's own function. Fixes: 86be408bfbd8 ("clk: Support for clock parents and rates assigned from device tree") Signed-off-by: Nuno Sá --- drivers/clk/clk-conf.c | 126 +++++++++++++++++++++++++---------------- 1 file changed, 78 insertions(+), 48 deletions(-) diff --git a/drivers/clk/clk-conf.c b/drivers/clk/clk-conf.c index 2ef819606c41..b8ea4f93f35e 100644 --- a/drivers/clk/clk-conf.c +++ b/drivers/clk/clk-conf.c @@ -11,11 +11,78 @@ #include #include -static int __set_clk_parents(struct device_node *node, bool clk_supplier) +static int __set_clk_parent(struct device_node *node, bool clk_supplier, + int index, bool *stop) { struct of_phandle_args clkspec; - int index, rc, num_parents; struct clk *clk, *pclk; + int rc; + + rc = of_parse_phandle_with_args(node, "assigned-clock-parents", + "#clock-cells", index, &clkspec); + if (rc) { + /* skip empty (null) phandles */ + if (rc == -ENOENT) + return 0; + + return rc; + } + + if (clkspec.np == node && !clk_supplier) { + *stop = true; + goto out_of_put; + } + + pclk = of_clk_get_from_provider(&clkspec); + of_node_put(clkspec.np); + if (IS_ERR(pclk)) { + if (PTR_ERR(pclk) != -EPROBE_DEFER) + pr_warn("clk: couldn't get parent clock %d for %pOF\n", + index, node); + + return PTR_ERR(pclk); + } + + rc = of_parse_phandle_with_args(node, "assigned-clocks", + "#clock-cells", index, &clkspec); + if (rc) { + clk_put(pclk); + return rc; + } + + if (clkspec.np == node && !clk_supplier) { + *stop = true; + goto out_clk_put; + } + + clk = of_clk_get_from_provider(&clkspec); + if (IS_ERR(clk)) { + if (PTR_ERR(clk) != -EPROBE_DEFER) + pr_warn("clk: couldn't get assigned clock %d for %pOF\n", + index, node); + + rc = PTR_ERR(clk); + goto out_clk_put; + } + + rc = clk_set_parent(clk, pclk); + if (rc) + pr_err("clk: failed to reparent %s to %s: %d\n", + __clk_get_name(clk), __clk_get_name(pclk), rc); + + clk_put(clk); + +out_clk_put: + clk_put(pclk); +out_of_put: + of_node_put(clkspec.np); + return rc; +} + +static int __set_clk_parents(struct device_node *node, bool clk_supplier) +{ + int index, rc, num_parents; + bool stop = false; num_parents = of_count_phandle_with_args(node, "assigned-clock-parents", "#clock-cells"); @@ -24,53 +91,12 @@ static int __set_clk_parents(struct device_node *node, bool clk_supplier) node); for (index = 0; index < num_parents; index++) { - rc = of_parse_phandle_with_args(node, "assigned-clock-parents", - "#clock-cells", index, &clkspec); - if (rc < 0) { - /* skip empty (null) phandles */ - if (rc == -ENOENT) - continue; - else - return rc; - } - if (clkspec.np == node && !clk_supplier) - return 0; - pclk = of_clk_get_from_provider(&clkspec); - if (IS_ERR(pclk)) { - if (PTR_ERR(pclk) != -EPROBE_DEFER) - pr_warn("clk: couldn't get parent clock %d for %pOF\n", - index, node); - return PTR_ERR(pclk); - } - - rc = of_parse_phandle_with_args(node, "assigned-clocks", - "#clock-cells", index, &clkspec); - if (rc < 0) - goto err; - if (clkspec.np == node && !clk_supplier) { - rc = 0; - goto err; - } - clk = of_clk_get_from_provider(&clkspec); - if (IS_ERR(clk)) { - if (PTR_ERR(clk) != -EPROBE_DEFER) - pr_warn("clk: couldn't get assigned clock %d for %pOF\n", - index, node); - rc = PTR_ERR(clk); - goto err; - } - - rc = clk_set_parent(clk, pclk); - if (rc < 0) - pr_err("clk: failed to reparent %s to %s: %d\n", - __clk_get_name(clk), __clk_get_name(pclk), rc); - clk_put(clk); - clk_put(pclk); + rc = __set_clk_parent(node, clk_supplier, index, &stop); + if (rc || stop) + return rc; } + return 0; -err: - clk_put(pclk); - return rc; } static int __set_clk_rates(struct device_node *node, bool clk_supplier) @@ -93,14 +119,17 @@ static int __set_clk_rates(struct device_node *node, bool clk_supplier) else return rc; } - if (clkspec.np == node && !clk_supplier) + if (clkspec.np == node && !clk_supplier) { + of_node_put(clkspec.np); return 0; + } clk = of_clk_get_from_provider(&clkspec); if (IS_ERR(clk)) { if (PTR_ERR(clk) != -EPROBE_DEFER) pr_warn("clk: couldn't get clock %d for %pOF\n", index, node); + of_node_put(clkspec.np); return PTR_ERR(clk); } @@ -110,6 +139,7 @@ static int __set_clk_rates(struct device_node *node, bool clk_supplier) __clk_get_name(clk), rate, rc, clk_get_rate(clk)); clk_put(clk); + of_node_put(clkspec.np); } index++; } From patchwork Thu Jun 23 12:18:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Nuno Sa X-Patchwork-Id: 12892311 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 89EC6C43334 for ; Thu, 23 Jun 2022 12:19:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229967AbiFWMTY (ORCPT ); Thu, 23 Jun 2022 08:19:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37318 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229761AbiFWMTX (ORCPT ); Thu, 23 Jun 2022 08:19:23 -0400 Received: from mx0a-00128a01.pphosted.com (mx0a-00128a01.pphosted.com [148.163.135.77]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B73CDA470 for ; Thu, 23 Jun 2022 05:19:22 -0700 (PDT) Received: from pps.filterd (m0167089.ppops.net [127.0.0.1]) by mx0a-00128a01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 25N8rlFD024136; Thu, 23 Jun 2022 08:19:14 -0400 Received: from nwd2mta4.analog.com ([137.71.173.58]) by mx0a-00128a01.pphosted.com (PPS) with ESMTPS id 3gvemnb4tm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 23 Jun 2022 08:19:14 -0400 Received: from ASHBMBX9.ad.analog.com (ASHBMBX9.ad.analog.com [10.64.17.10]) by nwd2mta4.analog.com (8.14.7/8.14.7) with ESMTP id 25NCJCGZ054796 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Thu, 23 Jun 2022 08:19:12 -0400 Received: from ASHBMBX8.ad.analog.com (10.64.17.5) by ASHBMBX9.ad.analog.com (10.64.17.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.14; Thu, 23 Jun 2022 08:19:11 -0400 Received: from zeus.spd.analog.com (10.66.68.11) by ashbmbx8.ad.analog.com (10.64.17.5) with Microsoft SMTP Server id 15.2.986.14 via Frontend Transport; Thu, 23 Jun 2022 08:19:11 -0400 Received: from NSA-L01.ad.analog.com ([10.32.224.71]) by zeus.spd.analog.com (8.15.1/8.15.1) with ESMTP id 25NCJ2aq026429; Thu, 23 Jun 2022 08:19:09 -0400 From: =?utf-8?q?Nuno_S=C3=A1?= To: CC: Michael Turquette , Stephen Boyd Subject: [RESEND RFC PATCH v2 2/4] clk: fix clk not being unlinked from consumers list Date: Thu, 23 Jun 2022 14:18:55 +0200 Message-ID: <20220623121857.886-3-nuno.sa@analog.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220623121857.886-1-nuno.sa@analog.com> References: <20220623121857.886-1-nuno.sa@analog.com> MIME-Version: 1.0 X-ADIRuleOP-NewSCL: Rule Triggered X-Proofpoint-ORIG-GUID: DGXJjbTIcHhNcHU-R672KqxPcSfnLrMN X-Proofpoint-GUID: DGXJjbTIcHhNcHU-R672KqxPcSfnLrMN X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.883,Hydra:6.0.517,FMLib:17.11.122.1 definitions=2022-06-23_05,2022-06-23_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 mlxlogscore=916 malwarescore=0 impostorscore=0 lowpriorityscore=0 bulkscore=0 phishscore=0 adultscore=0 suspectscore=0 mlxscore=0 spamscore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2204290000 definitions=main-2206230050 Precedence: bulk List-ID: X-Mailing-List: linux-clk@vger.kernel.org When a clk_hw is registered we add a struct clk handle to it's consumers list. This handle is created in '__clk_register()' per the 'alloc_clk()' call. As such, we need to remove this handle when unregistering the clk_hw. This can actually lead to a use after free if a provider gets removed before a consumer. When removing the consumer, '__clk_put()' is called and that will do 'hlist_del(&clk->clks_node)' which will touch in already freed memory. Fixes: 1df4046a93e08 ("clk: Combine __clk_get() and __clk_create_clk()") Signed-off-by: Nuno Sá --- drivers/clk/clk.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c index 7fc191c15507..fdc711e53fb7 100644 --- a/drivers/clk/clk.c +++ b/drivers/clk/clk.c @@ -4191,6 +4191,7 @@ void clk_unregister(struct clk *clk) pr_warn("%s: unregistering protected clock: %s\n", __func__, clk->core->name); + clk_core_unlink_consumer(clk); kref_put(&clk->core->ref, __clk_release); free_clk(clk); unlock: From patchwork Thu Jun 23 12:18:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Nuno Sa X-Patchwork-Id: 12892313 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3881FC433EF for ; Thu, 23 Jun 2022 12:19:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229761AbiFWMTa (ORCPT ); Thu, 23 Jun 2022 08:19:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230257AbiFWMTZ (ORCPT ); Thu, 23 Jun 2022 08:19:25 -0400 Received: from mx0a-00128a01.pphosted.com (mx0a-00128a01.pphosted.com [148.163.135.77]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 925751A6 for ; Thu, 23 Jun 2022 05:19:24 -0700 (PDT) Received: from pps.filterd (m0167089.ppops.net [127.0.0.1]) by mx0a-00128a01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 25N8rlFE024136; Thu, 23 Jun 2022 08:19:15 -0400 Received: from nwd2mta4.analog.com ([137.71.173.58]) by mx0a-00128a01.pphosted.com (PPS) with ESMTPS id 3gvemnb4tp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 23 Jun 2022 08:19:15 -0400 Received: from ASHBMBX9.ad.analog.com (ASHBMBX9.ad.analog.com [10.64.17.10]) by nwd2mta4.analog.com (8.14.7/8.14.7) with ESMTP id 25NCJDQ0054799 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Thu, 23 Jun 2022 08:19:13 -0400 Received: from ASHBCASHYB5.ad.analog.com (10.64.17.133) by ASHBMBX9.ad.analog.com (10.64.17.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.14; Thu, 23 Jun 2022 08:19:13 -0400 Received: from ASHBMBX9.ad.analog.com (10.64.17.10) by ASHBCASHYB5.ad.analog.com (10.64.17.133) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.14; Thu, 23 Jun 2022 08:19:12 -0400 Received: from zeus.spd.analog.com (10.66.68.11) by ashbmbx9.ad.analog.com (10.64.17.10) with Microsoft SMTP Server id 15.2.986.14 via Frontend Transport; Thu, 23 Jun 2022 08:19:12 -0400 Received: from NSA-L01.ad.analog.com ([10.32.224.71]) by zeus.spd.analog.com (8.15.1/8.15.1) with ESMTP id 25NCJ2ar026429; Thu, 23 Jun 2022 08:19:10 -0400 From: =?utf-8?q?Nuno_S=C3=A1?= To: CC: Michael Turquette , Stephen Boyd Subject: [RESEND RFC PATCH v2 3/4] clk: refcount the active parent clk_core Date: Thu, 23 Jun 2022 14:18:56 +0200 Message-ID: <20220623121857.886-4-nuno.sa@analog.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220623121857.886-1-nuno.sa@analog.com> References: <20220623121857.886-1-nuno.sa@analog.com> MIME-Version: 1.0 X-ADIRuleOP-NewSCL: Rule Triggered X-Proofpoint-ORIG-GUID: VOibDaCigTKP30UzZCWAUkeMU2ff0O7L X-Proofpoint-GUID: VOibDaCigTKP30UzZCWAUkeMU2ff0O7L X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.883,Hydra:6.0.517,FMLib:17.11.122.1 definitions=2022-06-23_05,2022-06-23_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 mlxlogscore=999 malwarescore=0 impostorscore=0 lowpriorityscore=0 bulkscore=0 phishscore=0 adultscore=0 suspectscore=0 mlxscore=0 spamscore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2204290000 definitions=main-2206230050 Precedence: bulk List-ID: X-Mailing-List: linux-clk@vger.kernel.org As we keep a reference of the parent clk_hw, we should refcount it. Otherwise, we could end up with a use after free situation. With the following topology: providers | consumer +----------+ +----------+ | +-------+ | clk_hw A | --> | clk_hw B | <---- | clk C | +----------+ +----------+ | +-------+ Being clk_hw A and B provided by the same device, removing this device will effectively leave clk_core B with a pointer to clk_core C which was freed (clk C holds a reference to B and hence B won't be freed). Thus, when we do remove the clk consumer, bad things can (and will) happen. Signed-off-by: Nuno Sá --- drivers/clk/clk.c | 80 ++++++++++++++++++++++++++++++----------------- 1 file changed, 51 insertions(+), 29 deletions(-) diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c index fdc711e53fb7..92625f1523f5 100644 --- a/drivers/clk/clk.c +++ b/drivers/clk/clk.c @@ -1844,6 +1844,47 @@ static void __clk_set_parent_after(struct clk_core *core, } } +static void clk_core_free_parent_map(struct clk_core *core) +{ + int i = core->num_parents; + + if (!core->num_parents) + return; + + while (--i >= 0) { + kfree_const(core->parents[i].name); + kfree_const(core->parents[i].fw_name); + } + + kfree(core->parents); +} + +/* Free memory allocated for a clock. */ +static void __clk_release(struct kref *ref) +{ + struct clk_core *core = container_of(ref, struct clk_core, ref); + + lockdep_assert_held(&prepare_lock); + + if (core->parent) + kref_put(&core->parent->ref, __clk_release); + + clk_core_free_parent_map(core); + kfree_const(core->name); + kfree(core); +} + +/* deal with new, old parent references */ +static void __clk_reparent_refs_update(struct clk_core *new_parent, + struct clk_core *old_parent) +{ + if (new_parent) + kref_get(&new_parent->ref); + + if (old_parent) + kref_put(&old_parent->ref, __clk_release); +} + static int __clk_set_parent(struct clk_core *core, struct clk_core *parent, u8 p_index) { @@ -1871,6 +1912,7 @@ static int __clk_set_parent(struct clk_core *core, struct clk_core *parent, } __clk_set_parent_after(core, parent, old_parent); + __clk_reparent_refs_update(parent, old_parent); return 0; } @@ -2111,6 +2153,7 @@ static void clk_change_rate(struct clk_core *core) trace_clk_set_parent_complete(core, core->new_parent); __clk_set_parent_after(core, core->new_parent, old_parent); + __clk_reparent_refs_update(core->new_parent, old_parent); } if (core->flags & CLK_OPS_PARENT_ENABLE) @@ -3470,6 +3513,7 @@ static void clk_core_reparent_orphans_nolock(void) /* update the clk tree topology */ __clk_set_parent_before(orphan, parent); __clk_set_parent_after(orphan, parent, NULL); + __clk_reparent_refs_update(parent, NULL); __clk_recalc_accuracies(orphan); __clk_recalc_rates(orphan, 0); @@ -3592,6 +3636,7 @@ static int __clk_core_init(struct clk_core *core) if (parent) { hlist_add_head(&core->child_node, &parent->children); core->orphan = parent->orphan; + kref_get(&parent->ref); } else if (!core->num_parents) { hlist_add_head(&core->child_node, &clk_root_list); core->orphan = false; @@ -3670,10 +3715,14 @@ static int __clk_core_init(struct clk_core *core) } } - clk_core_reparent_orphans_nolock(); + /* + * Some orphan might be reparented to us grabbing a reference. Hence, + * this has to be initialized now. + */ + kref_init(&core->ref); + clk_core_reparent_orphans_nolock(); - kref_init(&core->ref); out: clk_pm_runtime_put(core); unlock: @@ -3887,21 +3936,6 @@ static int clk_core_populate_parent_map(struct clk_core *core, return 0; } -static void clk_core_free_parent_map(struct clk_core *core) -{ - int i = core->num_parents; - - if (!core->num_parents) - return; - - while (--i >= 0) { - kfree_const(core->parents[i].name); - kfree_const(core->parents[i].fw_name); - } - - kfree(core->parents); -} - static struct clk * __clk_register(struct device *dev, struct device_node *np, struct clk_hw *hw) { @@ -4061,18 +4095,6 @@ int of_clk_hw_register(struct device_node *node, struct clk_hw *hw) } EXPORT_SYMBOL_GPL(of_clk_hw_register); -/* Free memory allocated for a clock. */ -static void __clk_release(struct kref *ref) -{ - struct clk_core *core = container_of(ref, struct clk_core, ref); - - lockdep_assert_held(&prepare_lock); - - clk_core_free_parent_map(core); - kfree_const(core->name); - kfree(core); -} - /* * Empty clk_ops for unregistered clocks. These are used temporarily * after clk_unregister() was called on a clock and until last clock From patchwork Thu Jun 23 12:18:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Nuno Sa X-Patchwork-Id: 12892312 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 75658CCA480 for ; Thu, 23 Jun 2022 12:19:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230220AbiFWMTb (ORCPT ); Thu, 23 Jun 2022 08:19:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230381AbiFWMTZ (ORCPT ); Thu, 23 Jun 2022 08:19:25 -0400 Received: from mx0a-00128a01.pphosted.com (mx0a-00128a01.pphosted.com [148.163.135.77]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D510A1036 for ; Thu, 23 Jun 2022 05:19:24 -0700 (PDT) Received: from pps.filterd (m0167088.ppops.net [127.0.0.1]) by mx0a-00128a01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 25NAsG2x025531; Thu, 23 Jun 2022 08:19:16 -0400 Received: from nwd2mta3.analog.com ([137.71.173.56]) by mx0a-00128a01.pphosted.com (PPS) with ESMTPS id 3gs8j63shv-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 23 Jun 2022 08:19:15 -0400 Received: from ASHBMBX8.ad.analog.com (ASHBMBX8.ad.analog.com [10.64.17.5]) by nwd2mta3.analog.com (8.14.7/8.14.7) with ESMTP id 25NCJElH023755 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Thu, 23 Jun 2022 08:19:14 -0400 Received: from ASHBMBX9.ad.analog.com (10.64.17.10) by ASHBMBX8.ad.analog.com (10.64.17.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.14; Thu, 23 Jun 2022 08:19:13 -0400 Received: from zeus.spd.analog.com (10.66.68.11) by ashbmbx9.ad.analog.com (10.64.17.10) with Microsoft SMTP Server id 15.2.986.14 via Frontend Transport; Thu, 23 Jun 2022 08:19:13 -0400 Received: from NSA-L01.ad.analog.com ([10.32.224.71]) by zeus.spd.analog.com (8.15.1/8.15.1) with ESMTP id 25NCJ2as026429; Thu, 23 Jun 2022 08:19:11 -0400 From: =?utf-8?q?Nuno_S=C3=A1?= To: CC: Michael Turquette , Stephen Boyd Subject: [RESEND RFC PATCH v2 4/4] clk: use clk_core_unlink_consumer() helper Date: Thu, 23 Jun 2022 14:18:57 +0200 Message-ID: <20220623121857.886-5-nuno.sa@analog.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220623121857.886-1-nuno.sa@analog.com> References: <20220623121857.886-1-nuno.sa@analog.com> MIME-Version: 1.0 X-ADIRuleOP-NewSCL: Rule Triggered X-Proofpoint-ORIG-GUID: rBpM8PXzzjZiFW0B99_fdxHckFnnzNcP X-Proofpoint-GUID: rBpM8PXzzjZiFW0B99_fdxHckFnnzNcP X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.883,Hydra:6.0.517,FMLib:17.11.122.1 definitions=2022-06-23_05,2022-06-23_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 phishscore=0 mlxlogscore=845 bulkscore=0 suspectscore=0 spamscore=0 mlxscore=0 lowpriorityscore=0 adultscore=0 malwarescore=0 priorityscore=1501 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2204290000 definitions=main-2206230050 Precedence: bulk List-ID: X-Mailing-List: linux-clk@vger.kernel.org There is an helper to remove a consumer from the clk provider list. Hence, let's use it when releasing a consumer. Signed-off-by: Nuno Sá --- 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 92625f1523f5..9eb5a7c2c73b 100644 --- a/drivers/clk/clk.c +++ b/drivers/clk/clk.c @@ -4370,7 +4370,7 @@ void __clk_put(struct clk *clk) clk->exclusive_count = 0; } - hlist_del(&clk->clks_node); + clk_core_unlink_consumer(clk); if (clk->min_rate > clk->core->req_rate || clk->max_rate < clk->core->req_rate) clk_core_set_rate_nolock(clk->core, clk->core->req_rate);