[PATCH] out_of_memory() locking fix
[linux-2.6/libata-dev.git] / net / core / flow.c
blobc4f25385029f86ae4c0bcdd7d3fe0d3b151727df
1 /* flow.c: Generic flow cache.
3 * Copyright (C) 2003 Alexey N. Kuznetsov (kuznet@ms2.inr.ac.ru)
4 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
5 */
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/list.h>
10 #include <linux/jhash.h>
11 #include <linux/interrupt.h>
12 #include <linux/mm.h>
13 #include <linux/random.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/smp.h>
17 #include <linux/completion.h>
18 #include <linux/percpu.h>
19 #include <linux/bitops.h>
20 #include <linux/notifier.h>
21 #include <linux/cpu.h>
22 #include <linux/cpumask.h>
23 #include <net/flow.h>
24 #include <asm/atomic.h>
25 #include <asm/semaphore.h>
26 #include <linux/security.h>
28 struct flow_cache_entry {
29 struct flow_cache_entry *next;
30 u16 family;
31 u8 dir;
32 struct flowi key;
33 u32 genid;
34 u32 sk_sid;
35 void *object;
36 atomic_t *object_ref;
39 atomic_t flow_cache_genid = ATOMIC_INIT(0);
41 static u32 flow_hash_shift;
42 #define flow_hash_size (1 << flow_hash_shift)
43 static DEFINE_PER_CPU(struct flow_cache_entry **, flow_tables) = { NULL };
45 #define flow_table(cpu) (per_cpu(flow_tables, cpu))
47 static kmem_cache_t *flow_cachep __read_mostly;
49 static int flow_lwm, flow_hwm;
51 struct flow_percpu_info {
52 int hash_rnd_recalc;
53 u32 hash_rnd;
54 int count;
55 } ____cacheline_aligned;
56 static DEFINE_PER_CPU(struct flow_percpu_info, flow_hash_info) = { 0 };
58 #define flow_hash_rnd_recalc(cpu) \
59 (per_cpu(flow_hash_info, cpu).hash_rnd_recalc)
60 #define flow_hash_rnd(cpu) \
61 (per_cpu(flow_hash_info, cpu).hash_rnd)
62 #define flow_count(cpu) \
63 (per_cpu(flow_hash_info, cpu).count)
65 static struct timer_list flow_hash_rnd_timer;
67 #define FLOW_HASH_RND_PERIOD (10 * 60 * HZ)
69 struct flow_flush_info {
70 atomic_t cpuleft;
71 struct completion completion;
73 static DEFINE_PER_CPU(struct tasklet_struct, flow_flush_tasklets) = { NULL };
75 #define flow_flush_tasklet(cpu) (&per_cpu(flow_flush_tasklets, cpu))
77 static void flow_cache_new_hashrnd(unsigned long arg)
79 int i;
81 for_each_cpu(i)
82 flow_hash_rnd_recalc(i) = 1;
84 flow_hash_rnd_timer.expires = jiffies + FLOW_HASH_RND_PERIOD;
85 add_timer(&flow_hash_rnd_timer);
88 static void __flow_cache_shrink(int cpu, int shrink_to)
90 struct flow_cache_entry *fle, **flp;
91 int i;
93 for (i = 0; i < flow_hash_size; i++) {
94 int k = 0;
96 flp = &flow_table(cpu)[i];
97 while ((fle = *flp) != NULL && k < shrink_to) {
98 k++;
99 flp = &fle->next;
101 while ((fle = *flp) != NULL) {
102 *flp = fle->next;
103 if (fle->object)
104 atomic_dec(fle->object_ref);
105 kmem_cache_free(flow_cachep, fle);
106 flow_count(cpu)--;
111 static void flow_cache_shrink(int cpu)
113 int shrink_to = flow_lwm / flow_hash_size;
115 __flow_cache_shrink(cpu, shrink_to);
118 static void flow_new_hash_rnd(int cpu)
120 get_random_bytes(&flow_hash_rnd(cpu), sizeof(u32));
121 flow_hash_rnd_recalc(cpu) = 0;
123 __flow_cache_shrink(cpu, 0);
126 static u32 flow_hash_code(struct flowi *key, int cpu)
128 u32 *k = (u32 *) key;
130 return (jhash2(k, (sizeof(*key) / sizeof(u32)), flow_hash_rnd(cpu)) &
131 (flow_hash_size - 1));
134 #if (BITS_PER_LONG == 64)
135 typedef u64 flow_compare_t;
136 #else
137 typedef u32 flow_compare_t;
138 #endif
140 extern void flowi_is_missized(void);
142 /* I hear what you're saying, use memcmp. But memcmp cannot make
143 * important assumptions that we can here, such as alignment and
144 * constant size.
146 static int flow_key_compare(struct flowi *key1, struct flowi *key2)
148 flow_compare_t *k1, *k1_lim, *k2;
149 const int n_elem = sizeof(struct flowi) / sizeof(flow_compare_t);
151 if (sizeof(struct flowi) % sizeof(flow_compare_t))
152 flowi_is_missized();
154 k1 = (flow_compare_t *) key1;
155 k1_lim = k1 + n_elem;
157 k2 = (flow_compare_t *) key2;
159 do {
160 if (*k1++ != *k2++)
161 return 1;
162 } while (k1 < k1_lim);
164 return 0;
167 void *flow_cache_lookup(struct flowi *key, u32 sk_sid, u16 family, u8 dir,
168 flow_resolve_t resolver)
170 struct flow_cache_entry *fle, **head;
171 unsigned int hash;
172 int cpu;
174 local_bh_disable();
175 cpu = smp_processor_id();
177 fle = NULL;
178 /* Packet really early in init? Making flow_cache_init a
179 * pre-smp initcall would solve this. --RR */
180 if (!flow_table(cpu))
181 goto nocache;
183 if (flow_hash_rnd_recalc(cpu))
184 flow_new_hash_rnd(cpu);
185 hash = flow_hash_code(key, cpu);
187 head = &flow_table(cpu)[hash];
188 for (fle = *head; fle; fle = fle->next) {
189 if (fle->family == family &&
190 fle->dir == dir &&
191 fle->sk_sid == sk_sid &&
192 flow_key_compare(key, &fle->key) == 0) {
193 if (fle->genid == atomic_read(&flow_cache_genid)) {
194 void *ret = fle->object;
196 if (ret)
197 atomic_inc(fle->object_ref);
198 local_bh_enable();
200 return ret;
202 break;
206 if (!fle) {
207 if (flow_count(cpu) > flow_hwm)
208 flow_cache_shrink(cpu);
210 fle = kmem_cache_alloc(flow_cachep, SLAB_ATOMIC);
211 if (fle) {
212 fle->next = *head;
213 *head = fle;
214 fle->family = family;
215 fle->dir = dir;
216 fle->sk_sid = sk_sid;
217 memcpy(&fle->key, key, sizeof(*key));
218 fle->object = NULL;
219 flow_count(cpu)++;
223 nocache:
225 void *obj;
226 atomic_t *obj_ref;
228 resolver(key, sk_sid, family, dir, &obj, &obj_ref);
230 if (fle) {
231 fle->genid = atomic_read(&flow_cache_genid);
233 if (fle->object)
234 atomic_dec(fle->object_ref);
236 fle->object = obj;
237 fle->object_ref = obj_ref;
238 if (obj)
239 atomic_inc(fle->object_ref);
241 local_bh_enable();
243 return obj;
247 static void flow_cache_flush_tasklet(unsigned long data)
249 struct flow_flush_info *info = (void *)data;
250 int i;
251 int cpu;
253 cpu = smp_processor_id();
254 for (i = 0; i < flow_hash_size; i++) {
255 struct flow_cache_entry *fle;
257 fle = flow_table(cpu)[i];
258 for (; fle; fle = fle->next) {
259 unsigned genid = atomic_read(&flow_cache_genid);
261 if (!fle->object || fle->genid == genid)
262 continue;
264 fle->object = NULL;
265 atomic_dec(fle->object_ref);
269 if (atomic_dec_and_test(&info->cpuleft))
270 complete(&info->completion);
273 static void flow_cache_flush_per_cpu(void *) __attribute__((__unused__));
274 static void flow_cache_flush_per_cpu(void *data)
276 struct flow_flush_info *info = data;
277 int cpu;
278 struct tasklet_struct *tasklet;
280 cpu = smp_processor_id();
282 tasklet = flow_flush_tasklet(cpu);
283 tasklet->data = (unsigned long)info;
284 tasklet_schedule(tasklet);
287 void flow_cache_flush(void)
289 struct flow_flush_info info;
290 static DECLARE_MUTEX(flow_flush_sem);
292 /* Don't want cpus going down or up during this. */
293 lock_cpu_hotplug();
294 down(&flow_flush_sem);
295 atomic_set(&info.cpuleft, num_online_cpus());
296 init_completion(&info.completion);
298 local_bh_disable();
299 smp_call_function(flow_cache_flush_per_cpu, &info, 1, 0);
300 flow_cache_flush_tasklet((unsigned long)&info);
301 local_bh_enable();
303 wait_for_completion(&info.completion);
304 up(&flow_flush_sem);
305 unlock_cpu_hotplug();
308 static void __devinit flow_cache_cpu_prepare(int cpu)
310 struct tasklet_struct *tasklet;
311 unsigned long order;
313 for (order = 0;
314 (PAGE_SIZE << order) <
315 (sizeof(struct flow_cache_entry *)*flow_hash_size);
316 order++)
317 /* NOTHING */;
319 flow_table(cpu) = (struct flow_cache_entry **)
320 __get_free_pages(GFP_KERNEL, order);
321 if (!flow_table(cpu))
322 panic("NET: failed to allocate flow cache order %lu\n", order);
324 memset(flow_table(cpu), 0, PAGE_SIZE << order);
326 flow_hash_rnd_recalc(cpu) = 1;
327 flow_count(cpu) = 0;
329 tasklet = flow_flush_tasklet(cpu);
330 tasklet_init(tasklet, flow_cache_flush_tasklet, 0);
333 #ifdef CONFIG_HOTPLUG_CPU
334 static int flow_cache_cpu(struct notifier_block *nfb,
335 unsigned long action,
336 void *hcpu)
338 if (action == CPU_DEAD)
339 __flow_cache_shrink((unsigned long)hcpu, 0);
340 return NOTIFY_OK;
342 #endif /* CONFIG_HOTPLUG_CPU */
344 static int __init flow_cache_init(void)
346 int i;
348 flow_cachep = kmem_cache_create("flow_cache",
349 sizeof(struct flow_cache_entry),
350 0, SLAB_HWCACHE_ALIGN,
351 NULL, NULL);
353 if (!flow_cachep)
354 panic("NET: failed to allocate flow cache slab\n");
356 flow_hash_shift = 10;
357 flow_lwm = 2 * flow_hash_size;
358 flow_hwm = 4 * flow_hash_size;
360 init_timer(&flow_hash_rnd_timer);
361 flow_hash_rnd_timer.function = flow_cache_new_hashrnd;
362 flow_hash_rnd_timer.expires = jiffies + FLOW_HASH_RND_PERIOD;
363 add_timer(&flow_hash_rnd_timer);
365 for_each_cpu(i)
366 flow_cache_cpu_prepare(i);
368 hotcpu_notifier(flow_cache_cpu, 0);
369 return 0;
372 module_init(flow_cache_init);
374 EXPORT_SYMBOL(flow_cache_genid);
375 EXPORT_SYMBOL(flow_cache_lookup);