diff options
author | Florian Westphal <fw@strlen.de> | 2016-08-11 09:17:53 -0400 |
---|---|---|
committer | Steffen Klassert <steffen.klassert@secunet.com> | 2016-08-12 02:07:11 -0400 |
commit | e1e551bc56302b80ff930c966f9985095fb1b70d (patch) | |
tree | babb24413c09e1ab5ca3a6d690320b05d499204f /net/xfrm | |
parent | a5eefc1df641f3c99fe54b309e7b79c18cec4a1e (diff) |
xfrm: policy: prepare policy_bydst hash for rcu lookups
Since commit 56f047305dd4b6b617
("xfrm: add rcu grace period in xfrm_policy_destroy()") xfrm policy
objects are already free'd via rcu.
In order to make more places lockless (i.e. use rcu_read_lock instead of
grabbing read-side of policy rwlock) we only need to:
- use rcu_assign_pointer to store address of new hash table backend memory
- add rcu barrier so that freeing of old memory is delayed (expansion
and free happens from system workqueue, so synchronize_rcu is fine)
- use rcu_dereference to fetch current address of the hash table.
Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
Diffstat (limited to 'net/xfrm')
-rw-r--r-- | net/xfrm/xfrm_policy.c | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c index 93b8ff74001f..4a8d90a88c83 100644 --- a/net/xfrm/xfrm_policy.c +++ b/net/xfrm/xfrm_policy.c | |||
@@ -385,9 +385,11 @@ static struct hlist_head *policy_hash_bysel(struct net *net, | |||
385 | __get_hash_thresh(net, family, dir, &dbits, &sbits); | 385 | __get_hash_thresh(net, family, dir, &dbits, &sbits); |
386 | hash = __sel_hash(sel, family, hmask, dbits, sbits); | 386 | hash = __sel_hash(sel, family, hmask, dbits, sbits); |
387 | 387 | ||
388 | return (hash == hmask + 1 ? | 388 | if (hash == hmask + 1) |
389 | &net->xfrm.policy_inexact[dir] : | 389 | return &net->xfrm.policy_inexact[dir]; |
390 | net->xfrm.policy_bydst[dir].table + hash); | 390 | |
391 | return rcu_dereference_check(net->xfrm.policy_bydst[dir].table, | ||
392 | lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash; | ||
391 | } | 393 | } |
392 | 394 | ||
393 | static struct hlist_head *policy_hash_direct(struct net *net, | 395 | static struct hlist_head *policy_hash_direct(struct net *net, |
@@ -403,7 +405,8 @@ static struct hlist_head *policy_hash_direct(struct net *net, | |||
403 | __get_hash_thresh(net, family, dir, &dbits, &sbits); | 405 | __get_hash_thresh(net, family, dir, &dbits, &sbits); |
404 | hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits); | 406 | hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits); |
405 | 407 | ||
406 | return net->xfrm.policy_bydst[dir].table + hash; | 408 | return rcu_dereference_check(net->xfrm.policy_bydst[dir].table, |
409 | lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash; | ||
407 | } | 410 | } |
408 | 411 | ||
409 | static void xfrm_dst_hash_transfer(struct net *net, | 412 | static void xfrm_dst_hash_transfer(struct net *net, |
@@ -468,8 +471,8 @@ static void xfrm_bydst_resize(struct net *net, int dir) | |||
468 | unsigned int hmask = net->xfrm.policy_bydst[dir].hmask; | 471 | unsigned int hmask = net->xfrm.policy_bydst[dir].hmask; |
469 | unsigned int nhashmask = xfrm_new_hash_mask(hmask); | 472 | unsigned int nhashmask = xfrm_new_hash_mask(hmask); |
470 | unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head); | 473 | unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head); |
471 | struct hlist_head *odst = net->xfrm.policy_bydst[dir].table; | ||
472 | struct hlist_head *ndst = xfrm_hash_alloc(nsize); | 474 | struct hlist_head *ndst = xfrm_hash_alloc(nsize); |
475 | struct hlist_head *odst; | ||
473 | int i; | 476 | int i; |
474 | 477 | ||
475 | if (!ndst) | 478 | if (!ndst) |
@@ -477,14 +480,19 @@ static void xfrm_bydst_resize(struct net *net, int dir) | |||
477 | 480 | ||
478 | write_lock_bh(&net->xfrm.xfrm_policy_lock); | 481 | write_lock_bh(&net->xfrm.xfrm_policy_lock); |
479 | 482 | ||
483 | odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table, | ||
484 | lockdep_is_held(&net->xfrm.xfrm_policy_lock)); | ||
485 | |||
480 | for (i = hmask; i >= 0; i--) | 486 | for (i = hmask; i >= 0; i--) |
481 | xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir); | 487 | xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir); |
482 | 488 | ||
483 | net->xfrm.policy_bydst[dir].table = ndst; | 489 | rcu_assign_pointer(net->xfrm.policy_bydst[dir].table, ndst); |
484 | net->xfrm.policy_bydst[dir].hmask = nhashmask; | 490 | net->xfrm.policy_bydst[dir].hmask = nhashmask; |
485 | 491 | ||
486 | write_unlock_bh(&net->xfrm.xfrm_policy_lock); | 492 | write_unlock_bh(&net->xfrm.xfrm_policy_lock); |
487 | 493 | ||
494 | synchronize_rcu(); | ||
495 | |||
488 | xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head)); | 496 | xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head)); |
489 | } | 497 | } |
490 | 498 | ||