Linux PCI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Yazen Ghannam" <yazen.ghannam@amd.com>, linux-pci@vger.kernel.org
Subject: Re: [PATCH v3 4/6] PCI/FLIT: Add support for the Extended Flit Error Counter
Date: Fri, 04 Dec 2026 18:40:28 +0000	[thread overview]
Message-ID: <sashiko-outbox-250439@kernel.org> (raw)
In-Reply-To: <20261204181556.1418843-5-yazen.ghannam@amd.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] PCI/FLIT: extended Flit error counter never cleared via CLEAR
  bit
- [Medium] PCI/FLIT: slot_reset enables CTRL1 before restoring
  ERR_CNTR_CTRL and CTRL2
- [Medium] PCI/FLIT: missing PCI_POSSIBLE_ERROR() checks in
  flit_read_cntr_cap()
- [Medium] PCI/FLIT: flit_cntr_enable() omits trigger when updating
  saved state buffer
- [Low] PCI/FLIT: pci_flit_init() lacks PCI_POSSIBLE_ERROR() check on
  ctrl read
--

commit d722ed5d293f00a76cfef74bc7d0010083c5e137
Author: Yazen Ghannam <yazen.ghannam@amd.com>

PCI/FLIT: Add support for the Extended Flit Error Counter

This commit adds support for the Capability Version 2h Extended Flit Error
Counter in drivers/pci/pcie/flit.c, selecting it over the base counter when
supported and extending register access and save/restore handling to cover
the 32-bit control and status registers.

> diff --git a/drivers/pci/pcie/flit.c b/drivers/pci/pcie/flit.c
> index 00409dd7bb956..97395b6942389 100644
> --- a/drivers/pci/pcie/flit.c
> +++ b/drivers/pci/pcie/flit.c

[ ... ]

> @@ -93,10 +113,46 @@ static bool flit_is_native(struct pci_dev *pdev)
>  	return host->native_flit && host->native_aer;
>  }
>  
> +static u32 flit_read_cntr_cap(struct pci_dev *pdev)
> +{
> +	u32 hdr, cap;
> +
> +	pci_read_config_dword(pdev, pdev->flit_cap, &hdr);
> +	if (PCI_EXT_CAP_VER(hdr) < 2)
> +		return 0;
> +
> +	pci_read_config_dword(pdev, pdev->flit_cap + PCI_FLIT_ERR_CNTR_CAP, &cap);
> +
> +	return cap;
> +}
> +
> +static void flit_select_cntr(struct pci_dev *pdev)
> +{
> +	struct flit_info *info = pdev->flit_info;
> +
> +	info->cntr = &flit_base_cntr;
> +	if (!(info->cntr_cap & PCI_FLIT_ERR_CNTR_CAP_EXT))
> +		return;
> +
> +	/* Running both counters, or both interrupts, is undefined */
> +	pci_clear_and_set_config_word(pdev,
> +				      pdev->flit_cap + PCI_FLIT_ERR_CNTR_CTRL,
> +				      PCI_FLIT_ERR_CNTR_CTRL_EN |
> +				      PCI_FLIT_ERR_CNTR_CTRL_INTR_EN, 0);
> +
> +	info->cntr = &flit_ext_cntr;
> +}

[Severity: Medium]
Should flit_read_cntr_cap() check PCI_POSSIBLE_ERROR() on both hdr and
cap?  If a config read returns 0xffffffff, PCI_EXT_CAP_VER(hdr) is 0xf,
bypassing the version check and returning 0xffffffff so flit_select_cntr()
selects flit_ext_cntr.

On a Capability Version 1h device, later accesses to offsets 0x40 through
0x48 would then reach past the end of the Flit Logging capability and
could corrupt an adjacent extended capability.

[ ... ]

> @@ -130,17 +192,24 @@ void pci_flit_init(struct pci_dev *pdev)
>  
>  	pdev->flit_info = info;
>  
> +	info->cntr_cap = flit_read_cntr_cap(pdev);
> +
>  	spin_lock_init(&info->lock);
>  
> -	info->cntr = &flit_base_cntr;
> +	flit_select_cntr(pdev);
>  	info->enable = info->cntr->en | info->cntr->intr_en;
>  
>  	ctrl = flit_cntr_read(pdev, info->cntr->ctrl);
>  	trigger = field_get(info->cntr->trigger, ctrl);
>  	info->trigger = trigger ? trigger : FLIT_DEFAULT_TRIGGER;

[Severity: Low]
Does pci_flit_init() need a PCI_POSSIBLE_ERROR(ctrl) check here?  On an
all-ones error response, field_get() extracts 0xff or 0xffff instead of
falling back to FLIT_DEFAULT_TRIGGER, which can never be exceeded and
silently disables trigger events.

[ ... ]

> @@ -181,6 +250,17 @@ void pci_save_flit_state(struct pci_dev *pdev)
>  	pci_read_config_word(pdev, flit + PCI_FLIT_ERR_CNTR_CTRL, &ctrl);
>  	slots[FLIT_SAVE_CNTR_CTRL] = ctrl;
>  
> +	if (flit_ext(info)) {
> +		pci_read_config_dword(pdev, flit + PCI_FLIT_EXT_CNTR_CTRL1,
> +				      &ctrl1);
> +		pci_read_config_dword(pdev, flit + PCI_FLIT_EXT_CNTR_CTRL2,
> +				      &slots[FLIT_SAVE_EXT_CNTR_CTRL2]);
> +
> +		/* Reads as zero, but restoring a one would clear the counter */
> +		ctrl1 &= ~PCI_FLIT_EXT_CNTR_CTRL1_CLEAR;
> +		slots[FLIT_SAVE_EXT_CNTR_CTRL1] = ctrl1;
> +	}

[Severity: High]
Is the Extended Flit Error Counter ever cleared via
PCI_FLIT_EXT_CNTR_CTRL1_CLEAR?

For the base counter, flit_isr() writes cntr->intr_gen to
PCI_FLIT_ERR_CNTR_STA, zeroing the RW 8-bit count along with INTR_GEN.
For the extended counter, the 16-bit count in PCI_FLIT_EXT_CNTR_STA is
read-only and needs PCI_FLIT_EXT_CNTR_CTRL1_CLEAR.

That bit is never set anywhere in flit.c, and decrement rates in
PCI_FLIT_EXT_CNTR_CTRL2 are never programmed.  After the first trigger,
flit_cntr_resume() re-enables the counter while the count still exceeds
info->trigger, causing an interrupt storm or lost notifications.

Stale counts left by firmware are likewise never cleared in
flit_cntr_enable().

[ ... ]

> @@ -203,10 +283,21 @@ void pci_restore_flit_state(struct pci_dev *pdev)
>  	if (!slots)
>  		return;
>  
> -	/* Event servicing can run during a runtime resume or slot reset */
> +	/*
> +	 * Event servicing can run during a runtime resume or slot reset. Write
> +	 * Flit Error Counter Control first so the two counters never overlap,
> +	 * and the decrement rates before the counter they apply to.
> +	 */
>  	spin_lock_irqsave(&info->lock, flags);
>  	pci_write_config_word(pdev, flit + PCI_FLIT_ERR_CNTR_CTRL,
>  			      slots[FLIT_SAVE_CNTR_CTRL]);
> +
> +	if (flit_ext(info)) {
> +		pci_write_config_dword(pdev, flit + PCI_FLIT_EXT_CNTR_CTRL2,
> +				       slots[FLIT_SAVE_EXT_CNTR_CTRL2]);
> +		pci_write_config_dword(pdev, flit + PCI_FLIT_EXT_CNTR_CTRL1,
> +				       slots[FLIT_SAVE_EXT_CNTR_CTRL1]);
> +	}
>  	spin_unlock_irqrestore(&info->lock, flags);
>  }

[Severity: Medium]
Can slot_reset violate the restore ordering documented here by enabling
PCI_FLIT_EXT_CNTR_CTRL1 before PCI_FLIT_ERR_CNTR_CTRL and
PCI_FLIT_EXT_CNTR_CTRL2 are restored?

drivers/pci/pcie/portdrv.c:pcie_portdrv_slot_reset() {
	size_t off = offsetof(struct pcie_port_service_driver, slot_reset);
	device_for_each_child(&dev->dev, &off, pcie_port_device_iter);

	pci_restore_state(dev);
	return PCI_ERS_RESULT_RECOVERED;
}

flit_cntr_restore() calls flit_cntr_enable() first, enabling CTRL1 while
CTRL2 still holds post-reset defaults.  pci_restore_flit_state() then
writes ERR_CNTR_CTRL and CTRL2 without clearing CTRL1 EN first (which
also happens on a non-reset resume).

In addition, if a Flit interrupt fires between flit_cntr_restore() and
pci_restore_flit_state(), restoring slots[FLIT_SAVE_EXT_CNTR_CTRL1]
unconditionally writes EN=1 and prematurely re-enables a paused counter
while flit_ist() is still draining the log.

[Severity: Medium]
Can pci_restore_flit_state() clobber the programmed trigger threshold back
to 0 after a slot reset?

drivers/pci/pcie/flit.c:flit_cntr_enable() {
    ...
	__flit_cntr_ctrl_update(pdev, cntr->trigger | ctrl,
				field_prep(cntr->trigger, info->trigger) |
				info->enable);
	__flit_save_state_update(pdev, cntr->slot, ctrl, info->enable);
    ...
}

If a port boots with a hardware trigger of 0 and pci_save_state() runs
before flit_probe() (for example with driver_async_probe=flit), the save
buffer captures trigger as 0.

Because __flit_save_state_update() omits cntr->trigger,
pci_restore_flit_state() during pcie_portdrv_slot_reset() overwrites the
programmed trigger with 0 and stops events from firing.

Updating cntr->trigger alongside ctrl in __flit_save_state_update() would
keep the save buffer consistent.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204181556.1418843-1-yazen.ghannam@amd.com?part=4

  reply	other threads:[~2026-12-04 18:40 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 18:15 [PATCH v3 0/6] PCIe Flit Logging Ext Capability Support Yazen Ghannam
2026-12-04 18:15 ` [PATCH v3 1/6] PCI/portdrv: Group PCI Exp Cap services into a single mask Yazen Ghannam
2026-12-04 18:15 ` [PATCH v3 2/6] PCI: Add pci_clear_and_set_config_word() Yazen Ghannam
2026-12-04 18:35   ` sashiko-bot
2026-12-04 18:15 ` [PATCH v3 3/6] PCI/FLIT: Add support for Flit Logging Extended Capability Yazen Ghannam
2026-12-04 18:42   ` sashiko-bot
2026-12-04 18:15 ` [PATCH v3 4/6] PCI/FLIT: Add support for the Extended Flit Error Counter Yazen Ghannam
2026-12-04 18:40   ` sashiko-bot [this message]
2026-12-04 18:15 ` [PATCH v3 5/6] PCI/FLIT: Signal Flit Error Counter events with ERR_COR Yazen Ghannam
2026-12-04 18:46   ` sashiko-bot
2026-12-04 18:15 ` [PATCH v3 6/6] PCI/FLIT: Manage Flit Logging on Endpoint Upstream Ports Yazen Ghannam
2026-12-04 18:53   ` sashiko-bot
2026-12-04 18:53 ` [PATCH v3 0/6] PCIe Flit Logging Ext Capability Support 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-250439@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=yazen.ghannam@amd.com \
    /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