From: Josh Poimboeuf <jpoimboe@redhat.com>
To: Miroslav Benes <mbenes@suse.cz>
Cc: Seth Jennings <sjenning@redhat.com>,
Jiri Kosina <jikos@kernel.org>, Vojtech Pavlik <vojtech@suse.com>,
linux-kernel@vger.kernel.org, live-patching@vger.kernel.org,
"Cyril B." <cbay@alwaysdata.com>
Subject: [PATCH] livepatch: Cleanup page permission changes
Date: Tue, 3 Nov 2015 11:42:28 -0600 [thread overview]
Message-ID: <20151103174228.GN27488@treble.redhat.com> (raw)
In-Reply-To: <alpine.LNX.2.00.1511031109370.6257@pobox.suse.cz>
It's probably a good idea to keep the patches bisectable, so I made this
a separate patch which applies on top of the first one.
(Note that it completely removes all the code from the first patch, so
there's no need for a v2 of the first patch which would have had
Miroslav's suggested style changes.)
---8<---
Subject: [PATCH] livepatch: Cleanup page permission changes
Calling set_memory_rw() and set_memory_ro() for every iteration of the
loop in klp_write_object_relocations() is messy and inefficient. Change
all the RO pages to RW before the loop and convert them back to RO after
the loop.
Suggested-by: Miroslav Benes <mbenes@suse.cz>
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
---
arch/x86/kernel/livepatch.c | 25 ++-----------------------
kernel/livepatch/core.c | 42 +++++++++++++++++++++++++++++++++++++-----
2 files changed, 39 insertions(+), 28 deletions(-)
diff --git a/arch/x86/kernel/livepatch.c b/arch/x86/kernel/livepatch.c
index d1d35cc..1062eff 100644
--- a/arch/x86/kernel/livepatch.c
+++ b/arch/x86/kernel/livepatch.c
@@ -20,8 +20,6 @@
#include <linux/module.h>
#include <linux/uaccess.h>
-#include <asm/cacheflush.h>
-#include <asm/page_types.h>
#include <asm/elf.h>
#include <asm/livepatch.h>
@@ -38,8 +36,7 @@
int klp_write_module_reloc(struct module *mod, unsigned long type,
unsigned long loc, unsigned long value)
{
- int ret, numpages, size = 4;
- bool readonly;
+ int size = 4;
unsigned long val;
unsigned long core = (unsigned long)mod->module_core;
unsigned long core_size = mod->core_size;
@@ -69,23 +66,5 @@ int klp_write_module_reloc(struct module *mod, unsigned long type,
/* loc does not point to any symbol inside the module */
return -EINVAL;
- readonly = false;
-
-#ifdef CONFIG_DEBUG_SET_MODULE_RONX
- if (loc < core + mod->core_ro_size)
- readonly = true;
-#endif
-
- /* determine if the relocation spans a page boundary */
- numpages = ((loc & PAGE_MASK) == ((loc + size) & PAGE_MASK)) ? 1 : 2;
-
- if (readonly)
- set_memory_rw(loc & PAGE_MASK, numpages);
-
- ret = probe_kernel_write((void *)loc, &val, size);
-
- if (readonly)
- set_memory_ro(loc & PAGE_MASK, numpages);
-
- return ret;
+ return probe_kernel_write((void *)loc, &val, size);
}
diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c
index 6e53441..328fbd5 100644
--- a/kernel/livepatch/core.c
+++ b/kernel/livepatch/core.c
@@ -28,6 +28,7 @@
#include <linux/list.h>
#include <linux/kallsyms.h>
#include <linux/livepatch.h>
+#include <asm/cacheflush.h>
/**
* struct klp_ops - structure for tracking registered ftrace ops structs
@@ -131,6 +132,33 @@ static bool klp_initialized(void)
return !!klp_root_kobj;
}
+#ifdef CONFIG_DEBUG_SET_MODULE_RONX
+static void set_page_attributes(void *start, void *end,
+ int (*set)(unsigned long start, int num_pages))
+{
+ unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
+ unsigned long end_pfn = PFN_DOWN((unsigned long)end);
+
+ if (end_pfn > begin_pfn)
+ set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
+}
+static void set_module_ro_rw(struct module *mod)
+{
+ set_page_attributes(mod->module_core,
+ mod->module_core + mod->core_ro_size,
+ set_memory_rw);
+}
+static void set_module_ro_ro(struct module *mod)
+{
+ set_page_attributes(mod->module_core,
+ mod->module_core + mod->core_ro_size,
+ set_memory_ro);
+}
+#else
+static void set_module_ro_rw(struct module *mod) {}
+static void set_module_ro_ro(struct module *mod) {}
+#endif
+
struct klp_find_arg {
const char *objname;
const char *name;
@@ -283,7 +311,7 @@ static int klp_find_external_symbol(struct module *pmod, const char *name,
static int klp_write_object_relocations(struct module *pmod,
struct klp_object *obj)
{
- int ret;
+ int ret = 0;
struct klp_reloc *reloc;
if (WARN_ON(!klp_is_object_loaded(obj)))
@@ -292,12 +320,14 @@ static int klp_write_object_relocations(struct module *pmod,
if (WARN_ON(!obj->relocs))
return -EINVAL;
+ set_module_ro_rw(pmod);
+
for (reloc = obj->relocs; reloc->name; reloc++) {
if (!klp_is_module(obj)) {
ret = klp_verify_vmlinux_symbol(reloc->name,
reloc->val);
if (ret)
- return ret;
+ goto out;
} else {
/* module, reloc->val needs to be discovered */
if (reloc->external)
@@ -309,18 +339,20 @@ static int klp_write_object_relocations(struct module *pmod,
reloc->name,
&reloc->val);
if (ret)
- return ret;
+ goto out;
}
ret = klp_write_module_reloc(pmod, reloc->type, reloc->loc,
reloc->val + reloc->addend);
if (ret) {
pr_err("relocation failed for symbol '%s' at 0x%016lx (%d)\n",
reloc->name, reloc->val, ret);
- return ret;
+ goto out;
}
}
- return 0;
+out:
+ set_module_ro_ro(pmod);
+ return ret;
}
static void notrace klp_ftrace_handler(unsigned long ip,
--
2.4.3
next prev parent reply other threads:[~2015-11-03 17:42 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-11-02 20:00 [PATCH] x86/livepatch: Fix crash with !CONFIG_DEBUG_SET_MODULE_RONX Josh Poimboeuf
2015-11-03 10:22 ` Miroslav Benes
2015-11-03 15:22 ` Josh Poimboeuf
2015-11-03 17:42 ` Josh Poimboeuf [this message]
2015-11-04 9:18 ` [PATCH] livepatch: Cleanup page permission changes Jiri Kosina
2015-11-04 16:10 ` Josh Poimboeuf
2015-11-04 16:13 ` Josh Poimboeuf
2015-11-04 9:40 ` Miroslav Benes
2015-11-04 22:56 ` Jiri Kosina
2015-11-04 23:12 ` Josh Poimboeuf
2015-11-05 9:28 ` Jiri Kosina
2015-11-05 9:40 ` Jiri Kosina
2015-11-05 15:17 ` Josh Poimboeuf
2015-11-05 17:33 ` Josh Poimboeuf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20151103174228.GN27488@treble.redhat.com \
--to=jpoimboe@redhat.com \
--cc=cbay@alwaysdata.com \
--cc=jikos@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=live-patching@vger.kernel.org \
--cc=mbenes@suse.cz \
--cc=sjenning@redhat.com \
--cc=vojtech@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).