livepatch-patch-hook: clean up version checks

Clean up the kernel version checks a little bit.  Use 'HAVE_*' naming
everywhere for consistency.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
This commit is contained in:
Josh Poimboeuf 2017-10-18 06:29:10 -05:00
parent 7d99acf6ad
commit d44a4b9df5

View File

@ -35,8 +35,18 @@
#define UTS_UBUNTU_RELEASE_ABI 0 #define UTS_UBUNTU_RELEASE_ABI 0
#endif #endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 7, 0) #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
#define NEED_KLP_RELOCS #define HAVE_ELF_RELOCS
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0) || \
(LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0) && \
UTS_UBUNTU_RELEASE_ABI >= 7)
#define HAVE_SYMPOS
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
#define HAVE_IMMEDIATE
#endif #endif
/* /*
@ -87,7 +97,7 @@ static struct patch_object *patch_alloc_new_object(const char *name)
if (!object) if (!object)
return NULL; return NULL;
INIT_LIST_HEAD(&object->funcs); INIT_LIST_HEAD(&object->funcs);
#ifdef NEED_KLP_RELOCS #ifndef HAVE_ELF_RELOCS
INIT_LIST_HEAD(&object->relocs); INIT_LIST_HEAD(&object->relocs);
#endif #endif
if (strcmp(name, "vmlinux")) if (strcmp(name, "vmlinux"))
@ -129,7 +139,7 @@ static int patch_add_func_to_object(struct kpatch_patch_func *kfunc)
return 0; return 0;
} }
#ifdef NEED_KLP_RELOCS #ifndef HAVE_ELF_RELOCS
static int patch_add_reloc_to_object(struct kpatch_patch_dynrela *kdynrela) static int patch_add_reloc_to_object(struct kpatch_patch_dynrela *kdynrela)
{ {
struct patch_reloc *reloc; struct patch_reloc *reloc;
@ -155,7 +165,7 @@ static int patch_add_reloc_to_object(struct kpatch_patch_dynrela *kdynrela)
static void patch_free_scaffold(void) { static void patch_free_scaffold(void) {
struct patch_func *func, *safefunc; struct patch_func *func, *safefunc;
struct patch_object *object, *safeobject; struct patch_object *object, *safeobject;
#ifdef NEED_KLP_RELOCS #ifndef HAVE_ELF_RELOCS
struct patch_reloc *reloc, *safereloc; struct patch_reloc *reloc, *safereloc;
#endif #endif
@ -165,7 +175,7 @@ static void patch_free_scaffold(void) {
list_del(&func->list); list_del(&func->list);
kfree(func); kfree(func);
} }
#ifdef NEED_KLP_RELOCS #ifndef HAVE_ELF_RELOCS
list_for_each_entry_safe(reloc, safereloc, list_for_each_entry_safe(reloc, safereloc,
&object->relocs, list) { &object->relocs, list) {
list_del(&reloc->list); list_del(&reloc->list);
@ -185,7 +195,7 @@ static void patch_free_livepatch(struct klp_patch *patch)
for (object = patch->objs; object && object->funcs; object++) { for (object = patch->objs; object && object->funcs; object++) {
if (object->funcs) if (object->funcs)
kfree(object->funcs); kfree(object->funcs);
#ifdef NEED_KLP_RELOCS #ifndef HAVE_ELF_RELOCS
if (object->relocs) if (object->relocs)
kfree(object->relocs); kfree(object->relocs);
#endif #endif
@ -197,7 +207,7 @@ static void patch_free_livepatch(struct klp_patch *patch)
} }
extern struct kpatch_patch_func __kpatch_funcs[], __kpatch_funcs_end[]; extern struct kpatch_patch_func __kpatch_funcs[], __kpatch_funcs_end[];
#ifdef NEED_KLP_RELOCS #ifndef HAVE_ELF_RELOCS
extern struct kpatch_patch_dynrela __kpatch_dynrelas[], __kpatch_dynrelas_end[]; extern struct kpatch_patch_dynrela __kpatch_dynrelas[], __kpatch_dynrelas_end[];
#endif #endif
@ -209,7 +219,7 @@ static int __init patch_init(void)
struct patch_object *object; struct patch_object *object;
struct patch_func *func; struct patch_func *func;
int ret = 0, i, j; int ret = 0, i, j;
#ifdef NEED_KLP_RELOCS #ifndef HAVE_ELF_RELOCS
struct kpatch_patch_dynrela *kdynrela; struct kpatch_patch_dynrela *kdynrela;
struct patch_reloc *reloc; struct patch_reloc *reloc;
struct klp_reloc *lrelocs, *lreloc; struct klp_reloc *lrelocs, *lreloc;
@ -224,7 +234,7 @@ static int __init patch_init(void)
goto out; goto out;
} }
#ifdef NEED_KLP_RELOCS #ifndef HAVE_ELF_RELOCS
for (kdynrela = __kpatch_dynrelas; for (kdynrela = __kpatch_dynrelas;
kdynrela != __kpatch_dynrelas_end; kdynrela != __kpatch_dynrelas_end;
kdynrela++) { kdynrela++) {
@ -248,10 +258,8 @@ static int __init patch_init(void)
goto out; goto out;
lpatch->mod = THIS_MODULE; lpatch->mod = THIS_MODULE;
lpatch->objs = lobjects; lpatch->objs = lobjects;
#if __powerpc__ #if defined(__powerpc__) && defined(HAVE_IMMEDIATE)
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0))
lpatch->immediate = true; lpatch->immediate = true;
#endif
#endif #endif
i = 0; i = 0;
@ -268,10 +276,7 @@ static int __init patch_init(void)
lfunc = &lfuncs[j]; lfunc = &lfuncs[j];
lfunc->old_name = func->kfunc->name; lfunc->old_name = func->kfunc->name;
lfunc->new_func = (void *)func->kfunc->new_addr; lfunc->new_func = (void *)func->kfunc->new_addr;
#if (( LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0) ) || \ #ifdef HAVE_SYMPOS
( LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0) && \
UTS_UBUNTU_RELEASE_ABI >= 7 ) \
)
lfunc->old_sympos = func->kfunc->sympos; lfunc->old_sympos = func->kfunc->sympos;
#else #else
lfunc->old_addr = func->kfunc->old_addr; lfunc->old_addr = func->kfunc->old_addr;
@ -279,7 +284,7 @@ static int __init patch_init(void)
j++; j++;
} }
#ifdef NEED_KLP_RELOCS #ifndef HAVE_ELF_RELOCS
lrelocs = kzalloc(sizeof(struct klp_reloc) * lrelocs = kzalloc(sizeof(struct klp_reloc) *
(object->relocs_nr+1), GFP_KERNEL); (object->relocs_nr+1), GFP_KERNEL);
if (!lrelocs) if (!lrelocs)
@ -289,21 +294,18 @@ static int __init patch_init(void)
list_for_each_entry(reloc, &object->relocs, list) { list_for_each_entry(reloc, &object->relocs, list) {
lreloc = &lrelocs[j]; lreloc = &lrelocs[j];
lreloc->loc = reloc->kdynrela->dest; lreloc->loc = reloc->kdynrela->dest;
#if (( LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0) ) || \ #ifdef HAVE_SYMPOS
( LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0) && \
UTS_UBUNTU_RELEASE_ABI >= 7 ) \
)
lreloc->sympos = reloc->kdynrela->sympos; lreloc->sympos = reloc->kdynrela->sympos;
#else #else
lreloc->val = reloc->kdynrela->src; lreloc->val = reloc->kdynrela->src;
#endif /* 4.5.0 */ #endif /* HAVE_SYMPOS */
lreloc->type = reloc->kdynrela->type; lreloc->type = reloc->kdynrela->type;
lreloc->name = reloc->kdynrela->name; lreloc->name = reloc->kdynrela->name;
lreloc->addend = reloc->kdynrela->addend; lreloc->addend = reloc->kdynrela->addend;
lreloc->external = reloc->kdynrela->external; lreloc->external = reloc->kdynrela->external;
j++; j++;
} }
#endif /* 4.7.0 */ #endif /* HAVE_ELF_RELOCS */
i++; i++;
} }