Received: by 2002:ab2:2994:0:b0:1ef:ca3e:3cd5 with SMTP id n20csp853139lqb; Fri, 15 Mar 2024 08:09:46 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVOR/6zwMtERQPE7VKUaz54trrCWtYLTnjHGmV3n+YXcLDqrOk+fwkxu8fDY24VQNg1ymD7NKHlZDlltR1YkrDBjGI8bOi+oxFf4bMJIg== X-Google-Smtp-Source: AGHT+IGoGU9W8tD+TCt9Al8smFDul150ps3Zjp7SRBWjjqAIiBpTWP/LYxReH1T0cvzqYMaYqmcA X-Received: by 2002:a05:6a20:3d81:b0:1a3:b38:de7d with SMTP id s1-20020a056a203d8100b001a30b38de7dmr5578090pzi.16.1710515386298; Fri, 15 Mar 2024 08:09:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710515386; cv=pass; d=google.com; s=arc-20160816; b=lpr3oOdxQJx9nXxSCNYPvoe4jodCEREVxJ9jyoG2RxcFtNhsMMVC9xcDGpj3F6HEWf Zb8R90mOPAtdPtqTeGkCrzqOkJHjqLCMk1j5qgGROwxOPUNUyCwdgN1L6b2stV5Ikpxw C+12Ya2lrewpMWr4RL34CZnlyHqE7fZJnUaKZTuHmvSjzxAd7R3Ie9zzVd6/4ztjBSmY xkSWVe1R2/1fbloldSpw1HwZ5L1yXfch5oal4/hrnWivtFb3InvsvI8FBIGDl+sfd5Jm e1pjPmSUoTNgVm8R5IBBmLnQRzMk8G8Xnl4USjohW9J9whe/dkvmma55J68LYNxrwFMw SdYQ== 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=6T0BIzOuPE5LuqB1wHjUl+/fIgb/2kuGQnVeMY/YX2w=; fh=oz6LvlMHmNnSOu1PIDsm3dm+gsDr+QPYGdgMlttm8eY=; b=FuBZERs66uZdULy/F5THDb89dTW8oRyZOUMWpjONXG0bBogUowZm1pXCCdRFr84RNR CWYBsN7SsPPO9DSrrSicnOumFOp6pE7/Ooy15h4wi03qmXatWTWIXo/qqsPsWN9XbSRA UOYnBxPYEYiiZQ/fWL/PVi9qolY841cCQlvLXgufIqa0A5cgCOl7lXcaexUdLZVbYw3x 2Rhoi6uCdjtjp9hiFPjovMVn7mClNXYhLyvOMm6/mPFRwYHdJJwwBd9naP96Ki0gayOw gzUMgjNZIH4emRDKA5gv3ydt1TopfFHcr40j0hdNY6R8vXUBcsqyjRopvKP5CRM8C8CT S9Aw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@tesarici.cz header.s=mail header.b=vO5BaKxl; 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-104558-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-104558-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=tesarici.cz Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id s4-20020a056a00178400b006e7023e9bbfsi416619pfg.275.2024.03.15.08.09.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Mar 2024 08:09:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-104558-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@tesarici.cz header.s=mail header.b=vO5BaKxl; 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-104558-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-104558-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id EEF5A284A04 for ; Fri, 15 Mar 2024 15:09:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B27D53D0A8; Fri, 15 Mar 2024 15:09:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=tesarici.cz header.i=@tesarici.cz header.b="vO5BaKxl" Received: from bee.tesarici.cz (bee.tesarici.cz [37.205.15.56]) (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 CCB213BBCD for ; Fri, 15 Mar 2024 15:09:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=37.205.15.56 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710515381; cv=none; b=CMXmk3XRHKUP40wFsrATSTzv7CdS2jv+dkZjZUJbe75vyb/qC4cU1IsA3wEBq/0j32BqmW9HbBV9cxk0bm1RTTHn53fM2PZWuaqk50R2zUUrFBeGm2zTZga5C5ZnQBvXlucAJVISpp0BYiwyvpzVzuuO1Hj048ZYDEohEDYtAlo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710515381; c=relaxed/simple; bh=uysj4L8dszCTthjgDSnBVbbbyoveIsByuEO6DJzqfWE=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=YT8hkpCurp35FrYn4kt8RRXM1B4hsXHv/SmYMF2kP3FsnAnAX/Zra9LJ6KuoKZv5z/++mzQnckiXJjGeTdt4uQ4l/fCVJXWDUzM1AYeDROoVwqIqVMX9PIcdoNTr7IEewV0FP19p7jhE3yhHdCouzk+VjeeSK2WL5RCfds/ZkOw= 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=vO5BaKxl; arc=none smtp.client-ip=37.205.15.56 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 0AE88196E57; Fri, 15 Mar 2024 16:09:33 +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=1710515373; bh=6T0BIzOuPE5LuqB1wHjUl+/fIgb/2kuGQnVeMY/YX2w=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=vO5BaKxl3e9SUXW4uSTkVJ11QsDMqP5wxHIeZ5KyjOkM5TxkPkgVN8VeO79q2Hkm1 GToh2cK9Y420JuptkfHcSrQcnACwXCZGkFjEK21DH9GZzrfK9vyf1tg122+Iusj5T7 qarBmwhEsb9gFrRNOlecDMXTCTO3kNQ9AeUuM/Y4G6rGX0Trr2bbq7jPdkKyPrlGFx B0u/f3KMD20HASGWHOtoZPcxknDMVDdVCbPKtDoQphH6zE3Ck4QZ7k5eCZkcwcgCPR LTuOYimvDog3ULTKdBMXnQM8wxblsXCLnZ/hjCFd1ElBwOAnqGzG1A8avc+C85Gw3b oLaXbdIwyEJ9g== Date: Fri, 15 Mar 2024 16:09:31 +0100 From: Petr =?UTF-8?B?VGVzYcWZw61r?= To: Michael Kelley Cc: Petr Tesarik , Christoph Hellwig , Marek Szyprowski , Robin Murphy , Will Deacon , Nicolin Chen , "open list:DMA MAPPING HELPERS" , open list , Roberto Sassu , Petr Tesarik Subject: Re: [PATCH 1/1] swiotlb: extend buffer pre-padding to alloc_align_mask if necessary Message-ID: <20240315160931.48879f5c@meshulam.tesarici.cz> In-Reply-To: References: <20240312134149.497-1-petrtesarik@huaweicloud.com> <20240315132613.5a340a69@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, 15 Mar 2024 14:59:08 +0000 Michael Kelley wrote: > From: Petr Tesa=C5=99=C3=ADk Sent: Friday, March 15, 2= 024 5:26 AM > >=20 > > On Fri, 15 Mar 2024 02:53:10 +0000 > > Michael Kelley wrote: > > =20 > > > From: Petr Tesarik Sent: Tuesday, March= 12, 2024 6:42 AM =20 > > > > =20 >=20 > [snip] >=20 > > > > @@ -1349,6 +1353,15 @@ phys_addr_t swiotlb_tbl_map_single(struct de= vice *dev, phys_addr_t orig_addr, > > > > return (phys_addr_t)DMA_MAPPING_ERROR; > > > > } > > > > > > > > + /* > > > > + * Calculate buffer pre-padding within the allocated space. Use i= t to > > > > + * preserve the low bits of the original address according to dev= ice's > > > > + * min_align_mask. Limit the padding to alloc_align_mask or slot = size > > > > + * (whichever is bigger); higher bits of the original address are > > > > + * preserved by selecting a suitable IO TLB slot. > > > > + */ > > > > + offset =3D orig_addr & dma_get_min_align_mask(dev) & > > > > + (alloc_align_mask | (IO_TLB_SIZE - 1)); > > > > index =3D swiotlb_find_slots(dev, orig_addr, > > > > alloc_size + offset, alloc_align_mask, &pool); > > > > if (index =3D=3D -1) { > > > > @@ -1364,6 +1377,12 @@ phys_addr_t swiotlb_tbl_map_single(struct de= vice *dev, phys_addr_t orig_addr, > > > > * This is needed when we sync the memory. Then we sync the buff= er if > > > > * needed. > > > > */ > > > > + padding =3D 0; > > > > + while (offset >=3D IO_TLB_SIZE) { > > > > + pool->slots[index++].orig_addr =3D INVALID_PHYS_ADDR; > > > > + pool->slots[index].padding =3D ++padding; > > > > + offset -=3D IO_TLB_SIZE; > > > > + } =20 > > > > > > Looping to fill in the padding slots seems unnecessary. > > > The orig_addr field should already be initialized to > > > INVALID_PHYS_ADDR, and the padding field should already > > > be initialized to zero. =20 > >=20 > > Ack. > > =20 > > > The value of "padding" should be just > > > (offset / IO_TLB_SIZE), and it only needs to be stored in the > > > first non-padding slot where swiotlb_release_slots() will > > > find it. =20 > >=20 > > This is also right. I asked myself the question what should happen if > > somebody passes a padding slot's address to swiotlb_tbl_unmap_single(). > > Of course, it's an invalid address, but as a proponent of defensive > > programming, I still asked what would be the best response? If I store > > padding in each slot, the whole block is released. If I store it only > > in the first non-padding slot, some slots may leak. > >=20 > > On a second thought, the resulting SWIOTLB state is consistent either > > way, and we don't to care about leaking some slots if a driver is > > buggy. Maybe it's even better, because the leak will be noticed. > >=20 > > In short, I agree, let's keep the code simple. > > =20 >=20 > One other thought: Fundamentally, fixing the core problem > requires swiotlb_tbl_unmap_single() to have some information > it doesn't have in the current code. It needs to know the > number of padding slots, so that it can free them correctly. > Your solution is to store the # of padding slots in the > io_tlb_slot array. >=20 > Another approach would be to have callers pass the > alloc_align_mask as an argument to swiotlb_tbl_unmap_single(). > It can then calculate the offset and the number of padding > slots just like swiotlb_tbl_map_single() does. Nothing > additional would need to be stored in the io_tlb_slot array. > The IOMMU code is the only caller than uses a non-zero > alloc_align_mask. From a quick look at that code, the > unmap path has the iova_mask() available, so that would > work. Other callers would pass zero, just like they do for > swiotlb_tbl_map_single(). >=20 > I don't immediately have a strong opinion either way, but > it's something to think about a bit more. I believe it's slightly more robust to store how the buffer was actually allocated than to rely on the caller. It seems to me that this was also a design goal of the original author. For example, note that swiotlb_tbl_unmap_single() uses mapping_size only to do the final buffer sync, but not to determine how many slots should be released. This information is taken from struct io_tlb_slot.alloc_size. Petr T