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=1796325774; b=Z9HoYTUJ5C1Yo6pvmUXGYi22K4eeEcAB3GFiHqbOR5ohAw0c3h40NdENo5paeG5RpqpU Z62c6mHlBhxQJGbDR2876V6/dA6zv9CsDM4CCjPViX0YSzOvtlRgp86+QUhZJbSsLEC0J BQRLSVVcryMrtsmUeBEZzbgTaIgQj+2Y8gH59vraZvigsX3wq0Cpi54rzBAZvb8H7XETS xL0VndvjkQM/Ck+XpXOp8efyxghkVfEvrPf0q6jAJN96AYwR7hiccFlo53hitKz+Ul2fD FTRFQ2p888K5n7QPHNVrx8WCDgIm4sX5wgTZYBDreNWM5x73/ncTTFekrpJtiwiX9cQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796325774; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=XwyGZaZ9nlcqikLS56L7kB6oqL7DtW+d0QoKrzEde4c=; b=Z2dJN6nDVhPPDwndgdmjAf8vMTQLfv5xllIPQ1XWB/kwg0+p5C5Jxx3MX2eFDQDJgg7B 45LxnfwhaE1SUU2kyi2hnj22+9Oj1sbxOou3ozPg+/gyXRTTvOn7c3vvH8VX1rnMm0AYC QklS32XcNl252gmc98mkbg7cKMiWhw95qfYj62A/8zjOC1Eg3ugMxq0x6dPfd/Up6wsqy 3NEdlNYZzYtpiL1I5WIbfjD+dgDq60ASzMnuVg/WpZxM0albkcytHPyQW4QJ3o8Sk/0Df NZveU6cjseyff3/etJd2c1whAGzUD2H+ZtYLNmsjuRG6IxJHFC3P2Y1cfaq+2ssCxWQ== 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=Z3nkQ63e; 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 7B9D4606A338 for ; Thu, 3 Dec 2026 19:22:54 +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=Z3nkQ63e Received: by smtp.kernel.org (Postfix) with ESMTPSA id 362C51F0092C; Thu, 3 Dec 2026 19:22:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796325773; bh=XwyGZaZ9nlcqikLS56L7kB6oqL7DtW+d0QoKrzEde4c=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Z3nkQ63e8kLXqAcHn3KKkv7k8gLjrJ6Ql5XchHdN+91vukBqQYpNLbabnY1MxjRQH 65okkrZKriSFRY4eiYPxPbxmB1OfkrpHQ/xPaEMZMfMIi3siQcFjNmhhOUqUId1DTN eiUPNNib0BgF6rDT9nr8yXmYthLhizzOMFihrvk5Cm0iDYqhkAVulpVnD0JJX8SQOl xLNdLI9C3nx4CTiyvk/FItWSq9CH7gU3q7fkE5w+Dp3yFmMJzBD5/Fp+rdrRraMxlA cuUNS7a74aukrYvDPnaGt9ktvoG/KmCnJzuJyvtebs35sqg558zs2CaGTu6yOtRBlN cxxYV46aRlaxg== From: Sasha Levin To: stable@vger.kernel.org Cc: Marek Szyprowski , Giorgi Kobakhia , Michael Kelley , Sasha Levin Subject: [PATCH 6.12.y] dma: swiotlb: Round restricted DMA pool size down to IO_TLB_SEGSIZE Date: Thu, 3 Dec 2026 14:22:51 -0500 Message-ID: <20261203192217.628646-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026120222-bottom-shortwave-016f@gregkh> References: <2026120222-bottom-shortwave-016f@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: adapted to older tree: kzalloc(sizeof(*mem), GFP_KERNEL) instead of kzalloc_obj, and the size fix applied to the plain set_memory_decrypted() call (no cc_shared/ret 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 c0cc95297222b..58eba9f70b0f6 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -1849,6 +1849,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) @@ -1870,7 +1883,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