Message ID | 20240228133930.15400-6-will@kernel.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-85114-ouuuleilei=gmail.com@vger.kernel.org> 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 <ouuuleilei@gmail.com> (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 <ouuuleilei@gmail.com>; 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 <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>, Nicolin Chen <nicolinc@nvidia.com>, Michael Kelley <mhklinux@outlook.com> 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: <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: 1792150306780540643 X-GMAIL-MSGID: 1792150306780540643 |
Series |
Fix double allocation in swiotlb_alloc()
|
|
Commit Message
Will Deacon
Feb. 28, 2024, 1:39 p.m. UTC
From: Nicolin Chen <nicolinc@nvidia.com> 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 <nicolinc@nvidia.com> Link: https://lore.kernel.org/r/ee51a3a5c32cf885b18f6416171802669f4a718a.1707851466.git.nicolinc@nvidia.com Acked-by: Robin Murphy <robin.murphy@arm.com> [will: Drop redundant is_swiotlb_active(dev) check] Signed-off-by: Will Deacon <will@kernel.org> --- drivers/iommu/dma-iommu.c | 9 +++++++++ 1 file changed, 9 insertions(+)
Comments
From: Will Deacon <will@kernel.org> Sent: Wednesday, February 28, 2024 5:39 AM > > From: Nicolin Chen <nicolinc@nvidia.com> > > 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 <nicolinc@nvidia.com> > Link: https://lore.kernel.org/all/ee51a3a5c32cf885b18f6416171802669f4a718a.1707851466.git.nicolinc@nvidia.com/ > Acked-by: Robin Murphy <robin.murphy@arm.com> > [will: Drop redundant is_swiotlb_active(dev) check] > Signed-off-by: Will Deacon <will@kernel.org> > --- > 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, > }; > > /* > -- > 2.44.0.rc1.240.g4c46232300-goog Reviewed-by: Michael Kelley <mhklinux@outlook.com>
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, }; /*