mbox series

[net-next,00/12] net: Convert using devm_clk_get_enabled()/devm_clk_get_optional_enabled()

Message ID 20240831021334.1907921-1-lizetao1@huawei.com
Headers show
Series net: Convert using devm_clk_get_enabled()/devm_clk_get_optional_enabled() | expand

Message

Li Zetao Aug. 31, 2024, 2:13 a.m. UTC
There are many examples[1][2] of clk resource leakage in LTS. The
reason is that developers need to maintain the allocation and release
of clk resources themselves, but this will increase the burden on
developers. Using the API related to devm_clk_get_*_enable ensures
that the life cycle of clk is consistent with that of the device,
reducing the risk of unreleased resources like clk.

Several other developers are also working on converting to more
secure interfaces, and this patch set is in principle the same as
theirs.

[1]: https://lore.kernel.org/all/20240812160128.338041191@linuxfoundation.org/
[2]: https://lore.kernel.org/all/20240812160135.992451065@linuxfoundation.org/

Li Zetao (12):
  net: dsa: bcm_sf2: Convert using devm_clk_get_optional_enabled() in
    bcm_sf2_sw_probe()
  net: ethernet: Convert using devm_clk_get_enabled() in emac_probe()
  net: ethernet: arc: Convert using devm_clk_get_enabled() in
    emac_probe()
  net: ethernet: ethoc: Convert using devm_clk_get_enabled() in
    ethoc_probe()
  net: ftgmac100: Convert using devm_clk_get_enabled() in
    ftgmac100_setup_clk()
  net: ethernet: hisilicon: Convert using devm_clk_get_enabled() in
    hisi_femac_drv_probe()
  net: lantiq_xrx200: Convert using devm_clk_get_enabled() in
    xrx200_probe()
  net: stmmac: dwmac-dwc-qos-eth: Convert using devm_clk_get_enabled()
    in dwc_qos_probe()
  net: ethernet: sunplus: Convert using devm_clk_get_enabled() in
    spl2sw_probe()
  net: xilinx: axienet: Convert using devm_clk_get_optional_enabled() in
    axienet_probe()
  wifi: wilc1000: Convert using devm_clk_get_optional_enabled() in
    wilc_sdio_probe()
  wifi: wilc1000: Convert using devm_clk_get_optional_enabled() in
    wilc_bus_probe()

 drivers/net/dsa/bcm_sf2.c                     | 28 ++----
 drivers/net/ethernet/allwinner/sun4i-emac.c   | 13 +--
 drivers/net/ethernet/arc/emac_rockchip.c      | 34 ++-----
 drivers/net/ethernet/ethoc.c                  | 18 ++--
 drivers/net/ethernet/faraday/ftgmac100.c      | 27 ++---
 drivers/net/ethernet/hisilicon/hisi_femac.c   | 17 +---
 drivers/net/ethernet/lantiq_xrx200.c          | 17 +---
 .../stmicro/stmmac/dwmac-dwc-qos-eth.c        | 98 ++++---------------
 drivers/net/ethernet/sunplus/spl2sw_driver.c  | 18 +---
 .../net/ethernet/xilinx/xilinx_axienet_main.c | 15 +--
 .../net/wireless/microchip/wilc1000/sdio.c    | 10 +-
 drivers/net/wireless/microchip/wilc1000/spi.c |  5 +-
 12 files changed, 64 insertions(+), 236 deletions(-)

Comments

Radhey Shyam Pandey Sept. 1, 2024, 12:28 p.m. UTC | #1
> -----Original Message-----
> From: Li Zetao <lizetao1@huawei.com>
> Sent: Saturday, August 31, 2024 7:44 AM
> To: florian.fainelli@broadcom.com; andrew@lunn.ch; olteanv@gmail.com;
> davem@davemloft.net; edumazet@google.com; kuba@kernel.org;
> pabeni@redhat.com; wens@csie.org; jernej.skrabec@gmail.com;
> samuel@sholland.org; heiko@sntech.de; yisen.zhuang@huawei.com;
> salil.mehta@huawei.com; hauke@hauke-m.de;
> alexandre.torgue@foss.st.com; joabreu@synopsys.com;
> mcoquelin.stm32@gmail.com; wellslutw@gmail.com; Pandey, Radhey
> Shyam <radhey.shyam.pandey@amd.com>; Simek, Michal
> <michal.simek@amd.com>; ajay.kathat@microchip.com;
> claudiu.beznea@tuxon.dev; kvalo@kernel.org; lizetao1@huawei.com;
> u.kleine-koenig@pengutronix.de; jacky_chou@aspeedtech.com
> Cc: netdev@vger.kernel.org; linux-arm-kernel@lists.infradead.org; linux-
> sunxi@lists.linux.dev; linux-rockchip@lists.infradead.org; linux-stm32@st-
> md-mailman.stormreply.com; linux-wireless@vger.kernel.org
> Subject: [PATCH net-next 10/12] net: xilinx: axienet: Convert using
> devm_clk_get_optional_enabled() in axienet_probe()
> 
> Use devm_clk_get_optional_enabled() instead of devm_clk_get_optional() +
> clk_prepare_enable(), which can make the clk consistent with the device life
> cycle and reduce the risk of unreleased clk resources. Since the device
> framework has automatically released the clk resource, there is no need to
> execute clk_disable_unprepare(clk) on the error path.
> 
> Signed-off-by: Li Zetao <lizetao1@huawei.com>

Reviewed-by: Radhey Shyam Pandey <radhey.shyam.pandey@amd.com>
Thanks!

> ---
>  drivers/net/ethernet/xilinx/xilinx_axienet_main.c | 15 ++++-----------
>  1 file changed, 4 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/net/ethernet/xilinx/xilinx_axienet_main.c
> b/drivers/net/ethernet/xilinx/xilinx_axienet_main.c
> index fe6a0e2e463f..48b41e95aa74 100644
> --- a/drivers/net/ethernet/xilinx/xilinx_axienet_main.c
> +++ b/drivers/net/ethernet/xilinx/xilinx_axienet_main.c
> @@ -2584,22 +2584,17 @@ static int axienet_probe(struct platform_device
> *pdev)
>  	seqcount_mutex_init(&lp->hw_stats_seqcount, &lp->stats_lock);
>  	INIT_DEFERRABLE_WORK(&lp->stats_work, axienet_refresh_stats);
> 
> -	lp->axi_clk = devm_clk_get_optional(&pdev->dev, "s_axi_lite_clk");
> -	if (!lp->axi_clk) {
> +	lp->axi_clk = devm_clk_get_optional_enabled(&pdev->dev,
> "s_axi_lite_clk");
> +	if (!lp->axi_clk)
>  		/* For backward compatibility, if named AXI clock is not
> present,
>  		 * treat the first clock specified as the AXI clock.
>  		 */
> -		lp->axi_clk = devm_clk_get_optional(&pdev->dev, NULL);
> -	}
> +		lp->axi_clk = devm_clk_get_optional_enabled(&pdev->dev,
> NULL);
> +
>  	if (IS_ERR(lp->axi_clk)) {
>  		ret = PTR_ERR(lp->axi_clk);
>  		goto free_netdev;
>  	}
> -	ret = clk_prepare_enable(lp->axi_clk);
> -	if (ret) {
> -		dev_err(&pdev->dev, "Unable to enable AXI clock: %d\n",
> ret);
> -		goto free_netdev;
> -	}
> 
>  	lp->misc_clks[0].id = "axis_clk";
>  	lp->misc_clks[1].id = "ref_clk";
> @@ -2915,7 +2910,6 @@ static int axienet_probe(struct platform_device
> *pdev)
>  		axienet_mdio_teardown(lp);
>  cleanup_clk:

I also find that there is goto to cleanup_clk when devm_clk_bulk_get_optional/
clk_bulk_prepare_enable fails which is not correct but as it is existing bug it 
can go a separate patch.

>  	clk_bulk_disable_unprepare(XAE_NUM_MISC_CLOCKS, lp-
> >misc_clks);
> -	clk_disable_unprepare(lp->axi_clk);
> 
>  free_netdev:
>  	free_netdev(ndev);
> @@ -2939,7 +2933,6 @@ static void axienet_remove(struct platform_device
> *pdev)
>  	axienet_mdio_teardown(lp);
> 
>  	clk_bulk_disable_unprepare(XAE_NUM_MISC_CLOCKS, lp-
> >misc_clks);
> -	clk_disable_unprepare(lp->axi_clk);
> 
>  	free_netdev(ndev);
>  }
> --
> 2.34.1
Li Zetao Sept. 3, 2024, 2:27 a.m. UTC | #2
在 2024/9/1 20:28, Pandey, Radhey Shyam 写道:
>> -----Original Message-----
>> From: Li Zetao <lizetao1@huawei.com>
>> Sent: Saturday, August 31, 2024 7:44 AM
>> To: florian.fainelli@broadcom.com; andrew@lunn.ch; olteanv@gmail.com;
>> davem@davemloft.net; edumazet@google.com; kuba@kernel.org;
>> pabeni@redhat.com; wens@csie.org; jernej.skrabec@gmail.com;
>> samuel@sholland.org; heiko@sntech.de; yisen.zhuang@huawei.com;
>> salil.mehta@huawei.com; hauke@hauke-m.de;
>> alexandre.torgue@foss.st.com; joabreu@synopsys.com;
>> mcoquelin.stm32@gmail.com; wellslutw@gmail.com; Pandey, Radhey
>> Shyam <radhey.shyam.pandey@amd.com>; Simek, Michal
>> <michal.simek@amd.com>; ajay.kathat@microchip.com;
>> claudiu.beznea@tuxon.dev; kvalo@kernel.org; lizetao1@huawei.com;
>> u.kleine-koenig@pengutronix.de; jacky_chou@aspeedtech.com
>> Cc: netdev@vger.kernel.org; linux-arm-kernel@lists.infradead.org; linux-
>> sunxi@lists.linux.dev; linux-rockchip@lists.infradead.org; linux-stm32@st-
>> md-mailman.stormreply.com; linux-wireless@vger.kernel.org
>> Subject: [PATCH net-next 10/12] net: xilinx: axienet: Convert using
>> devm_clk_get_optional_enabled() in axienet_probe()
>>
>> Use devm_clk_get_optional_enabled() instead of devm_clk_get_optional() +
>> clk_prepare_enable(), which can make the clk consistent with the device life
>> cycle and reduce the risk of unreleased clk resources. Since the device
>> framework has automatically released the clk resource, there is no need to
>> execute clk_disable_unprepare(clk) on the error path.
>>
>> Signed-off-by: Li Zetao <lizetao1@huawei.com>
> 
> Reviewed-by: Radhey Shyam Pandey <radhey.shyam.pandey@amd.com>
> Thanks!
> 
>> ---
>>   drivers/net/ethernet/xilinx/xilinx_axienet_main.c | 15 ++++-----------
>>   1 file changed, 4 insertions(+), 11 deletions(-)
>>
>> diff --git a/drivers/net/ethernet/xilinx/xilinx_axienet_main.c
>> b/drivers/net/ethernet/xilinx/xilinx_axienet_main.c
>> index fe6a0e2e463f..48b41e95aa74 100644
>> --- a/drivers/net/ethernet/xilinx/xilinx_axienet_main.c
>> +++ b/drivers/net/ethernet/xilinx/xilinx_axienet_main.c
>> @@ -2584,22 +2584,17 @@ static int axienet_probe(struct platform_device
>> *pdev)
>>   	seqcount_mutex_init(&lp->hw_stats_seqcount, &lp->stats_lock);
>>   	INIT_DEFERRABLE_WORK(&lp->stats_work, axienet_refresh_stats);
>>
>> -	lp->axi_clk = devm_clk_get_optional(&pdev->dev, "s_axi_lite_clk");
>> -	if (!lp->axi_clk) {
>> +	lp->axi_clk = devm_clk_get_optional_enabled(&pdev->dev,
>> "s_axi_lite_clk");
>> +	if (!lp->axi_clk)
>>   		/* For backward compatibility, if named AXI clock is not
>> present,
>>   		 * treat the first clock specified as the AXI clock.
>>   		 */
>> -		lp->axi_clk = devm_clk_get_optional(&pdev->dev, NULL);
>> -	}
>> +		lp->axi_clk = devm_clk_get_optional_enabled(&pdev->dev,
>> NULL);
>> +
>>   	if (IS_ERR(lp->axi_clk)) {
>>   		ret = PTR_ERR(lp->axi_clk);
>>   		goto free_netdev;
>>   	}
>> -	ret = clk_prepare_enable(lp->axi_clk);
>> -	if (ret) {
>> -		dev_err(&pdev->dev, "Unable to enable AXI clock: %d\n",
>> ret);
>> -		goto free_netdev;
>> -	}
>>
>>   	lp->misc_clks[0].id = "axis_clk";
>>   	lp->misc_clks[1].id = "ref_clk";
>> @@ -2915,7 +2910,6 @@ static int axienet_probe(struct platform_device
>> *pdev)
>>   		axienet_mdio_teardown(lp);
>>   cleanup_clk:
> 
> I also find that there is goto to cleanup_clk when devm_clk_bulk_get_optional/
> clk_bulk_prepare_enable fails which is not correct but as it is existing bug it
> can go a separate patch.
Thanks for the reminder, I considered solving this problem by using 
devm_add_action_or_reset

Thanks,
Li Zetao.
> 
>>   	clk_bulk_disable_unprepare(XAE_NUM_MISC_CLOCKS, lp-
>>> misc_clks);
>> -	clk_disable_unprepare(lp->axi_clk);
>>
>>   free_netdev:
>>   	free_netdev(ndev);
>> @@ -2939,7 +2933,6 @@ static void axienet_remove(struct platform_device
>> *pdev)
>>   	axienet_mdio_teardown(lp);
>>
>>   	clk_bulk_disable_unprepare(XAE_NUM_MISC_CLOCKS, lp-
>>> misc_clks);
>> -	clk_disable_unprepare(lp->axi_clk);
>>
>>   	free_netdev(ndev);
>>   }
>> --
>> 2.34.1
>
Jeff Johnson Sept. 6, 2024, 11:17 p.m. UTC | #3
On 8/30/2024 7:13 PM, Li Zetao wrote:
> There are many examples[1][2] of clk resource leakage in LTS. The
> reason is that developers need to maintain the allocation and release
> of clk resources themselves, but this will increase the burden on
> developers. Using the API related to devm_clk_get_*_enable ensures
> that the life cycle of clk is consistent with that of the device,
> reducing the risk of unreleased resources like clk.
> 
> Several other developers are also working on converting to more
> secure interfaces, and this patch set is in principle the same as
> theirs.

...

>  drivers/net/dsa/bcm_sf2.c                     | 28 ++----
>  drivers/net/ethernet/allwinner/sun4i-emac.c   | 13 +--
>  drivers/net/ethernet/arc/emac_rockchip.c      | 34 ++-----
>  drivers/net/ethernet/ethoc.c                  | 18 ++--
>  drivers/net/ethernet/faraday/ftgmac100.c      | 27 ++---
>  drivers/net/ethernet/hisilicon/hisi_femac.c   | 17 +---
>  drivers/net/ethernet/lantiq_xrx200.c          | 17 +---
>  .../stmicro/stmmac/dwmac-dwc-qos-eth.c        | 98 ++++---------------
>  drivers/net/ethernet/sunplus/spl2sw_driver.c  | 18 +---
>  .../net/ethernet/xilinx/xilinx_axienet_main.c | 15 +--
>  .../net/wireless/microchip/wilc1000/sdio.c    | 10 +-
>  drivers/net/wireless/microchip/wilc1000/spi.c |  5 +-

note the wifi driver changes go through the wireless tree and not the net tree
so those should be split out separately
Li Zetao Sept. 7, 2024, 2:55 a.m. UTC | #4
Hi,

在 2024/9/7 7:17, Jeff Johnson 写道:
> On 8/30/2024 7:13 PM, Li Zetao wrote:
>> There are many examples[1][2] of clk resource leakage in LTS. The
>> reason is that developers need to maintain the allocation and release
>> of clk resources themselves, but this will increase the burden on
>> developers. Using the API related to devm_clk_get_*_enable ensures
>> that the life cycle of clk is consistent with that of the device,
>> reducing the risk of unreleased resources like clk.
>>
>> Several other developers are also working on converting to more
>> secure interfaces, and this patch set is in principle the same as
>> theirs.
> 
> ...
> 
>>   drivers/net/dsa/bcm_sf2.c                     | 28 ++----
>>   drivers/net/ethernet/allwinner/sun4i-emac.c   | 13 +--
>>   drivers/net/ethernet/arc/emac_rockchip.c      | 34 ++-----
>>   drivers/net/ethernet/ethoc.c                  | 18 ++--
>>   drivers/net/ethernet/faraday/ftgmac100.c      | 27 ++---
>>   drivers/net/ethernet/hisilicon/hisi_femac.c   | 17 +---
>>   drivers/net/ethernet/lantiq_xrx200.c          | 17 +---
>>   .../stmicro/stmmac/dwmac-dwc-qos-eth.c        | 98 ++++---------------
>>   drivers/net/ethernet/sunplus/spl2sw_driver.c  | 18 +---
>>   .../net/ethernet/xilinx/xilinx_axienet_main.c | 15 +--
>>   .../net/wireless/microchip/wilc1000/sdio.c    | 10 +-
>>   drivers/net/wireless/microchip/wilc1000/spi.c |  5 +-
> 
> note the wifi driver changes go through the wireless tree and not the net tree
> so those should be split out separately

I have separated the wifi related patches and sent them to the community:
https://lore.kernel.org/all/20240903110205.4127706-1-lizetao1@huawei.com/
> 
> 
Thanks,
Li Zetao.