Skip to content

Commit e76fadf

Browse files
d-tatianinxzpeter
authored andcommitted
os: add an ability to lock memory on_fault
This will be used in the following commits to make it possible to only lock memory on fault instead of right away. Signed-off-by: Daniil Tatianin <d-tatianin@yandex-team.ru> Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru> Link: https://lore.kernel.org/r/20250212143920.1269754-2-d-tatianin@yandex-team.ru [peterx: fail os_mlock(on_fault=1) when not supported] [peterx: use G_GNUC_UNUSED instead of "(void)on_fault", per Dan] Signed-off-by: Peter Xu <peterx@redhat.com>
1 parent 30943e4 commit e76fadf

File tree

6 files changed

+23
-6
lines changed

6 files changed

+23
-6
lines changed

include/system/os-posix.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ bool os_set_runas(const char *user_id);
5353
void os_set_chroot(const char *path);
5454
void os_setup_limits(void);
5555
void os_setup_post(void);
56-
int os_mlock(void);
56+
int os_mlock(bool on_fault);
5757

5858
/**
5959
* qemu_alloc_stack:

include/system/os-win32.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ static inline bool is_daemonized(void)
123123
return false;
124124
}
125125

126-
static inline int os_mlock(void)
126+
static inline int os_mlock(bool on_fault G_GNUC_UNUSED)
127127
{
128128
return -ENOSYS;
129129
}

meson.build

+6
Original file line numberDiff line numberDiff line change
@@ -2885,6 +2885,12 @@ config_host_data.set('HAVE_MLOCKALL', cc.links(gnu_source_prefix + '''
28852885
return mlockall(MCL_FUTURE);
28862886
}'''))
28872887

2888+
config_host_data.set('HAVE_MLOCK_ONFAULT', cc.links(gnu_source_prefix + '''
2889+
#include <sys/mman.h>
2890+
int main(void) {
2891+
return mlockall(MCL_FUTURE | MCL_ONFAULT);
2892+
}'''))
2893+
28882894
have_l2tpv3 = false
28892895
if get_option('l2tpv3').allowed() and have_system
28902896
have_l2tpv3 = cc.has_type('struct mmsghdr',

migration/postcopy-ram.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis)
652652
}
653653

654654
if (enable_mlock) {
655-
if (os_mlock() < 0) {
655+
if (os_mlock(false) < 0) {
656656
error_report("mlock: %s", strerror(errno));
657657
/*
658658
* It doesn't feel right to fail at this point, we have a valid

os-posix.c

+13-2
Original file line numberDiff line numberDiff line change
@@ -327,18 +327,29 @@ void os_set_line_buffering(void)
327327
setvbuf(stdout, NULL, _IOLBF, 0);
328328
}
329329

330-
int os_mlock(void)
330+
int os_mlock(bool on_fault)
331331
{
332332
#ifdef HAVE_MLOCKALL
333333
int ret = 0;
334+
int flags = MCL_CURRENT | MCL_FUTURE;
334335

335-
ret = mlockall(MCL_CURRENT | MCL_FUTURE);
336+
if (on_fault) {
337+
#ifdef HAVE_MLOCK_ONFAULT
338+
flags |= MCL_ONFAULT;
339+
#else
340+
error_report("mlockall: on_fault not supported");
341+
return -EINVAL;
342+
#endif
343+
}
344+
345+
ret = mlockall(flags);
336346
if (ret < 0) {
337347
error_report("mlockall: %s", strerror(errno));
338348
}
339349

340350
return ret;
341351
#else
352+
(void)on_fault;
342353
return -ENOSYS;
343354
#endif
344355
}

system/vl.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ static QemuOptsList qemu_run_with_opts = {
797797
static void realtime_init(void)
798798
{
799799
if (enable_mlock) {
800-
if (os_mlock() < 0) {
800+
if (os_mlock(false) < 0) {
801801
error_report("locking memory failed");
802802
exit(1);
803803
}

0 commit comments

Comments
 (0)