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: Chris Mason <chris.mason@fusionio.com>,
	Luis Henriques <luis.henriques@canonical.com>
Subject: [PATCH 030/102] Btrfs: fix race between mmap writes and compression
Date: Mon,  8 Apr 2013 10:49:45 +0100	[thread overview]
Message-ID: <1365414657-29191-31-git-send-email-luis.henriques@canonical.com> (raw)
In-Reply-To: <1365414657-29191-1-git-send-email-luis.henriques@canonical.com>

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

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

From: Chris Mason <chris.mason@fusionio.com>

commit 4adaa611020fa6ac65b0ac8db78276af4ec04e63 upstream.

Btrfs uses page_mkwrite to ensure stable pages during
crc calculations and mmap workloads.  We call clear_page_dirty_for_io
before we do any crcs, and this forces any application with the file
mapped to wait for the crc to finish before it is allowed to change
the file.

With compression on, the clear_page_dirty_for_io step is happening after
we've compressed the pages.  This means the applications might be
changing the pages while we are compressing them, and some of those
modifications might not hit the disk.

This commit adds the clear_page_dirty_for_io before compression starts
and makes sure to redirty the page if we have to fallback to
uncompressed IO as well.

Signed-off-by: Chris Mason <chris.mason@fusionio.com>
Reported-by: Alexandre Oliva <oliva@gnu.org>
Signed-off-by: Luis Henriques <luis.henriques@canonical.com>
---
 fs/btrfs/extent_io.c | 33 +++++++++++++++++++++++++++++++++
 fs/btrfs/extent_io.h |  2 ++
 fs/btrfs/inode.c     | 14 ++++++++++++++
 3 files changed, 49 insertions(+)

diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index b89ba9f..9d1970b 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -1229,6 +1229,39 @@ int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
 				GFP_NOFS);
 }
 
+int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
+{
+	unsigned long index = start >> PAGE_CACHE_SHIFT;
+	unsigned long end_index = end >> PAGE_CACHE_SHIFT;
+	struct page *page;
+
+	while (index <= end_index) {
+		page = find_get_page(inode->i_mapping, index);
+		BUG_ON(!page); /* Pages should be in the extent_io_tree */
+		clear_page_dirty_for_io(page);
+		page_cache_release(page);
+		index++;
+	}
+	return 0;
+}
+
+int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
+{
+	unsigned long index = start >> PAGE_CACHE_SHIFT;
+	unsigned long end_index = end >> PAGE_CACHE_SHIFT;
+	struct page *page;
+
+	while (index <= end_index) {
+		page = find_get_page(inode->i_mapping, index);
+		BUG_ON(!page); /* Pages should be in the extent_io_tree */
+		account_page_redirty(page);
+		__set_page_dirty_nobuffers(page);
+		page_cache_release(page);
+		index++;
+	}
+	return 0;
+}
+
 /*
  * helper function to set both pages and extents in the tree writeback
  */
diff --git a/fs/btrfs/extent_io.h b/fs/btrfs/extent_io.h
index 25900af..c5003db 100644
--- a/fs/btrfs/extent_io.h
+++ b/fs/btrfs/extent_io.h
@@ -314,6 +314,8 @@ int map_private_extent_buffer(struct extent_buffer *eb, unsigned long offset,
 		      unsigned long *map_len);
 int extent_range_uptodate(struct extent_io_tree *tree,
 			  u64 start, u64 end);
+int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end);
+int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end);
 int extent_clear_unlock_delalloc(struct inode *inode,
 				struct extent_io_tree *tree,
 				u64 start, u64 end, struct page *locked_page,
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index fadf1c3..4f71dc6 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -349,6 +349,7 @@ static noinline int compress_file_range(struct inode *inode,
 	int i;
 	int will_compress;
 	int compress_type = root->fs_info->compress_type;
+	int redirty = 0;
 
 	/* if this is a small write inside eof, kick off a defrag */
 	if ((end - start + 1) < 16 * 1024 &&
@@ -411,6 +412,17 @@ again:
 		if (BTRFS_I(inode)->force_compress)
 			compress_type = BTRFS_I(inode)->force_compress;
 
+		/*
+		 * we need to call clear_page_dirty_for_io on each
+		 * page in the range.  Otherwise applications with the file
+		 * mmap'd can wander in and change the page contents while
+		 * we are compressing them.
+		 *
+		 * If the compression fails for any reason, we set the pages
+		 * dirty again later on.
+		 */
+		extent_range_clear_dirty_for_io(inode, start, end);
+		redirty = 1;
 		ret = btrfs_compress_pages(compress_type,
 					   inode->i_mapping, start,
 					   total_compressed, pages,
@@ -552,6 +564,8 @@ cleanup_and_bail_uncompressed:
 			__set_page_dirty_nobuffers(locked_page);
 			/* unlocked later on in the async handlers */
 		}
+		if (redirty)
+			extent_range_redirty_for_io(inode, start, end);
 		add_async_extent(async_cow, start, end - start + 1,
 				 0, NULL, 0, BTRFS_COMPRESS_NONE);
 		*num_added += 1;
-- 
1.8.1.2


  parent reply	other threads:[~2013-04-08 10:18 UTC|newest]

Thread overview: 120+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-08  9:49 [ 3.5.y.z extended stable ] Linux 3.5.7.10 stable review Luis Henriques
2013-04-08  9:49 ` [PATCH 001/102] clockevents: Don't allow dummy broadcast timers Luis Henriques
2013-04-08  9:49 ` [PATCH 002/102] Bluetooth: Add support for Dell[QCA 0cf3:0036] Luis Henriques
2013-04-08  9:49 ` [PATCH 003/102] Bluetooth: Add support for Dell[QCA 0cf3:817a] Luis Henriques
2013-04-08  9:49 ` [PATCH 004/102] ath9k_hw: revert chainmask to user configuration after calibration Luis Henriques
2013-04-08  9:49 ` [PATCH 005/102] ath9k: limit tx path hang check to normal data queues Luis Henriques
2013-04-08  9:49 ` [PATCH 006/102] rtlwifi: usb: add missing freeing of skbuff Luis Henriques
2013-04-08  9:49 ` [PATCH 007/102] net/irda: add missing error path release_sock call Luis Henriques
2013-04-08  9:49 ` [PATCH 008/102] NFSv4: Fix the string length returned by the idmapper Luis Henriques
2013-04-08  9:49 ` [PATCH 009/102] pnfs-block: removing DM device maybe cause oops when call dev_remove Luis Henriques
2013-04-08  9:49 ` [PATCH 010/102] NFSv4.1: Fix a race in pNFS layoutcommit Luis Henriques
2013-04-08  9:49 ` [PATCH 011/102] IPoIB: Fix send lockup due to missed TX completion Luis Henriques
2013-04-08  9:49 ` [PATCH 012/102] SUNRPC: Add barriers to ensure read ordering in rpc_wake_up_task_queue_locked Luis Henriques
2013-04-08  9:49 ` [PATCH 013/102] Nest rename_lock inside vfsmount_lock Luis Henriques
2013-04-08  9:49 ` [PATCH 014/102] vt: synchronize_rcu() under spinlock is not nice Luis Henriques
2013-04-08  9:49 ` [PATCH 015/102] iommu/amd: Make sure dma_ops are set for hotplug devices Luis Henriques
2013-04-08  9:49 ` [PATCH 016/102] i915: initialize CADL in opregion Luis Henriques
2013-04-08  9:49 ` [PATCH 017/102] tracing: Protect tracer flags with trace_types_lock Luis Henriques
2013-04-08  9:49 ` [PATCH 018/102] tracing: Prevent buffer overwrite disabled for latency tracers Luis Henriques
2013-04-08  9:49 ` [PATCH 019/102] nohz: Make tick_nohz_irq_exit() irq safe Luis Henriques
2013-04-08  9:49 ` [PATCH 020/102] udf: Fix bitmap overflow on large filesystems with small block size Luis Henriques
2013-04-08  9:49 ` [PATCH 021/102] xen/blkback: correctly respond to unknown, non-native requests Luis Henriques
2013-04-08  9:49 ` [PATCH 022/102] tty: atmel_serial_probe(): index of atmel_ports[] fix Luis Henriques
2013-04-08  9:49 ` [PATCH 023/102] HID: usbhid: quirk for Realtek Multi-card reader Luis Henriques
2013-04-08  9:49 ` [PATCH 024/102] HID: usbhid: quirk for MSI GX680R led panel Luis Henriques
2013-04-08  9:49 ` [PATCH 025/102] xen-blkback: fix dispatch_rw_block_io() error path Luis Henriques
2013-04-08  9:49 ` [PATCH 026/102] sysfs: handle failure path correctly for readdir() Luis Henriques
2013-04-08  9:49 ` [PATCH 027/102] usb: xhci: Fix TRB transfer length macro used for Event TRB Luis Henriques
2013-04-08  9:49 ` [PATCH 028/102] staging: comedi: s626: fix continuous acquisition Luis Henriques
2013-04-08  9:49 ` [PATCH 029/102] USB: serial: fix hang when opening port Luis Henriques
2013-04-08  9:49 ` Luis Henriques [this message]
2013-04-08  9:49 ` [PATCH 031/102] Btrfs: fix space leak when we fail to reserve metadata space Luis Henriques
2013-04-08  9:49 ` [PATCH 032/102] Btrfs: limit the global reserve to 512mb Luis Henriques
2013-04-08  9:49 ` [PATCH 033/102] usb: ftdi_sio: Add support for Mitsubishi FX-USB-AW/-BD Luis Henriques
2013-04-08  9:49 ` [PATCH 034/102] Btrfs: don't drop path when printing out tree errors in scrub Luis Henriques
2013-04-08  9:49 ` [PATCH 035/102] USB: serial: add modem-status-change wait queue Luis Henriques
2013-04-08 10:01   ` Johan Hovold
2013-04-08 10:15     ` Luis Henriques
2013-04-08  9:49 ` [PATCH 036/102] USB: ark3116: fix use-after-free in TIOCMIWAIT Luis Henriques
2013-04-08  9:49 ` [PATCH 037/102] USB: ch341: " Luis Henriques
2013-04-08  9:49 ` [PATCH 038/102] USB: cypress_m8: " Luis Henriques
2013-04-08  9:49 ` [PATCH 039/102] USB: f81232: " Luis Henriques
2013-04-08  9:49 ` [PATCH 040/102] USB: ftdi_sio: " Luis Henriques
2013-04-08  9:49 ` [PATCH 041/102] USB: io_edgeport: " Luis Henriques
2013-04-08  9:49 ` [PATCH 042/102] USB: io_ti: " Luis Henriques
2013-04-08  9:49 ` [PATCH 043/102] USB: mct_u232: " Luis Henriques
2013-04-08  9:49 ` [PATCH 044/102] USB: mos7840: fix broken TIOCMIWAIT Luis Henriques
2013-04-08  9:50 ` [PATCH 045/102] USB: mos7840: fix use-after-free in TIOCMIWAIT Luis Henriques
2013-04-08  9:50 ` [PATCH 046/102] USB: oti6858: " Luis Henriques
2013-04-08  9:50 ` [PATCH 047/102] USB: pl2303: " Luis Henriques
2013-04-08  9:50 ` [PATCH 048/102] USB: quatech2: " Luis Henriques
2013-04-08  9:50 ` [PATCH 049/102] USB: spcp8x5: " Luis Henriques
2013-04-08  9:50 ` [PATCH 050/102] USB: ssu100: " Luis Henriques
2013-04-08  9:50 ` [PATCH 051/102] USB: ti_usb_3410_5052: " Luis Henriques
2013-04-08  9:50 ` [PATCH 052/102] Btrfs: use set_nlink if our i_nlink is 0 Luis Henriques
2013-04-08  9:50 ` [PATCH 053/102] Bluetooth: Fix not closing SCO sockets in the BT_CONNECT2 state Luis Henriques
2013-04-08  9:50 ` [PATCH 054/102] mwifiex: cancel cmd timer and free curr_cmd in shutdown process Luis Henriques
2013-04-08 18:03   ` Bing Zhao
2013-04-09  8:51     ` Luis Henriques
2013-04-08  9:50 ` [PATCH 055/102] HID: usbhid: fix build problem Luis Henriques
2013-04-08  9:50 ` [PATCH 056/102] sysfs: fix race between readdir and lseek Luis Henriques
2013-04-08  9:50 ` [PATCH 057/102] net: remove a WARN_ON() in net_enable_timestamp() Luis Henriques
2013-04-08  9:50 ` [PATCH 058/102] sky2: Receive Overflows not counted Luis Henriques
2013-04-08  9:50 ` [PATCH 059/102] sky2: Threshold for Pause Packet is set wrong Luis Henriques
2013-04-08  9:50 ` [PATCH 060/102] tcp: preserve ACK clocking in TSO Luis Henriques
2013-04-08  9:50 ` [PATCH 061/102] tcp: undo spurious timeout after SACK reneging Luis Henriques
2013-04-08  9:50 ` [PATCH 062/102] 8021q: fix a potential use-after-free Luis Henriques
2013-04-08  9:50 ` [PATCH 063/102] thermal: shorten too long mcast group name Luis Henriques
2013-04-08  9:50 ` [PATCH 064/102] genetlink: trigger BUG_ON if a group name is too long Luis Henriques
2013-04-08  9:50 ` [PATCH 065/102] unix: fix a race condition in unix_release() Luis Henriques
2013-04-08  9:50 ` [PATCH 066/102] bonding: remove already created master sysfs link on failure Luis Henriques
2013-04-08  9:50 ` [PATCH 067/102] bonding: fix miimon and arp_interval delayed work race conditions Luis Henriques
2013-04-08  9:50 ` [PATCH 068/102] bonding: fix disabling of arp_interval and miimon Luis Henriques
2013-04-08  9:50 ` [PATCH 069/102] drivers: net: ethernet: davinci_emac: use netif_wake_queue() while restarting tx queue Luis Henriques
2013-04-08  9:50 ` [PATCH 070/102] drivers: net: ethernet: cpsw: " Luis Henriques
2013-04-08  9:50 ` [PATCH 071/102] net: fix *_DIAG_MAX constants Luis Henriques
2013-04-08  9:50 ` [PATCH 072/102] aoe: reserve enough headroom on skbs Luis Henriques
2013-04-08  9:50 ` [PATCH 073/102] atl1e: drop pci-msi support because of packet corruption Luis Henriques
2013-04-08  9:50 ` [PATCH 074/102] DM9000B: driver initialization upgrade Luis Henriques
2013-04-08  9:50 ` [PATCH 075/102] ipv6: don't accept multicast traffic with scope 0 Luis Henriques
2013-04-08  9:50 ` [PATCH 076/102] ipv6: fix bad free of addrconf_init_net Luis Henriques
2013-04-08  9:50 ` [PATCH 077/102] ipv6: don't accept node local multicast traffic from the wire Luis Henriques
2013-04-08  9:50 ` [PATCH 078/102] ks8851: Fix interpretation of rxlen field Luis Henriques
2013-04-08  9:50 ` [PATCH 079/102] net: add a synchronize_net() in netdev_rx_handler_unregister() Luis Henriques
2013-04-08  9:50 ` [PATCH 080/102] pch_gbe: fix ip_summed checksum reporting on rx Luis Henriques
2013-04-08  9:50 ` [PATCH 081/102] smsc75xx: fix jumbo frame support Luis Henriques
2013-04-08  9:50 ` [PATCH 082/102] bonding: get netdev_rx_handler_unregister out of locks Luis Henriques
2013-04-08  9:50 ` [PATCH 083/102] mac80211: always synchronize_net() during station removal Luis Henriques
2013-04-08  9:50 ` [PATCH 084/102] regmap: cache Fix regcache-rbtree sync Luis Henriques
2013-04-08  9:50 ` [PATCH 085/102] iwlwifi: dvm: don't send HCMD in restart flow Luis Henriques
2013-04-08  9:50 ` [PATCH 086/102] nfsd4: reject "negative" acl lengths Luis Henriques
2013-04-08  9:50 ` [PATCH 087/102] can: sja1000: fix define conflict on SH Luis Henriques
2013-04-08  9:50 ` [PATCH 088/102] b43: N-PHY: increase initial value of "mind" in RSSI calibration Luis Henriques
2013-04-08  9:50 ` [PATCH 089/102] b43: A fix for DMA transmission sequence errors Luis Henriques
2013-04-08  9:50 ` [PATCH 090/102] b43: N-PHY: use more bits for offset in RSSI calibration Luis Henriques
2013-04-08  9:50 ` [PATCH 091/102] tg3: fix length overflow in VPD firmware parsing Luis Henriques
2013-04-08  9:50 ` [PATCH 092/102] tile: expect new initramfs name from hypervisor file system Luis Henriques
2013-04-08  9:50 ` [PATCH 093/102] virtio: console: rename cvq_lock to c_ivq_lock Luis Henriques
2013-04-08  9:50 ` [PATCH 094/102] virtio: console: add locking around c_ovq operations Luis Henriques
2013-04-08  9:50 ` [PATCH 095/102] ARM: cns3xxx: fix mapping of private memory region Luis Henriques
2013-04-08  9:50 ` [PATCH 096/102] loop: prevent bdev freeing while device in use Luis Henriques
2013-04-08  9:50 ` [PATCH 097/102] efivars: explicitly calculate length of VariableName Luis Henriques
2013-04-09 22:45   ` Ben Hutchings
2013-04-10  9:35     ` Luis Henriques
2013-04-10 10:27     ` Lingzhu Xiang
2013-04-10 12:17       ` Luis Henriques
2013-04-10 15:57         ` Seiji Aguchi
2013-04-11  9:12           ` Luis Henriques
2013-04-16 10:33             ` Luis Henriques
2013-04-17  4:37               ` Lingzhu Xiang
2013-04-17 11:56                 ` Andy Whitcroft
2013-04-17 12:13                   ` Lingzhu Xiang
2013-04-17 13:28                     ` Luis Henriques
2013-04-18  3:27                       ` Lingzhu Xiang
2013-04-18  8:58                         ` Luis Henriques
2013-04-08  9:50 ` [PATCH 098/102] efivars: Handle duplicate names from get_next_variable() Luis Henriques
2013-04-08  9:50 ` [PATCH 099/102] thermal: return an error on failure to register thermal class Luis Henriques
2013-04-08  9:50 ` [PATCH 100/102] UBIFS: make space fixup work in the remount case Luis Henriques
2013-04-08  9:50 ` [PATCH 101/102] reiserfs: Fix warning and inode leak when deleting inode with xattrs Luis Henriques
2013-04-08  9:50 ` [PATCH 102/102] mm: prevent mmap_cache race in find_vma() 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=1365414657-29191-31-git-send-email-luis.henriques@canonical.com \
    --to=luis.henriques@canonical.com \
    --cc=chris.mason@fusionio.com \
    --cc=kernel-team@lists.ubuntu.com \
    --cc=linux-kernel@vger.kernel.org \
    --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®