xref: /openbmc/linux/net/core/dst.c (revision b627b4ed)
1 /*
2  * net/core/dst.c	Protocol independent destination cache.
3  *
4  * Authors:		Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
5  *
6  */
7 
8 #include <linux/bitops.h>
9 #include <linux/errno.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/workqueue.h>
13 #include <linux/mm.h>
14 #include <linux/module.h>
15 #include <linux/netdevice.h>
16 #include <linux/skbuff.h>
17 #include <linux/string.h>
18 #include <linux/types.h>
19 #include <net/net_namespace.h>
20 
21 #include <net/dst.h>
22 
23 /*
24  * Theory of operations:
25  * 1) We use a list, protected by a spinlock, to add
26  *    new entries from both BH and non-BH context.
27  * 2) In order to keep spinlock held for a small delay,
28  *    we use a second list where are stored long lived
29  *    entries, that are handled by the garbage collect thread
30  *    fired by a workqueue.
31  * 3) This list is guarded by a mutex,
32  *    so that the gc_task and dst_dev_event() can be synchronized.
33  */
34 #if RT_CACHE_DEBUG >= 2
35 static atomic_t			 dst_total = ATOMIC_INIT(0);
36 #endif
37 
38 /*
39  * We want to keep lock & list close together
40  * to dirty as few cache lines as possible in __dst_free().
41  * As this is not a very strong hint, we dont force an alignment on SMP.
42  */
43 static struct {
44 	spinlock_t		lock;
45 	struct dst_entry 	*list;
46 	unsigned long		timer_inc;
47 	unsigned long		timer_expires;
48 } dst_garbage = {
49 	.lock = __SPIN_LOCK_UNLOCKED(dst_garbage.lock),
50 	.timer_inc = DST_GC_MAX,
51 };
52 static void dst_gc_task(struct work_struct *work);
53 static void ___dst_free(struct dst_entry * dst);
54 
55 static DECLARE_DELAYED_WORK(dst_gc_work, dst_gc_task);
56 
57 static DEFINE_MUTEX(dst_gc_mutex);
58 /*
59  * long lived entries are maintained in this list, guarded by dst_gc_mutex
60  */
61 static struct dst_entry         *dst_busy_list;
62 
63 static void dst_gc_task(struct work_struct *work)
64 {
65 	int    delayed = 0;
66 	int    work_performed = 0;
67 	unsigned long expires = ~0L;
68 	struct dst_entry *dst, *next, head;
69 	struct dst_entry *last = &head;
70 #if RT_CACHE_DEBUG >= 2
71 	ktime_t time_start = ktime_get();
72 	struct timespec elapsed;
73 #endif
74 
75 	mutex_lock(&dst_gc_mutex);
76 	next = dst_busy_list;
77 
78 loop:
79 	while ((dst = next) != NULL) {
80 		next = dst->next;
81 		prefetch(&next->next);
82 		if (likely(atomic_read(&dst->__refcnt))) {
83 			last->next = dst;
84 			last = dst;
85 			delayed++;
86 			continue;
87 		}
88 		work_performed++;
89 
90 		dst = dst_destroy(dst);
91 		if (dst) {
92 			/* NOHASH and still referenced. Unless it is already
93 			 * on gc list, invalidate it and add to gc list.
94 			 *
95 			 * Note: this is temporary. Actually, NOHASH dst's
96 			 * must be obsoleted when parent is obsoleted.
97 			 * But we do not have state "obsoleted, but
98 			 * referenced by parent", so it is right.
99 			 */
100 			if (dst->obsolete > 1)
101 				continue;
102 
103 			___dst_free(dst);
104 			dst->next = next;
105 			next = dst;
106 		}
107 	}
108 
109 	spin_lock_bh(&dst_garbage.lock);
110 	next = dst_garbage.list;
111 	if (next) {
112 		dst_garbage.list = NULL;
113 		spin_unlock_bh(&dst_garbage.lock);
114 		goto loop;
115 	}
116 	last->next = NULL;
117 	dst_busy_list = head.next;
118 	if (!dst_busy_list)
119 		dst_garbage.timer_inc = DST_GC_MAX;
120 	else {
121 		/*
122 		 * if we freed less than 1/10 of delayed entries,
123 		 * we can sleep longer.
124 		 */
125 		if (work_performed <= delayed/10) {
126 			dst_garbage.timer_expires += dst_garbage.timer_inc;
127 			if (dst_garbage.timer_expires > DST_GC_MAX)
128 				dst_garbage.timer_expires = DST_GC_MAX;
129 			dst_garbage.timer_inc += DST_GC_INC;
130 		} else {
131 			dst_garbage.timer_inc = DST_GC_INC;
132 			dst_garbage.timer_expires = DST_GC_MIN;
133 		}
134 		expires = dst_garbage.timer_expires;
135 		/*
136 		 * if the next desired timer is more than 4 seconds in the future
137 		 * then round the timer to whole seconds
138 		 */
139 		if (expires > 4*HZ)
140 			expires = round_jiffies_relative(expires);
141 		schedule_delayed_work(&dst_gc_work, expires);
142 	}
143 
144 	spin_unlock_bh(&dst_garbage.lock);
145 	mutex_unlock(&dst_gc_mutex);
146 #if RT_CACHE_DEBUG >= 2
147 	elapsed = ktime_to_timespec(ktime_sub(ktime_get(), time_start));
148 	printk(KERN_DEBUG "dst_total: %d delayed: %d work_perf: %d"
149 		" expires: %lu elapsed: %lu us\n",
150 		atomic_read(&dst_total), delayed, work_performed,
151 		expires,
152 		elapsed.tv_sec * USEC_PER_SEC + elapsed.tv_nsec / NSEC_PER_USEC);
153 #endif
154 }
155 
156 int dst_discard(struct sk_buff *skb)
157 {
158 	kfree_skb(skb);
159 	return 0;
160 }
161 EXPORT_SYMBOL(dst_discard);
162 
163 void * dst_alloc(struct dst_ops * ops)
164 {
165 	struct dst_entry * dst;
166 
167 	if (ops->gc && atomic_read(&ops->entries) > ops->gc_thresh) {
168 		if (ops->gc(ops))
169 			return NULL;
170 	}
171 	dst = kmem_cache_zalloc(ops->kmem_cachep, GFP_ATOMIC);
172 	if (!dst)
173 		return NULL;
174 	atomic_set(&dst->__refcnt, 0);
175 	dst->ops = ops;
176 	dst->lastuse = jiffies;
177 	dst->path = dst;
178 	dst->input = dst->output = dst_discard;
179 #if RT_CACHE_DEBUG >= 2
180 	atomic_inc(&dst_total);
181 #endif
182 	atomic_inc(&ops->entries);
183 	return dst;
184 }
185 
186 static void ___dst_free(struct dst_entry * dst)
187 {
188 	/* The first case (dev==NULL) is required, when
189 	   protocol module is unloaded.
190 	 */
191 	if (dst->dev == NULL || !(dst->dev->flags&IFF_UP)) {
192 		dst->input = dst->output = dst_discard;
193 	}
194 	dst->obsolete = 2;
195 }
196 
197 void __dst_free(struct dst_entry * dst)
198 {
199 	spin_lock_bh(&dst_garbage.lock);
200 	___dst_free(dst);
201 	dst->next = dst_garbage.list;
202 	dst_garbage.list = dst;
203 	if (dst_garbage.timer_inc > DST_GC_INC) {
204 		dst_garbage.timer_inc = DST_GC_INC;
205 		dst_garbage.timer_expires = DST_GC_MIN;
206 		cancel_delayed_work(&dst_gc_work);
207 		schedule_delayed_work(&dst_gc_work, dst_garbage.timer_expires);
208 	}
209 	spin_unlock_bh(&dst_garbage.lock);
210 }
211 
212 struct dst_entry *dst_destroy(struct dst_entry * dst)
213 {
214 	struct dst_entry *child;
215 	struct neighbour *neigh;
216 	struct hh_cache *hh;
217 
218 	smp_rmb();
219 
220 again:
221 	neigh = dst->neighbour;
222 	hh = dst->hh;
223 	child = dst->child;
224 
225 	dst->hh = NULL;
226 	if (hh && atomic_dec_and_test(&hh->hh_refcnt))
227 		kfree(hh);
228 
229 	if (neigh) {
230 		dst->neighbour = NULL;
231 		neigh_release(neigh);
232 	}
233 
234 	atomic_dec(&dst->ops->entries);
235 
236 	if (dst->ops->destroy)
237 		dst->ops->destroy(dst);
238 	if (dst->dev)
239 		dev_put(dst->dev);
240 #if RT_CACHE_DEBUG >= 2
241 	atomic_dec(&dst_total);
242 #endif
243 	kmem_cache_free(dst->ops->kmem_cachep, dst);
244 
245 	dst = child;
246 	if (dst) {
247 		int nohash = dst->flags & DST_NOHASH;
248 
249 		if (atomic_dec_and_test(&dst->__refcnt)) {
250 			/* We were real parent of this dst, so kill child. */
251 			if (nohash)
252 				goto again;
253 		} else {
254 			/* Child is still referenced, return it for freeing. */
255 			if (nohash)
256 				return dst;
257 			/* Child is still in his hash table */
258 		}
259 	}
260 	return NULL;
261 }
262 
263 void dst_release(struct dst_entry *dst)
264 {
265 	if (dst) {
266                int newrefcnt;
267 
268 		smp_mb__before_atomic_dec();
269                newrefcnt = atomic_dec_return(&dst->__refcnt);
270                WARN_ON(newrefcnt < 0);
271 	}
272 }
273 EXPORT_SYMBOL(dst_release);
274 
275 /* Dirty hack. We did it in 2.2 (in __dst_free),
276  * we have _very_ good reasons not to repeat
277  * this mistake in 2.3, but we have no choice
278  * now. _It_ _is_ _explicit_ _deliberate_
279  * _race_ _condition_.
280  *
281  * Commented and originally written by Alexey.
282  */
283 static inline void dst_ifdown(struct dst_entry *dst, struct net_device *dev,
284 			      int unregister)
285 {
286 	if (dst->ops->ifdown)
287 		dst->ops->ifdown(dst, dev, unregister);
288 
289 	if (dev != dst->dev)
290 		return;
291 
292 	if (!unregister) {
293 		dst->input = dst->output = dst_discard;
294 	} else {
295 		dst->dev = dev_net(dst->dev)->loopback_dev;
296 		dev_hold(dst->dev);
297 		dev_put(dev);
298 		if (dst->neighbour && dst->neighbour->dev == dev) {
299 			dst->neighbour->dev = dst->dev;
300 			dev_hold(dst->dev);
301 			dev_put(dev);
302 		}
303 	}
304 }
305 
306 static int dst_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
307 {
308 	struct net_device *dev = ptr;
309 	struct dst_entry *dst, *last = NULL;
310 
311 	switch (event) {
312 	case NETDEV_UNREGISTER:
313 	case NETDEV_DOWN:
314 		mutex_lock(&dst_gc_mutex);
315 		for (dst = dst_busy_list; dst; dst = dst->next) {
316 			last = dst;
317 			dst_ifdown(dst, dev, event != NETDEV_DOWN);
318 		}
319 
320 		spin_lock_bh(&dst_garbage.lock);
321 		dst = dst_garbage.list;
322 		dst_garbage.list = NULL;
323 		spin_unlock_bh(&dst_garbage.lock);
324 
325 		if (last)
326 			last->next = dst;
327 		else
328 			dst_busy_list = dst;
329 		for (; dst; dst = dst->next) {
330 			dst_ifdown(dst, dev, event != NETDEV_DOWN);
331 		}
332 		mutex_unlock(&dst_gc_mutex);
333 		break;
334 	}
335 	return NOTIFY_DONE;
336 }
337 
338 static struct notifier_block dst_dev_notifier = {
339 	.notifier_call	= dst_dev_event,
340 };
341 
342 void __init dst_init(void)
343 {
344 	register_netdevice_notifier(&dst_dev_notifier);
345 }
346 
347 EXPORT_SYMBOL(__dst_free);
348 EXPORT_SYMBOL(dst_alloc);
349 EXPORT_SYMBOL(dst_destroy);
350