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 92CEBE7849A for ; Mon, 2 Oct 2023 08:58:34 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qnEkc-0001Pp-Eb; Mon, 02 Oct 2023 04:58:02 -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 1qnEkO-0001Ok-Vz for qemu-devel@nongnu.org; Mon, 02 Oct 2023 04:57:49 -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 1qnEkN-0003PP-0E for qemu-devel@nongnu.org; Mon, 02 Oct 2023 04:57:48 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1696237065; 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; bh=fVukFMHb4sPlTBNho7wcg3FCMW675wzniIBiQO49dIQ=; b=R3+nrteagWKVrZW+oo0oFGizGg8mtojpGFIhsBuiaNX1SNYyzJYzuHtu/J1DRcv76TQ4Eb MRy0MJ/x3wfflQ0RHHxDN2bld7xSQ0ruilM9dHhZH1nx8Hx6EyrIBWTIjUaPy8tOsk7jDr A5ONtxxGKjDTqvMHOqN++LMiviSL25Y= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-52-KQbGbYXXMMmqoDsGukY_kQ-1; Mon, 02 Oct 2023 04:57:43 -0400 X-MC-Unique: KQbGbYXXMMmqoDsGukY_kQ-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-3fd0fa4d08cso134062645e9.1 for ; Mon, 02 Oct 2023 01:57:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696237062; x=1696841862; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=fVukFMHb4sPlTBNho7wcg3FCMW675wzniIBiQO49dIQ=; b=bw9skY6tNc86H0lepDjIuPdJuJsE73hqE07lCL58Wm8JBZdocPmvAkXwkuZJTuNCvf zFv4dDOgIL+bJXw2ffzGDJKb23T3boFEjJpQNSj0WVR28n3Lq1UbL2c3Gc6ncYpIkwdu kic9NLrUKhvm33huh23+nn3GcgGOHK8NaLsHE6yA01Mom8HYSKLvLCNb4+jf4ZmO/Hhu utx9ll7Myll7T7O3s2n2oQsVehFCAIqvbforhV6pi2fPxGpkFQ47DQiR4vukkz+rf9aH vOu2ALP1vqC0IXbGAXRyZkjC7XCX7GH0RFfh1FWzwIyqF9J3CgmVH26QJaGSawIvRSdh 2I1A== X-Gm-Message-State: AOJu0YywKfv83iR6RiKvc1dA2SKF+z1gvAzvzamMXfzAL4N+PfDyE0mg 28c53rCXzblR5bq4MXaDrJWNHDXwj3faPaeRLWlE0/KaPzOc+vBJH0sjE6iGebK49iBPAri3dbG Fd+qrkQU7WAqcMzU= X-Received: by 2002:a5d:56ca:0:b0:31f:d8b3:e9f5 with SMTP id m10-20020a5d56ca000000b0031fd8b3e9f5mr9146172wrw.34.1696237062448; Mon, 02 Oct 2023 01:57:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFrzNVxIOfJOk15r++VqOXoeT1XnIyZuejaeShvDOTKPjy99A5uUDFfqMClE2pthwX0F/cLPg== X-Received: by 2002:a5d:56ca:0:b0:31f:d8b3:e9f5 with SMTP id m10-20020a5d56ca000000b0031fd8b3e9f5mr9146156wrw.34.1696237062088; Mon, 02 Oct 2023 01:57:42 -0700 (PDT) Received: from ?IPV6:2003:cb:c735:f200:cb49:cb8f:88fc:9446? (p200300cbc735f200cb49cb8f88fc9446.dip0.t-ipconnect.de. [2003:cb:c735:f200:cb49:cb8f:88fc:9446]) by smtp.gmail.com with ESMTPSA id n3-20020adff083000000b003143867d2ebsm27577613wro.63.2023.10.02.01.57.40 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 02 Oct 2023 01:57:41 -0700 (PDT) Message-ID: <56cec8a9-f4bb-5e9e-a1c4-223359f8b491@redhat.com> Date: Mon, 2 Oct 2023 10:57:40 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Subject: Re: [PATCH v4 16/18] virtio-mem: Expose device memory dynamically via multiple memslots if enabled Content-Language: en-US To: "Maciej S. Szmigiero" Cc: Paolo Bonzini , Igor Mammedov , Xiao Guangrong , "Michael S. Tsirkin" , Peter Xu , =?UTF-8?Q?Philippe_Mathieu-Daud=c3=a9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Michal Privoznik , =?UTF-8?Q?Daniel_P_=2e_Berrang=c3=a9?= , Gavin Shan , Alex Williamson , Stefan Hajnoczi , kvm@vger.kernel.org, qemu-devel@nongnu.org References: <20230926185738.277351-1-david@redhat.com> <20230926185738.277351-17-david@redhat.com> <11c6efbd-b794-4a05-9c51-4928fb545db4@maciej.szmigiero.name> From: David Hildenbrand Organization: Red Hat In-Reply-To: <11c6efbd-b794-4a05-9c51-4928fb545db4@maciej.szmigiero.name> 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: -51 X-Spam_score: -5.2 X-Spam_bar: ----- X-Spam_report: (-5.2 / 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, NICE_REPLY_A=-3.058, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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 30.09.23 19:31, Maciej S. Szmigiero wrote: > On 26.09.2023 20:57, David Hildenbrand wrote: >> Having large virtio-mem devices that only expose little memory to a VM >> is currently a problem: we map the whole sparse memory region into the >> guest using a single memslot, resulting in one gigantic memslot in KVM. >> KVM allocates metadata for the whole memslot, which can result in quite >> some memory waste. >> >> Assuming we have a 1 TiB virtio-mem device and only expose little (e.g., >> 1 GiB) memory, we would create a single 1 TiB memslot and KVM has to >> allocate metadata for that 1 TiB memslot: on x86, this implies allocating >> a significant amount of memory for metadata: >> >> (1) RMAP: 8 bytes per 4 KiB, 8 bytes per 2 MiB, 8 bytes per 1 GiB >> -> For 1 TiB: 2147483648 + 4194304 + 8192 = ~ 2 GiB (0.2 %) >> >> With the TDP MMU (cat /sys/module/kvm/parameters/tdp_mmu) this gets >> allocated lazily when required for nested VMs >> (2) gfn_track: 2 bytes per 4 KiB >> -> For 1 TiB: 536870912 = ~512 MiB (0.05 %) >> (3) lpage_info: 4 bytes per 2 MiB, 4 bytes per 1 GiB >> -> For 1 TiB: 2097152 + 4096 = ~2 MiB (0.0002 %) >> (4) 2x dirty bitmaps for tracking: 2x 1 bit per 4 KiB page >> -> For 1 TiB: 536870912 = 64 MiB (0.006 %) >> >> So we primarily care about (1) and (2). The bad thing is, that the >> memory consumption *doubles* once SMM is enabled, because we create the >> memslot once for !SMM and once for SMM. >> >> Having a 1 TiB memslot without the TDP MMU consumes around: >> * With SMM: 5 GiB >> * Without SMM: 2.5 GiB >> Having a 1 TiB memslot with the TDP MMU consumes around: >> * With SMM: 1 GiB >> * Without SMM: 512 MiB >> >> ... and that's really something we want to optimize, to be able to just >> start a VM with small boot memory (e.g., 4 GiB) and a virtio-mem device >> that can grow very large (e.g., 1 TiB). >> >> Consequently, using multiple memslots and only mapping the memslots we >> really need can significantly reduce memory waste and speed up >> memslot-related operations. Let's expose the sparse RAM memory region using >> multiple memslots, mapping only the memslots we currently need into our >> device memory region container. >> >> The feature can be enabled using "dynamic-memslots=on" and requires >> "unplugged-inaccessible=on", which is nowadays the default. >> >> Once enabled, we'll auto-detect the number of memslots to use based on the >> memslot limit provided by the core. We'll use at most 1 memslot per >> gigabyte. Note that our global limit of memslots accross all memory devices >> is currently set to 256: even with multiple large virtio-mem devices, >> we'd still have a sane limit on the number of memslots used. >> >> The default is to not dynamically map memslot for now >> ("dynamic-memslots=off"). The optimization must be enabled manually, >> because some vhost setups (e.g., hotplug of vhost-user devices) might be >> problematic until we support more memslots especially in vhost-user backends. >> >> Note that "dynamic-memslots=on" is just a hint that multiple memslots >> *may* be used for internal optimizations, not that multiple memslots >> *must* be used. The actual number of memslots that are used is an >> internal detail: for example, once memslot metadata is no longer an >> issue, we could simply stop optimizing for that. Migration source and >> destination can differ on the setting of "dynamic-memslots". >> >> Signed-off-by: David Hildenbrand >> --- > > The changes seem reasonable, so: > Reviewed-by: Maciej S. Szmigiero Thanks Maciej! -- Cheers, David / dhildenb