Input:evdev - Fix to avoid the execution of same instruction in every iteration of for loop.

Started by Shailendra Verma on May 20, 2015 · 2 messages from 2 authors · Last reply May 20, 2015

Subsystems (2)

Maintainers (2)

STALE4214d

open the first message on its own page

Message details and related content
Here in for loop the instruction "len / sizeof(compat_long_t)" is used
as a terminating condition which is executing (being computed) in every
iteration of for loop. Below is the armv7-a architecture assembly code with
similar for loop having the same instruction in condition.Note that the
instruction "lsr r3, r3, #2" i.e Logical Shift Right on behalf of
"len / sizeof(compat_long_t)" is getting executed every time the loop
iterates.

       .syntax unified
        .arch armv7-a
        .eabi_attribute 27, 3
        .fpu vfpv3-d16
        .eabi_attribute 20, 1
        .eabi_attribute 21, 1
        .eabi_attribute 23, 3
        .eabi_attribute 24, 1
        .eabi_attribute 25, 1
        .eabi_attribute 26, 2
        .eabi_attribute 30, 6
        .eabi_attribute 34, 1
        .eabi_attribute 18, 4
        .thumb
        .file   "test.c"
        .section        .rodata
        .align  2
.LC0:
        .ascii  "Shail\000"
        .text
        .align  2
        .global main
        .thumb
        .thumb_func
        .type   main, %function
main:
        @ args = 0, pretend = 0, frame = 8
        @ frame_needed = 1, uses_anonymous_args = 0
        push    {r7, lr}
        sub     sp, sp, #8
        add     r7, sp, #0
        mov     r3, #1000

103 more lines

diff --git a/drivers/input/evdev.c b/drivers/input/evdev.c
index dcc588e..bea576f 100644
--- a/drivers/input/evdev.c
+++ b/drivers/input/evdev.c
@@ -618,7 +618,9 @@ static int bits_to_user(unsigned long *bits, unsigned int maxbit,
 		if (len > maxlen)
 			len = maxlen;
 
-		for (i = 0; i < len / sizeof(compat_long_t); i++)
+		maxlen = len / sizeof(compat_long_t);
+
+		for (i = 0; i < maxlen; i++)
 			if (copy_to_user((compat_long_t __user *) p + i,
 					 (compat_long_t *) bits +
 						i + 1 - ((i % 2) << 1),
-- 
1.7.9.5
Message details and related content
Hi Shailendra,

On Thu, May 21, 2015 at 12:35:46AM +0530, Shailendra Verma wrote:
Here in for loop the instruction "len / sizeof(compat_long_t)" is used
as a terminating condition which is executing (being computed) in every
iteration of for loop. Below is the armv7-a architecture assembly code with
similar for loop having the same instruction in condition.Note that the
instruction "lsr r3, r3, #2" i.e Logical Shift Right on behalf of
"len / sizeof(compat_long_t)" is getting executed every time the loop
iterates.
No, it is not. We compile kernel with -O2 and compiler is smart enough
to optimize such constructs.

Thanks.

-- 
Dmitry
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help