Skip to content

Commit

Permalink
net: sched: htb: remove redundant resource cleanup in htb_init()
Browse files Browse the repository at this point in the history
If htb_init() fails, qdisc_create() invokes htb_destroy() to clear
resources. Therefore, remove redundant resource cleanup in htb_init().

Signed-off-by: Zhengchao Shao <shaozhengchao@huawei.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Zhengchao Shao authored and David S. Miller committed Sep 3, 2022
1 parent 494f506 commit d59f4e1
Showing 1 changed file with 9 additions and 27 deletions.
36 changes: 9 additions & 27 deletions net/sched/sch_htb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ static int htb_init(struct Qdisc *sch, struct nlattr *opt,

err = qdisc_class_hash_init(&q->clhash);
if (err < 0)
goto err_free_direct_qdiscs;
return err;

if (tb[TCA_HTB_DIRECT_QLEN])
q->direct_qlen = nla_get_u32(tb[TCA_HTB_DIRECT_QLEN]);
Expand All @@ -1123,8 +1123,7 @@ static int htb_init(struct Qdisc *sch, struct nlattr *opt,
qdisc = qdisc_create_dflt(dev_queue, &pfifo_qdisc_ops,
TC_H_MAKE(sch->handle, 0), extack);
if (!qdisc) {
err = -ENOMEM;
goto err_free_qdiscs;
return -ENOMEM;
}

htb_set_lockdep_class_child(qdisc);
Expand All @@ -1142,30 +1141,14 @@ static int htb_init(struct Qdisc *sch, struct nlattr *opt,
};
err = htb_offload(dev, &offload_opt);
if (err)
goto err_free_qdiscs;
return err;

/* Defer this assignment, so that htb_destroy skips offload-related
* parts (especially calling ndo_setup_tc) on errors.
*/
q->offload = true;

return 0;

err_free_qdiscs:
for (ntx = 0; ntx < q->num_direct_qdiscs && q->direct_qdiscs[ntx];
ntx++)
qdisc_put(q->direct_qdiscs[ntx]);

qdisc_class_hash_destroy(&q->clhash);
/* Prevent use-after-free and double-free when htb_destroy gets called.
*/
q->clhash.hash = NULL;
q->clhash.hashsize = 0;

err_free_direct_qdiscs:
kfree(q->direct_qdiscs);
q->direct_qdiscs = NULL;
return err;
}

static void htb_attach_offload(struct Qdisc *sch)
Expand Down Expand Up @@ -1688,13 +1671,12 @@ static void htb_destroy(struct Qdisc *sch)
qdisc_class_hash_destroy(&q->clhash);
__qdisc_reset_queue(&q->direct_queue);

if (!q->offload)
return;

offload_opt = (struct tc_htb_qopt_offload) {
.command = TC_HTB_DESTROY,
};
htb_offload(dev, &offload_opt);
if (q->offload) {
offload_opt = (struct tc_htb_qopt_offload) {
.command = TC_HTB_DESTROY,
};
htb_offload(dev, &offload_opt);
}

if (!q->direct_qdiscs)
return;
Expand Down

0 comments on commit d59f4e1

Please sign in to comment.