From 87cd3d689e0075a3eba9e3a805b3a7385345e2cf Mon Sep 17 00:00:00 2001 From: Su Hui Date: Thu, 8 Jun 2023 10:19:34 +0800 Subject: [PATCH] UPSTREAM: iommu/amd: Fix possible memory leak of 'domain' Move allocation code down to avoid memory leak. Bug: 254441685 Fixes: 29f54745f245 ("iommu/amd: Add missing domain type checks") Signed-off-by: Su Hui Reviewed-by: Jason Gunthorpe Reviewed-by: Jerry Snitselaar Reviewed-by: Vasant Hegde Link: https://lore.kernel.org/r/20230608021933.856045-1-suhui@nfschina.com Signed-off-by: Joerg Roedel (cherry picked from commit 5b00369fcf6d1ff9050b94800dc596925ff3623f) Signed-off-by: Lee Jones Change-Id: If78fac206bfdc899bed14335ed46eda3946591a2 --- 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 d7676008ff57..34674cc428e5 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -2055,10 +2055,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. @@ -2074,6 +2070,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);