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
196196 struct klp_ops *ops;
197197 int i;
198198
199− for (i = 0; i < nr_entries; i++) {
200− address = entries[i];
199+ if (klp_target_state == KLP_UNPATCHED) {
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);
201212
202− if (klp_target_state == KLP_UNPATCHED) {
203− /*
204− * Check for the to-be-unpatched function
205− * (the func itself).
206− */
207− func_addr = (unsigned long)func->new_func;
208− func_size = func->new_size;
213+ if (list_is_singular(&ops->func_stack)) {
214+ /* original function */
215+ func_addr = (unsigned long)func->old_func;
216+ func_size = func->old_size;
209217 } else {
210− /*
211− * Check for the to-be-patched function
212− * (the previous func).
213− */
214− ops = klp_find_ops(func->old_func);
215−
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− }
218+ /* previously patched function */
219+ struct klp_func *prev;
220+
221+ prev = list_next_entry(func, stack_node);
222+ func_addr = (unsigned long)prev->new_func;
223+ func_size = prev->new_size;
228224 }
225+ }
226+
227+ for (i = 0; i < nr_entries; i++) {
228+ address = entries[i];
229229
230230 if (address >= func_addr && address < func_addr + func_size)
231231 return -EAGAIN;