From patchwork Sat Jul 29 00:47:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 128013 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:918b:0:b0:3e4:2afc:c1 with SMTP id s11csp877123vqg; Fri, 28 Jul 2023 23:28:55 -0700 (PDT) X-Google-Smtp-Source: APBJJlGmrePqTpaEIaknQb5JUDWqRDLFlUzj9onouasxlV6RmgNn93C3DUkzGarAMkGz/xMq01/I X-Received: by 2002:a05:6a20:1587:b0:12e:3394:e2b6 with SMTP id h7-20020a056a20158700b0012e3394e2b6mr4767046pzj.8.1690612135076; Fri, 28 Jul 2023 23:28:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690612135; cv=none; d=google.com; s=arc-20160816; b=SWn7XvWddfLVPorJLx5CYLBs0FcMhQQwJkqMXazULmK76SP9V1rhyWH1Ineaf4kFpH MDwuKY5eonzYHAyEeTn8Ye5+tMg/2cL8EmEaeiiKcQVD5vk9Vi+wMfFJOC0N/oLZaNXk 6G+xdjUvB6q7eMuXSyYV13ftWnxpRk7fIYjdM6P+7N3bPYobFGTEcLAK2/0VmjE7s8+/ Mld0DstlD1DZsVm+1/OrkQif+ZOGg3EBu2cR6896sV3xpBKPCqaNyVHZvdTZw92rveDA kVYgeWrxf5cE2R/UAcpZdPXIk6ur4PBe9+yRdxpwMto59vWQItStVbRFbyL5s4qHoXWW otQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=LOrLJZZ+Gkhi9DkbaZIQBIYsMHKPcX3CcfpaZ/5IaME=; fh=sOnAx2GuCQaI5IJu7Mo1xgLCDrPH4l5HM1pSkvn/pIo=; b=YEgYI7uPVd38tDAKGpXn+YnP3qaVosEFWUn96dORoMRaIYPdTm/B0E/PMcLe5aMQ/a C0EeG7BlI4WzgO4RwlZRXNSNaNP5T/B5Of4nL4y9YWTZ7+M1VF4tGgwg3AyjuxGHXIjT JJJlGm2Gm+FayMbbLuCi80OpEnTCwgSk2yyybORy5rte5rJw9rXvLnP2c1CqzD+0osqi TAKyqcY48rTzGKM3Nyi0zADoA4qM3hhaZt8s7jtt/ngzGbITAcWoiuIHq+SJYLXGIp1e YR7b5ChFUFysfwt/Q5gh1FTj2f4HZmIgKvGru7aKZNwHke3GZA09nxLEgtYcdsaSrQMp aVzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=wJPC2MvJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d7-20020a056a0010c700b006871aa6882bsi910919pfu.165.2023.07.28.23.28.38; Fri, 28 Jul 2023 23:28:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=wJPC2MvJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237069AbjG2AuP (ORCPT + 99 others); Fri, 28 Jul 2023 20:50:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41716 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237083AbjG2Ati (ORCPT ); Fri, 28 Jul 2023 20:49:38 -0400 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1AFBC4EDB for ; Fri, 28 Jul 2023 17:49:10 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id d2e1a72fcca58-686baf1c2fbso2246937b3a.0 for ; Fri, 28 Jul 2023 17:49:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690591666; x=1691196466; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=LOrLJZZ+Gkhi9DkbaZIQBIYsMHKPcX3CcfpaZ/5IaME=; b=wJPC2MvJipPsQhRwbozBA3HvDrT0a5QCegS41zV5HxoUZ0Y6ijfEwm+l6nci1rioY8 hJvaIEMoKCVJicvr9/kZ/an8mz77VT84yBHhpPuT8o84KkzdLNC7eeOpFUjugL86uoN8 RbH4OMwhrRolWZ53p5VBH1t+MNMyrzktWrZPoM891QxOvYGn3A45JjlLbhrqePQO2emM /DQ4Ltj+KKkKQaoXChzdo08lSUNXUEJWG/vBjKQEXf66jFhqn1A9yHDuiRHichM4as62 nWjotbjNiYZvO1RMYdMKmOj2y9Gh1m3SvGyZIHBp6m+07bn1x6JGJA/cTeBoKTkH2mz1 CZgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690591666; x=1691196466; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=LOrLJZZ+Gkhi9DkbaZIQBIYsMHKPcX3CcfpaZ/5IaME=; b=aWHHDSUE01I7rkddVv1AZ5mmYESrPfIbaxnJEnokgjkeYGsXjq5Z63ubVrd+Ci3V+X HeZDou/fEJ5tj7jJRh1vuF5pnDw1k441lro6Pwk7vJCDngLnV3AHth0KnV0PaSEdT+JM vQ9V+yzknxChLvFF67DkBWBrZFm3pkBCTgUiEeBugG/KWtuyd1vHGYLcghMFBPf6gKRa ovT9p2E/sjTzLGOYNBZu6gL9VGbW2lkUuirYCzWMJWsbmtDLiw/wwh4kFSiL7NgknQXS DbqdITEbhha4gR4g3AQXvN70L4q9jYh48AGPOd5X+GfeU5gLam0oyeWXLDA4OjvaDmCl lMdw== X-Gm-Message-State: ABy/qLY4/nq0lFQ20ep/mXMZyIYHCXYdtbLf49nslGLRWkPqTgWovxz1 O+btOUa3UI0VwwLKVEh8stR5WtRUqcE= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:1382:b0:686:6234:5537 with SMTP id t2-20020a056a00138200b0068662345537mr22082pfg.1.1690591666681; Fri, 28 Jul 2023 17:47:46 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 28 Jul 2023 17:47:22 -0700 In-Reply-To: <20230729004722.1056172-1-seanjc@google.com> Mime-Version: 1.0 References: <20230729004722.1056172-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.487.g6d72f3e995-goog Message-ID: <20230729004722.1056172-13-seanjc@google.com> Subject: [PATCH v3 12/12] KVM: x86/mmu: BUG() in rmap helpers iff CONFIG_BUG_ON_DATA_CORRUPTION=y From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Mingwei Zhang , David Matlack , Jim Mattson X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772735310092773543 X-GMAIL-MSGID: 1772735310092773543 Introduce KVM_BUG_ON_DATA_CORRUPTION() and use it in the low-level rmap helpers to convert the existing BUG()s to WARN_ON_ONCE() when the kernel is built with CONFIG_BUG_ON_DATA_CORRUPTION=n, i.e. does NOT want to BUG() on corruption of host kernel data structures. Environments that don't have infrastructure to automatically capture crash dumps, i.e. aren't likely to enable CONFIG_BUG_ON_DATA_CORRUPTION=y, are typically better served overall by WARN-and-continue behavior (for the kernel, the VM is dead regardless), as a BUG() while holding mmu_lock all but guarantees the _best_ case scenario is a panic(). Make the BUG()s conditional instead of removing/replacing them entirely as there's a non-zero chance (though by no means a guarantee) that the damage isn't contained to the target VM, e.g. if no rmap is found for a SPTE then KVM may be double-zapping the SPTE, i.e. has already freed the memory the SPTE pointed at and thus KVM is reading/writing memory that KVM no longer owns. Link: https://lore.kernel.org/all/20221129191237.31447-1-mizhang@google.com Suggested-by: Mingwei Zhang Cc: David Matlack Cc: Jim Mattson Reviewed-by: Mingwei Zhang Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 21 ++++++++++----------- include/linux/kvm_host.h | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 67a25c957a16..92a3f40a6bb3 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -977,7 +977,7 @@ static void pte_list_desc_remove_entry(struct kvm *kvm, * when adding an entry and the previous head is full, and heads are * removed (this flow) when they become empty. */ - BUG_ON(j < 0); + KVM_BUG_ON_DATA_CORRUPTION(j < 0, kvm); /* * Replace the to-be-freed SPTE with the last valid entry from the head @@ -1008,14 +1008,13 @@ static void pte_list_remove(struct kvm *kvm, u64 *spte, struct pte_list_desc *desc; int i; - if (!rmap_head->val) { - pr_err("%s: %p 0->BUG\n", __func__, spte); - BUG(); - } else if (!(rmap_head->val & 1)) { - if ((u64 *)rmap_head->val != spte) { - pr_err("%s: %p 1->BUG\n", __func__, spte); - BUG(); - } + if (KVM_BUG_ON_DATA_CORRUPTION(!rmap_head->val, kvm)) + return; + + if (!(rmap_head->val & 1)) { + if (KVM_BUG_ON_DATA_CORRUPTION((u64 *)rmap_head->val != spte, kvm)) + return; + rmap_head->val = 0; } else { desc = (struct pte_list_desc *)(rmap_head->val & ~1ul); @@ -1029,8 +1028,8 @@ static void pte_list_remove(struct kvm *kvm, u64 *spte, } desc = desc->more; } - pr_err("%s: %p many->many\n", __func__, spte); - BUG(); + + KVM_BUG_ON_DATA_CORRUPTION(true, kvm); } } diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 9d3ac7720da9..cb86108c624d 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -865,6 +865,25 @@ static inline void kvm_vm_bugged(struct kvm *kvm) unlikely(__ret); \ }) +/* + * Note, "data corruption" refers to corruption of host kernel data structures, + * not guest data. Guest data corruption, suspected or confirmed, that is tied + * and contained to a single VM should *never* BUG() and potentially panic the + * host, i.e. use this variant of KVM_BUG() if and only if a KVM data structure + * is corrupted and that corruption can have a cascading effect to other parts + * of the hosts and/or to other VMs. + */ +#define KVM_BUG_ON_DATA_CORRUPTION(cond, kvm) \ +({ \ + bool __ret = !!(cond); \ + \ + if (IS_ENABLED(CONFIG_BUG_ON_DATA_CORRUPTION)) \ + BUG_ON(__ret); \ + else if (WARN_ON_ONCE(__ret && !(kvm)->vm_bugged)) \ + kvm_vm_bugged(kvm); \ + unlikely(__ret); \ +}) + static inline void kvm_vcpu_srcu_read_lock(struct kvm_vcpu *vcpu) { #ifdef CONFIG_PROVE_RCU