[PATCH] Fix get_unmapped_area and fsync for hugetlb shm segments

This patch provides the following hugetlb-related fixes to the recent stacked
shm files changes:
- Update is_file_hugepages() so it will reconize hugetlb shm segments.
- get_unmapped_area must be called with the nested file struct to handle
the sfd->file->f_ops->get_unmapped_area == NULL case.
- The fsync f_op must be wrapped since it is specified in the hugetlbfs
f_ops.

This is based on proposed fixes from Eric Biederman that were debugged and
tested by me. Without it, attempting to use hugetlb shared memory segments
on powerpc (and likely ia64) will kill your box.

Signed-off-by: Adam Litke <agl@us.ibm.com>
Cc: Eric Biederman <ebiederm@xmission.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Acked-by: William Irwin <bill.irwin@oracle.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by Adam Litke and committed by Linus Torvalds 516dffdc 7b965e08

+38 -7
+7 -1
include/linux/hugetlb.h
··· 4 #ifdef CONFIG_HUGETLB_PAGE 5 6 #include <linux/mempolicy.h> 7 #include <asm/tlbflush.h> 8 9 struct ctl_table; ··· 169 170 static inline int is_file_hugepages(struct file *file) 171 { 172 - return file->f_op == &hugetlbfs_file_operations; 173 } 174 175 static inline void set_file_hugepages(struct file *file)
··· 4 #ifdef CONFIG_HUGETLB_PAGE 5 6 #include <linux/mempolicy.h> 7 + #include <linux/shm.h> 8 #include <asm/tlbflush.h> 9 10 struct ctl_table; ··· 168 169 static inline int is_file_hugepages(struct file *file) 170 { 171 + if (file->f_op == &hugetlbfs_file_operations) 172 + return 1; 173 + if (is_file_shm_hugepages(file)) 174 + return 1; 175 + 176 + return 0; 177 } 178 179 static inline void set_file_hugepages(struct file *file)
+5
include/linux/shm.h
··· 96 97 #ifdef CONFIG_SYSVIPC 98 long do_shmat(int shmid, char __user *shmaddr, int shmflg, unsigned long *addr); 99 #else 100 static inline long do_shmat(int shmid, char __user *shmaddr, 101 int shmflg, unsigned long *addr) 102 { 103 return -ENOSYS; 104 } 105 #endif 106
··· 96 97 #ifdef CONFIG_SYSVIPC 98 long do_shmat(int shmid, char __user *shmaddr, int shmflg, unsigned long *addr); 99 + extern int is_file_shm_hugepages(struct file *file); 100 #else 101 static inline long do_shmat(int shmid, char __user *shmaddr, 102 int shmflg, unsigned long *addr) 103 { 104 return -ENOSYS; 105 + } 106 + static inline int is_file_shm_hugepages(struct file *file) 107 + { 108 + return 0; 109 } 110 #endif 111
+26 -6
ipc/shm.c
··· 285 return 0; 286 } 287 288 - #ifndef CONFIG_MMU 289 static unsigned long shm_get_unmapped_area(struct file *file, 290 unsigned long addr, unsigned long len, unsigned long pgoff, 291 unsigned long flags) 292 { 293 struct shm_file_data *sfd = shm_file_data(file); 294 - return sfd->file->f_op->get_unmapped_area(sfd->file, addr, len, pgoff, 295 - flags); 296 } 297 - #else 298 - #define shm_get_unmapped_area NULL 299 - #endif 300 301 static const struct file_operations shm_file_operations = { 302 .mmap = shm_mmap, 303 .release = shm_release, 304 .get_unmapped_area = shm_get_unmapped_area, 305 };
··· 285 return 0; 286 } 287 288 + static int shm_fsync(struct file *file, struct dentry *dentry, int datasync) 289 + { 290 + int (*fsync) (struct file *, struct dentry *, int datasync); 291 + struct shm_file_data *sfd = shm_file_data(file); 292 + int ret = -EINVAL; 293 + 294 + fsync = sfd->file->f_op->fsync; 295 + if (fsync) 296 + ret = fsync(sfd->file, sfd->file->f_path.dentry, datasync); 297 + return ret; 298 + } 299 + 300 static unsigned long shm_get_unmapped_area(struct file *file, 301 unsigned long addr, unsigned long len, unsigned long pgoff, 302 unsigned long flags) 303 { 304 struct shm_file_data *sfd = shm_file_data(file); 305 + return get_unmapped_area(sfd->file, addr, len, pgoff, flags); 306 } 307 + 308 + int is_file_shm_hugepages(struct file *file) 309 + { 310 + int ret = 0; 311 + 312 + if (file->f_op == &shm_file_operations) { 313 + struct shm_file_data *sfd; 314 + sfd = shm_file_data(file); 315 + ret = is_file_hugepages(sfd->file); 316 + } 317 + return ret; 318 + } 319 320 static const struct file_operations shm_file_operations = { 321 .mmap = shm_mmap, 322 + .fsync = shm_fsync, 323 .release = shm_release, 324 .get_unmapped_area = shm_get_unmapped_area, 325 };