Skip to content

Commit 816afe4

Browse files
rustyrussellingomolnar
authored andcommitted
x86/smp: Don't ever patch back to UP if we unplug cpus
We still patch SMP instructions to UP variants if we boot with a single CPU, but not at any other time. In particular, not if we unplug CPUs to return to a single cpu. Paul McKenney points out: mean offline overhead is 6251/48=130.2 milliseconds. If I remove the alternatives_smp_switch() from the offline path [...] the mean offline overhead is 550/42=13.1 milliseconds Basically, we're never going to get those 120ms back, and the code is pretty messy. We get rid of: 1) The "smp-alt-once" boot option. It's actually "smp-alt-boot", the documentation is wrong. It's now the default. 2) The skip_smp_alternatives flag used by suspend. 3) arch_disable_nonboot_cpus_begin() and arch_disable_nonboot_cpus_end() which were only used to set this one flag. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> Cc: Paul McKenney <paul.mckenney@us.ibm.com> Cc: Suresh Siddha <suresh.b.siddha@intel.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Link: http://lkml.kernel.org/r/87vcgwwive.fsf@rustcorp.com.au Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 23dcfa6 commit 816afe4

6 files changed

Lines changed: 32 additions & 119 deletions

File tree

‎Documentation/kernel-parameters.txt‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2638,9 +2638,6 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
26382638
smart2= [HW]
26392639
Format: <io1>[,<io2>[,...,<io8>]]
26402640

2641-
smp-alt-once [X86-32,SMP] On a hotplug CPU system, only
2642-
attempt to substitute SMP alternatives once at boot.
2643-
26442641
smsc-ircc2.nopnp [HW] Don't use PNP to discover SMC devices
26452642
smsc-ircc2.ircc_cfg= [HW] Device configuration I/O port
26462643
smsc-ircc2.ircc_sir= [HW] SIR base I/O port

‎arch/x86/include/asm/alternative.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ extern void alternatives_smp_module_add(struct module *mod, char *name,
6060
void *locks, void *locks_end,
6161
void *text, void *text_end);
6262
extern void alternatives_smp_module_del(struct module *mod);
63-
extern void alternatives_smp_switch(int smp);
63+
extern void alternatives_enable_smp(void);
6464
extern int alternatives_text_reserved(void *start, void *end);
6565
extern bool skip_smp_alternatives;
6666
#else
6767
static inline void alternatives_smp_module_add(struct module *mod, char *name,
6868
void *locks, void *locks_end,
6969
void *text, void *text_end) {}
7070
static inline void alternatives_smp_module_del(struct module *mod) {}
71-
static inline void alternatives_smp_switch(int smp) {}
71+
static inline void alternatives_enable_smp(void) {}
7272
static inline int alternatives_text_reserved(void *start, void *end)
7373
{
7474
return 0;

‎arch/x86/kernel/alternative.c‎

Lines changed: 26 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,6 @@
2323

2424
#define MAX_PATCH_LEN (255-1)
2525

26-
#ifdef CONFIG_HOTPLUG_CPU
27-
static int smp_alt_once;
28-
29-
static int __init bootonly(char *str)
30-
{
31-
smp_alt_once = 1;
32-
return 1;
33-
}
34-
__setup("smp-alt-boot", bootonly);
35-
#else
36-
#define smp_alt_once 1
37-
#endif
38-
3926
static int __initdata_or_module debug_alternative;
4027

4128
static int __init debug_alt(char *str)
@@ -326,9 +313,6 @@ static void alternatives_smp_unlock(const s32 *start, const s32 *end,
326313
{
327314
const s32 *poff;
328315

329-
if (noreplace_smp)
330-
return;
331-
332316
mutex_lock(&text_mutex);
333317
for (poff = start; poff < end; poff++) {
334318
u8 *ptr = (u8 *)poff + *poff;
@@ -359,7 +343,7 @@ struct smp_alt_module {
359343
};
360344
static LIST_HEAD(smp_alt_modules);
361345
static DEFINE_MUTEX(smp_alt);
362-
static int smp_mode = 1; /* protected by smp_alt */
346+
static bool uniproc_patched = false; /* protected by smp_alt */
363347

364348
void __init_or_module alternatives_smp_module_add(struct module *mod,
365349
char *name,
@@ -368,19 +352,18 @@ void __init_or_module alternatives_smp_module_add(struct module *mod,
368352
{
369353
struct smp_alt_module *smp;
370354

371-
if (noreplace_smp)
372-
return;
355+
mutex_lock(&smp_alt);
356+
if (!uniproc_patched)
357+
goto unlock;
373358

374-
if (smp_alt_once) {
375-
if (boot_cpu_has(X86_FEATURE_UP))
376-
alternatives_smp_unlock(locks, locks_end,
377-
text, text_end);
378-
return;
379-
}
359+
if (num_possible_cpus() == 1)
360+
/* Don't bother remembering, we'll never have to undo it. */
361+
goto smp_unlock;
380362

381363
smp = kzalloc(sizeof(*smp), GFP_KERNEL);
382364
if (NULL == smp)
383-
return; /* we'll run the (safe but slow) SMP code then ... */
365+
/* we'll run the (safe but slow) SMP code then ... */
366+
goto unlock;
384367

385368
smp->mod = mod;
386369
smp->name = name;
@@ -392,36 +375,29 @@ void __init_or_module alternatives_smp_module_add(struct module *mod,
392375
__func__, smp->locks, smp->locks_end,
393376
smp->text, smp->text_end, smp->name);
394377

395-
mutex_lock(&smp_alt);
396378
list_add_tail(&smp->next, &smp_alt_modules);
397-
if (boot_cpu_has(X86_FEATURE_UP))
398-
alternatives_smp_unlock(smp->locks, smp->locks_end,
399-
smp->text, smp->text_end);
379+
smp_unlock:
380+
alternatives_smp_unlock(locks, locks_end, text, text_end);
381+
unlock:
400382
mutex_unlock(&smp_alt);
401383
}
402384

403385
void __init_or_module alternatives_smp_module_del(struct module *mod)
404386
{
405387
struct smp_alt_module *item;
406388

407-
if (smp_alt_once || noreplace_smp)
408-
return;
409-
410389
mutex_lock(&smp_alt);
411390
list_for_each_entry(item, &smp_alt_modules, next) {
412391
if (mod != item->mod)
413392
continue;
414393
list_del(&item->next);
415-
mutex_unlock(&smp_alt);
416-
DPRINTK("%s: %s\n", __func__, item->name);
417394
kfree(item);
418-
return;
395+
break;
419396
}
420397
mutex_unlock(&smp_alt);
421398
}
422399

423-
bool skip_smp_alternatives;
424-
void alternatives_smp_switch(int smp)
400+
void alternatives_enable_smp(void)
425401
{
426402
struct smp_alt_module *mod;
427403

@@ -436,34 +412,21 @@ void alternatives_smp_switch(int smp)
436412
pr_info("lockdep: fixing up alternatives\n");
437413
#endif
438414

439-
if (noreplace_smp || smp_alt_once || skip_smp_alternatives)
440-
return;
441-
BUG_ON(!smp && (num_online_cpus() > 1));
415+
/* Why bother if there are no other CPUs? */
416+
BUG_ON(num_possible_cpus() == 1);
442417

443418
mutex_lock(&smp_alt);
444419

445-
/*
446-
* Avoid unnecessary switches because it forces JIT based VMs to
447-
* throw away all cached translations, which can be quite costly.
448-
*/
449-
if (smp == smp_mode) {
450-
/* nothing */
451-
} else if (smp) {
420+
if (uniproc_patched) {
452421
pr_info("switching to SMP code\n");
422+
BUG_ON(num_online_cpus() != 1);
453423
clear_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
454424
clear_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
455425
list_for_each_entry(mod, &smp_alt_modules, next)
456426
alternatives_smp_lock(mod->locks, mod->locks_end,
457427
mod->text, mod->text_end);
458-
} else {
459-
pr_info("switching to UP code\n");
460-
set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
461-
set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
462-
list_for_each_entry(mod, &smp_alt_modules, next)
463-
alternatives_smp_unlock(mod->locks, mod->locks_end,
464-
mod->text, mod->text_end);
428+
uniproc_patched = false;
465429
}
466-
smp_mode = smp;
467430
mutex_unlock(&smp_alt);
468431
}
469432

@@ -540,40 +503,22 @@ void __init alternative_instructions(void)
540503

541504
apply_alternatives(__alt_instructions, __alt_instructions_end);
542505

543-
/* switch to patch-once-at-boottime-only mode and free the
544-
* tables in case we know the number of CPUs will never ever
545-
* change */
546-
#ifdef CONFIG_HOTPLUG_CPU
547-
if (num_possible_cpus() < 2)
548-
smp_alt_once = 1;
549-
#endif
550-
551506
#ifdef CONFIG_SMP
552-
if (smp_alt_once) {
553-
if (1 == num_possible_cpus()) {
554-
pr_info("switching to UP code\n");
555-
set_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
556-
set_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
557-
558-
alternatives_smp_unlock(__smp_locks, __smp_locks_end,
559-
_text, _etext);
560-
}
561-
} else {
507+
/* Patch to UP if other cpus not imminent. */
508+
if (!noreplace_smp && (num_present_cpus() == 1 || setup_max_cpus <= 1)) {
509+
uniproc_patched = true;
562510
alternatives_smp_module_add(NULL, "core kernel",
563511
__smp_locks, __smp_locks_end,
564512
_text, _etext);
565-
566-
/* Only switch to UP mode if we don't immediately boot others */
567-
if (num_present_cpus() == 1 || setup_max_cpus <= 1)
568-
alternatives_smp_switch(0);
569513
}
570-
#endif
571-
apply_paravirt(__parainstructions, __parainstructions_end);
572514

573-
if (smp_alt_once)
515+
if (!uniproc_patched || num_possible_cpus() == 1)
574516
free_init_pages("SMP alternatives",
575517
(unsigned long)__smp_locks,
576518
(unsigned long)__smp_locks_end);
519+
#endif
520+
521+
apply_paravirt(__parainstructions, __parainstructions_end);
577522

578523
restart_nmi();
579524
}

‎arch/x86/kernel/smpboot.c‎

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,8 @@ static int __cpuinit do_boot_cpu(int apicid, int cpu, struct task_struct *idle)
665665
unsigned long boot_error = 0;
666666
int timeout;
667667

668-
alternatives_smp_switch(1);
668+
/* Just in case we booted with a single CPU. */
669+
alternatives_enable_smp();
669670

670671
idle->thread.sp = (unsigned long) (((struct pt_regs *)
671672
(THREAD_SIZE + task_stack_page(idle))) - 1);
@@ -1053,20 +1054,6 @@ void __init native_smp_prepare_cpus(unsigned int max_cpus)
10531054
preempt_enable();
10541055
}
10551056

1056-
void arch_disable_nonboot_cpus_begin(void)
1057-
{
1058-
/*
1059-
* Avoid the smp alternatives switch during the disable_nonboot_cpus().
1060-
* In the suspend path, we will be back in the SMP mode shortly anyways.
1061-
*/
1062-
skip_smp_alternatives = true;
1063-
}
1064-
1065-
void arch_disable_nonboot_cpus_end(void)
1066-
{
1067-
skip_smp_alternatives = false;
1068-
}
1069-
10701057
void arch_enable_nonboot_cpus_begin(void)
10711058
{
10721059
set_mtrr_aps_delayed_init();
@@ -1256,9 +1243,6 @@ void native_cpu_die(unsigned int cpu)
12561243
if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
12571244
if (system_state == SYSTEM_RUNNING)
12581245
pr_info("CPU %u is now offline\n", cpu);
1259-
1260-
if (1 == num_online_cpus())
1261-
alternatives_smp_switch(0);
12621246
return;
12631247
}
12641248
msleep(100);

‎arch/x86/xen/smp.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,8 @@ static int __cpuinit xen_cpu_up(unsigned int cpu, struct task_struct *idle)
377377
return rc;
378378

379379
if (num_online_cpus() == 1)
380-
alternatives_smp_switch(1);
380+
/* Just in case we booted with a single CPU. */
381+
alternatives_enable_smp();
381382

382383
rc = xen_smp_intr_init(cpu);
383384
if (rc)
@@ -424,9 +425,6 @@ static void xen_cpu_die(unsigned int cpu)
424425
unbind_from_irqhandler(per_cpu(xen_irq_work, cpu), NULL);
425426
xen_uninit_lock_cpu(cpu);
426427
xen_teardown_timer(cpu);
427-
428-
if (num_online_cpus() == 1)
429-
alternatives_smp_switch(0);
430428
}
431429

432430
static void __cpuinit xen_play_dead(void) /* used only with HOTPLUG_CPU */

‎kernel/cpu.c‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -439,14 +439,6 @@ EXPORT_SYMBOL_GPL(cpu_up);
439439
#ifdef CONFIG_PM_SLEEP_SMP
440440
static cpumask_var_t frozen_cpus;
441441

442-
void __weak arch_disable_nonboot_cpus_begin(void)
443-
{
444-
}
445-
446-
void __weak arch_disable_nonboot_cpus_end(void)
447-
{
448-
}
449-
450442
int disable_nonboot_cpus(void)
451443
{
452444
int cpu, first_cpu, error = 0;
@@ -458,7 +450,6 @@ int disable_nonboot_cpus(void)
458450
* with the userspace trying to use the CPU hotplug at the same time
459451
*/
460452
cpumask_clear(frozen_cpus);
461-
arch_disable_nonboot_cpus_begin();
462453

463454
printk("Disabling non-boot CPUs ...\n");
464455
for_each_online_cpu(cpu) {
@@ -474,8 +465,6 @@ int disable_nonboot_cpus(void)
474465
}
475466
}
476467

477-
arch_disable_nonboot_cpus_end();
478-
479468
if (!error) {
480469
BUG_ON(num_online_cpus() > 1);
481470
/* Make sure the CPUs won't be enabled by someone else */

0 commit comments

Comments
 (0)