Linux PCI subsystem development
 help / color / mirror / Atom feed
From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: sashiko-reviews@lists.linux.dev
Cc: linux-pci@vger.kernel.org
Subject: Re: [PATCH 3/5] PCI: Place resources to either edge of the window
Date: Thu, 24 Sep 2026 14:03:33 +0300 (EEST)	[thread overview]
Message-ID: <9aa21437-c8c1-8b51-62aa-aeaa64854f1f@linux.intel.com> (raw)
In-Reply-To: <20260923132848.B7A451F000FF@smtp.kernel.org>

[-- Attachment #1: Type: text/plain, Size: 4813 bytes --]

On Wed, 23 Sep 2026, sashiko-bot@kernel.org wrote:

> Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
> - [High] pci: undefined behavior from 64-bit resource sizes on 32-bit systems
> - [Medium] pci: res->end + 1 overflow breaks natural alignment logic
> --
> 
> commit 65c3dea851ed27dd4bf3576e0a9387eaa7359798
> Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
> Subject: PCI: Place resources to either edge of the window
> 
> This commit updates the PCI resource assignment phase to use a window edge
> aware resource assignment algorithm. It alters the greedy assignment logic
> to place resources either to the left or right edge of the window to avoid
> fragmenting contiguous free space.
> 
> > diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c
> > index eacce9e2486bf..1ab5d167ab5c6 100644
> > --- a/drivers/pci/setup-res.c
> > +++ b/drivers/pci/setup-res.c
> [ ... ]
> > @@ -262,16 +265,80 @@ 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 this code inadvertently truncate 64-bit resource sizes on 32-bit systems?
> 
> When a 32-bit kernel is built with a 64-bit phys_addr_t (like x86 PAE or
> ARM LPAE), passing a 64-bit size into rounddown_pow_of_two() casts it to an
> unsigned long. If the size is a multiple of 4GB (which is common for large
> GPU BARs), the lower 32 bits are silently truncated to 0, causing powof2 to
> be evaluated as 0.

This is a pre-existing issue. PCI core already uses __ffs() which is based 
on unsigned long (at least on x86).

There's also a serious discussion in x86 space about future of 32-bit 
support where keeping it wastes development effort:

https://lore.kernel.org/all/bd252483-fb0a-4818-bfaf-b9b6ebd187fe@intel.com/

The non-trivial cost here is avoiding the problematic generic helpers 
(custom coding them) or fixing ffs() and round*_pow_of_two() when input 
size > sizeof(unsigned long), for ability to run >=4GB BARs on a 32-bit 
platform. Yeah, modern GPUs have big BARs but running them on a 32-bit 
platform doesn't sound that great combination.


That being said, I think round*_pow_of_two() interface should be made
to return same type as the input because it simply doesn't make sense to 
convert the value to some other type. I briefly looked into that earlier 
but it requires auditting that all the callers can handle that (IIRC, 
there were even some build failures from making just the type change and
there could be logic flaws as well if something relies on type expansion
to unsigned long).

> > +	natural_start = ALIGN(res->start, powof2);
> > +	if (natural_start >= ALIGN_DOWN(res->end + 1, powof2)) {
> 
> [Severity: Medium]
> Could this boundary check overflow if the window ends exactly at ~0U?
> 
> If a 32-bit system maps PCI space just below the 4GB boundary, res->end
> could be 0xFFFFFFFF. In that scenario, res->end + 1 wraps to 0, which
> causes ALIGN_DOWN(0, powof2) to return 0. The check natural_start >= 0 then
> unconditionally evaluates to true for unsigned types, incorrectly forcing
> the algorithm to halve powof2 and potentially failing to pack memory windows
> tightly.

This will be fixed by subtracting 1 from the aligned result to underflow 
it back from 0 to ~0.

> > +		powof2 = max(powof2 / 2, 1U);
> > +		natural_start = ALIGN(res->start, powof2);
> > +	}
> > +
> > +	if (natural_start) {
> > +		*max_align <<= __ffs(natural_start);
> 
> [Severity: High]
> Does this trigger undefined behavior when passing a truncated 64-bit value?
> 
> Similar to rounddown_pow_of_two(), __ffs() takes an unsigned long. If a
> 64-bit natural_start is a multiple of 4GB, the cast truncates it to 0.
> Passing 0 to __ffs() invokes undefined behavior, which can lead to
> unpredictable results or kernel panics during boot (such as garbage shift
> amounts or arch-specific trap instructions on zero inputs).

Sashiko knows no shame with rather bold claims. I'm pretty sure the answer 
should have fit into its context window, no matter how small that was. ;-)

> > +	} 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;
> > +}
> 
> 

-- 
 i.

  reply	other threads:[~2026-09-24 11:03 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20260923131757.7792-1-ilpo.jarvinen@linux.intel.com>
2026-09-23 13:17 ` [PATCH 2/5] PCI: Fix nesting windows with remainder at the left edge Ilpo Järvinen
2026-09-23 13:32   ` sashiko-bot
2026-09-23 13:17 ` [PATCH 3/5] PCI: Place resources to either edge of the window Ilpo Järvinen
2026-09-23 13:28   ` sashiko-bot
2026-09-24 11:03     ` Ilpo Järvinen [this message]
2026-09-23 13:17 ` [PATCH 4/5] PCI: Fix composite resource sizing Ilpo Järvinen
2026-09-23 13:31   ` sashiko-bot
2026-09-23 13:39     ` Ilpo Järvinen
2026-09-23 13:17 ` [PATCH 5/5] PCI/quirks: Avoid certain BAR 0 address with igb Ilpo Järvinen
2026-09-23 13:29   ` sashiko-bot
2026-09-24 20:20   ` Bjorn Helgaas
2026-09-28 12:20     ` Ilpo Järvinen
2026-09-28 13:19       ` David Laight
2026-09-28 14:30         ` Ilpo Järvinen
2026-09-28 18:56           ` David Laight
2026-09-26  2:00 ` [PATCH 0/5] PCI: Resource placement algorithm fixes Nikolas Joshua Britton
2026-09-28 12:34   ` 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=9aa21437-c8c1-8b51-62aa-aeaa64854f1f@linux.intel.com \
    --to=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