Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp3095693pxb; Mon, 19 Apr 2021 23:56:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzukEaiJYDO5WwJQv7GnSDaTAsgY9SYcGDASw12T3tB9EkddpUnLqBxYJECjMX4/jSCUVim X-Received: by 2002:a50:fe8c:: with SMTP id d12mr30486943edt.336.1618901789679; Mon, 19 Apr 2021 23:56:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618901789; cv=none; d=google.com; s=arc-20160816; b=PzHLHjm1QxEnGiWxQ9fB/uGRUvwBwczKBHKBmgmarUhVL9HQF11b3/F6LmAElIswx9 1LzNdTKSP6O0HPgMkNDuhIjh5fBz//BLYXeREnGf/msfYTP6sLEGyVjn2zqYV3vDOZLa YXjzlxi/P8uebcFH2ouVDPKZQhUjTNd4oFVEPJxzelM/qPHJKIW8ubNxVA20rj+nEdky dknfdT5c3I7zr6H4Depjx80GHHw4He+HS2ZJ2oVRUgEHbjAqAVpQlGt8XNKA9qRNplWk KAqxcyLr9TesEXs9I8Tms2eKqnj6FIqqMkr2TlVP6JxxPLsYZUUV25uieTRSKDj8psj6 f2Ow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :to:subject:dkim-signature; bh=/RHLOcD8M249MwA8TbQiobmQMiRvQB/PzoDWZSaIlRs=; b=oBz4V11Fp2eDHrGmsqVuAa9nBNo1Ak/XVP0Vx/XfWEGyXo28WMbYMOENStKKy63++s 7pz6Cb3y2znuaRaRAalpVDp3L34syMm9WhI1T8hAYkxP1KKVx/fExVbKDHutVyHjS4sQ Cb1Y0JVXWf3duaofuH6FHwbvTGKgm/4syQoLyOBSXvcAy+AIQqPPiKqvqrex94Qgu/Q0 cw7h+DoMiCRMmFzhqV+HzWZ3v6Q7mq//ng/LU+SGwRRlV6XSqLeBqfFupn7lhT5AKElo 9WSQhPE7hvBJ/A3r3RFJ4xtWih8GQmoTUkVAAgLW3rwLxb4qgMzFsxAx4btIZk4vxayb HmtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Ssvubkvr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a19si13649474ejy.700.2021.04.19.23.56.05; Mon, 19 Apr 2021 23:56:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Ssvubkvr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230249AbhDTGxW (ORCPT + 99 others); Tue, 20 Apr 2021 02:53:22 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:28510 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229985AbhDTGxO (ORCPT ); Tue, 20 Apr 2021 02:53:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618901563; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/RHLOcD8M249MwA8TbQiobmQMiRvQB/PzoDWZSaIlRs=; b=Ssvubkvr/UFMkIOl99mC8y7qskAtjOy/PrAG0X4+Sqs9oa7NmUrsZNgX8yFBPQTopB9eRQ eAG5TZdkXc8IsCGa7fLC7Uo19Jm3DpnM07nLgwTvdNPZPQlUyYikL0huHsss3o9LOBu24f fjGEvaFfLYzXUjmheeo0LVUguM8BDrU= Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-290-ae4sSDMxPwGgwLxiI5IQhw-1; Tue, 20 Apr 2021 02:52:04 -0400 X-MC-Unique: ae4sSDMxPwGgwLxiI5IQhw-1 Received: by mail-ej1-f69.google.com with SMTP id j25-20020a1709060519b029037cb8ca241aso4384969eja.19 for ; Mon, 19 Apr 2021 23:52:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=/RHLOcD8M249MwA8TbQiobmQMiRvQB/PzoDWZSaIlRs=; b=FbIQxHOiVe0Cf0Al9m4ivHiwiq70DOEhSbAciMqlem51ND+/7hU5Dw0YlH4+eVHyzc oKJ/gKIdS8FagAd1H50na0bwfMatGWcO/oAxeMYBk4+u+hC/WXxVGXJJPoAtvdkdHg8T /q5AE89c+gJkaG55R5D2vqueHLnGDUpY/ntPR4SHxOdJjaaXzjpmh3oWpewkQG4WkdnG Egb6G9KQ2xacW6ZOjBjRvXRDRT+2kFw9BdwH698dyXeQOfwN1Wu+bk+jKSNW+OpEU6xO AfDSuOFDSMQk45bvUFjg0XPUf2xBnDydcrIHXQVJY6zund1FeE9USpakJLqn3vgpIMZl kVYw== X-Gm-Message-State: AOAM533FUumYcaLExnhpFM5qrQb7+BC40gBLpfSL9sb6/xV/8/wZViur nRyn3GYn19Nva9fqllP20+pDXbeCuO8gbR12SWIKAD0jThHB7RBTX5WKxOBfUvyj7g2O03BMv/o fKCz/Bp9uIXsvRqoZXvHHOeM7+tYK4qTshMcUcyOLvWQFDncT7+kF3xpcROLwFqDPNlpQulpIB+ WF X-Received: by 2002:aa7:d615:: with SMTP id c21mr17156917edr.176.1618901522508; Mon, 19 Apr 2021 23:52:02 -0700 (PDT) X-Received: by 2002:aa7:d615:: with SMTP id c21mr17156887edr.176.1618901522269; Mon, 19 Apr 2021 23:52:02 -0700 (PDT) Received: from ?IPv6:2001:b07:6468:f312:c8dd:75d4:99ab:290a? ([2001:b07:6468:f312:c8dd:75d4:99ab:290a]) by smtp.gmail.com with ESMTPSA id z14sm311068edc.62.2021.04.19.23.52.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 19 Apr 2021 23:52:01 -0700 (PDT) Subject: Re: Doubt regarding memory allocation in KVM To: Shivank Garg , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org References: From: Paolo Bonzini Message-ID: Date: Tue, 20 Apr 2021 08:52:00 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 20/04/21 07:45, Shivank Garg wrote: > Hi, > I'm learning about qemu KVM, looking into code and experimenting on > it. I have the following doubts regarding it, I would be grateful if > you help me to get some idea on them. > > 1. I observe that KVM allocates memory to guests when it needs it but > doesn't take it back (except for ballooning case). > Also, the Qemu/KVM process does not free the memory even when the > guest is rebooted. In this case, Does the Guest VM get access to > memory already pre-filled with some garbage from the previous run?? Yes. > (Since the host would allocate zeroed pages to guests the first time > it requests but after that it's up to guests). Can it be a security > issue? No, it's the same that happens on non-virtual machine. > 2. How does the KVM know if GPFN (guest physical frame number) is > backed by an actual machine frame number in host? If not mapped, then > it faults in the host and allocates a physical frame for guests in the > host. (kvm_mmu_page_fault) It's all handled by Linux. KVM only does a call to get_user_pages. See functions whose name starts with hva_to_pfn in virt/kvm/kvm_main.c Given a GPA, the GFN is simply the guest physical address minus bits 0:11, so shifted right by 12. Paolo