From: sashiko-bot@kernel.org
To: "Emfox Zhou" <emfox@debian.org>
Cc: linux-input@vger.kernel.org, dmitry.torokhov@gmail.com
Subject: Re: [PATCH v4] HID: intel-thc-hid: intel-thc: restore PRD table capacity
Date: Tue, 17 Nov 2026 00:48:04 +0000 [thread overview]
Message-ID: <sashiko-outbox-218024@kernel.org> (raw)
In-Reply-To: <20261117002755.936097-1-emfox@debian.org>
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
next prev parent reply other threads:[~2026-11-17 0:48 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 0:27 [PATCH v4] HID: intel-thc-hid: intel-thc: restore PRD table capacity Emfox Zhou
2026-11-17 0:48 ` sashiko-bot [this message]
-- strict thread matches above, loose matches on Subject: below --
2026-11-17 0:32 Emfox Zhou
2026-11-17 0:51 ` sashiko-bot
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=sashiko-outbox-218024@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=dmitry.torokhov@gmail.com \
--cc=emfox@debian.org \
--cc=linux-input@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.