Skip to content

Commit e37cc8a

Browse files
Florian Westphalklassert
authored andcommitted
xfrm: policy: use atomic_inc_not_zero in rcu section
If we don't hold the policy lock anymore the refcnt might already be 0, i.e. policy struct is about to be free'd. Switch to atomic_inc_not_zero to avoid this. On removal policies are already unlinked from the tables (lists) before the last _put occurs so we are not supposed to find the same 'dead' entry on the next loop, so its safe to just repeat the lookup. Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
1 parent 3084609 commit e37cc8a

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

net/xfrm/xfrm_policy.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ static void __xfrm_policy_link(struct xfrm_policy *pol, int dir);
6060
static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
6161
int dir);
6262

63+
static inline bool xfrm_pol_hold_rcu(struct xfrm_policy *policy)
64+
{
65+
return atomic_inc_not_zero(&policy->refcnt);
66+
}
67+
6368
static inline bool
6469
__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
6570
{
@@ -1164,7 +1169,8 @@ static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
11641169
if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence))
11651170
goto retry;
11661171

1167-
xfrm_pol_hold(ret);
1172+
if (ret && !xfrm_pol_hold_rcu(ret))
1173+
goto retry;
11681174
fail:
11691175
read_unlock_bh(&net->xfrm.xfrm_policy_lock);
11701176

0 commit comments

Comments
 (0)