Received: by 2002:a05:6358:51dd:b0:131:369:b2a3 with SMTP id 29csp299017rwl; Wed, 9 Aug 2023 14:59:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE0OGfNpzUxxU+Q9xktTgP8lHx438xBswyRiU+1ZGsFa7YtkXZvJs++ykj80Fom8LCK6RIX X-Received: by 2002:a05:6808:4:b0:3a4:243d:dc76 with SMTP id u4-20020a056808000400b003a4243ddc76mr603086oic.21.1691618350917; Wed, 09 Aug 2023 14:59:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691618350; cv=none; d=google.com; s=arc-20160816; b=S7w1p2+BOR33vC9YjQWog6Rm289lRCK9YghiNgDtYCeN7D4rCNp9y+giwGyZ48nQuj qHorDrrWjUBlt0/icyOa3zKk+OhjHtKqE61YR9uyxYiBzHPaLA5mY/BbEcUMPqKbpizX iRnHvZzXLHPDKGAgQukLQJ3g5Bi9Pk/YgEe/q9I9Qt0f8/nFv4xEOahivWLomo+1T5v8 Zq1lHGC8hxBPS93PG61+y+jr2o2pXrREESKL4Tb2G32wTGaWAiNeBM2xF1VD9CqjPum2 jrDw3/KtGSKW1oqC0Ju2/8XiWeeT9PrYRqUtIRnIP4ZG67vunhqeA2zgGl9xqK+SBDsZ 5W1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:dkim-signature:dkim-filter; bh=D3OCzS/TjQzlx859sDe4uhCo8ItizHSPamS05kU7M/E=; fh=A/U6xZkTv7WwdkmC1uKturhwGAnSBH2yPGPCnxPOdKo=; b=eZwEVNoaD/+zsMf1WSptPzva7pE+pmGQTBmkqU8B4tABrkZfdj9iB+rtFxEs96KG2P ORtgKgPWZ4dEEdNXju2xncMB1dlxSn68Jfkz0zMpKuFGdJa4716lS6XAgQNNyj5dG3N8 vqed4zn07OHLoF1ZqqS1Q7tx9oPsDBehjZQK7DD4YD2KjV8AM0r2u8r0MJ3dIOAr5CUB 4NFXVVA27Rx8fCqU4m4C+hRAX4doXSDRZxt5RdUswE6FbLre04USgK4ietrb68El+F8L mol1gYFznGcmhAXzIqbg5ZAiqhKXf/A1tjgxkft1oQ9pKivm46PusWF5YLMpv81y+uD5 uIGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lwn.net header.s=20201203 header.b=NXwT7fNZ; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u69-20020a638548000000b0055bbc74626esi145610pgd.217.2023.08.09.14.58.59; Wed, 09 Aug 2023 14:59:10 -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; dkim=pass header.i=@lwn.net header.s=20201203 header.b=NXwT7fNZ; 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 S231872AbjHIVUr (ORCPT + 99 others); Wed, 9 Aug 2023 17:20:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230501AbjHIVUq (ORCPT ); Wed, 9 Aug 2023 17:20:46 -0400 Received: from ms.lwn.net (ms.lwn.net [45.79.88.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1A5551724; Wed, 9 Aug 2023 14:20:45 -0700 (PDT) Received: from localhost (unknown [IPv6:2601:281:8300:73::5f6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ms.lwn.net (Postfix) with ESMTPSA id 5DF4E218; Wed, 9 Aug 2023 21:20:44 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 ms.lwn.net 5DF4E218 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lwn.net; s=20201203; t=1691616044; bh=D3OCzS/TjQzlx859sDe4uhCo8ItizHSPamS05kU7M/E=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=NXwT7fNZt30FAE6/rsWNAIxKfZAnHdE0QNzpPAfTcuuLXJv/8sSeNpRi/YYa5NjJC JPc/pZIj2m1/iKfmCA6z6g2y88sFf4ZH/bH/4XDB2qcRK5jWAwzLmabxM5iXzuAYwc gdH4oIFqQIHil75Is17wCUwIlXkrwODoldc2N5n5BFNAkaN5JRNZKvsISIklyJvP2F 4+b3s2bgvKX3Qdk1NsvqoUWqBFM5kFpduA+bStZQdIeCWxvmzzb6PeCz24WoTHyNsu hckvkWe4mo+JyUGpbBEyCK3lFgQYmdeewhQkWR1ZSjYdvGexm2T+wCrOys5KDkkoTp DAFJ4ZWqDCc4Q== From: Jonathan Corbet To: Petr Tesarik , Stefano Stabellini , Russell King , Thomas Bogendoerfer , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)" , "H. Peter Anvin" , Greg Kroah-Hartman , "Rafael J. Wysocki" , Juergen Gross , Oleksandr Tyshchenko , Christoph Hellwig , Marek Szyprowski , Robin Murphy , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Vlastimil Babka , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Petr Tesarik , Andy Shevchenko , Hans de Goede , James Seo , James Clark , Kees Cook , "moderated list:XEN HYPERVISOR ARM" , "moderated list:ARM PORT" , open list , "open list:MIPS" , "open list:XEN SWIOTLB SUBSYSTEM" , "open list:SLAB ALLOCATOR" Cc: Roberto Sassu , petr@tesarici.cz Subject: Re: [PATCH v7 9/9] swiotlb: search the software IO TLB only if the device makes use of it In-Reply-To: References: Date: Wed, 09 Aug 2023 15:20:43 -0600 Message-ID: <87a5uz3ob8.fsf@meer.lwn.net> MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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: X-Mailing-List: linux-kernel@vger.kernel.org Petr Tesarik writes: > From: Petr Tesarik > > Skip searching the software IO TLB if a device has never used it, making > sure these devices are not affected by the introduction of multiple IO TLB > memory pools. > > Additional memory barrier is required to ensure that the new value of the > flag is visible to other CPUs after mapping a new bounce buffer. For > efficiency, the flag check should be inlined, and then the memory barrier > must be moved to is_swiotlb_buffer(). However, it can replace the existing > barrier in swiotlb_find_pool(), because all callers use is_swiotlb_buffer() > first to verify that the buffer address belongs to the software IO TLB. > > Signed-off-by: Petr Tesarik > --- Excuse me if this is a silly question, but I'm not able to figure it out on my own... > include/linux/device.h | 2 ++ > include/linux/swiotlb.h | 7 ++++++- > kernel/dma/swiotlb.c | 14 ++++++-------- > 3 files changed, 14 insertions(+), 9 deletions(-) > > diff --git a/include/linux/device.h b/include/linux/device.h > index 5fd89c9d005c..6fc808d22bfd 100644 > --- a/include/linux/device.h > +++ b/include/linux/device.h > @@ -628,6 +628,7 @@ struct device_physical_location { > * @dma_io_tlb_mem: Software IO TLB allocator. Not for driver use. > * @dma_io_tlb_pools: List of transient swiotlb memory pools. > * @dma_io_tlb_lock: Protects changes to the list of active pools. > + * @dma_uses_io_tlb: %true if device has used the software IO TLB. > * @archdata: For arch-specific additions. > * @of_node: Associated device tree node. > * @fwnode: Associated device node supplied by platform firmware. > @@ -737,6 +738,7 @@ struct device { > #ifdef CONFIG_SWIOTLB_DYNAMIC > struct list_head dma_io_tlb_pools; > spinlock_t dma_io_tlb_lock; > + bool dma_uses_io_tlb; You add this new member here, fine... > #endif > /* arch specific additions */ > struct dev_archdata archdata; > diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h > index 8371c92a0271..b4536626f8ff 100644 > --- a/include/linux/swiotlb.h > +++ b/include/linux/swiotlb.h > @@ -172,8 +172,13 @@ static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr) > if (!mem) > return false; > > - if (IS_ENABLED(CONFIG_SWIOTLB_DYNAMIC)) > + if (IS_ENABLED(CONFIG_SWIOTLB_DYNAMIC)) { > + /* Pairs with smp_wmb() in swiotlb_find_slots() and > + * swiotlb_dyn_alloc(), which modify the RCU lists. > + */ > + smp_rmb(); > return swiotlb_find_pool(dev, paddr); > + } > return paddr >= mem->defpool.start && paddr < mem->defpool.end; > } > > diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c > index adf80dec42d7..d7eac84f975b 100644 > --- a/kernel/dma/swiotlb.c > +++ b/kernel/dma/swiotlb.c > @@ -730,7 +730,7 @@ static void swiotlb_dyn_alloc(struct work_struct *work) > > add_mem_pool(mem, pool); > > - /* Pairs with smp_rmb() in swiotlb_find_pool(). */ > + /* Pairs with smp_rmb() in is_swiotlb_buffer(). */ > smp_wmb(); > } > > @@ -764,11 +764,6 @@ struct io_tlb_pool *swiotlb_find_pool(struct device *dev, phys_addr_t paddr) > struct io_tlb_mem *mem = dev->dma_io_tlb_mem; > struct io_tlb_pool *pool; > > - /* Pairs with smp_wmb() in swiotlb_find_slots() and > - * swiotlb_dyn_alloc(), which modify the RCU lists. > - */ > - smp_rmb(); > - > rcu_read_lock(); > list_for_each_entry_rcu(pool, &mem->pools, node) { > if (paddr >= pool->start && paddr < pool->end) > @@ -813,6 +808,7 @@ void swiotlb_dev_init(struct device *dev) > #ifdef CONFIG_SWIOTLB_DYNAMIC > INIT_LIST_HEAD(&dev->dma_io_tlb_pools); > spin_lock_init(&dev->dma_io_tlb_lock); > + dev->dma_uses_io_tlb = false; ...here you initialize it, fine... > #endif > } > > @@ -1157,9 +1153,11 @@ static int swiotlb_find_slots(struct device *dev, phys_addr_t orig_addr, > list_add_rcu(&pool->node, &dev->dma_io_tlb_pools); > spin_unlock_irqrestore(&dev->dma_io_tlb_lock, flags); > > - /* Pairs with smp_rmb() in swiotlb_find_pool(). */ > - smp_wmb(); > found: > + dev->dma_uses_io_tlb = true; > + /* Pairs with smp_rmb() in is_swiotlb_buffer() */ > + smp_wmb(); > + ...and here you set it if swiotlb is used. But, as far as I can tell, you don't actually *use* this field anywhere. What am I missing? Thanks, jon