hulk inclusion category: cleanup bugzilla: https://gitee.com/openeuler/kernel/issues/I9R2TB
--------------------------------
Struct 'klp_func_list' is used to organize active functions with unidirectional linked list, it is too complicated.
Here rename it as 'actv_func', and organize with 'list_head' to make it clear.
Signed-off-by: Zheng Yejian zhengyejian1@huawei.com --- arch/arm/kernel/livepatch.c | 12 ++--- arch/arm64/kernel/livepatch.c | 12 ++--- arch/powerpc/kernel/livepatch_32.c | 10 ++-- arch/powerpc/kernel/livepatch_64.c | 12 ++--- arch/x86/kernel/livepatch.c | 10 ++-- include/linux/livepatch.h | 10 +--- kernel/livepatch/core.c | 79 +++++++++++++++--------------- 7 files changed, 62 insertions(+), 83 deletions(-)
diff --git a/arch/arm/kernel/livepatch.c b/arch/arm/kernel/livepatch.c index af298045f67f..46adc986eb2e 100644 --- a/arch/arm/kernel/livepatch.c +++ b/arch/arm/kernel/livepatch.c @@ -84,7 +84,7 @@ static bool check_jump_insn(unsigned long func_addr) }
int arch_klp_check_activeness_func(struct klp_patch *patch, int enable, - klp_add_func_t add_func, struct klp_func_list **func_list) + klp_add_func_t add_func, struct list_head *func_list) { int ret; struct klp_object *obj; @@ -92,7 +92,6 @@ int arch_klp_check_activeness_func(struct klp_patch *patch, int enable, struct klp_func *func; unsigned long func_addr = 0; unsigned long func_size; - struct klp_func_list *pcheck = NULL;
for (obj = patch->objs; obj->funcs; obj++) { for (func = obj->funcs; func->old_name; func++) { @@ -141,8 +140,7 @@ int arch_klp_check_activeness_func(struct klp_patch *patch, int enable, IS_ENABLED(CONFIG_LIVEPATCH_BREAKPOINT_NO_STOP_MACHINE) || (func->force == KLP_NORMAL_FORCE) || check_jump_insn(func_addr)) { - ret = add_func(func_list, &pcheck, - func_addr, func_size, + ret = add_func(func_list, func_addr, func_size, func->old_name, func->force); if (ret) return ret; @@ -183,8 +181,7 @@ int arch_klp_check_activeness_func(struct klp_patch *patch, int enable, func_addr = (unsigned long)prev->new_func; func_size = prev->new_size; } - ret = add_func(func_list, &pcheck, - func_addr, func_size, + ret = add_func(func_list, func_addr, func_size, func->old_name, 0); if (ret) return ret; @@ -197,8 +194,7 @@ int arch_klp_check_activeness_func(struct klp_patch *patch, int enable, #endif func_addr = (unsigned long)func->new_func; func_size = func->new_size; - ret = add_func(func_list, &pcheck, - func_addr, func_size, + ret = add_func(func_list, func_addr, func_size, func->old_name, 0); if (ret) return ret; diff --git a/arch/arm64/kernel/livepatch.c b/arch/arm64/kernel/livepatch.c index 71a95489855b..45ac28b0f448 100644 --- a/arch/arm64/kernel/livepatch.c +++ b/arch/arm64/kernel/livepatch.c @@ -77,7 +77,7 @@ static bool check_jump_insn(unsigned long func_addr) }
int arch_klp_check_activeness_func(struct klp_patch *patch, int enable, - klp_add_func_t add_func, struct klp_func_list **func_list) + klp_add_func_t add_func, struct list_head *func_list) { int ret; struct klp_object *obj; @@ -85,7 +85,6 @@ int arch_klp_check_activeness_func(struct klp_patch *patch, int enable, unsigned long func_addr = 0; unsigned long func_size; struct klp_func_node *func_node; - struct klp_func_list *pcheck = NULL;
for (obj = patch->objs; obj->funcs; obj++) { for (func = obj->funcs; func->old_name; func++) { @@ -130,8 +129,7 @@ int arch_klp_check_activeness_func(struct klp_patch *patch, int enable, IS_ENABLED(CONFIG_LIVEPATCH_BREAKPOINT_NO_STOP_MACHINE) || (func->force == KLP_NORMAL_FORCE) || check_jump_insn(func_addr)) { - ret = add_func(func_list, &pcheck, - func_addr, func_size, + ret = add_func(func_list, func_addr, func_size, func->old_name, func->force); if (ret) return ret; @@ -173,8 +171,7 @@ int arch_klp_check_activeness_func(struct klp_patch *patch, int enable, func_addr = (unsigned long)prev->new_func; func_size = prev->new_size; } - ret = add_func(func_list, &pcheck, - func_addr, func_size, + ret = add_func(func_list, func_addr, func_size, func->old_name, 0); if (ret) return ret; @@ -188,8 +185,7 @@ int arch_klp_check_activeness_func(struct klp_patch *patch, int enable,
func_addr = (unsigned long)func->new_func; func_size = func->new_size; - ret = add_func(func_list, &pcheck, - func_addr, func_size, + ret = add_func(func_list, func_addr, func_size, func->old_name, 0); if (ret) return ret; diff --git a/arch/powerpc/kernel/livepatch_32.c b/arch/powerpc/kernel/livepatch_32.c index 16f97475ef93..424c0c972ff7 100644 --- a/arch/powerpc/kernel/livepatch_32.c +++ b/arch/powerpc/kernel/livepatch_32.c @@ -74,7 +74,7 @@ static bool check_jump_insn(unsigned long func_addr) }
int arch_klp_check_activeness_func(struct klp_patch *patch, int enable, - klp_add_func_t add_func, struct klp_func_list **func_list) + klp_add_func_t add_func, struct list_head *func_list) { int ret; struct klp_object *obj; @@ -82,7 +82,6 @@ int arch_klp_check_activeness_func(struct klp_patch *patch, int enable, unsigned long func_addr = 0; unsigned long func_size; struct klp_func_node *func_node; - struct klp_func_list *pcheck = NULL;
for (obj = patch->objs; obj->funcs; obj++) { for (func = obj->funcs; func->old_name; func++) { @@ -131,8 +130,7 @@ int arch_klp_check_activeness_func(struct klp_patch *patch, int enable, IS_ENABLED(CONFIG_LIVEPATCH_BREAKPOINT_NO_STOP_MACHINE) || (func->force == KLP_NORMAL_FORCE) || check_jump_insn(func_addr)) { - ret = add_func(func_list, &pcheck, - func_addr, func_size, + ret = add_func(func_list, func_addr, func_size, func->old_name, func->force); if (ret) return ret; @@ -173,7 +171,7 @@ int arch_klp_check_activeness_func(struct klp_patch *patch, int enable, func_addr = (unsigned long)prev->new_func; func_size = prev->new_size; } - ret = add_func(func_list, &pcheck, func_addr, + ret = add_func(func_list, func_addr, func_size, func->old_name, 0); if (ret) return ret; @@ -186,7 +184,7 @@ int arch_klp_check_activeness_func(struct klp_patch *patch, int enable, #endif func_addr = (unsigned long)func->new_func; func_size = func->new_size; - ret = add_func(func_list, &pcheck, func_addr, + ret = add_func(func_list, func_addr, func_size, func->old_name, 0); if (ret) return ret; diff --git a/arch/powerpc/kernel/livepatch_64.c b/arch/powerpc/kernel/livepatch_64.c index 1dcca852618b..04a0508d09c8 100644 --- a/arch/powerpc/kernel/livepatch_64.c +++ b/arch/powerpc/kernel/livepatch_64.c @@ -79,14 +79,13 @@ static bool check_jump_insn(unsigned long func_addr) }
int arch_klp_check_activeness_func(struct klp_patch *patch, int enable, - klp_add_func_t add_func, struct klp_func_list **func_list) + klp_add_func_t add_func, struct list_head *func_list) { int ret; struct klp_object *obj; struct klp_func *func; unsigned long func_addr, func_size; struct klp_func_node *func_node = NULL; - struct klp_func_list *pcheck = NULL;
for (obj = patch->objs; obj->funcs; obj++) { for (func = obj->funcs; func->old_name; func++) { @@ -134,8 +133,7 @@ int arch_klp_check_activeness_func(struct klp_patch *patch, int enable, IS_ENABLED(CONFIG_LIVEPATCH_BREAKPOINT_NO_STOP_MACHINE) || (func->force == KLP_NORMAL_FORCE) || check_jump_insn(func_addr)) { - ret = add_func(func_list, &pcheck, - func_addr, func_size, + ret = add_func(func_list, func_addr, func_size, func->old_name, func->force); if (ret) return ret; @@ -148,7 +146,7 @@ int arch_klp_check_activeness_func(struct klp_patch *patch, int enable, func_addr = ppc_function_entry( (void *)func->new_func); func_size = func->new_size; - ret = add_func(func_list, &pcheck, func_addr, + ret = add_func(func_list, func_addr, func_size, func->old_name, 0); if (ret) return ret; @@ -174,7 +172,7 @@ int arch_klp_check_activeness_func(struct klp_patch *patch, int enable, */ func_addr = (unsigned long)func->old_func; func_size = func->old_size; - ret = add_func(func_list, &pcheck, func_addr, + ret = add_func(func_list, func_addr, func_size, "OLD_FUNC", 0); if (ret) return ret; @@ -186,7 +184,7 @@ int arch_klp_check_activeness_func(struct klp_patch *patch, int enable,
func_addr = (unsigned long)&func_node->arch_data.trampoline; func_size = sizeof(struct ppc64_klp_btramp_entry); - ret = add_func(func_list, &pcheck, func_addr, + ret = add_func(func_list, func_addr, func_size, "trampoline", 0); if (ret) return ret; diff --git a/arch/x86/kernel/livepatch.c b/arch/x86/kernel/livepatch.c index b630bab1060a..04aa07e95f46 100644 --- a/arch/x86/kernel/livepatch.c +++ b/arch/x86/kernel/livepatch.c @@ -73,7 +73,7 @@ static bool check_jump_insn(unsigned long func_addr) }
int arch_klp_check_activeness_func(struct klp_patch *patch, int enable, - klp_add_func_t add_func, struct klp_func_list **func_list) + klp_add_func_t add_func, struct list_head *func_list) { int ret; struct klp_object *obj; @@ -81,7 +81,6 @@ int arch_klp_check_activeness_func(struct klp_patch *patch, int enable, unsigned long func_addr = 0; unsigned long func_size; struct klp_func_node *func_node = NULL; - struct klp_func_list *pcheck = NULL;
for (obj = patch->objs; obj->funcs; obj++) { for (func = obj->funcs; func->old_name; func++) { @@ -127,8 +126,7 @@ int arch_klp_check_activeness_func(struct klp_patch *patch, int enable, IS_ENABLED(CONFIG_LIVEPATCH_BREAKPOINT_NO_STOP_MACHINE) || (func->force == KLP_NORMAL_FORCE) || check_jump_insn(func_addr)) { - ret = add_func(func_list, &pcheck, - func_addr, func_size, + ret = add_func(func_list, func_addr, func_size, func->old_name, func->force); if (ret) return ret; @@ -167,7 +165,7 @@ int arch_klp_check_activeness_func(struct klp_patch *patch, int enable, func_addr = (unsigned long)prev->new_func; func_size = prev->new_size; } - ret = add_func(func_list, &pcheck, func_addr, + ret = add_func(func_list, func_addr, func_size, func->old_name, 0); if (ret) return ret; @@ -181,7 +179,7 @@ int arch_klp_check_activeness_func(struct klp_patch *patch, int enable,
func_addr = (unsigned long)func->new_func; func_size = func->new_size; - ret = add_func(func_list, &pcheck, func_addr, + ret = add_func(func_list, func_addr, func_size, func->old_name, 0); if (ret) return ret; diff --git a/include/linux/livepatch.h b/include/linux/livepatch.h index 7146989b5fbc..427485f73793 100644 --- a/include/linux/livepatch.h +++ b/include/linux/livepatch.h @@ -259,15 +259,7 @@ int klp_compare_address(unsigned long pc, unsigned long func_addr, void arch_klp_init(void); int klp_module_delete_safety_check(struct module *mod);
-struct klp_func_list { - struct klp_func_list *next; - unsigned long func_addr; - unsigned long func_size; - const char *func_name; - int force; -}; - -typedef int (*klp_add_func_t)(struct klp_func_list **funcs, struct klp_func_list **func, +typedef int (*klp_add_func_t)(struct list_head *func_list, unsigned long func_addr, unsigned long func_size, const char *func_name, int force);
diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c index 3bf53800868e..d47d243499b3 100644 --- a/kernel/livepatch/core.c +++ b/kernel/livepatch/core.c @@ -1346,7 +1346,7 @@ int __weak arch_klp_check_calltrace(bool (*fn)(void *, int *, unsigned long), vo
int __weak arch_klp_check_activeness_func(struct klp_patch *patch, int enable, klp_add_func_t add_func, - struct klp_func_list **func_list) + struct list_head *func_list) { return -EINVAL; } @@ -1361,58 +1361,59 @@ static inline unsigned long klp_size_to_check(unsigned long func_size, return size; }
+struct actv_func { + struct list_head list; + unsigned long func_addr; + unsigned long func_size; + const char *func_name; + int force; +}; + static bool check_func_list(void *data, int *ret, unsigned long pc) { - struct klp_func_list *funcs = (struct klp_func_list *)data; + struct list_head *func_list = (struct list_head *)data; + struct actv_func *func = NULL;
- while (funcs != NULL) { - *ret = klp_compare_address(pc, funcs->func_addr, funcs->func_name, - klp_size_to_check(funcs->func_size, funcs->force)); + list_for_each_entry(func, func_list, list) { + *ret = klp_compare_address(pc, func->func_addr, func->func_name, + klp_size_to_check(func->func_size, func->force)); if (*ret) return false; - funcs = funcs->next; } return true; }
-static int add_func_to_list(struct klp_func_list **funcs, struct klp_func_list **func, - unsigned long func_addr, unsigned long func_size, const char *func_name, - int force) +static int add_func_to_list(struct list_head *func_list, unsigned long func_addr, + unsigned long func_size, const char *func_name, + int force) { - if (*func == NULL) { - *funcs = kzalloc(sizeof(**funcs), GFP_ATOMIC); - if (!(*funcs)) - return -ENOMEM; - *func = *funcs; - } else { - (*func)->next = kzalloc(sizeof(**funcs), GFP_ATOMIC); - if (!(*func)->next) - return -ENOMEM; - *func = (*func)->next; - } - (*func)->func_addr = func_addr; - (*func)->func_size = func_size; - (*func)->func_name = func_name; - (*func)->force = force; - (*func)->next = NULL; + struct actv_func *func = kzalloc(sizeof(struct actv_func), GFP_ATOMIC); + + if (!func) + return -ENOMEM; + func->func_addr = func_addr; + func->func_size = func_size; + func->func_name = func_name; + func->force = force; + list_add_tail(&func->list, func_list); return 0; }
-static void free_func_list(struct klp_func_list **funcs) +static void free_func_list(struct list_head *func_list) { - struct klp_func_list *p; + struct actv_func *func = NULL; + struct actv_func *tmp = NULL;
- while (*funcs != NULL) { - p = *funcs; - *funcs = (*funcs)->next; - kfree(p); + list_for_each_entry_safe(func, tmp, func_list, list) { + list_del(&func->list); + kfree(func); } }
static int klp_check_calltrace(struct klp_patch *patch, int enable) { int ret = 0; - struct klp_func_list *func_list = NULL; + LIST_HEAD(func_list);
ret = arch_klp_check_activeness_func(patch, enable, add_func_to_list, &func_list); if (ret) { @@ -1420,10 +1421,10 @@ static int klp_check_calltrace(struct klp_patch *patch, int enable) goto out; }
- if (!func_list) + if (list_empty(&func_list)) goto out;
- ret = arch_klp_check_calltrace(check_func_list, (void *)func_list); + ret = arch_klp_check_calltrace(check_func_list, (void *)&func_list);
out: free_func_list(&func_list); @@ -2087,7 +2088,7 @@ static void set_tasks_patch_state(int patch_state) put_online_cpus(); }
-static void update_patch_state(struct task_struct *task, struct klp_func_list *func_list) +static void update_patch_state(struct task_struct *task, struct list_head *func_list) { struct rq *rq; struct rq_flags flags; @@ -2115,7 +2116,7 @@ static void check_task_calltrace_ipi(void *func_list) current->patch_state = KLP_PATCHED; }
-static void update_patch_state_ipi(struct klp_func_list *func_list) +static void update_patch_state_ipi(struct list_head *func_list) { unsigned int cpu; unsigned int curr_cpu; @@ -2131,7 +2132,7 @@ static void update_patch_state_ipi(struct klp_func_list *func_list) } #endif
-static void update_tasks_patch_state(struct klp_func_list *func_list) +static void update_tasks_patch_state(struct list_head *func_list) { unsigned int cpu; struct task_struct *g, *task; @@ -2188,7 +2189,7 @@ static bool is_patchable(void)
static int klp_breakpoint_enable_patch(struct klp_patch *patch, int *cnt) { - struct klp_func_list *func_list = NULL; + LIST_HEAD(func_list); int ret = -EINVAL; int i; int retry_cnt = 0; @@ -2204,7 +2205,7 @@ static int klp_breakpoint_enable_patch(struct klp_patch *patch, int *cnt) for (i = 0; i < KLP_RETRY_COUNT; i++) { retry_cnt++;
- update_tasks_patch_state(func_list); + update_tasks_patch_state(&func_list); if (is_patchable()) { arch_klp_code_modify_prepare(); ret = enable_patch(patch, true);