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=1796325076; b=RvsU1LCworPjUeFmAE2odDmZnblTelLzbVR2bYlg5v0Lmk7uGmADJtWgDkCRQW2CpMQv HNukOaiI1ZPVgtMLNhJFzMVcrXCqv2saYknjw2CRsxvFQuaqowMyjUVVcPqVH6VANagd4 z56uRCA7MIAqmEb7P3k86w5IllG1oKKDvwiN3ItoQW1Iy+gNl5OfKRsSEDmGrAVeoNn8h czQ7Vv1TGlpzsaZIDjPI+RUJszmAAwqNFsqA6498BaqqBV31J6ld2MB9LXT/v7Lforznr 4VKKtWgg4Cilpaug0wWSNecaCpWpkP3evqpeyyao8LhGwJJMdQqVQgHRQdAgN98wF0g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796325076; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=yLmMLOHQppA+d+MmvDI0ItVQ4sIGA0ARSgbY8ANuq0s=; b=F4F0sNW0EpIcy/d6ZZPSnAUa4ncgRxYb0QhEw8TdSCy/Y44G/GRU0X4wxqmGObblVX6r bWzWYJYU6QIkr12CoUrxwovPlPuFUCMIdOA494S9oTBHlvE3KtfpOOG5XqRbxhS1psK7d dWa0SwuBEvX6d6jnslip1lmuVtUj97Bo2aRoqZKXVKjmoJjG4bvfmge1Iqw5WOfTurEKG fOZTBwpyGmaSgkEvPry9goudf6ocqqWp04CThyoPlGiElbSQARXqqV6hlOKjtq8AGiFT4 S3sO6Tqgfq9wIjEATW6dRoxaoGiKjPvaiWI4vI+vPw1SPsppVVaiZPUb6KIt1gOV3cw== 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=Xbh5gHbE; 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 EFC9D60A2604 for ; Thu, 3 Dec 2026 19:11:15 +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=Xbh5gHbE Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1F11F1F000C2; Thu, 3 Dec 2026 19:11:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796325075; bh=yLmMLOHQppA+d+MmvDI0ItVQ4sIGA0ARSgbY8ANuq0s=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Xbh5gHbEmfO2x/kALBmx7NYWN6qGT76o1Gz9Mvo3NV88F7bXGFNhmD5OT/TabFTVi 0WrYMS1xpn3n69+kwPpW0Wa3m8hO0QSbokIgoGv02jernSILf1gq+aLvitbVkL74DH 7KRkuvgmhYUlCizXA8MvrDsMqxiGrHVu4s2rAW1+SP0ulGtykVFjtWbQIFrVW+20EW OZmShUHrxYx2LAoxHiu7QfAl7xdO+eTpxt1DZGZj1GzmTXUmmAjIQ8v3IvBSw64M/A ak+wuotM6HOhqMNpv+DY0ne0gIM5Wu6VXTdhygH882J4Mpa+jOoDUwBFkHTGZIVeoW WisZ0qSs/FUsg== From: Sasha Levin To: stable@vger.kernel.org Cc: Marek Szyprowski , Giorgi Kobakhia , Michael Kelley , Sasha Levin Subject: [PATCH 6.18.y] dma: swiotlb: Round restricted DMA pool size down to IO_TLB_SEGSIZE Date: Thu, 3 Dec 2026 14:11:13 -0500 Message-ID: <20261203191032.607258-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026120222-jury-tactful-9b18@gregkh> References: <2026120222-jury-tactful-9b18@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 the set_memory_decrypted() hunk to the base's bare call (no cc_shared or error check) and used plain kzalloc(sizeof(*mem), GFP_KERNEL) in place of kzalloc_obj(). ] 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 3bb91e84409c3..ff49355d4801e 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