Bug 2056725

Summary: qemu-kvm leaks on qemu_rbd_snap_list
Product: Red Hat Enterprise Linux 7 Reporter: Germano Veit Michel <gveitmic>
Component: qemu-kvmAssignee: Stefano Garzarella <sgarzare>
Status: CLOSED ERRATA QA Contact: Tingting Mao <timao>
Severity: urgent Docs Contact:
Priority: urgent    
Version: 7.9CC: coli, jferlan, jinzhao, jmaloy, juzhang, kkiwi, kpfleming, lob+redhat, sgarzare, system-engineering, virt-maint, ymankad
Target Milestone: rcKeywords: Triaged, ZStream
Target Release: ---   
Hardware: x86_64   
OS: All   
Whiteboard:
Fixed In Version: qemu-kvm-1.5.3-175.el7_9.6 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2022-05-18 16:15:18 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Germano Veit Michel 2022-02-21 23:16:06 UTC
Description of problem:

Customer qemu-kvm processes memory usage over time far exceeds the memory allocated for the Guest. For example, a Guest configured with 8G of RAM is seen using 32G and so on. OOM killer is frequently seen unless the customer keeps migrating VMs around.

Using valgrind massif, we found the root cause:

n5: 1623668055 (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
 n3: 1358331883 0x67D775D: g_malloc (gmem.c:99)
  n1: 1290516000 0x1B7BB4: qemu_rbd_snap_list (rbd.c:953)
   n1: 1290516000 0x1A3D1B: bdrv_query_snapshot_info_list (qapi.c:46)
    n1: 1290516000 0x1A3FD4: bdrv_query_image_info (qapi.c:162)
     n1: 1290516000 0x1A42B8: bdrv_query_info (qapi.c:249)
      n1: 1290516000 0x1A451A: qmp_query_block (qapi.c:314)
       n1: 1290516000 0x299B17: qmp_marshal_input_query_block (qmp-marshal.c:787)
        n1: 1290516000 0x3298A6: qmp_call_cmd (monitor.c:4511)
         n1: 1290516000 0x3298A6: handle_qmp_command (monitor.c:4577)
          n1: 1290516000 0x3D7DA7: json_message_process_token (json-streamer.c:104)
           n1: 1290516000 0x3E708A: json_lexer_feed_char (json-lexer.c:310)
            n1: 1290516000 0x3E714D: json_lexer_feed (json-lexer.c:360)
             n1: 1290516000 0x3285F2: monitor_control_read (monitor.c:4598)
              n1: 1290516000 0x291030: qemu_chr_be_write (qemu-char.c:167)
               n1: 1290516000 0x291030: tcp_chr_read (qemu-char.c:2496)
                n1: 1290516000 0x67D2118: g_main_dispatch (gmain.c:3175)
                 n1: 1290516000 0x67D2118: g_main_context_dispatch (gmain.c:3828)
                  n1: 1290516000 0x263269: UnknownInlinedFun (main-loop.c:187)
                   n1: 1290516000 0x263269: UnknownInlinedFun (main-loop.c:235)
                    n1: 1290516000 0x263269: main_loop_wait (main-loop.c:475)
                     n1: 1290516000 0x1818BF: main_loop (vl.c:1997)
                      n0: 1290516000 0x1818BF: main (vl.c:4367)

Tracked this down to this change which was not backported to RHEL 7.9.z.

commit 9e6337d0818650362149b734d53edf9489f3acaa
Author: Stefan Hajnoczi <stefanha>
Date:   Wed Sep 25 16:00:48 2013 +0200

    rbd: avoid qemu_rbd_snap_list() memory leaks
    
    When there are no snapshots qemu_rbd_snap_list() returns 0 and the
    snapshot table pointer is NULL.  Don't forget to free the snaps buffer
    we allocated for librbd rbd_snap_list().
    
    When the function succeeds don't forget to free the snaps buffer after
    calling rbd_snap_list_end().
    
    Cc: qemu-stable
    Signed-off-by: Stefan Hajnoczi <stefanha>
    Signed-off-by: Kevin Wolf <kwolf>

diff --git a/block/rbd.c b/block/rbd.c
index f6d3237179..4a1ea5b5ce 100644
--- a/block/rbd.c
+++ b/block/rbd.c
@@ -943,7 +943,7 @@ static int qemu_rbd_snap_list(BlockDriverState *bs,
     do {
         snaps = g_malloc(sizeof(*snaps) * max_snaps);
         snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
-        if (snap_count < 0) {
+        if (snap_count <= 0) {
             g_free(snaps);
         }
     } while (snap_count == -ERANGE);
@@ -967,6 +967,7 @@ static int qemu_rbd_snap_list(BlockDriverState *bs,
         sn_info->vm_clock_nsec = 0;
     }
     rbd_snap_list_end(snaps);
+    g_free(snaps);
 
  done:
     *psn_tab = sn_tab;

Then built a test package for the customer based on latest 7.9.z with the patch above.
http://brew-task-repos.usersys.redhat.com/repos/scratch/gveitmic/qemu-kvm/1.5.3/175.el7.5.sfdc02961627/x86_64/

After this the problem was gone.

Version-Release number of selected component (if applicable):
qemu-kvm-1.5.3-175.el7_9.5.x86_64

How reproducible:
Always

Steps to Reproduce:
1. Configure qemu-kvm guest with RBD disk, and no snapshots
2. Run this in a loop
   while true ; do date ; virsh domblkinfo <vm name> --all ; sleep 0.1 ; done

Actual results:
* qemu-kvm memory usage goes through the roof

Expected results:
* no leaks

Comment 26 Tingting Mao 2022-03-03 06:33:09 UTC
Stefano and Bermano: Thanks for your info, when tried with 10 RBD disks and removed all the prints/sleeps from the stress test, I reproduced the issue. And also tried with the latest qemu, there is no the issue anymore. 

So I will set this bug as verified accordingly. Thanks again.



Reproduced with:
qemu-kvm-1.5.3-175.el7_9.5.x86_64
kernel-3.10.0-1160.el7.x86_64

Results:
The used memory of qemu-kvm pid is increased continuely, and after about 1 hour 10 minutes, the used memory increased to ~20G as below. And the size is still increased continuely.
# cat /proc/3274843/status | grep VmRSS
VmRSS:	21124024 kB


Verified with:
qemu-kvm-1.5.3-175.el7_9.6.x86_64
kernel-3.10.0-1160.el7.x86_64

Results:
The used memory of qemu-kvm pid is not increased, and after about 1 hour 20 minutes, the memory size is still ~1G.
# cat /proc/3412360/status | grep VmRSS
VmRSS:	  970676 kB


Steps:
1. Define the VM
# virsh define rhel7.9.xml

Note:
# cat rhel7.9.xml 
<domain type='kvm' id='2'>
  <name>rhel7.9</name>
  <uuid>33e879e7-6591-4f07-8ac0-2ddc33807f1b</uuid>
  <memory unit='KiB'>8388608</memory>
  <currentMemory unit='KiB'>8388608</currentMemory>
  <vcpu placement='static'>2</vcpu>
  <resource>
    <partition>/machine</partition>
  </resource>
  <os>
    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
  </features>
  <cpu mode='custom' match='exact' check='full'>
    <model fallback='forbid'>Skylake-Server-IBRS</model>
    <vendor>Intel</vendor>
    <topology sockets='1' cores='1' threads='2'/>
    <feature policy='disable' name='ds'/>
    <feature policy='disable' name='acpi'/>
    <feature policy='require' name='ss'/>
    <feature policy='disable' name='ht'/>
    <feature policy='disable' name='tm'/>
    <feature policy='disable' name='pbe'/>
    <feature policy='disable' name='dtes64'/>
    <feature policy='disable' name='monitor'/>
    <feature policy='disable' name='ds_cpl'/>
    <feature policy='disable' name='vmx'/>
    <feature policy='disable' name='smx'/>
    <feature policy='disable' name='est'/>
    <feature policy='disable' name='tm2'/>
    <feature policy='disable' name='xtpr'/>
    <feature policy='disable' name='pdcm'/>
    <feature policy='disable' name='dca'/>
    <feature policy='disable' name='osxsave'/>
    <feature policy='disable' name='tsc_adjust'/>
    <feature policy='require' name='clflushopt'/>
    <feature policy='disable' name='intel-pt'/>
    <feature policy='require' name='pku'/>
    <feature policy='disable' name='ospke'/>
    <feature policy='require' name='md-clear'/>
    <feature policy='require' name='stibp'/>
    <feature policy='require' name='ssbd'/>
    <feature policy='require' name='hypervisor'/>
    <feature policy='disable' name='arat'/>
  </cpu>
  <clock offset='utc'>
    <timer name='rtc' tickpolicy='catchup'/>
    <timer name='pit' tickpolicy='delay'/>
    <timer name='hpet' present='no'/>
  </clock>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <pm>
    <suspend-to-mem enabled='no'/>
    <suspend-to-disk enabled='no'/>
  </pm>
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2' cache='none'/>
      <source file='/home/timao/RHEL-7.9-x86_64-latest.qcow2'/>
      <backingStore/>
      <target dev='sda' bus='scsi'/>
      <alias name='scsi0-0-0-0'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
    <disk type='network' device='disk'>
      <driver name='qemu' type='raw' cache='none'/>
      <auth username='admin'>
        <secret type='ceph' uuid='7c1912af-3a87-40c1-a1e4-9924ec83cb14'/>
      </auth>
      <source protocol='rbd' name='rbd/disk'>
	<host name='10.73.114.12' port='6789'/>
      </source>
      <target dev='sdb' bus='scsi'/>
    </disk>
    <disk type='network' device='disk'>
      <driver name='qemu' type='raw' cache='none'/>
      <auth username='admin'>
        <secret type='ceph' uuid='7c1912af-3a87-40c1-a1e4-9924ec83cb14'/>
      </auth>
      <source protocol='rbd' name='rbd/disk1'>
	<host name='10.73.114.12' port='6789'/>
      </source>
      <target dev='sdc' bus='scsi'/>
    </disk>
    <disk type='network' device='disk'>
      <driver name='qemu' type='raw' cache='none'/>
      <auth username='admin'>
        <secret type='ceph' uuid='7c1912af-3a87-40c1-a1e4-9924ec83cb14'/>
      </auth>
      <source protocol='rbd' name='rbd/disk2'>
	<host name='10.73.114.12' port='6789'/>
      </source>
      <target dev='sdd' bus='scsi'/>
    </disk>
    <disk type='network' device='disk'>
      <driver name='qemu' type='raw' cache='none'/>
      <auth username='admin'>
        <secret type='ceph' uuid='7c1912af-3a87-40c1-a1e4-9924ec83cb14'/>
      </auth>
      <source protocol='rbd' name='rbd/disk3'>
	<host name='10.73.114.12' port='6789'/>
      </source>
      <target dev='sde' bus='scsi'/>
    </disk>
    <disk type='network' device='disk'>
      <driver name='qemu' type='raw' cache='none'/>
      <auth username='admin'>
        <secret type='ceph' uuid='7c1912af-3a87-40c1-a1e4-9924ec83cb14'/>
      </auth>
      <source protocol='rbd' name='rbd/disk4'>
	<host name='10.73.114.12' port='6789'/>
      </source>
      <target dev='sdf' bus='scsi'/>
    </disk>
    <disk type='network' device='disk'>
      <driver name='qemu' type='raw' cache='none'/>
      <auth username='admin'>
        <secret type='ceph' uuid='7c1912af-3a87-40c1-a1e4-9924ec83cb14'/>
      </auth>
      <source protocol='rbd' name='rbd/disk5'>
	<host name='10.73.114.12' port='6789'/>
      </source>
      <target dev='sdj' bus='scsi'/>
    </disk>
    <disk type='network' device='disk'>
      <driver name='qemu' type='raw' cache='none'/>
      <auth username='admin'>
        <secret type='ceph' uuid='7c1912af-3a87-40c1-a1e4-9924ec83cb14'/>
      </auth>
      <source protocol='rbd' name='rbd/disk6'>
	<host name='10.73.114.12' port='6789'/>
      </source>
      <target dev='sdh' bus='scsi'/>
    </disk>
    <disk type='network' device='disk'>
      <driver name='qemu' type='raw' cache='none'/>
      <auth username='admin'>
        <secret type='ceph' uuid='7c1912af-3a87-40c1-a1e4-9924ec83cb14'/>
      </auth>
      <source protocol='rbd' name='rbd/disk7'>
	<host name='10.73.114.12' port='6789'/>
      </source>
      <target dev='sdi' bus='scsi'/>
    </disk>
    <disk type='network' device='disk'>
      <driver name='qemu' type='raw' cache='none'/>
      <auth username='admin'>
        <secret type='ceph' uuid='7c1912af-3a87-40c1-a1e4-9924ec83cb14'/>
      </auth>
      <source protocol='rbd' name='rbd/disk8'>
	<host name='10.73.114.12' port='6789'/>
      </source>
      <target dev='sdg' bus='scsi'/>
    </disk>
    <disk type='network' device='disk'>
      <driver name='qemu' type='raw' cache='none'/>
      <auth username='admin'>
        <secret type='ceph' uuid='7c1912af-3a87-40c1-a1e4-9924ec83cb14'/>
      </auth>
      <source protocol='rbd' name='rbd/disk9'>
	<host name='10.73.114.12' port='6789'/>
      </source>
      <target dev='sdk' bus='scsi'/>
    </disk>
    <controller type='usb' index='0' model='ich9-ehci1'>
      <alias name='usb'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci1'>
      <alias name='usb'/>
      <master startport='0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci2'>
      <alias name='usb'/>
      <master startport='2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci3'>
      <alias name='usb'/>
      <master startport='4'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'>
      <alias name='pci.0'/>
    </controller>
    <controller type='scsi' index='0' model='virtio-scsi'>
      <alias name='scsi0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </controller>
    <interface type='bridge'>
      <mac address='52:54:00:78:3c:d9'/>
      <source bridge='switch'/>
      <target dev='vnet0'/>
      <model type='rtl8139'/>
      <alias name='net0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <serial type='unix'>
      <source mode='bind' path='/tmp/rhel8.6'/>
      <target type='isa-serial' port='0'>
        <model name='isa-serial'/>
      </target>
      <alias name='serial0'/>
    </serial>
    <console type='unix'>
      <source mode='bind' path='/tmp/rhel8.6'/>
      <target type='serial' port='0'/>
      <alias name='serial0'/>
    </console>
    <input type='mouse' bus='ps2'>
      <alias name='input0'/>
    </input>
    <input type='keyboard' bus='ps2'>
      <alias name='input1'/>
    </input>
    <graphics type='vnc' port='5900' autoport='yes' listen='0.0.0.0'>
      <listen type='address' address='0.0.0.0'/>
    </graphics>
    <video>
      <model type='cirrus' vram='16384' heads='1' primary='yes'/>
      <alias name='video0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <memballoon model='virtio'>
      <alias name='balloon0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
    </memballoon>
  </devices>
  <seclabel type='dynamic' model='selinux' relabel='yes'>
    <label>system_u:system_r:svirt_t:s0:c296,c922</label>
    <imagelabel>system_u:object_r:svirt_image_t:s0:c296,c922</imagelabel>
  </seclabel>
  <seclabel type='dynamic' model='dac' relabel='yes'>
    <label>+107:+107</label>
    <imagelabel>+107:+107</imagelabel>
  </seclabel>
</domain>

2. Start the VM
# virsh start rhel7.9

3. Stress to the VM
# while true; do virsh domblkinfo rhel7.9 --all;done

Comment 33 errata-xmlrpc 2022-05-18 16:15:18 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory (qemu-kvm bug fix and enhancement update), and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHBA-2022:4639