@@ -252,6 +252,11 @@ static struct platform_device *microcode_pdev;
252
252
* requirement can be relaxed in the future. Right now, this is conservative
253
253
* and good.
254
254
*/
255
+ struct microcode_ctrl {
256
+ enum ucode_state result ;
257
+ };
258
+
259
+ static DEFINE_PER_CPU (struct microcode_ctrl , ucode_ctrl ) ;
255
260
static atomic_t late_cpus_in , late_cpus_out ;
256
261
257
262
static bool wait_for_cpus (atomic_t * cnt )
@@ -274,23 +279,19 @@ static bool wait_for_cpus(atomic_t *cnt)
274
279
return false;
275
280
}
276
281
277
- /*
278
- * Returns:
279
- * < 0 - on error
280
- * 0 - success (no update done or microcode was updated)
281
- */
282
- static int __reload_late (void * info )
282
+ static int load_cpus_stopped (void * unused )
283
283
{
284
284
int cpu = smp_processor_id ();
285
- enum ucode_state err ;
286
- int ret = 0 ;
285
+ enum ucode_state ret ;
287
286
288
287
/*
289
288
* Wait for all CPUs to arrive. A load will not be attempted unless all
290
289
* CPUs show up.
291
290
* */
292
- if (!wait_for_cpus (& late_cpus_in ))
293
- return -1 ;
291
+ if (!wait_for_cpus (& late_cpus_in )) {
292
+ this_cpu_write (ucode_ctrl .result , UCODE_TIMEOUT );
293
+ return 0 ;
294
+ }
294
295
295
296
/*
296
297
* On an SMT system, it suffices to load the microcode on one sibling of
@@ -299,17 +300,11 @@ static int __reload_late(void *info)
299
300
* loading attempts happen on multiple threads of an SMT core. See
300
301
* below.
301
302
*/
302
- if (cpumask_first (topology_sibling_cpumask (cpu )) == cpu )
303
- err = microcode_ops -> apply_microcode (cpu );
304
- else
303
+ if (cpumask_first (topology_sibling_cpumask (cpu )) != cpu )
305
304
goto wait_for_siblings ;
306
305
307
- if (err >= UCODE_NFOUND ) {
308
- if (err == UCODE_ERROR ) {
309
- pr_warn ("Error reloading microcode on CPU %d\n" , cpu );
310
- ret = -1 ;
311
- }
312
- }
306
+ ret = microcode_ops -> apply_microcode (cpu );
307
+ this_cpu_write (ucode_ctrl .result , ret );
313
308
314
309
wait_for_siblings :
315
310
if (!wait_for_cpus (& late_cpus_out ))
@@ -321,19 +316,18 @@ static int __reload_late(void *info)
321
316
* per-cpu cpuinfo can be updated with right microcode
322
317
* revision.
323
318
*/
324
- if (cpumask_first (topology_sibling_cpumask (cpu )) ! = cpu )
325
- err = microcode_ops -> apply_microcode ( cpu ) ;
319
+ if (cpumask_first (topology_sibling_cpumask (cpu )) = = cpu )
320
+ return 0 ;
326
321
327
- return ret ;
322
+ ret = microcode_ops -> apply_microcode (cpu );
323
+ this_cpu_write (ucode_ctrl .result , ret );
324
+ return 0 ;
328
325
}
329
326
330
- /*
331
- * Reload microcode late on all CPUs. Wait for a sec until they
332
- * all gather together.
333
- */
334
- static int microcode_reload_late (void )
327
+ static int load_late_stop_cpus (void )
335
328
{
336
- int old = boot_cpu_data .microcode , ret ;
329
+ unsigned int cpu , updated = 0 , failed = 0 , timedout = 0 , siblings = 0 ;
330
+ int old_rev = boot_cpu_data .microcode ;
337
331
struct cpuinfo_x86 prev_info ;
338
332
339
333
pr_err ("Attempting late microcode loading - it is dangerous and taints the kernel.\n" );
@@ -348,26 +342,47 @@ static int microcode_reload_late(void)
348
342
*/
349
343
store_cpu_caps (& prev_info );
350
344
351
- ret = stop_machine_cpuslocked (__reload_late , NULL , cpu_online_mask );
345
+ stop_machine_cpuslocked (load_cpus_stopped , NULL , cpu_online_mask );
346
+
347
+ /* Analyze the results */
348
+ for_each_cpu_and (cpu , cpu_present_mask , & cpus_booted_once_mask ) {
349
+ switch (per_cpu (ucode_ctrl .result , cpu )) {
350
+ case UCODE_UPDATED : updated ++ ; break ;
351
+ case UCODE_TIMEOUT : timedout ++ ; break ;
352
+ case UCODE_OK : siblings ++ ; break ;
353
+ default : failed ++ ; break ;
354
+ }
355
+ }
352
356
353
357
if (microcode_ops -> finalize_late_load )
354
- microcode_ops -> finalize_late_load (ret );
355
-
356
- if (!ret ) {
357
- pr_info ("Reload succeeded, microcode revision: 0x%x -> 0x%x\n" ,
358
- old , boot_cpu_data .microcode );
359
- microcode_check (& prev_info );
360
- add_taint (TAINT_CPU_OUT_OF_SPEC , LOCKDEP_STILL_OK );
361
- } else {
362
- pr_info ("Reload failed, current microcode revision: 0x%x\n" ,
363
- boot_cpu_data .microcode );
358
+ microcode_ops -> finalize_late_load (!updated );
359
+
360
+ if (!updated ) {
361
+ /* Nothing changed. */
362
+ if (!failed && !timedout )
363
+ return 0 ;
364
+ pr_err ("update failed: %u CPUs failed %u CPUs timed out\n" ,
365
+ failed , timedout );
366
+ return - EIO ;
367
+ }
368
+
369
+ add_taint (TAINT_CPU_OUT_OF_SPEC , LOCKDEP_STILL_OK );
370
+ pr_info ("load: updated on %u primary CPUs with %u siblings\n" , updated , siblings );
371
+ if (failed || timedout ) {
372
+ pr_err ("load incomplete. %u CPUs timed out or failed\n" ,
373
+ num_online_cpus () - (updated + siblings ));
364
374
}
365
- return ret ;
375
+ pr_info ("revision: 0x%x -> 0x%x\n" , old_rev , boot_cpu_data .microcode );
376
+ microcode_check (& prev_info );
377
+
378
+ return updated + siblings == num_online_cpus () ? 0 : - EIO ;
366
379
}
367
380
368
381
/*
369
- * Ensure that all required CPUs which are present and have been booted
370
- * once are online.
382
+ * This function does two things:
383
+ *
384
+ * 1) Ensure that all required CPUs which are present and have been booted
385
+ * once are online.
371
386
*
372
387
* To pass this check, all primary threads must be online.
373
388
*
@@ -378,9 +393,12 @@ static int microcode_reload_late(void)
378
393
* behaviour is undefined. The default play_dead() implementation on
379
394
* modern CPUs uses MWAIT, which is also not guaranteed to be safe
380
395
* against a microcode update which affects MWAIT.
396
+ *
397
+ * 2) Initialize the per CPU control structure
381
398
*/
382
- static bool ensure_cpus_are_online (void )
399
+ static bool setup_cpus (void )
383
400
{
401
+ struct microcode_ctrl ctrl = { .result = -1 , };
384
402
unsigned int cpu ;
385
403
386
404
for_each_cpu_and (cpu , cpu_present_mask , & cpus_booted_once_mask ) {
@@ -390,18 +408,20 @@ static bool ensure_cpus_are_online(void)
390
408
return false;
391
409
}
392
410
}
411
+ /* Initialize the per CPU state */
412
+ per_cpu (ucode_ctrl , cpu ) = ctrl ;
393
413
}
394
414
return true;
395
415
}
396
416
397
- static int ucode_load_late_locked (void )
417
+ static int load_late_locked (void )
398
418
{
399
- if (!ensure_cpus_are_online ())
419
+ if (!setup_cpus ())
400
420
return - EBUSY ;
401
421
402
422
switch (microcode_ops -> request_microcode_fw (0 , & microcode_pdev -> dev )) {
403
423
case UCODE_NEW :
404
- return microcode_reload_late ();
424
+ return load_late_stop_cpus ();
405
425
case UCODE_NFOUND :
406
426
return - ENOENT ;
407
427
default :
@@ -421,7 +441,7 @@ static ssize_t reload_store(struct device *dev,
421
441
return - EINVAL ;
422
442
423
443
cpus_read_lock ();
424
- ret = ucode_load_late_locked ();
444
+ ret = load_late_locked ();
425
445
cpus_read_unlock ();
426
446
427
447
return ret ? : size ;
0 commit comments