Merge branch 'work.misc' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
authorLinus Torvalds <torvalds@linux-foundation.org>
Fri, 19 Jul 2019 18:35:08 +0000 (11:35 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Fri, 19 Jul 2019 18:35:08 +0000 (11:35 -0700)
Pull misc vfs updates from Al Viro:
 "Assorted stuff"

* 'work.misc' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs:
  perf_event_get(): don't bother with fget_raw()
  vfs: update d_make_root() description

1  2 
Documentation/filesystems/porting
kernel/events/core.c

index 2813a19389fe2b226fa7c9b475f0d616e7ae2774,f6714f7e6bb5cee7e588c08bb894d579cb232d30..209672010fb465c282ca597e694ad0bb635bca81
@@@ -330,14 -330,14 +330,14 @@@ unreferenced dentries, and is now only 
  [mandatory]
  
        .d_compare() calling convention and locking rules are significantly
 -changed. Read updated documentation in Documentation/filesystems/vfs.txt (and
 +changed. Read updated documentation in Documentation/filesystems/vfs.rst (and
  look at examples of other filesystems) for guidance.
  
  ---
  [mandatory]
  
        .d_hash() calling convention and locking rules are significantly
 -changed. Read updated documentation in Documentation/filesystems/vfs.txt (and
 +changed. Read updated documentation in Documentation/filesystems/vfs.rst (and
  look at examples of other filesystems) for guidance.
  
  ---
@@@ -377,12 -377,12 +377,12 @@@ where possible
  the filesystem provides it), which requires dropping out of rcu-walk mode. This
  may now be called in rcu-walk mode (nd->flags & LOOKUP_RCU). -ECHILD should be
  returned if the filesystem cannot handle rcu-walk. See
 -Documentation/filesystems/vfs.txt for more details.
 +Documentation/filesystems/vfs.rst for more details.
  
        permission is an inode permission check that is called on many or all
  directory inodes on the way down a path walk (to check for exec permission). It
  must now be rcu-walk aware (mask & MAY_NOT_BLOCK).  See
 -Documentation/filesystems/vfs.txt for more details.
 +Documentation/filesystems/vfs.rst for more details.
   
  --
  [mandatory]
@@@ -428,8 -428,19 +428,19 @@@ release it yourself
  --
  [mandatory]
        d_alloc_root() is gone, along with a lot of bugs caused by code
- misusing it.  Replacement: d_make_root(inode).  The difference is,
- d_make_root() drops the reference to inode if dentry allocation fails.  
+ misusing it.  Replacement: d_make_root(inode).  On success d_make_root(inode)
+ allocates and returns a new dentry instantiated with the passed in inode.
+ On failure NULL is returned and the passed in inode is dropped so the reference
+ to inode is consumed in all cases and failure handling need not do any cleanup
+ for the inode.  If d_make_root(inode) is passed a NULL inode it returns NULL
+ and also requires no further error handling. Typical usage is:
+       inode = foofs_new_inode(....);
+       s->s_root = d_make_inode(inode);
+       if (!s->s_root)
+               /* Nothing needed for the inode cleanup */
+               return -ENOMEM;
+       ...
  
  --
  [mandatory]
@@@ -625,7 -636,7 +636,7 @@@ in your dentry operations instead
  --
  [mandatory]
        ->clone_file_range() and ->dedupe_file_range have been replaced with
 -      ->remap_file_range().  See Documentation/filesystems/vfs.txt for more
 +      ->remap_file_range().  See Documentation/filesystems/vfs.rst for more
        information.
  --
  [recommended]
diff --combined kernel/events/core.c
index eea9d52b010c7a611dd08af16b934ff5508e5d45,f9ff04c8d084955420ed25ea2be616e33bd91606..026a14541a38223669a021585f0539e9d45f7628
@@@ -2553,9 -2553,6 +2553,9 @@@ unlock
        return ret;
  }
  
 +static bool exclusive_event_installable(struct perf_event *event,
 +                                      struct perf_event_context *ctx);
 +
  /*
   * Attach a performance event to a context.
   *
@@@ -2570,8 -2567,6 +2570,8 @@@ perf_install_in_context(struct perf_eve
  
        lockdep_assert_held(&ctx->mutex);
  
 +      WARN_ON_ONCE(!exclusive_event_installable(event, ctx));
 +
        if (event->cpu != -1)
                event->cpu = cpu;
  
@@@ -2957,12 -2952,6 +2957,12 @@@ static void ctx_sched_out(struct perf_e
        if (!ctx->nr_active || !(is_active & EVENT_ALL))
                return;
  
 +      /*
 +       * If we had been multiplexing, no rotations are necessary, now no events
 +       * are active.
 +       */
 +      ctx->rotate_necessary = 0;
 +
        perf_pmu_disable(ctx->pmu);
        if (is_active & EVENT_PINNED) {
                list_for_each_entry_safe(event, tmp, &ctx->pinned_active, active_list)
@@@ -3330,13 -3319,10 +3330,13 @@@ static int flexible_sched_in(struct per
                return 0;
  
        if (group_can_go_on(event, sid->cpuctx, sid->can_add_hw)) {
 -              if (!group_sched_in(event, sid->cpuctx, sid->ctx))
 -                      list_add_tail(&event->active_list, &sid->ctx->flexible_active);
 -              else
 +              int ret = group_sched_in(event, sid->cpuctx, sid->ctx);
 +              if (ret) {
                        sid->can_add_hw = 0;
 +                      sid->ctx->rotate_necessary = 1;
 +                      return 0;
 +              }
 +              list_add_tail(&event->active_list, &sid->ctx->flexible_active);
        }
  
        return 0;
@@@ -3704,17 -3690,24 +3704,17 @@@ ctx_first_active(struct perf_event_cont
  static bool perf_rotate_context(struct perf_cpu_context *cpuctx)
  {
        struct perf_event *cpu_event = NULL, *task_event = NULL;
 -      bool cpu_rotate = false, task_rotate = false;
 -      struct perf_event_context *ctx = NULL;
 +      struct perf_event_context *task_ctx = NULL;
 +      int cpu_rotate, task_rotate;
  
        /*
         * Since we run this from IRQ context, nobody can install new
         * events, thus the event count values are stable.
         */
  
 -      if (cpuctx->ctx.nr_events) {
 -              if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
 -                      cpu_rotate = true;
 -      }
 -
 -      ctx = cpuctx->task_ctx;
 -      if (ctx && ctx->nr_events) {
 -              if (ctx->nr_events != ctx->nr_active)
 -                      task_rotate = true;
 -      }
 +      cpu_rotate = cpuctx->ctx.rotate_necessary;
 +      task_ctx = cpuctx->task_ctx;
 +      task_rotate = task_ctx ? task_ctx->rotate_necessary : 0;
  
        if (!(cpu_rotate || task_rotate))
                return false;
        perf_pmu_disable(cpuctx->ctx.pmu);
  
        if (task_rotate)
 -              task_event = ctx_first_active(ctx);
 +              task_event = ctx_first_active(task_ctx);
        if (cpu_rotate)
                cpu_event = ctx_first_active(&cpuctx->ctx);
  
         * As per the order given at ctx_resched() first 'pop' task flexible
         * and then, if needed CPU flexible.
         */
 -      if (task_event || (ctx && cpu_event))
 -              ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
 +      if (task_event || (task_ctx && cpu_event))
 +              ctx_sched_out(task_ctx, cpuctx, EVENT_FLEXIBLE);
        if (cpu_event)
                cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  
        if (task_event)
 -              rotate_ctx(ctx, task_event);
 +              rotate_ctx(task_ctx, task_event);
        if (cpu_event)
                rotate_ctx(&cpuctx->ctx, cpu_event);
  
 -      perf_event_sched_in(cpuctx, ctx, current);
 +      perf_event_sched_in(cpuctx, task_ctx, current);
  
        perf_pmu_enable(cpuctx->ctx.pmu);
        perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
@@@ -4365,7 -4358,7 +4365,7 @@@ static int exclusive_event_init(struct 
  {
        struct pmu *pmu = event->pmu;
  
 -      if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
 +      if (!is_exclusive_pmu(pmu))
                return 0;
  
        /*
@@@ -4396,7 -4389,7 +4396,7 @@@ static void exclusive_event_destroy(str
  {
        struct pmu *pmu = event->pmu;
  
 -      if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
 +      if (!is_exclusive_pmu(pmu))
                return;
  
        /* see comment in exclusive_event_init() */
@@@ -4416,15 -4409,14 +4416,15 @@@ static bool exclusive_event_match(struc
        return false;
  }
  
 -/* Called under the same ctx::mutex as perf_install_in_context() */
  static bool exclusive_event_installable(struct perf_event *event,
                                        struct perf_event_context *ctx)
  {
        struct perf_event *iter_event;
        struct pmu *pmu = event->pmu;
  
 -      if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
 +      lockdep_assert_held(&ctx->mutex);
 +
 +      if (!is_exclusive_pmu(pmu))
                return true;
  
        list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
@@@ -4471,20 -4463,12 +4471,20 @@@ static void _free_event(struct perf_eve
        if (event->destroy)
                event->destroy(event);
  
 -      if (event->ctx)
 -              put_ctx(event->ctx);
 -
 +      /*
 +       * Must be after ->destroy(), due to uprobe_perf_close() using
 +       * hw.target.
 +       */
        if (event->hw.target)
                put_task_struct(event->hw.target);
  
 +      /*
 +       * perf_event_free_task() relies on put_ctx() being 'last', in particular
 +       * all task references must be cleaned up.
 +       */
 +      if (event->ctx)
 +              put_ctx(event->ctx);
 +
        exclusive_event_destroy(event);
        module_put(event->pmu->module);
  
@@@ -4664,17 -4648,8 +4664,17 @@@ again
        mutex_unlock(&event->child_mutex);
  
        list_for_each_entry_safe(child, tmp, &free_list, child_list) {
 +              void *var = &child->ctx->refcount;
 +
                list_del(&child->child_list);
                free_event(child);
 +
 +              /*
 +               * Wake any perf_event_free_task() waiting for this event to be
 +               * freed.
 +               */
 +              smp_mb(); /* pairs with wait_var_event() */
 +              wake_up_var(var);
        }
  
  no_ctx:
@@@ -5030,9 -5005,6 +5030,9 @@@ static int perf_event_period(struct per
        if (perf_event_check_period(event, value))
                return -EINVAL;
  
 +      if (!event->attr.freq && (value & (1ULL << 63)))
 +              return -EINVAL;
 +
        event_function_call(event, __perf_event_period, &value);
  
        return 0;
@@@ -5951,7 -5923,7 +5951,7 @@@ static void perf_sample_regs_user(struc
        if (user_mode(regs)) {
                regs_user->abi = perf_reg_abi(current);
                regs_user->regs = regs;
 -      } else if (current->mm) {
 +      } else if (!(current->flags & PF_KTHREAD)) {
                perf_get_regs_user(regs_user, regs, regs_user_copy);
        } else {
                regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
@@@ -8560,9 -8532,9 +8560,9 @@@ static int perf_tp_event_match(struct p
        if (event->hw.state & PERF_HES_STOPPED)
                return 0;
        /*
 -       * All tracepoints are from kernel-space.
 +       * If exclude_kernel, only trace user-space tracepoints (uprobes)
         */
 -      if (event->attr.exclude_kernel)
 +      if (event->attr.exclude_kernel && !user_mode(regs))
                return 0;
  
        if (!perf_tp_filter_match(event, data))
@@@ -9902,12 -9874,6 +9902,12 @@@ static int pmu_dev_alloc(struct pmu *pm
        if (ret)
                goto del_dev;
  
 +      if (pmu->attr_update)
 +              ret = sysfs_update_groups(&pmu->dev->kobj, pmu->attr_update);
 +
 +      if (ret)
 +              goto del_dev;
 +
  out:
        return ret;
  
@@@ -10067,12 -10033,6 +10067,12 @@@ void perf_pmu_unregister(struct pmu *pm
  }
  EXPORT_SYMBOL_GPL(perf_pmu_unregister);
  
 +static inline bool has_extended_regs(struct perf_event *event)
 +{
 +      return (event->attr.sample_regs_user & PERF_REG_EXTENDED_MASK) ||
 +             (event->attr.sample_regs_intr & PERF_REG_EXTENDED_MASK);
 +}
 +
  static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
  {
        struct perf_event_context *ctx = NULL;
                perf_event_ctx_unlock(event->group_leader, ctx);
  
        if (!ret) {
 +              if (!(pmu->capabilities & PERF_PMU_CAP_EXTENDED_REGS) &&
 +                  has_extended_regs(event))
 +                      ret = -EOPNOTSUPP;
 +
                if (pmu->capabilities & PERF_PMU_CAP_NO_EXCLUDE &&
 -                              event_has_any_exclude_flag(event)) {
 -                      if (event->destroy)
 -                              event->destroy(event);
 +                  event_has_any_exclude_flag(event))
                        ret = -EINVAL;
 -              }
 +
 +              if (ret && event->destroy)
 +                      event->destroy(event);
        }
  
        if (ret)
@@@ -10724,11 -10680,11 +10724,11 @@@ static int perf_event_set_clock(struct 
                break;
  
        case CLOCK_BOOTTIME:
 -              event->clock = &ktime_get_boot_ns;
 +              event->clock = &ktime_get_boottime_ns;
                break;
  
        case CLOCK_TAI:
 -              event->clock = &ktime_get_tai_ns;
 +              event->clock = &ktime_get_clocktai_ns;
                break;
  
        default:
@@@ -10953,6 -10909,11 +10953,6 @@@ SYSCALL_DEFINE5(perf_event_open
                goto err_alloc;
        }
  
 -      if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) {
 -              err = -EBUSY;
 -              goto err_context;
 -      }
 -
        /*
         * Look up the group leader (we will attach this event to it):
         */
                                move_group = 0;
                        }
                }
 +
 +              /*
 +               * Failure to create exclusive events returns -EBUSY.
 +               */
 +              err = -EBUSY;
 +              if (!exclusive_event_installable(group_leader, ctx))
 +                      goto err_locked;
 +
 +              for_each_sibling_event(sibling, group_leader) {
 +                      if (!exclusive_event_installable(sibling, ctx))
 +                              goto err_locked;
 +              }
        } else {
                mutex_lock(&ctx->mutex);
        }
         * because we need to serialize with concurrent event creation.
         */
        if (!exclusive_event_installable(event, ctx)) {
 -              /* exclusive and group stuff are assumed mutually exclusive */
 -              WARN_ON_ONCE(move_group);
 -
                err = -EBUSY;
                goto err_locked;
        }
@@@ -11554,11 -11506,11 +11554,11 @@@ static void perf_free_event(struct perf
  }
  
  /*
 - * Free an unexposed, unused context as created by inheritance by
 - * perf_event_init_task below, used by fork() in case of fail.
 + * Free a context as created by inheritance by perf_event_init_task() below,
 + * used by fork() in case of fail.
   *
 - * Not all locks are strictly required, but take them anyway to be nice and
 - * help out with the lockdep assertions.
 + * Even though the task has never lived, the context and events have been
 + * exposed through the child_list, so we must take care tearing it all down.
   */
  void perf_event_free_task(struct task_struct *task)
  {
                        perf_free_event(event, ctx);
  
                mutex_unlock(&ctx->mutex);
 -              put_ctx(ctx);
 +
 +              /*
 +               * perf_event_release_kernel() could've stolen some of our
 +               * child events and still have them on its free_list. In that
 +               * case we must wait for these events to have been freed (in
 +               * particular all their references to this task must've been
 +               * dropped).
 +               *
 +               * Without this copy_process() will unconditionally free this
 +               * task (irrespective of its reference count) and
 +               * _free_event()'s put_task_struct(event->hw.target) will be a
 +               * use-after-free.
 +               *
 +               * Wait for all events to drop their context reference.
 +               */
 +              wait_var_event(&ctx->refcount, refcount_read(&ctx->refcount) == 1);
 +              put_ctx(ctx); /* must be last */
        }
  }
  
@@@ -11618,9 -11554,7 +11618,7 @@@ void perf_event_delayed_put(struct task
  
  struct file *perf_event_get(unsigned int fd)
  {
-       struct file *file;
-       file = fget_raw(fd);
+       struct file *file = fget(fd);
        if (!file)
                return ERR_PTR(-EBADF);