Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp3343598rdb; Wed, 13 Sep 2023 09:11:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEqlBP/rk1nhhmr0pLm0YlmiEK9z+akruFARioqOsGav9zVWgp0uTFsBMjIevsp3UyE3h+q X-Received: by 2002:a05:6a20:9794:b0:14c:446c:b188 with SMTP id hx20-20020a056a20979400b0014c446cb188mr2510295pzc.37.1694621488507; Wed, 13 Sep 2023 09:11:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694621488; cv=none; d=google.com; s=arc-20160816; b=fvyPp8bJw0g2hX8RN9FpsWIWW9Z37+4ZKbLFjqBtUg0yWXv67GMR+aycvaRP8FR3En eyHjIFv5N/PvYA6fr7Rr5HpPshk8FbldQ2Vk1jCz1MNga3w8Vh/mKWy2qPql6Etleh2J sDyxoSEz17TSmw59OlmivpXfcLF/ukExCputvPt417M1fiiScvLipTj1COmZUkaD61+c TEs7Bu7uH6ToOG1DZHw/zfxQU0UVFkKR6fOtC2kczGSox+w7nip4Hm32Kl1cJ/gBRgiT WhaOQkpToLDW12KJfdi8w2E960771RTyQwxKYV9Pb1fpcKEv1R9/u16RveUo4lTG+7Dt b3qg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=AKGyuEbU5DTXCppgDldUig+9M8sGDO4BzcXOLQY67no=; fh=ANS6iRpdj3ntKSmwSzT4LEHUH9ZAudCj9zvP8/EeThQ=; b=AWqiRBOyKCgRwWMToVWNI9+CpZTY6S61MzLR4ChOlmg/Zskd2Bj4Nw94Wsv95xJtP7 cSrKqpN1Y77gY2+TtvxgfWAvssiMgbsowD7pZiXTNTppTlzcwqstoo1dokDqVkx4wBol ITs2YbyJWQgx+WamDOyY6/Luwp5QcJ9yxoFq5VIW3iT6lokVcQFULqOwW6hCeXIdluOu TF2g7Cu9G39Yl9LiPM/k00ZeADTdzMe160H0n5DsVyUWBNqWfOaPXRbjvJM3peCr2m2P L73B+yNQNYVK6dXK3Sj3DWywt4tMqhVcVUWA9WSpNLsANYfoj5X34vQl5VruNpu2U2Bq nmkg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=JSvUjgeR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id k5-20020a056a00134500b0068fb6fc3ff1si7405498pfu.209.2023.09.13.09.11.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 09:11:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=JSvUjgeR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 1D88C81D8091; Wed, 13 Sep 2023 03:49:29 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239776AbjIMKtW (ORCPT + 99 others); Wed, 13 Sep 2023 06:49:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239730AbjIMKtQ (ORCPT ); Wed, 13 Sep 2023 06:49:16 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 436491726; Wed, 13 Sep 2023 03:49:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1694602152; x=1726138152; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=GkBig6IwfAcfP2Qu/zXThXAnAkU33oLrNZahoClMAyU=; b=JSvUjgeRlXUPi9D8eYylh5/oBfAem5hmVE0l493FCApHLpgNBjwdDB+z Or3BzyABRBaACARZ/EkdG9c2YYUTSA5RHgY9ZAkF5cjnh62Sa/2BaS+vb HvKP4rphra3vSMfgnfQgoMndaAw6QK4qnpaNiQpVYCKSSuL/VWDlWCB6g WRQ+FEkHTm1e/tB7b76EmkzHVAvyi41x2BnXo0dBNO+V+YTA0L/xiFj/Y kHarpa2Fe+pilSsJT9/0jyxtj6EDSsAHmfeEc++/zXWOlc5OMQHd/o5Ey Hc36T65zJuXm6lbgeoswH1ABnVKSkSyEosb4dkzoZzx0ACgbIsteJn3pR w==; X-IronPort-AV: E=McAfee;i="6600,9927,10831"; a="377537881" X-IronPort-AV: E=Sophos;i="6.02,143,1688454000"; d="scan'208";a="377537881" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Sep 2023 03:49:11 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10831"; a="809635588" X-IronPort-AV: E=Sophos;i="6.02,143,1688454000"; d="scan'208";a="809635588" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Sep 2023 03:49:11 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Michael Roth , Paolo Bonzini , Sean Christopherson , erdemaktas@google.com, Sagi Shahar , David Matlack , Kai Huang , Zhi Wang , chen.bo@intel.com, linux-coco@lists.linux.dev, Chao Peng , Ackerley Tng , Vishal Annapurve , Yuan Yao , Jarkko Sakkinen , Xu Yilun , Quentin Perret , wei.w.wang@intel.com, Fuad Tabba Subject: [RFC PATCH 3/6] KVM: guest_memfd, x86: MEMORY_FAULT exit with hw poisoned page Date: Wed, 13 Sep 2023 03:48:52 -0700 Message-Id: <36f6fae6cd7aaba3b0fc18f10981bbba2c30b979.1694599703.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 13 Sep 2023 03:49:29 -0700 (PDT) From: Isaku Yamahata When resolving kvm page fault and hwpoisoned page is given, KVM exit with HWPOISONED flag so that user space VMM, e.g. qemu, handle it. - Add a new flag POISON to KVM_EXIT_MEMORY_FAULT to indicate the page is poisoned. - Make kvm_gmem_get_pfn() return hwpoison state by -EHWPOISON when the folio is hw-poisoned. - When page is hw-poisoned on faulting in private gmem, return KVM_EXIT_MEMORY_FAULT with HWPOISONED flag. Signed-off-by: Isaku Yamahata --- arch/x86/kvm/mmu/mmu.c | 21 +++++++++++++++------ include/uapi/linux/kvm.h | 3 ++- virt/kvm/guest_mem.c | 4 +++- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 05943ccb55a4..5dc9d1fdadca 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -4335,19 +4335,24 @@ static inline u8 kvm_max_level_for_order(int order) return PG_LEVEL_4K; } -static int kvm_do_memory_fault_exit(struct kvm_vcpu *vcpu, - struct kvm_page_fault *fault) +static int __kvm_do_memory_fault_exit(struct kvm_vcpu *vcpu, + struct kvm_page_fault *fault, __u64 flags) { vcpu->run->exit_reason = KVM_EXIT_MEMORY_FAULT; if (fault->is_private) - vcpu->run->memory.flags = KVM_MEMORY_EXIT_FLAG_PRIVATE; - else - vcpu->run->memory.flags = 0; + flags |= KVM_MEMORY_EXIT_FLAG_PRIVATE; + vcpu->run->flags = flags; vcpu->run->memory.gpa = fault->gfn << PAGE_SHIFT; vcpu->run->memory.size = PAGE_SIZE; return RET_PF_USER; } +static int kvm_do_memory_fault_exit(struct kvm_vcpu *vcpu, + struct kvm_page_fault *fault) +{ + return __kvm_do_memory_fault_exit(vcpu, fault, 0); +} + static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) { @@ -4358,12 +4363,16 @@ static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu, r = kvm_gmem_get_pfn(vcpu->kvm, fault->slot, fault->gfn, &fault->pfn, &max_order); - if (r) + if (r && r != -EHWPOISON) return r; fault->max_level = min(kvm_max_level_for_order(max_order), fault->max_level); fault->map_writable = !(fault->slot->flags & KVM_MEM_READONLY); + + if (r == -EHWPOISON) + return __kvm_do_memory_fault_exit(vcpu, fault, + KVM_MEMORY_EXIT_FLAG_HWPOISON); return RET_PF_CONTINUE; } diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index eb900344a054..48329cb44415 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -527,7 +527,8 @@ struct kvm_run { } notify; /* KVM_EXIT_MEMORY_FAULT */ struct { -#define KVM_MEMORY_EXIT_FLAG_PRIVATE (1ULL << 3) +#define KVM_MEMORY_EXIT_FLAG_PRIVATE BIT_ULL(3) +#define KVM_MEMORY_EXIT_FLAG_HWPOISON BIT_ULL(4) __u64 flags; __u64 gpa; __u64 size; diff --git a/virt/kvm/guest_mem.c b/virt/kvm/guest_mem.c index 746e683df589..3678287d7c9d 100644 --- a/virt/kvm/guest_mem.c +++ b/virt/kvm/guest_mem.c @@ -589,6 +589,7 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, { pgoff_t index = gfn - slot->base_gfn + slot->gmem.pgoff; struct kvm_gmem *gmem; + bool hwpoison = false; struct folio *folio; struct page *page; struct file *file; @@ -610,6 +611,7 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, return -ENOMEM; } + hwpoison = folio_test_hwpoison(folio); page = folio_file_page(folio, index); *pfn = page_to_pfn(page); @@ -618,7 +620,7 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, folio_unlock(folio); fput(file); - return 0; + return hwpoison ? -EHWPOISON : 0; } EXPORT_SYMBOL_GPL(kvm_gmem_get_pfn); -- 2.25.1