Skip to contentSkip to conversation

2.6.17-rc3 -- SMP alternatives: switching to UP code

PATCH In review Rene Herman 3 participants 8 messages May 10, 2006 – May 15, 2006

lore
Share & tools
lore
#0 lore raw

Hi list.

I just noticed this in the 2.6.17-rc3 dmesg:

===
Checking 'hlt' instruction... OK. SMP alternatives: switching to UP code Freeing SMP alternatives: 0k freed
ACPI: setting ELCR to 0400 (from 1608)
===

Should I be seeing this "SMP alternatives" thing on a !CONFIG_SMP kernel? It does say 0k, but something is apparently being done at runtime still. Why?

Rene.

#1 lore raw
Quoted 11 lines · Rene Herman

Hi list.

I just noticed this in the 2.6.17-rc3 dmesg:

===
Checking 'hlt' instruction... OK.
SMP alternatives: switching to UP code
Freeing SMP alternatives: 0k freed
ACPI: setting ELCR to 0400 (from 1608)
===

Quoted 3 lines

Should I be seeing this "SMP alternatives" thing on a !CONFIG_SMP kernel? It does say 0k, but something is apparently being done at runtime still. Why?

This is part of a recent patch by Gerd Hoffmann:

Quoted 3 lines
Quoted 3 lines

Implement SMP alternatives, i.e. switching at runtime between different code versions for UP and SMP. The code can patch both SMP->UP and UP->SMP. The UP->SMP case is useful for CPU hotplug.

An explicit test of the smp variable (not CONFIG_SMP macro) exists in this code. I missed the discussion about this patch, but it appears from reading the patch notes that the behavior is intentional. More information can be found in the git and lkml archives.

Quoted 1 line

Rene.

Thanks,
Chase

#2 lore raw
In reply to Rene Herman
Quoted 11 lines · Rene Herman

Hi list.

I just noticed this in the 2.6.17-rc3 dmesg:

===
Checking 'hlt' instruction... OK.
SMP alternatives: switching to UP code
Freeing SMP alternatives: 0k freed
ACPI: setting ELCR to 0400 (from 1608)
===

Quoted 3 lines

Should I be seeing this "SMP alternatives" thing on a !CONFIG_SMP kernel? It does say 0k, but something is apparently being done at runtime still. Why?

The UP kernel has empty alternatives tables (as you've noticed), thus the code doesn't do anything. Nevertheless it probably makes sense to add a few #ifdef CONFIG_SMP lines to avoid confusing people and safe a few bytes ...

cheers,

Gerd

Signature · 4 lines
Gerd Hoffmann <[email protected]>
Erst mal heiraten, ein, zwei Kinder, und wenn alles läuft
geh' ich nach drei Jahren mit der Familie an die Börse.
http://www.suse.de/~kraxel/julika-dora.jpeg
#3 lore raw
Quoted 10 lines · Gerd Hoffmann
Quoted 3 lines · Rene Herman

Should I be seeing this "SMP alternatives" thing on a !CONFIG_SMP kernel? It does say 0k, but something is apparently being done at runtime still. Why?

The UP kernel has empty alternatives tables (as you've noticed), thus the code doesn't do anything. Nevertheless it probably makes sense to add a few #ifdef CONFIG_SMP lines to avoid confusing people and safe a few bytes ...

Okay, thanks. Yes, I agree such would make sense.

Rene.

#4 lore raw

PATCH 1/1 SMP alternatives: skip with UP kernels.

Quoted 1 line

Okay, thanks. Yes, I agree such would make sense.

Patch below. It simply returns in case the tables are empty and nothing is do to, thus avoids printing the confusing message.

cheers,

Gerd

Signature · 4 lines
Gerd Hoffmann <[email protected]>
Erst mal heiraten, ein, zwei Kinder, und wenn alles läuft
geh' ich nach drei Jahren mit der Familie an die Börse.
http://www.suse.de/~kraxel/julika-dora.jpeg
arch/i386/kernel/alternative.c +3 -0
Index: vanilla-2.6.17-rc3-mm1/arch/i386/kernel/alternative.c
===================================================================
@@ -349,6 +349,9 @@ void __init alternative_instructions(voi
 	smp_alt_once = 1;
 #endif
 
+	if (0 == (__smp_alt_end - __smp_alt_begin))
+		return; /* no tables, nothing to patch (UP kernel) */
+
 	if (smp_alt_once) {
 		if (1 == num_possible_cpus()) {
 			printk(KERN_INFO "SMP alternatives: switching to UP code\n");
#5 lore raw

Re: [patch] SMP alternatives: skip with UP kernels.

Gerd Hoffmann wrote:

[ depending on CONFIG_SMP ]

Quoted 4 lines
Quoted 1 line

Okay, thanks. Yes, I agree such would make sense.

Patch below. It simply returns in case the tables are empty and nothing is do to, thus avoids printing the confusing message.

It does avoid the message but well, that's also _all_ it avoids. Why do you want to do it this way? If I apply the attached, things compile and work fine which seems to confirm that, yes, all this smp_alternatives code is simply dead baggage on !CONFIG_SMP.

Yes, the #ifdef in arch/i386/kernel/module.c is a bit clumsy. Just proof of concept, might be abstracted out a bit better. It does lose most of alternatives.c which would seem to be good thing.

Unless I'm missing some points, I believe this code should not be compiled in for !CONFIG_SMP.

One other point, although probably not a relevant one. Your changes in module_finalize() potentially change behaviour. It used to be:

for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
	void *seg;
	if (strcmp(".altinstructions", secstrings + s->sh_name))
		continue;
	seg = (void *)s->sh_addr;
	apply_alternatives(seg, seg + s->sh_size);
}

which means that any .altinstructions section would be patched. It's now:

for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
	...
	if (!strcmp(".altinstructions", secstrings + s->sh_name))
		alt = s;
	...
}
if (alt) {
	/* patch .altinstructions */
	void *aseg = (void *)alt->sh_addr;
	apply_alternatives(aseg, aseg + alt->sh_size);
}

which means that only the last such section would. I suppose there's only one such section anyway, but not seeing a break in the original does make me wonder if this was originally done on purpose.

Rene.

arch/i386/kernel/alternative.c +6 -0
Index: local/arch/i386/kernel/alternative.c
===================================================================
@@ -118,6 +118,8 @@ void apply_alternatives(struct alt_instr
 	}
 }
 
+#ifdef CONFIG_SMP
+
 static void alternatives_smp_save(struct alt_instr *start, struct alt_instr *end)
 {
 	struct alt_instr *a;
@@ -283,10 +285,13 @@ void alternatives_smp_switch(int smp)
 	spin_unlock_irqrestore(&smp_alt, flags);
 }
 
+#endif /* CONFIG_SMP */
+
 void __init alternative_instructions(void)
 {
 	apply_alternatives(__alt_instructions, __alt_instructions_end);
 
+#ifdef CONFIG_SMP
 	/* switch to patch-once-at-boottime-only mode and free the
 	 * tables in case we know the number of CPUs will never ever
 	 * change */
@@ -318,4 +323,5 @@ void __init alternative_instructions(voi
 					    _text, _etext);
 		alternatives_smp_switch(0);
 	}
+#endif
 }
arch/i386/kernel/module.c +8 -2
Index: local/arch/i386/kernel/module.c
===================================================================
@@ -112,12 +112,14 @@ int module_finalize(const Elf_Ehdr *hdr,
 	char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
 
 	for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) { 
-		if (!strcmp(".text", secstrings + s->sh_name))
-			text = s;
 		if (!strcmp(".altinstructions", secstrings + s->sh_name))
 			alt = s;
+#ifdef CONFIG_SMP
 		if (!strcmp(".smp_locks", secstrings + s->sh_name))
 			locks= s;
+		if (!strcmp(".text", secstrings + s->sh_name))
+			text = s;
+#endif
 	}
 
 	if (alt) {
@@ -126,16 +128,20 @@ int module_finalize(const Elf_Ehdr *hdr,
 		apply_alternatives(aseg, aseg + alt->sh_size);
 	}
 	if (locks && text) {
+#ifdef CONFIG_SMP
 		void *lseg = (void *)locks->sh_addr;
 		void *tseg = (void *)text->sh_addr;
 		alternatives_smp_module_add(me, me->name,
 					    lseg, lseg + locks->sh_size,
 					    tseg, tseg + text->sh_size);
+#endif
 	}
 	return 0;
 }
 
 void module_arch_cleanup(struct module *mod)
 {
+#ifdef CONFIG_SMP
 	alternatives_smp_module_del(mod);
+#endif
 }
#6 lore raw

Re: [patch] SMP alternatives: skip with UP kernels.

Quoted 7 lines
Quoted 2 lines

Patch below. It simply returns in case the tables are empty and nothing is do to, thus avoids printing the confusing message.

It does avoid the message but well, that's also _all_ it avoids. Why do you want to do it this way? [ ... patch ... ]

Yes, the #ifdef in arch/i386/kernel/module.c is a bit clumsy.

Yep, thats why. I wanted avoid exactly that. Having some code need to know that function foobar() is only available with CONFIG_BAZ is set is really ugly ...

The attached patch hides the magic in alternative.h and provides some dummy inline functions for the UP case (gcc should manage to optimize away these calls). No changes in module.c.

cheers,

Gerd

Signature · 4 lines
Gerd Hoffmann <[email protected]>
Erst mal heiraten, ein, zwei Kinder, und wenn alles läuft
geh' ich nach drei Jahren mit der Familie an die Börse.
http://www.suse.de/~kraxel/julika-dora.jpeg
arch/i386/kernel/alternative.c +6 -0
Index: vanilla-2.6.17-rc3-mm1/arch/i386/kernel/alternative.c
===================================================================
@@ -168,6 +168,8 @@ void apply_alternatives(struct alt_instr
 	}
 }
 
+#ifdef CONFIG_SMP
+
 static void alternatives_smp_save(struct alt_instr *start, struct alt_instr *end)
 {
 	struct alt_instr *a;
@@ -328,6 +330,8 @@ void alternatives_smp_switch(int smp)
 	spin_unlock_irqrestore(&smp_alt, flags);
 }
 
+#endif
+
 void __init alternative_instructions(void)
 {
 	if (no_replacement) {
@@ -349,6 +353,7 @@ void __init alternative_instructions(voi
 	smp_alt_once = 1;
 #endif
 
+#ifdef CONFIG_SMP
 	if (smp_alt_once) {
 		if (1 == num_possible_cpus()) {
 			printk(KERN_INFO "SMP alternatives: switching to UP code\n");
@@ -370,4 +375,5 @@ void __init alternative_instructions(voi
 					    _text, _etext);
 		alternatives_smp_switch(0);
 	}
+#endif
 }
include/asm-i386/alternative.h +8 -0
Index: vanilla-2.6.17-rc3-mm1/include/asm-i386/alternative.h
===================================================================
@@ -17,11 +17,19 @@ struct alt_instr {
 extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
 
 struct module;
+#ifdef CONFIG_SMP
 extern void alternatives_smp_module_add(struct module *mod, char *name,
 					void *locks, void *locks_end,
 					void *text, void *text_end);
 extern void alternatives_smp_module_del(struct module *mod);
 extern void alternatives_smp_switch(int smp);
+#else
+static inline void alternatives_smp_module_add(struct module *mod, char *name,
+					void *locks, void *locks_end,
+					void *text, void *text_end) {}
+static inline void alternatives_smp_module_del(struct module *mod) {}
+static inline void alternatives_smp_switch(int smp) {}
+#endif
 
 #endif
 
include/asm-x86_64/alternative.h +9 -0
Index: vanilla-2.6.17-rc3-mm1/include/asm-x86_64/alternative.h
===================================================================
@@ -17,11 +17,20 @@ struct alt_instr {
 extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
 
 struct module;
+
+#ifdef CONFIG_SMP
 extern void alternatives_smp_module_add(struct module *mod, char *name,
 					void *locks, void *locks_end,
 					void *text, void *text_end);
 extern void alternatives_smp_module_del(struct module *mod);
 extern void alternatives_smp_switch(int smp);
+#else
+static inline void alternatives_smp_module_add(struct module *mod, char *name,
+					void *locks, void *locks_end,
+					void *text, void *text_end) {}
+static inline void alternatives_smp_module_del(struct module *mod) {}
+static inline void alternatives_smp_switch(int smp) {}
+#endif
 
 #endif
 
#7 lore raw

Re: [patch] SMP alternatives: skip with UP kernels.

Quoted 9 lines · Gerd Hoffmann
Quoted 1 line

Yes, the #ifdef in arch/i386/kernel/module.c is a bit clumsy.

Yep, thats why. I wanted avoid exactly that. Having some code need to know that function foobar() is only available with CONFIG_BAZ is set is really ugly ...

The attached patch hides the magic in alternative.h and provides some dummy inline functions for the UP case (gcc should manage to optimize away these calls). No changes in module.c.

Works for me; it doesn't easily get non-clumsy in module.c I see. Sure you want to keep smp_alt_once outside the #ifdef? Seems to not be doing anything other than being set to 1 for !SMP.

Thanks,
Rene.