The patch titled i386: Text Edit Lock has been added to the -mm tree. Its filename is i386-text-edit-lock.patch *** Remember to use Documentation/SubmitChecklist when testing your code *** See http://www.zip.com.au/~akpm/linux/patches/stuff/added-to-mm.txt to find out what to do about this ------------------------------------------------------ Subject: i386: Text Edit Lock From: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxx> Interface to use for code patching : uses a mutex to insure mutual edit exclusion and makes sure the page is writable. Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxx> Cc: Andi Kleen <ak@xxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- arch/i386/mm/init.c | 69 ++++++++++++++++++++++++++------ include/asm-i386/cacheflush.h | 4 + 2 files changed, 61 insertions(+), 12 deletions(-) diff -puN arch/i386/mm/init.c~i386-text-edit-lock arch/i386/mm/init.c --- a/arch/i386/mm/init.c~i386-text-edit-lock +++ a/arch/i386/mm/init.c @@ -31,6 +31,7 @@ #include <linux/memory_hotplug.h> #include <linux/initrd.h> #include <linux/cpumask.h> +#include <linux/mutex.h> #include <asm/processor.h> #include <asm/system.h> @@ -53,6 +54,13 @@ unsigned long highstart_pfn, highend_pfn static int noinline do_test_wp_bit(void); +/* spin lock protecting text section modification (dynamic code patching) */ +static DEFINE_SPINLOCK(text_lock); + +#ifdef CONFIG_DEBUG_RODATA +static int rodata_marked; +#endif + /* * Creates a middle page table and puts a pointer to it in the * given global directory entry. This only returns the gd entry @@ -802,18 +810,11 @@ 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); - kernel_text_is_ro = 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); + kernel_text_is_ro = 1; + start += size; size = (unsigned long)__end_rodata - start; change_page_attr(virt_to_page(start), @@ -828,8 +829,52 @@ void mark_rodata_ro(void) * of who is the culprit. */ global_flush_tlb(); + rodata_marked = 1; +} +#endif + +/* + * Lock the kernel text for mutual write exclusion. + * Make sure the pages are writable. + */ +void kernel_text_lock(unsigned long address, size_t len) +{ + spin_lock(&text_lock); +#if defined(CONFIG_DEBUG_RODATA) + if (rodata_marked && address >= PFN_ALIGN(_text) + && (address + len) <= PFN_ALIGN(_etext)) { + unsigned long nr_pages; + nr_pages = ((address + len) >> PAGE_SHIFT) + - (address >> PAGE_SHIFT) + 1; + change_page_attr(virt_to_page(address), nr_pages, + PAGE_KERNEL_EXEC); + mb(); + global_flush_tlb(); + mb(); + } +#endif } +EXPORT_SYMBOL_GPL(kernel_text_lock); + +void kernel_text_unlock(unsigned long address, size_t len) +{ +#if defined(CONFIG_DEBUG_RODATA) + if (rodata_marked && address >= PFN_ALIGN(_text) + && (address + len) <= PFN_ALIGN(_etext)) { + unsigned long nr_pages; + wmb(); + nr_pages = ((address + len) >> PAGE_SHIFT) + - (address >> PAGE_SHIFT) + 1; + mb(); + change_page_attr(virt_to_page(address), nr_pages, + PAGE_KERNEL_RX); + mb(); + global_flush_tlb(); + } #endif + spin_unlock(&text_lock); +} +EXPORT_SYMBOL_GPL(kernel_text_unlock); void free_init_pages(char *what, unsigned long begin, unsigned long end) { diff -puN include/asm-i386/cacheflush.h~i386-text-edit-lock include/asm-i386/cacheflush.h --- a/include/asm-i386/cacheflush.h~i386-text-edit-lock +++ a/include/asm-i386/cacheflush.h @@ -36,4 +36,8 @@ void kernel_map_pages(struct page *page, void mark_rodata_ro(void); #endif +/* lock kernel text and mark pages writable */ +extern void kernel_text_lock(unsigned long address, size_t len); +extern void kernel_text_unlock(unsigned long address, size_t len); + #endif /* _I386_CACHEFLUSH_H */ _ Patches currently in -mm which might be from mathieu.desnoyers@xxxxxxxxxx are powerpc-promc-remove-undef-printk.patch i386-text-edit-lock.patch i386-text-edit-lock-alternative-instructions.patch i386-text-edit-lock-kprobes.patch - To unsubscribe from this list: send the line "unsubscribe mm-commits" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html