hc
2024-05-10 37f49e37ab4cb5d0bc4c60eb5c6d4dd57db767bb
kernel/include/linux/kprobes.h
....@@ -1,22 +1,9 @@
1
+/* SPDX-License-Identifier: GPL-2.0-or-later */
12 #ifndef _LINUX_KPROBES_H
23 #define _LINUX_KPROBES_H
34 /*
45 * Kernel Probes (KProbes)
56 * include/linux/kprobes.h
6
- *
7
- * This program is free software; you can redistribute it and/or modify
8
- * it under the terms of the GNU General Public License as published by
9
- * the Free Software Foundation; either version 2 of the License, or
10
- * (at your option) any later version.
11
- *
12
- * This program is distributed in the hope that it will be useful,
13
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- * GNU General Public License for more details.
16
- *
17
- * You should have received a copy of the GNU General Public License
18
- * along with this program; if not, write to the Free Software
19
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
207 *
218 * Copyright (C) IBM Corporation, 2002, 2004
229 *
....@@ -171,12 +158,15 @@
171158 #define KRETPROBE_MAX_DATA_SIZE 4096
172159
173160 struct kretprobe_instance {
174
- struct hlist_node hlist;
161
+ union {
162
+ struct hlist_node hlist;
163
+ struct rcu_head rcu;
164
+ };
175165 struct kretprobe *rp;
176166 kprobe_opcode_t *ret_addr;
177167 struct task_struct *task;
178168 void *fp;
179
- char data[0];
169
+ char data[];
180170 };
181171
182172 struct kretprobe_blackpoint {
....@@ -202,10 +192,37 @@
202192 return 1;
203193 }
204194
195
+extern void kprobe_busy_begin(void);
196
+extern void kprobe_busy_end(void);
197
+
205198 #ifdef CONFIG_KRETPROBES
206199 extern void arch_prepare_kretprobe(struct kretprobe_instance *ri,
207200 struct pt_regs *regs);
208201 extern int arch_trampoline_kprobe(struct kprobe *p);
202
+
203
+/* If the trampoline handler called from a kprobe, use this version */
204
+unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs,
205
+ void *trampoline_address,
206
+ void *frame_pointer);
207
+
208
+static nokprobe_inline
209
+unsigned long kretprobe_trampoline_handler(struct pt_regs *regs,
210
+ void *trampoline_address,
211
+ void *frame_pointer)
212
+{
213
+ unsigned long ret;
214
+ /*
215
+ * Set a dummy kprobe for avoiding kretprobe recursion.
216
+ * Since kretprobe never runs in kprobe handler, no kprobe must
217
+ * be running at this point.
218
+ */
219
+ kprobe_busy_begin();
220
+ ret = __kretprobe_trampoline_handler(regs, trampoline_address, frame_pointer);
221
+ kprobe_busy_end();
222
+
223
+ return ret;
224
+}
225
+
209226 #else /* CONFIG_KRETPROBES */
210227 static inline void arch_prepare_kretprobe(struct kretprobe *rp,
211228 struct pt_regs *regs)
....@@ -218,16 +235,6 @@
218235 #endif /* CONFIG_KRETPROBES */
219236
220237 extern struct kretprobe_blackpoint kretprobe_blacklist[];
221
-
222
-static inline void kretprobe_assert(struct kretprobe_instance *ri,
223
- unsigned long orig_ret_address, unsigned long trampoline_address)
224
-{
225
- if (!orig_ret_address || (orig_ret_address == trampoline_address)) {
226
- printk("kretprobe BUG!: Processing kretprobe %p @ %p\n",
227
- ri->rp, ri->rp->kp.addr);
228
- BUG();
229
- }
230
-}
231238
232239 #ifdef CONFIG_KPROBES_SANITY_TEST
233240 extern int init_test_probes(void);
....@@ -242,7 +249,6 @@
242249 extern void arch_arm_kprobe(struct kprobe *p);
243250 extern void arch_disarm_kprobe(struct kprobe *p);
244251 extern int arch_init_kprobes(void);
245
-extern void show_registers(struct pt_regs *regs);
246252 extern void kprobes_inc_nmissed_count(struct kprobe *p);
247253 extern bool arch_within_kprobe_blacklist(unsigned long addr);
248254 extern int arch_populate_kprobe_blacklist(void);
....@@ -257,6 +263,7 @@
257263 struct mutex mutex;
258264 void *(*alloc)(void); /* allocate insn page */
259265 void (*free)(void *); /* free insn page */
266
+ const char *sym; /* symbol for insn pages */
260267 struct list_head pages; /* list of kprobe_insn_page */
261268 size_t insn_size; /* size of instruction slot */
262269 int nr_garbage;
....@@ -287,6 +294,10 @@
287294 { \
288295 return __is_insn_slot_addr(&kprobe_##__name##_slots, addr); \
289296 }
297
+#define KPROBE_INSN_PAGE_SYM "kprobe_insn_page"
298
+#define KPROBE_OPTINSN_PAGE_SYM "kprobe_optinsn_page"
299
+int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum,
300
+ unsigned long *value, char *type, char *sym);
290301 #else /* __ARCH_WANT_KPROBES_INSN_SLOT */
291302 #define DEFINE_INSN_CACHE_OPS(__name) \
292303 static inline bool is_kprobe_##__name##_slot(unsigned long addr) \
....@@ -327,10 +338,12 @@
327338 #ifdef CONFIG_SYSCTL
328339 extern int sysctl_kprobes_optimization;
329340 extern int proc_kprobes_optimization_handler(struct ctl_table *table,
330
- int write, void __user *buffer,
341
+ int write, void *buffer,
331342 size_t *length, loff_t *ppos);
332343 #endif
333344 extern void wait_for_kprobe_optimizer(void);
345
+bool optprobe_queued_unopt(struct optimized_kprobe *op);
346
+bool kprobe_disarmed(struct kprobe *p);
334347 #else
335348 static inline void wait_for_kprobe_optimizer(void) { }
336349 #endif /* CONFIG_OPTPROBES */
....@@ -344,10 +357,6 @@
344357
345358 /* Get the kprobe at this addr (if any) - called with preemption disabled */
346359 struct kprobe *get_kprobe(void *addr);
347
-void kretprobe_hash_lock(struct task_struct *tsk,
348
- struct hlist_head **head, unsigned long *flags);
349
-void kretprobe_hash_unlock(struct task_struct *tsk, unsigned long *flags);
350
-struct hlist_head * kretprobe_inst_table_head(struct task_struct *tsk);
351360
352361 /* kprobe_running() will just return the current_kprobe on this CPU */
353362 static inline struct kprobe *kprobe_running(void)
....@@ -365,10 +374,6 @@
365374 return this_cpu_ptr(&kprobe_ctlblk);
366375 }
367376
368
-extern struct kprobe kprobe_busy;
369
-void kprobe_busy_begin(void);
370
-void kprobe_busy_end(void);
371
-
372377 kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset);
373378 int register_kprobe(struct kprobe *p);
374379 void unregister_kprobe(struct kprobe *p);
....@@ -382,13 +387,22 @@
382387 void unregister_kretprobes(struct kretprobe **rps, int num);
383388
384389 void kprobe_flush_task(struct task_struct *tk);
385
-void recycle_rp_inst(struct kretprobe_instance *ri, struct hlist_head *head);
390
+
391
+void kprobe_free_init_mem(void);
386392
387393 int disable_kprobe(struct kprobe *kp);
388394 int enable_kprobe(struct kprobe *kp);
389395
390396 void dump_kprobe(struct kprobe *kp);
391397
398
+void *alloc_insn_page(void);
399
+void free_insn_page(void *page);
400
+
401
+int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
402
+ char *sym);
403
+
404
+int arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
405
+ char *type, char *sym);
392406 #else /* !CONFIG_KPROBES: */
393407
394408 static inline int kprobes_built_in(void)
....@@ -438,6 +452,9 @@
438452 static inline void kprobe_flush_task(struct task_struct *tk)
439453 {
440454 }
455
+static inline void kprobe_free_init_mem(void)
456
+{
457
+}
441458 static inline int disable_kprobe(struct kprobe *kp)
442459 {
443460 return -ENOSYS;
....@@ -445,6 +462,16 @@
445462 static inline int enable_kprobe(struct kprobe *kp)
446463 {
447464 return -ENOSYS;
465
+}
466
+
467
+static inline bool within_kprobe_blacklist(unsigned long addr)
468
+{
469
+ return true;
470
+}
471
+static inline int kprobe_get_kallsym(unsigned int symnum, unsigned long *value,
472
+ char *type, char *sym)
473
+{
474
+ return -ERANGE;
448475 }
449476 #endif /* CONFIG_KPROBES */
450477 static inline int disable_kretprobe(struct kretprobe *rp)
....@@ -469,4 +496,23 @@
469496 }
470497 #endif
471498
499
+/* Returns true if kprobes handled the fault */
500
+static nokprobe_inline bool kprobe_page_fault(struct pt_regs *regs,
501
+ unsigned int trap)
502
+{
503
+ if (!kprobes_built_in())
504
+ return false;
505
+ if (user_mode(regs))
506
+ return false;
507
+ /*
508
+ * To be potentially processing a kprobe fault and to be allowed
509
+ * to call kprobe_running(), we have to be non-preemptible.
510
+ */
511
+ if (preemptible())
512
+ return false;
513
+ if (!kprobe_running())
514
+ return false;
515
+ return kprobe_fault_handler(regs, trap);
516
+}
517
+
472518 #endif /* _LINUX_KPROBES_H */