[PATCH v4 1/9] HID: haptic: Get rid of input_ff_destroy() in error paths

From: Rong Zhang

Date: Tue Nov 24 2026 - 11:46:00 EST


It's painful to have error paths after calling input_ff_create(),
because input_ff_destroy() frees too much things but does not restore
some other things. For example, every error path calling
input_ff_destroy() frees ff->private, while the others don't. This
divergence has caused quite a lot of trouble.

Additionally, the driver's get_device() bail-out path calls module_put()
right before hid_haptic_destroy() calling it again, causing refcount
underflow.

Since commit 2ae35f090d02 ("Input: destroy force-feedback device on
input unregistration"), the HID device is now guaranteed to have a
longer lifetime than the FF device, making get_device()/put_device()
unnecessary.

Due to the driver being unable to build as a module since commit
083a4f3f3cc7 ("HID: Kconfig: Fix build error from CONFIG_HID_HAPTIC"),
try_module_get()/module_put() is needless, too.

Remove the said calls, so that there is no error path after
input_ff_create(), eliminating the call to input_ff_destroy(). In this
manner, we don't need to restore dev->flush and dev->event either.

Fixes: 083a4f3f3cc7 ("HID: Kconfig: Fix build error from CONFIG_HID_HAPTIC")
Cc: stable@xxxxxxxxxxxxxxx
Suggested-by: Dmitry Torokhov <dmitry.torokhov@xxxxxxxxx>
Link: https://msgid.link/f44447354f28c295e79a025bbcab7a4e@xxxxxxxxx
Signed-off-by: Rong Zhang <i@xxxxxxxx>
---
Changes in v4:
- Drop get_device()/put_device() and try_module_get()/module_put()
completely, as they are needless now (thanks Dmitry Torokhov)
---
drivers/hid/hid-haptic.c | 25 -------------------------
1 file changed, 25 deletions(-)

diff --git a/drivers/hid/hid-haptic.c b/drivers/hid/hid-haptic.c
index b7fadfbddc4c..c675e14f06e1 100644
--- a/drivers/hid/hid-haptic.c
+++ b/drivers/hid/hid-haptic.c
@@ -380,12 +380,8 @@ static int hid_haptic_erase(struct input_dev *dev, int effect_id)
static void hid_haptic_destroy(struct ff_device *ff)
{
struct hid_haptic_device *haptic = ff->private;
- struct hid_device *hdev = haptic->hdev;
int r;

- if (hdev)
- put_device(&hdev->dev);
-
kfree(haptic->stop_effect.report_buf);
haptic->stop_effect.report_buf = NULL;

@@ -404,8 +400,6 @@ static void hid_haptic_destroy(struct ff_device *ff)

kfree(haptic->hid_usage_map);
haptic->hid_usage_map = NULL;
-
- module_put(THIS_MODULE);
}

int hid_haptic_init(struct hid_device *hdev,
@@ -419,8 +413,6 @@ int hid_haptic_init(struct hid_device *hdev,
};
const char *prefix = "hid-haptic";
char *name;
- int (*flush)(struct input_dev *dev, struct file *file);
- int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value);

haptic->hdev = hdev;
haptic->max_waveform_id = max(2u, haptic->max_waveform_id);
@@ -499,8 +491,6 @@ int hid_haptic_init(struct hid_device *hdev,

input_set_capability(dev, EV_FF, FF_HAPTIC);

- flush = dev->flush;
- event = dev->event;
ret = input_ff_create(dev, FF_MAX_EFFECTS);
if (ret) {
dev_err(&hdev->dev, "Failed to create ff device.\n");
@@ -513,23 +503,8 @@ int hid_haptic_init(struct hid_device *hdev,
ff->playback = hid_haptic_playback;
ff->erase = hid_haptic_erase;
ff->destroy = hid_haptic_destroy;
- if (!try_module_get(THIS_MODULE)) {
- dev_err(&hdev->dev, "Failed to increase module count.\n");
- goto input_free;
- }
- if (!get_device(&hdev->dev)) {
- dev_err(&hdev->dev, "Failed to get hdev device.\n");
- module_put(THIS_MODULE);
- goto input_free;
- }
return 0;

-input_free:
- input_ff_destroy(dev);
- /* Restore dev flush and event */
- dev->flush = flush;
- dev->event = event;
- return ret;
stop_buffer_free:
kfree(haptic->stop_effect.report_buf);
haptic->stop_effect.report_buf = NULL;

--
2.55.0