Skip to contentSkip to conversation

ipv6: remove useless spin_lock/spin_unlock

PATCH Superseded Duan Jiong 4 participants 6 messages Dec 3, 2014 – Dec 9, 2014

Subsystems (3)
lore
Share & tools
lore

Duan Jiong

author
#0 lore raw

PATCH 1/1 ipv6: remove useless spin_lock/spin_unlock

xchg is atomic, so there is no necessary to use spin_lock/spin_unlock to protect it.

---
 net/ipv6/ipv6_sockglue.c | 2 --
 1 file changed, 2 deletions(-)
net/ipv6/ipv6_sockglue.c +0 -2
index e1a9583..92ca907 100644
@@ -112,9 +112,7 @@ struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
 		}
 		opt = xchg(&inet6_sk(sk)->opt, opt);
 	} else {
-		spin_lock(&sk->sk_dst_lock);
 		opt = xchg(&inet6_sk(sk)->opt, opt);
-		spin_unlock(&sk->sk_dst_lock);
 	}
 	sk_dst_reset(sk);
 
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 13 lines · Duan Jiong

xchg is atomic, so there is no necessary to use spin_lock/spin_unlock to protect it.

---
 net/ipv6/ipv6_sockglue.c | 2 --
 1 file changed, 2 deletions(-)
net/ipv6/ipv6_sockglue.c +0 -0
index e1a9583..92ca907 100644

net/ipv6/ipv6_sockglue.c struct ipv6_txoptions *ipv6_update_options(struct sock *sk, · line 118

Quoted 9 lines
net/ipv6/ipv6_sockglue.c +0 -2
@@ -112,9 +112,7 @@ struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
 		}
 		opt = xchg(&inet6_sk(sk)->opt, opt);
 	} else {
-		spin_lock(&sk->sk_dst_lock);
 		opt = xchg(&inet6_sk(sk)->opt, opt);
-		spin_unlock(&sk->sk_dst_lock);
 	}
 	sk_dst_reset(sk);
 

Why keeping 2 copies of opt = xchg(&inet6_sk(sk)->opt, opt); then ?

author
#2 lore raw
Quoted 9 lines · Eric Dumazet
Quoted 8 lines · Duan Jiong

xchg is atomic, so there is no necessary to use spin_lock/spin_unlock to protect it.

---
 net/ipv6/ipv6_sockglue.c | 2 --
 1 file changed, 2 deletions(-)
Quoted 17 lines
Quoted 5 lines
net/ipv6/ipv6_sockglue.c +0 -0
index e1a9583..92ca907 100644

net/ipv6/ipv6_sockglue.c struct ipv6_txoptions *ipv6_update_options(struct sock *sk, · line 118

Quoted 9 lines
net/ipv6/ipv6_sockglue.c +0 -2
@@ -112,9 +112,7 @@ struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
 		}
 		opt = xchg(&inet6_sk(sk)->opt, opt);
 	} else {
-		spin_lock(&sk->sk_dst_lock);
 		opt = xchg(&inet6_sk(sk)->opt, opt);
-		spin_unlock(&sk->sk_dst_lock);
 	}
 	sk_dst_reset(sk);
 

Why keeping 2 copies of opt = xchg(&inet6_sk(sk)->opt, opt); then ?

Thanks for you remind, i didn't notice that.
The else statement could be removed, opt = xchg(&inet6_sk(sk)->opt, opt); should be moved out, and i will send v2.

Thanks,
Duan

Quoted 3 lines
author
#3 lore raw

PATCH v2 1/1 ipv6: remove useless spin_lock/spin_unlock

In reply to Duan Jiong

xchg is atomic, so there is no necessary to use spin_lock/spin_unlock to protect it. At last, remove the redundant
opt = xchg(&inet6_sk(sk)->opt, opt); statement.

---
v2: remove the redundant opt = xchg(&inet6_sk(sk)->opt, opt); statement.

 net/ipv6/ipv6_sockglue.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
net/ipv6/ipv6_sockglue.c +1 -5
index e1a9583..66980d8 100644
@@ -110,12 +110,8 @@ struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
 			icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen;
 			icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
 		}
-		opt = xchg(&inet6_sk(sk)->opt, opt);
-	} else {
-		spin_lock(&sk->sk_dst_lock);
-		opt = xchg(&inet6_sk(sk)->opt, opt);
-		spin_unlock(&sk->sk_dst_lock);
 	}
+	opt = xchg(&inet6_sk(sk)->opt, opt);
 	sk_dst_reset(sk);
 
 	return opt;
Signature · 1 line
1.8.3.1
#4 lore raw
Quoted 19 lines · Duan Jiong

xchg is atomic, so there is no necessary to use spin_lock/spin_unlock to protect it. At last, remove the redundant
opt = xchg(&inet6_sk(sk)->opt, opt); statement.

---
v2: remove the redundant opt = xchg(&inet6_sk(sk)->opt, opt); statement.

 net/ipv6/ipv6_sockglue.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
net/ipv6/ipv6_sockglue.c +0 -0
index e1a9583..66980d8 100644
@@ -110,12 +110,8 @@ struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
 			icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen;
 			icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
 		}

net/ipv6/ipv6_sockglue.c struct ipv6_txoptions *ipv6_update_options(struct sock *sk, · line 117

Quoted 10 lines
net/ipv6/ipv6_sockglue.c +1 -5
-		opt = xchg(&inet6_sk(sk)->opt, opt);
-	} else {
-		spin_lock(&sk->sk_dst_lock);
-		opt = xchg(&inet6_sk(sk)->opt, opt);
-		spin_unlock(&sk->sk_dst_lock);
 	}
+	opt = xchg(&inet6_sk(sk)->opt, opt);
 	sk_dst_reset(sk);
 
 	return opt;

The original function could now be written
integrating the multiple ifs like:

---
 net/ipv6/ipv6_sockglue.c | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)
net/ipv6/ipv6_sockglue.c +12 -13
index e1a9583..12c9c6b 100644
@@ -102,20 +102,19 @@ static
 struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
 					   struct ipv6_txoptions *opt)
 {
-	if (inet_sk(sk)->is_icsk) {
-		if (opt &&
-		    !((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
-		    inet_sk(sk)->inet_daddr != LOOPBACK4_IPV6) {
-			struct inet_connection_sock *icsk = inet_csk(sk);
-			icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen;
-			icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
-		}
-		opt = xchg(&inet6_sk(sk)->opt, opt);
-	} else {
-		spin_lock(&sk->sk_dst_lock);
-		opt = xchg(&inet6_sk(sk)->opt, opt);
-		spin_unlock(&sk->sk_dst_lock);
+	struct inet_sock *inet = inet_sk(sk);
+
+	if (inet->is_icsk &&
+	    opt &&
+	    !((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
+	    inet->inet_daddr != LOOPBACK4_IPV6) {
+		struct inet_connection_sock *icsk = inet_csk(sk);
+
+		icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen;
+		icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
 	}
+
+	opt = xchg(&inet6_sk(sk)->opt, opt);
 	sk_dst_reset(sk);
 
 	return opt;
maintainer Applied
#5 lore raw
In reply to Duan Jiong

From: Duan Jiong <[email protected]>
Date: Wed, 3 Dec 2014 10:29:40 +0800

Quoted 7 lines

xchg is atomic, so there is no necessary to use spin_lock/spin_unlock to protect it. At last, remove the redundant
opt = xchg(&inet6_sk(sk)->opt, opt); statement.

---
v2: remove the redundant opt = xchg(&inet6_sk(sk)->opt, opt); statement.

Applied, thank you.