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.2 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,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 6B596C433E0 for ; Thu, 28 Jan 2021 18:20:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 34E6D64DE5 for ; Thu, 28 Jan 2021 18:20:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231594AbhA1SUA (ORCPT ); Thu, 28 Jan 2021 13:20:00 -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.