xref: /dflybsd-src/contrib/lvm2/dist/lib/mm/memlock.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /*	$NetBSD: memlock.c,v 1.1.1.2 2009/12/02 00:26:25 haad Exp $	*/
286d7f5d3SJohn Marino 
386d7f5d3SJohn Marino /*
486d7f5d3SJohn Marino  * Copyright (C) 2003-2004 Sistina Software, Inc. All rights reserved.
586d7f5d3SJohn Marino  * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
686d7f5d3SJohn Marino  *
786d7f5d3SJohn Marino  * This file is part of LVM2.
886d7f5d3SJohn Marino  *
986d7f5d3SJohn Marino  * This copyrighted material is made available to anyone wishing to use,
1086d7f5d3SJohn Marino  * modify, copy, or redistribute it subject to the terms and conditions
1186d7f5d3SJohn Marino  * of the GNU Lesser General Public License v.2.1.
1286d7f5d3SJohn Marino  *
1386d7f5d3SJohn Marino  * You should have received a copy of the GNU Lesser General Public License
1486d7f5d3SJohn Marino  * along with this program; if not, write to the Free Software Foundation,
1586d7f5d3SJohn Marino  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1686d7f5d3SJohn Marino  */
1786d7f5d3SJohn Marino 
1886d7f5d3SJohn Marino #include "lib.h"
1986d7f5d3SJohn Marino #include "memlock.h"
2086d7f5d3SJohn Marino #include "defaults.h"
2186d7f5d3SJohn Marino #include "config.h"
2286d7f5d3SJohn Marino #include "toolcontext.h"
2386d7f5d3SJohn Marino 
2486d7f5d3SJohn Marino #include <limits.h>
2586d7f5d3SJohn Marino #include <fcntl.h>
2686d7f5d3SJohn Marino #include <unistd.h>
2786d7f5d3SJohn Marino #include <sys/mman.h>
2886d7f5d3SJohn Marino #include <sys/time.h>
2986d7f5d3SJohn Marino #include <sys/resource.h>
3086d7f5d3SJohn Marino 
3186d7f5d3SJohn Marino #ifndef DEVMAPPER_SUPPORT
3286d7f5d3SJohn Marino 
memlock_inc(void)3386d7f5d3SJohn Marino void memlock_inc(void)
3486d7f5d3SJohn Marino {
3586d7f5d3SJohn Marino 	return;
3686d7f5d3SJohn Marino }
memlock_dec(void)3786d7f5d3SJohn Marino void memlock_dec(void)
3886d7f5d3SJohn Marino {
3986d7f5d3SJohn Marino 	return;
4086d7f5d3SJohn Marino }
memlock(void)4186d7f5d3SJohn Marino int memlock(void)
4286d7f5d3SJohn Marino {
4386d7f5d3SJohn Marino 	return 0;
4486d7f5d3SJohn Marino }
memlock_init(struct cmd_context * cmd)4586d7f5d3SJohn Marino void memlock_init(struct cmd_context *cmd)
4686d7f5d3SJohn Marino {
4786d7f5d3SJohn Marino 	return;
4886d7f5d3SJohn Marino }
4986d7f5d3SJohn Marino 
5086d7f5d3SJohn Marino #else				/* DEVMAPPER_SUPPORT */
5186d7f5d3SJohn Marino 
5286d7f5d3SJohn Marino static size_t _size_stack;
5386d7f5d3SJohn Marino static size_t _size_malloc_tmp;
5486d7f5d3SJohn Marino static size_t _size_malloc = 2000000;
5586d7f5d3SJohn Marino 
5686d7f5d3SJohn Marino static void *_malloc_mem = NULL;
5786d7f5d3SJohn Marino static int _memlock_count = 0;
5886d7f5d3SJohn Marino static int _memlock_count_daemon = 0;
5986d7f5d3SJohn Marino static int _priority;
6086d7f5d3SJohn Marino static int _default_priority;
6186d7f5d3SJohn Marino 
_touch_memory(void * mem,size_t size)6286d7f5d3SJohn Marino static void _touch_memory(void *mem, size_t size)
6386d7f5d3SJohn Marino {
6486d7f5d3SJohn Marino 	size_t pagesize = lvm_getpagesize();
6586d7f5d3SJohn Marino 	void *pos = mem;
6686d7f5d3SJohn Marino 	void *end = mem + size - sizeof(long);
6786d7f5d3SJohn Marino 
6886d7f5d3SJohn Marino 	while (pos < end) {
6986d7f5d3SJohn Marino 		*(long *) pos = 1;
7086d7f5d3SJohn Marino 		pos += pagesize;
7186d7f5d3SJohn Marino 	}
7286d7f5d3SJohn Marino }
7386d7f5d3SJohn Marino 
_allocate_memory(void)7486d7f5d3SJohn Marino static void _allocate_memory(void)
7586d7f5d3SJohn Marino {
7686d7f5d3SJohn Marino 	void *stack_mem, *temp_malloc_mem;
7786d7f5d3SJohn Marino 
7886d7f5d3SJohn Marino 	if ((stack_mem = alloca(_size_stack)))
7986d7f5d3SJohn Marino 		_touch_memory(stack_mem, _size_stack);
8086d7f5d3SJohn Marino 
8186d7f5d3SJohn Marino 	if ((temp_malloc_mem = malloc(_size_malloc_tmp)))
8286d7f5d3SJohn Marino 		_touch_memory(temp_malloc_mem, _size_malloc_tmp);
8386d7f5d3SJohn Marino 
8486d7f5d3SJohn Marino 	if ((_malloc_mem = malloc(_size_malloc)))
8586d7f5d3SJohn Marino 		_touch_memory(_malloc_mem, _size_malloc);
8686d7f5d3SJohn Marino 
8786d7f5d3SJohn Marino 	free(temp_malloc_mem);
8886d7f5d3SJohn Marino }
8986d7f5d3SJohn Marino 
_release_memory(void)9086d7f5d3SJohn Marino static void _release_memory(void)
9186d7f5d3SJohn Marino {
9286d7f5d3SJohn Marino 	free(_malloc_mem);
9386d7f5d3SJohn Marino }
9486d7f5d3SJohn Marino 
9586d7f5d3SJohn Marino #undef MCL_CURRENT /* XXX: please implement m{,un}lockall */
9686d7f5d3SJohn Marino 
9786d7f5d3SJohn Marino /* Stop memory getting swapped out */
_lock_mem(void)9886d7f5d3SJohn Marino static void _lock_mem(void)
9986d7f5d3SJohn Marino {
10086d7f5d3SJohn Marino #ifdef MCL_CURRENT
10186d7f5d3SJohn Marino 	if (mlockall(MCL_CURRENT | MCL_FUTURE))
10286d7f5d3SJohn Marino 		log_sys_error("mlockall", "");
10386d7f5d3SJohn Marino 	else
10486d7f5d3SJohn Marino 		log_very_verbose("Locking memory");
10586d7f5d3SJohn Marino #endif
10686d7f5d3SJohn Marino 	_allocate_memory();
10786d7f5d3SJohn Marino 
10886d7f5d3SJohn Marino 	errno = 0;
10986d7f5d3SJohn Marino 	if (((_priority = getpriority(PRIO_PROCESS, 0)) == -1) && errno)
11086d7f5d3SJohn Marino 		log_sys_error("getpriority", "");
11186d7f5d3SJohn Marino 	else
11286d7f5d3SJohn Marino 		if (setpriority(PRIO_PROCESS, 0, _default_priority))
11386d7f5d3SJohn Marino 			log_error("setpriority %d failed: %s",
11486d7f5d3SJohn Marino 				  _default_priority, strerror(errno));
11586d7f5d3SJohn Marino }
11686d7f5d3SJohn Marino 
_unlock_mem(void)11786d7f5d3SJohn Marino static void _unlock_mem(void)
11886d7f5d3SJohn Marino {
11986d7f5d3SJohn Marino #ifdef MCL_CURRENT
12086d7f5d3SJohn Marino 	if (munlockall())
12186d7f5d3SJohn Marino 		log_sys_error("munlockall", "");
12286d7f5d3SJohn Marino 	else
12386d7f5d3SJohn Marino 		log_very_verbose("Unlocking memory");
12486d7f5d3SJohn Marino #endif
12586d7f5d3SJohn Marino 	_release_memory();
12686d7f5d3SJohn Marino 	if (setpriority(PRIO_PROCESS, 0, _priority))
12786d7f5d3SJohn Marino 		log_error("setpriority %u failed: %s", _priority,
12886d7f5d3SJohn Marino 			  strerror(errno));
12986d7f5d3SJohn Marino }
13086d7f5d3SJohn Marino 
_lock_mem_if_needed(void)13186d7f5d3SJohn Marino static void _lock_mem_if_needed(void) {
13286d7f5d3SJohn Marino 	if ((_memlock_count + _memlock_count_daemon) == 1)
13386d7f5d3SJohn Marino 		_lock_mem();
13486d7f5d3SJohn Marino }
13586d7f5d3SJohn Marino 
_unlock_mem_if_possible(void)13686d7f5d3SJohn Marino static void _unlock_mem_if_possible(void) {
13786d7f5d3SJohn Marino 	if ((_memlock_count + _memlock_count_daemon) == 0)
13886d7f5d3SJohn Marino 		_unlock_mem();
13986d7f5d3SJohn Marino }
14086d7f5d3SJohn Marino 
memlock_inc(void)14186d7f5d3SJohn Marino void memlock_inc(void)
14286d7f5d3SJohn Marino {
14386d7f5d3SJohn Marino 	++_memlock_count;
14486d7f5d3SJohn Marino 	_lock_mem_if_needed();
14586d7f5d3SJohn Marino 	log_debug("memlock_count inc to %d", _memlock_count);
14686d7f5d3SJohn Marino }
14786d7f5d3SJohn Marino 
memlock_dec(void)14886d7f5d3SJohn Marino void memlock_dec(void)
14986d7f5d3SJohn Marino {
15086d7f5d3SJohn Marino 	if (!_memlock_count)
15186d7f5d3SJohn Marino 		log_error("Internal error: _memlock_count has dropped below 0.");
15286d7f5d3SJohn Marino 	--_memlock_count;
15386d7f5d3SJohn Marino 	_unlock_mem_if_possible();
15486d7f5d3SJohn Marino 	log_debug("memlock_count dec to %d", _memlock_count);
15586d7f5d3SJohn Marino }
15686d7f5d3SJohn Marino 
15786d7f5d3SJohn Marino /*
15886d7f5d3SJohn Marino  * The memlock_*_daemon functions will force the mlockall() call that we need
15986d7f5d3SJohn Marino  * to stay in memory, but they will have no effect on device scans (unlike
16086d7f5d3SJohn Marino  * normal memlock_inc and memlock_dec). Memory is kept locked as long as either
16186d7f5d3SJohn Marino  * of memlock or memlock_daemon is in effect.
16286d7f5d3SJohn Marino  */
16386d7f5d3SJohn Marino 
memlock_inc_daemon(void)16486d7f5d3SJohn Marino void memlock_inc_daemon(void)
16586d7f5d3SJohn Marino {
16686d7f5d3SJohn Marino 	++_memlock_count_daemon;
16786d7f5d3SJohn Marino 	_lock_mem_if_needed();
16886d7f5d3SJohn Marino 	log_debug("memlock_count_daemon inc to %d", _memlock_count_daemon);
16986d7f5d3SJohn Marino }
17086d7f5d3SJohn Marino 
memlock_dec_daemon(void)17186d7f5d3SJohn Marino void memlock_dec_daemon(void)
17286d7f5d3SJohn Marino {
17386d7f5d3SJohn Marino 	if (!_memlock_count_daemon)
17486d7f5d3SJohn Marino 		log_error("Internal error: _memlock_count_daemon has dropped below 0.");
17586d7f5d3SJohn Marino 	--_memlock_count_daemon;
17686d7f5d3SJohn Marino 	_unlock_mem_if_possible();
17786d7f5d3SJohn Marino 	log_debug("memlock_count_daemon dec to %d", _memlock_count_daemon);
17886d7f5d3SJohn Marino }
17986d7f5d3SJohn Marino 
18086d7f5d3SJohn Marino /*
18186d7f5d3SJohn Marino  * This disregards the daemon (dmeventd) locks, since we use memlock() to check
18286d7f5d3SJohn Marino  * whether it is safe to run a device scan, which would normally coincide with
18386d7f5d3SJohn Marino  * !memlock() -- but the daemon global memory lock breaks this assumption, so
18486d7f5d3SJohn Marino  * we do not take those into account here.
18586d7f5d3SJohn Marino  */
memlock(void)18686d7f5d3SJohn Marino int memlock(void)
18786d7f5d3SJohn Marino {
18886d7f5d3SJohn Marino 	return _memlock_count;
18986d7f5d3SJohn Marino }
19086d7f5d3SJohn Marino 
memlock_init(struct cmd_context * cmd)19186d7f5d3SJohn Marino void memlock_init(struct cmd_context *cmd)
19286d7f5d3SJohn Marino {
19386d7f5d3SJohn Marino 	_size_stack = find_config_tree_int(cmd,
19486d7f5d3SJohn Marino 				      "activation/reserved_stack",
19586d7f5d3SJohn Marino 				      DEFAULT_RESERVED_STACK) * 1024;
19686d7f5d3SJohn Marino 	_size_malloc_tmp = find_config_tree_int(cmd,
19786d7f5d3SJohn Marino 					   "activation/reserved_memory",
19886d7f5d3SJohn Marino 					   DEFAULT_RESERVED_MEMORY) * 1024;
19986d7f5d3SJohn Marino 	_default_priority = find_config_tree_int(cmd,
20086d7f5d3SJohn Marino 					    "activation/process_priority",
20186d7f5d3SJohn Marino 					    DEFAULT_PROCESS_PRIORITY);
20286d7f5d3SJohn Marino }
20386d7f5d3SJohn Marino 
20486d7f5d3SJohn Marino #endif
205