Message ID | 20240131122543.14791-4-will@kernel.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-46418-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp1850151dyb; Wed, 31 Jan 2024 04:26:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IHpL97fdYXVQOdHyp+sMl/UvpUVHuq9Ln8uTX2ULuqmfiyEXo1iR8pW32PJ44aX8++eYpT6 X-Received: by 2002:a62:e311:0:b0:6d9:b588:2053 with SMTP id g17-20020a62e311000000b006d9b5882053mr1436461pfh.2.1706704019125; Wed, 31 Jan 2024 04:26:59 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706704019; cv=pass; d=google.com; s=arc-20160816; b=UdWPhbr2PyGHNFT1vn+f7nXJEaehNxxN3u0slq7TRMcnfye4VWHD/0OEk6pwiP50YT 1uhEkRfCCSUPGwCZjtYNtzIUYYKLJ48ajEpmjlICxJTPsGu6Vyt6SLH6I97Qs7Z7Oq0r ejWPxByOAkQtSHxlkIEiIhCXJHVAN+MnFIIzyjdQaLJe/s+WBjuazgSDkleI+d7B6tXw FZ6bEz/4AClvxjHUYy2Jqedd7zGOVW58FQPSbTVhSBRjQWrbji/CHDQUEg8lfw5vT5dw GwlfyeyUTLKKa8jipoahWNwv50XAhPNk6nh5Wk98APdthMUW5UuE9RGsfXOzrwCW2ge6 2daA== 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 :date:subject:cc:to:from:dkim-signature; bh=vN0PppCRl3fPf1FUdVod8fxZZ++AN2Z20bzt44AeK/A=; fh=74enmF4/q/XP/Vqs4K6A711d7TvkHr3uWTNAP45mN68=; b=DlR6lTgOV2+clm5WABKx5Kvn5K/r3CjYvCrb4VeMwhdbSO+QoFAv8EWIOQ42WW/o9A DjXNTHMEcnosmA1pS0FOcATiWxT8Ht5qa6WgfUp5NEwDVg9YLiHbT/DufjXD6h541X8S jrQCXWxVjqHYqknAeOg235uRbe5Mf3yGrBV5pcF+8OHydnfbFD76dw9xE23JNa17cvdU pFNSXKDXhQ3D8H2f5VVsLsbYfMQPM8ZNIS0EV9vCfpYcTD96U7nhUMdn3nDbHk7lnWbH 8WtN8sVKe0T/5d9UUGG+QSPZNhM3eDGxCu3+kQ2uaeoI/wqe9dBgMDJgo4luzef1158q +oTA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=PS8Lt+3J; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-46418-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46418-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCXb8MhbGohtEUVaoY0YrOwzrgQhkzM6b7nsYBSwu1AY76VOV9Am5+uJLtwJDqOuHDsHW7pC0/RHrcPPthOSLqIPL6hQVQ== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id e8-20020a631e08000000b005d8e22d633bsi3374855pge.70.2024.01.31.04.26.58 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 04:26:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-46418-ouuuleilei=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=@kernel.org header.s=k20201202 header.b=PS8Lt+3J; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-46418-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46418-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org 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 73542294945 for <ouuuleilei@gmail.com>; Wed, 31 Jan 2024 12:26:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 45DB979958; Wed, 31 Jan 2024 12:25:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="PS8Lt+3J" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 8891479938; Wed, 31 Jan 2024 12:25:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706703956; cv=none; b=phvaHaEcwLKqBV2fiNoGuOD/ucxEQvoY0bd+Xy23s5ZZ64kfb2y2C/ODJBwmEDlyjAw59+ezHOsO1YuwIa2ocu2mVgmeSqAwBsuecNLLAeWLmV+AfPCbjnHGW5hUWIeOnqZakIQNyARAGkx8R9qcC60VaD/4DTOVOOh+AAAwAXE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706703956; c=relaxed/simple; bh=97FSX4oU5s9ZlUZoIaV1rFcSltgv+fl+Z/Ia3ZsihrQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jdgpoiukA4FV2jxJ4idtvsyjbiE5U9LLtO5VxEEPlNbG0ecZljKTvoEjT51/r27YjA8Q4j7oOxXVSqzJC1soqCnrnaOPm1d6VWkfWY4+Ha035OXis+IdrEVNepm27wmE/c3RWPEX/a/Lb8mSGoElS4W7AR+6ty9yNtKWv8LYFTg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=PS8Lt+3J; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3AAD9C43390; Wed, 31 Jan 2024 12:25:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706703956; bh=97FSX4oU5s9ZlUZoIaV1rFcSltgv+fl+Z/Ia3ZsihrQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PS8Lt+3J3Xs0kHh+rJD0hfTP0DSeJUPH7CN4ilOfxfaxEBTtABTQKFhU1Vt6Fpl2h SXSdHDwsTGoxoDqLkFJctSVVpGe6bcg8D+ruZJSMQ9FtOZvbd2j6ArIUmJTydEP6Vq EtyOYy0RIbZaKV+hZb32fZ/s5BBWwSM3KYRey7qNdeHvVKj0KOC2C77zkelgMAGfM6 f1BcJFsEuDj8878ZiNGUZ8oz51mPpdupW5+NYpRewfsMIkoqZRWfkwondppd3ymkBO vNd4nflHvdfxun1SphQ32VxWLLgyY6a49tQ2rU989y6BJ5OHpugMz8mm/PWTuiVa8W nJvTjeNIafLhA== From: Will Deacon <will@kernel.org> To: linux-kernel@vger.kernel.org Cc: kernel-team@android.com, Will Deacon <will@kernel.org>, iommu@lists.linux.dev, Christoph Hellwig <hch@lst.de>, Marek Szyprowski <m.szyprowski@samsung.com>, Robin Murphy <robin.murphy@arm.com>, Petr Tesarik <petr.tesarik1@huawei-partners.com>, Dexuan Cui <decui@microsoft.com> Subject: [PATCH v2 3/3] swiotlb: Honour dma_alloc_coherent() alignment in swiotlb_alloc() Date: Wed, 31 Jan 2024 12:25:43 +0000 Message-Id: <20240131122543.14791-4-will@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240131122543.14791-1-will@kernel.org> References: <20240131122543.14791-1-will@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789608873490792091 X-GMAIL-MSGID: 1789608873490792091 |
Series |
Fix double allocation in swiotlb_alloc()
|
|
Commit Message
Will Deacon
Jan. 31, 2024, 12:25 p.m. UTC
core-api/dma-api-howto.rst states the following properties of
dma_alloc_coherent():
| The CPU virtual address and the DMA address are both guaranteed to
| be aligned to the smallest PAGE_SIZE order which is greater than or
| equal to the requested size.
However, swiotlb_alloc() passes zero for the 'alloc_align_mask'
parameter of swiotlb_find_slots() and so this property is not upheld.
Instead, allocations larger than a page are aligned to PAGE_SIZE,
Calculate the mask corresponding to the page order suitable for holding
the allocation and pass that to swiotlb_find_slots().
Cc: Christoph Hellwig <hch@lst.de>
Cc: Marek Szyprowski <m.szyprowski@samsung.com>
Cc: Robin Murphy <robin.murphy@arm.com>
Cc: Petr Tesarik <petr.tesarik1@huawei-partners.com>
Cc: Dexuan Cui <decui@microsoft.com>
Signed-off-by: Will Deacon <will@kernel.org>
---
kernel/dma/swiotlb.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
Comments
On 31/01/2024 12:25 pm, Will Deacon wrote: > core-api/dma-api-howto.rst states the following properties of > dma_alloc_coherent(): > > | The CPU virtual address and the DMA address are both guaranteed to > | be aligned to the smallest PAGE_SIZE order which is greater than or > | equal to the requested size. > > However, swiotlb_alloc() passes zero for the 'alloc_align_mask' > parameter of swiotlb_find_slots() and so this property is not upheld. > Instead, allocations larger than a page are aligned to PAGE_SIZE, > > Calculate the mask corresponding to the page order suitable for holding > the allocation and pass that to swiotlb_find_slots(). I guess this goes back to at least e81e99bacc9f ("swiotlb: Support aligned swiotlb buffers") when the explicit argument was added - not sure what we do about 5.15 LTS though (unless the answer is to not care...) As before, though, how much of patch #1 is needed if this comes first? Cheers, Robin. > Cc: Christoph Hellwig <hch@lst.de> > Cc: Marek Szyprowski <m.szyprowski@samsung.com> > Cc: Robin Murphy <robin.murphy@arm.com> > Cc: Petr Tesarik <petr.tesarik1@huawei-partners.com> > Cc: Dexuan Cui <decui@microsoft.com> > Signed-off-by: Will Deacon <will@kernel.org> > --- > kernel/dma/swiotlb.c | 4 +++- > 1 file changed, 3 insertions(+), 1 deletion(-) > > diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c > index 4485f216e620..8ec37006ac70 100644 > --- a/kernel/dma/swiotlb.c > +++ b/kernel/dma/swiotlb.c > @@ -1632,12 +1632,14 @@ struct page *swiotlb_alloc(struct device *dev, size_t size) > struct io_tlb_mem *mem = dev->dma_io_tlb_mem; > struct io_tlb_pool *pool; > phys_addr_t tlb_addr; > + unsigned int align; > int index; > > if (!mem) > return NULL; > > - index = swiotlb_find_slots(dev, 0, size, 0, &pool); > + align = (1 << (get_order(size) + PAGE_SHIFT)) - 1; > + index = swiotlb_find_slots(dev, 0, size, align, &pool); > if (index == -1) > return NULL; >
On Wed, Jan 31, 2024 at 04:03:38PM +0000, Robin Murphy wrote: > On 31/01/2024 12:25 pm, Will Deacon wrote: > > core-api/dma-api-howto.rst states the following properties of > > dma_alloc_coherent(): > > > > | The CPU virtual address and the DMA address are both guaranteed to > > | be aligned to the smallest PAGE_SIZE order which is greater than or > > | equal to the requested size. > > > > However, swiotlb_alloc() passes zero for the 'alloc_align_mask' > > parameter of swiotlb_find_slots() and so this property is not upheld. > > Instead, allocations larger than a page are aligned to PAGE_SIZE, > > > > Calculate the mask corresponding to the page order suitable for holding > > the allocation and pass that to swiotlb_find_slots(). > > I guess this goes back to at least e81e99bacc9f ("swiotlb: Support aligned > swiotlb buffers") when the explicit argument was added - not sure what we do > about 5.15 LTS though (unless the answer is to not care...) Thanks. I'll add the Fixes: tag but, to be honest, if we backport the first patch then I'm not hugely fussed about this one in -stable kernels simply because I spotted it my inspection rather than an real failure. > As before, though, how much of patch #1 is needed if this comes first? See my reply over there, but I think we need all of this. Will
diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 4485f216e620..8ec37006ac70 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -1632,12 +1632,14 @@ struct page *swiotlb_alloc(struct device *dev, size_t size) struct io_tlb_mem *mem = dev->dma_io_tlb_mem; struct io_tlb_pool *pool; phys_addr_t tlb_addr; + unsigned int align; int index; if (!mem) return NULL; - index = swiotlb_find_slots(dev, 0, size, 0, &pool); + align = (1 << (get_order(size) + PAGE_SHIFT)) - 1; + index = swiotlb_find_slots(dev, 0, size, align, &pool); if (index == -1) return NULL;