Skip to content

Commit b65e3d7

Browse files
Florian Westphalklassert
authored andcommitted
xfrm: state: add sequence count to detect hash resizes
Once xfrm_state_find is lockless we have to cope with a concurrent resize opertion. We use a sequence counter to block in case a resize is in progress and to detect if we might have missed a state that got moved to a new hash table. Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
1 parent df7274e commit b65e3d7

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

‎net/xfrm/xfrm_state.c‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
*/
3737

3838
static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
39+
static __read_mostly seqcount_t xfrm_state_hash_generation = SEQCNT_ZERO(xfrm_state_hash_generation);
3940

4041
static inline bool xfrm_state_hold_rcu(struct xfrm_state __rcu *x)
4142
{
@@ -127,6 +128,7 @@ static void xfrm_hash_resize(struct work_struct *work)
127128
}
128129

129130
spin_lock_bh(&net->xfrm.xfrm_state_lock);
131+
write_seqcount_begin(&xfrm_state_hash_generation);
130132

131133
nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
132134
for (i = net->xfrm.state_hmask; i >= 0; i--)
@@ -143,6 +145,7 @@ static void xfrm_hash_resize(struct work_struct *work)
143145
net->xfrm.state_byspi = nspi;
144146
net->xfrm.state_hmask = nhashmask;
145147

148+
write_seqcount_end(&xfrm_state_hash_generation);
146149
spin_unlock_bh(&net->xfrm.xfrm_state_lock);
147150

148151
osize = (ohashmask + 1) * sizeof(struct hlist_head);
@@ -787,10 +790,13 @@ xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
787790
struct xfrm_state *best = NULL;
788791
u32 mark = pol->mark.v & pol->mark.m;
789792
unsigned short encap_family = tmpl->encap_family;
793+
unsigned int sequence;
790794
struct km_event c;
791795

792796
to_put = NULL;
793797

798+
sequence = read_seqcount_begin(&xfrm_state_hash_generation);
799+
794800
spin_lock_bh(&net->xfrm.xfrm_state_lock);
795801
h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
796802
hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) {
@@ -894,6 +900,15 @@ xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
894900
spin_unlock_bh(&net->xfrm.xfrm_state_lock);
895901
if (to_put)
896902
xfrm_state_put(to_put);
903+
904+
if (read_seqcount_retry(&xfrm_state_hash_generation, sequence)) {
905+
*err = -EAGAIN;
906+
if (x) {
907+
xfrm_state_put(x);
908+
x = NULL;
909+
}
910+
}
911+
897912
return x;
898913
}
899914

0 commit comments

Comments
 (0)