{"id":2215469,"url":"http://patchwork.ozlabs.org/api/patches/2215469/?format=json","web_url":"http://patchwork.ozlabs.org/project/linuxppc-dev/patch/20260324165633.4583-10-ilpo.jarvinen@linux.intel.com/","project":{"id":2,"url":"http://patchwork.ozlabs.org/api/projects/2/?format=json","name":"Linux PPC development","link_name":"linuxppc-dev","list_id":"linuxppc-dev.lists.ozlabs.org","list_email":"linuxppc-dev@lists.ozlabs.org","web_url":"https://github.com/linuxppc/wiki/wiki","scm_url":"https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git","webscm_url":"https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git/","list_archive_url":"https://lore.kernel.org/linuxppc-dev/","list_archive_url_format":"https://lore.kernel.org/linuxppc-dev/{}/","commit_url_format":"https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git/commit/?id={}"},"msgid":"<20260324165633.4583-10-ilpo.jarvinen@linux.intel.com>","list_archive_url":"https://lore.kernel.org/linuxppc-dev/20260324165633.4583-10-ilpo.jarvinen@linux.intel.com/","date":"2026-03-24T16:56:32","name":"[09/10] PCI: Align head space better","commit_ref":null,"pull_url":null,"state":"handled-elsewhere","archived":false,"hash":"e178c15932663e65c66aba6932c6054a3c647309","submitter":{"id":83553,"url":"http://patchwork.ozlabs.org/api/people/83553/?format=json","name":"Ilpo Järvinen","email":"ilpo.jarvinen@linux.intel.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/linuxppc-dev/patch/20260324165633.4583-10-ilpo.jarvinen@linux.intel.com/mbox/","series":[{"id":497317,"url":"http://patchwork.ozlabs.org/api/series/497317/?format=json","web_url":"http://patchwork.ozlabs.org/project/linuxppc-dev/list/?series=497317","date":"2026-03-24T16:56:23","name":"PCI: Improve head free space usage","version":1,"mbox":"http://patchwork.ozlabs.org/series/497317/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2215469/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2215469/checks/","tags":{},"related":[],"headers":{"Return-Path":"\n <linuxppc-dev+bounces-18735-incoming=patchwork.ozlabs.org@lists.ozlabs.org>","X-Original-To":["incoming@patchwork.ozlabs.org","linuxppc-dev@lists.ozlabs.org"],"Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256\n header.s=Intel header.b=j7uc9T+U;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=lists.ozlabs.org\n (client-ip=112.213.38.117; helo=lists.ozlabs.org;\n envelope-from=linuxppc-dev+bounces-18735-incoming=patchwork.ozlabs.org@lists.ozlabs.org;\n receiver=patchwork.ozlabs.org)","lists.ozlabs.org;\n arc=none smtp.remote-ip=198.175.65.15","lists.ozlabs.org;\n dmarc=pass (p=none dis=none) header.from=linux.intel.com","lists.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256\n header.s=Intel header.b=j7uc9T+U;\n\tdkim-atps=neutral","lists.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=linux.intel.com\n (client-ip=198.175.65.15; helo=mgamail.intel.com;\n envelope-from=ilpo.jarvinen@linux.intel.com; receiver=lists.ozlabs.org)"],"Received":["from lists.ozlabs.org (lists.ozlabs.org [112.213.38.117])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4fgGWk1sd0z1xy1\n\tfor <incoming@patchwork.ozlabs.org>; Wed, 25 Mar 2026 04:00:50 +1100 (AEDT)","from boromir.ozlabs.org (localhost [127.0.0.1])\n\tby lists.ozlabs.org (Postfix) with ESMTP id 4fgGWk0vdXz2yqF;\n\tWed, 25 Mar 2026 04:00:50 +1100 (AEDT)","from mgamail.intel.com (mgamail.intel.com [198.175.65.15])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby lists.ozlabs.org (Postfix) with ESMTPS id 4fgGWj0ND9z2yng\n\tfor <linuxppc-dev@lists.ozlabs.org>; Wed, 25 Mar 2026 04:00:48 +1100 (AEDT)","from orviesa002.jf.intel.com ([10.64.159.142])\n  by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384;\n 24 Mar 2026 10:00:46 -0700","from ijarvine-mobl1.ger.corp.intel.com (HELO localhost)\n ([10.245.244.217])\n  by orviesa002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384;\n 24 Mar 2026 10:00:37 -0700"],"ARC-Seal":"i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1774371650;\n\tcv=none;\n b=egNzbfuvTJiHiLIB3OzjHh9rsLdPJQpvuQMkxQ3MtoVHvlYdQ5Y0CYI9u+i9+8yVmCR/1oLvr03/VKmU0MANKFH+8v3fNuV8ICU5QW0vq6Md4jWcDOF2vN4V9ff7cM3y5twaasyNbOYEghn73RRNMCfFEiR6DSNr1SLfru2ilIaYOBN2d79nrmaT+yzvSI3Y5nAKB9s1R72Gl+Sd/mzrmohsaYQ91VO3ImQhIT/MgTBt9JbYV1Dga2y2vBQy1UCAexbrez6m1NMtkPYxfe1T/H/ImUUJoO0Qhx82uEhX7EVWC9NqawHPSQNlYM+2n1En+LbZ5Jx6p/I7Nc8DjOxvwA==","ARC-Message-Signature":"i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707;\n\tt=1774371650; c=relaxed/relaxed;\n\tbh=yeghdZvYCJlWBn2Zu3U/aK11OnWx9GTVKC80/ZR268U=;\n\th=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References:\n\t MIME-Version:Content-Type;\n b=dDWzTIfS2TEsAcQt5D2SeO6GDeZdhIoykf3+a6+uAPUDnccdiDBgjnNoajNTN6I5u3iMaCxkAPtOBDwRcoFtn/HyIAOmEqtnFLZ/FrME5Seoi++UBE3PtXqZtg7oRzgJ8Vb/Ff9FZw+QiJapCloVT4/7xtvUvtRcGEWVNwFMmv6EVyprdVK9Eaov2VGLwfRhSTrChrR2xQ1qm/YaX2l8j2Tld9Fmey6cUQVEyXd83hCLIq/rNbxoygipcoQnkbXWLGoxIoGmgEAf8LaTRX4BJ3JnbqXacm5gs848pzuUnRg98bzxWKOVokKb3FYx5nImAovpdryGnDjhl+NaONKaSg==","ARC-Authentication-Results":"i=1; lists.ozlabs.org;\n dmarc=pass (p=none dis=none) header.from=linux.intel.com;\n dkim=pass (2048-bit key;\n unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256\n header.s=Intel header.b=j7uc9T+U; dkim-atps=neutral;\n spf=pass (client-ip=198.175.65.15; helo=mgamail.intel.com;\n envelope-from=ilpo.jarvinen@linux.intel.com;\n receiver=lists.ozlabs.org) smtp.mailfrom=linux.intel.com","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple;\n  d=intel.com; i=@intel.com; q=dns/txt; s=Intel;\n  t=1774371649; x=1805907649;\n  h=from:to:cc:subject:date:message-id:in-reply-to:\n   references:mime-version:content-transfer-encoding;\n  bh=vNoPxtdd4BxfeIP2GD5+Lm5ztZ/3WHvkEpVD97Kr7m4=;\n  b=j7uc9T+U2apd2CKTPtTyjH7HzYZxojysHUkqmtAnA//VBGO7CB8LlpKQ\n   PG9nB++4TLBq/b3THKpvETZi9NRd8JeuQUad2KPutWrfq/FvLi3USuWsC\n   DxZp6BUf+8jC0xO1/OOe9TEwZiHkiM7OvPIUV1g5RP77IeYSwKdie4f4f\n   a3DxtbOdnKIkLH823Qs5DvQpoG5/jP9WcqCt1gVS2QJc5rMKBWxM73LVM\n   KdFY1nFNJr8KtA9Y37jtlnV3PgG1qk+2SPzt9avARUSl/5uGOr8r8L1ot\n   mQ/76EZoSYakt2h68IRD2g51tm0d+BvXz5hNiIwkl59Zm3kFLwGrr9z9P\n   A==;","X-CSE-ConnectionGUID":["GT2I73dOSFS5MmHwboTqCA==","ApYT6ADHR4inoKAd9O076A=="],"X-CSE-MsgGUID":["DkjfL+3yRaiSLlJ64KPB6g==","yzqK2g13S9m6CJEqxk0sPQ=="],"X-IronPort-AV":["E=McAfee;i=\"6800,10657,11739\"; a=\"78991795\"","E=Sophos;i=\"6.23,138,1770624000\";\n   d=\"scan'208\";a=\"78991795\"","E=Sophos;i=\"6.23,138,1770624000\";\n   d=\"scan'208\";a=\"254911560\""],"X-ExtLoop1":"1","From":"=?utf-8?q?Ilpo_J=C3=A4rvinen?= <ilpo.jarvinen@linux.intel.com>","To":"linux-pci@vger.kernel.org,\n\tBjorn Helgaas <bhelgaas@google.com>,\n\tGuenter Roeck <linux@roeck-us.net>,\n\tlinux-alpha@vger.kernel.org,\n\tlinux-arm-kernel@lists.infradead.org,\n\tlinux-m68k@lists.linux-m68k.org,\n\tlinux-mips@vger.kernel.org,\n\tlinux-parisc@vger.kernel.org,\n\tlinuxppc-dev@lists.ozlabs.org,\n\tlinux-s390@vger.kernel.org,\n\tlinux-sh@vger.kernel.org,\n\tRussell King <linux@armlinux.org.uk>,\n\tGeert Uytterhoeven <geert@linux-m68k.org>,\n\tThomas Bogendoerfer <tsbogend@alpha.franken.de>,\n\t\"James E.J. Bottomley\" <James.Bottomley@HansenPartnership.com>,\n\tHelge Deller <deller@gmx.de>,\n\tMichael Ellerman <mpe@ellerman.id.au>,\n\tThomas Gleixner <tglx@kernel.org>,\n\tIngo Molnar <mingo@redhat.com>,\n\tBorislav Petkov <bp@alien8.de>,\n\tDave Hansen <dave.hansen@linux.intel.com>,\n\t\"H. Peter Anvin\" <hpa@zytor.com>,\n\tChris Zankel <chris@zankel.net>,\n\tMax Filippov <jcmvbkbc@gmail.com>,\n\tMadhavan Srinivasan <maddy@linux.ibm.com>,\n\tYoshinori Sato <ysato@users.sourceforge.jp>,\n\tRich Felker <dalias@libc.org>,\n\tJohn Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>,\n\tNicholas Piggin <npiggin@gmail.com>,\n\t\"Christophe Leroy (CS GROUP)\" <chleroy@kernel.org>,\n\tx86@kernel.org,\n\tlinux-kernel@vger.kernel.org","Cc":"=?utf-8?q?Ilpo_J=C3=A4rvinen?= <ilpo.jarvinen@linux.intel.com>","Subject":"[PATCH 09/10] PCI: Align head space better","Date":"Tue, 24 Mar 2026 18:56:32 +0200","Message-Id":"<20260324165633.4583-10-ilpo.jarvinen@linux.intel.com>","X-Mailer":"git-send-email 2.39.5","In-Reply-To":"<20260324165633.4583-1-ilpo.jarvinen@linux.intel.com>","References":"<20260324165633.4583-1-ilpo.jarvinen@linux.intel.com>","X-Mailing-List":"linuxppc-dev@lists.ozlabs.org","List-Id":"<linuxppc-dev.lists.ozlabs.org>","List-Help":"<mailto:linuxppc-dev+help@lists.ozlabs.org>","List-Owner":"<mailto:linuxppc-dev+owner@lists.ozlabs.org>","List-Post":"<mailto:linuxppc-dev@lists.ozlabs.org>","List-Archive":"<https://lore.kernel.org/linuxppc-dev/>,\n  <https://lists.ozlabs.org/pipermail/linuxppc-dev/>","List-Subscribe":"<mailto:linuxppc-dev+subscribe@lists.ozlabs.org>,\n  <mailto:linuxppc-dev+subscribe-digest@lists.ozlabs.org>,\n  <mailto:linuxppc-dev+subscribe-nomail@lists.ozlabs.org>","List-Unsubscribe":"<mailto:linuxppc-dev+unsubscribe@lists.ozlabs.org>","Precedence":"list","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","X-Spam-Status":"No, score=-2.3 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED,\n\tDKIM_VALID,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS autolearn=disabled\n\tversion=4.0.1 OzLabs 8","X-Spam-Checker-Version":"SpamAssassin 4.0.1 (2024-03-25) on lists.ozlabs.org"},"content":"When a bridge window contains big and small resource(s), the small\nresource(s) may not amount to the half of the size of the big resource\nwhich would allow calculate_head_align() to shrink the head alignment.\nThis results in always placing the small resource(s) after the big\nresource.\n\nIn general, it would be good to be able to place the small resource(s)\nbefore the big resource to achieve better utilization of the address\nspace. In the cases where the large resource can only fit at the end\nof the window, it is even required.\n\nHowever, carrying the information over from pbus_size_mem() and\ncalculate_head_align() to __pci_assign_resource() and\npcibios_align_resource() is not easy with the current data structures.\n\nA somewhat hacky way to move the non-aligning tail part to the head is\npossible within pcibios_align_resource(). The free space between the\nstart of the free space span and the aligned start address can be\ncompared with the non-aligning remainder of the size. If the free space\nis larger than the remainder, placing the remainder before the start\naddress is possible. This relocation should generally work, because PCI\nresources consist only power-of-2 atoms.\n\nVarious arch requirements may still need to override the relocation, so\nthe relocation is only applied selectively in such cases.\n\nCloses: https://bugzilla.kernel.org/show_bug.cgi?id=221205\nSigned-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>\n---\n arch/arm/kernel/bios32.c         |  3 +++\n arch/m68k/kernel/pcibios.c       |  4 ++++\n arch/mips/pci/pci-generic.c      |  3 +++\n arch/mips/pci/pci-legacy.c       |  2 ++\n arch/parisc/kernel/pci.c         |  3 +++\n arch/powerpc/kernel/pci-common.c |  2 ++\n arch/sh/drivers/pci/pci.c        |  2 ++\n arch/x86/pci/i386.c              |  2 ++\n arch/xtensa/kernel/pci.c         |  2 ++\n drivers/pci/setup-res.c          | 39 +++++++++++++++++++++++++++++++-\n include/linux/pci.h              |  5 ++++\n kernel/resource.c                |  2 +-\n 12 files changed, 67 insertions(+), 2 deletions(-)","diff":"diff --git a/arch/arm/kernel/bios32.c b/arch/arm/kernel/bios32.c\nindex cedb83a85dd9..ac0e890510da 100644\n--- a/arch/arm/kernel/bios32.c\n+++ b/arch/arm/kernel/bios32.c\n@@ -577,6 +577,9 @@ resource_size_t pcibios_align_resource(void *data, const struct resource *res,\n \t\treturn host_bridge->align_resource(dev, res,\n \t\t\t\tstart, size, align);\n \n+\tif (res->flags & IORESOURCE_MEM)\n+\t\treturn pci_align_resource(dev, res, empty_res, size, align);\n+\n \treturn start;\n }\n \ndiff --git a/arch/m68k/kernel/pcibios.c b/arch/m68k/kernel/pcibios.c\nindex 7e286ee1976b..7a9e60df79c5 100644\n--- a/arch/m68k/kernel/pcibios.c\n+++ b/arch/m68k/kernel/pcibios.c\n@@ -31,11 +31,15 @@ resource_size_t pcibios_align_resource(void *data, const struct resource *res,\n \t\t\t\t       resource_size_t size,\n \t\t\t\t       resource_size_t align)\n {\n+\tstruct pci_dev *dev = data;\n \tresource_size_t start = res->start;\n \n \tif ((res->flags & IORESOURCE_IO) && (start & 0x300))\n \t\tstart = (start + 0x3ff) & ~0x3ff;\n \n+\tif (res->flags & IORESOURCE_MEM)\n+\t\treturn pci_align_resource(dev, res, empty_res, size, align);\n+\n \treturn start;\n }\n \ndiff --git a/arch/mips/pci/pci-generic.c b/arch/mips/pci/pci-generic.c\nindex aaa1d6de8bef..c2e23d0c1d77 100644\n--- a/arch/mips/pci/pci-generic.c\n+++ b/arch/mips/pci/pci-generic.c\n@@ -38,6 +38,9 @@ resource_size_t pcibios_align_resource(void *data, const struct resource *res,\n \t\treturn host_bridge->align_resource(dev, res,\n \t\t\t\tstart, size, align);\n \n+\tif (res->flags & IORESOURCE_MEM)\n+\t\treturn pci_align_resource(dev, res, empty_res, size, align);\n+\n \treturn start;\n }\n \ndiff --git a/arch/mips/pci/pci-legacy.c b/arch/mips/pci/pci-legacy.c\nindex 817e97402afe..dae6dafdd6e0 100644\n--- a/arch/mips/pci/pci-legacy.c\n+++ b/arch/mips/pci/pci-legacy.c\n@@ -70,6 +70,8 @@ pcibios_align_resource(void *data, const struct resource *res,\n \t\tif (start & 0x300)\n \t\t\tstart = (start + 0x3ff) & ~0x3ff;\n \t} else if (res->flags & IORESOURCE_MEM) {\n+\t\tstart = pci_align_resource(dev, res, empty_res, size, align);\n+\n \t\t/* Make sure we start at our min on all hoses */\n \t\tif (start < PCIBIOS_MIN_MEM + hose->mem_resource->start)\n \t\t\tstart = PCIBIOS_MIN_MEM + hose->mem_resource->start;\ndiff --git a/arch/parisc/kernel/pci.c b/arch/parisc/kernel/pci.c\nindex f50be1a63c4c..b8007c7400d4 100644\n--- a/arch/parisc/kernel/pci.c\n+++ b/arch/parisc/kernel/pci.c\n@@ -201,6 +201,7 @@ resource_size_t pcibios_align_resource(void *data, const struct resource *res,\n \t\t\t\t       resource_size_t size,\n \t\t\t\t       resource_size_t alignment)\n {\n+\tstruct pci_dev *dev = data;\n \tresource_size_t align, start = res->start;\n \n \tDBG_RES(\"pcibios_align_resource(%s, (%p) [%lx,%lx]/%x, 0x%lx, 0x%lx)\\n\",\n@@ -212,6 +213,8 @@ resource_size_t pcibios_align_resource(void *data, const struct resource *res,\n \talign = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;\n \tif (align > alignment)\n \t\tstart = ALIGN(start, align);\n+\telse\n+\t\tstart = pci_align_resource(dev, res, empty_res, size, alignment);\n \n \treturn start;\n }\ndiff --git a/arch/powerpc/kernel/pci-common.c b/arch/powerpc/kernel/pci-common.c\nindex e7bfa15da043..8efe95a0c4ff 100644\n--- a/arch/powerpc/kernel/pci-common.c\n+++ b/arch/powerpc/kernel/pci-common.c\n@@ -1144,6 +1144,8 @@ resource_size_t pcibios_align_resource(void *data, const struct resource *res,\n \t\t\treturn start;\n \t\tif (start & 0x300)\n \t\t\tstart = (start + 0x3ff) & ~0x3ff;\n+\t} else if (res->flags & IORESOURCE_MEM) {\n+\t\tstart = pci_align_resource(dev, res, empty_res, size, align);\n \t}\n \n \treturn start;\ndiff --git a/arch/sh/drivers/pci/pci.c b/arch/sh/drivers/pci/pci.c\nindex 7a0522316ee3..994c3bd36ef2 100644\n--- a/arch/sh/drivers/pci/pci.c\n+++ b/arch/sh/drivers/pci/pci.c\n@@ -185,6 +185,8 @@ resource_size_t pcibios_align_resource(void *data, const struct resource *res,\n \t\t */\n \t\tif (start & 0x300)\n \t\t\tstart = (start + 0x3ff) & ~0x3ff;\n+\t} else (res->flags & IORESOURCE_MEM) {\n+\t\tstart = pci_align_resource(dev, res, empty_res, size, align);\n \t}\n \n \treturn start;\ndiff --git a/arch/x86/pci/i386.c b/arch/x86/pci/i386.c\nindex 6fbd4b34c3f7..e2de26b82940 100644\n--- a/arch/x86/pci/i386.c\n+++ b/arch/x86/pci/i386.c\n@@ -165,6 +165,8 @@ pcibios_align_resource(void *data, const struct resource *res,\n \t\tif (start & 0x300)\n \t\t\tstart = (start + 0x3ff) & ~0x3ff;\n \t} else if (res->flags & IORESOURCE_MEM) {\n+\t\tstart = pci_align_resource(dev, res, empty_res, size, align);\n+\n \t\t/* The low 1MB range is reserved for ISA cards */\n \t\tif (start < BIOS_END)\n \t\t\tstart = BIOS_END;\ndiff --git a/arch/xtensa/kernel/pci.c b/arch/xtensa/kernel/pci.c\nindex 64ccb7e0d92f..305031551136 100644\n--- a/arch/xtensa/kernel/pci.c\n+++ b/arch/xtensa/kernel/pci.c\n@@ -54,6 +54,8 @@ pcibios_align_resource(void *data, const struct resource *res,\n \n \t\tif (start & 0x300)\n \t\t\tstart = (start + 0x3ff) & ~0x3ff;\n+\t} else if (res->flags & IORESOURCE_MEM) {\n+\t\tstart = pci_align_resource(dev, res, empty_res, size, align);\n \t}\n \n \treturn start;\ndiff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c\nindex c375e255c509..fbc05cda96ee 100644\n--- a/drivers/pci/setup-res.c\n+++ b/drivers/pci/setup-res.c\n@@ -244,6 +244,41 @@ static int pci_revert_fw_address(struct resource *res, struct pci_dev *dev,\n \treturn 0;\n }\n \n+/*\n+ * For mem bridge windows, try to relocate tail remainder space to space\n+ * before res->start if there's enough free space there. This enables\n+ * tighter packing for resources.\n+ */\n+resource_size_t pci_align_resource(struct pci_dev *dev,\n+\t\t\t\t   const struct resource *res,\n+\t\t\t\t   const struct resource *empty_res,\n+\t\t\t\t   resource_size_t size,\n+\t\t\t\t   resource_size_t align)\n+{\n+\tresource_size_t remainder, start_addr;\n+\n+\tif (!(res->flags & IORESOURCE_MEM))\n+\t\treturn res->start;\n+\n+\tif (IS_ALIGNED(size, align))\n+\t\treturn res->start;\n+\n+\tremainder = size - ALIGN_DOWN(size, align);\n+\t/* Don't mess with size that doesn't align with window size granularity */\n+\tif (!IS_ALIGNED(remainder, pci_min_window_alignment(dev->bus, res->flags)))\n+\t\treturn res->start;\n+\t/* Try to place remainder that doesn't fill align before */\n+\tif (res->start < remainder)\n+\t\treturn res->start;\n+\tstart_addr = res->start - remainder;\n+\tif (empty_res->start > start_addr)\n+\t\treturn res->start;\n+\n+\tpci_dbg(dev, \"%pR: moving candidate start address below align to %llx\\n\",\n+\t\tres, (unsigned long long)start_addr);\n+\treturn start_addr;\n+}\n+\n /*\n  * We don't have to worry about legacy ISA devices, so nothing to do here.\n  * This is marked as __weak because multiple architectures define it; it should\n@@ -255,7 +290,9 @@ resource_size_t __weak pcibios_align_resource(void *data,\n \t\t\t\t\t      resource_size_t size,\n \t\t\t\t\t      resource_size_t align)\n {\n-\treturn res->start;\n+\tstruct pci_dev *dev = data;\n+\n+\treturn pci_align_resource(dev, res, empty_res, size, align);\n }\n \n static int __pci_assign_resource(struct pci_bus *bus, struct pci_dev *dev,\ndiff --git a/include/linux/pci.h b/include/linux/pci.h\nindex ac332ff9da9f..cedf948dc614 100644\n--- a/include/linux/pci.h\n+++ b/include/linux/pci.h\n@@ -1210,6 +1210,11 @@ resource_size_t pcibios_align_resource(void *data, const struct resource *res,\n \t\t\t\t       const struct resource *empty_res,\n \t\t\t\t       resource_size_t size,\n \t\t\t\t       resource_size_t align);\n+resource_size_t pci_align_resource(struct pci_dev *dev,\n+\t\t\t\t   const struct resource *res,\n+\t\t\t\t   const struct resource *empty_res,\n+\t\t\t\t   resource_size_t size,\n+\t\t\t\t   resource_size_t align);\n \n /* Generic PCI functions used internally */\n \ndiff --git a/kernel/resource.c b/kernel/resource.c\nindex 8c5fcb30fc33..d02a53fb95d8 100644\n--- a/kernel/resource.c\n+++ b/kernel/resource.c\n@@ -766,7 +766,7 @@ static int __find_resource_space(struct resource *root, struct resource *old,\n \t\t\t}\n \t\t\talloc.end = alloc.start + size - 1;\n \t\t\tif (alloc.start <= alloc.end &&\n-\t\t\t    __resource_contains_unbound(&avail, &alloc)) {\n+\t\t\t    __resource_contains_unbound(&full_avail, &alloc)) {\n \t\t\t\tnew->start = alloc.start;\n \t\t\t\tnew->end = alloc.end;\n \t\t\t\treturn 0;\n","prefixes":["09/10"]}