On Tue, 16 Jun 2026 14:41:18 +0800
Li Pengfei <[email protected]> wrote:

>  int set_tracer_flag(struct trace_array *tr, u64 mask, int enabled)
>  {
>       switch (mask) {
> @@ -3993,6 +4091,33 @@ int set_tracer_flag(struct trace_array *tr, u64 mask, 
> int enabled)
>       if (!!(tr->trace_flags & mask) == !!enabled)
>               return 0;
>  
> +#ifdef CONFIG_FTRACE_STACKMAP
> +     /*
> +      * STACKMAP is intentionally global-instance-only: the dedup map,
> +      * its tracefs files (stack_map / stack_map_stat / stack_map_bin)
> +      * and the lifetime/reset semantics are tied to the global trace
> +      * array. options/stackmap is hidden on secondary instances via
> +      * TOP_LEVEL_TRACE_FLAGS, but writes still reach set_tracer_flag()
> +      * through the aggregate trace_options file. Reject the enable on
> +      * a secondary instance so it cannot be silently accepted and then
> +      * become a no-op in the hot path (where tr->stackmap is NULL and
> +      * the code falls back to a full stack trace).
> +      *
> +      * On the global instance, allow the enable while init is still
> +      * pending (boot-time trace_options=stackmap is applied before the
> +      * tracefs init work creates the map; the hot path falls back
> +      * until the map is published). Only reject once init has
> +      * permanently failed, so options/stackmap never reports an
> +      * enabled no-op. READ_ONCE() suffices: this only inspects the
> +      * init state, it does not dereference the map (the hot path uses
> +      * smp_load_acquire(&tr->stackmap) for that).
> +      */
> +     if (mask == TRACE_ITER(STACKMAP) && enabled &&
> +         (tr != &global_trace ||
> +          READ_ONCE(stackmap_init_state) == STACKMAP_INIT_FAILED))
> +             return -EINVAL;
> +#endif
> +
>       /* Give the tracer a chance to approve the change */
>       if (tr->current_trace->flag_changed)
>               if (tr->current_trace->flag_changed(tr, mask, !!enabled))
> @@ -9222,6 +9347,91 @@ static __init void 
> tracer_init_tracefs_work_func(struct work_struct *work)
>                       NULL, &tracing_dyn_info_fops);
>  #endif
>  
> +#ifdef CONFIG_FTRACE_STACKMAP
> +     {
> +             struct ftrace_stackmap *smap;
> +             struct dentry *map_file;
> +
> +             smap = ftrace_stackmap_create(&global_trace);
> +             if (!IS_ERR(smap)) {
> +                     /*
> +                      * Failure-atomic init: stack_map is the single
> +                      * required tracefs file (it doubles as the reset
> +                      * interface and the human-readable resolver). If
> +                      * we cannot create it, the hot path must not be
> +                      * able to emit <stack_id N> events that no one can
> +                      * resolve or clear, so refuse to publish the map
> +                      * and tear it down.
> +                      *
> +                      * Create stack_map BEFORE smp_store_release() so an
> +                      * observed non-NULL global_trace.stackmap implies
> +                      * its resolver/reset file exists.
> +                      */
> +                     map_file = trace_create_file("stack_map",
> +                                                  TRACE_MODE_WRITE, NULL,
> +                                                  smap,
> +                                                  &ftrace_stackmap_fops);
> +                     if (!map_file) {
> +                             pr_warn("ftrace stackmap init: stack_map create 
> failed, dedup disabled\n");
> +                             ftrace_stackmap_destroy(smap);
> +                             /*
> +                              * Permanent failure. Record it and clear a
> +                              * STACKMAP flag that a boot-time
> +                              * trace_options=stackmap may have set, so
> +                              * options/stackmap does not report an
> +                              * enabled no-op and later userspace enables
> +                              * return -EINVAL.
> +                              */
> +                             WRITE_ONCE(stackmap_init_state,
> +                                        STACKMAP_INIT_FAILED);
> +                             global_trace.trace_flags &=
> +                                     ~TRACE_ITER(STACKMAP);

80 columns is no longer a hard requirement. 100 is more the default, so the
above should be:

                                WRITE_ONCE(stackmap_init_state, 
STACKMAP_INIT_FAILED);
                                global_trace.trace_flags &= 
~TRACE_ITER(STACKMAP);



> +                     } else {
> +                             /*
> +                              * smp_store_release pairs with the
> +                              * smp_load_acquire() in
> +                              * __ftrace_trace_stack(). Publishing only
> +                              * after the required file exists keeps
> +                              * "smap visible" => "resolver/reset
> +                              * available".
> +                              */

> +                             smp_store_release(&global_trace.stackmap,
> +                                               smap);
> +                             WRITE_ONCE(stackmap_init_state,
> +                                        STACKMAP_INIT_DONE);

Same with the above two.

> +                             /*
> +                              * stat and bin are auxiliary observability
> +                              * surfaces. If they fail to be created we
> +                              * keep dedup enabled (the kernel side still
> +                              * works, and stack_map alone is enough to
> +                              * resolve and reset); trace_create_file()
> +                              * already pr_warn()s on failure.
> +                              */
> +                             trace_create_file("stack_map_stat",
> +                                               TRACE_MODE_READ, NULL,
> +                                               smap,
> +                                               &ftrace_stackmap_stat_fops);
> +                             trace_create_file("stack_map_bin",
> +                                               TRACE_MODE_READ, NULL,
> +                                               smap,
> +                                               &ftrace_stackmap_bin_fops);
> +                     }

-- Steve

Reply via email to