From patchwork Wed Mar 29 10:05:26 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hajda X-Patchwork-Id: 9651153 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id A9ECD602C8 for ; Wed, 29 Mar 2017 10:06:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9C441206E2 for ; Wed, 29 Mar 2017 10:06:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8EBBE28405; Wed, 29 Mar 2017 10:06:14 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id A8FB0206E2 for ; Wed, 29 Mar 2017 10:06:13 +0000 (UTC) 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:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: Message-id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:List-Owner; bh=Qi0DC0BrspUZQe4Ke7EoUmI7gKlGpTKfwtdPoYT1TT4=; b=FU f4tYrQwmw4SeDqOPHzE95SdXIMw+HWy9YiB8OgI9/7M39oMyROdW/qSDs/uHb+UGQbU7FRFWBKqQs /pqo3hyDjn35XF1VhSWbjXaar9GvbStEdaKU5b8I+Tt7wii9u2Nhq2MT+OHsX+HTpzAQcS025UbMO Hrzrwz2MB/J/rfQHxobsbwYqNP1hBrIB/yMbLBFEH+/ptexEP0cu2moqitCW56e3AqzC/LcEGg6kw RfUD9LtD+lM1wsvgRYcpvJIHf8AdS+OBwaEO9Qbc7Df+zAMWlygXW08zE1kGA5/DyA8wFTl1wAXMl 8UT86+bjdVsvPcSRbetLqWMhpyNtNhiQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1ctAUW-0001OF-RZ; Wed, 29 Mar 2017 10:06:12 +0000 Received: from mailout2.w1.samsung.com ([210.118.77.12]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1ctAUS-0001Jb-Et for linux-arm-kernel@lists.infradead.org; Wed, 29 Mar 2017 10:06:10 +0000 Received: from eucas1p2.samsung.com (unknown [182.198.249.207]) by mailout2.w1.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTP id <0ONK00MBWMPIBG30@mailout2.w1.samsung.com> for linux-arm-kernel@lists.infradead.org; Wed, 29 Mar 2017 11:05:42 +0100 (BST) Received: from eusmges5.samsung.com (unknown [203.254.199.245]) by eucas1p2.samsung.com (KnoxPortal) with ESMTP id 20170329100541eucas1p23c9438571d9e2cc31c31926948d4ee1a~wUkbzpqZX0992309923eucas1p2S; Wed, 29 Mar 2017 10:05:41 +0000 (GMT) Received: from eucas1p2.samsung.com ( [182.198.249.207]) by eusmges5.samsung.com (EUCPMTA) with SMTP id 21.11.25577.4F68BD85; Wed, 29 Mar 2017 11:05:41 +0100 (BST) Received: from eusmgms1.samsung.com (unknown [182.198.249.179]) by eucas1p2.samsung.com (KnoxPortal) with ESMTP id 20170329100540eucas1p22d428e33aa678cbef0a24dd249820451~wUkbIrR0x0990909909eucas1p2L; Wed, 29 Mar 2017 10:05:40 +0000 (GMT) X-AuditID: cbfec7f5-f792f6d0000063e9-4f-58db86f4f548 Received: from eusync3.samsung.com ( [203.254.199.213]) by eusmgms1.samsung.com (EUCPMTA) with SMTP id F6.74.17452.5678BD85; Wed, 29 Mar 2017 11:07:33 +0100 (BST) Received: from AMDC2768.DIGITAL.local ([106.120.43.17]) by eusync3.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTPA id <0ONK0079GMPF7B30@eusync3.samsung.com>; Wed, 29 Mar 2017 11:05:40 +0100 (BST) From: Andrzej Hajda To: Catalin Marinas , linux-arm-kernel@lists.infradead.org Subject: [PATCH] arm64/dma-mapping: fix DMA_ATTR_FORCE_CONTIGUOUS mmaping code Date: Wed, 29 Mar 2017 12:05:26 +0200 Message-id: <1490781926-6209-1-git-send-email-a.hajda@samsung.com> X-Mailer: git-send-email 2.7.4 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrKIsWRmVeSWpSXmKPExsWy7djP87pf225HGPxuYrW4te4cq8XGGetZ Ld4v62G0mDt7EqPFgv3WFpseX2O1WHvkLrvFwQ9PWC1efjzB4sDpsWbeGkaPiWd1PTYvqfeY fGM5o0ffllWMAaxRXDYpqTmZZalF+nYJXBnrdvQzF7wWqljz6whrA+Ma/i5GTg4JAROJvvZD LBC2mMSFe+vZuhi5OIQEljJKPJz9kQkkISTwmVFi034XmIat17qYIIqWMUo0/LvHDuH8Z5SY 2v2NEaSKTUBT4u/mm2wgtohAqMSNrp1gY5kFVjFJ3D5zFmyssECAxLOeR2ANLAKqEuf2nWAG sXkFnCT2H2plg1gnJ3HzXCczSLOEwGM2iVPdm4GO5QByZCU2HWCGqHGRmD//ATuELSzx6vgW KFtGorPjIBNEbzejxKf+E+wQzhRGiX8fZkB1W0scPn6RFcRmFuCTmLRtOjPEAl6JjjYhiBIP iUXbJrNC2I4SF1ecYQcpERKIlWg5Yz6BU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uuL2/N8+7HvnvvvLLZ5MYVtRN O1li+jIhX76kdffksL/nvaot574q0szctrjH/k9vumTqEZkl8XcZItIa92szz1mlxFKckWio xVxUnAgAfxCAl2wCAAA= X-MTR: 20000000000000000@CPGS X-CMS-MailID: 20170329100540eucas1p22d428e33aa678cbef0a24dd249820451 X-Msg-Generator: CA X-Sender-IP: 182.198.249.179 X-Local-Sender: =?UTF-8?B?QW5kcnplaiBIYWpkYRtTUlBPTC1LZXJuZWwgKFRQKRvsgrw=?= =?UTF-8?B?7ISx7KCE7J6QG1NlbmlvciBTb2Z0d2FyZSBFbmdpbmVlcg==?= X-Global-Sender: =?UTF-8?B?QW5kcnplaiBIYWpkYRtTUlBPTC1LZXJuZWwgKFRQKRtTYW1z?= =?UTF-8?B?dW5nIEVsZWN0cm9uaWNzG1NlbmlvciBTb2Z0d2FyZSBFbmdpbmVlcg==?= X-Sender-Code: =?UTF-8?B?QzEwG0VIURtDMTBDRDAyQ0QwMjczOTI=?= CMS-TYPE: 201P X-HopCount: 7 X-CMS-RootMailID: 20170329100540eucas1p22d428e33aa678cbef0a24dd249820451 X-RootMTR: 20170329100540eucas1p22d428e33aa678cbef0a24dd249820451 References: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170329_030608_650425_859F9CF8 X-CRM114-Status: GOOD ( 17.69 ) 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: Geert Uytterhoeven , Bartlomiej Zolnierkiewicz , Will Deacon , Andrzej Hajda , iommu@lists.linux-foundation.org, Robin Murphy , Marek Szyprowski MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP In case of DMA_ATTR_FORCE_CONTIGUOUS allocations vm_area->pages is invalid. __iommu_mmap_attrs and __iommu_get_sgtable cannot use it. In first case temporary pages array is passed to iommu_dma_mmap, in 2nd case single entry sg table is created directly instead of calling helper. Fixes: 44176bb ("arm64: Add support for DMA_ATTR_FORCE_CONTIGUOUS to IOMMU") Signed-off-by: Andrzej Hajda --- Hi, I am not familiar with this framework so please don't be too cruel ;) Alternative solution I see is to always create vm_area->pages, I do not know which approach is preferred. Regards Andrzej --- arch/arm64/mm/dma-mapping.c | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/arch/arm64/mm/dma-mapping.c b/arch/arm64/mm/dma-mapping.c index f7b5401..bba2bc8 100644 --- a/arch/arm64/mm/dma-mapping.c +++ b/arch/arm64/mm/dma-mapping.c @@ -704,7 +704,30 @@ static int __iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma, return ret; area = find_vm_area(cpu_addr); - if (WARN_ON(!area || !area->pages)) + if (WARN_ON(!area)) + return -ENXIO; + + if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) { + struct page *page = vmalloc_to_page(cpu_addr); + unsigned int count = size >> PAGE_SHIFT; + struct page **pages; + unsigned long pfn; + int i; + + pages = kmalloc_array(count, sizeof(*pages), GFP_KERNEL); + if (!pages) + return -ENOMEM; + + for (i = 0, pfn = page_to_pfn(page); i < count; i++) + pages[i] = pfn_to_page(pfn + i); + + ret = iommu_dma_mmap(pages, size, vma); + kfree(pages); + + return ret; + } + + if (WARN_ON(!area->pages)) return -ENXIO; return iommu_dma_mmap(area->pages, size, vma); @@ -717,7 +740,20 @@ static int __iommu_get_sgtable(struct device *dev, struct sg_table *sgt, unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT; struct vm_struct *area = find_vm_area(cpu_addr); - if (WARN_ON(!area || !area->pages)) + if (WARN_ON(!area)) + return -ENXIO; + + if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) { + int ret = sg_alloc_table(sgt, 1, GFP_KERNEL); + + if (!ret) + sg_set_page(sgt->sgl, vmalloc_to_page(cpu_addr), + PAGE_ALIGN(size), 0); + + return ret; + } + + if (WARN_ON(!area->pages)) return -ENXIO; return sg_alloc_table_from_pages(sgt, area->pages, count, 0, size,