projects/linux·blob assoc-array-fixes · refs

161 lines · 4.3 KiB

1/* SPDX-License-Identifier: GPL-2.0 */
2/* Simple ftrace probe wrapper */
3#ifndef _LINUX_FPROBE_H
4#define _LINUX_FPROBE_H
5
6#include <linux/compiler.h>
7#include <linux/ftrace.h>
8#include <linux/rcupdate.h>
9#include <linux/refcount.h>
10#include <linux/rhashtable.h>
11#include <linux/slab.h>
12
13struct fprobe;
14typedef int (*fprobe_entry_cb)(struct fprobe *fp, unsigned long entry_ip,
15 unsigned long ret_ip, struct ftrace_regs *regs,
16 void *entry_data);
17
18typedef void (*fprobe_exit_cb)(struct fprobe *fp, unsigned long entry_ip,
19 unsigned long ret_ip, struct ftrace_regs *regs,
20 void *entry_data);
21
22/**
23 * struct fprobe_hlist_node - address based hash list node for fprobe.
24 *
25 * @hlist: The hlist node for address search hash table.
26 * @addr: One of the probing address of @fp.
27 * @fp: The fprobe which owns this.
28 */
29struct fprobe_hlist_node {
30 struct rhlist_head hlist;
31 unsigned long addr;
32 struct fprobe *fp;
33};
34
35/**
36 * struct fprobe_hlist - hash list nodes for fprobe.
37 *
38 * @hlist: The hlist node for existence checking hash table.
39 * @rcu: rcu_head for RCU deferred release.
40 * @fp: The fprobe which owns this fprobe_hlist.
41 * @size: The size of @array.
42 * @array: The fprobe_hlist_node for each address to probe.
43 */
44struct fprobe_hlist {
45 struct hlist_node hlist;
46 struct rcu_head rcu;
47 struct fprobe *fp;
48 int size;
49 struct fprobe_hlist_node array[] __counted_by(size);
50};
51
52/**
53 * struct fprobe - ftrace based probe.
54 *
55 * @nmissed: The counter for missing events.
56 * @flags: The status flag.
57 * @entry_data_size: The private data storage size.
58 * @entry_handler: The callback function for function entry.
59 * @exit_handler: The callback function for function exit.
60 * @hlist_array: The fprobe_hlist for fprobe search from IP hash table.
61 */
62struct fprobe {
63 unsigned long nmissed;
64 unsigned int flags;
65 size_t entry_data_size;
66
67 fprobe_entry_cb entry_handler;
68 fprobe_exit_cb exit_handler;
69
70 struct fprobe_hlist *hlist_array;
71};
72
73/* This fprobe is soft-disabled. */
74#define FPROBE_FL_DISABLED 1
75
76/*
77 * This fprobe handler will be shared with kprobes.
78 * This flag must be set before registering.
79 */
80#define FPROBE_FL_KPROBE_SHARED 2
81
82static inline bool fprobe_disabled(struct fprobe *fp)
83{
84 return (fp) ? fp->flags & FPROBE_FL_DISABLED : false;
85}
86
87static inline bool fprobe_shared_with_kprobes(struct fprobe *fp)
88{
89 return (fp) ? fp->flags & FPROBE_FL_KPROBE_SHARED : false;
90}
91
92#ifdef CONFIG_FPROBE
93int register_fprobe(struct fprobe *fp, const char *filter, const char *notfilter);
94int register_fprobe_ips(struct fprobe *fp, unsigned long *addrs, int num);
95int register_fprobe_syms(struct fprobe *fp, const char **syms, int num);
96int unregister_fprobe(struct fprobe *fp);
97int unregister_fprobe_async(struct fprobe *fp);
98bool fprobe_is_registered(struct fprobe *fp);
99int fprobe_count_ips_from_filter(const char *filter, const char *notfilter);
100#else
101static inline int register_fprobe(struct fprobe *fp, const char *filter, const char *notfilter)
102{
103 return -EOPNOTSUPP;
104}
105static inline int register_fprobe_ips(struct fprobe *fp, unsigned long *addrs, int num)
106{
107 return -EOPNOTSUPP;
108}
109static inline int register_fprobe_syms(struct fprobe *fp, const char **syms, int num)
110{
111 return -EOPNOTSUPP;
112}
113static inline int unregister_fprobe(struct fprobe *fp)
114{
115 return -EOPNOTSUPP;
116}
117static inline int unregister_fprobe_async(struct fprobe *fp)
118{
119 return -EOPNOTSUPP;
120}
121static inline bool fprobe_is_registered(struct fprobe *fp)
122{
123 return false;
124}
125static inline int fprobe_count_ips_from_filter(const char *filter, const char *notfilter)
126{
127 return -EOPNOTSUPP;
128}
129#endif
130
131/**
132 * disable_fprobe() - Disable fprobe
133 * @fp: The fprobe to be disabled.
134 *
135 * This will soft-disable @fp. Note that this doesn't remove the ftrace
136 * hooks from the function entry.
137 */
138static inline void disable_fprobe(struct fprobe *fp)
139{
140 if (fp)
141 fp->flags |= FPROBE_FL_DISABLED;
142}
143
144/**
145 * enable_fprobe() - Enable fprobe
146 * @fp: The fprobe to be enabled.
147 *
148 * This will soft-enable @fp.
149 */
150static inline void enable_fprobe(struct fprobe *fp)
151{
152 if (fp)
153 fp->flags &= ~FPROBE_FL_DISABLED;
154}
155
156/* The entry data size is 4 bits (=16) * sizeof(long) in maximum */
157#define FPROBE_DATA_SIZE_BITS 4
158#define MAX_FPROBE_DATA_SIZE_WORD ((1L << FPROBE_DATA_SIZE_BITS) - 1)
159#define MAX_FPROBE_DATA_SIZE (MAX_FPROBE_DATA_SIZE_WORD * sizeof(long))
160
161#endif