[PATCH 4/9] HID: haptic, multitouch: Fix haptic device double-free and unnecessary allocation

From: Rong Zhang

Date: Sun Nov 01 2026 - 12:43:26 EST


The haptic device is allocated by devm_kzalloc(), which will be freed by
devres on HID device release. At that time, the ff core has already
freed it on input device release previously, causing double-free.

Allocate the haptic device using kzalloc_obj() to ensure its release can
be safely delegates to the ff core.

The allocation code is also moved to hid-haptic.h, so that it can be
replaced by a stub in !CONFIG_HID_HAPTIC builds, eliminating the
meaningless alloc-and-free sequence.

Fixes: 344ff3584957 ("HID: haptic: initialize haptic device")
Signed-off-by: Rong Zhang <i@xxxxxxxx>
---
drivers/hid/hid-haptic.h | 20 ++++++++++++++++++++
drivers/hid/hid-multitouch.c | 39 +++++++++++++++++++++++++++++----------
2 files changed, 49 insertions(+), 10 deletions(-)

diff --git a/drivers/hid/hid-haptic.h b/drivers/hid/hid-haptic.h
index 1c11fcbc76f8..32f3c9304be3 100644
--- a/drivers/hid/hid-haptic.h
+++ b/drivers/hid/hid-haptic.h
@@ -5,7 +5,9 @@
* Copyright (c) 2021 Angela Czubak <acz@xxxxxxxxxxxx>
*/

+#include <linux/err.h>
#include <linux/hid.h>
+#include <linux/slab.h>

#define HID_HAPTIC_ORDINAL_WAVEFORMNONE 1
#define HID_HAPTIC_ORDINAL_WAVEFORMSTOP 2
@@ -54,6 +56,18 @@ struct hid_haptic_device {
};

#if IS_ENABLED(CONFIG_HID_HAPTIC)
+
+static inline struct hid_haptic_device *hid_haptic_alloc(struct hid_device *hdev, gfp_t gfp)
+{
+ struct hid_haptic_device *haptic;
+
+ haptic = kzalloc_obj(struct hid_haptic_device, gfp);
+ if (haptic)
+ haptic->hdev = hdev;
+
+ return haptic;
+}
+
void hid_haptic_feature_mapping(struct hid_device *hdev,
struct hid_haptic_device *haptic,
struct hid_field *field, struct hid_usage
@@ -74,6 +88,12 @@ void hid_haptic_pressure_reset(struct hid_haptic_device *haptic);
void hid_haptic_pressure_increase(struct hid_haptic_device *haptic,
__s32 pressure);
#else
+
+static inline struct hid_haptic_device *hid_haptic_alloc(struct hid_device *hdev, gfp_t gfp)
+{
+ return ERR_PTR(-EOPNOTSUPP);
+}
+
static inline
void hid_haptic_feature_mapping(struct hid_device *hdev,
struct hid_haptic_device *haptic,
diff --git a/drivers/hid/hid-multitouch.c b/drivers/hid/hid-multitouch.c
index d744ccc3e0b0..a1b2c47dd034 100644
--- a/drivers/hid/hid-multitouch.c
+++ b/drivers/hid/hid-multitouch.c
@@ -34,6 +34,7 @@
#include <linux/bitmap.h>
#include <linux/bits.h>
#include <linux/device.h>
+#include <linux/err.h>
#include <linux/hid.h>
#include <linux/module.h>
#include <linux/slab.h>
@@ -2130,11 +2131,18 @@ static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)
dev_err(&hdev->dev, "cannot allocate multitouch data\n");
return -ENOMEM;
}
- td->haptic = devm_kzalloc(&hdev->dev, sizeof(*(td->haptic)), GFP_KERNEL);
- if (!td->haptic)
+
+ td->haptic = hid_haptic_alloc(hdev, GFP_KERNEL);
+ if (IS_ERR(td->haptic)) {
+ ret = PTR_ERR(td->haptic);
+ if (ret == -EOPNOTSUPP)
+ td->haptic = NULL;
+ else
+ return ret;
+ } else if (!td->haptic) {
return -ENOMEM;
+ }

- td->haptic->hdev = hdev;
td->hdev = hdev;
td->mtclass = *mtclass;
td->inputmode_value = MT_INPUTMODE_TOUCHSCREEN;
@@ -2179,12 +2187,14 @@ static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)

ret = hid_parse(hdev);
if (ret != 0)
- return ret;
+ goto out;

if (mtclass->name == MT_CLS_APPLE_TOUCHBAR &&
!hid_find_field(hdev, HID_INPUT_REPORT,
- HID_DG_TOUCHPAD, HID_DG_TRANSDUCER_INDEX))
- return -ENODEV;
+ HID_DG_TOUCHPAD, HID_DG_TRANSDUCER_INDEX)) {
+ ret = -ENODEV;
+ goto out;
+ }

if (mtclass->quirks & MT_QUIRK_FIX_CONST_CONTACT_ID)
mt_fix_const_fields(hdev, HID_DG_CONTACTID);
@@ -2194,7 +2204,7 @@ static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)

ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
if (ret)
- return ret;
+ goto out;

ret = sysfs_create_group(&hdev->dev.kobj, &mt_attribute_group);
if (ret)
@@ -2203,10 +2213,19 @@ static int mt_probe(struct hid_device *hdev, const struct hid_device_id *id)

mt_set_modes(hdev, HID_LATENCY_NORMAL, TOUCHPAD_REPORT_ALL);

- if (!td->is_haptic_touchpad)
- devm_kfree(&hdev->dev, td->haptic);
+ ret = 0;

- return 0;
+out:
+ /*
+ * Don't double free when the haptic device has been delegated to the ff
+ * core.
+ */
+ if (td->haptic && !td->is_haptic_touchpad) {
+ kfree(td->haptic);
+ td->haptic = NULL;
+ }
+
+ return ret;
}

static int mt_suspend(struct hid_device *hdev, pm_message_t state)

--
2.55.0