1*7c604eeaShaad /* $NetBSD: memlock.c,v 1.1.1.2 2009/12/02 00:26:25 haad Exp $ */
256a34939Shaad
356a34939Shaad /*
456a34939Shaad * Copyright (C) 2003-2004 Sistina Software, Inc. All rights reserved.
556a34939Shaad * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
656a34939Shaad *
756a34939Shaad * This file is part of LVM2.
856a34939Shaad *
956a34939Shaad * This copyrighted material is made available to anyone wishing to use,
1056a34939Shaad * modify, copy, or redistribute it subject to the terms and conditions
1156a34939Shaad * of the GNU Lesser General Public License v.2.1.
1256a34939Shaad *
1356a34939Shaad * You should have received a copy of the GNU Lesser General Public License
1456a34939Shaad * along with this program; if not, write to the Free Software Foundation,
1556a34939Shaad * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1656a34939Shaad */
1756a34939Shaad
1856a34939Shaad #include "lib.h"
1956a34939Shaad #include "memlock.h"
2056a34939Shaad #include "defaults.h"
2156a34939Shaad #include "config.h"
2256a34939Shaad #include "toolcontext.h"
2356a34939Shaad
2456a34939Shaad #include <limits.h>
2556a34939Shaad #include <fcntl.h>
2656a34939Shaad #include <unistd.h>
2756a34939Shaad #include <sys/mman.h>
2856a34939Shaad #include <sys/time.h>
2956a34939Shaad #include <sys/resource.h>
3056a34939Shaad
3156a34939Shaad #ifndef DEVMAPPER_SUPPORT
3256a34939Shaad
memlock_inc(void)3356a34939Shaad void memlock_inc(void)
3456a34939Shaad {
3556a34939Shaad return;
3656a34939Shaad }
memlock_dec(void)3756a34939Shaad void memlock_dec(void)
3856a34939Shaad {
3956a34939Shaad return;
4056a34939Shaad }
memlock(void)4156a34939Shaad int memlock(void)
4256a34939Shaad {
4356a34939Shaad return 0;
4456a34939Shaad }
memlock_init(struct cmd_context * cmd)4556a34939Shaad void memlock_init(struct cmd_context *cmd)
4656a34939Shaad {
4756a34939Shaad return;
4856a34939Shaad }
4956a34939Shaad
5056a34939Shaad #else /* DEVMAPPER_SUPPORT */
5156a34939Shaad
5256a34939Shaad static size_t _size_stack;
5356a34939Shaad static size_t _size_malloc_tmp;
5456a34939Shaad static size_t _size_malloc = 2000000;
5556a34939Shaad
5656a34939Shaad static void *_malloc_mem = NULL;
5756a34939Shaad static int _memlock_count = 0;
58*7c604eeaShaad static int _memlock_count_daemon = 0;
5956a34939Shaad static int _priority;
6056a34939Shaad static int _default_priority;
6156a34939Shaad
_touch_memory(void * mem,size_t size)6256a34939Shaad static void _touch_memory(void *mem, size_t size)
6356a34939Shaad {
6456a34939Shaad size_t pagesize = lvm_getpagesize();
6556a34939Shaad void *pos = mem;
6656a34939Shaad void *end = mem + size - sizeof(long);
6756a34939Shaad
6856a34939Shaad while (pos < end) {
6956a34939Shaad *(long *) pos = 1;
7056a34939Shaad pos += pagesize;
7156a34939Shaad }
7256a34939Shaad }
7356a34939Shaad
_allocate_memory(void)7456a34939Shaad static void _allocate_memory(void)
7556a34939Shaad {
7656a34939Shaad void *stack_mem, *temp_malloc_mem;
7756a34939Shaad
7856a34939Shaad if ((stack_mem = alloca(_size_stack)))
7956a34939Shaad _touch_memory(stack_mem, _size_stack);
8056a34939Shaad
8156a34939Shaad if ((temp_malloc_mem = malloc(_size_malloc_tmp)))
8256a34939Shaad _touch_memory(temp_malloc_mem, _size_malloc_tmp);
8356a34939Shaad
8456a34939Shaad if ((_malloc_mem = malloc(_size_malloc)))
8556a34939Shaad _touch_memory(_malloc_mem, _size_malloc);
8656a34939Shaad
8756a34939Shaad free(temp_malloc_mem);
8856a34939Shaad }
8956a34939Shaad
_release_memory(void)9056a34939Shaad static void _release_memory(void)
9156a34939Shaad {
9256a34939Shaad free(_malloc_mem);
9356a34939Shaad }
9456a34939Shaad
9556a34939Shaad /* Stop memory getting swapped out */
_lock_mem(void)9656a34939Shaad static void _lock_mem(void)
9756a34939Shaad {
9856a34939Shaad #ifdef MCL_CURRENT
9956a34939Shaad if (mlockall(MCL_CURRENT | MCL_FUTURE))
10056a34939Shaad log_sys_error("mlockall", "");
10156a34939Shaad else
10256a34939Shaad log_very_verbose("Locking memory");
10356a34939Shaad #endif
10456a34939Shaad _allocate_memory();
10556a34939Shaad
10656a34939Shaad errno = 0;
10756a34939Shaad if (((_priority = getpriority(PRIO_PROCESS, 0)) == -1) && errno)
10856a34939Shaad log_sys_error("getpriority", "");
10956a34939Shaad else
11056a34939Shaad if (setpriority(PRIO_PROCESS, 0, _default_priority))
11156a34939Shaad log_error("setpriority %d failed: %s",
11256a34939Shaad _default_priority, strerror(errno));
11356a34939Shaad }
11456a34939Shaad
_unlock_mem(void)11556a34939Shaad static void _unlock_mem(void)
11656a34939Shaad {
11756a34939Shaad #ifdef MCL_CURRENT
11856a34939Shaad if (munlockall())
11956a34939Shaad log_sys_error("munlockall", "");
12056a34939Shaad else
12156a34939Shaad log_very_verbose("Unlocking memory");
12256a34939Shaad #endif
12356a34939Shaad _release_memory();
12456a34939Shaad if (setpriority(PRIO_PROCESS, 0, _priority))
12556a34939Shaad log_error("setpriority %u failed: %s", _priority,
12656a34939Shaad strerror(errno));
12756a34939Shaad }
12856a34939Shaad
_lock_mem_if_needed(void)129*7c604eeaShaad static void _lock_mem_if_needed(void) {
130*7c604eeaShaad if ((_memlock_count + _memlock_count_daemon) == 1)
131*7c604eeaShaad _lock_mem();
132*7c604eeaShaad }
133*7c604eeaShaad
_unlock_mem_if_possible(void)134*7c604eeaShaad static void _unlock_mem_if_possible(void) {
135*7c604eeaShaad if ((_memlock_count + _memlock_count_daemon) == 0)
136*7c604eeaShaad _unlock_mem();
137*7c604eeaShaad }
138*7c604eeaShaad
memlock_inc(void)13956a34939Shaad void memlock_inc(void)
14056a34939Shaad {
141*7c604eeaShaad ++_memlock_count;
142*7c604eeaShaad _lock_mem_if_needed();
14356a34939Shaad log_debug("memlock_count inc to %d", _memlock_count);
14456a34939Shaad }
14556a34939Shaad
memlock_dec(void)14656a34939Shaad void memlock_dec(void)
14756a34939Shaad {
148*7c604eeaShaad if (!_memlock_count)
149*7c604eeaShaad log_error("Internal error: _memlock_count has dropped below 0.");
150*7c604eeaShaad --_memlock_count;
151*7c604eeaShaad _unlock_mem_if_possible();
15256a34939Shaad log_debug("memlock_count dec to %d", _memlock_count);
15356a34939Shaad }
15456a34939Shaad
155*7c604eeaShaad /*
156*7c604eeaShaad * The memlock_*_daemon functions will force the mlockall() call that we need
157*7c604eeaShaad * to stay in memory, but they will have no effect on device scans (unlike
158*7c604eeaShaad * normal memlock_inc and memlock_dec). Memory is kept locked as long as either
159*7c604eeaShaad * of memlock or memlock_daemon is in effect.
160*7c604eeaShaad */
161*7c604eeaShaad
memlock_inc_daemon(void)162*7c604eeaShaad void memlock_inc_daemon(void)
163*7c604eeaShaad {
164*7c604eeaShaad ++_memlock_count_daemon;
165*7c604eeaShaad _lock_mem_if_needed();
166*7c604eeaShaad log_debug("memlock_count_daemon inc to %d", _memlock_count_daemon);
167*7c604eeaShaad }
168*7c604eeaShaad
memlock_dec_daemon(void)169*7c604eeaShaad void memlock_dec_daemon(void)
170*7c604eeaShaad {
171*7c604eeaShaad if (!_memlock_count_daemon)
172*7c604eeaShaad log_error("Internal error: _memlock_count_daemon has dropped below 0.");
173*7c604eeaShaad --_memlock_count_daemon;
174*7c604eeaShaad _unlock_mem_if_possible();
175*7c604eeaShaad log_debug("memlock_count_daemon dec to %d", _memlock_count_daemon);
176*7c604eeaShaad }
177*7c604eeaShaad
178*7c604eeaShaad /*
179*7c604eeaShaad * This disregards the daemon (dmeventd) locks, since we use memlock() to check
180*7c604eeaShaad * whether it is safe to run a device scan, which would normally coincide with
181*7c604eeaShaad * !memlock() -- but the daemon global memory lock breaks this assumption, so
182*7c604eeaShaad * we do not take those into account here.
183*7c604eeaShaad */
memlock(void)18456a34939Shaad int memlock(void)
18556a34939Shaad {
18656a34939Shaad return _memlock_count;
18756a34939Shaad }
18856a34939Shaad
memlock_init(struct cmd_context * cmd)18956a34939Shaad void memlock_init(struct cmd_context *cmd)
19056a34939Shaad {
19156a34939Shaad _size_stack = find_config_tree_int(cmd,
19256a34939Shaad "activation/reserved_stack",
19356a34939Shaad DEFAULT_RESERVED_STACK) * 1024;
19456a34939Shaad _size_malloc_tmp = find_config_tree_int(cmd,
19556a34939Shaad "activation/reserved_memory",
19656a34939Shaad DEFAULT_RESERVED_MEMORY) * 1024;
19756a34939Shaad _default_priority = find_config_tree_int(cmd,
19856a34939Shaad "activation/process_priority",
19956a34939Shaad DEFAULT_PROCESS_PRIORITY);
20056a34939Shaad }
20156a34939Shaad
20256a34939Shaad #endif
203