Received: by 2002:ac0:950c:0:0:0:0:0 with SMTP id f12csp2167430imc; Tue, 12 Mar 2019 08:18:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqzfhsM8QdWbT8kZZxYmAGKL18RQk+8PH+zfleHwFTrnssZlaPJUvUaZI/Ryayrk9rIBEvcE X-Received: by 2002:a65:5281:: with SMTP id y1mr36270294pgp.59.1552403938434; Tue, 12 Mar 2019 08:18:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552403938; cv=none; d=google.com; s=arc-20160816; b=j/m7oS43jq8+18S8a9spWw4xbJnVEFW6YNS9NuPjteFgtJOxlubfWan4dDtFvwLvuY 7IOHzvP9AsY6tes9s0iG7ReAzHOneTpIw7/+8Nba/LPzutsaiNnNUJFpap2A3IKJIUzi c4LJRzQyQMVDN0ZgNUevGIpgMV5ostxjpb19h3vbryJgEKg+8wAo5rLP2Ky3c6eHdIVe 8ckJd/VxWEaNtbrMZWPhQ+xASsEcTabP50MNkrNQejeXxYrMGrKEIak+qJbs7zhWGVuh hy5u3rInnbfnnhiFTrKNssGfYuvlV1XwmMRCGMG6VxEEife0iYWuMb5HAZ1LvM2WxxT5 vgFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id; bh=TeKsl+a/1YjtE8+EPi+JgP5FPkD43SfVNORgrdBW2W0=; b=R6oqmHPpeTB/Wj4F4l8SmJwQnkmqKaqaeIJXhsA+YHUoDgSN0p7rynnl41qEXU2tFT lM9Xd9baXU6NClRqu/+79QngKl7pxupTM6abC7EB4X2tbEmxEXAI2H+EtKL4FXMshmL+ 4s5r1heM0qkDtnQYMFMlXQ8ez/AF4L7hVjtP3xXG5faAc1tD+YQOaQtVtZK41F+FarJa MF61L4XVjM1zO6ZRf5rrwtn6N/lWnDfiOmSnM05O44G/mVJ1elcodnLRtyYZ+FSAB1xE Dcya3zFmHzYH58wibTsAP9GXAjcR71YY2KZ+0RoRFfbZfl5Rccec68ndcxA2ubBzSHGW 823g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n22si3099893plp.296.2019.03.12.08.18.41; Tue, 12 Mar 2019 08:18:58 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726561AbfCLPSS (ORCPT + 99 others); Tue, 12 Mar 2019 11:18:18 -0400 Received: from mga05.intel.com ([192.55.52.43]:41020 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726339AbfCLPSR (ORCPT ); Tue, 12 Mar 2019 11:18:17 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 12 Mar 2019 08:18:17 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.58,471,1544515200"; d="scan'208";a="133430143" Received: from ahduyck-desk1.jf.intel.com ([10.7.198.76]) by orsmga003.jf.intel.com with ESMTP; 12 Mar 2019 08:18:16 -0700 Message-ID: <67c062b183fbd5651a9aa990e677d31673ff7580.camel@linux.intel.com> Subject: Re: [PATCH] iommu/amd: fix sg->dma_address for sg->offset bigger than PAGE_SIZE From: Alexander Duyck To: Stanislaw Gruszka Cc: Rosen Penev , Joerg Roedel , Lorenzo Bianconi , linux-wireless , Samuel Sieb , iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, linux-nvme@lists.infradead.org, jan.viktorin@gmail.com Date: Tue, 12 Mar 2019 08:18:16 -0700 In-Reply-To: <20190312070832.GA2483@redhat.com> References: <20190226112407.GB2989@redhat.com> <20190228090411.GA24938@redhat.com> <20190228104223.GA2749@redhat.com> <20190228121948.GD6072@redhat.com> <20190228134029.GC1594@8bytes.org> <20190304071037.GA2787@redhat.com> <20190311084319.GA3310@redhat.com> <20190311090314.GB3310@redhat.com> <20190312070832.GA2483@redhat.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.30.5 (3.30.5-1.fc29) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 2019-03-12 at 08:08 +0100, Stanislaw Gruszka wrote: > On Mon, Mar 11, 2019 at 08:47:44AM -0700, Alexander Duyck wrote: > > > drivers/iommu/amd_iommu.c | 2 +- > > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > > > diff --git a/drivers/iommu/amd_iommu.c b/drivers/iommu/amd_iommu.c > > > index 6b0760dafb3e..949621f33624 100644 > > > --- a/drivers/iommu/amd_iommu.c > > > +++ b/drivers/iommu/amd_iommu.c > > > @@ -2604,7 +2604,7 @@ static int map_sg(struct device *dev, struct scatterlist *sglist, > > > > > > /* Everything is mapped - write the right values into s->dma_address */ > > > for_each_sg(sglist, s, nelems, i) { > > > - s->dma_address += address + s->offset; > > > + s->dma_address += address + (s->offset & ~PAGE_MASK); > > > s->dma_length = s->length; > > > } > > > > > > > You should add a comment calling out that this is needed because the > > sg_phys(s) call above this is masked with PAGE_MASK. Then this makes > > much more sense. Otherwise I would have assumed you needed either the > > full offset or none. > > Would something like this > > /* > * Everything is mapped - write the right values into s->dma_address. > * Take into account s->offset can be bigger than page size and sg_phys(s) > * address has to be aligned to page granularity. > */ > > be appropriate ? > > Stanislaw > No, that isn't a good description. If you take a look at the code a few lines up you find: phys_addr = (sg_phys(s) & PAGE_MASK) + (j << PAGE_SHIFT); Now if I am not mistaken the whole reason why you are having to make the change here is because the application of PAGE_MASK in this line. Basically what sg_phys() will do is take the address of the page, convert it to a physical address and add the offset. However what the mask is doing is limiting how much of that offset can be added. As a result you have to add the remainder that was masked out. So maybe a better comment would be something like: /* * Add in the remaining piece of the scatter-gather offset that was * masked out when we were determining the physical address via * (sg_phys(s) & PAGE_MASK) earlier. */