Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1204859pxb; Thu, 28 Jan 2021 10:21:16 -0800 (PST) X-Google-Smtp-Source: ABdhPJyyn7nUkU5yGMHkV/xllY7h1nqcxf076TWzRYJxyuKz50kcvWhkWTSKpfaGVKwAQEpe4Wzb X-Received: by 2002:aa7:c84b:: with SMTP id g11mr929565edt.169.1611858076085; Thu, 28 Jan 2021 10:21:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611858076; cv=none; d=google.com; s=arc-20160816; b=AWiCnBg+kYqpTPOMl8/9XU3FsL7LPPR/Jg3HJk47S7qebnvW5WP6ozdw6hA7iji28T 4OsO6qHdkiAomGKnbiHlkYkBJc4PFp0FQCvn0GkXwxaeg5kF57Q2VGFc1Sa7QaYxoNPR JqpAzQLaLFoIJKjahv8tG7WMiJrWC7UdKN4hy+zPLZQUkWgFq6ASaITi8GpqnsA3GLnY qce2n2HljZw2NkG5NuS3SWme1OwmLz37CfhfHgdHVxqsk8HIRKmpTdOkT8kcZERUI02Y zGzIx6HayICNRHqp8k3vhU1WRh4AZ95ydv0AeZXbCKtzksPmNnyNAjPosql7yq6jMGqk MzYQ== 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; bh=uklVV/FbU0eW5KMDXXEaTeVlxp68oiBHZuiT+AGJ2wI=; b=d9tXK+OKVj8z9I+o6VgAF+tPvRa1JD9DiKt5ZkFPL/+xzB9CMDIlqUlA2lJfsTwCA1 0eNYCnVUNs0ScOr/5b000Z1HkXSInLj+tfVe4RynZbF1sWh4RgvbQg9SM990NM5g/opU WlcW88SPRpidDkJ/BEJran5P/QzsgRMyYVwqma5xlgy4Ebq+5AOJsqpxhDy7FHoSfEjU 4o2GAfmLFgTGFgqZzWsyZ1PVD/kJNTvJBiFR0kw1DB4F5/Gu9db3HtFUwyja27UW5IYm Ev/Tv+Rgx2FoGmoXnQhdH1sCsruEuwLXQh7hmtEE9w3gLpiuX1uGpmQZ1V+wlc7y5LP6 DPsA== 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 re20si3048471ejb.277.2021.01.28.10.20.51; Thu, 28 Jan 2021 10:21:16 -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 S231534AbhA1STu (ORCPT + 99 others); Thu, 28 Jan 2021 13:19:50 -0500 Received: from verein.lst.de ([213.95.11.211]:58531 "EHLO verein.lst.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231354AbhA1SRM (ORCPT ); Thu, 28 Jan 2021 13:17:12 -0500 Received: by verein.lst.de (Postfix, from userid 2407) id 45B0F68B05; Thu, 28 Jan 2021 19:16:28 +0100 (CET) Date: Thu, 28 Jan 2021 19:16:27 +0100 From: Christoph Hellwig To: Jianxiong Gao Cc: erdemaktas@google.com, marcorr@google.com, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, gregkh@linuxfoundation.org, saravanak@google.com, heikki.krogerus@linux.intel.com, rafael.j.wysocki@intel.com, andriy.shevchenko@linux.intel.com, dan.j.williams@intel.com, bgolaszewski@baylibre.com, jroedel@suse.de, iommu@lists.linux-foundation.org, konrad.wilk@oracle.com, kbusch@kernel.org, axboe@fb.com, sagi@grimberg.me, linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 2/3] Add swiotlb offset preserving mapping when dma_dma_parameters->page_offset_mask is non zero. Message-ID: <20210128181627.GB11790@lst.de> References: <20210128003829.1892018-1-jxgao@google.com> <20210128003829.1892018-3-jxgao@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210128003829.1892018-3-jxgao@google.com> User-Agent: Mutt/1.5.17 (2007-11-01) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jan 27, 2021 at 04:38:28PM -0800, Jianxiong Gao wrote: > For devices that need to preserve address offset on mapping through > swiotlb, this patch adds offset preserving based on page_offset_mask > and keeps the offset if the mask is non zero. This is needed for > device drivers like NVMe. > > Signed-off-by: Jianxiong Gao > --- > kernel/dma/swiotlb.c | 25 ++++++++++++++++++++++--- > 1 file changed, 22 insertions(+), 3 deletions(-) > > diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c > index 7c42df6e6100..4cab35f2c9bc 100644 > --- a/kernel/dma/swiotlb.c > +++ b/kernel/dma/swiotlb.c > @@ -468,7 +468,7 @@ phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, phys_addr_t orig_addr, > dma_addr_t tbl_dma_addr = phys_to_dma_unencrypted(hwdev, io_tlb_start); > unsigned long flags; > phys_addr_t tlb_addr; > - unsigned int nslots, stride, index, wrap; > + unsigned int nslots, stride, index, wrap, page_offset_mask, page_offset; > int i; > unsigned long mask; > unsigned long offset_slots; > @@ -500,12 +500,16 @@ phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, phys_addr_t orig_addr, > ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT > : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT); > > + page_offset_mask = dma_get_page_offset_mask(hwdev); > + page_offset = orig_addr & page_offset_mask; > + alloc_size += page_offset; > + > /* > * For mappings greater than or equal to a page, we limit the stride > * (and hence alignment) to a page size. > */ > nslots = ALIGN(alloc_size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT; > - if (alloc_size >= PAGE_SIZE) > + if ((alloc_size >= PAGE_SIZE) || (page_offset_mask > (1 << IO_TLB_SHIFT))) > stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT)); > else > stride = 1; > @@ -583,6 +587,11 @@ phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, phys_addr_t orig_addr, > */ > for (i = 0; i < nslots; i++) > io_tlb_orig_addr[index+i] = orig_addr + (i << IO_TLB_SHIFT); > + /* > + * When keeping the offset of the original data, we need to advance > + * the tlb_addr by the offset of orig_addr. > + */ > + tlb_addr += page_offset; > if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) && > (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)) > swiotlb_bounce(orig_addr, tlb_addr, mapping_size, DMA_TO_DEVICE); > @@ -598,7 +607,9 @@ void swiotlb_tbl_unmap_single(struct device *hwdev, phys_addr_t tlb_addr, > enum dma_data_direction dir, unsigned long attrs) > { > unsigned long flags; > - int i, count, nslots = ALIGN(alloc_size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT; > + unsigned int num_page_offset_slabs, page_offset_mask = dma_get_page_offset_mask(hwdev); Yikes, please avoid these crazy long lines. > + num_page_offset_slabs = (tlb_addr & page_offset_mask) / (1 << IO_TLB_SHIFT); also a double whitespace here.