[PATCH 6.0 06/20] HID: playstation: stop DualSense output work on remove.

From: Greg Kroah-Hartman
Date: Mon Oct 24 2022 - 07:33:23 EST


From: Roderick Colenbrander <roderick@xxxxxxxxxx>

commit 182934a1e93b17f4edf71f4fcc8d19b19a6fe67a upstream.

Ensure we don't schedule any new output work on removal and wait
for any existing work to complete. If we don't do this e.g. rumble
work can get queued during deletion and we trigger a kernel crash.

Signed-off-by: Roderick Colenbrander <roderick.colenbrander@xxxxxxxx>
CC: stable@xxxxxxxxxxxxxxx
Signed-off-by: Benjamin Tissoires <benjamin.tissoires@xxxxxxxxxx>
Link: https://lore.kernel.org/r/20221010212313.78275-2-roderick.colenbrander@xxxxxxxx
Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
---
drivers/hid/hid-playstation.c | 41 ++++++++++++++++++++++++++++++++++++-----
1 file changed, 36 insertions(+), 5 deletions(-)

--- a/drivers/hid/hid-playstation.c
+++ b/drivers/hid/hid-playstation.c
@@ -46,6 +46,7 @@ struct ps_device {
uint32_t fw_version;

int (*parse_report)(struct ps_device *dev, struct hid_report *report, u8 *data, int size);
+ void (*remove)(struct ps_device *dev);
};

/* Calibration data for playstation motion sensors. */
@@ -174,6 +175,7 @@ struct dualsense {
struct led_classdev player_leds[5];

struct work_struct output_worker;
+ bool output_worker_initialized;
void *output_report_dmabuf;
uint8_t output_seq; /* Sequence number for output report. */
};
@@ -299,6 +301,7 @@ static const struct {int x; int y; } ps_
{0, 0},
};

+static inline void dualsense_schedule_work(struct dualsense *ds);
static void dualsense_set_lightbar(struct dualsense *ds, uint8_t red, uint8_t green, uint8_t blue);

/*
@@ -792,6 +795,7 @@ err_free:
return ret;
}

+
static int dualsense_get_firmware_info(struct dualsense *ds)
{
uint8_t *buf;
@@ -881,7 +885,7 @@ static int dualsense_player_led_set_brig
ds->update_player_leds = true;
spin_unlock_irqrestore(&ds->base.lock, flags);

- schedule_work(&ds->output_worker);
+ dualsense_schedule_work(ds);

return 0;
}
@@ -925,6 +929,16 @@ static void dualsense_init_output_report
}
}

+static inline void dualsense_schedule_work(struct dualsense *ds)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&ds->base.lock, flags);
+ if (ds->output_worker_initialized)
+ schedule_work(&ds->output_worker);
+ spin_unlock_irqrestore(&ds->base.lock, flags);
+}
+
/*
* Helper function to send DualSense output reports. Applies a CRC at the end of a report
* for Bluetooth reports.
@@ -1085,7 +1099,7 @@ static int dualsense_parse_report(struct
spin_unlock_irqrestore(&ps_dev->lock, flags);

/* Schedule updating of microphone state at hardware level. */
- schedule_work(&ds->output_worker);
+ dualsense_schedule_work(ds);
}
ds->last_btn_mic_state = btn_mic_state;

@@ -1200,10 +1214,22 @@ static int dualsense_play_effect(struct
ds->motor_right = effect->u.rumble.weak_magnitude / 256;
spin_unlock_irqrestore(&ds->base.lock, flags);

- schedule_work(&ds->output_worker);
+ dualsense_schedule_work(ds);
return 0;
}

+static void dualsense_remove(struct ps_device *ps_dev)
+{
+ struct dualsense *ds = container_of(ps_dev, struct dualsense, base);
+ unsigned long flags;
+
+ spin_lock_irqsave(&ds->base.lock, flags);
+ ds->output_worker_initialized = false;
+ spin_unlock_irqrestore(&ds->base.lock, flags);
+
+ cancel_work_sync(&ds->output_worker);
+}
+
static int dualsense_reset_leds(struct dualsense *ds)
{
struct dualsense_output_report report;
@@ -1240,7 +1266,7 @@ static void dualsense_set_lightbar(struc
ds->lightbar_blue = blue;
spin_unlock_irqrestore(&ds->base.lock, flags);

- schedule_work(&ds->output_worker);
+ dualsense_schedule_work(ds);
}

static void dualsense_set_player_leds(struct dualsense *ds)
@@ -1263,7 +1289,7 @@ static void dualsense_set_player_leds(st

ds->update_player_leds = true;
ds->player_leds_state = player_ids[player_id];
- schedule_work(&ds->output_worker);
+ dualsense_schedule_work(ds);
}

static struct ps_device *dualsense_create(struct hid_device *hdev)
@@ -1302,7 +1328,9 @@ static struct ps_device *dualsense_creat
ps_dev->battery_capacity = 100; /* initial value until parse_report. */
ps_dev->battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
ps_dev->parse_report = dualsense_parse_report;
+ ps_dev->remove = dualsense_remove;
INIT_WORK(&ds->output_worker, dualsense_output_worker);
+ ds->output_worker_initialized = true;
hid_set_drvdata(hdev, ds);

max_output_report_size = sizeof(struct dualsense_output_report_bt);
@@ -1470,6 +1498,9 @@ static void ps_remove(struct hid_device
ps_devices_list_remove(dev);
ps_device_release_player_id(dev);

+ if (dev->remove)
+ dev->remove(dev);
+
hid_hw_close(hdev);
hid_hw_stop(hdev);
}