Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp4819518iog; Wed, 22 Jun 2022 06:31:55 -0700 (PDT) X-Google-Smtp-Source: AGRyM1townI3IShbskStn6HXN9FG8uRU6gloLzeIgQW/pkvp8cT7V+E5y2ELL3JoN+9SrOC32XmS X-Received: by 2002:a17:906:9751:b0:722:e52b:aef7 with SMTP id o17-20020a170906975100b00722e52baef7mr3122201ejy.537.1655904715149; Wed, 22 Jun 2022 06:31:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655904715; cv=none; d=google.com; s=arc-20160816; b=ouwZUGxjGinoQmsFTfevMc/TdMAgC6qyCgc+W66aoGCS1dApK01oCs3dZo1bk/1m/d 1T4b7cmwVqT8Xs2zgQyp132caYw7z9i/OAvuVrAR3gceeY1FjSxoyGd0Tztg+1IZZsj8 LKOypwFt6RGtk1uEsUh1EVY8Yu3jhBwTd3ZsTIvza/PpLtQjOGU22vbUWoQrP98CKsKf U2h5cnw87+0wLxK6QRKABUTagU0SzWB210gx6I3Fws4TPawWaQhCA8+TXkr0D1iVZfzo 9wAXghlPdJuxfWZNLI+B998VbGC5adcBHZy0Yqj4Z8txiEJye4LxIYOhqXAhsodZ8Qb+ Vq9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=b1CzVAN2x3XqSNbnvalqdVA8xeb7gjeGwzeljqOJR5Q=; b=uQUKYGR5wUNwbmfBTRqrnmzqgIL2asuj5r3YXFSM4c7pSxvVr+QK0FG3GL0ZoCp7/q ku75wpk6O2NrqNqWXI6DIb8pKGNrjgE//jwA/gcTa8FgI6GaQ+QfXwfDqDMj0xXpOW55 M5PYPqqWfcCaAQf1bsU/t2aKOxlgNFrXYFFym7BkKjmKmV1IYrDRBShtMdUvDiCjqeBk VLYAK4gp4mWvEMYDnzRI2veJes2TH8HrHrrHm9bm/6mVJ8whIwPQMxDzUAJRHRuZalsw /Z2TzHflUcAJcwJsU1x1zA0xpamLKiKQifIVpJkU8ACqWaLKT5HheYZjtc2x2XobC4Nm GlFg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hq18-20020a1709073f1200b007117f5643e4si20579490ejc.944.2022.06.22.06.31.15; Wed, 22 Jun 2022 06:31:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357066AbiFVN3k (ORCPT + 99 others); Wed, 22 Jun 2022 09:29:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1357846AbiFVN2f (ORCPT ); Wed, 22 Jun 2022 09:28:35 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B497463FE for ; Wed, 22 Jun 2022 06:28:03 -0700 (PDT) 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 83B1113D5; Wed, 22 Jun 2022 06:28:03 -0700 (PDT) Received: from [10.57.85.1] (unknown [10.57.85.1]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 398063F534; Wed, 22 Jun 2022 06:28:01 -0700 (PDT) Message-ID: Date: Wed, 22 Jun 2022 14:27:57 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Subject: Re: [PATCH] iommu/dma: Fix race condition during iova_domain initialization Content-Language: en-GB To: Joerg Roedel , yf.wang@mediatek.com Cc: Miles Chen , wsd_upstream@mediatek.com, open list , Libo Kang , "open list:IOMMU DRIVERS" , "moderated list:ARM/Mediatek SoC support" , Ning Li , Matthias Brugger , Will Deacon , "moderated list:ARM/Mediatek SoC support" References: <20220530120748.31733-1-yf.wang@mediatek.com> From: Robin Murphy In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2022-06-22 13:46, Joerg Roedel wrote: > Please re-send with > > Robin Murphy > > in Cc. Apologies, I did spot this before, I've just been tied up with other things and dropping everything non-critical on the floor, so didn't get round to replying before it slipped my mind again. In summary, I hate it, but mostly because the whole situation of calling iommu_probe_device off the back of driver probe is fundamentally broken. I'm still a few steps away from fixing that properly, at which point I can just as well rip all these little bodges out again. If it really does need mitigating in the meantime (i.e. this is real-world async probe, not just some contrived testcase), then I can't easily think of any cleaner hack, so, Acked-by: Robin Murphy (somewhat reluctantly) Cheers, Robin. > On Mon, May 30, 2022 at 08:07:45PM +0800, yf.wang@mediatek.com wrote: >> From: Yunfei Wang >> >> When many devices share the same iova domain, iommu_dma_init_domain() >> may be called at the same time. The checking of iovad->start_pfn will >> all get false in iommu_dma_init_domain() and both enter init_iova_domain() >> to do iovad initialization. >> >> Fix this by protecting init_iova_domain() with iommu_dma_cookie->mutex. >> >> Exception backtrace: >> rb_insert_color(param1=0xFFFFFF80CD2BDB40, param3=1) + 64 >> init_iova_domain() + 180 >> iommu_setup_dma_ops() + 260 >> arch_setup_dma_ops() + 132 >> of_dma_configure_id() + 468 >> platform_dma_configure() + 32 >> really_probe() + 1168 >> driver_probe_device() + 268 >> __device_attach_driver() + 524 >> __device_attach() + 524 >> bus_probe_device() + 64 >> deferred_probe_work_func() + 260 >> process_one_work() + 580 >> worker_thread() + 1076 >> kthread() + 332 >> ret_from_fork() + 16 >> >> Signed-off-by: Ning Li >> Signed-off-by: Yunfei Wang >> --- >> drivers/iommu/dma-iommu.c | 17 +++++++++++++---- >> 1 file changed, 13 insertions(+), 4 deletions(-) >> >> diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c >> index 09f6e1c0f9c0..b38c5041eeab 100644 >> --- a/drivers/iommu/dma-iommu.c >> +++ b/drivers/iommu/dma-iommu.c >> @@ -63,6 +63,7 @@ struct iommu_dma_cookie { >> >> /* Domain for flush queue callback; NULL if flush queue not in use */ >> struct iommu_domain *fq_domain; >> + struct mutex mutex; >> }; >> >> static DEFINE_STATIC_KEY_FALSE(iommu_deferred_attach_enabled); >> @@ -309,6 +310,7 @@ int iommu_get_dma_cookie(struct iommu_domain *domain) >> if (!domain->iova_cookie) >> return -ENOMEM; >> >> + mutex_init(&domain->iova_cookie->mutex); >> return 0; >> } >> >> @@ -549,26 +551,33 @@ static int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base, >> } >> >> /* start_pfn is always nonzero for an already-initialised domain */ >> + mutex_lock(&cookie->mutex); >> if (iovad->start_pfn) { >> if (1UL << order != iovad->granule || >> base_pfn != iovad->start_pfn) { >> pr_warn("Incompatible range for DMA domain\n"); >> - return -EFAULT; >> + ret = -EFAULT; >> + goto done_unlock; >> } >> >> - return 0; >> + ret = 0; >> + goto done_unlock; >> } >> >> init_iova_domain(iovad, 1UL << order, base_pfn); >> ret = iova_domain_init_rcaches(iovad); >> if (ret) >> - return ret; >> + goto done_unlock; >> >> /* If the FQ fails we can simply fall back to strict mode */ >> if (domain->type == IOMMU_DOMAIN_DMA_FQ && iommu_dma_init_fq(domain)) >> domain->type = IOMMU_DOMAIN_DMA; >> >> - return iova_reserve_iommu_regions(dev, domain); >> + ret = iova_reserve_iommu_regions(dev, domain); >> + >> +done_unlock: >> + mutex_unlock(&cookie->mutex); >> + return ret; >> } >> >> /** >> -- >> 2.18.0 > _______________________________________________ > iommu mailing list > iommu@lists.linux-foundation.org > https://lists.linuxfoundation.org/mailman/listinfo/iommu