From nobody Fri Sep 20 08:35:00 2024 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 58F98C4332F for ; Tue, 12 Dec 2023 02:47:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345767AbjLLCrI (ORCPT ); Mon, 11 Dec 2023 21:47:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235004AbjLLCrE (ORCPT ); Mon, 11 Dec 2023 21:47:04 -0500 Received: from mailgw02.mediatek.com (unknown [210.61.82.184]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7B16112B; Mon, 11 Dec 2023 18:47:04 -0800 (PST) X-UUID: b6c73d96989811eeba30773df0976c77-20231212 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=3V6024Solz4jQYx03PCU2DnkByw7fBKMpDmrnrFyUcU=; b=pOmEvuXoxChbJWuq92cRacJm0mAMVy3dBVGjYOxuxr1fwA6Ek6HOSlnaku5zgJV37qK1aLd3vtkQ/4jXQ+QFldwXBMNYRpsVW24D9Qzs71ch8w95iFsjWoGXVkLQz5xezE7Vb3KihPJ3L5Pdk6OJmLRsYas3xHfWyWwPU05Adg8=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.35,REQID:1c248f56-d357-4ed1-8361-6c3eb6df2ef2,IP:0,U RL:0,TC:0,Content:0,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION: release,TS:0 X-CID-META: VersionHash:5d391d7,CLOUDID:2fda8efd-4a48-46e2-b946-12f04f20af8c,B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:-3,IP:nil,U RL:0,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO, DKR:0,DKP:0,BRR:0,BRE:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-UUID: b6c73d96989811eeba30773df0976c77-20231212 Received: from mtkmbs11n2.mediatek.inc [(172.21.101.187)] by mailgw02.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 72758738; Tue, 12 Dec 2023 10:46:56 +0800 Received: from mtkmbs11n2.mediatek.inc (172.21.101.187) by mtkmbs13n2.mediatek.inc (172.21.101.108) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Tue, 12 Dec 2023 10:46:55 +0800 Received: from mhfsdcap04.gcn.mediatek.inc (10.17.3.154) by mtkmbs11n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.26 via Frontend Transport; Tue, 12 Dec 2023 10:46:54 +0800 From: Yong Wu To: Rob Herring , Sumit Semwal , , Matthias Brugger CC: Krzysztof Kozlowski , Conor Dooley , Benjamin Gaignard , Brian Starkey , John Stultz , , AngeloGioacchino Del Regno , Yong Wu , , , , , , , , , , Vijayanand Jitta , Joakim Bech , Jeffrey Kardatzke , Nicolas Dufresne , Subject: [PATCH v3 3/7] dma-buf: heaps: secure_heap: Add private heap ops Date: Tue, 12 Dec 2023 10:46:03 +0800 Message-ID: <20231212024607.3681-4-yong.wu@mediatek.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231212024607.3681-1-yong.wu@mediatek.com> References: <20231212024607.3681-1-yong.wu@mediatek.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add "struct secure_heap_ops". For the secure memory, totally there are two steps: a) memory_alloc: Allocate the buffer in kernel; b) secure_the_memory: Secure/Proect that buffer. The memory_alloc is mandatory while secure_the_memory is optinal since it may be part of memory_alloc. Signed-off-by: Yong Wu --- drivers/dma-buf/heaps/secure_heap.c | 39 ++++++++++++++++++++++++++++- drivers/dma-buf/heaps/secure_heap.h | 11 ++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/drivers/dma-buf/heaps/secure_heap.c b/drivers/dma-buf/heaps/se= cure_heap.c index e087da5638aa..925cf8e1c7ce 100644 --- a/drivers/dma-buf/heaps/secure_heap.c +++ b/drivers/dma-buf/heaps/secure_heap.c @@ -12,10 +12,42 @@ =20 #include "secure_heap.h" =20 +static int secure_heap_memory_allocate(struct secure_heap *sec_heap, struc= t secure_buffer *sec_buf) +{ + const struct secure_heap_ops *ops =3D sec_heap->ops; + int ret; + + ret =3D ops->memory_alloc(sec_heap, sec_buf); + if (ret) + return ret; + + if (ops->secure_the_memory) { + ret =3D ops->secure_the_memory(sec_heap, sec_buf); + if (ret) + goto sec_memory_free; + } + return 0; + +sec_memory_free: + ops->memory_free(sec_heap, sec_buf); + return ret; +} + +static void secure_heap_memory_free(struct secure_heap *sec_heap, struct s= ecure_buffer *sec_buf) +{ + const struct secure_heap_ops *ops =3D sec_heap->ops; + + if (ops->unsecure_the_memory) + ops->unsecure_the_memory(sec_heap, sec_buf); + + ops->memory_free(sec_heap, sec_buf); +} + static struct dma_buf * secure_heap_allocate(struct dma_heap *heap, unsigned long size, unsigned long fd_flags, unsigned long heap_flags) { + struct secure_heap *sec_heap =3D dma_heap_get_drvdata(heap); struct secure_buffer *sec_buf; DEFINE_DMA_BUF_EXPORT_INFO(exp_info); struct dma_buf *dmabuf; @@ -28,6 +60,9 @@ secure_heap_allocate(struct dma_heap *heap, unsigned long= size, sec_buf->size =3D ALIGN(size, PAGE_SIZE); sec_buf->heap =3D heap; =20 + ret =3D secure_heap_memory_allocate(sec_heap, sec_buf); + if (ret) + goto err_free_buf; exp_info.exp_name =3D dma_heap_get_name(heap); exp_info.size =3D sec_buf->size; exp_info.flags =3D fd_flags; @@ -36,11 +71,13 @@ secure_heap_allocate(struct dma_heap *heap, unsigned lo= ng size, dmabuf =3D dma_buf_export(&exp_info); if (IS_ERR(dmabuf)) { ret =3D PTR_ERR(dmabuf); - goto err_free_buf; + goto err_free_sec_mem; } =20 return dmabuf; =20 +err_free_sec_mem: + secure_heap_memory_free(sec_heap, sec_buf); err_free_buf: kfree(sec_buf); return ERR_PTR(ret); diff --git a/drivers/dma-buf/heaps/secure_heap.h b/drivers/dma-buf/heaps/se= cure_heap.h index 50733dc6d4db..ec5349cd28d0 100644 --- a/drivers/dma-buf/heaps/secure_heap.h +++ b/drivers/dma-buf/heaps/secure_heap.h @@ -15,6 +15,17 @@ struct secure_buffer { =20 struct secure_heap { const char *name; + + const struct secure_heap_ops *ops; +}; + +struct secure_heap_ops { + int (*memory_alloc)(struct secure_heap *sec_heap, struct secure_buffer *s= ec_buf); + void (*memory_free)(struct secure_heap *sec_heap, struct secure_buffer *s= ec_buf); + + /* Protect/unprotect the memory */ + int (*secure_the_memory)(struct secure_heap *sec_heap, struct secure_buff= er *sec_buf); + void (*unsecure_the_memory)(struct secure_heap *sec_heap, struct secure_b= uffer *sec_buf); }; =20 int secure_heap_add(struct secure_heap *sec_heap); --=20 2.25.1