[RFC,05/13] powerpc/vas: Setup fault handler per VAS instance
diff mbox series

Message ID 1571464540.24387.30.camel@hbabu-laptop
State New
Headers show
Series
  • powerpc/vas: Page fault handling for user space NX requests
Related show

Commit Message

Haren Myneni Oct. 19, 2019, 5:55 a.m. UTC
Fault handler is created as kernel thread for each VAS instance and
invoked whenever NX generates page fault. This thread reads CRBs
from fault FIFO and process them.

Signed-off-by: Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com>
Signed-off-by: Haren Myneni <haren@us.ibm.com>
---
 arch/powerpc/platforms/powernv/vas-fault.c | 53 ++++++++++++++++++++++++++++++
 arch/powerpc/platforms/powernv/vas.c       |  6 ++++
 arch/powerpc/platforms/powernv/vas.h       |  6 ++++
 3 files changed, 65 insertions(+)

Patch
diff mbox series

diff --git a/arch/powerpc/platforms/powernv/vas-fault.c b/arch/powerpc/platforms/powernv/vas-fault.c
index b67651c..3e2f4cb 100644
--- a/arch/powerpc/platforms/powernv/vas-fault.c
+++ b/arch/powerpc/platforms/powernv/vas-fault.c
@@ -27,6 +27,54 @@ 
  */
 #define VAS_FAULT_WIN_FIFO_SIZE	(4 << 20)
 
+struct task_struct *fault_handler;
+
+void vas_wakeup_fault_handler(int virq, void *arg)
+{
+	struct vas_instance *vinst = arg;
+
+	atomic_inc(&vinst->pending_fault);
+	wake_up(&vinst->fault_wq);
+}
+
+/*
+ * Fault handler thread for each VAS instance and process fault CRBs.
+ */
+static int fault_handler_func(void *arg)
+{
+	struct vas_instance *vinst = (struct vas_instance *)arg;
+
+	do {
+		if (signal_pending(current))
+			flush_signals(current);
+
+		wait_event_interruptible(vinst->fault_wq,
+					atomic_read(&vinst->pending_fault) ||
+					kthread_should_stop());
+
+		if (kthread_should_stop())
+			break;
+
+		atomic_dec(&vinst->pending_fault);
+	} while (!kthread_should_stop());
+
+	return 0;
+}
+
+/*
+ * Create a thread that processes the fault CRBs.
+ */
+int vas_setup_fault_handler(struct vas_instance *vinst)
+{
+	vinst->fault_handler = kthread_run(fault_handler_func, (void *)vinst,
+					"vas-fault-%u", vinst->vas_id);
+
+	if (IS_ERR(vinst->fault_handler))
+		return PTR_ERR(vinst->fault_handler);
+
+	return 0;
+}
+
 /*
  * Fault window is opened per VAS instance. NX pastes fault CRB in fault
  * FIFO upon page faults.
@@ -105,4 +153,9 @@  int vas_cleanup_fault_window(struct vas_instance *vinst)
 
 	return rc;
 }
+
+void vas_cleanup_fault_handler(struct vas_instance *vinst)
+{
+	kthread_stop(vinst->fault_handler);
+}
 #endif
diff --git a/arch/powerpc/platforms/powernv/vas.c b/arch/powerpc/platforms/powernv/vas.c
index 01295ed..7295226 100644
--- a/arch/powerpc/platforms/powernv/vas.c
+++ b/arch/powerpc/platforms/powernv/vas.c
@@ -30,6 +30,7 @@  static irqreturn_t vas_irq_handler(int virq, void *data)
 	struct vas_instance *vinst = data;
 
 	pr_devel("VAS %d: virq %d\n", vinst->vas_id, virq);
+	vas_wakeup_fault_handler(virq, data);
 
 	return IRQ_HANDLED;
 }
@@ -53,6 +54,10 @@  static void vas_irq_fault_handle_setup(struct vas_instance *vinst)
 	 * for user space.
 	 */
 	rc = vas_setup_fault_window(vinst);
+
+	if (!rc)
+		rc = vas_setup_fault_handler(vinst);
+
 	if (rc) {
 		free_irq(vinst->virq, vinst);
 		vinst->virq = 0;
@@ -128,6 +133,7 @@  static int init_vas_instance(struct platform_device *pdev)
 		}
 	}
 
+	init_waitqueue_head(&vinst->fault_wq);
 	pr_devel("Initialized instance [%s, %d], paste_base 0x%llx, "
 			"paste_win_id_shift 0x%llx IRQ %d Port 0x%llx\n",
 			pdev->name, vasid, vinst->paste_base_addr,
diff --git a/arch/powerpc/platforms/powernv/vas.h b/arch/powerpc/platforms/powernv/vas.h
index e23fd69..ee284b3 100644
--- a/arch/powerpc/platforms/powernv/vas.h
+++ b/arch/powerpc/platforms/powernv/vas.h
@@ -317,6 +317,9 @@  struct vas_instance {
 	int virq;
 	int fault_fifo_size;
 	void *fault_fifo;
+	atomic_t pending_fault;
+	wait_queue_head_t fault_wq;
+	struct task_struct *fault_handler;
 	struct vas_window *fault_win; /* Fault window */
 
 	struct mutex mutex;
@@ -414,6 +417,9 @@  struct vas_winctx {
 extern void vas_window_free_dbgdir(struct vas_window *win);
 extern int vas_setup_fault_window(struct vas_instance *vinst);
 extern int vas_cleanup_fault_window(struct vas_instance *vinst);
+extern void vas_wakeup_fault_handler(int virq, void *arg);
+extern int vas_setup_fault_handler(struct vas_instance *vinst);
+extern void vas_cleanup_fault_handler(struct vas_instance *vinst);
 
 static inline void vas_log_write(struct vas_window *win, char *name,
 			void *regptr, u64 val)