mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Bjorn Helgaas <helgaas@kernel.org>
To: lirongqing <lirongqing@baidu.com>
Cc: "Bjorn Helgaas" <bhelgaas@google.com>,
	"Philipp Stanner" <pstanner@redhat.com>,
	"Krzysztof Wilczyński" <kwilczynski@kernel.org>,
	linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] PCI: Fix pcim_iomap_regions() error path rollback and leak
Date: Mon, 9 Nov 2026 17:33:37 -0600	[thread overview]
Message-ID: <20261109233337.GA347778@bhelgaas> (raw)
In-Reply-To: <20261109113136.2295-1-lirongqing@baidu.com>

On Mon, Nov 09, 2026 at 07:31:36PM +0800, lirongqing wrote:
> From: Li RongQing <lirongqing@baidu.com>
> 
> pcim_iomap_regions() has two problems in its error path.
> 
> First, the rollback loop walks every bar index below the failing one
> and unmaps it, regardless of whether that bar is in the mask this call
> was asked to map. pcim_iounmap_region() matches devres entries by
> (type, bar) only, so it can release a mapping that an earlier
> pcim_iomap_region()/pcim_iomap_regions() call made. The driver is then
> left with an __iomem pointer that has been unmapped and a legacy table
> entry cleared. Restrict the rollback to bars in mask.
> 
> Second, when pcim_add_mapping_to_legacy_table() fails, the current
> bar's mapping is not released: the rollback loop starts at --bar, so
> the failing bar itself is skipped, and the mapping stays registered
> until the driver detaches. Release it on this path as well.
> 
> Fixes: bbaff68bf4a4 ("PCI: Add managed partial-BAR request and map infrastructure")
> Signed-off-by: Li RongQing <lirongqing@baidu.com>

Hoping for a Reviewed-by from Philipp.

If there's a v2, s/bar/BAR/ above to match other usage.  If no v2,
I'll tweak that when applying.

> ---
>  drivers/pci/devres.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/pci/devres.c b/drivers/pci/devres.c
> index f075e78..9bdb4de 100644
> --- a/drivers/pci/devres.c
> +++ b/drivers/pci/devres.c
> @@ -689,14 +689,18 @@ int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)
>  			goto err;
>  		}
>  		ret = pcim_add_mapping_to_legacy_table(pdev, mapping, bar);
> -		if (ret != 0)
> +		if (ret != 0) {
> +			pcim_iounmap_region(pdev, bar);
>  			goto err;
> +		}
>  	}
>  
>  	return 0;
>  
>  err:
>  	while (--bar >= 0) {
> +		if (!mask_contains_bar(mask, bar))
> +			continue;
>  		pcim_iounmap_region(pdev, bar);
>  		pcim_remove_bar_from_legacy_table(pdev, bar);
>  	}
> -- 
> 2.9.4
> 

      reply	other threads:[~2026-11-09 23:33 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-09 11:31 lirongqing
2026-11-09 23:33 ` Bjorn Helgaas [this message]

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=20261109233337.GA347778@bhelgaas \
    --to=helgaas@kernel.org \
    --cc=bhelgaas@google.com \
    --cc=kwilczynski@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=lirongqing@baidu.com \
    --cc=pstanner@redhat.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

all inboxes | Powered by JetHome®