Message ID | 0334a54332ab75312c9de825548b616439dcc9f5.1679309810.git.petr.tesarik.ext@huawei.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp1201013wrt; Mon, 20 Mar 2023 06:01:24 -0700 (PDT) X-Google-Smtp-Source: AK7set+BVyXJBAJWQrEktqcL+bQvpoSqiYNaPYpjskLhxKSzfsn5owvVDFBR0j0Xhdblths69ZiP X-Received: by 2002:a17:90b:3e8b:b0:23b:3641:cf16 with SMTP id rj11-20020a17090b3e8b00b0023b3641cf16mr14062602pjb.11.1679317283787; Mon, 20 Mar 2023 06:01:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679317283; cv=none; d=google.com; s=arc-20160816; b=gDmviQ9VZj5MTHt/2uTZxn7fyNNqQYpzEduPuEtvfx187plXiBfnBejW0E01yRBikv fgX2A6iqORWBE8OHlrJiOXUHavNiXZf9b+6jxcyRjREkTWcSX4fgh3n5OcHKXyK90pIx qpMm+D/xNvEIzdB3CIlWs3I6vEy6aY2ortCd09jr/Q3fU/vw5WZUnMLxhGIEaVMY0bSx MPwAe3ptk2dKsaoAA0shDqSXU33qJ5LZSbnQJeejpo476GPmSRbGaQlBWykPG02XV7L4 P9M667sXihihgrF/2pXVW2bwcCcRrdZN4YzYehlCK/hvULM9H5nlzbm39s5qJo3kA8u3 PRHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=Z4jM+QJxT6pBKD9BtS3YFUDrhf9F3UhHWz3giDWSRyY=; b=GUZ78bAL0Feh2Fs/0T+0mvOCbCQakVXwKlU4kCA0MoUYyXU12NzSMgboOj4CXmOzk9 qPWng9B7CEjJB6e4zTKNtDHp3fmrVXzHxB1/zTYWg5H48r1aO+40tXqIBY3kTS56pa50 bua/7ZOAEFr3nLN7QPF0I2ge6gSRsPtlV6Q0ui/zcT4iacDz7whqfFfJotMmLiYMn3uV yGZ2rBGONF8LCmELTfHNGx9x49NNtwHlxqbASSOHKpIFWXNv+Hnb4HIumvhaxbSXcZi3 6MZlD+bwf35nfiGpUTheRWAVGVI4XnjtM01+MshzGilNk9u+YDZg+4T7vzrqLxLrYsuA c96Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i4-20020a17090a718400b0022c8ba1bd77si14903912pjk.174.2023.03.20.06.01.03; Mon, 20 Mar 2023 06:01:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230149AbjCTM3z (ORCPT <rfc822;pusanteemu@gmail.com> + 99 others); Mon, 20 Mar 2023 08:29:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57682 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231391AbjCTM3s (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 20 Mar 2023 08:29:48 -0400 Received: from frasgout11.his.huawei.com (frasgout11.his.huawei.com [14.137.139.23]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 473D427D6C; Mon, 20 Mar 2023 05:29:29 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.18.147.228]) by frasgout11.his.huawei.com (SkyGuard) with ESMTP id 4PgDMv19cBz9y4Sr; Mon, 20 Mar 2023 20:20:27 +0800 (CST) Received: from A2101119013HW2.china.huawei.com (unknown [10.48.148.162]) by APP2 (Coremail) with SMTP id GxC2BwBnOF9kURhkkqGyAQ--.46782S5; Mon, 20 Mar 2023 13:28:59 +0100 (CET) From: Petr Tesarik <petrtesarik@huaweicloud.com> To: Jonathan Corbet <corbet@lwn.net>, Christoph Hellwig <hch@lst.de>, Marek Szyprowski <m.szyprowski@samsung.com>, Robin Murphy <robin.murphy@arm.com>, Borislav Petkov <bp@suse.de>, "Paul E. McKenney" <paulmck@kernel.org>, Andrew Morton <akpm@linux-foundation.org>, Randy Dunlap <rdunlap@infradead.org>, Damien Le Moal <damien.lemoal@opensource.wdc.com>, Kim Phillips <kim.phillips@amd.com>, "Steven Rostedt (Google)" <rostedt@goodmis.org>, linux-doc@vger.kernel.org (open list:DOCUMENTATION), linux-kernel@vger.kernel.org (open list), iommu@lists.linux.dev (open list:DMA MAPPING HELPERS) Cc: Roberto Sassu <roberto.sassu@huawei.com>, petr@tesarici.cz Subject: [RFC v1 3/4] swiotlb: Allow dynamic allocation of bounce buffers Date: Mon, 20 Mar 2023 13:28:15 +0100 Message-Id: <0334a54332ab75312c9de825548b616439dcc9f5.1679309810.git.petr.tesarik.ext@huawei.com> X-Mailer: git-send-email 2.21.0.windows.1 In-Reply-To: <cover.1679309810.git.petr.tesarik.ext@huawei.com> References: <cover.1679309810.git.petr.tesarik.ext@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: GxC2BwBnOF9kURhkkqGyAQ--.46782S5 X-Coremail-Antispam: 1UD129KBjvAXoW3ZFykKry5CFyxCr17uFyxAFb_yoW8Jr1fto WxAF13Wr1fKw1UGrZ0kFZrJF47Zayvka1rAr4fZ3yYga9FyryYgw12gF4rJwn3Ww18KFWx Ar9Iga48Xan7Ar48n29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73VFW2AGmfu7bjvjm3 AaLaJ3UjIYCTnIWjp_UUUOe7kC6x804xWl14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK 8VAvwI8IcIk0rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jr Wl82xGYIkIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Cr0_Gr1UM28EF7xvwVC2z280aVAFwI0_Gr0_Cr1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6r4UJVWxJr1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I8CrVC2j2 WlYx0E2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCFs4IE7xkE bVWUJVW8JwACjcxG0xvY0x0EwIxGrwACI402YVCY1x02628vn2kIc2xKxwCY1x0264kExV AvwVAq07x20xyl42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAq x4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r4a6r W5MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF 7I0E14v26F4j6r4UJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI 0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8Jr0_Cr1UYxBIdaVFxhVjvjDU0xZFpf9x 07jeKZXUUUUU= X-CM-SenderInfo: hshw23xhvd2x3n6k3tpzhluzxrxghudrp/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760891799749400514?= X-GMAIL-MSGID: =?utf-8?q?1760891799749400514?= |
Series |
Allow dynamic allocation of software IO TLB bounce buffers
|
|
Commit Message
Petr Tesarik
March 20, 2023, 12:28 p.m. UTC
From: Petr Tesarik <petr.tesarik.ext@huawei.com> The software IO TLB was designed with the assumption that it is not used much, especially on 64-bit systems, so a small fixed memory area (currently 64 MiB) is sufficient to handle the few cases which still require a bounce buffer. However, these cases are not so rare in some circumstances. First, if SEV is active, all DMA must be done through shared unencrypted pages, and SWIOTLB is used to make this happen without changing device drivers. The software IO TLB size is increased to 6% of total memory in sev_setup_arch(), but that is more of an approximation. The actual requirements may vary depending on which drivers are used and the amount of I/O. Second, on the Raspberry Pi 4, swiotlb is used by dma-buf for pages moved from the rendering GPU (v3d driver), which can access all memory, to the display output (vc4 driver), which is connected to a bus with an address limit of 1 GiB and no IOMMU. These buffers can be large (several megabytes) and cannot be handled by SWIOTLB, because they exceed maximum segment size of 256 KiB. Such mapping failures can be easily reproduced on a Raspberry Pi4: Starting GNOME remote desktop results in a flood of kernel messages like these: [ 387.937625] vc4-drm gpu: swiotlb buffer is full (sz: 524288 bytes), total 32768 (slots), used 3136 (slots) [ 387.960381] vc4-drm gpu: swiotlb buffer is full (sz: 815104 bytes), total 32768 (slots), used 2 (slots) This second example cannot be even solved without increasing the segment size (and the complexity of {map,unmap}_single size). At that point, it's better to allocate bounce buffers dynamically with dma_direct_alloc_pages(). One caveat is that the DMA API often takes only the address of a buffer, and the implementation (direct or IOMMU) checks whether it belongs to the software IO TLB. This is easy if the IO TLB is a single chunk of physically contiguous memory, but not if some buffers are allocated dynamically. Testing on a Raspberry Pi 4 shows that there can be 1k+ such buffers. This requires something better than a linked list. I'm using a maple tree to track dynamically allocated buffers. This data structure was invented for a similar use case, but there are some challenges: 1. The value is limited to ULONG_MAX, which is too little both for physical addresses (e.g. x86 PAE or 32-bit ARM LPAE) and DMA addresses (e.g. Xen guests on 32-bit ARM). 2. Since buffers are currently allocated with page granularity, a PFN can be used instead. However, some values are reserved by the maple tree implementation. Liam suggests to use xa_mk_value() in that case, but that reduces the usable range by half. Luckily, 31 bits are still enough to hold a PFN on all 32-bit platforms. 3. Software IO TLB is used from interrupt context. The maple tree implementation is not IRQ-safe (MT_FLAGS_LOCK_IRQ does nothing AFAICS). Instead, I use an external lock, spin_lock_irqsave() and spin_unlock_irqrestore(). Note that bounce buffers are never allocated dynamically if the software IO TLB is in fact a DMA restricted pool, which is intended to be stay in its designated location in physical memory. Signed-off-by: Petr Tesarik <petr.tesarik.ext@huawei.com> --- include/linux/swiotlb.h | 11 ++- kernel/dma/swiotlb.c | 156 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 157 insertions(+), 10 deletions(-)
Comments
[adding Alex as he has been interested in this in the past] On Mon, Mar 20, 2023 at 01:28:15PM +0100, Petr Tesarik wrote: > Second, on the Raspberry Pi 4, swiotlb is used by dma-buf for pages > moved from the rendering GPU (v3d driver), which can access all > memory, to the display output (vc4 driver), which is connected to a > bus with an address limit of 1 GiB and no IOMMU. These buffers can > be large (several megabytes) and cannot be handled by SWIOTLB, > because they exceed maximum segment size of 256 KiB. Such mapping > failures can be easily reproduced on a Raspberry Pi4: Starting > GNOME remote desktop results in a flood of kernel messages like > these: Shouldn't we make sure dma-buf allocates the buffers for the most restricted devices, and more importantly does something like a dma coherent allocation instead of a dynamic mapping of random memory? While a larger swiotlb works around this I don't think this fixes the root cause. > 1. The value is limited to ULONG_MAX, which is too little both for > physical addresses (e.g. x86 PAE or 32-bit ARM LPAE) and DMA > addresses (e.g. Xen guests on 32-bit ARM). > > 2. Since buffers are currently allocated with page granularity, a > PFN can be used instead. However, some values are reserved by > the maple tree implementation. Liam suggests to use > xa_mk_value() in that case, but that reduces the usable range by > half. Luckily, 31 bits are still enough to hold a PFN on all > 32-bit platforms. > > 3. Software IO TLB is used from interrupt context. The maple tree > implementation is not IRQ-safe (MT_FLAGS_LOCK_IRQ does nothing > AFAICS). Instead, I use an external lock, spin_lock_irqsave() and > spin_unlock_irqrestore(). > > Note that bounce buffers are never allocated dynamically if the > software IO TLB is in fact a DMA restricted pool, which is intended > to be stay in its designated location in physical memory. I'm a little worried about all that because it causes quite a bit of overhead even for callers that don't end up going into the dynamic range or do not use swiotlb at all. I don't really have a good answer here except for the usual avoid bounce buffering whenever you can that might not always be easy to do. > + gfp = (attrs & DMA_ATTR_MAY_SLEEP) ? GFP_KERNEL : GFP_NOWAIT; > + slot = kmalloc(sizeof(*slot), gfp | __GFP_NOWARN); > + if (!slot) > + goto err; > + > + slot->orig_addr = orig_addr; > + slot->alloc_size = alloc_size; > + slot->page = dma_direct_alloc_pages(dev, PAGE_ALIGN(alloc_size), > + &slot->dma_addr, dir, > + gfp | __GFP_NOWARN); > + if (!slot->page) > + goto err_free_slot; Without GFP_NOIO allocations this will deadlock eventually.
On 3/28/2023 6:07 AM, Christoph Hellwig wrote: > [adding Alex as he has been interested in this in the past] > > On Mon, Mar 20, 2023 at 01:28:15PM +0100, Petr Tesarik wrote: >> Second, on the Raspberry Pi 4, swiotlb is used by dma-buf for pages >> moved from the rendering GPU (v3d driver), which can access all >> memory, to the display output (vc4 driver), which is connected to a >> bus with an address limit of 1 GiB and no IOMMU. These buffers can >> be large (several megabytes) and cannot be handled by SWIOTLB, >> because they exceed maximum segment size of 256 KiB. Such mapping >> failures can be easily reproduced on a Raspberry Pi4: Starting >> GNOME remote desktop results in a flood of kernel messages like >> these: > > Shouldn't we make sure dma-buf allocates the buffers for the most > restricted devices, and more importantly does something like a dma > coherent allocation instead of a dynamic mapping of random memory? > > While a larger swiotlb works around this I don't think this fixes the root > cause. I tend to agree here. However, it's the DMABUF design itself that causes some trouble. The buffer is allocated by the v3d driver, which does not have the restriction, so the DMA API typically allocates an address somewhere near the 4G boundary. Userspace then exports the buffer, sends it to another process as a file descriptor and imports it into the vc4 driver, which requires DMA below 1G. In the beginning, v3d had no idea that the buffer would be exported to userspace, much less that it would be later imported into vc4. Anyway, I suspected that the buffers need not be imported into the vc4 driver (also hinted by Eric Anholt in a 2018 blog post [1]), and it seems I was right. I encountered the issue with Ubuntu 22.10; I installed latest openSUSE Tumbleweed yesterday, and I was not able to reproduce the issue there, most likely because the Mesa drivers have been fixed meanwhile. This makes the specific case of the Raspberry Pi 4 drivers moot. The issue may still affect other combinations of drivers, but I don't have any other real-world example ATM. [1] https://anholt.github.io/twivc4/2018/02/12/twiv/ >> 1. The value is limited to ULONG_MAX, which is too little both for >> physical addresses (e.g. x86 PAE or 32-bit ARM LPAE) and DMA >> addresses (e.g. Xen guests on 32-bit ARM). >> >> 2. Since buffers are currently allocated with page granularity, a >> PFN can be used instead. However, some values are reserved by >> the maple tree implementation. Liam suggests to use >> xa_mk_value() in that case, but that reduces the usable range by >> half. Luckily, 31 bits are still enough to hold a PFN on all >> 32-bit platforms. >> >> 3. Software IO TLB is used from interrupt context. The maple tree >> implementation is not IRQ-safe (MT_FLAGS_LOCK_IRQ does nothing >> AFAICS). Instead, I use an external lock, spin_lock_irqsave() and >> spin_unlock_irqrestore(). >> >> Note that bounce buffers are never allocated dynamically if the >> software IO TLB is in fact a DMA restricted pool, which is intended >> to be stay in its designated location in physical memory. > > I'm a little worried about all that because it causes quite a bit > of overhead even for callers that don't end up going into the > dynamic range or do not use swiotlb at all. I don't really have a > good answer here except for the usual avoid bounce buffering whenever > you can that might not always be easy to do. I'm also worried about all this overhead. OTOH I was not able to confirm it, because the difference between two successive fio test runs on an unmodified kernel was bigger than the difference between a vanilla and a patched kernel, except the maximum completion latency, which OTOH affected less than 0.01% of all requests. BTW my testing also suggests that the streaming DMA API is quite inefficient, because UAS performance _improved_ with swiotlb=force. Sure, this should probably be addressed in the UAS and/or xHCI driver, but what I mean is that moving away from swiotlb may even cause performance regressions, which is counter-intuitive. At least I would _not_ have expected it. >> + gfp = (attrs & DMA_ATTR_MAY_SLEEP) ? GFP_KERNEL : GFP_NOWAIT; >> + slot = kmalloc(sizeof(*slot), gfp | __GFP_NOWARN); >> + if (!slot) >> + goto err; >> + >> + slot->orig_addr = orig_addr; >> + slot->alloc_size = alloc_size; >> + slot->page = dma_direct_alloc_pages(dev, PAGE_ALIGN(alloc_size), >> + &slot->dma_addr, dir, >> + gfp | __GFP_NOWARN); >> + if (!slot->page) >> + goto err_free_slot; > > Without GFP_NOIO allocations this will deadlock eventually. Ah, that would affect the non-sleeping case (GFP_KERNEL), right? Petr T
On 3/28/2023 9:54 AM, Petr Tesarik wrote: > On 3/28/2023 6:07 AM, Christoph Hellwig wrote: >> [adding Alex as he has been interested in this in the past] >> >[...]>> I'm a little worried about all that because it causes quite a bit >> of overhead even for callers that don't end up going into the >> dynamic range or do not use swiotlb at all. I don't really have a >> good answer here except for the usual avoid bounce buffering whenever >> you can that might not always be easy to do. > > I'm also worried about all this overhead. Oh, wait! I can do at least something for devices which do not use swiotlb at all. If a device does not use bounce buffers, it cannot pass an address that belongs to the swiotlb. Consequently, the potentially expensive check can be skipped. This avoids the dynamic lookup penalty for devices which do not need the swiotlb. Note that the counter always remains zero if dma_io_tlb_mem is NULL, so the NULL check is not required. diff --git a/drivers/base/core.c b/drivers/base/core.c index a3e14143ec0c..f36638f207b8 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -2957,6 +2957,7 @@ void device_initialize(struct device *dev) #endif #ifdef CONFIG_SWIOTLB dev->dma_io_tlb_mem = &io_tlb_default_mem; + atomic_set(&dev->dma_io_tlb_cnt, 0); #endif } EXPORT_SYMBOL_GPL(device_initialize); diff --git a/include/linux/device.h b/include/linux/device.h index 44e3acae7b36..cfdddce4cc30 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -504,6 +504,7 @@ struct device_physical_location { * @dma_mem: Internal for coherent mem override. * @cma_area: Contiguous memory area for dma allocations * @dma_io_tlb_mem: Pointer to the swiotlb pool used. Not for driver use. + * @dma_io_tlb_cnt: Number of buffers mapped from the swiotlb pool. * @archdata: For arch-specific additions. * @of_node: Associated device tree node. * @fwnode: Associated device node supplied by platform firmware. @@ -609,6 +610,7 @@ struct device { #endif #ifdef CONFIG_SWIOTLB struct io_tlb_mem *dma_io_tlb_mem; + atomic_t dma_io_tlb_cnt; #endif /* arch specific additions */ struct dev_archdata archdata; diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h index 628e25ad7db7..7a115f4db49d 100644 --- a/include/linux/swiotlb.h +++ b/include/linux/swiotlb.h @@ -122,7 +122,7 @@ static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr) { struct io_tlb_mem *mem = dev->dma_io_tlb_mem; - return mem && + return atomic_read(&dev->dma_io_tlb_cnt) && (is_swiotlb_fixed(mem, paddr) || (mem->allow_dyn && is_swiotlb_dyn(mem, paddr))); } diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 3efaefebb6af..3dda1d3a39e8 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -954,6 +954,8 @@ phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr, return tlb_addr; } + atomic_inc(&dev->dma_io_tlb_cnt); + /* * When dir == DMA_FROM_DEVICE we could omit the copy from the orig * to the tlb buffer, if we knew for sure the device will @@ -1030,6 +1032,7 @@ void swiotlb_tbl_unmap_single(struct device *dev, phys_addr_t tlb_addr, swiotlb_release_slots(dev, tlb_addr); else swiotlb_dyn_unmap(dev, tlb_addr, dir); + atomic_dec(&dev->dma_io_tlb_cnt); } void swiotlb_sync_single_for_device(struct device *dev, phys_addr_t tlb_addr,
On Tue, 28 Mar 2023 09:54:35 +0200 Petr Tesarik <petrtesarik@huaweicloud.com> wrote: > On 3/28/2023 6:07 AM, Christoph Hellwig wrote: > > [adding Alex as he has been interested in this in the past] > > > > On Mon, Mar 20, 2023 at 01:28:15PM +0100, Petr Tesarik wrote: > >> Second, on the Raspberry Pi 4, swiotlb is used by dma-buf for pages > >> moved from the rendering GPU (v3d driver), which can access all > >> memory, to the display output (vc4 driver), which is connected to a > >> bus with an address limit of 1 GiB and no IOMMU. These buffers can > >> be large (several megabytes) and cannot be handled by SWIOTLB, > >> because they exceed maximum segment size of 256 KiB. Such mapping > >> failures can be easily reproduced on a Raspberry Pi4: Starting > >> GNOME remote desktop results in a flood of kernel messages like > >> these: > > > > Shouldn't we make sure dma-buf allocates the buffers for the most > > restricted devices, and more importantly does something like a dma > > coherent allocation instead of a dynamic mapping of random memory? > > > > While a larger swiotlb works around this I don't think this fixes the root > > cause. > > I tend to agree here. However, it's the DMABUF design itself that causes > some trouble. The buffer is allocated by the v3d driver, which does not > have the restriction, so the DMA API typically allocates an address > somewhere near the 4G boundary. Userspace then exports the buffer, sends > it to another process as a file descriptor and imports it into the vc4 > driver, which requires DMA below 1G. In the beginning, v3d had no idea > that the buffer would be exported to userspace, much less that it would > be later imported into vc4. > > Anyway, I suspected that the buffers need not be imported into the vc4 > driver (also hinted by Eric Anholt in a 2018 blog post [1]), and it > seems I was right. I encountered the issue with Ubuntu 22.10; I > installed latest openSUSE Tumbleweed yesterday, and I was not able to > reproduce the issue there, most likely because the Mesa drivers have > been fixed meanwhile. This makes the specific case of the Raspberry Pi 4 > drivers moot. The issue may still affect other combinations of drivers, > but I don't have any other real-world example ATM. I'm only seeing this problem with Wayland, no issue when switching Ubuntu to X. It seems Tumbleweed is using X by default. ...Juerg > [1] https://anholt.github.io/twivc4/2018/02/12/twiv/ > > >> 1. The value is limited to ULONG_MAX, which is too little both for > >> physical addresses (e.g. x86 PAE or 32-bit ARM LPAE) and DMA > >> addresses (e.g. Xen guests on 32-bit ARM). > >> > >> 2. Since buffers are currently allocated with page granularity, a > >> PFN can be used instead. However, some values are reserved by > >> the maple tree implementation. Liam suggests to use > >> xa_mk_value() in that case, but that reduces the usable range by > >> half. Luckily, 31 bits are still enough to hold a PFN on all > >> 32-bit platforms. > >> > >> 3. Software IO TLB is used from interrupt context. The maple tree > >> implementation is not IRQ-safe (MT_FLAGS_LOCK_IRQ does nothing > >> AFAICS). Instead, I use an external lock, spin_lock_irqsave() and > >> spin_unlock_irqrestore(). > >> > >> Note that bounce buffers are never allocated dynamically if the > >> software IO TLB is in fact a DMA restricted pool, which is intended > >> to be stay in its designated location in physical memory. > > > > I'm a little worried about all that because it causes quite a bit > > of overhead even for callers that don't end up going into the > > dynamic range or do not use swiotlb at all. I don't really have a > > good answer here except for the usual avoid bounce buffering whenever > > you can that might not always be easy to do. > > I'm also worried about all this overhead. OTOH I was not able to confirm > it, because the difference between two successive fio test runs on an > unmodified kernel was bigger than the difference between a vanilla and a > patched kernel, except the maximum completion latency, which OTOH > affected less than 0.01% of all requests. > > BTW my testing also suggests that the streaming DMA API is quite > inefficient, because UAS performance _improved_ with swiotlb=force. > Sure, this should probably be addressed in the UAS and/or xHCI driver, > but what I mean is that moving away from swiotlb may even cause > performance regressions, which is counter-intuitive. At least I would > _not_ have expected it. > > >> + gfp = (attrs & DMA_ATTR_MAY_SLEEP) ? GFP_KERNEL : GFP_NOWAIT; > >> + slot = kmalloc(sizeof(*slot), gfp | __GFP_NOWARN); > >> + if (!slot) > >> + goto err; > >> + > >> + slot->orig_addr = orig_addr; > >> + slot->alloc_size = alloc_size; > >> + slot->page = dma_direct_alloc_pages(dev, PAGE_ALIGN(alloc_size), > >> + &slot->dma_addr, dir, > >> + gfp | __GFP_NOWARN); > >> + if (!slot->page) > >> + goto err_free_slot; > > > > Without GFP_NOIO allocations this will deadlock eventually. > > Ah, that would affect the non-sleeping case (GFP_KERNEL), right? > > Petr T >
Hi Juerg, On Fri, 31 Mar 2023 07:26:09 +0000 Juerg Haefliger <juergh@proton.me> wrote: > On Tue, 28 Mar 2023 09:54:35 +0200 > Petr Tesarik <petrtesarik@huaweicloud.com> wrote: > >[...] > > Anyway, I suspected that the buffers need not be imported into the vc4 > > driver (also hinted by Eric Anholt in a 2018 blog post [1]), and it > > seems I was right. I encountered the issue with Ubuntu 22.10; I > > installed latest openSUSE Tumbleweed yesterday, and I was not able to > > reproduce the issue there, most likely because the Mesa drivers have > > been fixed meanwhile. This makes the specific case of the Raspberry Pi 4 > > drivers moot. The issue may still affect other combinations of drivers, > > but I don't have any other real-world example ATM. > > I'm only seeing this problem with Wayland, no issue when switching Ubuntu to > X. It seems Tumbleweed is using X by default. I know; I was the team lead of SUSE low-level graphics engineers until end of last year... I have just double-checked, but this output of wayland-info in the GNOME session accessed over RDP is quite convincing: interface: 'wl_compositor', version: 5, name: 1 interface: 'wl_shm', version: 1, name: 2 formats (fourcc): 0x48344258 = 'XB4H' 0x48344241 = 'AB4H' 0x48345258 = 'XR4H' 0x48345241 = 'AR4H' 0x30334258 = 'XB30' 0x30334241 = 'AB30' 0x30335258 = 'XR30' 0x30335241 = 'AR30' 0x36314752 = 'RG16' 0x34324258 = 'XB24' 0x34324241 = 'AB24' 1 = 'XR24' 0 = 'AR24' interface: 'wl_output', version: 3, name: 3 x: 0, y: 0, scale: 1, physical_width: 430 mm, physical_height: 270 mm, make: 'FUS', model: 'P20W-5 ECO', subpixel_orientation: unknown, output_transform: normal, mode: width: 1680 px, height: 1050 px, refresh: 59.954 Hz, flags: current preferred interface: 'zxdg_output_manager_v1', version: 3, name: 4 xdg_output_v1 output: 3 name: 'HDMI-1' description: 'Fujitsu Siemens Computers GmbH 20"' logical_x: 0, logical_y: 0 logical_width: 1680, logical_height: 1050 interface: 'wl_data_device_manager', version: 3, name: 5 interface: 'zwp_primary_selection_device_manager_v1', version: 1, name: 6 interface: 'wl_subcompositor', version: 1, name: 7 interface: 'xdg_wm_base', version: 4, name: 8 interface: 'gtk_shell1', version: 5, name: 9 interface: 'wp_viewporter', version: 1, name: 10 interface: 'zwp_pointer_gestures_v1', version: 3, name: 11 interface: 'zwp_tablet_manager_v2', version: 1, name: 12 interface: 'wl_seat', version: 8, name: 13 name: seat0 capabilities: pointer keyboard keyboard repeat rate: 33 keyboard repeat delay: 500 interface: 'zwp_relative_pointer_manager_v1', version: 1, name: 14 interface: 'zwp_pointer_constraints_v1', version: 1, name: 15 interface: 'zxdg_exporter_v1', version: 1, name: 16 interface: 'zxdg_importer_v1', version: 1, name: 17 interface: 'zwp_linux_dmabuf_v1', version: 3, name: 18 formats (fourcc) and modifiers (names): 0x48344258 = 'XB4H'; 0x00ffffffffffffff = INVALID 0x48344241 = 'AB4H'; 0x00ffffffffffffff = INVALID 0x36314752 = 'RG16'; 0x00ffffffffffffff = INVALID 0x30334258 = 'XB30'; 0x00ffffffffffffff = INVALID 0x30335258 = 'XR30'; 0x00ffffffffffffff = INVALID 0x30334241 = 'AB30'; 0x00ffffffffffffff = INVALID 0x30335241 = 'AR30'; 0x00ffffffffffffff = INVALID 0x34324258 = 'XB24'; 0x00ffffffffffffff = INVALID 0x34325258 = 'XR24'; 0x00ffffffffffffff = INVALID 0x34324241 = 'AB24'; 0x00ffffffffffffff = INVALID 0x34325241 = 'AR24'; 0x00ffffffffffffff = INVALID interface: 'wp_single_pixel_buffer_manager_v1', version: 1, name: 19 interface: 'zwp_keyboard_shortcuts_inhibit_manager_v1', version: 1, name: 20 interface: 'zwp_text_input_manager_v3', version: 1, name: 21 interface: 'wp_presentation', version: 1, name: 22 presentation clock id: 1 (CLOCK_MONOTONIC) interface: 'xdg_activation_v1', version: 1, name: 23 Petr T
On Fri, 31 Mar 2023 11:00:43 +0200 Petr Tesařík <petr@tesarici.cz> wrote: > Hi Juerg, > > On Fri, 31 Mar 2023 07:26:09 +0000 > Juerg Haefliger <juergh@proton.me> wrote: > > > On Tue, 28 Mar 2023 09:54:35 +0200 > > Petr Tesarik <petrtesarik@huaweicloud.com> wrote: > > > >[...] > > > Anyway, I suspected that the buffers need not be imported into the vc4 > > > driver (also hinted by Eric Anholt in a 2018 blog post [1]), and it > > > seems I was right. I encountered the issue with Ubuntu 22.10; I > > > installed latest openSUSE Tumbleweed yesterday, and I was not able to > > > reproduce the issue there, most likely because the Mesa drivers have > > > been fixed meanwhile. This makes the specific case of the Raspberry Pi 4 > > > drivers moot. The issue may still affect other combinations of drivers, > > > but I don't have any other real-world example ATM. > > > > I'm only seeing this problem with Wayland, no issue when switching Ubuntu to > > X. It seems Tumbleweed is using X by default. > > I know; I was the team lead of SUSE low-level graphics engineers until > end of last year... I have just double-checked, but this output of > wayland-info in the GNOME session accessed over RDP is quite convincing: It sure is but how did you get that?? For me it's: $ wayland-info failed to create display: No such file or directory And from strace: connect(3, {sa_family=AF_UNIX, sun_path="/run/user/1000/wayland-0"}, 27) = -1 ENOENT (No such file or directory) Which is kind of expected when running X, no? $ ps -ef | grep -iP 'xorg|wayland' opensuse 1377 1375 0 09:13 tty2 00:00:16 /usr/bin/Xorg.bin vt2 -displayfd 3 -auth /run/user/1000/gdm/Xauthority -nolisten tcp -background none -noreset -keeptty -novtswitch -verbose 3 What am I missing? ...Juerg > interface: 'wl_compositor', version: 5, name: 1 > interface: 'wl_shm', version: 1, name: 2 > formats (fourcc): > 0x48344258 = 'XB4H' > 0x48344241 = 'AB4H' > 0x48345258 = 'XR4H' > 0x48345241 = 'AR4H' > 0x30334258 = 'XB30' > 0x30334241 = 'AB30' > 0x30335258 = 'XR30' > 0x30335241 = 'AR30' > 0x36314752 = 'RG16' > 0x34324258 = 'XB24' > 0x34324241 = 'AB24' > 1 = 'XR24' > 0 = 'AR24' > interface: 'wl_output', version: 3, name: 3 > x: 0, y: 0, scale: 1, > physical_width: 430 mm, physical_height: 270 mm, > make: 'FUS', model: 'P20W-5 ECO', > subpixel_orientation: unknown, output_transform: normal, > mode: > width: 1680 px, height: 1050 px, refresh: 59.954 Hz, > flags: current preferred > interface: 'zxdg_output_manager_v1', version: 3, name: 4 > xdg_output_v1 > output: 3 > name: 'HDMI-1' > description: 'Fujitsu Siemens Computers GmbH 20"' > logical_x: 0, logical_y: 0 > logical_width: 1680, logical_height: 1050 > interface: 'wl_data_device_manager', version: 3, name: 5 > interface: 'zwp_primary_selection_device_manager_v1', version: 1, name: 6 > interface: 'wl_subcompositor', version: 1, name: 7 > interface: 'xdg_wm_base', version: 4, name: 8 > interface: 'gtk_shell1', version: 5, name: 9 > interface: 'wp_viewporter', version: 1, name: 10 > interface: 'zwp_pointer_gestures_v1', version: 3, name: 11 > interface: 'zwp_tablet_manager_v2', version: 1, name: 12 > interface: 'wl_seat', version: 8, name: 13 > name: seat0 > capabilities: pointer keyboard > keyboard repeat rate: 33 > keyboard repeat delay: 500 > interface: 'zwp_relative_pointer_manager_v1', version: 1, name: 14 > interface: 'zwp_pointer_constraints_v1', version: 1, name: 15 > interface: 'zxdg_exporter_v1', version: 1, name: 16 > interface: 'zxdg_importer_v1', version: 1, name: 17 > interface: 'zwp_linux_dmabuf_v1', version: 3, name: 18 > formats (fourcc) and modifiers (names): > 0x48344258 = 'XB4H'; 0x00ffffffffffffff = INVALID > 0x48344241 = 'AB4H'; 0x00ffffffffffffff = INVALID > 0x36314752 = 'RG16'; 0x00ffffffffffffff = INVALID > 0x30334258 = 'XB30'; 0x00ffffffffffffff = INVALID > 0x30335258 = 'XR30'; 0x00ffffffffffffff = INVALID > 0x30334241 = 'AB30'; 0x00ffffffffffffff = INVALID > 0x30335241 = 'AR30'; 0x00ffffffffffffff = INVALID > 0x34324258 = 'XB24'; 0x00ffffffffffffff = INVALID > 0x34325258 = 'XR24'; 0x00ffffffffffffff = INVALID > 0x34324241 = 'AB24'; 0x00ffffffffffffff = INVALID > 0x34325241 = 'AR24'; 0x00ffffffffffffff = INVALID > interface: 'wp_single_pixel_buffer_manager_v1', version: 1, name: 19 > interface: 'zwp_keyboard_shortcuts_inhibit_manager_v1', version: 1, name: 20 > interface: 'zwp_text_input_manager_v3', version: 1, name: 21 > interface: 'wp_presentation', version: 1, name: 22 > presentation clock id: 1 (CLOCK_MONOTONIC) > interface: 'xdg_activation_v1', version: 1, name: 23 > > Petr T
On Tue, Mar 28, 2023 at 09:54:35AM +0200, Petr Tesarik wrote: > I tend to agree here. However, it's the DMABUF design itself that causes > some trouble. The buffer is allocated by the v3d driver, which does not > have the restriction, so the DMA API typically allocates an address > somewhere near the 4G boundary. Userspace then exports the buffer, sends > it to another process as a file descriptor and imports it into the vc4 > driver, which requires DMA below 1G. In the beginning, v3d had no idea > that the buffer would be exported to userspace, much less that it would > be later imported into vc4. Then we need to either: a) figure out a way to communicate these addressing limitations b) find a way to migrate a buffer into other memory, similar to how page migration works for page cache > BTW my testing also suggests that the streaming DMA API is quite > inefficient, because UAS performance _improved_ with swiotlb=force. > Sure, this should probably be addressed in the UAS and/or xHCI driver, > but what I mean is that moving away from swiotlb may even cause > performance regressions, which is counter-intuitive. At least I would > _not_ have expected it. That is indeed very odd. Are you running with a very slow iommu driver there? Or what is the actual use case there in general? > >> + gfp = (attrs & DMA_ATTR_MAY_SLEEP) ? GFP_KERNEL : GFP_NOWAIT; > >> + slot = kmalloc(sizeof(*slot), gfp | __GFP_NOWARN); > >> + if (!slot) > >> + goto err; > >> + > >> + slot->orig_addr = orig_addr; > >> + slot->alloc_size = alloc_size; > >> + slot->page = dma_direct_alloc_pages(dev, PAGE_ALIGN(alloc_size), > >> + &slot->dma_addr, dir, > >> + gfp | __GFP_NOWARN); > >> + if (!slot->page) > >> + goto err_free_slot; > > > > Without GFP_NOIO allocations this will deadlock eventually. > > Ah, that would affect the non-sleeping case (GFP_KERNEL), right? Yes.
On Tue, Mar 28, 2023 at 02:43:03PM +0200, Petr Tesarik wrote: > Oh, wait! I can do at least something for devices which do not use > swiotlb at all. > > If a device does not use bounce buffers, it cannot pass an address > that belongs to the swiotlb. Consequently, the potentially > expensive check can be skipped. This avoids the dynamic lookup > penalty for devices which do not need the swiotlb. > > Note that the counter always remains zero if dma_io_tlb_mem is > NULL, so the NULL check is not required. Hmm, that's yet another atomic for each map/unmap, and bloats struct device. (Btw, in case anyone is interested, we really need to get started on moving the dma fields out of struct device into a sub-struct only allocated for DMA capable busses)
On Fri, 7 Apr 2023 07:57:04 +0200 Christoph Hellwig <hch@lst.de> wrote: > On Tue, Mar 28, 2023 at 02:43:03PM +0200, Petr Tesarik wrote: > > Oh, wait! I can do at least something for devices which do not use > > swiotlb at all. > > > > If a device does not use bounce buffers, it cannot pass an address > > that belongs to the swiotlb. Consequently, the potentially > > expensive check can be skipped. This avoids the dynamic lookup > > penalty for devices which do not need the swiotlb. > > > > Note that the counter always remains zero if dma_io_tlb_mem is > > NULL, so the NULL check is not required. > > Hmm, that's yet another atomic for each map/unmap, and bloats > struct device. I'm not sure how bad it is to bloat struct device. It is already quite large, e.g. in my x86 build it is 768 bytes (exact size depends on config options), and nobody seems to be concerned... Regarding the atomic operations, I am currently testing a slightly different approach, which merely sets a flag if there are any dynamically allocated bounce buffers. The atomic check changes to smp_load_acquire(), and the atomic inc/dec to smp_store_release() only if the flag changes. That said, if I hammer this path with heavy parallel I/O, I can still see some performance cost for devices that use swiotlb, but at least devices that do not need such bounce buffers seem to be unaffected then. > (Btw, in case anyone is interested, we really need to get started > on moving the dma fields out of struct device into a sub-struct > only allocated for DMA capable busses) I like this idea. In fact, my WIP topic branch now moves the swiotlb fields into a separate struct, but I can surely go further and move all DMA-related fields. I doubt it is worth to allocate it separately, though. We are talking about replacing some 100 bytes (in the worst case) with a pointer to a dynamically allocated struct, but the dynamic allocator adds some overhead. I believe it pays off only if the vast majority of struct device instances do not need these DMA fields, but is that really the case? Petr T
On Fri, 7 Apr 2023 07:55:48 +0200 Christoph Hellwig <hch@lst.de> wrote: > On Tue, Mar 28, 2023 at 09:54:35AM +0200, Petr Tesarik wrote: > > I tend to agree here. However, it's the DMABUF design itself that causes > > some trouble. The buffer is allocated by the v3d driver, which does not > > have the restriction, so the DMA API typically allocates an address > > somewhere near the 4G boundary. Userspace then exports the buffer, sends > > it to another process as a file descriptor and imports it into the vc4 > > driver, which requires DMA below 1G. In the beginning, v3d had no idea > > that the buffer would be exported to userspace, much less that it would > > be later imported into vc4. > > Then we need to either: > > a) figure out a way to communicate these addressing limitations AFAICS this would require a complete overhaul of the dma-buf userspace API so that intended imports are communicated at export time. In other words, it would be quite intrusive. Not my preferrence. > b) find a way to migrate a buffer into other memory, similar to > how page migration works for page cache Let me express the idea in my own words to make sure I get it right. When a DMA buffer is imported, but before it is ultimately pinned in memory, the importing device driver checks whether the buffer meets its DMA constraints. If not, it calls a function provided by the exporting device driver to migrate the buffer. This makes sense, but: 1) The operation must be implemented in the exporting driver; this will take some time. 2) In theory, there may be no overlap between the exporting device and the importing device. OTOH I'm not aware of any real-world example, so we can probably return a suitable error code, and that's it. Anyway, I have already written in another reply that my original use case is moot, because a more recent distribution can do the job without using dma-buf, so it has been fixed in user space, be it in GNOME, pipewire, or Mesa (I don't really have to know). At this point I would go with the assumption that large buffers allocated by media subsystems will not hit swiotlb. Consequently, I don't plan to spend more time on this branch of the story. > > BTW my testing also suggests that the streaming DMA API is quite > > inefficient, because UAS performance _improved_ with swiotlb=force. > > Sure, this should probably be addressed in the UAS and/or xHCI driver, > > but what I mean is that moving away from swiotlb may even cause > > performance regressions, which is counter-intuitive. At least I would > > _not_ have expected it. > > That is indeed very odd. Are you running with a very slow iommu > driver there? Or what is the actual use case there in general? This was on a Raspberry Pi 4, which does not have any IOMMU. IOW it looks like copying data around can be faster than sending it straight to the device. When I have some more time, I must investigate what is really happening there, because it does not make any sense to me. Petr T
On Fri, Apr 07, 2023 at 12:46:27PM +0200, Petr Tesařík wrote: > > b) find a way to migrate a buffer into other memory, similar to > > how page migration works for page cache > > Let me express the idea in my own words to make sure I get it right. > When a DMA buffer is imported, but before it is ultimately pinned in > memory, the importing device driver checks whether the buffer meets its > DMA constraints. If not, it calls a function provided by the exporting > device driver to migrate the buffer. Yes. > This makes sense, but: > > 1) The operation must be implemented in the exporting driver; this > will take some time. > > 2) In theory, there may be no overlap between the exporting device > and the importing device. OTOH I'm not aware of any real-world > example, so we can probably return a suitable error code, and > that's it. Indeed. And if there is no overlap, which as you said is indeed very unlikely but in theory possible, we could still keep migrating forther and back. One important thing that we should do is to consolidate more of the dma-buf implementation code. Right now they just seem to be a wild mess of copy and pasted boilerplate code unfortunately. > Anyway, I have already written in another reply that my original use > case is moot, because a more recent distribution can do the job without > using dma-buf, so it has been fixed in user space, be it in GNOME, > pipewire, or Mesa (I don't really have to know). > > At this point I would go with the assumption that large buffers > allocated by media subsystems will not hit swiotlb. Consequently, I > don't plan to spend more time on this branch of the story. Sounds fine to me, and thanks for taking the effort so far. > > > BTW my testing also suggests that the streaming DMA API is quite > > > inefficient, because UAS performance _improved_ with swiotlb=force. > > > Sure, this should probably be addressed in the UAS and/or xHCI driver, > > > but what I mean is that moving away from swiotlb may even cause > > > performance regressions, which is counter-intuitive. At least I would > > > _not_ have expected it. > > > > That is indeed very odd. Are you running with a very slow iommu > > driver there? Or what is the actual use case there in general? > > This was on a Raspberry Pi 4, which does not have any IOMMU. IOW it > looks like copying data around can be faster than sending it straight > to the device. When I have some more time, I must investigate what is > really happening there, because it does not make any sense to me. If you're not using an IOMMU that doesn't actually make any sense to me. swiotlb calls into exactly the same routines as dma-direct does for the dma setup on each I/O, just after copying the data. So if you do have some spare cycles to investigate what is going on here, I'd be really curious about the results.
On 4/7/2023 12:15 PM, Petr Tesařík wrote: > On Fri, 7 Apr 2023 07:57:04 +0200 > Christoph Hellwig <hch@lst.de> wrote: >[...]>> (Btw, in case anyone is interested, we really need to get started >> on moving the dma fields out of struct device into a sub-struct >> only allocated for DMA capable busses) > > I like this idea. In fact, my WIP topic branch now moves the swiotlb > fields into a separate struct, but I can surely go further and move all > DMA-related fields. I have looked into this now, and it looks like a nice cleanup. The challenge is to get these patches reviewed by all affected maintainers, and it would be blocking my work on dynamically allocated bounce buffers. How about moving only some fields initially (coherent override, cma and swiotlb)? These few are not used outside kernel/dma, but at least the swiotlb part makes my patches easier to follow. I can move the rest as soon as the dynamic patch series is merged. Petr Tesarik
Hi Christoph! I'd like to follow up on this sub-thread: On Fri, 7 Apr 2023 12:15:55 +0200 Petr Tesařík <petr@tesarici.cz> wroe: > On Fri, 7 Apr 2023 07:57:04 +0200 > Christoph Hellwig <hch@lst.de> wrote: >[...] > > (Btw, in case anyone is interested, we really need to get started > > on moving the dma fields out of struct device into a sub-struct > > only allocated for DMA capable busses) > > I like this idea. In fact, my WIP topic branch now moves the swiotlb > fields into a separate struct, As you have noticed, I have removed that commit again in v2. The reason is that I'm not sure about the intended goal. I have looked around for examples of moving fields out of struct device and found different approaches: A. struct dev_msi_info The MSI fields are merely grouped in a separate struct, which is defined in device.h and embedded in struct device. I don't see much benefit. B. struct dev_pm_info This struct is also embedded in struct device, but it is defined in <linux/pm.h>, which is mentioned in MAINTAINERS. The benefit is that further changes are reviewed by this maintainer. The downside is that device.h includes pm.h. C. struct dev_pin_info This struct is merely declared in device.h and defined pinctrl/devinfo.h (which is not included). Only a pointer to this struct is stored in struct device. Of course, the pointer must be initialized (and released) somehow. Here my question: What did you want for DMA fields? A. Only grouping those fields in their own struct? B. Or move the definition to another include file (cf. MAINTAINERS)? C. Or store a pointer in struct device? Since you mentioned "allocated", it sounds like you want to achieve C, but: 1. Is it worth the extra dereference for every use? 2. How should the struct be allocated? Presumably not with kmalloc() in device_initialize(), because I don't know how to determine if a device is DMA capable this low in the call stack. So, should it be allocated together with the containing structure? AFAICS this would mean changing nearly all device drivers... As you can see, I need some more guidance from you before I can start working on this. ;-) Petr T
On 2023-04-21 14:03, Petr Tesařík wrote: > Hi Christoph! > > I'd like to follow up on this sub-thread: > > On Fri, 7 Apr 2023 12:15:55 +0200 > Petr Tesařík <petr@tesarici.cz> wroe: > >> On Fri, 7 Apr 2023 07:57:04 +0200 >> Christoph Hellwig <hch@lst.de> wrote: >> [...] >>> (Btw, in case anyone is interested, we really need to get started >>> on moving the dma fields out of struct device into a sub-struct >>> only allocated for DMA capable busses) >> >> I like this idea. In fact, my WIP topic branch now moves the swiotlb >> fields into a separate struct, > > As you have noticed, I have removed that commit again in v2. > > The reason is that I'm not sure about the intended goal. I have looked > around for examples of moving fields out of struct device and found > different approaches: > > A. struct dev_msi_info > The MSI fields are merely grouped in a separate struct, which is > defined in device.h and embedded in struct device. I don't see much > benefit. > > B. struct dev_pm_info > This struct is also embedded in struct device, but it is defined in > <linux/pm.h>, which is mentioned in MAINTAINERS. The benefit is that > further changes are reviewed by this maintainer. The downside is > that device.h includes pm.h. > > C. struct dev_pin_info > This struct is merely declared in device.h and defined > pinctrl/devinfo.h (which is not included). Only a pointer to this > struct is stored in struct device. Of course, the pointer must be > initialized (and released) somehow. > > Here my question: What did you want for DMA fields? > > A. Only grouping those fields in their own struct? > B. Or move the definition to another include file (cf. MAINTAINERS)? > C. Or store a pointer in struct device? dev->dma_parms is already this, and IIRC still has some very old comments somewhere about consolidating the other DMA-related fields in there. > Since you mentioned "allocated", it sounds like you want to achieve C, > but: > > 1. Is it worth the extra dereference for every use? > 2. How should the struct be allocated? Presumably not with kmalloc() in > device_initialize(), because I don't know how to determine if a > device is DMA capable this low in the call stack. So, should it be > allocated together with the containing structure? AFAICS this would > mean changing nearly all device drivers... The bus code knows whether it's a DMA-capable bus or not, and as such should already be providing a .dma_configure method and/or performing some initialisation of DMA fields. Many of the ones that would need to are already providing dma_parms, in fact. Thanks, Robin. > > As you can see, I need some more guidance from you before I can start > working on this. ;-) > > Petr T
Hi Robin, On Fri, 21 Apr 2023 15:58:18 +0100 Robin Murphy <robin.murphy@arm.com> wrote: > On 2023-04-21 14:03, Petr Tesařík wrote: >[...] > > Here my question: What did you want for DMA fields? > > > > A. Only grouping those fields in their own struct? > > B. Or move the definition to another include file (cf. MAINTAINERS)? > > C. Or store a pointer in struct device? > > dev->dma_parms is already this, and IIRC still has some very old > comments somewhere about consolidating the other DMA-related fields in > there. Thank you for the hint! I have actually seen dma_parms, but since it can be NULL and was initialized from various drivers, it did not occur to me that NULL simply means not DMA-capable. This is really helpful. Petr T > > Since you mentioned "allocated", it sounds like you want to achieve C, > > but: > > > > 1. Is it worth the extra dereference for every use? > > 2. How should the struct be allocated? Presumably not with kmalloc() in > > device_initialize(), because I don't know how to determine if a > > device is DMA capable this low in the call stack. So, should it be > > allocated together with the containing structure? AFAICS this would > > mean changing nearly all device drivers... > > The bus code knows whether it's a DMA-capable bus or not, and as such > should already be providing a .dma_configure method and/or performing > some initialisation of DMA fields. Many of the ones that would need to > are already providing dma_parms, in fact. > > Thanks, > Robin. > > > > > As you can see, I need some more guidance from you before I can start > > working on this. ;-) > > > > Petr T
On Fri, Apr 21, 2023 at 05:09:34PM +0200, Petr Tesařík wrote: > > > A. Only grouping those fields in their own struct? > > > B. Or move the definition to another include file (cf. MAINTAINERS)? > > > C. Or store a pointer in struct device? > > > > dev->dma_parms is already this, and IIRC still has some very old > > comments somewhere about consolidating the other DMA-related fields in > > there. > > Thank you for the hint! I have actually seen dma_parms, but since it > can be NULL and was initialized from various drivers, it did not occur > to me that NULL simply means not DMA-capable. Yes, dma_parms are still optional. A much better hint is the dma_mask itself, which for historic reasons is implemented in a completely awfull way as a pointer to something stored in the containing struture, which all kinds of platform devices or minor buses doing nasty hacks there.
On Thu, 06 Apr 2023 11:44:55 +0000 Juerg Haefliger <juergh@proton.me> wrote: > On Fri, 31 Mar 2023 11:00:43 +0200 > Petr Tesařík <petr@tesarici.cz> wrote: > > > Hi Juerg, > > > > On Fri, 31 Mar 2023 07:26:09 +0000 > > Juerg Haefliger <juergh@proton.me> wrote: > > > > > On Tue, 28 Mar 2023 09:54:35 +0200 > > > Petr Tesarik <petrtesarik@huaweicloud.com> wrote: > > > > > >[...] > > > > Anyway, I suspected that the buffers need not be imported into the vc4 > > > > driver (also hinted by Eric Anholt in a 2018 blog post [1]), and it > > > > seems I was right. I encountered the issue with Ubuntu 22.10; I > > > > installed latest openSUSE Tumbleweed yesterday, and I was not able to > > > > reproduce the issue there, most likely because the Mesa drivers have > > > > been fixed meanwhile. This makes the specific case of the Raspberry Pi 4 > > > > drivers moot. The issue may still affect other combinations of drivers, > > > > but I don't have any other real-world example ATM. > > > > > > I'm only seeing this problem with Wayland, no issue when switching Ubuntu to > > > X. It seems Tumbleweed is using X by default. > > > > I know; I was the team lead of SUSE low-level graphics engineers until > > end of last year... I have just double-checked, but this output of > > wayland-info in the GNOME session accessed over RDP is quite convincing: > > It sure is but how did you get that?? I'm sorry for late reply. Yes, the default is GNOME on X.org, but you can change it on the GDM login screen. After choosing your account, a settings gear icon appears in the lower right corner. Click on it and choose GNOME. That starts a Wayland session. HTH Petr T
diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h index b71adba03dc7..0ef27d6491b9 100644 --- a/include/linux/swiotlb.h +++ b/include/linux/swiotlb.h @@ -7,6 +7,7 @@ #include <linux/init.h> #include <linux/types.h> #include <linux/limits.h> +#include <linux/maple_tree.h> #include <linux/spinlock.h> struct device; @@ -87,6 +88,8 @@ dma_addr_t swiotlb_map(struct device *dev, phys_addr_t phys, * @for_alloc: %true if the pool is used for memory allocation * @nareas: The area number in the pool. * @area_nslabs: The slot number in the area. + * @dyn_lock: Protect dynamically allocated slots. + * @dyn_slots: Dynamically allocated slots. */ struct io_tlb_mem { phys_addr_t start; @@ -102,9 +105,13 @@ struct io_tlb_mem { unsigned int area_nslabs; struct io_tlb_area *areas; struct io_tlb_slot *slots; + spinlock_t dyn_lock; + struct maple_tree dyn_slots; }; extern struct io_tlb_mem io_tlb_default_mem; +bool is_swiotlb_dyn(struct io_tlb_mem *mem, phys_addr_t paddr); + static inline bool is_swiotlb_fixed(struct io_tlb_mem *mem, phys_addr_t paddr) { return paddr >= mem->start && paddr < mem->end; @@ -114,7 +121,9 @@ static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr) { struct io_tlb_mem *mem = dev->dma_io_tlb_mem; - return mem && is_swiotlb_fixed(mem, paddr); + return mem && + (is_swiotlb_fixed(mem, paddr) || + is_swiotlb_dyn(mem, paddr)); } static inline bool is_swiotlb_force_bounce(struct device *dev) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index e8608bcb205e..c6a0b8f2aa6f 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -41,6 +41,7 @@ #include <linux/string.h> #include <linux/swiotlb.h> #include <linux/types.h> +#include <linux/xarray.h> #ifdef CONFIG_DMA_RESTRICTED_POOL #include <linux/of.h> #include <linux/of_fdt.h> @@ -68,6 +69,13 @@ struct io_tlb_slot { unsigned int list; }; +struct io_tlb_dyn_slot { + phys_addr_t orig_addr; + size_t alloc_size; + struct page *page; + dma_addr_t dma_addr; +}; + static bool swiotlb_force_bounce; static bool swiotlb_force_disable; @@ -292,6 +300,10 @@ static void swiotlb_init_io_tlb_mem(struct io_tlb_mem *mem, phys_addr_t start, mem->slots[i].alloc_size = 0; } + spin_lock_init(&mem->dyn_lock); + mt_init_flags(&mem->dyn_slots, MT_FLAGS_LOCK_EXTERN); + mt_set_external_lock(&mem->dyn_slots, &mem->dyn_lock); + /* * If swiotlb_unencrypted_base is set, the bounce buffer memory will * be remapped and cleared in swiotlb_update_mem_attributes. @@ -516,6 +528,115 @@ void __init swiotlb_exit(void) memset(mem, 0, sizeof(*mem)); } +static struct io_tlb_dyn_slot *swiotlb_dyn_slot(struct io_tlb_mem *mem, + phys_addr_t paddr) +{ + unsigned long index = (uintptr_t)xa_mk_value(PHYS_PFN(paddr)); + struct io_tlb_dyn_slot *slot; + unsigned long flags; + + spin_lock_irqsave(&mem->dyn_lock, flags); + slot = mt_find(&mem->dyn_slots, &index, index); + spin_unlock_irqrestore(&mem->dyn_lock, flags); + return slot; +} + +bool is_swiotlb_dyn(struct io_tlb_mem *mem, phys_addr_t paddr) +{ + return !!swiotlb_dyn_slot(mem, paddr); +} + +static void swiotlb_dyn_bounce(struct device *dev, phys_addr_t tlb_addr, + size_t size, enum dma_data_direction dir) +{ + struct io_tlb_mem *mem = dev->dma_io_tlb_mem; + struct io_tlb_dyn_slot *slot = swiotlb_dyn_slot(mem, tlb_addr); + unsigned int tlb_offset; + unsigned char *vaddr; + + if (!slot) + return; + + tlb_offset = tlb_addr - page_to_phys(slot->page); + vaddr = page_address(slot->page) + tlb_offset; + + swiotlb_copy(dev, slot->orig_addr, vaddr, size, slot->alloc_size, + tlb_offset, dir); +} + +static phys_addr_t swiotlb_dyn_map(struct device *dev, phys_addr_t orig_addr, + size_t alloc_size, unsigned int alloc_align_mask, + enum dma_data_direction dir, unsigned long attrs) +{ + struct io_tlb_mem *mem = dev->dma_io_tlb_mem; + struct io_tlb_dyn_slot *slot; + unsigned long index; + unsigned long flags; + phys_addr_t paddr; + gfp_t gfp; + int err; + + /* Allocation has page granularity. Avoid small buffers. */ + if (alloc_size < PAGE_SIZE) + goto err; + + /* DMA direct does not deal with physical address constraints. */ + if (alloc_align_mask || dma_get_min_align_mask(dev)) + goto err; + + gfp = (attrs & DMA_ATTR_MAY_SLEEP) ? GFP_KERNEL : GFP_NOWAIT; + slot = kmalloc(sizeof(*slot), gfp | __GFP_NOWARN); + if (!slot) + goto err; + + slot->orig_addr = orig_addr; + slot->alloc_size = alloc_size; + slot->page = dma_direct_alloc_pages(dev, PAGE_ALIGN(alloc_size), + &slot->dma_addr, dir, + gfp | __GFP_NOWARN); + if (!slot->page) + goto err_free_slot; + + paddr = page_to_phys(slot->page); + index = (uintptr_t)xa_mk_value(PHYS_PFN(paddr)); + spin_lock_irqsave(&mem->dyn_lock, flags); + err = mtree_store_range(&mem->dyn_slots, index, + index + PFN_UP(alloc_size) - 1, + slot, GFP_NOWAIT | __GFP_NOWARN); + spin_unlock_irqrestore(&mem->dyn_lock, flags); + if (err) + goto err_free_dma; + + return paddr; + +err_free_dma: + dma_direct_free_pages(dev, slot->alloc_size, slot->page, + slot->dma_addr, dir); + +err_free_slot: + kfree(slot); +err: + return (phys_addr_t)DMA_MAPPING_ERROR; +} + +static void swiotlb_dyn_unmap(struct device *dev, phys_addr_t tlb_addr, + enum dma_data_direction dir) +{ + unsigned long index = (uintptr_t)xa_mk_value(PHYS_PFN(tlb_addr)); + struct io_tlb_mem *mem = dev->dma_io_tlb_mem; + struct io_tlb_dyn_slot *slot; + unsigned long flags; + + spin_lock_irqsave(&mem->dyn_lock, flags); + slot = mt_find(&mem->dyn_slots, &index, index); + mtree_erase(&mem->dyn_slots, index); + spin_unlock_irqrestore(&mem->dyn_lock, flags); + + dma_direct_free_pages(dev, slot->alloc_size, slot->page, + slot->dma_addr, dir); + kfree(slot); +} + /* * Return the offset into a iotlb slot required to keep the device happy. */ @@ -524,11 +645,8 @@ static unsigned int swiotlb_align_offset(struct device *dev, u64 addr) return addr & dma_get_min_align_mask(dev) & (IO_TLB_SIZE - 1); } -/* - * Bounce: copy the swiotlb buffer from or back to the original dma location - */ -static void swiotlb_bounce(struct device *dev, phys_addr_t tlb_addr, size_t size, - enum dma_data_direction dir) +static void swiotlb_fixed_bounce(struct device *dev, phys_addr_t tlb_addr, + size_t size, enum dma_data_direction dir) { struct io_tlb_mem *mem = dev->dma_io_tlb_mem; int index = (tlb_addr - mem->start) >> IO_TLB_SHIFT; @@ -608,6 +726,18 @@ static void swiotlb_copy(struct device *dev, phys_addr_t orig_addr, } } +/* + * Bounce: copy the swiotlb buffer from or back to the original dma location + */ +static void swiotlb_bounce(struct device *dev, phys_addr_t tlb_addr, size_t size, + enum dma_data_direction dir) +{ + if (is_swiotlb_fixed(dev->dma_io_tlb_mem, tlb_addr)) + swiotlb_fixed_bounce(dev, tlb_addr, size, dir); + else + swiotlb_dyn_bounce(dev, tlb_addr, size, dir); +} + static inline phys_addr_t slot_addr(phys_addr_t start, phys_addr_t idx) { return start + (idx << IO_TLB_SHIFT); @@ -799,8 +929,13 @@ phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr, return (phys_addr_t)DMA_MAPPING_ERROR; } - tlb_addr = swiotlb_fixed_map(dev, orig_addr, alloc_size, - alloc_align_mask, attrs); + tlb_addr = (phys_addr_t)DMA_MAPPING_ERROR; + if (!is_swiotlb_for_alloc(dev)) + tlb_addr = swiotlb_dyn_map(dev, orig_addr, alloc_size, + alloc_align_mask, dir, attrs); + if (tlb_addr == (phys_addr_t)DMA_MAPPING_ERROR) + tlb_addr = swiotlb_fixed_map(dev, orig_addr, alloc_size, + alloc_align_mask, attrs); if (tlb_addr == (phys_addr_t)DMA_MAPPING_ERROR) { if (!(attrs & DMA_ATTR_NO_WARN)) @@ -882,7 +1017,10 @@ void swiotlb_tbl_unmap_single(struct device *dev, phys_addr_t tlb_addr, (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)) swiotlb_bounce(dev, tlb_addr, mapping_size, DMA_FROM_DEVICE); - swiotlb_release_slots(dev, tlb_addr); + if (is_swiotlb_fixed(dev->dma_io_tlb_mem, tlb_addr)) + swiotlb_release_slots(dev, tlb_addr); + else + swiotlb_dyn_unmap(dev, tlb_addr, dir); } void swiotlb_sync_single_for_device(struct device *dev, phys_addr_t tlb_addr, @@ -1013,7 +1151,7 @@ bool swiotlb_free(struct device *dev, struct page *page, size_t size) { phys_addr_t tlb_addr = page_to_phys(page); - if (!is_swiotlb_buffer(dev, tlb_addr)) + if (!is_swiotlb_fixed(dev->dma_io_tlb_mem, tlb_addr)) return false; swiotlb_release_slots(dev, tlb_addr);