Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754713AbbLKFSI (ORCPT ); Fri, 11 Dec 2015 00:18:08 -0500 Received: from aserp1040.oracle.com ([141.146.126.69]:49514 "EHLO aserp1040.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753681AbbLKFSE (ORCPT ); Fri, 11 Dec 2015 00:18:04 -0500 From: Yinghai Lu To: Bjorn Helgaas , David Miller , Benjamin Herrenschmidt , Wei Yang , TJ , Yijing Wang , Khalid Aziz Cc: linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, Yinghai Lu Subject: [PATCH v9 29/60] PCI: Reorder resources list for required/optional resources Date: Thu, 10 Dec 2015 21:06:25 -0800 Message-Id: <1449810416-2950-30-git-send-email-yinghai@kernel.org> X-Mailer: git-send-email 1.8.4.5 In-Reply-To: <1449810416-2950-1-git-send-email-yinghai@kernel.org> References: <1449810416-2950-1-git-send-email-yinghai@kernel.org> X-Source-IP: aserv0021.oracle.com [141.146.126.233] Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4596 Lines: 142 We try to allocate required+optional before allocate required only and expand with optional. At first we update size and alignment for required+optional resource. And after that we reorder them with new alignment, but current we only do that STARTALIGN ones. For SIZEALIGN type resource, after add back add_size, the alignment get changed, so need to do sorting like STARTALIGN type resources. Also we need to reorder the sorting back after we restore resource to required only when required+optional fail to allocate for all. So move out the reordering code from the loop to separated function, and call it two times accordingly. Signed-off-by: Yinghai Lu --- drivers/pci/setup-bus.c | 62 +++++++++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c index 53014e1..885b9f8 100644 --- a/drivers/pci/setup-bus.c +++ b/drivers/pci/setup-bus.c @@ -286,6 +286,31 @@ static inline void reset_resource(struct resource *res) res->flags = 0; } +static void sort_resources(struct list_head *head) +{ + struct pci_dev_resource *res1, *tmp_res, *res2; + + list_for_each_entry_safe(res1, tmp_res, head, list) { + resource_size_t align1, size1, align2, size2; + + align1 = pci_resource_alignment(res1->dev, res1->res); + size1 = resource_size(res1->res); + + /* reorder it */ + list_for_each_entry(res2, head, list) { + if (res2 == res1) + break; + + align2 = pci_resource_alignment(res2->dev, res2->res); + size2 = resource_size(res2->res); + if (is_before(align1, size1, align2, size2)) { + list_move_tail(&res1->list, &res2->list); + break; + } + } + } +} + /** * reassign_resources_sorted() - satisfy any additional resource requests * @@ -454,9 +479,9 @@ static void __assign_resources_sorted(struct list_head *head, LIST_HEAD(save_head); LIST_HEAD(local_fail_head); struct pci_dev_resource *save_res; - struct pci_dev_resource *dev_res, *tmp_res, *dev_res2; + struct pci_dev_resource *dev_res, *tmp_res; unsigned long fail_type; - resource_size_t add_align, align; + resource_size_t add_align; /* Check if optional add_size is there */ if (!realloc_head || list_empty(realloc_head)) @@ -471,47 +496,32 @@ static void __assign_resources_sorted(struct list_head *head, } /* Update res in head list with add_size in realloc_head list */ - list_for_each_entry_safe(dev_res, tmp_res, head, list) { + list_for_each_entry(dev_res, head, list) { dev_res->res->end += get_res_add_size(realloc_head, dev_res->res); /* * There are two kinds of additional resources in the list: - * 1. bridge resource -- IORESOURCE_STARTALIGN - * 2. SR-IOV resource -- IORESOURCE_SIZEALIGN - * Here just fix the additional alignment for bridge + * 1. bridge resource with IORESOURCE_STARTALIGN + * need to update start to change alignment + * 2. resource with IORESOURCE_SIZEALIGN + * update size above already change alignment. */ if (!(dev_res->res->flags & IORESOURCE_STARTALIGN)) continue; add_align = get_res_add_align(realloc_head, dev_res->res); - /* - * The "head" list is sorted by the alignment to make sure - * resources with bigger alignment will be assigned first. - * After we change the alignment of a dev_res in "head" list, - * we need to reorder the list by alignment to make it - * consistent. - */ - if (add_align > dev_res->res->start) { + if (add_align) { resource_size_t r_size = resource_size(dev_res->res); dev_res->res->start = add_align; dev_res->res->end = add_align + r_size - 1; - - list_for_each_entry(dev_res2, head, list) { - align = pci_resource_alignment(dev_res2->dev, - dev_res2->res); - if (add_align > align) { - list_move_tail(&dev_res->list, - &dev_res2->list); - break; - } - } } - } + sort_resources(head); + /* Try updated head list with add_size added */ assign_requested_resources_sorted(head, &local_fail_head); @@ -553,6 +563,8 @@ static void __assign_resources_sorted(struct list_head *head, } free_list(&save_head); + sort_resources(head); + requested_and_reassign: /* Satisfy the must-have resource requests */ assign_requested_resources_sorted(head, fail_head); -- 1.8.4.5 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/