From patchwork Mon Nov 1 03:15:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Walter Wu X-Patchwork-Id: 12595615 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 86D23C433EF for ; Mon, 1 Nov 2021 03:17:44 +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 393E160EB9 for ; Mon, 1 Nov 2021 03:17:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 393E160EB9 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=mediatek.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:Subject:CC :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=P7i1ieV/cT1DBTphMHewYDYnZd3Su03VZzhJUWlAqd0=; b=VBDdhlgjhvlmse EcNFnxmipYBbH1thu0sFBFmRqHqq72peuiBCH1883/tB6fxbZOS2qTkx1mROuNVUrVWd0Q/pOTpQ2 mfi+zAI448IW5m1HbWr/JpDhLPMCKjmmYK8S5yZmYQkzzFOQy261fg4NuvmkeqWS40Wdx+XFswCUC caQp1t+ZyFDBZLlHehstd6Prlp01+frbR++Gdx28tnyq2k77g8+10hFLUt5fXdDsNKnHqCgXuaER9 uMeQHw1Vg8gc6hasrvHUDSozmorWwpQRYtk/eSCUbtNm8dF9KeDgOKi9TZDUOb0bYCoUsC68/RGxp xtzoRp3XXQnj+uuWLBGw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mhNnu-00F9b8-DU; Mon, 01 Nov 2021 03:16:10 +0000 Received: from mailgw02.mediatek.com ([216.200.240.185]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mhNnq-00F9aF-51; Mon, 01 Nov 2021 03:16:07 +0000 X-UUID: e3503b147d9f428397ec84e50885618c-20211031 X-UUID: e3503b147d9f428397ec84e50885618c-20211031 Received: from mtkcas66.mediatek.inc [(172.29.193.44)] by mailgw02.mediatek.com (envelope-from ) (musrelay.mediatek.com ESMTP with TLSv1.2 ECDHE-RSA-AES256-SHA384 256/256) with ESMTP id 1888997292; Sun, 31 Oct 2021 20:16:02 -0700 Received: from mtkmbs10n1.mediatek.inc (172.21.101.34) by MTKMBS62N2.mediatek.inc (172.29.193.42) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Sun, 31 Oct 2021 20:16:01 -0700 Received: from mtkcas11.mediatek.inc (172.21.101.40) by mtkmbs10n1.mediatek.inc (172.21.101.34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.2.792.15; Mon, 1 Nov 2021 11:15:59 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by mtkcas11.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Mon, 1 Nov 2021 11:15:59 +0800 From: Walter Wu To: Christoph Hellwig , Marek Szyprowski , Robin Murphy , "Matthias Brugger" CC: , , , wsd_upstream , , Walter Wu , Andrew Morton Subject: [PATCH] dma-direct: fix DMA_ATTR_NO_KERNEL_MAPPING Date: Mon, 1 Nov 2021 11:15:58 +0800 Message-ID: <20211101031558.7184-1-walter-zh.wu@mediatek.com> X-Mailer: git-send-email 2.18.0 MIME-Version: 1.0 X-MTK: N X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211031_201606_225809_FDBF0675 X-CRM114-Status: GOOD ( 15.63 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DMA_ATTR_NO_KERNEL_MAPPING is to avoid creating a kernel mapping for the allocated buffer, but current implementation is that PTE of allocated buffer in kernel page table is valid. So we should set invalid for PTE of allocate buffer so that there are no kernel mapping for the allocated buffer. In some cases, we don't hope the allocated buffer to be read by cpu or speculative execution, so we use DMA_ATTR_NO_KERNEL_MAPPING to get no kernel mapping in order to achieve this goal. Signed-off-by: Walter Wu Cc: Christoph Hellwig Cc: Marek Szyprowski Cc: Robin Murphy Cc: Matthias Brugger Cc: Andrew Morton Reported-by: kernel test robot --- kernel/dma/direct.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c index 4c6c5e0635e3..aa10b4c5d762 100644 --- a/kernel/dma/direct.c +++ b/kernel/dma/direct.c @@ -13,6 +13,7 @@ #include #include #include +#include #include "direct.h" /* @@ -169,6 +170,9 @@ void *dma_direct_alloc(struct device *dev, size_t size, if (!PageHighMem(page)) arch_dma_prep_coherent(page, size); *dma_handle = phys_to_dma_direct(dev, page_to_phys(page)); + /* remove kernel mapping for pages */ + set_memory_valid((unsigned long)phys_to_virt(dma_to_phys(dev, *dma_handle)), + size >> PAGE_SHIFT, 0); /* return the page pointer as the opaque cookie */ return page; } @@ -278,6 +282,10 @@ void dma_direct_free(struct device *dev, size_t size, if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) && !force_dma_unencrypted(dev) && !is_swiotlb_for_alloc(dev)) { + size = PAGE_ALIGN(size); + /* create kernel mapping for pages */ + set_memory_valid((unsigned long)phys_to_virt(dma_to_phys(dev, dma_addr)), + size >> PAGE_SHIFT, 1); /* cpu_addr is a struct page cookie, not a kernel address */ dma_free_contiguous(dev, cpu_addr, size); return;