From patchwork Wed Feb 28 13:39:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Deacon X-Patchwork-Id: 207860 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp3351014dyb; Wed, 28 Feb 2024 05:40:17 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWnPs1/Jf96htXTQWMZlYTQwN7epNhXeW0P5BGQWSrGhkX+vW+Iur+nCvl2InK6FL0Ap6Pvi/+qDMDfkyqf+r+MUlW+EA== X-Google-Smtp-Source: AGHT+IGPYKzzxlPh4InVSG0GzQD9QTG1tkceD2LED6KnjclgCCos38nHXyGLsEaZZFn+htV+izux X-Received: by 2002:aa7:d7c3:0:b0:565:a5aa:22e7 with SMTP id e3-20020aa7d7c3000000b00565a5aa22e7mr2300013eds.2.1709127617057; Wed, 28 Feb 2024 05:40:17 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709127617; cv=pass; d=google.com; s=arc-20160816; b=WEyw7/Wj/K9hpdJ3x9ZebMuxokNf6DHlFrSCu6BiqZ0sMg8OoB9CmjUtHNkW2pnnb4 mF4mYPu43Or33tFaIm6H6FY88K55NO+e9Y0k3m2K3fw/0XRXtUjBRA6O8fufBYcfTtgR dxP95MOsY6qEtHWQtW+SJV+OcJ32cSM3GqGzfuDJFtRPtr3Ht+a0tG/KB1BtlGgtolNN Xs3sN0Z4AdL+XTBZfY3DbXhMgspkiUAz8GjREx+ClZcTrpplMVKigFhmxAShmhAS8VwL AhZZpSRhelkNNjySiHHGNm5cWdfBvlyt3UTQGEaCZ7NMx6c4zz0vOiucFbkTpx7NfEMO bE+g== 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=1S27HHdqamGeFK0XUfl4PL51xwJwbdkuNLwjK/uwbVM=; fh=E8F5/vp/nAcPVJWJwVt5RE8c2HME7kVqVeSMFxe8huE=; b=Ho/gJCtEqTkOKNqkYnM6CMFY2yZXqRoXAJ0SRXluUc95YsV/5Mbwd3pC8BDqLEiFud 8Sa8D7lcwV6Jf2KU53YX1APilgu+EF6b2FAupY2ndYua/804tHnJC5ECW7IKuYNNLwoA p/G+cSd2zuc+jfCHDmtXcEJCNVYVRQhnqDztkvnEDSDpED+kTBO/2HUi7secldO7aVPy P6w/d1lXKN4ibCa+XPSJWEvu5khRZBQ/hJmra7APYNlsAToh/uT5CZeKobvcdXi4O7km lzV2BPIEWh5vQ5Y+tiOmBuWP7teq2tQlaL1Jbg6N0nri/8rsH3BfRL3Ch9UmR9gnsOzK 17gQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Ky8EVpbG; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-85109-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85109-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id o18-20020a509b12000000b00560f9a26f9csi1669440edi.580.2024.02.28.05.40.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Feb 2024 05:40:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-85109-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Ky8EVpbG; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-85109-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85109-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 am.mirrors.kernel.org (Postfix) with ESMTPS id AA8021F28AE3 for ; Wed, 28 Feb 2024 13:40:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id ED051153516; Wed, 28 Feb 2024 13:39:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Ky8EVpbG" 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 E8A8815098D; Wed, 28 Feb 2024 13:39:39 +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=1709127580; cv=none; b=BfdoqHrneBXLiPJ+w3wRJBQ4JLC23ZLPxT+D6//O6QM6K1JEWxS9ydtCtzWbr+frn0tPQHA7hwEKeAtziAFsxY9/skQAdvIGKBiSGFUzpPLtKKolXSIxTq3i16DBeb3oX6n5W6bSUTNIH3rfxeGYznLzIu7LLLo1VMnmJcKDH20= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709127580; c=relaxed/simple; bh=57ZOvQ6KKNZi0fcYi8vZNaS/w3eam5bO+zbtjWT55RI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=txfQYVJOIQqI87LG62veh+/2OpZa69bK4oW/LGsFkh4p0GbGgTWSrKe2MRX79W4qRDaFCE0z4ExciCqmzj2r2Z3cMDCaNvKXUHWkSKphPTvdsWUS0099OKvU+QvYDRopbYxO5HuJlUcvV0XIOzlKYWRRm73iOTucmjPgU61Ue48= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ky8EVpbG; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 35020C43390; Wed, 28 Feb 2024 13:39:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709127579; bh=57ZOvQ6KKNZi0fcYi8vZNaS/w3eam5bO+zbtjWT55RI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ky8EVpbGdxaBnQU3DHttREnxhLN2eQATa47x9AOoBmjsJYn/pMBLnEkO2d1XT/YLN 83D3tYoPNAPdt+f3kkUAjjlwVt48kiOGKt34mNYikh4klc8J/kntmVmghu5SzjUBws jLmsm0Zp4ZTuQJblnEGkQDEAhAkNAcH6ECCw12Sx5DE/zgKfAxydct9JjGfIgQCaL/ 1hXN06Nhg+NbLQM8M29whi5kW2PiO/y8Z+TMtM6J38RMefnFXIeMQQYMYGxkJ6h1ne SkGMNasS7WoLt58WKWQrGtkxKe57sDLV2uTywIjdFTAq6dMM9eqg2uL3QE5MDo2b7y +DDjay1AeUvDA== 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 1/6] swiotlb: Fix double-allocation of slots due to broken alignment handling Date: Wed, 28 Feb 2024 13:39:25 +0000 Message-Id: <20240228133930.15400-2-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: 1792150200622651817 X-GMAIL-MSGID: 1792150200622651817 Commit bbb73a103fbb ("swiotlb: fix a braino in the alignment check fix"), which was a fix for commit 0eee5ae10256 ("swiotlb: fix slot alignment checks"), causes a functional regression with vsock in a virtual machine using bouncing via a restricted DMA SWIOTLB pool. When virtio allocates the virtqueues for the vsock device using dma_alloc_coherent(), the SWIOTLB search can return page-unaligned allocations if 'area->index' was left unaligned by a previous allocation from the buffer: # Final address in brackets is the SWIOTLB address returned to the caller | virtio-pci 0000:00:07.0: orig_addr 0x0 alloc_size 0x2000, iotlb_align_mask 0x800 stride 0x2: got slot 1645-1649/7168 (0x98326800) | virtio-pci 0000:00:07.0: orig_addr 0x0 alloc_size 0x2000, iotlb_align_mask 0x800 stride 0x2: got slot 1649-1653/7168 (0x98328800) | virtio-pci 0000:00:07.0: orig_addr 0x0 alloc_size 0x2000, iotlb_align_mask 0x800 stride 0x2: got slot 1653-1657/7168 (0x9832a800) This ends badly (typically buffer corruption and/or a hang) because swiotlb_alloc() is expecting a page-aligned allocation and so blindly returns a pointer to the 'struct page' corresponding to the allocation, therefore double-allocating the first half (2KiB slot) of the 4KiB page. Fix the problem by treating the allocation alignment separately to any additional alignment requirements from the device, using the maximum of the two as the stride to search the buffer slots and taking care to ensure a minimum of page-alignment for buffers larger than a page. This also resolves swiotlb allocation failures occuring due to the inclusion of ~PAGE_MASK in 'iotlb_align_mask' for large allocations and resulting in alignment requirements exceeding swiotlb_max_mapping_size(). Fixes: bbb73a103fbb ("swiotlb: fix a braino in the alignment check fix") Fixes: 0eee5ae10256 ("swiotlb: fix slot alignment checks") Cc: Christoph Hellwig Cc: Marek Szyprowski Cc: Robin Murphy Cc: Dexuan Cui Reviewed-by: Petr Tesarik Tested-by: Nicolin Chen Signed-off-by: Will Deacon Reviewed-by: Michael Kelley --- kernel/dma/swiotlb.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index b079a9a8e087..2ec2cc81f1a2 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -982,7 +982,7 @@ static int swiotlb_search_pool_area(struct device *dev, struct io_tlb_pool *pool phys_to_dma_unencrypted(dev, pool->start) & boundary_mask; unsigned long max_slots = get_max_slots(boundary_mask); unsigned int iotlb_align_mask = - dma_get_min_align_mask(dev) | alloc_align_mask; + dma_get_min_align_mask(dev) & ~(IO_TLB_SIZE - 1); unsigned int nslots = nr_slots(alloc_size), stride; unsigned int offset = swiotlb_align_offset(dev, orig_addr); unsigned int index, slots_checked, count = 0, i; @@ -993,19 +993,18 @@ static int swiotlb_search_pool_area(struct device *dev, struct io_tlb_pool *pool BUG_ON(!nslots); BUG_ON(area_index >= pool->nareas); + /* + * For mappings with an alignment requirement don't bother looping to + * unaligned slots once we found an aligned one. + */ + stride = get_max_slots(max(alloc_align_mask, iotlb_align_mask)); + /* * For allocations of PAGE_SIZE or larger only look for page aligned * allocations. */ if (alloc_size >= PAGE_SIZE) - iotlb_align_mask |= ~PAGE_MASK; - iotlb_align_mask &= ~(IO_TLB_SIZE - 1); - - /* - * For mappings with an alignment requirement don't bother looping to - * unaligned slots once we found an aligned one. - */ - stride = (iotlb_align_mask >> IO_TLB_SHIFT) + 1; + stride = umax(stride, PAGE_SHIFT - IO_TLB_SHIFT + 1); spin_lock_irqsave(&area->lock, flags); if (unlikely(nslots > pool->area_nslabs - area->used)) @@ -1015,11 +1014,14 @@ static int swiotlb_search_pool_area(struct device *dev, struct io_tlb_pool *pool index = area->index; for (slots_checked = 0; slots_checked < pool->area_nslabs; ) { - slot_index = slot_base + index; + phys_addr_t tlb_addr; - if (orig_addr && - (slot_addr(tbl_dma_addr, slot_index) & - iotlb_align_mask) != (orig_addr & iotlb_align_mask)) { + slot_index = slot_base + index; + tlb_addr = slot_addr(tbl_dma_addr, slot_index); + + if ((tlb_addr & alloc_align_mask) || + (orig_addr && (tlb_addr & iotlb_align_mask) != + (orig_addr & iotlb_align_mask))) { index = wrap_area_index(pool, index + 1); slots_checked++; continue; From patchwork Wed Feb 28 13:39:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Deacon X-Patchwork-Id: 207861 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp3351427dyb; Wed, 28 Feb 2024 05:41:03 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXjp1PdVaROxWVKX5D8ecCB/fLUpZXIc2Pf2iBlNHqyPTCdPvVq2wdUI+RmMowexanojuyo27H6/8ApCDGubpPUKWshiA== X-Google-Smtp-Source: AGHT+IFckZ8DTBYrLd5/rmrWMtHreWSHHNWRbmAXmlgvSNrsTu6xS2GUShnZEZLVIBSErxZ4JpFi X-Received: by 2002:a05:6214:2461:b0:68f:e855:52c3 with SMTP id im1-20020a056214246100b0068fe85552c3mr6532908qvb.45.1709127663417; Wed, 28 Feb 2024 05:41:03 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709127663; cv=pass; d=google.com; s=arc-20160816; b=hqB6e7E3xJjjdIVVeotyaSJo40cBmoOBUX8yqbFSFOCpx70zRVd95qmfTITzkpd4Ng I2yp2OTdYIw10zHjYBTPff+95afubdIf/ebofL25dpMsuxrI3LM8RhYcBNnY8z8YrsmG NvjYMFR970Lc3bHrUKaj7heqJMZ8yODKavlGxka/U1QQ6Bb+Val9YujxLHmSIuBLJ749 XzTRd1mZ0LYeg1x3+Poe4gicM9oZEh/YQ1EyzN15za827czqCoQJ4GHvSjMgooL4ZUen 6npiqktgyUCjtzt+QcyCfu5Eqz/VWJCa5vHeEhCoTAe3epdFa/oXrUyb+5xVXPaUUulo NntA== 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=Y1/EKRATIU/aqMaJhexRC9e1cWN3fDUy6BrOLkel3w4=; fh=E8F5/vp/nAcPVJWJwVt5RE8c2HME7kVqVeSMFxe8huE=; b=GAglUAdROkS1W9dk4qwslAQ6bWjSX3tpHdEjD6H69WehSwZTkpllWgoa/EeLR4EpO6 O0FMkrCOZKy6On1E+62uZ7AKjj//wnFkPKGNZ7CzsJ1EuE3qhH4Oseud0N5Sl0EzFkOe 1ZL71SyEAJitRFeC4+Pbb/27+VdCU4/FygJjgqSmvOVY8xtwt033RAUPSZFv9JhozHid XYgIJ8MBet+wUIWIC5Dj1eVGsJtuu4xq4VYBfIZuwiCyMTVYc2MyJaCGfvgdZkZxppqh 9AOUm7oyn5FPg3T0hyXUNHYPHDrrYTfsgHTtM01vHaZY6SBUFB++DqeXQSqo9INEpgSl fgjA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Jxi0zrK2; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-85110-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85110-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id s3-20020a0562140ca300b0068f533c6dc2si9546103qvs.261.2024.02.28.05.41.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Feb 2024 05:41:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-85110-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Jxi0zrK2; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-85110-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85110-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id BDD521C2494F for ; Wed, 28 Feb 2024 13:40:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C92911552F8; Wed, 28 Feb 2024 13:39:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Jxi0zrK2" 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 0E00B151CF9; Wed, 28 Feb 2024 13:39:42 +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=1709127582; cv=none; b=WZrOiRASfEPXVXkQFGRFiTI1DDBVxqkfozwhoLRTblsORLudfRTonqTISiU2jhDoxD4TI2bIYL1v3fsMImogkbOBdKYH4Kl5Q3x3j6S+3YnG4J1wklQ8Hm4cIpsVajGZA+CtqBMSPEWUQOyteoURxjUxcj/X5tFbPfD6EXvRJME= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709127582; c=relaxed/simple; bh=JqSwzF4JV3f0dyOOThKfJ10MqkgAb5PBoxHTEu2M6eg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=X5W7imoC8C5ThSPNr+Ko2rHuge0iqKfyBbUy8lLQJJYTONgTOanAZQ5UsryC7lLZgNT9mzlVM0fobTL2AY2fGwt+zmMNG2B0+6WPeQTY30MMGNAWx3Eti8phyaDZuW5HclJUiymTUuEcM+bBstPpNujSwrbb7CU/NWxda7L0T20= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Jxi0zrK2; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id C7B80C43394; Wed, 28 Feb 2024 13:39:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709127581; bh=JqSwzF4JV3f0dyOOThKfJ10MqkgAb5PBoxHTEu2M6eg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Jxi0zrK22NyJ2K60C8LRKYeTxeUHMJEq7GqPktlk5+DIsCkQIAr+TGTo9D4W6SdNp DG6L/DLwEdTtaeomm5mlZU/EQ29z0bvJpMhgo87TEeI99ayBh2zS6049zeu6mU8JL+ lCWFgcY/rgwS2x0fXiNevJ8b17JGZUS2kD2iwsSzBjA6b/n0Xc4YMIztZcFANd+MFo G4PQ71ToheQ78cyICvgvOX7KDBjYxh21tll2HifVEQKmJmgRmXxXMTGAL9u4LkubtN vVgNU54CFM7rSubQVGftxnILgKwLu5Utx/zVHoj5y4dUbsNoEs6Kpur7vDG6a6WmDe eOOPet1TtYmLQ== 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 2/6] swiotlb: Enforce page alignment in swiotlb_alloc() Date: Wed, 28 Feb 2024 13:39:26 +0000 Message-Id: <20240228133930.15400-3-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: 1792150248819270201 X-GMAIL-MSGID: 1792150248819270201 When allocating pages from a restricted DMA pool in swiotlb_alloc(), the buffer address is blindly converted to a 'struct page *' that is returned to the caller. In the unlikely event of an allocation bug, page-unaligned addresses are not detected and slots can silently be double-allocated. Add a simple check of the buffer alignment in swiotlb_alloc() to make debugging a little easier if something has gone wonky. Cc: Christoph Hellwig Cc: Marek Szyprowski Cc: Robin Murphy Cc: Dexuan Cui Reviewed-by: Petr Tesarik Tested-by: Nicolin Chen Reviewed-by: Michael Kelley Signed-off-by: Will Deacon --- kernel/dma/swiotlb.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 2ec2cc81f1a2..ab7fbb40bc55 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -1643,6 +1643,12 @@ struct page *swiotlb_alloc(struct device *dev, size_t size) return NULL; tlb_addr = slot_addr(pool->start, index); + if (unlikely(!PAGE_ALIGNED(tlb_addr))) { + dev_WARN_ONCE(dev, 1, "Cannot allocate pages from non page-aligned swiotlb addr 0x%pa.\n", + &tlb_addr); + swiotlb_release_slots(dev, tlb_addr); + return NULL; + } return pfn_to_page(PFN_DOWN(tlb_addr)); } 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; From patchwork Wed Feb 28 13:39:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Deacon X-Patchwork-Id: 207862 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp3351534dyb; Wed, 28 Feb 2024 05:41:18 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWtZwc3MEUKuZYA7kivYdVgAgDVMyyGViDmENiBT/Z8VAX9z2lJEkxg48s9srQ5WqOxqI87ncvYV3GdliGuRyCCHneG3g== X-Google-Smtp-Source: AGHT+IHUJLOvhzIuvuBzP83smBkhkBZ1dG/OMLcrAr8mo92p3uSgompB/miz7zwk3nnxVv3E86iH X-Received: by 2002:a17:906:38c2:b0:a43:217b:ffa7 with SMTP id r2-20020a17090638c200b00a43217bffa7mr6797684ejd.50.1709127678416; Wed, 28 Feb 2024 05:41:18 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709127678; cv=pass; d=google.com; s=arc-20160816; b=DHk8+gVRGESfIvY0rQlo8QVB3zlrVHtyRanV4V8ZuFJBLgSQt6FgoOG3q6Arhhkfhw yZKwPfqWy2qZ+TUARKgwaZFqobBo9hPen7FfWxuBb6r6k1cvsN/EjrDyi2iSi5wjwO5p hRqcrClOjSirjsXU19aM0VXlMFNq9ypeD/TWl3C1U7gWw69bpuyNhOixJx5evEUMdJ/b oOnk1EhTTAJdFRqTlQkvgneXu9zmFclH7FqU5gnrbGkS1V8O4lLDfMnDwbtfWAFn2boD rTZJshaQ9MKl/epFj+diiTmjm39JJpU7VXxtXFtBfwMBgcboGNRn6tvD73hfF1jaHWB1 cdIA== 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=XSg1Hr117hY9HU1b3KnGkhSL0jgwnWW0OKGE3tZG49A=; fh=E8F5/vp/nAcPVJWJwVt5RE8c2HME7kVqVeSMFxe8huE=; b=xJ7pnKT8WN7BSVttlegkVLoT3V1fTnRHFH37D+8URi9mVpIakkQ9gljxLW/SC8ANN5 Hxcdf4psDWWRtGyjMJAzhUqdE32rvARhCCkPR9fPYM0ufWF3MeA3T19i+uiVN3dro03g 91rOUca8bHIwV+2mEEttdyQB59naE7i9x6EG5aG/3xpQr3GY63wcueXtn/uiESeA6IZw RdUWJYrylx951l1pW0xH5jftn8q1Eom83cAHkiu1LjlGQybDm49MGtiHXjmmXBEP7fv6 Oqeprgl+T4lp+tYXdAk8OpzPlXNsCAq3sqRjT1MGMjOiSv9aFCnDTTeTUUr8wPqSECBa QvSw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=kmB8OHgS; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-85113-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85113-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id q16-20020a1709066b1000b00a432149ff40si1733951ejr.108.2024.02.28.05.41.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Feb 2024 05:41:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-85113-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=kmB8OHgS; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-85113-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85113-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 0CC181F28BAF for ; Wed, 28 Feb 2024 13:41:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 78A88158D69; Wed, 28 Feb 2024 13:39:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="kmB8OHgS" 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 A0A3E157E8E; Wed, 28 Feb 2024 13:39:47 +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=1709127587; cv=none; b=ndBGOhCuLHxmQh5HLPBhUCqqDsr7mUCBptVA4dJOfdobcxdf7MzksqTJ1S2n7bZI2MMqnARIGww+QLT+XMvqqzWnD407yMhGmAmRQbcj/TDBeKHoCFyjy4V5fxmy4amLfb7IAYyecuKSH33fGAs9i5tI7uNugxxrz8xbQZLN+GM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709127587; c=relaxed/simple; bh=dxwWmvXzdSSRNkdcGEkNTrMB/45mYo1YIJ2aRH5yEZo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=A+ovFCLKM92PZeiebaVgYiNVlPnss2iIoAwjK3wRHxkWqf/Qwd+cU9+HRRdhjfscUDi1/vTQZHb8uLRyhjNnYnmJKtlYRudWm+oxhBB4vElbtL8akqHUBxXG1tzmgOwWWMoVqG+lfO02C9APMKbUAsFpq27WNrqnAmTWAHVV2JA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kmB8OHgS; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0115DC433B1; Wed, 28 Feb 2024 13:39:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709127587; bh=dxwWmvXzdSSRNkdcGEkNTrMB/45mYo1YIJ2aRH5yEZo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kmB8OHgSZHzFyoRS0a5E0qlqyW60IZS83omqXJKgkbElBp3VnU4QXVUd96IDeHblS cQnTR0bae0YQ/gc6istY2EiMzL6JL1E4rDDzEerKy9Cu6Z5p7I3byzknb96ojXpXUp xlsxWkYkV4AnmOwjJEUByvnuRIuuP9j4AsDcGz9GW3klb7tIO3MzE5rVuewi2Qp2pO cvC99Grhqb7/Azhwm7dk9+IDo/crbeloSM40ecGTSKW0c8ix2DYZ+VwcoSLYgYCHAK eW1U88dFw7jyoSAqIITEJfZlDvkeeBx5URecupCW3YEdpzTCSLk+cDeY+9BwxfUpJA CEs6IgHRiddbg== 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 4/6] swiotlb: Fix alignment checks when both allocation and DMA masks are present Date: Wed, 28 Feb 2024 13:39:28 +0000 Message-Id: <20240228133930.15400-5-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: 1792150264407655267 X-GMAIL-MSGID: 1792150264407655267 Nicolin reports that swiotlb buffer allocations fail for an NVME device behind an IOMMU using 64KiB pages. This is because we end up with a minimum allocation alignment of 64KiB (for the IOMMU to map the buffer safely) but a minimum DMA alignment mask corresponding to a 4KiB NVME page (i.e. preserving the 4KiB page offset from the original allocation). If the original address is not 4KiB-aligned, the allocation will fail because swiotlb_search_pool_area() erroneously compares these unmasked bits with the 64KiB-aligned candidate allocation. Tweak swiotlb_search_pool_area() so that the DMA alignment mask is reduced based on the required alignment of the allocation. Fixes: 82612d66d51d ("iommu: Allow the dma-iommu api to use bounce buffers") Reported-by: Nicolin Chen Link: https://lore.kernel.org/r/cover.1707851466.git.nicolinc@nvidia.com Tested-by: Nicolin Chen Reviewed-by: Michael Kelley Signed-off-by: Will Deacon --- kernel/dma/swiotlb.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index c20324fba814..c381a7ed718f 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -981,8 +981,7 @@ static int swiotlb_search_pool_area(struct device *dev, struct io_tlb_pool *pool dma_addr_t tbl_dma_addr = phys_to_dma_unencrypted(dev, pool->start) & boundary_mask; unsigned long max_slots = get_max_slots(boundary_mask); - unsigned int iotlb_align_mask = - dma_get_min_align_mask(dev) & ~(IO_TLB_SIZE - 1); + unsigned int iotlb_align_mask = dma_get_min_align_mask(dev); unsigned int nslots = nr_slots(alloc_size), stride; unsigned int offset = swiotlb_align_offset(dev, orig_addr); unsigned int index, slots_checked, count = 0, i; @@ -993,6 +992,14 @@ static int swiotlb_search_pool_area(struct device *dev, struct io_tlb_pool *pool BUG_ON(!nslots); BUG_ON(area_index >= pool->nareas); + /* + * Ensure that the allocation is at least slot-aligned and update + * 'iotlb_align_mask' to ignore bits that will be preserved when + * offsetting into the allocation. + */ + alloc_align_mask |= (IO_TLB_SIZE - 1); + iotlb_align_mask &= ~alloc_align_mask; + /* * For mappings with an alignment requirement don't bother looping to * unaligned slots once we found an aligned one. From patchwork Wed Feb 28 13:39:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Deacon X-Patchwork-Id: 207863 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp3351895dyb; Wed, 28 Feb 2024 05:41:58 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCW6hipJ6muSTvOY/Liib0VQX1ikwVLipaELPk6moJL/SqF0fKqbs+6xIDhm4UiYrh2WJFbSQmdD17a+TgXOHPKOOraIhQ== X-Google-Smtp-Source: AGHT+IH9CCZiS8jRvEh/MP/vFnTzM+Gox6y9uHKuy/y14rX7sdAxZxLSFTZz9Q9rU56Yubv3byxO X-Received: by 2002:a17:902:c943:b0:1dc:30f1:b8d3 with SMTP id i3-20020a170902c94300b001dc30f1b8d3mr15289636pla.18.1709127718600; Wed, 28 Feb 2024 05:41:58 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709127718; cv=pass; d=google.com; s=arc-20160816; b=LzgfUceAlyCt01xk3QgzZeVn4RrFRSEeoPfIIk/dfDHhBEqMsFJsZD0XmAMYmRdj7d t6SJUcGPuZeOxqXO9L08alQKMfgS1Xc5/VtkDSyP0Sd+LmHnah//Q3hkfgpgU6SPKU0x 4fck0LokZ91a4X6bXPLriOvu3LQDBPojeLmym5LrkWIyo0Ipw3zfCyxSsIZg9PEA4Fae cUv5PgOLx4JlgnR9WQaLe4D7/6RNDzVRbYAEoQPEGJF/Fk100qIq/oe+xiD1a8dN3JoX gl/iKZmOaOsac2wgc45gLqW2doTnS2TYGajFSDfwXcgwCpxMHUSEez8vfIfBO9NonTGQ 2WSg== 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=YoT0Mx5Cm1UcYGBZKbT+NyCGxOzVDESAE3tAQ1uQLHE=; fh=E8F5/vp/nAcPVJWJwVt5RE8c2HME7kVqVeSMFxe8huE=; b=SwIGQggu1biff9RTF9ZXGFV4ErlXoNTy3Bfw2XCsZggbm7FMrlZILwSszWP4t29oVX td7NmvnVgITs1oTDfJy5WgutUo2Ev64sMNZFkfb22/cnHqZcdzA4lyXlhRHGSGlP8XoE 86v6UXR1f+BhZBmVekHFxyd996RE1LMmtAp8frHFAxFSiNzpA/OsSt8aanXOVJN3V7T+ GRO3ePXeIlmy+OfqAReWFr0g3dpsygJNjcc9xIWhm5xbsHWNFDylc6AuBWhMRsq8jC6E Fz9QNRkAsTmh2GvLAK5KuM+kFa6yAxPSeFbgRA43ut3F6/80l0HoiOQyOEBxKtG+o4/E QP1g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=CHWG753i; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-85114-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85114-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id b17-20020a170902d51100b001db78534cd4si3504665plg.256.2024.02.28.05.41.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Feb 2024 05:41:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-85114-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=CHWG753i; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-85114-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85114-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 F2CFA28B75E for ; Wed, 28 Feb 2024 13:41:30 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8710214AD28; Wed, 28 Feb 2024 13:39:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="CHWG753i" 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 C8D4114E2F5; Wed, 28 Feb 2024 13:39:49 +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=1709127589; cv=none; b=j7XpI7XenHC/r9eqfigDVMRk+GdY/Y1pqns2wM009hTcCJJXN+N3kVES64NCZkT3Y9USFx9vtRT++Kz12eTkkGu7tZnOAnpX8Ie4qHFaqWJ2XPtGutQYsFor0Vyz6aKn86T0bzQ7rpvB+dR0iat3Ejo21M4GT9lUUI5vajNuRCM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709127589; c=relaxed/simple; bh=o75sRCQUI46CYjNWqbkykeA/mcL6EPkE3K/J+SlWBK8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Dk9NZMmWU68heRDGWHAVJFDRLne1CBUadwyar269bXnpq5nvbxs1t+jHnqzYd8g6KiThTAAUD9sALxQlnhWOXsI9dTdOhgDc6xp3+1pXlJuhjq1ZHZSDS35SnnxxT1/yA2c7t4WYmojAgXEbIv28ei3KZai2yJHlHpkC013QBp0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CHWG753i; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8F27AC43390; Wed, 28 Feb 2024 13:39:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709127589; bh=o75sRCQUI46CYjNWqbkykeA/mcL6EPkE3K/J+SlWBK8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CHWG753ie/82pXIpmtvyfWKwL5df+/wNFbjVyxgnrKSYnAOKMld2nb/BdZ8TAXOj7 I55OQumlwgTkpPgFEfH/d+3KdhHnrIcC8oosvWDnFbFRbUZQj9FxLqizrbHcGHxyLv 031mR320OQK4AOILfHnXCmU75Wd44PnqY1JtHwAFMBTl0VWcsI0Ni0MZlTF3MxL0D2 qm7p5yv6mbHo+J4WCLJPeMQwcLA9DL4FliaCOljaBjT8Jb+Arj8tINTAWaXopN5aEe ccDnx2ezyRfgpXfZyDsTadnJh3on3e0Yt6v4mbUkpsjdckRQL9U2zsvkJnLLChzyqp DLMa7QVk8uxHg== 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 5/6] iommu/dma: Force swiotlb_max_mapping_size on an untrusted device Date: Wed, 28 Feb 2024 13:39:29 +0000 Message-Id: <20240228133930.15400-6-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: 1792150306780540643 X-GMAIL-MSGID: 1792150306780540643 From: Nicolin Chen The swiotlb does not support a mapping size > swiotlb_max_mapping_size(). On the other hand, with a 64KB PAGE_SIZE configuration, it's observed that an NVME device can map a size between 300KB~512KB, which certainly failed the swiotlb mappings, though the default pool of swiotlb has many slots: systemd[1]: Started Journal Service. => nvme 0000:00:01.0: swiotlb buffer is full (sz: 327680 bytes), total 32768 (slots), used 32 (slots) note: journal-offline[392] exited with irqs disabled note: journal-offline[392] exited with preempt_count 1 Call trace: [ 3.099918] swiotlb_tbl_map_single+0x214/0x240 [ 3.099921] iommu_dma_map_page+0x218/0x328 [ 3.099928] dma_map_page_attrs+0x2e8/0x3a0 [ 3.101985] nvme_prep_rq.part.0+0x408/0x878 [nvme] [ 3.102308] nvme_queue_rqs+0xc0/0x300 [nvme] [ 3.102313] blk_mq_flush_plug_list.part.0+0x57c/0x600 [ 3.102321] blk_add_rq_to_plug+0x180/0x2a0 [ 3.102323] blk_mq_submit_bio+0x4c8/0x6b8 [ 3.103463] __submit_bio+0x44/0x220 [ 3.103468] submit_bio_noacct_nocheck+0x2b8/0x360 [ 3.103470] submit_bio_noacct+0x180/0x6c8 [ 3.103471] submit_bio+0x34/0x130 [ 3.103473] ext4_bio_write_folio+0x5a4/0x8c8 [ 3.104766] mpage_submit_folio+0xa0/0x100 [ 3.104769] mpage_map_and_submit_buffers+0x1a4/0x400 [ 3.104771] ext4_do_writepages+0x6a0/0xd78 [ 3.105615] ext4_writepages+0x80/0x118 [ 3.105616] do_writepages+0x90/0x1e8 [ 3.105619] filemap_fdatawrite_wbc+0x94/0xe0 [ 3.105622] __filemap_fdatawrite_range+0x68/0xb8 [ 3.106656] file_write_and_wait_range+0x84/0x120 [ 3.106658] ext4_sync_file+0x7c/0x4c0 [ 3.106660] vfs_fsync_range+0x3c/0xa8 [ 3.106663] do_fsync+0x44/0xc0 Since untrusted devices might go down the swiotlb pathway with dma-iommu, these devices should not map a size larger than swiotlb_max_mapping_size. To fix this bug, add iommu_dma_max_mapping_size() for untrusted devices to take into account swiotlb_max_mapping_size() v.s. iova_rcache_range() from the iommu_dma_opt_mapping_size(). Fixes: 82612d66d51d ("iommu: Allow the dma-iommu api to use bounce buffers") Signed-off-by: Nicolin Chen Link: https://lore.kernel.org/r/ee51a3a5c32cf885b18f6416171802669f4a718a.1707851466.git.nicolinc@nvidia.com Acked-by: Robin Murphy [will: Drop redundant is_swiotlb_active(dev) check] Signed-off-by: Will Deacon Reviewed-by: Michael Kelley --- drivers/iommu/dma-iommu.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 50ccc4f1ef81..639efa0c4072 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -1706,6 +1706,14 @@ static size_t iommu_dma_opt_mapping_size(void) return iova_rcache_range(); } +static size_t iommu_dma_max_mapping_size(struct device *dev) +{ + if (dev_is_untrusted(dev)) + return swiotlb_max_mapping_size(dev); + + return SIZE_MAX; +} + static const struct dma_map_ops iommu_dma_ops = { .flags = DMA_F_PCI_P2PDMA_SUPPORTED, .alloc = iommu_dma_alloc, @@ -1728,6 +1736,7 @@ static const struct dma_map_ops iommu_dma_ops = { .unmap_resource = iommu_dma_unmap_resource, .get_merge_boundary = iommu_dma_get_merge_boundary, .opt_mapping_size = iommu_dma_opt_mapping_size, + .max_mapping_size = iommu_dma_max_mapping_size, }; /* From patchwork Wed Feb 28 13:39:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Deacon X-Patchwork-Id: 207874 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp3360920dyb; Wed, 28 Feb 2024 05:59:54 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWQXOJmRKrN1+O2Bfi0EnCGF4wy8RiuLUvA8Ibw84qwKY1rQBPeRsPiB5eKiofngm4Q2crXWUkdLCwQ8F4L2Lz99iNFeQ== X-Google-Smtp-Source: AGHT+IEsKGnyJcyVSYAazxWCFaUHQJEQ0LO29JGFAQx8/WwrnGFLv9RaFE0sDc8aSAj7+goB0bFy X-Received: by 2002:a05:6a21:3511:b0:1a0:fea5:8405 with SMTP id zc17-20020a056a21351100b001a0fea58405mr5359637pzb.41.1709128794189; Wed, 28 Feb 2024 05:59:54 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709128794; cv=pass; d=google.com; s=arc-20160816; b=yAtHcQ9GaPXVAxJVRYaKLRMUx7NmoU2Y3FyhE3XeISDkiNGX/qPxQHmzjp40L81Tb0 0JlDW1bk2JZ2o6uxiWbKoiqNQZ2Aur/SO+l5uH+aOXVr5CyDSEZ3/lfQ5NGvZ9PHfDNp sH2l1m1GcX66OgYB7ZF0LNdqRcqdVsWZrLGJOE96La4rEp1y2FBR2W6zeQsdcB3jNrgw 7sAfNwdf3d/ZwwAlketyvVQkn0pcLt+Q0GajrXPwdFoYkHUG2KDCHDIw/TrH2CALmazH W8Ro+Jvmk/dLW5PrGw/xym2UDo9rRBcS67g18X3eMBZY03TP0HYtAs7cUmquUFfO3x0n 1lxg== 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=yKJtWXMUUZDiSXU7ORRKFKfyXQXAcRzYJs0Ixf4Gg2s=; fh=E8F5/vp/nAcPVJWJwVt5RE8c2HME7kVqVeSMFxe8huE=; b=vadzZl2ZOWNxYi0Jf5C43YZ+SzSViE9sC0/bLBMmYfCvUYw0LQkZ+hy7j6lWm8ahPD GkE7F1xjfjVL3DuAx2eYaf/0V19Ahqfda1b+22n4byRWZevB6Wafky63NEfAdxpX2fsc I7KRelSSxG//fa/e+AiTkBuaXWYc6FHB/bTfvSlIBBoJp+R/9IMSso6b52pgo1ZHjZ+1 T5lFVoD7rFKzXJQESNAlPIqgeQPQKaebaE+gjGDVuAaJwBna6m+MPJLjbaBqWtZ3CF7E RUFP486517aKKS+sBIik9LfRk1rrzBtcKknZY2g3ka7/aimUfgX6DqZh55t+UjHHoATl jnxQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZHQNrkmE; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-85115-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85115-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 e5-20020a656885000000b005d8c55d9669si7430035pgt.366.2024.02.28.05.59.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Feb 2024 05:59:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-85115-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=ZHQNrkmE; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-85115-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85115-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 F0EE2B27A4C for ; Wed, 28 Feb 2024 13:41:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 76CF9159580; Wed, 28 Feb 2024 13:39:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ZHQNrkmE" 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 BA2AF158D83; Wed, 28 Feb 2024 13:39:52 +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=1709127592; cv=none; b=CWtVK5q5VSgRtIiypKjQwA/UYgbBrKEFYF/4eRiuSxYNkjDqmpzUXHQrVdQsAi/BvehnYecJKXgNFkSfUFaukmEa2ZsYJ6Qe5s5HbHBj4aWIUPyKxq22Jf6q9l9agcsxk8jMqp5VB5JyU+ekSa2y3zFgM7wfMtmbDuy3rhpst6U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709127592; c=relaxed/simple; bh=aD3tZMex9G2YUndtdrq37+tQDE+4+Pl1DCirhnA679E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jTA7KXf2yOI5fF1qjcRs65BuzpXmMpJHT0EAtlB1vci3rlaoQdswlacdQScfCeU0FTUEJOPqwzcsJgFTPPAmv5JJ9WIakXQcMFh4DQJpifhNNZ6k8VhtvRtpovD7ypn69WsJO6G84xYwQGYJDJbKQ94n5E3DPQjFSWqLb2KCKZ4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZHQNrkmE; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2895CC433A6; Wed, 28 Feb 2024 13:39:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709127592; bh=aD3tZMex9G2YUndtdrq37+tQDE+4+Pl1DCirhnA679E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZHQNrkmEgy12WEFe8Jv/6qBhkOgpWG2e1degZiyvn0LRnsKBkD8hP4JCuu/CpirRZ E5WF8pyyGcE2r60DIS5WmAChmo8/4oGap1B0RpdsQBkyekKbiqQXkgZisyOndf5F1I CslGRSqiwlNv/3k8HP/jQ3oEozzZ9SRJVXLmjraro47W5uHsqGvgl5iwzEv+p8g86Y SuAOP/Yv0HD5yJun+tFlSvNzuxnwf9mQzayPmRpn9BwYrwTDEu+aLsLwpQoYNHbO3W VugD2V8V/AXwfyavEVwlfOy6PrZtVkypRCqeIldUmtlJVwuBTG8baksKhrDOgEV83W grPbgFR7fX4bQ== 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 6/6] swiotlb: Remove pointless stride adjustment for allocations >= PAGE_SIZE Date: Wed, 28 Feb 2024 13:39:30 +0000 Message-Id: <20240228133930.15400-7-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: 1792151434552043947 X-GMAIL-MSGID: 1792151434552043947 For swiotlb allocations >= PAGE_SIZE, the slab search historically adjusted the stride to avoid checking unaligned slots. However, this is no longer needed now that the code around it has evolved and the stride is calculated from the required alignment. Either 'alloc_align_mask' is used to specify the allocation alignment or the DMA 'min_align_mask' is used to align the allocation with 'orig_addr'. At least one of these masks is always non-zero. In light of that, remove the redundant (and slightly confusing) check. Link: https://lore.kernel.org/r/SN6PR02MB4157089980E6FC58D5557BCED4572@SN6PR02MB4157.namprd02.prod.outlook.com Reported-by: Michael Kelley Signed-off-by: Will Deacon --- kernel/dma/swiotlb.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index c381a7ed718f..0d8805569f5e 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -1006,13 +1006,6 @@ static int swiotlb_search_pool_area(struct device *dev, struct io_tlb_pool *pool */ stride = get_max_slots(max(alloc_align_mask, iotlb_align_mask)); - /* - * For allocations of PAGE_SIZE or larger only look for page aligned - * allocations. - */ - if (alloc_size >= PAGE_SIZE) - stride = umax(stride, PAGE_SHIFT - IO_TLB_SHIFT + 1); - spin_lock_irqsave(&area->lock, flags); if (unlikely(nslots > pool->area_nslabs - area->used)) goto not_found;