From patchwork Wed Feb 28 13:39:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Deacon X-Patchwork-Id: 207873 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp3360125dyb; Wed, 28 Feb 2024 05:58:13 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUgkYg58Qjyh0b/NsxhQF96gza2ccg9q/QDTngzfS8L68OmmktJ8r/Q0UlIXkBb21qRp88uBznLJCR7qJKIshHnX7BbNg== X-Google-Smtp-Source: AGHT+IEKLos6jvscW/axOHCul+8PIXDM1Ygs75t3mYFgU7Q3ZwqUYSW+oPEV7+JATxjhgyYOOM6q X-Received: by 2002:a17:90a:9bc5:b0:29a:be1a:ea40 with SMTP id b5-20020a17090a9bc500b0029abe1aea40mr8439226pjw.9.1709128693046; Wed, 28 Feb 2024 05:58:13 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709128693; cv=pass; d=google.com; s=arc-20160816; b=bsM3wBYz0vhnRicnffS2QXQpYtunNrV27nioTn7Qw53uBT4aUoBGSlsuQn33G2dviY +31j9NJLh966vRSmvuPjHoy22NIOZ0FPT8QS9zUIWK4jhtCXeUGziYPsjnjm6NK7Ve1R 0gLPcPd7s92CkVHsEZrVLI9ez2XM9uV15nAQcXs44Z3cusGW7gLqfGLyXLzMlN594SuF wu3TOdd1bC9hTheLBC0VfP5JP9ctBSQjuLBby7IJI+a2yRySyDuZPqbve1oaf+NMKpfH Gw3MFrvrq028hMss2M2+78Ukdw0jQpHhKnkBtxIwxKgikLMvVPHugw8DcJknFIbQ0QpX t/kQ== 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=OtYD9aRdpfMAuS8bpkhyLRlhrT8jp+UArwjUknxsdpM=; fh=E8F5/vp/nAcPVJWJwVt5RE8c2HME7kVqVeSMFxe8huE=; b=mftGSCy09gu3MCWKAN35S4Tlbf8lIHy5Ig6NOPceZFEmh9AcqvPQyfEPyj0YaI2yz3 qCLH0cLKNjRrQ7a42tgbJD6Duql0zRPjGd0/dnYyq+SroX7S2qPi89+UlMnTTKZKjbtl a46PgbgdL8gFMCJyUaBrRwsWNAdwg8CNFwxMAqI0+EzSWmfaGoIMiUmUjFvqgIRgXrO6 Zhr+57t3kfcgrVAOwdA+LG1T6R6xtplcFj4DD5akwMjt319ofh1GVdk6yHLFy7CXLAC7 m3lATn7BovZD7WMddZzp4jr5T0r4m+xFegxSvLU9Z99PBaA6TED7MclwEvlj6OwqvxNF +dwQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=J76irrat; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-85111-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85111-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id 90-20020a17090a09e300b0029a6469b14esi1347010pjo.189.2024.02.28.05.58.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Feb 2024 05:58:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-85111-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=J76irrat; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-85111-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85111-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id B2303B25887 for ; Wed, 28 Feb 2024 13:40:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CA106157E97; Wed, 28 Feb 2024 13:39:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="J76irrat" 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 0D8511552FD; Wed, 28 Feb 2024 13:39:44 +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=1709127585; cv=none; b=fHzmmAtty6rHpm9ctxom1Au3pzkd4SMnNl1Ep1H9vv7awZwGdBtz53gapUCWkfNI/kipRE/2/3e3e4W1AC0TbzY+V5h93ykZ5VFolHPcHUpPLLkDN/xhkcjEApq3m+LBrpnnVh4zVoMYhm47GvFLrDZj3zXgt1JaYiaRy1hsLEQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709127585; c=relaxed/simple; bh=b49x/ykHm5o5bbMJSwQ3cpWnXqcs2XbmjOgH/NQ/iTs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=okOc/Aw272V09JZSPPg03wlrF1r7uHwmrx/N2RmclNCpG1hxlQ/fKknad1uZLir9n5Xbi+VW7KFq4pLD8UnejZQQ+6EHgSL0j/EWY5bu93KLJHKLt5Jr5Ljf7EhUVDXjKPLwrTTibV2h9kPmr04mpBgyCCMCA6dZgMKLdNWV/H8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=J76irrat; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 663EEC433A6; Wed, 28 Feb 2024 13:39:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709127584; bh=b49x/ykHm5o5bbMJSwQ3cpWnXqcs2XbmjOgH/NQ/iTs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=J76irratocajmW2SDy9/e8xnA86MTWtA4xigYvKgjwwqiU7OWZwV0TtDgMu/4RZgv BahN412H0Btlb79CeteXuM7H93PJSljNMNet9W+MtP3SPJjIHIzf5btlJGjP7/WVkn kCuJWuHCsxgdFuNVQyhgTaf8QaWHG0itHhL6A3ybkgGYS87LNMkPsl83+DZ/aAgQO3 ZjqBrSwlEI81LLJso7aCifnSya/zOyFwyptMWg6/x5fpPNR+/+U7T4dKoE9+TjHbsg kD1v8wOLf/s8gVLxIEfJ121wRZA7/PsNtMntJs3kgE6lGEDGyunYDpBb6zIlgD6Nfr yHnCtsGlW9WFQ== From: Will Deacon To: linux-kernel@vger.kernel.org Cc: kernel-team@android.com, Will Deacon , iommu@lists.linux.dev, Christoph Hellwig , Marek Szyprowski , Robin Murphy , Petr Tesarik , Dexuan Cui , Nicolin Chen , Michael Kelley Subject: [PATCH v5 3/6] swiotlb: Honour dma_alloc_coherent() alignment in swiotlb_alloc() Date: Wed, 28 Feb 2024 13:39:27 +0000 Message-Id: <20240228133930.15400-4-will@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240228133930.15400-1-will@kernel.org> References: <20240228133930.15400-1-will@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792151328572911809 X-GMAIL-MSGID: 1792151328572911809 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 Cc: Marek Szyprowski Cc: Robin Murphy Cc: Dexuan Cui Fixes: e81e99bacc9f ("swiotlb: Support aligned swiotlb buffers") Reviewed-by: Petr Tesarik Tested-by: Nicolin Chen Reviewed-by: Michael Kelley Signed-off-by: Will Deacon --- 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 ab7fbb40bc55..c20324fba814 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -1633,12 +1633,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;