Skip to contentSkip to conversation

net: fec: avoid lock evasion when reading pps_enable

PATCH Superseded Wei Fang 4 participants 10 messages May 13, 2024 – May 14, 2024

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Wei Fang

author maintainer
#0 lore raw

PATCH 1/1 net: fec: avoid lock evasion when reading pps_enable

The assignment of pps_enable is protected by tmreg_lock, but the read operation of pps_enable is not. So the Coverity tool reports a lock evasion warning which may cause data race to occur when running in a multithread environment. Although this issue is almost impossible to occur, we'd better fix it, at least it seems more logically reasonable, and it also prevents Coverity from continuing to issue warnings.

Fixes 278d24047891 ("net: fec: ptp: Enable PPS output based on ptp clock")
---
 drivers/net/ethernet/freescale/fec_ptp.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/net/ethernet/freescale/fec_ptp.c +5 -3
index 181d9bfbee22..8d37274a3fb0 100644
@@ -104,14 +104,16 @@ static int fec_ptp_enable_pps(struct fec_enet_private *fep, uint enable)
 	struct timespec64 ts;
 	u64 ns;
 
-	if (fep->pps_enable == enable)
-		return 0;
-
 	fep->pps_channel = DEFAULT_PPS_CHANNEL;
 	fep->reload_period = PPS_OUPUT_RELOAD_PERIOD;
 
 	spin_lock_irqsave(&fep->tmreg_lock, flags);
 
+	if (fep->pps_enable == enable) {
+		spin_unlock_irqrestore(&fep->tmreg_lock, flags);
+		return 0;
+	}
+
 	if (enable) {
 		/* clear capture or output compare interrupt status if have.
 		 */
Signature · 1 line
2.34.1
#1 lore raw
Quoted 19 lines · Wei Fang

The assignment of pps_enable is protected by tmreg_lock, but the read operation of pps_enable is not. So the Coverity tool reports a lock evasion warning which may cause data race to occur when running in a multithread environment. Although this issue is almost impossible to occur, we'd better fix it, at least it seems more logically reasonable, and it also prevents Coverity from continuing to issue warnings.

Fixes 278d24047891 ("net: fec: ptp: Enable PPS output based on ptp clock")
---
 drivers/net/ethernet/freescale/fec_ptp.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/net/ethernet/freescale/fec_ptp.c +0 -0
index 181d9bfbee22..8d37274a3fb0 100644

drivers/net/ethernet/freescale/fec_ptp.c static int fec_ptp_enable_pps(struct fec_enet_private *fep, uint enable) · line 108

Quoted 8 lines
drivers/net/ethernet/freescale/fec_ptp.c +0 -3
@@ -104,14 +104,16 @@ static int fec_ptp_enable_pps(struct fec_enet_private *fep, uint enable)
        struct timespec64 ts;
        u64 ns;
 
-       if (fep->pps_enable == enable)
-               return 0;
-
        fep->pps_channel = DEFAULT_PPS_CHANNEL;
        fep->reload_period = PPS_OUPUT_RELOAD_PERIOD;

Why are these writes left without the spinlock protection ?

Quoted 14 lines
drivers/net/ethernet/freescale/fec_ptp.c +5 -1
        spin_lock_irqsave(&fep->tmreg_lock, flags);
 
+       if (fep->pps_enable == enable) {
+               spin_unlock_irqrestore(&fep->tmreg_lock, flags);
+               return 0;
+       }
+
        if (enable) {
                /* clear capture or output compare interrupt status if have.
                 */
--

2.34.1

author maintainer
#2 lore raw
Quoted 36 lines

-----Original Message-----
From: Eric Dumazet <[email protected]>
Sent: 2024年5月13日 15:29
To: Wei Fang <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; Shenwei Wang <[email protected]>; Clark Wang <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH net] net: fec: avoid lock evasion when reading pps_enable

drivers/net/ethernet/freescale/fec_ptp.c static int fec_ptp_enable_pps(struct

Quoted 21 lines · Wei Fang

The assignment of pps_enable is protected by tmreg_lock, but the read operation of pps_enable is not. So the Coverity tool reports a lock evasion warning which may cause data race to occur when running in a multithread environment. Although this issue is almost impossible to occur, we'd better fix it, at least it seems more logically reasonable, and it also prevents Coverity from continuing to issue warnings.

Fixes 278d24047891 ("net: fec: ptp: Enable PPS output based on ptp

clock")

---
 drivers/net/ethernet/freescale/fec_ptp.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/net/ethernet/freescale/fec_ptp.c +0 -0
index 181d9bfbee22..8d37274a3fb0 100644
@@ -104,14 +104,16 @@ static int fec_ptp_enable_pps(struct

fec_enet_private *fep, uint enable)

Quoted 3 lines
drivers/net/ethernet/freescale/fec_ptp.c +0 -0
        struct timespec64 ts;
        u64 ns;
 
Quoted 7 lines

drivers/net/ethernet/freescale/fec_ptp.c static int fec_ptp_enable_pps(struct fec_enet_private *fep, uint enable) · line 108

Quoted 5 lines
drivers/net/ethernet/freescale/fec_ptp.c +0 -3
-       if (fep->pps_enable == enable)
-               return 0;
-
        fep->pps_channel = DEFAULT_PPS_CHANNEL;
        fep->reload_period = PPS_OUPUT_RELOAD_PERIOD;

Why are these writes left without the spinlock protection ?

For fec driver, the pps_channel and the reload_period of PPS request are always fixed, and they were also not protected by the lock in the original code.

Quoted 17 lines

drivers/net/ethernet/freescale/fec_ptp.c static int fec_ptp_enable_pps(struct fec_enet_private *fep, uint enable)

Quoted 12 lines
drivers/net/ethernet/freescale/fec_ptp.c +5 -0
        spin_lock_irqsave(&fep->tmreg_lock, flags);
 
+       if (fep->pps_enable == enable) {
+               spin_unlock_irqrestore(&fep->tmreg_lock, flags);
+               return 0;
+       }
+
        if (enable) {
                /* clear capture or output compare interrupt status if

have.

Quoted 4 lines
drivers/net/ethernet/freescale/fec_ptp.c +0 -1
                 */
--

2.34.1

#3 lore raw
Quoted 43 lines · Wei Fang
Quoted 43 lines

-----Original Message-----
From: Eric Dumazet <[email protected]>
Sent: 2024年5月13日 15:29
To: Wei Fang <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; Shenwei Wang <[email protected]>; Clark Wang <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH net] net: fec: avoid lock evasion when reading pps_enable

drivers/net/ethernet/freescale/fec_ptp.c static int fec_ptp_enable_pps(struct

Quoted 21 lines · Wei Fang

The assignment of pps_enable is protected by tmreg_lock, but the read operation of pps_enable is not. So the Coverity tool reports a lock evasion warning which may cause data race to occur when running in a multithread environment. Although this issue is almost impossible to occur, we'd better fix it, at least it seems more logically reasonable, and it also prevents Coverity from continuing to issue warnings.

Fixes 278d24047891 ("net: fec: ptp: Enable PPS output based on ptp

clock")

---
 drivers/net/ethernet/freescale/fec_ptp.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/net/ethernet/freescale/fec_ptp.c +0 -0
index 181d9bfbee22..8d37274a3fb0 100644
@@ -104,14 +104,16 @@ static int fec_ptp_enable_pps(struct

fec_enet_private *fep, uint enable)

Quoted 8 lines
drivers/net/ethernet/freescale/fec_ptp.c +0 -3
        struct timespec64 ts;
        u64 ns;
 
-       if (fep->pps_enable == enable)
-               return 0;
-
        fep->pps_channel = DEFAULT_PPS_CHANNEL;
        fep->reload_period = PPS_OUPUT_RELOAD_PERIOD;
Quoted 4 lines
Quoted 1 line

Why are these writes left without the spinlock protection ?

For fec driver, the pps_channel and the reload_period of PPS request are always fixed, and they were also not protected by the lock in the original code.

If this is the case, please move this initialization elsewhere, so that we can be absolutely sure of the claim.

I see fep->reload_period being overwritten in this file, I do not see clear evidence this is all safe.

author maintainer
#4 lore raw
Quoted 61 lines

-----Original Message-----
From: Eric Dumazet <[email protected]>
Sent: 2024年5月13日 16:41
To: Wei Fang <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; Shenwei Wang <[email protected]>; Clark Wang <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH net] net: fec: avoid lock evasion when reading pps_enable

Quoted 6 lines · Wei Fang
Quoted 6 lines

-----Original Message-----
From: Eric Dumazet <[email protected]>
Sent: 2024年5月13日 15:29
To: Wei Fang <[email protected]>
Cc: [email protected]; [email protected]; [email protected];

Shenwei

Quoted 1 line
Quoted 1 line

Wang <[email protected]>; Clark Wang

<[email protected]>;

Quoted 6 lines
Quoted 6 lines

[email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH net] net: fec: avoid lock evasion when reading pps_enable

On Mon, May 13, 2024 at 4:02 AM Wei Fang <[email protected]>

Quoted 7 lines · wrote:
Quoted 7 lines
Quoted 7 lines

The assignment of pps_enable is protected by tmreg_lock, but the read operation of pps_enable is not. So the Coverity tool reports a lock evasion warning which may cause data race to occur when running in a multithread environment. Although this issue is almost impossible to occur, we'd better fix it, at least it seems more logically reasonable, and it also prevents Coverity from continuing

to issue warnings.

Quoted 25 lines
Quoted 25 lines

drivers/net/ethernet/freescale/fec_ptp.c static int fec_ptp_enable_pps(struct

Quoted 15 lines
Fixes 278d24047891 ("net: fec: ptp: Enable PPS output based on

ptp
clock")

---
 drivers/net/ethernet/freescale/fec_ptp.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/net/ethernet/freescale/fec_ptp.c +0 -0
index 181d9bfbee22..8d37274a3fb0 100644
@@ -104,14 +104,16 @@ static int fec_ptp_enable_pps(struct

fec_enet_private *fep, uint enable)

Quoted 8 lines
drivers/net/ethernet/freescale/fec_ptp.c +0 -3
        struct timespec64 ts;
        u64 ns;
 
-       if (fep->pps_enable == enable)
-               return 0;
-
        fep->pps_channel = DEFAULT_PPS_CHANNEL;
        fep->reload_period = PPS_OUPUT_RELOAD_PERIOD;
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Why are these writes left without the spinlock protection ?

For fec driver, the pps_channel and the reload_period of PPS request are always fixed, and they were also not protected by the lock in the original code.

If this is the case, please move this initialization elsewhere, so that we can be absolutely sure of the claim.

Accept, thanks

Quoted 2 lines

I see fep->reload_period being overwritten in this file, I do not see clear evidence this is all safe.

#5 lore raw

PATCH 1/1 net: fec: avoid lock evasion when reading pps_enable

In reply to Wei Fang

See inline,

Quoted 22 lines

The assignment of pps_enable is protected by tmreg_lock, but the read operation of pps_enable is not. So the Coverity tool reports a lock evasion warning which may cause data race to occur when running in a multithread environment. Although this issue is almost impossible to occur, we'd better fix it, at least it seems more logically reasonable, and it also prevents Coverity from continuing to issue warnings.

Fixes 278d24047891 ("net: fec: ptp: Enable PPS output based on ptp clock")
---
 drivers/net/ethernet/freescale/fec_ptp.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/net/ethernet/freescale/fec_ptp.c +0 -0
index 181d9bfbee22..8d37274a3fb0 100644
@@ -104,14 +104,16 @@ static int fec_ptp_enable_pps(struct
 fec_enet_private *fep, uint enable)
 	struct timespec64 ts;
 	u64 ns;

drivers/net/ethernet/freescale/fec_ptp.c static int fec_ptp_enable_pps(struct · line 113

Quoted 10 lines
drivers/net/ethernet/freescale/fec_ptp.c +1 -3
 
-	if (fep->pps_enable == enable)
-		return 0;
-
 	fep->pps_channel = DEFAULT_PPS_CHANNEL;
 	fep->reload_period = PPS_OUPUT_RELOAD_PERIOD;
 
 	spin_lock_irqsave(&fep->tmreg_lock, flags);
 
+	if (fep->pps_enable == enable) {

Can we atomic_set/get instead of spin_lock here.

Thanks,
Hariprasad k

Quoted 10 lines
drivers/net/ethernet/freescale/fec_ptp.c +4 -0
+		spin_unlock_irqrestore(&fep->tmreg_lock, flags);
+		return 0;
+	}
+
 	if (enable) {
 		/* clear capture or output compare interrupt status if have.

*/
--
2.34.1

author maintainer
#6 lore raw
Quoted 42 lines

-----Original Message-----
From: Hariprasad Kelam <[email protected]>
Sent: 2024年5月13日 17:27
To: Wei Fang <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Shenwei Wang <[email protected]>; Clark Wang <[email protected]>; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]
Subject: [PATCH net] net: fec: avoid lock evasion when reading pps_enable

See inline,

Quoted 30 lines

The assignment of pps_enable is protected by tmreg_lock, but the read operation of pps_enable is not. So the Coverity tool reports a lock evasion warning which may cause data race to occur when running in a multithread environment. Although this issue is almost impossible to occur, we'd better fix it, at least it seems more logically reasonable, and it also prevents Coverity from continuing to issue warnings.

Fixes 278d24047891 ("net: fec: ptp: Enable PPS output based on ptp

clock")

---
 drivers/net/ethernet/freescale/fec_ptp.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/net/ethernet/freescale/fec_ptp.c +0 -3
index 181d9bfbee22..8d37274a3fb0 100644
@@ -104,14 +104,16 @@ static int fec_ptp_enable_pps(struct
 fec_enet_private *fep, uint enable)
 	struct timespec64 ts;
 	u64 ns;
 
-	if (fep->pps_enable == enable)
-		return 0;
-
 	fep->pps_channel = DEFAULT_PPS_CHANNEL;
 	fep->reload_period = PPS_OUPUT_RELOAD_PERIOD;
 
Quoted 6 lines

drivers/net/ethernet/freescale/fec_ptp.c static int fec_ptp_enable_pps(struct fec_enet_private *fep, uint enable) · line 112

Quoted 3 lines
drivers/net/ethernet/freescale/fec_ptp.c +1 -0
 	spin_lock_irqsave(&fep->tmreg_lock, flags);
 
+	if (fep->pps_enable == enable) {

Can we atomic_set/get instead of spin_lock here.

I'm afraid that cannot eliminate the lock evasion warning, because it's still possible that multithreads take the false branch of "if (fep->pps_enable == enable)" before pps_enable is updated.

Quoted 12 lines

Thanks,
Hariprasad k

Quoted 10 lines
drivers/net/ethernet/freescale/fec_ptp.c +4 -0
+		spin_unlock_irqrestore(&fep->tmreg_lock, flags);
+		return 0;
+	}
+
 	if (enable) {
 		/* clear capture or output compare interrupt status if have.

*/
--
2.34.1

#7 lore raw
Quoted 52 lines
Quoted 6 lines

-----Original Message-----
From: Hariprasad Kelam <[email protected]>
Sent: 2024年5月13日 17:27
To: Wei Fang <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Shenwei

Wang

Quoted 6 lines

<[email protected]>; Clark Wang <[email protected]>; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]
Subject: [PATCH net] net: fec: avoid lock evasion when reading pps_enable

Quoted 8 lines

See inline,

Quoted 6 lines

The assignment of pps_enable is protected by tmreg_lock, but the read operation of pps_enable is not. So the Coverity tool reports a lock evasion warning which may cause data race to occur when running in a multithread environment. Although this issue is almost impossible to occur, we'd better fix it, at least it seems more logically reasonable, and it also prevents Coverity from continuing to issue

warnings.

Quoted 24 lines
Quoted 24 lines
Fixes 278d24047891 ("net: fec: ptp: Enable PPS output based on ptp

clock")

---
 drivers/net/ethernet/freescale/fec_ptp.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/net/ethernet/freescale/fec_ptp.c +0 -3
index 181d9bfbee22..8d37274a3fb0 100644
@@ -104,14 +104,16 @@ static int fec_ptp_enable_pps(struct
 fec_enet_private *fep, uint enable)
 	struct timespec64 ts;
 	u64 ns;
 
-	if (fep->pps_enable == enable)
-		return 0;
-
 	fep->pps_channel = DEFAULT_PPS_CHANNEL;
 	fep->reload_period = PPS_OUPUT_RELOAD_PERIOD;
 
Quoted 6 lines

drivers/net/ethernet/freescale/fec_ptp.c static int fec_ptp_enable_pps(struct fec_enet_private *fep, uint enable) · line 112

Quoted 3 lines
drivers/net/ethernet/freescale/fec_ptp.c +1 -0
 	spin_lock_irqsave(&fep->tmreg_lock, flags);
 
+	if (fep->pps_enable == enable) {

Can we atomic_set/get instead of spin_lock here.

Quoted 5 lines

I'm afraid that cannot eliminate the lock evasion warning, because it's still possible that multithreads take the false branch of "if (fep->pps_enable == enable)" before pps_enable is updated.

Since in fec_ptp_enable_pps(), value of pps_enable is checked before entering the actual code changes,

Better approach is to use atomic_read/write. This is not only for reading but for assigning the values as well. This way covertity wont complain.

Quoted 12 lines
Quoted 12 lines

Thanks,
Hariprasad k

Quoted 10 lines
drivers/net/ethernet/freescale/fec_ptp.c +4 -0
+		spin_unlock_irqrestore(&fep->tmreg_lock, flags);
+		return 0;
+	}
+
 	if (enable) {
 		/* clear capture or output compare interrupt status if have.

*/
--
2.34.1

reviewer Reviewed-by on v1 1/1
#8 lore raw
In reply to Wei Fang
Quoted 9 lines · Wei Fang

The assignment of pps_enable is protected by tmreg_lock, but the read operation of pps_enable is not. So the Coverity tool reports a lock evasion warning which may cause data race to occur when running in a multithread environment. Although this issue is almost impossible to occur, we'd better fix it, at least it seems more logically reasonable, and it also prevents Coverity from continuing to issue warnings.

Fixes 278d24047891 ("net: fec: ptp: Enable PPS output based on ptp clock")
reviewer
#9 lore raw
Quoted 8 lines · Simon Horman
Quoted 7 lines · Wei Fang

The assignment of pps_enable is protected by tmreg_lock, but the read operation of pps_enable is not. So the Coverity tool reports a lock evasion warning which may cause data race to occur when running in a multithread environment. Although this issue is almost impossible to occur, we'd better fix it, at least it seems more logically reasonable, and it also prevents Coverity from continuing to issue warnings.

Quoted 4 lines
Quoted 2 lines
Fixes 278d24047891 ("net: fec: ptp: Enable PPS output based on ptp clock")

Sorry, I convinced myself this is correct.
But I now see that questions have been raised by others. So please ignore the above.