From: Badari Pulavarty Here is the patch to implement madvise(MADV_REMOVE) - which frees up a given range of pages & its associated backing store. Current implementation supports only shmfs/tmpfs and other filesystems return -ENOSYS. "Some app allocates large tmpfs files, then when some task quits and some client disconnect, some memory can be released. However the only way to release tmpfs-swap is to MADV_REMOVE". - Andrea Arcangeli This feature is also useful for supporting hot-plug memory on UML. Concerns raised by Andrew Morton: - "We have no plan for holepunching! If we _do_ have such a plan (or might in the future) then what would the API look like? I think sys_holepunch(fd, start, len), so we should start out with that." - Using madvise is very weird, because people will ask "why do I need to mmap my file before I can stick a hole in it?" - None of the other madvise operations call into the filesystem in this manner. A broad question is: is this capability an MM operation or a filesytem operation? truncate, for example, is a filesystem operation which sometimes has MM side-effects. madvise is an mm operation and with this patch, it gains FS side-effects, only they're really, really significant ones." Comments: - Andrea suggested the fs operation too but then it's more efficient to have it as a mm operation with fs side effects, because they don't immediatly know fd and physical offset of the range. It's possible to fixup in userland and to use the fs operation but it's more expensive, the vmas are already in the kernel and we can use them. Short term plan & Future Direction: - We seem to need this interaface only for shmfs/tmpfs files in the short term. We have to add hooks into the filesystem for correctness and completeness. This is what this patch does. - In the future, plan is to support both fs and mmap apis also. This also involves (other) filesystem specific functions to be implemented. - Current patch doesn't support VM_NONLINEAR - which can be addressed in the future. Signed-off-by: Badari Pulavarty Cc: Hugh Dickins Cc: Andrea Arcangeli Signed-off-by: Andrew Morton --- include/asm-alpha/mman.h | 1 + include/asm-arm/mman.h | 1 + include/asm-arm26/mman.h | 1 + include/asm-cris/mman.h | 1 + include/asm-frv/mman.h | 1 + include/asm-h8300/mman.h | 1 + include/asm-i386/mman.h | 1 + include/asm-ia64/mman.h | 1 + include/asm-m32r/mman.h | 1 + include/asm-m68k/mman.h | 1 + include/asm-mips/mman.h | 1 + include/asm-parisc/mman.h | 1 + include/asm-powerpc/mman.h | 1 + include/asm-s390/mman.h | 1 + include/asm-sh/mman.h | 1 + include/asm-sparc/mman.h | 1 + include/asm-sparc64/mman.h | 1 + include/asm-v850/mman.h | 1 + include/asm-x86_64/mman.h | 1 + include/asm-xtensa/mman.h | 1 + include/linux/fs.h | 1 + include/linux/mm.h | 1 + mm/madvise.c | 38 ++++++++++++++++++++++++++++++++++++++ mm/memory.c | 26 ++++++++++++++++++++++++++ mm/shmem.c | 32 ++++++++++++++++++++++++-------- 25 files changed, 110 insertions(+), 8 deletions(-) diff -puN include/asm-alpha/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store include/asm-alpha/mman.h --- devel/include/asm-alpha/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store 2005-11-11 16:09:13.000000000 -0800 +++ devel-akpm/include/asm-alpha/mman.h 2005-11-11 16:09:13.000000000 -0800 @@ -42,6 +42,7 @@ #define MADV_WILLNEED 3 /* will need these pages */ #define MADV_SPACEAVAIL 5 /* ensure resources are available */ #define MADV_DONTNEED 6 /* don't need these pages */ +#define MADV_REMOVE 7 /* remove these pages & resources */ /* compatibility flags */ #define MAP_ANON MAP_ANONYMOUS diff -puN include/asm-arm26/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store include/asm-arm26/mman.h --- devel/include/asm-arm26/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store 2005-11-11 16:09:13.000000000 -0800 +++ devel-akpm/include/asm-arm26/mman.h 2005-11-11 16:09:13.000000000 -0800 @@ -35,6 +35,7 @@ #define MADV_SEQUENTIAL 0x2 /* read-ahead aggressively */ #define MADV_WILLNEED 0x3 /* pre-fault pages */ #define MADV_DONTNEED 0x4 /* discard these pages */ +#define MADV_REMOVE 0x5 /* remove these pages & resources */ /* compatibility flags */ #define MAP_ANON MAP_ANONYMOUS diff -puN include/asm-arm/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store include/asm-arm/mman.h --- devel/include/asm-arm/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store 2005-11-11 16:09:13.000000000 -0800 +++ devel-akpm/include/asm-arm/mman.h 2005-11-11 16:09:13.000000000 -0800 @@ -35,6 +35,7 @@ #define MADV_SEQUENTIAL 0x2 /* read-ahead aggressively */ #define MADV_WILLNEED 0x3 /* pre-fault pages */ #define MADV_DONTNEED 0x4 /* discard these pages */ +#define MADV_REMOVE 0x5 /* remove these pages & resources */ /* compatibility flags */ #define MAP_ANON MAP_ANONYMOUS diff -puN include/asm-cris/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store include/asm-cris/mman.h --- devel/include/asm-cris/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store 2005-11-11 16:09:13.000000000 -0800 +++ devel-akpm/include/asm-cris/mman.h 2005-11-11 16:09:13.000000000 -0800 @@ -37,6 +37,7 @@ #define MADV_SEQUENTIAL 0x2 /* read-ahead aggressively */ #define MADV_WILLNEED 0x3 /* pre-fault pages */ #define MADV_DONTNEED 0x4 /* discard these pages */ +#define MADV_REMOVE 0x5 /* remove these pages & resources */ /* compatibility flags */ #define MAP_ANON MAP_ANONYMOUS diff -puN include/asm-frv/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store include/asm-frv/mman.h --- devel/include/asm-frv/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store 2005-11-11 16:09:13.000000000 -0800 +++ devel-akpm/include/asm-frv/mman.h 2005-11-11 16:09:13.000000000 -0800 @@ -35,6 +35,7 @@ #define MADV_SEQUENTIAL 0x2 /* read-ahead aggressively */ #define MADV_WILLNEED 0x3 /* pre-fault pages */ #define MADV_DONTNEED 0x4 /* discard these pages */ +#define MADV_REMOVE 0x5 /* remove these pages & resources */ /* compatibility flags */ #define MAP_ANON MAP_ANONYMOUS diff -puN include/asm-h8300/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store include/asm-h8300/mman.h --- devel/include/asm-h8300/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store 2005-11-11 16:09:13.000000000 -0800 +++ devel-akpm/include/asm-h8300/mman.h 2005-11-11 16:09:13.000000000 -0800 @@ -35,6 +35,7 @@ #define MADV_SEQUENTIAL 0x2 /* read-ahead aggressively */ #define MADV_WILLNEED 0x3 /* pre-fault pages */ #define MADV_DONTNEED 0x4 /* discard these pages */ +#define MADV_REMOVE 0x5 /* remove these pages & resources */ /* compatibility flags */ #define MAP_ANON MAP_ANONYMOUS diff -puN include/asm-i386/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store include/asm-i386/mman.h --- devel/include/asm-i386/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store 2005-11-11 16:09:13.000000000 -0800 +++ devel-akpm/include/asm-i386/mman.h 2005-11-11 16:09:13.000000000 -0800 @@ -35,6 +35,7 @@ #define MADV_SEQUENTIAL 0x2 /* read-ahead aggressively */ #define MADV_WILLNEED 0x3 /* pre-fault pages */ #define MADV_DONTNEED 0x4 /* discard these pages */ +#define MADV_REMOVE 0x5 /* remove these pages & resources */ /* compatibility flags */ #define MAP_ANON MAP_ANONYMOUS diff -puN include/asm-ia64/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store include/asm-ia64/mman.h --- devel/include/asm-ia64/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store 2005-11-11 16:09:13.000000000 -0800 +++ devel-akpm/include/asm-ia64/mman.h 2005-11-11 16:09:13.000000000 -0800 @@ -43,6 +43,7 @@ #define MADV_SEQUENTIAL 0x2 /* read-ahead aggressively */ #define MADV_WILLNEED 0x3 /* pre-fault pages */ #define MADV_DONTNEED 0x4 /* discard these pages */ +#define MADV_REMOVE 0x5 /* remove these pages & resources */ /* compatibility flags */ #define MAP_ANON MAP_ANONYMOUS diff -puN include/asm-m32r/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store include/asm-m32r/mman.h --- devel/include/asm-m32r/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store 2005-11-11 16:09:13.000000000 -0800 +++ devel-akpm/include/asm-m32r/mman.h 2005-11-11 16:09:13.000000000 -0800 @@ -37,6 +37,7 @@ #define MADV_SEQUENTIAL 0x2 /* read-ahead aggressively */ #define MADV_WILLNEED 0x3 /* pre-fault pages */ #define MADV_DONTNEED 0x4 /* discard these pages */ +#define MADV_REMOVE 0x5 /* remove these pages & resources */ /* compatibility flags */ #define MAP_ANON MAP_ANONYMOUS diff -puN include/asm-m68k/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store include/asm-m68k/mman.h --- devel/include/asm-m68k/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store 2005-11-11 16:09:13.000000000 -0800 +++ devel-akpm/include/asm-m68k/mman.h 2005-11-11 16:09:13.000000000 -0800 @@ -35,6 +35,7 @@ #define MADV_SEQUENTIAL 0x2 /* read-ahead aggressively */ #define MADV_WILLNEED 0x3 /* pre-fault pages */ #define MADV_DONTNEED 0x4 /* discard these pages */ +#define MADV_REMOVE 0x5 /* remove these pages & resources */ /* compatibility flags */ #define MAP_ANON MAP_ANONYMOUS diff -puN include/asm-mips/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store include/asm-mips/mman.h --- devel/include/asm-mips/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store 2005-11-11 16:09:13.000000000 -0800 +++ devel-akpm/include/asm-mips/mman.h 2005-11-11 16:09:13.000000000 -0800 @@ -65,6 +65,7 @@ #define MADV_SEQUENTIAL 0x2 /* read-ahead aggressively */ #define MADV_WILLNEED 0x3 /* pre-fault pages */ #define MADV_DONTNEED 0x4 /* discard these pages */ +#define MADV_REMOVE 0x5 /* remove these pages & resources */ /* compatibility flags */ #define MAP_ANON MAP_ANONYMOUS diff -puN include/asm-parisc/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store include/asm-parisc/mman.h --- devel/include/asm-parisc/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store 2005-11-11 16:09:13.000000000 -0800 +++ devel-akpm/include/asm-parisc/mman.h 2005-11-11 16:09:13.000000000 -0800 @@ -38,6 +38,7 @@ #define MADV_SPACEAVAIL 5 /* insure that resources are reserved */ #define MADV_VPS_PURGE 6 /* Purge pages from VM page cache */ #define MADV_VPS_INHERIT 7 /* Inherit parents page size */ +#define MADV_REMOVE 8 /* remove these pages & resources */ /* The range 12-64 is reserved for page size specification. */ #define MADV_4K_PAGES 12 /* Use 4K pages */ diff -puN include/asm-powerpc/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store include/asm-powerpc/mman.h --- devel/include/asm-powerpc/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store 2005-11-11 16:09:13.000000000 -0800 +++ devel-akpm/include/asm-powerpc/mman.h 2005-11-11 16:09:13.000000000 -0800 @@ -44,6 +44,7 @@ #define MADV_SEQUENTIAL 0x2 /* read-ahead aggressively */ #define MADV_WILLNEED 0x3 /* pre-fault pages */ #define MADV_DONTNEED 0x4 /* discard these pages */ +#define MADV_REMOVE 0x5 /* remove these pages & resources */ /* compatibility flags */ #define MAP_ANON MAP_ANONYMOUS diff -puN include/asm-s390/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store include/asm-s390/mman.h --- devel/include/asm-s390/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store 2005-11-11 16:09:13.000000000 -0800 +++ devel-akpm/include/asm-s390/mman.h 2005-11-11 16:09:13.000000000 -0800 @@ -43,6 +43,7 @@ #define MADV_SEQUENTIAL 0x2 /* read-ahead aggressively */ #define MADV_WILLNEED 0x3 /* pre-fault pages */ #define MADV_DONTNEED 0x4 /* discard these pages */ +#define MADV_REMOVE 0x5 /* remove these pages & resources */ /* compatibility flags */ #define MAP_ANON MAP_ANONYMOUS diff -puN include/asm-sh/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store include/asm-sh/mman.h --- devel/include/asm-sh/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store 2005-11-11 16:09:13.000000000 -0800 +++ devel-akpm/include/asm-sh/mman.h 2005-11-11 16:09:13.000000000 -0800 @@ -35,6 +35,7 @@ #define MADV_SEQUENTIAL 0x2 /* read-ahead aggressively */ #define MADV_WILLNEED 0x3 /* pre-fault pages */ #define MADV_DONTNEED 0x4 /* discard these pages */ +#define MADV_REMOVE 0x5 /* remove these pages & resources */ /* compatibility flags */ #define MAP_ANON MAP_ANONYMOUS diff -puN include/asm-sparc64/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store include/asm-sparc64/mman.h --- devel/include/asm-sparc64/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store 2005-11-11 16:09:13.000000000 -0800 +++ devel-akpm/include/asm-sparc64/mman.h 2005-11-11 16:09:13.000000000 -0800 @@ -54,6 +54,7 @@ #define MADV_WILLNEED 0x3 /* pre-fault pages */ #define MADV_DONTNEED 0x4 /* discard these pages */ #define MADV_FREE 0x5 /* (Solaris) contents can be freed */ +#define MADV_REMOVE 0x6 /* remove these pages & resources */ /* compatibility flags */ #define MAP_ANON MAP_ANONYMOUS diff -puN include/asm-sparc/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store include/asm-sparc/mman.h --- devel/include/asm-sparc/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store 2005-11-11 16:09:13.000000000 -0800 +++ devel-akpm/include/asm-sparc/mman.h 2005-11-11 16:09:13.000000000 -0800 @@ -54,6 +54,7 @@ #define MADV_WILLNEED 0x3 /* pre-fault pages */ #define MADV_DONTNEED 0x4 /* discard these pages */ #define MADV_FREE 0x5 /* (Solaris) contents can be freed */ +#define MADV_REMOVE 0x6 /* remove these pages & resources */ /* compatibility flags */ #define MAP_ANON MAP_ANONYMOUS diff -puN include/asm-v850/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store include/asm-v850/mman.h --- devel/include/asm-v850/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store 2005-11-11 16:09:13.000000000 -0800 +++ devel-akpm/include/asm-v850/mman.h 2005-11-11 16:09:13.000000000 -0800 @@ -32,6 +32,7 @@ #define MADV_SEQUENTIAL 0x2 /* read-ahead aggressively */ #define MADV_WILLNEED 0x3 /* pre-fault pages */ #define MADV_DONTNEED 0x4 /* discard these pages */ +#define MADV_REMOVE 0x5 /* remove these pages & resources */ /* compatibility flags */ #define MAP_ANON MAP_ANONYMOUS diff -puN include/asm-x86_64/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store include/asm-x86_64/mman.h --- devel/include/asm-x86_64/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store 2005-11-11 16:09:13.000000000 -0800 +++ devel-akpm/include/asm-x86_64/mman.h 2005-11-11 16:09:13.000000000 -0800 @@ -36,6 +36,7 @@ #define MADV_SEQUENTIAL 0x2 /* read-ahead aggressively */ #define MADV_WILLNEED 0x3 /* pre-fault pages */ #define MADV_DONTNEED 0x4 /* discard these pages */ +#define MADV_REMOVE 0x5 /* remove these pages & resources */ /* compatibility flags */ #define MAP_ANON MAP_ANONYMOUS diff -puN include/asm-xtensa/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store include/asm-xtensa/mman.h --- devel/include/asm-xtensa/mman.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store 2005-11-11 16:09:13.000000000 -0800 +++ devel-akpm/include/asm-xtensa/mman.h 2005-11-11 16:09:13.000000000 -0800 @@ -72,6 +72,7 @@ #define MADV_SEQUENTIAL 0x2 /* read-ahead aggressively */ #define MADV_WILLNEED 0x3 /* pre-fault pages */ #define MADV_DONTNEED 0x4 /* discard these pages */ +#define MADV_REMOVE 0x5 /* remove these pages & resources */ /* compatibility flags */ #define MAP_ANON MAP_ANONYMOUS diff -puN include/linux/fs.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store include/linux/fs.h --- devel/include/linux/fs.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store 2005-11-11 16:09:13.000000000 -0800 +++ devel-akpm/include/linux/fs.h 2005-11-11 16:09:13.000000000 -0800 @@ -1052,6 +1052,7 @@ struct inode_operations { ssize_t (*getxattr) (struct dentry *, const char *, void *, size_t); ssize_t (*listxattr) (struct dentry *, char *, size_t); int (*removexattr) (struct dentry *, const char *); + void (*truncate_range)(struct inode *, loff_t, loff_t); }; struct seq_file; diff -puN include/linux/mm.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store include/linux/mm.h --- devel/include/linux/mm.h~madvise-remove-remove-pages-from-tmpfs-shm-backing-store 2005-11-11 16:09:13.000000000 -0800 +++ devel-akpm/include/linux/mm.h 2005-11-11 16:09:13.000000000 -0800 @@ -706,6 +706,7 @@ static inline void unmap_shared_mapping_ } extern int vmtruncate(struct inode * inode, loff_t offset); +extern int vmtruncate_range(struct inode * inode, loff_t offset, loff_t end); extern int install_page(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long addr, struct page *page, pgprot_t prot); extern int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long addr, unsigned long pgoff, pgprot_t prot); extern int __handle_mm_fault(struct mm_struct *mm,struct vm_area_struct *vma, unsigned long address, int write_access); diff -puN mm/madvise.c~madvise-remove-remove-pages-from-tmpfs-shm-backing-store mm/madvise.c --- devel/mm/madvise.c~madvise-remove-remove-pages-from-tmpfs-shm-backing-store 2005-11-11 16:09:13.000000000 -0800 +++ devel-akpm/mm/madvise.c 2005-11-11 16:25:28.000000000 -0800 @@ -140,6 +140,39 @@ static long madvise_dontneed(struct vm_a return 0; } +/* + * Application wants to free up the pages and associated backing store. + * This is effectively punching a hole into the middle of a file. + * + * NOTE: Currently, only shmfs/tmpfs is supported for this operation. + * Other filesystems return -ENOSYS. + */ +static long madvise_remove(struct vm_area_struct * vma, + unsigned long start, unsigned long end) +{ + struct address_space *mapping; + loff_t offset, endoff; + + if (vma->vm_flags & (VM_LOCKED|VM_NONLINEAR|VM_HUGETLB)) + return -EINVAL; + + if (!vma->vm_file || !vma->vm_file->f_mapping + || !vma->vm_file->f_mapping->host) { + return -EINVAL; + } + + mapping = vma->vm_file->f_mapping; + if (mapping == &swapper_space) { + return -EINVAL; + } + + offset = (loff_t)(start - vma->vm_start) + + (vma->vm_pgoff << PAGE_SHIFT); + endoff = (loff_t)(end - vma->vm_start - 1) + + (vma->vm_pgoff << PAGE_SHIFT); + return vmtruncate_range(mapping->host, offset, endoff); +} + static long madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev, unsigned long start, unsigned long end, int behavior) @@ -152,6 +185,9 @@ madvise_vma(struct vm_area_struct *vma, case MADV_RANDOM: error = madvise_behavior(vma, prev, start, end, behavior); break; + case MADV_REMOVE: + error = madvise_remove(vma, start, end); + break; case MADV_WILLNEED: error = madvise_willneed(vma, prev, start, end); @@ -190,6 +226,8 @@ madvise_vma(struct vm_area_struct *vma, * some pages ahead. * MADV_DONTNEED - the application is finished with the given range, * so the kernel can free resources associated with it. + * MADV_REMOVE - the application wants to free up the given range of + * pages and associated backing store. * * return values: * zero - success diff -puN mm/memory.c~madvise-remove-remove-pages-from-tmpfs-shm-backing-store mm/memory.c --- devel/mm/memory.c~madvise-remove-remove-pages-from-tmpfs-shm-backing-store 2005-11-11 16:09:13.000000000 -0800 +++ devel-akpm/mm/memory.c 2005-11-11 16:25:28.000000000 -0800 @@ -1611,6 +1611,32 @@ out_busy: EXPORT_SYMBOL(vmtruncate); +int vmtruncate_range(struct inode * inode, loff_t offset, loff_t end) +{ + struct address_space *mapping = inode->i_mapping; + + /* + * If the underlying filesystem is not going to provide + * a way to truncate a range of blocks (punch a hole) - + * we should return failure right now. + */ + if (!inode->i_op || !inode->i_op->truncate_range) + return -ENOSYS; + + /* XXX - Do we need both i_sem and i_allocsem all the way ? */ + down(&inode->i_sem); + down_write(&inode->i_alloc_sem); + unmap_mapping_range(mapping, offset, (end - offset), 1); + truncate_inode_pages_range(mapping, offset, end); + inode->i_op->truncate_range(inode, offset, end); + up_write(&inode->i_alloc_sem); + up(&inode->i_sem); + + return 0; +} + +EXPORT_SYMBOL(vmtruncate_range); + /* * Primitive swap readahead code. We simply read an aligned block of * (1 << page_cluster) entries in the swap area. This method is chosen diff -puN mm/shmem.c~madvise-remove-remove-pages-from-tmpfs-shm-backing-store mm/shmem.c --- devel/mm/shmem.c~madvise-remove-remove-pages-from-tmpfs-shm-backing-store 2005-11-11 16:09:13.000000000 -0800 +++ devel-akpm/mm/shmem.c 2005-11-11 16:09:13.000000000 -0800 @@ -457,7 +457,7 @@ static void shmem_free_pages(struct list } while (next); } -static void shmem_truncate(struct inode *inode) +static void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end) { struct shmem_inode_info *info = SHMEM_I(inode); unsigned long idx; @@ -475,18 +475,27 @@ static void shmem_truncate(struct inode long nr_swaps_freed = 0; int offset; int freed; + int punch_hole = 0; inode->i_ctime = inode->i_mtime = CURRENT_TIME; - idx = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; + idx = (start + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; if (idx >= info->next_index) return; spin_lock(&info->lock); info->flags |= SHMEM_TRUNCATE; - limit = info->next_index; - info->next_index = idx; + if (likely(end == (loff_t) -1)) { + limit = info->next_index; + info->next_index = idx; + } else { + limit = (end + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; + if (limit > info->next_index) + limit = info->next_index; + punch_hole = 1; + } + topdir = info->i_indirect; - if (topdir && idx <= SHMEM_NR_DIRECT) { + if (topdir && idx <= SHMEM_NR_DIRECT && !punch_hole) { info->i_indirect = NULL; nr_pages_to_free++; list_add(&topdir->lru, &pages_to_free); @@ -573,11 +582,12 @@ static void shmem_truncate(struct inode set_page_private(subdir, page_private(subdir) - freed); if (offset) spin_unlock(&info->lock); - BUG_ON(page_private(subdir) > offset); + if (!punch_hole) + BUG_ON(page_private(subdir) > offset); } if (offset) offset = 0; - else if (subdir) { + else if (subdir && !page_private(subdir)) { dir[diroff] = NULL; nr_pages_to_free++; list_add(&subdir->lru, &pages_to_free); @@ -594,7 +604,7 @@ done2: * Also, though shmem_getpage checks i_size before adding to * cache, no recheck after: so fix the narrow window there too. */ - truncate_inode_pages(inode->i_mapping, inode->i_size); + truncate_inode_pages_range(inode->i_mapping, start, end); } spin_lock(&info->lock); @@ -614,6 +624,11 @@ done2: } } +static void shmem_truncate(struct inode *inode) +{ + shmem_truncate_range(inode, inode->i_size, (loff_t)-1); +} + static int shmem_notify_change(struct dentry *dentry, struct iattr *attr) { struct inode *inode = dentry->d_inode; @@ -2083,6 +2098,7 @@ static struct file_operations shmem_file static struct inode_operations shmem_inode_operations = { .truncate = shmem_truncate, .setattr = shmem_notify_change, + .truncate_range = shmem_truncate_range, }; static struct inode_operations shmem_dir_inode_operations = { _