Merge tag 'v3.10.89' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / file_table.c
index cd4d87a82951f6169cd4f5fd8e24a61c45c16b8d..9798b66322aacffb155963f6cdb8974a24266076 100644 (file)
 
 #include "internal.h"
 
+#define FILE_OVER_MAX
+#ifdef FILE_OVER_MAX
+extern void fd_show_open_files(pid_t pid, struct files_struct *files, struct fdtable *fdt);
+#endif
+
 /* sysctl tunables... */
 struct files_stat_struct files_stat = {
        .max_files = NR_FILE
 };
 
-DEFINE_STATIC_LGLOCK(files_lglock);
-
 /* SLAB cache for file structures */
 static struct kmem_cache *filp_cachep __read_mostly;
 
@@ -134,7 +137,6 @@ struct file *get_empty_filp(void)
                return ERR_PTR(error);
        }
 
-       INIT_LIST_HEAD(&f->f_u.fu_list);
        atomic_long_set(&f->f_count, 1);
        rwlock_init(&f->f_owner.lock);
        spin_lock_init(&f->f_lock);
@@ -145,6 +147,22 @@ struct file *get_empty_filp(void)
 over:
        /* Ran out of filps - report that */
        if (get_nr_files() > old_max) {
+#ifdef FILE_OVER_MAX
+        static int fd_dump_all_files = 0;     
+        if(!fd_dump_all_files) { 
+               struct task_struct *p;
+               pr_debug("[FD_LEAK](PID:%d)files %ld over old_max:%ld", current->pid, get_nr_files(), old_max);
+               for_each_process(p) {
+                   pid_t pid = p->pid;
+                   struct files_struct *files = p->files;
+                   struct fdtable *fdt = files_fdtable(files);
+                   if(files && fdt) {
+                       fd_show_open_files(pid, files, fdt);
+                   }
+               }
+               fd_dump_all_files = 0x1;
+        }
+#endif
                pr_info("VFS: file-max limit %lu reached\n", get_max_files());
                old_max = get_nr_files();
        }
@@ -211,10 +229,10 @@ static void drop_file_write_access(struct file *file)
        struct dentry *dentry = file->f_path.dentry;
        struct inode *inode = dentry->d_inode;
 
-       put_write_access(inode);
-
        if (special_file(inode->i_mode))
                return;
+
+       put_write_access(inode);
        if (file_check_writeable(file) != 0)
                return;
        __mnt_drop_write(mnt);
@@ -265,18 +283,15 @@ static void __fput(struct file *file)
        mntput(mnt);
 }
 
-static DEFINE_SPINLOCK(delayed_fput_lock);
-static LIST_HEAD(delayed_fput_list);
+static LLIST_HEAD(delayed_fput_list);
 static void delayed_fput(struct work_struct *unused)
 {
-       LIST_HEAD(head);
-       spin_lock_irq(&delayed_fput_lock);
-       list_splice_init(&delayed_fput_list, &head);
-       spin_unlock_irq(&delayed_fput_lock);
-       while (!list_empty(&head)) {
-               struct file *f = list_first_entry(&head, struct file, f_u.fu_list);
-               list_del_init(&f->f_u.fu_list);
-               __fput(f);
+       struct llist_node *node = llist_del_all(&delayed_fput_list);
+       struct llist_node *next;
+
+       for (; node; node = next) {
+               next = llist_next(node);
+               __fput(llist_entry(node, struct file, f_u.fu_llist));
        }
 }
 
@@ -306,17 +321,15 @@ void fput(struct file *file)
 {
        if (atomic_long_dec_and_test(&file->f_count)) {
                struct task_struct *task = current;
-               file_sb_list_del(file);
-               if (unlikely(in_interrupt() || task->flags & PF_KTHREAD)) {
-                       unsigned long flags;
-                       spin_lock_irqsave(&delayed_fput_lock, flags);
-                       list_add(&file->f_u.fu_list, &delayed_fput_list);
-                       schedule_work(&delayed_fput_work);
-                       spin_unlock_irqrestore(&delayed_fput_lock, flags);
-                       return;
+
+               if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
+                       init_task_work(&file->f_u.fu_rcuhead, ____fput);
+                       if (!task_work_add(task, &file->f_u.fu_rcuhead, true))
+                               return;
                }
-               init_task_work(&file->f_u.fu_rcuhead, ____fput);
-               task_work_add(task, &file->f_u.fu_rcuhead, true);
+
+               if (llist_add(&file->f_u.fu_llist, &delayed_fput_list))
+                       schedule_work(&delayed_fput_work);
        }
 }
 
@@ -332,7 +345,6 @@ void __fput_sync(struct file *file)
 {
        if (atomic_long_dec_and_test(&file->f_count)) {
                struct task_struct *task = current;
-               file_sb_list_del(file);
                BUG_ON(!(task->flags & PF_KTHREAD));
                __fput(file);
        }
@@ -344,127 +356,10 @@ void put_filp(struct file *file)
 {
        if (atomic_long_dec_and_test(&file->f_count)) {
                security_file_free(file);
-               file_sb_list_del(file);
                file_free(file);
        }
 }
 
-static inline int file_list_cpu(struct file *file)
-{
-#ifdef CONFIG_SMP
-       return file->f_sb_list_cpu;
-#else
-       return smp_processor_id();
-#endif
-}
-
-/* helper for file_sb_list_add to reduce ifdefs */
-static inline void __file_sb_list_add(struct file *file, struct super_block *sb)
-{
-       struct list_head *list;
-#ifdef CONFIG_SMP
-       int cpu;
-       cpu = smp_processor_id();
-       file->f_sb_list_cpu = cpu;
-       list = per_cpu_ptr(sb->s_files, cpu);
-#else
-       list = &sb->s_files;
-#endif
-       list_add(&file->f_u.fu_list, list);
-}
-
-/**
- * file_sb_list_add - add a file to the sb's file list
- * @file: file to add
- * @sb: sb to add it to
- *
- * Use this function to associate a file with the superblock of the inode it
- * refers to.
- */
-void file_sb_list_add(struct file *file, struct super_block *sb)
-{
-       lg_local_lock(&files_lglock);
-       __file_sb_list_add(file, sb);
-       lg_local_unlock(&files_lglock);
-}
-
-/**
- * file_sb_list_del - remove a file from the sb's file list
- * @file: file to remove
- * @sb: sb to remove it from
- *
- * Use this function to remove a file from its superblock.
- */
-void file_sb_list_del(struct file *file)
-{
-       if (!list_empty(&file->f_u.fu_list)) {
-               lg_local_lock_cpu(&files_lglock, file_list_cpu(file));
-               list_del_init(&file->f_u.fu_list);
-               lg_local_unlock_cpu(&files_lglock, file_list_cpu(file));
-       }
-}
-
-#ifdef CONFIG_SMP
-
-/*
- * These macros iterate all files on all CPUs for a given superblock.
- * files_lglock must be held globally.
- */
-#define do_file_list_for_each_entry(__sb, __file)              \
-{                                                              \
-       int i;                                                  \
-       for_each_possible_cpu(i) {                              \
-               struct list_head *list;                         \
-               list = per_cpu_ptr((__sb)->s_files, i);         \
-               list_for_each_entry((__file), list, f_u.fu_list)
-
-#define while_file_list_for_each_entry                         \
-       }                                                       \
-}
-
-#else
-
-#define do_file_list_for_each_entry(__sb, __file)              \
-{                                                              \
-       struct list_head *list;                                 \
-       list = &(sb)->s_files;                                  \
-       list_for_each_entry((__file), list, f_u.fu_list)
-
-#define while_file_list_for_each_entry                         \
-}
-
-#endif
-
-/**
- *     mark_files_ro - mark all files read-only
- *     @sb: superblock in question
- *
- *     All files are marked read-only.  We don't care about pending
- *     delete files so this should be used in 'force' mode only.
- */
-void mark_files_ro(struct super_block *sb)
-{
-       struct file *f;
-
-       lg_global_lock(&files_lglock);
-       do_file_list_for_each_entry(sb, f) {
-               if (!S_ISREG(file_inode(f)->i_mode))
-                      continue;
-               if (!file_count(f))
-                       continue;
-               if (!(f->f_mode & FMODE_WRITE))
-                       continue;
-               spin_lock(&f->f_lock);
-               f->f_mode &= ~FMODE_WRITE;
-               spin_unlock(&f->f_lock);
-               if (file_check_writeable(f) != 0)
-                       continue;
-               __mnt_drop_write(f->f_path.mnt);
-               file_release_write(f);
-       } while_file_list_for_each_entry;
-       lg_global_unlock(&files_lglock);
-}
-
 void __init files_init(unsigned long mempages)
 { 
        unsigned long n;
@@ -480,6 +375,5 @@ void __init files_init(unsigned long mempages)
        n = (mempages * (PAGE_SIZE / 1024)) / 10;
        files_stat.max_files = max_t(unsigned long, n, NR_FILE);
        files_defer_init();
-       lg_lock_init(&files_lglock, "files_lglock");
        percpu_counter_init(&nr_files, 0);
 }