Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp2946088pxb; Mon, 1 Nov 2021 05:09:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzrtac/Dh/eNspD5d61geiC18QIr0jPJLN0cGsu4dVVjVorEeP/03ZfF2Ek6yFtpa5fiwW1 X-Received: by 2002:a05:6e02:bf4:: with SMTP id d20mr20006783ilu.146.1635768550088; Mon, 01 Nov 2021 05:09:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635768550; cv=none; d=google.com; s=arc-20160816; b=H5C1URZXatE+B9mXg2G6ZPutwfw1zzDV1HtyK1ynnpCEYz6aRYARwNyIngUTQDP3BZ 9Z4T9GOsHiMoBUW5uZRTRgyIcfv19tRu+CZXsm/bJ0YvD6KY1HUrNi9lzk/e1i7rUvDi kTnrmMcDfRIO9YMrFuF+eesjlvfA1Ci44vhkbahRyU7Zi8F0mXGTF4cK5Uq8aHqC4xZZ BYwcD6K6RzstlhEhnxsziBB+NZbQEItEfd3rGGGWG9J6QeF6t4xvTPB02QH+JLTRVxg8 oUFPkSLLKA1JojJxxf2wzO39YTg3cUgNqreWa3K+G4/GTEuQjSIJkjizaOJpVmnapvqK uw6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:date:cc:to:from:subject:message-id; bh=Ff9zSEOqxvYlEDboLgnQTlmpTPgO5o60oJ0qjwSw8LM=; b=QN8kRVQXoPdko6NBLAZOpWHcOofBApoOo4CQ1OY2VJf0KI+kBf0/x5uoxV1tsI8+oQ geiI/GSKWOfd+36S7FI1jxffAZWirRF93wyEXmgER5OSjBCwAZC1jLcaDVZYPtGGVNYN fpHW0S9XSzX+urHfyPsBDPQiCkM7+0u8f85ndn2TbJz4q0SK0qGBoU9hAkta1TOwhLsB 3VpyfagA/2WzAYc+gjdQlALrFcrys4dAfoBWIiulN10WqYPSilTZyxdNkhVezPY9Fy2k cGGlEztV3P+ZLIEYpU1ojiuvL2erakC/FjOP4y3hHRzp04vkAKiZ132xY22SBXVWnurU NdUw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mediatek.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v12si15910150jas.67.2021.11.01.05.08.57; Mon, 01 Nov 2021 05:09:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mediatek.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232241AbhKAMKd (ORCPT + 99 others); Mon, 1 Nov 2021 08:10:33 -0400 Received: from mailgw01.mediatek.com ([60.244.123.138]:43936 "EHLO mailgw01.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S231673AbhKAMKc (ORCPT ); Mon, 1 Nov 2021 08:10:32 -0400 X-UUID: 96666a0304944cc38cc087434f5944a7-20211101 X-UUID: 96666a0304944cc38cc087434f5944a7-20211101 Received: from mtkexhb02.mediatek.inc [(172.21.101.103)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-SHA384 256/256) with ESMTP id 909134349; Mon, 01 Nov 2021 20:07:56 +0800 Received: from mtkcas11.mediatek.inc (172.21.101.40) by mtkmbs10n2.mediatek.inc (172.21.101.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.2.792.3; Mon, 1 Nov 2021 20:07:54 +0800 Received: from mtksdccf07 (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 20:07:54 +0800 Message-ID: <73d900564ce9b0d773c801efe8b8689946bef150.camel@mediatek.com> Subject: Re: [PATCH] dma-direct: fix DMA_ATTR_NO_KERNEL_MAPPING From: Walter Wu To: Robin Murphy , Christoph Hellwig , Marek Szyprowski , Matthias Brugger CC: , , , wsd_upstream , , "Andrew Morton" Date: Mon, 1 Nov 2021 20:07:54 +0800 In-Reply-To: <0316e4f0-b0ad-c702-676f-36347b4ebcb1@arm.com> References: <20211101031558.7184-1-walter-zh.wu@mediatek.com> <0316e4f0-b0ad-c702-676f-36347b4ebcb1@arm.com> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.28.5-0ubuntu0.18.04.2 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Robin, On Mon, 2021-11-01 at 10:29 +0000, Robin Murphy wrote: > On 2021-11-01 03:15, Walter Wu wrote: > > 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. > > No, the semantic of NO_KERNEL_MAPPING is an indication that the > *caller* > does not need a mapping, such that the DMA API implementation may > choose > to optimise for that internally. It has never given any guarantee of > any > particular behaviour - like most attributes it is only a hint. > > > 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. > > If it's important that no CPU accesses to this memory can happen, > then I > think the only way to absolutely guarantee that is to exclude it > from > the kernel's memory map in the first place, e.g. as a DT reserved- > memory > region with the "no-map" property. > Yes, this is our previous implementation, but we hope to use kernel memory to fix it. Thanks for your suggestion. Walter > Robin. > > > Signed-off-by: Walter Wu > > Cc: Christoph Hellwig > > Cc: Marek Szyprowski > > Cc: Robin Murphy > > Cc: Matthias Brugger > > Cc: Andrew Morton > > --- > > 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; > >