Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp1265852pxm; Thu, 3 Mar 2022 13:49:11 -0800 (PST) X-Google-Smtp-Source: ABdhPJzvsfijBK3YRR4GOgK8AKYLScaqHz7lyP9qbTj9ml537VhzWz6SIjevSFZM86VBk3VXe/ZH X-Received: by 2002:a17:906:b252:b0:6ce:41e2:5bb7 with SMTP id ce18-20020a170906b25200b006ce41e25bb7mr28139279ejb.179.1646344151322; Thu, 03 Mar 2022 13:49:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646344151; cv=none; d=google.com; s=arc-20160816; b=GdH+EXHHE7Q2RtWfd1/QNATxE1CaYiYNGlp/3eBa8kspwKF9aeNn4rSu8B6YIhnvCD 2d2quzHpSLTt73SuukCpY5VMbhHvUu7iUjsk/Qd+tEeaNrByPwxEyneUNlE1VCQjoLOd lAMn+q4BdPCJ6Xns2/P+p19nvLfaxNRslNknkNu29jtAo9aiu7KnLCXwpS4NfL7XlNCc 7lotUkAbt9C3aJnQ5hd0wXhhkjhg+pRaziupzrYVu+xZ3L9kRASINnifYSWIXJIXikl2 p47Vo+WG5BYqREiHyZhvNF3SEvqr7zZOA02ZDKygCJoMB6Kl5fNIeg7/PY5yNM+rFG4q 3aeA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=0mhM6ZIZyG9V01QBK2obR6j7vDjgYn+2mbAVPRTrPE0=; b=M4tCyGKJS+oSv8uTeQEuNLMVh64Uq8CUxNodAiynmheIUVBQ2Y80z85miofNIIkO7i a8ig5gyK73amD6DDJOnVMlb7qUL7/HNc8lnaxj8vc53/B3NEbgX8YzkyXjJRcvGShGBX 1ZLwc9+zJcXyUXK3HfBp7IVV64Tva+lyN3rM6Ms+CR38rcJIsCCgLlUgnfOuPT3nYmLn eQN+wIJKHsvPy3uSRMjRmgLZ3juR8on7aRzw7wmgLW5EDBi76UmhXNHibw/OJKk9hzHI vCVkdfn2y8znwzIUOGrwGo+8JfoWLooQf7abJ+wJ2jLz3BfbuggmbATxw3qIWS8k/sy/ DS7Q== 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 g12-20020a056402090c00b00415ececba95si1382299edz.346.2022.03.03.13.48.48; Thu, 03 Mar 2022 13:49:11 -0800 (PST) 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 S234113AbiCCOlH (ORCPT + 99 others); Thu, 3 Mar 2022 09:41:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53046 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230385AbiCCOlG (ORCPT ); Thu, 3 Mar 2022 09:41:06 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B0EF21786A7 for ; Thu, 3 Mar 2022 06:40:19 -0800 (PST) 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 429EC1480; Thu, 3 Mar 2022 06:40:19 -0800 (PST) Received: from e121345-lin.cambridge.arm.com (e121345-lin.cambridge.arm.com [10.1.196.40]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 3CC483F66F; Thu, 3 Mar 2022 06:40:18 -0800 (PST) From: Robin Murphy To: joro@8bytes.org, will@kernel.org Cc: iommu@lists.linux-foundation.org, yf.wang@mediatek.com, miles.chen@mediatek.com, wsd_upstream@mediatek.com, linux-kernel@vger.kernel.org Subject: [PATCH] iommu/iova: Improve 32-bit free space estimate Date: Thu, 3 Mar 2022 14:40:08 +0000 Message-Id: <033815732d83ca73b13c11485ac39336f15c3b40.1646318408.git.robin.murphy@arm.com> X-Mailer: git-send-email 2.28.0.dirty MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,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 For various reasons based on the allocator behaviour and typical use-cases at the time, when the max32_alloc_size optimisation was introduced it seemed reasonable to couple the reset of the tracked size to the update of cached32_node upon freeing a relevant IOVA. However, since subsequent optimisations focused on helping genuine 32-bit devices make best use of even more limited address spaces, it is now a lot more likely for cached32_node to be anywhere in a "full" 32-bit address space, and as such more likely for space to become available from IOVAs below that node being freed. At this point, the short-cut in __cached_rbnode_delete_update() really doesn't hold up any more, and we need to fix the logic to reliably provide the expected behaviour. We still want cached32_node to only move upwards, but we should reset the allocation size if *any* 32-bit space has become available. Reported-by: Yunfei Wang Signed-off-by: Robin Murphy --- drivers/iommu/iova.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/iova.c b/drivers/iommu/iova.c index b28c9435b898..170e0f33040e 100644 --- a/drivers/iommu/iova.c +++ b/drivers/iommu/iova.c @@ -95,10 +95,11 @@ __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free) cached_iova = to_iova(iovad->cached32_node); if (free == cached_iova || (free->pfn_hi < iovad->dma_32bit_pfn && - free->pfn_lo >= cached_iova->pfn_lo)) { + free->pfn_lo >= cached_iova->pfn_lo)) iovad->cached32_node = rb_next(&free->node); + + if (free->pfn_lo < iovad->dma_32bit_pfn) iovad->max32_alloc_size = iovad->dma_32bit_pfn; - } cached_iova = to_iova(iovad->cached_node); if (free->pfn_lo >= cached_iova->pfn_lo) -- 2.28.0.dirty