Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp2343010pxu; Mon, 7 Dec 2020 04:19:37 -0800 (PST) X-Google-Smtp-Source: ABdhPJyrJPsYqnxdplOIEcC/giGfhYoXWvZZc3vGTBF/bYl8Td1Db5E3mbmrkwSKtpag1T6wgNjs X-Received: by 2002:a17:907:447d:: with SMTP id oo21mr18894072ejb.367.1607343576786; Mon, 07 Dec 2020 04:19:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607343576; cv=none; d=google.com; s=arc-20160816; b=l3BxtK6FE0Rgg7l+XLx1OX3mjvWwBBofizARuQiWKkmLp2xnWXMlu+eG7e+B02G3sW 8fHnTDtwJQcVfxcghAjID5IIjT9o0s/HS7vpcf5Xd5OGaiT5rlVvmoBrZganjisdb8XF uiT0A/eVjQvZcAELs0R1UJVWAmkpTVCicGnQ6eeGIdk56179F8CUVahUboSc6uCTl6/O iqgC49+d4Oaw4vgYSvk+VEopbuVPRRExwglaMjCdleaTZ3QLVnW7aEFs21SwVBxZ3e34 HJuu51nt1RwUVu17Xo6Gc8Rp8uEZuKd0qmxWO6oMzRP5Zoyx+gZg9pXi4fILttatBiaG WTCQ== 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 :mime-version:user-agent:date:message-id:from:cc:references:to :subject; bh=Vd7PJawTF4eHf8r47kEdqxK3dXZbbgzHg1+PhVxCrEo=; b=k0kh5waMVadyobasxTea5wwgHRpq0MJvZIm8Z7vuvkrNwkNR4VvVoHp08YOYBPI9d+ KFiHr5R7kG+WMvdWoEs9AhPTAjYAVBTm8xDPl7VJRqw1VZFzY6fC2eB5u+6cNiFUcQOp KAjoFxxWiyuBLG7rMOKBvvMwgF5vsBrLMYqtW9aGmU/f4vhg78pn7TYRUc9wIc7XmDhE +YmqqTm10Adcq3v/IuJxfBSXHqMgvRYgXNzm59y25zf3rqzwT04z9vTvjmPCCVZ+1chz BWJzl06ZhV6NnBiwshCmBq6+ytWQoLtAJ5wsAqNTkAWelkc2/3Or7n0KAsSCUXOsqEfJ xtUg== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x25si8064690edi.388.2020.12.07.04.19.13; Mon, 07 Dec 2020 04:19:36 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727457AbgLGMQO (ORCPT + 99 others); Mon, 7 Dec 2020 07:16:14 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:9027 "EHLO szxga05-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727099AbgLGMQO (ORCPT ); Mon, 7 Dec 2020 07:16:14 -0500 Received: from DGGEMS412-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4CqMh45cdtzhntL; Mon, 7 Dec 2020 20:15:00 +0800 (CST) Received: from [10.174.187.37] (10.174.187.37) by DGGEMS412-HUB.china.huawei.com (10.3.19.212) with Microsoft SMTP Server id 14.3.487.0; Mon, 7 Dec 2020 20:15:22 +0800 Subject: Re: [PATCH] iommu: Up front sanity check in the arm_lpae_map To: Will Deacon , Robin Murphy References: <20201205082957.12544-1-zhukeqian1@huawei.com> <20201207120527.GA4474@willie-the-truck> CC: , , , Marc Zyngier , "Joerg Roedel" , Catalin Marinas , "James Morse" , Suzuki K Poulose , "Sean Christopherson" , Julien Thierry , Mark Brown , "Thomas Gleixner" , Andrew Morton , Alexios Zavras , , From: zhukeqian Message-ID: <2b0ec25b-0fa4-65ca-7c1b-109ce766197f@huawei.com> Date: Mon, 7 Dec 2020 20:15:21 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.7.1 MIME-Version: 1.0 In-Reply-To: <20201207120527.GA4474@willie-the-truck> Content-Type: text/plain; charset="windows-1252" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.187.37] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, On 2020/12/7 20:05, Will Deacon wrote: > On Mon, Dec 07, 2020 at 12:01:09PM +0000, Robin Murphy wrote: >> On 2020-12-05 08:29, Keqian Zhu wrote: >>> ... then we have more chance to detect wrong code logic. >> >> I don't follow that justification - it's still the same check with the same >> outcome, so how does moving it have any effect on the chance to detect >> errors? >> >> AFAICS the only difference it would make is to make some errors *less* >> obvious - if a sufficiently broken caller passes an empty prot value >> alongside an invalid size or already-mapped address, this will now quietly >> hide the warnings from the more serious condition(s). >> >> Yes, it will bail out a bit faster in the specific case where the prot value >> is the only thing wrong, but since when do we optimise for fundamentally >> incorrect API usage? > > I thought it was the other way round -- doesn't this patch move the "empty > prot" check later, so we have a chance to check the size and addresses > first? Yes, this is my original idea. For that we treat iommu_prot with no permission as success at early start, defer this early return can expose hidden errors. Thanks, Keqian > > Will > >>> Signed-off-by: Keqian Zhu >>> --- >>> drivers/iommu/io-pgtable-arm.c | 8 ++++---- >>> 1 file changed, 4 insertions(+), 4 deletions(-) >>> >>> diff --git a/drivers/iommu/io-pgtable-arm.c b/drivers/iommu/io-pgtable-arm.c >>> index a7a9bc08dcd1..8ade72adab31 100644 >>> --- a/drivers/iommu/io-pgtable-arm.c >>> +++ b/drivers/iommu/io-pgtable-arm.c >>> @@ -444,10 +444,6 @@ static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova, >>> arm_lpae_iopte prot; >>> long iaext = (s64)iova >> cfg->ias; >>> - /* If no access, then nothing to do */ >>> - if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE))) >>> - return 0; >>> - >>> if (WARN_ON(!size || (size & cfg->pgsize_bitmap) != size)) >>> return -EINVAL; >>> @@ -456,6 +452,10 @@ static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova, >>> if (WARN_ON(iaext || paddr >> cfg->oas)) >>> return -ERANGE; >>> + /* If no access, then nothing to do */ >>> + if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE))) >>> + return 0; >>> + >>> prot = arm_lpae_prot_to_pte(data, iommu_prot); >>> ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep, gfp); >>> /* >>> > . >