From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4858AC5321E for ; Mon, 26 Aug 2024 15:56:29 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sic4g-0008QY-L4; Mon, 26 Aug 2024 11:56:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sic4f-0008Q2-0z for qemu-devel@nongnu.org; Mon, 26 Aug 2024 11:56:09 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sic4d-0006vx-5p for qemu-devel@nongnu.org; Mon, 26 Aug 2024 11:56:08 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724687766; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:autocrypt:autocrypt; bh=vjFJu0GFpDDQgKGywKvlXOskCbspsb1g7gVBaWvnoMI=; b=grFiFB5a9kUMba3U/8Yyaez7eX8+t10aiPrQ5mU3zefpiipA4BvkyZfP4SieW1HMfaTy5r Dt2YTpD1a8ab6eHmZEPSpUAdNNmuhA7F2Lktt4YrxRsP2msXZVjzsbXWKEHIdZOLrnrasr O2SyaeV7rlTlIWVoUmGo4UvkakQfCVg= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-454-7SxhVTE6NVug2ULNwkzTSw-1; Mon, 26 Aug 2024 11:56:04 -0400 X-MC-Unique: 7SxhVTE6NVug2ULNwkzTSw-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-42ac185e26cso43942365e9.3 for ; Mon, 26 Aug 2024 08:56:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724687763; x=1725292563; h=content-transfer-encoding:in-reply-to:organization:autocrypt :content-language:references:cc:to:from:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=vjFJu0GFpDDQgKGywKvlXOskCbspsb1g7gVBaWvnoMI=; b=MmkfrH/qrjhFEbW3a2BnJ8A6mc/mJpMuY4SmEfHG76Dysi+NA9p+0k2C9MeeB1WAca kdMB6HGy+1av6om7iOEGAO2ybsWBHiXkl1DJbT7Cueq5FNYMth9Tg4BrBCOcLIVZnsLw IJcgulbeDzWNIRIyTTaYRwJcfmjeUF8wbP4Z9LAPcua4GwcR/96WN3ZPPw5eEoPaSpRe u1YvCKx6yaSEH6maT13p8mpQitFpwZOxeazZgfX6lc4AfmBmh8KUFeIHR10G5YpzSFQP 8QVVL9TXrtWfx3j1ndprr5Pf36jccyv1sEzl9mNwXlW6osPzKsNPtzqImC0NTzk65/W3 0k8g== X-Gm-Message-State: AOJu0YwDlbLipaBpTpSM9st/oWO9colTFiGhJS4hwoZN/J7vsmTVCdOE 0ZmXTuIAbUpCcefmmt+VBVfj6g1Zyej5KIplXzl3m3VbF/VwuQHnLdmeFYanKmBMcyUjK/ZemTy 60H9qCc3AJp0V1/E30DGDfaZ8PRByY+GwZNzJRaDhceIDs8VJdoLv X-Received: by 2002:a05:600c:1391:b0:426:6765:d6b0 with SMTP id 5b1f17b1804b1-42acc8dc605mr70937015e9.15.1724687763061; Mon, 26 Aug 2024 08:56:03 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGSuEfmjuT3rJcBFN+Fo/3rUezy2CaddGYq/PLzTP+f0zUF4vw+Nwqwiw+EThOZKxpCRCLCTA== X-Received: by 2002:a05:600c:1391:b0:426:6765:d6b0 with SMTP id 5b1f17b1804b1-42acc8dc605mr70936815e9.15.1724687762135; Mon, 26 Aug 2024 08:56:02 -0700 (PDT) Received: from ?IPV6:2003:cb:c737:1900:16b0:8dc:77e:31af? (p200300cbc737190016b008dc077e31af.dip0.t-ipconnect.de. [2003:cb:c737:1900:16b0:8dc:77e:31af]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42ac21cd620sm176886385e9.37.2024.08.26.08.56.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 26 Aug 2024 08:56:01 -0700 (PDT) Message-ID: <840943aa-c737-45d4-89ad-0b7a75fd56a6@redhat.com> Date: Mon, 26 Aug 2024 17:56:00 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: apparent memory leak from object-add+object-del of memory-backend-ram From: David Hildenbrand To: Peter Maydell Cc: QEMU Developers , Igor Mammedov References: <7f3fd493-8652-4bb9-b94a-1484d24dc3f2@redhat.com> <57869819-eba7-4cc6-a1b1-c5581f5fb9e0@redhat.com> Content-Language: en-US Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW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Y7x6HfQJ3xajVH32Ms6t1trDQmPx2iP5+7iDsb7OKAb5eOS8h+BEBDeq 3ecsQDv0fFJOA9ag5O3LLNk+3x3q7e0uo06XMaY7UHS341ozXUUI7wC7iKfoUTv03iO9El5f XpNMx/YrIMduZ2+nd9Di7o5+KIwlb2mAB9sTNHdMrXesX8eBL6T9b+MZJk+mZuPxKNVfEQMQ a5SxUEADIPQTPNvBewdeI80yeOCrN+Zzwy/Mrx9EPeu59Y5vSJOx/z6OUImD/GhX7Xvkt3kq Er5KTrJz3++B6SH9pum9PuoE/k+nntJkNMmQpR4MCBaV/J9gIOPGodDKnjdng+mXliF3Ptu6 3oxc2RCyGzTlxyMwuc2U5Q7KtUNTdDe8T0uE+9b8BLMVQDDfJjqY0VVqSUwImzTDLX9S4g/8 kC4HRcclk8hpyhY2jKGluZO0awwTIMgVEzmTyBphDg/Gx7dZU1Xf8HFuE+UZ5UDHDTnwgv7E th6RC9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: <57869819-eba7-4cc6-a1b1-c5581f5fb9e0@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Received-SPF: pass client-ip=170.10.133.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org On 26.08.24 17:38, David Hildenbrand wrote: > On 20.08.24 10:50, Peter Maydell wrote: >> On Mon, 19 Aug 2024 at 20:07, David Hildenbrand wrote: >>> >>> On 19.08.24 18:24, Peter Maydell wrote: >>>> Hi; I'm looking at a memory leak apparently in the host memory backend >>>> code that you can see from the qmp-cmd-test. Repro instructions: >>> >>> Hi Peter, >>> >>>> >>>> (1) build QEMU with '--cc=clang' '--cxx=clang++' '--enable-debug' >>>> '--target-list=x86_64-softmmu' '--enable-sanitizers' >>>> (2) run 'make check'. More specifically, to get just this >>>> failure ('make check' on current head-of-tree produces some >>>> other unrelated leak errors) you can run the relevant single test: >>>> >>>> (cd build/asan && ASAN_OPTIONS="fast_unwind_on_malloc=0" >>>> QTEST_QEMU_BINARY=./qemu-system-x86_64 ./tests/qtest/qmp-cmd-test >>>> --tap -k -p /x86_64/qmp/object-add-failure-modes) >>>> >>>> The test case is doing a variety of object-add then object-del >>>> of the "memory-backend-ram" object, and this add-del cycle seems >>>> to result in a fairly large leak: >>>> >>>> Direct leak of 1572864 byte(s) in 6 object(s) allocated from: >>>> #0 0x555c1336efd8 in __interceptor_calloc >>>> (/mnt/nvmedisk/linaro/qemu-from-laptop/qemu/build/asan/qemu-system-x86_64+0x218efd8) >>>> (BuildId: fc7566a39db1253aed91d500b5b1784e0c438397) >>>> #1 0x7f5bf3472c50 in g_malloc0 debian/build/deb/../../../glib/gmem.c:161:13 >>>> #2 0x555c155bb134 in bitmap_new >>>> /mnt/nvmedisk/linaro/qemu-from-laptop/qemu/include/qemu/bitmap.h:102:12 >>>> #3 0x555c155ba4ee in dirty_memory_extend system/physmem.c:1831:37 >>>> #4 0x555c15585a2b in ram_block_add system/physmem.c:1907:9 >>>> #5 0x555c15589e50 in qemu_ram_alloc_internal system/physmem.c:2109:5 >>>> #6 0x555c1558a096 in qemu_ram_alloc system/physmem.c:2129:12 >>>> #7 0x555c15518b69 in memory_region_init_ram_flags_nomigrate >>>> system/memory.c:1571:21 >>>> #8 0x555c1464fd27 in ram_backend_memory_alloc backends/hostmem-ram.c:34:12 >>>> #9 0x555c146510ac in host_memory_backend_memory_complete >>>> backends/hostmem.c:345:10 >>>> #10 0x555c1580bc90 in user_creatable_complete qom/object_interfaces.c:28:9 >>>> #11 0x555c1580c6f8 in user_creatable_add_type qom/object_interfaces.c:125:10 >>>> #12 0x555c1580ccc4 in user_creatable_add_qapi qom/object_interfaces.c:157:11 >>>> #13 0x555c15ff0e2c in qmp_object_add qom/qom-qmp-cmds.c:227:5 >>>> #14 0x555c161ce508 in qmp_marshal_object_add >>>> /mnt/nvmedisk/linaro/qemu-from-laptop/qemu/build/asan/qapi/qapi-commands-qom.c:337:5 >>>> #15 0x555c162a7139 in do_qmp_dispatch_bh qapi/qmp-dispatch.c:128:5 >>>> #16 0x555c16387921 in aio_bh_call util/async.c:171:5 >>>> #17 0x555c163887fc in aio_bh_poll util/async.c:218:13 >>>> #18 0x555c162e1288 in aio_dispatch util/aio-posix.c:423:5 >>>> #19 0x555c1638f7be in aio_ctx_dispatch util/async.c:360:5 >>>> #20 0x7f5bf3469d3a in g_main_dispatch >>>> debian/build/deb/../../../glib/gmain.c:3419:28 >>>> #21 0x7f5bf3469d3a in g_main_context_dispatch >>>> debian/build/deb/../../../glib/gmain.c:4137:7 >>>> #22 0x555c163935c9 in glib_pollfds_poll util/main-loop.c:287:9 >>>> #23 0x555c16391f03 in os_host_main_loop_wait util/main-loop.c:310:5 >>>> #24 0x555c16391acc in main_loop_wait util/main-loop.c:589:11 >>>> #25 0x555c14614917 in qemu_main_loop system/runstate.c:801:9 >>>> #26 0x555c16008b8c in qemu_default_main system/main.c:37:14 >>>> #27 0x555c16008bd7 in main system/main.c:48:12 >>>> #28 0x7f5bf12fbd8f in __libc_start_call_main >>>> csu/../sysdeps/nptl/libc_start_call_main.h:58:16 >>>> >>>> My initial suspicion here is that the problem is that >>>> TYPE_MEMORY_BACKEND has a UserCreatableClass::complete method which >>>> calls HostMemoryBackend::alloc, but there is no corresponding >>>> "now free this" in instance_finalize. So ram_backend_memory_alloc() >>>> calls memory_region_init_ram_flags_nomigrate(), which allocates >>>> RAM, dirty blocks, etc, but nothing ever destroys the MR and the >>>> memory is leaked when the TYPE_MEMORY_BACKEND object is finalized. >>>> >>>> But there isn't a "free" method in HostMemoryBackendClass, >>>> only an "alloc", so this looks like an API with "leaks memory" >>>> baked into it. How is the freeing of the memory on object >>>> deletion intended to work? >>> >>> I *think* during object_del(), we would be un-refing the contained >>> memory-region, which in turn will make the refcount go to 0 and end up >>> calling memory_region_finalize(). >> >> Oh, yes, I'd forgotten about the MemoryRegions being refcounted. >> That explains why the MR itself doesn't show up as a leak, only >> these dirty memory bitmaps. >> >>> In memory_region_finalize, we do various things, including calling >>> mr->destructor(mr). >>> >>> For memory_region_init_ram_flags_nomigrate(), the deconstructor is set >>> to memory_region_destructor_ram(). This is the place where we call >>> qemu_ram_free(mr->ram_block); >>> >>> There we clean up. >>> >>> What we *don't* clean up is the allocation you are seeing: >>> dirty_memory_extend() will extend the ram_list.dirty_memory bitmap as >>> needed. It is not stored in the RAMBlock, it's a global list. >>> >>> It's not really a leak I think: when we object_del + object_add *I >>> think* that bitmap will simply get reused. >> >> I think there probably is a leak here somewhere, though -- >> lsan will only report if the memory is unreachable from >> anywhere on program exit, AIUI. If we still had the global >> list available to reuse on the next object-creation >> shouldn't it still be reachable from somewhere? > > Yes, that's what confusing me here. It's a global array that holds these > bitmap chunks. I don't see how there would be a leak, but maybe I'm > missing something. Let me have another look. > >> >> It's possible the leak only happens in some of the >> "check failure cases of object-add" code paths that the >> test is exercising, of course. > > Right, but I think in any case we would keep the global array > consistent. Let me try to re-understand that code. > Ah, yes there is a memory leak. The issue is that on unplug, when the RAMBlock goes away, we don't free any entries that dirty_memory_extend() allocated. The next time a RAMBlock is added, we call last_ram_page() again, to see if we have to extend. And we think we have to extend. As last_ram_page() is based on ramblocks, and the relevant RAMBlock vanished, we would call dirty_memory_extend() again, doing a bitmap_new() at spots where there would already have been a bitmap_new() before. That whole code is quite fragile I'm afraid. It would probably be best to just sore the numbers of allocated blocks per ram_list.dirty_memory[i] entry, and maybe try getting rid of this "old_ram_size" thingy completely. Of course, freeing bitmaps where possible might also be an option when removing RAMBlocks ... -- Cheers, David / dhildenb