]> git.kernelconcepts.de Git - karo-tx-linux.git/commitdiff
Merge branch 'linus' into sched/core, to pick up fixes
authorIngo Molnar <mingo@kernel.org>
Tue, 23 May 2017 07:50:35 +0000 (09:50 +0200)
committerIngo Molnar <mingo@kernel.org>
Tue, 23 May 2017 07:50:35 +0000 (09:50 +0200)
Signed-off-by: Ingo Molnar <mingo@kernel.org>
17 files changed:
arch/x86/events/core.c
arch/x86/include/asm/timer.h
arch/x86/kernel/tsc.c
arch/x86/platform/uv/tlb_uv.c
drivers/cpuidle/cpuidle.c
include/linux/clocksource.h
include/linux/cpumask.h
include/linux/sched/clock.h
kernel/sched/clock.c
kernel/sched/core.c
kernel/sched/fair.c
kernel/sched/features.h
kernel/sched/sched.h
kernel/sched/topology.c
kernel/time/clocksource.c
kernel/time/tick-sched.c
lib/cpumask.c

index 580b60f5ac83cea46a75a11185c8ef0a8c2da516..d3990462582c5d69ffa26a3670236fb4227cfc36 100644 (file)
@@ -2255,7 +2255,7 @@ static struct pmu pmu = {
 void arch_perf_update_userpage(struct perf_event *event,
                               struct perf_event_mmap_page *userpg, u64 now)
 {
-       struct cyc2ns_data *data;
+       struct cyc2ns_data data;
        u64 offset;
 
        userpg->cap_user_time = 0;
@@ -2267,17 +2267,17 @@ void arch_perf_update_userpage(struct perf_event *event,
        if (!using_native_sched_clock() || !sched_clock_stable())
                return;
 
-       data = cyc2ns_read_begin();
+       cyc2ns_read_begin(&data);
 
-       offset = data->cyc2ns_offset + __sched_clock_offset;
+       offset = data.cyc2ns_offset + __sched_clock_offset;
 
        /*
         * Internal timekeeping for enabled/running/stopped times
         * is always in the local_clock domain.
         */
        userpg->cap_user_time = 1;
-       userpg->time_mult = data->cyc2ns_mul;
-       userpg->time_shift = data->cyc2ns_shift;
+       userpg->time_mult = data.cyc2ns_mul;
+       userpg->time_shift = data.cyc2ns_shift;
        userpg->time_offset = offset - now;
 
        /*
@@ -2289,7 +2289,7 @@ void arch_perf_update_userpage(struct perf_event *event,
                userpg->time_zero = offset;
        }
 
-       cyc2ns_read_end(data);
+       cyc2ns_read_end();
 }
 
 void
index 27e9f9d769b892ef27fa3cf13cb95a7c9563b559..2016962103df5be9b858a82cca1cf355219e3b33 100644 (file)
@@ -29,11 +29,9 @@ struct cyc2ns_data {
        u32 cyc2ns_mul;
        u32 cyc2ns_shift;
        u64 cyc2ns_offset;
-       u32 __count;
-       /* u32 hole */
-}; /* 24 bytes -- do not grow */
+}; /* 16 bytes */
 
-extern struct cyc2ns_data *cyc2ns_read_begin(void);
-extern void cyc2ns_read_end(struct cyc2ns_data *);
+extern void cyc2ns_read_begin(struct cyc2ns_data *);
+extern void cyc2ns_read_end(void);
 
 #endif /* _ASM_X86_TIMER_H */
index 714dfba6a1e713fb6b5f4268c318f913bb266628..a3b54426436012386afdaa6a28d61dce87c8104b 100644 (file)
@@ -51,115 +51,34 @@ static u32 art_to_tsc_denominator;
 static u64 art_to_tsc_offset;
 struct clocksource *art_related_clocksource;
 
-/*
- * Use a ring-buffer like data structure, where a writer advances the head by
- * writing a new data entry and a reader advances the tail when it observes a
- * new entry.
- *
- * Writers are made to wait on readers until there's space to write a new
- * entry.
- *
- * This means that we can always use an {offset, mul} pair to compute a ns
- * value that is 'roughly' in the right direction, even if we're writing a new
- * {offset, mul} pair during the clock read.
- *
- * The down-side is that we can no longer guarantee strict monotonicity anymore
- * (assuming the TSC was that to begin with), because while we compute the
- * intersection point of the two clock slopes and make sure the time is
- * continuous at the point of switching; we can no longer guarantee a reader is
- * strictly before or after the switch point.
- *
- * It does mean a reader no longer needs to disable IRQs in order to avoid
- * CPU-Freq updates messing with his times, and similarly an NMI reader will
- * no longer run the risk of hitting half-written state.
- */
-
 struct cyc2ns {
-       struct cyc2ns_data data[2];     /*  0 + 2*24 = 48 */
-       struct cyc2ns_data *head;       /* 48 + 8    = 56 */
-       struct cyc2ns_data *tail;       /* 56 + 8    = 64 */
-}; /* exactly fits one cacheline */
-
-static DEFINE_PER_CPU_ALIGNED(struct cyc2ns, cyc2ns);
-
-struct cyc2ns_data *cyc2ns_read_begin(void)
-{
-       struct cyc2ns_data *head;
-
-       preempt_disable();
+       struct cyc2ns_data data[2];     /*  0 + 2*16 = 32 */
+       seqcount_t         seq;         /* 32 + 4    = 36 */
 
-       head = this_cpu_read(cyc2ns.head);
-       /*
-        * Ensure we observe the entry when we observe the pointer to it.
-        * matches the wmb from cyc2ns_write_end().
-        */
-       smp_read_barrier_depends();
-       head->__count++;
-       barrier();
-
-       return head;
-}
+}; /* fits one cacheline */
 
-void cyc2ns_read_end(struct cyc2ns_data *head)
-{
-       barrier();
-       /*
-        * If we're the outer most nested read; update the tail pointer
-        * when we're done. This notifies possible pending writers
-        * that we've observed the head pointer and that the other
-        * entry is now free.
-        */
-       if (!--head->__count) {
-               /*
-                * x86-TSO does not reorder writes with older reads;
-                * therefore once this write becomes visible to another
-                * cpu, we must be finished reading the cyc2ns_data.
-                *
-                * matches with cyc2ns_write_begin().
-                */
-               this_cpu_write(cyc2ns.tail, head);
-       }
-       preempt_enable();
-}
+static DEFINE_PER_CPU_ALIGNED(struct cyc2ns, cyc2ns);
 
-/*
- * Begin writing a new @data entry for @cpu.
- *
- * Assumes some sort of write side lock; currently 'provided' by the assumption
- * that cpufreq will call its notifiers sequentially.
- */
-static struct cyc2ns_data *cyc2ns_write_begin(int cpu)
+void cyc2ns_read_begin(struct cyc2ns_data *data)
 {
-       struct cyc2ns *c2n = &per_cpu(cyc2ns, cpu);
-       struct cyc2ns_data *data = c2n->data;
+       int seq, idx;
 
-       if (data == c2n->head)
-               data++;
+       preempt_disable_notrace();
 
-       /* XXX send an IPI to @cpu in order to guarantee a read? */
+       do {
+               seq = this_cpu_read(cyc2ns.seq.sequence);
+               idx = seq & 1;
 
-       /*
-        * When we observe the tail write from cyc2ns_read_end(),
-        * the cpu must be done with that entry and its safe
-        * to start writing to it.
-        */
-       while (c2n->tail == data)
-               cpu_relax();
+               data->cyc2ns_offset = this_cpu_read(cyc2ns.data[idx].cyc2ns_offset);
+               data->cyc2ns_mul    = this_cpu_read(cyc2ns.data[idx].cyc2ns_mul);
+               data->cyc2ns_shift  = this_cpu_read(cyc2ns.data[idx].cyc2ns_shift);
 
-       return data;
+       } while (unlikely(seq != this_cpu_read(cyc2ns.seq.sequence)));
 }
 
-static void cyc2ns_write_end(int cpu, struct cyc2ns_data *data)
+void cyc2ns_read_end(void)
 {
-       struct cyc2ns *c2n = &per_cpu(cyc2ns, cpu);
-
-       /*
-        * Ensure the @data writes are visible before we publish the
-        * entry. Matches the data-depencency in cyc2ns_read_begin().
-        */
-       smp_wmb();
-
-       ACCESS_ONCE(c2n->head) = data;
+       preempt_enable_notrace();
 }
 
 /*
@@ -191,7 +110,6 @@ static void cyc2ns_data_init(struct cyc2ns_data *data)
        data->cyc2ns_mul = 0;
        data->cyc2ns_shift = 0;
        data->cyc2ns_offset = 0;
-       data->__count = 0;
 }
 
 static void cyc2ns_init(int cpu)
@@ -201,51 +119,29 @@ static void cyc2ns_init(int cpu)
        cyc2ns_data_init(&c2n->data[0]);
        cyc2ns_data_init(&c2n->data[1]);
 
-       c2n->head = c2n->data;
-       c2n->tail = c2n->data;
+       seqcount_init(&c2n->seq);
 }
 
 static inline unsigned long long cycles_2_ns(unsigned long long cyc)
 {
-       struct cyc2ns_data *data, *tail;
+       struct cyc2ns_data data;
        unsigned long long ns;
 
-       /*
-        * See cyc2ns_read_*() for details; replicated in order to avoid
-        * an extra few instructions that came with the abstraction.
-        * Notable, it allows us to only do the __count and tail update
-        * dance when its actually needed.
-        */
-
-       preempt_disable_notrace();
-       data = this_cpu_read(cyc2ns.head);
-       tail = this_cpu_read(cyc2ns.tail);
-
-       if (likely(data == tail)) {
-               ns = data->cyc2ns_offset;
-               ns += mul_u64_u32_shr(cyc, data->cyc2ns_mul, data->cyc2ns_shift);
-       } else {
-               data->__count++;
-
-               barrier();
+       cyc2ns_read_begin(&data);
 
-               ns = data->cyc2ns_offset;
-               ns += mul_u64_u32_shr(cyc, data->cyc2ns_mul, data->cyc2ns_shift);
+       ns = data.cyc2ns_offset;
+       ns += mul_u64_u32_shr(cyc, data.cyc2ns_mul, data.cyc2ns_shift);
 
-               barrier();
-
-               if (!--data->__count)
-                       this_cpu_write(cyc2ns.tail, data);
-       }
-       preempt_enable_notrace();
+       cyc2ns_read_end();
 
        return ns;
 }
 
-static void set_cyc2ns_scale(unsigned long khz, int cpu)
+static void __set_cyc2ns_scale(unsigned long khz, int cpu, unsigned long long tsc_now)
 {
-       unsigned long long tsc_now, ns_now;
-       struct cyc2ns_data *data;
+       unsigned long long ns_now;
+       struct cyc2ns_data data;
+       struct cyc2ns *c2n;
        unsigned long flags;
 
        local_irq_save(flags);
@@ -254,9 +150,6 @@ static void set_cyc2ns_scale(unsigned long khz, int cpu)
        if (!khz)
                goto done;
 
-       data = cyc2ns_write_begin(cpu);
-
-       tsc_now = rdtsc();
        ns_now = cycles_2_ns(tsc_now);
 
        /*
@@ -264,7 +157,7 @@ static void set_cyc2ns_scale(unsigned long khz, int cpu)
         * time function is continuous; see the comment near struct
         * cyc2ns_data.
         */
-       clocks_calc_mult_shift(&data->cyc2ns_mul, &data->cyc2ns_shift, khz,
+       clocks_calc_mult_shift(&data.cyc2ns_mul, &data.cyc2ns_shift, khz,
                               NSEC_PER_MSEC, 0);
 
        /*
@@ -273,20 +166,31 @@ static void set_cyc2ns_scale(unsigned long khz, int cpu)
         * conversion algorithm shifting a 32-bit value (now specifies a 64-bit
         * value) - refer perf_event_mmap_page documentation in perf_event.h.
         */
-       if (data->cyc2ns_shift == 32) {
-               data->cyc2ns_shift = 31;
-               data->cyc2ns_mul >>= 1;
+       if (data.cyc2ns_shift == 32) {
+               data.cyc2ns_shift = 31;
+               data.cyc2ns_mul >>= 1;
        }
 
-       data->cyc2ns_offset = ns_now -
-               mul_u64_u32_shr(tsc_now, data->cyc2ns_mul, data->cyc2ns_shift);
+       data.cyc2ns_offset = ns_now -
+               mul_u64_u32_shr(tsc_now, data.cyc2ns_mul, data.cyc2ns_shift);
+
+       c2n = per_cpu_ptr(&cyc2ns, cpu);
 
-       cyc2ns_write_end(cpu, data);
+       raw_write_seqcount_latch(&c2n->seq);
+       c2n->data[0] = data;
+       raw_write_seqcount_latch(&c2n->seq);
+       c2n->data[1] = data;
 
 done:
-       sched_clock_idle_wakeup_event(0);
+       sched_clock_idle_wakeup_event();
        local_irq_restore(flags);
 }
+
+static void set_cyc2ns_scale(unsigned long khz, int cpu)
+{
+       __set_cyc2ns_scale(khz, cpu, rdtsc());
+}
+
 /*
  * Scheduler clock - returns current time in nanosec units.
  */
@@ -374,6 +278,8 @@ static int __init tsc_setup(char *str)
                tsc_clocksource_reliable = 1;
        if (!strncmp(str, "noirqtime", 9))
                no_sched_irq_time = 1;
+       if (!strcmp(str, "unstable"))
+               mark_tsc_unstable("boot parameter");
        return 1;
 }
 
@@ -1127,6 +1033,15 @@ static void tsc_cs_mark_unstable(struct clocksource *cs)
        pr_info("Marking TSC unstable due to clocksource watchdog\n");
 }
 
+static void tsc_cs_tick_stable(struct clocksource *cs)
+{
+       if (tsc_unstable)
+               return;
+
+       if (using_native_sched_clock())
+               sched_clock_tick_stable();
+}
+
 /*
  * .mask MUST be CLOCKSOURCE_MASK(64). See comment above read_tsc()
  */
@@ -1140,6 +1055,7 @@ static struct clocksource clocksource_tsc = {
        .archdata               = { .vclock_mode = VCLOCK_TSC },
        .resume                 = tsc_resume,
        .mark_unstable          = tsc_cs_mark_unstable,
+       .tick_stable            = tsc_cs_tick_stable,
 };
 
 void mark_tsc_unstable(char *reason)
@@ -1255,6 +1171,7 @@ static void tsc_refine_calibration_work(struct work_struct *work)
        static int hpet;
        u64 tsc_stop, ref_stop, delta;
        unsigned long freq;
+       int cpu;
 
        /* Don't bother refining TSC on unstable systems */
        if (check_tsc_unstable())
@@ -1305,6 +1222,10 @@ static void tsc_refine_calibration_work(struct work_struct *work)
        /* Inform the TSC deadline clockevent devices about the recalibration */
        lapic_update_tsc_freq();
 
+       /* Update the sched_clock() rate to match the clocksource one */
+       for_each_possible_cpu(cpu)
+               __set_cyc2ns_scale(tsc_khz, cpu, tsc_stop);
+
 out:
        if (boot_cpu_has(X86_FEATURE_ART))
                art_related_clocksource = &clocksource_tsc;
@@ -1350,7 +1271,7 @@ device_initcall(init_tsc_clocksource);
 
 void __init tsc_init(void)
 {
-       u64 lpj;
+       u64 lpj, cyc;
        int cpu;
 
        if (!boot_cpu_has(X86_FEATURE_TSC)) {
@@ -1390,9 +1311,10 @@ void __init tsc_init(void)
         * speed as the bootup CPU. (cpufreq notifiers will fix this
         * up if their speed diverges)
         */
+       cyc = rdtsc();
        for_each_possible_cpu(cpu) {
                cyc2ns_init(cpu);
-               set_cyc2ns_scale(tsc_khz, cpu);
+               __set_cyc2ns_scale(tsc_khz, cpu, cyc);
        }
 
        if (tsc_disabled > 0)
index 42e65fee5673e26a273fff2c12b712913f7d727a..795671593528c6ae12634b249f51cbf673eb1211 100644 (file)
@@ -456,12 +456,13 @@ static void reset_with_ipi(struct pnmask *distribution, struct bau_control *bcp)
  */
 static inline unsigned long long cycles_2_ns(unsigned long long cyc)
 {
-       struct cyc2ns_data *data = cyc2ns_read_begin();
+       struct cyc2ns_data data;
        unsigned long long ns;
 
-       ns = mul_u64_u32_shr(cyc, data->cyc2ns_mul, data->cyc2ns_shift);
+       cyc2ns_read_begin(&data);
+       ns = mul_u64_u32_shr(cyc, data.cyc2ns_mul, data.cyc2ns_shift);
+       cyc2ns_read_end();
 
-       cyc2ns_read_end(data);
        return ns;
 }
 
@@ -470,12 +471,13 @@ static inline unsigned long long cycles_2_ns(unsigned long long cyc)
  */
 static inline unsigned long long ns_2_cycles(unsigned long long ns)
 {
-       struct cyc2ns_data *data = cyc2ns_read_begin();
+       struct cyc2ns_data data;
        unsigned long long cyc;
 
-       cyc = (ns << data->cyc2ns_shift) / data->cyc2ns_mul;
+       cyc2ns_read_begin(&data);
+       cyc = (ns << data.cyc2ns_shift) / data.cyc2ns_mul;
+       cyc2ns_read_end();
 
-       cyc2ns_read_end(data);
        return cyc;
 }
 
index 2706be7ed3340fe41ad174d75dc27a9e010e4412..60bb64f4329da292f3eeac6846f4bf6fb179cbb9 100644 (file)
@@ -220,6 +220,7 @@ int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
        entered_state = target_state->enter(dev, drv, index);
        start_critical_timings();
 
+       sched_clock_idle_wakeup_event();
        time_end = ns_to_ktime(local_clock());
        trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
 
index f2b10d9ebd04e7d49d032b5cb097e7f2ae630e56..81490456c242936125205a99e68ecc3ec86c70fa 100644 (file)
@@ -96,6 +96,7 @@ struct clocksource {
        void (*suspend)(struct clocksource *cs);
        void (*resume)(struct clocksource *cs);
        void (*mark_unstable)(struct clocksource *cs);
+       void (*tick_stable)(struct clocksource *cs);
 
        /* private: */
 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
index 2404ad238c0b8c0309e8c1480339a19967968edf..a21b1fb9a9689fdf1bf8157c8b5457bd053784e2 100644 (file)
@@ -236,6 +236,23 @@ unsigned int cpumask_local_spread(unsigned int i, int node);
                (cpu) = cpumask_next_zero((cpu), (mask)),       \
                (cpu) < nr_cpu_ids;)
 
+extern int cpumask_next_wrap(int n, const struct cpumask *mask, int start, bool wrap);
+
+/**
+ * for_each_cpu_wrap - iterate over every cpu in a mask, starting at a specified location
+ * @cpu: the (optionally unsigned) integer iterator
+ * @mask: the cpumask poiter
+ * @start: the start location
+ *
+ * The implementation does not assume any bit in @mask is set (including @start).
+ *
+ * After the loop, cpu is >= nr_cpu_ids.
+ */
+#define for_each_cpu_wrap(cpu, mask, start)                                    \
+       for ((cpu) = cpumask_next_wrap((start)-1, (mask), (start), false);      \
+            (cpu) < nr_cpumask_bits;                                           \
+            (cpu) = cpumask_next_wrap((cpu), (mask), (start), true))
+
 /**
  * for_each_cpu_and - iterate over every cpu in both masks
  * @cpu: the (optionally unsigned) integer iterator
index 34fe92ce1ebd7c6e9dfa0dac94ff4512deb6f95f..a55600ffdf4b5c23210199c5335c5bc07b744518 100644 (file)
@@ -23,10 +23,6 @@ extern u64 sched_clock_cpu(int cpu);
 extern void sched_clock_init(void);
 
 #ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
-static inline void sched_clock_init_late(void)
-{
-}
-
 static inline void sched_clock_tick(void)
 {
 }
@@ -39,7 +35,7 @@ static inline void sched_clock_idle_sleep_event(void)
 {
 }
 
-static inline void sched_clock_idle_wakeup_event(u64 delta_ns)
+static inline void sched_clock_idle_wakeup_event(void)
 {
 }
 
@@ -53,7 +49,6 @@ static inline u64 local_clock(void)
        return sched_clock();
 }
 #else
-extern void sched_clock_init_late(void);
 extern int sched_clock_stable(void);
 extern void clear_sched_clock_stable(void);
 
@@ -63,10 +58,10 @@ extern void clear_sched_clock_stable(void);
  */
 extern u64 __sched_clock_offset;
 
-
 extern void sched_clock_tick(void);
+extern void sched_clock_tick_stable(void);
 extern void sched_clock_idle_sleep_event(void);
-extern void sched_clock_idle_wakeup_event(u64 delta_ns);
+extern void sched_clock_idle_wakeup_event(void);
 
 /*
  * As outlined in clock.c, provides a fast, high resolution, nanosecond
index 00a45c45beca09829ad479aad9ba299f5498a42e..1a0d389d2f2b94bc3d221156e2c8de06cef1c436 100644 (file)
@@ -64,6 +64,7 @@
 #include <linux/workqueue.h>
 #include <linux/compiler.h>
 #include <linux/tick.h>
+#include <linux/init.h>
 
 /*
  * Scheduler clock - returns current time in nanosec units.
@@ -124,6 +125,12 @@ int sched_clock_stable(void)
        return static_branch_likely(&__sched_clock_stable);
 }
 
+static void __scd_stamp(struct sched_clock_data *scd)
+{
+       scd->tick_gtod = ktime_get_ns();
+       scd->tick_raw = sched_clock();
+}
+
 static void __set_sched_clock_stable(void)
 {
        struct sched_clock_data *scd = this_scd();
@@ -141,8 +148,38 @@ static void __set_sched_clock_stable(void)
        tick_dep_clear(TICK_DEP_BIT_CLOCK_UNSTABLE);
 }
 
+/*
+ * If we ever get here, we're screwed, because we found out -- typically after
+ * the fact -- that TSC wasn't good. This means all our clocksources (including
+ * ktime) could have reported wrong values.
+ *
+ * What we do here is an attempt to fix up and continue sort of where we left
+ * off in a coherent manner.
+ *
+ * The only way to fully avoid random clock jumps is to boot with:
+ * "tsc=unstable".
+ */
 static void __sched_clock_work(struct work_struct *work)
 {
+       struct sched_clock_data *scd;
+       int cpu;
+
+       /* take a current timestamp and set 'now' */
+       preempt_disable();
+       scd = this_scd();
+       __scd_stamp(scd);
+       scd->clock = scd->tick_gtod + __gtod_offset;
+       preempt_enable();
+
+       /* clone to all CPUs */
+       for_each_possible_cpu(cpu)
+               per_cpu(sched_clock_data, cpu) = *scd;
+
+       printk(KERN_WARNING "TSC found unstable after boot, most likely due to broken BIOS. Use 'tsc=unstable'.\n");
+       printk(KERN_INFO "sched_clock: Marking unstable (%lld, %lld)<-(%lld, %lld)\n",
+                       scd->tick_gtod, __gtod_offset,
+                       scd->tick_raw,  __sched_clock_offset);
+
        static_branch_disable(&__sched_clock_stable);
 }
 
@@ -150,27 +187,11 @@ static DECLARE_WORK(sched_clock_work, __sched_clock_work);
 
 static void __clear_sched_clock_stable(void)
 {
-       struct sched_clock_data *scd = this_scd();
-
-       /*
-        * Attempt to make the stable->unstable transition continuous.
-        *
-        * Trouble is, this is typically called from the TSC watchdog
-        * timer, which is late per definition. This means the tick
-        * values can already be screwy.
-        *
-        * Still do what we can.
-        */
-       __gtod_offset = (scd->tick_raw + __sched_clock_offset) - (scd->tick_gtod);
-
-       printk(KERN_INFO "sched_clock: Marking unstable (%lld, %lld)<-(%lld, %lld)\n",
-                       scd->tick_gtod, __gtod_offset,
-                       scd->tick_raw,  __sched_clock_offset);
+       if (!sched_clock_stable())
+               return;
 
        tick_dep_set(TICK_DEP_BIT_CLOCK_UNSTABLE);
-
-       if (sched_clock_stable())
-               schedule_work(&sched_clock_work);
+       schedule_work(&sched_clock_work);
 }
 
 void clear_sched_clock_stable(void)
@@ -183,7 +204,11 @@ void clear_sched_clock_stable(void)
                __clear_sched_clock_stable();
 }
 
-void sched_clock_init_late(void)
+/*
+ * We run this as late_initcall() such that it runs after all built-in drivers,
+ * notably: acpi_processor and intel_idle, which can mark the TSC as unstable.
+ */
+static int __init sched_clock_init_late(void)
 {
        sched_clock_running = 2;
        /*
@@ -197,7 +222,10 @@ void sched_clock_init_late(void)
 
        if (__sched_clock_stable_early)
                __set_sched_clock_stable();
+
+       return 0;
 }
+late_initcall(sched_clock_init_late);
 
 /*
  * min, max except they take wrapping into account
@@ -347,21 +375,38 @@ void sched_clock_tick(void)
 {
        struct sched_clock_data *scd;
 
+       if (sched_clock_stable())
+               return;
+
+       if (unlikely(!sched_clock_running))
+               return;
+
        WARN_ON_ONCE(!irqs_disabled());
 
+       scd = this_scd();
+       __scd_stamp(scd);
+       sched_clock_local(scd);
+}
+
+void sched_clock_tick_stable(void)
+{
+       u64 gtod, clock;
+
+       if (!sched_clock_stable())
+               return;
+
        /*
-        * Update these values even if sched_clock_stable(), because it can
-        * become unstable at any point in time at which point we need some
-        * values to fall back on.
+        * Called under watchdog_lock.
         *
-        * XXX arguably we can skip this if we expose tsc_clocksource_reliable
+        * The watchdog just found this TSC to (still) be stable, so now is a
+        * good moment to update our __gtod_offset. Because once we find the
+        * TSC to be unstable, any computation will be computing crap.
         */
-       scd = this_scd();
-       scd->tick_raw  = sched_clock();
-       scd->tick_gtod = ktime_get_ns();
-
-       if (!sched_clock_stable() && likely(sched_clock_running))
-               sched_clock_local(scd);
+       local_irq_disable();
+       gtod = ktime_get_ns();
+       clock = sched_clock();
+       __gtod_offset = (clock + __sched_clock_offset) - gtod;
+       local_irq_enable();
 }
 
 /*
@@ -374,15 +419,21 @@ void sched_clock_idle_sleep_event(void)
 EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
 
 /*
- * We just idled delta nanoseconds (called with irqs disabled):
+ * We just idled; resync with ktime.
  */
-void sched_clock_idle_wakeup_event(u64 delta_ns)
+void sched_clock_idle_wakeup_event(void)
 {
-       if (timekeeping_suspended)
+       unsigned long flags;
+
+       if (sched_clock_stable())
+               return;
+
+       if (unlikely(timekeeping_suspended))
                return;
 
+       local_irq_save(flags);
        sched_clock_tick();
-       touch_softlockup_watchdog_sched();
+       local_irq_restore(flags);
 }
 EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
 
index 803c3bc274c4660bb672c2522331db967883aee4..dde5d1e860f0683f061dcae8993a35d4b4dd4537 100644 (file)
@@ -5958,7 +5958,6 @@ void __init sched_init_smp(void)
        cpumask_var_t non_isolated_cpus;
 
        alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
-       alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
 
        sched_init_numa();
 
@@ -5968,7 +5967,7 @@ void __init sched_init_smp(void)
         * happen.
         */
        mutex_lock(&sched_domains_mutex);
-       init_sched_domains(cpu_active_mask);
+       sched_init_domains(cpu_active_mask);
        cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
        if (cpumask_empty(non_isolated_cpus))
                cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
@@ -5984,7 +5983,6 @@ void __init sched_init_smp(void)
        init_sched_dl_class();
 
        sched_init_smt();
-       sched_clock_init_late();
 
        sched_smp_initialized = true;
 }
@@ -6000,7 +5998,6 @@ early_initcall(migration_init);
 void __init sched_init_smp(void)
 {
        sched_init_granularity();
-       sched_clock_init_late();
 }
 #endif /* CONFIG_SMP */
 
@@ -6199,7 +6196,6 @@ void __init sched_init(void)
        calc_load_update = jiffies + LOAD_FREQ;
 
 #ifdef CONFIG_SMP
-       zalloc_cpumask_var(&sched_domains_tmpmask, GFP_NOWAIT);
        /* May be allocated at isolcpus cmdline parse time */
        if (cpu_isolated_map == NULL)
                zalloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT);
index d711093218415d77ead6405004dd9e41323ad924..219fe58e30238592b7fc879a8e65992df84ec752 100644 (file)
@@ -369,8 +369,9 @@ static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
 }
 
 /* Iterate thr' all leaf cfs_rq's on a runqueue */
-#define for_each_leaf_cfs_rq(rq, cfs_rq) \
-       list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
+#define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos)                     \
+       list_for_each_entry_safe(cfs_rq, pos, &rq->leaf_cfs_rq_list,    \
+                                leaf_cfs_rq_list)
 
 /* Do the two (enqueued) entities belong to the same group ? */
 static inline struct cfs_rq *
@@ -463,8 +464,8 @@ static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
 {
 }
 
-#define for_each_leaf_cfs_rq(rq, cfs_rq) \
-               for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
+#define for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos)     \
+               for (cfs_rq = &rq->cfs, pos = NULL; cfs_rq; cfs_rq = pos)
 
 static inline struct sched_entity *parent_entity(struct sched_entity *se)
 {
@@ -2916,12 +2917,12 @@ ___update_load_avg(u64 now, int cpu, struct sched_avg *sa,
        /*
         * Step 2: update *_avg.
         */
-       sa->load_avg = div_u64(sa->load_sum, LOAD_AVG_MAX);
+       sa->load_avg = div_u64(sa->load_sum, LOAD_AVG_MAX - 1024 + sa->period_contrib);
        if (cfs_rq) {
                cfs_rq->runnable_load_avg =
-                       div_u64(cfs_rq->runnable_load_sum, LOAD_AVG_MAX);
+                       div_u64(cfs_rq->runnable_load_sum, LOAD_AVG_MAX - 1024 + sa->period_contrib);
        }
-       sa->util_avg = sa->util_sum / LOAD_AVG_MAX;
+       sa->util_avg = sa->util_sum / (LOAD_AVG_MAX - 1024 + sa->period_contrib);
 
        return 1;
 }
@@ -4642,24 +4643,43 @@ static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
        hrtimer_cancel(&cfs_b->slack_timer);
 }
 
+/*
+ * Both these cpu hotplug callbacks race against unregister_fair_sched_group()
+ *
+ * The race is harmless, since modifying bandwidth settings of unhooked group
+ * bits doesn't do much.
+ */
+
+/* cpu online calback */
 static void __maybe_unused update_runtime_enabled(struct rq *rq)
 {
-       struct cfs_rq *cfs_rq;
+       struct task_group *tg;
+
+       lockdep_assert_held(&rq->lock);
 
-       for_each_leaf_cfs_rq(rq, cfs_rq) {
-               struct cfs_bandwidth *cfs_b = &cfs_rq->tg->cfs_bandwidth;
+       rcu_read_lock();
+       list_for_each_entry_rcu(tg, &task_groups, list) {
+               struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
+               struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
 
                raw_spin_lock(&cfs_b->lock);
                cfs_rq->runtime_enabled = cfs_b->quota != RUNTIME_INF;
                raw_spin_unlock(&cfs_b->lock);
        }
+       rcu_read_unlock();
 }
 
+/* cpu offline callback */
 static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq)
 {
-       struct cfs_rq *cfs_rq;
+       struct task_group *tg;
+
+       lockdep_assert_held(&rq->lock);
+
+       rcu_read_lock();
+       list_for_each_entry_rcu(tg, &task_groups, list) {
+               struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
 
-       for_each_leaf_cfs_rq(rq, cfs_rq) {
                if (!cfs_rq->runtime_enabled)
                        continue;
 
@@ -4677,6 +4697,7 @@ static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq)
                if (cfs_rq_throttled(cfs_rq))
                        unthrottle_cfs_rq(cfs_rq);
        }
+       rcu_read_unlock();
 }
 
 #else /* CONFIG_CFS_BANDWIDTH */
@@ -5484,12 +5505,12 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p,
                int i;
 
                /* Skip over this group if it has no CPUs allowed */
-               if (!cpumask_intersects(sched_group_cpus(group),
+               if (!cpumask_intersects(sched_group_span(group),
                                        &p->cpus_allowed))
                        continue;
 
                local_group = cpumask_test_cpu(this_cpu,
-                                              sched_group_cpus(group));
+                                              sched_group_span(group));
 
                /*
                 * Tally up the load of all CPUs in the group and find
@@ -5499,7 +5520,7 @@ find_idlest_group(struct sched_domain *sd, struct task_struct *p,
                runnable_load = 0;
                max_spare_cap = 0;
 
-               for_each_cpu(i, sched_group_cpus(group)) {
+               for_each_cpu(i, sched_group_span(group)) {
                        /* Bias balancing toward cpus of our domain */
                        if (local_group)
                                load = source_load(i, load_idx);
@@ -5602,10 +5623,10 @@ find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
 
        /* Check if we have any choice: */
        if (group->group_weight == 1)
-               return cpumask_first(sched_group_cpus(group));
+               return cpumask_first(sched_group_span(group));
 
        /* Traverse only the allowed CPUs */
-       for_each_cpu_and(i, sched_group_cpus(group), &p->cpus_allowed) {
+       for_each_cpu_and(i, sched_group_span(group), &p->cpus_allowed) {
                if (idle_cpu(i)) {
                        struct rq *rq = cpu_rq(i);
                        struct cpuidle_state *idle = idle_get_state(rq);
@@ -5640,43 +5661,6 @@ find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
        return shallowest_idle_cpu != -1 ? shallowest_idle_cpu : least_loaded_cpu;
 }
 
-/*
- * Implement a for_each_cpu() variant that starts the scan at a given cpu
- * (@start), and wraps around.
- *
- * This is used to scan for idle CPUs; such that not all CPUs looking for an
- * idle CPU find the same CPU. The down-side is that tasks tend to cycle
- * through the LLC domain.
- *
- * Especially tbench is found sensitive to this.
- */
-
-static int cpumask_next_wrap(int n, const struct cpumask *mask, int start, int *wrapped)
-{
-       int next;
-
-again:
-       next = find_next_bit(cpumask_bits(mask), nr_cpumask_bits, n+1);
-
-       if (*wrapped) {
-               if (next >= start)
-                       return nr_cpumask_bits;
-       } else {
-               if (next >= nr_cpumask_bits) {
-                       *wrapped = 1;
-                       n = -1;
-                       goto again;
-               }
-       }
-
-       return next;
-}
-
-#define for_each_cpu_wrap(cpu, mask, start, wrap)                              \
-       for ((wrap) = 0, (cpu) = (start)-1;                                     \
-               (cpu) = cpumask_next_wrap((cpu), (mask), (start), &(wrap)),     \
-               (cpu) < nr_cpumask_bits; )
-
 #ifdef CONFIG_SCHED_SMT
 
 static inline void set_idle_cores(int cpu, int val)
@@ -5736,7 +5720,7 @@ unlock:
 static int select_idle_core(struct task_struct *p, struct sched_domain *sd, int target)
 {
        struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_idle_mask);
-       int core, cpu, wrap;
+       int core, cpu;
 
        if (!static_branch_likely(&sched_smt_present))
                return -1;
@@ -5746,7 +5730,7 @@ static int select_idle_core(struct task_struct *p, struct sched_domain *sd, int
 
        cpumask_and(cpus, sched_domain_span(sd), &p->cpus_allowed);
 
-       for_each_cpu_wrap(core, cpus, target, wrap) {
+       for_each_cpu_wrap(core, cpus, target) {
                bool idle = true;
 
                for_each_cpu(cpu, cpu_smt_mask(core)) {
@@ -5812,7 +5796,7 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, int t
        u64 avg_cost, avg_idle = this_rq()->avg_idle;
        u64 time, cost;
        s64 delta;
-       int cpu, wrap;
+       int cpu;
 
        this_sd = rcu_dereference(*this_cpu_ptr(&sd_llc));
        if (!this_sd)
@@ -5829,7 +5813,7 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, int t
 
        time = local_clock();
 
-       for_each_cpu_wrap(cpu, sched_domain_span(sd), target, wrap) {
+       for_each_cpu_wrap(cpu, sched_domain_span(sd), target) {
                if (!cpumask_test_cpu(cpu, &p->cpus_allowed))
                        continue;
                if (idle_cpu(cpu))
@@ -6970,10 +6954,28 @@ static void attach_tasks(struct lb_env *env)
 }
 
 #ifdef CONFIG_FAIR_GROUP_SCHED
+
+static inline bool cfs_rq_is_decayed(struct cfs_rq *cfs_rq)
+{
+       if (cfs_rq->load.weight)
+               return false;
+
+       if (cfs_rq->avg.load_sum)
+               return false;
+
+       if (cfs_rq->avg.util_sum)
+               return false;
+
+       if (cfs_rq->runnable_load_sum)
+               return false;
+
+       return true;
+}
+
 static void update_blocked_averages(int cpu)
 {
        struct rq *rq = cpu_rq(cpu);
-       struct cfs_rq *cfs_rq;
+       struct cfs_rq *cfs_rq, *pos;
        struct rq_flags rf;
 
        rq_lock_irqsave(rq, &rf);
@@ -6983,7 +6985,7 @@ static void update_blocked_averages(int cpu)
         * Iterates the task_group tree in a bottom up fashion, see
         * list_add_leaf_cfs_rq() for details.
         */
-       for_each_leaf_cfs_rq(rq, cfs_rq) {
+       for_each_leaf_cfs_rq_safe(rq, cfs_rq, pos) {
                struct sched_entity *se;
 
                /* throttled entities do not contribute to load */
@@ -6997,6 +6999,13 @@ static void update_blocked_averages(int cpu)
                se = cfs_rq->tg->se[cpu];
                if (se && !skip_blocked_update(se))
                        update_load_avg(se, 0);
+
+               /*
+                * There can be a lot of idle CPU cgroups.  Don't let fully
+                * decayed cfs_rqs linger on the list.
+                */
+               if (cfs_rq_is_decayed(cfs_rq))
+                       list_del_leaf_cfs_rq(cfs_rq);
        }
        rq_unlock_irqrestore(rq, &rf);
 }
@@ -7229,7 +7238,7 @@ void update_group_capacity(struct sched_domain *sd, int cpu)
                 * span the current group.
                 */
 
-               for_each_cpu(cpu, sched_group_cpus(sdg)) {
+               for_each_cpu(cpu, sched_group_span(sdg)) {
                        struct sched_group_capacity *sgc;
                        struct rq *rq = cpu_rq(cpu);
 
@@ -7408,7 +7417,7 @@ static inline void update_sg_lb_stats(struct lb_env *env,
 
        memset(sgs, 0, sizeof(*sgs));
 
-       for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
+       for_each_cpu_and(i, sched_group_span(group), env->cpus) {
                struct rq *rq = cpu_rq(i);
 
                /* Bias balancing toward cpus of our domain */
@@ -7572,7 +7581,7 @@ static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sd
                struct sg_lb_stats *sgs = &tmp_sgs;
                int local_group;
 
-               local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg));
+               local_group = cpumask_test_cpu(env->dst_cpu, sched_group_span(sg));
                if (local_group) {
                        sds->local = sg;
                        sgs = local;
@@ -7927,7 +7936,7 @@ static struct rq *find_busiest_queue(struct lb_env *env,
        unsigned long busiest_load = 0, busiest_capacity = 1;
        int i;
 
-       for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
+       for_each_cpu_and(i, sched_group_span(group), env->cpus) {
                unsigned long capacity, wl;
                enum fbq_type rt;
 
@@ -8033,7 +8042,6 @@ static int active_load_balance_cpu_stop(void *data);
 static int should_we_balance(struct lb_env *env)
 {
        struct sched_group *sg = env->sd->groups;
-       struct cpumask *sg_cpus, *sg_mask;
        int cpu, balance_cpu = -1;
 
        /*
@@ -8043,11 +8051,9 @@ static int should_we_balance(struct lb_env *env)
        if (env->idle == CPU_NEWLY_IDLE)
                return 1;
 
-       sg_cpus = sched_group_cpus(sg);
-       sg_mask = sched_group_mask(sg);
        /* Try to find first idle cpu */
-       for_each_cpu_and(cpu, sg_cpus, env->cpus) {
-               if (!cpumask_test_cpu(cpu, sg_mask) || !idle_cpu(cpu))
+       for_each_cpu_and(cpu, group_balance_mask(sg), env->cpus) {
+               if (!idle_cpu(cpu))
                        continue;
 
                balance_cpu = cpu;
@@ -8083,7 +8089,7 @@ static int load_balance(int this_cpu, struct rq *this_rq,
                .sd             = sd,
                .dst_cpu        = this_cpu,
                .dst_rq         = this_rq,
-               .dst_grpmask    = sched_group_cpus(sd->groups),
+               .dst_grpmask    = sched_group_span(sd->groups),
                .idle           = idle,
                .loop_break     = sched_nr_migrate_break,
                .cpus           = cpus,
@@ -9523,10 +9529,10 @@ const struct sched_class fair_sched_class = {
 #ifdef CONFIG_SCHED_DEBUG
 void print_cfs_stats(struct seq_file *m, int cpu)
 {
-       struct cfs_rq *cfs_rq;
+       struct cfs_rq *cfs_rq, *pos;
 
        rcu_read_lock();
-       for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
+       for_each_leaf_cfs_rq_safe(cpu_rq(cpu), cfs_rq, pos)
                print_cfs_rq(m, cpu, cfs_rq);
        rcu_read_unlock();
 }
index 11192e0cb122c72066a2f46417246464d909ea7c..dc4d1483b038176c20a0eb2d1af3c7cd499c6688 100644 (file)
@@ -76,7 +76,6 @@ SCHED_FEAT(WARN_DOUBLE_CLOCK, false)
 SCHED_FEAT(RT_PUSH_IPI, true)
 #endif
 
-SCHED_FEAT(FORCE_SD_OVERLAP, false)
 SCHED_FEAT(RT_RUNTIME_SHARE, true)
 SCHED_FEAT(LB_MIN, false)
 SCHED_FEAT(ATTACH_AGE_LOAD, true)
index 6dda2aab731e04c5e1f88272f180f44df22e907a..f8cf1d87f0659d50e70409a36d370b233ab9f0fd 100644 (file)
@@ -606,11 +606,9 @@ struct root_domain {
 
 extern struct root_domain def_root_domain;
 extern struct mutex sched_domains_mutex;
-extern cpumask_var_t fallback_doms;
-extern cpumask_var_t sched_domains_tmpmask;
 
 extern void init_defrootdomain(void);
-extern int init_sched_domains(const struct cpumask *cpu_map);
+extern int sched_init_domains(const struct cpumask *cpu_map);
 extern void rq_attach_root(struct rq *rq, struct root_domain *rd);
 
 #endif /* CONFIG_SMP */
@@ -1025,7 +1023,11 @@ struct sched_group_capacity {
        unsigned long next_update;
        int imbalance; /* XXX unrelated to capacity but shared group state */
 
-       unsigned long cpumask[0]; /* iteration mask */
+#ifdef CONFIG_SCHED_DEBUG
+       int id;
+#endif
+
+       unsigned long cpumask[0]; /* balance mask */
 };
 
 struct sched_group {
@@ -1046,16 +1048,15 @@ struct sched_group {
        unsigned long cpumask[0];
 };
 
-static inline struct cpumask *sched_group_cpus(struct sched_group *sg)
+static inline struct cpumask *sched_group_span(struct sched_group *sg)
 {
        return to_cpumask(sg->cpumask);
 }
 
 /*
- * cpumask masking which cpus in the group are allowed to iterate up the domain
- * tree.
+ * See build_balance_mask().
  */
-static inline struct cpumask *sched_group_mask(struct sched_group *sg)
+static inline struct cpumask *group_balance_mask(struct sched_group *sg)
 {
        return to_cpumask(sg->sgc->cpumask);
 }
@@ -1066,7 +1067,7 @@ static inline struct cpumask *sched_group_mask(struct sched_group *sg)
  */
 static inline unsigned int group_first_cpu(struct sched_group *group)
 {
-       return cpumask_first(sched_group_cpus(group));
+       return cpumask_first(sched_group_span(group));
 }
 
 extern int group_balance_cpu(struct sched_group *sg);
index 1b0b4fb12837f4d3181051f95f7a7161399e16f1..79895aec281eb5ad198fae3e5e8aed31849ed900 100644 (file)
@@ -10,6 +10,7 @@ DEFINE_MUTEX(sched_domains_mutex);
 
 /* Protected by sched_domains_mutex: */
 cpumask_var_t sched_domains_tmpmask;
+cpumask_var_t sched_domains_tmpmask2;
 
 #ifdef CONFIG_SCHED_DEBUG
 
@@ -35,7 +36,7 @@ static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
 
        cpumask_clear(groupmask);
 
-       printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
+       printk(KERN_DEBUG "%*s domain-%d: ", level, "", level);
 
        if (!(sd->flags & SD_LOAD_BALANCE)) {
                printk("does not load-balance\n");
@@ -45,14 +46,14 @@ static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
                return -1;
        }
 
-       printk(KERN_CONT "span %*pbl level %s\n",
+       printk(KERN_CONT "span=%*pbl level=%s\n",
               cpumask_pr_args(sched_domain_span(sd)), sd->name);
 
        if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
                printk(KERN_ERR "ERROR: domain->span does not contain "
                                "CPU%d\n", cpu);
        }
-       if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
+       if (!cpumask_test_cpu(cpu, sched_group_span(group))) {
                printk(KERN_ERR "ERROR: domain->groups does not contain"
                                " CPU%d\n", cpu);
        }
@@ -65,29 +66,47 @@ static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
                        break;
                }
 
-               if (!cpumask_weight(sched_group_cpus(group))) {
+               if (!cpumask_weight(sched_group_span(group))) {
                        printk(KERN_CONT "\n");
                        printk(KERN_ERR "ERROR: empty group\n");
                        break;
                }
 
                if (!(sd->flags & SD_OVERLAP) &&
-                   cpumask_intersects(groupmask, sched_group_cpus(group))) {
+                   cpumask_intersects(groupmask, sched_group_span(group))) {
                        printk(KERN_CONT "\n");
                        printk(KERN_ERR "ERROR: repeated CPUs\n");
                        break;
                }
 
-               cpumask_or(groupmask, groupmask, sched_group_cpus(group));
+               cpumask_or(groupmask, groupmask, sched_group_span(group));
 
-               printk(KERN_CONT " %*pbl",
-                      cpumask_pr_args(sched_group_cpus(group)));
-               if (group->sgc->capacity != SCHED_CAPACITY_SCALE) {
-                       printk(KERN_CONT " (cpu_capacity = %lu)",
-                               group->sgc->capacity);
+               printk(KERN_CONT " %d:{ span=%*pbl",
+                               group->sgc->id,
+                               cpumask_pr_args(sched_group_span(group)));
+
+               if ((sd->flags & SD_OVERLAP) &&
+                   !cpumask_equal(group_balance_mask(group), sched_group_span(group))) {
+                       printk(KERN_CONT " mask=%*pbl",
+                               cpumask_pr_args(group_balance_mask(group)));
+               }
+
+               if (group->sgc->capacity != SCHED_CAPACITY_SCALE)
+                       printk(KERN_CONT " cap=%lu", group->sgc->capacity);
+
+               if (group == sd->groups && sd->child &&
+                   !cpumask_equal(sched_domain_span(sd->child),
+                                  sched_group_span(group))) {
+                       printk(KERN_ERR "ERROR: domain->groups does not match domain->child\n");
                }
 
+               printk(KERN_CONT " }");
+
                group = group->next;
+
+               if (group != sd->groups)
+                       printk(KERN_CONT ",");
+
        } while (group != sd->groups);
        printk(KERN_CONT "\n");
 
@@ -113,7 +132,7 @@ static void sched_domain_debug(struct sched_domain *sd, int cpu)
                return;
        }
 
-       printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
+       printk(KERN_DEBUG "CPU%d attaching sched-domain(s):\n", cpu);
 
        for (;;) {
                if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask))
@@ -477,46 +496,214 @@ enum s_alloc {
 };
 
 /*
- * Build an iteration mask that can exclude certain CPUs from the upwards
- * domain traversal.
+ * Return the canonical balance CPU for this group, this is the first CPU
+ * of this group that's also in the balance mask.
  *
- * Asymmetric node setups can result in situations where the domain tree is of
- * unequal depth, make sure to skip domains that already cover the entire
- * range.
+ * The balance mask are all those CPUs that could actually end up at this
+ * group. See build_balance_mask().
  *
- * In that case build_sched_domains() will have terminated the iteration early
- * and our sibling sd spans will be empty. Domains should always include the
- * CPU they're built on, so check that.
+ * Also see should_we_balance().
  */
-static void build_group_mask(struct sched_domain *sd, struct sched_group *sg)
+int group_balance_cpu(struct sched_group *sg)
 {
-       const struct cpumask *span = sched_domain_span(sd);
+       return cpumask_first(group_balance_mask(sg));
+}
+
+
+/*
+ * NUMA topology (first read the regular topology blurb below)
+ *
+ * Given a node-distance table, for example:
+ *
+ *   node   0   1   2   3
+ *     0:  10  20  30  20
+ *     1:  20  10  20  30
+ *     2:  30  20  10  20
+ *     3:  20  30  20  10
+ *
+ * which represents a 4 node ring topology like:
+ *
+ *   0 ----- 1
+ *   |       |
+ *   |       |
+ *   |       |
+ *   3 ----- 2
+ *
+ * We want to construct domains and groups to represent this. The way we go
+ * about doing this is to build the domains on 'hops'. For each NUMA level we
+ * construct the mask of all nodes reachable in @level hops.
+ *
+ * For the above NUMA topology that gives 3 levels:
+ *
+ * NUMA-2      0-3             0-3             0-3             0-3
+ *  groups:    {0-1,3},{1-3}   {0-2},{0,2-3}   {1-3},{0-1,3}   {0,2-3},{0-2}
+ *
+ * NUMA-1      0-1,3           0-2             1-3             0,2-3
+ *  groups:    {0},{1},{3}     {0},{1},{2}     {1},{2},{3}     {0},{2},{3}
+ *
+ * NUMA-0      0               1               2               3
+ *
+ *
+ * As can be seen; things don't nicely line up as with the regular topology.
+ * When we iterate a domain in child domain chunks some nodes can be
+ * represented multiple times -- hence the "overlap" naming for this part of
+ * the topology.
+ *
+ * In order to minimize this overlap, we only build enough groups to cover the
+ * domain. For instance Node-0 NUMA-2 would only get groups: 0-1,3 and 1-3.
+ *
+ * Because:
+ *
+ *  - the first group of each domain is its child domain; this
+ *    gets us the first 0-1,3
+ *  - the only uncovered node is 2, who's child domain is 1-3.
+ *
+ * However, because of the overlap, computing a unique CPU for each group is
+ * more complicated. Consider for instance the groups of NODE-1 NUMA-2, both
+ * groups include the CPUs of Node-0, while those CPUs would not in fact ever
+ * end up at those groups (they would end up in group: 0-1,3).
+ *
+ * To correct this we have to introduce the group balance mask. This mask
+ * will contain those CPUs in the group that can reach this group given the
+ * (child) domain tree.
+ *
+ * With this we can once again compute balance_cpu and sched_group_capacity
+ * relations.
+ *
+ * XXX include words on how balance_cpu is unique and therefore can be
+ * used for sched_group_capacity links.
+ *
+ *
+ * Another 'interesting' topology is:
+ *
+ *   node   0   1   2   3
+ *     0:  10  20  20  30
+ *     1:  20  10  20  20
+ *     2:  20  20  10  20
+ *     3:  30  20  20  10
+ *
+ * Which looks a little like:
+ *
+ *   0 ----- 1
+ *   |     / |
+ *   |   /   |
+ *   | /     |
+ *   2 ----- 3
+ *
+ * This topology is asymmetric, nodes 1,2 are fully connected, but nodes 0,3
+ * are not.
+ *
+ * This leads to a few particularly weird cases where the sched_domain's are
+ * not of the same number for each cpu. Consider:
+ *
+ * NUMA-2      0-3                                             0-3
+ *  groups:    {0-2},{1-3}                                     {1-3},{0-2}
+ *
+ * NUMA-1      0-2             0-3             0-3             1-3
+ *
+ * NUMA-0      0               1               2               3
+ *
+ */
+
+
+/*
+ * Build the balance mask; it contains only those CPUs that can arrive at this
+ * group and should be considered to continue balancing.
+ *
+ * We do this during the group creation pass, therefore the group information
+ * isn't complete yet, however since each group represents a (child) domain we
+ * can fully construct this using the sched_domain bits (which are already
+ * complete).
+ */
+static void
+build_balance_mask(struct sched_domain *sd, struct sched_group *sg, struct cpumask *mask)
+{
+       const struct cpumask *sg_span = sched_group_span(sg);
        struct sd_data *sdd = sd->private;
        struct sched_domain *sibling;
        int i;
 
-       for_each_cpu(i, span) {
+       cpumask_clear(mask);
+
+       for_each_cpu(i, sg_span) {
                sibling = *per_cpu_ptr(sdd->sd, i);
-               if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
+
+               /*
+                * Can happen in the asymmetric case, where these siblings are
+                * unused. The mask will not be empty because those CPUs that
+                * do have the top domain _should_ span the domain.
+                */
+               if (!sibling->child)
                        continue;
 
-               cpumask_set_cpu(i, sched_group_mask(sg));
+               /* If we would not end up here, we can't continue from here */
+               if (!cpumask_equal(sg_span, sched_domain_span(sibling->child)))
+                       continue;
+
+               cpumask_set_cpu(i, mask);
        }
+
+       /* We must not have empty masks here */
+       WARN_ON_ONCE(cpumask_empty(mask));
 }
 
 /*
- * Return the canonical balance CPU for this group, this is the first CPU
- * of this group that's also in the iteration mask.
+ * XXX: This creates per-node group entries; since the load-balancer will
+ * immediately access remote memory to construct this group's load-balance
+ * statistics having the groups node local is of dubious benefit.
  */
-int group_balance_cpu(struct sched_group *sg)
+static struct sched_group *
+build_group_from_child_sched_domain(struct sched_domain *sd, int cpu)
 {
-       return cpumask_first_and(sched_group_cpus(sg), sched_group_mask(sg));
+       struct sched_group *sg;
+       struct cpumask *sg_span;
+
+       sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
+                       GFP_KERNEL, cpu_to_node(cpu));
+
+       if (!sg)
+               return NULL;
+
+       sg_span = sched_group_span(sg);
+       if (sd->child)
+               cpumask_copy(sg_span, sched_domain_span(sd->child));
+       else
+               cpumask_copy(sg_span, sched_domain_span(sd));
+
+       return sg;
+}
+
+static void init_overlap_sched_group(struct sched_domain *sd,
+                                    struct sched_group *sg)
+{
+       struct cpumask *mask = sched_domains_tmpmask2;
+       struct sd_data *sdd = sd->private;
+       struct cpumask *sg_span;
+       int cpu;
+
+       build_balance_mask(sd, sg, mask);
+       cpu = cpumask_first_and(sched_group_span(sg), mask);
+
+       sg->sgc = *per_cpu_ptr(sdd->sgc, cpu);
+       if (atomic_inc_return(&sg->sgc->ref) == 1)
+               cpumask_copy(group_balance_mask(sg), mask);
+       else
+               WARN_ON_ONCE(!cpumask_equal(group_balance_mask(sg), mask));
+
+       /*
+        * Initialize sgc->capacity such that even if we mess up the
+        * domains and no possible iteration will get us here, we won't
+        * die on a /0 trap.
+        */
+       sg_span = sched_group_span(sg);
+       sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sg_span);
+       sg->sgc->min_capacity = SCHED_CAPACITY_SCALE;
 }
 
 static int
 build_overlap_sched_groups(struct sched_domain *sd, int cpu)
 {
-       struct sched_group *first = NULL, *last = NULL, *groups = NULL, *sg;
+       struct sched_group *first = NULL, *last = NULL, *sg;
        const struct cpumask *span = sched_domain_span(sd);
        struct cpumask *covered = sched_domains_tmpmask;
        struct sd_data *sdd = sd->private;
@@ -525,7 +712,7 @@ build_overlap_sched_groups(struct sched_domain *sd, int cpu)
 
        cpumask_clear(covered);
 
-       for_each_cpu(i, span) {
+       for_each_cpu_wrap(i, span, cpu) {
                struct cpumask *sg_span;
 
                if (cpumask_test_cpu(i, covered))
@@ -533,44 +720,27 @@ build_overlap_sched_groups(struct sched_domain *sd, int cpu)
 
                sibling = *per_cpu_ptr(sdd->sd, i);
 
-               /* See the comment near build_group_mask(). */
+               /*
+                * Asymmetric node setups can result in situations where the
+                * domain tree is of unequal depth, make sure to skip domains
+                * that already cover the entire range.
+                *
+                * In that case build_sched_domains() will have terminated the
+                * iteration early and our sibling sd spans will be empty.
+                * Domains should always include the CPU they're built on, so
+                * check that.
+                */
                if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
                        continue;
 
-               sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
-                               GFP_KERNEL, cpu_to_node(cpu));
-
+               sg = build_group_from_child_sched_domain(sibling, cpu);
                if (!sg)
                        goto fail;
 
-               sg_span = sched_group_cpus(sg);
-               if (sibling->child)
-                       cpumask_copy(sg_span, sched_domain_span(sibling->child));
-               else
-                       cpumask_set_cpu(i, sg_span);
-
+               sg_span = sched_group_span(sg);
                cpumask_or(covered, covered, sg_span);
 
-               sg->sgc = *per_cpu_ptr(sdd->sgc, i);
-               if (atomic_inc_return(&sg->sgc->ref) == 1)
-                       build_group_mask(sd, sg);
-
-               /*
-                * Initialize sgc->capacity such that even if we mess up the
-                * domains and no possible iteration will get us here, we won't
-                * die on a /0 trap.
-                */
-               sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sg_span);
-               sg->sgc->min_capacity = SCHED_CAPACITY_SCALE;
-
-               /*
-                * Make sure the first group of this domain contains the
-                * canonical balance CPU. Otherwise the sched_domain iteration
-                * breaks. See update_sg_lb_stats().
-                */
-               if ((!groups && cpumask_test_cpu(cpu, sg_span)) ||
-                   group_balance_cpu(sg) == cpu)
-                       groups = sg;
+               init_overlap_sched_group(sd, sg);
 
                if (!first)
                        first = sg;
@@ -579,7 +749,7 @@ build_overlap_sched_groups(struct sched_domain *sd, int cpu)
                last = sg;
                last->next = first;
        }
-       sd->groups = groups;
+       sd->groups = first;
 
        return 0;
 
@@ -589,23 +759,106 @@ fail:
        return -ENOMEM;
 }
 
-static int get_group(int cpu, struct sd_data *sdd, struct sched_group **sg)
+
+/*
+ * Package topology (also see the load-balance blurb in fair.c)
+ *
+ * The scheduler builds a tree structure to represent a number of important
+ * topology features. By default (default_topology[]) these include:
+ *
+ *  - Simultaneous multithreading (SMT)
+ *  - Multi-Core Cache (MC)
+ *  - Package (DIE)
+ *
+ * Where the last one more or less denotes everything up to a NUMA node.
+ *
+ * The tree consists of 3 primary data structures:
+ *
+ *     sched_domain -> sched_group -> sched_group_capacity
+ *         ^ ^             ^ ^
+ *          `-'             `-'
+ *
+ * The sched_domains are per-cpu and have a two way link (parent & child) and
+ * denote the ever growing mask of CPUs belonging to that level of topology.
+ *
+ * Each sched_domain has a circular (double) linked list of sched_group's, each
+ * denoting the domains of the level below (or individual CPUs in case of the
+ * first domain level). The sched_group linked by a sched_domain includes the
+ * CPU of that sched_domain [*].
+ *
+ * Take for instance a 2 threaded, 2 core, 2 cache cluster part:
+ *
+ * CPU   0   1   2   3   4   5   6   7
+ *
+ * DIE  [                             ]
+ * MC   [             ] [             ]
+ * SMT  [     ] [     ] [     ] [     ]
+ *
+ *  - or -
+ *
+ * DIE  0-7 0-7 0-7 0-7 0-7 0-7 0-7 0-7
+ * MC  0-3 0-3 0-3 0-3 4-7 4-7 4-7 4-7
+ * SMT  0-1 0-1 2-3 2-3 4-5 4-5 6-7 6-7
+ *
+ * CPU   0   1   2   3   4   5   6   7
+ *
+ * One way to think about it is: sched_domain moves you up and down among these
+ * topology levels, while sched_group moves you sideways through it, at child
+ * domain granularity.
+ *
+ * sched_group_capacity ensures each unique sched_group has shared storage.
+ *
+ * There are two related construction problems, both require a CPU that
+ * uniquely identify each group (for a given domain):
+ *
+ *  - The first is the balance_cpu (see should_we_balance() and the
+ *    load-balance blub in fair.c); for each group we only want 1 CPU to
+ *    continue balancing at a higher domain.
+ *
+ *  - The second is the sched_group_capacity; we want all identical groups
+ *    to share a single sched_group_capacity.
+ *
+ * Since these topologies are exclusive by construction. That is, its
+ * impossible for an SMT thread to belong to multiple cores, and cores to
+ * be part of multiple caches. There is a very clear and unique location
+ * for each CPU in the hierarchy.
+ *
+ * Therefore computing a unique CPU for each group is trivial (the iteration
+ * mask is redundant and set all 1s; all CPUs in a group will end up at _that_
+ * group), we can simply pick the first CPU in each group.
+ *
+ *
+ * [*] in other words, the first group of each domain is its child domain.
+ */
+
+static struct sched_group *get_group(int cpu, struct sd_data *sdd)
 {
        struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
        struct sched_domain *child = sd->child;
+       struct sched_group *sg;
 
        if (child)
                cpu = cpumask_first(sched_domain_span(child));
 
-       if (sg) {
-               *sg = *per_cpu_ptr(sdd->sg, cpu);
-               (*sg)->sgc = *per_cpu_ptr(sdd->sgc, cpu);
+       sg = *per_cpu_ptr(sdd->sg, cpu);
+       sg->sgc = *per_cpu_ptr(sdd->sgc, cpu);
+
+       /* For claim_allocations: */
+       atomic_inc(&sg->ref);
+       atomic_inc(&sg->sgc->ref);
 
-               /* For claim_allocations: */
-               atomic_set(&(*sg)->sgc->ref, 1);
+       if (child) {
+               cpumask_copy(sched_group_span(sg), sched_domain_span(child));
+               cpumask_copy(group_balance_mask(sg), sched_group_span(sg));
+       } else {
+               cpumask_set_cpu(cpu, sched_group_span(sg));
+               cpumask_set_cpu(cpu, group_balance_mask(sg));
        }
 
-       return cpu;
+       sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sched_group_span(sg));
+       sg->sgc->min_capacity = SCHED_CAPACITY_SCALE;
+
+       return sg;
 }
 
 /*
@@ -624,34 +877,20 @@ build_sched_groups(struct sched_domain *sd, int cpu)
        struct cpumask *covered;
        int i;
 
-       get_group(cpu, sdd, &sd->groups);
-       atomic_inc(&sd->groups->ref);
-
-       if (cpu != cpumask_first(span))
-               return 0;
-
        lockdep_assert_held(&sched_domains_mutex);
        covered = sched_domains_tmpmask;
 
        cpumask_clear(covered);
 
-       for_each_cpu(i, span) {
+       for_each_cpu_wrap(i, span, cpu) {
                struct sched_group *sg;
-               int group, j;
 
                if (cpumask_test_cpu(i, covered))
                        continue;
 
-               group = get_group(i, sdd, &sg);
-               cpumask_setall(sched_group_mask(sg));
+               sg = get_group(i, sdd);
 
-               for_each_cpu(j, span) {
-                       if (get_group(j, sdd, NULL) != group)
-                               continue;
-
-                       cpumask_set_cpu(j, covered);
-                       cpumask_set_cpu(j, sched_group_cpus(sg));
-               }
+               cpumask_or(covered, covered, sched_group_span(sg));
 
                if (!first)
                        first = sg;
@@ -660,6 +899,7 @@ build_sched_groups(struct sched_domain *sd, int cpu)
                last = sg;
        }
        last->next = first;
+       sd->groups = first;
 
        return 0;
 }
@@ -683,12 +923,12 @@ static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
        do {
                int cpu, max_cpu = -1;
 
-               sg->group_weight = cpumask_weight(sched_group_cpus(sg));
+               sg->group_weight = cpumask_weight(sched_group_span(sg));
 
                if (!(sd->flags & SD_ASYM_PACKING))
                        goto next;
 
-               for_each_cpu(cpu, sched_group_cpus(sg)) {
+               for_each_cpu(cpu, sched_group_span(sg)) {
                        if (max_cpu < 0)
                                max_cpu = cpu;
                        else if (sched_asym_prefer(cpu, max_cpu))
@@ -1308,6 +1548,10 @@ static int __sdt_alloc(const struct cpumask *cpu_map)
                        if (!sgc)
                                return -ENOMEM;
 
+#ifdef CONFIG_SCHED_DEBUG
+                       sgc->id = j;
+#endif
+
                        *per_cpu_ptr(sdd->sgc, j) = sgc;
                }
        }
@@ -1407,7 +1651,7 @@ build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *att
                        sd = build_sched_domain(tl, cpu_map, attr, sd, i);
                        if (tl == sched_domain_topology)
                                *per_cpu_ptr(d.sd, i) = sd;
-                       if (tl->flags & SDTL_OVERLAP || sched_feat(FORCE_SD_OVERLAP))
+                       if (tl->flags & SDTL_OVERLAP)
                                sd->flags |= SD_OVERLAP;
                        if (cpumask_equal(cpu_map, sched_domain_span(sd)))
                                break;
@@ -1478,7 +1722,7 @@ static struct sched_domain_attr           *dattr_cur;
  * cpumask) fails, then fallback to a single sched domain,
  * as determined by the single cpumask fallback_doms.
  */
-cpumask_var_t                          fallback_doms;
+static cpumask_var_t                   fallback_doms;
 
 /*
  * arch_update_cpu_topology lets virtualized architectures update the
@@ -1520,10 +1764,14 @@ void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
  * For now this just excludes isolated CPUs, but could be used to
  * exclude other special cases in the future.
  */
-int init_sched_domains(const struct cpumask *cpu_map)
+int sched_init_domains(const struct cpumask *cpu_map)
 {
        int err;
 
+       zalloc_cpumask_var(&sched_domains_tmpmask, GFP_KERNEL);
+       zalloc_cpumask_var(&sched_domains_tmpmask2, GFP_KERNEL);
+       zalloc_cpumask_var(&fallback_doms, GFP_KERNEL);
+
        arch_update_cpu_topology();
        ndoms_cur = 1;
        doms_cur = alloc_sched_domains(ndoms_cur);
index 93621ae718d391ac6a95cd561dfc22bd6dc15a18..03918a19cf2da854bcefa9f8188daa53a7db82f4 100644 (file)
@@ -233,6 +233,9 @@ static void clocksource_watchdog(unsigned long data)
                        continue;
                }
 
+               if (cs == curr_clocksource && cs->tick_stable)
+                       cs->tick_stable(cs);
+
                if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
                    (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
                    (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
index 64c97fc130c4db2f1241185d9075af7a0f5175f5..9c2dc64e31d85b5161fd7080448faeb0da430755 100644 (file)
@@ -554,7 +554,7 @@ static void tick_nohz_stop_idle(struct tick_sched *ts, ktime_t now)
        update_ts_time_stats(smp_processor_id(), ts, now, NULL);
        ts->idle_active = 0;
 
-       sched_clock_idle_wakeup_event(0);
+       sched_clock_idle_wakeup_event();
 }
 
 static ktime_t tick_nohz_start_idle(struct tick_sched *ts)
index 81dedaab36ccfed3150281a0f915a4070f667bfd..4731a0895760e70cab07d59a0cff45c2590da450 100644 (file)
@@ -43,6 +43,38 @@ int cpumask_any_but(const struct cpumask *mask, unsigned int cpu)
 }
 EXPORT_SYMBOL(cpumask_any_but);
 
+/**
+ * cpumask_next_wrap - helper to implement for_each_cpu_wrap
+ * @n: the cpu prior to the place to search
+ * @mask: the cpumask pointer
+ * @start: the start point of the iteration
+ * @wrap: assume @n crossing @start terminates the iteration
+ *
+ * Returns >= nr_cpu_ids on completion
+ *
+ * Note: the @wrap argument is required for the start condition when
+ * we cannot assume @start is set in @mask.
+ */
+int cpumask_next_wrap(int n, const struct cpumask *mask, int start, bool wrap)
+{
+       int next;
+
+again:
+       next = cpumask_next(n, mask);
+
+       if (wrap && n < start && next >= start) {
+               return nr_cpumask_bits;
+
+       } else if (next >= nr_cpumask_bits) {
+               wrap = true;
+               n = -1;
+               goto again;
+       }
+
+       return next;
+}
+EXPORT_SYMBOL(cpumask_next_wrap);
+
 /* These are not inline because of header tangles. */
 #ifdef CONFIG_CPUMASK_OFFSTACK
 /**