net: fec: Convert fec driver to use lock guards
Subsystems (5)
Wei Fang
maintainerPATCH v2 1/1 net: fec: Convert fec driver to use lock guards
The Scope-based resource management mechanism has been introduced into kernel since the commit 54da6a092431 ("locking: Introduce __cleanup() based infrastructure"). The mechanism leverages the 'cleanup' attribute provided by GCC and Clang, which allows resources to be automatically released when they go out of scope.
Therefore, convert the fec driver to use guard() and scoped_guard() defined in linux/cleanup.h to automate lock lifetime control in the fec driver.
---
V2 changes:
1. Rephrase the commit message
2. Remove unnecessary '{}' if the scope of scoped_guard() is single line
--- drivers/net/ethernet/freescale/fec_main.c | 36 ++++---- drivers/net/ethernet/freescale/fec_ptp.c | 101 ++++++++-------------- 2 files changed, 54 insertions(+), 83 deletions(-)
drivers/net/ethernet/freescale/fec_main.c +16 -20
@@ -1397,12 +1397,10 @@ static void fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts, struct skb_shared_hwtstamps *hwtstamps) { - unsigned long flags; u64 ns; -spin_lock_irqsave (&fep->tmreg_lock, flags); - ns =timecounter_cyc2time (&fep->tc, ts); -spin_unlock_irqrestore (&fep->tmreg_lock, flags); +scoped_guard (spinlock_irqsave, &fep->tmreg_lock) + ns = timecounter_cyc2time(&fep->tc, ts);memset (hwtstamps,0 ,sizeof (*hwtstamps)); hwtstamps->hwtstamp =ns_to_ktime (ns);
@@ -2313,15 +2311,13 @@ static int fec_enet_clk_enable(struct net_device *ndev, bool enable)return ret;if (fep->clk_ptp) { -mutex_lock (&fep->ptp_clk_mutex); - ret =clk_prepare_enable (fep->clk_ptp); -if (ret) { -mutex_unlock (&fep->ptp_clk_mutex); - goto failed_clk_ptp; - }else { - fep->ptp_clk_on = true; + scoped_guard(mutex, &fep->ptp_clk_mutex) { + ret =clk_prepare_enable (fep->clk_ptp); +if (ret) + goto failed_clk_ptp; +else + fep->ptp_clk_on = true; } -mutex_unlock (&fep->ptp_clk_mutex); } ret =clk_prepare_enable (fep->clk_ref);
@@ -2336,10 +2332,10 @@ static int fec_enet_clk_enable(struct net_device *ndev, bool enable) }else {clk_disable_unprepare (fep->clk_enet_out);if (fep->clk_ptp) { -mutex_lock (&fep->ptp_clk_mutex); -clk_disable_unprepare (fep->clk_ptp); - fep->ptp_clk_on = false; -mutex_unlock (&fep->ptp_clk_mutex); + scoped_guard(mutex, &fep->ptp_clk_mutex) { +clk_disable_unprepare (fep->clk_ptp); + fep->ptp_clk_on = false; + } }clk_disable_unprepare (fep->clk_ref);clk_disable_unprepare (fep->clk_2x_txclk);
@@ -2352,10 +2348,10 @@ static int fec_enet_clk_enable(struct net_device *ndev, bool enable)clk_disable_unprepare (fep->clk_ref); failed_clk_ref:if (fep->clk_ptp) { -mutex_lock (&fep->ptp_clk_mutex); -clk_disable_unprepare (fep->clk_ptp); - fep->ptp_clk_on = false; -mutex_unlock (&fep->ptp_clk_mutex); + scoped_guard(mutex, &fep->ptp_clk_mutex) { +clk_disable_unprepare (fep->clk_ptp); + fep->ptp_clk_on = false; + } } failed_clk_ptp:clk_disable_unprepare (fep->clk_enet_out);
drivers/net/ethernet/freescale/fec_ptp.c +38 -63
233 lines not on this page · raw message
Signature · 1 line
2.34.1
Quoted 8 lines · Wei Fang
The Scope-based resource management mechanism has been introduced into kernel since the commit
54da6a092431("locking: Introduce__cleanup()based infrastructure"). The mechanism leverages the 'cleanup' attribute provided by GCC and Clang, which allows resources to be automatically released when they go out of scope.
Therefore, convert the fec driver to useguard()andscoped_guard()defined inlinux/cleanup.hto automate lock lifetime control in the fec driver.
Sorry, it has been decided for netdev we don't want these sort of conversions, at least not yet. The main worry is backporting fixes. It is likely such bcakports are going to be harder, and also more error prone, since the context is quite different.
If done correctly, scoped_guard() {} could be useful, and avoid issues. So we are O.K. with that in new code. That will also allow us to get some experience with it over the next few years. Maybe we will then re-evaluate this decision about converting existing code.
Andrew
---
pw-bot: cr
Wei Fang
maintainerQuoted 26 lines
-----Original Message-----
From: Andrew Lunn <[email protected]>
Sent: 2024年5月11日 22:30
To: Wei Fang <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Shenwei Wang <[email protected]>; Clark Wang <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v2 net-next] net: fec: Convert fec driver to use lock guardsQuoted 1 line · Wei Fang
The Scope-based resource management mechanism has been introduced
into
Quoted 7 lines
kernel since the commit
54da6a092431("locking: Introduce__cleanup()based infrastructure"). The mechanism leverages the 'cleanup' attribute provided by GCC and Clang, which allows resources to be automatically released when they go out of scope. Therefore, convert the fec driver to useguard()andscoped_guard()defined inlinux/cleanup.hto automate lock lifetime control in the fec driver.Sorry, it has been decided for netdev we don't want these sort of conversions, at least not yet. The main worry is backporting fixes. It is likely such bcakports are going to be harder, and also more error prone, since the context is quite different.
Quoted 5 lines
If done correctly,
scoped_guard(){} could be useful, and avoid issues. So we are O.K. with that in new code. That will also allow us to get some experience with it over the next few years. Maybe we will then re-evaluate this decision about converting existing code.
Okay, it's fine, thanks.
Quoted 1 line
Quoted 1 line
The Scope-based resource management mechanism has been introduced into
…
scope? was?
…
Quoted 14 lines
+++
b/drivers/net/ethernet/freescale/fec_ptp.cdrivers/net/ethernet/freescale/fec_ptp.c +0
-4@@ -99,18 +99,17 @@*/ static int fec_ptp_enable_pps (struct fec_enet_private *fep,uint enable) { -unsigned long flags;u32 val, tempval;struct timespec64 ts;u64 ns; -if (fep->pps_enable == enable) -return 0 ; - fep->pps_channel =DEFAULT_PPS_CHANNEL ;
drivers/net/ethernet/freescale/fec_ptp.c line 115
Quoted 10 lines
drivers/net/ethernet/freescale/fec_ptp.c +4
-1fep->reload_period =PPS_OUPUT_RELOAD_PERIOD ; -spin_lock_irqsave (&fep->tmreg_lock, flags); +guard (spinlock_irqsave)(&fep->tmreg_lock); + +if (fep->pps_enable == enable) +return 0 ;if (enable) {/* clear capture or output compare interrupt status if have.
…
Was this source code adjustment influenced also by a hint about “LOCK EVASION” from the analysis tool “Coverity”?
https://lore.kernel.org/linux-kernel/AM0PR0402MB38910DB23A6DABF1C074EF1D88E52@AM0PR0402MB3891.eurprd04.prod.outlook.com/
https://lkml.org/lkml/2024/5/8/77
Will any tags (like “Fixes” and “Cc”) become relevant here?
How do you think about to take the known advice “Solve only one problem per patch” better into account?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.10-rc4#n81
Under which circumstances will development interests grow for further approaches according to the presentation of similar change combinations?
Regards,
Markus
Quoted 4 lines
Was this source code adjustment influenced also by a hint about “LOCK EVASION” from the analysis tool “Coverity”?
https://lore.kernel.org/linux-kernel/AM0PR0402MB38910DB23A6DABF1C074EF1D88E52@AM0PR0402MB3891.eurprd04.prod.outlook.com/
https://lkml.org/lkml/2024/5/8/77
A corresponding software improvement was integrated on 2024-05-23.
net: fec: avoid lock evasion when reading pps_enable
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/drivers/net/ethernet/freescale/fec_ptp.c?h=v6.10-rc4&id=3b1c92f8e5371700fada307cc8fd2c51fa7bc8c1
Will further collateral evolution become interesting accordingly?
Regards,
Markus
Wei Fang
maintainerQuoted 82 lines
-----Original Message-----
From: Markus Elfring <[email protected]>
Sent: 2024年6月23日 19:01
To: Wei Fang <[email protected]>; [email protected]; [email protected]; [email protected]; Andrew Lunn <[email protected]>; Clark Wang <[email protected]>; David S. Miller <[email protected]>; Eric Dumazet <[email protected]>; Jakub Kicinski <[email protected]>; Paolo Abeni <[email protected]>; Richard Cochran <[email protected]>; Shenwei Wang <[email protected]>
Cc: Julia Lawall <[email protected]>; Peter Zijlstra <[email protected]>; Simon Horman <[email protected]>; Waiman Long <[email protected]>
Subject: Re: [PATCH v2 net-next] net: fec: Convert fec driver to use lock guardsQuoted 1 line
The Scope-based resource management mechanism has been introduced
into
…
scope? was?…
Quoted 14 lines
+++
b/drivers/net/ethernet/freescale/fec_ptp.cdrivers/net/ethernet/freescale/fec_ptp.c +0
-4@@ -99,18 +99,17 @@*/ static int fec_ptp_enable_pps (struct fec_enet_private *fep,uint enable) { -unsigned long flags;u32 val, tempval;struct timespec64 ts;u64 ns; -if (fep->pps_enable == enable) -return 0 ; - fep->pps_channel =DEFAULT_PPS_CHANNEL ;drivers/net/ethernet/freescale/fec_ptp.c line 115
Quoted 10 lines
drivers/net/ethernet/freescale/fec_ptp.c +4
-1fep->reload_period =PPS_OUPUT_RELOAD_PERIOD ; -spin_lock_irqsave (&fep->tmreg_lock, flags); +guard (spinlock_irqsave)(&fep->tmreg_lock); + +if (fep->pps_enable == enable) +return 0 ;if (enable) {/* clear capture or output compare interrupt status if have. …
Was this source code adjustment influenced also by a hint about “LOCK EVASION”
from the analysis tool “Coverity”?
https://lore.k/
ernel.org%2Flinux-kernel%2FAM0PR0402MB38910DB23A6DABF1C074EF1D 88E52%40AM0PR0402MB3891.eurprd04.prod.outlook.com%2F&data=05%7 C02%7Cwei.fang%40nxp.com%7Cd6bfb97d5f854c511a3c08dc9373d064%7 C686ea1d3bc2b4c6fa92cd99c5c301635%7C0%7C0%7C6385473728254555 50%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luM zIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=m9oRx%2FI z%2FLu1%2BlcQMRUlHsDfNznfVlJgbVwPramiEdA%3D&reserved=0
https://lkml.o/
rg%2Flkml%2F2024%2F5%2F8%2F77&data=05%7C02%7Cwei.fang%40nxp.c om%7Cd6bfb97d5f854c511a3c08dc9373d064%7C686ea1d3bc2b4c6fa92cd 99c5c301635%7C0%7C0%7C638547372825465871%7CUnknown%7CTWFp bGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVC I6Mn0%3D%7C0%7C%7C%7C&sdata=yEmDaP33Ss4FzUHv2IFkmGLV8udn9z1 kAFzi01idssU%3D&reserved=0Will any tags (like “Fixes” and “Cc”) become relevant here?
How do you think about to take the known advice “Solve only one problem per patch”
better into account?
https://git.ker/
nel.org%2Fpub%2Fscm%2Flinux%2Fkernel%2Fgit%2Ftorvalds%2Flinux.git%2 Ftree%2FDocumentation%2Fprocess%2Fsubmitting-patches.rst%3Fh%3Dv6. 10-rc4%23n81&data=05%7C02%7Cwei.fang%40nxp.com%7Cd6bfb97d5f854 c511a3c08dc9373d064%7C686ea1d3bc2b4c6fa92cd99c5c301635%7C0%7C 0%7C638547372825472413%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4 wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C% 7C%7C&sdata=Lc2BsYHMzGkAgSsZ3pNdnvl8OlDtTL7pb0CNhD%2Bz2fg%3D& reserved=0
Quoted 3 lines
Under which circumstances will development interests grow for further approaches according to the presentation of similar change combinations?
Hi Markus,
This patch has been rejected because netdev people don't want these sort of conversions at present which will make backporting more difficult. The LOCK EVASION issue has been fixed by another patch.
Quoted 2 lines
This patch has been rejected because netdev people don't want these sort of conversions at present which will make backporting more difficult.
Advanced development tools can help to adjust involved concerns another bit. Some contributors got used to capabilities of the semantic patch language (Coccinelle software) for example.
Will any clarifications become more helpful here?
Would you get useful insights from special information sources?
Looking at guard usage (with SmPL)
https://lore.kernel.org/cocci/[email protected]/
https://sympa.inria.fr/sympa/arc/cocci/2024-05/msg00090.html
Quoted 1 line
The LOCK EVASION issue has been fixed by another patch.
https://lore.kernel.org/r/[email protected]
Further software evolution might become more interesting also around the commit 3b1c92f8e5371700fada307cc8fd2c51fa7bc8c1 ("net: fec:
avoid lock evasion when reading pps_enable").
Regards,
Markus