Late module relocations are an issue on any arch that supports
livepatch, so move the text_mutex locking to the livepatch core code.

Signed-off-by: Dylan Hatch <dylanbha...@google.com>
---
 arch/x86/kernel/module.c |  8 ++------
 kernel/livepatch/core.c  | 18 +++++++++++++-----
 2 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/arch/x86/kernel/module.c b/arch/x86/kernel/module.c
index ff07558b7ebc6..38767e0047d0c 100644
--- a/arch/x86/kernel/module.c
+++ b/arch/x86/kernel/module.c
@@ -197,18 +197,14 @@ static int write_relocate_add(Elf64_Shdr *sechdrs,
        bool early = me->state == MODULE_STATE_UNFORMED;
        void *(*write)(void *, const void *, size_t) = memcpy;
 
-       if (!early) {
+       if (!early)
                write = text_poke;
-               mutex_lock(&text_mutex);
-       }
 
        ret = __write_relocate_add(sechdrs, strtab, symindex, relsec, me,
                                   write, apply);
 
-       if (!early) {
+       if (!early)
                text_poke_sync();
-               mutex_unlock(&text_mutex);
-       }
 
        return ret;
 }
diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c
index 0e73fac55f8eb..9968441f73510 100644
--- a/kernel/livepatch/core.c
+++ b/kernel/livepatch/core.c
@@ -294,9 +294,10 @@ static int klp_write_section_relocs(struct module *pmod, 
Elf_Shdr *sechdrs,
                                    unsigned int symndx, unsigned int secndx,
                                    const char *objname, bool apply)
 {
-       int cnt, ret;
+       int cnt, ret = 0;
        char sec_objname[MODULE_NAME_LEN];
        Elf_Shdr *sec = sechdrs + secndx;
+       bool early = pmod->state == MODULE_STATE_UNFORMED;
 
        /*
         * Format: .klp.rela.sec_objname.section_name
@@ -319,12 +320,19 @@ static int klp_write_section_relocs(struct module *pmod, 
Elf_Shdr *sechdrs,
                                          sec, sec_objname);
                if (ret)
                        return ret;
-
-               return apply_relocate_add(sechdrs, strtab, symndx, secndx, 
pmod);
        }
 
-       clear_relocate_add(sechdrs, strtab, symndx, secndx, pmod);
-       return 0;
+       if (!early)
+               mutex_lock(&text_mutex);
+
+       if (apply)
+               ret = apply_relocate_add(sechdrs, strtab, symndx, secndx, pmod);
+       else
+               clear_relocate_add(sechdrs, strtab, symndx, secndx, pmod);
+
+       if (!early)
+               mutex_unlock(&text_mutex);
+       return ret;
 }
 
 int klp_apply_section_relocs(struct module *pmod, Elf_Shdr *sechdrs,
-- 
2.49.0.1151.ga128411c76-goog


Reply via email to