From mboxrd@z Thu Jan 1 00:00:00 1970 From: Christoph Hellwig Subject: Re: [PATCH 08/21] dma-iommu: refactor iommu_dma_mmap Date: Tue, 9 Apr 2019 19:25:20 +0200 Message-ID: <20190409172520.GE14679@lst.de> References: <20190327080448.5500-1-hch@lst.de> <20190327080448.5500-9-hch@lst.de> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Content-Disposition: inline In-Reply-To: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: iommu-bounces-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org Errors-To: iommu-bounces-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org To: Robin Murphy Cc: Tom Lendacky , Catalin Marinas , Will Deacon , linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org, Christoph Hellwig , linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org List-Id: iommu@lists.linux-foundation.org On Tue, Apr 09, 2019 at 04:29:07PM +0100, Robin Murphy wrote: > On 27/03/2019 08:04, Christoph Hellwig wrote: >> Move the vm_area handling into __iommu_dma_mmap, which is renamed >> to iommu_dma_mmap_remap. >> >> Inline __iommu_dma_mmap_pfn into the main function to simplify the code >> flow a bit. >> >> Signed-off-by: Christoph Hellwig >> --- >> drivers/iommu/dma-iommu.c | 50 ++++++++++++++------------------------- >> 1 file changed, 18 insertions(+), 32 deletions(-) >> >> diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c >> index d14fe9f8c692..43bd3c7e0f6b 100644 >> --- a/drivers/iommu/dma-iommu.c >> +++ b/drivers/iommu/dma-iommu.c >> @@ -597,23 +597,27 @@ static struct page **__iommu_dma_alloc(struct device *dev, size_t size, >> } >> /** >> - * __iommu_dma_mmap - Map a buffer into provided user VMA >> - * @pages: Array representing buffer from __iommu_dma_alloc() >> + * iommu_dma_mmap_remap - Map a remapped page array into provided user VMA >> + * @cpu_addr: virtual address of the memory to be remapped >> * @size: Size of buffer in bytes >> * @vma: VMA describing requested userspace mapping >> * >> - * Maps the pages of the buffer in @pages into @vma. The caller is responsible >> + * Maps the pages pointed to by @cpu_addr into @vma. The caller is responsible >> * for verifying the correct size and protection of @vma beforehand. >> */ >> -static int __iommu_dma_mmap(struct page **pages, size_t size, >> +static int iommu_dma_mmap_remap(void *cpu_addr, size_t size, >> struct vm_area_struct *vma) >> { >> + struct vm_struct *area = find_vm_area(cpu_addr); >> unsigned long uaddr = vma->vm_start; >> unsigned int i, count = PAGE_ALIGN(size) >> PAGE_SHIFT; >> int ret = -ENXIO; >> + if (WARN_ON(!area || !area->pages)) >> + return -ENXIO; >> + >> for (i = vma->vm_pgoff; i < count && uaddr < vma->vm_end; i++) { >> - ret = vm_insert_page(vma, uaddr, pages[i]); >> + ret = vm_insert_page(vma, uaddr, area->pages[i]); >> if (ret) >> break; >> uaddr += PAGE_SIZE; >> @@ -1052,21 +1056,13 @@ static void iommu_dma_free(struct device *dev, size_t size, void *cpu_addr, >> } >> } >> -static int __iommu_dma_mmap_pfn(struct vm_area_struct *vma, >> - unsigned long pfn, size_t size) >> -{ >> - return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff, >> - vma->vm_end - vma->vm_start, >> - vma->vm_page_prot); >> -} >> - >> static int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma, >> void *cpu_addr, dma_addr_t dma_addr, size_t size, >> unsigned long attrs) >> { >> unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT; >> unsigned long off = vma->vm_pgoff; >> - struct vm_struct *area; >> + unsigned long pfn; >> int ret; >> vma->vm_page_prot = arch_dma_mmap_pgprot(dev, vma->vm_page_prot, >> attrs); >> @@ -1077,25 +1073,15 @@ static int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma, >> if (off >= nr_pages || vma_pages(vma) > nr_pages - off) >> return -ENXIO; >> - if (!is_vmalloc_addr(cpu_addr)) { >> - unsigned long pfn = page_to_pfn(virt_to_page(cpu_addr)); >> - return __iommu_dma_mmap_pfn(vma, pfn, size); >> - } >> + if (is_vmalloc_addr(cpu_addr)) { >> + if (!(attrs & DMA_ATTR_FORCE_CONTIGUOUS)) >> + return iommu_dma_mmap_remap(cpu_addr, size, vma); >> + pfn = vmalloc_to_pfn(cpu_addr); >> + } else >> + pfn = page_to_pfn(virt_to_page(cpu_addr)); > > Nit: braces around the else clause for correct style. So I generally prefer that style too. But in this case one of the later patches will add an ifdef around the if { ... } else, and adding braces would make that a lot more awkware. There is another occurance or two of this patter in this series elsewhere as well. 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=-8.5 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_PASS,URIBL_BLOCKED, USER_AGENT_MUTT 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 C3CC7C282DA for ; Tue, 9 Apr 2019 17:25:35 +0000 (UTC) Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (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 A19732084F for ; Tue, 9 Apr 2019 17:25:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A19732084F Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=iommu-bounces@lists.linux-foundation.org Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 64C55DDC; Tue, 9 Apr 2019 17:25:35 +0000 (UTC) Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 68602DD2 for ; Tue, 9 Apr 2019 17:25:34 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from newverein.lst.de (verein.lst.de [213.95.11.211]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id AB26FF4 for ; Tue, 9 Apr 2019 17:25:33 +0000 (UTC) Received: by newverein.lst.de (Postfix, from userid 2407) id D799268B02; Tue, 9 Apr 2019 19:25:20 +0200 (CEST) Date: Tue, 9 Apr 2019 19:25:20 +0200 From: Christoph Hellwig To: Robin Murphy Subject: Re: [PATCH 08/21] dma-iommu: refactor iommu_dma_mmap Message-ID: <20190409172520.GE14679@lst.de> References: <20190327080448.5500-1-hch@lst.de> <20190327080448.5500-9-hch@lst.de> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.17 (2007-11-01) Cc: Tom Lendacky , Catalin Marinas , Will Deacon , linux-kernel@vger.kernel.org, iommu@lists.linux-foundation.org, Christoph Hellwig , linux-arm-kernel@lists.infradead.org X-BeenThere: iommu@lists.linux-foundation.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: Development issues for Linux IOMMU support List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit Sender: iommu-bounces@lists.linux-foundation.org Errors-To: iommu-bounces@lists.linux-foundation.org Message-ID: <20190409172520.4Mh5lO4EoVI20FpFHAzyRpFgp6NOwr6CCwqc-rYg5c8@z> On Tue, Apr 09, 2019 at 04:29:07PM +0100, Robin Murphy wrote: > On 27/03/2019 08:04, Christoph Hellwig wrote: >> Move the vm_area handling into __iommu_dma_mmap, which is renamed >> to iommu_dma_mmap_remap. >> >> Inline __iommu_dma_mmap_pfn into the main function to simplify the code >> flow a bit. >> >> Signed-off-by: Christoph Hellwig >> --- >> drivers/iommu/dma-iommu.c | 50 ++++++++++++++------------------------- >> 1 file changed, 18 insertions(+), 32 deletions(-) >> >> diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c >> index d14fe9f8c692..43bd3c7e0f6b 100644 >> --- a/drivers/iommu/dma-iommu.c >> +++ b/drivers/iommu/dma-iommu.c >> @@ -597,23 +597,27 @@ static struct page **__iommu_dma_alloc(struct device *dev, size_t size, >> } >> /** >> - * __iommu_dma_mmap - Map a buffer into provided user VMA >> - * @pages: Array representing buffer from __iommu_dma_alloc() >> + * iommu_dma_mmap_remap - Map a remapped page array into provided user VMA >> + * @cpu_addr: virtual address of the memory to be remapped >> * @size: Size of buffer in bytes >> * @vma: VMA describing requested userspace mapping >> * >> - * Maps the pages of the buffer in @pages into @vma. The caller is responsible >> + * Maps the pages pointed to by @cpu_addr into @vma. The caller is responsible >> * for verifying the correct size and protection of @vma beforehand. >> */ >> -static int __iommu_dma_mmap(struct page **pages, size_t size, >> +static int iommu_dma_mmap_remap(void *cpu_addr, size_t size, >> struct vm_area_struct *vma) >> { >> + struct vm_struct *area = find_vm_area(cpu_addr); >> unsigned long uaddr = vma->vm_start; >> unsigned int i, count = PAGE_ALIGN(size) >> PAGE_SHIFT; >> int ret = -ENXIO; >> + if (WARN_ON(!area || !area->pages)) >> + return -ENXIO; >> + >> for (i = vma->vm_pgoff; i < count && uaddr < vma->vm_end; i++) { >> - ret = vm_insert_page(vma, uaddr, pages[i]); >> + ret = vm_insert_page(vma, uaddr, area->pages[i]); >> if (ret) >> break; >> uaddr += PAGE_SIZE; >> @@ -1052,21 +1056,13 @@ static void iommu_dma_free(struct device *dev, size_t size, void *cpu_addr, >> } >> } >> -static int __iommu_dma_mmap_pfn(struct vm_area_struct *vma, >> - unsigned long pfn, size_t size) >> -{ >> - return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff, >> - vma->vm_end - vma->vm_start, >> - vma->vm_page_prot); >> -} >> - >> static int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma, >> void *cpu_addr, dma_addr_t dma_addr, size_t size, >> unsigned long attrs) >> { >> unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT; >> unsigned long off = vma->vm_pgoff; >> - struct vm_struct *area; >> + unsigned long pfn; >> int ret; >> vma->vm_page_prot = arch_dma_mmap_pgprot(dev, vma->vm_page_prot, >> attrs); >> @@ -1077,25 +1073,15 @@ static int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma, >> if (off >= nr_pages || vma_pages(vma) > nr_pages - off) >> return -ENXIO; >> - if (!is_vmalloc_addr(cpu_addr)) { >> - unsigned long pfn = page_to_pfn(virt_to_page(cpu_addr)); >> - return __iommu_dma_mmap_pfn(vma, pfn, size); >> - } >> + if (is_vmalloc_addr(cpu_addr)) { >> + if (!(attrs & DMA_ATTR_FORCE_CONTIGUOUS)) >> + return iommu_dma_mmap_remap(cpu_addr, size, vma); >> + pfn = vmalloc_to_pfn(cpu_addr); >> + } else >> + pfn = page_to_pfn(virt_to_page(cpu_addr)); > > Nit: braces around the else clause for correct style. So I generally prefer that style too. But in this case one of the later patches will add an ifdef around the if { ... } else, and adding braces would make that a lot more awkware. There is another occurance or two of this patter in this series elsewhere as well. _______________________________________________ iommu mailing list iommu@lists.linux-foundation.org https://lists.linuxfoundation.org/mailman/listinfo/iommu 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=-8.5 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_PASS,URIBL_BLOCKED,USER_AGENT_MUTT 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 87DCAC282DA for ; Tue, 9 Apr 2019 17:25:38 +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 58B892133D for ; Tue, 9 Apr 2019 17:25:38 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="TCJKbxSg" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 58B892133D Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de 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:In-Reply-To:MIME-Version:References: Message-ID:Subject:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=+IQHtqW2JAeKnDwE/IC22uTsk/+XfR7NgfvaSTEHsqw=; b=TCJKbxSgkgydmC YJ5peYaDqhAYvwbhwhnTX95vK96NcOE+5dBaJalFRqbFSiTOZMDAVAy5sbGqb2WY6aoXJhURj3VTG 0DCTL3td6ShMEK/97Q93ZxYFcZwG0foIDTUbuHvq0pJyn76ndMHPFYeXrd96K/Mnq3Gk1rh2vIlVO rCDXl6kL6+5gbQlulow1HkfZMxtOTHRPDWI4Gv11gGL29m8vtTfZc2SskWr6b1RXf7yG82gfvEsw2 ert0awdwTy35CuAm3oMqFV5V25ihTTmirpwXwSWTSZ7qC18v90mJ/zcR/jAdMfFPyaJYTkyuW94xC yAd/EXdI2UO9aEcEOyMA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hDuV7-0004rJ-Px; Tue, 09 Apr 2019 17:25:37 +0000 Received: from verein.lst.de ([213.95.11.211] helo=newverein.lst.de) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hDuV4-0004qX-Ei for linux-arm-kernel@lists.infradead.org; Tue, 09 Apr 2019 17:25:36 +0000 Received: by newverein.lst.de (Postfix, from userid 2407) id D799268B02; Tue, 9 Apr 2019 19:25:20 +0200 (CEST) Date: Tue, 9 Apr 2019 19:25:20 +0200 From: Christoph Hellwig To: Robin Murphy Subject: Re: [PATCH 08/21] dma-iommu: refactor iommu_dma_mmap Message-ID: <20190409172520.GE14679@lst.de> References: <20190327080448.5500-1-hch@lst.de> <20190327080448.5500-9-hch@lst.de> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.17 (2007-11-01) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190409_102534_791862_3EC1983A X-CRM114-Status: GOOD ( 19.40 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Tom Lendacky , Catalin Marinas , Joerg Roedel , Will Deacon , linux-kernel@vger.kernel.org, iommu@lists.linux-foundation.org, Christoph Hellwig , linux-arm-kernel@lists.infradead.org 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 On Tue, Apr 09, 2019 at 04:29:07PM +0100, Robin Murphy wrote: > On 27/03/2019 08:04, Christoph Hellwig wrote: >> Move the vm_area handling into __iommu_dma_mmap, which is renamed >> to iommu_dma_mmap_remap. >> >> Inline __iommu_dma_mmap_pfn into the main function to simplify the code >> flow a bit. >> >> Signed-off-by: Christoph Hellwig >> --- >> drivers/iommu/dma-iommu.c | 50 ++++++++++++++------------------------- >> 1 file changed, 18 insertions(+), 32 deletions(-) >> >> diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c >> index d14fe9f8c692..43bd3c7e0f6b 100644 >> --- a/drivers/iommu/dma-iommu.c >> +++ b/drivers/iommu/dma-iommu.c >> @@ -597,23 +597,27 @@ static struct page **__iommu_dma_alloc(struct device *dev, size_t size, >> } >> /** >> - * __iommu_dma_mmap - Map a buffer into provided user VMA >> - * @pages: Array representing buffer from __iommu_dma_alloc() >> + * iommu_dma_mmap_remap - Map a remapped page array into provided user VMA >> + * @cpu_addr: virtual address of the memory to be remapped >> * @size: Size of buffer in bytes >> * @vma: VMA describing requested userspace mapping >> * >> - * Maps the pages of the buffer in @pages into @vma. The caller is responsible >> + * Maps the pages pointed to by @cpu_addr into @vma. The caller is responsible >> * for verifying the correct size and protection of @vma beforehand. >> */ >> -static int __iommu_dma_mmap(struct page **pages, size_t size, >> +static int iommu_dma_mmap_remap(void *cpu_addr, size_t size, >> struct vm_area_struct *vma) >> { >> + struct vm_struct *area = find_vm_area(cpu_addr); >> unsigned long uaddr = vma->vm_start; >> unsigned int i, count = PAGE_ALIGN(size) >> PAGE_SHIFT; >> int ret = -ENXIO; >> + if (WARN_ON(!area || !area->pages)) >> + return -ENXIO; >> + >> for (i = vma->vm_pgoff; i < count && uaddr < vma->vm_end; i++) { >> - ret = vm_insert_page(vma, uaddr, pages[i]); >> + ret = vm_insert_page(vma, uaddr, area->pages[i]); >> if (ret) >> break; >> uaddr += PAGE_SIZE; >> @@ -1052,21 +1056,13 @@ static void iommu_dma_free(struct device *dev, size_t size, void *cpu_addr, >> } >> } >> -static int __iommu_dma_mmap_pfn(struct vm_area_struct *vma, >> - unsigned long pfn, size_t size) >> -{ >> - return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff, >> - vma->vm_end - vma->vm_start, >> - vma->vm_page_prot); >> -} >> - >> static int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma, >> void *cpu_addr, dma_addr_t dma_addr, size_t size, >> unsigned long attrs) >> { >> unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT; >> unsigned long off = vma->vm_pgoff; >> - struct vm_struct *area; >> + unsigned long pfn; >> int ret; >> vma->vm_page_prot = arch_dma_mmap_pgprot(dev, vma->vm_page_prot, >> attrs); >> @@ -1077,25 +1073,15 @@ static int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma, >> if (off >= nr_pages || vma_pages(vma) > nr_pages - off) >> return -ENXIO; >> - if (!is_vmalloc_addr(cpu_addr)) { >> - unsigned long pfn = page_to_pfn(virt_to_page(cpu_addr)); >> - return __iommu_dma_mmap_pfn(vma, pfn, size); >> - } >> + if (is_vmalloc_addr(cpu_addr)) { >> + if (!(attrs & DMA_ATTR_FORCE_CONTIGUOUS)) >> + return iommu_dma_mmap_remap(cpu_addr, size, vma); >> + pfn = vmalloc_to_pfn(cpu_addr); >> + } else >> + pfn = page_to_pfn(virt_to_page(cpu_addr)); > > Nit: braces around the else clause for correct style. So I generally prefer that style too. But in this case one of the later patches will add an ifdef around the if { ... } else, and adding braces would make that a lot more awkware. There is another occurance or two of this patter in this series elsewhere as well. _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel 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=-8.5 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_PASS,URIBL_BLOCKED, USER_AGENT_MUTT autolearn=ham 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 D0666C282CE for ; Tue, 9 Apr 2019 17:25:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id AC7D72084F for ; Tue, 9 Apr 2019 17:25:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727130AbfDIRZf (ORCPT ); Tue, 9 Apr 2019 13:25:35 -0400 Received: from verein.lst.de ([213.95.11.211]:51855 "EHLO newverein.lst.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727104AbfDIRZd (ORCPT ); Tue, 9 Apr 2019 13:25:33 -0400 Received: by newverein.lst.de (Postfix, from userid 2407) id D799268B02; Tue, 9 Apr 2019 19:25:20 +0200 (CEST) Date: Tue, 9 Apr 2019 19:25:20 +0200 From: Christoph Hellwig To: Robin Murphy Cc: Christoph Hellwig , Joerg Roedel , Catalin Marinas , Will Deacon , Tom Lendacky , iommu@lists.linux-foundation.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 08/21] dma-iommu: refactor iommu_dma_mmap Message-ID: <20190409172520.GE14679@lst.de> References: <20190327080448.5500-1-hch@lst.de> <20190327080448.5500-9-hch@lst.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.17 (2007-11-01) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Apr 09, 2019 at 04:29:07PM +0100, Robin Murphy wrote: > On 27/03/2019 08:04, Christoph Hellwig wrote: >> Move the vm_area handling into __iommu_dma_mmap, which is renamed >> to iommu_dma_mmap_remap. >> >> Inline __iommu_dma_mmap_pfn into the main function to simplify the code >> flow a bit. >> >> Signed-off-by: Christoph Hellwig >> --- >> drivers/iommu/dma-iommu.c | 50 ++++++++++++++------------------------- >> 1 file changed, 18 insertions(+), 32 deletions(-) >> >> diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c >> index d14fe9f8c692..43bd3c7e0f6b 100644 >> --- a/drivers/iommu/dma-iommu.c >> +++ b/drivers/iommu/dma-iommu.c >> @@ -597,23 +597,27 @@ static struct page **__iommu_dma_alloc(struct device *dev, size_t size, >> } >> /** >> - * __iommu_dma_mmap - Map a buffer into provided user VMA >> - * @pages: Array representing buffer from __iommu_dma_alloc() >> + * iommu_dma_mmap_remap - Map a remapped page array into provided user VMA >> + * @cpu_addr: virtual address of the memory to be remapped >> * @size: Size of buffer in bytes >> * @vma: VMA describing requested userspace mapping >> * >> - * Maps the pages of the buffer in @pages into @vma. The caller is responsible >> + * Maps the pages pointed to by @cpu_addr into @vma. The caller is responsible >> * for verifying the correct size and protection of @vma beforehand. >> */ >> -static int __iommu_dma_mmap(struct page **pages, size_t size, >> +static int iommu_dma_mmap_remap(void *cpu_addr, size_t size, >> struct vm_area_struct *vma) >> { >> + struct vm_struct *area = find_vm_area(cpu_addr); >> unsigned long uaddr = vma->vm_start; >> unsigned int i, count = PAGE_ALIGN(size) >> PAGE_SHIFT; >> int ret = -ENXIO; >> + if (WARN_ON(!area || !area->pages)) >> + return -ENXIO; >> + >> for (i = vma->vm_pgoff; i < count && uaddr < vma->vm_end; i++) { >> - ret = vm_insert_page(vma, uaddr, pages[i]); >> + ret = vm_insert_page(vma, uaddr, area->pages[i]); >> if (ret) >> break; >> uaddr += PAGE_SIZE; >> @@ -1052,21 +1056,13 @@ static void iommu_dma_free(struct device *dev, size_t size, void *cpu_addr, >> } >> } >> -static int __iommu_dma_mmap_pfn(struct vm_area_struct *vma, >> - unsigned long pfn, size_t size) >> -{ >> - return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff, >> - vma->vm_end - vma->vm_start, >> - vma->vm_page_prot); >> -} >> - >> static int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma, >> void *cpu_addr, dma_addr_t dma_addr, size_t size, >> unsigned long attrs) >> { >> unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT; >> unsigned long off = vma->vm_pgoff; >> - struct vm_struct *area; >> + unsigned long pfn; >> int ret; >> vma->vm_page_prot = arch_dma_mmap_pgprot(dev, vma->vm_page_prot, >> attrs); >> @@ -1077,25 +1073,15 @@ static int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma, >> if (off >= nr_pages || vma_pages(vma) > nr_pages - off) >> return -ENXIO; >> - if (!is_vmalloc_addr(cpu_addr)) { >> - unsigned long pfn = page_to_pfn(virt_to_page(cpu_addr)); >> - return __iommu_dma_mmap_pfn(vma, pfn, size); >> - } >> + if (is_vmalloc_addr(cpu_addr)) { >> + if (!(attrs & DMA_ATTR_FORCE_CONTIGUOUS)) >> + return iommu_dma_mmap_remap(cpu_addr, size, vma); >> + pfn = vmalloc_to_pfn(cpu_addr); >> + } else >> + pfn = page_to_pfn(virt_to_page(cpu_addr)); > > Nit: braces around the else clause for correct style. So I generally prefer that style too. But in this case one of the later patches will add an ifdef around the if { ... } else, and adding braces would make that a lot more awkware. There is another occurance or two of this patter in this series elsewhere as well.