Skip to content

Commit 5faa5df

Browse files
klassertdavem330
authored andcommitted
inetpeer: Invalidate the inetpeer tree along with the routing cache
We initialize the routing metrics with the values cached on the inetpeer in rt_init_metrics(). So if we have the metrics cached on the inetpeer, we ignore the user configured fib_metrics. To fix this issue, we replace the old tree with a fresh initialized inet_peer_base. The old tree is removed later with a delayed work queue. Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent dcf353b commit 5faa5df

3 files changed

Lines changed: 83 additions & 1 deletion

File tree

‎include/net/inetpeer.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ struct inet_peer {
4141
u32 pmtu_orig;
4242
u32 pmtu_learned;
4343
struct inetpeer_addr_base redirect_learned;
44+
struct list_head gc_list;
4445
/*
4546
* Once inet_peer is queued for deletion (refcnt == -1), following fields
4647
* are not available: rid, ip_id_count, tcp_ts, tcp_ts_stamp
@@ -96,6 +97,8 @@ static inline struct inet_peer *inet_getpeer_v6(const struct in6_addr *v6daddr,
9697
extern void inet_putpeer(struct inet_peer *p);
9798
extern bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout);
9899

100+
extern void inetpeer_invalidate_tree(int family);
101+
99102
/*
100103
* temporary check to make sure we dont access rid, ip_id_count, tcp_ts,
101104
* tcp_ts_stamp if no refcount is taken on inet_peer

‎net/ipv4/inetpeer.c‎

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <linux/kernel.h>
1818
#include <linux/mm.h>
1919
#include <linux/net.h>
20+
#include <linux/workqueue.h>
2021
#include <net/ip.h>
2122
#include <net/inetpeer.h>
2223
#include <net/secure_seq.h>
@@ -66,6 +67,11 @@
6667

6768
static struct kmem_cache *peer_cachep __read_mostly;
6869

70+
static LIST_HEAD(gc_list);
71+
static const int gc_delay = 60 * HZ;
72+
static struct delayed_work gc_work;
73+
static DEFINE_SPINLOCK(gc_lock);
74+
6975
#define node_height(x) x->avl_height
7076

7177
#define peer_avl_empty ((struct inet_peer *)&peer_fake_node)
@@ -102,6 +108,50 @@ int inet_peer_threshold __read_mostly = 65536 + 128; /* start to throw entries m
102108
int inet_peer_minttl __read_mostly = 120 * HZ; /* TTL under high load: 120 sec */
103109
int inet_peer_maxttl __read_mostly = 10 * 60 * HZ; /* usual time to live: 10 min */
104110

111+
static void inetpeer_gc_worker(struct work_struct *work)
112+
{
113+
struct inet_peer *p, *n;
114+
LIST_HEAD(list);
115+
116+
spin_lock_bh(&gc_lock);
117+
list_replace_init(&gc_list, &list);
118+
spin_unlock_bh(&gc_lock);
119+
120+
if (list_empty(&list))
121+
return;
122+
123+
list_for_each_entry_safe(p, n, &list, gc_list) {
124+
125+
if(need_resched())
126+
cond_resched();
127+
128+
if (p->avl_left != peer_avl_empty) {
129+
list_add_tail(&p->avl_left->gc_list, &list);
130+
p->avl_left = peer_avl_empty;
131+
}
132+
133+
if (p->avl_right != peer_avl_empty) {
134+
list_add_tail(&p->avl_right->gc_list, &list);
135+
p->avl_right = peer_avl_empty;
136+
}
137+
138+
n = list_entry(p->gc_list.next, struct inet_peer, gc_list);
139+
140+
if (!atomic_read(&p->refcnt)) {
141+
list_del(&p->gc_list);
142+
kmem_cache_free(peer_cachep, p);
143+
}
144+
}
145+
146+
if (list_empty(&list))
147+
return;
148+
149+
spin_lock_bh(&gc_lock);
150+
list_splice(&list, &gc_list);
151+
spin_unlock_bh(&gc_lock);
152+
153+
schedule_delayed_work(&gc_work, gc_delay);
154+
}
105155

106156
/* Called from ip_output.c:ip_init */
107157
void __init inet_initpeers(void)
@@ -126,6 +176,7 @@ void __init inet_initpeers(void)
126176
0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
127177
NULL);
128178

179+
INIT_DELAYED_WORK_DEFERRABLE(&gc_work, inetpeer_gc_worker);
129180
}
130181

131182
static int addr_compare(const struct inetpeer_addr *a,
@@ -449,7 +500,7 @@ struct inet_peer *inet_getpeer(const struct inetpeer_addr *daddr, int create)
449500
p->pmtu_orig = 0;
450501
p->redirect_genid = 0;
451502
memset(&p->redirect_learned, 0, sizeof(p->redirect_learned));
452-
503+
INIT_LIST_HEAD(&p->gc_list);
453504

454505
/* Link the node. */
455506
link_to_pool(p, base);
@@ -509,3 +560,30 @@ bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout)
509560
return rc;
510561
}
511562
EXPORT_SYMBOL(inet_peer_xrlim_allow);
563+
564+
void inetpeer_invalidate_tree(int family)
565+
{
566+
struct inet_peer *old, *new, *prev;
567+
struct inet_peer_base *base = family_to_base(family);
568+
569+
write_seqlock_bh(&base->lock);
570+
571+
old = base->root;
572+
if (old == peer_avl_empty_rcu)
573+
goto out;
574+
575+
new = peer_avl_empty_rcu;
576+
577+
prev = cmpxchg(&base->root, old, new);
578+
if (prev == old) {
579+
base->total = 0;
580+
spin_lock(&gc_lock);
581+
list_add_tail(&prev->gc_list, &gc_list);
582+
spin_unlock(&gc_lock);
583+
schedule_delayed_work(&gc_work, gc_delay);
584+
}
585+
586+
out:
587+
write_sequnlock_bh(&base->lock);
588+
}
589+
EXPORT_SYMBOL(inetpeer_invalidate_tree);

‎net/ipv4/route.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,7 @@ static void rt_cache_invalidate(struct net *net)
938938
get_random_bytes(&shuffle, sizeof(shuffle));
939939
atomic_add(shuffle + 1U, &net->ipv4.rt_genid);
940940
redirect_genid++;
941+
inetpeer_invalidate_tree(AF_INET);
941942
}
942943

943944
/*

0 commit comments

Comments
 (0)