Skip to content

Commit 65de58c

Browse files
jwrdegoededtor
authored andcommitted
Input: goodix - fix race on driver unbind
Because there is no way to detect if the touchscreen has pen support, the driver is allocating and registering the input_pen input_dev on receiving the first pen event. But this means that the input_dev gets allocated after the request_irq() call which means that the devm framework will free it before disabling the irq, leaving a window where the irq handler may run and reference the free-ed input_dev. To fix this move the allocation of the input_pen input_dev to before the request_irq() call, while still only registering it on the first pen event so that the driver does not advertise pen capability on touchscreens without it (most goodix touchscreens do not have pen support). Signed-off-by: Hans de Goede <hdegoede@redhat.com> Link: https://lore.kernel.org/r/20220131143539.109142-4-hdegoede@redhat.com Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
1 parent ae8e80c commit 65de58c

2 files changed

Lines changed: 23 additions & 13 deletions

File tree

drivers/input/touchscreen/goodix.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -297,14 +297,14 @@ static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data)
297297
return -ENOMSG;
298298
}
299299

300-
static struct input_dev *goodix_create_pen_input(struct goodix_ts_data *ts)
300+
static int goodix_create_pen_input(struct goodix_ts_data *ts)
301301
{
302302
struct device *dev = &ts->client->dev;
303303
struct input_dev *input;
304304

305305
input = devm_input_allocate_device(dev);
306306
if (!input)
307-
return NULL;
307+
return -ENOMEM;
308308

309309
input_copy_abs(input, ABS_X, ts->input_dev, ABS_MT_POSITION_X);
310310
input_copy_abs(input, ABS_Y, ts->input_dev, ABS_MT_POSITION_Y);
@@ -331,25 +331,23 @@ static struct input_dev *goodix_create_pen_input(struct goodix_ts_data *ts)
331331
input->id.product = 0x1001;
332332
input->id.version = ts->version;
333333

334-
if (input_register_device(input) != 0) {
335-
input_free_device(input);
336-
return NULL;
337-
}
338-
339-
return input;
334+
ts->input_pen = input;
335+
return 0;
340336
}
341337

342338
static void goodix_ts_report_pen_down(struct goodix_ts_data *ts, u8 *data)
343339
{
344-
int input_x, input_y, input_w;
340+
int input_x, input_y, input_w, error;
345341
u8 key_value;
346342

347-
if (!ts->input_pen) {
348-
ts->input_pen = goodix_create_pen_input(ts);
349-
if (!ts->input_pen)
350-
return;
343+
if (!ts->pen_input_registered) {
344+
error = input_register_device(ts->input_pen);
345+
ts->pen_input_registered = (error == 0) ? 1 : error;
351346
}
352347

348+
if (ts->pen_input_registered < 0)
349+
return;
350+
353351
if (ts->contact_size == 9) {
354352
input_x = get_unaligned_le16(&data[4]);
355353
input_y = get_unaligned_le16(&data[6]);
@@ -1207,6 +1205,17 @@ static int goodix_configure_dev(struct goodix_ts_data *ts)
12071205
return error;
12081206
}
12091207

1208+
/*
1209+
* Create the input_pen device before goodix_request_irq() calls
1210+
* devm_request_threaded_irq() so that the devm framework frees
1211+
* it after disabling the irq.
1212+
* Unfortunately there is no way to detect if the touchscreen has pen
1213+
* support, so registering the dev is delayed till the first pen event.
1214+
*/
1215+
error = goodix_create_pen_input(ts);
1216+
if (error)
1217+
return error;
1218+
12101219
ts->irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
12111220
error = goodix_request_irq(ts);
12121221
if (error) {

drivers/input/touchscreen/goodix.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ struct goodix_ts_data {
9494
u16 version;
9595
bool reset_controller_at_probe;
9696
bool load_cfg_from_disk;
97+
int pen_input_registered;
9798
struct completion firmware_loading_complete;
9899
unsigned long irq_flags;
99100
enum goodix_irq_pin_access_method irq_pin_access_method;

0 commit comments

Comments
 (0)