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=1796326701; b=dDguE9RgQDQ+SH8L/kF0Uit/c9SM+Wlg+U08RHK00+xp9x5L1r+xs2r4T1seYWpf1XuN IYfzWEXZSaJHFfukldTnEjrCKnVRd0c8gDMiIv7sB0o5OGX11kw1NCJbw37HBh7/i7Azr JDdJKdYDQq9o7xs0FHrMXtCcXZN8MyfnmrqVFXEm05css115GcYQIFbAdkYCGnbAZf9MN 1RuwoXiHNrGaww0mf1MgnscgUOvec4vrDMkfx6WriAcKZs9U/IQ4bEZvo2Ygpi/AyLZe/ mfPvFvtJ1q4qG2tAP6wJDLCmHWlD/y4dosOkv4QRRmnLDWrkKi14wlhjZgP0YNflKGA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796326701; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=blrB5/2vcEeiHTBI6PHYdAx0vlNI2yWMb9ZSytorhZc=; b=UDdQTHO+A7bmNvyFq+RGSjerXOnknLjzny4CUJrB4uZObw/vItpQVwhuw8mllq/JMt12 LOxhb3Ephq9OqjD1qnUGHmhU2cIB+CN5VTsEvxv+8syDAC/logECzB0wXsAuR1+80gQw0 cZKQbrF2Y9opEQywLqQleRG5Uv8ygNGxOUN+doUnJ4sMMYy1aWYuBcOgpUrlCNacfym6x 1zd6oLl6NbAnAtCjebMiWGmS7+rzPAJ19u+zQGF0TSdR8xjzoMzZUJ2LZgXIN5HCsqCew /R//jHeFUqFoXZfK48eTcY6DPUXoJubxgADcc7T36mxDd9R3EyHLLW7Nbi/Z3NXDPRw== 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=hCjMJ5NW; 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 D3F0460A3808 for ; Thu, 3 Dec 2026 19:38:21 +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=hCjMJ5NW Received: by smtp.kernel.org (Postfix) with ESMTPSA id C74CF1F000C2; Thu, 3 Dec 2026 19:38:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796326701; bh=blrB5/2vcEeiHTBI6PHYdAx0vlNI2yWMb9ZSytorhZc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=hCjMJ5NW4SLYxjsJqixhKfu4u99mrrbygZsIFDXG9KgQuajDInwLQ/Vq3fgaYYxdW kK+A8u2zyKmA11dZ+LXUyAGiduZMNmroLhB4WeG2obIWh4eKvZjRuQ3GPvPHLPzQYw BiAMcacIA5o1u15zZLTIx8PyitZGXq36XJSAoAB5fhZrvi3GN8LPDaBdzouqJDu2Pw eFiJCz7vR/CQjgQ3zaX/kQTse5sP+ke3xo2VnZ8TF2B/xkTseC16/FUUbiXA9PA7il tMTjZkzlJ0BuhNunJGs+sVgs/K6X6kXnkBJyiZR8tW6Tdp6FhBwe3DcvBSBv1qNu1j 1YNWo3uZv0rQw== From: Sasha Levin To: stable@vger.kernel.org Cc: Marek Szyprowski , Giorgi Kobakhia , Michael Kelley , Sasha Levin Subject: [PATCH 6.1.y] dma: swiotlb: Round restricted DMA pool size down to IO_TLB_SEGSIZE Date: Thu, 3 Dec 2026 14:38:19 -0500 Message-ID: <20261203193743.667075-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026120224-send-each-3f44@gregkh> References: <2026120224-send-each-3f44@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: Backported by rounding the pool to whole segments in the first-init branch, adapted to the older code that has no separate pool struct and a plain set_memory_decrypted() call without a return check. ] Signed-off-by: Sasha Levin --- kernel/dma/swiotlb.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 6df331909a3b5..28542069edf79 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -1069,6 +1069,20 @@ static int rmem_swiotlb_device_init(struct reserved_mem *rmem, guard(mutex)(&rmem_swiotlb_mutex); mem = rmem->priv; if (!mem) { + 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) return -ENOMEM; @@ -1088,7 +1102,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_mem(mem, rmem->base, nslabs, SWIOTLB_FORCE, false, nareas); mem->for_alloc = true; -- 2.53.0