Merge branch 'parisc-4.9-1' of git://git.kernel.org/pub/scm/linux/kernel/git/deller...
[cascardo/linux.git] / fs / locks.c
index 8f609ec..90ec671 100644 (file)
 #include <linux/pid_namespace.h>
 #include <linux/hashtable.h>
 #include <linux/percpu.h>
-#include <linux/lglock.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/filelock.h>
@@ -158,12 +157,17 @@ int lease_break_time = 45;
 
 /*
  * The global file_lock_list is only used for displaying /proc/locks, so we
- * keep a list on each CPU, with each list protected by its own spinlock via
- * the file_lock_lglock. Note that alterations to the list also require that
- * the relevant flc_lock is held.
+ * keep a list on each CPU, with each list protected by its own spinlock.
+ * Global serialization is done using file_rwsem.
+ *
+ * Note that alterations to the list also require that the relevant flc_lock is
+ * held.
  */
-DEFINE_STATIC_LGLOCK(file_lock_lglock);
-static DEFINE_PER_CPU(struct hlist_head, file_lock_list);
+struct file_lock_list_struct {
+       spinlock_t              lock;
+       struct hlist_head       hlist;
+};
+static DEFINE_PER_CPU(struct file_lock_list_struct, file_lock_list);
 DEFINE_STATIC_PERCPU_RWSEM(file_rwsem);
 
 /*
@@ -588,17 +592,21 @@ static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
 /* Must be called with the flc_lock held! */
 static void locks_insert_global_locks(struct file_lock *fl)
 {
+       struct file_lock_list_struct *fll = this_cpu_ptr(&file_lock_list);
+
        percpu_rwsem_assert_held(&file_rwsem);
 
-       lg_local_lock(&file_lock_lglock);
+       spin_lock(&fll->lock);
        fl->fl_link_cpu = smp_processor_id();
-       hlist_add_head(&fl->fl_link, this_cpu_ptr(&file_lock_list));
-       lg_local_unlock(&file_lock_lglock);
+       hlist_add_head(&fl->fl_link, &fll->hlist);
+       spin_unlock(&fll->lock);
 }
 
 /* Must be called with the flc_lock held! */
 static void locks_delete_global_locks(struct file_lock *fl)
 {
+       struct file_lock_list_struct *fll;
+
        percpu_rwsem_assert_held(&file_rwsem);
 
        /*
@@ -608,9 +616,11 @@ static void locks_delete_global_locks(struct file_lock *fl)
         */
        if (hlist_unhashed(&fl->fl_link))
                return;
-       lg_local_lock_cpu(&file_lock_lglock, fl->fl_link_cpu);
+
+       fll = per_cpu_ptr(&file_lock_list, fl->fl_link_cpu);
+       spin_lock(&fll->lock);
        hlist_del_init(&fl->fl_link);
-       lg_local_unlock_cpu(&file_lock_lglock, fl->fl_link_cpu);
+       spin_unlock(&fll->lock);
 }
 
 static unsigned long
@@ -920,7 +930,7 @@ static int flock_lock_inode(struct inode *inode, struct file_lock *request)
                        return -ENOMEM;
        }
 
-       percpu_down_read(&file_rwsem);
+       percpu_down_read_preempt_disable(&file_rwsem);
        spin_lock(&ctx->flc_lock);
        if (request->fl_flags & FL_ACCESS)
                goto find_conflict;
@@ -961,7 +971,7 @@ find_conflict:
 
 out:
        spin_unlock(&ctx->flc_lock);
-       percpu_up_read(&file_rwsem);
+       percpu_up_read_preempt_enable(&file_rwsem);
        if (new_fl)
                locks_free_lock(new_fl);
        locks_dispose_list(&dispose);
@@ -998,7 +1008,7 @@ static int posix_lock_inode(struct inode *inode, struct file_lock *request,
                new_fl2 = locks_alloc_lock();
        }
 
-       percpu_down_read(&file_rwsem);
+       percpu_down_read_preempt_disable(&file_rwsem);
        spin_lock(&ctx->flc_lock);
        /*
         * New lock request. Walk all POSIX locks and look for conflicts. If
@@ -1170,7 +1180,7 @@ static int posix_lock_inode(struct inode *inode, struct file_lock *request,
        }
  out:
        spin_unlock(&ctx->flc_lock);
-       percpu_up_read(&file_rwsem);
+       percpu_up_read_preempt_enable(&file_rwsem);
        /*
         * Free any unused locks.
         */
@@ -1445,7 +1455,7 @@ int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
                return error;
        }
 
-       percpu_down_read(&file_rwsem);
+       percpu_down_read_preempt_disable(&file_rwsem);
        spin_lock(&ctx->flc_lock);
 
        time_out_leases(inode, &dispose);
@@ -1497,13 +1507,13 @@ restart:
        locks_insert_block(fl, new_fl);
        trace_break_lease_block(inode, new_fl);
        spin_unlock(&ctx->flc_lock);
-       percpu_up_read(&file_rwsem);
+       percpu_up_read_preempt_enable(&file_rwsem);
 
        locks_dispose_list(&dispose);
        error = wait_event_interruptible_timeout(new_fl->fl_wait,
                                                !new_fl->fl_next, break_time);
 
-       percpu_down_read(&file_rwsem);
+       percpu_down_read_preempt_disable(&file_rwsem);
        spin_lock(&ctx->flc_lock);
        trace_break_lease_unblock(inode, new_fl);
        locks_delete_block(new_fl);
@@ -1520,7 +1530,7 @@ restart:
        }
 out:
        spin_unlock(&ctx->flc_lock);
-       percpu_up_read(&file_rwsem);
+       percpu_up_read_preempt_enable(&file_rwsem);
        locks_dispose_list(&dispose);
        locks_free_lock(new_fl);
        return error;
@@ -1675,7 +1685,7 @@ generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **pr
                return -EINVAL;
        }
 
-       percpu_down_read(&file_rwsem);
+       percpu_down_read_preempt_disable(&file_rwsem);
        spin_lock(&ctx->flc_lock);
        time_out_leases(inode, &dispose);
        error = check_conflicting_open(dentry, arg, lease->fl_flags);
@@ -1746,7 +1756,7 @@ out_setup:
                lease->fl_lmops->lm_setup(lease, priv);
 out:
        spin_unlock(&ctx->flc_lock);
-       percpu_up_read(&file_rwsem);
+       percpu_up_read_preempt_enable(&file_rwsem);
        locks_dispose_list(&dispose);
        if (is_deleg)
                inode_unlock(inode);
@@ -1769,7 +1779,7 @@ static int generic_delete_lease(struct file *filp, void *owner)
                return error;
        }
 
-       percpu_down_read(&file_rwsem);
+       percpu_down_read_preempt_disable(&file_rwsem);
        spin_lock(&ctx->flc_lock);
        list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
                if (fl->fl_file == filp &&
@@ -1782,7 +1792,7 @@ static int generic_delete_lease(struct file *filp, void *owner)
        if (victim)
                error = fl->fl_lmops->lm_change(victim, F_UNLCK, &dispose);
        spin_unlock(&ctx->flc_lock);
-       percpu_up_read(&file_rwsem);
+       percpu_up_read_preempt_enable(&file_rwsem);
        locks_dispose_list(&dispose);
        return error;
 }
@@ -2593,9 +2603,20 @@ static void lock_get_status(struct seq_file *f, struct file_lock *fl,
        struct inode *inode = NULL;
        unsigned int fl_pid;
 
-       if (fl->fl_nspid)
-               fl_pid = pid_vnr(fl->fl_nspid);
-       else
+       if (fl->fl_nspid) {
+               struct pid_namespace *proc_pidns = file_inode(f->file)->i_sb->s_fs_info;
+
+               /* Don't let fl_pid change based on who is reading the file */
+               fl_pid = pid_nr_ns(fl->fl_nspid, proc_pidns);
+
+               /*
+                * If there isn't a fl_pid don't display who is waiting on
+                * the lock if we are called from locks_show, or if we are
+                * called from __show_fd_info - skip lock entirely
+                */
+               if (fl_pid == 0)
+                       return;
+       } else
                fl_pid = fl->fl_pid;
 
        if (fl->fl_file != NULL)
@@ -2667,9 +2688,13 @@ static int locks_show(struct seq_file *f, void *v)
 {
        struct locks_iterator *iter = f->private;
        struct file_lock *fl, *bfl;
+       struct pid_namespace *proc_pidns = file_inode(f->file)->i_sb->s_fs_info;
 
        fl = hlist_entry(v, struct file_lock, fl_link);
 
+       if (fl->fl_nspid && !pid_nr_ns(fl->fl_nspid, proc_pidns))
+               return 0;
+
        lock_get_status(f, fl, iter->li_pos, "");
 
        list_for_each_entry(bfl, &fl->fl_block, fl_block)
@@ -2723,9 +2748,8 @@ static void *locks_start(struct seq_file *f, loff_t *pos)
 
        iter->li_pos = *pos + 1;
        percpu_down_write(&file_rwsem);
-       lg_global_lock(&file_lock_lglock);
        spin_lock(&blocked_lock_lock);
-       return seq_hlist_start_percpu(&file_lock_list, &iter->li_cpu, *pos);
+       return seq_hlist_start_percpu(&file_lock_list.hlist, &iter->li_cpu, *pos);
 }
 
 static void *locks_next(struct seq_file *f, void *v, loff_t *pos)
@@ -2733,14 +2757,13 @@ static void *locks_next(struct seq_file *f, void *v, loff_t *pos)
        struct locks_iterator *iter = f->private;
 
        ++iter->li_pos;
-       return seq_hlist_next_percpu(v, &file_lock_list, &iter->li_cpu, pos);
+       return seq_hlist_next_percpu(v, &file_lock_list.hlist, &iter->li_cpu, pos);
 }
 
 static void locks_stop(struct seq_file *f, void *v)
        __releases(&blocked_lock_lock)
 {
        spin_unlock(&blocked_lock_lock);
-       lg_global_unlock(&file_lock_lglock);
        percpu_up_write(&file_rwsem);
 }
 
@@ -2782,10 +2805,13 @@ static int __init filelock_init(void)
        filelock_cache = kmem_cache_create("file_lock_cache",
                        sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
 
-       lg_lock_init(&file_lock_lglock, "file_lock_lglock");
 
-       for_each_possible_cpu(i)
-               INIT_HLIST_HEAD(per_cpu_ptr(&file_lock_list, i));
+       for_each_possible_cpu(i) {
+               struct file_lock_list_struct *fll = per_cpu_ptr(&file_lock_list, i);
+
+               spin_lock_init(&fll->lock);
+               INIT_HLIST_HEAD(&fll->hlist);
+       }
 
        return 0;
 }