Skip to contentSkip to conversation

KVM: always use MADV_DONTFORK

PATCH Applied Andrea Arcangeli 6 participants 13 messages Jul 25, 2013 – Sep 1, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: always use MADV_DONTFORK

MADV_DONTFORK prevents fork to fail with -ENOMEM if the default overcommit heuristics decides there's too much anonymous virtual memory allocated. If the KVM secondary MMU is synchronized with MMU notifiers or not, doesn't make a difference in that regard.

Secondly it's always more efficient to avoid copying the guest physical address space in the fork child (so we avoid to mark all the guest memory readonly in the parent and so we skip the establishment and teardown of lots of pagetables in the child).

In the common case we can ignore the error if MADV_DONTFORK is not available. Leave a second invocation that errors out in the KVM path if MMU notifiers are missing and KVM is enabled, to abort in such case.

---
 exec.c | 1 +
 1 file changed, 1 insertion(+)
exec.c +1 -0
index c99a883..d3bb58d 100644
@@ -1162,6 +1162,7 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
 
     qemu_ram_setup_dump(new_block->host, size);
     qemu_madvise(new_block->host, size, QEMU_MADV_HUGEPAGE);
+    qemu_madvise(new_block->host, size, QEMU_MADV_DONTFORK);
 
     if (kvm_enabled())
         kvm_setup_guest_memory(new_block->host, size);
#1 lore raw

exec.c ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host, · line 1168

Quoted 13 lines · Andrea Arcangeli
exec.c +1 -0
index c99a883..d3bb58d 100644
@@ -1162,6 +1162,7 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
 
     qemu_ram_setup_dump(new_block->host, size);
     qemu_madvise(new_block->host, size, QEMU_MADV_HUGEPAGE);
+    qemu_madvise(new_block->host, size, QEMU_MADV_DONTFORK);
 
     if (kvm_enabled())
         kvm_setup_guest_memory(new_block->host, size);

kvm_setup_guest_memory() already calls
qemu_madvise(start, size, QEMU_MADV_DONTFORK)
so why do we need to do it here as well?
If we should be doing it in all cases presumably the right fix is to move the if (!kvm_has_sync_mmu()) check in kvm_setup_guest_memory() from "do we call madvise" to "do we fail with an error if it failed".

thanks
-- PMM

#2 lore raw
Quoted 11 lines · Peter Maydell
Quoted 10 lines · Andrea Arcangeli
exec.c +1 -0
index c99a883..d3bb58d 100644
@@ -1162,6 +1162,7 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
 
     qemu_ram_setup_dump(new_block->host, size);
     qemu_madvise(new_block->host, size, QEMU_MADV_HUGEPAGE);
+    qemu_madvise(new_block->host, size, QEMU_MADV_DONTFORK);
 
Quoted 7 lines

exec.c ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host, · line 1168

Quoted 3 lines
exec.c +0 -0
     if (kvm_enabled())
         kvm_setup_guest_memory(new_block->host, size);

kvm_setup_guest_memory() already calls
qemu_madvise(start, size, QEMU_MADV_DONTFORK)
so why do we need to do it here as well?

That only runs if kvm is enabled and mmu is not sync. But we need it in the common case too, to prevent -ENOMEM (if MADV_DONTFORK is available in the host OS, otherwise well we'll just do best effort and skip). See commit message for more details.

Quoted 4 lines

If we should be doing it in all cases presumably the right fix is to move the if (!kvm_has_sync_mmu()) check in kvm_setup_guest_memory() from "do we call madvise" to "do we fail with an error if it failed".

We could pass an error to kvm_setup_guest_memory but it's not worth it considering more likely we should abort if kvm is enabled and mmu is not sync (without bothering to call MADV_DONTFORK there).

#5 lore raw

[Qemu-devel] PING for-1.6 Re: [PATCH] KVM: always use MADV_DONTFORK

In reply to Andrea Arcangeli
Quoted 27 lines · Andrea Arcangeli

MADV_DONTFORK prevents fork to fail with -ENOMEM if the default overcommit heuristics decides there's too much anonymous virtual memory allocated. If the KVM secondary MMU is synchronized with MMU notifiers or not, doesn't make a difference in that regard.

Secondly it's always more efficient to avoid copying the guest physical address space in the fork child (so we avoid to mark all the guest memory readonly in the parent and so we skip the establishment and teardown of lots of pagetables in the child).

In the common case we can ignore the error if MADV_DONTFORK is not available. Leave a second invocation that errors out in the KVM path if MMU notifiers are missing and KVM is enabled, to abort in such case.

---
  exec.c | 1 +
  1 file changed, 1 insertion(+)
exec.c +0 -0
index c99a883..d3bb58d 100644

exec.c ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host, · line 1168

Quoted 7 lines
exec.c +1 -0
@@ -1162,6 +1162,7 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
 
      qemu_ram_setup_dump(new_block->host, size);
      qemu_madvise(new_block->host, size, QEMU_MADV_HUGEPAGE);
+    qemu_madvise(new_block->host, size, QEMU_MADV_DONTFORK);
 
      if (kvm_enabled())
          kvm_setup_guest_memory(new_block->host, size);

PING.

Benoit reported this on IRC, too.

Paolo

#3 lore raw
In reply to Andrea Arcangeli

Am 25.07.2013 12:32, schrieb Andrea Arcangeli:

Quoted 20 lines · Peter Maydell
Quoted 11 lines · Andrea Arcangeli
Quoted 10 lines
exec.c +1 -0
index c99a883..d3bb58d 100644
@@ -1162,6 +1162,7 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
 
     qemu_ram_setup_dump(new_block->host, size);
     qemu_madvise(new_block->host, size, QEMU_MADV_HUGEPAGE);
+    qemu_madvise(new_block->host, size, QEMU_MADV_DONTFORK);
 
Quoted 7 lines

exec.c ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host, · line 1168

Quoted 3 lines
exec.c +0 -0
     if (kvm_enabled())
         kvm_setup_guest_memory(new_block->host, size);

kvm_setup_guest_memory() already calls
qemu_madvise(start, size, QEMU_MADV_DONTFORK)
so why do we need to do it here as well?

Quoted 4 lines

That only runs if kvm is enabled and mmu is not sync. But we need it in the common case too, to prevent -ENOMEM (if MADV_DONTFORK is available in the host OS, otherwise well we'll just do best effort and skip). See commit message for more details.

So if we add the DONTFORK unconditionally here, why not drop it in said kvm_setup_guest_memory()? That would make the patch more self-documenting while at it.

Andreas

Quoted 10 lines
Quoted 5 lines

If we should be doing it in all cases presumably the right fix is to move the if (!kvm_has_sync_mmu()) check in kvm_setup_guest_memory() from "do we call madvise" to "do we fail with an error if it failed".

We could pass an error to kvm_setup_guest_memory but it's not worth it considering more likely we should abort if kvm is enabled and mmu is not sync (without bothering to call MADV_DONTFORK there).

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
Tested-by Benoit Canet on v1 1/1
#11 lore raw
In reply to Andrea Arcangeli
Quoted 26 lines · Andrea Arcangeli

MADV_DONTFORK prevents fork to fail with -ENOMEM if the default overcommit heuristics decides there's too much anonymous virtual memory allocated. If the KVM secondary MMU is synchronized with MMU notifiers or not, doesn't make a difference in that regard.

Secondly it's always more efficient to avoid copying the guest physical address space in the fork child (so we avoid to mark all the guest memory readonly in the parent and so we skip the establishment and teardown of lots of pagetables in the child).

In the common case we can ignore the error if MADV_DONTFORK is not available. Leave a second invocation that errors out in the KVM path if MMU notifiers are missing and KVM is enabled, to abort in such case.

---
 exec.c | 1 +
 1 file changed, 1 insertion(+)
exec.c +0 -0
index c99a883..d3bb58d 100644

exec.c ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host, · line 1168

Quoted 7 lines
exec.c +1 -0
@@ -1162,6 +1162,7 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
 
     qemu_ram_setup_dump(new_block->host, size);
     qemu_madvise(new_block->host, size, QEMU_MADV_HUGEPAGE);
+    qemu_madvise(new_block->host, size, QEMU_MADV_DONTFORK);
 
     if (kvm_enabled())
         kvm_setup_guest_memory(new_block->host, size);

This patch solve a bug where the network down script of a regular tap interface is not executed because fork fail when a pci-assigment is done on a large guest.

#6 lore raw

[Qemu-devel] PING^2 Re: [PATCH] KVM: always use MADV_DONTFORK

In reply to Paolo Bonzini

Il 06/08/2013 18:47, Paolo Bonzini ha scritto:

Quoted 39 lines · Andrea Arcangeli
Quoted 32 lines

MADV_DONTFORK prevents fork to fail with -ENOMEM if the default overcommit heuristics decides there's too much anonymous virtual memory allocated. If the KVM secondary MMU is synchronized with MMU notifiers or not, doesn't make a difference in that regard.

Secondly it's always more efficient to avoid copying the guest physical address space in the fork child (so we avoid to mark all the guest memory readonly in the parent and so we skip the establishment and teardown of lots of pagetables in the child).

In the common case we can ignore the error if MADV_DONTFORK is not available. Leave a second invocation that errors out in the KVM path if MMU notifiers are missing and KVM is enabled, to abort in such case.

---
  exec.c | 1 +
  1 file changed, 1 insertion(+)
exec.c +1 -0
index c99a883..d3bb58d 100644
@@ -1162,6 +1162,7 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t
 size, void *host,
 
      qemu_ram_setup_dump(new_block->host, size);
      qemu_madvise(new_block->host, size, QEMU_MADV_HUGEPAGE);
+    qemu_madvise(new_block->host, size, QEMU_MADV_DONTFORK);
 
      if (kvm_enabled())
Quoted 3 lines
          kvm_setup_guest_memory(new_block->host, size);

PING.

Quoted 5 lines

Benoit reported this on IRC, too.

Paolo

PING^2

The last paragraph of the commit message should answer Andreas's objection.

Paolo

#4 lore raw
In reply to Andreas Färber

Il 06/08/2013 18:55, Andreas Färber ha scritto:

Quoted 21 lines

Am 25.07.2013 12:32, schrieb Andrea Arcangeli:

Quoted 20 lines · Peter Maydell
Quoted 18 lines · Andrea Arcangeli

exec.c ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host, · line 1168

Quoted 13 lines
exec.c +1 -0
index c99a883..d3bb58d 100644
@@ -1162,6 +1162,7 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
 
     qemu_ram_setup_dump(new_block->host, size);
     qemu_madvise(new_block->host, size, QEMU_MADV_HUGEPAGE);
+    qemu_madvise(new_block->host, size, QEMU_MADV_DONTFORK);
 
     if (kvm_enabled())
         kvm_setup_guest_memory(new_block->host, size);

kvm_setup_guest_memory() already calls
qemu_madvise(start, size, QEMU_MADV_DONTFORK)
so why do we need to do it here as well?

Quoted 8 lines
Quoted 4 lines

That only runs if kvm is enabled and mmu is not sync. But we need it in the common case too, to prevent -ENOMEM (if MADV_DONTFORK is available in the host OS, otherwise well we'll just do best effort and skip). See commit message for more details.

So if we add the DONTFORK unconditionally here, why not drop it in said kvm_setup_guest_memory()? That would make the patch more self-documenting while at it.

This is mentioned in the commit message:

"In the common case we can ignore the error if MADV_DONTFORK is not available. Leave a second invocation that errors out in the KVM path if MMU notifiers are missing and KVM is enabled, to abort in such case".

Paolo

#7 lore raw

Re: [Qemu-devel] PING^2 Re: [PATCH] KVM: always use MADV_DONTFORK

In reply to Paolo Bonzini

Am 30.08.2013 17:48, schrieb Paolo Bonzini:

Quoted 42 lines

Il 06/08/2013 18:47, Paolo Bonzini ha scritto:

Quoted 41 lines · Andrea Arcangeli
Quoted 32 lines

MADV_DONTFORK prevents fork to fail with -ENOMEM if the default overcommit heuristics decides there's too much anonymous virtual memory allocated. If the KVM secondary MMU is synchronized with MMU notifiers or not, doesn't make a difference in that regard.

Secondly it's always more efficient to avoid copying the guest physical address space in the fork child (so we avoid to mark all the guest memory readonly in the parent and so we skip the establishment and teardown of lots of pagetables in the child).

In the common case we can ignore the error if MADV_DONTFORK is not available. Leave a second invocation that errors out in the KVM path if MMU notifiers are missing and KVM is enabled, to abort in such case.

---
  exec.c | 1 +
  1 file changed, 1 insertion(+)
exec.c +1 -0
index c99a883..d3bb58d 100644
@@ -1162,6 +1162,7 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t
 size, void *host,
 
      qemu_ram_setup_dump(new_block->host, size);
      qemu_madvise(new_block->host, size, QEMU_MADV_HUGEPAGE);
+    qemu_madvise(new_block->host, size, QEMU_MADV_DONTFORK);
 
      if (kvm_enabled())
Quoted 3 lines
          kvm_setup_guest_memory(new_block->host, size);

PING.

Benoit reported this on IRC, too.

Quoted 7 lines
Quoted 3 lines

Paolo

PING^2

The last paragraph of the commit message should answer Andreas's objection.

OK. The patch is mislabelled as "KVM:" though putting this in a common code path and touching exec.c only, so please put it on uq/master and fix that up before you set the history in stone. :)

Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#8 lore raw

Re: [Qemu-devel] PING^2 Re: [PATCH] KVM: always use MADV_DONTFORK

Il 30/08/2013 17:52, Andreas Färber ha scritto:

Quoted 3 lines

OK. The patch is mislabelled as "KVM:" though putting this in a common code path and touching exec.c only, so please put it on uq/master and fix that up before you set the history in stone. :)

Well, touching exec.c is why I wasn't picking it up through uq/master, but that's indeed a good observation!

If Anthony doesn't pick it up, I'll do so as soon as it's my turn to manage the qemu-kvm.git repository.

Paolo

#9 lore raw

Re: [Qemu-devel] PING^2 Re: [PATCH] KVM: always use MADV_DONTFORK

Quoted 11 lines · Paolo Bonzini

Il 30/08/2013 17:52, Andreas Färber ha scritto:

Quoted 3 lines

OK. The patch is mislabelled as "KVM:" though putting this in a common code path and touching exec.c only, so please put it on uq/master and fix that up before you set the history in stone. :)

Well, touching exec.c is why I wasn't picking it up through uq/master, but that's indeed a good observation!

If Anthony doesn't pick it up, I'll do so as soon as it's my turn to manage the qemu-kvm.git repository.

Send your acks and I will merge it.

--
Gleb.

Acked-by
#10 lore raw

Re: [Qemu-devel] PING^2 Re: [PATCH] KVM: always use MADV_DONTFORK

Il 30/08/2013 18:21, Gleb Natapov ha scritto:

Quoted 6 lines · Paolo Bonzini
Quoted 5 lines

Il 30/08/2013 17:52, Andreas Färber ha scritto:

Quoted 3 lines

OK. The patch is mislabelled as "KVM:" though putting this in a common code path and touching exec.c only, so please put it on uq/master and fix that up before you set the history in stone. :)

Quoted 7 lines
Quoted 6 lines

Well, touching exec.c is why I wasn't picking it up through uq/master, but that's indeed a good observation!

If Anthony doesn't pick it up, I'll do so as soon as it's my turn to manage the qemu-kvm.git repository.

Send your acks and I will merge it.

with s/KVM/exec/ in the subject line

Applied
#12 lore raw
In reply to Andrea Arcangeli
Quoted 10 lines · Andrea Arcangeli

MADV_DONTFORK prevents fork to fail with -ENOMEM if the default overcommit heuristics decides there's too much anonymous virtual memory allocated. If the KVM secondary MMU is synchronized with MMU notifiers or not, doesn't make a difference in that regard.

Secondly it's always more efficient to avoid copying the guest physical address space in the fork child (so we avoid to mark all the guest memory readonly in the parent and so we skip the establishment and teardown of lots of pagetables in the child).

Quoted 6 lines

In the common case we can ignore the error if MADV_DONTFORK is not available. Leave a second invocation that errors out in the KVM path if MMU notifiers are missing and KVM is enabled, to abort in such case.

Applied to uq/master, thanks.

Quoted 9 lines
---
 exec.c | 1 +
 1 file changed, 1 insertion(+)
exec.c +0 -0
index c99a883..d3bb58d 100644

exec.c ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host, · line 1168

Quoted 7 lines
exec.c +1 -0
@@ -1162,6 +1162,7 @@ ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
 
     qemu_ram_setup_dump(new_block->host, size);
     qemu_madvise(new_block->host, size, QEMU_MADV_HUGEPAGE);
+    qemu_madvise(new_block->host, size, QEMU_MADV_DONTFORK);
 
     if (kvm_enabled())
         kvm_setup_guest_memory(new_block->host, size);

--
Gleb.