Received: by 2002:ab2:6991:0:b0:1f7:f6c3:9cb1 with SMTP id v17csp43101lqo; Tue, 7 May 2024 11:33:23 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXmiyfblwSy9Wk/3PvSjSMecK1l6d9R5rAZxWwI09LuPIq2Q06rOmfFMw1cGzH2GuNWWbafV2b9Bc+BZVuuNfO9buxy3txf02cEJOYT6g== X-Google-Smtp-Source: AGHT+IHte3K0CPYpx015fMrfKTT9C0FvJU68Ksh/Z1VJeOlHW3yhfepdM42IjttDRKSLzfdJ+4Pb X-Received: by 2002:a05:6359:4c8b:b0:18f:81e1:dc33 with SMTP id e5c5f4694b2df-192d2908c34mr80445455d.2.1715106802999; Tue, 07 May 2024 11:33:22 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715106802; cv=pass; d=google.com; s=arc-20160816; b=WMCDfsFTbN3LVlwVCcVXoR+gxFmuSwUKxr9FRskUp/tPvV6jQHQ4uLCjHxgaBnZZg1 gxpFiGLO4oa6M/bAqv21gU1gGupaZeuQSjave2g89XicQDFXDUSBTwGY914mWk3K9Cr8 Ak0urNFNaNraT53qCbIwXXYy+g7rlQlYm1mkY8ZbUK/WD0lPb/dcnWK6ES34EacoGp7v IIPp8VppAcWpkebOLfXDHqoK9YNGpTqX0qaLdTMLIpnhu/CW0PGXtxvcxOWvJ33U5TRb mczgQQj0FhGcaH7OxjE8yq6j1J/31DAo8VlN80vhXPb6M4pddnKc8LpGWGKzM43O8DzK GkSg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=gHuV5lFHC0UB6R5XPoecvLq/6suqEUmHMMZiSdK/AsE=; fh=3WFKSb8Mo56bxpqfiW2ErIZJheVwguz4nTX9BTlehrE=; b=zkLkJ5rSs3YW7LeCTt7pgTP9XXdEHKAnJLwtVDnOWYqBFbJ1AvYfHrsjVuiJduXKBf AcydicsnBjiimumP4U7eiTcgHfJHzsPlno2CiIS/C0zBPAEOAZvV5xST+lHpdUUBFS1M fUUbhEeDqEP4GlNeIsLXPDLHtp5ZpxW8tXFwCHXToXsvc5FlaArafyod3orjyqI8PqYJ 7Z36WVb1akODa7jgMOJPe6OQLklazSYqIi+KasKEhnqxSaU6klqn0Cf5V5j7/5O7SL14 WasI7CQ24vGAJisOIXN4sz6b1GOc3KF033m7U9XxdRTU6JiTPl2Z7dvNxTq7Rq7SDVdJ xfvA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WcXysAcJ; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-171957-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-171957-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id n30-20020a63721e000000b0061d7db93d07si9357768pgc.13.2024.05.07.11.33.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 11:33:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-171957-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WcXysAcJ; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-171957-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-171957-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 494F0B27367 for ; Tue, 7 May 2024 18:08:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2D36116DEDF; Tue, 7 May 2024 18:07:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="WcXysAcJ" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 90FEC16D317 for ; Tue, 7 May 2024 18:07:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715105258; cv=none; b=OHOldJJVZxHnaJomMQrAQmyhCC01WAOAzYHB9MX48S9AGaIVFdYXRESzO7Y4S/1NXcj0XEmMWOFt32srLdM4tIw9WqAu5jLM50RpFazGKaCnr7Hu6P0VBFsfdH4KMwE0Zew8a5TTHqe6xQMcQCCgbbMwubSHy5a8Enr8bTVT06Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715105258; c=relaxed/simple; bh=hVFNZWdC9DOlDIg8Up+tLBUHcZP6ppMY7wF+VHGHOKk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=PIv81MGutGMs/64YYGm7LlsQySWGYv11uUNqWBgItiqrwQinVBYGWuKD5Gi+vjRUvTL2+oTtwcyjKshlvcsC/VaeUHgCShtMIIvSSCGOADKLAU5pTRqsWvZIR3ViRYwI+5sJnQgN2i+isQjf9VGJQ3NkGNzu1sFTYZa4bpRFlS0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=WcXysAcJ; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715105254; 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=gHuV5lFHC0UB6R5XPoecvLq/6suqEUmHMMZiSdK/AsE=; b=WcXysAcJ/oYrXV42onvZ/n8Am01Bom+jCUebho7reWIemIE6ofU4zeyDH1Y9/GmofBF8xU T3X0JTbuUv9A9/GIoApdoSDzoNCMjEVTa9kcEShhuaCusHTTuHKIhDqz9HfOC3FDO3E8eW Eb12p7Jr2XBQWEz57G3JQiPQlMOqQ0s= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-120-kJxJYozMOuOAWSI42hlSTw-1; Tue, 07 May 2024 14:07:31 -0400 X-MC-Unique: kJxJYozMOuOAWSI42hlSTw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BD98C85A58C; Tue, 7 May 2024 18:07:30 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 76BC040C6EB7; Tue, 7 May 2024 18:07:30 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: vbabka@suse.cz, isaku.yamahata@intel.com, xiaoyao.li@intel.com, binbin.wu@linux.intel.com, seanjc@google.com, rick.p.edgecombe@intel.com, michael.roth@amd.com, yilun.xu@intel.com, linux-mm@kvack.org Subject: [PATCH 2/9] KVM: guest_memfd: Use AS_INACCESSIBLE when creating guest_memfd inode Date: Tue, 7 May 2024 14:07:22 -0400 Message-ID: <20240507180729.3975856-3-pbonzini@redhat.com> In-Reply-To: <20240507180729.3975856-1-pbonzini@redhat.com> References: <20240507180729.3975856-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 From: Michael Roth truncate_inode_pages_range() may attempt to zero pages before truncating them, and this will occur before arch-specific invalidations can be triggered via .invalidate_folio/.free_folio hooks via kvm_gmem_aops. For AMD SEV-SNP this would result in an RMP #PF being generated by the hardware, which is currently treated as fatal (and even if specifically allowed for, would not result in anything other than garbage being written to guest pages due to encryption). On Intel TDX this would also result in undesirable behavior. Set the AS_INACCESSIBLE flag to prevent the MM from attempting unexpected accesses of this sort during operations like truncation. This may also in some cases yield a decent performance improvement for guest_memfd userspace implementations that hole-punch ranges immediately after private->shared conversions via KVM_SET_MEMORY_ATTRIBUTES, since the current implementation of truncate_inode_pages_range() always ends up zero'ing an entire 4K range if it is backing by a 2M folio. Link: https://lore.kernel.org/lkml/ZR9LYhpxTaTk6PJX@google.com/ Cc: linux-mm@kvack.org Suggested-by: Sean Christopherson Signed-off-by: Michael Roth Message-ID: <20240329212444.395559-6-michael.roth@amd.com> Acked-by: Vlastimil Babka Signed-off-by: Paolo Bonzini --- virt/kvm/guest_memfd.c | 1 + 1 file changed, 1 insertion(+) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 0f4e0cf4f158..5a929536ecf2 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -357,6 +357,7 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) inode->i_private = (void *)(unsigned long)flags; inode->i_op = &kvm_gmem_iops; inode->i_mapping->a_ops = &kvm_gmem_aops; + inode->i_mapping->flags |= AS_INACCESSIBLE; inode->i_mode |= S_IFREG; inode->i_size = size; mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER); -- 2.43.0