Skip to content

Commit ce6eb0d

Browse files
kaberummakynes
authored andcommitted
netfilter: nft_hash: bug fixes and resizing
The hash set type is very broken and was never meant to be merged in this state. Missing RCU synchronization on element removal, leaking chain refcounts when used as a verdict map, races during lookups, a fixed table size are probably just some of the problems. Luckily it is currently never chosen by the kernel when the rbtree type is also available. Rewrite it to be usable. The new implementation supports automatic hash table resizing using RCU, based on Paul McKenney's and Josh Triplett's algorithm "Optimized Resizing For RCU-Protected Hash Tables" described in [1]. Resizing doesn't require a second list head in the elements, it works by chosing a hash function that remaps elements to a predictable set of buckets, only resizing by integral factors and - during expansion: linking new buckets to the old bucket that contains elements for any of the new buckets, thereby creating imprecise chains, then incrementally seperating the elements until the new buckets only contain elements that hash directly to them. - during shrinking: linking the hash chains of all old buckets that hash to the same new bucket to form a single chain. Expansion requires at most the number of elements in the longest hash chain grace periods, shrinking requires a single grace period. Due to the requirement of having hash chains/elements linked to multiple buckets during resizing, homemade single linked lists are used instead of the existing list helpers, that don't support this in a clean fashion. As a side effect, the amount of memory required per element is reduced by one pointer. Expansion is triggered when the load factors exceeds 75%, shrinking when the load factor goes below 30%. Both operations are allowed to fail and will be retried on the next insertion or removal if their respective conditions still hold. [1] http://dl.acm.org/citation.cfm?id=2002181.2002192 Reviewed-by: Josh Triplett <josh@joshtriplett.org> Reviewed-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Signed-off-by: Patrick McHardy <kaber@trash.net> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 93bb0ce commit ce6eb0d

1 file changed

Lines changed: 214 additions & 46 deletions

File tree

‎net/netfilter/nft_hash.c‎

Lines changed: 214 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
2+
* Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License version 2 as
@@ -18,17 +18,29 @@
1818
#include <linux/netfilter/nf_tables.h>
1919
#include <net/netfilter/nf_tables.h>
2020

21+
#define NFT_HASH_MIN_SIZE 4
22+
2123
struct nft_hash {
22-
struct hlist_head *hash;
23-
unsigned int hsize;
24+
struct nft_hash_table __rcu *tbl;
25+
};
26+
27+
struct nft_hash_table {
28+
unsigned int size;
29+
unsigned int elements;
30+
struct nft_hash_elem __rcu *buckets[];
2431
};
2532

2633
struct nft_hash_elem {
27-
struct hlist_node hnode;
28-
struct nft_data key;
29-
struct nft_data data[];
34+
struct nft_hash_elem __rcu *next;
35+
struct nft_data key;
36+
struct nft_data data[];
3037
};
3138

39+
#define nft_hash_for_each_entry(i, head) \
40+
for (i = nft_dereference(head); i != NULL; i = nft_dereference(i->next))
41+
#define nft_hash_for_each_entry_rcu(i, head) \
42+
for (i = rcu_dereference(head); i != NULL; i = rcu_dereference(i->next))
43+
3244
static u32 nft_hash_rnd __read_mostly;
3345
static bool nft_hash_rnd_initted __read_mostly;
3446

@@ -38,19 +50,20 @@ static unsigned int nft_hash_data(const struct nft_data *data,
3850
unsigned int h;
3951

4052
h = jhash(data->data, len, nft_hash_rnd);
41-
return ((u64)h * hsize) >> 32;
53+
return h & (hsize - 1);
4254
}
4355

4456
static bool nft_hash_lookup(const struct nft_set *set,
4557
const struct nft_data *key,
4658
struct nft_data *data)
4759
{
4860
const struct nft_hash *priv = nft_set_priv(set);
61+
const struct nft_hash_table *tbl = rcu_dereference(priv->tbl);
4962
const struct nft_hash_elem *he;
5063
unsigned int h;
5164

52-
h = nft_hash_data(key, priv->hsize, set->klen);
53-
hlist_for_each_entry(he, &priv->hash[h], hnode) {
65+
h = nft_hash_data(key, tbl->size, set->klen);
66+
nft_hash_for_each_entry_rcu(he, tbl->buckets[h]) {
5467
if (nft_data_cmp(&he->key, key, set->klen))
5568
continue;
5669
if (set->flags & NFT_SET_MAP)
@@ -60,19 +73,148 @@ static bool nft_hash_lookup(const struct nft_set *set,
6073
return false;
6174
}
6275

63-
static void nft_hash_elem_destroy(const struct nft_set *set,
64-
struct nft_hash_elem *he)
76+
static void nft_hash_tbl_free(const struct nft_hash_table *tbl)
6577
{
66-
nft_data_uninit(&he->key, NFT_DATA_VALUE);
67-
if (set->flags & NFT_SET_MAP)
68-
nft_data_uninit(he->data, set->dtype);
69-
kfree(he);
78+
if (is_vmalloc_addr(tbl))
79+
vfree(tbl);
80+
else
81+
kfree(tbl);
82+
}
83+
84+
static struct nft_hash_table *nft_hash_tbl_alloc(unsigned int nbuckets)
85+
{
86+
struct nft_hash_table *tbl;
87+
size_t size;
88+
89+
size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
90+
tbl = kzalloc(size, GFP_KERNEL | __GFP_REPEAT | __GFP_NOWARN);
91+
if (tbl == NULL)
92+
tbl = vzalloc(size);
93+
if (tbl == NULL)
94+
return NULL;
95+
tbl->size = nbuckets;
96+
97+
return tbl;
98+
}
99+
100+
static void nft_hash_chain_unzip(const struct nft_set *set,
101+
const struct nft_hash_table *ntbl,
102+
struct nft_hash_table *tbl, unsigned int n)
103+
{
104+
struct nft_hash_elem *he, *last, *next;
105+
unsigned int h;
106+
107+
he = nft_dereference(tbl->buckets[n]);
108+
if (he == NULL)
109+
return;
110+
h = nft_hash_data(&he->key, ntbl->size, set->klen);
111+
112+
/* Find last element of first chain hashing to bucket h */
113+
last = he;
114+
nft_hash_for_each_entry(he, he->next) {
115+
if (nft_hash_data(&he->key, ntbl->size, set->klen) != h)
116+
break;
117+
last = he;
118+
}
119+
120+
/* Unlink first chain from the old table */
121+
RCU_INIT_POINTER(tbl->buckets[n], last->next);
122+
123+
/* If end of chain reached, done */
124+
if (he == NULL)
125+
return;
126+
127+
/* Find first element of second chain hashing to bucket h */
128+
next = NULL;
129+
nft_hash_for_each_entry(he, he->next) {
130+
if (nft_hash_data(&he->key, ntbl->size, set->klen) != h)
131+
continue;
132+
next = he;
133+
break;
134+
}
135+
136+
/* Link the two chains */
137+
RCU_INIT_POINTER(last->next, next);
138+
}
139+
140+
static int nft_hash_tbl_expand(const struct nft_set *set, struct nft_hash *priv)
141+
{
142+
struct nft_hash_table *tbl = nft_dereference(priv->tbl), *ntbl;
143+
struct nft_hash_elem *he;
144+
unsigned int i, h;
145+
bool complete;
146+
147+
ntbl = nft_hash_tbl_alloc(tbl->size * 2);
148+
if (ntbl == NULL)
149+
return -ENOMEM;
150+
151+
/* Link new table's buckets to first element in the old table
152+
* hashing to the new bucket.
153+
*/
154+
for (i = 0; i < ntbl->size; i++) {
155+
h = i < tbl->size ? i : i - tbl->size;
156+
nft_hash_for_each_entry(he, tbl->buckets[h]) {
157+
if (nft_hash_data(&he->key, ntbl->size, set->klen) != i)
158+
continue;
159+
RCU_INIT_POINTER(ntbl->buckets[i], he);
160+
break;
161+
}
162+
}
163+
ntbl->elements = tbl->elements;
164+
165+
/* Publish new table */
166+
rcu_assign_pointer(priv->tbl, ntbl);
167+
168+
/* Unzip interleaved hash chains */
169+
do {
170+
/* Wait for readers to use new table/unzipped chains */
171+
synchronize_rcu();
172+
173+
complete = true;
174+
for (i = 0; i < tbl->size; i++) {
175+
nft_hash_chain_unzip(set, ntbl, tbl, i);
176+
if (tbl->buckets[i] != NULL)
177+
complete = false;
178+
}
179+
} while (!complete);
180+
181+
nft_hash_tbl_free(tbl);
182+
return 0;
183+
}
184+
185+
static int nft_hash_tbl_shrink(const struct nft_set *set, struct nft_hash *priv)
186+
{
187+
struct nft_hash_table *tbl = nft_dereference(priv->tbl), *ntbl;
188+
struct nft_hash_elem __rcu **pprev;
189+
unsigned int i;
190+
191+
ntbl = nft_hash_tbl_alloc(tbl->size / 2);
192+
if (ntbl == NULL)
193+
return -ENOMEM;
194+
195+
for (i = 0; i < ntbl->size; i++) {
196+
ntbl->buckets[i] = tbl->buckets[i];
197+
198+
for (pprev = &ntbl->buckets[i]; *pprev != NULL;
199+
pprev = &nft_dereference(*pprev)->next)
200+
;
201+
RCU_INIT_POINTER(*pprev, tbl->buckets[i + ntbl->size]);
202+
}
203+
ntbl->elements = tbl->elements;
204+
205+
/* Publish new table */
206+
rcu_assign_pointer(priv->tbl, ntbl);
207+
synchronize_rcu();
208+
209+
nft_hash_tbl_free(tbl);
210+
return 0;
70211
}
71212

72213
static int nft_hash_insert(const struct nft_set *set,
73214
const struct nft_set_elem *elem)
74215
{
75216
struct nft_hash *priv = nft_set_priv(set);
217+
struct nft_hash_table *tbl = nft_dereference(priv->tbl);
76218
struct nft_hash_elem *he;
77219
unsigned int size, h;
78220

@@ -91,33 +233,66 @@ static int nft_hash_insert(const struct nft_set *set,
91233
if (set->flags & NFT_SET_MAP)
92234
nft_data_copy(he->data, &elem->data);
93235

94-
h = nft_hash_data(&he->key, priv->hsize, set->klen);
95-
hlist_add_head_rcu(&he->hnode, &priv->hash[h]);
236+
h = nft_hash_data(&he->key, tbl->size, set->klen);
237+
RCU_INIT_POINTER(he->next, tbl->buckets[h]);
238+
rcu_assign_pointer(tbl->buckets[h], he);
239+
tbl->elements++;
240+
241+
/* Expand table when exceeding 75% load */
242+
if (tbl->elements > tbl->size / 4 * 3)
243+
nft_hash_tbl_expand(set, priv);
244+
96245
return 0;
97246
}
98247

248+
static void nft_hash_elem_destroy(const struct nft_set *set,
249+
struct nft_hash_elem *he)
250+
{
251+
nft_data_uninit(&he->key, NFT_DATA_VALUE);
252+
if (set->flags & NFT_SET_MAP)
253+
nft_data_uninit(he->data, set->dtype);
254+
kfree(he);
255+
}
256+
99257
static void nft_hash_remove(const struct nft_set *set,
100258
const struct nft_set_elem *elem)
101259
{
102-
struct nft_hash_elem *he = elem->cookie;
260+
struct nft_hash *priv = nft_set_priv(set);
261+
struct nft_hash_table *tbl = nft_dereference(priv->tbl);
262+
struct nft_hash_elem *he, __rcu **pprev;
103263

104-
hlist_del_rcu(&he->hnode);
264+
pprev = elem->cookie;
265+
he = nft_dereference((*pprev));
266+
267+
RCU_INIT_POINTER(*pprev, he->next);
268+
synchronize_rcu();
105269
kfree(he);
270+
tbl->elements--;
271+
272+
/* Shrink table beneath 30% load */
273+
if (tbl->elements < tbl->size * 3 / 10 &&
274+
tbl->size > NFT_HASH_MIN_SIZE)
275+
nft_hash_tbl_shrink(set, priv);
106276
}
107277

108278
static int nft_hash_get(const struct nft_set *set, struct nft_set_elem *elem)
109279
{
110280
const struct nft_hash *priv = nft_set_priv(set);
281+
const struct nft_hash_table *tbl = nft_dereference(priv->tbl);
282+
struct nft_hash_elem __rcu * const *pprev;
111283
struct nft_hash_elem *he;
112284
unsigned int h;
113285

114-
h = nft_hash_data(&elem->key, priv->hsize, set->klen);
115-
hlist_for_each_entry(he, &priv->hash[h], hnode) {
116-
if (nft_data_cmp(&he->key, &elem->key, set->klen))
286+
h = nft_hash_data(&elem->key, tbl->size, set->klen);
287+
pprev = &tbl->buckets[h];
288+
nft_hash_for_each_entry(he, tbl->buckets[h]) {
289+
if (nft_data_cmp(&he->key, &elem->key, set->klen)) {
290+
pprev = &he->next;
117291
continue;
292+
}
118293

119-
elem->cookie = he;
120-
elem->flags = 0;
294+
elem->cookie = (void *)pprev;
295+
elem->flags = 0;
121296
if (set->flags & NFT_SET_MAP)
122297
nft_data_copy(&elem->data, he->data);
123298
return 0;
@@ -129,12 +304,13 @@ static void nft_hash_walk(const struct nft_ctx *ctx, const struct nft_set *set,
129304
struct nft_set_iter *iter)
130305
{
131306
const struct nft_hash *priv = nft_set_priv(set);
307+
const struct nft_hash_table *tbl = nft_dereference(priv->tbl);
132308
const struct nft_hash_elem *he;
133309
struct nft_set_elem elem;
134310
unsigned int i;
135311

136-
for (i = 0; i < priv->hsize; i++) {
137-
hlist_for_each_entry(he, &priv->hash[i], hnode) {
312+
for (i = 0; i < tbl->size; i++) {
313+
nft_hash_for_each_entry(he, tbl->buckets[i]) {
138314
if (iter->count < iter->skip)
139315
goto cont;
140316

@@ -161,43 +337,35 @@ static int nft_hash_init(const struct nft_set *set,
161337
const struct nlattr * const tb[])
162338
{
163339
struct nft_hash *priv = nft_set_priv(set);
164-
unsigned int cnt, i;
340+
struct nft_hash_table *tbl;
165341

166342
if (unlikely(!nft_hash_rnd_initted)) {
167343
get_random_bytes(&nft_hash_rnd, 4);
168344
nft_hash_rnd_initted = true;
169345
}
170346

171-
/* Aim for a load factor of 0.75 */
172-
// FIXME: temporarily broken until we have set descriptions
173-
cnt = 100;
174-
cnt = cnt * 4 / 3;
175-
176-
priv->hash = kcalloc(cnt, sizeof(struct hlist_head), GFP_KERNEL);
177-
if (priv->hash == NULL)
347+
tbl = nft_hash_tbl_alloc(NFT_HASH_MIN_SIZE);
348+
if (tbl == NULL)
178349
return -ENOMEM;
179-
priv->hsize = cnt;
180-
181-
for (i = 0; i < cnt; i++)
182-
INIT_HLIST_HEAD(&priv->hash[i]);
183-
350+
RCU_INIT_POINTER(priv->tbl, tbl);
184351
return 0;
185352
}
186353

187354
static void nft_hash_destroy(const struct nft_set *set)
188355
{
189356
const struct nft_hash *priv = nft_set_priv(set);
190-
const struct hlist_node *next;
191-
struct nft_hash_elem *elem;
357+
const struct nft_hash_table *tbl = nft_dereference(priv->tbl);
358+
struct nft_hash_elem *he, *next;
192359
unsigned int i;
193360

194-
for (i = 0; i < priv->hsize; i++) {
195-
hlist_for_each_entry_safe(elem, next, &priv->hash[i], hnode) {
196-
hlist_del(&elem->hnode);
197-
nft_hash_elem_destroy(set, elem);
361+
for (i = 0; i < tbl->size; i++) {
362+
for (he = nft_dereference(tbl->buckets[i]); he != NULL;
363+
he = next) {
364+
next = nft_dereference(he->next);
365+
nft_hash_elem_destroy(set, he);
198366
}
199367
}
200-
kfree(priv->hash);
368+
kfree(tbl);
201369
}
202370

203371
static struct nft_set_ops nft_hash_ops __read_mostly = {

0 commit comments

Comments
 (0)