Re: [PATCH 01/13] perf: Simplify perf_event_alloc() error path

Namhyung Kim
2023-11-03T19:50:34Z

Hi Jiri and Peter,

On Fri, Nov 3, 2023 at 5:38 AM Jiri Olsa <olsajiri@gmail.com> wrote:
>
> On Thu, Nov 02, 2023 at 04:09:20PM +0100, Peter Zijlstra wrote:
>
> SNIP
>
> > @@ -11936,24 +11957,24 @@ perf_event_alloc(struct perf_event_attr
> > */
> > if (pmu->task_ctx_nr == perf_invalid_context && (task || cgroup_fd != -1)) {
> > err = -EINVAL;
> > - goto err_pmu;
> > + goto err;
> > }
> >
> > if (event->attr.aux_output &&
> > !(pmu->capabilities & PERF_PMU_CAP_AUX_OUTPUT)) {
> > err = -EOPNOTSUPP;
> > - goto err_pmu;
> > + goto err;
> > }
> >
> > if (cgroup_fd != -1) {
> > err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
> > if (err)
> > - goto err_pmu;
> > + goto err;
> > }
> >
> > err = exclusive_event_init(event);
> > if (err)
> > - goto err_pmu;
> > + goto err;
> >
> > if (has_addr_filter(event)) {
> > event->addr_filter_ranges = kcalloc(pmu->nr_addr_filters,
> > @@ -11961,7 +11982,7 @@ perf_event_alloc(struct perf_event_attr
> > GFP_KERNEL);
> > if (!event->addr_filter_ranges) {
> > err = -ENOMEM;
> > - goto err_per_task;
> > + goto err;
> > }
> >
> > /*
> > @@ -11986,41 +12007,21 @@ perf_event_alloc(struct perf_event_attr
> > if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
> > err = get_callchain_buffers(attr->sample_max_stack);
> > if (err)
> > - goto err_addr_filters;
> > + goto err;
> > }
> > }
> >
> > err = security_perf_event_alloc(event);
> > if (err)
> > - goto err_callchain_buffer;
> > + goto err;
> >
> > /* symmetric to unaccount_event() in _free_event() */
> > account_event(event);
> >
> > return event;
> >
> > -err_callchain_buffer:
> > - if (!event->parent) {
> > - if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
> > - put_callchain_buffers();
> > - }
>
> hum, so this is now called all the time via __free_event, but it should
> be called only if we passed get_callchain_buffers call.. this could screw
> up nr_callchain_events number eventually no?

Looks like so.

>
> jirka
>
> > -err_addr_filters:
> > - kfree(event->addr_filter_ranges);
> > -
> > -err_per_task:
> > - exclusive_event_destroy(event);
> > -
> > -err_pmu:
> > - if (is_cgroup_event(event))
> > - perf_detach_cgroup(event);
> > - if (event->destroy)
> > - event->destroy(event);
> > - module_put(pmu->module);

I'm afraid of this part. If it failed at perf_init_event(), it might
call event->destroy() already. I saw you cleared event->pmu
in the failure path. Maybe we need the same thing for the
event->destroy.

Thanks,
Namhyung

> > -err_ns:
> > - if (event->hw.target)
> > - put_task_struct(event->hw.target);
> > - call_rcu(&event->rcu_head, free_event_rcu);
> > -
> > +err:
> > + __free_event(event);
> > return ERR_PTR(err);
> > }
> >
> >
> >