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

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] HID: intel-thc-hid: intel-ipts: race on recover_seq in
  thc_ipts_get_feature()
- [Medium] HID: intel-thc-hid: intel-ipts: stale recovery state in
  thc_ipts_recover()
--

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

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

This commit registers a HID device for Intel THC ports in IPTS mode and
implements report descriptor parsing, input report forwarding, and raw
feature and output report transfers.

> diff --git a/drivers/hid/intel-thc-hid/intel-ipts/ipts.c b/drivers/hid/intel-thc-hid/intel-ipts/ipts.c
> index e7896b16f1ada..f519ba2f7ce3d 100644
> --- a/drivers/hid/intel-thc-hid/intel-ipts/ipts.c
> +++ b/drivers/hid/intel-thc-hid/intel-ipts/ipts.c

[ ... ]

> @@ -318,6 +440,8 @@ void thc_ipts_recover(struct work_struct *work)
>  	if (thc_rxdma_reset(idev->thc)) {
>  		/* Nothing left: keep the port masked until it restarts. */
>  		dev_err(idev->dev, "cannot reset the receive engine\n");
> +		/* It stays masked, so the waiters learn it from the flags. */
> +		wake_up(&idev->wq);
>  		return;
>  	}

[Severity: Medium]
When thc_rxdma_reset() fails, does leaving idev->recover_pending and
idev->recovering set while idev->dma_ready remains true keep waiters and
callers of thc_ipts_send() stuck in a transient recovery state?

thc_ipts_hid_parse() waits on thc_ipts_idle(idev), which requires both
recover_pending and recovering to be false, so wake_up(&idev->wq) here does
not wake it and it waits out all THC_IPTS_REQ_TRIES timeouts.  In addition,
thc_ipts_send() checks recover_pending and recovering before dma_ready, so
subsequent sends keep returning -EAGAIN instead of -ENODEV.

[ ... ]

> +/* 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)
> +{
> +	unsigned long timeout = msecs_to_jiffies(THC_IPTS_REQ_TIMEOUT_MS);
> +	unsigned int seq;
> +	int ret;

[ ... ]

> +	ret = thc_ipts_send(idev, THC_IPTS_TX_GET_FEATURE, &reportnum, 1);
> +	if (ret) {
> +		mutex_lock(&idev->feature_lock);
> +		idev->feature_pending = false;
> +
> +		/* The start bit was written before the wait, so the frame may
> +		 * well be on the wire: its answer cannot be told from the next
> +		 * request's one.
> +		 */
> +		if (ret == -ETIMEDOUT)
> +			WRITE_ONCE(idev->feature_off, true);
> +		mutex_unlock(&idev->feature_lock);
> +
> +		if (ret == -ETIMEDOUT)
> +			dev_err(idev->dev, "feature report %u not acknowledged\n",
> +				reportnum);
> +
> +		return ret;
> +	}
> +
> +	/* Taken after the write, or a reset that ran before it would be
> +	 * mistaken for one that dropped this frame.
> +	 */
> +	seq = READ_ONCE(idev->recover_seq);
> +
> +	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 a reset that was queued during thc_ipts_send() finish before
thc_ipts_get_feature() samples idev->recover_seq, causing the dropped
feature response to be treated as a timeout?

CPU0                                  CPU1 (kworker)
thc_ipts_get_feature()
  thc_ipts_send()
    lock(&idev->tx_lock)
    thc_dma_write()
      ... (Rx error queues recover_work)
                                      thc_ipts_recover()
                                        lock(&idev->tx_lock)
    unlock(&idev->tx_lock)
                                        unlock(&idev->tx_lock)
                                        thc_rxdma_reset()
                                        idev->recover_seq++
                                        idev->recover_pending = false
                                        idev->recovering = false
  seq = READ_ONCE(idev->recover_seq)
  wait_event_timeout(...)
  /* times out with recover_seq == seq */
  idev->feature_off = true            <-- disables feature reports

Because idev->recover_seq is read after thc_ipts_send() drops
idev->tx_lock, thc_ipts_recover() can reset the receive engine (dropping
the answer), increment recover_seq, and clear recover_pending and
recovering before seq is sampled.  thc_ipts_get_feature() then sees no
change in recover_seq, returns -ETIMEDOUT instead of -EIO, and sets
idev->feature_off = true, shutting off all further feature requests until
the port restarts.

Also, wait_event_timeout() does not check READ_ONCE(idev->recover_seq) !=
seq in its condition, so if thc_ipts_recover() completes before the wait
condition is evaluated, the caller sleeps for the full timeout before
returning -EIO.

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

  reply	other threads:[~2026-11-18  7:14 UTC|newest]

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