Skip to content

Commit 04b8e4f

Browse files
markto-atlnzgregkh
authored andcommitted
Revert "netfilter: x_tables: Switch synchronization to RCU"
[ Upstream commit d3d40f2 ] This reverts commit cc00bca. This (and the preceding) patch basically re-implemented the RCU mechanisms of patch 7845447. That patch was replaced because of the performance problems that it created when replacing tables. Now, we have the same issue: the call to synchronize_rcu() makes replacing tables slower by as much as an order of magnitude. Prior to using RCU a script calling "iptables" approx. 200 times was taking 1.16s. With RCU this increased to 11.59s. Revert these patches and fix the issue in a different way. Signed-off-by: Mark Tomlinson <mark.tomlinson@alliedtelesis.co.nz> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent f6a6d0a commit 04b8e4f

5 files changed

Lines changed: 56 additions & 40 deletions

File tree

‎include/linux/netfilter/x_tables.h‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ struct xt_table {
227227
unsigned int valid_hooks;
228228

229229
/* Man behind the curtain... */
230-
struct xt_table_info __rcu *private;
230+
struct xt_table_info *private;
231231

232232
/* Set this to THIS_MODULE if you are a module, otherwise NULL */
233233
struct module *me;
@@ -448,9 +448,6 @@ xt_get_per_cpu_counter(struct xt_counters *cnt, unsigned int cpu)
448448

449449
struct nf_hook_ops *xt_hook_ops_alloc(const struct xt_table *, nf_hookfn *);
450450

451-
struct xt_table_info
452-
*xt_table_get_private_protected(const struct xt_table *table);
453-
454451
#ifdef CONFIG_COMPAT
455452
#include <net/compat.h>
456453

‎net/ipv4/netfilter/arp_tables.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ unsigned int arpt_do_table(struct sk_buff *skb,
203203

204204
local_bh_disable();
205205
addend = xt_write_recseq_begin();
206-
private = rcu_access_pointer(table->private);
206+
private = READ_ONCE(table->private); /* Address dependency. */
207207
cpu = smp_processor_id();
208208
table_base = private->entries;
209209
jumpstack = (struct arpt_entry **)private->jumpstack[cpu];
@@ -649,7 +649,7 @@ static struct xt_counters *alloc_counters(const struct xt_table *table)
649649
{
650650
unsigned int countersize;
651651
struct xt_counters *counters;
652-
const struct xt_table_info *private = xt_table_get_private_protected(table);
652+
const struct xt_table_info *private = table->private;
653653

654654
/* We need atomic snapshot of counters: rest doesn't change
655655
* (other than comefrom, which userspace doesn't care
@@ -673,7 +673,7 @@ static int copy_entries_to_user(unsigned int total_size,
673673
unsigned int off, num;
674674
const struct arpt_entry *e;
675675
struct xt_counters *counters;
676-
struct xt_table_info *private = xt_table_get_private_protected(table);
676+
struct xt_table_info *private = table->private;
677677
int ret = 0;
678678
void *loc_cpu_entry;
679679

@@ -807,7 +807,7 @@ static int get_info(struct net *net, void __user *user, const int *len)
807807
t = xt_request_find_table_lock(net, NFPROTO_ARP, name);
808808
if (!IS_ERR(t)) {
809809
struct arpt_getinfo info;
810-
const struct xt_table_info *private = xt_table_get_private_protected(t);
810+
const struct xt_table_info *private = t->private;
811811
#ifdef CONFIG_COMPAT
812812
struct xt_table_info tmp;
813813

@@ -860,7 +860,7 @@ static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
860860

861861
t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
862862
if (!IS_ERR(t)) {
863-
const struct xt_table_info *private = xt_table_get_private_protected(t);
863+
const struct xt_table_info *private = t->private;
864864

865865
if (get.size == private->size)
866866
ret = copy_entries_to_user(private->size,
@@ -1017,7 +1017,7 @@ static int do_add_counters(struct net *net, sockptr_t arg, unsigned int len)
10171017
}
10181018

10191019
local_bh_disable();
1020-
private = xt_table_get_private_protected(t);
1020+
private = t->private;
10211021
if (private->number != tmp.num_counters) {
10221022
ret = -EINVAL;
10231023
goto unlock_up_free;
@@ -1330,7 +1330,7 @@ static int compat_copy_entries_to_user(unsigned int total_size,
13301330
void __user *userptr)
13311331
{
13321332
struct xt_counters *counters;
1333-
const struct xt_table_info *private = xt_table_get_private_protected(table);
1333+
const struct xt_table_info *private = table->private;
13341334
void __user *pos;
13351335
unsigned int size;
13361336
int ret = 0;

‎net/ipv4/netfilter/ip_tables.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ ipt_do_table(struct sk_buff *skb,
258258
WARN_ON(!(table->valid_hooks & (1 << hook)));
259259
local_bh_disable();
260260
addend = xt_write_recseq_begin();
261-
private = rcu_access_pointer(table->private);
261+
private = READ_ONCE(table->private); /* Address dependency. */
262262
cpu = smp_processor_id();
263263
table_base = private->entries;
264264
jumpstack = (struct ipt_entry **)private->jumpstack[cpu];
@@ -791,7 +791,7 @@ static struct xt_counters *alloc_counters(const struct xt_table *table)
791791
{
792792
unsigned int countersize;
793793
struct xt_counters *counters;
794-
const struct xt_table_info *private = xt_table_get_private_protected(table);
794+
const struct xt_table_info *private = table->private;
795795

796796
/* We need atomic snapshot of counters: rest doesn't change
797797
(other than comefrom, which userspace doesn't care
@@ -815,7 +815,7 @@ copy_entries_to_user(unsigned int total_size,
815815
unsigned int off, num;
816816
const struct ipt_entry *e;
817817
struct xt_counters *counters;
818-
const struct xt_table_info *private = xt_table_get_private_protected(table);
818+
const struct xt_table_info *private = table->private;
819819
int ret = 0;
820820
const void *loc_cpu_entry;
821821

@@ -964,7 +964,7 @@ static int get_info(struct net *net, void __user *user, const int *len)
964964
t = xt_request_find_table_lock(net, AF_INET, name);
965965
if (!IS_ERR(t)) {
966966
struct ipt_getinfo info;
967-
const struct xt_table_info *private = xt_table_get_private_protected(t);
967+
const struct xt_table_info *private = t->private;
968968
#ifdef CONFIG_COMPAT
969969
struct xt_table_info tmp;
970970

@@ -1018,7 +1018,7 @@ get_entries(struct net *net, struct ipt_get_entries __user *uptr,
10181018

10191019
t = xt_find_table_lock(net, AF_INET, get.name);
10201020
if (!IS_ERR(t)) {
1021-
const struct xt_table_info *private = xt_table_get_private_protected(t);
1021+
const struct xt_table_info *private = t->private;
10221022
if (get.size == private->size)
10231023
ret = copy_entries_to_user(private->size,
10241024
t, uptr->entrytable);
@@ -1173,7 +1173,7 @@ do_add_counters(struct net *net, sockptr_t arg, unsigned int len)
11731173
}
11741174

11751175
local_bh_disable();
1176-
private = xt_table_get_private_protected(t);
1176+
private = t->private;
11771177
if (private->number != tmp.num_counters) {
11781178
ret = -EINVAL;
11791179
goto unlock_up_free;
@@ -1543,7 +1543,7 @@ compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
15431543
void __user *userptr)
15441544
{
15451545
struct xt_counters *counters;
1546-
const struct xt_table_info *private = xt_table_get_private_protected(table);
1546+
const struct xt_table_info *private = table->private;
15471547
void __user *pos;
15481548
unsigned int size;
15491549
int ret = 0;

‎net/ipv6/netfilter/ip6_tables.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ ip6t_do_table(struct sk_buff *skb,
280280

281281
local_bh_disable();
282282
addend = xt_write_recseq_begin();
283-
private = rcu_access_pointer(table->private);
283+
private = READ_ONCE(table->private); /* Address dependency. */
284284
cpu = smp_processor_id();
285285
table_base = private->entries;
286286
jumpstack = (struct ip6t_entry **)private->jumpstack[cpu];
@@ -807,7 +807,7 @@ static struct xt_counters *alloc_counters(const struct xt_table *table)
807807
{
808808
unsigned int countersize;
809809
struct xt_counters *counters;
810-
const struct xt_table_info *private = xt_table_get_private_protected(table);
810+
const struct xt_table_info *private = table->private;
811811

812812
/* We need atomic snapshot of counters: rest doesn't change
813813
(other than comefrom, which userspace doesn't care
@@ -831,7 +831,7 @@ copy_entries_to_user(unsigned int total_size,
831831
unsigned int off, num;
832832
const struct ip6t_entry *e;
833833
struct xt_counters *counters;
834-
const struct xt_table_info *private = xt_table_get_private_protected(table);
834+
const struct xt_table_info *private = table->private;
835835
int ret = 0;
836836
const void *loc_cpu_entry;
837837

@@ -980,7 +980,7 @@ static int get_info(struct net *net, void __user *user, const int *len)
980980
t = xt_request_find_table_lock(net, AF_INET6, name);
981981
if (!IS_ERR(t)) {
982982
struct ip6t_getinfo info;
983-
const struct xt_table_info *private = xt_table_get_private_protected(t);
983+
const struct xt_table_info *private = t->private;
984984
#ifdef CONFIG_COMPAT
985985
struct xt_table_info tmp;
986986

@@ -1035,7 +1035,7 @@ get_entries(struct net *net, struct ip6t_get_entries __user *uptr,
10351035

10361036
t = xt_find_table_lock(net, AF_INET6, get.name);
10371037
if (!IS_ERR(t)) {
1038-
struct xt_table_info *private = xt_table_get_private_protected(t);
1038+
struct xt_table_info *private = t->private;
10391039
if (get.size == private->size)
10401040
ret = copy_entries_to_user(private->size,
10411041
t, uptr->entrytable);
@@ -1189,7 +1189,7 @@ do_add_counters(struct net *net, sockptr_t arg, unsigned int len)
11891189
}
11901190

11911191
local_bh_disable();
1192-
private = xt_table_get_private_protected(t);
1192+
private = t->private;
11931193
if (private->number != tmp.num_counters) {
11941194
ret = -EINVAL;
11951195
goto unlock_up_free;
@@ -1552,7 +1552,7 @@ compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
15521552
void __user *userptr)
15531553
{
15541554
struct xt_counters *counters;
1555-
const struct xt_table_info *private = xt_table_get_private_protected(table);
1555+
const struct xt_table_info *private = table->private;
15561556
void __user *pos;
15571557
unsigned int size;
15581558
int ret = 0;

‎net/netfilter/x_tables.c‎

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,21 +1351,14 @@ struct xt_counters *xt_counters_alloc(unsigned int counters)
13511351
}
13521352
EXPORT_SYMBOL(xt_counters_alloc);
13531353

1354-
struct xt_table_info
1355-
*xt_table_get_private_protected(const struct xt_table *table)
1356-
{
1357-
return rcu_dereference_protected(table->private,
1358-
mutex_is_locked(&xt[table->af].mutex));
1359-
}
1360-
EXPORT_SYMBOL(xt_table_get_private_protected);
1361-
13621354
struct xt_table_info *
13631355
xt_replace_table(struct xt_table *table,
13641356
unsigned int num_counters,
13651357
struct xt_table_info *newinfo,
13661358
int *error)
13671359
{
13681360
struct xt_table_info *private;
1361+
unsigned int cpu;
13691362
int ret;
13701363

13711364
ret = xt_jumpstack_alloc(newinfo);
@@ -1375,20 +1368,47 @@ xt_replace_table(struct xt_table *table,
13751368
}
13761369

13771370
/* Do the substitution. */
1378-
private = xt_table_get_private_protected(table);
1371+
local_bh_disable();
1372+
private = table->private;
13791373

13801374
/* Check inside lock: is the old number correct? */
13811375
if (num_counters != private->number) {
13821376
pr_debug("num_counters != table->private->number (%u/%u)\n",
13831377
num_counters, private->number);
1378+
local_bh_enable();
13841379
*error = -EAGAIN;
13851380
return NULL;
13861381
}
13871382

13881383
newinfo->initial_entries = private->initial_entries;
1384+
/*
1385+
* Ensure contents of newinfo are visible before assigning to
1386+
* private.
1387+
*/
1388+
smp_wmb();
1389+
table->private = newinfo;
1390+
1391+
/* make sure all cpus see new ->private value */
1392+
smp_wmb();
13891393

1390-
rcu_assign_pointer(table->private, newinfo);
1391-
synchronize_rcu();
1394+
/*
1395+
* Even though table entries have now been swapped, other CPU's
1396+
* may still be using the old entries...
1397+
*/
1398+
local_bh_enable();
1399+
1400+
/* ... so wait for even xt_recseq on all cpus */
1401+
for_each_possible_cpu(cpu) {
1402+
seqcount_t *s = &per_cpu(xt_recseq, cpu);
1403+
u32 seq = raw_read_seqcount(s);
1404+
1405+
if (seq & 1) {
1406+
do {
1407+
cond_resched();
1408+
cpu_relax();
1409+
} while (seq == raw_read_seqcount(s));
1410+
}
1411+
}
13921412

13931413
audit_log_nfcfg(table->name, table->af, private->number,
13941414
!private->number ? AUDIT_XT_OP_REGISTER :
@@ -1424,12 +1444,12 @@ struct xt_table *xt_register_table(struct net *net,
14241444
}
14251445

14261446
/* Simplifies replace_table code. */
1427-
rcu_assign_pointer(table->private, bootstrap);
1447+
table->private = bootstrap;
14281448

14291449
if (!xt_replace_table(table, 0, newinfo, &ret))
14301450
goto unlock;
14311451

1432-
private = xt_table_get_private_protected(table);
1452+
private = table->private;
14331453
pr_debug("table->private->number = %u\n", private->number);
14341454

14351455
/* save number of initial entries */
@@ -1452,8 +1472,7 @@ void *xt_unregister_table(struct xt_table *table)
14521472
struct xt_table_info *private;
14531473

14541474
mutex_lock(&xt[table->af].mutex);
1455-
private = xt_table_get_private_protected(table);
1456-
RCU_INIT_POINTER(table->private, NULL);
1475+
private = table->private;
14571476
list_del(&table->list);
14581477
mutex_unlock(&xt[table->af].mutex);
14591478
audit_log_nfcfg(table->name, table->af, private->number,

0 commit comments

Comments
 (0)