History

livepatch: Move the result-invariant calculation out of the loop

The calculation results of the variables 'func_addr' and 'func_size' are not affected by the for loop and do not change due to the changes of entries[i]. The performance can be improved by moving it outside the loop. No functional change.

Zhen Lei committed 4 years ago

Commit 53910ef1 parent 4de65c51 file · +27 −27Browse files

Showing the changes to kernel/livepatch (1 of 1 file). Show all

Unified · Split

kernel/livepatch/transition.c+27−27
@@ -196,36 +196,36 @@ static int klp_check_stack_func(struct klp_func *func, unsigned long *entries, · 195 unmodified lines
196 struct klp_ops *ops;196 struct klp_ops *ops;
197 int i;197 int i;
198 198
199 for (i = 0; i < nr_entries; i++) {199 if (klp_target_state == KLP_UNPATCHED) {
200 address = entries[i];200 /*
201 * Check for the to-be-unpatched function
202 * (the func itself).
203 */
204 func_addr = (unsigned long)func->new_func;
205 func_size = func->new_size;
206 } else {
207 /*
208 * Check for the to-be-patched function
209 * (the previous func).
210 */
211 ops = klp_find_ops(func->old_func);
201 212
202 if (klp_target_state == KLP_UNPATCHED) {213 if (list_is_singular(&ops->func_stack)) {
203 /*214 /* original function */
204 * Check for the to-be-unpatched function215 func_addr = (unsigned long)func->old_func;
205 * (the func itself).216 func_size = func->old_size;
206 */
207 func_addr = (unsigned long)func->new_func;
208 func_size = func->new_size;
209 } else {217 } else {
210 /*218 /* previously patched function */
211 * Check for the to-be-patched function219 struct klp_func *prev;
212 * (the previous func).220
213 */221 prev = list_next_entry(func, stack_node);
214 ops = klp_find_ops(func->old_func);222 func_addr = (unsigned long)prev->new_func;
215 223 func_size = prev->new_size;
216 if (list_is_singular(&ops->func_stack)) {
217 /* original function */
218 func_addr = (unsigned long)func->old_func;
219 func_size = func->old_size;
220 } else {
221 /* previously patched function */
222 struct klp_func *prev;
223
224 prev = list_next_entry(func, stack_node);
225 func_addr = (unsigned long)prev->new_func;
226 func_size = prev->new_size;
227 }
228 }224 }
225 }
226
227 for (i = 0; i < nr_entries; i++) {
228 address = entries[i];
229 229
230 if (address >= func_addr && address < func_addr + func_size)230 if (address >= func_addr && address < func_addr + func_size)
231 return -EAGAIN;231 return -EAGAIN;