Skip to content

Commit 5a8cdf6

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: nft_set_pipapo: remove scratch_aligned pointer
use ->scratch for both avx2 and the generic implementation. After previous change the scratch->map member is always aligned properly for AVX2, so we can just use scratch->map in AVX2 too. The alignoff delta is stored in the scratchpad so we can reconstruct the correct address to free the area again. Fixes: 7400b06 ("nft_set_pipapo: Introduce AVX2-based lookup implementation") Reviewed-by: Stefano Brivio <[email protected]> Signed-off-by: Florian Westphal <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 47b1c03 commit 5a8cdf6

File tree

3 files changed

+10
-39
lines changed

3 files changed

+10
-39
lines changed

net/netfilter/nft_set_pipapo.c

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,7 @@ static void pipapo_free_scratch(const struct nft_pipapo_match *m, unsigned int c
11251125
return;
11261126

11271127
mem = s;
1128+
mem -= s->align_off;
11281129
kfree(mem);
11291130
}
11301131

@@ -1144,6 +1145,7 @@ static int pipapo_realloc_scratch(struct nft_pipapo_match *clone,
11441145
struct nft_pipapo_scratch *scratch;
11451146
#ifdef NFT_PIPAPO_ALIGN
11461147
void *scratch_aligned;
1148+
u32 align_off;
11471149
#endif
11481150
scratch = kzalloc_node(struct_size(scratch, map,
11491151
bsize_max * 2) +
@@ -1162,8 +1164,6 @@ static int pipapo_realloc_scratch(struct nft_pipapo_match *clone,
11621164

11631165
pipapo_free_scratch(clone, i);
11641166

1165-
*per_cpu_ptr(clone->scratch, i) = scratch;
1166-
11671167
#ifdef NFT_PIPAPO_ALIGN
11681168
/* Align &scratch->map (not the struct itself): the extra
11691169
* %NFT_PIPAPO_ALIGN_HEADROOM bytes passed to kzalloc_node()
@@ -1175,8 +1175,12 @@ static int pipapo_realloc_scratch(struct nft_pipapo_match *clone,
11751175

11761176
scratch_aligned = NFT_PIPAPO_LT_ALIGN(&scratch->map);
11771177
scratch_aligned -= offsetof(struct nft_pipapo_scratch, map);
1178-
*per_cpu_ptr(clone->scratch_aligned, i) = scratch_aligned;
1178+
align_off = scratch_aligned - (void *)scratch;
1179+
1180+
scratch = scratch_aligned;
1181+
scratch->align_off = align_off;
11791182
#endif
1183+
*per_cpu_ptr(clone->scratch, i) = scratch;
11801184
}
11811185

11821186
return 0;
@@ -1331,11 +1335,6 @@ static struct nft_pipapo_match *pipapo_clone(struct nft_pipapo_match *old)
13311335
if (!new->scratch)
13321336
goto out_scratch;
13331337

1334-
#ifdef NFT_PIPAPO_ALIGN
1335-
new->scratch_aligned = alloc_percpu(*new->scratch_aligned);
1336-
if (!new->scratch_aligned)
1337-
goto out_scratch;
1338-
#endif
13391338
for_each_possible_cpu(i)
13401339
*per_cpu_ptr(new->scratch, i) = NULL;
13411340

@@ -1388,9 +1387,6 @@ static struct nft_pipapo_match *pipapo_clone(struct nft_pipapo_match *old)
13881387
out_scratch_realloc:
13891388
for_each_possible_cpu(i)
13901389
pipapo_free_scratch(new, i);
1391-
#ifdef NFT_PIPAPO_ALIGN
1392-
free_percpu(new->scratch_aligned);
1393-
#endif
13941390
out_scratch:
13951391
free_percpu(new->scratch);
13961392
kfree(new);
@@ -1673,11 +1669,7 @@ static void pipapo_free_match(struct nft_pipapo_match *m)
16731669
for_each_possible_cpu(i)
16741670
pipapo_free_scratch(m, i);
16751671

1676-
#ifdef NFT_PIPAPO_ALIGN
1677-
free_percpu(m->scratch_aligned);
1678-
#endif
16791672
free_percpu(m->scratch);
1680-
16811673
pipapo_free_fields(m);
16821674

16831675
kfree(m);
@@ -2171,16 +2163,6 @@ static int nft_pipapo_init(const struct nft_set *set,
21712163
for_each_possible_cpu(i)
21722164
*per_cpu_ptr(m->scratch, i) = NULL;
21732165

2174-
#ifdef NFT_PIPAPO_ALIGN
2175-
m->scratch_aligned = alloc_percpu(struct nft_pipapo_scratch *);
2176-
if (!m->scratch_aligned) {
2177-
err = -ENOMEM;
2178-
goto out_free;
2179-
}
2180-
for_each_possible_cpu(i)
2181-
*per_cpu_ptr(m->scratch_aligned, i) = NULL;
2182-
#endif
2183-
21842166
rcu_head_init(&m->rcu);
21852167

21862168
nft_pipapo_for_each_field(f, i, m) {
@@ -2211,9 +2193,6 @@ static int nft_pipapo_init(const struct nft_set *set,
22112193
return 0;
22122194

22132195
out_free:
2214-
#ifdef NFT_PIPAPO_ALIGN
2215-
free_percpu(m->scratch_aligned);
2216-
#endif
22172196
free_percpu(m->scratch);
22182197
out_scratch:
22192198
kfree(m);
@@ -2267,9 +2246,6 @@ static void nft_pipapo_destroy(const struct nft_ctx *ctx,
22672246

22682247
nft_set_pipapo_match_destroy(ctx, set, m);
22692248

2270-
#ifdef NFT_PIPAPO_ALIGN
2271-
free_percpu(m->scratch_aligned);
2272-
#endif
22732249
for_each_possible_cpu(cpu)
22742250
pipapo_free_scratch(m, cpu);
22752251
free_percpu(m->scratch);
@@ -2284,9 +2260,6 @@ static void nft_pipapo_destroy(const struct nft_ctx *ctx,
22842260
if (priv->dirty)
22852261
nft_set_pipapo_match_destroy(ctx, set, m);
22862262

2287-
#ifdef NFT_PIPAPO_ALIGN
2288-
free_percpu(priv->clone->scratch_aligned);
2289-
#endif
22902263
for_each_possible_cpu(cpu)
22912264
pipapo_free_scratch(priv->clone, cpu);
22922265
free_percpu(priv->clone->scratch);

net/netfilter/nft_set_pipapo.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,27 +133,25 @@ struct nft_pipapo_field {
133133
/**
134134
* struct nft_pipapo_scratch - percpu data used for lookup and matching
135135
* @map_index: Current working bitmap index, toggled between field matches
136+
* @align_off: Offset to get the originally allocated address
136137
* @map: store partial matching results during lookup
137138
*/
138139
struct nft_pipapo_scratch {
139140
u8 map_index;
141+
u32 align_off;
140142
unsigned long map[];
141143
};
142144

143145
/**
144146
* struct nft_pipapo_match - Data used for lookup and matching
145147
* @field_count Amount of fields in set
146148
* @scratch: Preallocated per-CPU maps for partial matching results
147-
* @scratch_aligned: Version of @scratch aligned to NFT_PIPAPO_ALIGN bytes
148149
* @bsize_max: Maximum lookup table bucket size of all fields, in longs
149150
* @rcu Matching data is swapped on commits
150151
* @f: Fields, with lookup and mapping tables
151152
*/
152153
struct nft_pipapo_match {
153154
int field_count;
154-
#ifdef NFT_PIPAPO_ALIGN
155-
struct nft_pipapo_scratch * __percpu *scratch_aligned;
156-
#endif
157155
struct nft_pipapo_scratch * __percpu *scratch;
158156
size_t bsize_max;
159157
struct rcu_head rcu;

net/netfilter/nft_set_pipapo_avx2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,7 @@ bool nft_pipapo_avx2_lookup(const struct net *net, const struct nft_set *set,
11391139
*/
11401140
kernel_fpu_begin_mask(0);
11411141

1142-
scratch = *raw_cpu_ptr(m->scratch_aligned);
1142+
scratch = *raw_cpu_ptr(m->scratch);
11431143
if (unlikely(!scratch)) {
11441144
kernel_fpu_end();
11451145
return false;

0 commit comments

Comments
 (0)