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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A31B7C19F2A for ; Thu, 4 Aug 2022 13:37:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 290768E0002; Thu, 4 Aug 2022 09:37:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2408D6B0072; Thu, 4 Aug 2022 09:37:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0E14E8E0002; Thu, 4 Aug 2022 09:37:21 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id F20FF6B0071 for ; Thu, 4 Aug 2022 09:37:20 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id C5C0F1A00DF for ; Thu, 4 Aug 2022 13:37:20 +0000 (UTC) X-FDA: 79762011840.04.918B15A Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by imf06.hostedemail.com (Postfix) with ESMTP id 4704C18010D for ; Thu, 4 Aug 2022 13:37:19 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 2896921068; Thu, 4 Aug 2022 13:37:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1659620238; h=from:from:reply-to: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=4z4bB0wf9LcF9P6u2FPUyZHxaeD5A+EOAqqR1WSbcG4=; b=BXgO1RCi6DXmG5yZlxDlMsnurvSrUNO/6LU+fwYMGsA2KiyInuHZy5B+Z+0xmwLjX9vvTg TGwZgIzeXkTEwi9Wl7yuERuGSQRaLUK5aJj9VMY+ky1oVWsdMoEQrcCEWqAIGrWvhJXgS+ b3PLvVILPAybITOU257Nx/Dfo6dDTws= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1659620238; h=from:from:reply-to: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=4z4bB0wf9LcF9P6u2FPUyZHxaeD5A+EOAqqR1WSbcG4=; b=z0C+PLAADumOtWMXMulg5tP5ulysMkSTVGsW6yee5rER0go9eeH1Ci+9u33zw6DI9k6HDD TmakTx0UQv3ICaDA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id A4FA113434; Thu, 4 Aug 2022 13:37:17 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id Rje4J43L62K/YAAAMHmgww (envelope-from ); Thu, 04 Aug 2022 13:37:17 +0000 Message-ID: <5c806192-f62c-5ea3-820d-8d629e0b0eba@suse.cz> Date: Thu, 4 Aug 2022 15:37:17 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.0.3 Subject: Re: [PATCH Part2 v6 25/49] KVM: SVM: Disallow registering memory range from HugeTLB for SNP guest Content-Language: en-US To: Ashish Kalra , x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: tglx@linutronix.de, mingo@redhat.com, jroedel@suse.de, thomas.lendacky@amd.com, hpa@zytor.com, ardb@kernel.org, pbonzini@redhat.com, seanjc@google.com, vkuznets@redhat.com, jmattson@google.com, luto@kernel.org, dave.hansen@linux.intel.com, slp@redhat.com, pgonda@google.com, peterz@infradead.org, srinivas.pandruvada@linux.intel.com, rientjes@google.com, dovmurik@linux.ibm.com, tobin@ibm.com, bp@alien8.de, michael.roth@amd.com, kirill@shutemov.name, ak@linux.intel.com, tony.luck@intel.com, marcorr@google.com, sathyanarayanan.kuppuswamy@linux.intel.com, alpergun@google.com, dgilbert@redhat.com, jarkko@kernel.org References: From: Vlastimil Babka In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1659620239; h=from:from:sender: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:dkim-signature; bh=4z4bB0wf9LcF9P6u2FPUyZHxaeD5A+EOAqqR1WSbcG4=; b=eVMd+rB8dp1mQ7/+s2WWgv/d6gR0YsNF7EWJmod+lwT+wnMGtDnKlKZBUalbhX7q6R7u+l bxixHK29aE0iv58LJpqefaGF74a9x0BrE4uPmKN3M/3S5Zlz8oXTBeETG1whGawNvep3l+ w4TKUixLdR8lViGINxxhrMCm/makUEY= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=BXgO1RCi; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=z0C+PLAA; spf=pass (imf06.hostedemail.com: domain of vbabka@suse.cz designates 195.135.220.29 as permitted sender) smtp.mailfrom=vbabka@suse.cz; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1659620239; a=rsa-sha256; cv=none; b=xXEWbOwsdYq6rdsmuf2nksoYKxrpV3wxz03SHPDwQzKM0+9qp1c/lp0E/hJA6sBLJCL0/m yKp1y3Le0QfIz/YhO8f5CWPJY2RnBy21hk6xm8jqdfT4zaK1trKYCZ+ZK3QBZ3LD/H67uc G70y6ehj8RrL7b+4kkyN8s5lvKk0Eio= Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=BXgO1RCi; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=z0C+PLAA; spf=pass (imf06.hostedemail.com: domain of vbabka@suse.cz designates 195.135.220.29 as permitted sender) smtp.mailfrom=vbabka@suse.cz; dmarc=none X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 4704C18010D X-Stat-Signature: hkr1geuu3meaot37xb5k6wdtkc6n53p3 X-Rspam-User: X-HE-Tag: 1659620239-311293 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 6/21/22 01:07, Ashish Kalra wrote: > From: Brijesh Singh > > While creating the VM, userspace call the KVM_MEMORY_ENCRYPT_REG_REGION > ioctl to register the memory regions for the guest. This registered > memory region is typically used as a guest RAM. Later, the guest may > issue the page state change (PSC) request that will require splitting > the large page into smaller page. If the memory is allocated from the > HugeTLB then hypervisor will not be able to split it. > > Do not allow registering the memory range backed by the HugeTLB until > the hypervisor support is added to handle the case. > > Signed-off-by: Brijesh Singh > --- > arch/x86/kvm/svm/sev.c | 37 +++++++++++++++++++++++++++++++++++++ > 1 file changed, 37 insertions(+) > > diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c > index 9e6fc7a94ed7..41b83aa6b5f4 100644 > --- a/arch/x86/kvm/svm/sev.c > +++ b/arch/x86/kvm/svm/sev.c > @@ -17,6 +17,7 @@ > #include > #include > #include > +#include > > #include > #include > @@ -2007,6 +2008,35 @@ int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp) > return r; > } > > +static bool is_range_hugetlb(struct kvm *kvm, struct kvm_enc_region *range) > +{ > + struct vm_area_struct *vma; > + u64 start, end; > + bool ret = true; > + > + start = range->addr; > + end = start + range->size; > + > + mmap_read_lock(kvm->mm); > + > + do { > + vma = find_vma_intersection(kvm->mm, start, end); > + if (!vma) > + goto unlock; > + > + if (is_vm_hugetlb_page(vma)) > + goto unlock; > + > + start = vma->vm_end; > + } while (end > vma->vm_end); Note it's more efficient to only find the first vma and then iterate using vma->vm_next. But the details will change when maple tree is merged, and likely this patch won't exist after rebasing to UPM anyway... > + ret = false; > + > +unlock: > + mmap_read_unlock(kvm->mm); > + return ret; > +} > + > int sev_mem_enc_register_region(struct kvm *kvm, > struct kvm_enc_region *range) > { > @@ -2024,6 +2054,13 @@ int sev_mem_enc_register_region(struct kvm *kvm, > if (range->addr > ULONG_MAX || range->size > ULONG_MAX) > return -EINVAL; > > + /* > + * SEV-SNP does not support the backing pages from the HugeTLB. Verify > + * that the registered memory range is not from the HugeTLB. > + */ > + if (sev_snp_guest(kvm) && is_range_hugetlb(kvm, range)) > + return -EINVAL; > + > region = kzalloc(sizeof(*region), GFP_KERNEL_ACCOUNT); > if (!region) > return -ENOMEM;