Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp2597203pxb; Sun, 31 Oct 2021 20:17:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzY+cpLgZOcUdFnPWFp2eG/merbji/rdPJf+V9bT3yPk1DeWsW+3y/0WGKSLDWUpSYjXEV6 X-Received: by 2002:a6b:c403:: with SMTP id y3mr1999671ioa.21.1635736645879; Sun, 31 Oct 2021 20:17:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635736645; cv=none; d=google.com; s=arc-20160816; b=yVFAbLmtT6iuIdlKRM51n38E3ggAWqQcsCBr0z2ZpmJtJ0wH9yKIguwgCbs3PEgIaF jTyTIhFr3Yut90QUgqwl3LEfYFZZjMFYCJNdrxBFSMZvIx0pq2d4qlq0qWxpA2zPzjbD WpTBQd9Dp2b9Oy9X94OPrdxQ61oqR6tsriHVOojZr+J8BJm13VSkorHLY9qshxkkFOw+ 6e5+MuqxZNk/IG7ScOsYoEzLNq/ven6yAHn6bY4bD22nu0qV8z7w5qqxSx6W6N/XwQHV S+xHABfYlU86X2naIUE8PbPEISrhUHc0uvMcTeeuPfj87FPeV6+uNw6l4mMbpyimRx9z KNog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:cc:to:from; bh=5Zgw7i1m5yJtGzGFhhEqX/43fNiGJ6wHEApkMGg4xJU=; b=iapRKOcOOJzL0J/ZX0sSIEwlp2YeukrrrMX4c6rEOhIMFgMxhTuP1hzFuJyQJPibB+ tjk9InCpjJ75UgtpzQN3s6lI8DaE5GgUxBi3K47RLdpaIlZocCL3JmBIQgQIpaRfzgS7 p/+c6A6NlVOY0K5eMRzTkAh/DlA1ztZjkSRtWFN5tXyzrTnDAlYWhxjRXThegN3jQtjk NdFyVvxhUctXREzYFypgfCFh/bTUnbvy7HjpAoPuYxV+AV6HYpzrq7aZqqBxbvHR4Kn+ XHjrDPVOKgPGkTBLD82x1Sg5sz1CubRsskUgFWTD4qytviEBSdBMYQLEiiChAc/ZJo5a BW2Q== 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 t2si11639370jam.80.2021.10.31.20.17.14; Sun, 31 Oct 2021 20:17:25 -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 S230395AbhKADSi (ORCPT + 99 others); Sun, 31 Oct 2021 23:18:38 -0400 Received: from mailgw01.mediatek.com ([60.244.123.138]:35560 "EHLO mailgw01.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S230222AbhKADSh (ORCPT ); Sun, 31 Oct 2021 23:18:37 -0400 X-UUID: a197251d0ae64fcd84ee44743b2b31b3-20211101 X-UUID: a197251d0ae64fcd84ee44743b2b31b3-20211101 Received: from mtkmbs10n1.mediatek.inc [(172.21.101.34)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 1302630291; Mon, 01 Nov 2021 11:16:01 +0800 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 Content-Type: text/plain X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.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 --- 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; -- 2.18.0