Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp71254rwd; Wed, 7 Jun 2023 19:37:12 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6Zru1HxBM0mzCNcqAeufC3Cxapdbj5a3JHcI4jkoseVHcwGYoyiMKYzGX3Nt992h0swXkx X-Received: by 2002:a05:622a:19aa:b0:3f3:8057:9587 with SMTP id u42-20020a05622a19aa00b003f380579587mr5115607qtc.20.1686191832263; Wed, 07 Jun 2023 19:37:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686191832; cv=none; d=google.com; s=arc-20160816; b=ckxLj05mUxw5W6DvjzwOOHynUHGd6JsCghhjsKMwNK6gspa0NY2HU75RinoOWaGwaD VmyUCz3iB2bUC46wxALP8zu9WU7ESqLpnZJe3BweXafKIhhGCF8UcHJIg1uBJJgbtgQ7 RZMuhc41f+m+P/qB6jsSTA93GkXMRvAs/wx4/4vourBKF7sBRA/7GJyCuK+4v7ml/Na2 UUnR+Ui9miA4anmlCSW4NExQOwG4xkyqYpoS6UIr/bB8vZQrQsESdDgCa1QmQG/6ePUj 1fn1l2opU5n2A7Qo+ap2EeDU3MaOItfGhP7zow+ws0/NDWlAv5vwoTPb2cfJ2ThMXr7G F/iA== 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 :message-id:date:subject:cc:to:from; bh=VxSXqPGNprxd8Nkvhp3lRnc4VyFj1KXT2/b9/MVmmuI=; b=pdn9zHpz70S6scHnVBgmQvhghjXSDYBgUwIemriZri1/6r1j3ogEklIX78hmMfL+uX 821nEOqup/RGJ82WPLcmEb+70l8ilDSLzwkC6nJEcQepwBxgs2z8cW7oRF1AoG03UPVO aFEBiAK+l+apm4hr5JV+0UfQjwszfiZNJfBy0VK8FXJjzMtwB6oO9z+22SaHDOOMhjHQ 08cTXiFUoA01usMJUFuV+QvZmy2OG2oMKGiHZ/magm+8s9SjFoTtHCbHCAlUI6XXNva1 /h2T+BoIv8jYleAEF41fCiOIdazAR7+9TadWejitjaF3NNrlc8XrHZWDeG1jmuQ/RYVC wyoQ== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h130-20020a636c88000000b0052c27a0125bsi211272pgc.738.2023.06.07.19.37.00; Wed, 07 Jun 2023 19:37:12 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233485AbjFHCWu (ORCPT + 99 others); Wed, 7 Jun 2023 22:22:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40650 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233153AbjFHCWr (ORCPT ); Wed, 7 Jun 2023 22:22:47 -0400 Received: from mail.nfschina.com (unknown [42.101.60.195]) by lindbergh.monkeyblade.net (Postfix) with SMTP id 7E26D1BE2 for ; Wed, 7 Jun 2023 19:22:45 -0700 (PDT) Received: from localhost.localdomain (unknown [180.167.10.98]) by mail.nfschina.com (Maildata Gateway V2.8.8) with ESMTPA id E2E3E1801128CB; Thu, 8 Jun 2023 10:20:42 +0800 (CST) X-MD-Sfrom: suhui@nfschina.com X-MD-SrcIP: 180.167.10.98 From: Su Hui To: Joerg Roedel , Suravee Suthikulpanit , Will Deacon , Robin Murphy Cc: kevin.tian@intel.com, vasant.hegde@amd.com, jgg@ziepe.ca, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Su Hui Subject: [PATCH v2] iommu/amd: Fix possible memory leak of 'domain' Date: Thu, 8 Jun 2023 10:19:34 +0800 Message-Id: <20230608021933.856045-1-suhui@nfschina.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,RDNS_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no 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 Move allocation code down to avoid memory leak. Fixes: 29f54745f245 ("iommu/amd: Add missing domain type checks") Signed-off-by: Su Hui --- Changes in v2: change code order is better and more consistent with other drivers. drivers/iommu/amd/iommu.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index dc1ec6849775..e8a2e5984acb 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -2078,10 +2078,6 @@ static struct protection_domain *protection_domain_alloc(unsigned int type) int mode = DEFAULT_PGTABLE_LEVEL; int ret; - domain = kzalloc(sizeof(*domain), GFP_KERNEL); - if (!domain) - return NULL; - /* * Force IOMMU v1 page table when iommu=pt and * when allocating domain for pass-through devices. @@ -2097,6 +2093,10 @@ static struct protection_domain *protection_domain_alloc(unsigned int type) return NULL; } + domain = kzalloc(sizeof(*domain), GFP_KERNEL); + if (!domain) + return NULL; + switch (pgtable) { case AMD_IOMMU_V1: ret = protection_domain_init_v1(domain, mode); -- 2.30.2