Skip to contentSkip to conversation

generic TEE subsystem

PATCH Superseded Jens Wiklander 4 participants 18 messages Jul 8, 2015 – Jul 9, 2015

Subsystems (7)

v4 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v4 cover generic TEE subsystem

Hi,

This patch set introduces a generic TEE subsystem. The TEE subsystem will be able contain drivers for various TEE implementations. A TEE (Trusted Execution Environment) is a trusted OS running in some secure environment, for example, TrustZone on ARM cpus, or a separate secure co-processor etc.

Regarding use cases, TrustZone has traditionally been used for offloading secure tasks to the secure world. Examples include banking applications, Digital Rights Management (DRM), or specific secure solutions.

This TEE subsystem can serve a TEE driver for a Global Platform compliant TEE, but it's not limited to only Global Platform TEEs. One reason why I'm doing this to be able to get an OP-TEE (https://github.com/OP-TEE/optee_os)
driver upstream.

"tee: generic TEE subsystem" brings in the generic TEE subsystem which helps when writing a driver for a specific TEE, for example, OP-TEE.

"tee: add OP-TEE driver" is an OP-TEE driver which uses the subsystem to do its work.

This patch set has been prepared in cooperation with Javier Gonz?lez who proposed "Generic TrustZone Driver in Linux Kernel" patches 28 Nov 2014,
https://lwn.net/Articles/623380/ . We've since then changed the scope to TEE instead of TrustZone.

We have discussed the design on tee-dev at lists.linaro.org (archive at
https://lists.linaro.org/pipermail/tee-dev/) with people from other companies, including Valentin Manea <[email protected]>, Emmanuel MICHEL <[email protected]>,
Jean-michel DELORME <[email protected]>,
and Joakim Bech <[email protected]>. Our main concern has been to agree on something that is generic enough to support many different TEEs while still keeping the interface together.

v4:
* Rebased on 4.1
* Redesigned the synchronization around entry exit of normal SMC
* Replaced rwsem on the driver instance with kref and completion since rwsem wasn't intended to be used in this way
* Expanded the TEE_IOCTL_PARAM_ATTR_TYPE_MASK to make room for future additional parameter types
* Documents TEE subsystem and OP-TEE driver
* Replaced TEE_IOC_CMD with TEE_IOC_OPEN_SESSION, TEE_IOC_INVOKE, TEE_IOC_CANCEL and TEE_IOC_CLOSE_SESSION
* DT bindings in a separate patch
* Assembly parts moved to arch/arm and arch/arm64 respectively, in a separate patch
* Redefined/clarified the meaning of OPTEE_SMC_SHM_CACHED
* Removed CMA usage to limit the scope of the patch set

v3:
* Rebased on 4.1-rc3 (dma_buf_export() API change)
* A couple of small sparse fixes
* Documents bindings for OP-TEE driver
* Updated MAINTAINERS

v2:
* Replaced the stubbed OP-TEE driver with a real OP-TEE driver
* Removed most APIs not needed by OP-TEE in current state
* Update Documentation/ioctl/ioctl-number.txt with correct path to tee.h
* Rename tee_shm_pool_alloc_cma() to tee_shm_pool_alloc()
* Moved tee.h into include/uapi/linux/
* Redefined tee.h IOCTL macros to be directly based on _IOR and friends
* Removed version info on the API to user space, a data blob which can contain an UUID is left for user space to be able to tell which protocol to use in TEE_IOC_CMD
* Changed user space exposed structures to only have types with __ prefix
* Dropped THIS_MODULE from tee_fops
* Reworked how the driver is registered and ref counted:
- moved from using an embedded struct miscdevice to an embedded struct device.
- uses an struct rw_semaphore as synchronization for driver detachment
- uses alloc/register pattern from TPM

Thanks,
Jens

Jens Wiklander (5):
arm/arm64: add smccc ARCH32
dt/bindings: add bindings for optee
tee: generic TEE subsystem
tee: add OP-TEE driver
Documentation: tee subsystem and op-tee driver

 Documentation/00-INDEX                             |   2 +
 Documentation/devicetree/bindings/optee/optee.txt  |  17 +
 .../devicetree/bindings/vendor-prefixes.txt        |   1 +
 Documentation/ioctl/ioctl-number.txt               |   1 +
 Documentation/tee.txt                              | 117 +++
 MAINTAINERS                                        |  14 +
 arch/arm/Kconfig                                   |   4 +
 arch/arm/kernel/Makefile                           |   2 +
 arch/arm/kernel/smccc-call.S                       |  26 +
 arch/arm/kernel/smccc.c                            |  17 +
 arch/arm64/Kconfig                                 |   4 +
 arch/arm64/kernel/Makefile                         |   1 +
 arch/arm64/kernel/smccc-call.S                     |  34 +
 arch/arm64/kernel/smccc.c                          |  17 +
 drivers/Kconfig                                    |   2 +
 drivers/Makefile                                   |   1 +
 drivers/tee/Kconfig                                |  18 +
 drivers/tee/Makefile                               |   4 +
 drivers/tee/optee/Kconfig                          |   8 +
 drivers/tee/optee/Makefile                         |   5 +
 drivers/tee/optee/call.c                           | 390 ++++++++++
 drivers/tee/optee/core.c                           | 451 +++++++++++
 drivers/tee/optee/optee_msg.h                      | 334 ++++++++
 drivers/tee/optee/optee_private.h                  | 129 ++++
 drivers/tee/optee/optee_smc.h                      | 466 ++++++++++++
 drivers/tee/optee/rpc.c                            | 248 ++++++
 drivers/tee/optee/supp.c                           | 212 ++++++
 drivers/tee/tee.c                                  | 839 +++++++++++++++++++++
 drivers/tee/tee_private.h                          |  80 ++
 drivers/tee/tee_shm.c                              | 324 ++++++++
 drivers/tee/tee_shm_pool.c                         | 133 ++++
 include/linux/arm-smccc.h                          |  80 ++
 include/linux/tee_drv.h                            | 306 ++++++++
 include/uapi/linux/tee.h                           | 376 +++++++++
 34 files changed, 4663 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/optee/optee.txt
 create mode 100644 Documentation/tee.txt
 create mode 100644 arch/arm/kernel/smccc-call.S
 create mode 100644 arch/arm/kernel/smccc.c
 create mode 100644 arch/arm64/kernel/smccc-call.S
 create mode 100644 arch/arm64/kernel/smccc.c
 create mode 100644 drivers/tee/Kconfig
 create mode 100644 drivers/tee/Makefile
 create mode 100644 drivers/tee/optee/Kconfig
 create mode 100644 drivers/tee/optee/Makefile
 create mode 100644 drivers/tee/optee/call.c
 create mode 100644 drivers/tee/optee/core.c
 create mode 100644 drivers/tee/optee/optee_msg.h
 create mode 100644 drivers/tee/optee/optee_private.h
 create mode 100644 drivers/tee/optee/optee_smc.h
 create mode 100644 drivers/tee/optee/rpc.c
 create mode 100644 drivers/tee/optee/supp.c
 create mode 100644 drivers/tee/tee.c
 create mode 100644 drivers/tee/tee_private.h
 create mode 100644 drivers/tee/tee_shm.c
 create mode 100644 drivers/tee/tee_shm_pool.c
 create mode 100644 include/linux/arm-smccc.h
 create mode 100644 include/linux/tee_drv.h
 create mode 100644 include/uapi/linux/tee.h
Signature · 1 line
1.9.1
#1 lore raw

PATCH v4 1/5 arm/arm64: add smccc ARCH32

Adds helpers to do SMC based on ARM SMC Calling Convention. CONFIG_HAVE_SMCCC is enabled for architectures that may support the SMC instruction. It's the responsibility of the caller to know if the SMC instruction is supported by the platform.

---
 arch/arm/Kconfig               |  4 +++
 arch/arm/kernel/Makefile       |  2 ++
 arch/arm/kernel/smccc-call.S   | 26 ++++++++++++++
 arch/arm/kernel/smccc.c        | 17 +++++++++
 arch/arm64/Kconfig             |  4 +++
 arch/arm64/kernel/Makefile     |  1 +
 arch/arm64/kernel/smccc-call.S | 34 ++++++++++++++++++
 arch/arm64/kernel/smccc.c      | 17 +++++++++
 include/linux/arm-smccc.h      | 80 ++++++++++++++++++++++++++++++++++++++++++
 9 files changed, 185 insertions(+)
 create mode 100644 arch/arm/kernel/smccc-call.S
 create mode 100644 arch/arm/kernel/smccc.c
 create mode 100644 arch/arm64/kernel/smccc-call.S
 create mode 100644 arch/arm64/kernel/smccc.c
 create mode 100644 include/linux/arm-smccc.h
arch/arm/Kconfig +4 -0
index 45df48b..75e4da3 100644
@@ -221,6 +221,9 @@ config NEED_RET_TO_USER
 config ARCH_MTD_XIP
 	bool
 
+config HAVE_SMCCC
+	bool
+
 config VECTORS_BASE
 	hex
 	default 0xffff0000 if MMU || CPU_HIGH_VECTOR
@@ -324,6 +327,7 @@ config ARCH_MULTIPLATFORM
 	select CLKSRC_OF
 	select COMMON_CLK
 	select GENERIC_CLOCKEVENTS
+	select HAVE_SMCCC if CPU_V7
 	select MIGHT_HAVE_PCI
 	select MULTI_IRQ_HANDLER
 	select SPARSE_IRQ
arch/arm/kernel/Makefile +2 -0
index 752725d..8cdd25b 100644
@@ -90,4 +90,6 @@ obj-y				+= psci.o psci-call.o
 obj-$(CONFIG_SMP)		+= psci_smp.o
 endif
 
+obj-$(CONFIG_HAVE_SMCCC)	+= smccc-call.o smccc.o
+
 extra-y := $(head-y) vmlinux.lds
arch/arm/kernel/smccc-call.S new file +26 -0
new file mode 100644
index 0000000..05bc554
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2015, Linaro Limited
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+#include <linux/linkage.h>
+
+#include <asm/opcodes-sec.h>
+
+/* void smccc_call32(struct smccc_param32 *param) */
+ENTRY(smccc_call32)
+	push	{r4-r8, lr}
+	mov	r8, r0
+	ldm	r8, {r0-r7}
+	__SMC(0)
+	stm	r8, {r0-r7}
+	pop	{r4-r8, pc}
+ENDPROC(smccc_call32)
arch/arm/kernel/smccc.c new file +17 -0
new file mode 100644
index 0000000..ba4039e
@@ -0,0 +1,17 @@
+/*
+ * Copyright (c) 2015, Linaro Limited
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+#include <linux/export.h>
+#include <linux/arm-smccc.h>
+
+EXPORT_SYMBOL_GPL(smccc_call32);
arch/arm64/Kconfig +4 -0
index 7796af4..b3ea778 100644
@@ -83,6 +83,7 @@ config ARM64
 	select SPARSE_IRQ
 	select SYSCTL_EXCEPTION_TRACE
 	select HAVE_CONTEXT_TRACKING
+	select HAVE_SMCCC
 	help
 	  ARM 64-bit (AArch64) Linux support.
 
@@ -146,6 +147,9 @@ config KERNEL_MODE_NEON
 config FIX_EARLYCON_MEM
 	def_bool y
 
+config HAVE_SMCCC
+	bool
+
 config PGTABLE_LEVELS
 	int
 	default 2 if ARM64_64K_PAGES && ARM64_VA_BITS_42
arch/arm64/kernel/Makefile +1 -0
index 426d076..f7804f7 100644
@@ -36,6 +36,7 @@ arm64-obj-$(CONFIG_EFI)			+= efi.o efi-stub.o efi-entry.o
 arm64-obj-$(CONFIG_PCI)			+= pci.o
 arm64-obj-$(CONFIG_ARMV8_DEPRECATED)	+= armv8_deprecated.o
 arm64-obj-$(CONFIG_ACPI)		+= acpi.o
+arm64-obj-$(CONFIG_HAVE_SMCCC)		+= smccc-call.o smccc.o
 
 obj-y					+= $(arm64-obj-y) vdso/
 obj-m					+= $(arm64-obj-m)
arch/arm64/kernel/smccc-call.S new file +34 -0
new file mode 100644
index 0000000..3ce7fe8
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2015, Linaro Limited
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License Version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ */
+#include <linux/linkage.h>
+
+#define SMC_PARAM_W0_OFFS	0
+#define SMC_PARAM_W2_OFFS	8
+#define SMC_PARAM_W4_OFFS	16
+#define SMC_PARAM_W6_OFFS	24
+
+/* void smccc_call32(struct smccc_param32 *param) */
+ENTRY(smccc_call32)
+	stp	x28, x30, [sp, #-16]!
+	mov	x28, x0
+	ldp	w0, w1, [x28, #SMC_PARAM_W0_OFFS]
+	ldp	w2, w3, [x28, #SMC_PARAM_W2_OFFS]
+	ldp	w4, w5, [x28, #SMC_PARAM_W4_OFFS]
+	ldp	w6, w7, [x28, #SMC_PARAM_W6_OFFS]
+	smc	#0
+	stp	w0, w1, [x28, #SMC_PARAM_W0_OFFS]
+	stp	w2, w3, [x28, #SMC_PARAM_W2_OFFS]
+	ldp	x28, x30, [sp], #16
+	ret
+ENDPROC(smccc_call32)
arch/arm64/kernel/smccc.c new file +17 -0
new file mode 100644
index 0000000..ba4039e
@@ -0,0 +1,17 @@
+/*
+ * Copyright (c) 2015, Linaro Limited
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+#include <linux/export.h>
+#include <linux/arm-smccc.h>
+
+EXPORT_SYMBOL_GPL(smccc_call32);
include/linux/arm-smccc.h new file +80 -0

80 lines not on this page · raw message

Signature · 1 line
1.9.1
#2 lore raw

PATCH v4 2/5 dt/bindings: add bindings for optee

In reply to Jens Wiklander

Introduces optee prefix and adds bindings for ARM TrustZone based OP-TEE implementation.

---
 Documentation/devicetree/bindings/optee/optee.txt     | 17 +++++++++++++++++
 Documentation/devicetree/bindings/vendor-prefixes.txt |  1 +
 2 files changed, 18 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/optee/optee.txt
Documentation/devicetree/bindings/optee/optee.txt new file +17 -0
new file mode 100644
index 0000000..4a6ff10
@@ -0,0 +1,17 @@
+OP-TEE Device Tree Bindings
+
+OP-TEE is a piece of software using hardware features to provide a Trusted
+Execution Environment. The security can be provided with ARM TrustZone, but
+also by virtualization or a separate chip. As there's no single OP-TEE
+vendor we're using "optee" as the first part of compatible property,
+indicating the OP-TEE protocol is used when communicating with the secure
+world.
+
+* OP-TEE based on ARM TrustZone required properties:
+
+- compatible="optee,optee-tz"
+
+Example:
+	optee {
+		compatible="optee,optee-tz";
+	};
Documentation/devicetree/bindings/vendor-prefixes.txt +1 -0
index 8033919..17c2a7e 100644
@@ -141,6 +141,7 @@ nvidia	NVIDIA
 nxp	NXP Semiconductors
 onnn	ON Semiconductor Corp.
 opencores	OpenCores.org
+optee	OP-TEE, Open Portable Trusted Execution Environment
 ortustech	Ortus Technology Co., Ltd.
 ovti	OmniVision Technologies
 panasonic	Panasonic Corporation
Signature · 1 line
1.9.1
#3 lore raw

PATCH v4 3/5 tee: generic TEE subsystem

In reply to Jens Wiklander

Initial patch for generic TEE subsystem.
This subsystem provides:
* Registration/un-registration of TEE drivers.
* Shared memory between normal world and secure world.
* Ioctl interface for interaction with user space.

A TEE (Trusted Execution Environment) driver is a driver that interfaces with a trusted OS running in some secure environment, for example, TrustZone on ARM cpus, or a separate secure co-processor etc.

The TEE subsystem can serve a TEE driver for a Global Platform compliant TEE, but it's not limited to only Global Platform TEEs.

This patch builds on other similar implementations trying to solve the same problem:
* "optee_linuxdriver" by among others
Jean-michel DELORME<[email protected]> and Emmanuel MICHEL <[email protected]>
* "Generic TrustZone Driver" by Javier Gonz?lez <[email protected]>

---
 Documentation/ioctl/ioctl-number.txt |   1 +
 MAINTAINERS                          |   8 +
 drivers/Kconfig                      |   2 +
 drivers/Makefile                     |   1 +
 drivers/tee/Kconfig                  |   8 +
 drivers/tee/Makefile                 |   3 +
 drivers/tee/tee.c                    | 839 +++++++++++++++++++++++++++++++++++
 drivers/tee/tee_private.h            |  80 ++++
 drivers/tee/tee_shm.c                | 324 ++++++++++++++
 drivers/tee/tee_shm_pool.c           | 133 ++++++
 include/linux/tee_drv.h              | 306 +++++++++++++
 include/uapi/linux/tee.h             | 376 ++++++++++++++++
 12 files changed, 2081 insertions(+)
 create mode 100644 drivers/tee/Kconfig
 create mode 100644 drivers/tee/Makefile
 create mode 100644 drivers/tee/tee.c
 create mode 100644 drivers/tee/tee_private.h
 create mode 100644 drivers/tee/tee_shm.c
 create mode 100644 drivers/tee/tee_shm_pool.c
 create mode 100644 include/linux/tee_drv.h
 create mode 100644 include/uapi/linux/tee.h
Documentation/ioctl/ioctl-number.txt +1 -0
index 51f4221..f54dfc02 100644
@@ -301,6 +301,7 @@ Code  Seq#(hex)	Include File		Comments
 0xA3	80-8F	Port ACL		in development:
 					<mailto:[email protected]>
 0xA3	90-9F	linux/dtlk.h
+0xA4	00-1F	uapi/linux/tee.h	Generic TEE subsystem
 0xAB	00-1F	linux/nbd.h
 0xAC	00-1F	linux/raw.h
 0xAD	00	Netfilter device	in development:
MAINTAINERS +8 -0
index d8afd29..869635f 100644
@@ -8634,6 +8634,14 @@ S:	Maintained
 F:	include/linux/mmc/dw_mmc.h
 F:	drivers/mmc/host/dw_mmc*
 
+TEE SUBSYSTEM
+M:	Jens Wiklander <[email protected]>
+M:	Javier Gonz?lez <[email protected]>
+S:	Maintained
+F:	include/linux/tee_drv.h
+F:	include/uapi/linux/tee.h
+F:	drivers/tee/
+
 THUNDERBOLT DRIVER
 M:	Andreas Noever <[email protected]>
 S:	Maintained
drivers/Kconfig +2 -0
index c0cc96b..7510f69 100644
@@ -182,4 +182,6 @@ source "drivers/thunderbolt/Kconfig"
 
 source "drivers/android/Kconfig"
 
+source "drivers/tee/Kconfig"
+
 endmenu
drivers/Makefile +1 -0
index 46d2554..852f0af 100644
@@ -165,3 +165,4 @@ obj-$(CONFIG_RAS)		+= ras/
 obj-$(CONFIG_THUNDERBOLT)	+= thunderbolt/
 obj-$(CONFIG_CORESIGHT)		+= hwtracing/coresight/
 obj-$(CONFIG_ANDROID)		+= android/
+obj-$(CONFIG_TEE)		+= tee/
drivers/tee/Kconfig new file +8 -0
new file mode 100644
index 0000000..64a8cd7
@@ -0,0 +1,8 @@
+# Generic Trusted Execution Environment Configuration
+config TEE
+	bool "Trusted Execution Environment support"
+	default n
+	select DMA_SHARED_BUFFER
+	help
+	  This implements a generic interface towards a Trusted Execution
+	  Environment (TEE).
drivers/tee/Makefile new file +3 -0
new file mode 100644
index 0000000..60d2dab
@@ -0,0 +1,3 @@
+obj-y += tee.o
+obj-y += tee_shm.o
+obj-y += tee_shm_pool.o
drivers/tee/tee.c new file +839 -0

839 lines not on this page · raw message

drivers/tee/tee_private.h new file +80 -0

80 lines not on this page · raw message

drivers/tee/tee_shm.c new file +324 -0

324 lines not on this page · raw message

drivers/tee/tee_shm_pool.c new file +133 -0

133 lines not on this page · raw message

include/linux/tee_drv.h new file +306 -0

306 lines not on this page · raw message

include/uapi/linux/tee.h new file +376 -0

376 lines not on this page · raw message

Signature · 1 line
1.9.1
#16 lore raw

PATCH v4 4/5 tee: add OP-TEE driver

In reply to Jens Wiklander

Adds a OP-TEE driver which also can be compiled as a loadable module.

* Targets ARM and ARM64
* Supports using reserved memory from OP-TEE as shared memory
* Probes OP-TEE version using SMCs
* Accepts requests on privileged and unprivileged device
* Uses OPTEE message protocol version 2 to communicate with secure world

---
 MAINTAINERS                       |   5 +
 drivers/tee/Kconfig               |  10 +
 drivers/tee/Makefile              |   1 +
 drivers/tee/optee/Kconfig         |   8 +
 drivers/tee/optee/Makefile        |   5 +
 drivers/tee/optee/call.c          | 390 +++++++++++++++++++++++++++++++
 drivers/tee/optee/core.c          | 451 ++++++++++++++++++++++++++++++++++++
 drivers/tee/optee/optee_msg.h     | 334 +++++++++++++++++++++++++++
 drivers/tee/optee/optee_private.h | 129 +++++++++++
 drivers/tee/optee/optee_smc.h     | 466 ++++++++++++++++++++++++++++++++++++++
 drivers/tee/optee/rpc.c           | 248 ++++++++++++++++++++
 drivers/tee/optee/supp.c          | 212 +++++++++++++++++
 12 files changed, 2259 insertions(+)
 create mode 100644 drivers/tee/optee/Kconfig
 create mode 100644 drivers/tee/optee/Makefile
 create mode 100644 drivers/tee/optee/call.c
 create mode 100644 drivers/tee/optee/core.c
 create mode 100644 drivers/tee/optee/optee_msg.h
 create mode 100644 drivers/tee/optee/optee_private.h
 create mode 100644 drivers/tee/optee/optee_smc.h
 create mode 100644 drivers/tee/optee/rpc.c
 create mode 100644 drivers/tee/optee/supp.c
MAINTAINERS +5 -0
index 869635f..2df3f03 100644
@@ -7314,6 +7314,11 @@ F:	arch/*/oprofile/
 F:	drivers/oprofile/
 F:	include/linux/oprofile.h
 
+OP-TEE DRIVER
+M:	Jens Wiklander <[email protected]>
+S:	Maintained
+F:	drivers/tee/optee/
+
 ORACLE CLUSTER FILESYSTEM 2 (OCFS2)
 M:	Mark Fasheh <[email protected]>
 M:	Joel Becker <[email protected]>
drivers/tee/Kconfig +10 -0
index 64a8cd7..b269276 100644
@@ -6,3 +6,13 @@ config TEE
 	help
 	  This implements a generic interface towards a Trusted Execution
 	  Environment (TEE).
+
+if TEE
+
+menu "TEE drivers"
+
+source "drivers/tee/optee/Kconfig"
+
+endmenu
+
+endif
drivers/tee/Makefile +1 -0
index 60d2dab..53f3c76 100644
@@ -1,3 +1,4 @@
 obj-y += tee.o
 obj-y += tee_shm.o
 obj-y += tee_shm_pool.o
+obj-$(CONFIG_OPTEE) += optee/
drivers/tee/optee/Kconfig new file +8 -0
new file mode 100644
index 0000000..b550b18
@@ -0,0 +1,8 @@
+# OP-TEE Trusted Execution Environment Configuration
+config OPTEE
+	tristate "OP-TEE"
+	default n
+	depends on HAVE_SMCCC
+	help
+	  This implements the OP-TEE Trusted Execution Environment (TEE)
+	  driver.
drivers/tee/optee/Makefile new file +5 -0
new file mode 100644
index 0000000..92fe578
@@ -0,0 +1,5 @@
+obj-$(CONFIG_OPTEE) += optee.o
+optee-objs += core.o
+optee-objs += call.o
+optee-objs += rpc.o
+optee-objs += supp.o
drivers/tee/optee/call.c new file +390 -0

390 lines not on this page · raw message

drivers/tee/optee/core.c new file +451 -0

451 lines not on this page · raw message

drivers/tee/optee/optee_msg.h new file +334 -0

334 lines not on this page · raw message

drivers/tee/optee/optee_private.h new file +129 -0

129 lines not on this page · raw message

drivers/tee/optee/optee_smc.h new file +466 -0

466 lines not on this page · raw message

drivers/tee/optee/rpc.c new file +248 -0

248 lines not on this page · raw message

drivers/tee/optee/supp.c new file +212 -0

212 lines not on this page · raw message

Signature · 1 line
1.9.1
#17 lore raw

PATCH v4 5/5 Documentation: tee subsystem and op-tee driver

In reply to Jens Wiklander
---
 Documentation/00-INDEX |   2 +
 Documentation/tee.txt  | 117 +++++++++++++++++++++++++++++++++++++++++++++++++
 MAINTAINERS            |   1 +
 3 files changed, 120 insertions(+)
 create mode 100644 Documentation/tee.txt
Documentation/00-INDEX +2 -0
index cd077ca..bd3f803 100644
@@ -435,6 +435,8 @@ sysrq.txt
 	- info on the magic SysRq key.
 target/
 	- directory with info on generating TCM v4 fabric .ko modules
+tee.txt
+	- info on the TEE subsystem and drivers
 this_cpu_ops.txt
 	- List rationale behind and the way to use this_cpu operations.
 thermal/
Documentation/tee.txt new file +117 -0

117 lines not on this page · raw message

MAINTAINERS +1 -0
index 2df3f03..a48d718 100644
@@ -8646,6 +8646,7 @@ S:	Maintained
 F:	include/linux/tee_drv.h
 F:	include/uapi/linux/tee.h
 F:	drivers/tee/
+F:	Documentation/tee.txt
 
 THUNDERBOLT DRIVER
 M:	Andreas Noever <[email protected]>
Signature · 1 line
1.9.1
#4 lore raw

Re: [PATCH v4 3/5] tee: generic TEE subsystem

In reply to Jens Wiklander
Quoted 20 lines · Jens Wiklander
drivers/tee/tee.c new file +20 -0
+static void tee_device_complete_unused(struct kref *kref)
+{
+	struct tee_device *teedev;
+
+	teedev = container_of(kref, struct tee_device, users);
+	/* When the mutex is released, no other tee_device_get() will succeed */
+	teedev->desc = NULL;
+	complete(&teedev->c_no_users);
+}
+
+void tee_device_put(struct tee_device *teedev)
+{
+	mutex_lock(&teedev->mutex);
+	/* Shouldn't put in this state */
+	if (!WARN_ON(!teedev->desc))
+		kref_put(&teedev->users, tee_device_complete_unused);
+	mutex_unlock(&teedev->mutex);
+}
+
+bool tee_device_get(struct tee_device *teedev)

drivers/tee/tee.c line 787

Quoted 10 lines
drivers/tee/tee.c new file +10 -0
+{
+	mutex_lock(&teedev->mutex);
+	if (!teedev->desc) {
+		mutex_unlock(&teedev->mutex);
+		return false;
+	}
+	kref_get(&teedev->users);
+	mutex_unlock(&teedev->mutex);
+	return true;
+}

If you are holding the mutex then you don't really need a kref, just a simple active count counter.

I've been a bit learly lately about seeing krefs used for something other than kfree, I've seen a few subtle mistakes in those schemes - yours looks OK, only because of the lock, and the lock makes the kref redundant..

drivers/tee/tee.c line 691

Quoted 2 lines
drivers/tee/tee.c new file +2 -0
+       cdev_init(&teedev->cdev, &tee_fops);
+       teedev->cdev.owner = teedesc->owner;

This also needs to set teedev->cdev.kobj.parent.
I'm guessing:

teedev->cdev.kobj.parent = &teedev->dev.kobj;

TPM had the same mistake..

drivers/tee/tee.c line 792

Quoted 4 lines
drivers/tee/tee.c new file +4 -0
+void tee_device_unregister(struct tee_device *teedev)
+{
+	if (IS_ERR_OR_NULL(teedev))
+		return;

See for some general colour on IS_ERR_OR_NULL

https://www.mail-archive.com/linux-omap at vger.kernel.org/msg78030.html

IMHO, you should never, store an ERR pointer into long term storage, so I wonder why this is like this...

drivers/tee/tee.c line 800

Quoted 7 lines
drivers/tee/tee.c new file +7 -0
+	if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED) {
+		cdev_del(&teedev->cdev);
+		device_del(&teedev->dev);
+	}
+
+	tee_device_put(teedev);
+	wait_for_completion(&teedev->c_no_users);

Generally in a scheme like this we'd see open and release get/put the underlying module handle to prevent driver removal while the char dev is open. Otherwise module removal will hang here.

Jason

#5 lore raw

Re: [PATCH v4 3/5] tee: generic TEE subsystem

Quoted 44 lines · Jason Gunthorpe
Quoted 20 lines · Jens Wiklander
drivers/tee/tee.c new file +20 -0
+static void tee_device_complete_unused(struct kref *kref)
+{
+	struct tee_device *teedev;
+
+	teedev = container_of(kref, struct tee_device, users);
+	/* When the mutex is released, no other tee_device_get() will succeed */
+	teedev->desc = NULL;
+	complete(&teedev->c_no_users);
+}
+
+void tee_device_put(struct tee_device *teedev)
+{
+	mutex_lock(&teedev->mutex);
+	/* Shouldn't put in this state */
+	if (!WARN_ON(!teedev->desc))
+		kref_put(&teedev->users, tee_device_complete_unused);
+	mutex_unlock(&teedev->mutex);
+}
+
+bool tee_device_get(struct tee_device *teedev)

drivers/tee/tee.c line 787

Quoted 10 lines
drivers/tee/tee.c new file +10 -0
+{
+	mutex_lock(&teedev->mutex);
+	if (!teedev->desc) {
+		mutex_unlock(&teedev->mutex);
+		return false;
+	}
+	kref_get(&teedev->users);
+	mutex_unlock(&teedev->mutex);
+	return true;
+}

If you are holding the mutex then you don't really need a kref, just a simple active count counter.

I've been a bit learly lately about seeing krefs used for something other than kfree, I've seen a few subtle mistakes in those schemes - yours looks OK, only because of the lock, and the lock makes the kref redundant..

drivers/tee/tee.c line 691

Quoted 2 lines
drivers/tee/tee.c new file +2 -0
+       cdev_init(&teedev->cdev, &tee_fops);
+       teedev->cdev.owner = teedesc->owner;
Quoted 6 lines

This also needs to set teedev->cdev.kobj.parent.
I'm guessing:

teedev->cdev.kobj.parent = &teedev->dev.kobj;

TPM had the same mistake..

Really? As of a few years ago, A cdev's kobject should not be touched by anything other than the cdev core. It's not a "real" kobject in that it is never registered in sysfs, and no one sees it. I keep meaning to just use something else one of these days for that structure, as lots of people get it wrong. Or has things changed there?

thanks,

greg k-h

Acked-by Al Viro
#6 lore raw

Re: [PATCH v4 3/5] tee: generic TEE subsystem

Quoted 6 lines · Greg Kroah-Hartman
Quoted 6 lines

drivers/tee/tee.c line 691

Quoted 2 lines
drivers/tee/tee.c new file +2 -0
+       cdev_init(&teedev->cdev, &tee_fops);
+       teedev->cdev.owner = teedesc->owner;

This also needs to set teedev->cdev.kobj.parent.
I'm guessing:

Quoted 7 lines
Quoted 3 lines

teedev->cdev.kobj.parent = &teedev->dev.kobj;

TPM had the same mistake..

Really? As of a few years ago, A cdev's kobject should not be touched by anything other than the cdev core. It's not a "real" kobject in that it is never registered in sysfs, and no one sees it. I keep meaning to

Well, when I looked at it, it looked like it was necessary to maintain the refcount on the memory that is holding cdev.

The basic issue is that cdev_del doesn't seem to be synchronizing.

The use after free race is then something like:

struct tpm_chip {

 	struct device dev;
	struct cdev cdev;

       CPU0                            CPU1

================= ======================
tpm_chip = kalloc
cdev_add(&tpm_chip->cdev)
device_add(&tpm_chip->dev)

                                chrdev_open
		                 filp->f_op->open

cdev_del(&tpm_chip->cdev)
device_unregister
(&tpm_chip->dev)
kfree(tpm_chip)

		                  tpm_chip = container_of
				 fput
				  cdev_put(.. cdev)

Ie we need cdev to hold a ref on tpm_chip->dev until cdev_put is called.

Quoted 2 lines

just use something else one of these days for that structure, as lots of people get it wrong. Or has things changed there?

Not recently, but this is the commit:

commit 2f0157f13f42800aa3d9017ebb0fb80a65f7b2de

Author: Dmitry Torokhov <[email protected]>
Date:   Sun Oct 21 17:57:19 2012 -0700

char_dev: pin parent kobject

In certain cases (for example when a cdev structure is embedded into another object whose lifetime is controlled by a separate kobject) it is beneficial to tie lifetime of another object to the lifetime of character device so that related object is not freed until after char_dev object is freed.

To achieve this let's pin kobject's parent when doing cdev_add() and unpin when last reference to cdev structure is being released.

It doesn't seem the be the best situation, this is the 3rd time this week I've noticed cdev with a kalloc'd struct being used improperly.

Perhaps cdev_init should accept the module and kref parent as an argument?

Jason

#7 lore raw

Re: [PATCH v4 3/5] tee: generic TEE subsystem

Quoted 18 lines · Jason Gunthorpe
Quoted 6 lines · Greg Kroah-Hartman
Quoted 6 lines

drivers/tee/tee.c line 691

Quoted 2 lines
drivers/tee/tee.c new file +2 -0
+       cdev_init(&teedev->cdev, &tee_fops);
+       teedev->cdev.owner = teedesc->owner;

This also needs to set teedev->cdev.kobj.parent.
I'm guessing:

Quoted 7 lines
Quoted 3 lines

teedev->cdev.kobj.parent = &teedev->dev.kobj;

TPM had the same mistake..

Really? As of a few years ago, A cdev's kobject should not be touched by anything other than the cdev core. It's not a "real" kobject in that it is never registered in sysfs, and no one sees it. I keep meaning to

Well, when I looked at it, it looked like it was necessary to maintain the refcount on the memory that is holding cdev.

Quoted 7 lines

The basic issue is that cdev_del doesn't seem to be synchronizing.

The use after free race is then something like:

   struct tpm_chip {
 	struct device dev;
	struct cdev cdev;

Oops, right there's your problem. You can't have two reference counted objects trying to manage the memory of a single structure. No matter what you do, it's going to be a pain to deal with this, so don't :)

Quoted 12 lines

CPU0 CPU1
================= ======================
tpm_chip = kalloc
cdev_add(&tpm_chip->cdev)
device_add(&tpm_chip->dev)

                                chrdev_open
		                 filp->f_op->open

cdev_del(&tpm_chip->cdev)
device_unregister
(&tpm_chip->dev)
kfree(tpm_chip)

Quoted 6 lines
		                  tpm_chip = container_of
				 fput
				  cdev_put(.. cdev)

Ie we need cdev to hold a ref on tpm_chip->dev until cdev_put is called.

No, separate them, make the cdev a pointer and all should be fine.

Quoted 24 lines
Quoted 2 lines

just use something else one of these days for that structure, as lots of people get it wrong. Or has things changed there?

Not recently, but this is the commit:

commit 2f0157f13f42800aa3d9017ebb0fb80a65f7b2de

Author: Dmitry Torokhov <[email protected]>
Date:   Sun Oct 21 17:57:19 2012 -0700

char_dev: pin parent kobject

In certain cases (for example when a cdev structure is embedded into another object whose lifetime is controlled by a separate kobject) it is beneficial to tie lifetime of another object to the lifetime of character device so that related object is not freed until after char_dev object is freed.

To achieve this let's pin kobject's parent when doing cdev_add() and unpin when last reference to cdev structure is being released.

Quoted 5 lines

It doesn't seem the be the best situation, this is the 3rd time this week I've noticed cdev with a kalloc'd struct being used improperly.

Perhaps cdev_init should accept the module and kref parent as an argument?

Oh yeah, that commit :(

If you know _exactly_ what you are doing, you can get away with this, but I strongly recommend not doing that. As proof of that, in some new code I'm working on, I did not do this, just because I'm not smart enough to ensure it's all working properly...

thanks,

greg k-h

#8 lore raw

Re: [PATCH v4 3/5] tee: generic TEE subsystem

Quoted 11 lines · Greg Kroah-Hartman
Quoted 7 lines

The basic issue is that cdev_del doesn't seem to be synchronizing.

The use after free race is then something like:

   struct tpm_chip {
 	struct device dev;
	struct cdev cdev;

Oops, right there's your problem. You can't have two reference counted objects trying to manage the memory of a single structure. No matter what you do, it's going to be a pain to deal with this, so don't :)

Sure, generally, yes, but that isn't done for no reason, it is to make open straightforward:

static int tpm_open(struct inode *inode, struct file *file)

{
	struct tpm_chip *chip =
		container_of(inode->i_cdev, struct tpm_chip, cdev);

We need to recover the tpm_chip associated with the char device node, in a way that is holding a kref on it, without racing with cdev_del/etc

This scheme does mean that if we have a struct file we have a kref on the cdev, and if we have cdev then we have a kref on the tpm_chip, which is really easy to use properly.

Quoted 4 lines
Quoted 2 lines

Ie we need cdev to hold a ref on tpm_chip->dev until cdev_put is called.

No, separate them, make the cdev a pointer and all should be fine.

Okay, cdev_alloc takes care of the cdev lifetime.

Do you have a simple solution to replace container_of as well?

What would you think about something like:

cdev_alloc(&chip->dev.kref)

?

Jason

#11 lore raw

Re: [PATCH v4 3/5] tee: generic TEE subsystem

In reply to Greg Kroah-Hartman
Quoted 82 lines · Greg Kroah-Hartman
Quoted 18 lines · Jason Gunthorpe
Quoted 6 lines · Greg Kroah-Hartman
Quoted 6 lines

drivers/tee/tee.c line 691

Quoted 2 lines
drivers/tee/tee.c new file +2 -0
+       cdev_init(&teedev->cdev, &tee_fops);
+       teedev->cdev.owner = teedesc->owner;

This also needs to set teedev->cdev.kobj.parent.
I'm guessing:

Quoted 7 lines
Quoted 3 lines

teedev->cdev.kobj.parent = &teedev->dev.kobj;

TPM had the same mistake..

Really? As of a few years ago, A cdev's kobject should not be touched by anything other than the cdev core. It's not a "real" kobject in that it is never registered in sysfs, and no one sees it. I keep meaning to

Well, when I looked at it, it looked like it was necessary to maintain the refcount on the memory that is holding cdev.

Quoted 7 lines

The basic issue is that cdev_del doesn't seem to be synchronizing.

The use after free race is then something like:

   struct tpm_chip {
 	struct device dev;
	struct cdev cdev;

Oops, right there's your problem. You can't have two reference counted objects trying to manage the memory of a single structure. No matter what you do, it's going to be a pain to deal with this, so don't :)

Quoted 12 lines

CPU0 CPU1
================= ======================
tpm_chip = kalloc
cdev_add(&tpm_chip->cdev)
device_add(&tpm_chip->dev)

                                chrdev_open
		                 filp->f_op->open

cdev_del(&tpm_chip->cdev)
device_unregister
(&tpm_chip->dev)
kfree(tpm_chip)

Quoted 6 lines
		                  tpm_chip = container_of
				 fput
				  cdev_put(.. cdev)

Ie we need cdev to hold a ref on tpm_chip->dev until cdev_put is called.

No, separate them, make the cdev a pointer and all should be fine.

Quoted 24 lines
Quoted 2 lines

just use something else one of these days for that structure, as lots of people get it wrong. Or has things changed there?

Not recently, but this is the commit:

commit 2f0157f13f42800aa3d9017ebb0fb80a65f7b2de

Author: Dmitry Torokhov <[email protected]>
Date:   Sun Oct 21 17:57:19 2012 -0700

char_dev: pin parent kobject

In certain cases (for example when a cdev structure is embedded into another object whose lifetime is controlled by a separate kobject) it is beneficial to tie lifetime of another object to the lifetime of character device so that related object is not freed until after char_dev object is freed.

To achieve this let's pin kobject's parent when doing cdev_add() and unpin when last reference to cdev structure is being released.

Quoted 5 lines

It doesn't seem the be the best situation, this is the 3rd time this week I've noticed cdev with a kalloc'd struct being used improperly.

Perhaps cdev_init should accept the module and kref parent as an argument?

Quoted 6 lines

Oh yeah, that commit :(

If you know _exactly_ what you are doing, you can get away with this, but I strongly recommend not doing that. As proof of that, in some new code I'm working on, I did not do this, just because I'm not smart enough to ensure it's all working properly...

I know you like to allocate everything separately and access it via pointers (ala device_create) but cdevs explicitly allow embedding them into other structures (cdev_init vs cdev_alloc). I do not think there is anything wrong with this, as well as there is nothing wrong in embedding a struct device into other structures, but it does require coordinating lifetime rules and selecting a "master" kobject. I think having cdev_init accept such "master" kobject would bring author's attention to the issue and avoid such mistakes in the future.

Thanks.

Signature · 1 line
Dmitry
#12 lore raw

Re: [PATCH v4 3/5] tee: generic TEE subsystem

Quoted 90 lines · Dmitry Torokhov
Quoted 82 lines · Greg Kroah-Hartman
Quoted 18 lines · Jason Gunthorpe
Quoted 6 lines · Greg Kroah-Hartman
Quoted 6 lines

drivers/tee/tee.c line 691

Quoted 2 lines
drivers/tee/tee.c new file +2 -0
+       cdev_init(&teedev->cdev, &tee_fops);
+       teedev->cdev.owner = teedesc->owner;

This also needs to set teedev->cdev.kobj.parent.
I'm guessing:

Quoted 7 lines
Quoted 3 lines

teedev->cdev.kobj.parent = &teedev->dev.kobj;

TPM had the same mistake..

Really? As of a few years ago, A cdev's kobject should not be touched by anything other than the cdev core. It's not a "real" kobject in that it is never registered in sysfs, and no one sees it. I keep meaning to

Well, when I looked at it, it looked like it was necessary to maintain the refcount on the memory that is holding cdev.

Quoted 7 lines

The basic issue is that cdev_del doesn't seem to be synchronizing.

The use after free race is then something like:

   struct tpm_chip {
 	struct device dev;
	struct cdev cdev;

Oops, right there's your problem. You can't have two reference counted objects trying to manage the memory of a single structure. No matter what you do, it's going to be a pain to deal with this, so don't :)

Quoted 12 lines

CPU0 CPU1
================= ======================
tpm_chip = kalloc
cdev_add(&tpm_chip->cdev)
device_add(&tpm_chip->dev)

                                chrdev_open
		                 filp->f_op->open

cdev_del(&tpm_chip->cdev)
device_unregister
(&tpm_chip->dev)
kfree(tpm_chip)

Quoted 6 lines
		                  tpm_chip = container_of
				 fput
				  cdev_put(.. cdev)

Ie we need cdev to hold a ref on tpm_chip->dev until cdev_put is called.

No, separate them, make the cdev a pointer and all should be fine.

Quoted 24 lines
Quoted 2 lines

just use something else one of these days for that structure, as lots of people get it wrong. Or has things changed there?

Not recently, but this is the commit:

commit 2f0157f13f42800aa3d9017ebb0fb80a65f7b2de

Author: Dmitry Torokhov <[email protected]>
Date:   Sun Oct 21 17:57:19 2012 -0700

char_dev: pin parent kobject

In certain cases (for example when a cdev structure is embedded into another object whose lifetime is controlled by a separate kobject) it is beneficial to tie lifetime of another object to the lifetime of character device so that related object is not freed until after char_dev object is freed.

To achieve this let's pin kobject's parent when doing cdev_add() and unpin when last reference to cdev structure is being released.

Quoted 5 lines

It doesn't seem the be the best situation, this is the 3rd time this week I've noticed cdev with a kalloc'd struct being used improperly.

Perhaps cdev_init should accept the module and kref parent as an argument?

Quoted 6 lines

Oh yeah, that commit :(

If you know _exactly_ what you are doing, you can get away with this, but I strongly recommend not doing that. As proof of that, in some new code I'm working on, I did not do this, just because I'm not smart enough to ensure it's all working properly...

Quoted 8 lines

I know you like to allocate everything separately and access it via pointers (ala device_create) but cdevs explicitly allow embedding them into other structures (cdev_init vs cdev_alloc). I do not think there is anything wrong with this, as well as there is nothing wrong in embedding a struct device into other structures, but it does require coordinating lifetime rules and selecting a "master" kobject. I think having cdev_init accept such "master" kobject would bring author's attention to the issue and avoid such mistakes in the future.

Embedding cdevs into other structures is great, I like that. What I don't like is having two different reference counts for the same structure based on the lifetime rules of two different embedded structures. That's a very difficult thing to get right and I would argue, is something that should almost never be done.

thanks,

greg k-h

#9 lore raw

Re: [PATCH v4 3/5] tee: generic TEE subsystem

In reply to Jason Gunthorpe
Quoted 34 lines · Jason Gunthorpe
Quoted 11 lines · Greg Kroah-Hartman
Quoted 7 lines

The basic issue is that cdev_del doesn't seem to be synchronizing.

The use after free race is then something like:

   struct tpm_chip {
 	struct device dev;
	struct cdev cdev;

Oops, right there's your problem. You can't have two reference counted objects trying to manage the memory of a single structure. No matter what you do, it's going to be a pain to deal with this, so don't :)

Sure, generally, yes, but that isn't done for no reason, it is to make open straightforward:

static int tpm_open(struct inode *inode, struct file *file)

{
	struct tpm_chip *chip =
		container_of(inode->i_cdev, struct tpm_chip, cdev);

We need to recover the tpm_chip associated with the char device node, in a way that is holding a kref on it, without racing with cdev_del/etc

This scheme does mean that if we have a struct file we have a kref on the cdev, and if we have cdev then we have a kref on the tpm_chip, which is really easy to use properly.

Quoted 4 lines
Quoted 2 lines

Ie we need cdev to hold a ref on tpm_chip->dev until cdev_put is called.

No, separate them, make the cdev a pointer and all should be fine.

Quoted 7 lines

Okay, cdev_alloc takes care of the cdev lifetime.

Do you have a simple solution to replace container_of as well?

What would you think about something like:

cdev_alloc(&chip->dev.kref)

Just pick either the cdev to handle the lifetime rules, or the struct device, you'll still need a container_of(). Just don't do both as odds are the lifetime rules is going to get really hard to debug and ensure that everything is correct on the shutdown/release path.

thanks,

greg k-h

#10 lore raw

Re: [PATCH v4 3/5] tee: generic TEE subsystem

Quoted 39 lines · Greg Kroah-Hartman
Quoted 38 lines · Jason Gunthorpe
Quoted 11 lines · Greg Kroah-Hartman
Quoted 7 lines

The basic issue is that cdev_del doesn't seem to be synchronizing.

The use after free race is then something like:

   struct tpm_chip {
 	struct device dev;
	struct cdev cdev;

Oops, right there's your problem. You can't have two reference counted objects trying to manage the memory of a single structure. No matter what you do, it's going to be a pain to deal with this, so don't :)

Sure, generally, yes, but that isn't done for no reason, it is to make open straightforward:

static int tpm_open(struct inode *inode, struct file *file)

{
	struct tpm_chip *chip =
		container_of(inode->i_cdev, struct tpm_chip, cdev);

We need to recover the tpm_chip associated with the char device node, in a way that is holding a kref on it, without racing with cdev_del/etc

This scheme does mean that if we have a struct file we have a kref on the cdev, and if we have cdev then we have a kref on the tpm_chip, which is really easy to use properly.

Quoted 4 lines
Quoted 2 lines

Ie we need cdev to hold a ref on tpm_chip->dev until cdev_put is called.

No, separate them, make the cdev a pointer and all should be fine.

Okay, cdev_alloc takes care of the cdev lifetime.

Do you have a simple solution to replace container_of as well?

Quoted 8 lines
Quoted 3 lines

What would you think about something like:

cdev_alloc(&chip->dev.kref)

Just pick either the cdev to handle the lifetime rules, or the struct device, you'll still need a container_of(). Just don't do both as odds are the lifetime rules is going to get really hard to debug and ensure that everything is correct on the shutdown/release path.

It is not that simple. The issue is that we need to ensure that cdev does not outlive the structure that represents the device, otherwise it may disappear while cdev code tries to access it. So we need a way to pin the "main" structure in memory until cdev is gone. But as I mentioned in [1] cdev is "sealed" and does not give us a way of hooking into add/release so that we can try to pin the object we need. And given that the main driver model primitive is kobject even if we "unseal" cdev everyone will end up doing pretty much what the above commit did.

Thanks.

Signature · 3 lines
Dmitry

[1] http://www.spinics.net/lists/kernel/msg1421595.html
#13 lore raw

Re: [PATCH v4 3/5] tee: generic TEE subsystem

In reply to Greg Kroah-Hartman
Quoted 100 lines · Greg Kroah-Hartman
Quoted 90 lines · Dmitry Torokhov
Quoted 82 lines · Greg Kroah-Hartman
Quoted 18 lines · Jason Gunthorpe
Quoted 6 lines · Greg Kroah-Hartman
Quoted 6 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+       cdev_init(&teedev->cdev, &tee_fops);
+       teedev->cdev.owner = teedesc->owner;

This also needs to set teedev->cdev.kobj.parent.
I'm guessing:

Quoted 7 lines
Quoted 3 lines

teedev->cdev.kobj.parent = &teedev->dev.kobj;

TPM had the same mistake..

Really? As of a few years ago, A cdev's kobject should not be touched by anything other than the cdev core. It's not a "real" kobject in that it is never registered in sysfs, and no one sees it. I keep meaning to

Well, when I looked at it, it looked like it was necessary to maintain the refcount on the memory that is holding cdev.

Quoted 7 lines

The basic issue is that cdev_del doesn't seem to be synchronizing.

The use after free race is then something like:

   struct tpm_chip {
 	struct device dev;
	struct cdev cdev;

Oops, right there's your problem. You can't have two reference counted objects trying to manage the memory of a single structure. No matter what you do, it's going to be a pain to deal with this, so don't :)

Quoted 12 lines

CPU0 CPU1
================= ======================
tpm_chip = kalloc
cdev_add(&tpm_chip->cdev)
device_add(&tpm_chip->dev)

                                chrdev_open
		                 filp->f_op->open

cdev_del(&tpm_chip->cdev)
device_unregister
(&tpm_chip->dev)
kfree(tpm_chip)

Quoted 6 lines
		                  tpm_chip = container_of
				 fput
				  cdev_put(.. cdev)

Ie we need cdev to hold a ref on tpm_chip->dev until cdev_put is called.

No, separate them, make the cdev a pointer and all should be fine.

Quoted 24 lines
Quoted 2 lines

just use something else one of these days for that structure, as lots of people get it wrong. Or has things changed there?

Not recently, but this is the commit:

commit 2f0157f13f42800aa3d9017ebb0fb80a65f7b2de

Author: Dmitry Torokhov <[email protected]>
Date:   Sun Oct 21 17:57:19 2012 -0700

char_dev: pin parent kobject

In certain cases (for example when a cdev structure is embedded into another object whose lifetime is controlled by a separate kobject) it is beneficial to tie lifetime of another object to the lifetime of character device so that related object is not freed until after char_dev object is freed.

To achieve this let's pin kobject's parent when doing cdev_add() and unpin when last reference to cdev structure is being released.

Quoted 5 lines

It doesn't seem the be the best situation, this is the 3rd time this week I've noticed cdev with a kalloc'd struct being used improperly.

Perhaps cdev_init should accept the module and kref parent as an argument?

Quoted 6 lines

Oh yeah, that commit :(

If you know _exactly_ what you are doing, you can get away with this, but I strongly recommend not doing that. As proof of that, in some new code I'm working on, I did not do this, just because I'm not smart enough to ensure it's all working properly...

Quoted 8 lines

I know you like to allocate everything separately and access it via pointers (ala device_create) but cdevs explicitly allow embedding them into other structures (cdev_init vs cdev_alloc). I do not think there is anything wrong with this, as well as there is nothing wrong in embedding a struct device into other structures, but it does require coordinating lifetime rules and selecting a "master" kobject. I think having cdev_init accept such "master" kobject would bring author's attention to the issue and avoid such mistakes in the future.

Quoted 5 lines

Embedding cdevs into other structures is great, I like that. What I don't like is having two different reference counts for the same structure based on the lifetime rules of two different embedded structures. That's a very difficult thing to get right and I would argue, is something that should almost never be done.

I think if we stop thinking about reference counts in the context of releasing memory that will clear the issue. The reference count for cdev is for cdev alone. The reference count for struct device is for the struct device. What happens when either one or both of them drop to 0 is up to the driver controlling the objects.

Thanks.

Signature · 1 line
Dmitry
#14 lore raw

Re: [PATCH v4 3/5] tee: generic TEE subsystem

In reply to Jason Gunthorpe
Quoted 36 lines · Jason Gunthorpe
Quoted 20 lines · Jens Wiklander
drivers/tee/tee.c new file +20 -0
+static void tee_device_complete_unused(struct kref *kref)
+{
+	struct tee_device *teedev;
+
+	teedev = container_of(kref, struct tee_device, users);
+	/* When the mutex is released, no other tee_device_get() will succeed */
+	teedev->desc = NULL;
+	complete(&teedev->c_no_users);
+}
+
+void tee_device_put(struct tee_device *teedev)
+{
+	mutex_lock(&teedev->mutex);
+	/* Shouldn't put in this state */
+	if (!WARN_ON(!teedev->desc))
+		kref_put(&teedev->users, tee_device_complete_unused);
+	mutex_unlock(&teedev->mutex);
+}
+
+bool tee_device_get(struct tee_device *teedev)

drivers/tee/tee.c line 787

Quoted 10 lines
drivers/tee/tee.c new file +10 -0
+{
+	mutex_lock(&teedev->mutex);
+	if (!teedev->desc) {
+		mutex_unlock(&teedev->mutex);
+		return false;
+	}
+	kref_get(&teedev->users);
+	mutex_unlock(&teedev->mutex);
+	return true;
+}

If you are holding the mutex then you don't really need a kref, just a simple active count counter.

Quoted 4 lines

I've been a bit learly lately about seeing krefs used for something other than kfree, I've seen a few subtle mistakes in those schemes - yours looks OK, only because of the lock, and the lock makes the kref redundant..

Thanks, I'll fix.

Quoted 10 lines

drivers/tee/tee.c line 691

Quoted 3 lines
drivers/tee/tee.c new file +2 -0
+       cdev_init(&teedev->cdev, &tee_fops);
+       teedev->cdev.owner = teedesc->owner;

This also needs to set teedev->cdev.kobj.parent.
I'm guessing:

teedev->cdev.kobj.parent = &teedev->dev.kobj;

TPM had the same mistake..

OK. This triggered a discussion, from what I understand the outcome is that what you're suggesting is the right thing to do here.

Quoted 8 lines

drivers/tee/tee.c line 792

Quoted 5 lines
drivers/tee/tee.c new file +4 -0
+void tee_device_unregister(struct tee_device *teedev)
+{
+	if (IS_ERR_OR_NULL(teedev))
+		return;

See for some general colour on IS_ERR_OR_NULL

Quoted 4 lines

https://www.mail-archive.com/linux-omap at vger.kernel.org/msg78030.html

IMHO, you should never, store an ERR pointer into long term storage, so I wonder why this is like this...

OK, I'll replace the IS_ERR_OR_NULL with !teedev and only store a real pointer or NULL here.

Quoted 12 lines

drivers/tee/tee.c line 800

Quoted 8 lines
drivers/tee/tee.c new file +7 -0
+	if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED) {
+		cdev_del(&teedev->cdev);
+		device_del(&teedev->dev);
+	}
+
+	tee_device_put(teedev);
+	wait_for_completion(&teedev->c_no_users);

Generally in a scheme like this we'd see open and release get/put the underlying module handle to prevent driver removal while the char dev is open. Otherwise module removal will hang here.

I'm perhaps misunderstanding you. While the cdev has any open file descriptors rmmod will fail with "Resource temporarily unavailable" because of fops_get() in chrdev_open().

tee_device_get()/put() deals with detaching of the driver, I see no other way than blocking here once the detaching has been triggered.

--
Thanks,
Jens

#15 lore raw

Re: [PATCH v4 3/5] tee: generic TEE subsystem

Quoted 7 lines · Jens Wiklander
Quoted 3 lines

Generally in a scheme like this we'd see open and release get/put the underlying module handle to prevent driver removal while the char dev is open. Otherwise module removal will hang here.

I'm perhaps misunderstanding you. While the cdev has any open file descriptors rmmod will fail with "Resource temporarily unavailable" because of fops_get() in chrdev_open().

Hmm, I see, you've done this:

+ cdev_init(&teedev->cdev, &tee_fops);
+ teedev->cdev.owner = teedesc->owner;

And owner here is the driver module? Interesting.. Looks OK..

Jason