[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 05/14] overcommit: introduce mem-lock=on-fault
From: |
Peter Xu |
Subject: |
[PULL 05/14] overcommit: introduce mem-lock=on-fault |
Date: |
Tue, 11 Feb 2025 17:50:49 -0500 |
From: Daniil Tatianin <d-tatianin@yandex-team.ru>
Locking the memory without MCL_ONFAULT instantly prefaults any mmaped
anonymous memory with a write-fault, which introduces a lot of extra
overhead in terms of memory usage when all you want to do is to prevent
kcompactd from migrating and compacting QEMU pages. Add an option to
only lock pages lazily as they're faulted by the process by using
MCL_ONFAULT if asked.
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Reviewed-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Daniil Tatianin <d-tatianin@yandex-team.ru>
Link:
https://lore.kernel.org/r/20250123131944.391886-5-d-tatianin@yandex-team.ru
Signed-off-by: Peter Xu <peterx@redhat.com>
---
include/system/system.h | 2 ++
migration/postcopy-ram.c | 2 +-
system/globals.c | 7 ++++++-
system/vl.c | 34 +++++++++++++++++++++++++++-------
qemu-options.hx | 14 +++++++++-----
5 files changed, 45 insertions(+), 14 deletions(-)
diff --git a/include/system/system.h b/include/system/system.h
index dc7628357a..a7effe7dfd 100644
--- a/include/system/system.h
+++ b/include/system/system.h
@@ -50,9 +50,11 @@ extern QEMUClockType rtc_clock;
typedef enum {
MLOCK_OFF = 0,
MLOCK_ON,
+ MLOCK_ON_FAULT,
} MlockState;
bool should_mlock(MlockState);
+bool is_mlock_on_fault(MlockState);
extern MlockState mlock_state;
diff --git a/migration/postcopy-ram.c b/migration/postcopy-ram.c
index 04068ee039..5d3edfcfec 100644
--- a/migration/postcopy-ram.c
+++ b/migration/postcopy-ram.c
@@ -652,7 +652,7 @@ int postcopy_ram_incoming_cleanup(MigrationIncomingState
*mis)
}
if (should_mlock(mlock_state)) {
- if (os_mlock(false) < 0) {
+ if (os_mlock(is_mlock_on_fault(mlock_state)) < 0) {
error_report("mlock: %s", strerror(errno));
/*
* It doesn't feel right to fail at this point, we have a valid
diff --git a/system/globals.c b/system/globals.c
index adeff38348..316623bd20 100644
--- a/system/globals.c
+++ b/system/globals.c
@@ -33,7 +33,12 @@
bool should_mlock(MlockState state)
{
- return state == MLOCK_ON;
+ return state == MLOCK_ON || state == MLOCK_ON_FAULT;
+}
+
+bool is_mlock_on_fault(MlockState state)
+{
+ return state == MLOCK_ON_FAULT;
}
enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
diff --git a/system/vl.c b/system/vl.c
index 2895824c1a..3c0fa2ff64 100644
--- a/system/vl.c
+++ b/system/vl.c
@@ -351,7 +351,7 @@ static QemuOptsList qemu_overcommit_opts = {
.desc = {
{
.name = "mem-lock",
- .type = QEMU_OPT_BOOL,
+ .type = QEMU_OPT_STRING,
},
{
.name = "cpu-pm",
@@ -797,7 +797,7 @@ static QemuOptsList qemu_run_with_opts = {
static void realtime_init(void)
{
if (should_mlock(mlock_state)) {
- if (os_mlock(false) < 0) {
+ if (os_mlock(is_mlock_on_fault(mlock_state)) < 0) {
error_report("locking memory failed");
exit(1);
}
@@ -1878,7 +1878,7 @@ static void object_option_parse(const char *str)
static void overcommit_parse(const char *str)
{
QemuOpts *opts;
- bool enable_mlock;
+ const char *mem_lock_opt;
opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
str, false);
@@ -1886,11 +1886,31 @@ static void overcommit_parse(const char *str)
exit(1);
}
- enable_mlock = qemu_opt_get_bool(opts, "mem-lock",
- should_mlock(mlock_state));
- mlock_state = enable_mlock ? MLOCK_ON : MLOCK_OFF;
-
enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", enable_cpu_pm);
+
+ mem_lock_opt = qemu_opt_get(opts, "mem-lock");
+ if (!mem_lock_opt) {
+ return;
+ }
+
+ if (strcmp(mem_lock_opt, "on") == 0) {
+ mlock_state = MLOCK_ON;
+ return;
+ }
+
+ if (strcmp(mem_lock_opt, "off") == 0) {
+ mlock_state = MLOCK_OFF;
+ return;
+ }
+
+ if (strcmp(mem_lock_opt, "on-fault") == 0) {
+ mlock_state = MLOCK_ON_FAULT;
+ return;
+ }
+
+ error_report("parameter 'mem-lock' expects one of "
+ "'on', 'off', 'on-fault'");
+ exit(1);
}
/*
diff --git a/qemu-options.hx b/qemu-options.hx
index 1b26ad53bd..61270e3206 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -4632,21 +4632,25 @@ SRST
ERST
DEF("overcommit", HAS_ARG, QEMU_OPTION_overcommit,
- "-overcommit [mem-lock=on|off][cpu-pm=on|off]\n"
+ "-overcommit [mem-lock=on|off|on-fault][cpu-pm=on|off]\n"
" run qemu with overcommit hints\n"
- " mem-lock=on|off controls memory lock support (default:
off)\n"
+ " mem-lock=on|off|on-fault controls memory lock support
(default: off)\n"
" cpu-pm=on|off controls cpu power management (default:
off)\n",
QEMU_ARCH_ALL)
SRST
-``-overcommit mem-lock=on|off``
+``-overcommit mem-lock=on|off|on-fault``
\
``-overcommit cpu-pm=on|off``
Run qemu with hints about host resource overcommit. The default is
to assume that host overcommits all resources.
Locking qemu and guest memory can be enabled via ``mem-lock=on``
- (disabled by default). This works when host memory is not
- overcommitted and reduces the worst-case latency for guest.
+ or ``mem-lock=on-fault`` (disabled by default). This works when
+ host memory is not overcommitted and reduces the worst-case latency for
+ guest. The on-fault option is better for reducing the memory footprint
+ since it makes allocations lazy, but the pages still get locked in place
+ once faulted by the guest or QEMU. Note that the two options are mutually
+ exclusive.
Guest ability to manage power state of host cpus (increasing latency
for other processes on the same host cpu, but decreasing latency for
--
2.47.0
- [PULL 00/14] Mem next patches, Peter Xu, 2025/02/11
- [PULL 01/14] system/physmem: take into account fd_offset for file fallocate, Peter Xu, 2025/02/11
- [PULL 02/14] os: add an ability to lock memory on_fault, Peter Xu, 2025/02/11
- [PULL 03/14] system/vl: extract overcommit option parsing into a helper, Peter Xu, 2025/02/11
- [PULL 04/14] system: introduce a new MlockState enum, Peter Xu, 2025/02/11
- [PULL 06/14] physmem: factor out memory_region_is_ram_device() check in memory_access_is_direct(), Peter Xu, 2025/02/11
- [PULL 07/14] physmem: factor out RAM/ROMD check in memory_access_is_direct(), Peter Xu, 2025/02/11
- [PULL 05/14] overcommit: introduce mem-lock=on-fault,
Peter Xu <=
- [PULL 09/14] physmem: disallow direct access to RAM DEVICE in address_space_write_rom(), Peter Xu, 2025/02/11
- [PULL 10/14] memory: pass MemTxAttrs to memory_access_is_direct(), Peter Xu, 2025/02/11
- [PULL 11/14] hmp: use cpu_get_phys_page_debug() in hmp_gva2gpa(), Peter Xu, 2025/02/11
- [PULL 08/14] physmem: factor out direct access check into memory_region_supports_direct_access(), Peter Xu, 2025/02/11
- [PULL 12/14] physmem: teach cpu_memory_rw_debug() to write to more memory regions, Peter Xu, 2025/02/11
- [PULL 13/14] system/physmem: handle hugetlb correctly in qemu_ram_remap(), Peter Xu, 2025/02/11
- [PULL 14/14] system/physmem: poisoned memory discard on reboot, Peter Xu, 2025/02/11