Skip to content

Commit 3f31d07

Browse files
Hugh Dickinstorvalds
authored andcommitted
mm/fs: route MADV_REMOVE to FALLOC_FL_PUNCH_HOLE
Now tmpfs supports hole-punching via fallocate(), switch madvise_remove() to use do_fallocate() instead of vmtruncate_range(): which extends madvise(,,MADV_REMOVE) support from tmpfs to ext4, ocfs2 and xfs. There is one more user of vmtruncate_range() in our tree, staging/android's ashmem_shrink(): convert it to use do_fallocate() too (but if its unpinned areas are already unmapped - I don't know - then it would do better to use shmem_truncate_range() directly). Based-on-patch-by: Cong Wang <amwang@redhat.com> Signed-off-by: Hugh Dickins <hughd@google.com> Cc: Christoph Hellwig <hch@infradead.org> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Colin Cross <ccross@android.com> Cc: John Stultz <john.stultz@linaro.org> Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org> Cc: "Theodore Ts'o" <tytso@mit.edu> Cc: Andreas Dilger <adilger@dilger.ca> Cc: Mark Fasheh <mfasheh@suse.de> Cc: Joel Becker <jlbec@evilplan.org> Cc: Dave Chinner <david@fromorbit.com> Cc: Ben Myers <bpm@sgi.com> Cc: Michael Kerrisk <mtk.manpages@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 83e4fa9 commit 3f31d07

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

drivers/staging/android/ashmem.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/module.h>
2020
#include <linux/file.h>
2121
#include <linux/fs.h>
22+
#include <linux/falloc.h>
2223
#include <linux/miscdevice.h>
2324
#include <linux/security.h>
2425
#include <linux/mm.h>
@@ -363,11 +364,12 @@ static int ashmem_shrink(struct shrinker *s, struct shrink_control *sc)
363364

364365
mutex_lock(&ashmem_mutex);
365366
list_for_each_entry_safe(range, next, &ashmem_lru_list, lru) {
366-
struct inode *inode = range->asma->file->f_dentry->d_inode;
367367
loff_t start = range->pgstart * PAGE_SIZE;
368-
loff_t end = (range->pgend + 1) * PAGE_SIZE - 1;
368+
loff_t end = (range->pgend + 1) * PAGE_SIZE;
369369

370-
vmtruncate_range(inode, start, end);
370+
do_fallocate(range->asma->file,
371+
FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
372+
start, end - start);
371373
range->purged = ASHMEM_WAS_PURGED;
372374
lru_del(range);
373375

mm/madvise.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
#include <linux/mempolicy.h>
1212
#include <linux/page-isolation.h>
1313
#include <linux/hugetlb.h>
14+
#include <linux/falloc.h>
1415
#include <linux/sched.h>
1516
#include <linux/ksm.h>
17+
#include <linux/fs.h>
1618

1719
/*
1820
* Any behaviour which results in changes to the vma->vm_flags needs to
@@ -200,8 +202,7 @@ static long madvise_remove(struct vm_area_struct *vma,
200202
struct vm_area_struct **prev,
201203
unsigned long start, unsigned long end)
202204
{
203-
struct address_space *mapping;
204-
loff_t offset, endoff;
205+
loff_t offset;
205206
int error;
206207

207208
*prev = NULL; /* tell sys_madvise we drop mmap_sem */
@@ -217,16 +218,14 @@ static long madvise_remove(struct vm_area_struct *vma,
217218
if ((vma->vm_flags & (VM_SHARED|VM_WRITE)) != (VM_SHARED|VM_WRITE))
218219
return -EACCES;
219220

220-
mapping = vma->vm_file->f_mapping;
221-
222221
offset = (loff_t)(start - vma->vm_start)
223222
+ ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
224-
endoff = (loff_t)(end - vma->vm_start - 1)
225-
+ ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
226223

227-
/* vmtruncate_range needs to take i_mutex */
224+
/* filesystem's fallocate may need to take i_mutex */
228225
up_read(&current->mm->mmap_sem);
229-
error = vmtruncate_range(mapping->host, offset, endoff);
226+
error = do_fallocate(vma->vm_file,
227+
FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
228+
offset, end - start);
230229
down_read(&current->mm->mmap_sem);
231230
return error;
232231
}

0 commit comments

Comments
 (0)