Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp744358rdf; Fri, 3 Nov 2023 13:51:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG/D7D7WbcctPIthZnMevHye+PgYz8KrfmfKdJOYR2Sxmyx2SHM7T+N2mh23SBCZTYPqG8a X-Received: by 2002:a17:90a:f48d:b0:280:46ac:be71 with SMTP id bx13-20020a17090af48d00b0028046acbe71mr15575110pjb.15.1699044705111; Fri, 03 Nov 2023 13:51:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1699044705; cv=none; d=google.com; s=arc-20160816; b=QJaC8M4fbhQhm4ohlSS6Rg8ZB/tj9uB7n9ydb8A1S9tnqtF9rz1gFreo/Yk9pk36jM 5Flis11ZCzHMPfgUoEamvvpV0EdDE3w1Pi88H45GIxINljHgHw8T82vN303AP3ykVkDj j1M+V3xv+zjMK7AAC/6iOFbfY65B08D0Xb1rh6zXvsK99Oe1SLjL0nMCJbCwBKp7B1Cj 9sd82yBO5obYT6LSDdU9Lvri2hDDgw6q7XhKxcvKv1b0JCPq7UsNWNgs/zMvPAYKe7vW MuJ3bqXuw9mlQQvhFLHBeeIikW7js7e+KsK2ohuhop93phSlA3XysOWQu5PvpNPzP5/q aqUg== 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:subject:cc:to:from:date :dkim-signature; bh=vmW7KATCKETFRN9nqtnJ6uoKr1tUjWCqR3cpjzNf1Ls=; fh=LdLpYcpZE03JD4YcNxb9Dw9iOLXUDgLWv94pAmCFIew=; b=th90MjhofwxKrtkb9CsvGAG1VchbbmjV167ilynCxtuToQM3PTYe90W7GvapV+riHg /NCL/mdVSLpS3Jyp15bKFa70w0Sh7zv6F7x82oGa31qe672+olwqlXWAVWji/DqWG/R8 hmqMjMpEaRLYvdjKraWZV4obM0mjiVrDtogErSqDcby9nyVcx8tm/OVcZXvRk6/GzkDx ENWH6zzngnRVakkqibtMDBrkfqoCwbDs+GWCUT9wPqtWyK7OiReG7sS4Ou5gtW2hfa6X LIrJ0rZvEZIP4KRR9VAiVVyYmKrOwD/c5HjrmqibcLrMCdPToy1sWT5d4s+80wCholKy YDLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@tesarici.cz header.s=mail header.b=OKDh8aDX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=tesarici.cz Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id c3-20020a17090abf0300b00274d3f62044si2252489pjs.111.2023.11.03.13.51.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 13:51:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@tesarici.cz header.s=mail header.b=OKDh8aDX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=tesarici.cz Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id A4FF28239126; Fri, 3 Nov 2023 13:51:42 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230076AbjKCUvC (ORCPT + 99 others); Fri, 3 Nov 2023 16:51:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54354 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229484AbjKCUvB (ORCPT ); Fri, 3 Nov 2023 16:51:01 -0400 Received: from bee.tesarici.cz (bee.tesarici.cz [IPv6:2a03:3b40:fe:2d4::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7BB001BF; Fri, 3 Nov 2023 13:50:57 -0700 (PDT) Received: from meshulam.tesarici.cz (dynamic-2a00-1028-83b8-1e7a-4427-cc85-6706-c595.ipv6.o2.cz [IPv6:2a00:1028:83b8:1e7a:4427:cc85:6706:c595]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bee.tesarici.cz (Postfix) with ESMTPSA id 89C841953CF; Fri, 3 Nov 2023 21:50:54 +0100 (CET) Authentication-Results: mail.tesarici.cz; dmarc=fail (p=none dis=none) header.from=tesarici.cz DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tesarici.cz; s=mail; t=1699044654; bh=ovF2UxeFKGVoKgOj3oQMqaLMJ/vL6MISuASAGJ+7cOo=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=OKDh8aDXW5UlBWkhlrSF+eWWUCy4nesCqv1sSWA8SKz3LtzRyonOKo29z948jxFn8 IP0OAsvNwuKaro8+qHqn3sTG3glqpExjeHguIxFqapE/hWSGLaPHOThV5ofhLMM0N4 ZD4zVycNv0+4kwjtBNZmagUDOJJHW23CCP+axUGAz5o8UxXHXTcCyuukm0Fz51SMtI pvyIjLrJn+VAqScqSvK6YCeZIkP0+YStxsNx/lanF4fG2gGB7t+0IAFt3pXSJU86+v db4SKPxpo1B9rsrQpHgQMeMk08n9RVtjGPdgHGlERFIC+X5JS4KT1fq0w0xs1so5K3 mFANpzOhAXKtw== Date: Fri, 3 Nov 2023 21:50:53 +0100 From: Petr =?UTF-8?B?VGVzYcWZw61r?= To: Halil Pasic Cc: Niklas Schnelle , Christoph Hellwig , Bjorn Helgaas , Marek Szyprowski , Robin Murphy , Petr Tesarik , Ross Lagerwall , linux-pci , linux-kernel@vger.kernel.org, iommu@lists.linux.dev, Matthew Rosato Subject: Re: Memory corruption with CONFIG_SWIOTLB_DYNAMIC=y Message-ID: <20231103214831.26d29f4d@meshulam.tesarici.cz> In-Reply-To: <20231103171447.02759771.pasic@linux.ibm.com> References: <104a8c8fedffd1ff8a2890983e2ec1c26bff6810.camel@linux.ibm.com> <20231103171447.02759771.pasic@linux.ibm.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; x86_64-suse-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.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 (agentk.vger.email [0.0.0.0]); Fri, 03 Nov 2023 13:51:42 -0700 (PDT) On Fri, 3 Nov 2023 17:14:47 +0100 Halil Pasic wrote: >[...] > In our case min_align_mask == 0 and a) is thus not applicable, because b) and > c) we end up with iotlb_align_mask = 0x800. And because orig_add & 0x800 == > 0x800 but pool->start & 0x800 == 0 and the slot at index i is skipped over. The > slot 0 is skipped over because it is page aligned, when !!((1UL << PAGE_SHIFT) > & orig_addr) Wait. These mask values can quickly become confusing. Do you mean iotlb_align_mask == 0xfff? > Let us note that with the current implementation the min_align_size mask, that > is mechanism a) also controls the tlb_addr within the first slot so that: > tlb_addr & min_align_mask == orig_addr & min_align_mask. In that sense a) is > very unlike b) and c). It is silently assumed that PAGE_SIZE >= IO_TLB_SIZE, so if the buffer is page-aligned, the lower bits of the alignment inside the io tlb slot must be zero. If the same assumption is made about alloc_align_mask, it should be documented, but it is not. >[...] > In our opinion the first step towards getting this right is to figure out what > the different kinds of alignments are really supposed to mean. For each of the > mechanisms we need to understand and document, whether making sure that the > bounce buffer does not stretch over more of certain units of memory (like, > pages, iova granule size, whatever), or is it about preserving offset within a > certain unit of memory, and if yes to what extent (the least significant n-bits > of the orig_addr dictated by the respective mask, or something different). Seconded. I have also been struggling with the various alignment constraints. I have even written (but not yet submitted) a patch to calculate the combined alignment mask in swiotlb_tbl_map_single() and pass it down to all other functions, just to make it clear what alignment mask is used. My understanding is that buffer alignment may be required by: 1. hardware which cannot handle an unaligned base address (presumably because the chip performs a simple OR operation to get the addresses of individual fields); 2. isolation of untrusted devices, where no two bounce buffers should end up in the same iova granule; 3. allocation size; I could not find an explanation, so this might be merely an attempt at reducing SWIOTLB internal fragmentation. I hope other people on the Cc list can shed more light on the intended behaviour. Petr T