pistachio: add 4.9 kernel support
[librecmc/librecmc.git] / target / linux / pistachio / patches-4.9 / 107-clockevents-Retry-programming-min-delta-up-to-10-tim.patch
1 From b46f8c74afdd30cd52bfdcc2231470a0bab04416 Mon Sep 17 00:00:00 2001
2 From: James Hogan <james.hogan@imgtec.com>
3 Date: Fri, 22 Apr 2016 18:22:45 +0100
4 Subject: clockevents: Retry programming min delta up to 10 times
5
6 Under virtualisation it is possible to get unexpected latency during a
7 clockevent device's set_next_event() callback which can make it return
8 -ETIME even for a delta based on min_delta_ns.
9
10 The clockevents_program_min_delta() implementation for
11 CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST=n doesn't handle retries when this
12 happens, nor does clockevents_program_event() or its callers when force
13 is true (for example hrtimer_reprogram()). This can result in hangs
14 until the clock event device does a full period.
15
16 It isn't appropriate to use MIN_ADJUST in this case as occasional
17 hypervisor induced high latency will cause min_delta_ns to quickly
18 increase to the maximum.
19 Instead, borrow the retry pattern from the MIN_ADJUST case, but without
20 making adjustments. We retry up to 10 times before giving up.
21
22 (picked https://patchwork.kernel.org/patch/8909491/)
23
24 Signed-off-by: James Hogan <james.hogan@imgtec.com>
25 ---
26  kernel/time/clockevents.c | 26 +++++++++++++++++++-------
27  1 file changed, 19 insertions(+), 7 deletions(-)
28
29 diff --git a/kernel/time/clockevents.c b/kernel/time/clockevents.c
30 index 2c5bc77..ddd7e82 100644
31 --- a/kernel/time/clockevents.c
32 +++ b/kernel/time/clockevents.c
33 @@ -281,16 +281,28 @@ static int clockevents_program_min_delta(struct clock_event_device *dev)
34  {
35         unsigned long long clc;
36         int64_t delta;
37 +       int i;
38  
39 -       delta = dev->min_delta_ns;
40 -       dev->next_event = ktime_add_ns(ktime_get(), delta);
41 +       for (i = 0;;) {
42 +               delta = dev->min_delta_ns;
43 +               dev->next_event = ktime_add_ns(ktime_get(), delta);
44  
45 -       if (clockevent_state_shutdown(dev))
46 -               return 0;
47 +               if (clockevent_state_shutdown(dev))
48 +                       return 0;
49  
50 -       dev->retries++;
51 -       clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
52 -       return dev->set_next_event((unsigned long) clc, dev);
53 +               dev->retries++;
54 +               clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
55 +               if (dev->set_next_event((unsigned long) clc, dev) == 0)
56 +                       return 0;
57 +
58 +               if (++i > 9) {
59 +                       /*
60 +                        * We tried 10 times to program the device with the
61 +                        * given min_delta_ns. Get out of here.
62 +                        */
63 +                       return -ETIME;
64 +               }
65 +       }
66  }
67  
68  #endif /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
69 -- 
70 2.7.4
71