From: Even Xu <even.xu@intel.com>
To: bentiss@kernel.org, jikos@kernel.org
Cc: srinivas.pandruvada@linux.intel.com, linux-input@vger.kernel.org,
linux-kernel@vger.kernel.org, Even Xu <even.xu@intel.com>,
Rui Zhang <rui1.zhang@intel.com>
Subject: [PATCH 1/4] HID: Intel-thc-hid: Intel-thc: Add safe DMA read interfaces
Date: Mon, 16 Nov 2026 16:27:57 +0800 [thread overview]
Message-ID: <20261116082805.3986615-2-even.xu@intel.com> (raw)
In-Reply-To: <20261116082805.3986615-1-even.xu@intel.com>
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
next prev parent reply other threads:[~2026-11-16 8:27 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-16 8:27 [PATCH 0/4] Fix potential THC DMA issues Even Xu
2026-11-16 8:27 ` Even Xu [this message]
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 ` [PATCH 3/4] HID: Intel-thc-hid: Intel-quicki2c: Use safe DMA reads Even Xu
2026-11-16 8:28 ` [PATCH 4/4] HID: Intel-thc-hid: Intel-thc: Restore RXDMA after SWDMA operation errors Even Xu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261116082805.3986615-2-even.xu@intel.com \
--to=even.xu@intel.com \
--cc=bentiss@kernel.org \
--cc=jikos@kernel.org \
--cc=linux-input@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rui1.zhang@intel.com \
--cc=srinivas.pandruvada@linux.intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox