From a219343fcdc93d91d4e102abc746db68bf45b4ff Mon Sep 17 00:00:00 2001
From: Steffen Vogel <post@steffenvogel.de>
Date: Mon, 14 Aug 2017 14:29:23 +0200
Subject: [PATCH] rt: lock all memory by default

---
 lib/kernel/rt.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/lib/kernel/rt.c b/lib/kernel/rt.c
index d296cbde0..8675c90df 100644
--- a/lib/kernel/rt.c
+++ b/lib/kernel/rt.c
@@ -22,6 +22,7 @@
 
 #include <sched.h>
 #include <unistd.h>
+#include <sys/mman.h>
 
 #include "config.h"
 #include "utils.h"
@@ -52,6 +53,8 @@ int rt_init(int priority, int affinity)
 		rt_set_affinity(affinity);
 	else
 		warn("You might want to use the 'affinity' setting to pin VILLASnode to dedicate CPU cores");
+	
+	rt_lock_memory();
 #else
 	warn("This platform is not optimized for real-time execution");
 #endif
@@ -62,6 +65,19 @@ int rt_init(int priority, int affinity)
 
 #ifdef __linux__
 
+int rt_lock_memory()
+{
+	int ret;
+
+#ifdef _POSIX_MEMLOCK
+	ret = mlockall(MCL_CURRENT | MCL_FUTURE);
+	if (ret)
+		error("Failed to lock memory");
+#endif
+	
+	return 0;
+}
+
 int rt_set_affinity(int affinity)
 {
 	char isolcpus[255];