From nobody Thu Oct 2 20:23:05 2025 Received: from mail-yw1-f170.google.com (mail-yw1-f170.google.com [209.85.128.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 09153334379 for ; Thu, 11 Sep 2025 13:50:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757598626; cv=none; b=ThM32Q68NLFersXXG5k1/DbcyV/uHpf1yCatW0SIJfNh9qJOcVWRTGrPl1cZgeSjrKk9THet04P9v0Di+WzlnU7NqUgCJ5HskDcUVlDWLg3bFaE8SMeX1p8NE6z4779zN7fm78wdiY9YKwcop9NKTHmauMIq6p6rYzPb5zfR/Uw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757598626; c=relaxed/simple; bh=ovdW9RwxPXvPyFLUcp3vOmS7R37lIdCV3kq+hjKNdhk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Qd5xWra7lNpeHr/F9TPz4RoSlOvkvgNiPvgyLeOKCJ/knNEr0Do5wVbeTBXJ4uzX1eprl6rJiobx9fNCgHF06cYXh4XKFsgm9jmbHUcijJdZQlubgg3G5Gw2pzOC20bV3WOaYGsoaMt35963SBA8RG3liHh8/ESo5AJ0L9w+wFY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=dKE3usdj; arc=none smtp.client-ip=209.85.128.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="dKE3usdj" Received: by mail-yw1-f170.google.com with SMTP id 00721157ae682-71d6083cc69so7188667b3.2 for ; Thu, 11 Sep 2025 06:50:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1757598621; x=1758203421; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=x4btL1Ny/VgPh+KNykLdZfm62nIfCyoogsQt1MKlMMs=; b=dKE3usdj1BLXO9i8Wke4t7pjXIz7pJsU09AazCyZcytYOFV6txvXRkLL2gNoQEZcta G5kU8Vf9CxRmHsh9O+KmQpu7hp14oSg3ZGO71DwOjuCmuRb4gEdzV959vdHXx4iYh3n4 7Q+og8K89vzlva1lOR9M20k2yyPgZQNsQijNghaCktqN6ZM9d0YPv4KL5jRi3LFf9Ral nmG4ztZtisMDqSBFDEQUaxz4Pqgjdh7d0NW02FE4hSCNitA1LE9jmFELZEbBMvj4m3F5 47JBBnosUoxW40AQlFrJmQPZoDI7AEqGON4PUl76nT+a3dYehrRAQWwu07wUlN8KDGTG e1MA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757598621; x=1758203421; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=x4btL1Ny/VgPh+KNykLdZfm62nIfCyoogsQt1MKlMMs=; b=cZURnv/Mb/ftgG8lKB3EZXNlvw51QFSHkj4ld26IHEYgBbVY3Q3K/8KNpdk0CTKP5a TCIU7wdIEiiNwIz/tDSzdaJq8Fku2mRaQvBflvb6K81ldvXCXNFl+L+K9DlIStJ71dHE 1jCch2CrYxW64VcbZA5PuQYs0MrGgS1LjWRVhlyt2BdEZpDqylG5m+5T31PC/d+bQ72m UR4VtP77NYoeoie2aNR+m8xp2LALScugKz3ohj4av0bPGaFEx+jJXteCT9B5BDgWtQ0F Mju3nVIJwZMSAkJqFqqSpMIcjqW9aZ/1Usj/WUq3Err8FVHDIb3woQrOVaiWQad/XIAC FxEg== X-Gm-Message-State: AOJu0YyYbungzv/gNw8JGxBe7zRyst3s/VVc1UF4VHlZUC5YKoq6h2Cu GtEPnYXYzunqk0Uvtw3GM2+qdiBb9hyH+KKLPvfwMoQOM5eIhwUP3LO2FHthGlylTHKqndaph18 iZGVjf9peKQ== X-Gm-Gg: ASbGncuU/Kd80fvDWM2xWPvnNGds1yusGc/9QbokjVXPqgrBmCzrPEidgSgxK/Mhy8D yc3v5o6wxPgM2hSi5yyQFcq8ocxQv4lVwxQS2Nn3BDsa7P62mWb3JphZ23FwBdw7Ve/9w7M4SuD sB24iG7mr0WG8wszYmJcXMWtlasuxxHAhND+iN88s/K3xpSyl0VztXEpDm6po/4QVuP2+bbvRIR myIw2UhMSyOchrltsW92jKsF7WcahWK1dYnu7cp1ZVohSRCJ+i9jke5vbjQ8KiF024poSmCgpLu XjLPeJayxcfpR/Cfh//ZMgfg0YoYidiG2U3fq0+5bcjC1U1XJR/IjKdhZHummgrN/4pgBSsX1zA w50wQnxyLl1NbdisX//G3x/W399suT/TYP0SvutkKdlvf1F98t7YtNEoIlWlN+Wi6jvDEeC3cP3 MVY9rozqkfEA== X-Google-Smtp-Source: AGHT+IEHpsckeeWyVXpemY4tDq3wSUZW+nPm11vA/lsrhCABjpCLlAsACaAGZ/LnfZ+HJsZwehMFlQ== X-Received: by 2002:a05:690c:6087:b0:725:25b1:c775 with SMTP id 00721157ae682-727f2bd1f4dmr151131397b3.8.1757598620963; Thu, 11 Sep 2025 06:50:20 -0700 (PDT) Received: from rayden.urgonet (h-37-123-177-177.A175.priv.bahnhof.se. [37.123.177.177]) by smtp.gmail.com with ESMTPSA id 00721157ae682-72f7623434csm3526257b3.11.2025.09.11.06.50.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Sep 2025 06:50:19 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, op-tee@lists.trustedfirmware.org, linux-arm-kernel@lists.infradead.org Cc: Olivier Masse , Thierry Reding , Yong Wu , Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T . J . Mercier" , =?UTF-8?q?Christian=20K=C3=B6nig?= , Sumit Garg , Matthias Brugger , AngeloGioacchino Del Regno , azarrabi@qti.qualcomm.com, Simona Vetter , Daniel Stone , Rouven Czerwinski , robin.murphy@arm.com, Jens Wiklander , Sumit Garg Subject: [PATCH v12 1/9] optee: sync secure world ABI headers Date: Thu, 11 Sep 2025 15:49:42 +0200 Message-ID: <20250911135007.1275833-2-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250911135007.1275833-1-jens.wiklander@linaro.org> References: <20250911135007.1275833-1-jens.wiklander@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Update the header files describing the secure world ABI, both with and without FF-A. The ABI is extended to deal with protected memory, but as usual backward compatible. Reviewed-by: Sumit Garg Signed-off-by: Jens Wiklander --- drivers/tee/optee/optee_ffa.h | 27 ++++++++--- drivers/tee/optee/optee_msg.h | 84 ++++++++++++++++++++++++++++++----- drivers/tee/optee/optee_smc.h | 37 ++++++++++++++- 3 files changed, 130 insertions(+), 18 deletions(-) diff --git a/drivers/tee/optee/optee_ffa.h b/drivers/tee/optee/optee_ffa.h index 257735ae5b56..cc257e7956a3 100644 --- a/drivers/tee/optee/optee_ffa.h +++ b/drivers/tee/optee/optee_ffa.h @@ -81,7 +81,7 @@ * as the second MSG arg struct for * OPTEE_FFA_YIELDING_CALL_WITH_ARG. * Bit[31:8]: Reserved (MBZ) - * w5: Bitfield of secure world capabilities OPTEE_FFA_SEC_CAP_* below, + * w5: Bitfield of OP-TEE capabilities OPTEE_FFA_SEC_CAP_* * w6: The maximum secure world notification number * w7: Not used (MBZ) */ @@ -94,6 +94,8 @@ #define OPTEE_FFA_SEC_CAP_ASYNC_NOTIF BIT(1) /* OP-TEE supports probing for RPMB device if needed */ #define OPTEE_FFA_SEC_CAP_RPMB_PROBE BIT(2) +/* OP-TEE supports Protected Memory for secure data path */ +#define OPTEE_FFA_SEC_CAP_PROTMEM BIT(3) =20 #define OPTEE_FFA_EXCHANGE_CAPABILITIES OPTEE_FFA_BLOCKING_CALL(2) =20 @@ -108,7 +110,7 @@ * * Return register usage: * w3: Error code, 0 on success - * w4-w7: Note used (MBZ) + * w4-w7: Not used (MBZ) */ #define OPTEE_FFA_UNREGISTER_SHM OPTEE_FFA_BLOCKING_CALL(3) =20 @@ -119,16 +121,31 @@ * Call register usage: * w3: Service ID, OPTEE_FFA_ENABLE_ASYNC_NOTIF * w4: Notification value to request bottom half processing, should be - * less than OPTEE_FFA_MAX_ASYNC_NOTIF_VALUE. + * less than OPTEE_FFA_MAX_ASYNC_NOTIF_VALUE * w5-w7: Not used (MBZ) * * Return register usage: * w3: Error code, 0 on success - * w4-w7: Note used (MBZ) + * w4-w7: Not used (MBZ) */ #define OPTEE_FFA_ENABLE_ASYNC_NOTIF OPTEE_FFA_BLOCKING_CALL(5) =20 -#define OPTEE_FFA_MAX_ASYNC_NOTIF_VALUE 64 +#define OPTEE_FFA_MAX_ASYNC_NOTIF_VALUE 64 + +/* + * Release Protected memory + * + * Call register usage: + * w3: Service ID, OPTEE_FFA_RECLAIM_PROTMEM + * w4: Shared memory handle, lower bits + * w5: Shared memory handle, higher bits + * w6-w7: Not used (MBZ) + * + * Return register usage: + * w3: Error code, 0 on success + * w4-w7: Note used (MBZ) + */ +#define OPTEE_FFA_RELEASE_PROTMEM OPTEE_FFA_BLOCKING_CALL(8) =20 /* * Call with struct optee_msg_arg as argument in the supplied shared memory diff --git a/drivers/tee/optee/optee_msg.h b/drivers/tee/optee/optee_msg.h index e8840a82b983..838e1d4a22f0 100644 --- a/drivers/tee/optee/optee_msg.h +++ b/drivers/tee/optee/optee_msg.h @@ -133,13 +133,13 @@ struct optee_msg_param_rmem { }; =20 /** - * struct optee_msg_param_fmem - ffa memory reference parameter + * struct optee_msg_param_fmem - FF-A memory reference parameter * @offs_lower: Lower bits of offset into shared memory reference * @offs_upper: Upper bits of offset into shared memory reference * @internal_offs: Internal offset into the first page of shared memory * reference * @size: Size of the buffer - * @global_id: Global identifier of Shared memory + * @global_id: Global identifier of the shared memory */ struct optee_msg_param_fmem { u32 offs_low; @@ -165,7 +165,7 @@ struct optee_msg_param_value { * @attr: attributes * @tmem: parameter by temporary memory reference * @rmem: parameter by registered memory reference - * @fmem: parameter by ffa registered memory reference + * @fmem: parameter by FF-A registered memory reference * @value: parameter by opaque value * @octets: parameter by octet string * @@ -296,6 +296,18 @@ struct optee_msg_arg { */ #define OPTEE_MSG_FUNCID_GET_OS_REVISION 0x0001 =20 +/* + * Values used in OPTEE_MSG_CMD_LEND_PROTMEM below + * OPTEE_MSG_PROTMEM_RESERVED Reserved + * OPTEE_MSG_PROTMEM_SECURE_VIDEO_PLAY Secure Video Playback + * OPTEE_MSG_PROTMEM_TRUSTED_UI Trused UI + * OPTEE_MSG_PROTMEM_SECURE_VIDEO_RECORD Secure Video Recording + */ +#define OPTEE_MSG_PROTMEM_RESERVED 0 +#define OPTEE_MSG_PROTMEM_SECURE_VIDEO_PLAY 1 +#define OPTEE_MSG_PROTMEM_TRUSTED_UI 2 +#define OPTEE_MSG_PROTMEM_SECURE_VIDEO_RECORD 3 + /* * Do a secure call with struct optee_msg_arg as argument * The OPTEE_MSG_CMD_* below defines what goes in struct optee_msg_arg::cmd @@ -337,15 +349,63 @@ struct optee_msg_arg { * OPTEE_MSG_CMD_STOP_ASYNC_NOTIF informs secure world that from now is * normal world unable to process asynchronous notifications. Typically * used when the driver is shut down. + * + * OPTEE_MSG_CMD_LEND_PROTMEM lends protected memory. The passed normal + * physical memory is protected from normal world access. The memory + * should be unmapped prior to this call since it becomes inaccessible + * during the request. + * Parameters are passed as: + * [in] param[0].attr OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + * [in] param[0].u.value.a OPTEE_MSG_PROTMEM_* defined above + * [in] param[1].attr OPTEE_MSG_ATTR_TYPE_TMEM_INPUT + * [in] param[1].u.tmem.buf_ptr physical address + * [in] param[1].u.tmem.size size + * [in] param[1].u.tmem.shm_ref holds protected memory reference + * + * OPTEE_MSG_CMD_RECLAIM_PROTMEM reclaims a previously lent protected + * memory reference. The physical memory is accessible by the normal world + * after this function has return and can be mapped again. The information + * is passed as: + * [in] param[0].attr OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + * [in] param[0].u.value.a holds protected memory cookie + * + * OPTEE_MSG_CMD_GET_PROTMEM_CONFIG get configuration for a specific + * protected memory use case. Parameters are passed as: + * [in] param[0].attr OPTEE_MSG_ATTR_TYPE_VALUE_INOUT + * [in] param[0].value.a OPTEE_MSG_PROTMEM_* + * [in] param[1].attr OPTEE_MSG_ATTR_TYPE_{R,F}MEM_OUTPUT + * [in] param[1].u.{r,f}mem Buffer or NULL + * [in] param[1].u.{r,f}mem.size Provided size of buffer or 0 for query + * output for the protected use case: + * [out] param[0].value.a Minimal size of protected memory + * [out] param[0].value.b Required alignment of size and start of + * protected memory + * [out] param[0].value.c PA width, max 64 + * [out] param[1].{r,f}mem.size Size of output data + * [out] param[1].{r,f}mem If non-NULL, contains an array of + * uint32_t memory attributes that must be + * included when lending memory for this + * use case + * + * OPTEE_MSG_CMD_ASSIGN_PROTMEM assigns use-case to protected memory + * previously lent using the FFA_LEND framework ABI. Parameters are passed + * as: + * [in] param[0].attr OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + * [in] param[0].u.value.a holds protected memory cookie + * [in] param[0].u.value.b OPTEE_MSG_PROTMEM_* defined above */ -#define OPTEE_MSG_CMD_OPEN_SESSION 0 -#define OPTEE_MSG_CMD_INVOKE_COMMAND 1 -#define OPTEE_MSG_CMD_CLOSE_SESSION 2 -#define OPTEE_MSG_CMD_CANCEL 3 -#define OPTEE_MSG_CMD_REGISTER_SHM 4 -#define OPTEE_MSG_CMD_UNREGISTER_SHM 5 -#define OPTEE_MSG_CMD_DO_BOTTOM_HALF 6 -#define OPTEE_MSG_CMD_STOP_ASYNC_NOTIF 7 -#define OPTEE_MSG_FUNCID_CALL_WITH_ARG 0x0004 +#define OPTEE_MSG_CMD_OPEN_SESSION 0 +#define OPTEE_MSG_CMD_INVOKE_COMMAND 1 +#define OPTEE_MSG_CMD_CLOSE_SESSION 2 +#define OPTEE_MSG_CMD_CANCEL 3 +#define OPTEE_MSG_CMD_REGISTER_SHM 4 +#define OPTEE_MSG_CMD_UNREGISTER_SHM 5 +#define OPTEE_MSG_CMD_DO_BOTTOM_HALF 6 +#define OPTEE_MSG_CMD_STOP_ASYNC_NOTIF 7 +#define OPTEE_MSG_CMD_LEND_PROTMEM 8 +#define OPTEE_MSG_CMD_RECLAIM_PROTMEM 9 +#define OPTEE_MSG_CMD_GET_PROTMEM_CONFIG 10 +#define OPTEE_MSG_CMD_ASSIGN_PROTMEM 11 +#define OPTEE_MSG_FUNCID_CALL_WITH_ARG 0x0004 =20 #endif /* _OPTEE_MSG_H */ diff --git a/drivers/tee/optee/optee_smc.h b/drivers/tee/optee/optee_smc.h index 879426300821..accf76a99288 100644 --- a/drivers/tee/optee/optee_smc.h +++ b/drivers/tee/optee/optee_smc.h @@ -264,7 +264,6 @@ struct optee_smc_get_shm_config_result { #define OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM BIT(0) /* Secure world can communicate via previously unregistered shared memory = */ #define OPTEE_SMC_SEC_CAP_UNREGISTERED_SHM BIT(1) - /* * Secure world supports commands "register/unregister shared memory", * secure world accepts command buffers located in any parts of non-secure= RAM @@ -280,6 +279,10 @@ struct optee_smc_get_shm_config_result { #define OPTEE_SMC_SEC_CAP_RPC_ARG BIT(6) /* Secure world supports probing for RPMB device if needed */ #define OPTEE_SMC_SEC_CAP_RPMB_PROBE BIT(7) +/* Secure world supports protected memory */ +#define OPTEE_SMC_SEC_CAP_PROTMEM BIT(8) +/* Secure world supports dynamic protected memory */ +#define OPTEE_SMC_SEC_CAP_DYNAMIC_PROTMEM BIT(9) =20 #define OPTEE_SMC_FUNCID_EXCHANGE_CAPABILITIES 9 #define OPTEE_SMC_EXCHANGE_CAPABILITIES \ @@ -451,6 +454,38 @@ struct optee_smc_disable_shm_cache_result { =20 /* See OPTEE_SMC_CALL_WITH_REGD_ARG above */ #define OPTEE_SMC_FUNCID_CALL_WITH_REGD_ARG 19 +/* + * Get protected memory config + * + * Returns the protected memory config. + * + * Call register usage: + * a0 SMC Function ID, OPTEE_SMC_GET_PROTMEM_CONFIG + * a2-6 Not used, must be zero + * a7 Hypervisor Client ID register + * + * Have config return register usage: + * a0 OPTEE_SMC_RETURN_OK + * a1 Physical address of start of protected memory + * a2 Size of protected memory + * a3 PA width, max 64 + * a4-7 Preserved + * + * Not available register usage: + * a0 OPTEE_SMC_RETURN_ENOTAVAIL + * a1-3 Not used + * a4-7 Preserved + */ +#define OPTEE_SMC_FUNCID_GET_PROTMEM_CONFIG 20 +#define OPTEE_SMC_GET_PROTMEM_CONFIG \ + OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_PROTMEM_CONFIG) + +struct optee_smc_get_protmem_config_result { + unsigned long status; + unsigned long start; + unsigned long size; + unsigned long pa_width; +}; =20 /* * Resume from RPC (for example after processing a foreign interrupt) --=20 2.43.0 From nobody Thu Oct 2 20:23:05 2025 Received: from mail-yw1-f182.google.com (mail-yw1-f182.google.com [209.85.128.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B95E633CE83 for ; Thu, 11 Sep 2025 13:50:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757598630; cv=none; b=H7PTDE1vGStThPCHKzOxIyMmrcWrYLZsUVGAN35dH1+z3i7NGWPztV9E1S0MFRb/5C/KsXMHJkB+CKkVm7SJPJvpKDmbs71/vqGaXsk+do11h+xFAUNdcPtls1btxZH3xWueJFvGxC7IrLLW9o/kMEb22MbV4jwOMInS1nA2sI0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757598630; c=relaxed/simple; bh=IkVF+7F9WyqmG87CnPExaTvcAvbRHkr9Pe/0B1R1Ayc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=f5Ewf0nuBtW8YmcRf4q2vKJW/KZEM/x22QtZxXLTgonFGaVYRIU6zljtrfOSO8vQH8iZL1CxDqAWzXiump3tSH9OkMOgKIM/l7VsFNw/WjDs6TFcyyV3lGrJptzqKEXziLOxVSh+XxUpDMT8ItRkuxez2h17OVTqmboOxph5no8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=CJurTIBg; arc=none smtp.client-ip=209.85.128.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="CJurTIBg" Received: by mail-yw1-f182.google.com with SMTP id 00721157ae682-71d71bcac45so5334857b3.0 for ; Thu, 11 Sep 2025 06:50:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1757598626; x=1758203426; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=oMt0VPE9nsvTEazWH1lgiwsk3x9NY1aluOH28YbXegk=; b=CJurTIBg17pcMGNTwpvrMd8BbgdD8ZkQwQKZ4nRT4PAtJW+4bZ+dIS4lpb2i7gx2FY oPXV9gK0lFLYB7S+jsLX0Md5i76WRl1N2Zzfyg45+HUllQtxOaYRoyp3kXNkSpfHPn0W yCpFxh9tD1v5kEE3kWLrd73q680BywnmRzRFtyupxV4l65D26q/1EFa1lngRDN1knR8N F4HsXnCw/6sKZKjgNh5SNTUhcSN8O0c41smMbaPmsyWfvatYSAtnV//hdYrm1OTU8L9P KoXosQfDOvRRKcGBLFwdL4B1TX6EgAhqlXR+gB+B4p9mvtptCZjBn9481694/VhUt0Zn Ge5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757598626; x=1758203426; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=oMt0VPE9nsvTEazWH1lgiwsk3x9NY1aluOH28YbXegk=; b=j/f9jyAKlmm5W5uNJayaWGzrlSYY45zHuJJj00dxxriEObwir5gggA0ezUsUi5q0jY bm2VbMv6ElRcIzLSY6s1R/5gPciJB3wHWY3EsgH0oWZefibn0J0i2HjhQdvQlzsIY0zy VGi4/1pyIhDLUb9gnGZfNcOH0n2E78hJ0vXEiwPoGeSSW4Y/cnUIMomktMAmj2M7TwFE wwKPYHudLOykQ4ZwSv7dCaFL0p8BuLnPewBCKW2hWKoKAcbeaFgHVI3j/Xydpd8W8cUf B2IC/oTU3P69V4vGK4XLcBsakCQHPgv/dkQWLTZag2RUDUU82wxfB85XegeWk49KJb+3 WSLA== X-Gm-Message-State: AOJu0Yxt6WGmuu4Oef8PszIj42vz7I044HITnAr2h8jOz6tgWDH71Cju kKqkHvGN8XFYb5BKGPScGWifigzCVdEwT6DNIYUcCPeQtSs5/bNzF52m9wXsHCcjtPnogPnJjpo 8uk2lzB+Rjg== X-Gm-Gg: ASbGncsM2k/i/CMQ/F8E+JYmxuDG7h47lTaHP0+640Iz13f5OXwc4Rtn99DkRXtLzMZ JypZv2jAiJKZXWJpKpnT7JTeH70CaJSCNsWPg8Bz5ufa50VzF5EzsOWFM8X6ErRy46fGqmzTkxp rJYoI9FYAZ/NgpvABg2Ji/RHctYkF3l/XBzEbda2bvYCapGhOrhlvmiL88QKwACMiBymp4MOpnd Z2fHtx+9Qfw7jq7iNbHAtoMFV80ld311+i1rWYfIZKaMZN2xYbZ9IItb3wSEG9++YDdddjZW4jW 40qRpwyl+Uakv37/bKFODHr5yxUADNmSNhBI5ZAtValH55CTdragiqQiOz3WQihYJEZsrlm/cea 7UAOBY4ObXflzMFuPwi6CJVfRwPgtt37Lm2QJpAOBJyIOTDJdhFLliDEt3kTPWJTUgT8tWE1WRl g= X-Google-Smtp-Source: AGHT+IG0oRMoou++RsxpxUsUtwpzC5AT+90eCVdw6DARCJFWQoizSSi/Z2eS8ssSBxttr2gzHNfb5w== X-Received: by 2002:a05:690c:7506:b0:72a:6e48:d7a9 with SMTP id 00721157ae682-72a6e49273amr160447507b3.10.1757598625802; Thu, 11 Sep 2025 06:50:25 -0700 (PDT) Received: from rayden.urgonet (h-37-123-177-177.A175.priv.bahnhof.se. [37.123.177.177]) by smtp.gmail.com with ESMTPSA id 00721157ae682-72f7623434csm3526257b3.11.2025.09.11.06.50.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Sep 2025 06:50:24 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, op-tee@lists.trustedfirmware.org, linux-arm-kernel@lists.infradead.org Cc: Olivier Masse , Thierry Reding , Yong Wu , Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T . J . Mercier" , =?UTF-8?q?Christian=20K=C3=B6nig?= , Sumit Garg , Matthias Brugger , AngeloGioacchino Del Regno , azarrabi@qti.qualcomm.com, Simona Vetter , Daniel Stone , Rouven Czerwinski , robin.murphy@arm.com, Jens Wiklander , Sumit Garg Subject: [PATCH v12 2/9] dma-buf: dma-heap: export declared functions Date: Thu, 11 Sep 2025 15:49:43 +0200 Message-ID: <20250911135007.1275833-3-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250911135007.1275833-1-jens.wiklander@linaro.org> References: <20250911135007.1275833-1-jens.wiklander@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Export the dma-buf heap functions to allow them to be used by the OP-TEE driver. The OP-TEE driver wants to register and manage specific secure DMA heaps with it. Reviewed-by: Sumit Garg Reviewed-by: T.J. Mercier Acked-by: Sumit Semwal Signed-off-by: Jens Wiklander --- drivers/dma-buf/dma-heap.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c index 3cbe87d4a464..8ab49924f8b7 100644 --- a/drivers/dma-buf/dma-heap.c +++ b/drivers/dma-buf/dma-heap.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -202,6 +203,7 @@ void *dma_heap_get_drvdata(struct dma_heap *heap) { return heap->priv; } +EXPORT_SYMBOL_NS_GPL(dma_heap_get_drvdata, "DMA_BUF_HEAP"); =20 /** * dma_heap_get_name - get heap name @@ -214,6 +216,7 @@ const char *dma_heap_get_name(struct dma_heap *heap) { return heap->name; } +EXPORT_SYMBOL_NS_GPL(dma_heap_get_name, "DMA_BUF_HEAP"); =20 /** * dma_heap_add - adds a heap to dmabuf heaps @@ -303,6 +306,7 @@ struct dma_heap *dma_heap_add(const struct dma_heap_exp= ort_info *exp_info) kfree(heap); return err_ret; } +EXPORT_SYMBOL_NS_GPL(dma_heap_add, "DMA_BUF_HEAP"); =20 static char *dma_heap_devnode(const struct device *dev, umode_t *mode) { --=20 2.43.0 From nobody Thu Oct 2 20:23:05 2025 Received: from mail-yw1-f175.google.com (mail-yw1-f175.google.com [209.85.128.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 86F0A33EB18 for ; Thu, 11 Sep 2025 13:50:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757598636; cv=none; b=c6qte5jjbhS/0ZHXVo8KYsuFkgXhM708FfvRXH9uqFzOTtIfMX8JEOBKkurdsfDS7GDlLLiScIG7X0lhr8TGg5IYydAHe65xi2j7u/F8/VSWZGpbffbk7TtB4vvrxWv2cVmSTIOSpva2cOKCOw88yfbkNBJH2S9qRMiBzCeCUFQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757598636; c=relaxed/simple; bh=JClCcXsFOpe8ltg2r8Tj9Jqe4t3Q+6BET+kNtyJ9Zm8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RipjNoX7eh/HY1aF9Q8GxFFagWOjPYeGUyz6C3D8+3QILPfLICb5rZKxNnthb2MRfJWSByyrt9eOfRkroSK1MJjQZi8YVXxLOFrfgSGUjuvCILVVCVEhk2HOk8iitqO8irTgewUXEzCCzZLOR0TrGuhUH2OfA88OBeCgoFOPGoo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=tQXOkCtd; arc=none smtp.client-ip=209.85.128.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="tQXOkCtd" Received: by mail-yw1-f175.google.com with SMTP id 00721157ae682-71d60157747so5513067b3.0 for ; Thu, 11 Sep 2025 06:50:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1757598632; x=1758203432; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+V+AWXb8up2Msrqiaf69upU8kliovcRX1LdolHvEDfU=; b=tQXOkCtdOKyZSuI2BBK2cPr7c/WHXqrx8KNYgo6rBcDFBNCbp0u/uJPFlpslE3fRPi uDa5P2bUBLmzAwv2nSFWfNuLPpByANUy5e2vWoIv/C+MZEpkNBQyEDpcZvYp8e3DQ9FH pxZgEC+laMF/6JKB94mZ4YYabZ3amJ0dTNjvwHT3Vio89bFnOl3tEDyL+RbcUElKdNWB OzVPCjnruizs67WcwUBMf1rc3LqS5DgrT/MRYVl3ot11Gey/In8W4qRSFMTqvoGeyUJC /RTdSSdcirjxK/sUKxfjRBmuVkD3ZPzWgh4/eWenCv0tudqhg8f+SJ+62AUNUBcBMkQz suwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757598632; x=1758203432; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+V+AWXb8up2Msrqiaf69upU8kliovcRX1LdolHvEDfU=; b=HKEeFgjXnexIRZpjFVmNPYbwkxPjie5TDOTKQi0JUVf2a13H/Z/UrCpt1kN3t4TqDr 5DzPcCNx7ut4OaUuAYGDRnY8N20rdAXPQJLptjaa+34YTJYsjMHIOgdvQ52lUQmeZ5YC bGhWSsgOaNylXS29FuLwTC0lfWHagCpkGWQ9U0XsV8zuI3RJ8mDtHSc6iEsCX/6IqQG7 ANCeax9panvDRIDEA1PBPDjV0P4U5u7sk7TXNZmQerXPdUo6OypHT3WslxC4LGzKeCa0 Ro7fXRcK9EBuL7SHIMuEPi22DOiKSpZNgRfNTEckUNMlusbFvGEyeYJuJFRxP0nTEJTc eeng== X-Gm-Message-State: AOJu0YwX1+2DL75iqAGdguXqpyYylzAH6wtW6q1YGb5OjAzX+zRU/KpO X+hxOovS4drztNe8ixhlPBhQm2Hbyj0syqJ5gdQpMqwsC4d08PQ6zFk/rA27fM3tN6+qCDQAhFg hM41h6CHphQ== X-Gm-Gg: ASbGncsLOq6Q6Ah4D7qcsNNZiVs3CIpiwRvbyGeiGMZB5WZTN50ldxts/P1as5A8hpd llzOXH0q4qapqKxI+8DC4CSchNljlgxezg1KK9nYrfB3wxVFXgud1yDuUHS9gF7OIGbfohr4XSV M6f+aWgkEc2x/4UHBoGfarlVnHQ6CKJ2OdpA1jo+I/6DSGSe735VdFH88TNcSMnImhcYqCDnW1P MDbybthn7foGgjtab2DU7fZJb7lo2TbGiQ4zQB49+LfEjLMvuuSVc70Jzc1fpY8rI3ci6K4Q8hd zJJLuMR3sRkhxjMU0svDz7GNZmDe5gZ3fSbfLuHMSXJ91lhuK/AKTjRGcujzdy4ZXydk4LAyj32 E8y32SQvCxvxtZiR+TCjO1cNRA7A1wXmbgjiAcs8pS4j99NoeRI/9CMJX8H4WQEEfm2gXmXkS7L s= X-Google-Smtp-Source: AGHT+IGcqSHifGRBLTF86nlgV9mYIf36Slk57ckeYssAkakoMK+ud/qiZfTXzzanKip8RAnWRMKS6g== X-Received: by 2002:a05:690c:6c11:b0:724:b868:d543 with SMTP id 00721157ae682-727f5938a8fmr221677097b3.41.1757598631603; Thu, 11 Sep 2025 06:50:31 -0700 (PDT) Received: from rayden.urgonet (h-37-123-177-177.A175.priv.bahnhof.se. [37.123.177.177]) by smtp.gmail.com with ESMTPSA id 00721157ae682-72f7623434csm3526257b3.11.2025.09.11.06.50.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Sep 2025 06:50:29 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, op-tee@lists.trustedfirmware.org, linux-arm-kernel@lists.infradead.org Cc: Olivier Masse , Thierry Reding , Yong Wu , Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T . J . Mercier" , =?UTF-8?q?Christian=20K=C3=B6nig?= , Sumit Garg , Matthias Brugger , AngeloGioacchino Del Regno , azarrabi@qti.qualcomm.com, Simona Vetter , Daniel Stone , Rouven Czerwinski , robin.murphy@arm.com, Jens Wiklander , Sumit Garg Subject: [PATCH v12 3/9] tee: implement protected DMA-heap Date: Thu, 11 Sep 2025 15:49:44 +0200 Message-ID: <20250911135007.1275833-4-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250911135007.1275833-1-jens.wiklander@linaro.org> References: <20250911135007.1275833-1-jens.wiklander@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Implement DMA heap for protected DMA-buf allocation in the TEE subsystem. Protected memory refers to memory buffers behind a hardware enforced firewall. It is not accessible to the kernel during normal circumstances but rather only accessible to certain hardware IPs or CPUs executing in higher or differently privileged mode than the kernel itself. This interface allows to allocate and manage such protected memory buffers via interaction with a TEE implementation. The protected memory is allocated for a specific use-case, like Secure Video Playback, Trusted UI, or Secure Video Recording where certain hardware devices can access the memory. The DMA-heaps are enabled explicitly by the TEE backend driver. The TEE backend drivers needs to implement protected memory pool to manage the protected memory. Reviewed-by: Sumit Garg Signed-off-by: Jens Wiklander --- drivers/tee/Kconfig | 5 + drivers/tee/Makefile | 1 + drivers/tee/tee_core.c | 4 + drivers/tee/tee_heap.c | 500 ++++++++++++++++++++++++++++++++++++++ drivers/tee/tee_private.h | 6 + include/linux/tee_core.h | 53 ++++ 6 files changed, 569 insertions(+) create mode 100644 drivers/tee/tee_heap.c diff --git a/drivers/tee/Kconfig b/drivers/tee/Kconfig index 61b507c18780..90600607a9d8 100644 --- a/drivers/tee/Kconfig +++ b/drivers/tee/Kconfig @@ -13,6 +13,11 @@ menuconfig TEE =20 if TEE =20 +config TEE_DMABUF_HEAPS + bool + depends on HAS_DMA && DMABUF_HEAPS + default y + source "drivers/tee/optee/Kconfig" source "drivers/tee/amdtee/Kconfig" source "drivers/tee/tstee/Kconfig" diff --git a/drivers/tee/Makefile b/drivers/tee/Makefile index 5488cba30bd2..949a6a79fb06 100644 --- a/drivers/tee/Makefile +++ b/drivers/tee/Makefile @@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_TEE) +=3D tee.o tee-objs +=3D tee_core.o +tee-objs +=3D tee_heap.o tee-objs +=3D tee_shm.o tee-objs +=3D tee_shm_pool.o obj-$(CONFIG_OPTEE) +=3D optee/ diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c index acc7998758ad..2411d1e2aa7a 100644 --- a/drivers/tee/tee_core.c +++ b/drivers/tee/tee_core.c @@ -1064,6 +1064,8 @@ void tee_device_unregister(struct tee_device *teedev) if (!teedev) return; =20 + tee_device_put_all_dma_heaps(teedev); + if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED) cdev_device_del(&teedev->cdev, &teedev->dev); =20 @@ -1287,3 +1289,5 @@ MODULE_AUTHOR("Linaro"); MODULE_DESCRIPTION("TEE Driver"); MODULE_VERSION("1.0"); MODULE_LICENSE("GPL v2"); +MODULE_IMPORT_NS("DMA_BUF"); +MODULE_IMPORT_NS("DMA_BUF_HEAP"); diff --git a/drivers/tee/tee_heap.c b/drivers/tee/tee_heap.c new file mode 100644 index 000000000000..d8d7735cdffb --- /dev/null +++ b/drivers/tee/tee_heap.c @@ -0,0 +1,500 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2025, Linaro Limited + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "tee_private.h" + +struct tee_dma_heap { + struct dma_heap *heap; + enum tee_dma_heap_id id; + struct kref kref; + struct tee_protmem_pool *pool; + struct tee_device *teedev; + bool shutting_down; + /* Protects pool, teedev, and shutting_down above */ + struct mutex mu; +}; + +struct tee_heap_buffer { + struct tee_dma_heap *heap; + size_t size; + size_t offs; + struct sg_table table; +}; + +struct tee_heap_attachment { + struct sg_table table; + struct device *dev; +}; + +struct tee_protmem_static_pool { + struct tee_protmem_pool pool; + struct gen_pool *gen_pool; + phys_addr_t pa_base; +}; + +#if IS_ENABLED(CONFIG_TEE_DMABUF_HEAPS) +static DEFINE_XARRAY_ALLOC(tee_dma_heap); + +static void tee_heap_release(struct kref *kref) +{ + struct tee_dma_heap *h =3D container_of(kref, struct tee_dma_heap, kref); + + h->pool->ops->destroy_pool(h->pool); + tee_device_put(h->teedev); + h->pool =3D NULL; + h->teedev =3D NULL; +} + +static void put_tee_heap(struct tee_dma_heap *h) +{ + kref_put(&h->kref, tee_heap_release); +} + +static void get_tee_heap(struct tee_dma_heap *h) +{ + kref_get(&h->kref); +} + +static int copy_sg_table(struct sg_table *dst, struct sg_table *src) +{ + struct scatterlist *dst_sg; + struct scatterlist *src_sg; + int ret; + int i; + + ret =3D sg_alloc_table(dst, src->orig_nents, GFP_KERNEL); + if (ret) + return ret; + + dst_sg =3D dst->sgl; + for_each_sgtable_sg(src, src_sg, i) { + sg_set_page(dst_sg, sg_page(src_sg), src_sg->length, + src_sg->offset); + dst_sg =3D sg_next(dst_sg); + } + + return 0; +} + +static int tee_heap_attach(struct dma_buf *dmabuf, + struct dma_buf_attachment *attachment) +{ + struct tee_heap_buffer *buf =3D dmabuf->priv; + struct tee_heap_attachment *a; + int ret; + + a =3D kzalloc(sizeof(*a), GFP_KERNEL); + if (!a) + return -ENOMEM; + + ret =3D copy_sg_table(&a->table, &buf->table); + if (ret) { + kfree(a); + return ret; + } + + a->dev =3D attachment->dev; + attachment->priv =3D a; + + return 0; +} + +static void tee_heap_detach(struct dma_buf *dmabuf, + struct dma_buf_attachment *attachment) +{ + struct tee_heap_attachment *a =3D attachment->priv; + + sg_free_table(&a->table); + kfree(a); +} + +static struct sg_table * +tee_heap_map_dma_buf(struct dma_buf_attachment *attachment, + enum dma_data_direction direction) +{ + struct tee_heap_attachment *a =3D attachment->priv; + int ret; + + ret =3D dma_map_sgtable(attachment->dev, &a->table, direction, + DMA_ATTR_SKIP_CPU_SYNC); + if (ret) + return ERR_PTR(ret); + + return &a->table; +} + +static void tee_heap_unmap_dma_buf(struct dma_buf_attachment *attachment, + struct sg_table *table, + enum dma_data_direction direction) +{ + struct tee_heap_attachment *a =3D attachment->priv; + + WARN_ON(&a->table !=3D table); + + dma_unmap_sgtable(attachment->dev, table, direction, + DMA_ATTR_SKIP_CPU_SYNC); +} + +static void tee_heap_buf_free(struct dma_buf *dmabuf) +{ + struct tee_heap_buffer *buf =3D dmabuf->priv; + + buf->heap->pool->ops->free(buf->heap->pool, &buf->table); + mutex_lock(&buf->heap->mu); + put_tee_heap(buf->heap); + mutex_unlock(&buf->heap->mu); + kfree(buf); +} + +static const struct dma_buf_ops tee_heap_buf_ops =3D { + .attach =3D tee_heap_attach, + .detach =3D tee_heap_detach, + .map_dma_buf =3D tee_heap_map_dma_buf, + .unmap_dma_buf =3D tee_heap_unmap_dma_buf, + .release =3D tee_heap_buf_free, +}; + +static struct dma_buf *tee_dma_heap_alloc(struct dma_heap *heap, + unsigned long len, u32 fd_flags, + u64 heap_flags) +{ + struct tee_dma_heap *h =3D dma_heap_get_drvdata(heap); + DEFINE_DMA_BUF_EXPORT_INFO(exp_info); + struct tee_device *teedev =3D NULL; + struct tee_heap_buffer *buf; + struct tee_protmem_pool *pool; + struct dma_buf *dmabuf; + int rc; + + mutex_lock(&h->mu); + if (h->teedev) { + teedev =3D h->teedev; + pool =3D h->pool; + get_tee_heap(h); + } + mutex_unlock(&h->mu); + + if (!teedev) + return ERR_PTR(-EINVAL); + + buf =3D kzalloc(sizeof(*buf), GFP_KERNEL); + if (!buf) { + dmabuf =3D ERR_PTR(-ENOMEM); + goto err; + } + buf->size =3D len; + buf->heap =3D h; + + rc =3D pool->ops->alloc(pool, &buf->table, len, &buf->offs); + if (rc) { + dmabuf =3D ERR_PTR(rc); + goto err_kfree; + } + + exp_info.ops =3D &tee_heap_buf_ops; + exp_info.size =3D len; + exp_info.priv =3D buf; + exp_info.flags =3D fd_flags; + dmabuf =3D dma_buf_export(&exp_info); + if (IS_ERR(dmabuf)) + goto err_protmem_free; + + return dmabuf; + +err_protmem_free: + pool->ops->free(pool, &buf->table); +err_kfree: + kfree(buf); +err: + mutex_lock(&h->mu); + put_tee_heap(h); + mutex_unlock(&h->mu); + return dmabuf; +} + +static const struct dma_heap_ops tee_dma_heap_ops =3D { + .allocate =3D tee_dma_heap_alloc, +}; + +static const char *heap_id_2_name(enum tee_dma_heap_id id) +{ + switch (id) { + case TEE_DMA_HEAP_SECURE_VIDEO_PLAY: + return "protected,secure-video"; + case TEE_DMA_HEAP_TRUSTED_UI: + return "protected,trusted-ui"; + case TEE_DMA_HEAP_SECURE_VIDEO_RECORD: + return "protected,secure-video-record"; + default: + return NULL; + } +} + +static int alloc_dma_heap(struct tee_device *teedev, enum tee_dma_heap_id = id, + struct tee_protmem_pool *pool) +{ + struct dma_heap_export_info exp_info =3D { + .ops =3D &tee_dma_heap_ops, + .name =3D heap_id_2_name(id), + }; + struct tee_dma_heap *h; + int rc; + + if (!exp_info.name) + return -EINVAL; + + if (xa_reserve(&tee_dma_heap, id, GFP_KERNEL)) { + if (!xa_load(&tee_dma_heap, id)) + return -EEXIST; + return -ENOMEM; + } + + h =3D kzalloc(sizeof(*h), GFP_KERNEL); + if (!h) + return -ENOMEM; + h->id =3D id; + kref_init(&h->kref); + h->teedev =3D teedev; + h->pool =3D pool; + mutex_init(&h->mu); + + exp_info.priv =3D h; + h->heap =3D dma_heap_add(&exp_info); + if (IS_ERR(h->heap)) { + rc =3D PTR_ERR(h->heap); + kfree(h); + + return rc; + } + + /* "can't fail" due to the call to xa_reserve() above */ + return WARN_ON(xa_is_err(xa_store(&tee_dma_heap, id, h, GFP_KERNEL))); +} + +int tee_device_register_dma_heap(struct tee_device *teedev, + enum tee_dma_heap_id id, + struct tee_protmem_pool *pool) +{ + struct tee_dma_heap *h; + int rc; + + if (!tee_device_get(teedev)) + return -EINVAL; + + h =3D xa_load(&tee_dma_heap, id); + if (h) { + mutex_lock(&h->mu); + if (h->teedev) { + rc =3D -EBUSY; + } else { + kref_init(&h->kref); + h->shutting_down =3D false; + h->teedev =3D teedev; + h->pool =3D pool; + rc =3D 0; + } + mutex_unlock(&h->mu); + } else { + rc =3D alloc_dma_heap(teedev, id, pool); + } + + if (rc) { + tee_device_put(teedev); + dev_err(&teedev->dev, "can't register DMA heap id %d (%s)\n", + id, heap_id_2_name(id)); + } + + return rc; +} +EXPORT_SYMBOL_GPL(tee_device_register_dma_heap); + +void tee_device_put_all_dma_heaps(struct tee_device *teedev) +{ + struct tee_dma_heap *h; + u_long i; + + xa_for_each(&tee_dma_heap, i, h) { + if (h) { + mutex_lock(&h->mu); + if (h->teedev =3D=3D teedev && !h->shutting_down) { + h->shutting_down =3D true; + put_tee_heap(h); + } + mutex_unlock(&h->mu); + } + } +} +EXPORT_SYMBOL_GPL(tee_device_put_all_dma_heaps); + +int tee_heap_update_from_dma_buf(struct tee_device *teedev, + struct dma_buf *dmabuf, size_t *offset, + struct tee_shm *shm, + struct tee_shm **parent_shm) +{ + struct tee_heap_buffer *buf; + int rc; + + /* The DMA-buf must be from our heap */ + if (dmabuf->ops !=3D &tee_heap_buf_ops) + return -EINVAL; + + buf =3D dmabuf->priv; + /* The buffer must be from the same teedev */ + if (buf->heap->teedev !=3D teedev) + return -EINVAL; + + shm->size =3D buf->size; + + rc =3D buf->heap->pool->ops->update_shm(buf->heap->pool, &buf->table, + buf->offs, shm, parent_shm); + if (!rc && *parent_shm) + *offset =3D buf->offs; + + return rc; +} +#else +int tee_device_register_dma_heap(struct tee_device *teedev __always_unused, + enum tee_dma_heap_id id __always_unused, + struct tee_protmem_pool *pool __always_unused) +{ + return -EINVAL; +} +EXPORT_SYMBOL_GPL(tee_device_register_dma_heap); + +void +tee_device_put_all_dma_heaps(struct tee_device *teedev __always_unused) +{ +} +EXPORT_SYMBOL_GPL(tee_device_put_all_dma_heaps); + +int tee_heap_update_from_dma_buf(struct tee_device *teedev __always_unused, + struct dma_buf *dmabuf __always_unused, + size_t *offset __always_unused, + struct tee_shm *shm __always_unused, + struct tee_shm **parent_shm __always_unused) +{ + return -EINVAL; +} +#endif + +static struct tee_protmem_static_pool * +to_protmem_static_pool(struct tee_protmem_pool *pool) +{ + return container_of(pool, struct tee_protmem_static_pool, pool); +} + +static int protmem_pool_op_static_alloc(struct tee_protmem_pool *pool, + struct sg_table *sgt, size_t size, + size_t *offs) +{ + struct tee_protmem_static_pool *stp =3D to_protmem_static_pool(pool); + phys_addr_t pa; + int ret; + + pa =3D gen_pool_alloc(stp->gen_pool, size); + if (!pa) + return -ENOMEM; + + ret =3D sg_alloc_table(sgt, 1, GFP_KERNEL); + if (ret) { + gen_pool_free(stp->gen_pool, pa, size); + return ret; + } + + sg_set_page(sgt->sgl, phys_to_page(pa), size, 0); + *offs =3D pa - stp->pa_base; + + return 0; +} + +static void protmem_pool_op_static_free(struct tee_protmem_pool *pool, + struct sg_table *sgt) +{ + struct tee_protmem_static_pool *stp =3D to_protmem_static_pool(pool); + struct scatterlist *sg; + int i; + + for_each_sgtable_sg(sgt, sg, i) + gen_pool_free(stp->gen_pool, sg_phys(sg), sg->length); + sg_free_table(sgt); +} + +static int protmem_pool_op_static_update_shm(struct tee_protmem_pool *pool, + struct sg_table *sgt, size_t offs, + struct tee_shm *shm, + struct tee_shm **parent_shm) +{ + struct tee_protmem_static_pool *stp =3D to_protmem_static_pool(pool); + + shm->paddr =3D stp->pa_base + offs; + *parent_shm =3D NULL; + + return 0; +} + +static void protmem_pool_op_static_destroy_pool(struct tee_protmem_pool *p= ool) +{ + struct tee_protmem_static_pool *stp =3D to_protmem_static_pool(pool); + + gen_pool_destroy(stp->gen_pool); + kfree(stp); +} + +static struct tee_protmem_pool_ops protmem_pool_ops_static =3D { + .alloc =3D protmem_pool_op_static_alloc, + .free =3D protmem_pool_op_static_free, + .update_shm =3D protmem_pool_op_static_update_shm, + .destroy_pool =3D protmem_pool_op_static_destroy_pool, +}; + +struct tee_protmem_pool *tee_protmem_static_pool_alloc(phys_addr_t paddr, + size_t size) +{ + const size_t page_mask =3D PAGE_SIZE - 1; + struct tee_protmem_static_pool *stp; + int rc; + + /* Check it's page aligned */ + if ((paddr | size) & page_mask) + return ERR_PTR(-EINVAL); + + if (!pfn_valid(PHYS_PFN(paddr))) + return ERR_PTR(-EINVAL); + + stp =3D kzalloc(sizeof(*stp), GFP_KERNEL); + if (!stp) + return ERR_PTR(-ENOMEM); + + stp->gen_pool =3D gen_pool_create(PAGE_SHIFT, -1); + if (!stp->gen_pool) { + rc =3D -ENOMEM; + goto err_free; + } + + rc =3D gen_pool_add(stp->gen_pool, paddr, size, -1); + if (rc) + goto err_free_pool; + + stp->pool.ops =3D &protmem_pool_ops_static; + stp->pa_base =3D paddr; + return &stp->pool; + +err_free_pool: + gen_pool_destroy(stp->gen_pool); +err_free: + kfree(stp); + + return ERR_PTR(rc); +} +EXPORT_SYMBOL_GPL(tee_protmem_static_pool_alloc); diff --git a/drivers/tee/tee_private.h b/drivers/tee/tee_private.h index 9bc50605227c..6c6ff5d5eed2 100644 --- a/drivers/tee/tee_private.h +++ b/drivers/tee/tee_private.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -24,4 +25,9 @@ struct tee_shm *tee_shm_alloc_user_buf(struct tee_context= *ctx, size_t size); struct tee_shm *tee_shm_register_user_buf(struct tee_context *ctx, unsigned long addr, size_t length); =20 +int tee_heap_update_from_dma_buf(struct tee_device *teedev, + struct dma_buf *dmabuf, size_t *offset, + struct tee_shm *shm, + struct tee_shm **parent_shm); + #endif /*TEE_PRIVATE_H*/ diff --git a/include/linux/tee_core.h b/include/linux/tee_core.h index a38494d6b5f4..28b65010b9ed 100644 --- a/include/linux/tee_core.h +++ b/include/linux/tee_core.h @@ -8,9 +8,11 @@ =20 #include #include +#include #include #include #include +#include #include #include #include @@ -30,6 +32,12 @@ #define TEE_DEVICE_FLAG_REGISTERED 0x1 #define TEE_MAX_DEV_NAME_LEN 32 =20 +enum tee_dma_heap_id { + TEE_DMA_HEAP_SECURE_VIDEO_PLAY =3D 1, + TEE_DMA_HEAP_TRUSTED_UI, + TEE_DMA_HEAP_SECURE_VIDEO_RECORD, +}; + /** * struct tee_device - TEE Device representation * @name: name of device @@ -116,6 +124,36 @@ struct tee_desc { u32 flags; }; =20 +/** + * struct tee_protmem_pool - protected memory pool + * @ops: operations + * + * This is an abstract interface where this struct is expected to be + * embedded in another struct specific to the implementation. + */ +struct tee_protmem_pool { + const struct tee_protmem_pool_ops *ops; +}; + +/** + * struct tee_protmem_pool_ops - protected memory pool operations + * @alloc: called when allocating protected memory + * @free: called when freeing protected memory + * @update_shm: called when registering a dma-buf to update the @shm + * with physical address of the buffer or to return the + * @parent_shm of the memory pool + * @destroy_pool: called when destroying the pool + */ +struct tee_protmem_pool_ops { + int (*alloc)(struct tee_protmem_pool *pool, struct sg_table *sgt, + size_t size, size_t *offs); + void (*free)(struct tee_protmem_pool *pool, struct sg_table *sgt); + int (*update_shm)(struct tee_protmem_pool *pool, struct sg_table *sgt, + size_t offs, struct tee_shm *shm, + struct tee_shm **parent_shm); + void (*destroy_pool)(struct tee_protmem_pool *pool); +}; + /** * tee_device_alloc() - Allocate a new struct tee_device instance * @teedesc: Descriptor for this driver @@ -154,6 +192,11 @@ int tee_device_register(struct tee_device *teedev); */ void tee_device_unregister(struct tee_device *teedev); =20 +int tee_device_register_dma_heap(struct tee_device *teedev, + enum tee_dma_heap_id id, + struct tee_protmem_pool *pool); +void tee_device_put_all_dma_heaps(struct tee_device *teedev); + /** * tee_device_set_dev_groups() - Set device attribute groups * @teedev: Device to register @@ -229,6 +272,16 @@ static inline void tee_shm_pool_free(struct tee_shm_po= ol *pool) pool->ops->destroy_pool(pool); } =20 +/** + * tee_protmem_static_pool_alloc() - Create a protected memory manager + * @paddr: Physical address of start of pool + * @size: Size in bytes of the pool + * + * @returns pointer to a 'struct tee_protmem_pool' or an ERR_PTR on failur= e. + */ +struct tee_protmem_pool *tee_protmem_static_pool_alloc(phys_addr_t paddr, + size_t size); + /** * tee_get_drvdata() - Return driver_data pointer * @returns the driver_data pointer supplied to tee_register(). --=20 2.43.0 From nobody Thu Oct 2 20:23:05 2025 Received: from mail-yw1-f171.google.com (mail-yw1-f171.google.com [209.85.128.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4450632ED58 for ; Thu, 11 Sep 2025 13:50:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757598640; cv=none; b=Na2p824cJGkwsjB4iixr5ND4f05LV8O+yEpsrT3TvooN53FSEKAyKhVT5++PtkFWS3UEStNv4s5Uh1Xrjdhr7z6UnYdIjKpxIF1AGbGkeVBKgY5RVro6RHaYOWJJTxm1Bfan3RVwb18YEmmCV2jk3HVExJoHU6piZeh0vfSlTUc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757598640; c=relaxed/simple; bh=k5Jww2JK2ae51rsOpo5VdeRKiDm+DOy3GENCcdCVfVg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CoccHQZofbdyN/KgkQXJQOh1l0p6BawFkzs82BRzQWWopAnkvwxDjlBFaG3Eh2g18ZDeq3/vJDA3o5fs94BNJV+GysEBD+r+HC5UFC90Mqo8wLvqMA9fXQqW2GcSjS2VyyVM+Pzxs76mkzDXyDVaHvq2H3Q0fHa5vhR5FozSLUo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=klE5AKnw; arc=none smtp.client-ip=209.85.128.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="klE5AKnw" Received: by mail-yw1-f171.google.com with SMTP id 00721157ae682-71d60504788so5304027b3.2 for ; Thu, 11 Sep 2025 06:50:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1757598637; x=1758203437; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=yi0r5nq64oj31QVOjln422OWaHa6DWnYy7B2T1z3rUg=; b=klE5AKnwrX8+utdXHXiA/AkI7a/T/OWsh9BQxvyR5kL6lAQOp5BNU6+zSmm1XyYNpm YVuEMTmHTdLAy1xx1lWKCL8Emjs672WyPHsKIjaVckQTGb+9eJvloi/KboUIZwGmDv8z Dgajkj461QdJ6l7AepJR5n99eAFv5qWDnt23t9Fm8U/W/sxqWN1ks0Ug5QczO/VM6yFm ckOryi6LrS9GR7eeY6OSai2Ogj7Abawszlyeg5afPu41CPgsdXsUDV0vp/q7P5HfqVv2 Y8qXV27knPGN76S0KQUBvlCG3yaG/nmngN8G/sX28eOOM18lEj28hRSm1/FeHh9mcikq tSvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757598637; x=1758203437; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yi0r5nq64oj31QVOjln422OWaHa6DWnYy7B2T1z3rUg=; b=YgcPs8U18ylpeZ1fWUnH6+Nrek4IOwQiBFmlx+EVv5Vm1nmGUExWe5ILwBf0mOhwOc 6hqYvqMd6LF2rD5n3O4PC+IRnwFCbyaYZ4ys5SxlTY3PEq8O+tdYpVFH19siaQvIIPdH DOHMFxPUmKcBUlVsfMw9OJzzX4ag/qlxYqXl7kvSGafM2LVxVO+ryloC6AyrVA6lTNxp GmlkD9bPYn3TpxluOqU1RjLJ81URouxgzZ71MEMmwN1w+tpLxCuDuH/fpOXN50wfOMKM 0ZUIDOWvJP4GEDM+aYibCNPFmYy6XUJina1lRgc/szyEYbo/4GcnLr8sK6a+B1yMNcrS i4ig== X-Gm-Message-State: AOJu0Ywdjq6kWji6Kf8KN8aRjJYDVf1ssMRApXvKOY6mfS/7JfF//JXa xZAP+y57Njek0JPVbfLDGHaJ3Zl5ScNFw6QlfB9t9PHVSZ5iO3/KVQmTuku3ewoSEKfufKxXs2z s6uPnSkXavQ== X-Gm-Gg: ASbGncvZw/momlTL0DrltRSjktlY3taAP6l4+KNG0d6/aNCpQlfm/APkT88v+m8gXvo fsYRBGvA11BtDjU30ZByV2jkaHeFLU118h1Rpip4Kv8i9EbwtZfkv9nJTdozKRSGfLqJlTz5zLg ELF24WX0S/Ivv0R8FHw31G6sk5edPh177L/oAr9JoIN6ehWxKHPYN95u9JHmeIN2pNCW3Rhv2lt N3DsjRjkMy01ZuAz6nYrvwmKLkEeToJGX8C9Rb1BsdKG+OrOmOAzrixTHy6rtXz67eoss2A1oOe 6KZAmpGCmGSMzF3GWeSbfhtedP2rHYFNJRkDpiN3Kw8zFT8/ZUw18I0K4nZ9nqIP1MuNVEfITso be/Krpe9u2XHZW3wdxy+VxG+wNO/+2Jpn8ltR4amYu6u0ETun3EGACehqM4mGewVj+oFbF84p0Y 7yAbZgx4ERmA== X-Google-Smtp-Source: AGHT+IFhFq5K/L+li5E/fnG6u0IEPzqvQNG67ywCbR49sqE1DlYxZ0kS6UjsMtnPo2ezjd7bDqomGA== X-Received: by 2002:a05:690c:62c1:b0:726:76f0:4b89 with SMTP id 00721157ae682-727f505434fmr181457897b3.22.1757598636586; Thu, 11 Sep 2025 06:50:36 -0700 (PDT) Received: from rayden.urgonet (h-37-123-177-177.A175.priv.bahnhof.se. [37.123.177.177]) by smtp.gmail.com with ESMTPSA id 00721157ae682-72f7623434csm3526257b3.11.2025.09.11.06.50.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Sep 2025 06:50:35 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, op-tee@lists.trustedfirmware.org, linux-arm-kernel@lists.infradead.org Cc: Olivier Masse , Thierry Reding , Yong Wu , Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T . J . Mercier" , =?UTF-8?q?Christian=20K=C3=B6nig?= , Sumit Garg , Matthias Brugger , AngeloGioacchino Del Regno , azarrabi@qti.qualcomm.com, Simona Vetter , Daniel Stone , Rouven Czerwinski , robin.murphy@arm.com, Jens Wiklander , Sumit Garg Subject: [PATCH v12 4/9] tee: refactor params_from_user() Date: Thu, 11 Sep 2025 15:49:45 +0200 Message-ID: <20250911135007.1275833-5-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250911135007.1275833-1-jens.wiklander@linaro.org> References: <20250911135007.1275833-1-jens.wiklander@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Break out the memref handling into a separate helper function. No change in behavior. Reviewed-by: Sumit Garg Signed-off-by: Jens Wiklander --- drivers/tee/tee_core.c | 94 ++++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 40 deletions(-) diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c index 2411d1e2aa7a..4996f194cd9e 100644 --- a/drivers/tee/tee_core.c +++ b/drivers/tee/tee_core.c @@ -354,6 +354,55 @@ tee_ioctl_shm_register(struct tee_context *ctx, return ret; } =20 +static int param_from_user_memref(struct tee_context *ctx, + struct tee_param_memref *memref, + struct tee_ioctl_param *ip) +{ + struct tee_shm *shm; + + /* + * If a NULL pointer is passed to a TA in the TEE, + * the ip.c IOCTL parameters is set to TEE_MEMREF_NULL + * indicating a NULL memory reference. + */ + if (ip->c !=3D TEE_MEMREF_NULL) { + /* + * If we fail to get a pointer to a shared + * memory object (and increase the ref count) + * from an identifier we return an error. All + * pointers that has been added in params have + * an increased ref count. It's the callers + * responibility to do tee_shm_put() on all + * resolved pointers. + */ + shm =3D tee_shm_get_from_id(ctx, ip->c); + if (IS_ERR(shm)) + return PTR_ERR(shm); + + /* + * Ensure offset + size does not overflow + * offset and does not overflow the size of + * the referred shared memory object. + */ + if ((ip->a + ip->b) < ip->a || + (ip->a + ip->b) > shm->size) { + tee_shm_put(shm); + return -EINVAL; + } + } else if (ctx->cap_memref_null) { + /* Pass NULL pointer to OP-TEE */ + shm =3D NULL; + } else { + return -EINVAL; + } + + memref->shm_offs =3D ip->a; + memref->size =3D ip->b; + memref->shm =3D shm; + + return 0; +} + static int params_from_user(struct tee_context *ctx, struct tee_param *par= ams, size_t num_params, struct tee_ioctl_param __user *uparams) @@ -361,8 +410,8 @@ static int params_from_user(struct tee_context *ctx, st= ruct tee_param *params, size_t n; =20 for (n =3D 0; n < num_params; n++) { - struct tee_shm *shm; struct tee_ioctl_param ip; + int rc; =20 if (copy_from_user(&ip, uparams + n, sizeof(ip))) return -EFAULT; @@ -385,45 +434,10 @@ static int params_from_user(struct tee_context *ctx, = struct tee_param *params, case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT: - /* - * If a NULL pointer is passed to a TA in the TEE, - * the ip.c IOCTL parameters is set to TEE_MEMREF_NULL - * indicating a NULL memory reference. - */ - if (ip.c !=3D TEE_MEMREF_NULL) { - /* - * If we fail to get a pointer to a shared - * memory object (and increase the ref count) - * from an identifier we return an error. All - * pointers that has been added in params have - * an increased ref count. It's the callers - * responibility to do tee_shm_put() on all - * resolved pointers. - */ - shm =3D tee_shm_get_from_id(ctx, ip.c); - if (IS_ERR(shm)) - return PTR_ERR(shm); - - /* - * Ensure offset + size does not overflow - * offset and does not overflow the size of - * the referred shared memory object. - */ - if ((ip.a + ip.b) < ip.a || - (ip.a + ip.b) > shm->size) { - tee_shm_put(shm); - return -EINVAL; - } - } else if (ctx->cap_memref_null) { - /* Pass NULL pointer to OP-TEE */ - shm =3D NULL; - } else { - return -EINVAL; - } - - params[n].u.memref.shm_offs =3D ip.a; - params[n].u.memref.size =3D ip.b; - params[n].u.memref.shm =3D shm; + rc =3D param_from_user_memref(ctx, ¶ms[n].u.memref, + &ip); + if (rc) + return rc; break; default: /* Unknown attribute */ --=20 2.43.0 From nobody Thu Oct 2 20:23:05 2025 Received: from mail-yb1-f178.google.com (mail-yb1-f178.google.com [209.85.219.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AB3FA341674 for ; Thu, 11 Sep 2025 13:50:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757598646; cv=none; b=uvJLW8fNjfvmNTqyzRFH96xoGVg4sT0f7YDWAfETXyfoTd25YsRnSU19ak0SNvn3YzyPK06J6AFIDBTcFlLLEEoSTI45ros8aCqBnVndB8n8QddHi9JpibBiOuQKHQl1yrQoTLO9NNJFrsMallKHlRoUFmOJw0u9UVqv0ctt2bY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757598646; c=relaxed/simple; bh=8Z13IZBYFS+StugnRW4GvMZkOOjQCkhuo5FbJoYB+DM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TounVJsOttDnoPKLDD9H3NUYYIorMhKXi3c/boCNw0Co+H/wLGSlU5Qe+Rftow9fyPJiafShHMGNgOvHp6LvZd0+hYmrROCZ+D7E/Zt+yoieB01tJKUtD6QzVhqFLDDWxeFZHqJUrROmPUPY3TTKI0JVVFZJ2y6TanWv9kjZDWI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=Q+3VPwjM; arc=none smtp.client-ip=209.85.219.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="Q+3VPwjM" Received: by mail-yb1-f178.google.com with SMTP id 3f1490d57ef6-e9baa0310cbso704175276.2 for ; Thu, 11 Sep 2025 06:50:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1757598642; x=1758203442; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=X+FC88lAOdKXh3VCPeIVeUsYuG4OuiQSHmuwOGzFRZ8=; b=Q+3VPwjMm2mR6o9rYQqOJef5101otxuFXXBkxik+mo2fD3NU11hkz2UofcnKV98tk/ hzXRkWfd2CphHcucVfmO4LT+OZDsQZGJhip3Y1MpRRJgMJzIF+hzuSr/ZlV7RUXpTHdN NdmWO/MBV9Rzyk2u8iV6eMHKpdxhzoeaPRfUV2p2LESgu+yurxdNl9Zs0ZMgRQHtPVZh C25jNSvm5h3lSkjcxzGxgkhwcXxGqJXjH62EdBTl9iI+UZMxB45djolsMtl3/YWXxYnj aa3NGcF4DNB4NaDwBSswuKyZR3FT1h3BUMtqKLfsmp8ymxdoTwmVNtNqGbqah4JcYdkY T0DA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757598642; x=1758203442; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=X+FC88lAOdKXh3VCPeIVeUsYuG4OuiQSHmuwOGzFRZ8=; b=W48OPZDacFQw1r+SGPCnj/dzD1At8WM06frrw8q1VVWBs19nicxfZklf8IXb8hhBv/ +Lhm1HMVtAOKHEjFgePSJHU2WAsjGbjPKQ2ildGMmWmfu//0wmB00sG5uZ35g/VCNO2i ZErAuvp2UF0ANu89lfS4xfUGl0SBGpshMw/lD8/Q57j/91ubiBrj+xNmUJTxjt9MiD0n xUAv8FKTMGaC/ZebHdordToWPDbNvCUUk/JwRH7WZQun4TitwlAwuOgoyv+B9kjvzsYt Df4P8WcLknK95VqMn3tKiguy3FH0oZBq+QjVQn6oEVekX44JcEhBYN5vCn55DWKwHMPv PA9g== X-Gm-Message-State: AOJu0YzmtS2ZynBXSAyyIFD5JtodmXHptAKS24lyEFbrebGYgve1COEG pvqZVUgQTs+JfSNSOhdzj5lQBr1CF5kr9bIbUJcx3d/kVg8BM3MTB8GNaAiw3fKrS0KoO+enOv6 noUoD2ATUSQ== X-Gm-Gg: ASbGnctEdQeF7TPUvmE78+fFfPzdd8VbbEGFodIkzwUspv+DZbY92SP8C+MQ6dkTcH4 CPvOc3/yPwE7ic7lmybQw5SO5r+QvtEfFLTjCbSG/2U44DEBIynYYfEGDuJ+Ps3YJoEowHgfxMx g7KfWlBkkG0g26gr1zwQfgPE18WAkcm/o02lerBpW23hcCquFPD6xTBuUl3DGYfIy2avjd8qsnd 1wp5UBrsOEyUH88B9LHrbnW4wOEm/oI5vSxO1V4P7JllB62v+Fylh9XPFJvYAE2Bi0xc5/obONF cCkz6Ft/MNDg+8v8z4IteN7gnXdMHvEOqgomgBAh5Qu/iBiqIwmPGIqNYKu8ut9NRqIaKX1jQCw YRJXzxnHQFUODIynxGHivW2dMb9ghO6gzPw6Z/9YNvx4XkrtHNjHxZTRen8rNluNeanorBJPsJb Wuw9u9dXU7Og== X-Google-Smtp-Source: AGHT+IF7IsUABirUCx7wBc5wUFCsq52tfHeM1YIL+ogUEqr6eMKV7g8gEl6JNjmzrrCvHvRLkXGvgw== X-Received: by 2002:a05:690e:1587:20b0:5f3:316d:1cea with SMTP id 956f58d0204a3-610232fb2d5mr13516020d50.9.1757598641533; Thu, 11 Sep 2025 06:50:41 -0700 (PDT) Received: from rayden.urgonet (h-37-123-177-177.A175.priv.bahnhof.se. [37.123.177.177]) by smtp.gmail.com with ESMTPSA id 00721157ae682-72f7623434csm3526257b3.11.2025.09.11.06.50.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Sep 2025 06:50:40 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, op-tee@lists.trustedfirmware.org, linux-arm-kernel@lists.infradead.org Cc: Olivier Masse , Thierry Reding , Yong Wu , Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T . J . Mercier" , =?UTF-8?q?Christian=20K=C3=B6nig?= , Sumit Garg , Matthias Brugger , AngeloGioacchino Del Regno , azarrabi@qti.qualcomm.com, Simona Vetter , Daniel Stone , Rouven Czerwinski , robin.murphy@arm.com, Etienne Carriere , Sumit Garg , Jens Wiklander Subject: [PATCH v12 5/9] tee: new ioctl to a register tee_shm from a dmabuf file descriptor Date: Thu, 11 Sep 2025 15:49:46 +0200 Message-ID: <20250911135007.1275833-6-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250911135007.1275833-1-jens.wiklander@linaro.org> References: <20250911135007.1275833-1-jens.wiklander@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Etienne Carriere Add a userspace API to create a tee_shm object that refers to a dmabuf reference. Userspace registers the dmabuf file descriptor as in a tee_shm object. The registration is completed with a tee_shm returned file descriptor. Userspace is free to close the dmabuf file descriptor after it has been registered since all the resources are now held via the new tee_shm object. Closing the tee_shm file descriptor will eventually release all resources used by the tee_shm object when all references are released. The new IOCTL, TEE_IOC_SHM_REGISTER_FD, supports dmabuf references to physically contiguous memory buffers. Dmabuf references acquired from the TEE DMA-heap can be used as protected memory for Secure Video Path and such use cases. It depends on the TEE and the TEE driver if dmabuf references acquired by other means can be used. A new tee_shm flag is added to identify tee_shm objects built from a registered dmabuf, TEE_SHM_DMA_BUF. Signed-off-by: Etienne Carriere Signed-off-by: Olivier Masse Reviewed-by: Sumit Garg Signed-off-by: Jens Wiklander --- drivers/tee/tee_core.c | 62 +++++++++++++++++++++++++++++++- drivers/tee/tee_private.h | 8 +++++ drivers/tee/tee_shm.c | 74 +++++++++++++++++++++++++++++++++++++-- include/linux/tee_core.h | 1 + include/linux/tee_drv.h | 10 ++++++ include/uapi/linux/tee.h | 31 ++++++++++++++++ 6 files changed, 182 insertions(+), 4 deletions(-) diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c index 4996f194cd9e..cea4da3ea46f 100644 --- a/drivers/tee/tee_core.c +++ b/drivers/tee/tee_core.c @@ -354,11 +354,49 @@ tee_ioctl_shm_register(struct tee_context *ctx, return ret; } =20 +static int +tee_ioctl_shm_register_fd(struct tee_context *ctx, + struct tee_ioctl_shm_register_fd_data __user *udata) +{ + struct tee_ioctl_shm_register_fd_data data; + struct tee_shm *shm; + long ret; + + if (copy_from_user(&data, udata, sizeof(data))) + return -EFAULT; + + /* Currently no input flags are supported */ + if (data.flags) + return -EINVAL; + + shm =3D tee_shm_register_fd(ctx, data.fd); + if (IS_ERR(shm)) + return -EINVAL; + + data.id =3D shm->id; + data.flags =3D shm->flags; + data.size =3D shm->size; + + if (copy_to_user(udata, &data, sizeof(data))) + ret =3D -EFAULT; + else + ret =3D tee_shm_get_fd(shm); + + /* + * When user space closes the file descriptor the shared memory + * should be freed or if tee_shm_get_fd() failed then it will + * be freed immediately. + */ + tee_shm_put(shm); + return ret; +} + static int param_from_user_memref(struct tee_context *ctx, struct tee_param_memref *memref, struct tee_ioctl_param *ip) { struct tee_shm *shm; + size_t offs =3D 0; =20 /* * If a NULL pointer is passed to a TA in the TEE, @@ -389,6 +427,26 @@ static int param_from_user_memref(struct tee_context *= ctx, tee_shm_put(shm); return -EINVAL; } + + if (shm->flags & TEE_SHM_DMA_BUF) { + struct tee_shm_dmabuf_ref *ref; + + ref =3D container_of(shm, struct tee_shm_dmabuf_ref, shm); + if (ref->parent_shm) { + /* + * The shm already has one reference to + * ref->parent_shm so we are clear of 0. + * We're getting another reference since + * this shm will be used in the parameter + * list instead of the shm we got with + * tee_shm_get_from_id() above. + */ + refcount_inc(&ref->parent_shm->refcount); + tee_shm_put(shm); + shm =3D ref->parent_shm; + offs =3D ref->offset; + } + } } else if (ctx->cap_memref_null) { /* Pass NULL pointer to OP-TEE */ shm =3D NULL; @@ -396,7 +454,7 @@ static int param_from_user_memref(struct tee_context *c= tx, return -EINVAL; } =20 - memref->shm_offs =3D ip->a; + memref->shm_offs =3D ip->a + offs; memref->size =3D ip->b; memref->shm =3D shm; =20 @@ -842,6 +900,8 @@ static long tee_ioctl(struct file *filp, unsigned int c= md, unsigned long arg) return tee_ioctl_shm_alloc(ctx, uarg); case TEE_IOC_SHM_REGISTER: return tee_ioctl_shm_register(ctx, uarg); + case TEE_IOC_SHM_REGISTER_FD: + return tee_ioctl_shm_register_fd(ctx, uarg); case TEE_IOC_OPEN_SESSION: return tee_ioctl_open_session(ctx, uarg); case TEE_IOC_INVOKE: diff --git a/drivers/tee/tee_private.h b/drivers/tee/tee_private.h index 6c6ff5d5eed2..a9b5e4a6a8f7 100644 --- a/drivers/tee/tee_private.h +++ b/drivers/tee/tee_private.h @@ -13,6 +13,14 @@ #include #include =20 +/* extra references appended to shm object for registered shared memory */ +struct tee_shm_dmabuf_ref { + struct tee_shm shm; + size_t offset; + struct dma_buf *dmabuf; + struct tee_shm *parent_shm; +}; + int tee_shm_get_fd(struct tee_shm *shm); =20 bool tee_device_get(struct tee_device *teedev); diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c index daf6e5cfd59a..76195a398c89 100644 --- a/drivers/tee/tee_shm.c +++ b/drivers/tee/tee_shm.c @@ -4,6 +4,7 @@ */ #include #include +#include #include #include #include @@ -45,7 +46,15 @@ static void release_registered_pages(struct tee_shm *shm) =20 static void tee_shm_release(struct tee_device *teedev, struct tee_shm *shm) { - if (shm->flags & TEE_SHM_POOL) { + void *p =3D shm; + + if (shm->flags & TEE_SHM_DMA_BUF) { + struct tee_shm_dmabuf_ref *ref; + + ref =3D container_of(shm, struct tee_shm_dmabuf_ref, shm); + p =3D ref; + dma_buf_put(ref->dmabuf); + } else if (shm->flags & TEE_SHM_POOL) { teedev->pool->ops->free(teedev->pool, shm); } else if (shm->flags & TEE_SHM_DYNAMIC) { int rc =3D teedev->desc->ops->shm_unregister(shm->ctx, shm); @@ -59,7 +68,7 @@ static void tee_shm_release(struct tee_device *teedev, st= ruct tee_shm *shm) =20 teedev_ctx_put(shm->ctx); =20 - kfree(shm); + kfree(p); =20 tee_device_put(teedev); } @@ -169,7 +178,7 @@ struct tee_shm *tee_shm_alloc_user_buf(struct tee_conte= xt *ctx, size_t size) * tee_client_invoke_func(). The memory allocated is later freed with a * call to tee_shm_free(). * - * @returns a pointer to 'struct tee_shm' + * @returns a pointer to 'struct tee_shm' on success, and ERR_PTR on failu= re */ struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t s= ize) { @@ -179,6 +188,62 @@ struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_co= ntext *ctx, size_t size) } EXPORT_SYMBOL_GPL(tee_shm_alloc_kernel_buf); =20 +struct tee_shm *tee_shm_register_fd(struct tee_context *ctx, int fd) +{ + struct tee_shm_dmabuf_ref *ref; + int rc; + + if (!tee_device_get(ctx->teedev)) + return ERR_PTR(-EINVAL); + + teedev_ctx_get(ctx); + + ref =3D kzalloc(sizeof(*ref), GFP_KERNEL); + if (!ref) { + rc =3D -ENOMEM; + goto err_put_tee; + } + + refcount_set(&ref->shm.refcount, 1); + ref->shm.ctx =3D ctx; + ref->shm.id =3D -1; + ref->shm.flags =3D TEE_SHM_DMA_BUF; + + ref->dmabuf =3D dma_buf_get(fd); + if (IS_ERR(ref->dmabuf)) { + rc =3D PTR_ERR(ref->dmabuf); + goto err_kfree_ref; + } + + rc =3D tee_heap_update_from_dma_buf(ctx->teedev, ref->dmabuf, + &ref->offset, &ref->shm, + &ref->parent_shm); + if (rc) + goto err_put_dmabuf; + + mutex_lock(&ref->shm.ctx->teedev->mutex); + ref->shm.id =3D idr_alloc(&ref->shm.ctx->teedev->idr, &ref->shm, + 1, 0, GFP_KERNEL); + mutex_unlock(&ref->shm.ctx->teedev->mutex); + if (ref->shm.id < 0) { + rc =3D ref->shm.id; + goto err_put_dmabuf; + } + + return &ref->shm; + +err_put_dmabuf: + dma_buf_put(ref->dmabuf); +err_kfree_ref: + kfree(ref); +err_put_tee: + teedev_ctx_put(ctx); + tee_device_put(ctx->teedev); + + return ERR_PTR(rc); +} +EXPORT_SYMBOL_GPL(tee_shm_register_fd); + /** * tee_shm_alloc_priv_buf() - Allocate shared memory for a privately shared * kernel buffer @@ -442,6 +507,9 @@ static int tee_shm_fop_mmap(struct file *filp, struct v= m_area_struct *vma) /* Refuse sharing shared memory provided by application */ if (shm->flags & TEE_SHM_USER_MAPPED) return -EINVAL; + /* Refuse sharing registered DMA_bufs with the application */ + if (shm->flags & TEE_SHM_DMA_BUF) + return -EINVAL; =20 /* check for overflowing the buffer's size */ if (vma->vm_pgoff + vma_pages(vma) > shm->size >> PAGE_SHIFT) diff --git a/include/linux/tee_core.h b/include/linux/tee_core.h index 28b65010b9ed..b6c54b34a8b5 100644 --- a/include/linux/tee_core.h +++ b/include/linux/tee_core.h @@ -28,6 +28,7 @@ #define TEE_SHM_USER_MAPPED BIT(1) /* Memory mapped in user space */ #define TEE_SHM_POOL BIT(2) /* Memory allocated from pool */ #define TEE_SHM_PRIV BIT(3) /* Memory private to TEE driver */ +#define TEE_SHM_DMA_BUF BIT(4) /* Memory with dma-buf handle */ =20 #define TEE_DEVICE_FLAG_REGISTERED 0x1 #define TEE_MAX_DEV_NAME_LEN 32 diff --git a/include/linux/tee_drv.h b/include/linux/tee_drv.h index a54c203000ed..824f1251de60 100644 --- a/include/linux/tee_drv.h +++ b/include/linux/tee_drv.h @@ -116,6 +116,16 @@ struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_co= ntext *ctx, size_t size); struct tee_shm *tee_shm_register_kernel_buf(struct tee_context *ctx, void *addr, size_t length); =20 +/** + * tee_shm_register_fd() - Register shared memory from file descriptor + * + * @ctx: Context that allocates the shared memory + * @fd: Shared memory file descriptor reference + * + * @returns a pointer to 'struct tee_shm' on success, and ERR_PTR on failu= re + */ +struct tee_shm *tee_shm_register_fd(struct tee_context *ctx, int fd); + /** * tee_shm_free() - Free shared memory * @shm: Handle to shared memory to free diff --git a/include/uapi/linux/tee.h b/include/uapi/linux/tee.h index d0430bee8292..d843cf980d98 100644 --- a/include/uapi/linux/tee.h +++ b/include/uapi/linux/tee.h @@ -378,6 +378,37 @@ struct tee_ioctl_shm_register_data { __s32 id; }; =20 +/** + * struct tee_ioctl_shm_register_fd_data - Shared memory registering argum= ent + * @fd: [in] File descriptor identifying dmabuf reference + * @size: [out] Size of referenced memory + * @flags: [in] Flags to/from allocation. + * @id: [out] Identifier of the shared memory + * + * The flags field should currently be zero as input. Updated by the call + * with actual flags as defined by TEE_IOCTL_SHM_* above. + * This structure is used as argument for TEE_IOC_SHM_REGISTER_FD below. + */ +struct tee_ioctl_shm_register_fd_data { + __s64 fd; + __u64 size; + __u32 flags; + __s32 id; +}; + +/** + * TEE_IOC_SHM_REGISTER_FD - register a shared memory from a file descript= or + * + * Returns a file descriptor on success or < 0 on failure + * + * The returned file descriptor refers to the shared memory object in the + * kernel. The supplied file deccriptor can be closed if it's not needed + * for other purposes. The shared memory is freed when the descriptor is + * closed. + */ +#define TEE_IOC_SHM_REGISTER_FD _IOWR(TEE_IOC_MAGIC, TEE_IOC_BASE + 8, \ + struct tee_ioctl_shm_register_fd_data) + /** * TEE_IOC_SHM_REGISTER - Register shared memory argument * --=20 2.43.0 From nobody Thu Oct 2 20:23:05 2025 Received: from mail-yw1-f170.google.com (mail-yw1-f170.google.com [209.85.128.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7F4DD342CAC for ; Thu, 11 Sep 2025 13:50:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757598653; cv=none; b=ErE8HEG3ljw6ptz00AmyouvKGaVDfu/gnblKDoVCw5iwUwsPBNnZLAv44yXHFodTbYlX/aZ6LW6d7cjaPPi6oo/Oc747QhbRxM432MMZFxcFuAlSQP/rmD38a079jz7IDQ6KGROGJN/08b7VH1YuZmhgW7JValt/p8aO5jqVwRY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757598653; c=relaxed/simple; bh=ljVsBiUSoqP9nQ6E/AbsEzgi5WJ+mPSnwPoiSvNWF84=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Mb96n1DlX4LrXra5LAGopxE7wtDSYOv0uH6JU1Va0cVnQGbCXhmG1uFeXiZf+eQaBp+jhDZC+Ttwy6R6+Teuhg9dVu0se3SJOVOONHU1T3+Ib6txDs6SsEMkMSh2t8RZsj5wBYid5TArRHKe/YF8zT7mtVzNPi0awhh7yRPkIXU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=Oi8nvu2G; arc=none smtp.client-ip=209.85.128.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="Oi8nvu2G" Received: by mail-yw1-f170.google.com with SMTP id 00721157ae682-71d603a9cfaso5633097b3.1 for ; Thu, 11 Sep 2025 06:50:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1757598648; x=1758203448; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=r7AlbrbWF01PAGMb+n5yznNoBl+a3KG2eoPDEibc0TE=; b=Oi8nvu2GhqmYk3tturpJWEuSTV1rgaqOVoiOyNFmxxGtwMguvhTjYTRpjkLEzyjFk6 Bxb+dxYTN4gOgUyIj1y1KWDi9uBvSR6V7BnZGCAuIT8CDjOuzxtMIuRpJmoErsqEssQ1 gedDTUzS6zD6Rmh9gW3bjc7bkdZjoVcAyPodfmqwmvrnf8eTzmqv8jmxwi15WNqo6uBV 2g+Q2zSQARWr/Qt01Rtyi/OopVLolo1PBOv6YH+8RrRRNHebGr6WJHq/syQyUBtRnX1K zmE7k7N1uOy1I5ZCz7OPAteHOG4t/xP3Sym0+KrhyniALmJbi9xt5G6DRdvpnXIXj+6G TtWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757598648; x=1758203448; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=r7AlbrbWF01PAGMb+n5yznNoBl+a3KG2eoPDEibc0TE=; b=K0VDC3kA0u+WRxMNKkzge+EqbaFWrIudaJsQsqdywqwJpeZXVf1xH+QIuC5RoWPNjr x9FLWujPITbv1+B/mNBBmtgnz36yqdRDUecvLf6GcMnc78vlZjPcCa+b67MB5Uwh4L7b YRIP075J8AYhMYncoSMoLXHRkbj25pizXBCoGTMOHWUFlgn2ecJTsDZUyjbXb/yx8B9C VZv41IiYsVivjVP9pY5tnpc/byM7oiQH3tjwvNP5NNc59rTrK3mwR/K8dvVvPq8ky0Yq 0u0hZ0VZQ0vb8zqc4M1iE1Fjc0xN+ceRrd8fT47PH6ZLdnfLGj2F5TITpAJwkgJQDdzj z0Mw== X-Gm-Message-State: AOJu0YyiS7spp3RXTm1AuqVGJh6l20yUr4VzmwElycwrfWHsN26elAkX 6OVQ6uvAozovjHE7zfFYERY8tjhn4jGTwtr+h1Udyig+YLf0vGYa/036+qGgj5ZcfSPjJlmi5sQ cOWnFOBXboQ== X-Gm-Gg: ASbGnctyng6wZ9sJwOM+xNvAIYSgEElvUr6lq6Wln+8wr6GroQDekikjHbiowRYF2SH sXCtk5kgiWrLN3J3c+uUZGmAGpndX2n95XO1WJVigu1UfuVvXuXgjYDpXrWvh/h5yluKoL3wqSC QZCG3Z46TlGuH0GBEYm+OO0GueDprjczOUXMA0yPQnANJzh56LFs2eIp8L/1ytq3owCjpJHtWez kpTOqj2KTlW5pPv+TS8S82Y0Emcd4prnj2jki2G0fiUcdVctw2TMtzaH+73kLpjBCsfDU/gw4nV WJss1xvVrUcl0eT+RLKRgzDX5nouZH3wXlRBgZt/mCBFgFxqnOtBq904lD8bzj5d+35sV0tKkQ8 b1Zp7cPpNLX2TqvFPU01Yxf89DbslsOG3MSUntgoJCjJY16O6sUn2EN4Yam0tuZr8m+28qlRbdC 2ri7JRTOG5eg== X-Google-Smtp-Source: AGHT+IENGIldq5JqNWVYj879nv5c12Hu2gAcLXkQcT4am7WHRztg/K9ENUf9gRzRFcua+CJvdep3gQ== X-Received: by 2002:a05:690c:4d88:b0:724:2cad:8df6 with SMTP id 00721157ae682-727f2ebd695mr176268437b3.16.1757598647587; Thu, 11 Sep 2025 06:50:47 -0700 (PDT) Received: from rayden.urgonet (h-37-123-177-177.A175.priv.bahnhof.se. [37.123.177.177]) by smtp.gmail.com with ESMTPSA id 00721157ae682-72f7623434csm3526257b3.11.2025.09.11.06.50.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Sep 2025 06:50:45 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, op-tee@lists.trustedfirmware.org, linux-arm-kernel@lists.infradead.org Cc: Olivier Masse , Thierry Reding , Yong Wu , Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T . J . Mercier" , =?UTF-8?q?Christian=20K=C3=B6nig?= , Sumit Garg , Matthias Brugger , AngeloGioacchino Del Regno , azarrabi@qti.qualcomm.com, Simona Vetter , Daniel Stone , Rouven Czerwinski , robin.murphy@arm.com, Jens Wiklander , Sumit Garg Subject: [PATCH v12 6/9] tee: add tee_shm_alloc_dma_mem() Date: Thu, 11 Sep 2025 15:49:47 +0200 Message-ID: <20250911135007.1275833-7-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250911135007.1275833-1-jens.wiklander@linaro.org> References: <20250911135007.1275833-1-jens.wiklander@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add tee_shm_alloc_dma_mem() to allocate DMA memory. The memory is represented by a tee_shm object using the new flag TEE_SHM_DMA_MEM to identify it as DMA memory. The allocated memory will later be lent to the TEE to be used as protected memory. Reviewed-by: Sumit Garg Signed-off-by: Jens Wiklander --- drivers/tee/tee_shm.c | 85 +++++++++++++++++++++++++++++++++++++++- include/linux/tee_core.h | 5 +++ 2 files changed, 88 insertions(+), 2 deletions(-) diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c index 76195a398c89..e195c892431d 100644 --- a/drivers/tee/tee_shm.c +++ b/drivers/tee/tee_shm.c @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include #include #include @@ -13,9 +15,14 @@ #include #include #include -#include #include "tee_private.h" =20 +struct tee_shm_dma_mem { + struct tee_shm shm; + dma_addr_t dma_addr; + struct page *page; +}; + static void shm_put_kernel_pages(struct page **pages, size_t page_count) { size_t n; @@ -48,7 +55,16 @@ static void tee_shm_release(struct tee_device *teedev, s= truct tee_shm *shm) { void *p =3D shm; =20 - if (shm->flags & TEE_SHM_DMA_BUF) { + if (shm->flags & TEE_SHM_DMA_MEM) { +#if IS_ENABLED(CONFIG_TEE_DMABUF_HEAPS) + struct tee_shm_dma_mem *dma_mem; + + dma_mem =3D container_of(shm, struct tee_shm_dma_mem, shm); + p =3D dma_mem; + dma_free_pages(&teedev->dev, shm->size, dma_mem->page, + dma_mem->dma_addr, DMA_BIDIRECTIONAL); +#endif + } else if (shm->flags & TEE_SHM_DMA_BUF) { struct tee_shm_dmabuf_ref *ref; =20 ref =3D container_of(shm, struct tee_shm_dmabuf_ref, shm); @@ -268,6 +284,71 @@ struct tee_shm *tee_shm_alloc_priv_buf(struct tee_cont= ext *ctx, size_t size) } EXPORT_SYMBOL_GPL(tee_shm_alloc_priv_buf); =20 +#if IS_ENABLED(CONFIG_TEE_DMABUF_HEAPS) +/** + * tee_shm_alloc_dma_mem() - Allocate DMA memory as shared memory object + * @ctx: Context that allocates the shared memory + * @page_count: Number of pages + * + * The allocated memory is expected to be lent (made inaccessible to the + * kernel) to the TEE while it's used and returned (accessible to the + * kernel again) before it's freed. + * + * This function should normally only be used internally in the TEE + * drivers. + * + * @returns a pointer to 'struct tee_shm' + */ +struct tee_shm *tee_shm_alloc_dma_mem(struct tee_context *ctx, + size_t page_count) +{ + struct tee_device *teedev =3D ctx->teedev; + struct tee_shm_dma_mem *dma_mem; + dma_addr_t dma_addr; + struct page *page; + + if (!tee_device_get(teedev)) + return ERR_PTR(-EINVAL); + + page =3D dma_alloc_pages(&teedev->dev, page_count * PAGE_SIZE, + &dma_addr, DMA_BIDIRECTIONAL, GFP_KERNEL); + if (!page) + goto err_put_teedev; + + dma_mem =3D kzalloc(sizeof(*dma_mem), GFP_KERNEL); + if (!dma_mem) + goto err_free_pages; + + refcount_set(&dma_mem->shm.refcount, 1); + dma_mem->shm.ctx =3D ctx; + dma_mem->shm.paddr =3D page_to_phys(page); + dma_mem->dma_addr =3D dma_addr; + dma_mem->page =3D page; + dma_mem->shm.size =3D page_count * PAGE_SIZE; + dma_mem->shm.flags =3D TEE_SHM_DMA_MEM; + + teedev_ctx_get(ctx); + + return &dma_mem->shm; + +err_free_pages: + dma_free_pages(&teedev->dev, page_count * PAGE_SIZE, page, dma_addr, + DMA_BIDIRECTIONAL); +err_put_teedev: + tee_device_put(teedev); + + return ERR_PTR(-ENOMEM); +} +EXPORT_SYMBOL_GPL(tee_shm_alloc_dma_mem); +#else +struct tee_shm *tee_shm_alloc_dma_mem(struct tee_context *ctx, + size_t page_count) +{ + return ERR_PTR(-EINVAL); +} +EXPORT_SYMBOL_GPL(tee_shm_alloc_dma_mem); +#endif + int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t alig= n, int (*shm_register)(struct tee_context *ctx, struct tee_shm *shm, diff --git a/include/linux/tee_core.h b/include/linux/tee_core.h index b6c54b34a8b5..7b0c1da2ca6c 100644 --- a/include/linux/tee_core.h +++ b/include/linux/tee_core.h @@ -29,6 +29,8 @@ #define TEE_SHM_POOL BIT(2) /* Memory allocated from pool */ #define TEE_SHM_PRIV BIT(3) /* Memory private to TEE driver */ #define TEE_SHM_DMA_BUF BIT(4) /* Memory with dma-buf handle */ +#define TEE_SHM_DMA_MEM BIT(5) /* Memory allocated with */ + /* dma_alloc_pages() */ =20 #define TEE_DEVICE_FLAG_REGISTERED 0x1 #define TEE_MAX_DEV_NAME_LEN 32 @@ -298,6 +300,9 @@ void *tee_get_drvdata(struct tee_device *teedev); */ struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t siz= e); =20 +struct tee_shm *tee_shm_alloc_dma_mem(struct tee_context *ctx, + size_t page_count); + int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t alig= n, int (*shm_register)(struct tee_context *ctx, struct tee_shm *shm, --=20 2.43.0 From nobody Thu Oct 2 20:23:05 2025 Received: from mail-yw1-f169.google.com (mail-yw1-f169.google.com [209.85.128.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C54BC3431F2 for ; Thu, 11 Sep 2025 13:50:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757598657; cv=none; b=WQB2n36X1eK5oml2W9iNxqalE/aqYVLvzoDsemH+jZ5ERC+A2zW7FCuQafXe+k3goBI9P6MMnM2mMnvTByXN6jJQvNL+Mhz7Ig+t8DxGUoUN5tMYD0grq8tZZWabwHtewPxqeYhisIN9B28Uv3i+yllXk4zdJBdK9d3G0YCtvTY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757598657; c=relaxed/simple; bh=sQ/l2bP0mTeojthGX/AWBBFSxz1LKDRMF9Unjz3DfpY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Zt4xG9Gl1Pqxrourd66OxXW78bnz5os2x9BbqMI6VqXTQHHkeXUTVWEwdIQqL+raIK2xEfwN21AbJaAvbLlrm8Iez3stj/IUOJerpP3JAY48at9bCcTpdDqAxwR7xEeS9WKa9f3tFygLSdM+KKsAk4prcy7hI0PbM6dEpmLR0GY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=TUvJxuM4; arc=none smtp.client-ip=209.85.128.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="TUvJxuM4" Received: by mail-yw1-f169.google.com with SMTP id 00721157ae682-723bc91d7bbso6081317b3.3 for ; Thu, 11 Sep 2025 06:50:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1757598653; x=1758203453; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=vA8l0L1pnbhQj/VVM6tv+d6BLx7uJN5MQdkMqAYWy2o=; b=TUvJxuM4/SyQjYFPqJdMMx/QoYIFpgbU52MI0ksbmKIwwaGdY7HEWDhx3vbHcYOhj3 +ZgCId5Hm6rLjVS65X0/yqu07cdpSBZ4iCqG/g4HlY2qBnq5Lxoi4D2/0UDHV95/ax8e pJ9+P+UK+mxBXSZ7kXxntatcO8XonfJBUdEhrmk6gH4mbDUpZFVwPgJdCcyIcY3EVhj0 dc4ocCFHeO0v9yTdLO+s6IcpPaTI7jd2wLM6sQJRq57vOuSiMImcCXYX1XSDNsp2bCH0 ymcnp+qxZG7Vq/mFy4HvKX9rdKmzKjg9rX6t0I7xws4N5Pys+NRC+qgGRYmsDq/w/ib4 jPDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757598653; x=1758203453; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vA8l0L1pnbhQj/VVM6tv+d6BLx7uJN5MQdkMqAYWy2o=; b=hHSzmWYZF8Ee/JzZVWhz8AOWvwAEe3R362EsjSP+EX+1J2qg9ORCd14JlhnsEgXlRs NrP/gT6CT9Y9YkUl8wGOEIRdb24nY1u5XwE5iUA/qPrTG9eBV9TVXpAEFr1IGujHnUSq z40YBynuJDCCpGvmpb1xSKRoY8Eq6Pkr/H7xlnvVzOCDgOZouNb5B6dH2P+uf8NR4ofc e3BkHdY5o1HEYf7elm7Ng4i+3XHfM15z6rFzzxlpofr3v5TdXSNjbNco9GsYLslrzhFb Wl/cXAWU0J+FG6CLeEqlR70wmZgNZ75y6MgZiNN3OzwKwCtIEssgPUFWMHvCIKt3aOwj vqFg== X-Gm-Message-State: AOJu0Yz3rO5mATKJigU0b/k91vaoCmNIZmZEwu8PdOqwE/X3G+y6pfCU peoQQAOmDYSbFzSShWpvPr9Lwr48gxYHZ2kaQzYupN1FZR4En27TSUaDAr82iRWC8JyG9YtPEy9 DcDRuridguA== X-Gm-Gg: ASbGnctH0eyFEftxoqAca4NW2NpB3zjr9sUAEc9oU6TR69QLp0wDtoBSMix0S6eKE4k beZI93b3yl8ma6IJxVHz9tPQJILnFTdHY0B1/b2aw0U89UAukB12/gjhBDhtRSAsNVuJPhf/Oze OWOybeQivEWAc+nfsraf2ibQj2JDfgvwHqDONuN9DvmPXDBjRHak6v+yai44+YJd8v7+JY1A2Z8 F72DGrmQW4K4HrGHoZGV8Hp72pnkXz3OqTmRQjKEXSkF0oAnJjpiWpdX9aq9YvAWeUXxgiu/8Bh Bq6/lkimWMv9j6399fAYnldI8nNYkwT2kmrMMfo3PhUZdkGEPhiir1mmfrhp0MfFh70WoKzdYPL ybEgTgK0w8yZqGTf/cTKGK7q9XE2v7+8MpJq4Pk3BlZOWrjhW744pxC4GuRPTyV0SPhZumTWhea 0= X-Google-Smtp-Source: AGHT+IFmdm+Po0P1++N5LtyQw+r4rWBFNbbkWpZkWUC8+P6AqlV0PH43L8/Dr0h9kNUYe7HQBV7meg== X-Received: by 2002:a05:690c:9692:b0:724:bd2d:ac97 with SMTP id 00721157ae682-727f4d6270amr177781537b3.32.1757598652962; Thu, 11 Sep 2025 06:50:52 -0700 (PDT) Received: from rayden.urgonet (h-37-123-177-177.A175.priv.bahnhof.se. [37.123.177.177]) by smtp.gmail.com with ESMTPSA id 00721157ae682-72f7623434csm3526257b3.11.2025.09.11.06.50.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Sep 2025 06:50:51 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, op-tee@lists.trustedfirmware.org, linux-arm-kernel@lists.infradead.org Cc: Olivier Masse , Thierry Reding , Yong Wu , Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T . J . Mercier" , =?UTF-8?q?Christian=20K=C3=B6nig?= , Sumit Garg , Matthias Brugger , AngeloGioacchino Del Regno , azarrabi@qti.qualcomm.com, Simona Vetter , Daniel Stone , Rouven Czerwinski , robin.murphy@arm.com, Jens Wiklander , Sumit Garg Subject: [PATCH v12 7/9] optee: support protected memory allocation Date: Thu, 11 Sep 2025 15:49:48 +0200 Message-ID: <20250911135007.1275833-8-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250911135007.1275833-1-jens.wiklander@linaro.org> References: <20250911135007.1275833-1-jens.wiklander@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support in the OP-TEE backend driver for protected memory allocation. The support is limited to only the SMC ABI and for secure video buffers. OP-TEE is probed for the range of protected physical memory and a memory pool allocator is initialized if OP-TEE have support for such memory. Reviewed-by: Sumit Garg Signed-off-by: Jens Wiklander --- drivers/tee/optee/Kconfig | 5 +++ drivers/tee/optee/core.c | 7 ++++ drivers/tee/optee/optee_private.h | 2 + drivers/tee/optee/smc_abi.c | 69 ++++++++++++++++++++++++++++++- 4 files changed, 81 insertions(+), 2 deletions(-) diff --git a/drivers/tee/optee/Kconfig b/drivers/tee/optee/Kconfig index 7bb7990d0b07..50d2051f7f20 100644 --- a/drivers/tee/optee/Kconfig +++ b/drivers/tee/optee/Kconfig @@ -25,3 +25,8 @@ config OPTEE_INSECURE_LOAD_IMAGE =20 Additional documentation on kernel security risks are at Documentation/tee/op-tee.rst. + +config OPTEE_STATIC_PROTMEM_POOL + bool + depends on HAS_IOMEM && TEE_DMABUF_HEAPS + default y diff --git a/drivers/tee/optee/core.c b/drivers/tee/optee/core.c index c75fddc83576..49ccfe6f6583 100644 --- a/drivers/tee/optee/core.c +++ b/drivers/tee/optee/core.c @@ -56,6 +56,13 @@ int optee_rpmb_intf_rdev(struct notifier_block *intf, un= signed long action, return 0; } =20 +int optee_set_dma_mask(struct optee *optee, u_int pa_width) +{ + u64 mask =3D DMA_BIT_MASK(min(64, pa_width)); + + return dma_coerce_mask_and_coherent(&optee->teedev->dev, mask); +} + static void optee_bus_scan(struct work_struct *work) { WARN_ON(optee_enumerate_devices(PTA_CMD_GET_DEVICES_SUPP)); diff --git a/drivers/tee/optee/optee_private.h b/drivers/tee/optee/optee_pr= ivate.h index 9526087f0e68..4969b83a9851 100644 --- a/drivers/tee/optee/optee_private.h +++ b/drivers/tee/optee/optee_private.h @@ -274,6 +274,8 @@ struct optee_call_ctx { =20 extern struct blocking_notifier_head optee_rpmb_intf_added; =20 +int optee_set_dma_mask(struct optee *optee, u_int pa_width); + int optee_notif_init(struct optee *optee, u_int max_key); void optee_notif_uninit(struct optee *optee); int optee_notif_wait(struct optee *optee, u_int key, u32 timeout); diff --git a/drivers/tee/optee/smc_abi.c b/drivers/tee/optee/smc_abi.c index 26f8f7bbbe56..b4c007ed3b94 100644 --- a/drivers/tee/optee/smc_abi.c +++ b/drivers/tee/optee/smc_abi.c @@ -1583,6 +1583,68 @@ static inline int optee_load_fw(struct platform_devi= ce *pdev, } #endif =20 +static struct tee_protmem_pool *static_protmem_pool_init(struct optee *opt= ee) +{ +#if IS_ENABLED(CONFIG_OPTEE_STATIC_PROTMEM_POOL) + union { + struct arm_smccc_res smccc; + struct optee_smc_get_protmem_config_result result; + } res; + struct tee_protmem_pool *pool; + void *p; + int rc; + + optee->smc.invoke_fn(OPTEE_SMC_GET_PROTMEM_CONFIG, 0, 0, 0, 0, + 0, 0, 0, &res.smccc); + if (res.result.status !=3D OPTEE_SMC_RETURN_OK) + return ERR_PTR(-EINVAL); + + rc =3D optee_set_dma_mask(optee, res.result.pa_width); + if (rc) + return ERR_PTR(rc); + + /* + * Map the memory as uncached to make sure the kernel can work with + * __pfn_to_page() and friends since that's needed when passing the + * protected DMA-buf to a device. The memory should otherwise not + * be touched by the kernel since it's likely to cause an external + * abort due to the protection status. + */ + p =3D devm_memremap(&optee->teedev->dev, res.result.start, + res.result.size, MEMREMAP_WC); + if (IS_ERR(p)) + return p; + + pool =3D tee_protmem_static_pool_alloc(res.result.start, res.result.size); + if (IS_ERR(pool)) + devm_memunmap(&optee->teedev->dev, p); + + return pool; +#else + return ERR_PTR(-EINVAL); +#endif +} + +static int optee_protmem_pool_init(struct optee *optee) +{ + enum tee_dma_heap_id heap_id =3D TEE_DMA_HEAP_SECURE_VIDEO_PLAY; + struct tee_protmem_pool *pool =3D ERR_PTR(-EINVAL); + int rc; + + if (!(optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_PROTMEM)) + return 0; + + pool =3D static_protmem_pool_init(optee); + if (IS_ERR(pool)) + return PTR_ERR(pool); + + rc =3D tee_device_register_dma_heap(optee->teedev, heap_id, pool); + if (rc) + pool->ops->destroy_pool(pool); + + return rc; +} + static int optee_probe(struct platform_device *pdev) { optee_invoke_fn *invoke_fn; @@ -1678,7 +1740,7 @@ static int optee_probe(struct platform_device *pdev) optee =3D kzalloc(sizeof(*optee), GFP_KERNEL); if (!optee) { rc =3D -ENOMEM; - goto err_free_pool; + goto err_free_shm_pool; } =20 optee->ops =3D &optee_ops; @@ -1751,6 +1813,9 @@ static int optee_probe(struct platform_device *pdev) pr_info("Asynchronous notifications enabled\n"); } =20 + if (optee_protmem_pool_init(optee)) + pr_info("Protected memory service not available\n"); + /* * Ensure that there are no pre-existing shm objects before enabling * the shm cache so that there's no chance of receiving an invalid @@ -1802,7 +1867,7 @@ static int optee_probe(struct platform_device *pdev) tee_device_unregister(optee->teedev); err_free_optee: kfree(optee); -err_free_pool: +err_free_shm_pool: tee_shm_pool_free(pool); if (memremaped_shm) memunmap(memremaped_shm); --=20 2.43.0 From nobody Thu Oct 2 20:23:05 2025 Received: from mail-yw1-f171.google.com (mail-yw1-f171.google.com [209.85.128.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 50AE2343D7A for ; Thu, 11 Sep 2025 13:51:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757598663; cv=none; b=Nkgyasg+AnOA4h02xno1oTDrIEmF198hF7CcD0WW7wOixHxMOnQM9uxH3dWLO5tbp7/hKcahIYg+r4eyLAeq3qnaCZg1sr9Iz2+aaoOrTQBcUbwf8QO892EgY+DdQ9KDgHPV3gj7sUgQ5SWDt43g7AEtCZ+ttWuM7CijhnqrWhs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757598663; c=relaxed/simple; bh=7lx2sbEAZDM9zU/kFQuU496T4A2wzllAaTdHsOmFOXk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=R4+j/GuFS7QuXVNHk5BFK2BmQ3sRmVkzWjNC6BRUQ24LDr1B8r1cCFgamlTaaFmyOjr4c7mY/xT0OhMBPDJAt7aO8o+E6oYzKjgcgGpl3KzeYERoXTcmVmIS+LgIQKkjF49cl5SC+mypUZMdOZaGtJU4sxDO+9ROCnHobVfw7H0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=JbCXiOFV; arc=none smtp.client-ip=209.85.128.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="JbCXiOFV" Received: by mail-yw1-f171.google.com with SMTP id 00721157ae682-7227bc08c97so12588327b3.1 for ; Thu, 11 Sep 2025 06:51:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1757598658; x=1758203458; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bZXu7fgTcjszXUQnAeh7sStk1EAI2XgsdeRAagx8XY4=; b=JbCXiOFVY7/6XDj8DT4VSyIYlIdk+ttxeAEb4lg2jkAWP+3q9QZZouqvFIDB//5wd0 UYufUe5RibpezkAPx5Y1UVN/jn4QP+IJCzKq5R08rioVvutSeH6trqqCuYXLspM6ouz8 T1ai/B/M82UmkRb5gdPO7aOrlVSldZRcYfpa/022QEL8bDKkigtfzSyVrvZlTdYfuVcm 48LfRKrcFyJawNqSyblCDnXPf7wCzVp0eiOn64JSTfllb0akr3kXD8RndjNxeXJ2T2Tb j7nRs+/AbTXbfIRfGeRtyci9VMFlA7qyMHQtxt7UFzyGKX6JykZgA1E0CVLddACxPKZk /CSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757598658; x=1758203458; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bZXu7fgTcjszXUQnAeh7sStk1EAI2XgsdeRAagx8XY4=; b=VpgjutBcvXD+i4QlQkQMOtfegzLtfQSZzZXU7EUEtXmI8+G1UvyKdTwwhE1x+tSSnY 7vdMjNOtxodiw94VSdm9kZw5uxPmCfKa6gGcJ7p2KbhHRrjRokLOKsx5GYmbKsWjS6Aa Vl1mel28LXrzsRXtXjlwnEPNSdas0horYs/ErXsbZqtGq6u9QHqxiauMsmxDo2UBY68r yLgZHPmwIyOx513EbG+pCLBLSDm3MUxfhs7ferDRqIi+sO03+hEWTndE2hKcH9cgc9uS 2JQLBrPpfBVJLZR2rzD+3HyfHqOD5CiwKD9/G0WJxyHlruOMitmYlKShPVchOcfj+azG vHnQ== X-Gm-Message-State: AOJu0Yxt8B086zGWyqwzGWa7aIwktpZ7A/1ig0AE0Z6nsgqalE8oJ4LE aH+ImzRoYmBAF1L5npgc4NZDaOFwvPj11ffA6hW7hmQpM9Y8EX0j0E91jJ9dfY8eQ7rJIAGlJ6h cZYivQLIzDA== X-Gm-Gg: ASbGnct5vmgOs3yIfjDB1S/JRUhIqB06IWlrktQLrTiWxG3QZZtIVR6ukqhzm0Vjb0U el+dda2unSiZEAAyM3o/dL6pANt/Wp8tVfhVL/h6ryygpJmp2XbiPaYGxkrcO0BkxAXOaRzMoDP w94fLFm+Etid0p2chKvBVNHFvVIvoKvgvt/hM11TcuupOtQPY9emCaID1rHTwB/FDRIT1u70YT3 dcDHz3juis8hSiD0pvMFu7fkMrM0IlJzqD4iVZ2fItwyHW97xzvKKGkok5ftCogRv2q7Q32EFGk b+cZNaeIy7LDb4xBVtbFNW5yC5TDWR94flsdVh4zVgGJhsrPhyCNs0k2kZujOu7JdspZJ1y5UY0 ie4g67UwAQLBf1cdSmWHRIf86FEJ0W4moGbmDZqr6DXRhcs4jZLu03/o/p8TeNnm5tK/jVtftau CGwhEl9FA9Ag== X-Google-Smtp-Source: AGHT+IGJvxXuHGQAHBLN6Wk8nLrSTTM66Jh1PJ6NtLa7Xlqec5cFnXU2qXxdnqx13/0Po7daKljK+g== X-Received: by 2002:a05:690e:2141:b0:5f4:f81b:d70e with SMTP id 956f58d0204a3-623389c387emr2417496d50.2.1757598657880; Thu, 11 Sep 2025 06:50:57 -0700 (PDT) Received: from rayden.urgonet (h-37-123-177-177.A175.priv.bahnhof.se. [37.123.177.177]) by smtp.gmail.com with ESMTPSA id 00721157ae682-72f7623434csm3526257b3.11.2025.09.11.06.50.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Sep 2025 06:50:56 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, op-tee@lists.trustedfirmware.org, linux-arm-kernel@lists.infradead.org Cc: Olivier Masse , Thierry Reding , Yong Wu , Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T . J . Mercier" , =?UTF-8?q?Christian=20K=C3=B6nig?= , Sumit Garg , Matthias Brugger , AngeloGioacchino Del Regno , azarrabi@qti.qualcomm.com, Simona Vetter , Daniel Stone , Rouven Czerwinski , robin.murphy@arm.com, Jens Wiklander , Sumit Garg Subject: [PATCH v12 8/9] optee: FF-A: dynamic protected memory allocation Date: Thu, 11 Sep 2025 15:49:49 +0200 Message-ID: <20250911135007.1275833-9-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250911135007.1275833-1-jens.wiklander@linaro.org> References: <20250911135007.1275833-1-jens.wiklander@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support in the OP-TEE backend driver dynamic protected memory allocation with FF-A. The protected memory pools for dynamically allocated protected memory are instantiated when requested by user-space. This instantiation can fail if OP-TEE doesn't support the requested use-case of protected memory. Restricted memory pools based on a static carveout or dynamic allocation can coexist for different use-cases. We use only dynamic allocation with FF-A. Reviewed-by: Sumit Garg Signed-off-by: Jens Wiklander --- drivers/tee/optee/Makefile | 1 + drivers/tee/optee/ffa_abi.c | 146 ++++++++++++- drivers/tee/optee/optee_private.h | 13 +- drivers/tee/optee/protmem.c | 335 ++++++++++++++++++++++++++++++ 4 files changed, 492 insertions(+), 3 deletions(-) create mode 100644 drivers/tee/optee/protmem.c diff --git a/drivers/tee/optee/Makefile b/drivers/tee/optee/Makefile index a6eff388d300..ad7049c1c107 100644 --- a/drivers/tee/optee/Makefile +++ b/drivers/tee/optee/Makefile @@ -4,6 +4,7 @@ optee-objs +=3D core.o optee-objs +=3D call.o optee-objs +=3D notif.o optee-objs +=3D rpc.o +optee-objs +=3D protmem.o optee-objs +=3D supp.o optee-objs +=3D device.o optee-objs +=3D smc_abi.o diff --git a/drivers/tee/optee/ffa_abi.c b/drivers/tee/optee/ffa_abi.c index f9ef7d94cebd..11c543ac65c6 100644 --- a/drivers/tee/optee/ffa_abi.c +++ b/drivers/tee/optee/ffa_abi.c @@ -649,6 +649,124 @@ static int optee_ffa_do_call_with_arg(struct tee_cont= ext *ctx, return optee_ffa_yielding_call(ctx, &data, rpc_arg, system_thread); } =20 +static int do_call_lend_protmem(struct optee *optee, u64 cookie, u32 use_c= ase) +{ + struct optee_shm_arg_entry *entry; + struct optee_msg_arg *msg_arg; + struct tee_shm *shm; + u_int offs; + int rc; + + msg_arg =3D optee_get_msg_arg(optee->ctx, 1, &entry, &shm, &offs); + if (IS_ERR(msg_arg)) + return PTR_ERR(msg_arg); + + msg_arg->cmd =3D OPTEE_MSG_CMD_ASSIGN_PROTMEM; + msg_arg->params[0].attr =3D OPTEE_MSG_ATTR_TYPE_VALUE_INPUT; + msg_arg->params[0].u.value.a =3D cookie; + msg_arg->params[0].u.value.b =3D use_case; + + rc =3D optee->ops->do_call_with_arg(optee->ctx, shm, offs, false); + if (rc) + goto out; + if (msg_arg->ret !=3D TEEC_SUCCESS) { + rc =3D -EINVAL; + goto out; + } + +out: + optee_free_msg_arg(optee->ctx, entry, offs); + return rc; +} + +static int optee_ffa_lend_protmem(struct optee *optee, struct tee_shm *pro= tmem, + u32 *mem_attrs, unsigned int ma_count, + u32 use_case) +{ + struct ffa_device *ffa_dev =3D optee->ffa.ffa_dev; + const struct ffa_mem_ops *mem_ops =3D ffa_dev->ops->mem_ops; + const struct ffa_msg_ops *msg_ops =3D ffa_dev->ops->msg_ops; + struct ffa_send_direct_data data; + struct ffa_mem_region_attributes *mem_attr; + struct ffa_mem_ops_args args =3D { + .use_txbuf =3D true, + .tag =3D use_case, + }; + struct page *page; + struct scatterlist sgl; + unsigned int n; + int rc; + + mem_attr =3D kcalloc(ma_count, sizeof(*mem_attr), GFP_KERNEL); + for (n =3D 0; n < ma_count; n++) { + mem_attr[n].receiver =3D mem_attrs[n] & U16_MAX; + mem_attr[n].attrs =3D mem_attrs[n] >> 16; + } + args.attrs =3D mem_attr; + args.nattrs =3D ma_count; + + page =3D phys_to_page(protmem->paddr); + sg_init_table(&sgl, 1); + sg_set_page(&sgl, page, protmem->size, 0); + + args.sg =3D &sgl; + rc =3D mem_ops->memory_lend(&args); + kfree(mem_attr); + if (rc) + return rc; + + rc =3D do_call_lend_protmem(optee, args.g_handle, use_case); + if (rc) + goto err_reclaim; + + rc =3D optee_shm_add_ffa_handle(optee, protmem, args.g_handle); + if (rc) + goto err_unreg; + + protmem->sec_world_id =3D args.g_handle; + + return 0; + +err_unreg: + data =3D (struct ffa_send_direct_data){ + .data0 =3D OPTEE_FFA_RELEASE_PROTMEM, + .data1 =3D (u32)args.g_handle, + .data2 =3D (u32)(args.g_handle >> 32), + }; + msg_ops->sync_send_receive(ffa_dev, &data); +err_reclaim: + mem_ops->memory_reclaim(args.g_handle, 0); + return rc; +} + +static int optee_ffa_reclaim_protmem(struct optee *optee, + struct tee_shm *protmem) +{ + struct ffa_device *ffa_dev =3D optee->ffa.ffa_dev; + const struct ffa_msg_ops *msg_ops =3D ffa_dev->ops->msg_ops; + const struct ffa_mem_ops *mem_ops =3D ffa_dev->ops->mem_ops; + u64 global_handle =3D protmem->sec_world_id; + struct ffa_send_direct_data data =3D { + .data0 =3D OPTEE_FFA_RELEASE_PROTMEM, + .data1 =3D (u32)global_handle, + .data2 =3D (u32)(global_handle >> 32) + }; + int rc; + + optee_shm_rem_ffa_handle(optee, global_handle); + protmem->sec_world_id =3D 0; + + rc =3D msg_ops->sync_send_receive(ffa_dev, &data); + if (rc) + pr_err("Release SHM id 0x%llx rc %d\n", global_handle, rc); + + rc =3D mem_ops->memory_reclaim(global_handle, 0); + if (rc) + pr_err("mem_reclaim: 0x%llx %d", global_handle, rc); + + return rc; +} + /* * 6. Driver initialization * @@ -819,6 +937,8 @@ static const struct optee_ops optee_ffa_ops =3D { .do_call_with_arg =3D optee_ffa_do_call_with_arg, .to_msg_param =3D optee_ffa_to_msg_param, .from_msg_param =3D optee_ffa_from_msg_param, + .lend_protmem =3D optee_ffa_lend_protmem, + .reclaim_protmem =3D optee_ffa_reclaim_protmem, }; =20 static void optee_ffa_remove(struct ffa_device *ffa_dev) @@ -891,6 +1011,25 @@ static int optee_ffa_async_notif_init(struct ffa_devi= ce *ffa_dev, return rc; } =20 +static int optee_ffa_protmem_pool_init(struct optee *optee, u32 sec_caps) +{ + enum tee_dma_heap_id id =3D TEE_DMA_HEAP_SECURE_VIDEO_PLAY; + struct tee_protmem_pool *pool; + int rc =3D 0; + + if (sec_caps & OPTEE_FFA_SEC_CAP_PROTMEM) { + pool =3D optee_protmem_alloc_dyn_pool(optee, id); + if (IS_ERR(pool)) + return PTR_ERR(pool); + + rc =3D tee_device_register_dma_heap(optee->teedev, id, pool); + if (rc) + pool->ops->destroy_pool(pool); + } + + return rc; +} + static int optee_ffa_probe(struct ffa_device *ffa_dev) { const struct ffa_notifier_ops *notif_ops; @@ -941,7 +1080,7 @@ static int optee_ffa_probe(struct ffa_device *ffa_dev) optee); if (IS_ERR(teedev)) { rc =3D PTR_ERR(teedev); - goto err_free_pool; + goto err_free_shm_pool; } optee->teedev =3D teedev; =20 @@ -988,6 +1127,9 @@ static int optee_ffa_probe(struct ffa_device *ffa_dev) rc); } =20 + if (optee_ffa_protmem_pool_init(optee, sec_caps)) + pr_info("Protected memory service not available\n"); + rc =3D optee_enumerate_devices(PTA_CMD_GET_DEVICES); if (rc) goto err_unregister_devices; @@ -1018,7 +1160,7 @@ static int optee_ffa_probe(struct ffa_device *ffa_dev) tee_device_unregister(optee->supp_teedev); err_unreg_teedev: tee_device_unregister(optee->teedev); -err_free_pool: +err_free_shm_pool: tee_shm_pool_free(pool); err_free_optee: kfree(optee); diff --git a/drivers/tee/optee/optee_private.h b/drivers/tee/optee/optee_pr= ivate.h index 4969b83a9851..db9ea673fbca 100644 --- a/drivers/tee/optee/optee_private.h +++ b/drivers/tee/optee/optee_private.h @@ -176,9 +176,14 @@ struct optee; * @do_call_with_arg: enters OP-TEE in secure world * @to_msg_param: converts from struct tee_param to OPTEE_MSG parameters * @from_msg_param: converts from OPTEE_MSG parameters to struct tee_param + * @lend_protmem: lends physically contiguous memory as restricted + * memory, inaccessible by the kernel + * @reclaim_protmem: reclaims restricted memory previously lent with + * @lend_protmem() and makes it accessible by the + * kernel again * * These OPs are only supposed to be used internally in the OP-TEE driver - * as a way of abstracting the different methogs of entering OP-TEE in + * as a way of abstracting the different methods of entering OP-TEE in * secure world. */ struct optee_ops { @@ -191,6 +196,10 @@ struct optee_ops { int (*from_msg_param)(struct optee *optee, struct tee_param *params, size_t num_params, const struct optee_msg_param *msg_params); + int (*lend_protmem)(struct optee *optee, struct tee_shm *protmem, + u32 *mem_attr, unsigned int ma_count, + u32 use_case); + int (*reclaim_protmem)(struct optee *optee, struct tee_shm *protmem); }; =20 /** @@ -287,6 +296,8 @@ u32 optee_supp_thrd_req(struct tee_context *ctx, u32 fu= nc, size_t num_params, void optee_supp_init(struct optee_supp *supp); void optee_supp_uninit(struct optee_supp *supp); void optee_supp_release(struct optee_supp *supp); +struct tee_protmem_pool *optee_protmem_alloc_dyn_pool(struct optee *optee, + enum tee_dma_heap_id id); =20 int optee_supp_recv(struct tee_context *ctx, u32 *func, u32 *num_params, struct tee_param *param); diff --git a/drivers/tee/optee/protmem.c b/drivers/tee/optee/protmem.c new file mode 100644 index 000000000000..2eba48d5ac73 --- /dev/null +++ b/drivers/tee/optee/protmem.c @@ -0,0 +1,335 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2025, Linaro Limited + */ +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include +#include +#include +#include "optee_private.h" + +struct optee_protmem_dyn_pool { + struct tee_protmem_pool pool; + struct gen_pool *gen_pool; + struct optee *optee; + size_t page_count; + u32 *mem_attrs; + u_int mem_attr_count; + refcount_t refcount; + u32 use_case; + struct tee_shm *protmem; + /* Protects when initializing and tearing down this struct */ + struct mutex mutex; +}; + +static struct optee_protmem_dyn_pool * +to_protmem_dyn_pool(struct tee_protmem_pool *pool) +{ + return container_of(pool, struct optee_protmem_dyn_pool, pool); +} + +static int init_dyn_protmem(struct optee_protmem_dyn_pool *rp) +{ + int rc; + + rp->protmem =3D tee_shm_alloc_dma_mem(rp->optee->ctx, rp->page_count); + if (IS_ERR(rp->protmem)) { + rc =3D PTR_ERR(rp->protmem); + goto err_null_protmem; + } + + /* + * TODO unmap the memory range since the physical memory will + * become inaccesible after the lend_protmem() call. + * + * If the platform supports a hypervisor at EL2, it will unmap the + * intermediate physical memory for us and stop cache pre-fetch of + * the memory. + */ + rc =3D rp->optee->ops->lend_protmem(rp->optee, rp->protmem, + rp->mem_attrs, + rp->mem_attr_count, rp->use_case); + if (rc) + goto err_put_shm; + rp->protmem->flags |=3D TEE_SHM_DYNAMIC; + + rp->gen_pool =3D gen_pool_create(PAGE_SHIFT, -1); + if (!rp->gen_pool) { + rc =3D -ENOMEM; + goto err_reclaim; + } + + rc =3D gen_pool_add(rp->gen_pool, rp->protmem->paddr, + rp->protmem->size, -1); + if (rc) + goto err_free_pool; + + refcount_set(&rp->refcount, 1); + return 0; + +err_free_pool: + gen_pool_destroy(rp->gen_pool); + rp->gen_pool =3D NULL; +err_reclaim: + rp->optee->ops->reclaim_protmem(rp->optee, rp->protmem); +err_put_shm: + tee_shm_put(rp->protmem); +err_null_protmem: + rp->protmem =3D NULL; + return rc; +} + +static int get_dyn_protmem(struct optee_protmem_dyn_pool *rp) +{ + int rc =3D 0; + + if (!refcount_inc_not_zero(&rp->refcount)) { + mutex_lock(&rp->mutex); + if (rp->gen_pool) { + /* + * Another thread has already initialized the pool + * before us, or the pool was just about to be torn + * down. Either way we only need to increase the + * refcount and we're done. + */ + refcount_inc(&rp->refcount); + } else { + rc =3D init_dyn_protmem(rp); + } + mutex_unlock(&rp->mutex); + } + + return rc; +} + +static void release_dyn_protmem(struct optee_protmem_dyn_pool *rp) +{ + gen_pool_destroy(rp->gen_pool); + rp->gen_pool =3D NULL; + + rp->optee->ops->reclaim_protmem(rp->optee, rp->protmem); + rp->protmem->flags &=3D ~TEE_SHM_DYNAMIC; + + WARN(refcount_read(&rp->protmem->refcount) !=3D 1, "Unexpected refcount"); + tee_shm_put(rp->protmem); + rp->protmem =3D NULL; +} + +static void put_dyn_protmem(struct optee_protmem_dyn_pool *rp) +{ + if (refcount_dec_and_test(&rp->refcount)) { + mutex_lock(&rp->mutex); + if (rp->gen_pool) + release_dyn_protmem(rp); + mutex_unlock(&rp->mutex); + } +} + +static int protmem_pool_op_dyn_alloc(struct tee_protmem_pool *pool, + struct sg_table *sgt, size_t size, + size_t *offs) +{ + struct optee_protmem_dyn_pool *rp =3D to_protmem_dyn_pool(pool); + size_t sz =3D ALIGN(size, PAGE_SIZE); + phys_addr_t pa; + int rc; + + rc =3D get_dyn_protmem(rp); + if (rc) + return rc; + + pa =3D gen_pool_alloc(rp->gen_pool, sz); + if (!pa) { + rc =3D -ENOMEM; + goto err_put; + } + + rc =3D sg_alloc_table(sgt, 1, GFP_KERNEL); + if (rc) + goto err_free; + + sg_set_page(sgt->sgl, phys_to_page(pa), size, 0); + *offs =3D pa - rp->protmem->paddr; + + return 0; +err_free: + gen_pool_free(rp->gen_pool, pa, size); +err_put: + put_dyn_protmem(rp); + + return rc; +} + +static void protmem_pool_op_dyn_free(struct tee_protmem_pool *pool, + struct sg_table *sgt) +{ + struct optee_protmem_dyn_pool *rp =3D to_protmem_dyn_pool(pool); + struct scatterlist *sg; + int i; + + for_each_sgtable_sg(sgt, sg, i) + gen_pool_free(rp->gen_pool, sg_phys(sg), sg->length); + sg_free_table(sgt); + put_dyn_protmem(rp); +} + +static int protmem_pool_op_dyn_update_shm(struct tee_protmem_pool *pool, + struct sg_table *sgt, size_t offs, + struct tee_shm *shm, + struct tee_shm **parent_shm) +{ + struct optee_protmem_dyn_pool *rp =3D to_protmem_dyn_pool(pool); + + *parent_shm =3D rp->protmem; + + return 0; +} + +static void pool_op_dyn_destroy_pool(struct tee_protmem_pool *pool) +{ + struct optee_protmem_dyn_pool *rp =3D to_protmem_dyn_pool(pool); + + mutex_destroy(&rp->mutex); + kfree(rp); +} + +static struct tee_protmem_pool_ops protmem_pool_ops_dyn =3D { + .alloc =3D protmem_pool_op_dyn_alloc, + .free =3D protmem_pool_op_dyn_free, + .update_shm =3D protmem_pool_op_dyn_update_shm, + .destroy_pool =3D pool_op_dyn_destroy_pool, +}; + +static int get_protmem_config(struct optee *optee, u32 use_case, + size_t *min_size, u_int *pa_width, + u32 *mem_attrs, u_int *ma_count) +{ + struct tee_param params[2] =3D { + [0] =3D { + .attr =3D TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT, + .u.value.a =3D use_case, + }, + [1] =3D { + .attr =3D TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT, + }, + }; + struct optee_shm_arg_entry *entry; + struct tee_shm *shm_param =3D NULL; + struct optee_msg_arg *msg_arg; + struct tee_shm *shm; + u_int offs; + int rc; + + if (mem_attrs && *ma_count) { + params[1].u.memref.size =3D *ma_count * sizeof(*mem_attrs); + shm_param =3D tee_shm_alloc_priv_buf(optee->ctx, + params[1].u.memref.size); + if (IS_ERR(shm_param)) + return PTR_ERR(shm_param); + params[1].u.memref.shm =3D shm_param; + } + + msg_arg =3D optee_get_msg_arg(optee->ctx, ARRAY_SIZE(params), &entry, + &shm, &offs); + if (IS_ERR(msg_arg)) { + rc =3D PTR_ERR(msg_arg); + goto out_free_shm; + } + msg_arg->cmd =3D OPTEE_MSG_CMD_GET_PROTMEM_CONFIG; + + rc =3D optee->ops->to_msg_param(optee, msg_arg->params, + ARRAY_SIZE(params), params); + if (rc) + goto out_free_msg; + + rc =3D optee->ops->do_call_with_arg(optee->ctx, shm, offs, false); + if (rc) + goto out_free_msg; + if (msg_arg->ret && msg_arg->ret !=3D TEEC_ERROR_SHORT_BUFFER) { + rc =3D -EINVAL; + goto out_free_msg; + } + + rc =3D optee->ops->from_msg_param(optee, params, ARRAY_SIZE(params), + msg_arg->params); + if (rc) + goto out_free_msg; + + if (!msg_arg->ret && mem_attrs && + *ma_count < params[1].u.memref.size / sizeof(*mem_attrs)) { + rc =3D -EINVAL; + goto out_free_msg; + } + + *min_size =3D params[0].u.value.a; + *pa_width =3D params[0].u.value.c; + *ma_count =3D params[1].u.memref.size / sizeof(*mem_attrs); + + if (msg_arg->ret =3D=3D TEEC_ERROR_SHORT_BUFFER) { + rc =3D -ENOSPC; + goto out_free_msg; + } + + if (mem_attrs) + memcpy(mem_attrs, tee_shm_get_va(shm_param, 0), + params[1].u.memref.size); + +out_free_msg: + optee_free_msg_arg(optee->ctx, entry, offs); +out_free_shm: + if (shm_param) + tee_shm_free(shm_param); + return rc; +} + +struct tee_protmem_pool *optee_protmem_alloc_dyn_pool(struct optee *optee, + enum tee_dma_heap_id id) +{ + struct optee_protmem_dyn_pool *rp; + size_t min_size; + u_int pa_width; + int rc; + + rp =3D kzalloc(sizeof(*rp), GFP_KERNEL); + if (!rp) + return ERR_PTR(-ENOMEM); + rp->use_case =3D id; + + rc =3D get_protmem_config(optee, id, &min_size, &pa_width, NULL, + &rp->mem_attr_count); + if (rc) { + if (rc !=3D -ENOSPC) + goto err; + rp->mem_attrs =3D kcalloc(rp->mem_attr_count, + sizeof(*rp->mem_attrs), GFP_KERNEL); + if (!rp->mem_attrs) { + rc =3D -ENOMEM; + goto err; + } + rc =3D get_protmem_config(optee, id, &min_size, &pa_width, + rp->mem_attrs, &rp->mem_attr_count); + if (rc) + goto err_kfree_eps; + } + + rc =3D optee_set_dma_mask(optee, pa_width); + if (rc) + goto err_kfree_eps; + + rp->pool.ops =3D &protmem_pool_ops_dyn; + rp->optee =3D optee; + rp->page_count =3D min_size / PAGE_SIZE; + mutex_init(&rp->mutex); + + return &rp->pool; + +err_kfree_eps: + kfree(rp->mem_attrs); +err: + kfree(rp); + return ERR_PTR(rc); +} --=20 2.43.0 From nobody Thu Oct 2 20:23:05 2025 Received: from mail-yx1-f47.google.com (mail-yx1-f47.google.com [74.125.224.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 33A46343D79 for ; Thu, 11 Sep 2025 13:51:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.224.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757598666; cv=none; b=q1qruP6zlQtjcXSuLofwYWxsJh1Kv/Ff/0P6T6i7uC2rmM6cZ/jCUVeEJsA6BMqYSkph8Xwou5ggU/O/K7TMvsQvsFLB0Notavu4QAGr03ml3K17ngzIzjpz4Rwm5yiEbaZZXMiTzFOw9pEZ4mVJww13trig3++RtbfXebrsi2o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757598666; c=relaxed/simple; bh=4ZNRIraxOBMzfn+Ru1PC7uaIfHrTQ+8/5VMDk1s40M8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pZbLaI4UxKF0gfP/Zr1e08TvHqU8loLqie3UFgJLlnbSd/+Iz74qT5iZmrzAwaKtpyM/P1ZvLEv7Q8yE8eshgtK2XI7oejI7UsmP+EXVVnCeuqMoFpaDK6GjkKZ4B9nOMESn0+TjB2UhYKWHt8bIJkbW+lmzAJ4zFovrD+GxnkA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=lg+RDWVY; arc=none smtp.client-ip=74.125.224.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="lg+RDWVY" Received: by mail-yx1-f47.google.com with SMTP id 956f58d0204a3-6045eb3848eso430765d50.0 for ; Thu, 11 Sep 2025 06:51:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1757598663; x=1758203463; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YC/jPvTDCH7V0MdE5QEYPuycikAgoYKWyE534nDYx4M=; b=lg+RDWVYY9euTBFYnW5Qvy36AVDUgGacZv2IuU15hdNsHYrhD1tCErhGlFo0RBXpGO O/HIXCFCKqNxb2SpvM16AIt2diXMOXSq4Zm/z4Zs0gLaNGr2kKOVVNpHjCaMJcgArXd7 BAdFny6BCWQXxwEJY6y3JINTwTSwp8fXIqW7PGNmZxlzo+JVCzgufaoImwUxBiM3QR/w 2OsN7emaaIiM108EDnNEJT19s+nThIKk4N6DhO4gVXHreA2p/k2oKpxsIISeZ4NjFYnQ zR/7Au2xBip7/Zg35firgYtfrdkKNUHBeWWhHt4667IyiG6nTVEaHgXYFgvxVw2wsnUH C/rA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757598663; x=1758203463; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YC/jPvTDCH7V0MdE5QEYPuycikAgoYKWyE534nDYx4M=; b=jJsrEjyHOgrNbtZH3wlejpSz/XenHOXKOLG6QEo1N688B6NtPY9wI9b8K0B4gJ5M2q KbS+8V0Klp+IYzdgzx8E9uGljuouX7LRWcz5zwl9nlcFlqOOz5znW0bY/em3Z1eCvJek UGYkaH3cTE2afYUZYibyxuRkMNL5gi8057KTnMGWfqJTzFrQOKRVzuLgS5pGiVgT81fn LNYgEH2TH7hoWTpPaV1LAseGz7ZDCC0yGEuETMbzLGXn44E8eZ8mytgF25LBdNgDC22u XAW2HcW953S8dqFTJPP+AO/+fbNlh9ptZOi+uuX7bJ/G0qaRuFC/AGbVzf/69A542z5Z DTzg== X-Gm-Message-State: AOJu0YxM5/YBEdc8nM+B78TiWjqJdE42NgSHE0Vp1FOftKJ8q8u1FtHa qyEYK+1epFpQTB+OrHCqHLFdYB+QAU+aCT+OOZqQknLBubBzoe2V6W/sw2AwPHW+zhVGJ4oyiga oOj7PtyWKug== X-Gm-Gg: ASbGncu/fW57mZNsefNCFErxNWlKLumcT//xy9BezphajdHfsfNSXtkUBZxXg25YudI RtjEgriTvJDlzK4V6aPjA7JN4mitjYp1mBPCCRzSunPXNzyDDVflG0ZjnILUDFbKSj1bXPHK7Al KMxGw6Lpg0feH0FxHXVrWYhMMwniQ1ozCm4B9g7dBSUpuVlMSh0mHJoKSdGGgwhXFMAGmLtfghH Hdo++Uue56DDYV0dNseJSuCQFUdj2dwKM4vKVmKgzeSsfxlW18yWZxbbFHqdRbF4pO00vbitROc Y3M52mh2JRKV6waW/BaiiOtZUuoPqytCyvWLLKdpZOWLpa5MNUYQl8D/YlinVzLmPQEk3vFHG/f lESn82dz70DJIpQnpVV3HuOsF7wn7LJ08n9kPgpr2/JqH5fI/m4tQO7rtKCjirIxu5rGpOqzHKV c= X-Google-Smtp-Source: AGHT+IHZBm0VWF5Q2HiB0+9X7Vnwcu0dm3iI+l+WvOP7Jt3IX2w3wk8CNJ5VHY3b6oDMOP/vs9Jw5w== X-Received: by 2002:a53:b3c8:0:b0:5fb:957d:d74f with SMTP id 956f58d0204a3-6102163daa0mr12706114d50.3.1757598662617; Thu, 11 Sep 2025 06:51:02 -0700 (PDT) Received: from rayden.urgonet (h-37-123-177-177.A175.priv.bahnhof.se. [37.123.177.177]) by smtp.gmail.com with ESMTPSA id 00721157ae682-72f7623434csm3526257b3.11.2025.09.11.06.50.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Sep 2025 06:51:01 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, op-tee@lists.trustedfirmware.org, linux-arm-kernel@lists.infradead.org Cc: Olivier Masse , Thierry Reding , Yong Wu , Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T . J . Mercier" , =?UTF-8?q?Christian=20K=C3=B6nig?= , Sumit Garg , Matthias Brugger , AngeloGioacchino Del Regno , azarrabi@qti.qualcomm.com, Simona Vetter , Daniel Stone , Rouven Czerwinski , robin.murphy@arm.com, Jens Wiklander , Sumit Garg Subject: [PATCH v12 9/9] optee: smc abi: dynamic protected memory allocation Date: Thu, 11 Sep 2025 15:49:50 +0200 Message-ID: <20250911135007.1275833-10-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250911135007.1275833-1-jens.wiklander@linaro.org> References: <20250911135007.1275833-1-jens.wiklander@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support in the OP-TEE backend driver for dynamic protected memory allocation using the SMC ABI. Reviewed-by: Sumit Garg Signed-off-by: Jens Wiklander --- drivers/tee/optee/smc_abi.c | 78 +++++++++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 3 deletions(-) diff --git a/drivers/tee/optee/smc_abi.c b/drivers/tee/optee/smc_abi.c index b4c007ed3b94..0be663fcd52b 100644 --- a/drivers/tee/optee/smc_abi.c +++ b/drivers/tee/optee/smc_abi.c @@ -965,6 +965,70 @@ static int optee_smc_do_call_with_arg(struct tee_conte= xt *ctx, return rc; } =20 +static int optee_smc_lend_protmem(struct optee *optee, struct tee_shm *pro= tmem, + u32 *mem_attrs, unsigned int ma_count, + u32 use_case) +{ + struct optee_shm_arg_entry *entry; + struct optee_msg_arg *msg_arg; + struct tee_shm *shm; + u_int offs; + int rc; + + msg_arg =3D optee_get_msg_arg(optee->ctx, 2, &entry, &shm, &offs); + if (IS_ERR(msg_arg)) + return PTR_ERR(msg_arg); + + msg_arg->cmd =3D OPTEE_MSG_CMD_LEND_PROTMEM; + msg_arg->params[0].attr =3D OPTEE_MSG_ATTR_TYPE_VALUE_INPUT; + msg_arg->params[0].u.value.a =3D use_case; + msg_arg->params[1].attr =3D OPTEE_MSG_ATTR_TYPE_TMEM_INPUT; + msg_arg->params[1].u.tmem.buf_ptr =3D protmem->paddr; + msg_arg->params[1].u.tmem.size =3D protmem->size; + msg_arg->params[1].u.tmem.shm_ref =3D (u_long)protmem; + + rc =3D optee->ops->do_call_with_arg(optee->ctx, shm, offs, false); + if (rc) + goto out; + if (msg_arg->ret !=3D TEEC_SUCCESS) { + rc =3D -EINVAL; + goto out; + } + protmem->sec_world_id =3D (u_long)protmem; + +out: + optee_free_msg_arg(optee->ctx, entry, offs); + return rc; +} + +static int optee_smc_reclaim_protmem(struct optee *optee, + struct tee_shm *protmem) +{ + struct optee_shm_arg_entry *entry; + struct optee_msg_arg *msg_arg; + struct tee_shm *shm; + u_int offs; + int rc; + + msg_arg =3D optee_get_msg_arg(optee->ctx, 1, &entry, &shm, &offs); + if (IS_ERR(msg_arg)) + return PTR_ERR(msg_arg); + + msg_arg->cmd =3D OPTEE_MSG_CMD_RECLAIM_PROTMEM; + msg_arg->params[0].attr =3D OPTEE_MSG_ATTR_TYPE_RMEM_INPUT; + msg_arg->params[0].u.rmem.shm_ref =3D (u_long)protmem; + + rc =3D optee->ops->do_call_with_arg(optee->ctx, shm, offs, false); + if (rc) + goto out; + if (msg_arg->ret !=3D TEEC_SUCCESS) + rc =3D -EINVAL; + +out: + optee_free_msg_arg(optee->ctx, entry, offs); + return rc; +} + /* * 5. Asynchronous notification */ @@ -1216,6 +1280,8 @@ static const struct optee_ops optee_ops =3D { .do_call_with_arg =3D optee_smc_do_call_with_arg, .to_msg_param =3D optee_to_msg_param, .from_msg_param =3D optee_from_msg_param, + .lend_protmem =3D optee_smc_lend_protmem, + .reclaim_protmem =3D optee_smc_reclaim_protmem, }; =20 static int enable_async_notif(optee_invoke_fn *invoke_fn) @@ -1627,14 +1693,20 @@ static struct tee_protmem_pool *static_protmem_pool= _init(struct optee *optee) =20 static int optee_protmem_pool_init(struct optee *optee) { + bool protm =3D optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_PROTMEM; + bool dyn_protm =3D optee->smc.sec_caps & + OPTEE_SMC_SEC_CAP_DYNAMIC_PROTMEM; enum tee_dma_heap_id heap_id =3D TEE_DMA_HEAP_SECURE_VIDEO_PLAY; struct tee_protmem_pool *pool =3D ERR_PTR(-EINVAL); - int rc; + int rc =3D -EINVAL; =20 - if (!(optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_PROTMEM)) + if (!protm && !dyn_protm) return 0; =20 - pool =3D static_protmem_pool_init(optee); + if (protm) + pool =3D static_protmem_pool_init(optee); + if (dyn_protm && IS_ERR(pool)) + pool =3D optee_protmem_alloc_dyn_pool(optee, heap_id); if (IS_ERR(pool)) return PTR_ERR(pool); =20 --=20 2.43.0