Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Input: preallocate memory to hold event values

Preallocate memory for holding event values (input_dev->vals) so that
there is no need to check if it was allocated or not in the event
processing code.

The amount of memory will be adjusted after input device has been fully
set up upon registering device with the input core.

Reviewed-by: Jeff LaBundy <jeff@labundy.com>
Reviewed-by: Benjamin Tissoires <bentiss@kernel.org>
Link: https://lore.kernel.org/r/20240703213756.3375978-7-dmitry.torokhov@gmail.com
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

+44 -17
+44 -17
drivers/input/input.c
··· 323 323 if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event) 324 324 dev->event(dev, type, code, value); 325 325 326 - if (!dev->vals) 327 - return; 328 - 329 326 if (disposition & INPUT_PASS_TO_HANDLERS) { 330 327 struct input_value *v; 331 328 ··· 1982 1985 if (!dev) 1983 1986 return NULL; 1984 1987 1988 + /* 1989 + * Start with space for SYN_REPORT + 7 EV_KEY/EV_MSC events + 2 spare, 1990 + * see input_estimate_events_per_packet(). We will tune the number 1991 + * when we register the device. 1992 + */ 1993 + dev->max_vals = 10; 1994 + dev->vals = kcalloc(dev->max_vals, sizeof(*dev->vals), GFP_KERNEL); 1995 + if (!dev->vals) { 1996 + kfree(dev); 1997 + return NULL; 1998 + } 1999 + 1985 2000 mutex_init(&dev->mutex); 1986 2001 spin_lock_init(&dev->event_lock); 1987 2002 timer_setup(&dev->timer, NULL, 0); ··· 2353 2344 } 2354 2345 EXPORT_SYMBOL_GPL(input_device_enabled); 2355 2346 2347 + static int input_device_tune_vals(struct input_dev *dev) 2348 + { 2349 + struct input_value *vals; 2350 + unsigned int packet_size; 2351 + unsigned int max_vals; 2352 + 2353 + packet_size = input_estimate_events_per_packet(dev); 2354 + if (dev->hint_events_per_packet < packet_size) 2355 + dev->hint_events_per_packet = packet_size; 2356 + 2357 + max_vals = dev->hint_events_per_packet + 2; 2358 + if (dev->max_vals >= max_vals) 2359 + return 0; 2360 + 2361 + vals = kcalloc(max_vals, sizeof(*vals), GFP_KERNEL); 2362 + if (!vals) 2363 + return -ENOMEM; 2364 + 2365 + spin_lock_irq(&dev->event_lock); 2366 + dev->max_vals = max_vals; 2367 + swap(dev->vals, vals); 2368 + spin_unlock_irq(&dev->event_lock); 2369 + 2370 + /* Because of swap() above, this frees the old vals memory */ 2371 + kfree(vals); 2372 + 2373 + return 0; 2374 + } 2375 + 2356 2376 /** 2357 2377 * input_register_device - register device with input core 2358 2378 * @dev: device to be registered ··· 2409 2371 { 2410 2372 struct input_devres *devres = NULL; 2411 2373 struct input_handler *handler; 2412 - unsigned int packet_size; 2413 2374 const char *path; 2414 2375 int error; 2415 2376 ··· 2436 2399 /* Make sure that bitmasks not mentioned in dev->evbit are clean. */ 2437 2400 input_cleanse_bitmasks(dev); 2438 2401 2439 - packet_size = input_estimate_events_per_packet(dev); 2440 - if (dev->hint_events_per_packet < packet_size) 2441 - dev->hint_events_per_packet = packet_size; 2442 - 2443 - dev->max_vals = dev->hint_events_per_packet + 2; 2444 - dev->vals = kcalloc(dev->max_vals, sizeof(*dev->vals), GFP_KERNEL); 2445 - if (!dev->vals) { 2446 - error = -ENOMEM; 2402 + error = input_device_tune_vals(dev); 2403 + if (error) 2447 2404 goto err_devres_free; 2448 - } 2449 2405 2450 2406 /* 2451 2407 * If delay and period are pre-set by the driver, then autorepeating ··· 2458 2428 2459 2429 error = device_add(&dev->dev); 2460 2430 if (error) 2461 - goto err_free_vals; 2431 + goto err_devres_free; 2462 2432 2463 2433 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL); 2464 2434 pr_info("%s as %s\n", ··· 2488 2458 2489 2459 err_device_del: 2490 2460 device_del(&dev->dev); 2491 - err_free_vals: 2492 - kfree(dev->vals); 2493 - dev->vals = NULL; 2494 2461 err_devres_free: 2495 2462 devres_free(devres); 2496 2463 return error;