Commit hnyman's changes
[lede.git] / target / linux / generic / patches-4.4 / 036-net_sched-avoid-too-many-hrtimer_start-calls.patch
1 From: Eric Dumazet <edumazet@google.com>
2 Date: Mon, 23 May 2016 14:24:56 -0700
3 Subject: [PATCH] net_sched: avoid too many hrtimer_start() calls
4
5 I found a serious performance bug in packet schedulers using hrtimers.
6
7 sch_htb and sch_fq are definitely impacted by this problem.
8
9 We constantly rearm high resolution timers if some packets are throttled
10 in one (or more) class, and other packets are flying through qdisc on
11 another (non throttled) class.
12
13 hrtimer_start() does not have the mod_timer() trick of doing nothing if
14 expires value does not change :
15
16         if (timer_pending(timer) &&
17             timer->expires == expires)
18                 return 1;
19
20 This issue is particularly visible when multiple cpus can queue/dequeue
21 packets on the same qdisc, as hrtimer code has to lock a remote base.
22
23 I used following fix :
24
25 1) Change htb to use qdisc_watchdog_schedule_ns() instead of open-coding
26 it.
27
28 2) Cache watchdog prior expiration. hrtimer might provide this, but I
29 prefer to not rely on some hrtimer internal.
30
31 Signed-off-by: Eric Dumazet <edumazet@google.com>
32 Signed-off-by: David S. Miller <davem@davemloft.net>
33 ---
34
35 --- a/include/net/pkt_sched.h
36 +++ b/include/net/pkt_sched.h
37 @@ -61,6 +61,7 @@ psched_tdiff_bounded(psched_time_t tv1,
38  }
39  
40  struct qdisc_watchdog {
41 +       u64             last_expires;
42         struct hrtimer  timer;
43         struct Qdisc    *qdisc;
44  };
45 --- a/net/sched/sch_api.c
46 +++ b/net/sched/sch_api.c
47 @@ -607,6 +607,10 @@ void qdisc_watchdog_schedule_ns(struct q
48         if (throttle)
49                 qdisc_throttled(wd->qdisc);
50  
51 +       if (wd->last_expires == expires)
52 +               return;
53 +
54 +       wd->last_expires = expires;
55         hrtimer_start(&wd->timer,
56                       ns_to_ktime(expires),
57                       HRTIMER_MODE_ABS_PINNED);
58 --- a/net/sched/sch_htb.c
59 +++ b/net/sched/sch_htb.c
60 @@ -928,17 +928,10 @@ ok:
61                 }
62         }
63         qdisc_qstats_overlimit(sch);
64 -       if (likely(next_event > q->now)) {
65 -               if (!test_bit(__QDISC_STATE_DEACTIVATED,
66 -                             &qdisc_root_sleeping(q->watchdog.qdisc)->state)) {
67 -                       ktime_t time = ns_to_ktime(next_event);
68 -                       qdisc_throttled(q->watchdog.qdisc);
69 -                       hrtimer_start(&q->watchdog.timer, time,
70 -                                     HRTIMER_MODE_ABS_PINNED);
71 -               }
72 -       } else {
73 +       if (likely(next_event > q->now))
74 +               qdisc_watchdog_schedule_ns(&q->watchdog, next_event, true);
75 +       else
76                 schedule_work(&q->work);
77 -       }
78  fin:
79         return skb;
80  }