From mboxrd@z Thu Jan 1 00:00:00 1970 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 6C0A332B99B; Mon, 29 Dec 2025 16:14:20 +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=1767024860; cv=none; b=HOaQsBp9uDxBmPq4hMKvGKmsRnqYbNwj+BpzeDePN14K2R+LAXVGCjxR/InyfDxpY2C+jicyF+3shtRpmEIGj14FFIJeCbW+fjZgBdpe2Ltaa/3YaXuEKd96XzR03vFOkoVIL4RVDVrEiGf9NwfFVNZdKGLIvexyDVnKUtDnJJ4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767024860; c=relaxed/simple; bh=LGavTELx2z3r9W1bdLdmCfH3fUqX3mEvrkuBXH6OX3o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LaQ6MSEddj1faAJVzHrYHu5eFzxZIpkp138rwUcrKlMWI6f9V+Ii+Wnf2KGWN67H6pjKidXu6GU5WmVHHs+pe2ssUbBLfBh8NF27fOqNUiFQjbdc7KzOAvFjueuPjg5C2MIkbKks91ju43Hk3613Cbpng0Su+O5G/Gb7DcBxBWs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=2TIP9uba; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="2TIP9uba" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B5D4EC4CEF7; Mon, 29 Dec 2025 16:14:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1767024860; bh=LGavTELx2z3r9W1bdLdmCfH3fUqX3mEvrkuBXH6OX3o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2TIP9ubaHxxx1ES7I7/sb56dl9xf+CmsHUM2UZo6c1i1OD0z+VcBKLBTg6Hg63H5+ 7fYLySX6B0C8EeyaFWI3SjTvlrrLi0ywrGlS1DpIlJRZLjV0luYoVhnJofhK89kebG NixVCYjxifsRV5vRJlF1XKeZGdls2YzerGDGMbYU= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Joanne Koong , Christoph Hellwig , "Darrick J. Wong" , Christian Brauner , Sasha Levin Subject: [PATCH 6.18 010/430] iomap: account for unaligned end offsets when truncating read range Date: Mon, 29 Dec 2025 17:06:52 +0100 Message-ID: <20251229160724.530282649@linuxfoundation.org> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20251229160724.139406961@linuxfoundation.org> References: <20251229160724.139406961@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Joanne Koong [ Upstream commit 9d875e0eef8ec15b6b1da0cb9a0f8ed13efee89e ] The end position to start truncating from may be at an offset into a block, which under the current logic would result in overtruncation. Adjust the calculation to account for unaligned end offsets. Signed-off-by: Joanne Koong Link: https://patch.msgid.link/20251111193658.3495942-3-joannelkoong@gmail.com Reviewed-by: Christoph Hellwig Reviewed-by: Darrick J. Wong Signed-off-by: Christian Brauner Signed-off-by: Sasha Levin --- fs/iomap/buffered-io.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c index 1c95a0a7b302d..c0fa6acf19375 100644 --- a/fs/iomap/buffered-io.c +++ b/fs/iomap/buffered-io.c @@ -217,6 +217,22 @@ static void ifs_free(struct folio *folio) kfree(ifs); } +/* + * Calculate how many bytes to truncate based off the number of blocks to + * truncate and the end position to start truncating from. + */ +static size_t iomap_bytes_to_truncate(loff_t end_pos, unsigned block_bits, + unsigned blocks_truncated) +{ + unsigned block_size = 1 << block_bits; + unsigned block_offset = end_pos & (block_size - 1); + + if (!block_offset) + return blocks_truncated << block_bits; + + return ((blocks_truncated - 1) << block_bits) + block_offset; +} + /* * Calculate the range inside the folio that we actually need to read. */ @@ -262,7 +278,8 @@ static void iomap_adjust_read_range(struct inode *inode, struct folio *folio, /* truncate len if we find any trailing uptodate block(s) */ while (++i <= last) { if (ifs_block_is_uptodate(ifs, i)) { - plen -= (last - i + 1) * block_size; + plen -= iomap_bytes_to_truncate(*pos + plen, + block_bits, last - i + 1); last = i - 1; break; } @@ -278,7 +295,8 @@ static void iomap_adjust_read_range(struct inode *inode, struct folio *folio, unsigned end = offset_in_folio(folio, isize - 1) >> block_bits; if (first <= end && last > end) - plen -= (last - end) * block_size; + plen -= iomap_bytes_to_truncate(*pos + plen, block_bits, + last - end); } *offp = poff; -- 2.51.0