Skip to contentSkip to conversation

x86: make SMP locks handling interact properly with CONFIG_DEBUG_RODATA (2nd try)

PATCH In review Jan Beulich 5 participants 11 messages Jul 4, 2007 – Jul 18, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: make SMP locks handling interact properly with CONFIG_DEBUG_RODATA (2nd try)

Instead of suppressing the change of .text to become readonly, make the SMP locks patching code properly adjust/restore the page access rights.

On x86-64 additionally remove all mappings past the kernel image, and remove leftovers from the removal of the more general (but abandoned) SMP alternatives.

Note that while I expect this to work properly on it own, it was only tested together with the change_page_attr() patch submitted before, which namely fixed the reference counting on split pages (which affected earlier versions of this patch).

Assumes the respective KPROBES adjustment is also in place (CONFIG_KPROBES being removed here at once).

 arch/i386/kernel/alternative.c   |   67 +++++++++++++++++++++++++++++++++------
 arch/i386/mm/init.c              |   14 +-------
 arch/x86_64/kernel/vmlinux.lds.S |    9 -----
 arch/x86_64/mm/init.c            |   14 ++------
 4 files changed, 64 insertions(+), 40 deletions(-)
arch/i386/kernel/alternative.c +57 -10
@@ -2,8 +2,11 @@
 #include <linux/sched.h>
 #include <linux/spinlock.h>
 #include <linux/list.h>
+#include <linux/pfn.h>
 #include <asm/alternative.h>
 #include <asm/sections.h>
+#include <asm/pgtable.h>
+#include <asm/cacheflush.h>
 
 static int noreplace_smp     = 0;
 static int smp_alt_once      = 0;
@@ -150,6 +153,46 @@ static void nop_out(void *insns, unsigne
 	}
 }
 
+#ifdef CONFIG_DEBUG_RODATA
+
+#ifdef CONFIG_X86_32
+#include <asm/highmem.h>
+#define MODULES_VADDR VMALLOC_START
+#endif
+
+static inline void make_writable(const void *instr, unsigned int len)
+{
+	unsigned long va = (unsigned long)instr;
+
+	if (va < MODULES_VADDR) {
+		change_page_attr(virt_to_page(instr),
+				 PFN_UP(va + len) - PFN_DOWN(va),
+				 PAGE_KERNEL_EXEC);
+		global_flush_tlb();
+	}
+}
+
+static inline void make_readonly(const void *instr, unsigned int len)
+{
+	unsigned long va = (unsigned long)instr;
+
+	if (va < MODULES_VADDR) {
+		change_page_attr(virt_to_page(instr),
+				 PFN_UP(va + len) - PFN_DOWN(va),
+#ifdef CONFIG_X86_64
+				 PAGE_KERNEL_RO);
+#else
+				 PAGE_KERNEL_RX);
+#endif
+		global_flush_tlb();
+	}
+}
+
+#else /* !CONFIG_DEBUG_RODATA */
+#define make_writable (void)
+#define make_readonly (void)
+#endif
+
 extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
 extern u8 *__smp_locks[], *__smp_locks_end[];
 
@@ -196,7 +239,9 @@ static void alternatives_smp_lock(u8 **s
 			continue;
 		if (*ptr > text_end)
 			continue;
+		make_writable(*ptr, 1);
 		**ptr = 0xf0; /* lock prefix */
+		make_readonly(*ptr, 1);
 	};
 }
 
@@ -212,7 +257,9 @@ static void alternatives_smp_unlock(u8 *
 			continue;
 		if (*ptr > text_end)
 			continue;
+		make_writable(*ptr, 1);
 		nop_out(*ptr, 1);
+		make_readonly(*ptr, 1);
 	};
 }
 
@@ -239,7 +286,6 @@ void alternatives_smp_module_add(struct 
 				 void *text,  void *text_end)
 {
 	struct smp_alt_module *smp;
-	unsigned long flags;
 
 	if (noreplace_smp)
 		return;
@@ -265,39 +311,37 @@ void alternatives_smp_module_add(struct 
 		__FUNCTION__, smp->locks, smp->locks_end,
 		smp->text, smp->text_end, smp->name);
 
-	spin_lock_irqsave(&smp_alt, flags);
+	spin_lock(&smp_alt);
 	list_add_tail(&smp->next, &smp_alt_modules);
 	if (boot_cpu_has(X86_FEATURE_UP))
 		alternatives_smp_unlock(smp->locks, smp->locks_end,
 					smp->text, smp->text_end);
-	spin_unlock_irqrestore(&smp_alt, flags);
+	spin_unlock(&smp_alt);
 }
 
 void alternatives_smp_module_del(struct module *mod)
 {
 	struct smp_alt_module *item;
-	unsigned long flags;
 
 	if (smp_alt_once || noreplace_smp)
 		return;
 
-	spin_lock_irqsave(&smp_alt, flags);
+	spin_lock(&smp_alt);
 	list_for_each_entry(item, &smp_alt_modules, next) {
 		if (mod != item->mod)
 			continue;
 		list_del(&item->next);
-		spin_unlock_irqrestore(&smp_alt, flags);
+		spin_unlock(&smp_alt);
 		DPRINTK("%s: %s\n", __FUNCTION__, item->name);
 		kfree(item);
 		return;
 	}
-	spin_unlock_irqrestore(&smp_alt, flags);
+	spin_unlock(&smp_alt);
 }
 
 void alternatives_smp_switch(int smp)
 {
 	struct smp_alt_module *mod;
-	unsigned long flags;
 
 #ifdef CONFIG_LOCKDEP
 	/*
@@ -313,7 +357,7 @@ void alternatives_smp_switch(int smp)
 		return;
 	BUG_ON(!smp && (num_online_cpus() > 1));
 
-	spin_lock_irqsave(&smp_alt, flags);
+	spin_lock(&smp_alt);
 	if (smp) {
 		printk(KERN_INFO "SMP alternatives: switching to SMP code\n");
 		clear_bit(X86_FEATURE_UP, boot_cpu_data.x86_capability);
@@ -329,7 +373,7 @@ void alternatives_smp_switch(int smp)
 			alternatives_smp_unlock(mod->locks, mod->locks_end,
 						mod->text, mod->text_end);
 	}
-	spin_unlock_irqrestore(&smp_alt, flags);
+	spin_unlock(&smp_alt);
 }
 
 #endif
@@ -369,6 +413,7 @@ void __init alternative_instructions(voi
 
 	local_irq_save(flags);
 	apply_alternatives(__alt_instructions, __alt_instructions_end);
+	local_irq_restore(flags);
 
 	/* switch to patch-once-at-boottime-only mode and free the
 	 * tables in case we know the number of CPUs will never ever
@@ -399,6 +444,8 @@ void __init alternative_instructions(voi
 		alternatives_smp_switch(0);
 	}
 #endif
+
+	local_irq_save(flags);
  	apply_paravirt(__parainstructions, __parainstructions_end);
 	local_irq_restore(flags);
 }
arch/i386/mm/init.c +3 -11
@@ -799,17 +799,9 @@ void mark_rodata_ro(void)
 	unsigned long start = PFN_ALIGN(_text);
 	unsigned long size = PFN_ALIGN(_etext) - start;
 
-#ifndef CONFIG_KPROBES
-#ifdef CONFIG_HOTPLUG_CPU
-	/* It must still be possible to apply SMP alternatives. */
-	if (num_possible_cpus() <= 1)
-#endif
-	{
-		change_page_attr(virt_to_page(start),
-		                 size >> PAGE_SHIFT, PAGE_KERNEL_RX);
-		printk("Write protecting the kernel text: %luk\n", size >> 10);
-	}
-#endif
+	change_page_attr(virt_to_page(start),
+	                 size >> PAGE_SHIFT, PAGE_KERNEL_RX);
+	printk("Write protecting the kernel text: %luk\n", size >> 10);
 	start += size;
 	size = (unsigned long)__end_rodata - start;
 	change_page_attr(virt_to_page(start),
arch/x86_64/kernel/vmlinux.lds.S +0 -9
@@ -131,20 +131,11 @@ SECTIONS
   /* might get freed after init */
   . = ALIGN(4096);
   __smp_alt_begin = .;
-  __smp_alt_instructions = .;
-  .smp_altinstructions : AT(ADDR(.smp_altinstructions) - LOAD_OFFSET) {
-	*(.smp_altinstructions)
-  }
-  __smp_alt_instructions_end = .;
-  . = ALIGN(8);
   __smp_locks = .;
   .smp_locks : AT(ADDR(.smp_locks) - LOAD_OFFSET) {
 	*(.smp_locks)
   }
   __smp_locks_end = .;
-  .smp_altinstr_replacement : AT(ADDR(.smp_altinstr_replacement) - LOAD_OFFSET) {
-	*(.smp_altinstr_replacement)
-  }
   . = ALIGN(4096);
   __smp_alt_end = .;
 
arch/x86_64/mm/init.c +4 -10
@@ -592,6 +592,10 @@ void free_initmem(void)
 	free_init_pages("unused kernel memory",
 			(unsigned long)(&__init_begin),
 			(unsigned long)(&__init_end));
+	change_page_attr_addr(PFN_ALIGN(_end),
+			      (KERNEL_TEXT_SIZE - __pa_symbol(_end)) >> PAGE_SHIFT,
+			      __pgprot(0));
+	global_flush_tlb();
 }
 
 #ifdef CONFIG_DEBUG_RODATA
@@ -600,16 +604,6 @@ void mark_rodata_ro(void)
 {
 	unsigned long start = (unsigned long)_stext, end;
 
-#ifdef CONFIG_HOTPLUG_CPU
-	/* It must still be possible to apply SMP alternatives. */
-	if (num_possible_cpus() > 1)
-		start = (unsigned long)_etext;
-#endif
-
-#ifdef CONFIG_KPROBES
-	start = (unsigned long)__start_rodata;
-#endif
-	
 	end = (unsigned long)__end_rodata;
 	start = (start + PAGE_SIZE - 1) & PAGE_MASK;
 	end &= PAGE_MASK;
#1 lore raw
Quoted 4 lines · Jan Beulich

Instead of suppressing the change of .text to become readonly, make the SMP locks patching code properly adjust/restore the page access rights.

I guess we'll need to do the same thing for paravirt_ops patching?

Quoted 4 lines

On x86-64 additionally remove all mappings past the kernel image, and remove leftovers from the removal of the more general (but abandoned) SMP alternatives.

I'd already posted a patch to remove smp alternatives. I thought it had been merged already, but I guess it's in Andi's queue.

J

#2 lore raw
Quoted 7 lines · On Wednesday 04 July 2007 17:41:39 Jeremy Fitzhardinge wrote:
Quoted 4 lines · Jan Beulich

Instead of suppressing the change of .text to become readonly, make the SMP locks patching code properly adjust/restore the page access rights.

I guess we'll need to do the same thing for paravirt_ops patching?

Not if the patching happens early enough at boot. That's true for Xen and lguest, but I'm not sure about VMI

-Andi

#3 lore raw
Quoted 2 lines · Andi Kleen

Not if the patching happens early enough at boot. That's true for Xen and lguest, but I'm not sure about VMI

paravirt_ops patching happens at the same time as all other patching, via check_bugs(). So its the same regardless of backend, and quite late.

J

#4 lore raw
In reply to Jeremy Fitzhardinge
Quoted 8 lines
Quoted 1 line
Quoted 1 line

Jeremy Fitzhardinge <[email protected]> 04.07.07 17:41 >>>

Quoted 4 lines · Jan Beulich

Instead of suppressing the change of .text to become readonly, make the SMP locks patching code properly adjust/restore the page access rights.

I guess we'll need to do the same thing for paravirt_ops patching?

No, the patch already does this (as long as there no code to do that other than in alternatives.c).

Quoted 7 lines
Quoted 4 lines

On x86-64 additionally remove all mappings past the kernel image, and remove leftovers from the removal of the more general (but abandoned) SMP alternatives.

I'd already posted a patch to remove smp alternatives. I thought it had been merged already, but I guess it's in Andi's queue.

Oh, sorry, I forgot to remove that part of the comment after merging up to 2.6..22-rcX.

Jan

#5 lore raw
In reply to Jeremy Fitzhardinge
Quoted 2 lines · Jeremy Fitzhardinge

I'd already posted a patch to remove smp alternatives. I thought it had been merged already, but I guess it's in Andi's queue.

What was the rationale behind that? Given many distros have now moved to shipping just an SMP kernel, this seems like a step backwards?

Dave

Signature · 1 line
http://www.codemonkey.org.uk
#6 lore raw
Quoted 8 lines · Dave Jones
Quoted 2 lines · Jeremy Fitzhardinge

I'd already posted a patch to remove smp alternatives. I thought it had been merged already, but I guess it's in Andi's queue.

What was the rationale behind that? Given many distros have now moved to shipping just an SMP kernel, this seems like a step backwards?

No, there's two mechanisms. There's the clobber the lock prefix thing, which is unaffected. And there's smp_alternatives, which was a more ambitious mechanism to completely replace instructions in the SMP vs non-SMP cases, which is completely unused and therefore removed.

J

#7 lore raw
In reply to Jan Beulich

On Wednesday 04 July 2007 08:33:24 Jan Beulich wrote:

Sorry for late feedback.

Quoted 24 lines
arch/i386/kernel/alternative.c +24 -0
+#ifdef CONFIG_DEBUG_RODATA
+
+#ifdef CONFIG_X86_32
+#include <asm/highmem.h>
+#define MODULES_VADDR VMALLOC_START
+#endif
+
+static inline void make_writable(const void *instr, unsigned int len)
+{
+	unsigned long va = (unsigned long)instr;
+
+	if (va < MODULES_VADDR) {
+		change_page_attr(virt_to_page(instr),
+				 PFN_UP(va + len) - PFN_DOWN(va),
+				 PAGE_KERNEL_EXEC);
+		global_flush_tlb();
+	}
+}
+
+static inline void make_readonly(const void *instr, unsigned int len)
+{
+	unsigned long va = (unsigned long)instr;
+
+	if (va < MODULES_VADDR) {

arch/i386/kernel/alternative.c static void nop_out(void *insns, unsigne · line 189

Quoted 10 lines
arch/i386/kernel/alternative.c +10 -0
+		change_page_attr(virt_to_page(instr),
+				 PFN_UP(va + len) - PFN_DOWN(va),
+#ifdef CONFIG_X86_64
+				 PAGE_KERNEL_RO);
+#else
+				 PAGE_KERNEL_RX);
+#endif
+		global_flush_tlb();
+	}
+}

Can you please move those into the respective pageattr.cs without ifdefs? Also the va < MODULES_VADDR test seems overly magic; while it may work right now it seems fragile. It would be better to test the exact ranges in the respective architectures

arch/i386/kernel/alternative.c void alternatives_smp_module_add(struct · line 329

Quoted 10 lines

void alternatives_smp_module_del(struct module *mod) {

arch/i386/kernel/alternative.c +1 -2
 	struct smp_alt_module *item;
-	unsigned long flags;
 
 	if (smp_alt_once || noreplace_smp)
 		return;
 
-	spin_lock_irqsave(&smp_alt, flags);
+	spin_lock(&smp_alt);

Unrelated change? Why? Should probably be separate patch.

Quoted 10 lines
arch/x86_64/kernel/vmlinux.lds.S +0 -4
@@ -131,20 +131,11 @@ SECTIONS
   /* might get freed after init */
   . = ALIGN(4096);
   __smp_alt_begin = .;
-  __smp_alt_instructions = .;
-  .smp_altinstructions : AT(ADDR(.smp_altinstructions) - LOAD_OFFSET) {
-	*(.smp_altinstructions)
-  }

arch/x86_64/kernel/vmlinux.lds.S SECTIONS · line 147

Quoted 10 lines
arch/x86_64/kernel/vmlinux.lds.S +0 -5
-  __smp_alt_instructions_end = .;
-  . = ALIGN(8);
   __smp_locks = .;
   .smp_locks : AT(ADDR(.smp_locks) - LOAD_OFFSET) {
 	*(.smp_locks)
   }
   __smp_locks_end = .;
-  .smp_altinstr_replacement : AT(ADDR(.smp_altinstr_replacement) - LOAD_OFFSET) {
-	*(.smp_altinstr_replacement)
-  }

While ok too it should be also separate please.

-Andi

#8 lore raw

Hi Andi,

I guess the patch I proposed there:
"Text Edit Lock"
http://www.ussg.iu.edu/hypermail/linux/kernel/0707.1/2980.html

solves the problem more generally than the patch discussed in this thread. It provides locking (architecture independent) and correct page protection (architecture dependent) for paravirt, alternatives kprobes and, eventually, immediate values.

Mathieu

Quoted 88 lines · * Andi Kleen ([email protected]) wrote:

On Wednesday 04 July 2007 08:33:24 Jan Beulich wrote:

Sorry for late feedback.

Quoted 24 lines
arch/i386/kernel/alternative.c +24 -0
+#ifdef CONFIG_DEBUG_RODATA
+
+#ifdef CONFIG_X86_32
+#include <asm/highmem.h>
+#define MODULES_VADDR VMALLOC_START
+#endif
+
+static inline void make_writable(const void *instr, unsigned int len)
+{
+	unsigned long va = (unsigned long)instr;
+
+	if (va < MODULES_VADDR) {
+		change_page_attr(virt_to_page(instr),
+				 PFN_UP(va + len) - PFN_DOWN(va),
+				 PAGE_KERNEL_EXEC);
+		global_flush_tlb();
+	}
+}
+
+static inline void make_readonly(const void *instr, unsigned int len)
+{
+	unsigned long va = (unsigned long)instr;
+
+	if (va < MODULES_VADDR) {

arch/i386/kernel/alternative.c static void nop_out(void *insns, unsigne · line 189

Quoted 10 lines
arch/i386/kernel/alternative.c +10 -0
+		change_page_attr(virt_to_page(instr),
+				 PFN_UP(va + len) - PFN_DOWN(va),
+#ifdef CONFIG_X86_64
+				 PAGE_KERNEL_RO);
+#else
+				 PAGE_KERNEL_RX);
+#endif
+		global_flush_tlb();
+	}
+}

Can you please move those into the respective pageattr.cs without ifdefs? Also the va < MODULES_VADDR test seems overly magic; while it may work right now it seems fragile. It would be better to test the exact ranges in the respective architectures

arch/i386/kernel/alternative.c void alternatives_smp_module_add(struct · line 329

Quoted 10 lines

void alternatives_smp_module_del(struct module *mod) {

arch/i386/kernel/alternative.c +1 -2
 	struct smp_alt_module *item;
-	unsigned long flags;
 
 	if (smp_alt_once || noreplace_smp)
 		return;
 
-	spin_lock_irqsave(&smp_alt, flags);
+	spin_lock(&smp_alt);

Unrelated change? Why? Should probably be separate patch.

Quoted 10 lines
arch/x86_64/kernel/vmlinux.lds.S +0 -4
@@ -131,20 +131,11 @@ SECTIONS
   /* might get freed after init */
   . = ALIGN(4096);
   __smp_alt_begin = .;
-  __smp_alt_instructions = .;
-  .smp_altinstructions : AT(ADDR(.smp_altinstructions) - LOAD_OFFSET) {
-	*(.smp_altinstructions)
-  }

arch/x86_64/kernel/vmlinux.lds.S SECTIONS · line 147

Quoted 10 lines
arch/x86_64/kernel/vmlinux.lds.S +0 -5
-  __smp_alt_instructions_end = .;
-  . = ALIGN(8);
   __smp_locks = .;
   .smp_locks : AT(ADDR(.smp_locks) - LOAD_OFFSET) {
 	*(.smp_locks)
   }
   __smp_locks_end = .;
-  .smp_altinstr_replacement : AT(ADDR(.smp_altinstr_replacement) - LOAD_OFFSET) {
-	*(.smp_altinstr_replacement)
-  }

While ok too it should be also separate please.

(quoted diff) +0 -2
-Andi
-

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

Signature · 3 lines
Mathieu Desnoyers
Computer Engineering Ph.D. Student, Ecole Polytechnique de Montreal
OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F  BA06 3F25 A8FE 3BAE 9A68
#9 lore raw
In reply to Andi Kleen
Quoted 5 lines
Quoted 5 lines

void alternatives_smp_module_del(struct module *mod) {

(quoted diff) +0 -1
 	struct smp_alt_module *item;
-	unsigned long flags;
Quoted 7 lines

arch/i386/kernel/alternative.c void alternatives_smp_module_add(struct · line 329

Quoted 5 lines
arch/i386/kernel/alternative.c +1 -1
 	if (smp_alt_once || noreplace_smp)
 		return;
 
-	spin_lock_irqsave(&smp_alt, flags);
+	spin_lock(&smp_alt);

Unrelated change? Why? Should probably be separate patch.

Absolutely not: You can't call change_page_attr(), or more precisely global_flush_tlb(), with interrupts disabled. And since interrupt disabling is unneeded here afaics, I simply changed the calls instead of inventing some ugly workaround.

I'll do the other adjustments you asked for, but it'll be only in about three weeks from now...

Jan

#10 lore raw
In reply to Jan Beulich

arch/i386/kernel/alternative.c static void nop_out(void *insns, unsigne · line 186

Quoted 9 lines · Jan Beulich
arch/i386/kernel/alternative.c +8 -0
+	if (va < MODULES_VADDR) {
+		change_page_attr(virt_to_page(instr),
+				 PFN_UP(va + len) - PFN_DOWN(va),
+#ifdef CONFIG_X86_64
+				 PAGE_KERNEL_RO);
+#else
+				 PAGE_KERNEL_RX);
+#endif

This kind if unbalanced parenthesis can confuse various tools. Format it differently?

J