Skip to contentSkip to conversation

netfilter: conntrack: don't acquire lock during seq_printf

PATCH In review Florian Westphal 2 participants 5 messages Apr 11, 2016 – Apr 14, 2016 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 netfilter: conntrack: don't acquire lock during seq_printf

read access doesn't need any lock here.

---
 net/netfilter/nf_conntrack_proto_sctp.c | 8 +-------
 net/netfilter/nf_conntrack_proto_tcp.c  | 8 +-------
 2 files changed, 2 insertions(+), 14 deletions(-)
net/netfilter/nf_conntrack_proto_sctp.c +1 -7
index 9578a7c..1d7ab96 100644
@@ -191,13 +191,7 @@ static void sctp_print_tuple(struct seq_file *s,
 /* Print out the private part of the conntrack. */
 static void sctp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
 {
-	enum sctp_conntrack state;
-
-	spin_lock_bh(&ct->lock);
-	state = ct->proto.sctp.state;
-	spin_unlock_bh(&ct->lock);
-
-	seq_printf(s, "%s ", sctp_conntrack_names[state]);
+	seq_printf(s, "%s ", sctp_conntrack_names[ct->proto.sctp.state]);
 }
 
 #define for_each_sctp_chunk(skb, sch, _sch, offset, dataoff, count)	\
net/netfilter/nf_conntrack_proto_tcp.c +1 -7
index 278f3b9..e0cb0ce 100644
@@ -313,13 +313,7 @@ static void tcp_print_tuple(struct seq_file *s,
 /* Print out the private part of the conntrack. */
 static void tcp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
 {
-	enum tcp_conntrack state;
-
-	spin_lock_bh(&ct->lock);
-	state = ct->proto.tcp.state;
-	spin_unlock_bh(&ct->lock);
-
-	seq_printf(s, "%s ", tcp_conntrack_names[state]);
+	seq_printf(s, "%s ", tcp_conntrack_names[ct->proto.tcp.state]);
 }
 
 static unsigned int get_conntrack_index(const struct tcphdr *tcph)
Signature · 1 line
2.7.3

A message that is not in the archive (sent off-list or not yet received): 20160414094346.GA2056@salvia

#2 lore raw
Quoted 13 lines · Florian Westphal

read access doesn't need any lock here.

---
 net/netfilter/nf_conntrack_proto_sctp.c | 8 +-------
 net/netfilter/nf_conntrack_proto_tcp.c  | 8 +-------
 2 files changed, 2 insertions(+), 14 deletions(-)
net/netfilter/nf_conntrack_proto_sctp.c +0 -0
index 9578a7c..1d7ab96 100644

net/netfilter/nf_conntrack_proto_sctp.c static void sctp_print_tuple(struct seq_file *s, · line 197

Quoted 7 lines
net/netfilter/nf_conntrack_proto_sctp.c +0 -4
@@ -191,13 +191,7 @@ static void sctp_print_tuple(struct seq_file *s,
 /* Print out the private part of the conntrack. */
 static void sctp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
 {
-	enum sctp_conntrack state;
-
-	spin_lock_bh(&ct->lock);
-	state = ct->proto.sctp.state;

Don't we need at least READ_ONCE() here?

Quoted 7 lines
net/netfilter/nf_conntrack_proto_sctp.c +1 -3
-	spin_unlock_bh(&ct->lock);
-
-	seq_printf(s, "%s ", sctp_conntrack_names[state]);
+	seq_printf(s, "%s ", sctp_conntrack_names[ct->proto.sctp.state]);
 }
 
 #define for_each_sctp_chunk(skb, sch, _sch, offset, dataoff, count)	\
author maintainer
#3 lore raw
Quoted 4 lines · Pablo Neira Ayuso
Quoted 4 lines
 net/netfilter/nf_conntrack_proto_sctp.c | 8 +-------
 net/netfilter/nf_conntrack_proto_tcp.c  | 8 +-------
 2 files changed, 2 insertions(+), 14 deletions(-)
Quoted 14 lines

net/netfilter/nf_conntrack_proto_sctp.c static void sctp_print_tuple(struct seq_file *s, · line 197

Quoted 12 lines
net/netfilter/nf_conntrack_proto_sctp.c +0 -4
index 9578a7c..1d7ab96 100644
@@ -191,13 +191,7 @@ static void sctp_print_tuple(struct seq_file *s,
 /* Print out the private part of the conntrack. */
 static void sctp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
 {
-	enum sctp_conntrack state;
-
-	spin_lock_bh(&ct->lock);
-	state = ct->proto.sctp.state;

Don't we need at least READ_ONCE() here?

Why?

seq_printf(s, "%s ", sctp_conntrack_names[ct->proto.sctp.state]);

I think thats fine, where do you see a problem?

#4 lore raw
Quoted 20 lines · Florian Westphal
Quoted 4 lines · Pablo Neira Ayuso
Quoted 4 lines
 net/netfilter/nf_conntrack_proto_sctp.c | 8 +-------
 net/netfilter/nf_conntrack_proto_tcp.c  | 8 +-------
 2 files changed, 2 insertions(+), 14 deletions(-)
Quoted 14 lines

net/netfilter/nf_conntrack_proto_sctp.c static void sctp_print_tuple(struct seq_file *s, · line 197

Quoted 12 lines
net/netfilter/nf_conntrack_proto_sctp.c +0 -4
index 9578a7c..1d7ab96 100644
@@ -191,13 +191,7 @@ static void sctp_print_tuple(struct seq_file *s,
 /* Print out the private part of the conntrack. */
 static void sctp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
 {
-	enum sctp_conntrack state;
-
-	spin_lock_bh(&ct->lock);
-	state = ct->proto.sctp.state;

Don't we need at least READ_ONCE() here?

Quoted 5 lines

Why?

seq_printf(s, "%s ", sctp_conntrack_names[ct->proto.sctp.state]);

I think thats fine, where do you see a problem?

ct->proto.sctp.state may be modified from another cpu while reading this, is this read guaranteed to be atomic in any arch?

author maintainer
#5 lore raw
Quoted 21 lines · Pablo Neira Ayuso
Quoted 20 lines · Florian Westphal
Quoted 18 lines · Pablo Neira Ayuso
Quoted 9 lines
 net/netfilter/nf_conntrack_proto_sctp.c | 8 +-------
 net/netfilter/nf_conntrack_proto_tcp.c  | 8 +-------
 2 files changed, 2 insertions(+), 14 deletions(-)
net/netfilter/nf_conntrack_proto_sctp.c +0 -0
index 9578a7c..1d7ab96 100644

net/netfilter/nf_conntrack_proto_sctp.c static void sctp_print_tuple(struct seq_file *s, · line 197

Quoted 7 lines
net/netfilter/nf_conntrack_proto_sctp.c +0 -4
@@ -191,13 +191,7 @@ static void sctp_print_tuple(struct seq_file *s,
 /* Print out the private part of the conntrack. */
 static void sctp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
 {
-	enum sctp_conntrack state;
-
-	spin_lock_bh(&ct->lock);
-	state = ct->proto.sctp.state;

Don't we need at least READ_ONCE() here?

Quoted 8 lines
Quoted 5 lines

Why?

seq_printf(s, "%s ", sctp_conntrack_names[ct->proto.sctp.state]);

I think thats fine, where do you see a problem?

ct->proto.sctp.state may be modified from another cpu while reading this, is this read guaranteed to be atomic in any arch?

Yes, safe on all. Only problem would be if we change sctp.state to u64, we'd have to add locking for 32bit arches.

I can add a compiletime_assert_atomic_type() for this but I think its not needed.

I don't feel strongly about it; if you prefer locking here just mark patch as rejected.