Linux Input/HID development
 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 v11 3/6] HID: intel-thc-hid: intel-ipts: receive reports
Date: Thu, 03 Dec 2026 02:26:04 +0000	[thread overview]
Message-ID: <sashiko-outbox-246991@kernel.org> (raw)
In-Reply-To: <20261203020433.2631656-3-emfox@debian.org>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] HID: intel-thc-hid: intel-ipts: IRQ storm and livelock in error
  recovery
- [High] HID: intel-thc-hid: intel-ipts: race with IRQ and recovery in
  thc_ipts_port_stop()
- [Medium] HID: intel-thc-hid: intel-ipts: invalid wait context in
  thc_ipts_hardirq()
--

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

HID: intel-thc-hid: intel-ipts: receive reports

This commit adds DMA configuration, threaded interrupt handling, frame
transmission and reception, and error recovery work to the Intel IPTS
transport driver.

> diff --git a/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c b/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c
> index 5d3d8d4005b8f..9ef4b3cfa733c 100644
> --- a/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c
> +++ b/drivers/hid/intel-thc-hid/intel-ipts/ipts-protocol.c

[ ... ]

> +irqreturn_t thc_ipts_hardirq(int irq, void *data)
> +{
> +	struct thc_ipts *idev = data;
> +
> +	/*
> +	 * Nothing of ours is pending: the cause belongs to a sharer. A reset
> +	 * in flight owns the status registers and acknowledges nothing.
> +	 */
> +	if (READ_ONCE(idev->state) < THC_IPTS_DMA ||
> +	    READ_ONCE(idev->recovering))
> +		return IRQ_NONE;
> +
> +	/* Level triggered and shared: mask first, or it stays asserted. */\
> +	thc_interrupt_enable(idev->thc, false);
> +
> +	/*
> +	 * A teardown still needs this thread: it delivers the completion a
> +	 * pending write holds the bus lock for.
> +	 */
> +	return IRQ_WAKE_THREAD;
> +}

[Severity: Medium]
Can calling thc_interrupt_enable() from thc_ipts_hardirq() trigger an
invalid wait context warning under CONFIG_PROVE_RAW_LOCK_NESTING or sleep
in hardirq context on PREEMPT_RT?

thc_ipts_probe() requests a legacy PCI_IRQ_INTX vector with IRQF_SHARED |
IRQF_COND_ONESHOT.  When the line is shared with another handler that uses
IRQF_ONESHOT, __setup_irq() propagates IRQF_ONESHOT to this action, so
__handle_irq_event_percpu() does not mark the hardirq as threaded for
lockdep and leaves the wait context at LD_WAIT_SPIN.

thc_interrupt_enable() then calls regmap_write_bits() on dev->thc_regmap,
which is configured without .use_raw_spinlock = true and therefore takes a
non-raw spinlock_t:

drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c:
static const struct regmap_config thc_regmap_cfg = {
	...
	.cache_type = REGCACHE_NONE,
	.fast_io = true,
};

[ ... ]

> +void thc_ipts_recover(struct work_struct *work)
> +{
> +	struct thc_ipts *idev = container_of(work, struct thc_ipts,
> +					     recover_work);
> +
> +	/*
> +	 * A write on the wire needs the interrupt this reset is about to mask:
> +	 * let it finish first.
> +	 */
> +	mutex_lock(&idev->tx_lock);
> +	WRITE_ONCE(idev->recovering, true);
> +	mutex_unlock(&idev->tx_lock);
> +
> +	/*
> +	 * Mask, then wait out a run already inside: the status registers belong
> +	 * to the reset.
> +	 */
> +	thc_interrupt_enable(idev->thc, false);
> +	synchronize_irq(idev->irq);
> +	/*
> +	 * A run may have unmasked on its way out: mask again.
> +	 */
> +	thc_interrupt_enable(idev->thc, false);
> +
> +	if (thc_rxdma_reset(idev->thc)) {
> +		/*
> +		 * Nothing left to reset: stay masked and fail with -ENODEV.
> +		 */
> +		dev_err(idev->dev, "cannot reset the receive engine\n");
> +		WRITE_ONCE(idev->state, THC_IPTS_CONFIG);
> +		WRITE_ONCE(idev->recover_seq, READ_ONCE(idev->recover_seq) + 1);
> +		WRITE_ONCE(idev->recover_pending, false);
> +		WRITE_ONCE(idev->recovering, false);
> +		wake_up(&idev->wq);
> +		return;
> +	}
> +
> +	/*
> +	 * Clear the queued flag before the running one.
> +	 */
> +	WRITE_ONCE(idev->recover_seq, READ_ONCE(idev->recover_seq) + 1);
> +	WRITE_ONCE(idev->recover_pending, false);
> +	WRITE_ONCE(idev->recovering, false);
> +
> +	/* A request waiting out the reset is looking at that flag. */
> +	wake_up(&idev->wq);
> +
> +	if (!READ_ONCE(idev->stopping))
> +		thc_interrupt_enable(idev->thc, true);
> +}
> +
> +irqreturn_t thc_ipts_thread(int irq, void *data)
> +{
> +	struct thc_ipts *idev = data;
> +	irqreturn_t ret = IRQ_NONE;
> +	bool need_recover = false;
> +	bool reported = false;
> +	int pass;
> +
> +	/*
> +	 * Remember this thread: a request from it would wait for a completion
> +	 * only it delivers.
> +	 */
> +	WRITE_ONCE(idev->irq_thread, current);
> +
> +	/*
> +	 * A sharer can queue a second run: mask again.
> +	 */
> +	thc_interrupt_enable(idev->thc, false);
> +
> +	/*
> +	 * The port stays masked while reading: causes only leave status bits.
> +	 */
> +	for (pass = 0; pass < THC_IPTS_IRQ_PASSES; pass++) {
> +		int mask;
> +
> +		/*
> +		 * A reset owns the status registers: acknowledge nothing here.
> +		 */
> +		if (READ_ONCE(idev->recovering))
> +			break;
> +
> +		mask = thc_interrupt_handler(idev->thc);
> +
> +		if (mask == BIT(THC_UNKNOWN_INT))
> +			break;
> +
> +		ret = IRQ_HANDLED;
> +		if (mask & (BIT(THC_FATAL_ERR_INT) | BIT(THC_TXN_ERR_INT))) {
> +			/*
> +			 * An overrun repeats until the reset: report the first one.
> +			 */
> +			if (!reported && !READ_ONCE(idev->recover_pending)) {
> +				dev_err(idev->dev,
> +					"transfer error, resetting the engine\n");
> +				reported = true;
> +			}
> +			/* The teardown has its own reset to wait for. */\
> +			if (!READ_ONCE(idev->stopping))
> +				need_recover = true;
> +			/*
> +			 * The core stops at the first cause; a completion behind it would time
> +			 * out.
> +			 */
> +			continue;
> +		}
> +
> +		/*
> +		 * A reset is queued or running: read the status, not the ring.
> +		 */
> +		if (need_recover || READ_ONCE(idev->recover_pending) ||
> +		    READ_ONCE(idev->recovering) ||
> +		    READ_ONCE(idev->stopping))
> +			continue;
> +
> +		if (mask & BIT(THC_RXDMA2_INT) && thc_ipts_read_frames(idev))
> +			need_recover = true;
> +	}
> +
> +	/*
> +	 * Only queue the reset: a frame read now would corrupt the ring.
> +	 */
> +	if (need_recover)
> +		thc_ipts_request_recover(idev);
> +
> +	/*
> +	 * Keep the line open unless a reset is running: a write on the wire needs
> +	 * this handler to deliver its completion.
> +	 */
> +	if (!READ_ONCE(idev->recovering))
> +		thc_interrupt_enable(idev->thc, true);
> +
> +	return ret;
> +}

[Severity: High]
Can error recovery trigger a level-triggered hardirq storm or starve
recover_work behind the threaded IRQ handler?

kworker                           <hardirq> / irq/thc_ipts
thc_ipts_thread()
  need_recover = true
  thc_ipts_request_recover()
    recover_pending = true
    schedule_work(&recover_work)
  if (!idev->recovering)
    thc_interrupt_enable(..., true)
thc_ipts_recover()
  WRITE_ONCE(idev->recovering, true)
                                  <hardirq> (overrun still asserted)
                                    thc_ipts_hardirq()
                                      if (idev->recovering)
                                        return IRQ_NONE;
                                  <hardirq> (line stays asserted)
                                    thc_ipts_hardirq()
                                      return IRQ_NONE;  <-- IRQ storm
  thc_interrupt_enable(..., false)

First, thc_ipts_thread() re-enables hardware interrupts on exit whenever
idev->recovering is still false, even after queuing recover_work with
need_recover / recover_pending set.  Because an RxDMA overrun or stall
persists in hardware until thc_rxdma_reset() runs, unmasking the port
immediately re-triggers the interrupt and wakes the SCHED_FIFO IRQ thread
again (or spins while thc_ipts_recover() waits on idev->tx_lock),
preempting and starving the SCHED_NORMAL system_percpu_wq worker on the
same CPU.  Also, continuing across all 10 passes of THC_IPTS_IRQ_PASSES on
a repeating overrun does not reach the TxDMA completion check in
thc_interrupt_handler(), because thc_interrupt_handler() returns early on
THC_TXN_ERR_INT and logs an unratelimited dev_err() on each pass.

Second, thc_ipts_recover() sets idev->recovering = true while hardware
interrupts are still enabled in THC_M_PRT_INT_EN_OFFSET (or while a
concurrent thc_ipts_thread() is about to call thc_interrupt_enable(thc,
true)).  If the level-triggered INTx line asserts before
thc_interrupt_enable(idev->thc, false) runs, thc_ipts_hardirq() sees
idev->recovering == true and returns IRQ_NONE without masking the port,
re-entering thc_ipts_hardirq() until the spurious IRQ detector disables
the shared interrupt line.

[ ... ]

> @@ -202,11 +638,38 @@ int thc_ipts_port_start(struct thc_ipts *idev)
>   */
>  void thc_ipts_port_stop(struct thc_ipts *idev)
>  {
> +	/*
> +	 * Refuse requests, then wait for one inside thc_dma_write(): its
> +	 * completion comes through this handler.
> +	 */
> +	WRITE_ONCE(idev->stopping, true);
> +	wake_up(&idev->wq);
> +	mutex_lock(&idev->tx_lock);
> +	mutex_unlock(&idev->tx_lock);
> +
> +	thc_interrupt_enable(idev->thc, false);
> +	synchronize_irq(idev->irq);
> +	cancel_work_sync(&idev->recover_work);
> +
> +	/* A run that was already in flight may have unmasked on its way out. */\
> +	thc_interrupt_enable(idev->thc, false);
> +
> +	/* Stop the controller and keep it in reset until the next start. */\
>  	if (READ_ONCE(idev->state) >= THC_IPTS_CONFIG)
>  		thc_ipts_touch_set(idev, false);
>  
> -	/* Keep the touch IC in reset until the next start. */
>  	thc_port_reset(idev->thc, true);
> -	thc_interrupt_quiesce(idev->thc, true);
> +
> +	/*
> +	 * The tables are not devm-managed; release them even if the engine did
> +	 * not stop.
> +	 */
> +	if (thc_interrupt_quiesce(idev->thc, true))
> +		dev_warn(idev->dev, "engine did not stop, releasing anyway\n");
> +	thc_ipts_dma_stop(idev);
>  	WRITE_ONCE(idev->state, THC_IPTS_DOWN);
> +
> +	/* Wait for a handler that may still look at the ring. */
> +	synchronize_irq(idev->irq);
> +	thc_interrupt_enable(idev->thc, false);
>  }

[Severity: High]
Can thc_ipts_thread() and thc_ipts_recover() race with teardown in
thc_ipts_port_stop() after idev->tx_lock has been drained?

CPU0                              CPU1 (shared INTx / IRQ thread)
thc_ipts_port_stop()
  WRITE_ONCE(idev->stopping, true)
  mutex_lock(&idev->tx_lock)
  mutex_unlock(&idev->tx_lock)
  thc_interrupt_enable(..., false)
  synchronize_irq(idev->irq)
  cancel_work_sync(&recover_work)
  thc_interrupt_enable(..., false)
                                  <hardirq> (sharer on INTx line)
                                    thc_ipts_hardirq()
                                      /* state == THC_IPTS_DMA */
                                      return IRQ_WAKE_THREAD;
  thc_ipts_touch_set(..., false)
  thc_port_reset(..., true)
                                  thc_ipts_thread()
                                    thc_interrupt_handler(idev->thc)
                                    if (!idev->recovering)
                                      thc_interrupt_enable(..., true)
  thc_ipts_dma_stop(idev)           <-- concurrent MMIO & IRQ re-enabled

Once mutex_unlock(&idev->tx_lock) completes, no write is in flight, yet
idev->state remains THC_IPTS_DMA until thc_ipts_dma_stop() and
idev->recovering is false.  Because the INTx line is requested with
IRQF_SHARED, any interrupt from a device sharing the line causes
thc_ipts_hardirq() to return IRQ_WAKE_THREAD.  thc_ipts_thread() also
unmasks hardware interrupts on exit when idev->recovering is false (even
when idev->stopping is true), so a run finishing during the first
synchronize_irq() can re-enable interrupts before the second
thc_interrupt_enable(idev->thc, false), which has no synchronize_irq()
after it.

In addition, if recover_work is already running when cancel_work_sync() is
called, thc_ipts_recover() does not check idev->stopping before calling
thc_rxdma_reset(idev->thc), which resets and restarts RxDMA2 and
unquiesces device interrupts in the middle of teardown.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796215044.git.emfox@debian.org?part=3

  reply	other threads:[~2026-12-03  2:26 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  2:04 [PATCH v11 0/6] HID: intel-thc-hid: intel-ipts: support THC IPTS ports Emfox Zhou
2026-12-03  2:04 ` [PATCH v11 1/6] HID: intel-thc-hid: intel-thc: expose the port operations IPTS needs Emfox Zhou
2026-12-03  2:11   ` sashiko-bot
2026-12-03  2:04 ` [PATCH v11 2/6] HID: intel-thc-hid: intel-ipts: bring up the port Emfox Zhou
2026-12-03  2:15   ` sashiko-bot
2026-12-03  2:04 ` [PATCH v11 3/6] HID: intel-thc-hid: intel-ipts: receive reports Emfox Zhou
2026-12-03  2:26   ` sashiko-bot [this message]
2026-12-03  2:04 ` [PATCH v11 4/6] HID: intel-thc-hid: intel-ipts: add the HID device Emfox Zhou
2026-12-03  2:04 ` [PATCH v11 5/6] HID: intel-thc-hid: intel-ipts: handle system sleep Emfox Zhou
2026-12-03  2:04 ` [PATCH v11 6/6] HID: intel-thc-hid: intel-ipts: document the IPTS mode Emfox Zhou

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-246991@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox