Skip to content

Commit bd16a6c

Browse files
Eric Dumazetdavem330
authored andcommitted
net_sched: sfq: fix mem alloc error recovery
Since commit 817fb15 (net_sched: sfq: allow divisor to be a parameter), we can leave perturbation timer armed if a memory allocation error aborts sfq_init(). Memory containing active struct timer_list is freed and kernel can crash. Call sfq_destroy() from sfq_init() to properly dismantle qdisc. Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 6cfb5e7 commit bd16a6c

1 file changed

Lines changed: 33 additions & 19 deletions

File tree

‎net/sched/sch_sfq.c‎

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -544,10 +544,38 @@ static int sfq_change(struct Qdisc *sch, struct nlattr *opt)
544544
return 0;
545545
}
546546

547+
static void *sfq_alloc(size_t sz)
548+
{
549+
void *ptr = kmalloc(sz, GFP_KERNEL | __GFP_NOWARN);
550+
551+
if (!ptr)
552+
ptr = vmalloc(sz);
553+
return ptr;
554+
}
555+
556+
static void sfq_free(void *addr)
557+
{
558+
if (addr) {
559+
if (is_vmalloc_addr(addr))
560+
vfree(addr);
561+
else
562+
kfree(addr);
563+
}
564+
}
565+
566+
static void sfq_destroy(struct Qdisc *sch)
567+
{
568+
struct sfq_sched_data *q = qdisc_priv(sch);
569+
570+
tcf_destroy_chain(&q->filter_list);
571+
q->perturb_period = 0;
572+
del_timer_sync(&q->perturb_timer);
573+
sfq_free(q->ht);
574+
}
575+
547576
static int sfq_init(struct Qdisc *sch, struct nlattr *opt)
548577
{
549578
struct sfq_sched_data *q = qdisc_priv(sch);
550-
size_t sz;
551579
int i;
552580

553581
q->perturb_timer.function = sfq_perturbation;
@@ -574,12 +602,11 @@ static int sfq_init(struct Qdisc *sch, struct nlattr *opt)
574602
return err;
575603
}
576604

577-
sz = sizeof(q->ht[0]) * q->divisor;
578-
q->ht = kmalloc(sz, GFP_KERNEL);
579-
if (!q->ht && sz > PAGE_SIZE)
580-
q->ht = vmalloc(sz);
581-
if (!q->ht)
605+
q->ht = sfq_alloc(sizeof(q->ht[0]) * q->divisor);
606+
if (!q->ht) {
607+
sfq_destroy(sch);
582608
return -ENOMEM;
609+
}
583610
for (i = 0; i < q->divisor; i++)
584611
q->ht[i] = SFQ_EMPTY_SLOT;
585612

@@ -594,19 +621,6 @@ static int sfq_init(struct Qdisc *sch, struct nlattr *opt)
594621
return 0;
595622
}
596623

597-
static void sfq_destroy(struct Qdisc *sch)
598-
{
599-
struct sfq_sched_data *q = qdisc_priv(sch);
600-
601-
tcf_destroy_chain(&q->filter_list);
602-
q->perturb_period = 0;
603-
del_timer_sync(&q->perturb_timer);
604-
if (is_vmalloc_addr(q->ht))
605-
vfree(q->ht);
606-
else
607-
kfree(q->ht);
608-
}
609-
610624
static int sfq_dump(struct Qdisc *sch, struct sk_buff *skb)
611625
{
612626
struct sfq_sched_data *q = qdisc_priv(sch);

0 commit comments

Comments
 (0)