xref: /dflybsd-src/contrib/lvm2/dist/lib/mm/memlock.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1*86d7f5d3SJohn Marino /*	$NetBSD: memlock.c,v 1.1.1.2 2009/12/02 00:26:25 haad Exp $	*/
2*86d7f5d3SJohn Marino 
3*86d7f5d3SJohn Marino /*
4*86d7f5d3SJohn Marino  * Copyright (C) 2003-2004 Sistina Software, Inc. All rights reserved.
5*86d7f5d3SJohn Marino  * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
6*86d7f5d3SJohn Marino  *
7*86d7f5d3SJohn Marino  * This file is part of LVM2.
8*86d7f5d3SJohn Marino  *
9*86d7f5d3SJohn Marino  * This copyrighted material is made available to anyone wishing to use,
10*86d7f5d3SJohn Marino  * modify, copy, or redistribute it subject to the terms and conditions
11*86d7f5d3SJohn Marino  * of the GNU Lesser General Public License v.2.1.
12*86d7f5d3SJohn Marino  *
13*86d7f5d3SJohn Marino  * You should have received a copy of the GNU Lesser General Public License
14*86d7f5d3SJohn Marino  * along with this program; if not, write to the Free Software Foundation,
15*86d7f5d3SJohn Marino  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16*86d7f5d3SJohn Marino  */
17*86d7f5d3SJohn Marino 
18*86d7f5d3SJohn Marino #include "lib.h"
19*86d7f5d3SJohn Marino #include "memlock.h"
20*86d7f5d3SJohn Marino #include "defaults.h"
21*86d7f5d3SJohn Marino #include "config.h"
22*86d7f5d3SJohn Marino #include "toolcontext.h"
23*86d7f5d3SJohn Marino 
24*86d7f5d3SJohn Marino #include <limits.h>
25*86d7f5d3SJohn Marino #include <fcntl.h>
26*86d7f5d3SJohn Marino #include <unistd.h>
27*86d7f5d3SJohn Marino #include <sys/mman.h>
28*86d7f5d3SJohn Marino #include <sys/time.h>
29*86d7f5d3SJohn Marino #include <sys/resource.h>
30*86d7f5d3SJohn Marino 
31*86d7f5d3SJohn Marino #ifndef DEVMAPPER_SUPPORT
32*86d7f5d3SJohn Marino 
memlock_inc(void)33*86d7f5d3SJohn Marino void memlock_inc(void)
34*86d7f5d3SJohn Marino {
35*86d7f5d3SJohn Marino 	return;
36*86d7f5d3SJohn Marino }
memlock_dec(void)37*86d7f5d3SJohn Marino void memlock_dec(void)
38*86d7f5d3SJohn Marino {
39*86d7f5d3SJohn Marino 	return;
40*86d7f5d3SJohn Marino }
memlock(void)41*86d7f5d3SJohn Marino int memlock(void)
42*86d7f5d3SJohn Marino {
43*86d7f5d3SJohn Marino 	return 0;
44*86d7f5d3SJohn Marino }
memlock_init(struct cmd_context * cmd)45*86d7f5d3SJohn Marino void memlock_init(struct cmd_context *cmd)
46*86d7f5d3SJohn Marino {
47*86d7f5d3SJohn Marino 	return;
48*86d7f5d3SJohn Marino }
49*86d7f5d3SJohn Marino 
50*86d7f5d3SJohn Marino #else				/* DEVMAPPER_SUPPORT */
51*86d7f5d3SJohn Marino 
52*86d7f5d3SJohn Marino static size_t _size_stack;
53*86d7f5d3SJohn Marino static size_t _size_malloc_tmp;
54*86d7f5d3SJohn Marino static size_t _size_malloc = 2000000;
55*86d7f5d3SJohn Marino 
56*86d7f5d3SJohn Marino static void *_malloc_mem = NULL;
57*86d7f5d3SJohn Marino static int _memlock_count = 0;
58*86d7f5d3SJohn Marino static int _memlock_count_daemon = 0;
59*86d7f5d3SJohn Marino static int _priority;
60*86d7f5d3SJohn Marino static int _default_priority;
61*86d7f5d3SJohn Marino 
_touch_memory(void * mem,size_t size)62*86d7f5d3SJohn Marino static void _touch_memory(void *mem, size_t size)
63*86d7f5d3SJohn Marino {
64*86d7f5d3SJohn Marino 	size_t pagesize = lvm_getpagesize();
65*86d7f5d3SJohn Marino 	void *pos = mem;
66*86d7f5d3SJohn Marino 	void *end = mem + size - sizeof(long);
67*86d7f5d3SJohn Marino 
68*86d7f5d3SJohn Marino 	while (pos < end) {
69*86d7f5d3SJohn Marino 		*(long *) pos = 1;
70*86d7f5d3SJohn Marino 		pos += pagesize;
71*86d7f5d3SJohn Marino 	}
72*86d7f5d3SJohn Marino }
73*86d7f5d3SJohn Marino 
_allocate_memory(void)74*86d7f5d3SJohn Marino static void _allocate_memory(void)
75*86d7f5d3SJohn Marino {
76*86d7f5d3SJohn Marino 	void *stack_mem, *temp_malloc_mem;
77*86d7f5d3SJohn Marino 
78*86d7f5d3SJohn Marino 	if ((stack_mem = alloca(_size_stack)))
79*86d7f5d3SJohn Marino 		_touch_memory(stack_mem, _size_stack);
80*86d7f5d3SJohn Marino 
81*86d7f5d3SJohn Marino 	if ((temp_malloc_mem = malloc(_size_malloc_tmp)))
82*86d7f5d3SJohn Marino 		_touch_memory(temp_malloc_mem, _size_malloc_tmp);
83*86d7f5d3SJohn Marino 
84*86d7f5d3SJohn Marino 	if ((_malloc_mem = malloc(_size_malloc)))
85*86d7f5d3SJohn Marino 		_touch_memory(_malloc_mem, _size_malloc);
86*86d7f5d3SJohn Marino 
87*86d7f5d3SJohn Marino 	free(temp_malloc_mem);
88*86d7f5d3SJohn Marino }
89*86d7f5d3SJohn Marino 
_release_memory(void)90*86d7f5d3SJohn Marino static void _release_memory(void)
91*86d7f5d3SJohn Marino {
92*86d7f5d3SJohn Marino 	free(_malloc_mem);
93*86d7f5d3SJohn Marino }
94*86d7f5d3SJohn Marino 
95*86d7f5d3SJohn Marino #undef MCL_CURRENT /* XXX: please implement m{,un}lockall */
96*86d7f5d3SJohn Marino 
97*86d7f5d3SJohn Marino /* Stop memory getting swapped out */
_lock_mem(void)98*86d7f5d3SJohn Marino static void _lock_mem(void)
99*86d7f5d3SJohn Marino {
100*86d7f5d3SJohn Marino #ifdef MCL_CURRENT
101*86d7f5d3SJohn Marino 	if (mlockall(MCL_CURRENT | MCL_FUTURE))
102*86d7f5d3SJohn Marino 		log_sys_error("mlockall", "");
103*86d7f5d3SJohn Marino 	else
104*86d7f5d3SJohn Marino 		log_very_verbose("Locking memory");
105*86d7f5d3SJohn Marino #endif
106*86d7f5d3SJohn Marino 	_allocate_memory();
107*86d7f5d3SJohn Marino 
108*86d7f5d3SJohn Marino 	errno = 0;
109*86d7f5d3SJohn Marino 	if (((_priority = getpriority(PRIO_PROCESS, 0)) == -1) && errno)
110*86d7f5d3SJohn Marino 		log_sys_error("getpriority", "");
111*86d7f5d3SJohn Marino 	else
112*86d7f5d3SJohn Marino 		if (setpriority(PRIO_PROCESS, 0, _default_priority))
113*86d7f5d3SJohn Marino 			log_error("setpriority %d failed: %s",
114*86d7f5d3SJohn Marino 				  _default_priority, strerror(errno));
115*86d7f5d3SJohn Marino }
116*86d7f5d3SJohn Marino 
_unlock_mem(void)117*86d7f5d3SJohn Marino static void _unlock_mem(void)
118*86d7f5d3SJohn Marino {
119*86d7f5d3SJohn Marino #ifdef MCL_CURRENT
120*86d7f5d3SJohn Marino 	if (munlockall())
121*86d7f5d3SJohn Marino 		log_sys_error("munlockall", "");
122*86d7f5d3SJohn Marino 	else
123*86d7f5d3SJohn Marino 		log_very_verbose("Unlocking memory");
124*86d7f5d3SJohn Marino #endif
125*86d7f5d3SJohn Marino 	_release_memory();
126*86d7f5d3SJohn Marino 	if (setpriority(PRIO_PROCESS, 0, _priority))
127*86d7f5d3SJohn Marino 		log_error("setpriority %u failed: %s", _priority,
128*86d7f5d3SJohn Marino 			  strerror(errno));
129*86d7f5d3SJohn Marino }
130*86d7f5d3SJohn Marino 
_lock_mem_if_needed(void)131*86d7f5d3SJohn Marino static void _lock_mem_if_needed(void) {
132*86d7f5d3SJohn Marino 	if ((_memlock_count + _memlock_count_daemon) == 1)
133*86d7f5d3SJohn Marino 		_lock_mem();
134*86d7f5d3SJohn Marino }
135*86d7f5d3SJohn Marino 
_unlock_mem_if_possible(void)136*86d7f5d3SJohn Marino static void _unlock_mem_if_possible(void) {
137*86d7f5d3SJohn Marino 	if ((_memlock_count + _memlock_count_daemon) == 0)
138*86d7f5d3SJohn Marino 		_unlock_mem();
139*86d7f5d3SJohn Marino }
140*86d7f5d3SJohn Marino 
memlock_inc(void)141*86d7f5d3SJohn Marino void memlock_inc(void)
142*86d7f5d3SJohn Marino {
143*86d7f5d3SJohn Marino 	++_memlock_count;
144*86d7f5d3SJohn Marino 	_lock_mem_if_needed();
145*86d7f5d3SJohn Marino 	log_debug("memlock_count inc to %d", _memlock_count);
146*86d7f5d3SJohn Marino }
147*86d7f5d3SJohn Marino 
memlock_dec(void)148*86d7f5d3SJohn Marino void memlock_dec(void)
149*86d7f5d3SJohn Marino {
150*86d7f5d3SJohn Marino 	if (!_memlock_count)
151*86d7f5d3SJohn Marino 		log_error("Internal error: _memlock_count has dropped below 0.");
152*86d7f5d3SJohn Marino 	--_memlock_count;
153*86d7f5d3SJohn Marino 	_unlock_mem_if_possible();
154*86d7f5d3SJohn Marino 	log_debug("memlock_count dec to %d", _memlock_count);
155*86d7f5d3SJohn Marino }
156*86d7f5d3SJohn Marino 
157*86d7f5d3SJohn Marino /*
158*86d7f5d3SJohn Marino  * The memlock_*_daemon functions will force the mlockall() call that we need
159*86d7f5d3SJohn Marino  * to stay in memory, but they will have no effect on device scans (unlike
160*86d7f5d3SJohn Marino  * normal memlock_inc and memlock_dec). Memory is kept locked as long as either
161*86d7f5d3SJohn Marino  * of memlock or memlock_daemon is in effect.
162*86d7f5d3SJohn Marino  */
163*86d7f5d3SJohn Marino 
memlock_inc_daemon(void)164*86d7f5d3SJohn Marino void memlock_inc_daemon(void)
165*86d7f5d3SJohn Marino {
166*86d7f5d3SJohn Marino 	++_memlock_count_daemon;
167*86d7f5d3SJohn Marino 	_lock_mem_if_needed();
168*86d7f5d3SJohn Marino 	log_debug("memlock_count_daemon inc to %d", _memlock_count_daemon);
169*86d7f5d3SJohn Marino }
170*86d7f5d3SJohn Marino 
memlock_dec_daemon(void)171*86d7f5d3SJohn Marino void memlock_dec_daemon(void)
172*86d7f5d3SJohn Marino {
173*86d7f5d3SJohn Marino 	if (!_memlock_count_daemon)
174*86d7f5d3SJohn Marino 		log_error("Internal error: _memlock_count_daemon has dropped below 0.");
175*86d7f5d3SJohn Marino 	--_memlock_count_daemon;
176*86d7f5d3SJohn Marino 	_unlock_mem_if_possible();
177*86d7f5d3SJohn Marino 	log_debug("memlock_count_daemon dec to %d", _memlock_count_daemon);
178*86d7f5d3SJohn Marino }
179*86d7f5d3SJohn Marino 
180*86d7f5d3SJohn Marino /*
181*86d7f5d3SJohn Marino  * This disregards the daemon (dmeventd) locks, since we use memlock() to check
182*86d7f5d3SJohn Marino  * whether it is safe to run a device scan, which would normally coincide with
183*86d7f5d3SJohn Marino  * !memlock() -- but the daemon global memory lock breaks this assumption, so
184*86d7f5d3SJohn Marino  * we do not take those into account here.
185*86d7f5d3SJohn Marino  */
memlock(void)186*86d7f5d3SJohn Marino int memlock(void)
187*86d7f5d3SJohn Marino {
188*86d7f5d3SJohn Marino 	return _memlock_count;
189*86d7f5d3SJohn Marino }
190*86d7f5d3SJohn Marino 
memlock_init(struct cmd_context * cmd)191*86d7f5d3SJohn Marino void memlock_init(struct cmd_context *cmd)
192*86d7f5d3SJohn Marino {
193*86d7f5d3SJohn Marino 	_size_stack = find_config_tree_int(cmd,
194*86d7f5d3SJohn Marino 				      "activation/reserved_stack",
195*86d7f5d3SJohn Marino 				      DEFAULT_RESERVED_STACK) * 1024;
196*86d7f5d3SJohn Marino 	_size_malloc_tmp = find_config_tree_int(cmd,
197*86d7f5d3SJohn Marino 					   "activation/reserved_memory",
198*86d7f5d3SJohn Marino 					   DEFAULT_RESERVED_MEMORY) * 1024;
199*86d7f5d3SJohn Marino 	_default_priority = find_config_tree_int(cmd,
200*86d7f5d3SJohn Marino 					    "activation/process_priority",
201*86d7f5d3SJohn Marino 					    DEFAULT_PROCESS_PRIORITY);
202*86d7f5d3SJohn Marino }
203*86d7f5d3SJohn Marino 
204*86d7f5d3SJohn Marino #endif
205