Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp1147755ybv; Sun, 23 Feb 2020 00:13:47 -0800 (PST) X-Google-Smtp-Source: APXvYqziatmSo/EqcBufDK7OBpjAyGkyu2CJyQxtUnRLS5iUQW6dLp3wSm82kfhuxmzzkKVp3Gic X-Received: by 2002:a05:6830:138b:: with SMTP id d11mr33683727otq.38.1582445627035; Sun, 23 Feb 2020 00:13:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582445627; cv=none; d=google.com; s=arc-20160816; b=U9vn1RcFsc6bA/OCqRZm3WSzNmWlS2QBDN1y02WFeLIZLoUArjwa+6n4W/Z32wsxQW EJclbkzi0eeK5xz1pZdqit3lx6nYz1vf70KwsrcrWBYADeP2bVZA0PXMtbQAkJrfuoXX JhVPZFZuMWUM4iVq8bttp42cF/nrgwZrtkO0ebHEAITLYMKSsTI1zsq15FBeizOrDFNH BQKasyLIfBzEMB+4DSDXxd+ST74bk7Sj3n7K3tdVdW0CGMeKIl8i/ABlEv1435lPBBwX P04UyZVBEn4NfWydV8e/wTjXcQPiz4HU66g0qpL8y3dux38zYAwUeBFQnlnS8vBF+gUE 50FA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:to:from; bh=0B6gBVizzxVSoYpQsNNfnHPnHQR8YONgUzRdD67fmPY=; b=OK0nvEp+CIM9nqiH9/L35q+FwLomd02qAluKaL6xO3Ev042e4hlzbn8rkR99aiOLMc KzmPqLOVMRZRXS6jvT1GRJqiwzZHzn57TPWE/aiz8xW9jWBcbwOrGfPyfeMBt84YV8Le e7ZFupNuyzgANeRKeZ6qYkwsqFg5dm81GFRV3/hfwx7siZ6Jts//LagCM6duJMNXtfVm gTKmymi9jMhOubWC2pA0js6k6A5pwpCOEUBloTchKSr+tRDCfJ0jlXxkjZYhGbzFwsS3 3MPwwy/XgvIiZtqnJa4Ooz0rrpSgsZjALfLL6fXEXNAafuQEZxEVftFmcxkNr58xa8JI Ks+g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 2si2687109oij.97.2020.02.23.00.13.34; Sun, 23 Feb 2020 00:13:47 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726748AbgBWIN0 (ORCPT + 99 others); Sun, 23 Feb 2020 03:13:26 -0500 Received: from mx59.baidu.com ([61.135.168.59]:25356 "EHLO tc-sys-mailedm02.tc.baidu.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726386AbgBWIN0 (ORCPT ); Sun, 23 Feb 2020 03:13:26 -0500 Received: from localhost (cp01-cos-dev01.cp01.baidu.com [10.92.119.46]) by tc-sys-mailedm02.tc.baidu.com (Postfix) with ESMTP id 82DE111C0034; Sun, 23 Feb 2020 16:13:12 +0800 (CST) From: Li RongQing To: pbonzini@redhat.com, sean.j.christopherson@intel.com, vkuznets@redhat.com, wanpengli@tencent.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH][resend] KVM: fix error handling in svm_hardware_setup Date: Sun, 23 Feb 2020 16:13:12 +0800 Message-Id: <1582445592-732-1-git-send-email-lirongqing@baidu.com> X-Mailer: git-send-email 1.7.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org rename svm_hardware_unsetup as svm_hardware_teardown, move it before svm_hardware_setup, and call it to free all memory if fail to setup in svm_hardware_setup, otherwise memory will be leaked remove __exit attribute for it since it is called in __init function Signed-off-by: Li RongQing Reviewed-by: Vitaly Kuznetsov --- original patch: https://patchwork.kernel.org/patch/10852055/ arch/x86/kvm/svm.c | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index a3e32d61d60c..7cde582fb624 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -1350,6 +1350,24 @@ static __init void svm_adjust_mmio_mask(void) kvm_mmu_set_mmio_spte_mask(mask, mask, PT_WRITABLE_MASK | PT_USER_MASK); } +static void svm_hardware_teardown(void) +{ + int cpu; + + if (svm_sev_enabled()) { + bitmap_free(sev_asid_bitmap); + bitmap_free(sev_reclaim_asid_bitmap); + + sev_flush_asids(); + } + + for_each_possible_cpu(cpu) + svm_cpu_uninit(cpu); + + __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER); + iopm_base = 0; +} + static __init int svm_hardware_setup(void) { int cpu; @@ -1463,29 +1481,10 @@ static __init int svm_hardware_setup(void) return 0; err: - __free_pages(iopm_pages, IOPM_ALLOC_ORDER); - iopm_base = 0; + svm_hardware_teardown(); return r; } -static __exit void svm_hardware_unsetup(void) -{ - int cpu; - - if (svm_sev_enabled()) { - bitmap_free(sev_asid_bitmap); - bitmap_free(sev_reclaim_asid_bitmap); - - sev_flush_asids(); - } - - for_each_possible_cpu(cpu) - svm_cpu_uninit(cpu); - - __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER); - iopm_base = 0; -} - static void init_seg(struct vmcb_seg *seg) { seg->selector = 0; @@ -7378,7 +7377,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .cpu_has_kvm_support = has_svm, .disabled_by_bios = is_disabled, .hardware_setup = svm_hardware_setup, - .hardware_unsetup = svm_hardware_unsetup, + .hardware_unsetup = svm_hardware_teardown, .check_processor_compatibility = svm_check_processor_compat, .hardware_enable = svm_hardware_enable, .hardware_disable = svm_hardware_disable, -- 2.16.2