From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796388983; b=e4o80ZQyidwzCvaFreopbMY3n1x8Dd9cpoKmPjOMrkMjkpa7H/sGoluOmgSyUqF+lFXi w/5ICuJKSGq6gnEaKYiJTIQdiYk3k0g4eYaKGG+9HU5CHpC1CiAY8JaBEz0zAAI1kKDyM TLB9fj4UYG3bHgA2hQY4RjglawQ1+3pHjLy5DQZ45oXG/2AihldTEz5yllYm8DPxcyCif YfHAMfZvNmkeRb+7UG5cPQ6z9Oaj8vY78qrO/eEaYE0Ie1i1kstMR12F5WDqcwK+U1dBf vQcscRPe33kNCj2AyeX2HusmhZpdPUR0LW3pfwGbB3GtyMkH4a/2j87PVQMi9fRzuhw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796388983; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=ztCpTwnQKZ1wUL/bU0RLrfD1eB+HeN7MBGnAGv3oIf8=; b=Je9/L1Xy32ztpbabbyb3gdx4nKnnf4nRSw+045CqZjOES4KhiF6wseqMgMSHvzPDpFmP qDhS7Rn0UIUa/Eeanz0oh8gxH5moeAOVEB5R2aqqVIRNwHTfNUek5EZc7wYPGX31O+h9Y AXT8wnRwhpPuGKfiPbPUKRRy+siPbK7diDFIZvqHllLHKWwIPQTVibbbso0X6cq3oci5W GAF8QVw0YrG/0KTdT4EvyuPq2xmcfs4xTiicY33eA5OxxkNml0FSCi30T4yrGE+TaiK7G XNULmaQuf1xeD/gI3gJD5ucQPUub3HsL4PUjOc658Oh7zdU/+GBZFimlNhvIm/49aoQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=SkE3yGu8; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 810F9606A322; Fri, 4 Dec 2026 12:56:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=SkE3yGu8 Received: by smtp.kernel.org (Postfix) with ESMTPSA id EF6521F0093B; Fri, 4 Dec 2026 12:56:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796388983; bh=ztCpTwnQKZ1wUL/bU0RLrfD1eB+HeN7MBGnAGv3oIf8=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=SkE3yGu8keVOfQOZz6sfs6C2hwQZiedslSGGPQqGMOLPHRr56BJHajTRWzGkGj6Cm gik/AzX9jc7sBmA0b8srskyb2Hv5A3uIzoizb/dMt1UWpWVVLi/JrpEc6VcDfAt9YJ TOQrz9hztq/kBLXpbzNiFMhEQzAvPB740vS94DNg= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Semyon Maryasin , Zhang Lixu , Srinivas Pandruvada , Jonathan Cameron , Jiri Kosina , Sasha Levin Subject: [PATCH 6.1 0823/1059] HID: sensor: custom: Fix state lifetime during removal Date: Fri, 4 Dec 2026 19:05:54 +0900 Message-ID: <20261204100705.343088129@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100643.980430235@linuxfoundation.org> References: <20261204100643.980430235@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Srinivas Pandruvada [ Upstream commit ac6c19392c7996ac8135dba6907c391a4be5beaf ] Open misc device files can outlive platform-device removal. Since the devm-allocated state can be freed while a reader is queued on its wait queue, subsequent file operations may access freed memory. Allocate the state independently of devres and give every open file a reference to keep the FIFO and state alive after removal. On removal, mark the device as disconnected and wake readers; make reads return -ENODEV and polls report hangup and error. Free the FIFO and state when the final reference is released. Unregister the sensor hub callback before tearing down the interface so in-flight raw-event processing finishes before the sensor state can be freed. Fixes: 4a7de0519df5 ("HID: sensor: Custom and Generic sensor support") Reported-by: Semyon Maryasin Closes: https://lore.kernel.org/all/CAM2dg7ewfVJSP4rFyhkCB1Rmd1+X56sAg=eUdbPthG2xrLqwQQ@mail.gmail.com/ Tested-by: Semyon Maryasin Tested-by: Zhang Lixu Signed-off-by: Srinivas Pandruvada Reviewed-by: Jonathan Cameron Signed-off-by: Jiri Kosina Signed-off-by: Sasha Levin --- drivers/hid/hid-sensor-custom.c | 82 ++++++++++++++++++++++----------- 1 file changed, 56 insertions(+), 26 deletions(-) diff --git a/drivers/hid/hid-sensor-custom.c b/drivers/hid/hid-sensor-custom.c index ec8948f5d4144..4c6d890cd7a0f 100644 --- a/drivers/hid/hid-sensor-custom.c +++ b/drivers/hid/hid-sensor-custom.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -36,6 +37,8 @@ struct hid_sensor_custom_field { struct hid_sensor_custom { struct mutex mutex; + struct kref ref; + bool removed; struct platform_device *pdev; struct hid_sensor_hub_device *hsdev; struct hid_sensor_hub_callbacks callbacks; @@ -643,6 +646,15 @@ static void hid_sensor_custom_remove_attributes(struct hid_sensor_custom * kfree(sensor_inst->fields); } +static void hid_sensor_custom_free(struct kref *ref) +{ + struct hid_sensor_custom *sensor_inst = + container_of(ref, struct hid_sensor_custom, ref); + + kfifo_free(&sensor_inst->data_fifo); + kfree(sensor_inst); +} + static ssize_t hid_sensor_custom_read(struct file *file, char __user *buf, size_t count, loff_t *f_ps) { @@ -656,22 +668,27 @@ static ssize_t hid_sensor_custom_read(struct file *file, char __user *buf, if (count < sizeof(struct hid_sensor_sample)) return -EINVAL; - do { - if (kfifo_is_empty(&sensor_inst->data_fifo)) { - if (file->f_flags & O_NONBLOCK) - return -EAGAIN; + for (;;) { + if (READ_ONCE(sensor_inst->removed)) + return -ENODEV; - ret = wait_event_interruptible(sensor_inst->wait, - !kfifo_is_empty(&sensor_inst->data_fifo)); - if (ret) - return ret; - } ret = kfifo_to_user(&sensor_inst->data_fifo, buf, count, &copied); + if (ret || copied) + break; + + if (file->f_flags & O_NONBLOCK) + return -EAGAIN; + + ret = wait_event_interruptible(sensor_inst->wait, + READ_ONCE(sensor_inst->removed) || + !kfifo_is_empty(&sensor_inst->data_fifo)); if (ret) return ret; + } - } while (copied == 0); + if (ret) + return ret; return copied; } @@ -684,6 +701,7 @@ static int hid_sensor_custom_release(struct inode *inode, struct file *file) struct hid_sensor_custom, custom_dev); clear_bit(0, &sensor_inst->misc_opened); + kref_put(&sensor_inst->ref, hid_sensor_custom_free); return 0; } @@ -691,14 +709,25 @@ static int hid_sensor_custom_release(struct inode *inode, struct file *file) static int hid_sensor_custom_open(struct inode *inode, struct file *file) { struct hid_sensor_custom *sensor_inst; + int ret; sensor_inst = container_of(file->private_data, struct hid_sensor_custom, custom_dev); + if (READ_ONCE(sensor_inst->removed)) + return -ENODEV; + /* We essentially have single reader and writer */ if (test_and_set_bit(0, &sensor_inst->misc_opened)) return -EBUSY; - return stream_open(inode, file); + kref_get(&sensor_inst->ref); + ret = stream_open(inode, file); + if (ret) { + clear_bit(0, &sensor_inst->misc_opened); + kref_put(&sensor_inst->ref, hid_sensor_custom_free); + } + + return ret; } static __poll_t hid_sensor_custom_poll(struct file *file, @@ -712,6 +741,9 @@ static __poll_t hid_sensor_custom_poll(struct file *file, poll_wait(file, &sensor_inst->wait, wait); + if (READ_ONCE(sensor_inst->removed)) + return EPOLLHUP | EPOLLERR; + if (!kfifo_is_empty(&sensor_inst->data_fifo)) mask = EPOLLIN | EPOLLRDNORM; @@ -741,21 +773,15 @@ static int hid_sensor_custom_dev_if_add(struct hid_sensor_custom *sensor_inst) sensor_inst->custom_dev.minor = MISC_DYNAMIC_MINOR; sensor_inst->custom_dev.name = dev_name(&sensor_inst->pdev->dev); sensor_inst->custom_dev.fops = &hid_sensor_custom_fops; - ret = misc_register(&sensor_inst->custom_dev); - if (ret) { - kfifo_free(&sensor_inst->data_fifo); - return ret; - } - return 0; + return misc_register(&sensor_inst->custom_dev); } static void hid_sensor_custom_dev_if_remove(struct hid_sensor_custom *sensor_inst) { - wake_up(&sensor_inst->wait); + WRITE_ONCE(sensor_inst->removed, true); + wake_up_interruptible_all(&sensor_inst->wait); misc_deregister(&sensor_inst->custom_dev); - kfifo_free(&sensor_inst->data_fifo); - } /* luid defined in FW (e.g. ISH). Maybe used to identify sensor. */ @@ -883,8 +909,7 @@ static int hid_sensor_custom_probe(struct platform_device *pdev) int ret; int index; - sensor_inst = devm_kzalloc(&pdev->dev, sizeof(*sensor_inst), - GFP_KERNEL); + sensor_inst = kzalloc(sizeof(*sensor_inst), GFP_KERNEL); if (!sensor_inst) return -ENOMEM; @@ -894,6 +919,7 @@ static int hid_sensor_custom_probe(struct platform_device *pdev) sensor_inst->hsdev = hsdev; sensor_inst->pdev = pdev; mutex_init(&sensor_inst->mutex); + kref_init(&sensor_inst->ref); platform_set_drvdata(pdev, sensor_inst); index = get_known_custom_sensor_index(hsdev); @@ -905,7 +931,7 @@ static int hid_sensor_custom_probe(struct platform_device *pdev) if (ret) { dev_err(&pdev->dev, "register_platform_device failed\n"); - return ret; + goto err_put; } return 0; @@ -915,7 +941,7 @@ static int hid_sensor_custom_probe(struct platform_device *pdev) &sensor_inst->callbacks); if (ret < 0) { dev_err(&pdev->dev, "callback reg failed\n"); - return ret; + goto err_put; } ret = hid_sensor_custom_add_attributes(sensor_inst); @@ -940,6 +966,8 @@ static int hid_sensor_custom_probe(struct platform_device *pdev) hid_sensor_custom_remove_attributes(sensor_inst); err_remove_callback: sensor_hub_remove_callback(hsdev, hsdev->usage); +err_put: + kref_put(&sensor_inst->ref, hid_sensor_custom_free); return ret; } @@ -951,9 +979,10 @@ static void hid_sensor_custom_remove(struct platform_device *pdev) if (sensor_inst->custom_pdev) { platform_device_unregister(sensor_inst->custom_pdev); - return; + goto out; } + sensor_hub_remove_callback(hsdev, hsdev->usage); hid_sensor_custom_dev_if_remove(sensor_inst); /* Remove enable_sensor first as it uses fields via power_state/report_state. */ sysfs_remove_group(&sensor_inst->pdev->dev.kobj, @@ -966,7 +995,8 @@ static void hid_sensor_custom_remove(struct platform_device *pdev) } mutex_unlock(&sensor_inst->mutex); hid_sensor_custom_remove_attributes(sensor_inst); - sensor_hub_remove_callback(hsdev, hsdev->usage); +out: + kref_put(&sensor_inst->ref, hid_sensor_custom_free); } static const struct platform_device_id hid_sensor_custom_ids[] = { -- 2.53.0