Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp4398976ybf; Wed, 4 Mar 2020 03:12:07 -0800 (PST) X-Google-Smtp-Source: ADFU+vulA07t3SOIpf1EzDUADY511jGVNaaNrQlVaQwqoTQ3FvjuYA26utfezZ41ElAFYVe1Zr+W X-Received: by 2002:aca:cd0e:: with SMTP id d14mr1255021oig.167.1583320327354; Wed, 04 Mar 2020 03:12:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583320327; cv=none; d=google.com; s=arc-20160816; b=Wojl5tbijWMWTDe/nXKXzI+weeLub3EfIqBDGaF/wPoTLj6VbK2U83uMpVv1ODQR9c 43IUlra9JJ6mfpCrZwVbe+gDAj3WZGyAoQrCRW3YGaJvSawgw+EiQoCO5iN4Bub6uRbL jVE7+jH72VcT66I7MfwBUqfYvgilY+/Les6da+3qK6IVBZLg5l6uAIhc8DhCB3qP5v7L Te6pCej2GJRu/GJqXGUXy+wlVp2EzN93DSwaeaM0weC+Y5LEtZpjXO88vwglRr9YLTGC 0W0RUimbOIFXS/Ae+fxwnwUarD1wAETfuIqrB0T6KaEVK4lDJYQl6HI0xtbMd0oVYFqW fPNg== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=BADOEHRqRI/pvE/QZh+/utSTAw1AxePZgmL+8gdhMTU=; b=wWTmU/6JoawgDvAYoCSLFeTWsnXhTGfMXZd1Tc4BcIUJY0Qk/D4qmU6/J4OFkNJ40d q0SNUMa4jBZUim60vAEcCd+AxKM+4q/TnZqoYxoJzg4MoUX+wxQ/i26u2k1GqIRjH2Lr 94b7yTZg10ndgpnAbhJO4oUiY+FYH/eVwH8cGuytoc3VcBVGFtFd0j9tfSMFf12yg0Aq k+WLM+3vBD2NnBMtOBEzn29Ym/11zg+EqFzEPMhcbaqyzKsKn8MabJYCktXYJ+pafqDp Ftadp0DG1SqEK+heen7aCw7cXkQ6zk0uRlyviYfUaKj1UbJMhWGf/ym5PfaqFDVgOz1P 7aOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=CQKgD6CQ; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n11si983612otf.36.2020.03.04.03.11.55; Wed, 04 Mar 2020 03:12:07 -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=CQKgD6CQ; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387833AbgCDLLq (ORCPT + 99 others); Wed, 4 Mar 2020 06:11:46 -0500 Received: from mail.kernel.org ([198.145.29.99]:55236 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387488AbgCDLLq (ORCPT ); Wed, 4 Mar 2020 06:11:46 -0500 Received: from disco-boy.misterjones.org (disco-boy.misterjones.org [51.254.78.96]) (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 41F6520709; Wed, 4 Mar 2020 11:11:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1583320305; bh=6QGmwW91nEJvgxEq+40qvnflN5tJsMSuNiAE6L04isA=; h=From:To:Cc:Subject:Date:From; b=CQKgD6CQPxw+O3Y3MLwSfh7ElCLjT18osaxje158rq6UtfnVF+Xj0GmwvRoitv2Hv fYaxSDobn4YRorkPEW5a8eWk6X9VbJQM/liqcf0rQFLiOqr7kjaLEEAxxtbvIV1CQ0 oBTWpTNEtELu72smIlmu02oZceuEvNkfsyooD/rI= Received: from 78.163-31-62.static.virginmediabusiness.co.uk ([62.31.163.78] helo=why.lan) by disco-boy.misterjones.org with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1j9RwF-009yDa-GB; Wed, 04 Mar 2020 11:11:43 +0000 From: Marc Zyngier To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Eric Auger , Robin Murphy , Joerg Roedel , Will Deacon , stable@vger.kernel.org Subject: [PATCH v2] iommu/dma: Fix MSI reservation allocation Date: Wed, 4 Mar 2020 11:11:17 +0000 Message-Id: <20200304111117.3540-1-maz@kernel.org> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 62.31.163.78 X-SA-Exim-Rcpt-To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, eric.auger@redhat.com, robin.murphy@arm.com, jroedel@suse.de, will@kernel.org, stable@vger.kernel.org X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The way cookie_init_hw_msi_region() allocates the iommu_dma_msi_page structures doesn't match the way iommu_put_dma_cookie() frees them. The former performs a single allocation of all the required structures, while the latter tries to free them one at a time. It doesn't quite work for the main use case (the GICv3 ITS where the range is 64kB) when the base granule size is 4kB. This leads to a nice slab corruption on teardown, which is easily observable by simply creating a VF on a SRIOV-capable device, and tearing it down immediately (no need to even make use of it). Fortunately, this only affects systems where the ITS isn't translated by the SMMU, which are both rare and non-standard. Fix it by allocating iommu_dma_msi_page structures one at a time. Fixes: 7c1b058c8b5a3 ("iommu/dma: Handle IOMMU API reserved regions") Signed-off-by: Marc Zyngier Reviewed-by: Eric Auger Cc: Robin Murphy Cc: Joerg Roedel Cc: Will Deacon Cc: stable@vger.kernel.org --- * From v1: - Got rid of the superfluous error handling (Robin) - Clarified that it only affects a very small set of systems - Added Eric's RB (which I assume still stands) drivers/iommu/dma-iommu.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index a2e96a5fd9a7..ba128d1cdaee 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -177,15 +177,15 @@ static int cookie_init_hw_msi_region(struct iommu_dma_cookie *cookie, start -= iova_offset(iovad, start); num_pages = iova_align(iovad, end - start) >> iova_shift(iovad); - msi_page = kcalloc(num_pages, sizeof(*msi_page), GFP_KERNEL); - if (!msi_page) - return -ENOMEM; - for (i = 0; i < num_pages; i++) { - msi_page[i].phys = start; - msi_page[i].iova = start; - INIT_LIST_HEAD(&msi_page[i].list); - list_add(&msi_page[i].list, &cookie->msi_page_list); + msi_page = kmalloc(sizeof(*msi_page), GFP_KERNEL); + if (!msi_page) + return -ENOMEM; + + msi_page->phys = start; + msi_page->iova = start; + INIT_LIST_HEAD(&msi_page->list); + list_add(&msi_page->list, &cookie->msi_page_list); start += iovad->granule; } -- 2.20.1