forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/net/sched/cls_tcindex.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * net/sched/cls_tcindex.c Packet classifier for skb->tc_index
34 *
....@@ -10,6 +11,7 @@
1011 #include <linux/skbuff.h>
1112 #include <linux/errno.h>
1213 #include <linux/slab.h>
14
+#include <linux/refcount.h>
1315 #include <net/act_api.h>
1416 #include <net/netlink.h>
1517 #include <net/pkt_cls.h>
....@@ -25,9 +27,12 @@
2527 #define DEFAULT_HASH_SIZE 64 /* optimized for diffserv */
2628
2729
30
+struct tcindex_data;
31
+
2832 struct tcindex_filter_result {
2933 struct tcf_exts exts;
3034 struct tcf_result res;
35
+ struct tcindex_data *p;
3136 struct rcu_work rwork;
3237 };
3338
....@@ -48,12 +53,27 @@
4853 u32 hash; /* hash table size; 0 if undefined */
4954 u32 alloc_hash; /* allocated size */
5055 u32 fall_through; /* 0: only classify if explicit match */
56
+ refcount_t refcnt; /* a temporary refcnt for perfect hash */
5157 struct rcu_work rwork;
5258 };
5359
5460 static inline int tcindex_filter_is_set(struct tcindex_filter_result *r)
5561 {
5662 return tcf_exts_has_actions(&r->exts) || r->res.classid;
63
+}
64
+
65
+static void tcindex_data_get(struct tcindex_data *p)
66
+{
67
+ refcount_inc(&p->refcnt);
68
+}
69
+
70
+static void tcindex_data_put(struct tcindex_data *p)
71
+{
72
+ if (refcount_dec_and_test(&p->refcnt)) {
73
+ kfree(p->perfect);
74
+ kfree(p->h);
75
+ kfree(p);
76
+ }
5777 }
5878
5979 static struct tcindex_filter_result *tcindex_lookup(struct tcindex_data *p,
....@@ -131,6 +151,7 @@
131151 p->mask = 0xffff;
132152 p->hash = DEFAULT_HASH_SIZE;
133153 p->fall_through = 1;
154
+ refcount_set(&p->refcnt, 1); /* Paired with tcindex_destroy_work() */
134155
135156 rcu_assign_pointer(tp->root, p);
136157 return 0;
....@@ -140,6 +161,7 @@
140161 {
141162 tcf_exts_destroy(&r->exts);
142163 tcf_exts_put_net(&r->exts);
164
+ tcindex_data_put(r->p);
143165 }
144166
145167 static void tcindex_destroy_rexts_work(struct work_struct *work)
....@@ -173,7 +195,7 @@
173195 }
174196
175197 static int tcindex_delete(struct tcf_proto *tp, void *arg, bool *last,
176
- struct netlink_ext_ack *extack)
198
+ bool rtnl_held, struct netlink_ext_ack *extack)
177199 {
178200 struct tcindex_data *p = rtnl_dereference(tp->root);
179201 struct tcindex_filter_result *r = arg;
....@@ -211,6 +233,8 @@
211233 else
212234 __tcindex_destroy_fexts(f);
213235 } else {
236
+ tcindex_data_get(p);
237
+
214238 if (tcf_exts_get_net(&r->exts))
215239 tcf_queue_work(&r->rwork, tcindex_destroy_rexts_work);
216240 else
....@@ -227,9 +251,7 @@
227251 struct tcindex_data,
228252 rwork);
229253
230
- kfree(p->perfect);
231
- kfree(p->h);
232
- kfree(p);
254
+ tcindex_data_put(p);
233255 }
234256
235257 static inline int
....@@ -246,11 +268,17 @@
246268 [TCA_TCINDEX_CLASSID] = { .type = NLA_U32 },
247269 };
248270
249
-static int tcindex_filter_result_init(struct tcindex_filter_result *r)
271
+static int tcindex_filter_result_init(struct tcindex_filter_result *r,
272
+ struct tcindex_data *p,
273
+ struct net *net)
250274 {
251275 memset(r, 0, sizeof(*r));
252
- return tcf_exts_init(&r->exts, TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
276
+ r->p = p;
277
+ return tcf_exts_init(&r->exts, net, TCA_TCINDEX_ACT,
278
+ TCA_TCINDEX_POLICE);
253279 }
280
+
281
+static void tcindex_free_perfect_hash(struct tcindex_data *cp);
254282
255283 static void tcindex_partial_destroy_work(struct work_struct *work)
256284 {
....@@ -258,8 +286,11 @@
258286 struct tcindex_data,
259287 rwork);
260288
261
- kfree(p->perfect);
289
+ rtnl_lock();
290
+ if (p->perfect)
291
+ tcindex_free_perfect_hash(p);
262292 kfree(p);
293
+ rtnl_unlock();
263294 }
264295
265296 static void tcindex_free_perfect_hash(struct tcindex_data *cp)
....@@ -281,13 +312,11 @@
281312 return -ENOMEM;
282313
283314 for (i = 0; i < cp->hash; i++) {
284
- err = tcf_exts_init(&cp->perfect[i].exts,
315
+ err = tcf_exts_init(&cp->perfect[i].exts, net,
285316 TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
286317 if (err < 0)
287318 goto errout;
288
-#ifdef CONFIG_NET_CLS_ACT
289
- cp->perfect[i].exts.net = net;
290
-#endif
319
+ cp->perfect[i].p = cp;
291320 }
292321
293322 return 0;
....@@ -310,10 +339,10 @@
310339 int err, balloc = 0;
311340 struct tcf_exts e;
312341
313
- err = tcf_exts_init(&e, TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
342
+ err = tcf_exts_init(&e, net, TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
314343 if (err < 0)
315344 return err;
316
- err = tcf_exts_validate(net, tp, tb, est, &e, ovr, extack);
345
+ err = tcf_exts_validate(net, tp, tb, est, &e, ovr, true, extack);
317346 if (err < 0)
318347 goto errout;
319348
....@@ -332,6 +361,7 @@
332361 cp->alloc_hash = p->alloc_hash;
333362 cp->fall_through = p->fall_through;
334363 cp->tp = tp;
364
+ refcount_set(&cp->refcnt, 1); /* Paired with tcindex_destroy_work() */
335365
336366 if (tb[TCA_TCINDEX_HASH])
337367 cp->hash = nla_get_u32(tb[TCA_TCINDEX_HASH]);
....@@ -368,7 +398,7 @@
368398 }
369399 cp->h = p->h;
370400
371
- err = tcindex_filter_result_init(&new_filter_result);
401
+ err = tcindex_filter_result_init(&new_filter_result, cp, net);
372402 if (err < 0)
373403 goto errout_alloc;
374404 if (old_r)
....@@ -436,7 +466,7 @@
436466 goto errout_alloc;
437467 f->key = handle;
438468 f->next = NULL;
439
- err = tcindex_filter_result_init(&f->result);
469
+ err = tcindex_filter_result_init(&f->result, cp, net);
440470 if (err < 0) {
441471 kfree(f);
442472 goto errout_alloc;
....@@ -449,7 +479,7 @@
449479 }
450480
451481 if (old_r && old_r != r) {
452
- err = tcindex_filter_result_init(old_r);
482
+ err = tcindex_filter_result_init(old_r, cp, net);
453483 if (err < 0) {
454484 kfree(f);
455485 goto errout_alloc;
....@@ -500,7 +530,7 @@
500530 tcindex_change(struct net *net, struct sk_buff *in_skb,
501531 struct tcf_proto *tp, unsigned long base, u32 handle,
502532 struct nlattr **tca, void **arg, bool ovr,
503
- struct netlink_ext_ack *extack)
533
+ bool rtnl_held, struct netlink_ext_ack *extack)
504534 {
505535 struct nlattr *opt = tca[TCA_OPTIONS];
506536 struct nlattr *tb[TCA_TCINDEX_MAX + 1];
....@@ -510,12 +540,13 @@
510540
511541 pr_debug("tcindex_change(tp %p,handle 0x%08x,tca %p,arg %p),opt %p,"
512542 "p %p,r %p,*arg %p\n",
513
- tp, handle, tca, arg, opt, p, r, arg ? *arg : NULL);
543
+ tp, handle, tca, arg, opt, p, r, *arg);
514544
515545 if (!opt)
516546 return 0;
517547
518
- err = nla_parse_nested(tb, TCA_TCINDEX_MAX, opt, tcindex_policy, NULL);
548
+ err = nla_parse_nested_deprecated(tb, TCA_TCINDEX_MAX, opt,
549
+ tcindex_policy, NULL);
519550 if (err < 0)
520551 return err;
521552
....@@ -523,7 +554,8 @@
523554 tca[TCA_RATE], ovr, extack);
524555 }
525556
526
-static void tcindex_walk(struct tcf_proto *tp, struct tcf_walker *walker)
557
+static void tcindex_walk(struct tcf_proto *tp, struct tcf_walker *walker,
558
+ bool rtnl_held)
527559 {
528560 struct tcindex_data *p = rtnl_dereference(tp->root);
529561 struct tcindex_filter *f, *next;
....@@ -559,7 +591,7 @@
559591 }
560592 }
561593
562
-static void tcindex_destroy(struct tcf_proto *tp,
594
+static void tcindex_destroy(struct tcf_proto *tp, bool rtnl_held,
563595 struct netlink_ext_ack *extack)
564596 {
565597 struct tcindex_data *p = rtnl_dereference(tp->root);
....@@ -570,6 +602,14 @@
570602 if (p->perfect) {
571603 for (i = 0; i < p->hash; i++) {
572604 struct tcindex_filter_result *r = p->perfect + i;
605
+
606
+ /* tcf_queue_work() does not guarantee the ordering we
607
+ * want, so we have to take this refcnt temporarily to
608
+ * ensure 'p' is freed after all tcindex_filter_result
609
+ * here. Imperfect hash does not need this, because it
610
+ * uses linked lists rather than an array.
611
+ */
612
+ tcindex_data_get(p);
573613
574614 tcf_unbind_filter(tp, &r->res);
575615 if (tcf_exts_get_net(&r->exts))
....@@ -586,7 +626,7 @@
586626
587627 for (f = rtnl_dereference(p->h[i]); f; f = next) {
588628 next = rtnl_dereference(f->next);
589
- tcindex_delete(tp, &f->result, &last, NULL);
629
+ tcindex_delete(tp, &f->result, &last, rtnl_held, NULL);
590630 }
591631 }
592632
....@@ -595,7 +635,7 @@
595635
596636
597637 static int tcindex_dump(struct net *net, struct tcf_proto *tp, void *fh,
598
- struct sk_buff *skb, struct tcmsg *t)
638
+ struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
599639 {
600640 struct tcindex_data *p = rtnl_dereference(tp->root);
601641 struct tcindex_filter_result *r = fh;
....@@ -605,7 +645,7 @@
605645 tp, fh, skb, t, p, r);
606646 pr_debug("p->perfect %p p->h %p\n", p->perfect, p->h);
607647
608
- nest = nla_nest_start(skb, TCA_OPTIONS);
648
+ nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
609649 if (nest == NULL)
610650 goto nla_put_failure;
611651