Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp3649771ybf; Tue, 3 Mar 2020 09:46:58 -0800 (PST) X-Google-Smtp-Source: ADFU+vulxzMdvLZagZDHm+VGjkpx01P5RNjHaIXBR/oMntS2672Vzx0t+FhXTDt9lA0KIIi1dWmk X-Received: by 2002:a9d:178:: with SMTP id 111mr3862402otu.238.1583257617885; Tue, 03 Mar 2020 09:46:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583257617; cv=none; d=google.com; s=arc-20160816; b=pmyfEpc89/cCNs7OvMwktogbchtCn/LAHH9/LeK4Uu0NvXMGaH2Mbi9LYKhqc56b/h pqIwhpZh6dbW6Fj8upqg7HKGo59xv0MHuyP2tGEqcUoTvOXSsS7niB9k49WduDLFyFGE T3n2hc3lwqUBXR5tAfp43BbMT4PvN1bVBTHPWrI/0Sn0lvq39nHsDMe/QmbmpnjUeS3k LdxOvH8pYf+ygBrSlL/TjkXg0/9HQyRBRxZWXlQt3BlsIP0maQBPbhldSkCq+MLwajW9 ucoY1USk4LMLosYxglBjsGddVUXBc3+522NjiIUcpiqWWsuglin00x5l+WkF+D9tQkvR ehgg== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=VC9HqUyrAitJKyRCNMq/L6dm1O4NvuMNyPNndlcBMCc=; b=XFQxpCySxttikcA6AqM82hAuHIZbUfcPD00tqUuj2H0NSnuhH/nFVFsKSnffvQx94b gov+1anywwpPGdIzUAHNsiUZrQKgPwWWcIStkXDZGn8w6cVF8iFaYf3GJ1FwYsr8JEGi fOiw89zZK+t/7jokAQdhWjuBvmm6nkEPdupS7B5R6fCePnjCAjZwkdJ0QG8Ut4rm4pOQ vpJ4qbMtFNO1uRqn4SQwdypeZM/0zr9lBfNzLy0cDmELbvzWfaTBC5/YcVTP7BvRvaty iAfkn4sJwCVewv7RpJDC7GJ8tCaZnGm1r5jaZ3LNLnyzfL9QvEK1EB1gn2xkYqWHLugF VXQg== 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 h5si5435537otr.68.2020.03.03.09.46.45; Tue, 03 Mar 2020 09:46:57 -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; 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 S1730164AbgCCRXV (ORCPT + 99 others); Tue, 3 Mar 2020 12:23:21 -0500 Received: from foss.arm.com ([217.140.110.172]:50200 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727198AbgCCRXV (ORCPT ); Tue, 3 Mar 2020 12:23:21 -0500 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 BC5852F; Tue, 3 Mar 2020 09:23:20 -0800 (PST) Received: from [10.1.196.37] (e121345-lin.cambridge.arm.com [10.1.196.37]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D2C7F3F534; Tue, 3 Mar 2020 09:23:19 -0800 (PST) Subject: Re: [PATCH] iommu/dma: Fix MSI reservation allocation To: Marc Zyngier , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Joerg Roedel , Eric Auger , Will Deacon , stable@vger.kernel.org References: <20200303115154.32263-1-maz@kernel.org> From: Robin Murphy Message-ID: Date: Tue, 3 Mar 2020 17:23:14 +0000 User-Agent: Mozilla/5.0 (X11; Linux aarch64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <20200303115154.32263-1-maz@kernel.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 03/03/2020 11:51 am, Marc Zyngier wrote: > 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 ganule 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). > > Fix it by allocating iommu_dma_msi_page structures one at a time. Bleh, you know you're supposed to be using 64K pages on those things, right? :P > Fixes: 7c1b058c8b5a3 ("iommu/dma: Handle IOMMU API reserved regions") > Signed-off-by: Marc Zyngier > Cc: Robin Murphy > Cc: Joerg Roedel > Cc: Eric Auger > Cc: Will Deacon > Cc: stable@vger.kernel.org > --- > drivers/iommu/dma-iommu.c | 36 ++++++++++++++++++++++++------------ > 1 file changed, 24 insertions(+), 12 deletions(-) > > diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c > index a2e96a5fd9a7..01fa64856c12 100644 > --- a/drivers/iommu/dma-iommu.c > +++ b/drivers/iommu/dma-iommu.c > @@ -171,25 +171,37 @@ static int cookie_init_hw_msi_region(struct iommu_dma_cookie *cookie, > phys_addr_t start, phys_addr_t end) > { > struct iova_domain *iovad = &cookie->iovad; > - struct iommu_dma_msi_page *msi_page; > - int i, num_pages; > + struct iommu_dma_msi_page *msi_page, *tmp; > + int i, num_pages, ret = 0; > + phys_addr_t base; > > - start -= iova_offset(iovad, start); > + base = 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) { > + ret = -ENOMEM; I think we can just return here and skip the cleanup below - by the time we get here the cookie itself has already been allocated and initialised, so even if iommu_dma_init_domain() fails someone else has already accepted the responsibility of calling iommu_put_dma_cookie() at some point later, which will clean up properly. Cheers, Robin. > + break; > + } > + 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; > } > > - return 0; > + if (ret) { > + list_for_each_entry_safe(msi_page, tmp, > + &cookie->msi_page_list, list) { > + if (msi_page->phys >= base && msi_page->phys < start) { > + list_del(&msi_page->list); > + kfree(msi_page); > + } > + } > + } > + > + return ret; > } > > static int iova_reserve_pci_windows(struct pci_dev *dev, >