^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Common header file for generic dynamic events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #ifndef _TRACE_DYNEVENT_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #define _TRACE_DYNEVENT_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct dyn_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * struct dyn_event_operations - Methods for each type of dynamic events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * These methods must be set for each type, since there is no default method.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * Before using this for dyn_event_init(), it must be registered by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * dyn_event_register().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * @create: Parse and create event method. This is invoked when user passes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * a event definition to dynamic_events interface. This must not destruct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * the arguments and return -ECANCELED if given arguments doesn't match its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * command prefix.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * @show: Showing method. This is invoked when user reads the event definitions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * via dynamic_events interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * @is_busy: Check whether given event is busy so that it can not be deleted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * Return true if it is busy, otherwides false.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * @free: Delete the given event. Return 0 if success, otherwides error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * @match: Check whether given event and system name match this event. The argc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * and argv is used for exact match. Return true if it matches, otherwides
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * false.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * Except for @create, these methods are called under holding event_mutex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct dyn_event_operations {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int (*create)(int argc, const char *argv[]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int (*show)(struct seq_file *m, struct dyn_event *ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) bool (*is_busy)(struct dyn_event *ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) int (*free)(struct dyn_event *ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) bool (*match)(const char *system, const char *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int argc, const char **argv, struct dyn_event *ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /* Register new dyn_event type -- must be called at first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int dyn_event_register(struct dyn_event_operations *ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * struct dyn_event - Dynamic event list header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * The dyn_event structure encapsulates a list and a pointer to the operators
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * for making a global list of dynamic events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * User must includes this in each event structure, so that those events can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * be added/removed via dynamic_events interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct dyn_event {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct dyn_event_operations *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) extern struct list_head dyn_event_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static inline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) int dyn_event_init(struct dyn_event *ev, struct dyn_event_operations *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (!ev || !ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) INIT_LIST_HEAD(&ev->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) ev->ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static inline int dyn_event_add(struct dyn_event *ev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) lockdep_assert_held(&event_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (!ev || !ev->ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) list_add_tail(&ev->list, &dyn_event_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static inline void dyn_event_remove(struct dyn_event *ev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) lockdep_assert_held(&event_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) list_del_init(&ev->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) void *dyn_event_seq_start(struct seq_file *m, loff_t *pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) void *dyn_event_seq_next(struct seq_file *m, void *v, loff_t *pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) void dyn_event_seq_stop(struct seq_file *m, void *v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int dyn_events_release_all(struct dyn_event_operations *type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) int dyn_event_release(int argc, char **argv, struct dyn_event_operations *type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * for_each_dyn_event - iterate over the dyn_event list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * @pos: the struct dyn_event * to use as a loop cursor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * This is just a basement of for_each macro. Wrap this for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * each actual event structure with ops filtering.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) #define for_each_dyn_event(pos) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) list_for_each_entry(pos, &dyn_event_list, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * for_each_dyn_event - iterate over the dyn_event list safely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * @pos: the struct dyn_event * to use as a loop cursor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * @n: the struct dyn_event * to use as temporary storage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) #define for_each_dyn_event_safe(pos, n) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) list_for_each_entry_safe(pos, n, &dyn_event_list, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) extern void dynevent_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) enum dynevent_type type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) dynevent_create_fn_t run_command);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) typedef int (*dynevent_check_arg_fn_t)(void *data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct dynevent_arg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) const char *str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) char separator; /* e.g. ';', ',', or nothing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) extern void dynevent_arg_init(struct dynevent_arg *arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) char separator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) extern int dynevent_arg_add(struct dynevent_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct dynevent_arg *arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) dynevent_check_arg_fn_t check_arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct dynevent_arg_pair {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) const char *lhs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) const char *rhs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) char operator; /* e.g. '=' or nothing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) char separator; /* e.g. ';', ',', or nothing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) extern void dynevent_arg_pair_init(struct dynevent_arg_pair *arg_pair,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) char operator, char separator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) extern int dynevent_arg_pair_add(struct dynevent_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct dynevent_arg_pair *arg_pair,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) dynevent_check_arg_fn_t check_arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) extern int dynevent_str_add(struct dynevent_cmd *cmd, const char *str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) #endif