Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp835576pxu; Mon, 23 Nov 2020 05:30:54 -0800 (PST) X-Google-Smtp-Source: ABdhPJyYd3oTQ457oUMU3dwmc6N9wxfX/3PCOWwEnNQiY8F3e6Lh4+QH2AJynT8ylkhGej+fzOJk X-Received: by 2002:a17:906:e96:: with SMTP id p22mr25812971ejf.451.1606138254435; Mon, 23 Nov 2020 05:30:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606138254; cv=none; d=google.com; s=arc-20160816; b=sgRnrrwvLbKjAgcNTmrrXLEFpwx9nk9Fb171Q0DjjmN87u0Lp4FJzeil1Z14OHu6jj ZMJ/clCoPqvftE/rOgmR3c+3sk0JTU9Y+DIhesYx7yo5V+Os+YwEwEe2cDmRJbcQ8fTM LHcR9RYxe6+c6l2Th9PG+YTsWSxCsEx1l5XM4IZEqobJT6cVB4Rk3P+0iYnlVN5ppr44 8uOp+xM/T85NNtR5R15MywA3T7/nu/n2zw7lyP5Iy/Q6srC9WItx12VokoUb0h6dNH8P +lsNFij57Vrje1VVWM9qh7w/RuBUojQe9fjm1pCivn1RHFqxLHycU/WodKsVRFDE2IRb r70Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=j7xL3Hk4rTZnTrjxUgI0I5/P/bfyXk+4V27Aaeh/0ms=; b=Qrx+JOrYTIEpk5j5Ku/Ggl2sK86gKM7qI/G3JDFifnYGAtBwsm+fzLkviPfsZwey9p 4iIQY6fCP4iNlpgMgRzrezDF8/JaHwwGqD2itgXHiRVnx3Ch0YQN4Vxy/zllWmiREKge PrgKS/ktd/S5B9gaLqMQKpWIQbRzPYuwbOMs+CkYTlM/jdKKjgzeAltapx1YGNHM65Cr LfI8Sw8Sy9lD03EAgGwr29js1w3+CnPfZA9UN+orMHwKim5gSz6Or1xCkLAjPPNUlnND snRB1PxQFYrpTXQvxziJQ3miQqP45AoVhV5M2oIr8aY6UnrG6jD5U9Zi1/DzkkA2rgtB /G+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=wRhhY5oD; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e17si6787865edq.469.2020.11.23.05.30.31; Mon, 23 Nov 2020 05:30:54 -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; dkim=pass header.i=@kernel.org header.s=default header.b=wRhhY5oD; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389131AbgKWNZj (ORCPT + 99 others); Mon, 23 Nov 2020 08:25:39 -0500 Received: from mail.kernel.org ([198.145.29.99]:44344 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730458AbgKWMdF (ORCPT ); Mon, 23 Nov 2020 07:33:05 -0500 Received: from willie-the-truck (236.31.169.217.in-addr.arpa [217.169.31.236]) (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 769942076E; Mon, 23 Nov 2020 12:33:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1606134785; bh=0QSv09rGW0WuAiAunEEQ7oj/27dyFGEZgt+e6cLrLtI=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=wRhhY5oDR9O6SI4elKUy6j4JiYwRnLeUIusACXuaJrzeN9ZZiguwygT+Vh+BIK5bn 1QQ4VhGF8+m8rzx/29h0VjrBkQpBujH/X1jgFMI76jBaK+oDdnmWyGqrWuW6MUTBOc 5iEqZjkGYXfposO9rTtzNODSeMyzzsozvxw40LXc= Date: Mon, 23 Nov 2020 12:32:58 +0000 From: Will Deacon To: Yong Wu Cc: Joerg Roedel , Robin Murphy , 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 Subject: Re: [PATCH] iommu: Improve the performance for direct_mapping Message-ID: <20201123123258.GC10233@willie-the-truck> References: <20201120090628.6566-1-yong.wu@mediatek.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201120090628.6566-1-yong.wu@mediatek.com> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Nov 20, 2020 at 05:06:28PM +0800, 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; I think "unmapped" is the wrong word here, as this variable actually represents the amount we want to map! I suggest "map_size" instead. > 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; > + } > > - 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; I think it's a bit confusing to update start like this. Can we call iommu_map(domain, addr - map_size, addr - map_size, map_size, entry->prot) instead? > + unmapped_sz = 0; > + } > + start += pg_size; > + } > + if (unmapped_sz) { > + ret = iommu_map(domain, start, start, unmapped_sz, > + entry->prot); Can you avoid this hunk by changing your loop check to something like: if (!phys_addr) { map_size += pg_size; if (addr + pg_size < end) continue; } Will