1 /*
2  * Copyright (C) 2013-2021 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * This code is a complete clean re-write of the stress tool by
19  * Colin Ian King <colin.king@canonical.com> and attempts to be
20  * backwardly compatible with the stress tool by Amos Waterland
21  * <apw@rossby.metr.ou.edu> but has more stress tests and more
22  * functionality.
23  *
24  */
25 #include "stress-ng.h"
26 
27 /*
28  *  stress_mlock_region
29  *	mlock a region of memory so it can't be swapped out
30  *	- used to lock sighandlers for faster response
31  */
stress_mlock_region(const void * addr_start,const void * addr_end)32 int stress_mlock_region(const void *addr_start, const void *addr_end)
33 {
34 #if defined(HAVE_MLOCK)
35 	const size_t page_size = stress_get_pagesize();
36 	const void *m_addr_start =
37 		(void *)((uintptr_t)addr_start & ~(page_size - 1));
38 	const void *m_addr_end =
39 		(void *)(((uintptr_t)addr_end + page_size - 1) &
40 		~(page_size - 1));
41 	const size_t len = (uintptr_t)m_addr_end - (uintptr_t)m_addr_start;
42 
43 	return shim_mlock((const void *)m_addr_start, len);
44 #else
45 	(void)addr_start;
46 	(void)addr_end;
47 
48 	return 0;
49 #endif
50 }
51