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 v7 3/5] HID: intel-thc-hid: intel-ipts: add the HID device
Date: Wed, 18 Nov 2026 06:00:02 +0000	[thread overview]
Message-ID: <sashiko-outbox-220188@kernel.org> (raw)
In-Reply-To: <20261118054004.363766-4-emfox@debian.org>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] HID: intel-thc-hid: device-wide in_thread in thc_ipts_send()
  causes -EBUSY
- [Medium] HID: intel-thc-hid: early recover_seq read in
  thc_ipts_get_feature()
- [Medium] HID: intel-thc-hid: IRQ unmask race in thc_ipts_thread() on
  reset and stop
--

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

HID: intel-thc-hid: intel-ipts: add the HID device

This commit registers a HID device for the Intel IPTS transport and
implements report descriptor parsing, input report forwarding, and raw
feature and output report requests over THC DMA.

> diff --git a/drivers/hid/intel-thc-hid/intel-ipts/ipts.c b/drivers/hid/intel-thc-hid/intel-ipts/ipts.c
> index 17f6f664e95b5..347a705719e6e 100644
> --- a/drivers/hid/intel-thc-hid/intel-ipts/ipts.c
> +++ b/drivers/hid/intel-thc-hid/intel-ipts/ipts.c
[ ... ]
> @@ -144,8 +146,86 @@ static int thc_ipts_touch_set(struct thc_ipts *idev, bool enable)
>  				 &touch);
>  }
>  
> +/*
> + * Frames are built in @idev->tx_frame, so the lock is taken here rather than
> + * by the callers: none of them may hold it across a wait.
> + */
> +static int thc_ipts_send(struct thc_ipts *idev, u32 code, const void *data,
> +			 size_t len)
> +{
> +	size_t padded = round_up(len, 4);
> +	u8 *frame = idev->tx_frame;
> +
> +	/* The completion of a write is delivered by the threaded handler, so
> +	 * one issued from there would wait out its timeout: refuse it instead,
> +	 * and let the caller retry from its own context.
> +	 */
> +	if (READ_ONCE(idev->in_thread))
> +		return -EBUSY;

[Severity: High]
Does checking the device-wide idev->in_thread flag reject normal process-context
callers on other CPUs whenever thc_ipts_thread() is running?

thc_ipts_thread() sets idev->in_thread for its entire execution, including
when woken by a shared INTx interrupt or while delivering touch input
reports.  In thc_ipts_port_start(), interrupts are enabled while
idev->dma_ready is true before calling thc_ipts_hid_add() ->
hid_add_device() -> thc_ipts_hid_parse():

CPU0                              CPU1
thc_ipts_port_start()
  thc_interrupt_enable(..., true)
                                  thc_ipts_thread()
                                    WRITE_ONCE(idev->in_thread, true)
  thc_ipts_hid_add()
    ...
      thc_ipts_hid_parse()
        thc_ipts_send()
          if (READ_ONCE(idev->in_thread))
            return -EBUSY;  <-- aborts hid_add_device() and probe

Because thc_ipts_hid_parse() only retries -EAGAIN and -ETIMEDOUT, -EBUSY
immediately fails HID registration and driver probe, and at runtime hidraw
feature and output requests fail with -EBUSY whenever they overlap with
incoming touch frames.

[ ... ]
> @@ -399,17 +525,19 @@ irqreturn_t thc_ipts_thread(int irq, void *data)
>  			need_recover = true;
>  	}
>  
> -	/* The work item resets the engine and unmasks again; a frame read
> -	 * meanwhile would corrupt the ring.
> +	/*
> +	 * The work item resets the engine in its own time; a frame read
> +	 * meanwhile would corrupt the ring, so only queue the reset.
>  	 */
> -	if (need_recover) {
> +	if (need_recover)
>  		thc_ipts_request_recover(idev);
> -		return IRQ_HANDLED;
> -	}
>  
> -	/* A reset unmasks when it is done. A teardown masks on its own, so the
> -	 * line stays open here: a write already on the wire needs the
> -	 * completion this handler delivers.
> +	/*
> +	 * A reset unmasks when it is done, and a teardown masks on its own.
> +	 * Leaving the line open here is what a write already on the wire
> +	 * needs: this handler is where its completion is delivered, and
> +	 * masking it away would make that write time out over the bus lock
> +	 * the reset is waiting for.
>  	 */
>  	if (!READ_ONCE(idev->recovering))
>  		thc_interrupt_enable(idev->thc, true);

[Severity: Medium]
Can thc_ipts_thread() unmask interrupts while thc_ipts_recover() or
thc_ipts_port_stop() expects the port to stay masked?

In the recovery path, thc_ipts_recover() does not mask interrupts again
after synchronize_irq(), so a handler that checked idev->recovering just
before it was set leaves interrupts enabled across thc_rxdma_reset() and
after a reset failure:

CPU0 (thc_ipts_thread)            CPU1 (thc_ipts_recover)
if (!READ_ONCE(idev->recovering))
                                  WRITE_ONCE(idev->recovering, true)
                                  thc_interrupt_enable(idev->thc, false)
  thc_interrupt_enable(..., true)
                                  synchronize_irq(idev->irq)
                                  thc_rxdma_reset(idev->thc)  <-- IRQ unmasked

Inaddition, thc_ipts_thread() does not check idev->stopping before calling
thc_interrupt_enable(idev->thc, true), so any shared interrupt that wakes
the thread in thc_ipts_port_stop() before thc_ipts_dma_stop() clears
idev->dma_ready will re-enable device interrupts during teardown.

[ ... ]
> @@ -419,6 +547,274 @@ irqreturn_t thc_ipts_thread(int irq, void *data)
[ ... ]
> +/* A feature report comes back as a frame of its own: the request waits. */
> +static int thc_ipts_get_feature(struct thc_ipts *idev, unsigned char reportnum,
> +				u8 *buf, size_t len)
> +{
[ ... ]
> +	seq = READ_ONCE(idev->recover_seq);
> +
> +	/* A refusal happens before anything is written, so the channel stays
> +	 * open for it; a timeout is a different matter, see below.
> +	 */
> +	ret = thc_ipts_send(idev, THC_IPTS_TX_GET_FEATURE, &reportnum, 1);
[ ... ]
> +	wait_event_timeout(idev->wq, READ_ONCE(idev->feature_done) ||
> +			   READ_ONCE(idev->recover_pending) ||
> +			   READ_ONCE(idev->recovering) ||
> +			   READ_ONCE(idev->stopping), timeout);
> +
> +	mutex_lock(&idev->feature_lock);
> +	/* An answer that landed while the wait was expiring counts too. */
> +	if (!idev->feature_done) {
> +		/* A reset discards what the engines had queued, so an answer
> +		 * that was on its way is gone and cannot be waited for.
> +		 */
> +		if (READ_ONCE(idev->recover_seq) != seq ||
> +		    READ_ONCE(idev->recover_pending) ||
> +		    READ_ONCE(idev->recovering) ||
> +		    READ_ONCE(idev->stopping))
> +			ret = -EIO;
> +		else
> +			ret = -ETIMEDOUT;
> +
> +		idev->feature_pending = false;
> +		/* Only a timeout leaves a frame out there: after a reset the
> +		 * answer was dropped and the channel is still in step.
> +		 */
> +		if (ret == -ETIMEDOUT)
> +			WRITE_ONCE(idev->feature_off, true);

[Severity: Medium]
Can sampling idev->recover_seq before thc_ipts_send() leave idev->feature_off
false after a feature request times out on the wire?

CPU0 (thc_ipts_get_feature)       CPU1 (thc_ipts_recover)
seq = READ_ONCE(idev->recover_seq)
                                  WRITE_ONCE(idev->recover_seq, ... + 1)
                                  WRITE_ONCE(idev->recover_pending, false)
                                  WRITE_ONCE(idev->recovering, false)
thc_ipts_send()
  guard(mutex)(&idev->tx_lock)
  thc_dma_write()  <-- sent after reset
wait_event_timeout()  <-- times out
if (READ_ONCE(idev->recover_seq) != seq ...)
  ret = -EIO;  <-- feature_off not set

Because thc_ipts_recover() finishes before thc_ipts_send() checks
idev->recover_pending and idev->recovering under idev->tx_lock, the request
is transmitted after the reset, but a subsequent timeout sees
idev->recover_seq != seq and leaves the feature channel open while an
unanswered request is still in flight.

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

  reply	other threads:[~2026-11-18  6:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  5:39 [PATCH v7 0/5] HID: intel-thc-hid: intel-ipts: support THC IPTS ports Emfox Zhou
2026-11-18  5:40 ` [PATCH v7 1/5] HID: intel-thc-hid: intel-ipts: bring up the port Emfox Zhou
2026-11-18  5:40 ` [PATCH v7 2/5] HID: intel-thc-hid: intel-ipts: receive reports Emfox Zhou
2026-11-18  6:06   ` sashiko-bot
2026-11-18  5:40 ` [PATCH v7 3/5] HID: intel-thc-hid: intel-ipts: add the HID device Emfox Zhou
2026-11-18  6:00   ` sashiko-bot [this message]
2026-11-18  5:40 ` [PATCH v7 4/5] HID: intel-thc-hid: intel-ipts: handle system sleep Emfox Zhou
2026-11-18  5:40 ` [PATCH v7 5/5] 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-220188@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.