Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp1184961ybf; Thu, 27 Feb 2020 06:29:22 -0800 (PST) X-Google-Smtp-Source: APXvYqzemwAsAqCBMU/O/mhxPP/9y7WmjdK5lHg11bk5ylGSc50sr7+j36GzYLbTt8d0yVMMtUXO X-Received: by 2002:a9d:3df6:: with SMTP id l109mr3571006otc.284.1582813762060; Thu, 27 Feb 2020 06:29:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582813762; cv=none; d=google.com; s=arc-20160816; b=dOyBo0628qjNPlmZQYRhI5hXstDmmxdEJbC0nCrcrv7nnCTQHuKg5Lw94p7KEeIewu YvUyMEy/aLkzrT4P1cnE7OPhC8YbdaZpgtlfQTObE68QongqIaUSUOrgZ6D2iu9oAOlq IdjkanSrjopNeWTVwAI8aTr+EwLSXze8IrTvlY+/FDNYbC1Y+DcoNw4WtZs/rkDmJfUX GckFxZD9xnzNbx0HXkShmTRGPVthd3aO9YFpTIAfuHqcd/RKm+39arwNguKVsTSZwPyL /8xwuCpxAEsLuROSQm6lIhH1KhJvRmd2/+Yb1lxlqv81FYvj8S5GSP8FfkcX8JUI/OJM sYRw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=NNs/N/0tGkWL5E3Th7mZdv8gBcBT0BbFRSi9xkbgjGw=; b=l9+jPTOB6uER2xSTO1M3pUzBu3/mWtsnVAy2cXNSXENJHGMISkKvG/1qFy6yoXiAle 42sCXkHSisN/9AcbC4evzwv5SyzPobOhIkKUIalpMkUug03WVDsn8+AdpPG/HYSkt66B Y/i+hQyAHi1HEu3zlSp7FwjXkkk7Ntn64yZCYodNheiwoXQtnmUWF2HIxi7+CBdQq/RO q9pD1O1IM1ZfuLHQJb4UwMzKLbbqqvAwnDrBiiXkSgewUGC0wk3TXNvdcqn3poplB553 K5ZkAxg9B3lz3rRqpi86IG9Skthstiz24P/f6gVVDkZpfUBoKqsw7WG35a/UASZc1KxO ZSlQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=t3gSnPa0; 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 a22si1387135oii.98.2020.02.27.06.29.09; Thu, 27 Feb 2020 06:29:22 -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; dkim=pass header.i=@kernel.org header.s=default header.b=t3gSnPa0; 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 S1733108AbgB0OHD (ORCPT + 99 others); Thu, 27 Feb 2020 09:07:03 -0500 Received: from mail.kernel.org ([198.145.29.99]:44254 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387995AbgB0OGz (ORCPT ); Thu, 27 Feb 2020 09:06:55 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 3818421D7E; Thu, 27 Feb 2020 14:06:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1582812414; bh=uPZNUo6hZRw9EESZm12SGL2n6SkjBacg92Va+/AxakM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=t3gSnPa0iz84SYkLVicvPEzwWgyV3lx7aL7zKx7fDJ/iO4qHPznw5kTKU1yx6fxWs 4WC0pCbesqmYqMKObbc9yRPV++tClDtRww7264iUQc1EW7+zol+QqyqHiovd6BR/P3 tNglbppIrYZ2WBaxMRYFqSRwltuB0LqpcAqmhO0w= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Brian Masney , Naresh Kamboju , Robin Murphy , Stephan Gerhold , Joerg Roedel Subject: [PATCH 5.4 001/135] iommu/qcom: Fix bogus detach logic Date: Thu, 27 Feb 2020 14:35:41 +0100 Message-Id: <20200227132228.973307226@linuxfoundation.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200227132228.710492098@linuxfoundation.org> References: <20200227132228.710492098@linuxfoundation.org> User-Agent: quilt/0.66 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Robin Murphy commit faf305c51aeabd1ea2d7131e798ef5f55f4a7750 upstream. Currently, the implementation of qcom_iommu_domain_free() is guaranteed to do one of two things: WARN() and leak everything, or dereference NULL and crash. That alone is terrible, but in fact the whole idea of trying to track the liveness of a domain via the qcom_domain->iommu pointer as a sanity check is full of fundamentally flawed assumptions. Make things robust and actually functional by not trying to be quite so clever. Reported-by: Brian Masney Tested-by: Brian Masney Reported-by: Naresh Kamboju Fixes: 0ae349a0f33f ("iommu/qcom: Add qcom_iommu") Signed-off-by: Robin Murphy Tested-by: Stephan Gerhold Cc: stable@vger.kernel.org # v4.14+ Signed-off-by: Joerg Roedel Signed-off-by: Greg Kroah-Hartman --- drivers/iommu/qcom_iommu.c | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) --- a/drivers/iommu/qcom_iommu.c +++ b/drivers/iommu/qcom_iommu.c @@ -345,21 +345,19 @@ static void qcom_iommu_domain_free(struc { struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain); - if (WARN_ON(qcom_domain->iommu)) /* forgot to detach? */ - return; - iommu_put_dma_cookie(domain); - /* NOTE: unmap can be called after client device is powered off, - * for example, with GPUs or anything involving dma-buf. So we - * cannot rely on the device_link. Make sure the IOMMU is on to - * avoid unclocked accesses in the TLB inv path: - */ - pm_runtime_get_sync(qcom_domain->iommu->dev); - - free_io_pgtable_ops(qcom_domain->pgtbl_ops); - - pm_runtime_put_sync(qcom_domain->iommu->dev); + if (qcom_domain->iommu) { + /* + * NOTE: unmap can be called after client device is powered + * off, for example, with GPUs or anything involving dma-buf. + * So we cannot rely on the device_link. Make sure the IOMMU + * is on to avoid unclocked accesses in the TLB inv path: + */ + pm_runtime_get_sync(qcom_domain->iommu->dev); + free_io_pgtable_ops(qcom_domain->pgtbl_ops); + pm_runtime_put_sync(qcom_domain->iommu->dev); + } kfree(qcom_domain); } @@ -405,7 +403,7 @@ static void qcom_iommu_detach_dev(struct struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain); unsigned i; - if (!qcom_domain->iommu) + if (WARN_ON(!qcom_domain->iommu)) return; pm_runtime_get_sync(qcom_iommu->dev); @@ -418,8 +416,6 @@ static void qcom_iommu_detach_dev(struct ctx->domain = NULL; } pm_runtime_put_sync(qcom_iommu->dev); - - qcom_domain->iommu = NULL; } static int qcom_iommu_map(struct iommu_domain *domain, unsigned long iova,