netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steffen Klassert <steffen.klassert@secunet.com>
To: David Miller <davem@davemloft.net>
Cc: timo.teras@iki.fi, netdev@vger.kernel.org
Subject: [PATCH 3/4] inetpeer: protect the inetpeerpeer metrics with rcu
Date: Thu, 2 Feb 2012 11:13:36 +0100	[thread overview]
Message-ID: <20120202101336.GF23142@secunet.com> (raw)
In-Reply-To: <20120202101141.GC23142@secunet.com>

In order to be able to exchange the inetpeer metrics,
we add rcu protection to the struct inetpeer_metrics.

Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
---
 include/net/dst.h      |   21 +++++++++++++++++----
 include/net/inetpeer.h |    8 ++++++--
 net/ipv4/inetpeer.c    |   26 ++++++++++++++++++++++++++
 net/ipv4/route.c       |    6 +++++-
 net/ipv6/route.c       |    6 +++++-
 5 files changed, 59 insertions(+), 8 deletions(-)

diff --git a/include/net/dst.h b/include/net/dst.h
index be1ffc8..f1141d8 100644
--- a/include/net/dst.h
+++ b/include/net/dst.h
@@ -159,21 +159,31 @@ static inline void dst_init_metrics(struct dst_entry *dst,
 
 static inline void dst_copy_metrics(struct dst_entry *dest, const struct dst_entry *src)
 {
-	u32 *dst_metrics = dst_metrics_write_ptr(dest);
+	u32 *dst_metrics;
+
+	rcu_read_lock();
+	dst_metrics = dst_metrics_write_ptr(dest);
 
 	if (dst_metrics) {
 		u32 *src_metrics = dst_metrics_ptr(src);
 
 		memcpy(dst_metrics, src_metrics, RTAX_MAX * sizeof(u32));
 	}
+	rcu_read_unlock();
 }
 
 static inline u32
 dst_metric_raw(const struct dst_entry *dst, const int metric)
 {
-	u32 *p = dst_metrics_ptr(dst);
+	u32 *p;
+	u32 val;
+
+	rcu_read_lock();
+	p = dst_metrics_ptr(dst);
+	val = p[metric-1];
+	rcu_read_unlock();
 
-	return p[metric-1];
+	return val;
 }
 
 static inline u32
@@ -198,10 +208,13 @@ dst_metric_advmss(const struct dst_entry *dst)
 
 static inline void dst_metric_set(struct dst_entry *dst, int metric, u32 val)
 {
-	u32 *p = dst_metrics_write_ptr(dst);
+	u32 *p;
 
+	rcu_read_lock();
+	p = dst_metrics_write_ptr(dst);
 	if (p)
 		p[metric-1] = val;
+	rcu_read_unlock();
 }
 
 static inline u32
diff --git a/include/net/inetpeer.h b/include/net/inetpeer.h
index 6bb8060..d97211a 100644
--- a/include/net/inetpeer.h
+++ b/include/net/inetpeer.h
@@ -28,6 +28,7 @@ struct inetpeer_addr {
 };
 
 struct inetpeer_metrics {
+	struct rcu_head	rcu;
 	u32		m[RTAX_MAX];
 };
 
@@ -37,7 +38,7 @@ struct inet_peer {
 	struct inetpeer_addr	daddr;
 	__u32			avl_height;
 
-	struct inetpeer_metrics *metrics;
+	struct inetpeer_metrics __rcu *metrics;
 	u32			rate_tokens;	/* rate limiting for ICMP */
 	int			redirect_genid;
 	unsigned long		rate_last;
@@ -75,9 +76,11 @@ static inline bool inet_metrics_new(const struct inet_peer *p)
 	return p->metrics->m[RTAX_LOCK-1] == INETPEER_METRICS_NEW;
 }
 
+/* called in rcu_read_lock() section */
 static inline u32 *inetpeer_metrics(const struct inet_peer *p)
 {
-	return p->metrics->m;
+	struct inetpeer_metrics *metrics = rcu_dereference(p->metrics);
+	return metrics->m;
 }
 
 /* can be called with or without local BH being disabled */
@@ -130,4 +133,5 @@ static inline int inet_getid(struct inet_peer *p, int more)
 	return new;
 }
 
+extern bool inetpeer_reset_metrics(struct inet_peer *p);
 #endif /* _NET_INETPEER_H */
diff --git a/net/ipv4/inetpeer.c b/net/ipv4/inetpeer.c
index 92071a4..2bf23a9 100644
--- a/net/ipv4/inetpeer.c
+++ b/net/ipv4/inetpeer.c
@@ -519,3 +519,29 @@ bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout)
 	return rc;
 }
 EXPORT_SYMBOL(inet_peer_xrlim_allow);
+
+bool inetpeer_reset_metrics(struct inet_peer *p)
+{
+	struct inetpeer_metrics *old, *prev, *new;
+
+	if (inet_metrics_new(p))
+		return true;
+
+	new = kmalloc(sizeof(struct inetpeer_metrics), GFP_ATOMIC);
+	if (!new)
+		return false;
+
+	new->m[RTAX_LOCK-1] = INETPEER_METRICS_NEW;
+
+	old = p->metrics;
+
+	prev = cmpxchg(&p->metrics, old, new);
+	if (prev != old) {
+		kfree(new);
+		return false;
+	}
+
+	kfree_rcu(old, rcu);
+	return true;
+}
+EXPORT_SYMBOL(inetpeer_reset_metrics);
diff --git a/net/ipv4/route.c b/net/ipv4/route.c
index dc22d6f..1a88484 100644
--- a/net/ipv4/route.c
+++ b/net/ipv4/route.c
@@ -2971,6 +2971,7 @@ static int rt_fill_info(struct net *net,
 	unsigned long expires = 0;
 	const struct inet_peer *peer = rt->peer;
 	u32 id = 0, ts = 0, tsage = 0, error;
+	int ret;
 
 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*r), flags);
 	if (nlh == NULL)
@@ -3010,7 +3011,10 @@ static int rt_fill_info(struct net *net,
 	if (rt->rt_dst != rt->rt_gateway)
 		NLA_PUT_BE32(skb, RTA_GATEWAY, rt->rt_gateway);
 
-	if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
+	rcu_read_lock();
+	ret = rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst));
+	rcu_read_unlock();
+	if (ret < 0)
 		goto nla_put_failure;
 
 	if (rt->rt_mark)
diff --git a/net/ipv6/route.c b/net/ipv6/route.c
index d8b01c0..5a1c256 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -2378,6 +2378,7 @@ static int rt6_fill_node(struct net *net,
 	u32 table;
 	struct neighbour *n;
 	u32 ts, tsage;
+	int ret;
 
 	if (prefix) {	/* user wants prefix routes only */
 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
@@ -2463,7 +2464,10 @@ static int rt6_fill_node(struct net *net,
 		NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
 	}
 
-	if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
+	rcu_read_lock();
+	ret = rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst));
+	rcu_read_unlock();
+	if (ret < 0)
 		goto nla_put_failure;
 
 	rcu_read_lock();
-- 
1.7.0.4

  parent reply	other threads:[~2012-02-02 10:13 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-02 10:11 [PATCH 0/4] Fix routing metrics Steffen Klassert
2012-02-02 10:12 ` [PATCH 1/4] inetpeer: Allocate the peer metrics dynamically Steffen Klassert
2012-02-02 10:12 ` [PATCH 2/4] net: Unlink the inetpeer metrics from dst_entry Steffen Klassert
2012-02-02 10:13 ` Steffen Klassert [this message]
2012-02-02 10:14 ` [PATCH 4/4] route: Invalidate the peer metrics along with the routing cache Steffen Klassert
2012-02-06 20:29 ` [PATCH 0/4] Fix routing metrics David Miller
2012-02-08  7:30   ` Steffen Klassert
2012-02-08 20:18     ` David Miller
2012-02-09 12:44       ` Steffen Klassert
2012-02-09 18:40         ` David Miller
2012-02-10  6:50           ` Steffen Klassert
2012-02-10  7:38             ` David Miller
2012-02-10  7:51               ` Steffen Klassert
2012-02-10  8:12                 ` David Miller
2012-02-10  8:44                   ` Steffen Klassert
2012-02-10 18:25                     ` David Miller
2012-02-21  6:19                       ` Steffen Klassert
2012-02-21  6:36                         ` David Miller
2012-02-21  8:18                           ` Steffen Klassert
2012-02-21 19:24                             ` David Miller
2012-02-24  9:08                               ` Steffen Klassert
2012-02-24  9:13                                 ` David Miller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120202101336.GF23142@secunet.com \
    --to=steffen.klassert@secunet.com \
    --cc=davem@davemloft.net \
    --cc=netdev@vger.kernel.org \
    --cc=timo.teras@iki.fi \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).