netfilter: x_tables: speed up iptables-restore
Subsystems (2)
Florian Westphal
maintainerv3 cover netfilter: x_tables: speed up iptables-restore
iptables-restore can take quite a long time when sytem is busy, in order of half a minute or more.
The main reason for this is the way ip(6)tables performs table swap:
When xt_replace_table assigns the new ruleset pointer, it does not wait for other processors to finish with old ruleset.
Instead it relies on the counter sequence lock in get_counters().
This works but this is costly if system is busy as each counter read operation can possibly be restarted indefinitely.
Instead, make xt_replace_table wait until all processors are known to not use the old ruleset anymore.
This allows to read the old counters without any locking, no cpu is using the ruleset anymore so counters can't change either.
Florian Westphal
maintainerPATCH v3 1/2 netfilter: x_tables: wait until old table isn't used anymore
xt_replace_table relies on table replacement counter retrieval (which uses xt_recseq to synchronize pcpu counters).
This is fine, however with large rule set get_counters() can take a very long time -- it needs to synchronize all counters because it has to assume concurrent modifications can occur.
Make xt_replace_table synchronize by itself by waiting until all cpus had an even seqcount.
This allows a followup patch to copy the counters of the old ruleset without any synchonization after xt_replace_table has completed.
---
v3: check for 'seq is uneven' OR 'has changed' since last check. Its fine if seq is uneven iff its a different sequence number than the initial one.
v2: fix Erics email address
net/netfilter/x_tables.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-)
net/netfilter/x_tables.c +15 -3
@@ -1153,6 +1153,7 @@ xt_replace_table(struct xt_table *table, int *error) {struct xt_table_info *private; +unsigned int cpu;int ret; ret =xt_jumpstack_alloc (newinfo);
@@ -1184,12 +1185,23 @@ xt_replace_table(struct xt_table *table,/* * Even though table entries have now been swapped, other CPU's -* may still be using the old entries. This is okay, because -* resynchronization happens because of the locking done -* during the get_counters() routine. +* may still be using the old entries... */ local_bh_enable (); +/* ... so wait for even xt_recseq on all cpus */ +for_each_possible_cpu (cpu) { +seqcount_t *s = &per_cpu (xt_recseq, cpu); +u32 seq =raw_read_seqcount (s); + +if (seq &1 ) { +do { +cond_resched (); +cpu_relax (); + }while (seq ==raw_read_seqcount (s)); + } + } +#ifdef CONFIG_AUDIT if (audit_enabled) { audit_log(current->audit_context, GFP_KERNEL,
Signature · 1 line
2.13.6
Re: [PATCH v3 nf-next 1/2] netfilter: x_tables: wait until old table isn't used anymore
Quoted 16 lines · Florian Westphal
xt_replace_tablerelies on table replacement counter retrieval (which usesxt_recseqto synchronize pcpu counters).This is fine, however with large rule set
get_counters()can take a very long time -- it needs to synchronize all counters because it has to assume concurrent modifications can occur.Make
xt_replace_tablesynchronize by itself by waiting until all cpus had an even seqcount.This allows a followup patch to copy the counters of the old ruleset without any synchonization after
xt_replace_tablehas completed.Cc Dan Williams <[email protected]>Cc Eric Dumazet <[email protected]>Signed-off-by Florian Westphal <[email protected]>
Quoted 10 lines
---
v3: check for 'seq is uneven' OR 'has changed' since last check. Its fine if seq is uneven iff its a different sequence number than the initial one.v2: fix Erics email address
net/netfilter/x_tables.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-)
But it seems we need an extra smp_wmb() after
smp_wmb();
table->private = newinfo;+ smp_wmb();
Otherwise we have no guarantee other cpus actually see the new ->private value.
Florian Westphal
maintainerRe: [PATCH v3 nf-next 1/2] netfilter: x_tables: wait until old table isn't used anymore
Quoted 30 lines · Eric Dumazet
Quoted 16 lines · Florian Westphal
xt_replace_tablerelies on table replacement counter retrieval (which usesxt_recseqto synchronize pcpu counters).This is fine, however with large rule set
get_counters()can take a very long time -- it needs to synchronize all counters because it has to assume concurrent modifications can occur.Make
xt_replace_tablesynchronize by itself by waiting until all cpus had an even seqcount.This allows a followup patch to copy the counters of the old ruleset without any synchonization after
xt_replace_tablehas completed.Cc Dan Williams <[email protected]>Cc Eric Dumazet <[email protected]>Signed-off-by Florian Westphal <[email protected]>Quoted 10 lines
---
v3: check for 'seq is uneven' OR 'has changed' since last check. Its fine if seq is uneven iff its a different sequence number than the initial one.v2: fix Erics email address
net/netfilter/x_tables.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-)Reviewed-by Eric Dumazet <[email protected]>
Quoted 7 lines
But it seems we need an extra
smp_wmb()aftersmp_wmb(); table->private = newinfo;+
smp_wmb();Otherwise we have no guarantee other cpus actually see the new ->private value.
Seems to be unrelated to this change, so I will submit a separate patch for nf.git that adds this.
Thanks!
Re: [PATCH v3 nf-next 1/2] netfilter: x_tables: wait until old table isn't used anymore
Quoted 33 lines · Florian Westphal
Quoted 32 lines · Eric Dumazet
Quoted 16 lines · Florian Westphal
xt_replace_tablerelies on table replacement counter retrieval (which usesxt_recseqto synchronize pcpu counters).This is fine, however with large rule set
get_counters()can take a very long time -- it needs to synchronize all counters because it has to assume concurrent modifications can occur.Make
xt_replace_tablesynchronize by itself by waiting until all cpus had an even seqcount.This allows a followup patch to copy the counters of the old ruleset without any synchonization after
xt_replace_tablehas completed.Cc Dan Williams <[email protected]>Cc Eric Dumazet <[email protected]>Signed-off-by Florian Westphal <[email protected]>Quoted 10 lines
---
v3: check for 'seq is uneven' OR 'has changed' since last check. Its fine if seq is uneven iff its a different sequence number than the initial one.v2: fix Erics email address
net/netfilter/x_tables.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-)Reviewed-by Eric Dumazet <[email protected]>But it seems we need an extra
smp_wmb()after
Quoted 8 lines
Quoted 5 lines
smp_wmb(); table->private = newinfo;+
smp_wmb();Otherwise we have no guarantee other cpus actually see the new ->private value.
Seems to be unrelated to this change, so I will submit a separate patch for nf.git that adds this.
This is related to this change, please read the comment before the local_bh_enable9)
/*
* Even though table entries have now been swapped, other CPU's
* may still be using the old entries. This is okay, because
* resynchronization happens because of the locking done
* during the get_counters() routine.
*/
Since your new code happens right after table->private = newinfo ; the smp_wmb() is required.
Florian Westphal
maintainerRe: [PATCH v3 nf-next 1/2] netfilter: x_tables: wait until old table isn't used anymore
Quoted 46 lines · Eric Dumazet
Quoted 33 lines · Florian Westphal
Quoted 32 lines · Eric Dumazet
Quoted 16 lines · Florian Westphal
xt_replace_tablerelies on table replacement counter retrieval (which usesxt_recseqto synchronize pcpu counters).This is fine, however with large rule set
get_counters()can take a very long time -- it needs to synchronize all counters because it has to assume concurrent modifications can occur.Make
xt_replace_tablesynchronize by itself by waiting until all cpus had an even seqcount.This allows a followup patch to copy the counters of the old ruleset without any synchonization after
xt_replace_tablehas completed.Cc Dan Williams <[email protected]>Cc Eric Dumazet <[email protected]>Signed-off-by Florian Westphal <[email protected]>Quoted 10 lines
---
v3: check for 'seq is uneven' OR 'has changed' since last check. Its fine if seq is uneven iff its a different sequence number than the initial one.v2: fix Erics email address
net/netfilter/x_tables.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-)Reviewed-by Eric Dumazet <[email protected]>But it seems we need an extra
smp_wmb()afterQuoted 8 lines
Quoted 5 lines
smp_wmb(); table->private = newinfo;+
smp_wmb();Otherwise we have no guarantee other cpus actually see the new ->private value.
Seems to be unrelated to this change, so I will submit a separate patch for nf.git that adds this.
This is related to this change, please read the comment before the
local_bh_enable9)
Quoted 6 lines
/*
* Even though table entries have now been swapped, other CPU's
* may still be using the old entries. This is okay, because
* resynchronization happens because of the locking done
* during theget_counters()routine.
*/
Hmm, but get_counters() does not issue a wmb, and the 'new' code added here essentially is the same as get_counters(), except that we only read seqcount until we saw a change (and not for each counter in the rule set).
What am I missing?
Re: [PATCH v3 nf-next 1/2] netfilter: x_tables: wait until old table isn't used anymore
Quoted 54 lines · Florian Westphal
Quoted 46 lines · Eric Dumazet
Quoted 33 lines · Florian Westphal
Quoted 32 lines · Eric Dumazet
Quoted 16 lines · Florian Westphal
xt_replace_tablerelies on table replacement counter retrieval (which usesxt_recseqto synchronize pcpu counters).This is fine, however with large rule set
get_counters()can take a very long time -- it needs to synchronize all counters because it has to assume concurrent modifications can occur.Make
xt_replace_tablesynchronize by itself by waiting until all cpus had an even seqcount.This allows a followup patch to copy the counters of the old ruleset without any synchonization after
xt_replace_tablehas completed.Cc Dan Williams <[email protected]>Cc Eric Dumazet <[email protected]>Signed-off-by Florian Westphal <[email protected]>Quoted 10 lines
---
v3: check for 'seq is uneven' OR 'has changed' since last check. Its fine if seq is uneven iff its a different sequence number than the initial one.v2: fix Erics email address
net/netfilter/x_tables.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-)Reviewed-by Eric Dumazet <[email protected]>But it seems we need an extra
smp_wmb()afterQuoted 8 lines
Quoted 5 lines
smp_wmb(); table->private = newinfo;+
smp_wmb();Otherwise we have no guarantee other cpus actually see the new ->private value.
Seems to be unrelated to this change, so I will submit a separate patch for nf.git that adds this.
This is related to this change, please read the comment before the
local_bh_enable9)Quoted 6 lines
/*
* Even though table entries have now been swapped, other CPU's
* may still be using the old entries. This is okay, because
* resynchronization happens because of the locking done
* during theget_counters()routine.
*/
Quoted 6 lines
Hmm, but
get_counters()does not issue a wmb, and the 'new' code added here essentially is the same asget_counters(), except that we only read seqcount until we saw a change (and not for each counter in the rule set).What am I missing?
Your sync code does nothing interesting if we are not sure new table->private value is visible
Without barriers, compiler/cpu could do this :
+ /* ... so wait for even xt_recseq on all cpus */
+ for_each_possible_cpu(cpu) {
+ seqcount_t *s = &per_cpu(xt_recseq, cpu);
+ u32 seq = raw_read_seqcount(s);
+
+ if (seq & 1) {
+ do {
+ cond_resched();
+ cpu_relax();
+ } while (seq == raw_read_seqcount(s));
+ }
+ }
/* finally, write new private value */table->private = newinfo;
Basically, your loop is now useless and you could remove it. So there is definitely a bug.
Florian Westphal
maintainerPATCH v3 2/2 netfilter: x_tables: don't use seqlock when fetching old counters
In reply to Florian Westphalafter previous commit xt_replace_table will wait until all cpus had even seqcount (i.e., no cpu is accessing old ruleset).
Add a 'old' counter retrival version that doesn't synchronize counters. Its not needed, the old counters are not in use anymore at this point.
This speeds up table replacement on busy systems with large tables (and many cores).
---
no changes since v3.
v2 only fixed Cc: tag in changelog.
net/ipv4/netfilter/arp_tables.c | 22 ++++++++++++++++++++-- net/ipv4/netfilter/ip_tables.c | 23 +++++++++++++++++++++-- net/ipv6/netfilter/ip6_tables.c | 22 ++++++++++++++++++++-- 3 files changed, 61 insertions(+), 6 deletions(-)
net/ipv4/netfilter/arp_tables.c +20 -2
@@ -634,6 +634,25 @@ static void get_counters(const struct xt_table_info *t, } } +static void get_old_counters (const struct xt_table_info *t, +struct xt_counters counters[]) +{ +struct arpt_entry *iter; +unsigned int cpu, i; + +for_each_possible_cpu (cpu) { + i =0 ; + xt_entry_foreach(iter, t->entries, t->size) { +struct xt_counters *tmp; + + tmp =xt_get_per_cpu_counter (&iter->counters, cpu); +ADD_COUNTER (counters[i], tmp->bcnt, tmp->pcnt); + ++i; + } +cond_resched (); + } +} +static struct xt_counters *alloc_counters (const struct xt_table *table) {unsigned int countersize;
@@ -910,8 +929,7 @@ static int __do_replace(struct net *net, const char *name, (newinfo->number <= oldinfo->initial_entries))module_put (t->me); -/* Get the old counters, and synchronize with replace */ -get_counters (oldinfo, counters); +get_old_counters (oldinfo, counters);/* Decrease module usage counts and free resource */ loc_cpu_old_entry = oldinfo->entries;
net/ipv4/netfilter/ip_tables.c +21 -2
@@ -781,6 +781,26 @@ get_counters(const struct xt_table_info *t, } } +static void get_old_counters (const struct xt_table_info *t, +struct xt_counters counters[]) +{ +struct ipt_entry *iter; +unsigned int cpu, i; + +for_each_possible_cpu (cpu) { + i =0 ; + xt_entry_foreach(iter, t->entries, t->size) { +const struct xt_counters *tmp; + + tmp =xt_get_per_cpu_counter (&iter->counters, cpu); +ADD_COUNTER (counters[i], tmp->bcnt, tmp->pcnt); + ++i;/* macro does multi eval of i */ + } + +cond_resched (); + } +} +static struct xt_counters *alloc_counters (const struct xt_table *table) {unsigned int countersize;
@@ -1070,8 +1090,7 @@ __do_replace(struct net *net, const char *name, unsigned int valid_hooks, (newinfo->number <= oldinfo->initial_entries))module_put (t->me); -/* Get the old counters, and synchronize with replace */ -get_counters (oldinfo, counters); +get_old_counters (oldinfo, counters);/* Decrease module usage counts and free resource */ xt_entry_foreach (iter, oldinfo->entries, oldinfo->size)
net/ipv6/netfilter/ip6_tables.c +20 -2
@@ -800,6 +800,25 @@ get_counters(const struct xt_table_info *t, } } +static void get_old_counters (const struct xt_table_info *t, +struct xt_counters counters[]) +{ +struct ip6t_entry *iter; +unsigned int cpu, i; + +for_each_possible_cpu (cpu) { + i =0 ; + xt_entry_foreach(iter, t->entries, t->size) { +const struct xt_counters *tmp; + + tmp =xt_get_per_cpu_counter (&iter->counters, cpu); +ADD_COUNTER (counters[i], tmp->bcnt, tmp->pcnt); + ++i; + } +cond_resched (); + } +} +static struct xt_counters *alloc_counters (const struct xt_table *table) {unsigned int countersize;
@@ -1090,8 +1109,7 @@ __do_replace(struct net *net, const char *name, unsigned int valid_hooks, (newinfo->number <= oldinfo->initial_entries))module_put (t->me); -/* Get the old counters, and synchronize with replace */ -get_counters (oldinfo, counters); +get_old_counters (oldinfo, counters);/* Decrease module usage counts and free resource */ xt_entry_foreach (iter, oldinfo->entries, oldinfo->size)
Signature · 1 line
2.13.6