Debian Codegraph @

runtime/transport/transport.c

raw
1 /* -*- linux-c -*-
2 * transport.c - stp transport functions
3 *
4 * Copyright (C) IBM Corporation, 2005
5 * Copyright (C) Red Hat Inc, 2005-2014
6 * Copyright (C) Intel Corporation, 2006
7 *
8 * This file is part of systemtap, and is free software. You can
9 * redistribute it and/or modify it under the terms of the GNU General
10 * Public License (GPL); either version 2, or (at your option) any
11 * later version.
12 */
13
14 #ifndef _TRANSPORT_TRANSPORT_C_
15 #define _TRANSPORT_TRANSPORT_C_
16
17 #include "transport.h"
18 #include "control.h"
19 #include "../sym.h"
20 #include <linux/debugfs.h>
21 #include <linux/namei.h>
22 #include <linux/delay.h>
23 #include <linux/mutex.h>
24 #ifdef STAPCONF_LOCKDOWN_DEBUGFS
25 #include <linux/security.h>
26 #endif
27 #ifdef STAPCONF_514_PANIC
28 #include <linux/panic_notifier.h>
29 #endif
30 #include "../uidgid_compatibility.h"
31
32 static int _stp_exit_flag = 0;
33
34 static uid_t _stp_uid = 0;
35 static gid_t _stp_gid = 0;
36 static int _stp_pid = 0;
37 static int _stp_namespaces_pid = 0;
38 static int _stp_remote_id = -1;
39 static char _stp_remote_uri[MAXSTRINGLEN];
40
41 static atomic_t _stp_ctl_attached = ATOMIC_INIT(0);
42
43 static pid_t _stp_target = 0;
44 static int _stp_probes_started = 0;
45
46 #define _STP_TS_UNINITIALIZED 0
47 #define _STP_TS_START_CALLED 1
48 #define _STP_TS_START_FINISHED 2
49 #define _STP_TS_EXIT_CALLED 3
50
51 /* We only want to do the startup and exit sequences once, so the
52 * transport state flag is atomic. */
53 static atomic_t _stp_transport_state = ATOMIC_INIT(_STP_TS_UNINITIALIZED);
54
55 static inline void _stp_lock_inode(struct inode *inode);
56 static inline void _stp_unlock_inode(struct inode *inode);
57
58 #ifndef STP_CTL_TIMER_INTERVAL
59 /* ctl timer interval in jiffies (default 20 ms) */
60 #define STP_CTL_TIMER_INTERVAL ((HZ+49)/50)
61 #endif
62
63 /* Defines the number of buffers allocated in control.c (which #includes
64 this file) for the _stp_pool_q. This is the number of .cmd messages
65 the module can store before they have to be read by stapio.
66 40 is somewhat arbitrary, 8 pre-allocated messages, 32 dynamic. */
67 #define STP_DEFAULT_BUFFERS 256
68
69 #include "control.h"
70 #include "relay_v2.c"
71 #include "debugfs.c"
72 #include "procfs.c"
73 #include "control.c"
74
75 /* set default buffer parameters. User may override these via stap -s #, and
76 the runtime may auto-shrink it on low memory machines too. */
77 /* NB: Note default in man/stap.1.in */
78 static unsigned _stp_nsubbufs = 16*1024*1024 / PAGE_SIZE;
79 static unsigned _stp_subbuf_size = PAGE_SIZE;
80
81 /* module parameters */
82 static int _stp_bufsize;
83 module_param(_stp_bufsize, int, 0);
84 MODULE_PARM_DESC(_stp_bufsize, "buffer size");
85
86 /* forward declarations */
87 static void systemtap_module_exit(void);
88 static int systemtap_module_init(void);
89
90 static int _stp_module_notifier_active = 0;
91 static int _stp_module_notifier (struct notifier_block * nb,
92 unsigned long val, void *data);
93 static struct notifier_block _stp_module_notifier_nb = {
94 .notifier_call = _stp_module_notifier,
95 // We used to have this set to 1 before since that is also what
96 // kernel/trace/trace_kprobe.c does as well. The idea was that we should
97 // be notified _after_ the kprobe infrastruture itself is notified.
98 // However, that was the exact opposite of what was happening (we were
99 // called _before_ kprobes). In the end, we do not have a hard
100 // requirement as to being called before or after kprobes itself, so
101 // just leave the default of 0. (See also PR16861).
102 .priority = 0
103 };
104
105 static int _stp_module_panic_notifier (struct notifier_block * nb,
106 unsigned long val, void *data);
107 static struct notifier_block _stp_module_panic_notifier_nb = {
108 .notifier_call = _stp_module_panic_notifier,
109 .priority = INT_MAX
110 };
111
112 static struct timer_list _stp_ctl_work_timer;
113
114
115
116 // ------------------------------------------------------------------------
117
118 // Dispatching functions to choose between procfs and debugfs variants
119 // of the transport. PR26665
120
121 static int _stp_transport_fs_init(const char *module_name)
122 {
123 // Default to using procfs
124 procfs_p = 1;
125
126 // BTW: testing the other !FOO_p first is to protect against repeated
127 // invocations of this function with security_locked_down() changing
128 #ifdef STAPCONF_LOCKDOWN_DEBUGFS
129 if (!debugfs_p && security_locked_down (LOCKDOWN_DEBUGFS)) {
130 procfs_p = 1;
131 dbug_trans(1, "choosing procfs_p=1\n");
132 }
133 #endif
134 #ifdef STAPCONF_LOCKDOWN_KERNEL
135 if (!debugfs_p && kernel_is_locked_down ("debugfs")) {
136 procfs_p = 1;
137 dbug_trans(1, "choosing procfs_p=1\n");
138 }
139 #endif
140 #ifdef STAP_TRANS_PROCFS
141 procfs_p = 1;
142 debugfs_p = 0;
143 dbug_trans(1, "forcing procfs_p=1\n");
144 #endif
145 #ifdef STAP_TRANS_DEBUGFS
146 procfs_p = 0;
147 debugfs_p = 1;
148 dbug_trans(1, "forcing debugfs_p=1\n");
149 #endif
150
151 if (debugfs_p)
152 return _stp_debugfs_transport_fs_init(module_name);
153 if (procfs_p)
154 return _stp_procfs_transport_fs_init(module_name);
155 return -ENOSYS;
156 }
157
158 static void _stp_transport_fs_close(void)
159 {
160 if (debugfs_p)
161 _stp_debugfs_transport_fs_close();
162 if (procfs_p)
163 _stp_procfs_transport_fs_close();
164 }
165
166
167 static int _stp_ctl_write_fs(int type, void *data, unsigned len)
168 {
169 if (procfs_p)
170 return _stp_procfs_ctl_write_fs (type, data, len);
171 if (debugfs_p)
172 return _stp_debugfs_ctl_write_fs (type, data, len);
173 return -ENOSYS;
174 }
175
176
177 static int _stp_register_ctl_channel_fs(void)
178 {
179 if (debugfs_p)
180 return _stp_debugfs_register_ctl_channel_fs();
181 if (procfs_p)
182 return _stp_procfs_register_ctl_channel_fs();
183
184 return -ENOSYS;
185 }
186
187
188 static void _stp_unregister_ctl_channel_fs(void)
189 {
190 if (procfs_p)
191 _stp_procfs_unregister_ctl_channel_fs();
192 if (debugfs_p)
193 _stp_debugfs_unregister_ctl_channel_fs();
194 }
195
196
197
198 static struct dentry *_stp_get_module_dir(void)
199 {
200 if (procfs_p)
201 return _stp_procfs_get_module_dir();
202 if (debugfs_p)
203 return _stp_debugfs_get_module_dir();
204 return NULL;
205 }
206
207
208
209 // ------------------------------------------------------------------------
210
211 /*
212 * _stp_handle_start - handle STP_START
213 */
214
215 // PR17232: This might be called more than once, but not concurrently
216 // or reentrantly with itself, or with _stp_cleanup_and_exit. (The
217 // latter case is not obvious: _stp_cleanup_and_exit could be called
218 // from the mutex-protected ctl message handler, so that's fine; or
219 // it could be called from the module cleanup function, by which time
220 // we know there is no ctl connection and thus no messages. So again
221 // no concurrency.
222
223 static void _stp_handle_start(struct _stp_msg_start *st)
224 {
225 int handle_startup;
226 #if defined(CONFIG_USER_NS)
227 struct pid *_upid = NULL;
228 struct task_struct *_utask = NULL;
229 #endif
230
231 // protect against excessive or premature startup
232 handle_startup = (atomic_cmpxchg(&_stp_transport_state,
233 _STP_TS_UNINITIALIZED,
234 _STP_TS_START_CALLED)
235 == _STP_TS_UNINITIALIZED);
236 if (handle_startup) {
237 dbug_trans(1, "stp_handle_start\n");
238
239 _stp_target = st->target;
240 _stp_orig_target = _stp_target;
241
242 #if defined(CONFIG_USER_NS)
243 rcu_read_lock();
244 _upid = find_vpid(_stp_target);
245 if (_upid)
246 {
247 _utask = pid_task(_upid, PIDTYPE_PID);
248 if (_utask)
249 {
250 #ifdef DEBUG_UPROBES
251 _stp_dbug(__FUNCTION__,__LINE__, "translating vpid %d to pid %d\n", _stp_target, _utask->pid);
252 #endif
253 _stp_target = _utask->pid;
254 }
255 }
256
257 #ifdef DEBUG_UPROBES
258 if (!_upid || !_utask)
259 _stp_dbug(__FUNCTION__,__LINE__, "cannot map pid %d to host namespace pid\n", _stp_target);
260 #endif
261
262 rcu_read_unlock();
263 #endif
264
265 #ifdef STAP_MODULE_INIT_HOOK
266 st->res = STAP_MODULE_INIT_HOOK();
267 if (st->res != 0) {
268 _stp_error ("Failed to run STAP_MODULE_INIT_HOOK "
269 "(%d)\n", st->res);
270 } else {
271 #else
272 {
273 #endif
274 st->res = systemtap_module_init();
275 }
276
277 if (st->res == 0) {
278 _stp_probes_started = 1;
279
280 /* Register the module notifier ... */
281 /* NB: but not if the module_init stuff
282 failed: something nasty has happened, and
283 we want no further probing started. PR16766 */
284 if (!_stp_module_notifier_active) {
285 int rc = register_module_notifier(& _stp_module_notifier_nb);
286 if (rc == 0)
287 _stp_module_notifier_active = 1;
288 else
289 _stp_warn ("Cannot register module notifier (%d)\n", rc);
290 }
291 }
292
293 /* Called from the user context in response to a proc
294 file write (in _stp_ctl_write_cmd), so may notify
295 the reader directly. */
296 _stp_ctl_send_notify(STP_START, st, sizeof(*st));
297
298 /* Register the panic notifier. */
299 atomic_notifier_chain_register(&panic_notifier_list, &_stp_module_panic_notifier_nb);
300
301 /* If we're here, we know that _stp_transport_state
302 * is _STP_TS_START_CALLED. Go ahead and set it to
303 * _STP_TS_START_FINISHED. */
304 atomic_set(&_stp_transport_state, _STP_TS_START_FINISHED);
305 }
306 }
307
308 #if defined(CONFIG_KALLSYMS) && !defined(STAPCONF_KALLSYMS_LOOKUP_NAME_EXPORTED)
309 unsigned _stp_need_kallsyms_stext;
310 #endif
311
312 // PR26074: Most kallsyms_lookup_name() calls are done in appropriate
313 // subsystem init() functions, but a handful are done at module load
314 // time:
315 static int _stp_handle_kallsyms_lookups(void)
316 {
317 const char *symname = NULL;
318 might_sleep();
319
320 #define NEW_KALLSYMS_SYM(name) \
321 kallsyms_ ## name = (void *) kallsyms_lookup_name (#name); \
322 cond_resched(); \
323 if (unlikely(kallsyms_ ## name == NULL)) { \
324 /* ignore */ \
325 ; \
326 }
327
328 NEW_KALLSYMS_SYM(switch_task_namespaces)
329 NEW_KALLSYMS_SYM(unshare_nsproxy_namespaces)
330 NEW_KALLSYMS_SYM(proc_ns_file)
331 NEW_KALLSYMS_SYM(free_nsproxy)
332
333 #undef KALLSYMS_LOOKUP_SYM
334
335 #ifndef STAPCONF_NMI_UACCESS_OKAY
336 kallsyms_nmi_uaccess_okay = (void*) kallsyms_lookup_name ("nmi_uaccess_okay");
337 cond_resched(); /* kallsyms_lookup_name is expensive; avoid blocking here */
338 if (kallsyms_nmi_uaccess_okay == NULL) {
339 /* Not finding this symbol is non-fatal for non-X86 architectures
340 * since nmi_uaccess_okay() does not do anything for non-X86 archs.
341 * Also, we don't treat it as fatal even on X86 since nmi_uaccess_okay()
342 * is a relatively new addition in upstream kernel commit 4012e77a90
343 * since Aug 2018.
344 */
345 ;
346 }
347 #endif /* STAPCONF_NMI_UACCESS_OKAY */
348
349 #if !defined(STAPCONF_SET_FS)
350 /* PR26811, missing copy_to_kernel_nofault symbol-export */
351 kallsyms_copy_to_kernel_nofault = (void*) kallsyms_lookup_name ("copy_to_kernel_nofault");
352 cond_resched(); /* kallsyms_lookup_name is expensive; avoid blocking here */
353 /* Not finding this symbol is non-fatal. Kernel writes will fault safely: */
354 if (kallsyms_copy_to_kernel_nofault == NULL) {
355 ;
356 }
357 #endif
358 /* PR13489, missing inode-uprobes symbol-export workaround */
359 #if !defined(STAPCONF_TASK_USER_REGSET_VIEW_EXPORTED) /* RHEL5 era utrace */
360 kallsyms_task_user_regset_view = (void*) kallsyms_lookup_name ("task_user_regset_view");
361 cond_resched(); /* kallsyms_lookup_name is expensive; avoid blocking here */
362 /* There exist interesting kernel versions without task_user_regset_view(), like ARM before 3.0.
363 For these kernels, uprobes etc. are out of the question, but plain kernel stap works fine.
364 All we have to accomplish is have the loc2c runtime code compile. For that, it's enough
365 to leave this pointer zero. */
366 if (kallsyms_task_user_regset_view == NULL) {
367 ;
368 }
369 #endif
370 #if defined(CONFIG_UPROBES) // i.e., kernel-embedded uprobes
371 #if !defined(STAPCONF_UPROBE_REGISTER_EXPORTED)
372 kallsyms_uprobe_register = (void*) kallsyms_lookup_name ("uprobe_register");
373 cond_resched(); /* kallsyms_lookup_name is expensive; avoid blocking here */
374 if (kallsyms_uprobe_register == NULL) {
375 kallsyms_uprobe_register = (void*) kallsyms_lookup_name ("register_uprobe");
376 cond_resched(); /* kallsyms_lookup_name is expensive; avoid blocking here */
377 }
378 if (kallsyms_uprobe_register == NULL) {
379 _stp_error("Can't resolve uprobe_register!");
380 goto err0;
381 }
382 #endif
383 #if !defined(STAPCONF_UPROBE_UNREGISTER_EXPORTED)
384 kallsyms_uprobe_unregister = (void*) kallsyms_lookup_name ("uprobe_unregister");
385 cond_resched(); /* kallsyms_lookup_name is expensive; avoid blocking here */
386 #if defined(STAPCONF_PR32194_UPROBE_REGISTER_UNREGISTER)
387 if (kallsyms_uprobe_unregister == NULL) {
388 kallsyms_uprobe_unregister = (void*) kallsyms_lookup_name ("uprobe_unregister_nosync");
389 cond_resched(); /* kallsyms_lookup_name is expensive; avoid blocking here */
390 }
391 #endif
392 if (kallsyms_uprobe_unregister == NULL) {
393 kallsyms_uprobe_unregister = (void*) kallsyms_lookup_name ("unregister_uprobe");
394 cond_resched(); /* kallsyms_lookup_name is expensive; avoid blocking here */
395 }
396 if (kallsyms_uprobe_unregister == NULL) {
397 _stp_error("Can't resolve uprobe_unregister!");
398 goto err0;
399 }
400 #endif
401 #if !defined(STAPCONF_UPROBE_GET_SWBP_ADDR_EXPORTED)
402 kallsyms_uprobe_get_swbp_addr = (void*) kallsyms_lookup_name ("uprobe_get_swbp_addr");
403 cond_resched(); /* kallsyms_lookup_name is expensive; avoid blocking here */
404 if (kallsyms_uprobe_get_swbp_addr == NULL) {
405 _stp_error("Can't resolve uprobe_get_swbp_addr!");
406 goto err0;
407 }
408 #endif
409 #if !defined(STAPCONF_GET_MM_EXE_FILE_EXPORTED) && (defined(get_file_rcu) || LINUX_VERSION_CODE >= KERNEL_VERSION(4,1,0))
410 kallsyms_get_mm_exe_file = (void*) kallsyms_lookup_name ("get_mm_exe_file");
411 cond_resched(); /* kallsyms_lookup_name is expensive; avoid blocking here */
412 if (kallsyms_get_mm_exe_file == NULL) {
413 _stp_error("Can't resolve get_mm_exe_file!");
414 goto err0;
415 }
416 #endif
417 #endif
418 #if defined(CONFIG_KALLSYMS) && !defined(STAPCONF_KALLSYMS_LOOKUP_NAME_EXPORTED)
419 {
420 uint64_t address;
421 if (_stp_need_kallsyms_stext) {
422 address = kallsyms_lookup_name("_stext");
423 cond_resched(); /* kallsyms_lookup_name is expensive; avoid blocking here */
424 _stp_set_stext(address);
425 _stp_need_kallsyms_stext = 0;
426 }
427 }
428 #endif
429 return 0;
430 err0:
431 return -1;
432 }
433
434 // _stp_cleanup_and_exit: handle STP_EXIT and cleanup_module
435 //
436 /* We need to call it both times because we want to clean up properly */
437 /* when someone does /sbin/rmmod on a loaded systemtap module. */
438 static void _stp_cleanup_and_exit(int send_exit)
439 {
440 int handle_exit;
441
442 // protect against excessive or premature cleanup
443 handle_exit = (atomic_cmpxchg(&_stp_transport_state,
444 _STP_TS_START_FINISHED,
445 _STP_TS_EXIT_CALLED)
446 == _STP_TS_START_FINISHED);
447 if (handle_exit) {
448 int failures;
449
450 dbug_trans(1, "cleanup_and_exit (%d)\n", send_exit);
451
452 /* Unregister the module notifier. */
453 if (_stp_module_notifier_active) {
454 int rc = unregister_module_notifier(& _stp_module_notifier_nb);
455 if (rc)
456 _stp_warn("module_notifier unregister error %d", rc);
457 _stp_module_notifier_active = 0;
458 stp_synchronize_sched(); // paranoia: try to ensure no further calls in progress
459 }
460
461 _stp_exit_flag = 1;
462
463 if (_stp_probes_started) {
464 dbug_trans(1, "calling systemtap_module_exit\n");
465 /* tell the stap-generated code to unload its probes, etc */
466 systemtap_module_exit();
467 dbug_trans(1, "done with systemtap_module_exit\n");
468 }
469
470 failures = atomic_read(&_stp_transport_failures);
471 if (failures)
472 _stp_warn("There were %d transport failures. Try stap -s to increase the buffer size from %d.\n", failures, _stp_bufsize);
473
474 dbug_trans(1, "*** calling _stp_transport_data_fs_stop ***\n");
475 _stp_transport_data_fs_stop();
476
477 dbug_trans(1, "ctl_send STP_EXIT\n");
478 if (send_exit) {
479 /* send_exit is only set to one if called from
480 _stp_ctl_write_cmd() in response to a write
481 to the proc cmd file, so in user context. It
482 is safe to immediately notify the reader. */
483 _stp_ctl_send_notify(STP_EXIT, NULL, 0);
484 }
485 dbug_trans(1, "done with ctl_send STP_EXIT\n");
486
487 /* Unregister the panic notifier. */
488 atomic_notifier_chain_unregister(&panic_notifier_list, &_stp_module_panic_notifier_nb);
489 }
490 }
491
492
493 // Coming from script type sources, e.g. the exit() tapset function:
494 // consists of sending a message to staprun/stapio, and definitely
495 // NOT calling _stp_cleanup_and_exit(), since that function requires
496 // a more user context to run from.
497 static void _stp_request_exit(void)
498 {
499 static int called = 0;
500 if (!called) {
501 /* we only want to do this once; XXX: why? what's the harm? */
502 called = 1;
503 dbug_trans(1, "ctl_send STP_REQUEST_EXIT\n");
504 /* Called from the timer when _stp_exit_flag has been
505 been set. So safe to immediately notify any readers. */
506 _stp_ctl_send_notify(STP_REQUEST_EXIT, NULL, 0);
507 dbug_trans(1, "done with ctl_send STP_REQUEST_EXIT\n");
508 }
509 }
510
511 /*
512 * Called when stapio closes the control channel.
513 */
514 static void _stp_detach(void)
515 {
516 dbug_trans(1, "detach\n");
517 _stp_pid = 0;
518 _stp_namespaces_pid = 0;
519 _stp_target_mnt_ns_fd = -1;
520 _stp_orig_mnt_ns_fd = -1;
521 _stp_fs_struct_unshared = false;
522
523 if (!_stp_exit_flag)
524 _stp_transport_data_fs_overwrite(1);
525
526 STP_TIMER_DELETE_SYNC(&_stp_ctl_work_timer);
527 wake_up_interruptible(&_stp_ctl_wq);
528 }
529
530
531 static void _stp_ctl_work_callback(stp_timer_callback_parameter_t unused);
532
533 /*
534 * Called when stapio opens the control channel.
535 */
536 static void _stp_attach(void)
537 {
538 dbug_trans(1, "attach\n");
539 _stp_pid = current->pid;
540 if (_stp_namespaces_pid < 1)
541 _stp_namespaces_pid = _stp_pid;
542 _stp_transport_data_fs_overwrite(0);
543
544 timer_setup(&_stp_ctl_work_timer, _stp_ctl_work_callback, 0);
545 _stp_ctl_work_timer.expires = jiffies + STP_CTL_TIMER_INTERVAL;
546 add_timer(&_stp_ctl_work_timer);
547 }
548
549 /*
550 * _stp_ctl_work_callback - periodically check for IO or exit
551 * This IO comes from control messages like system(), warn(),
552 * that could potentially have been send from krpobe context,
553 * so they don't immediately trigger a wake_up of _stp_ctl_wq.
554 * This is run by a kernel thread and may NOT sleep, but it
555 * may call wake_up_interruptible on _stp_ctl_wq to notify
556 * any readers, or send messages itself that are immediately
557 * notified. Reschedules itself if someone is still attached
558 * to the cmd channel.
559 */
560 static void _stp_ctl_work_callback(stp_timer_callback_parameter_t unused)
561 {
562 int do_io = 0;
563 unsigned long flags;
564 struct context* __restrict__ c = NULL;
565
566 /* Prevent probe reentrancy while grabbing probe-used locks. */
567 c = _stp_runtime_entryfn_get_context();
568
569 stp_spin_lock_irqsave(&_stp_ctl_ready_lock, flags);
570 if (!list_empty(&_stp_ctl_ready_q))
571 do_io = 1;
572 stp_spin_unlock_irqrestore(&_stp_ctl_ready_lock, flags);
573
574 _stp_runtime_entryfn_put_context(c);
575
576 if (do_io)
577 wake_up_interruptible(&_stp_ctl_wq);
578
579 /* if exit flag is set AND we have finished with systemtap_module_init() */
580 if (unlikely(_stp_exit_flag && _stp_probes_started))
581 _stp_request_exit();
582 if (atomic_read(& _stp_ctl_attached))
583 mod_timer (&_stp_ctl_work_timer, jiffies + STP_CTL_TIMER_INTERVAL);
584 }
585
586 /**
587 * _stp_transport_close - close ctl and relayfs channels
588 *
589 * This is called automatically when the module is unloaded.
590 *
591 */
592 static void _stp_transport_close(void)
593 {
594 dbug_trans(1, "%d: ************** transport_close *************\n",
595 current->pid);
596 _stp_cleanup_and_exit(0);
597 _stp_unregister_ctl_channel();
598 _stp_print_cleanup(); /* Requires the transport, so free this first */
599 _stp_transport_fs_close();
600 #ifdef STAP_MODULE_EXIT_HOOK
601 STAP_MODULE_EXIT_HOOK();
602 #endif
603 _stp_mem_debug_done();
604
605 dbug_trans(1, "---- CLOSED ----\n");
606 }
607
608 /**
609 * _stp_transport_init() is called from the module initialization.
610 * It does the bare minimum to exchange commands with staprun
611 */
612 static int _stp_transport_init(void)
613 {
614 int ret;
615
616 dbug_trans(1, "transport_init\n");
617 #ifdef STAPCONF_TASK_UID
618 _stp_uid = current->uid;
619 _stp_gid = current->gid;
620 #else
621 #if defined(CONFIG_USER_NS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0))
622 _stp_uid = from_kuid_munged(current_user_ns(), current_uid());
623 _stp_gid = from_kgid_munged(current_user_ns(), current_gid());
624 #else
625 _stp_uid = current_uid();
626 _stp_gid = current_gid();
627 #endif
628 #endif
629
630 /* PR26074: kallsyms_lookup_name() invocations are now performed later.
631 Just in case, we should init some of the variables to NULL. */
632 #if !defined(STAPCONF_TASK_USER_REGSET_VIEW_EXPORTED)
633 kallsyms_task_user_regset_view = NULL;
634 #endif
635 #if defined(CONFIG_UPROBES) // i.e., kernel-embedded uprobes
636 #if !defined(STAPCONF_UPROBE_REGISTER_EXPORTED)
637 kallsyms_uprobe_register = NULL;
638 #endif
639 #if !defined(STAPCONF_UPROBE_UNREGISTER_EXPORTED)
640 kallsyms_uprobe_unregister = NULL;
641 #endif
642 #if !defined(STAPCONF_UPROBE_GET_SWBP_ADDR_EXPORTED)
643 kallsyms_uprobe_get_swbp_addr = NULL;
644 #endif
645 #endif
646 #if !defined(STAPCONF_KALLSYMS_LOOKUP_NAME_EXPORTED)
647 _stp_kallsyms_lookup_name = NULL;
648 #endif
649 #if defined(STAPCONF_KALLSYMS_ON_EACH_SYMBOL) && !defined(STAPCONF_KALLSYMS_ON_EACH_SYMBOL_EXPORTED)
650 _stp_kallsyms_on_each_symbol = NULL;
651 #endif
652 _stp_module_kallsyms_on_each_symbol = NULL;
653 #if defined(CONFIG_KALLSYMS) && !defined(STAPCONF_KALLSYMS_LOOKUP_NAME_EXPORTED)
654 _stp_need_kallsyms_stext = 0;
655 #endif
656
657 if (_stp_bufsize == 0) { // option not specified?
658 struct sysinfo si;
659 long _stp_bufsize_avail;
660 si_meminfo(&si);
661 _stp_bufsize_avail = (long)((si.freeram + si.bufferram) / 4 / num_online_cpus())
662 << PAGE_SHIFT; // limit to quarter of free ram total, divided between cpus
663 if ((_stp_nsubbufs * _stp_subbuf_size) > _stp_bufsize_avail) {
664 _stp_bufsize = max_t (int, 1, _stp_bufsize_avail / 1024 / 1024);
665 dbug_trans(1, "Shrinking default _stp_bufsize to %d MB/cpu due to low free memory\n", _stp_bufsize);
666 }
667 }
668
669 if (_stp_bufsize) { // overridden by user or by si_meminfo heuristic?
670 long size = _stp_bufsize * 1024 * 1024;
671 _stp_subbuf_size = 1 << PAGE_SHIFT; // XXX: allow this to be bumped up too?
672 _stp_nsubbufs = size / _stp_subbuf_size;
673 }
674
675 _stp_bufsize = (long)_stp_subbuf_size * (long)_stp_nsubbufs / 1024 / 1024; // for diagnostics later
676 dbug_trans(1, "Using %d subbufs of size %d * %d CPUs\n",
677 _stp_nsubbufs, _stp_subbuf_size, num_online_cpus());
678
679 ret = _stp_transport_fs_init(THIS_MODULE->name);
680 if (ret)
681 goto err0;
682
683 /* create control channel */
684 ret = _stp_register_ctl_channel();
685 if (ret < 0)
686 goto err1;
687
688 /* create print buffers */
689 ret = _stp_print_init();
690 if (ret < 0) {
691 errk("%s: can't create print buffers!", THIS_MODULE->name);
692 goto err2;
693 }
694
695 /* set _stp_module_self dynamic info */
696 ret = _stp_module_update_self();
697 if (ret < 0) {
698 errk("%s: can't update dynamic info!", THIS_MODULE->name);
699 goto err3;
700 }
701
702 /* start transport */
703 _stp_transport_data_fs_start();
704
705 /* Signal stapio to send us STP_START back.
706 This is an historic convention. This was called
707 STP_TRANSPORT_INFO and had a payload that described the
708 transport buffering, this is no longer the case.
709 Called during module initialization time, so safe to immediately
710 notify reader we are ready. */
711 _stp_ctl_send_notify(STP_TRANSPORT, NULL, 0);
712
713 dbug_trans(1, "returning 0...\n");
714 return 0;
715
716 err3:
717 _stp_print_cleanup();
718 err2:
719 _stp_unregister_ctl_channel();
720 err1:
721 _stp_transport_fs_close();
722 err0:
723 return ret;
724 }
725
726 static inline void _stp_lock_inode(struct inode *inode)
727 {
728 #ifdef STAPCONF_INODE_RWSEM
729 inode_lock(inode);
730 #else
731 might_sleep();
732 mutex_lock(&inode->i_mutex);
733 #endif
734 }
735
736 static inline void _stp_unlock_inode(struct inode *inode)
737 {
738 #ifdef STAPCONF_INODE_RWSEM
739 inode_unlock(inode);
740 #else
741 mutex_unlock(&inode->i_mutex);
742 #endif
743 }
744
745
746
747 /* NB: Accessed from tzinfo.stp tapset */
748 static uint64_t tz_gmtoff;
749 static char tz_name[MAXSTRINGLEN];
750
751 static void _stp_handle_tzinfo (struct _stp_msg_tzinfo* tzi)
752 {
753 tz_gmtoff = tzi->tz_gmtoff;
754 strlcpy (tz_name, tzi->tz_name, MAXSTRINGLEN);
755 /* We may silently truncate the incoming string,
756 * for example if MAXSTRINGLEN < STP_TZ_NAME_LEN-1 */
757 }
758
759
760 static int32_t _stp_privilege_credentials = 0;
761
762 static void _stp_handle_privilege_credentials (struct _stp_msg_privilege_credentials* pc)
763 {
764 _stp_privilege_credentials = pc->pc_group_mask;
765 }
766
767 static void _stp_handle_remote_id (struct _stp_msg_remote_id* rem)
768 {
769 _stp_remote_id = (int64_t) rem->remote_id;
770 strlcpy(_stp_remote_uri, rem->remote_uri, min(STP_REMOTE_URI_LEN,MAXSTRINGLEN));
771 }
772
773 static void _stp_handle_namespaces_pid (struct _stp_msg_ns_pid *nspid)
774 {
775 if (nspid->target > 0) {
776 _stp_namespaces_pid = (int) nspid->target;
777 }
778 }
779
780 static void _stp_handle_mnt_ns_fds (struct _stp_msg_mnt_ns_fds *nsfds)
781 {
782 if (nsfds->target_fd > 0) {
783 _stp_target_mnt_ns_fd = nsfds->target_fd;
784 _stp_orig_mnt_ns_fd = nsfds->orig_fd;
785 }
786 }
787
788
789 #endif /* _TRANSPORT_C_ */