mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Luis Henriques <luis.henriques@canonical.com>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org,
	kernel-team@lists.ubuntu.com
Cc: Mikulas Patocka <mpatocka@redhat.com>,
	Mike Snitzer <snitzer@redhat.com>,
	Luis Henriques <luis.henriques@canonical.com>
Subject: [PATCH 3.5 086/103] dm snapshot: avoid snapshot space leak on crash
Date: Tue, 17 Dec 2013 18:13:15 +0000	[thread overview]
Message-ID: <1387304012-23805-87-git-send-email-luis.henriques@canonical.com> (raw)
In-Reply-To: <1387304012-23805-1-git-send-email-luis.henriques@canonical.com>

3.5.7.28 -stable review patch.  If anyone has any objections, please let me know.

------------------

From: Mikulas Patocka <mpatocka@redhat.com>

commit 230c83afdd9cd384348475bea1e14b80b3b6b1b8 upstream.

There is a possible leak of snapshot space in case of crash.

The reason for space leaking is that chunks in the snapshot device are
allocated sequentially, but they are finished (and stored in the metadata)
out of order, depending on the order in which copying finished.

For example, supposed that the metadata contains the following records
SUPERBLOCK
METADATA (blocks 0 ... 250)
DATA 0
DATA 1
DATA 2
...
DATA 250

Now suppose that you allocate 10 new data blocks 251-260. Suppose that
copying of these blocks finish out of order (block 260 finished first
and the block 251 finished last). Now, the snapshot device looks like
this:
SUPERBLOCK
METADATA (blocks 0 ... 250, 260, 259, 258, 257, 256)
DATA 0
DATA 1
DATA 2
...
DATA 250
DATA 251
DATA 252
DATA 253
DATA 254
DATA 255
METADATA (blocks 255, 254, 253, 252, 251)
DATA 256
DATA 257
DATA 258
DATA 259
DATA 260

Now, if the machine crashes after writing the first metadata block but
before writing the second metadata block, the space for areas DATA 250-255
is leaked, it contains no valid data and it will never be used in the
future.

This patch makes dm-snapshot complete exceptions in the same order they
were allocated, thus fixing this bug.

Note: when backporting this patch to the stable kernel, change the version
field in the following way:
* if version in the stable kernel is {1, 11, 1}, change it to {1, 12, 0}
* if version in the stable kernel is {1, 10, 0} or {1, 10, 1}, change it
  to {1, 10, 2}
Userspace reads the version to determine if the bug was fixed, so the
version change is needed.

Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Signed-off-by: Mike Snitzer <snitzer@redhat.com>
[ luis: backported to 3.5:
  - as requested by author, bumped version field from {1, 10, 0} to {1, 10, 2} ]
Signed-off-by: Luis Henriques <luis.henriques@canonical.com>
---
 drivers/md/dm-snap.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 64 insertions(+), 7 deletions(-)

diff --git a/drivers/md/dm-snap.c b/drivers/md/dm-snap.c
index e106267..b092338 100644
--- a/drivers/md/dm-snap.c
+++ b/drivers/md/dm-snap.c
@@ -66,6 +66,18 @@ struct dm_snapshot {
 
 	atomic_t pending_exceptions_count;
 
+	/* Protected by "lock" */
+	sector_t exception_start_sequence;
+
+	/* Protected by kcopyd single-threaded callback */
+	sector_t exception_complete_sequence;
+
+	/*
+	 * A list of pending exceptions that completed out of order.
+	 * Protected by kcopyd single-threaded callback.
+	 */
+	struct list_head out_of_order_list;
+
 	mempool_t *pending_pool;
 
 	struct dm_exception_table pending;
@@ -171,6 +183,14 @@ struct dm_snap_pending_exception {
 	 */
 	int started;
 
+	/* There was copying error. */
+	int copy_error;
+
+	/* A sequence number, it is used for in-order completion. */
+	sector_t exception_sequence;
+
+	struct list_head out_of_order_entry;
+
 	/*
 	 * For writing a complete chunk, bypassing the copy.
 	 */
@@ -1090,6 +1110,9 @@ static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 	s->valid = 1;
 	s->active = 0;
 	atomic_set(&s->pending_exceptions_count, 0);
+	s->exception_start_sequence = 0;
+	s->exception_complete_sequence = 0;
+	INIT_LIST_HEAD(&s->out_of_order_list);
 	init_rwsem(&s->lock);
 	INIT_LIST_HEAD(&s->list);
 	spin_lock_init(&s->pe_lock);
@@ -1448,6 +1471,19 @@ static void commit_callback(void *context, int success)
 	pending_complete(pe, success);
 }
 
+static void complete_exception(struct dm_snap_pending_exception *pe)
+{
+	struct dm_snapshot *s = pe->snap;
+
+	if (unlikely(pe->copy_error))
+		pending_complete(pe, 0);
+
+	else
+		/* Update the metadata if we are persistent */
+		s->store->type->commit_exception(s->store, &pe->e,
+						 commit_callback, pe);
+}
+
 /*
  * Called when the copy I/O has finished.  kcopyd actually runs
  * this code so don't block.
@@ -1457,13 +1493,32 @@ static void copy_callback(int read_err, unsigned long write_err, void *context)
 	struct dm_snap_pending_exception *pe = context;
 	struct dm_snapshot *s = pe->snap;
 
-	if (read_err || write_err)
-		pending_complete(pe, 0);
+	pe->copy_error = read_err || write_err;
 
-	else
-		/* Update the metadata if we are persistent */
-		s->store->type->commit_exception(s->store, &pe->e,
-						 commit_callback, pe);
+	if (pe->exception_sequence == s->exception_complete_sequence) {
+		s->exception_complete_sequence++;
+		complete_exception(pe);
+
+		while (!list_empty(&s->out_of_order_list)) {
+			pe = list_entry(s->out_of_order_list.next,
+					struct dm_snap_pending_exception, out_of_order_entry);
+			if (pe->exception_sequence != s->exception_complete_sequence)
+				break;
+			s->exception_complete_sequence++;
+			list_del(&pe->out_of_order_entry);
+			complete_exception(pe);
+		}
+	} else {
+		struct list_head *lh;
+		struct dm_snap_pending_exception *pe2;
+
+		list_for_each_prev(lh, &s->out_of_order_list) {
+			pe2 = list_entry(lh, struct dm_snap_pending_exception, out_of_order_entry);
+			if (pe2->exception_sequence < pe->exception_sequence)
+				break;
+		}
+		list_add(&pe->out_of_order_entry, lh);
+	}
 }
 
 /*
@@ -1558,6 +1613,8 @@ __find_pending_exception(struct dm_snapshot *s,
 		return NULL;
 	}
 
+	pe->exception_sequence = s->exception_start_sequence++;
+
 	dm_insert_exception(&s->pending, &pe->e);
 
 	return pe;
@@ -2200,7 +2257,7 @@ static struct target_type origin_target = {
 
 static struct target_type snapshot_target = {
 	.name    = "snapshot",
-	.version = {1, 10, 0},
+	.version = {1, 10, 2},
 	.module  = THIS_MODULE,
 	.ctr     = snapshot_ctr,
 	.dtr     = snapshot_dtr,
-- 
1.8.3.2


  parent reply	other threads:[~2013-12-17 18:21 UTC|newest]

Thread overview: 109+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-17 18:11 [3.5.y.z extended stable] Linux 3.5.7.28 stable review Luis Henriques
2013-12-17 18:11 ` [PATCH 3.5 001/103] [media] lirc_zilog: Don't use dynamic static allocation Luis Henriques
2013-12-17 18:11 ` [PATCH 3.5 002/103] net: Fix "ip rule delete table 256" Luis Henriques
2013-12-17 18:11 ` [PATCH 3.5 003/103] ipv6: use rt6_get_dflt_router to get default router in rt6_route_rcv Luis Henriques
2013-12-17 18:11 ` [PATCH 3.5 004/103] random32: fix off-by-one in seeding requirement Luis Henriques
2013-12-17 18:11 ` [PATCH 3.5 005/103] bonding: don't permit to use ARP monitoring in 802.3ad mode Luis Henriques
2013-12-17 18:11 ` [PATCH 3.5 006/103] 6lowpan: Uncompression of traffic class field was incorrect Luis Henriques
2013-12-17 18:11 ` [PATCH 3.5 007/103] bonding: fix two race conditions in bond_store_updelay/downdelay Luis Henriques
2013-12-17 18:11 ` [PATCH 3.5 008/103] isdnloop: use strlcpy() instead of strcpy() Luis Henriques
2013-12-17 18:11 ` [PATCH 3.5 009/103] connector: improved unaligned access error fix Luis Henriques
2013-12-17 18:11 ` [PATCH 3.5 010/103] ipv4: fix possible seqlock deadlock Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 011/103] inet: prevent leakage of uninitialized memory to user in recv syscalls Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 012/103] atm: idt77252: fix dev refcnt leak Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 013/103] net: core: Always propagate flag changes to interfaces Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 014/103] net: rework recvmsg handler msg_name and msg_namelen logic Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 015/103] net: add BUG_ON if kernel advertises msg_namelen > sizeof(struct sockaddr_storage) Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 016/103] packet: fix use after free race in send path when dev is released Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 017/103] inet: fix addr_len/msg->msg_namelen assignment in recv_error and rxpmtu functions Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 018/103] ipv6: fix leaking uninitialized port number of offender sockaddr Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 019/103] net: update consumers of MSG_MORE to recognize MSG_SENDPAGE_NOTLAST Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 020/103] net: clamp ->msg_namelen instead of returning an error Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 021/103] inet: fix possible seqlock deadlocks Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 022/103] ipv6: fix possible seqlock deadlock in ip6_finish_output2 Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 023/103] af_packet: block BH in prb_shutdown_retire_blk_timer() Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 024/103] {pktgen, xfrm} Update IPv4 header total len and checksum after tranformation Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 025/103] bridge: flush br's address entry in fdb when remove the Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 026/103] mmc: block: fix a bug of error handling in MMC driver Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 027/103] NFSv4: Update list of irrecoverable errors on DELEGRETURN Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 028/103] Update of blkg_stat and blkg_rwstat may happen in bh context. While u64_stats_fetch_retry is only preempt_disable on 32bit UP system. This is not enough to avoid preemption by bh and may read strange 64 bit value Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 029/103] ASoC: wm8990: Mark the register map as dirty when powering down Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 030/103] irq: Enable all irqs unconditionally in irq_resume Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 031/103] xen/gnttab: leave lazy MMU mode in the case of a m2p override failure Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 032/103] [SCSI] libsas: fix usage of ata_tf_to_fis Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 033/103] crypto: authenc - Find proper IV address in ablkcipher callback Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 034/103] s390/crypto: Don't panic after crypto instruction failures Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 035/103] crypto: s390 - Fix aes-xts parameter corruption Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 036/103] crypto: ccm - Fix handling of zero plaintext when computing mac Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 037/103] [SCSI] Disable WRITE SAME for RAID and virtual host adapter drivers Luis Henriques
2013-12-17 22:13   ` Martin K. Petersen
2013-12-18  9:43     ` Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 038/103] ALSA: hda - Fix silent output on ASUS W7J laptop Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 039/103] net: smc91: fix crash regression on the versatile Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 040/103] ARM: footbridge: fix VGA initialisation Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 041/103] [SCSI] hpsa: do not discard scsi status on aborted commands Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 042/103] [SCSI] hpsa: return 0 from driver probe function on success, not 1 Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 043/103] ALSA: hda - Another fixup for ASUS laptop with ALC660 codec Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 044/103] [SCSI] enclosure: fix WARN_ON in dual path device removing Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 045/103] powerpc/gpio: Fix the wrong GPIO input data on MPC8572/MPC8536 Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 046/103] ASoC: wm8731: fix dsp mode configuration Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 047/103] cpuidle: Check for dev before deregistering it Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 048/103] iwlwifi: dvm: don't override mac80211's queue setting Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 049/103] um: add missing declaration of 'getrlimit()' and friends Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 050/103] dm: fix truncated status strings Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 051/103] elevator: acquire q->sysfs_lock in elevator_change() Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 052/103] iommu/vt-d: Fixed interaction of VFIO_IOMMU_MAP_DMA with IOMMU address limits Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 053/103] video: kyro: fix incorrect sizes when copying to userspace Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 054/103] Input: allow deselecting serio drivers even without CONFIG_EXPERT Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 055/103] Input: mousedev - allow disabling " Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 056/103] i2c: i801: SMBus patch for Intel Avoton DeviceIDs Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 057/103] HID: add quirk for Freescale i.MX23 ROM recovery Luis Henriques
2013-12-17 19:39   ` Marek Vasut
2013-12-18 16:16     ` Luis Henriques
2013-12-18 17:13       ` Marek Vasut
2013-12-17 18:12 ` [PATCH 3.5 058/103] HID: hid-multitouch: add support for SiS panels Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 059/103] HID: roccat: fix Coverity CID 141438 Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 060/103] HID: apple: option to swap the 'Option' ("Alt") and 'Command' ("Flag") keys Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 061/103] 9p: send uevent after adding/removing mount_tag attribute Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 062/103] NFSv4 wait on recovery for async session errors Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 063/103] sched: Avoid throttle_cfs_rq() racing with period_timer stopping Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 064/103] nfs: fix do_div() warning by instead using sector_div() Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 065/103] dm delay: fix a possible deadlock due to shared workqueue Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 066/103] mac80211: don't attempt to reorder multicast frames Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 067/103] USB: serial: fix race in generic write Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 068/103] usb: gadget: composite: reset delayed_status on reset_config Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 069/103] usb: dwc3: fix implementation of endpoint wedge Luis Henriques
2013-12-17 18:12 ` [PATCH 3.5 070/103] [media] saa7164: fix return value check in saa7164_initdev() Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 071/103] USB: serial: option: blacklist interface 1 for Huawei E173s-6 Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 072/103] USB: option: support new huawei devices Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 073/103] USB: spcp8x5: correct handling of CS5 setting Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 074/103] USB: mos7840: " Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 075/103] USB: ftdi_sio: fixed handling of unsupported CSIZE setting Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 076/103] USB: pl2303: fixed handling of CS5 setting Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 077/103] USB: cdc-acm: Added support for the Lenovo RD02-D400 USB Modem Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 078/103] drm/radeon: fixup bad vram size on SI Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 079/103] usb: hub: Use correct reset for wedged USB3 devices that are NOTATTACHED Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 080/103] drivers/char/i8k.c: add Dell XPLS L421X Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 081/103] crypto: scatterwalk - Set the chain pointer indication bit Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 082/103] crypto: scatterwalk - Use sg_chain_ptr on chain entries Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 083/103] ARM: 7912/1: check stack pointer in get_wchan Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 084/103] ARM: 7913/1: fix framepointer check in unwind_frame Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 085/103] ALSA: memalloc.h - fix wrong truncation of dma_addr_t Luis Henriques
2013-12-17 18:13 ` Luis Henriques [this message]
2013-12-17 18:13 ` [PATCH 3.5 087/103] dm table: fail dm_table_create on dm_round_up overflow Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 088/103] x86, build, icc: Remove uninitialized_var() from compiler-intel.h Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 089/103] x86, efi: Don't use (U)EFI time services on 32 bit Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 090/103] dm bufio: initialize read-only module parameters Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 091/103] ARM: pxa: tosa: fix keys mapping Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 092/103] hwmon: (w83l786ng) Fix fan speed control mode setting and reporting Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 093/103] hwmon: (w83l768ng) Fix fan speed control range Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 094/103] hwmon: Prevent some divide by zeros in FAN_TO_REG() Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 095/103] futex: fix handling of read-only-mapped hugepages Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 096/103] KVM: Improve create VCPU parameter (CVE-2013-4587) Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 097/103] KVM: x86: Fix potential divide by 0 in lapic (CVE-2013-6367) Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 098/103] KVM: x86: Convert vapic synchronization to _cached functions (CVE-2013-6368) Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 099/103] selinux: handle TCP SYN-ACK packets correctly in selinux_ip_output() Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 100/103] selinux: handle TCP SYN-ACK packets correctly in selinux_ip_postroute() Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 101/103] drivers/rtc/rtc-at91rm9200.c: correct alarm over day/month wrap Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 102/103] MIPS: DMA: For BMIPS5000 cores flush region just like non-coherent R10000 Luis Henriques
2013-12-17 18:13 ` [PATCH 3.5 103/103] xfs: underflow bug in xfs_attrlist_by_handle() Luis Henriques

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1387304012-23805-87-git-send-email-luis.henriques@canonical.com \
    --to=luis.henriques@canonical.com \
    --cc=kernel-team@lists.ubuntu.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mpatocka@redhat.com \
    --cc=snitzer@redhat.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®