Skip to content

Commit 711bdde

Browse files
edumazetummakynes
authored andcommitted
netfilter: x_tables: remove XT_TABLE_INFO_SZ and a dereference.
After Florian patches, there is no need for XT_TABLE_INFO_SZ anymore : Only one copy of table is kept, instead of one copy per cpu. We also can avoid a dereference if we put table data right after xt_table_info. It reduces register pressure and helps compiler. Then, we attempt a kmalloc() if total size is under order-3 allocation, to reduce TLB pressure, as in many cases, rules fit in 32 KB. Signed-off-by: Eric Dumazet <edumazet@google.com> Cc: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 53b8762 commit 711bdde

5 files changed

Lines changed: 19 additions & 30 deletions

File tree

‎include/linux/netfilter/x_tables.h‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,9 @@ struct xt_table_info {
225225
unsigned int __percpu *stackptr;
226226
void ***jumpstack;
227227

228-
/* Note : this field MUST be the last one, see XT_TABLE_INFO_SZ */
229-
void *entries;
228+
unsigned char entries[0] __aligned(8);
230229
};
231230

232-
#define XT_TABLE_INFO_SZ (offsetof(struct xt_table_info, entries) \
233-
+ nr_cpu_ids * sizeof(char *))
234231
int xt_register_target(struct xt_target *target);
235232
void xt_unregister_target(struct xt_target *target);
236233
int xt_register_targets(struct xt_target *target, unsigned int n);

‎net/ipv4/netfilter/arp_tables.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ unsigned int arpt_do_table(struct sk_buff *skb,
256256
const struct arphdr *arp;
257257
struct arpt_entry *e, *back;
258258
const char *indev, *outdev;
259-
void *table_base;
259+
const void *table_base;
260260
const struct xt_table_info *private;
261261
struct xt_action_param acpar;
262262
unsigned int addend;
@@ -868,7 +868,7 @@ static int compat_table_info(const struct xt_table_info *info,
868868
struct xt_table_info *newinfo)
869869
{
870870
struct arpt_entry *iter;
871-
void *loc_cpu_entry;
871+
const void *loc_cpu_entry;
872872
int ret;
873873

874874
if (!newinfo || !info)

‎net/ipv4/netfilter/ip_tables.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ copy_entries_to_user(unsigned int total_size,
938938
struct xt_counters *counters;
939939
const struct xt_table_info *private = table->private;
940940
int ret = 0;
941-
void *loc_cpu_entry;
941+
const void *loc_cpu_entry;
942942

943943
counters = alloc_counters(table);
944944
if (IS_ERR(counters))
@@ -1052,7 +1052,7 @@ static int compat_table_info(const struct xt_table_info *info,
10521052
struct xt_table_info *newinfo)
10531053
{
10541054
struct ipt_entry *iter;
1055-
void *loc_cpu_entry;
1055+
const void *loc_cpu_entry;
10561056
int ret;
10571057

10581058
if (!newinfo || !info)

‎net/ipv6/netfilter/ip6_tables.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ copy_entries_to_user(unsigned int total_size,
951951
struct xt_counters *counters;
952952
const struct xt_table_info *private = table->private;
953953
int ret = 0;
954-
void *loc_cpu_entry;
954+
const void *loc_cpu_entry;
955955

956956
counters = alloc_counters(table);
957957
if (IS_ERR(counters))
@@ -1065,7 +1065,7 @@ static int compat_table_info(const struct xt_table_info *info,
10651065
struct xt_table_info *newinfo)
10661066
{
10671067
struct ip6t_entry *iter;
1068-
void *loc_cpu_entry;
1068+
const void *loc_cpu_entry;
10691069
int ret;
10701070

10711071
if (!newinfo || !info)

‎net/netfilter/x_tables.c‎

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -658,38 +658,30 @@ EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
658658

659659
struct xt_table_info *xt_alloc_table_info(unsigned int size)
660660
{
661-
struct xt_table_info *newinfo;
661+
struct xt_table_info *info = NULL;
662+
size_t sz = sizeof(*info) + size;
662663

663664
/* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
664665
if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > totalram_pages)
665666
return NULL;
666667

667-
newinfo = kzalloc(XT_TABLE_INFO_SZ, GFP_KERNEL);
668-
if (!newinfo)
669-
return NULL;
670-
671-
newinfo->size = size;
672-
673-
if (size <= PAGE_SIZE)
674-
newinfo->entries = kmalloc(size, GFP_KERNEL);
675-
else
676-
newinfo->entries = vmalloc(size);
677-
678-
if (newinfo->entries == NULL) {
679-
xt_free_table_info(newinfo);
680-
return NULL;
668+
if (sz <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
669+
info = kmalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
670+
if (!info) {
671+
info = vmalloc(sz);
672+
if (!info)
673+
return NULL;
681674
}
682-
683-
return newinfo;
675+
memset(info, 0, sizeof(*info));
676+
info->size = size;
677+
return info;
684678
}
685679
EXPORT_SYMBOL(xt_alloc_table_info);
686680

687681
void xt_free_table_info(struct xt_table_info *info)
688682
{
689683
int cpu;
690684

691-
kvfree(info->entries);
692-
693685
if (info->jumpstack != NULL) {
694686
for_each_possible_cpu(cpu)
695687
kvfree(info->jumpstack[cpu]);
@@ -698,7 +690,7 @@ void xt_free_table_info(struct xt_table_info *info)
698690

699691
free_percpu(info->stackptr);
700692

701-
kfree(info);
693+
kvfree(info);
702694
}
703695
EXPORT_SYMBOL(xt_free_table_info);
704696

0 commit comments

Comments
 (0)