Received: by 2002:a05:7208:13ce:b0:7f:395a:35b6 with SMTP id r14csp1276870rbe; Fri, 1 Mar 2024 09:10:12 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWI8PIL5tL1HcVTVgINLAE1B5kW4L7/PJ50mljKH5ptkBWO0hU17O8Shi4OFnvwXW7cgGEKwe1GaM/KCKKrTRwKwOxGuaf0lUdUeXqwHw== X-Google-Smtp-Source: AGHT+IEE6NVwEmioGGrSIOFVIxI8/iZ6n6GhnuBRozrqajRtonhXDAfhTQ4QWTOMBkblZykTozWn X-Received: by 2002:ac8:5802:0:b0:42e:bdc1:aaaf with SMTP id g2-20020ac85802000000b0042ebdc1aaafmr2385604qtg.20.1709313012246; Fri, 01 Mar 2024 09:10:12 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709313012; cv=pass; d=google.com; s=arc-20160816; b=DhgYTE8W9Ue5FKHPDF83N4jOBbZNqDIUftC9UkipahVf+SqBqyzHcHyIbnbjbsUVOJ H3/64s3Ljg5WdchLz78mZdtCnym+UoPzZ83pu3WEQPmrKKeESasOAP+K1S9V6xA6v6cH o/FjBsofIG9pr24w/4Xu73vpViYpkI32J3ktjrlJ18h62FI1nYYOwrIm5yF/Ikxtajsp go9W9VvXvBJ69uiH696Zt3LiMtGySHYZddtWDG5bb5Jd08dvGlSWUzlWuYkAjFIrtiDB IILF5fu/uq1SKjM5p+npCG05ONtuu6uYE2JgL23A46teUCOTdkYYZ9V7Ftv0+vNgd7ZM pckg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :subject:cc:to:from:date:dkim-signature; bh=fKBJWgCSyxi8x9tBCvENvmDDLlpNob1Z0JntFTlnozA=; fh=TV6y3XhubkLCuMGIlcFwfDVoHGUXr9HJv/0e49WKdfo=; b=l0X1MuxhOtayUW8RS6RK8oAlMXOwHgG+9dlj1oJezaOpPxUTrwgGxbIKGtH1ysM4/8 3a8tRzvxoPZnfK3vuhZTs1Ie+wmzowy+crnI63mup6gqOaq9l9W8hOE5ZDPgL7/RrhRj Pvt+0T9y/F/Izale60PujeXs74lagNtA8NpkGyy8l2h87fcdi3FFWSPej8BZWC9ImZg2 b0LCMSU6d4VvObnlK6syCUyjkOrSvi5zzRzalQ3XgflG/GWdAe91nvdZdsItqvPkProP X4nPfElQ1PFkkaNskvPrtC0bu1gOjDpkd45l2LVTO71ko00JoEuyjOmP7wYIMuLWWYqs X75g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@tesarici.cz header.s=mail header.b=kKeq0RwH; arc=pass (i=1 spf=pass spfdomain=tesarici.cz dkim=pass dkdomain=tesarici.cz dmarc=pass fromdomain=tesarici.cz); spf=pass (google.com: domain of linux-kernel+bounces-88805-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88805-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=tesarici.cz Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id h10-20020ac846ca000000b0042dedf3dc9fsi3703802qto.434.2024.03.01.09.10.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 09:10:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-88805-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@tesarici.cz header.s=mail header.b=kKeq0RwH; arc=pass (i=1 spf=pass spfdomain=tesarici.cz dkim=pass dkdomain=tesarici.cz dmarc=pass fromdomain=tesarici.cz); spf=pass (google.com: domain of linux-kernel+bounces-88805-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88805-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=tesarici.cz Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 1D4851C250A3 for ; Fri, 1 Mar 2024 17:09:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2F16C848D; Fri, 1 Mar 2024 17:09:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=tesarici.cz header.i=@tesarici.cz header.b="kKeq0RwH" Received: from bee.tesarici.cz (unknown [77.93.223.253]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D4B271097B for ; Fri, 1 Mar 2024 17:08:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=77.93.223.253 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709312940; cv=none; b=ifx+J5xOrQq69NQ9WQanj3qds65N24CUpwscAYqGoZFiK5EUVf9SrRZMBr84aF9ft2OOJRNw38TXo5fgPWGtdzBUncE+5De8GtsR628n4RJZaWPusbAX71KiX6rEw/U/h2KnSReRRe4x7/rgeFUHH400Ij8lgOF4TG6r4e2wSIM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709312940; c=relaxed/simple; bh=Wfe9zGhPuffkvAN8I3WeLl9RfnM0vmZnVhlcI423hZo=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=RMIekXC2gZPbmoB455N6vK0oyLuCSzacP4+ql/nqx9hUJCRzCBG+qnsBo489nTKthGTj0B0F90oVqVXIq3JU6RZGw1ctHunbAy9L6sMUD1FAQr/lwOEBvFbEYLPbi8jEAr0VFxtmEUxrSXe+SqbNUpHIYLLrKJr+5JrZZvz2cvg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=tesarici.cz; spf=pass smtp.mailfrom=tesarici.cz; dkim=pass (2048-bit key) header.d=tesarici.cz header.i=@tesarici.cz header.b=kKeq0RwH; arc=none smtp.client-ip=77.93.223.253 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=tesarici.cz Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tesarici.cz 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 B40601C3920; Fri, 1 Mar 2024 18:08:54 +0100 (CET) Authentication-Results: mail.tesarici.cz; dmarc=fail (p=quarantine dis=none) header.from=tesarici.cz DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tesarici.cz; s=mail; t=1709312934; bh=fKBJWgCSyxi8x9tBCvENvmDDLlpNob1Z0JntFTlnozA=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=kKeq0RwHgtDgyqbxhsaNIScOvBXOWNfdJNgSIifu8MgdBdQL3V6nRIe70/Zox+pqT CBCDG9jr+eg/fPQZd0XxtrV/bJUWnv5HIcCs6mUBGrzD0sozJXJxJpPIuuHkuMLuDU D1tmeJU1ngvBuC91/RhfUZ3yhFPHWs8pGw13b5PNu95Oysb3VuIE5gCfrAhfalIUHH V7LwnXyZ+pJeYveqYZV3+BYwrAZdHqLunhiQasXanZm3/a5uIUQAILwHeks+BgLk17 vBrZ/6e9ogpWzPCX7ro1271HViBhzc9Oam0k1TKoWWx3UyDu4STLQ8UBwbYV2e6hnI YGfOKMj5++t+A== Date: Fri, 1 Mar 2024 18:08:53 +0100 From: Petr =?UTF-8?B?VGVzYcWZw61r?= To: Christoph Hellwig Cc: Michael Kelley , Will Deacon , "linux-kernel@vger.kernel.org" , Petr Tesarik , "kernel-team@android.com" , "iommu@lists.linux.dev" , Marek Szyprowski , Robin Murphy , Dexuan Cui , Nicolin Chen Subject: Re: [PATCH v5 6/6] swiotlb: Remove pointless stride adjustment for allocations >= PAGE_SIZE Message-ID: <20240301180853.5ac20b27@meshulam.tesarici.cz> In-Reply-To: <20240301163927.18358ee2@meshulam.tesarici.cz> References: <20240228133930.15400-1-will@kernel.org> <20240228133930.15400-7-will@kernel.org> <20240229133346.GA7177@lst.de> <20240229154756.GA10137@lst.de> <20240301163927.18358ee2@meshulam.tesarici.cz> X-Mailer: Claws Mail 4.2.0 (GTK 3.24.39; x86_64-suse-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Fri, 1 Mar 2024 16:39:27 +0100 Petr Tesa=C5=99=C3=ADk wrote: > On Thu, 29 Feb 2024 16:47:56 +0100 > Christoph Hellwig wrote: >=20 > > On Thu, Feb 29, 2024 at 03:44:11PM +0000, Michael Kelley wrote: =20 > > > Any thoughts on how that historical behavior should apply if > > > the DMA min_align_mask is non-zero, or the alloc_align_mask > > > parameter to swiotbl_tbl_map_single() is non-zero? As currently > > > used, alloc_align_mask is page aligned if the IOMMU granule is =20 > > > >=3D PAGE_SIZE. But a non-zero min_align_mask could mandate =20 > > > returning a buffer that is not page aligned. Perhaps do the > > > historical behavior only if alloc_align_mask and min_align_mask > > > are both zero? =20 > >=20 > > I think the driver setting min_align_mask is a clear indicator > > that the driver requested a specific alignment and the defaults > > don't apply. For swiotbl_tbl_map_single as used by dma-iommu > > I'd have to tak a closer look at how it is used. =20 >=20 > I'm not sure it helps in this discussion, but let me dive into a bit > of ancient history to understand how we ended up here. >=20 > IIRC this behaviour was originally motivated by limitations of PC AT > hardware. Intel 8237 is a 16-bit DMA controller. To make it somehow > usable with addresses up to 16MB (yeah, the infamous DMA zone), IBM > added a page register, but it was on a separate chip and it did not > increment when the 8237 address rolled over back to zero. Effectively, > the page register selected a 64K-aligned window of 64K buffers. > Consequently, DMA buffers could not cross a 64K physical boundary. >=20 > Thanks to how the buddy allocator works, the 64K-boundary constraint > was satisfied by allocation size, and drivers took advantage of it when > allocating device buffers. IMO software bounce buffers simply followed > the same logic that worked for buffers allocated by the buddy allocator. >=20 > OTOH min_align_mask was motivated by NVME which prescribes the value of > a certain number of low bits in the DMA address (for simplicity assumed > to be identical with the same bits in the physical address). >=20 > The only pre-existing user of alloc_align_mask is x86 IOMMU code, and > IIUC it is used to guarantee that unaligned transactions do not share > the IOMMU granule with another device. This whole thing is weird, > because swiotlb_tbl_map_single() is called like this: >=20 > aligned_size =3D iova_align(iovad, size); > phys =3D swiotlb_tbl_map_single(dev, phys, size, aligned_= size, > iova_mask(iovad), dir, attr= s); >=20 > Here: >=20 > * alloc_size =3D iova_align(iovad, size) > * alloc_align_mask =3D iova_mask(iovad) >=20 > Now, iova_align() rounds up its argument to a multiple of iova granule > and iova_mask() is simply "granule - 1". This works, because granule > size must be a power of 2, and I assume it must also be >=3D PAGE_SIZE. >=20 > In that case, the alloc_align_mask argument is not even needed if you > adjust the code to match documentation---the resulting buffer will be > aligned to a granule boundary by virtue of having a size that is a > multiple of the granule size. >=20 > To sum it up: >=20 > 1. min_align_mask is by far the most important constraint. Devices will > simply stop working if it is not met. > 2. Alignment to the smallest PAGE_SIZE order which is greater than or > equal to the requested size has been documented, and some drivers > may rely on it. > 3. alloc_align_mask is a misguided fix for a bug in the above. >=20 > Correct me if anything of the above is wrong. I thought about it some more, and I believe I know what should happen if the first two constraints appear to be mutually exclusive. First, the alignment based on size does not guarantee that the resulting physical address is aligned. In fact, the lowest IO_TLB_SHIFT bits must be always identical to the original buffer address. Let's take an example request like this: TLB_SIZE =3D 0x00000800 min_align_mask =3D 0x0000ffff orig_addr =3D 0x....1234 alloc_size =3D 0x00002800 Minimum alignment mask requires to keep the 1234 at the end. Allocation size requires a buffer that is aligned to 16K. Of course, there is no 16K-aligned slot with slot_address & 0x7ff =3D=3D 0x200, but if IO_TLB_SHIFT was 14, it would be slot_address & 0x3fff =3D=3D 0 (low IO_TLB_SHIFT are masked off). Since the SWIOTLB API does not guarantee any specific value of IO_TLB_SHIFT, callers cannot rely on it. That means 0x1234 is a perfectly valid bounce buffer address for this example. The caller may rightfully expect that the 16K granule containing the bounce buffer is not shared with any other user. For the above case I suggest to increase the allocation size to 0x4000 already in swiotlb_tbl_map_single() and treat 0x1234 as the offset from the slot address. Will, do you want to incorporate it into your patch series, or should I send the corresponding patch? Petr T