Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp2130052rdh; Tue, 26 Sep 2023 13:28:06 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEq6OjCqQ4aY+JRI0x5c+M0oT5prk/KVVw7rb+2lUHTbZIwiAq8RGfjYiwFjZAmGPPdqNnr X-Received: by 2002:a05:6871:88d:b0:1c7:ebf5:b6cb with SMTP id r13-20020a056871088d00b001c7ebf5b6cbmr111873oaq.25.1695760086479; Tue, 26 Sep 2023 13:28:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695760086; cv=none; d=google.com; s=arc-20160816; b=UdaUBXFIh/WkWq9q9aI9lZZLi8UJ8vzVmXJYtZd6eGKkiQd6HqEhMwjL5auzXMKJjF V+oZ/ozzqa/AG2UsosmsQhqPVHgsQ4vYdnVgqlVYnNjO09JqFc+dnwya4M9lJhhry8gL SEfXfPmmfDE6BvhR8tsJRnkV/j7frE8hFcp16j9MvC9VEZgohfFzqc8cdyN9T2OX4Igb gtt/RGORt8XC24Wsd+f/pRwoqZkuz7jdrFMjWWCbzmXAJUqmbjtp+D8+K/pG9AvNqP4H 9dIZe32Q62Cm3HpsLYegM2nzonYH8rlGdThVLO/jLP7M3FXH3xuoyEg+wjoCAdrE737g Om6w== 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=tjMiRqS4GNU1Qqhncwk811pes4kbwUfVUuQ9R9VcQ5U=; fh=CjaQVTGMcCsV/um84TxZuQPjAzug38KyAugO5Fe0cjE=; b=GIVzINoG9/CWyc1Hvt1UxiegdreMgfoHekQ8WBxjhJqWuPM0wZs1Vfx7xFmq57zdJT XqaO0kvAM7GEOkT3csYBFZVgB7fnbtOKuM0ImwnId8U2/w5iALZXEkmoEVSKtnmz+Kow SAdAhDFMEYEBebauEkUXKjvM/agmne1+tpM6Dp8KOgzKe+neq1IWmOxBWkm9fmeg3DLr rB12o+KtPMYyJWRhTl/PcQ+ylqMFkwuovcYx4b38bhLRozEbgUbzgGIewy9OtzAGp9S6 ngOAJX4fMj9t6lCUgLiiBICjvjPRXwxpw0koi1iwEUrr8IjyAskQIrO4tYkCi8c+egrc 7wqg== 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:6 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 pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id h63-20020a638342000000b00584b7f082bdsi2441801pge.449.2023.09.26.13.28.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 13:28:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (Postfix) with ESMTP id BA8AA8031EA3; Tue, 26 Sep 2023 13:23:54 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235807AbjIZUX4 (ORCPT + 99 others); Tue, 26 Sep 2023 16:23:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46548 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231564AbjIZUXy (ORCPT ); Tue, 26 Sep 2023 16:23:54 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5BCD11D for ; Tue, 26 Sep 2023 13:23:47 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DC976C433CA; Tue, 26 Sep 2023 20:23:45 +0000 (UTC) Date: Tue, 26 Sep 2023 21:23:43 +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 v3] swiotlb: fix the check whether a device has used software IO TLB Message-ID: References: <20230926185556.16433-1-petr@tesarici.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230926185556.16433-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 pete.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 (pete.vger.email [0.0.0.0]); Tue, 26 Sep 2023 13:23:55 -0700 (PDT) On Tue, Sep 26, 2023 at 08:55:56PM +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: > > C swiotlb-new-pool > > (* > * Result: Never > * > * Check that a newly allocated pool is always visible when the > * corresponding swiotlb buffer is visible. > *) > > { > mem_pools = default; > } > > P0(int **mem_pools, int *pool) > { > /* add_mem_pool() */ > WRITE_ONCE(*pool, 999); > rcu_assign_pointer(*mem_pools, pool); > } > > P1(int **mem_pools, int *flag, int *buf) > { > /* swiotlb_find_slots() */ > int *r0; > int r1; > > rcu_read_lock(); > r0 = READ_ONCE(*mem_pools); > r1 = READ_ONCE(*r0); > rcu_read_unlock(); > > if (r1) { > WRITE_ONCE(*flag, 1); > smp_mb(); > } > > /* device driver (presumed) */ > WRITE_ONCE(*buf, r1); > } > > P2(int **mem_pools, int *flag, int *buf) > { > /* device driver (presumed) */ > int r0 = READ_ONCE(*buf); > > /* is_swiotlb_buffer() */ > int r1; > int *r2; > int r3; > > smp_rmb(); > r1 = READ_ONCE(*flag); > if (r1) { > /* swiotlb_find_pool() */ > rcu_read_lock(); > r2 = READ_ONCE(*mem_pools); > r3 = READ_ONCE(*r2); > rcu_read_unlock(); > } > } > > exists (2:r0<>0 /\ 2:r3=0) (* Not found. *) > > 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 I added this for v2, here it is again: Reviewed-by: Catalin Marinas