Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 33f611c

Browse files
Florian Westphalklassert
authored andcommitted
xfrm: policy: don't iterate inexact policies twice at insert time
Since commit 6be3b0d ("xfrm: policy: add inexact policy search tree infrastructure") policy lookup no longer walks a list but has a set of candidate lists. This set has to be searched for the best match. In case there are several matches, the priority wins. If the priority is also the same, then the historic behaviour with a single list was to return the first match (first-in-list). With introduction of serval lists, this doesn't work and a new 'pos' member was added that reflects the xfrm_policy structs position in the list. This value is not exported to userspace and it does not need to be the 'position in the list', it just needs to make sure that a->pos < b->pos means that a was added to the lists more recently than b. This re-walk is expensive when many inexact policies are in use. Speed this up: when appending the policy to the end of the walker list, then just take the ->pos value of the last entry made and add 1. Add a slowpath version to prevent overflow, if we'd assign UINT_MAX then iterate the entire list and fix the ordering. While this speeds up insertion considerably finding the insertion spot in the inexact list still requires a partial list walk. This is addressed in followup patches. Before: ./xfrm_policy_add_speed.sh Inserted 1000 policies in 72 ms Inserted 10000 policies in 1540 ms Inserted 100000 policies in 334780 ms After: Inserted 1000 policies in 68 ms Inserted 10000 policies in 1137 ms Inserted 100000 policies in 157307 ms Reported-by: Noel Kuntze <[email protected]> Cc: Tobias Brunner <[email protected]> Signed-off-by: Florian Westphal <[email protected]> Signed-off-by: Steffen Klassert <[email protected]>
1 parent 9c5b6d4 commit 33f611c

File tree

1 file changed

+53
-6
lines changed

1 file changed

+53
-6
lines changed

net/xfrm/xfrm_policy.c

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,17 @@ xfrm_policy_inexact_insert(struct xfrm_policy *policy, u8 dir, int excl)
12371237
return delpol;
12381238
}
12391239

1240+
static bool xfrm_policy_is_dead_or_sk(const struct xfrm_policy *policy)
1241+
{
1242+
int dir;
1243+
1244+
if (policy->walk.dead)
1245+
return true;
1246+
1247+
dir = xfrm_policy_id2dir(policy->index);
1248+
return dir >= XFRM_POLICY_MAX;
1249+
}
1250+
12401251
static void xfrm_hash_rebuild(struct work_struct *work)
12411252
{
12421253
struct net *net = container_of(work, struct net,
@@ -1524,7 +1535,6 @@ static void xfrm_policy_insert_inexact_list(struct hlist_head *chain,
15241535
{
15251536
struct xfrm_policy *pol, *delpol = NULL;
15261537
struct hlist_node *newpos = NULL;
1527-
int i = 0;
15281538

15291539
hlist_for_each_entry(pol, chain, bydst_inexact_list) {
15301540
if (pol->type == policy->type &&
@@ -1548,11 +1558,6 @@ static void xfrm_policy_insert_inexact_list(struct hlist_head *chain,
15481558
hlist_add_behind_rcu(&policy->bydst_inexact_list, newpos);
15491559
else
15501560
hlist_add_head_rcu(&policy->bydst_inexact_list, chain);
1551-
1552-
hlist_for_each_entry(pol, chain, bydst_inexact_list) {
1553-
pol->pos = i;
1554-
i++;
1555-
}
15561561
}
15571562

15581563
static struct xfrm_policy *xfrm_policy_insert_list(struct hlist_head *chain,
@@ -2294,10 +2299,52 @@ static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
22942299
return pol;
22952300
}
22962301

2302+
static u32 xfrm_gen_pos_slow(struct net *net)
2303+
{
2304+
struct xfrm_policy *policy;
2305+
u32 i = 0;
2306+
2307+
/* oldest entry is last in list */
2308+
list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
2309+
if (!xfrm_policy_is_dead_or_sk(policy))
2310+
policy->pos = ++i;
2311+
}
2312+
2313+
return i;
2314+
}
2315+
2316+
static u32 xfrm_gen_pos(struct net *net)
2317+
{
2318+
const struct xfrm_policy *policy;
2319+
u32 i = 0;
2320+
2321+
/* most recently added policy is at the head of the list */
2322+
list_for_each_entry(policy, &net->xfrm.policy_all, walk.all) {
2323+
if (xfrm_policy_is_dead_or_sk(policy))
2324+
continue;
2325+
2326+
if (policy->pos == UINT_MAX)
2327+
return xfrm_gen_pos_slow(net);
2328+
2329+
i = policy->pos + 1;
2330+
break;
2331+
}
2332+
2333+
return i;
2334+
}
2335+
22972336
static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
22982337
{
22992338
struct net *net = xp_net(pol);
23002339

2340+
switch (dir) {
2341+
case XFRM_POLICY_IN:
2342+
case XFRM_POLICY_FWD:
2343+
case XFRM_POLICY_OUT:
2344+
pol->pos = xfrm_gen_pos(net);
2345+
break;
2346+
}
2347+
23012348
list_add(&pol->walk.all, &net->xfrm.policy_all);
23022349
net->xfrm.policy_count[dir]++;
23032350
xfrm_pol_hold(pol);

0 commit comments

Comments
 (0)