Skip to content

Commit fabeb16

Browse files
kempniudtor
authored andcommitted
Input: sparse-keymap - use a managed allocation for keymap copy
Some platform drivers use devm_input_allocate_device() together with sparse_keymap_setup() in their .probe callbacks. While using the former simplifies error handling, using the latter necessitates calling sparse_keymap_free() in the error path and upon module unloading to avoid leaking the copy of the keymap allocated by sparse_keymap_setup(). To help prevent such leaks and enable simpler error handling, make sparse_keymap_setup() use devm_kmemdup() to create the keymap copy so that it gets automatically freed. This works for both managed and non-managed input devices as the keymap is freed after the last reference to the input device is dropped. Note that actions previously taken by sparse_keymap_free(), i.e. taking the input device's mutex and zeroing its keycode and keycodemax fields, are now redundant because the managed keymap will always be freed after the input device is unregistered. Signed-off-by: Michał Kępień <kernel@kempniu.pl> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
1 parent c470abd commit fabeb16

1 file changed

Lines changed: 9 additions & 30 deletions

File tree

‎drivers/input/sparse-keymap.c‎

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,12 @@ static int sparse_keymap_setkeycode(struct input_dev *dev,
160160
* @keymap: Keymap in form of array of &key_entry structures ending
161161
* with %KE_END type entry
162162
* @setup: Function that can be used to adjust keymap entries
163-
* depending on device's deeds, may be %NULL
163+
* depending on device's needs, may be %NULL
164164
*
165165
* The function calculates size and allocates copy of the original
166166
* keymap after which sets up input device event bits appropriately.
167-
* Before destroying input device allocated keymap should be freed
168-
* with a call to sparse_keymap_free().
167+
* The allocated copy of the keymap is automatically freed when it
168+
* is no longer needed.
169169
*/
170170
int sparse_keymap_setup(struct input_dev *dev,
171171
const struct key_entry *keymap,
@@ -180,19 +180,18 @@ int sparse_keymap_setup(struct input_dev *dev,
180180
for (e = keymap; e->type != KE_END; e++)
181181
map_size++;
182182

183-
map = kcalloc(map_size, sizeof(struct key_entry), GFP_KERNEL);
183+
map = devm_kmemdup(&dev->dev, keymap, map_size * sizeof(*map),
184+
GFP_KERNEL);
184185
if (!map)
185186
return -ENOMEM;
186187

187-
memcpy(map, keymap, map_size * sizeof(struct key_entry));
188-
189188
for (i = 0; i < map_size; i++) {
190189
entry = &map[i];
191190

192191
if (setup) {
193192
error = setup(dev, entry);
194193
if (error)
195-
goto err_out;
194+
return error;
196195
}
197196

198197
switch (entry->type) {
@@ -221,40 +220,20 @@ int sparse_keymap_setup(struct input_dev *dev,
221220
dev->setkeycode = sparse_keymap_setkeycode;
222221

223222
return 0;
224-
225-
err_out:
226-
kfree(map);
227-
return error;
228223
}
229224
EXPORT_SYMBOL(sparse_keymap_setup);
230225

231226
/**
232227
* sparse_keymap_free - free memory allocated for sparse keymap
233228
* @dev: Input device using sparse keymap
234229
*
235-
* This function is used to free memory allocated by sparse keymap
230+
* This function used to free memory allocated by sparse keymap
236231
* in an input device that was set up by sparse_keymap_setup().
237-
* NOTE: It is safe to cal this function while input device is
238-
* still registered (however the drivers should care not to try to
239-
* use freed keymap and thus have to shut off interrupts/polling
240-
* before freeing the keymap).
232+
* Since sparse_keymap_setup() now uses a managed allocation for the
233+
* keymap copy, use of this function is deprecated.
241234
*/
242235
void sparse_keymap_free(struct input_dev *dev)
243236
{
244-
unsigned long flags;
245-
246-
/*
247-
* Take event lock to prevent racing with input_get_keycode()
248-
* and input_set_keycode() if we are called while input device
249-
* is still registered.
250-
*/
251-
spin_lock_irqsave(&dev->event_lock, flags);
252-
253-
kfree(dev->keycode);
254-
dev->keycode = NULL;
255-
dev->keycodemax = 0;
256-
257-
spin_unlock_irqrestore(&dev->event_lock, flags);
258237
}
259238
EXPORT_SYMBOL(sparse_keymap_free);
260239

0 commit comments

Comments
 (0)