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=1796388319; b=HXM4u13qwnt+34yraY6oBn/mmAuK7I4PNWP+byNLJKoQqLye/A9L8CYkL9cTmOOl2CZo 6Bku5fDp9AI+0PnDa3kmUSfLWaXz4eRe4lOvCd4F1lilZDqOv/E4Gi25VSmSYgVcmqg7t DEigl8rrN7km2H4w5HubdLrYTY22hwbayB0DPZo9Akk+58EwpcxuCL1sA1KfXivK6s1GM c2cqEC4EhUB7gsh+mkgdFz4HqNlYTZxhQi7UL4iiMVpnybkhjNYivZEpLmIWfiTvvdfOi xN3WaviU3QO/kOy38I5tHOzT2bTlcGLYO4F32Y3iSdtxSjyWea/ZJhJbuNG0OmVakDA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796388319; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=33b9Jn1f6ylbPy9oZ1dCOypsjMX5Joihd/MUC7I4T9w=; b=CRISt+7hrEUUE/2nUcJV62qYVx1mGcU/LZRKhf+9oLkb/hlOiUJ/Lea/GXiQmBVIjNr+ 1Dcqo1gvQ+OgtUr5JIq3wApoWVYi9ewSWirceGHnECOkQyZJmZFNtvpED+4YpTxpJY5Kg lJeELIwp/NiaQxifW2VWeiik1Ngy8xM5dXLcnpoFvXtJO5lLpndc3W1sDeCO378MUO1tk fOl3GEZzbMD1kxOLxyXWv7xvWCqb7XJCmnEfnFNoO+IVU7nyDr3UgtkzPA6dnLqgPUrvC vRSG3LmAwaRz+Jhba9Gh4rEURAeKohcZwYfkW3wnUiPqyNWH1sa+VqoETbwzd5cgWtA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b="kaRJjqk/"; 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 0619E606A32C; Fri, 4 Dec 2026 12:45:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=kaRJjqk/ Received: by smtp.kernel.org (Postfix) with ESMTPSA id C7ECC1F000C2; Fri, 4 Dec 2026 12:45:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796388318; bh=33b9Jn1f6ylbPy9oZ1dCOypsjMX5Joihd/MUC7I4T9w=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=kaRJjqk//TxKO3G8o97mKMgH9eWluD3PHBLsE7qZKrp3eyy5BtPBr5pwD29E9vXlk tXEUmty6bhsCdb/r/31LMmpc6z9zks0QcEtMeycz090WZ+XKQaeh87i+CPAqSPLoqf ohFI4rMkKWCIkR5ER9ia0+zAVFegaqkwB9d9AyJU= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Chen Cheng , Mikulas Patocka Subject: [PATCH 6.1 0568/1059] dm snapshot: fix discard writing into the next COW chunk Date: Fri, 4 Dec 2026 19:01:39 +0900 Message-ID: <20261204100658.873016518@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100643.980430235@linuxfoundation.org> References: <20261204100643.980430235@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Chen Cheng commit 73174b4bb6fcd0e6cf973c9402a0fae2e567ad9f upstream. zero_exception() zeroes one chunk of the COW device and takes the start sector from bio->bi_iter.bi_sector. remap_exception() has already set that to the COW chunk start plus the offset inside the chunk. That offset is needed for reads and writes, but for a discard it is wrong: the zeroing window is shifted, so the head of the target chunk keeps its old data and the tail lands in the next COW chunk, which holds a different exception. io_overlaps_chunk() should have caught this, but it only compares the length. "maps to entire chunks" in snapshot.rst also means the discard has to start on a chunk boundary. A discard that is not a full chunk has a second problem: after remap_exception() it is sent to the COW device, where it hits a chunk that still backs a live exception. After a discard the contents are undefined, but the exception stays in the table and keeps pointing at that chunk, so a later read returns whatever the discard left instead of the snapshot's data. Reproducer: truncate -s 4M o.img c.img o=$(losetup -f --show o.img) c=$(losetup -f --show c.img) dmsetup create snap --table \ "0 8192 snapshot $o $c P 512 1 discard_zeroes_cow" # write two chunks, this creates two exceptions head -c 256K /dev/zero | tr '\000' '\252' > pat dd if=pat of=/dev/mapper/snap bs=256K seek=1 count=1 conv=notrunc dd if=pat of=/dev/mapper/snap bs=256K seek=2 count=1 conv=notrunc # shift the snapshot by 8 sectors, so a discard that is aligned on # top is unaligned inside the snapshot dmsetup create top --table "0 8184 linear /dev/mapper/snap 8" blkdiscard -o 262144 -l 262144 /dev/mapper/top sync echo 3 > /proc/sys/vm/drop_caches # chunk 1 dd if=/dev/mapper/snap bs=512 skip=512 count=8 | od -An -tx1 -N1 # chunk 2 dd if=/dev/mapper/snap bs=512 skip=1024 count=8 | od -An -tx1 -N1 Before: aa 00 <- chunk 2 was zeroed although it was not discarded After: aa aa <- nothing written, a discard is only a hint Fix it by requiring the discard to start on a chunk boundary in io_overlaps_chunk(), and by finishing a discard that is not a full chunk with bio_endio() instead of passing it down to the COW device. Fixes: 2e6023850e17 ("dm snapshot: add optional discard support features") Cc: stable@vger.kernel.org Signed-off-by: Chen Cheng Signed-off-by: Mikulas Patocka Signed-off-by: Greg Kroah-Hartman --- drivers/md/dm-snap.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) --- a/drivers/md/dm-snap.c +++ b/drivers/md/dm-snap.c @@ -1958,8 +1958,8 @@ static void zero_exception(struct dm_sna static bool io_overlaps_chunk(struct dm_snapshot *s, struct bio *bio) { - return bio->bi_iter.bi_size == - (s->store->chunk_size << SECTOR_SHIFT); + return bio->bi_iter.bi_size == (s->store->chunk_size << SECTOR_SHIFT) && + !(bio->bi_iter.bi_sector & s->store->chunk_mask); } static int snapshot_map(struct dm_target *ti, struct bio *bio) @@ -2019,11 +2019,13 @@ static int snapshot_map(struct dm_target e = dm_lookup_exception(&s->complete, chunk); if (e) { remap_exception(s, e, bio, chunk); - if (unlikely(bio_op(bio) == REQ_OP_DISCARD) && - io_overlaps_chunk(s, bio)) { + if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) { dm_exception_table_unlock(&lock); up_read(&s->lock); - zero_exception(s, e, bio, chunk); + if (io_overlaps_chunk(s, bio)) + zero_exception(s, e, bio, chunk); + else + bio_endio(bio); r = DM_MAPIO_SUBMITTED; /* discard is not issued */ goto out; }