Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp3531927ybi; Mon, 10 Jun 2019 11:52:32 -0700 (PDT) X-Google-Smtp-Source: APXvYqzSzWdw4qTmU5jf9fYwIRhI1RsBDsCALHb3WRIUux3Q3Y30VxQ7RERW7/I1I6wE3ref/Gy/ X-Received: by 2002:a63:6ec6:: with SMTP id j189mr11595434pgc.168.1560192752597; Mon, 10 Jun 2019 11:52:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560192752; cv=none; d=google.com; s=arc-20160816; b=diQikaL62cuwbXYHTCexxRw58NVvgPPrko6hA0eHkj3Ow4ioTOp5A4wCGGwH/jM3Lh ODpQhiRHO4yfWplR9/S0UXsECR3HyskEfMKkwSCR/WTTeUkeyEt1zydIpkfwMeaoKPsI SG6ktryXumcV0FzeCgt0y09g+A6DejTR9HUilqdU5U3ojQnYAVmRIBOgepk7F6h4tpyO DgJQsRhkzNxFvgn+QgZ7hXZjosesyEiUWhQ9csqP6v5KstjKzy4UFQ5hbfkN5XUGa6qP Da0chf2YYrkoGP92Q3evbfZ9UKvhNM8xg2bavMNmtyphW0EnYZeSwLyy/ovlpHTLbmNs dytw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=+WQWlI5tnOOcKZwUptMexsep3kPDZGl2yhTtybtZoDE=; b=oJesLjN5qk5tbXVfTS42p/Dh3qSui0nFZ5QNw+0WUaP2KqtgCBmAuy6UmMeUGyXrpu Qpn3nbMGWlkXh1IkcyEOtC+LEfRW35uf8uTho8OpT9zjYwaaRHZt/6h5+WgTHMlxDF1d wUZw/SrSUYxGiWcC5XiH5Bp/qDUcEcGXuIjkSxDxJXuiWNpxCdS4RToI++1oXu6kiINm CNYmBXaRl1MSt/QmZr9ECUaKwSxbWsNpZbd3zhmzYiavifLKT5VL1NO52qZmwbY3QUDn IdwgIEKM8uc6LdbPwd27RenHl863hqy0T5OSHm6pJh/CuzMo9GfYmH8VzQyOpIgAsX1t 1Kmg== 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 x3si9900291pgr.22.2019.06.10.11.52.17; Mon, 10 Jun 2019 11:52:32 -0700 (PDT) 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 S2388945AbfFJSwD (ORCPT + 99 others); Mon, 10 Jun 2019 14:52:03 -0400 Received: from foss.arm.com ([217.140.110.172]:47620 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388909AbfFJSwB (ORCPT ); Mon, 10 Jun 2019 14:52:01 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 854E4ED1; Mon, 10 Jun 2019 11:52:00 -0700 (PDT) Received: from ostrya.cambridge.arm.com (ostrya.cambridge.arm.com [10.1.196.129]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 167603F246; Mon, 10 Jun 2019 11:51:58 -0700 (PDT) From: Jean-Philippe Brucker To: will.deacon@arm.com Cc: joro@8bytes.org, robh+dt@kernel.org, mark.rutland@arm.com, robin.murphy@arm.com, jacob.jun.pan@linux.intel.com, iommu@lists.linux-foundation.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, eric.auger@redhat.com Subject: [PATCH 7/8] iommu/arm-smmu-v3: Improve add_device() error handling Date: Mon, 10 Jun 2019 19:47:13 +0100 Message-Id: <20190610184714.6786-8-jean-philippe.brucker@arm.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190610184714.6786-1-jean-philippe.brucker@arm.com> References: <20190610184714.6786-1-jean-philippe.brucker@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Let add_device() clean up behind itself. The iommu_bus_init() function does call remove_device() on error, but other sites (e.g. of_iommu) do not. Don't free level-2 stream tables because we'd have to track if we allocated each of them or if they are used by other endpoints. It's not worth the hassle since they are managed resources. Signed-off-by: Jean-Philippe Brucker --- drivers/iommu/arm-smmu-v3.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/drivers/iommu/arm-smmu-v3.c b/drivers/iommu/arm-smmu-v3.c index 633d829f246f..972bfb80f964 100644 --- a/drivers/iommu/arm-smmu-v3.c +++ b/drivers/iommu/arm-smmu-v3.c @@ -2398,14 +2398,16 @@ static int arm_smmu_add_device(struct device *dev) for (i = 0; i < master->num_sids; i++) { u32 sid = master->sids[i]; - if (!arm_smmu_sid_in_range(smmu, sid)) - return -ERANGE; + if (!arm_smmu_sid_in_range(smmu, sid)) { + ret = -ERANGE; + goto err_free_master; + } /* Ensure l2 strtab is initialised */ if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) { ret = arm_smmu_init_l2_strtab(smmu, sid); if (ret) - return ret; + goto err_free_master; } } @@ -2419,13 +2421,25 @@ static int arm_smmu_add_device(struct device *dev) if (!(smmu->features & ARM_SMMU_FEAT_2_LVL_CDTAB)) master->ssid_bits = min(master->ssid_bits, 10U); + ret = iommu_device_link(&smmu->iommu, dev); + if (ret) + goto err_free_master; + group = iommu_group_get_for_dev(dev); - if (!IS_ERR(group)) { - iommu_group_put(group); - iommu_device_link(&smmu->iommu, dev); + if (IS_ERR(group)) { + ret = PTR_ERR(group); + goto err_unlink; } - return PTR_ERR_OR_ZERO(group); + iommu_group_put(group); + return 0; + +err_unlink: + iommu_device_unlink(&smmu->iommu, dev); +err_free_master: + kfree(master); + fwspec->iommu_priv = NULL; + return ret; } static void arm_smmu_remove_device(struct device *dev) -- 2.21.0