Skip to content

Commit 1e052be

Browse files
congwang1986davem330
authored andcommitted
net_sched: destroy proto tp when all filters are gone
Kernel automatically creates a tp for each (kind, protocol, priority) tuple, which has handle 0, when we add a new filter, but it still is left there after we remove our own, unless we don't specify the handle (literally means all the filters under the tuple). For example this one is left: # tc filter show dev eth0 filter parent 8001: protocol arp pref 49152 basic The user-space is hard to clean up these for kernel because filters like u32 are organized in a complex way. So kernel is responsible to remove it after all filters are gone. Each type of filter has its own way to store the filters, so each type has to provide its way to check if all filters are gone. Cc: Jamal Hadi Salim <jhs@mojatatu.com> Signed-off-by: Cong Wang <cwang@twopensource.com> Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com> Acked-by: Jamal Hadi Salim<jhs@mojatatu.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent fc6c6c2 commit 1e052be

12 files changed

Lines changed: 99 additions & 23 deletions

File tree

‎include/net/sch_generic.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ struct tcf_proto_ops {
213213
const struct tcf_proto *,
214214
struct tcf_result *);
215215
int (*init)(struct tcf_proto*);
216-
void (*destroy)(struct tcf_proto*);
216+
bool (*destroy)(struct tcf_proto*, bool);
217217

218218
unsigned long (*get)(struct tcf_proto*, u32 handle);
219219
int (*change)(struct net *net, struct sk_buff *,
@@ -399,7 +399,7 @@ struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
399399
const struct Qdisc_ops *ops, u32 parentid);
400400
void __qdisc_calculate_pkt_len(struct sk_buff *skb,
401401
const struct qdisc_size_table *stab);
402-
void tcf_destroy(struct tcf_proto *tp);
402+
bool tcf_destroy(struct tcf_proto *tp, bool force);
403403
void tcf_destroy_chain(struct tcf_proto __rcu **fl);
404404

405405
/* Reset all TX qdiscs greater then index of a device. */

‎net/sched/cls_api.c‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n)
286286
RCU_INIT_POINTER(*back, next);
287287

288288
tfilter_notify(net, skb, n, tp, fh, RTM_DELTFILTER);
289-
tcf_destroy(tp);
289+
tcf_destroy(tp, true);
290290
err = 0;
291291
goto errout;
292292
}
@@ -301,14 +301,20 @@ static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n)
301301
err = -EEXIST;
302302
if (n->nlmsg_flags & NLM_F_EXCL) {
303303
if (tp_created)
304-
tcf_destroy(tp);
304+
tcf_destroy(tp, true);
305305
goto errout;
306306
}
307307
break;
308308
case RTM_DELTFILTER:
309309
err = tp->ops->delete(tp, fh);
310-
if (err == 0)
310+
if (err == 0) {
311311
tfilter_notify(net, skb, n, tp, fh, RTM_DELTFILTER);
312+
if (tcf_destroy(tp, false)) {
313+
struct tcf_proto *next = rtnl_dereference(tp->next);
314+
315+
RCU_INIT_POINTER(*back, next);
316+
}
317+
}
312318
goto errout;
313319
case RTM_GETTFILTER:
314320
err = tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER);
@@ -329,7 +335,7 @@ static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n)
329335
tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER);
330336
} else {
331337
if (tp_created)
332-
tcf_destroy(tp);
338+
tcf_destroy(tp, true);
333339
}
334340

335341
errout:

‎net/sched/cls_basic.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,22 @@ static void basic_delete_filter(struct rcu_head *head)
9696
kfree(f);
9797
}
9898

99-
static void basic_destroy(struct tcf_proto *tp)
99+
static bool basic_destroy(struct tcf_proto *tp, bool force)
100100
{
101101
struct basic_head *head = rtnl_dereference(tp->root);
102102
struct basic_filter *f, *n;
103103

104+
if (!force && !list_empty(&head->flist))
105+
return false;
106+
104107
list_for_each_entry_safe(f, n, &head->flist, link) {
105108
list_del_rcu(&f->link);
106109
tcf_unbind_filter(tp, &f->res);
107110
call_rcu(&f->rcu, basic_delete_filter);
108111
}
109112
RCU_INIT_POINTER(tp->root, NULL);
110113
kfree_rcu(head, rcu);
114+
return true;
111115
}
112116

113117
static int basic_delete(struct tcf_proto *tp, unsigned long arg)

‎net/sched/cls_bpf.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,14 @@ static int cls_bpf_delete(struct tcf_proto *tp, unsigned long arg)
137137
return 0;
138138
}
139139

140-
static void cls_bpf_destroy(struct tcf_proto *tp)
140+
static bool cls_bpf_destroy(struct tcf_proto *tp, bool force)
141141
{
142142
struct cls_bpf_head *head = rtnl_dereference(tp->root);
143143
struct cls_bpf_prog *prog, *tmp;
144144

145+
if (!force && !list_empty(&head->plist))
146+
return false;
147+
145148
list_for_each_entry_safe(prog, tmp, &head->plist, link) {
146149
list_del_rcu(&prog->link);
147150
tcf_unbind_filter(tp, &prog->res);
@@ -150,6 +153,7 @@ static void cls_bpf_destroy(struct tcf_proto *tp)
150153

151154
RCU_INIT_POINTER(tp->root, NULL);
152155
kfree_rcu(head, rcu);
156+
return true;
153157
}
154158

155159
static unsigned long cls_bpf_get(struct tcf_proto *tp, u32 handle)

‎net/sched/cls_cgroup.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,18 @@ static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb,
143143
return err;
144144
}
145145

146-
static void cls_cgroup_destroy(struct tcf_proto *tp)
146+
static bool cls_cgroup_destroy(struct tcf_proto *tp, bool force)
147147
{
148148
struct cls_cgroup_head *head = rtnl_dereference(tp->root);
149149

150+
if (!force)
151+
return false;
152+
150153
if (head) {
151154
RCU_INIT_POINTER(tp->root, NULL);
152155
call_rcu(&head->rcu, cls_cgroup_destroy_rcu);
153156
}
157+
return true;
154158
}
155159

156160
static int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg)

‎net/sched/cls_flow.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,17 +557,21 @@ static int flow_init(struct tcf_proto *tp)
557557
return 0;
558558
}
559559

560-
static void flow_destroy(struct tcf_proto *tp)
560+
static bool flow_destroy(struct tcf_proto *tp, bool force)
561561
{
562562
struct flow_head *head = rtnl_dereference(tp->root);
563563
struct flow_filter *f, *next;
564564

565+
if (!force && !list_empty(&head->filters))
566+
return false;
567+
565568
list_for_each_entry_safe(f, next, &head->filters, list) {
566569
list_del_rcu(&f->list);
567570
call_rcu(&f->rcu, flow_destroy_filter);
568571
}
569572
RCU_INIT_POINTER(tp->root, NULL);
570573
kfree_rcu(head, rcu);
574+
return true;
571575
}
572576

573577
static unsigned long flow_get(struct tcf_proto *tp, u32 handle)

‎net/sched/cls_fw.c‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,20 @@ static void fw_delete_filter(struct rcu_head *head)
133133
kfree(f);
134134
}
135135

136-
static void fw_destroy(struct tcf_proto *tp)
136+
static bool fw_destroy(struct tcf_proto *tp, bool force)
137137
{
138138
struct fw_head *head = rtnl_dereference(tp->root);
139139
struct fw_filter *f;
140140
int h;
141141

142142
if (head == NULL)
143-
return;
143+
return true;
144+
145+
if (!force) {
146+
for (h = 0; h < HTSIZE; h++)
147+
if (rcu_access_pointer(head->ht[h]))
148+
return false;
149+
}
144150

145151
for (h = 0; h < HTSIZE; h++) {
146152
while ((f = rtnl_dereference(head->ht[h])) != NULL) {
@@ -152,6 +158,7 @@ static void fw_destroy(struct tcf_proto *tp)
152158
}
153159
RCU_INIT_POINTER(tp->root, NULL);
154160
kfree_rcu(head, rcu);
161+
return true;
155162
}
156163

157164
static int fw_delete(struct tcf_proto *tp, unsigned long arg)

‎net/sched/cls_route.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,20 @@ route4_delete_filter(struct rcu_head *head)
277277
kfree(f);
278278
}
279279

280-
static void route4_destroy(struct tcf_proto *tp)
280+
static bool route4_destroy(struct tcf_proto *tp, bool force)
281281
{
282282
struct route4_head *head = rtnl_dereference(tp->root);
283283
int h1, h2;
284284

285285
if (head == NULL)
286-
return;
286+
return true;
287+
288+
if (!force) {
289+
for (h1 = 0; h1 <= 256; h1++) {
290+
if (rcu_access_pointer(head->table[h1]))
291+
return false;
292+
}
293+
}
287294

288295
for (h1 = 0; h1 <= 256; h1++) {
289296
struct route4_bucket *b;
@@ -308,6 +315,7 @@ static void route4_destroy(struct tcf_proto *tp)
308315
}
309316
RCU_INIT_POINTER(tp->root, NULL);
310317
kfree_rcu(head, rcu);
318+
return true;
311319
}
312320

313321
static int route4_delete(struct tcf_proto *tp, unsigned long arg)

‎net/sched/cls_rsvp.h‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,20 @@ rsvp_delete_filter(struct tcf_proto *tp, struct rsvp_filter *f)
291291
kfree_rcu(f, rcu);
292292
}
293293

294-
static void rsvp_destroy(struct tcf_proto *tp)
294+
static bool rsvp_destroy(struct tcf_proto *tp, bool force)
295295
{
296296
struct rsvp_head *data = rtnl_dereference(tp->root);
297297
int h1, h2;
298298

299299
if (data == NULL)
300-
return;
300+
return true;
301+
302+
if (!force) {
303+
for (h1 = 0; h1 < 256; h1++) {
304+
if (rcu_access_pointer(data->ht[h1]))
305+
return false;
306+
}
307+
}
301308

302309
RCU_INIT_POINTER(tp->root, NULL);
303310

@@ -319,6 +326,7 @@ static void rsvp_destroy(struct tcf_proto *tp)
319326
}
320327
}
321328
kfree_rcu(data, rcu);
329+
return true;
322330
}
323331

324332
static int rsvp_delete(struct tcf_proto *tp, unsigned long arg)

‎net/sched/cls_tcindex.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,11 +468,14 @@ static void tcindex_walk(struct tcf_proto *tp, struct tcf_walker *walker)
468468
}
469469
}
470470

471-
static void tcindex_destroy(struct tcf_proto *tp)
471+
static bool tcindex_destroy(struct tcf_proto *tp, bool force)
472472
{
473473
struct tcindex_data *p = rtnl_dereference(tp->root);
474474
struct tcf_walker walker;
475475

476+
if (!force)
477+
return false;
478+
476479
pr_debug("tcindex_destroy(tp %p),p %p\n", tp, p);
477480
walker.count = 0;
478481
walker.skip = 0;
@@ -481,6 +484,7 @@ static void tcindex_destroy(struct tcf_proto *tp)
481484

482485
RCU_INIT_POINTER(tp->root, NULL);
483486
call_rcu(&p->rcu, __tcindex_destroy);
487+
return true;
484488
}
485489

486490

0 commit comments

Comments
 (0)