[1/3] optee: add per cpu asynchronous notification

Message ID 20230112145424.3791276-2-etienne.carriere@linaro.org
State New
Headers
Series optee: async notif with PPI + interrupt provider |

Commit Message

Etienne Carriere Jan. 12, 2023, 2:54 p.m. UTC
  Implements use of per CPU irq for asynchronous notification next to
existing standard irq support. This change allows for example to use
GIC_PPI on platforms where no GIC_SPI is provisioned for OP-TEE
asynchronous notification.

Co-developed-by: Alexandre Torgue <alexandre.torgue@foss.st.com>
Signed-off-by: Alexandre Torgue <alexandre.torgue@foss.st.com>
Signed-off-by: Etienne Carriere <etienne.carriere@linaro.org>
---
 drivers/tee/optee/optee_private.h |  22 ++++++
 drivers/tee/optee/smc_abi.c       | 107 ++++++++++++++++++++++++++++--
 2 files changed, 124 insertions(+), 5 deletions(-)
  

Comments

kernel test robot Jan. 17, 2023, 4:56 a.m. UTC | #1
Hi Etienne,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on robh/for-next]
[also build test WARNING on krzk/for-next krzk-dt/for-next krzk-mem-ctrl/for-next linus/master v6.2-rc4]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Etienne-Carriere/optee-add-per-cpu-asynchronous-notification/20230112-232957
base:   https://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git for-next
patch link:    https://lore.kernel.org/r/20230112145424.3791276-2-etienne.carriere%40linaro.org
patch subject: [PATCH 1/3] optee: add per cpu asynchronous notification
config: arm64-randconfig-s043-20230116
compiler: aarch64-linux-gcc (GCC) 12.1.0
reproduce:
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # apt-get install sparse
        # sparse version: v0.6.4-39-gce1a6720-dirty
        # https://github.com/intel-lab-lkp/linux/commit/3ae0dd52e3ae0cb2b3847b7c1e83381563751041
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Etienne-Carriere/optee-add-per-cpu-asynchronous-notification/20230112-232957
        git checkout 3ae0dd52e3ae0cb2b3847b7c1e83381563751041
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=arm64 olddefconfig
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=arm64 SHELL=/bin/bash drivers/tee/optee/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>

sparse warnings: (new ones prefixed by >>)
>> drivers/tee/optee/smc_abi.c:1071:20: sparse: sparse: incorrect type in assignment (different address spaces) @@     expected struct optee_pcpu *optee_pcpu @@     got struct optee_pcpu [noderef] __percpu * @@
   drivers/tee/optee/smc_abi.c:1071:20: sparse:     expected struct optee_pcpu *optee_pcpu
   drivers/tee/optee/smc_abi.c:1071:20: sparse:     got struct optee_pcpu [noderef] __percpu *
>> drivers/tee/optee/smc_abi.c:1076:40: sparse: sparse: incorrect type in initializer (different address spaces) @@     expected void const [noderef] __percpu *__vpp_verify @@     got struct optee_pcpu * @@
   drivers/tee/optee/smc_abi.c:1076:40: sparse:     expected void const [noderef] __percpu *__vpp_verify
   drivers/tee/optee/smc_abi.c:1076:40: sparse:     got struct optee_pcpu *
>> drivers/tee/optee/smc_abi.c:1082:60: sparse: sparse: incorrect type in argument 4 (different address spaces) @@     expected void [noderef] __percpu *percpu_dev_id @@     got struct optee_pcpu *optee_pcpu @@
   drivers/tee/optee/smc_abi.c:1082:60: sparse:     expected void [noderef] __percpu *percpu_dev_id
   drivers/tee/optee/smc_abi.c:1082:60: sparse:     got struct optee_pcpu *optee_pcpu
>> drivers/tee/optee/smc_abi.c:1099:31: sparse: sparse: incorrect type in assignment (different address spaces) @@     expected struct optee_pcpu [noderef] __percpu *optee_pcpu @@     got struct optee_pcpu *optee_pcpu @@
   drivers/tee/optee/smc_abi.c:1099:31: sparse:     expected struct optee_pcpu [noderef] __percpu *optee_pcpu
   drivers/tee/optee/smc_abi.c:1099:31: sparse:     got struct optee_pcpu *optee_pcpu
>> drivers/tee/optee/smc_abi.c:1108:30: sparse: sparse: incorrect type in argument 2 (different address spaces) @@     expected void [noderef] __percpu * @@     got struct optee_pcpu *optee_pcpu @@
   drivers/tee/optee/smc_abi.c:1108:30: sparse:     expected void [noderef] __percpu *
   drivers/tee/optee/smc_abi.c:1108:30: sparse:     got struct optee_pcpu *optee_pcpu
>> drivers/tee/optee/smc_abi.c:1110:21: sparse: sparse: incorrect type in argument 1 (different address spaces) @@     expected void [noderef] __percpu *__pdata @@     got struct optee_pcpu *optee_pcpu @@
   drivers/tee/optee/smc_abi.c:1110:21: sparse:     expected void [noderef] __percpu *__pdata
   drivers/tee/optee/smc_abi.c:1110:21: sparse:     got struct optee_pcpu *optee_pcpu

vim +1071 drivers/tee/optee/smc_abi.c

  1063	
  1064	static int init_pcpu_irq(struct optee *optee, u_int irq)
  1065	{
  1066		struct optee_pcpu *optee_pcpu;
  1067		spinlock_t lock;
  1068		int cpu;
  1069		int rc;
  1070	
> 1071		optee_pcpu = alloc_percpu(struct optee_pcpu);
  1072		if (!optee_pcpu)
  1073			return -ENOMEM;
  1074	
  1075		for_each_present_cpu(cpu) {
> 1076			struct optee_pcpu *p = per_cpu_ptr(optee_pcpu, cpu);
  1077	
  1078			p->optee = optee;
  1079		}
  1080	
  1081		rc = request_percpu_irq(irq, notif_irq_handler,
> 1082					"optee_pcpu_notification", optee_pcpu);
  1083		if (rc)
  1084			goto err_free_pcpu;
  1085	
  1086		spin_lock_init(&lock);
  1087	
  1088		spin_lock(&lock);
  1089		enable_percpu_irq(irq, 0);
  1090		spin_unlock(&lock);
  1091	
  1092		INIT_WORK(&optee->smc.notif_pcpu_work, notif_pcpu_irq_work_fn);
  1093		optee->smc.notif_pcpu_wq = create_workqueue("optee_pcpu_notification");
  1094		if (!optee->smc.notif_pcpu_wq) {
  1095			rc = -EINVAL;
  1096			goto err_free_pcpu_irq;
  1097		}
  1098	
> 1099		optee->smc.optee_pcpu = optee_pcpu;
  1100		optee->smc.notif_irq = irq;
  1101	
  1102		return 0;
  1103	
  1104	err_free_pcpu_irq:
  1105		spin_lock(&lock);
  1106		disable_percpu_irq(irq);
  1107		spin_unlock(&lock);
> 1108		free_percpu_irq(irq, optee_pcpu);
  1109	err_free_pcpu:
> 1110		free_percpu(optee_pcpu);
  1111	
  1112		return rc;
  1113	}
  1114
  

Patch

diff --git a/drivers/tee/optee/optee_private.h b/drivers/tee/optee/optee_private.h
index 04ae58892608..e5bd3548691f 100644
--- a/drivers/tee/optee/optee_private.h
+++ b/drivers/tee/optee/optee_private.h
@@ -94,11 +94,33 @@  struct optee_supp {
 	struct completion reqs_c;
 };
 
+/*
+ * struct optee_pcpu - per cpu notif private struct passed to work functions
+ * @optee		optee device reference
+ */
+struct optee_pcpu {
+	struct optee *optee;
+};
+
+/*
+ * struct optee_smc - optee smc communication struct
+ * @invoke_fn		handler function to invoke secure monitor
+ * @memremaped_shm	virtual address of memory in shared memory pool
+ * @sec_caps:		secure world capabilities defined by
+ *			OPTEE_SMC_SEC_CAP_* in optee_smc.h
+ * @notif_irq		interrupt used as async notification by OP-TEE or 0
+ * @optee_pcpu		per_cpu optee instance for per cpu work or NULL
+ * @notif_pcpu_wq	workqueue for per cpu aynchronous notification or NULL
+ * @notif_pcpu_work	work for per cpu asynchronous notification
+ */
 struct optee_smc {
 	optee_invoke_fn *invoke_fn;
 	void *memremaped_shm;
 	u32 sec_caps;
 	unsigned int notif_irq;
+	struct optee_pcpu __percpu *optee_pcpu;
+	struct workqueue_struct *notif_pcpu_wq;
+	struct work_struct notif_pcpu_work;
 };
 
 /**
diff --git a/drivers/tee/optee/smc_abi.c b/drivers/tee/optee/smc_abi.c
index a1c1fa1a9c28..8c2d58d605ac 100644
--- a/drivers/tee/optee/smc_abi.c
+++ b/drivers/tee/optee/smc_abi.c
@@ -993,12 +993,20 @@  static u32 get_async_notif_value(optee_invoke_fn *invoke_fn, bool *value_valid,
 
 static irqreturn_t notif_irq_handler(int irq, void *dev_id)
 {
-	struct optee *optee = dev_id;
+	struct optee *optee;
 	bool do_bottom_half = false;
 	bool value_valid;
 	bool value_pending;
 	u32 value;
 
+	if (irq_is_percpu_devid(irq)) {
+		struct optee_pcpu *pcpu = (struct optee_pcpu *)dev_id;
+
+		optee = pcpu->optee;
+	} else {
+		optee = dev_id;
+	}
+
 	do {
 		value = get_async_notif_value(optee->smc.invoke_fn,
 					      &value_valid, &value_pending);
@@ -1011,8 +1019,13 @@  static irqreturn_t notif_irq_handler(int irq, void *dev_id)
 			optee_notif_send(optee, value);
 	} while (value_pending);
 
-	if (do_bottom_half)
-		return IRQ_WAKE_THREAD;
+	if (do_bottom_half) {
+		if (irq_is_percpu_devid(irq))
+			queue_work(optee->smc.notif_pcpu_wq, &optee->smc.notif_pcpu_work);
+		else
+			return IRQ_WAKE_THREAD;
+	}
+
 	return IRQ_HANDLED;
 }
 
@@ -1025,7 +1038,7 @@  static irqreturn_t notif_irq_thread_fn(int irq, void *dev_id)
 	return IRQ_HANDLED;
 }
 
-static int optee_smc_notif_init_irq(struct optee *optee, u_int irq)
+static int init_irq(struct optee *optee, u_int irq)
 {
 	int rc;
 
@@ -1040,12 +1053,96 @@  static int optee_smc_notif_init_irq(struct optee *optee, u_int irq)
 	return 0;
 }
 
+static void notif_pcpu_irq_work_fn(struct work_struct *work)
+{
+	struct optee_smc *optee_smc = container_of(work, struct optee_smc, notif_pcpu_work);
+	struct optee *optee = container_of(optee_smc, struct optee, smc);
+
+	optee_smc_do_bottom_half(optee->ctx);
+}
+
+static int init_pcpu_irq(struct optee *optee, u_int irq)
+{
+	struct optee_pcpu *optee_pcpu;
+	spinlock_t lock;
+	int cpu;
+	int rc;
+
+	optee_pcpu = alloc_percpu(struct optee_pcpu);
+	if (!optee_pcpu)
+		return -ENOMEM;
+
+	for_each_present_cpu(cpu) {
+		struct optee_pcpu *p = per_cpu_ptr(optee_pcpu, cpu);
+
+		p->optee = optee;
+	}
+
+	rc = request_percpu_irq(irq, notif_irq_handler,
+				"optee_pcpu_notification", optee_pcpu);
+	if (rc)
+		goto err_free_pcpu;
+
+	spin_lock_init(&lock);
+
+	spin_lock(&lock);
+	enable_percpu_irq(irq, 0);
+	spin_unlock(&lock);
+
+	INIT_WORK(&optee->smc.notif_pcpu_work, notif_pcpu_irq_work_fn);
+	optee->smc.notif_pcpu_wq = create_workqueue("optee_pcpu_notification");
+	if (!optee->smc.notif_pcpu_wq) {
+		rc = -EINVAL;
+		goto err_free_pcpu_irq;
+	}
+
+	optee->smc.optee_pcpu = optee_pcpu;
+	optee->smc.notif_irq = irq;
+
+	return 0;
+
+err_free_pcpu_irq:
+	spin_lock(&lock);
+	disable_percpu_irq(irq);
+	spin_unlock(&lock);
+	free_percpu_irq(irq, optee_pcpu);
+err_free_pcpu:
+	free_percpu(optee_pcpu);
+
+	return rc;
+}
+
+static int optee_smc_notif_init_irq(struct optee *optee, u_int irq)
+{
+	if (irq_is_percpu_devid(irq))
+		return init_pcpu_irq(optee, irq);
+	else
+		return init_irq(optee, irq);
+}
+
+static void uninit_pcpu_irq(struct optee *optee)
+{
+	spinlock_t lock;
+
+	spin_lock_init(&lock);
+	spin_lock(&lock);
+	disable_percpu_irq(optee->smc.notif_irq);
+	spin_unlock(&lock);
+
+	free_percpu_irq(optee->smc.notif_irq, optee->smc.optee_pcpu);
+	free_percpu(optee->smc.optee_pcpu);
+}
+
 static void optee_smc_notif_uninit_irq(struct optee *optee)
 {
 	if (optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_ASYNC_NOTIF) {
 		optee_smc_stop_async_notif(optee->ctx);
 		if (optee->smc.notif_irq) {
-			free_irq(optee->smc.notif_irq, optee);
+			if (irq_is_percpu_devid(optee->smc.notif_irq))
+				uninit_pcpu_irq(optee);
+			else
+				free_irq(optee->smc.notif_irq, optee);
+
 			irq_dispose_mapping(optee->smc.notif_irq);
 		}
 	}