Linux PCI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
Cc: linux-pci@vger.kernel.org
Subject: Re: [PATCH v2 5/7] PCI: Place resources to either edge of the window
Date: Fri, 02 Oct 2026 11:46:10 +0000	[thread overview]
Message-ID: <20261002114611.197B21F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20261002113319.6652-6-ilpo.jarvinen@linux.intel.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] pci: 64-bit truncation in pci_max_natural_size() on 32-bit archs
--

commit a3daecf300277b71086888c6fc930ab2932e9d1b
Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>

PCI: Place resources to either edge of the window

This introduces a new greedy algorithm for PCI resource assignment that
is aware of window edges. It aims to avoid fragmenting the free space
when placing composite resources like bridge windows and VF BARs.

> diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c
> index 2ca3784aa323f..4c73bf6e0f706 100644
> --- a/drivers/pci/setup-res.c
> +++ b/drivers/pci/setup-res.c
[ ... ]
> @@ -262,16 +265,89 @@ resource_size_t pci_resource_alignment(const struct pci_dev *dev,
>  	return resource_alignment(res);
>  }
>  
> +static resource_size_t pci_max_natural_size(const struct resource *res,
> +					    resource_size_t *max_align)
> +{
> +	resource_size_t size = resource_size(res);
> +	resource_size_t powof2, natural_start;
> +
> +	*max_align = 1;
> +	if (!size)
> +		return 0;
> +
> +	powof2 = rounddown_pow_of_two(size);

[Severity: High]
Does pci_max_natural_size() in drivers/pci/setup-res.c silently truncate the
64-bit size to 32 bits on 32-bit architectures?

If CONFIG_PHYS_ADDR_T_64BIT=y is enabled on a 32-bit architecture and a
resource is >= 4GB, rounddown_pow_of_two() will receive a 0-truncated value
since it expects an unsigned long. Can this result in a shift by -1
(undefined behavior) and cause incorrect BAR size and alignment calculations?

> +	natural_start = ALIGN(res->start, powof2);
> +	/* end = ~0 first overflows, then -1 brings it back */
> +	if (natural_start >= ALIGN_DOWN(res->end + 1, powof2) - 1) {
> +		powof2 = max(powof2 / 2, 1U);
> +		natural_start = ALIGN(res->start, powof2);
> +	}
> +
> +	if (natural_start) {
> +		*max_align <<= __ffs(natural_start);

[Severity: High]
Can pci_max_natural_size() in drivers/pci/setup-res.c invoke undefined
behavior on 32-bit architectures if the start address is aligned to a 4GB
boundary?

The __ffs() function expects an unsigned long. If the 64-bit natural_start
has its lower 32 bits as zero, this implicitly casts to a 32-bit zero, which
triggers undefined behavior in __builtin_ctzl(0).

> +	} else {
> +		/*
> +		 * Zero address has infinite alignment, return the largest
> +		 * representable number even if it's not a power of two.
> +		 */
> +		*max_align = RESOURCE_SIZE_MAX;
> +	}
> +
> +	return powof2;
> +}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261002113319.6652-1-ilpo.jarvinen@linux.intel.com?part=5

  reply	other threads:[~2026-10-02 11:46 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-02 11:33 [PATCH v2 0/7] PCI: Resource placement algorithm fixes Ilpo Järvinen
2026-10-02 11:33 ` [PATCH v2 1/7] resource: Mark free space assigned Ilpo Järvinen
2026-10-02 11:43   ` sashiko-bot
2026-10-02 11:33 ` [PATCH v2 2/7] PCI/parisc: Clean up resource debug print & use dynamic debug Ilpo Järvinen
2026-10-02 11:43   ` sashiko-bot
2026-10-02 11:33 ` [PATCH v2 3/7] PCI: Honor alignment overrides Ilpo Järvinen
2026-10-02 11:46   ` Jani Nikula
2026-10-02 11:49   ` sashiko-bot
2026-10-02 11:33 ` [PATCH v2 4/7] PCI: Fix nesting windows with remainder at the left edge Ilpo Järvinen
2026-10-02 11:42   ` sashiko-bot
2026-10-02 11:33 ` [PATCH v2 5/7] PCI: Place resources to either edge of the window Ilpo Järvinen
2026-10-02 11:46   ` sashiko-bot [this message]
2026-10-02 11:33 ` [PATCH v2 6/7] PCI: Fix composite resource sizing Ilpo Järvinen
2026-10-02 11:51   ` sashiko-bot
2026-10-02 11:33 ` [PATCH v2 7/7] PCI/quirks: Avoid certain address on Genoa systems Ilpo Järvinen
2026-10-02 11:48   ` sashiko-bot
2026-10-02 13:47     ` Ilpo Järvinen
2026-10-02 13:12   ` Mario Limonciello
2026-10-04 15:44   ` Borislav Petkov
2026-10-05 18:47   ` Bjorn Helgaas
2026-10-06 12:02     ` Ilpo Järvinen

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=20261002114611.197B21F000FF@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=linux-pci@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