Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1151878pxu; Fri, 27 Nov 2020 00:33:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJzg5kGsT5AZqXNuPTPPy6dOeTyXfoYNiY6v6hoYjZ6r0Hgv67OSFEyuxqEpIDU9y0mlvpam X-Received: by 2002:a17:906:4410:: with SMTP id x16mr6209318ejo.536.1606466018712; Fri, 27 Nov 2020 00:33:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606466018; cv=none; d=google.com; s=arc-20160816; b=Y1xXcj8ikRKkdRT8AQIG8jQNnZnju4VfNUVBAsWuKysbMowesCTAkA8tzhXsmi/AP0 d5MI3Y7/iOYWEX+bnOFOcwGDAqG/YOevLs3s0z8K6gRk/4ni3+KLdpaUUwV/d1/rQieu PUrpQGqZhSPDc5qAfqRfWhGFT8+hf9QsyL58XolDi0DI60BeLwuqovpVxryL756LEOaX 42P/wWFQp+YjG9If0XQT1MmyzrIjpV/xWQbvKBr/Qtckmx+4aG3XfbgAuVLcFgLND6E8 UdTB7acNzEZZkseD3pdAiFDbBnZSAv39o++jIJM9Pn16D4Jns3ciysMbGbWdv01HAXOf 15HQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=pDxJy+QlW0ndrJ/eoWJ1qqIyij6Fb8zhVn3E4NzyXyc=; b=bCuei/0MQTZr0yqWLTAwQN2Z/3UnmWMmV/1QwVX+RZJ325jKTkkvNXVHezpxMVW1oy n5cyX0MZZ58Lp/RpmKVjtndmFCoSu93izKLojjwhEk+3rcdI5wInmjAkEIIzSx+aHd8s 4Fu/KCcH8Lw8vi1PED4iRwmXKzGoCyb0Ps6J5wGWV8QVYIpCMiXNpnNR9Bna+9cO+ywp ijQJrxDgIsASRXWjiyzqzcf5m/K5Iul1M+etN+ceIBQVRhHZYlWgMK87xtK+kfEVLrZM ROzljHhnnudX/KYGzSZzewXCRYoqGig+mOohdM8VS4zBTBcDlvtRM9N4QtutAOkLm01S m+Dw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f16si4674281eds.523.2020.11.27.00.33.16; Fri, 27 Nov 2020 00:33:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S2391239AbgKZPTe (ORCPT + 99 others); Thu, 26 Nov 2020 10:19:34 -0500 Received: from foss.arm.com ([217.140.110.172]:36320 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389756AbgKZPTd (ORCPT ); Thu, 26 Nov 2020 10:19:33 -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 DFC2631B; Thu, 26 Nov 2020 07:19:32 -0800 (PST) Received: from [10.57.59.159] (unknown [10.57.59.159]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 47AEB3F71F; Thu, 26 Nov 2020 07:19:30 -0800 (PST) Subject: Re: [PATCH] iommu: Improve the performance for direct_mapping To: Yong Wu , Joerg Roedel , Will Deacon Cc: Matthias Brugger , Krzysztof Kozlowski , Tomasz Figa , linux-mediatek@lists.infradead.org, srv_heupstream@mediatek.com, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, iommu@lists.linux-foundation.org, youlin.pei@mediatek.com, Nicolas Boichat , anan.sun@mediatek.com, chao.hao@mediatek.com References: <20201120090628.6566-1-yong.wu@mediatek.com> From: Robin Murphy Message-ID: Date: Thu, 26 Nov 2020 15:19:28 +0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Thunderbird/78.5.0 MIME-Version: 1.0 In-Reply-To: <20201120090628.6566-1-yong.wu@mediatek.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2020-11-20 09:06, Yong Wu wrote: > Currently direct_mapping always use the smallest pgsize which is SZ_4K > normally to mapping. This is unnecessary. we could gather the size, and > call iommu_map then, iommu_map could decide how to map better with the > just right pgsize. > > From the original comment, we should take care overlap, otherwise, > iommu_map may return -EEXIST. In this overlap case, we should map the > previous region before overlap firstly. then map the left part. > > Each a iommu device will call this direct_mapping when its iommu > initialize, This patch is effective to improve the boot/initialization > time especially while it only needs level 1 mapping. > > Signed-off-by: Anan Sun > Signed-off-by: Yong Wu > --- > drivers/iommu/iommu.c | 20 ++++++++++++++++++-- > 1 file changed, 18 insertions(+), 2 deletions(-) > > diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c > index df87c8e825f7..854a8fcb928d 100644 > --- a/drivers/iommu/iommu.c > +++ b/drivers/iommu/iommu.c > @@ -737,6 +737,7 @@ static int iommu_create_device_direct_mappings(struct iommu_group *group, > /* We need to consider overlapping regions for different devices */ > list_for_each_entry(entry, &mappings, list) { > dma_addr_t start, end, addr; > + size_t unmapped_sz = 0; > > if (domain->ops->apply_resv_region) > domain->ops->apply_resv_region(dev, domain, entry); > @@ -752,10 +753,25 @@ static int iommu_create_device_direct_mappings(struct iommu_group *group, > phys_addr_t phys_addr; > > phys_addr = iommu_iova_to_phys(domain, addr); > - if (phys_addr) > + if (phys_addr == 0) { > + unmapped_sz += pg_size; /* Gather the size. */ > continue; > + } I guess the reason we need to validate every page is because they may already have been legitimately mapped if someone else's reserved region overlaps - is it worth explicitly validating that, i.e. bail out if something's gone wrong enough that phys_addr != addr? Other than the naming issue (I agree that map_size is a far, far better choice), I don't have any strong opinions about the rest of the implementation - I've written enough variations of this pattern to know that there's just no "nice" way to do it in C; all you can do is shuffle the clunkiness around :) Robin. > > - ret = iommu_map(domain, addr, addr, pg_size, entry->prot); > + if (unmapped_sz) { > + /* Map the region before the overlap. */ > + ret = iommu_map(domain, start, start, > + unmapped_sz, entry->prot); > + if (ret) > + goto out; > + start += unmapped_sz; > + unmapped_sz = 0; > + } > + start += pg_size; > + } > + if (unmapped_sz) { > + ret = iommu_map(domain, start, start, unmapped_sz, > + entry->prot); > if (ret) > goto out; > } >