aboutsummaryrefslogtreecommitdiffstats
path: root/mm
diff options
context:
space:
mode:
authorHugh Dickins <hughd@google.com>2014-06-23 16:22:06 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2014-06-23 19:47:44 -0400
commitf00cdc6df7d7cfcabb5b740911e6788cb0802bdb (patch)
tree2d540f918e013b5dcca920e904c264c703c794eb /mm
parentf72e7dcdd25229446b102e587ef2f826f76bff28 (diff)
shmem: fix faulting into a hole while it's punched
Trinity finds that mmap access to a hole while it's punched from shmem can prevent the madvise(MADV_REMOVE) or fallocate(FALLOC_FL_PUNCH_HOLE) from completing, until the reader chooses to stop; with the puncher's hold on i_mutex locking out all other writers until it can complete. It appears that the tmpfs fault path is too light in comparison with its hole-punching path, lacking an i_data_sem to obstruct it; but we don't want to slow down the common case. Extend shmem_fallocate()'s existing range notification mechanism, so shmem_fault() can refrain from faulting pages into the hole while it's punched, waiting instead on i_mutex (when safe to sleep; or repeatedly faulting when not). [akpm@linux-foundation.org: coding-style fixes] Signed-off-by: Hugh Dickins <hughd@google.com> Reported-by: Sasha Levin <sasha.levin@oracle.com> Tested-by: Sasha Levin <sasha.levin@oracle.com> Cc: Dave Jones <davej@redhat.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'mm')
-rw-r--r--mm/shmem.c56
1 files changed, 52 insertions, 4 deletions
diff --git a/mm/shmem.c b/mm/shmem.c
index 91b912b37b8c..8f419cff9e34 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -80,11 +80,12 @@ static struct vfsmount *shm_mnt;
80#define SHORT_SYMLINK_LEN 128 80#define SHORT_SYMLINK_LEN 128
81 81
82/* 82/*
83 * shmem_fallocate and shmem_writepage communicate via inode->i_private 83 * shmem_fallocate communicates with shmem_fault or shmem_writepage via
84 * (with i_mutex making sure that it has only one user at a time): 84 * inode->i_private (with i_mutex making sure that it has only one user at
85 * we would prefer not to enlarge the shmem inode just for that. 85 * a time): we would prefer not to enlarge the shmem inode just for that.
86 */ 86 */
87struct shmem_falloc { 87struct shmem_falloc {
88 int mode; /* FALLOC_FL mode currently operating */
88 pgoff_t start; /* start of range currently being fallocated */ 89 pgoff_t start; /* start of range currently being fallocated */
89 pgoff_t next; /* the next page offset to be fallocated */ 90 pgoff_t next; /* the next page offset to be fallocated */
90 pgoff_t nr_falloced; /* how many new pages have been fallocated */ 91 pgoff_t nr_falloced; /* how many new pages have been fallocated */
@@ -759,6 +760,7 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)
759 spin_lock(&inode->i_lock); 760 spin_lock(&inode->i_lock);
760 shmem_falloc = inode->i_private; 761 shmem_falloc = inode->i_private;
761 if (shmem_falloc && 762 if (shmem_falloc &&
763 !shmem_falloc->mode &&
762 index >= shmem_falloc->start && 764 index >= shmem_falloc->start &&
763 index < shmem_falloc->next) 765 index < shmem_falloc->next)
764 shmem_falloc->nr_unswapped++; 766 shmem_falloc->nr_unswapped++;
@@ -1233,6 +1235,44 @@ static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1233 int error; 1235 int error;
1234 int ret = VM_FAULT_LOCKED; 1236 int ret = VM_FAULT_LOCKED;
1235 1237
1238 /*
1239 * Trinity finds that probing a hole which tmpfs is punching can
1240 * prevent the hole-punch from ever completing: which in turn
1241 * locks writers out with its hold on i_mutex. So refrain from
1242 * faulting pages into the hole while it's being punched, and
1243 * wait on i_mutex to be released if vmf->flags permits.
1244 */
1245 if (unlikely(inode->i_private)) {
1246 struct shmem_falloc *shmem_falloc;
1247
1248 spin_lock(&inode->i_lock);
1249 shmem_falloc = inode->i_private;
1250 if (!shmem_falloc ||
1251 shmem_falloc->mode != FALLOC_FL_PUNCH_HOLE ||
1252 vmf->pgoff < shmem_falloc->start ||
1253 vmf->pgoff >= shmem_falloc->next)
1254 shmem_falloc = NULL;
1255 spin_unlock(&inode->i_lock);
1256 /*
1257 * i_lock has protected us from taking shmem_falloc seriously
1258 * once return from shmem_fallocate() went back up that stack.
1259 * i_lock does not serialize with i_mutex at all, but it does
1260 * not matter if sometimes we wait unnecessarily, or sometimes
1261 * miss out on waiting: we just need to make those cases rare.
1262 */
1263 if (shmem_falloc) {
1264 if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
1265 !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
1266 up_read(&vma->vm_mm->mmap_sem);
1267 mutex_lock(&inode->i_mutex);
1268 mutex_unlock(&inode->i_mutex);
1269 return VM_FAULT_RETRY;
1270 }
1271 /* cond_resched? Leave that to GUP or return to user */
1272 return VM_FAULT_NOPAGE;
1273 }
1274 }
1275
1236 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret); 1276 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
1237 if (error) 1277 if (error)
1238 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS); 1278 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
@@ -1729,18 +1769,26 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset,
1729 1769
1730 mutex_lock(&inode->i_mutex); 1770 mutex_lock(&inode->i_mutex);
1731 1771
1772 shmem_falloc.mode = mode & ~FALLOC_FL_KEEP_SIZE;
1773
1732 if (mode & FALLOC_FL_PUNCH_HOLE) { 1774 if (mode & FALLOC_FL_PUNCH_HOLE) {
1733 struct address_space *mapping = file->f_mapping; 1775 struct address_space *mapping = file->f_mapping;
1734 loff_t unmap_start = round_up(offset, PAGE_SIZE); 1776 loff_t unmap_start = round_up(offset, PAGE_SIZE);
1735 loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1; 1777 loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
1736 1778
1779 shmem_falloc.start = unmap_start >> PAGE_SHIFT;
1780 shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
1781 spin_lock(&inode->i_lock);
1782 inode->i_private = &shmem_falloc;
1783 spin_unlock(&inode->i_lock);
1784
1737 if ((u64)unmap_end > (u64)unmap_start) 1785 if ((u64)unmap_end > (u64)unmap_start)
1738 unmap_mapping_range(mapping, unmap_start, 1786 unmap_mapping_range(mapping, unmap_start,
1739 1 + unmap_end - unmap_start, 0); 1787 1 + unmap_end - unmap_start, 0);
1740 shmem_truncate_range(inode, offset, offset + len - 1); 1788 shmem_truncate_range(inode, offset, offset + len - 1);
1741 /* No need to unmap again: hole-punching leaves COWed pages */ 1789 /* No need to unmap again: hole-punching leaves COWed pages */
1742 error = 0; 1790 error = 0;
1743 goto out; 1791 goto undone;
1744 } 1792 }
1745 1793
1746 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */ 1794 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */