Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp2865788pxb; Mon, 1 Nov 2021 03:31:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyYbwijpbsX5vxIC95EZafKgJqrBPcF3M7IVMF3VZx8/ubNh0SAaKLDEmi1G9KIp3HsedpO X-Received: by 2002:a05:6e02:194a:: with SMTP id x10mr8819840ilu.115.1635762661423; Mon, 01 Nov 2021 03:31:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635762661; cv=none; d=google.com; s=arc-20160816; b=w7M7nhjQmK+KJkGrK2KnZjT4G+k+q8xXhQuH0QBOsglXmd8/r85Sq4xDienDK9pYyM BP4Rbqwa6ew0xOIdH55HZMEFrNi1UU5F/UlVtBR2HKgFh52tydS4HWal27zYnGcbNCAh PZbnWJ6EDX4ihPGu6nJ1X/hxaU52oCgRBLJWZqq1IZRPxnc2bXd3H4efjmmjJV7Kdsr7 PtASnMN2Enbs+fnfsYnkEeSD8yc5qPxsIe18TuJ1ZkaDhbXkpNYV3C/NPjzMjBLNT6tj 7IVU0LRG53dlw3/Sk+Hm3UVpmdk3G8g1lSnehD/AZt/96nxgRsmKWDMyQtvTfiEjntLg 1X+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=QxuElwCJOAz8Rf8xtAcNWt3xdVU7XAGoe6tgXSB0Fqc=; b=eA1+YsORpwkl4C8Rj5EJVatUA7kQK0ipqI+fmOkROU42x2yG30Puw0TOqmCZMv3uZM HrRRtEr73pG8T0F99dKBuIm+mp/0nHP3Q3Ocz9LyO+uUWL37+kKBtoUvA4texclkoZvH nD28KNegO+8e3nLBOEK9vQHWiNzG6+cYQgiyXyvVEBBuz6k70acB8cTLFmMnojFW3mIF V0nqkcue62kOJOqoSIFnPmwSOd0ryIsjnGJw7juKPfSo0bG015cJOAru/r9O/8M++aAo v2nTVVnDiYFQ+Zyw4vtje29Neti6Qbt4CXBhYBD+T4x+CBAlfkGhaGatC8TqY6CSUM7m HwrQ== 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=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x11si14027301iov.52.2021.11.01.03.30.49; Mon, 01 Nov 2021 03:31:01 -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=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232135AbhKAKcC (ORCPT + 99 others); Mon, 1 Nov 2021 06:32:02 -0400 Received: from foss.arm.com ([217.140.110.172]:37610 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232100AbhKAKcB (ORCPT ); Mon, 1 Nov 2021 06:32:01 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 2382411B3; Mon, 1 Nov 2021 03:29:28 -0700 (PDT) Received: from [10.57.80.217] (unknown [10.57.80.217]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 21D2B3F719; Mon, 1 Nov 2021 03:29:26 -0700 (PDT) Message-ID: <0316e4f0-b0ad-c702-676f-36347b4ebcb1@arm.com> Date: Mon, 1 Nov 2021 10:29:19 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Thunderbird/91.2.1 Subject: Re: [PATCH] dma-direct: fix DMA_ATTR_NO_KERNEL_MAPPING Content-Language: en-GB To: Walter Wu , Christoph Hellwig , Marek Szyprowski , Matthias Brugger Cc: iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, wsd_upstream , linux-mediatek@lists.infradead.org, Andrew Morton References: <20211101031558.7184-1-walter-zh.wu@mediatek.com> From: Robin Murphy In-Reply-To: <20211101031558.7184-1-walter-zh.wu@mediatek.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. 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; >