All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Emfox Zhou" <emfox@debian.org>
Cc: linux-input@vger.kernel.org, dmitry.torokhov@gmail.com
Subject: Re: [PATCH v5] HID: intel-thc-hid: intel-thc: restore PRD table capacity
Date: Tue, 17 Nov 2026 09:46:55 +0000	[thread overview]
Message-ID: <sashiko-outbox-218522@kernel.org> (raw)
In-Reply-To: <20261117093212.4122283-1-emfox@debian.org>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] HID: intel-thc-hid: unlocked DMA stop and pointer rewind races
  with thc_dma_read()
- [High] HID: intel-thc-hid: callers process stale buffer when
  thc_dma_read() reads 0 bytes
--

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

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

This commit reinitializes PRD tables before DMA engines are started,
serializes table rebuilds and ring reads with a mutex, and tracks whether
each DMA channel is running so stopped rings are not read.

Link: https://lore.kernel.org/r/20261117003247.940876-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..e315989a4c8fd 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
[ ... ]
> @@ -266,6 +302,14 @@ static int setup_dma_buffers(struct thc_device *dev,
>  
>  static void thc_reset_dma_settings(struct thc_device *dev)
>  {
> +	struct thc_dma_context *dma_ctx = dev->dma_ctx;
> +	int chan;
> +
> +	/* The rewind below discards the queue, so no read may start. */
> +	if (dma_ctx)
> +		for (chan = 0; chan < MAX_THC_DMA_CHANNEL; chan++)
> +			WRITE_ONCE(dma_ctx->dma_config[chan].is_started, false);
> +
>  	/* Stop all DMA channels and reset DMA read pointers */
>  	regmap_write_bits(dev->thc_regmap, THC_M_PRT_READ_DMA_CNTRL_1_OFFSET,
>  			  THC_M_PRT_READ_DMA_CNTRL_START, 0);

[Severity: High]
Can thc_reset_dma_settings() rewind the hardware read pointer while
thc_dma_read() is already past the is_started check?

thc_wait_for_dma_pause(), thc_reset_dma_settings() and
thc_dma_unconfigure() clear is_started and stop or rewind the channel
without taking dev->dma_ctx->prd_lock, while the threaded IRQ handler
drains THC_RXDMA2 outside dev->thc_bus_lock:

CPU0                                  CPU1
quickspi_irq_thread_handler()         thc_swdma_read()
  thc_rxdma_read()                      thc_swdma_read_start()
    thc_dma_read()                        thc_wait_for_dma_pause(THC_RXDMA2)
      guard(mutex)(&prd_lock)               is_started = false
      if (!READ_ONCE(is_started))         thc_reset_dma_settings()
                                            /* rewinds TPCRP to 0 */
      is_dma_buf_empty()
        /* TPCRP == 0, TPCWP == 0x81 */
      read_dma_buffer()               <-- reads unwritten PRD entry

Because thc_copy_one_sgl_to_prd() restored that entry's length to
max_packet_size (ALIGN(size, SZ_4K)), read_dma_buffer() copies up to a
4K-aligned buffer into read_buff (overflowing qsdev->input_buf in
intel-quickspi when max_input_len is smaller than SZ_4K) and
update_write_pointer() corrupts TPCWP on the stopped ring.  Similarly,
on probe error unwind, quicki2c_dma_deinit() and quickspi_dma_deinit()
call thc_dma_unconfigure() and thc_dma_release() before interrupts are
quiesced, which can unmap and free sgls and prd_tbls while an in-flight
thc_dma_read() is still using them under prd_lock.

Acquiring dev->dma_ctx->prd_lock when stopping the engines, rewinding
the pointers, and releasing the buffers would serialize against an
in-progress thc_dma_read().

[ ... ]
> @@ -685,6 +735,21 @@ static int thc_dma_read(struct thc_device *dev,
>  	u8 read_ptr, write_ptr, prd_table_index;
>  	int status;
>  
> +	/* The caller may look at this when nothing was read. */
> +	*read_len = 0;
> +
> +	guard(mutex)(&dev->dma_ctx->prd_lock);
> +
> +	/*
> +	 * Nothing in the ring is ours until the engine is started: after a
> +	 * reset the pointers alone say nothing about what was written.
> +	 */
> +	if (!READ_ONCE(read_config->is_started)) {
> +		if (read_finished)
> +			*read_finished = 1;
> +		return 0;
> +	}
> +
>  	if (!is_dma_buf_empty(dev, read_config, &read_ptr, &write_ptr)) {
>  		prd_table_index = write_ptr & THC_POINTER_MASK;

[Severity: High]
When thc_dma_read() returns 0 with *read_len = 0 and *read_finished = 1,
won't callers process stale data in read_buff?

If the threaded IRQ handler drains THC_RXDMA2 while the engine is paused
for an SWDMA transfer or reset, thc_rxdma_read() returns 0 without
touching the buffer, and neither caller checks for a zero length:

drivers/hid/intel-thc-hid/intel-quicki2c/pci-quicki2c.c:handle_input_report() {
	while (!rx_dma_finished) {
		ret = thc_rxdma_read(qcdev->thc_hw, THC_RXDMA2,
				     (u8 *)pkt, &report_len,
				     &rx_dma_finished);
		if (ret)
			return ret;

		if (!pkt->len) {
...
		quicki2c_hid_send_report(qcdev, pkt->data, qcdev->input_len,
					 HIDI2C_DATA_LEN(le16_to_cpu(pkt->len)));
	}
}

handle_input_report() will either replay the previous packet in
qcdev->input_buf to the HID core or, if pkt->len was 0, falsely mark
qcdev->reset_ack = true.  In intel-quickspi,
quickspi_irq_thread_handler() passes input_len == 0 to
quickspi_handle_input_data(), logging a false report length error, and
thc_swdma_read() returns 0 to quicki2c_get_report() and
quicki2c_get_report_descriptor() without populating read_buff.

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

      reply	other threads:[~2026-11-17  9:46 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17  9:32 [PATCH v5] HID: intel-thc-hid: intel-thc: restore PRD table capacity Emfox Zhou
2026-11-17  9:46 ` sashiko-bot [this message]

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-218522@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.