Skip to contentSkip to conversation

When close VM the hugepage not freed

Qemu-devel Linhaifeng 3 participants 6 messages Oct 14, 2014 – Oct 15, 2014

lore
Share & tools
lore

Linhaifeng

author
#0 lore raw

Hi,all

I was trying to use hugepage with VM and found that the hugepage not freed when close VM.

1.Before start VM the /proc/meminfo is:

AnonHugePages:    124928 kB
HugePages_Total:    4096
HugePages_Free:     3072
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB

2.Start VM the /proc/meminfo is:

AnonHugePages:    139264 kB
HugePages_Total:    4096
HugePages_Free:     2048
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB

3.Close VM the /proc/meminfo is:

AnonHugePages:    124928 kB
HugePages_Total:    4096
HugePages_Free:     2048
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB

We can see there are 1024 hugepage leak!

I try to found which function used to free hugepage but i'm not sure where the qemu_ram_free is the function to free hugepage. I found that the qemu_ram_free function not call unlink and we know unlink is used to free hugepage(see example of hugepage-mmap.c in kernel source).

void qemu_ram_free(ram_addr_t addr)

{
    RAMBlock *block;

    /* This assumes the iothread lock is taken here too.  */
    qemu_mutex_lock_ramlist();
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
        if (addr == block->offset) {
            QTAILQ_REMOVE(&ram_list.blocks, block, next);
            ram_list.mru_block = NULL;
            ram_list.version++;
            if (block->flags & RAM_PREALLOC) {
                ;
            } else if (xen_enabled()) {
                xen_invalidate_map_cache_entry(block->host);
#ifndef _WIN32
            } else if (block->fd >= 0) {
                munmap(block->host, block->length);
                close(block->fd);
		// should we add unlink here to free hugepage?
#endif
            } else {
                qemu_anon_ram_free(block->host, block->length);
            }
            g_free(block);
            break;
        }
    }
    qemu_mutex_unlock_ramlist();

}

#1 lore raw
Quoted 29 lines · Linhaifeng

Hi,all

I was trying to use hugepage with VM and found that the hugepage not freed when close VM.

1.Before start VM the /proc/meminfo is:

AnonHugePages:    124928 kB
HugePages_Total:    4096
HugePages_Free:     3072
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB

2.Start VM the /proc/meminfo is:

AnonHugePages:    139264 kB
HugePages_Total:    4096
HugePages_Free:     2048
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB

3.Close VM the /proc/meminfo is:

AnonHugePages:    124928 kB
HugePages_Total:    4096
HugePages_Free:     2048
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
Quoted 7 lines

We can see there are 1024 hugepage leak!

I try to found which function used to free hugepage but i'm not sure where the qemu_ram_free is the function to free hugepage. I found that the qemu_ram_free function not call unlink and we know unlink is used to free hugepage(see example of hugepage-mmap.c in kernel source).

We can't rely on 'qemu_ram_free' ever executing because we must ensure hugepages are freed upon QEMU crash.

It seems we should rely on UNIX filesytstem semantics and simply unlink the memory segment the moment we create it & open the FD. That way the kernel will automatically free it when the FD is closed when QEMU process exits.

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
#2 lore raw
Quoted 41 lines · Daniel P. Berrange
Quoted 29 lines · Linhaifeng

Hi,all

I was trying to use hugepage with VM and found that the hugepage not freed when close VM.

1.Before start VM the /proc/meminfo is:

AnonHugePages:    124928 kB
HugePages_Total:    4096
HugePages_Free:     3072
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB

2.Start VM the /proc/meminfo is:

AnonHugePages:    139264 kB
HugePages_Total:    4096
HugePages_Free:     2048
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB

3.Close VM the /proc/meminfo is:

AnonHugePages:    124928 kB
HugePages_Total:    4096
HugePages_Free:     2048
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
Quoted 7 lines

We can see there are 1024 hugepage leak!

I try to found which function used to free hugepage but i'm not sure where the qemu_ram_free is the function to free hugepage. I found that the qemu_ram_free function not call unlink and we know unlink is used to free hugepage(see example of hugepage-mmap.c in kernel source).

We can't rely on 'qemu_ram_free' ever executing because we must ensure hugepages are freed upon QEMU crash.

Quoted 8 lines

It seems we should rely on UNIX filesytstem semantics and simply unlink the memory segment the moment we create it & open the FD. That way the kernel will automatically free it when the FD is closed when QEMU process exits.

Regards,
Daniel

We being libvirt?

Quoted 5 lines
Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
author
#4 lore raw
In reply to Daniel P. Berrange
Quoted 41 lines · Daniel P. Berrange
Quoted 29 lines · Linhaifeng

Hi,all

I was trying to use hugepage with VM and found that the hugepage not freed when close VM.

1.Before start VM the /proc/meminfo is:

AnonHugePages:    124928 kB
HugePages_Total:    4096
HugePages_Free:     3072
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB

2.Start VM the /proc/meminfo is:

AnonHugePages:    139264 kB
HugePages_Total:    4096
HugePages_Free:     2048
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB

3.Close VM the /proc/meminfo is:

AnonHugePages:    124928 kB
HugePages_Total:    4096
HugePages_Free:     2048
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
Quoted 7 lines

We can see there are 1024 hugepage leak!

I try to found which function used to free hugepage but i'm not sure where the qemu_ram_free is the function to free hugepage. I found that the qemu_ram_free function not call unlink and we know unlink is used to free hugepage(see example of hugepage-mmap.c in kernel source).

We can't rely on 'qemu_ram_free' ever executing because we must ensure hugepages are freed upon QEMU crash.

Quoted 9 lines

It seems we should rely on UNIX filesytstem semantics and simply unlink the memory segment the moment we create it & open the FD. That way the kernel will automatically free it when the FD is closed when QEMU process exits.

Regards,
Daniel

Hi, daniel

Thank you for your answer.

Does it means libvirt should free the hugepage?

QEMU create the hugepage with template file and unlink it before mmap. Do you know why to unlink the hugepage before mmap?

When unlink the hugepage before mmap libvirt cannot found the hugepage. How does libvirt to free the hugepage ?

Regards,
Haifeng

#3 lore raw
In reply to Michael S. Tsirkin
Quoted 42 lines · Michael S. Tsirkin
Quoted 41 lines · Daniel P. Berrange
Quoted 29 lines · Linhaifeng

Hi,all

I was trying to use hugepage with VM and found that the hugepage not freed when close VM.

1.Before start VM the /proc/meminfo is:

AnonHugePages:    124928 kB
HugePages_Total:    4096
HugePages_Free:     3072
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB

2.Start VM the /proc/meminfo is:

AnonHugePages:    139264 kB
HugePages_Total:    4096
HugePages_Free:     2048
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB

3.Close VM the /proc/meminfo is:

AnonHugePages:    124928 kB
HugePages_Total:    4096
HugePages_Free:     2048
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
Quoted 7 lines

We can see there are 1024 hugepage leak!

I try to found which function used to free hugepage but i'm not sure where the qemu_ram_free is the function to free hugepage. I found that the qemu_ram_free function not call unlink and we know unlink is used to free hugepage(see example of hugepage-mmap.c in kernel source).

We can't rely on 'qemu_ram_free' ever executing because we must ensure hugepages are freed upon QEMU crash.

Quoted 6 lines
Quoted 5 lines

It seems we should rely on UNIX filesytstem semantics and simply unlink the memory segment the moment we create it & open the FD. That way the kernel will automatically free it when the FD is closed when QEMU process exits.

We being libvirt?

No, QEMU is responsible for creating the mmap file, so QEMU should take care of unlinking it after opening it.

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
author
#5 lore raw
In reply to Linhaifeng
Quoted 64 lines · Linhaifeng

Hi,all

I was trying to use hugepage with VM and found that the hugepage not freed when close VM.

1.Before start VM the /proc/meminfo is:

AnonHugePages:    124928 kB
HugePages_Total:    4096
HugePages_Free:     3072
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB

2.Start VM the /proc/meminfo is:

AnonHugePages:    139264 kB
HugePages_Total:    4096
HugePages_Free:     2048
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB

3.Close VM the /proc/meminfo is:

AnonHugePages:    124928 kB
HugePages_Total:    4096
HugePages_Free:     2048
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB

We can see there are 1024 hugepage leak!

I try to found which function used to free hugepage but i'm not sure where the qemu_ram_free is the function to free hugepage. I found that the qemu_ram_free function not call unlink and we know unlink is used to free hugepage(see example of hugepage-mmap.c in kernel source).

void qemu_ram_free(ram_addr_t addr)

{
    RAMBlock *block;

    /* This assumes the iothread lock is taken here too.  */
    qemu_mutex_lock_ramlist();
    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
        if (addr == block->offset) {
            QTAILQ_REMOVE(&ram_list.blocks, block, next);
            ram_list.mru_block = NULL;
            ram_list.version++;
            if (block->flags & RAM_PREALLOC) {
                ;
            } else if (xen_enabled()) {
                xen_invalidate_map_cache_entry(block->host);
#ifndef _WIN32
            } else if (block->fd >= 0) {
                munmap(block->host, block->length);
                close(block->fd);
		// should we add unlink here to free hugepage?
#endif
            } else {
                qemu_anon_ram_free(block->host, block->length);
            }
            g_free(block);
            break;
        }
    }
    qemu_mutex_unlock_ramlist();
Quoted 5 lines

}

When i run the QEMU with tap backend the hugepage will be freed but not the vhost-user backend. Maybe the vhost-user process should close the hugefile.

Signature · 2 lines
Regards,
Haifeng