Skip to contentSkip to conversation

Fix potential THC DMA issues

PATCH Applied Even Xu 2 participants 7 messages Nov 16 – Nov 20

Subsystems (2)
lore
Share & tools
lore

Even Xu

author maintainer
#0 lore raw

cover Fix potential THC DMA issues

This patch set fix two potential THC DMA issues:
1. 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.

2. thc_swdma_read_start() quiesces device interrupts, stops RXDMA and temporarily disables the I2C special controls. If reading the response of a completed SWDMA transfer fails, thc_swdma_read_safe() skips thc_swdma_read_completion(), leaving RXDMA stopped and the saved I2C controls unrestored. Normal input processing can no longer resume.

The fixes are:
1. 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.

2. Call thc_swdma_read_completion() even when reading the completed response fails, so RXDMA and the saved I2C controls can be restored.

Even Xu (4):
HID: Intel-thc-hid: Intel-thc: Add safe DMA read interfaces
HID: Intel-thc-hid: Intel-quickspi: Use safe RXDMA reads
HID: Intel-thc-hid: Intel-quicki2c: Use safe DMA reads
HID: Intel-thc-hid: Intel-thc: Restore RXDMA after SWDMA operation
errors

 .../intel-quicki2c/pci-quicki2c.c             |   9 +-
 .../intel-quicki2c/quicki2c-protocol.c        |   7 +-
 .../intel-quickspi/pci-quickspi.c             |  12 +-
 .../intel-quickspi/quickspi-dev.h             |   7 ++
 .../intel-thc-hid/intel-thc/intel-thc-dma.c   | 103 ++++++++++++++----
 .../intel-thc-hid/intel-thc/intel-thc-dma.h   |   6 +
 6 files changed, 115 insertions(+), 29 deletions(-)
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH 1/4 HID: Intel-thc-hid: Intel-thc: Add safe DMA read interfaces

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")
---
 .../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(-)
drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c +80 -15
index c80ef8c20574..11d29c8daebe 100644
@@ -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,
drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.h +6 -0
index 715423453a9d..f6152b4209f7 100644
@@ -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);
Signature · 1 line
2.43.0
maintainer Applied
#2 lore raw

Re: [PATCH 1/4] HID: Intel-thc-hid: Intel-thc: Add safe DMA read interfaces

Quoted 9 lines · Even Xu

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.

Quoted 5 lines

Keep the original interfaces as compatibility wrappers.

Fixes a688404b2e20 ("HID: intel-thc-hid: intel-thc: Add THC DMA interfaces")

I have applied the series, thanks.

Signature · 2 lines
Jiri Kosina
SUSE Labs
author maintainer
#3 lore raw

RE: [PATCH 1/4] HID: Intel-thc-hid: Intel-thc: Add safe DMA read interfaces

Thanks Jiri!

Best Regards,
Even Xu

Quoted 33 lines

-----Original Message-----
From: Jiri Kosina <[email protected]>
Sent: Friday, November 20, 2026 5:19 AM
To: Xu, Even <[email protected]>
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]; Zhang, Rui1 <[email protected]>
Subject: Re: [PATCH 1/4] HID: Intel-thc-hid: Intel-thc: Add safe DMA read interfaces

Quoted 9 lines · Even Xu

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.

Quoted 6 lines

Keep the original interfaces as compatibility wrappers.

Fixes a688404b2e20 ("HID: intel-thc-hid: intel-thc: Add THC DMA

interfaces")

I have applied the series, thanks.

--
Jiri Kosina
SUSE Labs

author maintainer
#4 lore raw

PATCH 2/4 HID: Intel-thc-hid: Intel-quickspi: Use safe RXDMA reads

In reply to Even Xu

Use thc_rxdma_read_safe() and pass the full input_buf allocation, including the body header. Handle -EMSGSIZE in the receive loop by skipping the discarded frame and continuing pending reports instead of scheduling a full RXDMA recovery.

Fixes 4138f21115ae ("HID: intel-thc-hid: intel-quickspi: Complete THC QuickSPI driver")
---
 .../hid/intel-thc-hid/intel-quickspi/pci-quickspi.c  | 12 ++++++++----
 .../hid/intel-thc-hid/intel-quickspi/quickspi-dev.h  |  7 +++++++
 2 files changed, 15 insertions(+), 4 deletions(-)
drivers/hid/intel-thc-hid/intel-quickspi/pci-quickspi.c +8 -4
index ec974d549a92..703ba238c02a 100644
@@ -324,8 +324,12 @@ static irqreturn_t quickspi_irq_thread_handler(int irq, void *dev_id)
 
 	if (int_mask & BIT(THC_RXDMA2_INT)) {
 		while (!read_finished) {
-			ret = thc_rxdma_read(qsdev->thc_hw, THC_RXDMA2, qsdev->input_buf,
-					     &input_len, &read_finished);
+			ret = thc_rxdma_read_safe(qsdev->thc_hw, THC_RXDMA2, qsdev->input_buf,
+						  HIDSPI_INPUT_BODY_SIZE(qsdev->input_len),
+						  &input_len, &read_finished);
+			if (ret == -EMSGSIZE)
+				continue;
+
 			if (ret) {
 				err_recover = 1;
 				goto end;
@@ -550,7 +554,7 @@ static int quickspi_alloc_report_buf(struct quickspi_device *qsdev)
 		return -ENOMEM;
 
 	max_input_len = max(le16_to_cpu(qsdev->dev_desc.rep_desc_len),
-			    le16_to_cpu(qsdev->dev_desc.max_input_len));
+			    le16_to_cpu(qsdev->dev_desc.max_input_len)) + QUICKSPI_DMA_EXTRA_BYTE;
 
 	qsdev->input_buf = devm_kzalloc(qsdev->dev,
 					HIDSPI_INPUT_BODY_SIZE(max_input_len), GFP_KERNEL);
@@ -560,7 +564,7 @@ static int quickspi_alloc_report_buf(struct quickspi_device *qsdev)
 	qsdev->input_len = max_input_len;
 
 	max_report_len = max(le16_to_cpu(qsdev->dev_desc.max_output_len),
-			     le16_to_cpu(qsdev->dev_desc.max_input_len));
+			     le16_to_cpu(qsdev->dev_desc.max_input_len)) + QUICKSPI_DMA_EXTRA_BYTE;
 
 	qsdev->report_buf = devm_kzalloc(qsdev->dev,
 					 HIDSPI_OUTPUT_REPORT_SIZE(max_report_len), GFP_KERNEL);
drivers/hid/intel-thc-hid/intel-quickspi/quickspi-dev.h +7 -0
index 4362ac568bda..e8ddb504ccc7 100644
@@ -54,6 +54,13 @@
 #define MAX_PACKET_SIZE_VALUE_MTL			128
 #define MAX_PACKET_SIZE_VALUE_LNL			256
 
+/*
+ * During HID-over-SPI transfers, THC DMA may also read bytes that aren’t
+ * part of the actual data, such as a placeholder byte used for timing
+ * control (8 clock cycles).
+ */
+#define QUICKSPI_DMA_EXTRA_BYTE				1
+
 /*
  * THC uses runtime auto suspend to dynamically switch between THC active LTR
  * and low power LTR to save CPU power.
Signature · 1 line
2.43.0
author maintainer
#5 lore raw

PATCH 3/4 HID: Intel-thc-hid: Intel-quicki2c: Use safe DMA reads

In reply to Even Xu

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")
---
 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(-)
drivers/hid/intel-thc-hid/intel-quicki2c/pci-quicki2c.c +6 -3
index 29319b6227f3..f87f33f61d34 100644
@@ -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;
 
drivers/hid/intel-thc-hid/intel-quicki2c/quicki2c-protocol.c +4 -3
index 41271301215a..aab17c4aaca8 100644
@@ -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);
Signature · 1 line
2.43.0
author maintainer
#6 lore raw

PATCH 4/4 HID: Intel-thc-hid: Intel-thc: Restore RXDMA after SWDMA operation errors

In reply to Even Xu

thc_swdma_read_start() quiesces device interrupts, stops RXDMA and temporarily disables the I2C special controls. If reading the response of a completed SWDMA transfer fails, thc_swdma_read_safe() skips thc_swdma_read_completion(), leaving RXDMA stopped and the saved I2C controls unrestored. Normal input processing can no longer resume.

Call thc_swdma_read_completion() even when reading the completed response fails, so RXDMA and the saved I2C controls can be restored.

Fixes a688404b2e20 ("HID: intel-thc-hid: intel-thc: Add THC DMA interfaces")
---
 drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c +4 -4
index 11d29c8daebe..7f9c9017049c 100644
@@ -911,6 +911,7 @@ static int thc_swdma_read_completion(struct thc_device *dev)
  * @read_len: The pointer of the read data length
  *
  * Oversized frames are discarded without copying data and @read_len is zero.
+ * The completed transaction is restored before returning the read error.
  *
  * Return: 0 on success, other error codes on failed.
  */
@@ -949,12 +950,11 @@ int thc_swdma_read_safe(struct thc_device *dev, void *write_buff, size_t write_l
 
 	ret = thc_dma_read(dev, &dev->dma_ctx->dma_config[THC_SWDMA],
 			   read_buff, read_buff_len, read_len, NULL);
-	if (ret)
-		goto end;
-
-	ret = thc_swdma_read_completion(dev);
 
 end:
+	if (thc_swdma_read_completion(dev))
+		dev_err(dev->dev, "Failed to restore DMA after SWDMA read\n");
+
 	mutex_unlock(&dev->thc_bus_lock);
 	return ret;
 }
Signature · 1 line
2.43.0