[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[lvm-devel] PATCH: Replace mlockall() with interal implementation



Here is attached the first proposal patch for our mlockall() and locales
problems. Feel free to comment.

Zdenek

>From caf5e127fa9200800060cb76a06f8763410ba460 Mon Sep 17 00:00:00 2001
From: Zdenek Kabelac <zkabelac redhat com>
Date: Fri, 26 Feb 2010 15:13:19 +0100
Subject: [PATCH 04/36] Replace mlockall() with interal implementation

This patch replaces call of mlockall() system call with interal
reimplementation using only mlock() system call instead.
Lines from /proc/self/maps are scanned for memory areas and we
try to avoid locking areas which are sure we will not use during
mlock-ed state.

Patch also adds struct cmd_context to all memlock calls so we
have activation/memory_list available.

Signed-off-by: Zdenek Kabelac <zkabelac redhat com>
---
 lib/activate/activate.c |   16 +++---
 lib/locking/locking.c   |   12 ++--
 lib/mm/memlock.c        |  153 ++++++++++++++++++++++++++++++++++++++--------
 lib/mm/memlock.h        |    8 +-
 tools/lvmcmdlib.c       |    4 +-
 5 files changed, 146 insertions(+), 47 deletions(-)

diff --git a/lib/activate/activate.c b/lib/activate/activate.c
index c0ffd53..eb94ac0 100644
--- a/lib/activate/activate.c
+++ b/lib/activate/activate.c
@@ -849,7 +849,7 @@ static int _lv_suspend(struct cmd_context *cmd, const char *lvid_s,
 		if (!error_if_not_suspended) {
 			r = 1;
 			if (info.suspended)
-				memlock_inc();
+				memlock_inc(cmd);
 		}
 		goto out;
 	}
@@ -868,13 +868,13 @@ static int _lv_suspend(struct cmd_context *cmd, const char *lvid_s,
 		/* FIXME Consider aborting here */
 		stack;
 
-	memlock_inc();
+	memlock_inc(cmd);
 
 	if (lv_is_origin(lv_pre) || lv_is_cow(lv_pre))
 		lockfs = 1;
 
 	if (!_lv_suspend_lv(lv, lockfs, flush_required)) {
-		memlock_dec();
+		memlock_dec(cmd);
 		fs_unlock();
 		goto out;
 	}
@@ -930,7 +930,7 @@ static int _lv_resume(struct cmd_context *cmd, const char *lvid_s,
 	if (!_lv_activate_lv(lv))
 		goto_out;
 
-	memlock_dec();
+	memlock_dec(cmd);
 	fs_unlock();
 
 	if (!monitor_dev_for_events(cmd, lv, 1))
@@ -1019,9 +1019,9 @@ int lv_deactivate(struct cmd_context *cmd, const char *lvid_s)
 	if (!monitor_dev_for_events(cmd, lv, 0))
 		stack;
 
-	memlock_inc();
+	memlock_inc(cmd);
 	r = _lv_deactivate(lv);
-	memlock_dec();
+	memlock_dec(cmd);
 	fs_unlock();
 
 	if (!lv_info(cmd, lv, &info, 1, 0) || info.exists)
@@ -1112,10 +1112,10 @@ static int _lv_activate(struct cmd_context *cmd, const char *lvid_s,
 	if (exclusive)
 		lv->status |= ACTIVATE_EXCL;
 
-	memlock_inc();
+	memlock_inc(cmd);
 	if (!(r = _lv_activate_lv(lv)))
 		stack;
-	memlock_dec();
+	memlock_dec(cmd);
 	fs_unlock();
 
 	if (r && !monitor_dev_for_events(cmd, lv, 1))
diff --git a/lib/locking/locking.c b/lib/locking/locking.c
index a636f7c..76dbda5 100644
--- a/lib/locking/locking.c
+++ b/lib/locking/locking.c
@@ -161,22 +161,22 @@ static void _unblock_signals(void)
 	_signals_blocked = 0;
 }
 
-static void _lock_memory(lv_operation_t lv_op)
+static void _lock_memory(struct cmd_context *cmd, lv_operation_t lv_op)
 {
 	if (!(_locking.flags & LCK_PRE_MEMLOCK))
 		return;
 
 	if (lv_op == LV_SUSPEND)
-		memlock_inc();
+		memlock_inc(cmd);
 }
 
-static void _unlock_memory(lv_operation_t lv_op)
+static void _unlock_memory(struct cmd_context *cmd, lv_operation_t lv_op)
 {
 	if (!(_locking.flags & LCK_PRE_MEMLOCK))
 		return;
 
 	if (lv_op == LV_RESUME)
-		memlock_dec();
+		memlock_dec(cmd);
 }
 
 void reset_locking(void)
@@ -363,7 +363,7 @@ static int _lock_vol(struct cmd_context *cmd, const char *resource,
 	int ret = 0;
 
 	_block_signals(flags);
-	_lock_memory(lv_op);
+	_lock_memory(cmd, lv_op);
 
 	assert(resource);
 
@@ -390,7 +390,7 @@ static int _lock_vol(struct cmd_context *cmd, const char *resource,
 		_update_vg_lock_count(resource, flags);
 	}
 
-	_unlock_memory(lv_op);
+	_unlock_memory(cmd, lv_op);
 	_unblock_signals();
 
 	return ret;
diff --git a/lib/mm/memlock.c b/lib/mm/memlock.c
index 3ceb5f4..d98c088 100644
--- a/lib/mm/memlock.c
+++ b/lib/mm/memlock.c
@@ -28,11 +28,11 @@
 
 #ifndef DEVMAPPER_SUPPORT
 
-void memlock_inc(void)
+void memlock_inc(struct cmd_context *cmd)
 {
 	return;
 }
-void memlock_dec(void)
+void memlock_dec(struct cmd_context *cmd)
 {
 	return;
 }
@@ -90,17 +90,119 @@ static void _release_memory(void)
 	free(_malloc_mem);
 }
 
+typedef enum { LVM_MLOCK, LVM_MUNLOCK } lvmlock_t;
+/*
+ * mlock/munlock memory areas from /proc/self/maps
+ * format described in kernel/Documentation/filesystem/proc.txt
+ */
+static int _maps_line(struct cmd_context *cmd, lvmlock_t lock, const char* line)
+{
+	static int first_run = 0;
+	const struct config_node *cn;
+	struct config_value *cv;
+	char *str;
+	long from, to;
+	int pos;
+	char fr, fw, fx, fp;
+
+	sscanf(line, "%lx-%lx %c%c%c%c%n",
+	       &from, &to, &fr, &fw, &fx, &fp, &pos);
+
+	/* skip  ---p,  select with r,w,x */
+	if (fr != 'r' && fw != 'w' && fx != 'x')
+		return 0;
+
+	if (strstr(line + pos, "[vsyscall]"))
+		return 0; /* always ignore this area */
+
+	/* blacklist specific files/areas */
+	if (!(cn = find_config_tree_node(cmd, "activation/memory_list"))) {
+		/* If no blacklist configured, use an internal set */
+		/* more candidates: /libreadline.so, /libdevmapper-event.so */
+		if (strstr(line + pos, "locale/locale-archive") ||
+		    strstr(line + pos, "gconv/gconv-modules.cache")) {
+			log_debug("Blacklisted by configure: %s", line);
+			return 0;
+		}
+	} else {
+		for (cv = cn->v; cv; cv = cv->next) {
+			if (cv->type != CFG_STRING) {
+				if (first_run == 0)
+					log_error("Ignoring invalid string in config file "
+						  "activation/memory_list");
+				continue;
+			}
+			str = cv->v.str;
+			if (!*str) {
+				if (first_run == 0)
+					log_error("Ignoring invalid string in config file "
+						  "activation/memory_list");
+				continue;
+			}
+			if (strstr(line + pos, str)) {
+				log_debug("Blacklisted by configure: %s", line);
+				return 0;
+			}
+		}
+	}
+
+	log_debug("%s %10ldKiB %12lx - %12lx %c%c%c%c %s",
+		  (lock == LVM_MLOCK) ? "mlock" : "munlock",
+		  (to - from + 1023) / 1024, from, to, fr, fw, fx, fp, line + pos);
+
+	if (lock == LVM_MLOCK) {
+		if (mlock((const void*)from, to - from) < 0) {
+			log_sys_error("mlock", line);
+			return 1;
+		}
+	} else {
+		if (munlock((const void*)from, to - from) < 0) {
+			log_sys_error("munlock", line);
+			return 1;
+		}
+	}
+
+	return 0;
+}
+
+static int _memlock_maps(struct cmd_context *cmd, lvmlock_t lock)
+{
+	static const char selfmaps[] = "/self/maps";
+	int ret = 0;
+	char *line = NULL;
+	size_t len;
+	FILE *fh;
+	ssize_t r;
+	char *procselfmaps = alloca(strlen(cmd->proc_dir) + sizeof(selfmaps));
+
+	strcpy(procselfmaps, cmd->proc_dir);
+	strcat(procselfmaps, selfmaps);
+
+	if ((fh = fopen(procselfmaps, "r")) == NULL) {
+		log_sys_error("fopen", procselfmaps);
+		return 1;
+	}
+
+	while ((r = getline(&line, &len, fh)) != -1) {
+		line[r > 0 ? r - 1 : 0] = '\0'; /* remove \n */
+		if ((ret = _maps_line(cmd, lock, line) != 0))
+			break;
+	}
+
+	free(line);
+	fclose(fh);
+
+	return ret;
+}
+
 /* Stop memory getting swapped out */
-static void _lock_mem(void)
+static void _lock_mem(struct cmd_context *cmd)
 {
-#ifdef MCL_CURRENT
-	if (mlockall(MCL_CURRENT | MCL_FUTURE))
-		log_sys_error("mlockall", "");
-	else
-		log_very_verbose("Locking memory");
-#endif
 	_allocate_memory();
 
+	if (_memlock_maps(cmd, LVM_MLOCK) == 0)
+		log_very_verbose("Locking memory");
+
 	errno = 0;
 	if (((_priority = getpriority(PRIO_PROCESS, 0)) == -1) && errno)
 		log_sys_error("getpriority", "");
@@ -110,43 +212,40 @@ static void _lock_mem(void)
 				  _default_priority, strerror(errno));
 }
 
-static void _unlock_mem(void)
+static void _unlock_mem(struct cmd_context *cmd)
 {
-#ifdef MCL_CURRENT
-	if (munlockall())
-		log_sys_error("munlockall", "");
-	else
+	if (_memlock_maps(cmd, LVM_MUNLOCK) == 0)
 		log_very_verbose("Unlocking memory");
-#endif
+
 	_release_memory();
 	if (setpriority(PRIO_PROCESS, 0, _priority))
 		log_error("setpriority %u failed: %s", _priority,
 			  strerror(errno));
 }
 
-static void _lock_mem_if_needed(void) {
+static void _lock_mem_if_needed(struct cmd_context *cmd) {
 	if ((_memlock_count + _memlock_count_daemon) == 1)
-		_lock_mem();
+		_lock_mem(cmd);
 }
 
-static void _unlock_mem_if_possible(void) {
+static void _unlock_mem_if_possible(struct cmd_context *cmd) {
 	if ((_memlock_count + _memlock_count_daemon) == 0)
-		_unlock_mem();
+		_unlock_mem(cmd);
 }
 
-void memlock_inc(void)
+void memlock_inc(struct cmd_context *cmd)
 {
 	++_memlock_count;
-	_lock_mem_if_needed();
+	_lock_mem_if_needed(cmd);
 	log_debug("memlock_count inc to %d", _memlock_count);
 }
 
-void memlock_dec(void)
+void memlock_dec(struct cmd_context *cmd)
 {
 	if (!_memlock_count)
 		log_error(INTERNAL_ERROR "_memlock_count has dropped below 0.");
 	--_memlock_count;
-	_unlock_mem_if_possible();
+	_unlock_mem_if_possible(cmd);
 	log_debug("memlock_count dec to %d", _memlock_count);
 }
 
@@ -157,19 +256,19 @@ void memlock_dec(void)
  * of memlock or memlock_daemon is in effect.
  */
 
-void memlock_inc_daemon(void)
+void memlock_inc_daemon(struct cmd_context *cmd)
 {
 	++_memlock_count_daemon;
-	_lock_mem_if_needed();
+	_lock_mem_if_needed(cmd);
 	log_debug("memlock_count_daemon inc to %d", _memlock_count_daemon);
 }
 
-void memlock_dec_daemon(void)
+void memlock_dec_daemon(struct cmd_context *cmd)
 {
 	if (!_memlock_count_daemon)
 		log_error(INTERNAL_ERROR "_memlock_count_daemon has dropped below 0.");
 	--_memlock_count_daemon;
-	_unlock_mem_if_possible();
+	_unlock_mem_if_possible(cmd);
 	log_debug("memlock_count_daemon dec to %d", _memlock_count_daemon);
 }
 
diff --git a/lib/mm/memlock.h b/lib/mm/memlock.h
index 1f72eaa..fd19317 100644
--- a/lib/mm/memlock.h
+++ b/lib/mm/memlock.h
@@ -18,10 +18,10 @@
 
 struct cmd_context;
 
-void memlock_inc(void);
-void memlock_dec(void);
-void memlock_inc_daemon(void);
-void memlock_dec_daemon(void);
+void memlock_inc(struct cmd_context *cmd);
+void memlock_dec(struct cmd_context *cmd);
+void memlock_inc_daemon(struct cmd_context *cmd);
+void memlock_dec_daemon(struct cmd_context *cmd);
 int memlock(void);
 void memlock_init(struct cmd_context *cmd);
 
diff --git a/tools/lvmcmdlib.c b/tools/lvmcmdlib.c
index 580aad9..6b2bc9d 100644
--- a/tools/lvmcmdlib.c
+++ b/tools/lvmcmdlib.c
@@ -82,9 +82,9 @@ int lvm2_run(void *handle, const char *cmdline)
 	/* FIXME Temporary - move to libdevmapper */
 	ret = ECMD_PROCESSED;
 	if (!strcmp(cmdline, "_memlock_inc"))
-		memlock_inc_daemon();
+		memlock_inc_daemon(cmd);
 	else if (!strcmp(cmdline, "_memlock_dec"))
-		memlock_dec_daemon();
+		memlock_dec_daemon(cmd);
 	else
 		ret = lvm_run_command(cmd, argc, argv);
 
-- 
1.7.0


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]