ip_tables NUMA optimization
Subsystems (3)
PATCH 1/1 ip_tables NUMA optimization
I've hand-merged Eric Dumazet's original ip_tables numa optimization patch to current git head. Also, I've ported it to ip6_tables and arp_tables.
Since this is 2.6.16 stuff, I don't want to have it applied at this time, but merely request testers (esp. for the non-ipv4 part).
Thanks,
Signature · 5 lines
- Harald Welte <[email protected]> http://netfilter.org/ ============================================================================ "Fragmentation is like classful addressing -- an interesting early architectural error that shows how much experimentation was going on while IP was being designed." -- Paul Vixie
[NETFILTER] ip_tables: NUMA-aware allocation
Part of a performance problem with ip_tables is that memory allocation is not NUMA aware, but 'only' SMP aware (ie each CPU normally touch separate cache lines)
Even with small iptables rules, the cost of this misplacement can be high on common workloads. Instead of using one vmalloc() area (located in the node of the iptables process), we now allocate an area for each possible CPU, using vmalloc_node() so that memory should be allocated in the CPU's node if possible.
Port to arp_tables and ip6_tables by Harald Welte.
---
net/ipv4/netfilter/arp_tables.c +124 -51
@@ -68,19 +68,14 @@ struct arpt_table_info {unsigned int initial_entries;unsigned int hook_entry[NF_ARP_NUMHOOKS ];unsigned int underflow[NF_ARP_NUMHOOKS ]; - char entries[0] __attribute__((aligned(SMP_CACHE_BYTES))); +void *entries[NR_CPUS ]; };static LIST_HEAD (arpt_target);static LIST_HEAD (arpt_tables); +#define SET_COUNTER (c,b,p) do { (c).bcnt = (b); (c).pcnt = (p); } while(0)#define ADD_COUNTER (c,b,p) do { (c).bcnt += (b); (c).pcnt += (p); } while(0) -#ifdef CONFIG_SMP -#define TABLE_OFFSET (t,p) (SMP_ALIGN((t)->size)*(p)) -#else -#define TABLE_OFFSET (t,p) 0 -#endif -static inline int arp_devaddr_compare (const struct arpt_devaddr_info *ap,char *hdr_addr,int len) {
@@ -269,9 +264,7 @@ unsigned int arpt_do_table(struct sk_buf outdev = out ? out->name : nulldevname;read_lock_bh (&table->lock); - table_base = (void *)table->private->entries - +TABLE_OFFSET (table->private, -smp_processor_id ()); + table_base = (void *)table->private->entries[smp_processor_id ()]; e =get_entry (table_base, table->private->hook_entry[hook]); back =get_entry (table_base, table->private->underflow[hook]);
@@ -462,7 +455,8 @@ static inline int unconditional(const st/* Figures out from what hook each rule can be called: returns 0 if * there are loops. Puts hook bitmask in comefrom. */ -static int mark_source_chains (struct arpt_table_info *newinfo,unsigned int valid_hooks) +static int mark_source_chains (struct arpt_table_info *newinfo, +unsigned int valid_hooks,void *entry0) {unsigned int hook;
@@ -472,7 +466,7 @@ static int mark_source_chains(struct arpfor (hook =0 ; hook <NF_ARP_NUMHOOKS ; hook++) {unsigned int pos = newinfo->hook_entry[hook];struct arpt_entry *e - = (struct arpt_entry *)(newinfo->entries + pos); + = (struct arpt_entry *)(entry0 + pos);if (!(valid_hooks & (1 << hook)))continue ;
@@ -514,13 +508,13 @@ static int mark_source_chains(struct arp goto next; e = (struct arpt_entry *) - (newinfo->entries + pos); + (entry0 + pos); }while (oldpos == pos + e->next_offset);/* Move along one */ size = e->next_offset; e = (struct arpt_entry *) - (newinfo->entries + pos + size); + (entry0 + pos + size); e->counters.pcnt = pos; pos += size; }else {
@@ -537,7 +531,7 @@ static int mark_source_chains(struct arp newpos = pos + e->next_offset; } e = (struct arpt_entry *) - (newinfo->entries + newpos); + (entry0 + newpos); e->counters.pcnt = pos; pos = newpos; }
@@ -689,6 +683,7 @@ static inline int cleanup_entry(struct a static int translate_table(const char *name, unsigned int valid_hooks, struct arpt_table_info *newinfo, + void *entry0, unsigned int size, unsigned int number, const unsigned int *hook_entries,
@@ -713,8 +708,8 @@ static int translate_table(const char *n ret =ARPT_ENTRY_ITERATE (newinfo->entries, newinfo->size, check_entry_size_and_hooks, newinfo, - newinfo->entries, - newinfo->entries + size, + entry0, + entry0 + size, hook_entries, underflows, &i);duprintf ("translate_table: ARPT_ENTRY_ITERATE gives %d\n" , ret);if (ret !=0 )
@@ -743,29 +738,26 @@ static int translate_table(const char *n } } -if (!mark_source_chains (newinfo, valid_hooks)) { +if (!mark_source_chains (newinfo, valid_hooks, entry0)) {duprintf ("Looping hook\n" );return -ELOOP ; }/* Finally, each sanity check must pass */ i =0 ; - ret =ARPT_ENTRY_ITERATE (newinfo->entries, newinfo->size, + ret =ARPT_ENTRY_ITERATE (entry0, newinfo->size, check_entry, name, size, &i);if (ret !=0 ) { -ARPT_ENTRY_ITERATE (newinfo->entries, newinfo->size, +ARPT_ENTRY_ITERATE (entry0, newinfo->size, cleanup_entry, &i);return ret; }/* And one copy for every other CPU */ for_each_cpu (i) { -if (i ==0 ) -continue ; -memcpy (newinfo->entries +SMP_ALIGN (newinfo->size) * i, - newinfo->entries, -SMP_ALIGN (newinfo->size)); +if (newinfo->entries[i] && newinfo->entries[i] != entry0) +memcpy (newinfo->entries[smp_processor_id ()], entry0, newinfo->size); }return ret;
@@ -807,20 +799,45 @@ static inline int add_entry_to_counter(creturn 0 ; } +static inline int set_entry_to_counter (const struct arpt_entry *e, +struct arpt_counters total[], +unsigned int *i) +{ +SET_COUNTER (total[*i], e->counters.bcnt, e->counters.pcnt); + + (*i)++; +return 0 ; +} +static void get_counters (const struct arpt_table_info *t,struct arpt_counters counters[]) {unsigned int cpu;unsigned int i; +unsigned int curcpu =get_cpu (); + +/* Instead of clearing (by a previous call to memset()) +* the counters and using adds, we set the counters +* with data used by current CPU */ + + i =0 ; +ARPT_ENTRY_ITERATE (t->entries[curcpu], + t->size, + set_entry_to_counter, + counters, + &i);for_each_cpu (cpu) { +if (cpu == curcpu) +continue ; i =0 ; -ARPT_ENTRY_ITERATE (t->entries +TABLE_OFFSET (t, cpu), +ARPT_ENTRY_ITERATE (t->entries[cpu], t->size, add_entry_to_counter, counters, &i); } +put_cpu (); } static int copy_entries_to_user(unsigned int total_size,
@@ -831,6 +848,7 @@ static int copy_entries_to_user(unsignedstruct arpt_entry *e;struct arpt_counters *counters;int ret =0 ; +void *loc_cpu_entry;/* We need atomic snapshot of counters: rest doesn't change * (other than comefrom, which userspace doesn't care
@@ -843,13 +861,13 @@ static int copy_entries_to_user(unsignedreturn -ENOMEM ;/* First, sum counters... */ -memset (counters,0 , countersize);write_lock_bh (&table->lock);get_counters (table->private, counters);write_unlock_bh (&table->lock); -/* ... then copy entire thing from CPU 0... */ -if (copy_to_user (userptr, table->private->entries, total_size) !=0 ) { + loc_cpu_entry = table->private->entries[get_cpu ()]; +/* ... then copy entire thing ... */ +if (copy_to_user (userptr, loc_cpu_entry, total_size) !=0 ) { ret = -EFAULT ; goto free_counters; }
@@ -859,7 +877,7 @@ static int copy_entries_to_user(unsignedfor (off =0 , num =0 ; off < total_size; off += e->next_offset, num++){struct arpt_entry_target *t; - e = (struct arpt_entry *)(table->private->entries + off); + e = (struct arpt_entry *)(loc_cpu_entry + off); if (copy_to_user(userptr + off + offsetof(struct arpt_entry, counters), &counters[num],
@@ -880,6 +898,7 @@ static int copy_entries_to_user(unsigned } free_counters: +put_cpu ();vfree (counters);return ret; }
@@ -911,6 +930,47 @@ static int get_entries(const struct arptreturn ret; } +static void free_table_info (struct arpt_table_info *info) +{ +int cpu; +for_each_cpu (cpu) { +if (info->size <=PAGE_SIZE ) +kfree (info->entries[cpu]); +else +kfree (info->entries[cpu]); + } +kfree (info); +} + +static struct arpt_table_info *alloc_table_info (unsigned int size) +{ +struct arpt_table_info *newinfo; +int cpu; + + newinfo =kzalloc (sizeof (struct arpt_table_info ),GFP_KERNEL ); +if (!newinfo) +return NULL ; + + newinfo->size = size; + +for_each_cpu (cpu) { +if (size <=PAGE_SIZE ) + newinfo->entries[cpu] =kmalloc_node (size, +GFP_KERNEL , +cpu_to_node (cpu)); +else + newinfo->entries[cpu] =vmalloc_node (size, +cpu_to_node (cpu)); + +if (newinfo->entries[cpu] ==NULL ) { +free_table_info (newinfo); +return NULL ; + } + } + +return newinfo; +} +static int do_replace (void __user *user,unsigned int len) {int ret;
@@ -918,6 +978,7 @@ static int do_replace(void __user *user,struct arpt_table *t;struct arpt_table_info *newinfo, *oldinfo;struct arpt_counters *counters; +void *loc_cpu_entry, *loc_cpu_old_entry;if (copy_from_user (&tmp, user,sizeof (tmp)) !=0 )return -EFAULT ;
@@ -930,13 +991,13 @@ static int do_replace(void __user *user,if ((SMP_ALIGN (tmp.size) >>PAGE_SHIFT ) +2 > num_physpages)return -ENOMEM ; - newinfo =vmalloc (sizeof (struct arpt_table_info ) - +SMP_ALIGN (tmp.size) * - (highest_possible_processor_id ()+1 )); + newinfo =alloc_table_info (tmp.size);if (!newinfo)return -ENOMEM ; -if (copy_from_user (newinfo->entries, user +sizeof (tmp), +/* choose the copy that is on our node/cpu */ + loc_cpu_entry = newinfo->entries[get_cpu ()]; +if (copy_from_user (loc_cpu_entry, user +sizeof (tmp), tmp.size) !=0 ) { ret = -EFAULT ; goto free_newinfo;
@@ -947,10 +1008,9 @@ static int do_replace(void __user *user, ret = -ENOMEM ; goto free_newinfo; } -memset (counters,0 , tmp.num_counters *sizeof (struct arpt_counters )); ret =translate_table (tmp.name, tmp.valid_hooks, - newinfo, tmp.size, tmp.num_entries, + newinfo, loc_cpu_entry, tmp.size, tmp.num_entries, tmp.hook_entry, tmp.underflow);if (ret !=0 ) goto free_newinfo_counters;
@@ -989,8 +1049,10 @@ static int do_replace(void __user *user,/* Get the old counters. */ get_counters (oldinfo, counters);/* Decrease module usage counts and free resource */ -ARPT_ENTRY_ITERATE (oldinfo->entries, oldinfo->size, cleanup_entry,NULL ); -vfree (oldinfo); + loc_cpu_old_entry = oldinfo->entries[smp_processor_id ()]; +ARPT_ENTRY_ITERATE (loc_cpu_old_entry, oldinfo->size, cleanup_entry,NULL ); +put_cpu (); +free_table_info (oldinfo);if (copy_to_user (tmp.counters, counters,sizeof (struct arpt_counters ) * tmp.num_counters) !=0 ) ret = -EFAULT ;
@@ -1002,11 +1064,12 @@ static int do_replace(void __user *user,module_put (t->me);up (&arpt_mutex); free_newinfo_counters_untrans: -ARPT_ENTRY_ITERATE (newinfo->entries, newinfo->size, cleanup_entry,NULL ); +ARPT_ENTRY_ITERATE (loc_cpu_entry, newinfo->size, cleanup_entry,NULL ); free_newinfo_counters:vfree (counters); free_newinfo: -vfree (newinfo); +put_cpu (); +free_table_info (newinfo);return ret; }
@@ -1030,6 +1093,7 @@ static int do_add_counters(void __user *struct arpt_counters_info tmp, *paddc;struct arpt_table *t;int ret =0 ; +void *loc_cpu_entry;if (copy_from_user (&tmp, user,sizeof (tmp)) !=0 )return -EFAULT ;
@@ -1059,7 +1123,9 @@ static int do_add_counters(void __user * } i =0 ; - ARPT_ENTRY_ITERATE(t->private->entries, +/* Choose the copy that is on our node */ + loc_cpu_entry = t->private->entries[smp_processor_id ()]; + ARPT_ENTRY_ITERATE(loc_cpu_entry, t->private->size, add_counter_to_entry, paddc->counters,
@@ -1220,30 +1286,33 @@ int arpt_register_table(struct arpt_tablstruct arpt_table_info *newinfo;static struct arpt_table_info bootstrap = {0 ,0 ,0 , {0 }, {0 }, { } }; +void *loc_cpu_entry; - newinfo =vmalloc (sizeof (struct arpt_table_info ) - +SMP_ALIGN (repl->size) * - (highest_possible_processor_id ()+1 )); + newinfo =alloc_table_info (repl->size);if (!newinfo) { ret = -ENOMEM ;return ret; } -memcpy (newinfo->entries, repl->entries, repl->size); + +/* choose the copy on our node/cpu */ + loc_cpu_entry = newinfo->entries[get_cpu ()]; +memcpy (loc_cpu_entry, repl->entries, repl->size); ret =translate_table (table->name, table->valid_hooks, - newinfo, repl->size, + newinfo, loc_cpu_entry, repl->size, repl->num_entries, repl->hook_entry, repl->underflow); +put_cpu ();duprintf ("arpt_register_table: translate table gives %d\n" , ret);if (ret !=0 ) { -vfree (newinfo); +free_table_info (newinfo);return ret; } ret =down_interruptible (&arpt_mutex);if (ret !=0 ) { -vfree (newinfo); +free_table_info (newinfo);return ret; }
@@ -1272,20 +1341,24 @@ int arpt_register_table(struct arpt_tablreturn ret; free_unlock: -vfree (newinfo); +free_table_info (newinfo); goto unlock; }void arpt_unregister_table (struct arpt_table *table) { +void *loc_cpu_entry; +down (&arpt_mutex);LIST_DELETE (&arpt_tables, table);up (&arpt_mutex);/* Decrease module usage counts and free resources */ -ARPT_ENTRY_ITERATE (table->private->entries, table->private->size, + loc_cpu_entry = table->private->entries[get_cpu ()]; +ARPT_ENTRY_ITERATE (loc_cpu_entry, table->private->size, cleanup_entry,NULL ); -vfree (table->private); +put_cpu (); +free_table_info (table->private); }/* The built-in targets: standard (NULL) and error. */
net/ipv4/netfilter/ip_tables.c +129 -64
429 lines not on this page · raw message
net/ipv6/netfilter/ip6_tables.c +128 -63
425 lines not on this page · raw message
Quoted 9 lines · Harald Welte
I've hand-merged Eric Dumazet's original
ip_tablesnuma optimization patch to current git head. Also, I've ported it toip6_tablesandarp_tables.Since this is 2.6.16 stuff, I don't want to have it applied at this time, but merely request testers (esp. for the non-ipv4 part).
Thanks,
Hi Harald
Thank you for doing this, since I'm currently too busy with my day job to update the patch myself.
However I did read your patch and caught one vfree() call wrongly replaced by a kfree() call in arp_tables.c
Quoted 5 lines
net/ipv4/netfilter/arp_tables.c +0
-0@@ -911,6 +930,47 @@ static int get_entries(const struct arptreturn ret; }
net/ipv4/netfilter/arp_tables.c static int get_entries(const struct arpt · line 942
Quoted 10 lines
net/ipv4/netfilter/arp_tables.c +10
-0+static void free_table_info (struct arpt_table_info *info) +{ +int cpu; +for_each_cpu (cpu) { +if (info->size <=PAGE_SIZE ) +kfree (info->entries[cpu]); +else +kfree (info->entries[cpu]); !!!! + } +kfree (info);
It should probably use vfree() like :
net/ipv4/netfilter/ip_tables.c get_entries(const struct ipt_get_entries · line 1135
Quoted 6 lines
net/ipv4/netfilter/ip_tables.c +6
-0+for_each_cpu (cpu) { +if (info->size <=PAGE_SIZE ) +kfree (info->entries[cpu]); +else +vfree (info->entries[cpu]); + }
See you
Eric Dumazet
David S. Miller
maintainerFrom: Eric Dumazet <[email protected]>
Date: Thu, 17 Nov 2005 16:21:45 +0100
Quoted 8 lines
It should probably use
vfree()like :net/ipv4/netfilter/ip_tables.c get_entries(const struct ipt_get_entries · line 1135
Quoted 6 lines
net/ipv4/netfilter/ip_tables.c +6
-0+for_each_cpu (cpu) { +if (info->size <=PAGE_SIZE ) +kfree (info->entries[cpu]); +else +vfree (info->entries[cpu]); + }
I've put the patch into the net-2.6.16 tree with the obvious fix Eric points out.
Thanks.
Quoted 11 lines · David S. Miller
Quoted 8 lines
It should probably use
vfree()like :net/ipv4/netfilter/ip_tables.c get_entries(const struct ipt_get_entries · line 1135
Quoted 6 lines
net/ipv4/netfilter/ip_tables.c +6
-0+for_each_cpu (cpu) { +if (info->size <=PAGE_SIZE ) +kfree (info->entries[cpu]); +else +vfree (info->entries[cpu]); + }I've put the patch into the net-2.6.16 tree with the obvious fix Eric points out.
thanks!
Signature · 5 lines
- Harald Welte <[email protected]> http://netfilter.org/ ============================================================================ "Fragmentation is like classful addressing -- an interesting early architectural error that shows how much experimentation was going on while IP was being designed." -- Paul Vixie
PATCH 1/1 NETFILTER arp_tables: Fix NUMA optimization
In reply to David S. MillerHi Dave!
Quoted 2 lines · David S. Miller
I've put the patch into the net-2.6.16 tree with the obvious fix Eric points out.
There's another one that I detected while merging those changes with x_tables (painful). Please merge:
[NETFILTER] arp_tables: Fix bug introduced with NUMA aware allocation
This fix shows how bad I am with copy & paste.
---
commit 4fa8b41b4adc117876114c149885fc8921fcabde
tree 950bd802e3daf69ad55fc203765c31ebead4fe5d
parent a7b935151849464c804437ee411dc64641c6f298
author Harald Welte <[email protected]> Sat, 19 Nov 2005 11:29:40 +0100 committer Harald Welte <[email protected]> Sat, 19 Nov 2005 11:29:40 +0100
net/ipv4/netfilter/arp_tables.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
net/ipv4/netfilter/arp_tables.c +1 -1
@@ -757,7 +757,7 @@ static int translate_table(const char *n/* And one copy for every other CPU */ for_each_cpu (i) {if (newinfo->entries[i] && newinfo->entries[i] != entry0) -memcpy (newinfo->entries[smp_processor_id ()], entry0, newinfo->size); +memcpy (newinfo->entries[i], entry0, newinfo->size); }return ret;
Signature · 5 lines
- Harald Welte <[email protected]> http://netfilter.org/ ============================================================================ "Fragmentation is like classful addressing -- an interesting early architectural error that shows how much experimentation was going on while IP was being designed." -- Paul Vixie
David S. Miller
maintainerRe: [PATCH] NETFILTER arp_tables: Fix NUMA optimization
From: Harald Welte <[email protected]>
Date: Sat, 19 Nov 2005 11:31:31 +0100
Quoted 8 lines
There's another one that I detected while merging those changes with
x_tables(painful). Please merge:[NETFILTER]
arp_tables: Fix bug introduced with NUMA aware allocationThis fix shows how bad I am with copy & paste.
Signed-off-by Harald Welte <[email protected]>
Added to net-2.6.16, thanks a lot.