Skip to content

Commit 0cd5877

Browse files
committed
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>
1 parent 3544cf5 commit 0cd5877

1 file changed

Lines changed: 44 additions & 17 deletions

File tree

drivers/input/input.c

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,6 @@ static void input_event_dispose(struct input_dev *dev, int disposition,
323323
if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
324324
dev->event(dev, type, code, value);
325325

326-
if (!dev->vals)
327-
return;
328-
329326
if (disposition & INPUT_PASS_TO_HANDLERS) {
330327
struct input_value *v;
331328

@@ -1985,6 +1982,18 @@ struct input_dev *input_allocate_device(void)
19851982
if (!dev)
19861983
return NULL;
19871984

1985+
/*
1986+
* Start with space for SYN_REPORT + 7 EV_KEY/EV_MSC events + 2 spare,
1987+
* see input_estimate_events_per_packet(). We will tune the number
1988+
* when we register the device.
1989+
*/
1990+
dev->max_vals = 10;
1991+
dev->vals = kcalloc(dev->max_vals, sizeof(*dev->vals), GFP_KERNEL);
1992+
if (!dev->vals) {
1993+
kfree(dev);
1994+
return NULL;
1995+
}
1996+
19881997
mutex_init(&dev->mutex);
19891998
spin_lock_init(&dev->event_lock);
19901999
timer_setup(&dev->timer, NULL, 0);
@@ -2344,6 +2353,35 @@ bool input_device_enabled(struct input_dev *dev)
23442353
}
23452354
EXPORT_SYMBOL_GPL(input_device_enabled);
23462355

2356+
static int input_device_tune_vals(struct input_dev *dev)
2357+
{
2358+
struct input_value *vals;
2359+
unsigned int packet_size;
2360+
unsigned int max_vals;
2361+
2362+
packet_size = input_estimate_events_per_packet(dev);
2363+
if (dev->hint_events_per_packet < packet_size)
2364+
dev->hint_events_per_packet = packet_size;
2365+
2366+
max_vals = dev->hint_events_per_packet + 2;
2367+
if (dev->max_vals >= max_vals)
2368+
return 0;
2369+
2370+
vals = kcalloc(max_vals, sizeof(*vals), GFP_KERNEL);
2371+
if (!vals)
2372+
return -ENOMEM;
2373+
2374+
spin_lock_irq(&dev->event_lock);
2375+
dev->max_vals = max_vals;
2376+
swap(dev->vals, vals);
2377+
spin_unlock_irq(&dev->event_lock);
2378+
2379+
/* Because of swap() above, this frees the old vals memory */
2380+
kfree(vals);
2381+
2382+
return 0;
2383+
}
2384+
23472385
/**
23482386
* input_register_device - register device with input core
23492387
* @dev: device to be registered
@@ -2371,7 +2409,6 @@ int input_register_device(struct input_dev *dev)
23712409
{
23722410
struct input_devres *devres = NULL;
23732411
struct input_handler *handler;
2374-
unsigned int packet_size;
23752412
const char *path;
23762413
int error;
23772414

@@ -2399,16 +2436,9 @@ int input_register_device(struct input_dev *dev)
23992436
/* Make sure that bitmasks not mentioned in dev->evbit are clean. */
24002437
input_cleanse_bitmasks(dev);
24012438

2402-
packet_size = input_estimate_events_per_packet(dev);
2403-
if (dev->hint_events_per_packet < packet_size)
2404-
dev->hint_events_per_packet = packet_size;
2405-
2406-
dev->max_vals = dev->hint_events_per_packet + 2;
2407-
dev->vals = kcalloc(dev->max_vals, sizeof(*dev->vals), GFP_KERNEL);
2408-
if (!dev->vals) {
2409-
error = -ENOMEM;
2439+
error = input_device_tune_vals(dev);
2440+
if (error)
24102441
goto err_devres_free;
2411-
}
24122442

24132443
/*
24142444
* If delay and period are pre-set by the driver, then autorepeating
@@ -2428,7 +2458,7 @@ int input_register_device(struct input_dev *dev)
24282458

24292459
error = device_add(&dev->dev);
24302460
if (error)
2431-
goto err_free_vals;
2461+
goto err_devres_free;
24322462

24332463
path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
24342464
pr_info("%s as %s\n",
@@ -2458,9 +2488,6 @@ int input_register_device(struct input_dev *dev)
24582488

24592489
err_device_del:
24602490
device_del(&dev->dev);
2461-
err_free_vals:
2462-
kfree(dev->vals);
2463-
dev->vals = NULL;
24642491
err_devres_free:
24652492
devres_free(devres);
24662493
return error;

0 commit comments

Comments
 (0)