Input: do not pass injected events back to the originating handler

Sometimes input handlers (as opposed to input devices) have a need to
inject (or re-inject) events back into input core. For example sysrq
filter may want to inject previously suppressed Alt-SysRq so that user
can take a screen print. In this case we do not want to pass such events
back to the same same handler that injected them to avoid loops.

Signed-off-by: Dmitry Torokhov <dtor@mail.ru>

+26 -11
+26 -11
drivers/input/input.c
··· 74 * dev->event_lock held and interrupts disabled. 75 */ 76 static void input_pass_event(struct input_dev *dev, 77 unsigned int type, unsigned int code, int value) 78 { 79 struct input_handler *handler; ··· 93 continue; 94 95 handler = handle->handler; 96 if (!handler->filter) { 97 if (filtered) 98 break; ··· 131 if (test_bit(dev->repeat_key, dev->key) && 132 is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) { 133 134 - input_pass_event(dev, EV_KEY, dev->repeat_key, 2); 135 136 if (dev->sync) { 137 /* ··· 140 * Otherwise assume that the driver will send 141 * SYN_REPORT once it's done. 142 */ 143 - input_pass_event(dev, EV_SYN, SYN_REPORT, 1); 144 } 145 146 if (dev->rep[REP_PERIOD]) ··· 173 #define INPUT_PASS_TO_ALL (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE) 174 175 static int input_handle_abs_event(struct input_dev *dev, 176 unsigned int code, int *pval) 177 { 178 bool is_mt_event; ··· 217 /* Flush pending "slot" event */ 218 if (is_mt_event && dev->slot != input_abs_get_val(dev, ABS_MT_SLOT)) { 219 input_abs_set_val(dev, ABS_MT_SLOT, dev->slot); 220 - input_pass_event(dev, EV_ABS, ABS_MT_SLOT, dev->slot); 221 } 222 223 return INPUT_PASS_TO_HANDLERS; 224 } 225 226 static void input_handle_event(struct input_dev *dev, 227 unsigned int type, unsigned int code, int value) 228 { 229 int disposition = INPUT_IGNORE_EVENT; ··· 278 279 case EV_ABS: 280 if (is_event_supported(code, dev->absbit, ABS_MAX)) 281 - disposition = input_handle_abs_event(dev, code, &value); 282 283 break; 284 ··· 337 dev->event(dev, type, code, value); 338 339 if (disposition & INPUT_PASS_TO_HANDLERS) 340 - input_pass_event(dev, type, code, value); 341 } 342 343 /** ··· 366 367 spin_lock_irqsave(&dev->event_lock, flags); 368 add_input_randomness(type, code, value); 369 - input_handle_event(dev, type, code, value); 370 spin_unlock_irqrestore(&dev->event_lock, flags); 371 } 372 } ··· 396 rcu_read_lock(); 397 grab = rcu_dereference(dev->grab); 398 if (!grab || grab == handle) 399 - input_handle_event(dev, type, code, value); 400 rcu_read_unlock(); 401 402 spin_unlock_irqrestore(&dev->event_lock, flags); ··· 610 for (code = 0; code <= KEY_MAX; code++) { 611 if (is_event_supported(code, dev->keybit, KEY_MAX) && 612 __test_and_clear_bit(code, dev->key)) { 613 - input_pass_event(dev, EV_KEY, code, 0); 614 } 615 } 616 - input_pass_event(dev, EV_SYN, SYN_REPORT, 1); 617 } 618 } 619 ··· 888 !is_event_supported(old_keycode, dev->keybit, KEY_MAX) && 889 __test_and_clear_bit(old_keycode, dev->key)) { 890 891 - input_pass_event(dev, EV_KEY, old_keycode, 0); 892 if (dev->sync) 893 - input_pass_event(dev, EV_SYN, SYN_REPORT, 1); 894 } 895 896 out:
··· 74 * dev->event_lock held and interrupts disabled. 75 */ 76 static void input_pass_event(struct input_dev *dev, 77 + struct input_handler *src_handler, 78 unsigned int type, unsigned int code, int value) 79 { 80 struct input_handler *handler; ··· 92 continue; 93 94 handler = handle->handler; 95 + 96 + /* 97 + * If this is the handler that injected this 98 + * particular event we want to skip it to avoid 99 + * filters firing again and again. 100 + */ 101 + if (handler == src_handler) 102 + continue; 103 + 104 if (!handler->filter) { 105 if (filtered) 106 break; ··· 121 if (test_bit(dev->repeat_key, dev->key) && 122 is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) { 123 124 + input_pass_event(dev, NULL, EV_KEY, dev->repeat_key, 2); 125 126 if (dev->sync) { 127 /* ··· 130 * Otherwise assume that the driver will send 131 * SYN_REPORT once it's done. 132 */ 133 + input_pass_event(dev, NULL, EV_SYN, SYN_REPORT, 1); 134 } 135 136 if (dev->rep[REP_PERIOD]) ··· 163 #define INPUT_PASS_TO_ALL (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE) 164 165 static int input_handle_abs_event(struct input_dev *dev, 166 + struct input_handler *src_handler, 167 unsigned int code, int *pval) 168 { 169 bool is_mt_event; ··· 206 /* Flush pending "slot" event */ 207 if (is_mt_event && dev->slot != input_abs_get_val(dev, ABS_MT_SLOT)) { 208 input_abs_set_val(dev, ABS_MT_SLOT, dev->slot); 209 + input_pass_event(dev, src_handler, 210 + EV_ABS, ABS_MT_SLOT, dev->slot); 211 } 212 213 return INPUT_PASS_TO_HANDLERS; 214 } 215 216 static void input_handle_event(struct input_dev *dev, 217 + struct input_handler *src_handler, 218 unsigned int type, unsigned int code, int value) 219 { 220 int disposition = INPUT_IGNORE_EVENT; ··· 265 266 case EV_ABS: 267 if (is_event_supported(code, dev->absbit, ABS_MAX)) 268 + disposition = input_handle_abs_event(dev, src_handler, 269 + code, &value); 270 271 break; 272 ··· 323 dev->event(dev, type, code, value); 324 325 if (disposition & INPUT_PASS_TO_HANDLERS) 326 + input_pass_event(dev, src_handler, type, code, value); 327 } 328 329 /** ··· 352 353 spin_lock_irqsave(&dev->event_lock, flags); 354 add_input_randomness(type, code, value); 355 + input_handle_event(dev, NULL, type, code, value); 356 spin_unlock_irqrestore(&dev->event_lock, flags); 357 } 358 } ··· 382 rcu_read_lock(); 383 grab = rcu_dereference(dev->grab); 384 if (!grab || grab == handle) 385 + input_handle_event(dev, handle->handler, 386 + type, code, value); 387 rcu_read_unlock(); 388 389 spin_unlock_irqrestore(&dev->event_lock, flags); ··· 595 for (code = 0; code <= KEY_MAX; code++) { 596 if (is_event_supported(code, dev->keybit, KEY_MAX) && 597 __test_and_clear_bit(code, dev->key)) { 598 + input_pass_event(dev, NULL, EV_KEY, code, 0); 599 } 600 } 601 + input_pass_event(dev, NULL, EV_SYN, SYN_REPORT, 1); 602 } 603 } 604 ··· 873 !is_event_supported(old_keycode, dev->keybit, KEY_MAX) && 874 __test_and_clear_bit(old_keycode, dev->key)) { 875 876 + input_pass_event(dev, NULL, EV_KEY, old_keycode, 0); 877 if (dev->sync) 878 + input_pass_event(dev, NULL, EV_SYN, SYN_REPORT, 1); 879 } 880 881 out: