Linux Input/HID development
 help / color / mirror / Atom feed
* [PATCH v4] HID: intel-thc-hid: intel-thc: restore PRD table capacity
@ 2026-11-17  0:27 Emfox Zhou
  2026-11-17  0:48 ` sashiko-bot
  0 siblings, 1 reply; 4+ messages in thread
From: Emfox Zhou @ 2026-11-17  0:27 UTC (permalink / raw)
  To: Even Xu, Xinpeng Sun, Jiri Kosina, Benjamin Tissoires
  Cc: linux-input, linux-kernel

The controller writes the frame length into the PRD entry it filled and
marks the end of the frame there; only a read puts the capacity back.
thc_reset_dma_settings() rewinds the read pointers but not those lengths:
a frame still queued when it runs is dropped, and the next frame that does
not fit runs past the end of its entry. The controller reports a buffer
overrun, which is a transfer error and leads back into the same reset,
with the table still short.

Rebuild a table before the engine that reads it is started:
thc_dma_configure() for probe, resume and the RxDMA reset, the SWDMA start
for its own table once that engine reports idle (a transfer that timed out
leaves it running), and the end of an SWDMA round trip for the receive
tables that the same reset rewound.

A drain in the threaded handler reads a table without the bus lock, so a
rebuild can land in the middle of it, and a reset does not rule the
handler out either. Give the tables a mutex taken by both, inside the bus
lock where the caller holds it, and no path takes the two in the other
order.

thc_copy_sgls_to_prd() returns early for a channel whose tables are gone.

Fixes: a688404b2e20 ("HID: intel-thc-hid: intel-thc: Add THC DMA interfaces")
Signed-off-by: Emfox Zhou <emfox@debian.org>
---
Changes in v4:
  - The tables are rebuilt under a mutex that the receive path takes as
    well: the threaded handler drains the ring outside the bus lock, so a
    rebuild could land in the middle of a read.
  - The SWDMA table is rebuilt after its engine is waited for, and the
    receive tables are rebuilt where an SWDMA round trip restarts the
    receive engine.

Link: https://lore.kernel.org/r/20261115040756.602092-1-emfox@debian.org
---
 .../intel-thc-hid/intel-thc/intel-thc-dma.c   | 31 ++++++++++++++++++-
 .../intel-thc-hid/intel-thc/intel-thc-dma.h   |  7 +++++
 2 files changed, 37 insertions(+), 1 deletion(-)

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 c80ef8c20..413f264e3 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
@@ -211,12 +211,27 @@ static void thc_copy_sgls_to_prd(struct thc_device *dev,
 {
 	unsigned int i;
 
+	guard(mutex)(&dev->dma_ctx->prd_lock);
+
+	if (!config->is_enabled || !config->prd_tbls)
+		return;
+
 	memset(config->prd_tbls, 0, array_size(PRD_TABLE_SIZE, config->prd_tbl_num));
 
 	for (i = 0; i < config->prd_tbl_num; i++)
 		thc_copy_one_sgl_to_prd(dev, config, i);
 }
 
+/*
+ * A frame that is never read leaves its entry short, and the engine reads
+ * the table again on every start: put the capacity back.
+ */
+static void thc_rebuild_rx_prd(struct thc_device *dev)
+{
+	thc_copy_sgls_to_prd(dev, &dev->dma_ctx->dma_config[THC_RXDMA1]);
+	thc_copy_sgls_to_prd(dev, &dev->dma_ctx->dma_config[THC_RXDMA2]);
+}
+
 static int setup_dma_buffers(struct thc_device *dev,
 			     struct thc_dma_configuration *config,
 			     enum dma_data_direction dir)
@@ -327,6 +342,7 @@ struct thc_dma_context *thc_dma_init(struct thc_device *dev)
 		return NULL;
 
 	dev->dma_ctx = dma_ctx;
+	mutex_init(&dma_ctx->prd_lock);
 
 	dma_ctx->dma_config[THC_RXDMA1].dma_channel = THC_RXDMA1;
 	dma_ctx->dma_config[THC_RXDMA2].dma_channel = THC_RXDMA2;
@@ -491,6 +507,9 @@ int thc_dma_configure(struct thc_device *dev)
 		return -EINVAL;
 	}
 
+	/* An entry may still be short from a frame nobody read. */
+	thc_rebuild_rx_prd(dev);
+
 	for (chan = 0; chan < MAX_THC_DMA_CHANNEL; chan++) {
 		dma_set_prd_base_addr(dev,
 				      dma_ctx->dma_config[chan].prd_tbls_dma_handle,
@@ -685,6 +704,8 @@ static int thc_dma_read(struct thc_device *dev,
 	u8 read_ptr, write_ptr, prd_table_index;
 	int status;
 
+	guard(mutex)(&dev->dma_ctx->prd_lock);
+
 	if (!is_dma_buf_empty(dev, read_config, &read_ptr, &write_ptr)) {
 		prd_table_index = write_ptr & THC_POINTER_MASK;
 
@@ -765,11 +786,16 @@ static int thc_swdma_read_start(struct thc_device *dev, void *write_buff,
 	if (ret)
 		return ret;
 
-	if (thc_wait_for_dma_pause(dev, THC_RXDMA1) || thc_wait_for_dma_pause(dev, THC_RXDMA2))
+	if (thc_wait_for_dma_pause(dev, THC_RXDMA1) ||
+	    thc_wait_for_dma_pause(dev, THC_RXDMA2) ||
+	    thc_wait_for_dma_pause(dev, THC_SWDMA))
 		return -EIO;
 
 	thc_reset_dma_settings(dev);
 
+	/* A transfer that timed out left its frame length in the entry. */
+	thc_copy_sgls_to_prd(dev, &dev->dma_ctx->dma_config[THC_SWDMA]);
+
 	/*
 	 * Max input size control feature is only available for RxDMA, it must keep disabled
 	 * during SWDMA operation, and restore to previous state after SWDMA is done.
@@ -854,6 +880,9 @@ static int thc_swdma_read_completion(struct thc_device *dev)
 
 	thc_reset_dma_settings(dev);
 
+	/* The receive engine starts again just below. */
+	thc_rebuild_rx_prd(dev);
+
 	dma_set_start_bit(dev, &dev->dma_ctx->dma_config[THC_RXDMA2]);
 
 	ret = thc_interrupt_quiesce(dev, false);
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 715423453..264cd26b7 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
@@ -6,6 +6,7 @@
 
 #include <linux/bits.h>
 #include <linux/dma-mapping.h>
+#include <linux/mutex.h>
 #include <linux/sizes.h>
 #include <linux/time64.h>
 #include <linux/types.h>
@@ -123,6 +124,9 @@ struct thc_dma_configuration {
  * struct thc_dma_context - THC DMA context
  * @thc_dma_configuration: Array of all THC Channel configures
  * @use_write_interrupts: Indicate TxDMA using interrupt or polling
+ * @prd_lock: Keeps a table from being rebuilt while the DMA read path is
+ *            using it; a sleeping lock, taken inside the bus lock where the
+ *            caller holds that one.
  * @rx_max_size_en: Temp flag to indicate THC I2C Rx max input size control feature
  *                  enabled or not, only be used during SWDMA operation.
  * @rx_int_delay_en: Temp flag to indicate THC I2C Rx interrupt delay feature
@@ -132,6 +136,9 @@ struct thc_dma_context {
 	struct thc_dma_configuration dma_config[MAX_THC_DMA_CHANNEL];
 	u8 use_write_interrupts;
 
+	/* Serialises a rebuild of the tables with the DMA read path. */
+	struct mutex prd_lock;
+
 	bool rx_max_size_en;
 	bool rx_int_delay_en;
 };

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v4] HID: intel-thc-hid: intel-thc: restore PRD table capacity
@ 2026-11-17  0:32 Emfox Zhou
  2026-11-17  0:51 ` sashiko-bot
  0 siblings, 1 reply; 4+ messages in thread
From: Emfox Zhou @ 2026-11-17  0:32 UTC (permalink / raw)
  To: Even Xu, Xinpeng Sun, Jiri Kosina, Benjamin Tissoires
  Cc: linux-input, linux-kernel

The controller writes the frame length into the PRD entry it filled and
marks the end of the frame there; only a read puts the capacity back.
thc_reset_dma_settings() rewinds the read pointers but not those lengths:
a frame still queued when it runs is dropped, and the next frame that does
not fit runs past the end of its entry. The controller reports a buffer
overrun, which is a transfer error and leads back into the same reset,
with the table still short.

Rebuild a table before the engine that reads it is started:
thc_dma_configure() for probe, resume and the RxDMA reset, the SWDMA start
for its own table once that engine reports idle (a transfer that timed out
leaves it running), and the end of an SWDMA round trip for the receive
tables that the same reset rewound.

A drain in the threaded handler reads a table without the bus lock, so a
rebuild can land in the middle of it, and a reset does not rule the
handler out either. Give the tables a mutex taken by both, inside the bus
lock where the caller holds it, and no path takes the two in the other
order.

thc_copy_sgls_to_prd() returns early for a channel whose tables are gone.

Fixes: a688404b2e20 ("HID: intel-thc-hid: intel-thc: Add THC DMA interfaces")
Signed-off-by: Emfox Zhou <emfox@debian.org>
---
Changes in v4:
  - The tables are rebuilt under a mutex that the receive path takes as
    well: the threaded handler drains the ring outside the bus lock, so a
    rebuild could land in the middle of a read.
  - The SWDMA table is rebuilt after its engine is waited for, and the
    receive tables are rebuilt where an SWDMA round trip restarts the
    receive engine.

Link: https://lore.kernel.org/r/20261115040756.602092-1-emfox@debian.org
---
 .../intel-thc-hid/intel-thc/intel-thc-dma.c   | 31 ++++++++++++++++++-
 .../intel-thc-hid/intel-thc/intel-thc-dma.h   |  7 +++++
 2 files changed, 37 insertions(+), 1 deletion(-)

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 c80ef8c20..413f264e3 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
@@ -211,12 +211,27 @@ static void thc_copy_sgls_to_prd(struct thc_device *dev,
 {
 	unsigned int i;
 
+	guard(mutex)(&dev->dma_ctx->prd_lock);
+
+	if (!config->is_enabled || !config->prd_tbls)
+		return;
+
 	memset(config->prd_tbls, 0, array_size(PRD_TABLE_SIZE, config->prd_tbl_num));
 
 	for (i = 0; i < config->prd_tbl_num; i++)
 		thc_copy_one_sgl_to_prd(dev, config, i);
 }
 
+/*
+ * A frame that is never read leaves its entry short, and the engine reads
+ * the table again on every start: put the capacity back.
+ */
+static void thc_rebuild_rx_prd(struct thc_device *dev)
+{
+	thc_copy_sgls_to_prd(dev, &dev->dma_ctx->dma_config[THC_RXDMA1]);
+	thc_copy_sgls_to_prd(dev, &dev->dma_ctx->dma_config[THC_RXDMA2]);
+}
+
 static int setup_dma_buffers(struct thc_device *dev,
 			     struct thc_dma_configuration *config,
 			     enum dma_data_direction dir)
@@ -327,6 +342,7 @@ struct thc_dma_context *thc_dma_init(struct thc_device *dev)
 		return NULL;
 
 	dev->dma_ctx = dma_ctx;
+	mutex_init(&dma_ctx->prd_lock);
 
 	dma_ctx->dma_config[THC_RXDMA1].dma_channel = THC_RXDMA1;
 	dma_ctx->dma_config[THC_RXDMA2].dma_channel = THC_RXDMA2;
@@ -491,6 +507,9 @@ int thc_dma_configure(struct thc_device *dev)
 		return -EINVAL;
 	}
 
+	/* An entry may still be short from a frame nobody read. */
+	thc_rebuild_rx_prd(dev);
+
 	for (chan = 0; chan < MAX_THC_DMA_CHANNEL; chan++) {
 		dma_set_prd_base_addr(dev,
 				      dma_ctx->dma_config[chan].prd_tbls_dma_handle,
@@ -685,6 +704,8 @@ static int thc_dma_read(struct thc_device *dev,
 	u8 read_ptr, write_ptr, prd_table_index;
 	int status;
 
+	guard(mutex)(&dev->dma_ctx->prd_lock);
+
 	if (!is_dma_buf_empty(dev, read_config, &read_ptr, &write_ptr)) {
 		prd_table_index = write_ptr & THC_POINTER_MASK;
 
@@ -765,11 +786,16 @@ static int thc_swdma_read_start(struct thc_device *dev, void *write_buff,
 	if (ret)
 		return ret;
 
-	if (thc_wait_for_dma_pause(dev, THC_RXDMA1) || thc_wait_for_dma_pause(dev, THC_RXDMA2))
+	if (thc_wait_for_dma_pause(dev, THC_RXDMA1) ||
+	    thc_wait_for_dma_pause(dev, THC_RXDMA2) ||
+	    thc_wait_for_dma_pause(dev, THC_SWDMA))
 		return -EIO;
 
 	thc_reset_dma_settings(dev);
 
+	/* A transfer that timed out left its frame length in the entry. */
+	thc_copy_sgls_to_prd(dev, &dev->dma_ctx->dma_config[THC_SWDMA]);
+
 	/*
 	 * Max input size control feature is only available for RxDMA, it must keep disabled
 	 * during SWDMA operation, and restore to previous state after SWDMA is done.
@@ -854,6 +880,9 @@ static int thc_swdma_read_completion(struct thc_device *dev)
 
 	thc_reset_dma_settings(dev);
 
+	/* The receive engine starts again just below. */
+	thc_rebuild_rx_prd(dev);
+
 	dma_set_start_bit(dev, &dev->dma_ctx->dma_config[THC_RXDMA2]);
 
 	ret = thc_interrupt_quiesce(dev, false);
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 715423453..264cd26b7 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
@@ -6,6 +6,7 @@
 
 #include <linux/bits.h>
 #include <linux/dma-mapping.h>
+#include <linux/mutex.h>
 #include <linux/sizes.h>
 #include <linux/time64.h>
 #include <linux/types.h>
@@ -123,6 +124,9 @@ struct thc_dma_configuration {
  * struct thc_dma_context - THC DMA context
  * @thc_dma_configuration: Array of all THC Channel configures
  * @use_write_interrupts: Indicate TxDMA using interrupt or polling
+ * @prd_lock: Keeps a table from being rebuilt while the DMA read path is
+ *            using it; a sleeping lock, taken inside the bus lock where the
+ *            caller holds that one.
  * @rx_max_size_en: Temp flag to indicate THC I2C Rx max input size control feature
  *                  enabled or not, only be used during SWDMA operation.
  * @rx_int_delay_en: Temp flag to indicate THC I2C Rx interrupt delay feature
@@ -132,6 +136,9 @@ struct thc_dma_context {
 	struct thc_dma_configuration dma_config[MAX_THC_DMA_CHANNEL];
 	u8 use_write_interrupts;
 
+	/* Serialises a rebuild of the tables with the DMA read path. */
+	struct mutex prd_lock;
+
 	bool rx_max_size_en;
 	bool rx_int_delay_en;
 };

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v4] HID: intel-thc-hid: intel-thc: restore PRD table capacity
  2026-11-17  0:27 Emfox Zhou
@ 2026-11-17  0:48 ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-17  0:48 UTC (permalink / raw)
  To: Emfox Zhou; +Cc: linux-input, dmitry.torokhov

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] HID: intel-thc-hid: race between ring pointer reset and
  thc_dma_read()
--

commit c665e6597716a85d87cab2130b44755b45a6cc6e
Author: Emfox Zhou <emfox@debian.org>

HID: intel-thc-hid: intel-thc: restore PRD table capacity

This commit reinitializes RxDMA and SWDMA PRD table entries from their
scatter-gather lists before starting the DMA engines and adds a mutex to
serialize PRD table rebuilds with thc_dma_read().

Link: https://lore.kernel.org/r/20261115040756.602092-1-emfox@debian.org

> 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 c80ef8c205742..413f264e30106 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
> @@ -211,12 +211,27 @@ static void thc_copy_sgls_to_prd(struct thc_device *dev,
>  {
>  	unsigned int i;
>  
> +	guard(mutex)(&dev->dma_ctx->prd_lock);
> +
> +	if (!config->is_enabled || !config->prd_tbls)
> +		return;
> +
>  	memset(config->prd_tbls, 0, array_size(PRD_TABLE_SIZE, config->prd_tbl_num));
>  
>  	for (i = 0; i < config->prd_tbl_num; i++)
>  		thc_copy_one_sgl_to_prd(dev, config, i);
>  }
>  
> +/*
> + * A frame that is never read leaves its entry short, and the engine reads
> + * the table again on every start: put the capacity back.
> + */
> +static void thc_rebuild_rx_prd(struct thc_device *dev)
> +{
> +	thc_copy_sgls_to_prd(dev, &dev->dma_ctx->dma_config[THC_RXDMA1]);
> +	thc_copy_sgls_to_prd(dev, &dev->dma_ctx->dma_config[THC_RXDMA2]);
> +}

[ ... ]

> @@ -491,6 +507,9 @@ int thc_dma_configure(struct thc_device *dev)
>  		return -EINVAL;
>  	}
>  
> +	/* An entry may still be short from a frame nobody read. */
> +	thc_rebuild_rx_prd(dev);
> +
>  	for (chan = 0; chan < MAX_THC_DMA_CHANNEL; chan++) {
>  		dma_set_prd_base_addr(dev,
>  				      dma_ctx->dma_config[chan].prd_tbls_dma_handle,
> @@ -685,6 +704,8 @@ static int thc_dma_read(struct thc_device *dev,
>  	u8 read_ptr, write_ptr, prd_table_index;
>  	int status;
>  
> +	guard(mutex)(&dev->dma_ctx->prd_lock);
> +
>  	if (!is_dma_buf_empty(dev, read_config, &read_ptr, &write_ptr)) {
>  		prd_table_index = write_ptr & THC_POINTER_MASK;
>  
> @@ -765,11 +786,16 @@ static int thc_swdma_read_start(struct thc_device *dev, void *write_buff,
>  	if (ret)
>  		return ret;
>  
> -	if (thc_wait_for_dma_pause(dev, THC_RXDMA1) || thc_wait_for_dma_pause(dev, THC_RXDMA2))
> +	if (thc_wait_for_dma_pause(dev, THC_RXDMA1) ||
> +	    thc_wait_for_dma_pause(dev, THC_RXDMA2) ||
> +	    thc_wait_for_dma_pause(dev, THC_SWDMA))
>  		return -EIO;
>  
>  	thc_reset_dma_settings(dev);
>  
> +	/* A transfer that timed out left its frame length in the entry. */
> +	thc_copy_sgls_to_prd(dev, &dev->dma_ctx->dma_config[THC_SWDMA]);

[ ... ]

> @@ -854,6 +880,9 @@ static int thc_swdma_read_completion(struct thc_device *dev)
>  
>  	thc_reset_dma_settings(dev);
>  
> +	/* The receive engine starts again just below. */
> +	thc_rebuild_rx_prd(dev);
> +
>  	dma_set_start_bit(dev, &dev->dma_ctx->dma_config[THC_RXDMA2]);
>  
>  	ret = thc_interrupt_quiesce(dev, false);

[Severity: High]
Can thc_dma_read() race with the ring pointer reset around
thc_rebuild_rx_prd() and overflow qsdev->input_buf?

prd_lock is only held inside thc_copy_sgls_to_prd(), leaving
thc_reset_dma_settings() and dma_set_start_bit() outside the lock in both
thc_dma_configure() and thc_swdma_read_start() /
thc_swdma_read_completion():

CPU0                                  CPU1 (IRQ thread)
thc_dma_configure()
  thc_reset_dma_settings()
    /* TPCRP = 0, TPCWP = 0 */
  thc_rebuild_rx_prd()
    guard(mutex)(&prd_lock)
    /* PRD len = max_packet_size */
                                      quickspi_irq_thread_handler()
                                        thc_rxdma_read()
                                          thc_dma_read()
                                            guard(mutex)(&prd_lock)
                                            !is_dma_buf_empty()
                                            read_dma_buffer()
                                              <-- copies SZ_4K-aligned entry
                                            update_write_pointer()
  dma_set_start_bit()

is_dma_buf_empty() only treats the ring as empty when the masked pointers
match and the wraparound bits differ, so between thc_reset_dma_settings()
(TPCRP == 0, TPCWP == 0) and dma_set_start_bit() (TPCWP == 0x80) it
returns false.  thc_dma_read() then copies a rebuilt PRD entry whose
length was restored to max_packet_size (aligned up to SZ_4K by
dma_set_max_packet_size()), which exceeds qsdev->input_buf in
quickspi_irq_thread_handler() and corrupts TPCWP when
update_write_pointer() races with dma_set_start_bit().

Conversely, if the reset and dma_set_start_bit() finish emptying the ring
before thc_dma_read() acquires prd_lock, is_dma_buf_empty() returns true
and thc_dma_read() returns 0 without setting *read_len, causing
quickspi_irq_thread_handler() and handle_input_report() to process stale
input_buf data and an uninitialized stack input_len.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117002755.936097-1-emfox@debian.org?part=1

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v4] HID: intel-thc-hid: intel-thc: restore PRD table capacity
  2026-11-17  0:32 [PATCH v4] HID: intel-thc-hid: intel-thc: restore PRD table capacity Emfox Zhou
@ 2026-11-17  0:51 ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-17  0:51 UTC (permalink / raw)
  To: Emfox Zhou; +Cc: linux-input, dmitry.torokhov

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] HID: intel-thc-hid: race between thc_dma_read() and DMA pointer
  reset
--

commit 16c1e87f8c9f9a7660927e9a01abdc216355c96d
Author: Emfox Zhou <emfox@debian.org>

HID: intel-thc-hid: intel-thc: restore PRD table capacity

This commit rebuilds the RxDMA and SWDMA PRD tables before starting the DMA
engines to restore entry capacities overwritten by unread frames, and adds a
mutex to serialize table rebuilds with thc_dma_read().

Link: https://lore.kernel.org/r/20261115040756.602092-1-emfox@debian.org

> 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 c80ef8c205742..413f264e30106 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
> @@ -211,12 +211,27 @@ static void thc_copy_sgls_to_prd(struct thc_device *dev,
>  {
>  	unsigned int i;
>  
> +	guard(mutex)(&dev->dma_ctx->prd_lock);
> +
> +	if (!config->is_enabled || !config->prd_tbls)
> +		return;
> +
>  	memset(config->prd_tbls, 0, array_size(PRD_TABLE_SIZE, config->prd_tbl_num));
>  
>  	for (i = 0; i < config->prd_tbl_num; i++)
>  		thc_copy_one_sgl_to_prd(dev, config, i);
>  }
>  
> +/*
> + * A frame that is never read leaves its entry short, and the engine reads
> + * the table again on every start: put the capacity back.
> + */
> +static void thc_rebuild_rx_prd(struct thc_device *dev)
> +{
> +	thc_copy_sgls_to_prd(dev, &dev->dma_ctx->dma_config[THC_RXDMA1]);
> +	thc_copy_sgls_to_prd(dev, &dev->dma_ctx->dma_config[THC_RXDMA2]);
> +}

[ ... ]

> @@ -491,6 +507,9 @@ int thc_dma_configure(struct thc_device *dev)
>  		return -EINVAL;
>  	}
>  
> +	/* An entry may still be short from a frame nobody read. */
> +	thc_rebuild_rx_prd(dev);
> +
>  	for (chan = 0; chan < MAX_THC_DMA_CHANNEL; chan++) {
>  		dma_set_prd_base_addr(dev,
>  				      dma_ctx->dma_config[chan].prd_tbls_dma_handle,
> @@ -685,6 +704,8 @@ static int thc_dma_read(struct thc_device *dev,
>  	u8 read_ptr, write_ptr, prd_table_index;
>  	int status;
>  
> +	guard(mutex)(&dev->dma_ctx->prd_lock);
> +
>  	if (!is_dma_buf_empty(dev, read_config, &read_ptr, &write_ptr)) {
>  		prd_table_index = write_ptr & THC_POINTER_MASK;
>  
> @@ -765,11 +786,16 @@ static int thc_swdma_read_start(struct thc_device *dev, void *write_buff,
>  	if (ret)
>  		return ret;
>  
> -	if (thc_wait_for_dma_pause(dev, THC_RXDMA1) || thc_wait_for_dma_pause(dev, THC_RXDMA2))
> +	if (thc_wait_for_dma_pause(dev, THC_RXDMA1) ||
> +	    thc_wait_for_dma_pause(dev, THC_RXDMA2) ||
> +	    thc_wait_for_dma_pause(dev, THC_SWDMA))
>  		return -EIO;
>  
>  	thc_reset_dma_settings(dev);
>  
> +	/* A transfer that timed out left its frame length in the entry. */
> +	thc_copy_sgls_to_prd(dev, &dev->dma_ctx->dma_config[THC_SWDMA]);
> +
>  	/*
>  	 * Max input size control feature is only available for RxDMA, it must keep disabled
>  	 * during SWDMA operation, and restore to previous state after SWDMA is done.
> @@ -854,6 +880,9 @@ static int thc_swdma_read_completion(struct thc_device *dev)
>  
>  	thc_reset_dma_settings(dev);
>  
> +	/* The receive engine starts again just below. */
> +	thc_rebuild_rx_prd(dev);
> +
>  	dma_set_start_bit(dev, &dev->dma_ctx->dma_config[THC_RXDMA2]);
>  
>  	ret = thc_interrupt_quiesce(dev, false);

[Severity: High]
Can thc_dma_read() still race with thc_reset_dma_settings() and
dma_set_start_bit() because prd_lock only covers thc_copy_sgls_to_prd()?

is_dma_buf_empty() treats the ring as empty only when the masked TPCRP and
TPCWP pointers match with different wraparound bits.  thc_reset_dma_settings()
rewinds TPCRP to 0 without setting TPCWP to THC_POINTER_WRAPAROUND (0x80), and
TPCWP is only set to 0x80 when dma_set_start_bit() restarts THC_RXDMA2.
Because both register updates happen outside prd_lock in thc_dma_configure(),
thc_swdma_read_start() and thc_swdma_read_completion(), the threaded IRQ
handler can enter thc_dma_read() in between:

CPU0                                  CPU1 (threaded IRQ)
thc_swdma_read_completion()
  thc_reset_dma_settings()
    /* TPCRP = 0, TPCWP != 0x80 */
  thc_rebuild_rx_prd()
    thc_copy_sgls_to_prd()
      lock(&dma_ctx->prd_lock)
      /* restores entry.len = SZ_4K */
      unlock(&dma_ctx->prd_lock)
                                      quickspi_irq_thread_handler()
                                        thc_rxdma_read()
                                          thc_dma_read()
                                            lock(&dma_ctx->prd_lock)
                                            is_dma_buf_empty() -> false
                                            read_dma_buffer()  <-- buffer overflow
                                            update_write_pointer()
                                            unlock(&dma_ctx->prd_lock)
  dma_set_start_bit()

When thc_dma_read() sees the non-empty pointers after thc_rebuild_rx_prd(),
read_dma_buffer() copies a rebuilt PRD table entry whose length was restored
to max_packet_size (aligned to SZ_4K) instead of a frame length written by
hardware, overflowing qsdev->input_buf in intel-quickspi when
HIDSPI_INPUT_BODY_SIZE(max_input_len) is smaller than SZ_4K and corrupting
TPCWP.  The same window remains open across the entire SWDMA transfer after
thc_swdma_read_start() calls thc_reset_dma_settings() while THC_RXDMA2 is
stopped.

Even if the reset and dma_set_start_bit() complete before the woken IRQ thread
calls thc_dma_read(), is_dma_buf_empty() now returns true on the first call,
so thc_dma_read() returns 0 without setting *read_len:

drivers/hid/intel-thc-hid/intel-quickspi/pci-quickspi.c:quickspi_irq_thread_handler() {
	...
		while (!read_finished) {
			ret = thc_rxdma_read(qsdev->thc_hw, THC_RXDMA2, qsdev->input_buf,
					     &input_len, &read_finished);
			if (ret) {
				err_recover = 1;
				goto end;
			}

			quickspi_handle_input_data(qsdev, input_len);
		}
}

quickspi_irq_thread_handler() then passes an uninitialized input_len and stale
qsdev->input_buf to quickspi_handle_input_data() (and quicki2c's
handle_input_report() replays stale data in qcdev->input_buf).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117003247.940876-1-emfox@debian.org?part=1

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-17  0:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17  0:32 [PATCH v4] HID: intel-thc-hid: intel-thc: restore PRD table capacity Emfox Zhou
2026-11-17  0:51 ` sashiko-bot
  -- strict thread matches above, loose matches on Subject: below --
2026-11-17  0:27 Emfox Zhou
2026-11-17  0:48 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox