From patchwork Wed Mar 29 16:33:35 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Russell King (Oracle)" X-Patchwork-Id: 9651949 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 C8CC660113 for ; Wed, 29 Mar 2017 16:36:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BB9B8284E7 for ; Wed, 29 Mar 2017 16:36:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B0642284F9; Wed, 29 Mar 2017 16:36:08 +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 CDFEA284F2 for ; Wed, 29 Mar 2017 16:36:07 +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: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=bLao7pNAOxe1ehzFgJIkGHpP+HQ/0c45mCV9YLSkaBI=; b=rCOn+7CEmsqiIW zCYWLkg4y7mJkmCvb+vBxEyrF9EHUW8UcE19w2Tn2/PAkXKeQ5hZODiZQFTkrA60/EODWZqA2P/xM t7rihK3I+jLVlzHtl4DHK1kKXFz7dzucDSC+V5/ULQw+KuvW50SOzH9j1dbCW4ZeROkvGGANXHSRm PQW9oMZhH1opkFbm0SsAtr05VzITGcHw/ouJQPH5G2jgLWidEPyQJpnH0yVGJnF/OfM7LxtcYPrC4 ijxboT7NOlBegFTy+QfFjwiVA1d7iRLSZtvyfqw52bRTaTWERh5YORskLHF5A7nuZObNcHv+8iLvm gD8Z0fOLzrOzu+08SfeQ==; 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 1ctGZp-0004O9-7x; Wed, 29 Mar 2017 16:36:05 +0000 Received: from pandora.armlinux.org.uk ([2001:4d48:ad52:3201:214:fdff:fe10:1be6]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1ctGY4-0002Gk-LH for linux-arm-kernel@lists.infradead.org; Wed, 29 Mar 2017 16:34:20 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=armlinux.org.uk; s=pandora-2014; h=Sender:In-Reply-To:Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date; bh=q42a9vd+Yjh1vtBqRl7/TjuJF3kvMjgE7nvTO/1NaOo=; b=FD33IOEl1HvVP2GgtBVGeJVhB73lxeOzaOMGCOoWhyTS8nsGtbE4pNAwlhfgmlQOOGFODSNXHj210aZMSbt0F//47oSbUZiPqmRIZ9A1h/LrpPvbAH2isCVvKmqDuUsL1RXCuhOr+DJsi0GaEAVCgsBE7osv4uvRt6JJR8b29Ko=; Received: from n2100.armlinux.org.uk ([fd8f:7570:feb6:1:214:fdff:fe10:4f86]:56900) by pandora.armlinux.org.uk with esmtpsa (TLSv1:DHE-RSA-AES256-SHA:256) (Exim 4.82_1-5b7a7c0-XX) (envelope-from ) id 1ctGXT-0002nh-DW; Wed, 29 Mar 2017 17:33:39 +0100 Received: from linux by n2100.armlinux.org.uk with local (Exim 4.76) (envelope-from ) id 1ctGXQ-0006Yg-78; Wed, 29 Mar 2017 17:33:36 +0100 Date: Wed, 29 Mar 2017 17:33:35 +0100 From: Russell King - ARM Linux To: Shuah Khan Subject: Re: [PATCH] ARM: dma-mapping: Fix dma_get_sgtable() for regions without struct page Message-ID: <20170329163335.GA7909@n2100.armlinux.org.uk> References: <1487682998-2564-1-git-send-email-m.szyprowski@samsung.com> <20170322111020.GD21222@n2100.armlinux.org.uk> <20170322163237.GF21222@n2100.armlinux.org.uk> <20170322224200.GH21222@n2100.armlinux.org.uk> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20170322224200.GH21222@n2100.armlinux.org.uk> User-Agent: Mutt/1.5.23 (2014-03-12) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170329_093417_236687_C9B974A6 X-CRM114-Status: GOOD ( 20.35 ) 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: linux-arm-kernel@lists.infradead.org, Marek Szyprowski , shuahkh@osg.samsung.com, stable , Bartlomiej Zolnierkiewicz 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 Okay, I'm about to merge the following patch for -rc, since refusing to create a scattertable for non-page backed memory is the only valid solution for that case. I'm intending to queue this for -rc this evening. 8<==== ARM: dma-mapping: disallow dma_get_sgtable() for non-kernel managed memory dma_get_sgtable() tries to create a scatterlist table containing valid struct page pointers for the coherent memory allocation passed in to it. However, memory can be declared via dma_declare_coherent_memory(), or via other reservation schemes which means that coherent memory is not guaranteed to be backed by struct pages. This means it is not possible to create a valid scatterlist via this mechanism. This patch adds detection of such memory, and refuses to create a scatterlist table for such memory. Signed-off-by: Russell King --- arch/arm/mm/dma-mapping.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/arch/arm/mm/dma-mapping.c b/arch/arm/mm/dma-mapping.c index 63eabb06f9f1..475811f5383a 100644 --- a/arch/arm/mm/dma-mapping.c +++ b/arch/arm/mm/dma-mapping.c @@ -935,13 +935,31 @@ static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_add __arm_dma_free(dev, size, cpu_addr, handle, attrs, true); } +/* + * The whole dma_get_sgtable() idea is fundamentally unsafe - it seems + * that the intention is to allow exporting memory allocated via the + * coherent DMA APIs through the dma_buf API, which only accepts a + * scattertable. This presents a couple of problems: + * 1. Not all memory allocated via the coherent DMA APIs is backed by + * a struct page + * 2. Passing coherent DMA memory into the streaming APIs is not allowed + * as we will try to flush the memory through a different alias to that + * actually being used (and the flushes are redundant.) + */ int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt, void *cpu_addr, dma_addr_t handle, size_t size, unsigned long attrs) { - struct page *page = pfn_to_page(dma_to_pfn(dev, handle)); + unsigned long pfn = dma_to_pfn(dev, handle); + struct page *page; int ret; + /* If the PFN is not valid, we do not have a struct page */ + if (!pfn_valid(pfn)) + return -ENXIO; + + page = pfn_to_page(pfn); + ret = sg_alloc_table(sgt, 1, GFP_KERNEL); if (unlikely(ret)) return ret;