Skip to content

Commit 43a365a

Browse files
committed
Input: gpio-keys - switch to using cleanup functions
Start using __free() and guard() primitives to simplify the code and error handling. This makes the code more compact and error handling more robust by ensuring that locks are released in all code paths when control leaves critical section and all allocated memory is freed. Reviewed-by: Hans de Goede <hdegoede@redhat.com> Link: https://lore.kernel.org/r/20240825051627.2848495-6-dmitry.torokhov@gmail.com Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
1 parent 934976f commit 43a365a

1 file changed

Lines changed: 17 additions & 27 deletions

File tree

drivers/input/keyboard/gpio_keys.c

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -245,23 +245,20 @@ static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
245245
{
246246
int n_events = get_n_events_by_type(type);
247247
const unsigned long *bitmap = get_bm_events_by_type(ddata->input, type);
248-
unsigned long *bits;
249248
ssize_t error;
250249
int i;
251250

252-
bits = bitmap_alloc(n_events, GFP_KERNEL);
251+
unsigned long *bits __free(bitmap) = bitmap_alloc(n_events, GFP_KERNEL);
253252
if (!bits)
254253
return -ENOMEM;
255254

256255
error = bitmap_parselist(buf, bits, n_events);
257256
if (error)
258-
goto out;
257+
return error;
259258

260259
/* First validate */
261-
if (!bitmap_subset(bits, bitmap, n_events)) {
262-
error = -EINVAL;
263-
goto out;
264-
}
260+
if (!bitmap_subset(bits, bitmap, n_events))
261+
return -EINVAL;
265262

266263
for (i = 0; i < ddata->pdata->nbuttons; i++) {
267264
struct gpio_button_data *bdata = &ddata->data[i];
@@ -271,12 +268,11 @@ static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
271268

272269
if (test_bit(*bdata->code, bits) &&
273270
!bdata->button->can_disable) {
274-
error = -EINVAL;
275-
goto out;
271+
return -EINVAL;
276272
}
277273
}
278274

279-
mutex_lock(&ddata->disable_lock);
275+
guard(mutex)(&ddata->disable_lock);
280276

281277
for (i = 0; i < ddata->pdata->nbuttons; i++) {
282278
struct gpio_button_data *bdata = &ddata->data[i];
@@ -290,11 +286,7 @@ static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
290286
gpio_keys_enable_button(bdata);
291287
}
292288

293-
mutex_unlock(&ddata->disable_lock);
294-
295-
out:
296-
bitmap_free(bits);
297-
return error;
289+
return 0;
298290
}
299291

300292
#define ATTR_SHOW_FN(name, type, only_disabled) \
@@ -470,11 +462,10 @@ static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
470462
{
471463
struct gpio_button_data *bdata = dev_id;
472464
struct input_dev *input = bdata->input;
473-
unsigned long flags;
474465

475466
BUG_ON(irq != bdata->irq);
476467

477-
spin_lock_irqsave(&bdata->lock, flags);
468+
guard(spinlock_irqsave)(&bdata->lock);
478469

479470
if (!bdata->key_pressed) {
480471
if (bdata->button->wakeup)
@@ -497,7 +488,6 @@ static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
497488
ms_to_ktime(bdata->release_delay),
498489
HRTIMER_MODE_REL_HARD);
499490
out:
500-
spin_unlock_irqrestore(&bdata->lock, flags);
501491
return IRQ_HANDLED;
502492
}
503493

@@ -1062,10 +1052,10 @@ static int gpio_keys_suspend(struct device *dev)
10621052
if (error)
10631053
return error;
10641054
} else {
1065-
mutex_lock(&input->mutex);
1055+
guard(mutex)(&input->mutex);
1056+
10661057
if (input_device_enabled(input))
10671058
gpio_keys_close(input);
1068-
mutex_unlock(&input->mutex);
10691059
}
10701060

10711061
return 0;
@@ -1075,20 +1065,20 @@ static int gpio_keys_resume(struct device *dev)
10751065
{
10761066
struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
10771067
struct input_dev *input = ddata->input;
1078-
int error = 0;
1068+
int error;
10791069

10801070
if (device_may_wakeup(dev)) {
10811071
gpio_keys_disable_wakeup(ddata);
10821072
} else {
1083-
mutex_lock(&input->mutex);
1084-
if (input_device_enabled(input))
1073+
guard(mutex)(&input->mutex);
1074+
1075+
if (input_device_enabled(input)) {
10851076
error = gpio_keys_open(input);
1086-
mutex_unlock(&input->mutex);
1077+
if (error)
1078+
return error;
1079+
}
10871080
}
10881081

1089-
if (error)
1090-
return error;
1091-
10921082
gpio_keys_report_state(ddata);
10931083
return 0;
10941084
}

0 commit comments

Comments
 (0)