From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5DB44C2B9F4 for ; Thu, 17 Jun 2021 07:19:16 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 27C4461241 for ; Thu, 17 Jun 2021 07:19:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 27C4461241 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.intel.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Content-Type: Content-Transfer-Encoding:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:Date:Message-ID:From: References:To:Subject:Cc:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=J97MqA181X3ZOHtzQyNRDsLouEw/ychpt2rp/C1jFOQ=; b=rPGMV4B9tMYJ3KWu2+a0g5kmYL mQ6xbeiMkRgBz8IAZAZQjVhwtUKr237Hlzyl833/4IPDFuxm4is6MRcBLSWDmHVyYsGMW7AHZUJcK VU3k4l2mUMwQU7BLePkdkesvxMID4Gno5ocyImJr4kwEWpwxFPp0DwhMoIJxdIrpztQZKgTSidFfo wq2AWB3XDCjxv/xlxsx1QT+qlGZZX3ptSotq07bXRQ7Tp4xbqU1B+ucD3XYiOECh8NgT4fXL+BcpI WlP2xPATGkDirjxBFpT5tiUYUGy4i5oN7Lu9HUQMKYwn+hNjw24R8HeW43f4BSG5l4qNaeHdQRmso IolqA/MA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1ltmHX-009EXX-Bu; Thu, 17 Jun 2021 07:17:43 +0000 Received: from mga06.intel.com ([134.134.136.31]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ltmHT-009EWb-Kb for linux-arm-kernel@lists.infradead.org; Thu, 17 Jun 2021 07:17:41 +0000 IronPort-SDR: IA8IHly0UdjLgvGFRK1nhMzwVTPuM+sEh+Yvy2OqkBzHERZOeKUJ+Xt6LUUmN5wvrJvwg3eHrj gcn3nzD7nYQg== X-IronPort-AV: E=McAfee;i="6200,9189,10017"; a="267465152" X-IronPort-AV: E=Sophos;i="5.83,278,1616482800"; d="scan'208";a="267465152" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2021 00:17:36 -0700 IronPort-SDR: 7mIaHJqyAcgjcjKemwRozT1mCbDOnCiIhOiokO+K3rim8RJj1C3utmbA4rTiduWFf1HbU9gXXn D9ohL96aueZg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.83,278,1616482800"; d="scan'208";a="472325834" Received: from allen-box.sh.intel.com (HELO [10.239.159.118]) ([10.239.159.118]) by fmsmga004.fm.intel.com with ESMTP; 17 Jun 2021 00:17:32 -0700 Cc: baolu.lu@linux.intel.com, joro@8bytes.org, isaacm@codeaurora.org, pratikp@codeaurora.org, iommu@lists.linux-foundation.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, djakov@kernel.org Subject: Re: [PATCH v7 05/15] iommu: Use bitmap to calculate page size in iommu_pgsize() To: Georgi Djakov , will@kernel.org, robin.murphy@arm.com References: <1623850736-389584-1-git-send-email-quic_c_gdjako@quicinc.com> <1623850736-389584-6-git-send-email-quic_c_gdjako@quicinc.com> From: Lu Baolu Message-ID: <4d28e9cf-ac5d-06db-a110-2cdd3f506924@linux.intel.com> Date: Thu, 17 Jun 2021 15:16:05 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: <1623850736-389584-6-git-send-email-quic_c_gdjako@quicinc.com> Content-Language: en-US X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210617_001739_770600_975D44B3 X-CRM114-Status: GOOD ( 22.23 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 6/16/21 9:38 PM, Georgi Djakov wrote: > From: Will Deacon > > Avoid the potential for shifting values by amounts greater than the > width of their type by using a bitmap to compute page size in > iommu_pgsize(). > > Signed-off-by: Will Deacon > Signed-off-by: Isaac J. Manjarres > Signed-off-by: Georgi Djakov > --- > drivers/iommu/iommu.c | 31 ++++++++++++------------------- > 1 file changed, 12 insertions(+), 19 deletions(-) > > diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c > index 5419c4b9f27a..80e471ada358 100644 > --- a/drivers/iommu/iommu.c > +++ b/drivers/iommu/iommu.c > @@ -8,6 +8,7 @@ > > #include > #include > +#include > #include > #include > #include > @@ -2378,30 +2379,22 @@ static size_t iommu_pgsize(struct iommu_domain *domain, > unsigned long addr_merge, size_t size) > { > unsigned int pgsize_idx; > + unsigned long pgsizes; > size_t pgsize; > > - /* Max page size that still fits into 'size' */ > - pgsize_idx = __fls(size); > + /* Page sizes supported by the hardware and small enough for @size */ > + pgsizes = domain->pgsize_bitmap & GENMASK(__fls(size), 0); > > - /* need to consider alignment requirements ? */ > - if (likely(addr_merge)) { > - /* Max page size allowed by address */ > - unsigned int align_pgsize_idx = __ffs(addr_merge); > - pgsize_idx = min(pgsize_idx, align_pgsize_idx); > - } > - > - /* build a mask of acceptable page sizes */ > - pgsize = (1UL << (pgsize_idx + 1)) - 1; > - > - /* throw away page sizes not supported by the hardware */ > - pgsize &= domain->pgsize_bitmap; > + /* Constrain the page sizes further based on the maximum alignment */ > + if (likely(addr_merge)) > + pgsizes &= GENMASK(__ffs(addr_merge), 0); > > - /* make sure we're still sane */ > - BUG_ON(!pgsize); > + /* Make sure we have at least one suitable page size */ > + BUG_ON(!pgsizes); > > - /* pick the biggest page */ > - pgsize_idx = __fls(pgsize); > - pgsize = 1UL << pgsize_idx; > + /* Pick the biggest page size remaining */ > + pgsize_idx = __fls(pgsizes); > + pgsize = BIT(pgsize_idx); > > return pgsize; > } > Reviewed-by: Lu Baolu Best regards, baolu _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel