Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp2070921rdh; Tue, 26 Sep 2023 11:22:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHGIfwtwkH2UiC9NqcP3Xu6rZTi/GS5waJnqkZCuWu+tjMvYLaYmcVZbWERzHi856y2AtB3 X-Received: by 2002:a17:902:6b88:b0:1c5:e5de:debd with SMTP id p8-20020a1709026b8800b001c5e5dedebdmr6624077plk.16.1695752545612; Tue, 26 Sep 2023 11:22:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695752545; cv=none; d=google.com; s=arc-20160816; b=g5pTAFekjTj/HWttKXAGDxnlrJJIGMh4H0mxO6/xr98asRs2HjCLxWrXYbyve6plpZ r3dO2T1SijGHw6cbtDoPcThwCrgJzIFV04HD5kGnQUlgilmuPEAaC+KSIqdS0Wr8DPSu BGq+OOix0LnqhKV4Uo7skZ6/jT0UwFph2XozqOn/QBlcdtSW+aP8/54tw0FuI+zweTIC cb+r3cd/A6oVz2vn4SMC02ghPEavSVOSPDuuZOdf+SW/i14hNG2e2fyRu9EK9mMtAJxs 6lh07IhQordEOYo5bkViEiVU9jxzTwb8NO+R4TH9RQgpadpTr1D9x5ETE+nN/FvUGYHl gRwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=cpyhm0OWoxTt2kSnV4u4I9giBVzc2RbLiLtx4Lbfw/8=; fh=CjaQVTGMcCsV/um84TxZuQPjAzug38KyAugO5Fe0cjE=; b=zS0iH/kwTKHIyCwM6VeOeAifk81Xu6xr1ZQwBQ6v3mW3Gvqiks5cPs/FqAPk+Q5RxR 8QPhcspIdtofy072bnH6Rjq8e8y+AeiCY21rZDhvDpvCbnugMukffNOhnfAzBb7mqkKS sJ6IenOWMdx5pU1RIP3sX6Zqw1qxhnBCO4lktXT/MBP7DPVs9azaf8iua6gqiZsowJbp G8RLHy/VVS8/MSs7skWm2GjXQ1waYHbgAUeMiStLHJAaLXTzQNSmG8wxX1mf38QRfgTG LZeQKzYGQKa0NddWnT/3accFOFT/L53CdURoBJXm7TjhbVSfhkSfAOU32V+69RrC3IvT zguw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id t11-20020a1709027fcb00b001bc17ab8d6esi6504959plb.530.2023.09.26.11.22.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 11:22:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 2878480212C0; Tue, 26 Sep 2023 11:17:51 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229674AbjIZSRm (ORCPT + 99 others); Tue, 26 Sep 2023 14:17:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34580 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229519AbjIZSRl (ORCPT ); Tue, 26 Sep 2023 14:17:41 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 45B09FC for ; Tue, 26 Sep 2023 11:17:34 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1BE77C433C9; Tue, 26 Sep 2023 18:17:31 +0000 (UTC) Date: Tue, 26 Sep 2023 19:17:29 +0100 From: Catalin Marinas To: Petr Tesarik Cc: Christoph Hellwig , Marek Szyprowski , Robin Murphy , "open list:DMA MAPPING HELPERS" , open list , Roberto Sassu , Jonathan Corbet Subject: Re: [PATCH v2] swiotlb: fix the check whether a device has used software IO TLB Message-ID: References: <20230926162339.12940-1-petr@tesarici.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230926162339.12940-1-petr@tesarici.cz> X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 26 Sep 2023 11:17:51 -0700 (PDT) On Tue, Sep 26, 2023 at 06:23:39PM +0200, Petr Tesarik wrote: > When CONFIG_SWIOTLB_DYNAMIC=y, devices which do not use the software IO TLB > can avoid swiotlb lookup. A flag is added by commit 1395706a1490 ("swiotlb: > search the software IO TLB only if the device makes use of it"), the flag > is correctly set, but it is then never checked. Add the actual check here. > > Note that this code is an alternative to the default pool check, not an > additional check, because: > > 1. swiotlb_find_pool() also searches the default pool; > 2. if dma_uses_io_tlb is false, the default swiotlb pool is not used. > > Tested in a KVM guest against a QEMU RAM-backed SATA disk over virtio and > *not* using software IO TLB, this patch increases IOPS by approx 2% for > 4-way parallel I/O. > > The write memory barrier in swiotlb_dyn_alloc() is not needed, because a > newly allocated pool must always be observed by swiotlb_find_slots() before > an address from that pool is passed to is_swiotlb_buffer(). > > Correctness was verified using the following litmus test: [...] > Fixes: 1395706a1490 ("swiotlb: search the software IO TLB only if the device makes use of it") > Reported-by: Jonathan Corbet > Closes: https://lore.kernel.org/linux-iommu/87a5uz3ob8.fsf@meer.lwn.net/ > Signed-off-by: Petr Tesarik Thanks for the update. Reviewed-by: Catalin Marinas > diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h > index b4536626f8ff..93b400d9be91 100644 > --- a/include/linux/swiotlb.h > +++ b/include/linux/swiotlb.h > @@ -172,14 +172,22 @@ static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr) > if (!mem) > return false; > > - 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); > - } > +#ifdef CONFIG_SWIOTLB_DYNAMIC > + /* All SWIOTLB buffer addresses must have been returned by > + * swiotlb_tbl_map_single() and passed to a device driver. > + * If a SWIOTLB address is checked on another CPU, then it was > + * presumably loaded by the device driver from an unspecified private > + * data structure. Make sure that this load is ordered before reading > + * dev->dma_uses_io_tlb here and mem->pools in swiotlb_find_pool(). > + * > + * This barrier pairs with smp_mb() in swiotlb_find_slots(). > + */ Nitpick. The official multi-line comment style is: /* * Text. */ i.e. it starts with an empty /* line. > @@ -1152,9 +1149,25 @@ static int swiotlb_find_slots(struct device *dev, phys_addr_t orig_addr, > spin_unlock_irqrestore(&dev->dma_io_tlb_lock, flags); > > found: > - dev->dma_uses_io_tlb = true; > - /* Pairs with smp_rmb() in is_swiotlb_buffer() */ > - smp_wmb(); > + WRITE_ONCE(dev->dma_uses_io_tlb, true); > + > + /* The general barrier orders reads and writes against a presumed store > + * of the SWIOTLB buffer address by a device driver (to a driver private > + * data structure). It serves two purposes. Same here. -- Catalin