Merge branches 'pm-cpufreq-fixes' and 'pm-cpuidle-fixes'
[linux/fpc-iii.git] / net / core / dst_cache.c
blob554d364492314fee6da31cbfe1089f7e6e1ee5ec
1 /*
2 * net/core/dst_cache.c - dst entry cache
4 * Copyright (c) 2016 Paolo Abeni <pabeni@redhat.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #include <linux/kernel.h>
13 #include <linux/percpu.h>
14 #include <net/dst_cache.h>
15 #include <net/route.h>
16 #if IS_ENABLED(CONFIG_IPV6)
17 #include <net/ip6_fib.h>
18 #endif
19 #include <uapi/linux/in.h>
21 struct dst_cache_pcpu {
22 unsigned long refresh_ts;
23 struct dst_entry *dst;
24 u32 cookie;
25 union {
26 struct in_addr in_saddr;
27 struct in6_addr in6_saddr;
31 static void dst_cache_per_cpu_dst_set(struct dst_cache_pcpu *dst_cache,
32 struct dst_entry *dst, u32 cookie)
34 dst_release(dst_cache->dst);
35 if (dst)
36 dst_hold(dst);
38 dst_cache->cookie = cookie;
39 dst_cache->dst = dst;
42 static struct dst_entry *dst_cache_per_cpu_get(struct dst_cache *dst_cache,
43 struct dst_cache_pcpu *idst)
45 struct dst_entry *dst;
47 dst = idst->dst;
48 if (!dst)
49 goto fail;
51 /* the cache already hold a dst reference; it can't go away */
52 dst_hold(dst);
54 if (unlikely(!time_after(idst->refresh_ts, dst_cache->reset_ts) ||
55 (dst->obsolete && !dst->ops->check(dst, idst->cookie)))) {
56 dst_cache_per_cpu_dst_set(idst, NULL, 0);
57 dst_release(dst);
58 goto fail;
60 return dst;
62 fail:
63 idst->refresh_ts = jiffies;
64 return NULL;
67 struct dst_entry *dst_cache_get(struct dst_cache *dst_cache)
69 if (!dst_cache->cache)
70 return NULL;
72 return dst_cache_per_cpu_get(dst_cache, this_cpu_ptr(dst_cache->cache));
74 EXPORT_SYMBOL_GPL(dst_cache_get);
76 struct rtable *dst_cache_get_ip4(struct dst_cache *dst_cache, __be32 *saddr)
78 struct dst_cache_pcpu *idst;
79 struct dst_entry *dst;
81 if (!dst_cache->cache)
82 return NULL;
84 idst = this_cpu_ptr(dst_cache->cache);
85 dst = dst_cache_per_cpu_get(dst_cache, idst);
86 if (!dst)
87 return NULL;
89 *saddr = idst->in_saddr.s_addr;
90 return container_of(dst, struct rtable, dst);
92 EXPORT_SYMBOL_GPL(dst_cache_get_ip4);
94 void dst_cache_set_ip4(struct dst_cache *dst_cache, struct dst_entry *dst,
95 __be32 saddr)
97 struct dst_cache_pcpu *idst;
99 if (!dst_cache->cache)
100 return;
102 idst = this_cpu_ptr(dst_cache->cache);
103 dst_cache_per_cpu_dst_set(idst, dst, 0);
104 idst->in_saddr.s_addr = saddr;
106 EXPORT_SYMBOL_GPL(dst_cache_set_ip4);
108 #if IS_ENABLED(CONFIG_IPV6)
109 void dst_cache_set_ip6(struct dst_cache *dst_cache, struct dst_entry *dst,
110 const struct in6_addr *addr)
112 struct dst_cache_pcpu *idst;
114 if (!dst_cache->cache)
115 return;
117 idst = this_cpu_ptr(dst_cache->cache);
118 dst_cache_per_cpu_dst_set(this_cpu_ptr(dst_cache->cache), dst,
119 rt6_get_cookie((struct rt6_info *)dst));
120 idst->in6_saddr = *addr;
122 EXPORT_SYMBOL_GPL(dst_cache_set_ip6);
124 struct dst_entry *dst_cache_get_ip6(struct dst_cache *dst_cache,
125 struct in6_addr *saddr)
127 struct dst_cache_pcpu *idst;
128 struct dst_entry *dst;
130 if (!dst_cache->cache)
131 return NULL;
133 idst = this_cpu_ptr(dst_cache->cache);
134 dst = dst_cache_per_cpu_get(dst_cache, idst);
135 if (!dst)
136 return NULL;
138 *saddr = idst->in6_saddr;
139 return dst;
141 EXPORT_SYMBOL_GPL(dst_cache_get_ip6);
142 #endif
144 int dst_cache_init(struct dst_cache *dst_cache, gfp_t gfp)
146 dst_cache->cache = alloc_percpu_gfp(struct dst_cache_pcpu,
147 gfp | __GFP_ZERO);
148 if (!dst_cache->cache)
149 return -ENOMEM;
151 dst_cache_reset(dst_cache);
152 return 0;
154 EXPORT_SYMBOL_GPL(dst_cache_init);
156 void dst_cache_destroy(struct dst_cache *dst_cache)
158 int i;
160 if (!dst_cache->cache)
161 return;
163 for_each_possible_cpu(i)
164 dst_release(per_cpu_ptr(dst_cache->cache, i)->dst);
166 free_percpu(dst_cache->cache);
168 EXPORT_SYMBOL_GPL(dst_cache_destroy);