Skip to content

Commit e761437

Browse files
congwangdavem330
authored andcommitted
net_sched: use idr to allocate u32 filter handles
Instead of calling u32_lookup_ht() in a loop to find a unused handle, just switch to idr API to allocate new handles. u32 filters are special as the handle could contain a hash table id and a key id, so we need two IDR to allocate each of them. Cc: Chris Mi <[email protected]> Cc: Jamal Hadi Salim <[email protected]> Signed-off-by: Cong Wang <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 1d8134f commit e761437

File tree

1 file changed

+67
-41
lines changed

1 file changed

+67
-41
lines changed

net/sched/cls_u32.c

Lines changed: 67 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include <net/act_api.h>
4747
#include <net/pkt_cls.h>
4848
#include <linux/netdevice.h>
49+
#include <linux/idr.h>
4950

5051
struct tc_u_knode {
5152
struct tc_u_knode __rcu *next;
@@ -82,6 +83,7 @@ struct tc_u_hnode {
8283
struct tc_u_common *tp_c;
8384
int refcnt;
8485
unsigned int divisor;
86+
struct idr handle_idr;
8587
struct rcu_head rcu;
8688
/* The 'ht' field MUST be the last field in structure to allow for
8789
* more entries allocated at end of structure.
@@ -93,7 +95,7 @@ struct tc_u_common {
9395
struct tc_u_hnode __rcu *hlist;
9496
struct Qdisc *q;
9597
int refcnt;
96-
u32 hgenerator;
98+
struct idr handle_idr;
9799
struct hlist_node hnode;
98100
struct rcu_head rcu;
99101
};
@@ -311,19 +313,19 @@ static void *u32_get(struct tcf_proto *tp, u32 handle)
311313
return u32_lookup_key(ht, handle);
312314
}
313315

314-
static u32 gen_new_htid(struct tc_u_common *tp_c)
316+
static u32 gen_new_htid(struct tc_u_common *tp_c, struct tc_u_hnode *ptr)
315317
{
316-
int i = 0x800;
318+
unsigned long idr_index;
319+
int err;
317320

318-
/* hgenerator only used inside rtnl lock it is safe to increment
321+
/* This is only used inside rtnl lock it is safe to increment
319322
* without read _copy_ update semantics
320323
*/
321-
do {
322-
if (++tp_c->hgenerator == 0x7FF)
323-
tp_c->hgenerator = 1;
324-
} while (--i > 0 && u32_lookup_ht(tp_c, (tp_c->hgenerator|0x800)<<20));
325-
326-
return i > 0 ? (tp_c->hgenerator|0x800)<<20 : 0;
324+
err = idr_alloc_ext(&tp_c->handle_idr, ptr, &idr_index,
325+
1, 0x7FF, GFP_KERNEL);
326+
if (err)
327+
return 0;
328+
return (u32)(idr_index | 0x800) << 20;
327329
}
328330

329331
static struct hlist_head *tc_u_common_hash;
@@ -366,8 +368,9 @@ static int u32_init(struct tcf_proto *tp)
366368
return -ENOBUFS;
367369

368370
root_ht->refcnt++;
369-
root_ht->handle = tp_c ? gen_new_htid(tp_c) : 0x80000000;
371+
root_ht->handle = tp_c ? gen_new_htid(tp_c, root_ht) : 0x80000000;
370372
root_ht->prio = tp->prio;
373+
idr_init(&root_ht->handle_idr);
371374

372375
if (tp_c == NULL) {
373376
tp_c = kzalloc(sizeof(*tp_c), GFP_KERNEL);
@@ -377,6 +380,7 @@ static int u32_init(struct tcf_proto *tp)
377380
}
378381
tp_c->q = tp->q;
379382
INIT_HLIST_NODE(&tp_c->hnode);
383+
idr_init(&tp_c->handle_idr);
380384

381385
h = tc_u_hash(tp);
382386
hlist_add_head(&tp_c->hnode, &tc_u_common_hash[h]);
@@ -565,6 +569,7 @@ static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
565569
rtnl_dereference(n->next));
566570
tcf_unbind_filter(tp, &n->res);
567571
u32_remove_hw_knode(tp, n->handle);
572+
idr_remove_ext(&ht->handle_idr, n->handle);
568573
call_rcu(&n->rcu, u32_delete_key_freepf_rcu);
569574
}
570575
}
@@ -586,6 +591,8 @@ static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
586591
hn = &phn->next, phn = rtnl_dereference(*hn)) {
587592
if (phn == ht) {
588593
u32_clear_hw_hnode(tp, ht);
594+
idr_destroy(&ht->handle_idr);
595+
idr_remove_ext(&tp_c->handle_idr, ht->handle);
589596
RCU_INIT_POINTER(*hn, ht->next);
590597
kfree_rcu(ht, rcu);
591598
return 0;
@@ -633,6 +640,7 @@ static void u32_destroy(struct tcf_proto *tp)
633640
kfree_rcu(ht, rcu);
634641
}
635642

643+
idr_destroy(&tp_c->handle_idr);
636644
kfree(tp_c);
637645
}
638646

@@ -701,27 +709,21 @@ static int u32_delete(struct tcf_proto *tp, void *arg, bool *last)
701709
return ret;
702710
}
703711

704-
#define NR_U32_NODE (1<<12)
705-
static u32 gen_new_kid(struct tc_u_hnode *ht, u32 handle)
712+
static u32 gen_new_kid(struct tc_u_hnode *ht, u32 htid)
706713
{
707-
struct tc_u_knode *n;
708-
unsigned long i;
709-
unsigned long *bitmap = kzalloc(BITS_TO_LONGS(NR_U32_NODE) * sizeof(unsigned long),
710-
GFP_KERNEL);
711-
if (!bitmap)
712-
return handle | 0xFFF;
713-
714-
for (n = rtnl_dereference(ht->ht[TC_U32_HASH(handle)]);
715-
n;
716-
n = rtnl_dereference(n->next))
717-
set_bit(TC_U32_NODE(n->handle), bitmap);
718-
719-
i = find_next_zero_bit(bitmap, NR_U32_NODE, 0x800);
720-
if (i >= NR_U32_NODE)
721-
i = find_next_zero_bit(bitmap, NR_U32_NODE, 1);
714+
unsigned long idr_index;
715+
u32 start = htid | 0x800;
716+
u32 max = htid | 0xFFF;
717+
u32 min = htid;
718+
719+
if (idr_alloc_ext(&ht->handle_idr, NULL, &idr_index,
720+
start, max + 1, GFP_KERNEL)) {
721+
if (idr_alloc_ext(&ht->handle_idr, NULL, &idr_index,
722+
min + 1, max + 1, GFP_KERNEL))
723+
return max;
724+
}
722725

723-
kfree(bitmap);
724-
return handle | (i >= NR_U32_NODE ? 0xFFF : i);
726+
return (u32)idr_index;
725727
}
726728

727729
static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = {
@@ -806,6 +808,7 @@ static void u32_replace_knode(struct tcf_proto *tp, struct tc_u_common *tp_c,
806808
if (pins->handle == n->handle)
807809
break;
808810

811+
idr_replace_ext(&ht->handle_idr, n, n->handle);
809812
RCU_INIT_POINTER(n->next, pins->next);
810813
rcu_assign_pointer(*ins, n);
811814
}
@@ -937,22 +940,33 @@ static int u32_change(struct net *net, struct sk_buff *in_skb,
937940
return -EINVAL;
938941
if (TC_U32_KEY(handle))
939942
return -EINVAL;
940-
if (handle == 0) {
941-
handle = gen_new_htid(tp->data);
942-
if (handle == 0)
943-
return -ENOMEM;
944-
}
945943
ht = kzalloc(sizeof(*ht) + divisor*sizeof(void *), GFP_KERNEL);
946944
if (ht == NULL)
947945
return -ENOBUFS;
946+
if (handle == 0) {
947+
handle = gen_new_htid(tp->data, ht);
948+
if (handle == 0) {
949+
kfree(ht);
950+
return -ENOMEM;
951+
}
952+
} else {
953+
err = idr_alloc_ext(&tp_c->handle_idr, ht, NULL,
954+
handle, handle + 1, GFP_KERNEL);
955+
if (err) {
956+
kfree(ht);
957+
return err;
958+
}
959+
}
948960
ht->tp_c = tp_c;
949961
ht->refcnt = 1;
950962
ht->divisor = divisor;
951963
ht->handle = handle;
952964
ht->prio = tp->prio;
965+
idr_init(&ht->handle_idr);
953966

954967
err = u32_replace_hw_hnode(tp, ht, flags);
955968
if (err) {
969+
idr_remove_ext(&tp_c->handle_idr, handle);
956970
kfree(ht);
957971
return err;
958972
}
@@ -986,24 +1000,33 @@ static int u32_change(struct net *net, struct sk_buff *in_skb,
9861000
if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid))
9871001
return -EINVAL;
9881002
handle = htid | TC_U32_NODE(handle);
1003+
err = idr_alloc_ext(&ht->handle_idr, NULL, NULL,
1004+
handle, handle + 1,
1005+
GFP_KERNEL);
1006+
if (err)
1007+
return err;
9891008
} else
9901009
handle = gen_new_kid(ht, htid);
9911010

992-
if (tb[TCA_U32_SEL] == NULL)
993-
return -EINVAL;
1011+
if (tb[TCA_U32_SEL] == NULL) {
1012+
err = -EINVAL;
1013+
goto erridr;
1014+
}
9941015

9951016
s = nla_data(tb[TCA_U32_SEL]);
9961017

9971018
n = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
998-
if (n == NULL)
999-
return -ENOBUFS;
1019+
if (n == NULL) {
1020+
err = -ENOBUFS;
1021+
goto erridr;
1022+
}
10001023

10011024
#ifdef CONFIG_CLS_U32_PERF
10021025
size = sizeof(struct tc_u32_pcnt) + s->nkeys * sizeof(u64);
10031026
n->pf = __alloc_percpu(size, __alignof__(struct tc_u32_pcnt));
10041027
if (!n->pf) {
1005-
kfree(n);
1006-
return -ENOBUFS;
1028+
err = -ENOBUFS;
1029+
goto errfree;
10071030
}
10081031
#endif
10091032

@@ -1066,9 +1089,12 @@ static int u32_change(struct net *net, struct sk_buff *in_skb,
10661089
errout:
10671090
tcf_exts_destroy(&n->exts);
10681091
#ifdef CONFIG_CLS_U32_PERF
1092+
errfree:
10691093
free_percpu(n->pf);
10701094
#endif
10711095
kfree(n);
1096+
erridr:
1097+
idr_remove_ext(&ht->handle_idr, handle);
10721098
return err;
10731099
}
10741100

0 commit comments

Comments
 (0)