Input: fix regression when re-registering input handlers

Commit d469647bafd9 ("Input: simplify event handling logic") introduced
code that would set handler->events() method to either
input_handler_events_filter() or input_handler_events_default() or
input_handler_events_null(), depending on the kind of input handler
(a filter or a regular one) we are dealing with. Unfortunately this
breaks cases when we try to re-register the same filter (as is the case
with sysrq handler): after initial registration the handler will have 2
event handling methods defined, and will run afoul of the check in
input_handler_check_methods():

input: input_handler_check_methods: only one event processing method can be defined (sysrq)
sysrq: Failed to register input handler, error -22

Fix this by adding handle_events() method to input_handle structure and
setting it up when registering a new input handle according to event
handling methods defined in associated input_handler structure, thus
avoiding modifying the input_handler structure.

Reported-by: "Ned T. Crigler" <crigler@gmail.com>
Reported-by: Christian Heusel <christian@heusel.eu>
Tested-by: "Ned T. Crigler" <crigler@gmail.com>
Tested-by: Peter Seiderer <ps.report@gmx.net>
Fixes: d469647bafd9 ("Input: simplify event handling logic")
Link: https://lore.kernel.org/r/Zx2iQp6csn42PJA7@xavtug
Cc: stable@vger.kernel.org
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

+82 -62
+73 -61
drivers/input/input.c
··· 119 119 120 120 handle = rcu_dereference(dev->grab); 121 121 if (handle) { 122 - count = handle->handler->events(handle, vals, count); 122 + count = handle->handle_events(handle, vals, count); 123 123 } else { 124 124 list_for_each_entry_rcu(handle, &dev->h_list, d_node) 125 125 if (handle->open) { 126 - count = handle->handler->events(handle, vals, 127 - count); 126 + count = handle->handle_events(handle, vals, 127 + count); 128 128 if (!count) 129 129 break; 130 130 } ··· 2537 2537 return 0; 2538 2538 } 2539 2539 2540 - /* 2541 - * An implementation of input_handler's events() method that simply 2542 - * invokes handler->event() method for each event one by one. 2543 - */ 2544 - static unsigned int input_handler_events_default(struct input_handle *handle, 2545 - struct input_value *vals, 2546 - unsigned int count) 2547 - { 2548 - struct input_handler *handler = handle->handler; 2549 - struct input_value *v; 2550 - 2551 - for (v = vals; v != vals + count; v++) 2552 - handler->event(handle, v->type, v->code, v->value); 2553 - 2554 - return count; 2555 - } 2556 - 2557 - /* 2558 - * An implementation of input_handler's events() method that invokes 2559 - * handler->filter() method for each event one by one and removes events 2560 - * that were filtered out from the "vals" array. 2561 - */ 2562 - static unsigned int input_handler_events_filter(struct input_handle *handle, 2563 - struct input_value *vals, 2564 - unsigned int count) 2565 - { 2566 - struct input_handler *handler = handle->handler; 2567 - struct input_value *end = vals; 2568 - struct input_value *v; 2569 - 2570 - for (v = vals; v != vals + count; v++) { 2571 - if (handler->filter(handle, v->type, v->code, v->value)) 2572 - continue; 2573 - if (end != v) 2574 - *end = *v; 2575 - end++; 2576 - } 2577 - 2578 - return end - vals; 2579 - } 2580 - 2581 - /* 2582 - * An implementation of input_handler's events() method that does nothing. 2583 - */ 2584 - static unsigned int input_handler_events_null(struct input_handle *handle, 2585 - struct input_value *vals, 2586 - unsigned int count) 2587 - { 2588 - return count; 2589 - } 2590 - 2591 2540 /** 2592 2541 * input_register_handler - register a new input handler 2593 2542 * @handler: handler to be registered ··· 2555 2606 return error; 2556 2607 2557 2608 INIT_LIST_HEAD(&handler->h_list); 2558 - 2559 - if (handler->filter) 2560 - handler->events = input_handler_events_filter; 2561 - else if (handler->event) 2562 - handler->events = input_handler_events_default; 2563 - else if (!handler->events) 2564 - handler->events = input_handler_events_null; 2565 2609 2566 2610 error = mutex_lock_interruptible(&input_mutex); 2567 2611 if (error) ··· 2629 2687 } 2630 2688 EXPORT_SYMBOL(input_handler_for_each_handle); 2631 2689 2690 + /* 2691 + * An implementation of input_handle's handle_events() method that simply 2692 + * invokes handler->event() method for each event one by one. 2693 + */ 2694 + static unsigned int input_handle_events_default(struct input_handle *handle, 2695 + struct input_value *vals, 2696 + unsigned int count) 2697 + { 2698 + struct input_handler *handler = handle->handler; 2699 + struct input_value *v; 2700 + 2701 + for (v = vals; v != vals + count; v++) 2702 + handler->event(handle, v->type, v->code, v->value); 2703 + 2704 + return count; 2705 + } 2706 + 2707 + /* 2708 + * An implementation of input_handle's handle_events() method that invokes 2709 + * handler->filter() method for each event one by one and removes events 2710 + * that were filtered out from the "vals" array. 2711 + */ 2712 + static unsigned int input_handle_events_filter(struct input_handle *handle, 2713 + struct input_value *vals, 2714 + unsigned int count) 2715 + { 2716 + struct input_handler *handler = handle->handler; 2717 + struct input_value *end = vals; 2718 + struct input_value *v; 2719 + 2720 + for (v = vals; v != vals + count; v++) { 2721 + if (handler->filter(handle, v->type, v->code, v->value)) 2722 + continue; 2723 + if (end != v) 2724 + *end = *v; 2725 + end++; 2726 + } 2727 + 2728 + return end - vals; 2729 + } 2730 + 2731 + /* 2732 + * An implementation of input_handle's handle_events() method that does nothing. 2733 + */ 2734 + static unsigned int input_handle_events_null(struct input_handle *handle, 2735 + struct input_value *vals, 2736 + unsigned int count) 2737 + { 2738 + return count; 2739 + } 2740 + 2741 + /* 2742 + * Sets up appropriate handle->event_handler based on the input_handler 2743 + * associated with the handle. 2744 + */ 2745 + static void input_handle_setup_event_handler(struct input_handle *handle) 2746 + { 2747 + struct input_handler *handler = handle->handler; 2748 + 2749 + if (handler->filter) 2750 + handle->handle_events = input_handle_events_filter; 2751 + else if (handler->event) 2752 + handle->handle_events = input_handle_events_default; 2753 + else if (handler->events) 2754 + handle->handle_events = handler->events; 2755 + else 2756 + handle->handle_events = input_handle_events_null; 2757 + } 2758 + 2632 2759 /** 2633 2760 * input_register_handle - register a new input handle 2634 2761 * @handle: handle to register ··· 2715 2704 struct input_dev *dev = handle->dev; 2716 2705 int error; 2717 2706 2707 + input_handle_setup_event_handler(handle); 2718 2708 /* 2719 2709 * We take dev->mutex here to prevent race with 2720 2710 * input_release_device().
+9 -1
include/linux/input.h
··· 339 339 * @name: name given to the handle by handler that created it 340 340 * @dev: input device the handle is attached to 341 341 * @handler: handler that works with the device through this handle 342 + * @handle_events: event sequence handler. It is set up by the input core 343 + * according to event handling method specified in the @handler. See 344 + * input_handle_setup_event_handler(). 345 + * This method is being called by the input core with interrupts disabled 346 + * and dev->event_lock spinlock held and so it may not sleep. 342 347 * @d_node: used to put the handle on device's list of attached handles 343 348 * @h_node: used to put the handle on handler's list of handles from which 344 349 * it gets events 345 350 */ 346 351 struct input_handle { 347 - 348 352 void *private; 349 353 350 354 int open; ··· 356 352 357 353 struct input_dev *dev; 358 354 struct input_handler *handler; 355 + 356 + unsigned int (*handle_events)(struct input_handle *handle, 357 + struct input_value *vals, 358 + unsigned int count); 359 359 360 360 struct list_head d_node; 361 361 struct list_head h_node;