From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.6 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B6707C3F68F for ; Tue, 14 Jan 2020 08:10:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8C7012465B for ; Tue, 14 Jan 2020 08:10:18 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EJZy9hG0" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729259AbgANIKR (ORCPT ); Tue, 14 Jan 2020 03:10:17 -0500 Received: from mail-ot1-f67.google.com ([209.85.210.67]:40523 "EHLO mail-ot1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728992AbgANIKR (ORCPT ); Tue, 14 Jan 2020 03:10:17 -0500 Received: by mail-ot1-f67.google.com with SMTP id w21so11765983otj.7; Tue, 14 Jan 2020 00:10:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=ADoHczfnfwvTHTvwEzQykNkOfVFkdqsmaGJm2GfcQqg=; b=EJZy9hG0LMeuJBkesqHDRBgsI1lak03o5EWwTcPsm0XV1Sjy9jZeGHd+x8m9HSLUUu kGnImO9rXK+21uKSB0+arziaUPK61JW7/fLPTYI6IBlLAMpa1uDDltoEkaxgngi/Y7fd 9A+k0su7mh/0T+7fB3mlyf81lFyFel7S1J/YiQAyHgEIWnARtzzmn7Z/E06rvh73IJRr 9Kn49GH4VsA31yuyv9gIwwI4cu5jzWZNbD6wZwJjk+KC7texmBP2WJHF8SLqcVBOeMje 3OKg3EKsyxNhvSNukICOYTnjLGYCFI6qKEGNkKrOlPGb5qQ9bygr2jO3cvYxCAn8sXow cNRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=ADoHczfnfwvTHTvwEzQykNkOfVFkdqsmaGJm2GfcQqg=; b=bZ70+cscHcEhJc7fKPZk1AxB3jkYsXPG/Hve10fVdcZbzBHPgXOtlIZ4iCv5XoqMxT 2xeKMiYc9tM4QiduykUf1X9CEf61vLiFS9CASUDtWGsRrP+yA5JkBNH23nZk6Rvgd1RJ xBRsQydg58r5wBXue55284Bv4DA7JOTrs5J+MQy/IabhThoSknwSPYHLCo7ZPDp3hXXr 8ka2mldK2OuSnqZw4vc3Bv1p21tOuYqvi90BsuXo7n1uu6qiXN4NCfzuKIAuOMgfNuzM XbokTi3TJdvMfaDWN7dBPP7c8Njxl9f7nFejXahNQDHLiQz2ogp4c/RTqRp6rgOJUFjC 2ozA== X-Gm-Message-State: APjAAAX/yQPICkRY6k/kM4jva3jxi1Mgolw46QPe0KBwsMrdO5UgOmoo EdWxXv/gauqAMLV7+6u7Q7jT8FTOrx8tX/eKItM= X-Google-Smtp-Source: APXvYqygXu/f7SsTk/IQwbPRIFhQDBJ91FPpJ8e2VaIQ7vAfiBxNzcLSBq8YksHvkEr0WgUlfuhm6rV7ESZA9BHenTw= X-Received: by 2002:a05:6830:147:: with SMTP id j7mr16417215otp.44.1578989415836; Tue, 14 Jan 2020 00:10:15 -0800 (PST) MIME-Version: 1.0 References: <20200108162211.22358-1-prabhakar.mahadev-lad.rj@bp.renesas.com> <20200108162211.22358-4-prabhakar.mahadev-lad.rj@bp.renesas.com> <53d74632-34ee-f7f7-656f-a93a6c10e7ba@ti.com> <2b4dd351-76ee-60bd-bd91-20d5f1ac4e79@ti.com> In-Reply-To: <2b4dd351-76ee-60bd-bd91-20d5f1ac4e79@ti.com> From: "Lad, Prabhakar" Date: Tue, 14 Jan 2020 08:09:49 +0000 Message-ID: Subject: Re: [v3 3/6] PCI: endpoint: Add support to handle multiple base for mapping outbound memory To: Kishon Vijay Abraham I Cc: Bjorn Helgaas , Rob Herring , Mark Rutland , Geert Uytterhoeven , Magnus Damm , Marek Vasut , Yoshihiro Shimoda , linux-pci , Catalin Marinas , Will Deacon , Lorenzo Pieralisi , Arnd Bergmann , Greg Kroah-Hartman , Andrew Murray , "open list:OPEN FIRMWARE AND FLATTENED DEVICE TREE BINDINGS" , LKML , LAK , Linux-Renesas , Chris Paterson , Frank Rowand , Gustavo Pimentel , Jingoo Han , Simon Horman , Shawn Lin , Tom Joseph , Heiko Stuebner , "open list:ARM/Rockchip SoC..." , Lad Prabhakar Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Kishon, On Mon, Jan 13, 2020 at 8:56 AM Kishon Vijay Abraham I wrote: > > Hi Prabhakar, > > On 10/01/20 11:38 PM, Lad, Prabhakar wrote: > > Hi Kishon, > > > > Thank you for the review. > > > > On Thu, Jan 9, 2020 at 6:25 AM Kishon Vijay Abraham I wrote: > >> > >> Hi Prabhakar, > >> > >> On 08/01/20 9:52 PM, Lad Prabhakar wrote: > >>> R-Car PCIe controller has support to map multiple memory regions for > >>> mapping the outbound memory in local system also the controller limits > >>> single allocation for each region (that is, once a chunk is used from the > >>> region it cannot be used to allocate a new one). This features inspires to > >>> add support for handling multiple memory bases in endpoint framework. > >>> > >>> With this patch pci_epc_mem_init() now accepts multiple regions, also > >>> page_size for each memory region is passed during initialization so as > >>> to handle single allocation for each region by setting the page_size to > >>> window_size. > >>> > >>> Signed-off-by: Lad Prabhakar > >>> --- > >>> .../pci/controller/cadence/pcie-cadence-ep.c | 12 +- > >>> .../pci/controller/dwc/pcie-designware-ep.c | 31 ++- > >>> drivers/pci/controller/pcie-rockchip-ep.c | 14 +- > >>> drivers/pci/endpoint/functions/pci-epf-test.c | 29 +-- > >>> drivers/pci/endpoint/pci-epc-core.c | 7 +- > >>> drivers/pci/endpoint/pci-epc-mem.c | 199 ++++++++++++++---- > >>> include/linux/pci-epc.h | 46 ++-- > >>> 7 files changed, 245 insertions(+), 93 deletions(-) > >>> > >> . > >> . > >> > >> . > >> . > >>> diff --git a/drivers/pci/endpoint/pci-epc-core.c b/drivers/pci/endpoint/pci-epc-core.c > >>> index 2091508c1620..289c266c2d90 100644 > >>> --- a/drivers/pci/endpoint/pci-epc-core.c > >>> +++ b/drivers/pci/endpoint/pci-epc-core.c > >>> @@ -358,13 +358,15 @@ EXPORT_SYMBOL_GPL(pci_epc_unmap_addr); > >>> * @epc: the EPC device on which address is allocated > >>> * @func_no: the endpoint function number in the EPC device > >>> * @phys_addr: physical address of the local system > >>> + * @window: index to the window region where PCI address will be mapped > >>> * @pci_addr: PCI address to which the physical address should be mapped > >>> * @size: the size of the allocation > >>> * > >>> * Invoke to map CPU address with PCI address. > >>> */ > >>> int pci_epc_map_addr(struct pci_epc *epc, u8 func_no, > >>> - phys_addr_t phys_addr, u64 pci_addr, size_t size) > >>> + phys_addr_t phys_addr, int window, > >>> + u64 pci_addr, size_t size) > >>> { > >>> int ret; > >>> unsigned long flags; > >>> @@ -376,7 +378,8 @@ int pci_epc_map_addr(struct pci_epc *epc, u8 func_no, > >>> return 0; > >>> > >>> spin_lock_irqsave(&epc->lock, flags); > >>> - ret = epc->ops->map_addr(epc, func_no, phys_addr, pci_addr, size); > >>> + ret = epc->ops->map_addr(epc, func_no, phys_addr, > >>> + window, pci_addr, size); > >>> spin_unlock_irqrestore(&epc->lock, flags); > >>> > >>> return ret; > >>> diff --git a/drivers/pci/endpoint/pci-epc-mem.c b/drivers/pci/endpoint/pci-epc-mem.c > >>> index d2b174ce15de..f205f7819292 100644 > >>> --- a/drivers/pci/endpoint/pci-epc-mem.c > >>> +++ b/drivers/pci/endpoint/pci-epc-mem.c > >>> @@ -38,57 +38,77 @@ static int pci_epc_mem_get_order(struct pci_epc_mem *mem, size_t size) > >>> /** > >>> * __pci_epc_mem_init() - initialize the pci_epc_mem structure > >>> * @epc: the EPC device that invoked pci_epc_mem_init > >>> - * @phys_base: the physical address of the base > >>> - * @size: the size of the address space > >>> - * @page_size: size of each page > >>> + * @windows: pointer to windows supported by the device > >>> + * @num_windows: number of windows device supports > >>> * > >>> * Invoke to initialize the pci_epc_mem structure used by the > >>> * endpoint functions to allocate mapped PCI address. > >>> */ > >>> -int __pci_epc_mem_init(struct pci_epc *epc, phys_addr_t phys_base, size_t size, > >>> - size_t page_size) > >>> +int __pci_epc_mem_init(struct pci_epc *epc, struct pci_epc_mem_window *windows, > >>> + int num_windows) > >>> { > >>> - int ret; > >>> - struct pci_epc_mem *mem; > >>> - unsigned long *bitmap; > >>> + struct pci_epc_mem *mem = NULL; > >>> + unsigned long *bitmap = NULL; > >>> unsigned int page_shift; > >>> - int pages; > >>> + size_t page_size; > >>> int bitmap_size; > >>> + int pages; > >>> + int ret; > >>> + int i; > >>> > >>> - if (page_size < PAGE_SIZE) > >>> - page_size = PAGE_SIZE; > >>> + epc->mem_windows = 0; > >>> > >>> - page_shift = ilog2(page_size); > >>> - pages = size >> page_shift; > >>> - bitmap_size = BITS_TO_LONGS(pages) * sizeof(long); > >>> + if (!windows) > >>> + return -EINVAL; > >>> > >>> - mem = kzalloc(sizeof(*mem), GFP_KERNEL); > >>> - if (!mem) { > >>> - ret = -ENOMEM; > >>> - goto err; > >>> - } > >>> + if (num_windows <= 0) > >>> + return -EINVAL; > >>> > >>> - bitmap = kzalloc(bitmap_size, GFP_KERNEL); > >>> - if (!bitmap) { > >>> - ret = -ENOMEM; > >>> - goto err_mem; > >>> - } > >>> + epc->mem = kcalloc(num_windows, sizeof(*mem), GFP_KERNEL); > >>> + if (!epc->mem) > >>> + return -EINVAL; > >>> + > >>> + for (i = 0; i < num_windows; i++) { > >>> + page_size = windows[i].page_size; > >>> + if (page_size < PAGE_SIZE) > >>> + page_size = PAGE_SIZE; > >>> + page_shift = ilog2(page_size); > >>> + pages = windows[i].size >> page_shift; > >>> + bitmap_size = BITS_TO_LONGS(pages) * sizeof(long); > >>> + > >>> + mem = kzalloc(sizeof(*mem), GFP_KERNEL); > >>> + if (!mem) { > >>> + ret = -ENOMEM; > >>> + goto err_mem; > >>> + } > >>> > >>> - mem->bitmap = bitmap; > >>> - mem->phys_base = phys_base; > >>> - mem->page_size = page_size; > >>> - mem->pages = pages; > >>> - mem->size = size; > >>> + bitmap = kzalloc(bitmap_size, GFP_KERNEL); > >>> + if (!bitmap) { > >>> + ret = -ENOMEM; > >>> + goto err_mem; > >>> + } > >>> > >>> - epc->mem = mem; > >>> + mem->bitmap = bitmap; > >>> + mem->window.phys_base = windows[i].phys_base; > >>> + mem->page_size = page_size; > >>> + mem->pages = pages; > >>> + mem->window.size = windows[i].size; > >>> + mem->window.map_size = 0; > >>> + > >>> + epc->mem[i] = mem; > >>> + } > >>> + epc->mem_windows = num_windows; > >>> > >>> return 0; > >>> > >>> err_mem: > >>> - kfree(mem); > >>> + for (; i >= 0; i--) { > >> > >> mem has to be reinitialized for every iteration of the loop. > > not sure what exactly you mean here, could you please elaborate. > > You are invoking "kfree(mem->bitmap);" in a loop without re-initializing > mem. Refer pci_epc_mem_exit() where you are doing the free properly. > good catch will fix that. Cheers, --Prabhakar > > > >>> + kfree(mem->bitmap); > >>> + kfree(epc->mem[i]); > >>> + } > >>> + kfree(epc->mem); > >>> > >>> -err: > >>> -return ret; > >>> + return ret; > >>> } > >>> EXPORT_SYMBOL_GPL(__pci_epc_mem_init); > >>> > >>> @@ -101,48 +121,127 @@ EXPORT_SYMBOL_GPL(__pci_epc_mem_init); > >>> */ > >>> void pci_epc_mem_exit(struct pci_epc *epc) > >>> { > >>> - struct pci_epc_mem *mem = epc->mem; > >>> + struct pci_epc_mem *mem; > >>> + int i; > >>> + > >>> + if (!epc->mem_windows) > >>> + return; > >>> + > >>> + for (i = 0; i <= epc->mem_windows; i++) { > >>> + mem = epc->mem[i]; > > Missing the above line in the error handling above. > > > >>> + kfree(mem->bitmap); > >>> + kfree(epc->mem[i]); > >>> + } > > Thanks > Kishon From mboxrd@z Thu Jan 1 00:00:00 1970 From: "Lad, Prabhakar" Subject: Re: [v3 3/6] PCI: endpoint: Add support to handle multiple base for mapping outbound memory Date: Tue, 14 Jan 2020 08:09:49 +0000 Message-ID: References: <20200108162211.22358-1-prabhakar.mahadev-lad.rj@bp.renesas.com> <20200108162211.22358-4-prabhakar.mahadev-lad.rj@bp.renesas.com> <53d74632-34ee-f7f7-656f-a93a6c10e7ba@ti.com> <2b4dd351-76ee-60bd-bd91-20d5f1ac4e79@ti.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <2b4dd351-76ee-60bd-bd91-20d5f1ac4e79@ti.com> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=m.gmane-mx.org@lists.infradead.org To: Kishon Vijay Abraham I Cc: Mark Rutland , Heiko Stuebner , Geert Uytterhoeven , linux-pci , Shawn Lin , Frank Rowand , Marek Vasut , Lorenzo Pieralisi , Will Deacon , Magnus Damm , "open list:ARM/Rockchip SoC..." , Catalin Marinas , "open list:OPEN FIRMWARE AND FLATTENED DEVICE TREE BINDINGS" , Chris Paterson , Arnd Bergmann , Lad Prabhakar , Rob Herring , Bjorn Helgaas , LAK , Greg Kroah-Hartman List-Id: linux-rockchip.vger.kernel.org Hi Kishon, On Mon, Jan 13, 2020 at 8:56 AM Kishon Vijay Abraham I wrote: > > Hi Prabhakar, > > On 10/01/20 11:38 PM, Lad, Prabhakar wrote: > > Hi Kishon, > > > > Thank you for the review. > > > > On Thu, Jan 9, 2020 at 6:25 AM Kishon Vijay Abraham I wrote: > >> > >> Hi Prabhakar, > >> > >> On 08/01/20 9:52 PM, Lad Prabhakar wrote: > >>> R-Car PCIe controller has support to map multiple memory regions for > >>> mapping the outbound memory in local system also the controller limits > >>> single allocation for each region (that is, once a chunk is used from the > >>> region it cannot be used to allocate a new one). This features inspires to > >>> add support for handling multiple memory bases in endpoint framework. > >>> > >>> With this patch pci_epc_mem_init() now accepts multiple regions, also > >>> page_size for each memory region is passed during initialization so as > >>> to handle single allocation for each region by setting the page_size to > >>> window_size. > >>> > >>> Signed-off-by: Lad Prabhakar > >>> --- > >>> .../pci/controller/cadence/pcie-cadence-ep.c | 12 +- > >>> .../pci/controller/dwc/pcie-designware-ep.c | 31 ++- > >>> drivers/pci/controller/pcie-rockchip-ep.c | 14 +- > >>> drivers/pci/endpoint/functions/pci-epf-test.c | 29 +-- > >>> drivers/pci/endpoint/pci-epc-core.c | 7 +- > >>> drivers/pci/endpoint/pci-epc-mem.c | 199 ++++++++++++++---- > >>> include/linux/pci-epc.h | 46 ++-- > >>> 7 files changed, 245 insertions(+), 93 deletions(-) > >>> > >> . > >> . > >> > >> . > >> . > >>> diff --git a/drivers/pci/endpoint/pci-epc-core.c b/drivers/pci/endpoint/pci-epc-core.c > >>> index 2091508c1620..289c266c2d90 100644 > >>> --- a/drivers/pci/endpoint/pci-epc-core.c > >>> +++ b/drivers/pci/endpoint/pci-epc-core.c > >>> @@ -358,13 +358,15 @@ EXPORT_SYMBOL_GPL(pci_epc_unmap_addr); > >>> * @epc: the EPC device on which address is allocated > >>> * @func_no: the endpoint function number in the EPC device > >>> * @phys_addr: physical address of the local system > >>> + * @window: index to the window region where PCI address will be mapped > >>> * @pci_addr: PCI address to which the physical address should be mapped > >>> * @size: the size of the allocation > >>> * > >>> * Invoke to map CPU address with PCI address. > >>> */ > >>> int pci_epc_map_addr(struct pci_epc *epc, u8 func_no, > >>> - phys_addr_t phys_addr, u64 pci_addr, size_t size) > >>> + phys_addr_t phys_addr, int window, > >>> + u64 pci_addr, size_t size) > >>> { > >>> int ret; > >>> unsigned long flags; > >>> @@ -376,7 +378,8 @@ int pci_epc_map_addr(struct pci_epc *epc, u8 func_no, > >>> return 0; > >>> > >>> spin_lock_irqsave(&epc->lock, flags); > >>> - ret = epc->ops->map_addr(epc, func_no, phys_addr, pci_addr, size); > >>> + ret = epc->ops->map_addr(epc, func_no, phys_addr, > >>> + window, pci_addr, size); > >>> spin_unlock_irqrestore(&epc->lock, flags); > >>> > >>> return ret; > >>> diff --git a/drivers/pci/endpoint/pci-epc-mem.c b/drivers/pci/endpoint/pci-epc-mem.c > >>> index d2b174ce15de..f205f7819292 100644 > >>> --- a/drivers/pci/endpoint/pci-epc-mem.c > >>> +++ b/drivers/pci/endpoint/pci-epc-mem.c > >>> @@ -38,57 +38,77 @@ static int pci_epc_mem_get_order(struct pci_epc_mem *mem, size_t size) > >>> /** > >>> * __pci_epc_mem_init() - initialize the pci_epc_mem structure > >>> * @epc: the EPC device that invoked pci_epc_mem_init > >>> - * @phys_base: the physical address of the base > >>> - * @size: the size of the address space > >>> - * @page_size: size of each page > >>> + * @windows: pointer to windows supported by the device > >>> + * @num_windows: number of windows device supports > >>> * > >>> * Invoke to initialize the pci_epc_mem structure used by the > >>> * endpoint functions to allocate mapped PCI address. > >>> */ > >>> -int __pci_epc_mem_init(struct pci_epc *epc, phys_addr_t phys_base, size_t size, > >>> - size_t page_size) > >>> +int __pci_epc_mem_init(struct pci_epc *epc, struct pci_epc_mem_window *windows, > >>> + int num_windows) > >>> { > >>> - int ret; > >>> - struct pci_epc_mem *mem; > >>> - unsigned long *bitmap; > >>> + struct pci_epc_mem *mem = NULL; > >>> + unsigned long *bitmap = NULL; > >>> unsigned int page_shift; > >>> - int pages; > >>> + size_t page_size; > >>> int bitmap_size; > >>> + int pages; > >>> + int ret; > >>> + int i; > >>> > >>> - if (page_size < PAGE_SIZE) > >>> - page_size = PAGE_SIZE; > >>> + epc->mem_windows = 0; > >>> > >>> - page_shift = ilog2(page_size); > >>> - pages = size >> page_shift; > >>> - bitmap_size = BITS_TO_LONGS(pages) * sizeof(long); > >>> + if (!windows) > >>> + return -EINVAL; > >>> > >>> - mem = kzalloc(sizeof(*mem), GFP_KERNEL); > >>> - if (!mem) { > >>> - ret = -ENOMEM; > >>> - goto err; > >>> - } > >>> + if (num_windows <= 0) > >>> + return -EINVAL; > >>> > >>> - bitmap = kzalloc(bitmap_size, GFP_KERNEL); > >>> - if (!bitmap) { > >>> - ret = -ENOMEM; > >>> - goto err_mem; > >>> - } > >>> + epc->mem = kcalloc(num_windows, sizeof(*mem), GFP_KERNEL); > >>> + if (!epc->mem) > >>> + return -EINVAL; > >>> + > >>> + for (i = 0; i < num_windows; i++) { > >>> + page_size = windows[i].page_size; > >>> + if (page_size < PAGE_SIZE) > >>> + page_size = PAGE_SIZE; > >>> + page_shift = ilog2(page_size); > >>> + pages = windows[i].size >> page_shift; > >>> + bitmap_size = BITS_TO_LONGS(pages) * sizeof(long); > >>> + > >>> + mem = kzalloc(sizeof(*mem), GFP_KERNEL); > >>> + if (!mem) { > >>> + ret = -ENOMEM; > >>> + goto err_mem; > >>> + } > >>> > >>> - mem->bitmap = bitmap; > >>> - mem->phys_base = phys_base; > >>> - mem->page_size = page_size; > >>> - mem->pages = pages; > >>> - mem->size = size; > >>> + bitmap = kzalloc(bitmap_size, GFP_KERNEL); > >>> + if (!bitmap) { > >>> + ret = -ENOMEM; > >>> + goto err_mem; > >>> + } > >>> > >>> - epc->mem = mem; > >>> + mem->bitmap = bitmap; > >>> + mem->window.phys_base = windows[i].phys_base; > >>> + mem->page_size = page_size; > >>> + mem->pages = pages; > >>> + mem->window.size = windows[i].size; > >>> + mem->window.map_size = 0; > >>> + > >>> + epc->mem[i] = mem; > >>> + } > >>> + epc->mem_windows = num_windows; > >>> > >>> return 0; > >>> > >>> err_mem: > >>> - kfree(mem); > >>> + for (; i >= 0; i--) { > >> > >> mem has to be reinitialized for every iteration of the loop. > > not sure what exactly you mean here, could you please elaborate. > > You are invoking "kfree(mem->bitmap);" in a loop without re-initializing > mem. Refer pci_epc_mem_exit() where you are doing the free properly. > good catch will fix that. Cheers, --Prabhakar > > > >>> + kfree(mem->bitmap); > >>> + kfree(epc->mem[i]); > >>> + } > >>> + kfree(epc->mem); > >>> > >>> -err: > >>> -return ret; > >>> + return ret; > >>> } > >>> EXPORT_SYMBOL_GPL(__pci_epc_mem_init); > >>> > >>> @@ -101,48 +121,127 @@ EXPORT_SYMBOL_GPL(__pci_epc_mem_init); > >>> */ > >>> void pci_epc_mem_exit(struct pci_epc *epc) > >>> { > >>> - struct pci_epc_mem *mem = epc->mem; > >>> + struct pci_epc_mem *mem; > >>> + int i; > >>> + > >>> + if (!epc->mem_windows) > >>> + return; > >>> + > >>> + for (i = 0; i <= epc->mem_windows; i++) { > >>> + mem = epc->mem[i]; > > Missing the above line in the error handling above. > > > >>> + kfree(mem->bitmap); > >>> + kfree(epc->mem[i]); > >>> + } > > Thanks > Kishon From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.5 required=3.0 tests=DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,DKIM_VALID,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B3869C33CB3 for ; Tue, 14 Jan 2020 08:10:23 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 8021D222C3 for ; Tue, 14 Jan 2020 08:10:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="k++OkOlJ"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EJZy9hG0" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8021D222C3 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:To:Subject:Message-ID:Date:From: In-Reply-To:References:MIME-Version:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=YIPwlRebsUcxsqSESxUllh0aV2dsUFY5wtAbLyBh49c=; b=k++OkOlJQDqpOG 3thDh3Qq7kiWOTca9oc3PAreTj/3QLDjGcNb1BQl4vbzslwOYOeeaX1eAUMf1Uufp3BJfO6/wZV0h ha5hk4CIZk0d+ECScs6RZ+TpcUmED9CHRYIUhOeyDJ8Y1fbIoBvcN2gRcFeE+WIKg+gLnOdeo2Fmi muV98XZeAu1T2zvYC+FdoG0UVwGzeINwKKyZtSHoMUHkUjIZWF0fWFO1axU0SbCIgbA8ZyIGMWzcX LX1MxdYCzmSp85QleEC1WqDvG6Dn6GLLW8vSFkVll1wIWJu8+S0tshDoAwBzocnwoql0FiZ9Brxer MWr+fIodZb1pEABIy3gw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1irHHI-0004b0-AH; Tue, 14 Jan 2020 08:10:20 +0000 Received: from mail-ot1-x341.google.com ([2607:f8b0:4864:20::341]) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1irHHF-0004aN-50; Tue, 14 Jan 2020 08:10:18 +0000 Received: by mail-ot1-x341.google.com with SMTP id 77so11779714oty.6; Tue, 14 Jan 2020 00:10:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=ADoHczfnfwvTHTvwEzQykNkOfVFkdqsmaGJm2GfcQqg=; b=EJZy9hG0LMeuJBkesqHDRBgsI1lak03o5EWwTcPsm0XV1Sjy9jZeGHd+x8m9HSLUUu kGnImO9rXK+21uKSB0+arziaUPK61JW7/fLPTYI6IBlLAMpa1uDDltoEkaxgngi/Y7fd 9A+k0su7mh/0T+7fB3mlyf81lFyFel7S1J/YiQAyHgEIWnARtzzmn7Z/E06rvh73IJRr 9Kn49GH4VsA31yuyv9gIwwI4cu5jzWZNbD6wZwJjk+KC7texmBP2WJHF8SLqcVBOeMje 3OKg3EKsyxNhvSNukICOYTnjLGYCFI6qKEGNkKrOlPGb5qQ9bygr2jO3cvYxCAn8sXow cNRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=ADoHczfnfwvTHTvwEzQykNkOfVFkdqsmaGJm2GfcQqg=; b=HeTNBBSlD8AedLpYn+TcIBXNHsOSgO4ZwDnGmhwR95wjPj5wbiHzNTQiIas0ETEYS7 q7EwUthmNnaGK627CsxMGj+YnBA+j8AlWwAcS13krRSwF4HqYw7A/sRGdujLzf1Uv8p7 XesX4HvTk5xtxPw1EvPYhm2HI4iXoi7sP0DKs82MFtyhmYJeyRWFwbuFLfIWe1XRqlSo cXfKRycbI2fGhi1o4L5WJw6UC6vnsNJrtGyrKqqoPuAA9xyc3fOWSA7f2w/OoI/F+AJZ ZK+A26M6G+LWzRJy9SiN9rpqtLC0divSBQW5mA0YIFQknyb/R5J1oPrVAjCtjH1BjS8o Mqvg== X-Gm-Message-State: APjAAAU1/Bc+SeScsPHqWAvFDjHsQXYrUG6kVZ8redc+VWLSe2Fdb5Y+ bQOCu6wP/1vctSDzaqExUcMyMnv1XBSR2tBFayU= X-Google-Smtp-Source: APXvYqygXu/f7SsTk/IQwbPRIFhQDBJ91FPpJ8e2VaIQ7vAfiBxNzcLSBq8YksHvkEr0WgUlfuhm6rV7ESZA9BHenTw= X-Received: by 2002:a05:6830:147:: with SMTP id j7mr16417215otp.44.1578989415836; Tue, 14 Jan 2020 00:10:15 -0800 (PST) MIME-Version: 1.0 References: <20200108162211.22358-1-prabhakar.mahadev-lad.rj@bp.renesas.com> <20200108162211.22358-4-prabhakar.mahadev-lad.rj@bp.renesas.com> <53d74632-34ee-f7f7-656f-a93a6c10e7ba@ti.com> <2b4dd351-76ee-60bd-bd91-20d5f1ac4e79@ti.com> In-Reply-To: <2b4dd351-76ee-60bd-bd91-20d5f1ac4e79@ti.com> From: "Lad, Prabhakar" Date: Tue, 14 Jan 2020 08:09:49 +0000 Message-ID: Subject: Re: [v3 3/6] PCI: endpoint: Add support to handle multiple base for mapping outbound memory To: Kishon Vijay Abraham I X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200114_001017_220306_DAC40779 X-CRM114-Status: GOOD ( 28.49 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , Heiko Stuebner , Geert Uytterhoeven , linux-pci , Shawn Lin , Frank Rowand , Marek Vasut , Lorenzo Pieralisi , Will Deacon , Magnus Damm , "open list:ARM/Rockchip SoC..." , Catalin Marinas , "open list:OPEN FIRMWARE AND FLATTENED DEVICE TREE BINDINGS" , Chris Paterson , Arnd Bergmann , Lad Prabhakar , Rob Herring , Bjorn Helgaas , LAK , Greg Kroah-Hartman , Yoshihiro Shimoda , LKML , Linux-Renesas , Tom Joseph , Simon Horman , Jingoo Han , Andrew Murray , Gustavo Pimentel Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hi Kishon, On Mon, Jan 13, 2020 at 8:56 AM Kishon Vijay Abraham I wrote: > > Hi Prabhakar, > > On 10/01/20 11:38 PM, Lad, Prabhakar wrote: > > Hi Kishon, > > > > Thank you for the review. > > > > On Thu, Jan 9, 2020 at 6:25 AM Kishon Vijay Abraham I wrote: > >> > >> Hi Prabhakar, > >> > >> On 08/01/20 9:52 PM, Lad Prabhakar wrote: > >>> R-Car PCIe controller has support to map multiple memory regions for > >>> mapping the outbound memory in local system also the controller limits > >>> single allocation for each region (that is, once a chunk is used from the > >>> region it cannot be used to allocate a new one). This features inspires to > >>> add support for handling multiple memory bases in endpoint framework. > >>> > >>> With this patch pci_epc_mem_init() now accepts multiple regions, also > >>> page_size for each memory region is passed during initialization so as > >>> to handle single allocation for each region by setting the page_size to > >>> window_size. > >>> > >>> Signed-off-by: Lad Prabhakar > >>> --- > >>> .../pci/controller/cadence/pcie-cadence-ep.c | 12 +- > >>> .../pci/controller/dwc/pcie-designware-ep.c | 31 ++- > >>> drivers/pci/controller/pcie-rockchip-ep.c | 14 +- > >>> drivers/pci/endpoint/functions/pci-epf-test.c | 29 +-- > >>> drivers/pci/endpoint/pci-epc-core.c | 7 +- > >>> drivers/pci/endpoint/pci-epc-mem.c | 199 ++++++++++++++---- > >>> include/linux/pci-epc.h | 46 ++-- > >>> 7 files changed, 245 insertions(+), 93 deletions(-) > >>> > >> . > >> . > >> > >> . > >> . > >>> diff --git a/drivers/pci/endpoint/pci-epc-core.c b/drivers/pci/endpoint/pci-epc-core.c > >>> index 2091508c1620..289c266c2d90 100644 > >>> --- a/drivers/pci/endpoint/pci-epc-core.c > >>> +++ b/drivers/pci/endpoint/pci-epc-core.c > >>> @@ -358,13 +358,15 @@ EXPORT_SYMBOL_GPL(pci_epc_unmap_addr); > >>> * @epc: the EPC device on which address is allocated > >>> * @func_no: the endpoint function number in the EPC device > >>> * @phys_addr: physical address of the local system > >>> + * @window: index to the window region where PCI address will be mapped > >>> * @pci_addr: PCI address to which the physical address should be mapped > >>> * @size: the size of the allocation > >>> * > >>> * Invoke to map CPU address with PCI address. > >>> */ > >>> int pci_epc_map_addr(struct pci_epc *epc, u8 func_no, > >>> - phys_addr_t phys_addr, u64 pci_addr, size_t size) > >>> + phys_addr_t phys_addr, int window, > >>> + u64 pci_addr, size_t size) > >>> { > >>> int ret; > >>> unsigned long flags; > >>> @@ -376,7 +378,8 @@ int pci_epc_map_addr(struct pci_epc *epc, u8 func_no, > >>> return 0; > >>> > >>> spin_lock_irqsave(&epc->lock, flags); > >>> - ret = epc->ops->map_addr(epc, func_no, phys_addr, pci_addr, size); > >>> + ret = epc->ops->map_addr(epc, func_no, phys_addr, > >>> + window, pci_addr, size); > >>> spin_unlock_irqrestore(&epc->lock, flags); > >>> > >>> return ret; > >>> diff --git a/drivers/pci/endpoint/pci-epc-mem.c b/drivers/pci/endpoint/pci-epc-mem.c > >>> index d2b174ce15de..f205f7819292 100644 > >>> --- a/drivers/pci/endpoint/pci-epc-mem.c > >>> +++ b/drivers/pci/endpoint/pci-epc-mem.c > >>> @@ -38,57 +38,77 @@ static int pci_epc_mem_get_order(struct pci_epc_mem *mem, size_t size) > >>> /** > >>> * __pci_epc_mem_init() - initialize the pci_epc_mem structure > >>> * @epc: the EPC device that invoked pci_epc_mem_init > >>> - * @phys_base: the physical address of the base > >>> - * @size: the size of the address space > >>> - * @page_size: size of each page > >>> + * @windows: pointer to windows supported by the device > >>> + * @num_windows: number of windows device supports > >>> * > >>> * Invoke to initialize the pci_epc_mem structure used by the > >>> * endpoint functions to allocate mapped PCI address. > >>> */ > >>> -int __pci_epc_mem_init(struct pci_epc *epc, phys_addr_t phys_base, size_t size, > >>> - size_t page_size) > >>> +int __pci_epc_mem_init(struct pci_epc *epc, struct pci_epc_mem_window *windows, > >>> + int num_windows) > >>> { > >>> - int ret; > >>> - struct pci_epc_mem *mem; > >>> - unsigned long *bitmap; > >>> + struct pci_epc_mem *mem = NULL; > >>> + unsigned long *bitmap = NULL; > >>> unsigned int page_shift; > >>> - int pages; > >>> + size_t page_size; > >>> int bitmap_size; > >>> + int pages; > >>> + int ret; > >>> + int i; > >>> > >>> - if (page_size < PAGE_SIZE) > >>> - page_size = PAGE_SIZE; > >>> + epc->mem_windows = 0; > >>> > >>> - page_shift = ilog2(page_size); > >>> - pages = size >> page_shift; > >>> - bitmap_size = BITS_TO_LONGS(pages) * sizeof(long); > >>> + if (!windows) > >>> + return -EINVAL; > >>> > >>> - mem = kzalloc(sizeof(*mem), GFP_KERNEL); > >>> - if (!mem) { > >>> - ret = -ENOMEM; > >>> - goto err; > >>> - } > >>> + if (num_windows <= 0) > >>> + return -EINVAL; > >>> > >>> - bitmap = kzalloc(bitmap_size, GFP_KERNEL); > >>> - if (!bitmap) { > >>> - ret = -ENOMEM; > >>> - goto err_mem; > >>> - } > >>> + epc->mem = kcalloc(num_windows, sizeof(*mem), GFP_KERNEL); > >>> + if (!epc->mem) > >>> + return -EINVAL; > >>> + > >>> + for (i = 0; i < num_windows; i++) { > >>> + page_size = windows[i].page_size; > >>> + if (page_size < PAGE_SIZE) > >>> + page_size = PAGE_SIZE; > >>> + page_shift = ilog2(page_size); > >>> + pages = windows[i].size >> page_shift; > >>> + bitmap_size = BITS_TO_LONGS(pages) * sizeof(long); > >>> + > >>> + mem = kzalloc(sizeof(*mem), GFP_KERNEL); > >>> + if (!mem) { > >>> + ret = -ENOMEM; > >>> + goto err_mem; > >>> + } > >>> > >>> - mem->bitmap = bitmap; > >>> - mem->phys_base = phys_base; > >>> - mem->page_size = page_size; > >>> - mem->pages = pages; > >>> - mem->size = size; > >>> + bitmap = kzalloc(bitmap_size, GFP_KERNEL); > >>> + if (!bitmap) { > >>> + ret = -ENOMEM; > >>> + goto err_mem; > >>> + } > >>> > >>> - epc->mem = mem; > >>> + mem->bitmap = bitmap; > >>> + mem->window.phys_base = windows[i].phys_base; > >>> + mem->page_size = page_size; > >>> + mem->pages = pages; > >>> + mem->window.size = windows[i].size; > >>> + mem->window.map_size = 0; > >>> + > >>> + epc->mem[i] = mem; > >>> + } > >>> + epc->mem_windows = num_windows; > >>> > >>> return 0; > >>> > >>> err_mem: > >>> - kfree(mem); > >>> + for (; i >= 0; i--) { > >> > >> mem has to be reinitialized for every iteration of the loop. > > not sure what exactly you mean here, could you please elaborate. > > You are invoking "kfree(mem->bitmap);" in a loop without re-initializing > mem. Refer pci_epc_mem_exit() where you are doing the free properly. > good catch will fix that. Cheers, --Prabhakar > > > >>> + kfree(mem->bitmap); > >>> + kfree(epc->mem[i]); > >>> + } > >>> + kfree(epc->mem); > >>> > >>> -err: > >>> -return ret; > >>> + return ret; > >>> } > >>> EXPORT_SYMBOL_GPL(__pci_epc_mem_init); > >>> > >>> @@ -101,48 +121,127 @@ EXPORT_SYMBOL_GPL(__pci_epc_mem_init); > >>> */ > >>> void pci_epc_mem_exit(struct pci_epc *epc) > >>> { > >>> - struct pci_epc_mem *mem = epc->mem; > >>> + struct pci_epc_mem *mem; > >>> + int i; > >>> + > >>> + if (!epc->mem_windows) > >>> + return; > >>> + > >>> + for (i = 0; i <= epc->mem_windows; i++) { > >>> + mem = epc->mem[i]; > > Missing the above line in the error handling above. > > > >>> + kfree(mem->bitmap); > >>> + kfree(epc->mem[i]); > >>> + } > > Thanks > Kishon _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel