From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796326249; b=tMnoWExqJ1fXsR8ZUvPuNeuFwxfZbnWHZu4h0VjLFxfqREiaqPicoY/cf2RFZnCecHOR RV9C3A1VvrxbVusNSaPPZGc0nGgwpQha/gkdx8HiD31TuksmQPtb7EDrPhhigvx90UVuf Rkl3NBlBx5hBHFOdR8UHxOfF72CVGUejdZbU6E9x2qarvR6I/Dz4K4MiZE0B2orG5rCVs 12vH34js3Es6/BLZyrbTRnOmzPe17nMCKSphlEFi0Vw/kNcFS2KmNgpgi467+yNM412bD ngV5A1LUjc7TkbDumxIl9yrrzLn1CFgApxQOHpbZP5pJFXmGYm4i0NZjF5t8pDqsahA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796326249; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=eRmyOoAqrTuu3HddL6ToZBf803RUC26NZ7FHeYBc+k8=; b=iVGYMl2AzKSrlEh9P4GJJjJnVMSQiNHiNWv6CITTWJWAN4Yk/MVVrV0kLGKNl4L2QBC8 9b4VPh2UshvoHw9v9Um/ctorLbUIKCpG6yv+iWPU258VsYlve1Hw3DUzeCez6O4cxH3pL uzkXCJ+1jEVkWGRvK5UmcLRgocJxMQA7smI9yj34RRsX5GKd++8UK3mYIY42hJktE8coZ 2EhNQ9gvTx78WAxigLOpAN5l96WY4yupgeGIoh38HXqPKJM3fPaUcYJMw3xDi2TQBXxG0 EgLanornzrdw1DVFtPxN/hhqU+UVhzKIDMao07oP7/bJgoFoyNUBz8VHTKswLWWJ1Wg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=OGXwX8il; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 004B9606A685 for ; Thu, 3 Dec 2026 19:30:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=OGXwX8il Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2D5BB1F000CE; Thu, 3 Dec 2026 19:30:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796326248; bh=eRmyOoAqrTuu3HddL6ToZBf803RUC26NZ7FHeYBc+k8=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=OGXwX8ilaleWgR1B46eGgS1eOMR7C1BOx1313IdJHgWsCauJXjgkgjbsfuOVISy6D rvSwyqkZ/yT3IEsf7GSpqZInDJ4G6amEpQ/IhnUh3o3kCejPGnkSH6oNkeV0+eC9Rg 5LkavzGWRkkwwjMrorUxliFt0NWTeGGD554jtu9sbhrb3uIxRjKXgqdy7uQE6X5k9K Z9bZUSNvrZ7Fr5sRqPnEtVhNggKyu9eBEXXs/64dgLcdI4cEtwkpRG6ZkE4ES9CFay F6C6H8dpsa52zhpZV7jnMUrdV5EEmkpAH/iuJtA1Tnew5LwS8UykGuc5qBHu6sRvLj 5ZGqtlx6yh6aw== From: Sasha Levin To: stable@vger.kernel.org Cc: Marek Szyprowski , Giorgi Kobakhia , Michael Kelley , Sasha Levin Subject: [PATCH 6.6.y] dma: swiotlb: Round restricted DMA pool size down to IO_TLB_SEGSIZE Date: Thu, 3 Dec 2026 14:30:46 -0500 Message-ID: <20261203193010.650502-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026120223-underline-scoff-4a47@gregkh> References: <2026120223-underline-scoff-4a47@gregkh> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Marek Szyprowski [ Upstream commit 5b4b87935bf9c6c81b603558c2e167fc27c22a1b ] All other paths creating a swiotlb pool ensure that nslabs is a multiple of IO_TLB_SEGSIZE, and the swiotlb code relies on that. Since commit 0b84e4f8b793 ("swiotlb: Add restricted DMA pool initialization") a restricted pool can have an arbitrary size taken from the device tree. If the pool size is not a multiple of IO_TLB_SEGSIZE, the last segment is partial and swiotlb_release_slots() reads mem->slots[index + nslabs], which is one past the end of the slots array when the freed buffer ends at the end of the pool: BUG: KASAN: slab-out-of-bounds in swiotlb_release_slots Read of size 2 Round the pool size down to a multiple of IO_TLB_SEGSIZE, wasting a bit of memory, and warn about it. Reported-by: Giorgi Kobakhia Closes: https://lore.kernel.org/all/20261026032007.1957949-1-gkobakhi@asu.edu/ Fixes: 0b84e4f8b793 ("swiotlb: Add restricted DMA pool initialization") CC: stable@vger.kernel.org Reviewed-by: Michael Kelley Link: https://lore.kernel.org/r/20261103155016.1107958-1-m.szyprowski@samsung.com Signed-off-by: Marek Szyprowski [ sashal: Replaced kzalloc_obj(*mem) with kzalloc(sizeof(*mem), GFP_KERNEL) and adapted the set_memory_decrypted size change to the older call without the cc_shared/error handling. ] Signed-off-by: Sasha Levin --- kernel/dma/swiotlb.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index c52ffa66eacd8..590032cf21e74 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -1746,6 +1746,19 @@ static int rmem_swiotlb_device_init(struct reserved_mem *rmem, mem = rmem->priv; if (!mem) { struct io_tlb_pool *pool; + unsigned long aligned_nslabs = ALIGN_DOWN(nslabs, IO_TLB_SEGSIZE); + + if (aligned_nslabs != nslabs) { + dev_warn(dev, "Restricted DMA pool size %zu is not a multiple of %lu, rounding down to %lu bytes\n", + (size_t)rmem->size, + (unsigned long)IO_TLB_SEGSIZE << IO_TLB_SHIFT, + aligned_nslabs << IO_TLB_SHIFT); + nslabs = aligned_nslabs; + } + if (!nslabs) { + dev_err(dev, "Restricted DMA pool is too small\n"); + return -EINVAL; + } mem = kzalloc(sizeof(*mem), GFP_KERNEL); if (!mem) @@ -1767,7 +1780,7 @@ static int rmem_swiotlb_device_init(struct reserved_mem *rmem, } set_memory_decrypted((unsigned long)phys_to_virt(rmem->base), - rmem->size >> PAGE_SHIFT); + (nslabs << IO_TLB_SHIFT) >> PAGE_SHIFT); swiotlb_init_io_tlb_pool(pool, rmem->base, phys_to_virt(rmem->base), nslabs, false, nareas); -- 2.53.0