Debian Codegraph @

linux-kernel-patches/12-bb8c13d61a629276a162c1d2b1a20a815cbcfbb7.patch

raw
1 From bb8c13d61a629276a162c1d2b1a20a815cbcfbb7 Mon Sep 17 00:00:00 2001
2 From: Borislav Petkov <[email protected]>
3 Date: Wed, 14 Mar 2018 19:36:15 +0100
4 Subject: x86/microcode: Fix CPU synchronization routine
5
6 Emanuel reported an issue with a hang during microcode update because my
7 dumb idea to use one atomic synchronization variable for both rendezvous
8 - before and after update - was simply bollocks:
9
10 microcode: microcode_reload_late: late_cpus: 4
11 microcode: __reload_late: cpu 2 entered
12 microcode: __reload_late: cpu 1 entered
13 microcode: __reload_late: cpu 3 entered
14 microcode: __reload_late: cpu 0 entered
15 microcode: __reload_late: cpu 1 left
16 microcode: Timeout while waiting for CPUs rendezvous, remaining: 1
17
18 CPU1 above would finish, leave and the others will still spin waiting for
19 it to join.
20
21 So do two synchronization atomics instead, which makes the code a lot more
22 straightforward.
23
24 Also, since the update is serialized and it also takes quite some time per
25 microcode engine, increase the exit timeout by the number of CPUs on the
26 system.
27
28 That's ok because the moment all CPUs are done, that timeout will be cut
29 short.
30
31 Furthermore, panic when some of the CPUs timeout when returning from a
32 microcode update: we can't allow a system with not all cores updated.
33
34 Also, as an optimization, do not do the exit sync if microcode wasn't
35 updated.
36
37 Reported-by: Emanuel Czirai <[email protected]>
38 Signed-off-by: Borislav Petkov <[email protected]>
39 Signed-off-by: Thomas Gleixner <[email protected]>
40 Tested-by: Emanuel Czirai <[email protected]>
41 Tested-by: Ashok Raj <[email protected]>
42 Tested-by: Tom Lendacky <[email protected]>
43 Link: https://lkml.kernel.org/r/[email protected]
44 ---
45 arch/x86/kernel/cpu/microcode/core.c | 68 ++++++++++++++++++++++--------------
46 1 file changed, 41 insertions(+), 27 deletions(-)
47
48 diff --git a/arch/x86/kernel/cpu/microcode/core.c b/arch/x86/kernel/cpu/microcode/core.c
49 index 9f0fe5b..10c4fc2 100644
50 --- a/arch/x86/kernel/cpu/microcode/core.c
51 +++ b/arch/x86/kernel/cpu/microcode/core.c
52 @@ -517,7 +517,29 @@ static int check_online_cpus(void)
53 return -EINVAL;
54 }
55
56 -static atomic_t late_cpus;
57 +static atomic_t late_cpus_in;
58 +static atomic_t late_cpus_out;
59 +
60 +static int __wait_for_cpus(atomic_t *t, long long timeout)
61 +{
62 + int all_cpus = num_online_cpus();
63 +
64 + atomic_inc(t);
65 +
66 + while (atomic_read(t) < all_cpus) {
67 + if (timeout < SPINUNIT) {
68 + pr_err("Timeout while waiting for CPUs rendezvous, remaining: %d\n",
69 + all_cpus - atomic_read(t));
70 + return 1;
71 + }
72 +
73 + ndelay(SPINUNIT);
74 + timeout -= SPINUNIT;
75 +
76 + touch_nmi_watchdog();
77 + }
78 + return 0;
79 +}
80
81 /*
82 * Returns:
83 @@ -527,30 +549,16 @@ static atomic_t late_cpus;
84 */
85 static int __reload_late(void *info)
86 {
87 - unsigned int timeout = NSEC_PER_SEC;
88 - int all_cpus = num_online_cpus();
89 int cpu = smp_processor_id();
90 enum ucode_state err;
91 int ret = 0;
92
93 - atomic_dec(&late_cpus);
94 -
95 /*
96 * Wait for all CPUs to arrive. A load will not be attempted unless all
97 * CPUs show up.
98 * */
99 - while (atomic_read(&late_cpus)) {
100 - if (timeout < SPINUNIT) {
101 - pr_err("Timeout while waiting for CPUs rendezvous, remaining: %d\n",
102 - atomic_read(&late_cpus));
103 - return -1;
104 - }
105 -
106 - ndelay(SPINUNIT);
107 - timeout -= SPINUNIT;
108 -
109 - touch_nmi_watchdog();
110 - }
111 + if (__wait_for_cpus(&late_cpus_in, NSEC_PER_SEC))
112 + return -1;
113
114 spin_lock(&update_lock);
115 apply_microcode_local(&err);
116 @@ -558,15 +566,22 @@ static int __reload_late(void *info)
117
118 if (err > UCODE_NFOUND) {
119 pr_warn("Error reloading microcode on CPU %d\n", cpu);
120 - ret = -1;
121 - } else if (err == UCODE_UPDATED) {
122 + return -1;
123 + /* siblings return UCODE_OK because their engine got updated already */
124 + } else if (err == UCODE_UPDATED || err == UCODE_OK) {
125 ret = 1;
126 + } else {
127 + return ret;
128 }
129
130 - atomic_inc(&late_cpus);
131 -
132 - while (atomic_read(&late_cpus) != all_cpus)
133 - cpu_relax();
134 + /*
135 + * Increase the wait timeout to a safe value here since we're
136 + * serializing the microcode update and that could take a while on a
137 + * large number of CPUs. And that is fine as the *actual* timeout will
138 + * be determined by the last CPU finished updating and thus cut short.
139 + */
140 + if (__wait_for_cpus(&late_cpus_out, NSEC_PER_SEC * num_online_cpus()))
141 + panic("Timeout during microcode update!\n");
142
143 return ret;
144 }
145 @@ -579,12 +594,11 @@ static int microcode_reload_late(void)
146 {
147 int ret;
148
149 - atomic_set(&late_cpus, num_online_cpus());
150 + atomic_set(&late_cpus_in, 0);
151 + atomic_set(&late_cpus_out, 0);
152
153 ret = stop_machine_cpuslocked(__reload_late, NULL, cpu_online_mask);
154 - if (ret < 0)
155 - return ret;
156 - else if (ret > 0)
157 + if (ret > 0)
158 microcode_check();
159
160 return ret;
161 --
162 cgit v1.1
163