[patch 1/3] Text Edit Lock - i386

Mathieu Desnoyers
2007-06-18T22:06:29Z

[-- Attachment #1: text-edit-lock-i386.patch --]
[-- Type: text/plain, Size: 3636 bytes --]

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@polymtl.ca>
---
arch/i386/mm/init.c | 52 ++++++++++++++++++++++++++++++++++--------
include/asm-i386/cacheflush.h | 4 +++
2 files changed, 47 insertions(+), 9 deletions(-)

Index: linux-2.6-lttng/arch/i386/mm/init.c
===================================================================
--- linux-2.6-lttng.orig/arch/i386/mm/init.c 2007-06-18 17:42:42.000000000 -0400
+++ linux-2.6-lttng/arch/i386/mm/init.c 2007-06-18 17:56:31.000000000 -0400
@@ -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>
@@ -52,6 +53,9 @@

static int noinline do_test_wp_bit(void);

+/* Mutex protecting text section modification (dynamic code patching) */
+static DEFINE_MUTEX(text_mutex);
+
/*
* Creates a middle page table and puts a pointer to it in the
* given global directory entry. This only returns the gd entry
@@ -800,15 +804,9 @@
unsigned long start = PFN_ALIGN(_text);
unsigned long size = PFN_ALIGN(_etext) - start;

-#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);
- }
+ 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;
@@ -827,6 +825,42 @@
}
#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)
+{
+ mutex_lock(&text_mutex);
+#if defined(CONFIG_DEBUG_RODATA)
+ if (is_kernel_text(address) && is_kernel_text(address + len)) {
+ 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);
+ global_flush_tlb();
+ }
+#endif
+}
+EXPORT_SYMBOL_GPL(kernel_text_lock);
+
+void kernel_text_unlock(unsigned long address, size_t len)
+{
+#if defined(CONFIG_DEBUG_RODATA)
+ if (is_kernel_text(address) && is_kernel_text(address + len)) {
+ 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_RX);
+ global_flush_tlb();
+ }
+#endif
+ mutex_unlock(&text_mutex);
+}
+EXPORT_SYMBOL_GPL(kernel_text_unlock);
+
void free_init_pages(char *what, unsigned long begin, unsigned long end)
{
unsigned long addr;
Index: linux-2.6-lttng/include/asm-i386/cacheflush.h
===================================================================
--- linux-2.6-lttng.orig/include/asm-i386/cacheflush.h 2007-06-18 17:42:42.000000000 -0400
+++ linux-2.6-lttng/include/asm-i386/cacheflush.h 2007-06-18 17:42:44.000000000 -0400
@@ -36,4 +36,8 @@
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 */