Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp1454200rwe; Thu, 1 Sep 2022 20:04:54 -0700 (PDT) X-Google-Smtp-Source: AA6agR6Su+Fpv9BebVut8ssqC9BB7tSDbGS59YJKCKJlmk6VMJu93emYeJJMOEcE/5RL+0Jcw8wS X-Received: by 2002:a17:90b:33d1:b0:1fd:6e9f:548c with SMTP id lk17-20020a17090b33d100b001fd6e9f548cmr2519511pjb.137.1662087894544; Thu, 01 Sep 2022 20:04:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662087894; cv=none; d=google.com; s=arc-20160816; b=EM8yV7rcqcBAg6/SAgMN8o5NsywW3RkPkyVkq2JpTXFx2PB9zlMHXL2VOxCTNEIw/Q /snPOzjxbIg+FLFfauGTQIJ2MZJkyf0A/WJSLmlXJWoGNJIhPrycnD7Yz5FTIKfQEW8Y H5pQZGaMLZYL2IV8J+oBteofUI42R9k3Qyj7yXHTbk3ivoQaupnaaQjX2Jusw3lbm4IG Kdp/eFS57fhrx/oYL36V2iJ5sdWIxbMI2CFLqW0DD/BLSDEdLqUZJjYQaQUUC2hlLHun I6NRf7mylWBOXYwj8pU6TXPk7pzZbFTivkHMjQ3JV5x1UtDtO8mt6kZE7302Yh0kX9Oh dong== 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; bh=u1G9MyTmJ6mAVJyq3jw98SLO+LEX77uz6eUb4Lm4ynA=; b=CQIOOInrl09dJ4guNEEhDvEDCKJbFgL1mbcvG5ncb4Q4NAli7h7Dww/F5JkTr4+B5l 9ySXqoaR2UNBxLJuzA8YdDY77y1NXof16JM2amrhNTJJE/rtED/783oXv+Jc66iuSFZA UVLvu6/jc/NGYeerWsTu6C2Lp2Pb0GIZ+WJKDdlE6DafpX6GPubjXI+HU0EieuLgL2+4 GlBytrvIXB2xGTKJeoENENjxTx9HV6E7m1+xpHH9slj9KcWtPtdkSbebcAwv9LZK9AHy sZzhkB1t2ecefzDFFte6hNCV3kMSbnPTO0dhUB+wtPYna9PhDSzhv879G+br2z9X1guk rZkw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=antgroup.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c4-20020a170902f30400b001728c1ed8bdsi734164ple.153.2022.09.01.20.04.37; Thu, 01 Sep 2022 20:04:54 -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; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=antgroup.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234695AbiIBCrN (ORCPT + 99 others); Thu, 1 Sep 2022 22:47:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32954 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231764AbiIBCrJ (ORCPT ); Thu, 1 Sep 2022 22:47:09 -0400 Received: from out0-143.mail.aliyun.com (out0-143.mail.aliyun.com [140.205.0.143]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0D0A28C03F; Thu, 1 Sep 2022 19:47:06 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R651e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018047203;MF=houwenlong.hwl@antgroup.com;NM=1;PH=DS;RN=10;SR=0;TI=SMTPD_---.P5kqMp4_1662086823; Received: from localhost(mailfrom:houwenlong.hwl@antgroup.com fp:SMTPD_---.P5kqMp4_1662086823) by smtp.aliyun-inc.com; Fri, 02 Sep 2022 10:47:03 +0800 From: "Hou Wenlong" To: kvm@vger.kernel.org Cc: Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org Subject: [PATCH v2 1/2] KVM: x86: Return emulator error if RDMSR/WRMSR emulation failed Date: Fri, 02 Sep 2022 10:47:00 +0800 Message-Id: <09b2847fc3bcb8937fb11738f0ccf7be7f61d9dd.1661930557.git.houwenlong.hwl@antgroup.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY 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 The return value of emulator_{get|set}_mst_with_filter() is confused, since msr access error and emulator error are mixed. Although, KVM_MSR_RET_* doesn't conflict with X86EMUL_IO_NEEDED at present, it is better to convert msr access error to emulator error if error value is needed. So move "r < 0" handling for wrmsr emulation into the set helper function, then only X86EMUL_* is returned in the helper functions. Also add "r < 0" check in the get helper function, although KVM doesn't return -errno today, but assuming that will always hold true is unnecessarily risking. Suggested-by: Sean Christopherson Signed-off-by: Hou Wenlong --- arch/x86/kvm/emulate.c | 20 ++++++++------------ arch/x86/kvm/x86.c | 26 ++++++++++++++++---------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index f092c54d1a2f..d16f7d6a81bb 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -3645,13 +3645,10 @@ static int em_wrmsr(struct x86_emulate_ctxt *ctxt) | ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32); r = ctxt->ops->set_msr_with_filter(ctxt, msr_index, msr_data); - if (r == X86EMUL_IO_NEEDED) - return r; - - if (r > 0) + if (r == X86EMUL_PROPAGATE_FAULT) return emulate_gp(ctxt, 0); - return r < 0 ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE; + return r; } static int em_rdmsr(struct x86_emulate_ctxt *ctxt) @@ -3662,15 +3659,14 @@ static int em_rdmsr(struct x86_emulate_ctxt *ctxt) r = ctxt->ops->get_msr_with_filter(ctxt, msr_index, &msr_data); - if (r == X86EMUL_IO_NEEDED) - return r; - - if (r) + if (r == X86EMUL_PROPAGATE_FAULT) return emulate_gp(ctxt, 0); - *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data; - *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32; - return X86EMUL_CONTINUE; + if (r == X86EMUL_CONTINUE) { + *reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data; + *reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32; + } + return r; } static int em_store_sreg(struct x86_emulate_ctxt *ctxt, int segment) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index d7374d768296..2596f5736743 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7920,14 +7920,17 @@ static int emulator_get_msr_with_filter(struct x86_emulate_ctxt *ctxt, int r; r = kvm_get_msr_with_filter(vcpu, msr_index, pdata); + if (r < 0) + return X86EMUL_UNHANDLEABLE; - if (r && kvm_msr_user_space(vcpu, msr_index, KVM_EXIT_X86_RDMSR, 0, - complete_emulated_rdmsr, r)) { - /* Bounce to user space */ - return X86EMUL_IO_NEEDED; + if (r) { + if (kvm_msr_user_space(vcpu, msr_index, KVM_EXIT_X86_RDMSR, 0, + complete_emulated_rdmsr, r)) + return X86EMUL_IO_NEEDED; + return X86EMUL_PROPAGATE_FAULT; } - return r; + return X86EMUL_CONTINUE; } static int emulator_set_msr_with_filter(struct x86_emulate_ctxt *ctxt, @@ -7937,14 +7940,17 @@ static int emulator_set_msr_with_filter(struct x86_emulate_ctxt *ctxt, int r; r = kvm_set_msr_with_filter(vcpu, msr_index, data); + if (r < 0) + return X86EMUL_UNHANDLEABLE; - if (r && kvm_msr_user_space(vcpu, msr_index, KVM_EXIT_X86_WRMSR, data, - complete_emulated_msr_access, r)) { - /* Bounce to user space */ - return X86EMUL_IO_NEEDED; + if (r) { + if (kvm_msr_user_space(vcpu, msr_index, KVM_EXIT_X86_WRMSR, data, + complete_emulated_msr_access, r)) + return X86EMUL_IO_NEEDED; + return X86EMUL_PROPAGATE_FAULT; } - return r; + return X86EMUL_CONTINUE; } static int emulator_get_msr(struct x86_emulate_ctxt *ctxt, -- 2.31.1