* [PATCH 1/4] HID: Intel-thc-hid: Intel-thc: Add safe DMA read interfaces
2026-11-16 8:27 [PATCH 0/4] Fix potential THC DMA issues Even Xu
@ 2026-11-16 8:27 ` Even Xu
2026-11-16 8:27 ` [PATCH 2/4] HID: Intel-thc-hid: Intel-quickspi: Use safe RXDMA reads Even Xu
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Even Xu @ 2026-11-16 8:27 UTC (permalink / raw)
To: bentiss, jikos
Cc: srinivas.pandruvada, linux-input, linux-kernel, Even Xu,
Rui Zhang
THC DMA buffers are rounded up to a 4 KB boundary, but callers may
allocate smaller receive buffers. Checking a frame against the DMA
capacity alone does not protect the destination when do DMA buffer
copying.
Add thc_rxdma_read_safe() and thc_swdma_read_safe() with an explicit
destination capacity to check both the DMA capacity and the supplied
size before copying.
Keep the original interfaces as compatibility wrappers.
Fixes: a688404b2e20 ("HID: intel-thc-hid: intel-thc: Add THC DMA interfaces")
Signed-off-by: Even Xu <even.xu@intel.com>
Tested-by: Rui Zhang <rui1.zhang@intel.com>
---
.../intel-thc-hid/intel-thc/intel-thc-dma.c | 95 ++++++++++++++++---
.../intel-thc-hid/intel-thc/intel-thc-dma.h | 6 ++
2 files changed, 86 insertions(+), 15 deletions(-)
diff --git a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c
index c80ef8c20574..11d29c8daebe 100644
--- a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c
+++ b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c
@@ -614,7 +614,7 @@ EXPORT_SYMBOL_NS_GPL(thc_rxdma_reset, "INTEL_THC");
static int read_dma_buffer(struct thc_device *dev,
struct thc_dma_configuration *read_config,
- u8 prd_table_index, void *read_buff)
+ u8 prd_table_index, void *read_buff, size_t read_buff_len)
{
struct thc_prd_table *prd_tbl;
struct scatterlist *sg;
@@ -640,6 +640,13 @@ static int read_dma_buffer(struct thc_device *dev,
return -EMSGSIZE;
}
+ if (mes_len > read_buff_len) {
+ dev_err(dev->dev,
+ "Message length %zu is bigger than destination buffer length %zu\n",
+ mes_len, read_buff_len);
+ return -EMSGSIZE;
+ }
+
sg = read_config->sgls[prd_table_index];
ret = sg_copy_to_buffer(sg, nent, read_buff, mes_len);
if (ret != mes_len) {
@@ -680,21 +687,26 @@ static int is_dma_buf_empty(struct thc_device *dev,
static int thc_dma_read(struct thc_device *dev,
struct thc_dma_configuration *read_config,
- void *read_buff, size_t *read_len, int *read_finished)
+ void *read_buff, size_t read_buff_len,
+ size_t *read_len, int *read_finished)
{
u8 read_ptr, write_ptr, prd_table_index;
- int status;
+ int status = 0;
+
+ *read_len = 0;
if (!is_dma_buf_empty(dev, read_config, &read_ptr, &write_ptr)) {
prd_table_index = write_ptr & THC_POINTER_MASK;
- status = read_dma_buffer(dev, read_config, prd_table_index, read_buff);
- if (status <= 0) {
+ status = read_dma_buffer(dev, read_config, prd_table_index,
+ read_buff, read_buff_len);
+ if (status <= 0 && status != -EMSGSIZE) {
dev_err_once(dev->dev, "read DMA buffer failed %d\n", status);
return -EIO;
}
- *read_len = status;
+ if (status > 0)
+ *read_len = status;
/* Clear the relevant PRD table */
thc_copy_one_sgl_to_prd(dev, read_config, prd_table_index);
@@ -711,22 +723,27 @@ static int thc_dma_read(struct thc_device *dev,
if (read_finished)
*read_finished = is_dma_buf_empty(dev, read_config, &read_ptr, &write_ptr) ? 1 : 0;
- return 0;
+ return status < 0 ? status : 0;
}
/**
- * thc_rxdma_read - Read data from RXDMA buffer
+ * thc_rxdma_read_safe - Read data from RXDMA buffer with a destination size limit
*
* @dev: The pointer of THC private device context
* @dma_channel: The RXDMA engine of read data source
* @read_buff: The pointer of the read data buffer
+ * @read_buff_len: The allocated size of the read data buffer in bytes
* @read_len: The pointer of the read data length
* @read_finished: The pointer of the flag indicating if all pending data has been read out
*
+ * Oversized frames are discarded and their PRD table is recycled without
+ * copying data. In this case, @read_len is zero and @read_finished is updated.
+ *
* Return: 0 on success, other error codes on failed.
*/
-int thc_rxdma_read(struct thc_device *dev, enum thc_dma_channel dma_channel,
- void *read_buff, size_t *read_len, int *read_finished)
+int thc_rxdma_read_safe(struct thc_device *dev, enum thc_dma_channel dma_channel,
+ void *read_buff, size_t read_buff_len,
+ size_t *read_len, int *read_finished)
{
struct thc_dma_configuration *dma_config;
int ret;
@@ -749,10 +766,31 @@ int thc_rxdma_read(struct thc_device *dev, enum thc_dma_channel dma_channel,
return -EINVAL;
}
- ret = thc_dma_read(dev, dma_config, read_buff, read_len, read_finished);
+ ret = thc_dma_read(dev, dma_config, read_buff, read_buff_len, read_len, read_finished);
return ret;
}
+EXPORT_SYMBOL_NS_GPL(thc_rxdma_read_safe, "INTEL_THC");
+
+/**
+ * thc_rxdma_read - Read data from RXDMA buffer
+ *
+ * @dev: The pointer of THC private device context
+ * @dma_channel: The RXDMA engine of read data source
+ * @read_buff: The pointer of the read data buffer
+ * @read_len: The pointer of the read data length
+ * @read_finished: The pointer of the flag indicating if all pending data has been read out
+ *
+ * Legacy callers do not provide a destination capacity.
+ *
+ * Return: 0 on success, other error codes on failed.
+ */
+int thc_rxdma_read(struct thc_device *dev, enum thc_dma_channel dma_channel,
+ void *read_buff, size_t *read_len, int *read_finished)
+{
+ return thc_rxdma_read_safe(dev, dma_channel, read_buff, SIZE_MAX,
+ read_len, read_finished);
+}
EXPORT_SYMBOL_NS_GPL(thc_rxdma_read, "INTEL_THC");
static int thc_swdma_read_start(struct thc_device *dev, void *write_buff,
@@ -862,19 +900,23 @@ static int thc_swdma_read_completion(struct thc_device *dev)
}
/**
- * thc_swdma_read - Use software DMA to read data from touch device
+ * thc_swdma_read_safe - Read touch device data with a destination size limit
*
* @dev: The pointer of THC private device context
* @write_buff: The pointer of write buffer for SWDMA sequence
* @write_len: The write data length for SWDMA sequence
* @prd_tbl_len: The prd table length of SWDMA engine, can be set to NULL
* @read_buff: The pointer of the read data buffer
+ * @read_buff_len: The allocated size of the read data buffer in bytes
* @read_len: The pointer of the read data length
*
+ * Oversized frames are discarded without copying data and @read_len is zero.
+ *
* Return: 0 on success, other error codes on failed.
*/
-int thc_swdma_read(struct thc_device *dev, void *write_buff, size_t write_len,
- u32 *prd_tbl_len, void *read_buff, size_t *read_len)
+int thc_swdma_read_safe(struct thc_device *dev, void *write_buff, size_t write_len,
+ u32 *prd_tbl_len, void *read_buff, size_t read_buff_len,
+ size_t *read_len)
{
int ret;
@@ -905,7 +947,8 @@ int thc_swdma_read(struct thc_device *dev, void *write_buff, size_t write_len,
goto end;
}
- ret = thc_dma_read(dev, &dev->dma_ctx->dma_config[THC_SWDMA], read_buff, read_len, NULL);
+ ret = thc_dma_read(dev, &dev->dma_ctx->dma_config[THC_SWDMA],
+ read_buff, read_buff_len, read_len, NULL);
if (ret)
goto end;
@@ -915,6 +958,28 @@ int thc_swdma_read(struct thc_device *dev, void *write_buff, size_t write_len,
mutex_unlock(&dev->thc_bus_lock);
return ret;
}
+EXPORT_SYMBOL_NS_GPL(thc_swdma_read_safe, "INTEL_THC");
+
+/**
+ * thc_swdma_read - Use software DMA to read data from touch device
+ *
+ * @dev: The pointer of THC private device context
+ * @write_buff: The pointer of write buffer for SWDMA sequence
+ * @write_len: The write data length for SWDMA sequence
+ * @prd_tbl_len: The prd table length of SWDMA engine, can be set to NULL
+ * @read_buff: The pointer of the read data buffer
+ * @read_len: The pointer of the read data length
+ *
+ * Legacy callers do not provide a destination capacity.
+ *
+ * Return: 0 on success, other error codes on failed.
+ */
+int thc_swdma_read(struct thc_device *dev, void *write_buff, size_t write_len,
+ u32 *prd_tbl_len, void *read_buff, size_t *read_len)
+{
+ return thc_swdma_read_safe(dev, write_buff, write_len, prd_tbl_len,
+ read_buff, SIZE_MAX, read_len);
+}
EXPORT_SYMBOL_NS_GPL(thc_swdma_read, "INTEL_THC");
static int write_dma_buffer(struct thc_device *dev,
diff --git a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.h b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.h
index 715423453a9d..f6152b4209f7 100644
--- a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.h
+++ b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.h
@@ -150,6 +150,12 @@ int thc_rxdma_read(struct thc_device *dev, enum thc_dma_channel dma_channel,
void *read_buff, size_t *read_len, int *read_finished);
int thc_swdma_read(struct thc_device *dev, void *write_buff, size_t write_len,
u32 *prd_tbl_len, void *read_buff, size_t *read_len);
+int thc_rxdma_read_safe(struct thc_device *dev, enum thc_dma_channel dma_channel,
+ void *read_buff, size_t read_buff_len,
+ size_t *read_len, int *read_finished);
+int thc_swdma_read_safe(struct thc_device *dev, void *write_buff, size_t write_len,
+ u32 *prd_tbl_len, void *read_buff, size_t read_buff_len,
+ size_t *read_len);
int thc_dma_write(struct thc_device *dev, void *buffer, size_t buf_len);
struct thc_dma_context *thc_dma_init(struct thc_device *dev);
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH 3/4] HID: Intel-thc-hid: Intel-quicki2c: Use safe DMA reads
2026-11-16 8:27 [PATCH 0/4] Fix potential THC DMA issues Even Xu
2026-11-16 8:27 ` [PATCH 1/4] HID: Intel-thc-hid: Intel-thc: Add safe DMA read interfaces Even Xu
2026-11-16 8:27 ` [PATCH 2/4] HID: Intel-thc-hid: Intel-quickspi: Use safe RXDMA reads Even Xu
@ 2026-11-16 8:27 ` Even Xu
2026-11-16 8:28 ` [PATCH 4/4] HID: Intel-thc-hid: Intel-thc: Restore RXDMA after SWDMA operation errors Even Xu
3 siblings, 0 replies; 5+ messages in thread
From: Even Xu @ 2026-11-16 8:27 UTC (permalink / raw)
To: bentiss, jikos
Cc: srinivas.pandruvada, linux-input, linux-kernel, Even Xu,
Rui Zhang
Use thc_rxdma_read_safe() and thc_swdma_read_safe(). Pass
HIDI2C_PACKET_LEN(input_len) as the destination capacity for RXDMA input
reports and SWDMA GET_REPORT responses. Handle -EMSGSIZE in the receive
loop by continuing pending reports instead of scheduling full RXDMA
recovery.
Fixes: 66b59bfce6d9 ("HID: intel-thc-hid: intel-quicki2c: Complete THC QuickI2C driver")
Signed-off-by: Even Xu <even.xu@intel.com>
Tested-by: Rui Zhang <rui1.zhang@intel.com>
---
drivers/hid/intel-thc-hid/intel-quicki2c/pci-quicki2c.c | 9 ++++++---
.../hid/intel-thc-hid/intel-quicki2c/quicki2c-protocol.c | 7 ++++---
2 files changed, 10 insertions(+), 6 deletions(-)
diff --git a/drivers/hid/intel-thc-hid/intel-quicki2c/pci-quicki2c.c b/drivers/hid/intel-thc-hid/intel-quicki2c/pci-quicki2c.c
index 29319b6227f3..f87f33f61d34 100644
--- a/drivers/hid/intel-thc-hid/intel-quicki2c/pci-quicki2c.c
+++ b/drivers/hid/intel-thc-hid/intel-quicki2c/pci-quicki2c.c
@@ -292,9 +292,12 @@ static int handle_input_report(struct quicki2c_device *qcdev)
int ret;
while (!rx_dma_finished) {
- ret = thc_rxdma_read(qcdev->thc_hw, THC_RXDMA2,
- (u8 *)pkt, &report_len,
- &rx_dma_finished);
+ ret = thc_rxdma_read_safe(qcdev->thc_hw, THC_RXDMA2, (u8 *)pkt,
+ HIDI2C_PACKET_LEN(qcdev->input_len), &report_len,
+ &rx_dma_finished);
+ if (ret == -EMSGSIZE)
+ continue;
+
if (ret)
return ret;
diff --git a/drivers/hid/intel-thc-hid/intel-quicki2c/quicki2c-protocol.c b/drivers/hid/intel-thc-hid/intel-quicki2c/quicki2c-protocol.c
index 41271301215a..aab17c4aaca8 100644
--- a/drivers/hid/intel-thc-hid/intel-quicki2c/quicki2c-protocol.c
+++ b/drivers/hid/intel-thc-hid/intel-quicki2c/quicki2c-protocol.c
@@ -122,8 +122,8 @@ int quicki2c_get_report_descriptor(struct quicki2c_device *qcdev)
size_t read_len = le16_to_cpu(qcdev->dev_desc.report_desc_len);
u32 prd_len = read_len;
- return thc_swdma_read(qcdev->thc_hw, (u8 *)&desc_reg, HIDI2C_REG_LEN,
- &prd_len, qcdev->report_descriptor, &read_len);
+ return thc_swdma_read_safe(qcdev->thc_hw, (u8 *)&desc_reg, HIDI2C_REG_LEN,
+ &prd_len, qcdev->report_descriptor, read_len, &read_len);
}
int quicki2c_get_report(struct quicki2c_device *qcdev, u8 report_type,
@@ -153,7 +153,8 @@ int quicki2c_get_report(struct quicki2c_device *qcdev, u8 report_type,
rpt = (struct hidi2c_report_packet *)qcdev->input_buf;
- ret = thc_swdma_read(qcdev->thc_hw, qcdev->report_buf, len, NULL, rpt, &read_len);
+ ret = thc_swdma_read_safe(qcdev->thc_hw, qcdev->report_buf, len, NULL, rpt,
+ HIDI2C_PACKET_LEN(qcdev->input_len), &read_len);
if (ret) {
dev_err_once(qcdev->dev, "Get report failed, ret %d, read len (%zu vs %zu)\n",
ret, read_len, buf_len);
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread