Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp907525pxk; Thu, 10 Sep 2020 01:34:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJynKsuAWeVwtl0VjoloqUdFBhLyGpKqw4p/4xwfsshmVq/RdIPOODjbXd9lKezWKYx/a4I+ X-Received: by 2002:a05:6402:7d2:: with SMTP id u18mr8300449edy.69.1599726893280; Thu, 10 Sep 2020 01:34:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599726893; cv=none; d=google.com; s=arc-20160816; b=Lwme9T40lhFtt8FLUUG1QJ+JuEJ7JpX+x2ipAS1DrTFq1DIxg984buJlAK9/7O0s/1 cwmXKSLKR5A50i8TaQvXIp+Ff60/4j7jyEzUFDoT736OMQSr0c/x/M9I9/I/EFDhCwqx v8E+kh0XUWDDcFW8MFtZ/v8PRwFCHqzbF/Y7m8VMA3gNGPlXCd3LWAg6rgy9Uhiqy/C/ SASiOj3gp1+HasiAZsQBNd/PXcfwgftNUJPe9mELGJ4Xd+sA3xw+h7vYcI2jUFG6mAYB aEcQDsnsBvpLL6TmmrOVbyP6W8+GUYkdrLr+Ijtv+9pAKhgs2SZ934ssGDZdwqVe0/Jg kLaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=jV15et7Yw8A7Imw4OszYwam1pGezadF7ay4AoPCRTlw=; b=Q9a1IiaTNZFKksOne7q9mbfqeCUTssoi84DNBKoFhuh21X4k9wgxvCnZCwxGzJCHLb rGG3nXQmdrKixozUO8z5gBSvgu0q46P+vRclOStZRS8sNP6Y9jhUPy5vwPHwQdiZVBjl KuBO5IaD90XBoFSE61G+524Mddti2zTj38AUp9oTEHx/LmHRUgBaLgX2sZd8ivcvGR8u 8ZkJ5LVmyaL09wHGSHX2A7BY1Hh1LOKof+Ibg5yurFkrOxIqTD0QXukLknMvsk4IlTO4 o5PRY+e0uQ+UQ9EBtOmpAAMrkxck2tGA57NW/emgbihKt5NEThfs9GQRlmLJ594VIVAy z6Aw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@broadcom.com header.s=google header.b=YFX3C21Z; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=broadcom.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ga23si3153107ejb.691.2020.09.10.01.34.29; Thu, 10 Sep 2020 01:34:53 -0700 (PDT) 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=@broadcom.com header.s=google header.b=YFX3C21Z; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=broadcom.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730271AbgIJIa3 (ORCPT + 99 others); Thu, 10 Sep 2020 04:30:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55758 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730224AbgIJI0V (ORCPT ); Thu, 10 Sep 2020 04:26:21 -0400 Received: from mail-wm1-x341.google.com (mail-wm1-x341.google.com [IPv6:2a00:1450:4864:20::341]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B4C8DC061573 for ; Thu, 10 Sep 2020 01:26:20 -0700 (PDT) Received: by mail-wm1-x341.google.com with SMTP id c19so1343188wmd.1 for ; Thu, 10 Sep 2020 01:26:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=jV15et7Yw8A7Imw4OszYwam1pGezadF7ay4AoPCRTlw=; b=YFX3C21ZwVO7s35wZGtviE1XqbE7aCLtO3wFFDiZBQLPyeEj7XL687V0Ka17r+pHow trvRePYr75Vgj+evG/sbfJ5EEYunaBaoAbSvIka3o38lU7/FKTwwJQ7jj74iOjPtp4iL YrUv5JVWK1FJoTvd7t+cUgJAdvv6LvUo9pvjg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=jV15et7Yw8A7Imw4OszYwam1pGezadF7ay4AoPCRTlw=; b=p+AXdCPsj9nc+VfpX01l/yo2GOo2UQX9oPlWbagxs09snVUF/8aqAsFsrsrm0DRF3u 1cWbZ8UzItOrsNfgfBWR/+/mM+UlNk1J/Jg6tITInEwt+m3knvtX20G6R7NkyWbq9mtK EmXtAzStfk/rKCuboZonxszS8tkc/FQhIL+aVMMpt/O8fOBFu6YJkSof8vjDWTWRUSGm A9qWGkquFkWWc7cgzv3ymBv2do28ojcZ6gyF8Lu3J4I+ccZqIVG2gsQJdYbJHs0BYB+I FE2zsszbqruOVLUsabW7oYihAt5XlI2XHezV2rbVlyQWHYhpu1ni/YQcYT/OGrSn7JVq 43lQ== X-Gm-Message-State: AOAM533OOKjLXnzG4Xun8EOf31LtvzeaLyY6rhuK01bWb85ox4rQxGtM LKnKAhO5yRt6ggTRYTV/lNw3slYQ7m0JwZmrReMbj2w3hF2IMg== X-Received: by 2002:a7b:c384:: with SMTP id s4mr7073300wmj.138.1599726379101; Thu, 10 Sep 2020 01:26:19 -0700 (PDT) MIME-Version: 1.0 References: <20200909053234.17027-1-srinath.mannam@broadcom.com> <1996b772-774c-3475-05cc-77ae87176c3f@arm.com> In-Reply-To: <1996b772-774c-3475-05cc-77ae87176c3f@arm.com> From: Srinath Mannam Date: Thu, 10 Sep 2020 13:56:07 +0530 Message-ID: Subject: Re: [PATCH] iommu/dma: Fix IOVA reserve dma ranges To: Robin Murphy Cc: Joerg Roedel , Lorenzo Pieralisi , Bjorn Helgaas , poza@codeaurora.org, BCM Kernel Feedback , iommu@lists.linux-foundation.org, Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Sep 9, 2020 at 5:35 PM Robin Murphy wrote: > Hi Robin, Thanks for review > On 2020-09-09 06:32, Srinath Mannam wrote: > > Fix IOVA reserve failure for memory regions listed in dma-ranges in the > > following cases. > > > > - start address of memory region is 0x0. > > That's fair enough, and in fact generalises to the case of zero-sized > gaps between regions, which is indeed an oversight. Yes this is the main reason for the requirement of this fix. > > > - end address of a memory region is equal to start address of next memory > > region. > > This part doesn't make much sense, however - if the regions described in > bridge->dma_ranges overlap, that's a bug in whoever created a malformed > list to begin with. Possibly it's just poor wording, and you're using > "memory regions" to refer to any or all of the dma_ranges, the reserved > IOVA ranges, and what "start" and "end" in this function represent which > isn't quite either of those. You are right, this case is very unlikely that nobody lists regions with zero gap, in such a case they will combine both the regions. Reason for highlighting this point is, the same fix will handle this case also. Here I used memory regions to refer entries of dma-ranges(allowed IOVA addresses range) not reserved IOVA ranges. start and end variables in this function refers to start and end addresses of reserved IOVA ranges which are derived from dma ranges resources start and end values. > > > Fixes: aadad097cd46f ("iommu/dma: Reserve IOVA for PCIe inaccessible DMA address") > > Signed-off-by: Srinath Mannam > > --- > > drivers/iommu/dma-iommu.c | 15 +++++++++++---- > > 1 file changed, 11 insertions(+), 4 deletions(-) > > > > diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c > > index 5141d49a046b..0a3f67a4f9ae 100644 > > --- a/drivers/iommu/dma-iommu.c > > +++ b/drivers/iommu/dma-iommu.c > > @@ -213,14 +213,21 @@ static int iova_reserve_pci_windows(struct pci_dev *dev, > > resource_list_for_each_entry(window, &bridge->dma_ranges) { > > end = window->res->start - window->offset; > > resv_iova: > > + if (end < start) { > > + /* dma_ranges list should be sorted */ > > + dev_err(&dev->dev, "Failed to reserve IOVA\n"); > > + return -EINVAL; > > + } > > + /* > > + * Skip the cases when start address of first memory region is > > + * 0x0 and end address of one memory region and start address > > + * of next memory region are equal. Reserve IOVA for rest of > > + * addresses fall in between given memory ranges. > > + */ > > if (end > start) { > > lo = iova_pfn(iovad, start); > > hi = iova_pfn(iovad, end); > > reserve_iova(iovad, lo, hi); > > - } else { > > Surely this only needs to be a one-liner? Yes I agree with you this one line is sufficient. > > - } else { > + } else if (end < start) { > > (or possibly "end != start"; I can't quite decide which expresses the > semantic intent better) I think "end < start" is better choice because it tells list is not sorted and "!=" contradicts previous condition "end > start". > > The rest just looks like unnecessary churn - I don't think it needs > commenting that a sorted list may simply not have gaps between entries, > and as above I think the wording of that comment is actively misleading. I agree with you, these lines were added to explain the issue and fix with more details. I will send a new patch with a single line change as you said. " } else if (end < start) {" Thanks & Regards, Srinath. > > Robin. > > > - /* dma_ranges list should be sorted */ > > - dev_err(&dev->dev, "Failed to reserve IOVA\n"); > > - return -EINVAL; > > } > > > > start = window->res->end - window->offset + 1; > >