Skip to content

Commit 02efdff

Browse files
Florian Westphalklassert
authored andcommitted
xfrm: state: use atomic_inc_not_zero to increment refcount
Once xfrm_state_lookup_byaddr no longer acquires the state lock another cpu might be freeing the state entry at the same time. To detect this we use atomic_inc_not_zero, we then signal -EAGAIN to caller in case our result was stale. Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
1 parent ae3fb6d commit 02efdff

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

‎net/xfrm/xfrm_state.c‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737

3838
static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
3939

40+
static inline bool xfrm_state_hold_rcu(struct xfrm_state __rcu *x)
41+
{
42+
return atomic_inc_not_zero(&x->refcnt);
43+
}
44+
4045
static inline unsigned int xfrm_dst_hash(struct net *net,
4146
const xfrm_address_t *daddr,
4247
const xfrm_address_t *saddr,
@@ -668,7 +673,8 @@ static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
668673

669674
if ((mark & x->mark.m) != x->mark.v)
670675
continue;
671-
xfrm_state_hold(x);
676+
if (!xfrm_state_hold_rcu(x))
677+
continue;
672678
return x;
673679
}
674680

@@ -692,7 +698,8 @@ static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
692698

693699
if ((mark & x->mark.m) != x->mark.v)
694700
continue;
695-
xfrm_state_hold(x);
701+
if (!xfrm_state_hold_rcu(x))
702+
continue;
696703
return x;
697704
}
698705

@@ -871,10 +878,14 @@ xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
871878
}
872879
}
873880
out:
874-
if (x)
875-
xfrm_state_hold(x);
876-
else
881+
if (x) {
882+
if (!xfrm_state_hold_rcu(x)) {
883+
*err = -EAGAIN;
884+
x = NULL;
885+
}
886+
} else {
877887
*err = acquire_in_progress ? -EAGAIN : error;
888+
}
878889
spin_unlock_bh(&net->xfrm.xfrm_state_lock);
879890
if (to_put)
880891
xfrm_state_put(to_put);

0 commit comments

Comments
 (0)