Debian Codegraph @

runtime/transport/procfs.c

raw
1 /* -*- linux-c -*-
2 *
3 * /proc transport and control
4 * Copyright (C) 2005-2018 Red Hat Inc.
5 *
6 * This file is part of systemtap, and is free software. You can
7 * redistribute it and/or modify it under the terms of the GNU General
8 * Public License (GPL); either version 2, or (at your option) any
9 * later version.
10 */
11
12 #include "relay_compat.h"
13 #include "proc_fs_compatibility.h"
14
15 #if defined(STAPCONF_PATH_LOOKUP) && !defined(STAPCONF_KERN_PATH_PARENT)
16 #define kern_path_parent(name, nameidata) \
17 path_lookup(name, LOOKUP_PARENT, nameidata)
18 #endif
19
20 /* _stp_procfs_module_dir is the '/proc/systemtap/{module_name}' directory. */
21 static struct proc_dir_entry *_stp_procfs_module_dir = NULL;
22 static struct path _stp_procfs_module_dir_path;
23
24 static bool _stp_proc_dir_exists(void)
25 {
26 int found = 0;
27 #if defined(STAPCONF_PATH_LOOKUP) || defined(STAPCONF_KERN_PATH_PARENT)
28 struct nameidata nd;
29 #else /* STAPCONF_VFS_PATH_LOOKUP or STAPCONF_KERN_PATH */
30 struct path path;
31 #if defined(STAPCONF_VFS_PATH_LOOKUP)
32 struct vfsmount *mnt;
33 #endif
34 int rc;
35 #endif /* STAPCONF_VFS_PATH_LOOKUP or STAPCONF_KERN_PATH */
36
37 #if defined(STAPCONF_PATH_LOOKUP) || defined(STAPCONF_KERN_PATH_PARENT)
38 /* Why "/proc/systemtap/foo"? kern_path_parent() is basically
39 * the same thing as calling the old path_lookup() with flags
40 * set to LOOKUP_PARENT, which means to look up the parent of
41 * the path, which in this case is "/proc/systemtap". */
42 if (! kern_path_parent("/proc/systemtap/foo", &nd)) {
43 found = 1;
44 #ifdef STAPCONF_NAMEIDATA_CLEANUP
45 path_put(&nd.path);
46 #else /* !STAPCONF_NAMEIDATA_CLEANUP */
47 path_release(&nd);
48 #endif /* !STAPCONF_NAMEIDATA_CLEANUP */
49 }
50
51 #elif defined(STAPCONF_KERN_PATH)
52 /* Prefer kern_path() over vfs_path_lookup(), since on some
53 * kernels the declaration for vfs_path_lookup() was moved to
54 * a private header. */
55
56 /* See if '/proc/systemtap' exists. */
57 rc = kern_path("/proc/systemtap", 0, &path);
58 if (rc == 0) {
59 found = 1;
60 path_put (&path);
61 }
62
63 #else /* STAPCONF_VFS_PATH_LOOKUP */
64 /* See if '/proc/systemtap' exists. */
65 mnt = init_pid_ns.proc_mnt;
66 rc = vfs_path_lookup(mnt->mnt_root, mnt, "systemtap", 0, &path);
67 if (rc == 0) {
68 found = 1;
69 path_put (&path);
70 }
71 #endif /* STAPCONF_VFS_PATH_LOOKUP */
72
73 return found;
74 }
75
76 /*
77 * Safely creates '/proc/systemtap' (if necessary) and
78 * '/proc/systemtap/{module_name}'.
79 *
80 * NB: this function is suitable to call from early in the the
81 * module-init function, and doesn't rely on any other facilities
82 * in our runtime. PR19833. See also PR15408.
83 */
84 static int _stp_mkdir_proc_module(void)
85 {
86 static char proc_root_name[STP_MODULE_NAME_LEN + sizeof("systemtap/")];
87 int rc;
88
89 if (_stp_procfs_module_dir != NULL)
90 return 0;
91
92 /* If we couldn't find "/proc/systemtap", create it. */
93 if (!_stp_proc_dir_exists()) {
94 /*
95 * We need some sleepable way to synchronize with other stap
96 * modules which are also being loaded for the first time on
97 * this system. The `/proc/systemtap` directory is never removed
98 * after it's made, so this race only happens briefly the first
99 * time stap is used on the current boot. On 3.19+ kernels, the
100 * race results in only a WARN and proc_mkdir() failing; nothing
101 * more than that. However, on kernels <3.19, proc_mkdir()
102 * doesn't error out when a duplicate directory is made, and
103 * instead there are leaks in addition to the WARN (see kernel
104 * commit b208d54b7539 for details). We'd like to fix the leak
105 * and ideally not scare sysadmins with WARNs, so we abuse
106 * `module_mutex` in the kernel for mutual exclusion between all
107 * stap modules. Since `module_mutex` isn't ours to abuse
108 * freely, we elide it by checking if `/proc/systemtap` exists
109 * first, and if it doesn't, then we check again after taking
110 * the lock. This means we'll only use `module_mutex` and
111 * redundantly check for the existence of `/proc/systemtap` just
112 * once for each of the first stap modules loaded on the system,
113 * and only for those stap modules which encounter the race.
114 * After the race window, we're back to just the single check
115 * for `/proc/systemtap` and nothing more.
116 *
117 * This doesn't work on 5.12+ kernels though, as `module_mutex`
118 * is no longer exported, but that isn't a big deal. Since
119 * there's no risk of a leak on 5.12+ kernels, the worst that
120 * can happen is a cosmetic WARN.
121 *
122 * We never need to check proc_mkdir() for an error because, if
123 * it fails on 5.12+ without `module_mutex` due to the directory
124 * already existing, then it's guaranteed that the directory
125 * will be immediately available to use since procfs serializes
126 * the existence check and the registration under the same hold
127 * of a global lock. And if there's a proc_mkdir() error even
128 * with `module_mutex`, then the other proc_mkdir() attempt
129 * below which *is* checked for errors will fail anyway and
130 * produce a fatal error message.
131 */
132 #ifdef STAPCONF_MODULE_MUTEX
133 mutex_lock(&module_mutex);
134 if (!_stp_proc_dir_exists())
135 proc_mkdir("systemtap", NULL);
136 mutex_unlock(&module_mutex);
137 #else
138 proc_mkdir("systemtap", NULL);
139 #endif
140 }
141
142 /* Create the "systemtap/{module_name} directory in procfs. */
143 strlcpy(proc_root_name, "/proc/systemtap/", sizeof(proc_root_name));
144 strlcat(proc_root_name, THIS_MODULE->name, sizeof(proc_root_name));
145 _stp_procfs_module_dir = proc_mkdir(&proc_root_name[6], NULL); // skip the /proc/
146 #ifdef STAPCONF_PROCFS_OWNER
147 if (_stp_procfs_module_dir != NULL)
148 _stp_procfs_module_dir->owner = THIS_MODULE;
149 #endif
150 if (_stp_procfs_module_dir == NULL)
151 errk("Unable to create '/proc/systemap/%s':"
152 " proc_mkdir failed.\n", THIS_MODULE->name);
153 else {
154 rc = kern_path(proc_root_name, 0, &_stp_procfs_module_dir_path);
155 if (rc != 0) {
156 errk("Unable to resolve /proc/systemap/%s':"
157 " to path.\n", THIS_MODULE->name);
158 proc_remove(_stp_procfs_module_dir);
159 _stp_procfs_module_dir = NULL;
160 return rc;
161 }
162 }
163
164 done:
165 return (_stp_procfs_module_dir) ? 0 : -EINVAL;
166 }
167
168
169 /*
170 * Removes '/proc/systemtap/{module_name}'. Notice we're leaving
171 * '/proc/systemtap' behind. There is no way on newer kernels to know
172 * if a procfs directory is empty.
173 *
174 * NB: this is suitable to call late in the module cleanup function,
175 * and does not rely on any other facilities in the runtime. PR19833.
176 * See also PR15408.
177 */
178 static void _stp_rmdir_proc_module(void)
179 {
180 if (_stp_procfs_module_dir) {
181 path_put(& _stp_procfs_module_dir_path);
182 proc_remove(_stp_procfs_module_dir);
183 _stp_procfs_module_dir = NULL;
184 }
185 }
186
187
188 inline static int _stp_procfs_ctl_write_fs(int type, void *data, unsigned len)
189 {
190 struct _stp_buffer *bptr;
191 unsigned long flags;
192
193 #define WRITE_AGG
194 #ifdef WRITE_AGG
195 stp_spin_lock_irqsave(&_stp_ctl_ready_lock, flags);
196 if (!list_empty(&_stp_ctl_ready_q)) {
197 bptr = (struct _stp_buffer *)_stp_ctl_ready_q.prev;
198 if ((bptr->len + len) <= STP_CTL_BUFFER_SIZE
199 && type == STP_REALTIME_DATA
200 && bptr->type == STP_REALTIME_DATA) {
201 memcpy(bptr->buf + bptr->len, data, len);
202 bptr->len += len;
203 stp_spin_unlock_irqrestore(&_stp_ctl_ready_lock, flags);
204 return len;
205 }
206 }
207 stp_spin_unlock_irqrestore(&_stp_ctl_ready_lock, flags);
208 #endif
209 return 0;
210 }
211
212 static int _stp_proc_ctl_read_bufsize(char *page, char **start, off_t off, int count, int *eof, void *data)
213 {
214 int len = sprintf(page, "%d,%d\n", _stp_nsubbufs, _stp_subbuf_size);
215 if (len <= off + count)
216 *eof = 1;
217 *start = page + off;
218 len -= off;
219 if (len > count)
220 len = count;
221 if (len < 0)
222 len = 0;
223 return len;
224 }
225
226
227 static struct file_operations _stp_ctl_fops_cmd;
228 #ifdef STAPCONF_PROC_OPS /* control.c */
229 static struct proc_ops _stp_ctl_proc_ops_cmd;
230 #endif
231
232
233 static int _stp_procfs_register_ctl_channel_fs(void)
234 {
235 struct proc_dir_entry *bs = NULL;
236 struct proc_dir_entry *de;
237
238 if (_stp_mkdir_proc_module())
239 goto err0;
240
241 /* create /proc/systemtap/module_name/.cmd */
242 #ifdef STAPCONF_PROC_OPS
243 de = proc_create(".cmd", 0600, _stp_procfs_module_dir, &_stp_ctl_proc_ops_cmd);
244 #else
245 de = proc_create(".cmd", 0600, _stp_procfs_module_dir, &_stp_ctl_fops_cmd);
246 #endif
247 if (de == NULL)
248 goto err1;
249 proc_set_user(de, KUIDT_INIT(_stp_uid), KGIDT_INIT(_stp_gid));
250
251 return 0;
252
253 err1:
254 _stp_rmdir_proc_module();
255 err0:
256 return -1;
257 }
258
259 static void _stp_procfs_unregister_ctl_channel_fs(void)
260 {
261 remove_proc_entry(".cmd", _stp_procfs_module_dir);
262 _stp_rmdir_proc_module();
263 }
264
265
266
267 #ifdef STAPCONF_PROC_OPS
268 struct proc_ops relay_procfs_operations;
269 #else
270 struct file_operations relay_procfs_operations;
271 #endif
272
273
274 // We need to map procfs concepts of proc_dir_entry* and relayfs/vfs of path/dentry*.
275 struct procfs_relay_file
276 {
277 struct path p; // contains the dentry*
278 struct proc_dir_entry *pde; // entry valid if this pointer non-NULL
279 };
280 struct procfs_relay_file *p_r_files;
281
282
283 static int _stp_procfs_transport_fs_init(const char *module_name)
284 {
285 p_r_files = _stp_vzalloc(num_possible_cpus()
286 * sizeof(struct procfs_relay_file));
287 if (unlikely(p_r_files == NULL))
288 return -ENOMEM;
289
290 #ifdef STAPCONF_PROC_OPS
291 relay_procfs_operations.proc_open = __stp_relay_file_open;
292 relay_procfs_operations.proc_poll = __stp_relay_file_poll;
293 relay_procfs_operations.proc_mmap = relay_file_operations.mmap;
294 relay_procfs_operations.proc_read = __stp_relay_file_read;
295 relay_procfs_operations.proc_lseek = relay_file_operations.llseek;
296 relay_procfs_operations.proc_release = relay_file_operations.release;
297 #else
298 relay_procfs_operations = relay_file_operations;
299 relay_procfs_operations.open = __stp_relay_file_open;
300 relay_procfs_operations.owner = THIS_MODULE;
301 relay_procfs_operations.poll = __stp_relay_file_poll;
302 relay_procfs_operations.read = __stp_relay_file_read;
303 #endif
304
305 if (_stp_mkdir_proc_module()) { // get the _stp_procfs_module_dir* created
306 _stp_vfree (p_r_files);
307 p_r_files = NULL;
308 return -1;
309 }
310
311 dbug_trans(1, "transport_fs_init dentry=%08lx pde=%08lx ",
312 (unsigned long) _stp_procfs_module_dir_path.dentry,
313 (unsigned long) _stp_procfs_module_dir);
314
315 if (_stp_transport_data_fs_init() != 0) {
316 _stp_rmdir_proc_module();
317 _stp_vfree (p_r_files);
318 p_r_files = NULL;
319 return -1;
320 }
321
322 return 0;
323 }
324
325
326 static void _stp_procfs_transport_fs_close(void)
327 {
328 _stp_transport_data_fs_close();
329
330 if (likely(p_r_files)) {
331 _stp_vfree (p_r_files);
332 p_r_files = NULL;
333 }
334 }
335
336
337 static struct dentry *_stp_procfs_get_module_dir(void)
338 {
339 return _stp_procfs_module_dir_path.dentry;
340 }
341
342
343 static int __stp_procfs_relay_remove_buf_file_callback(struct dentry *dentry)
344 {
345 unsigned i;
346 struct proc_dir_entry *pde = NULL;
347
348 // find the corresponding pde*
349
350 /* NB We cannot use the for_each_online_cpu() here since online
351 * CPUs may get changed on-the-fly through the CPU hotplug feature
352 * of the kernel.
353 */
354 for_each_possible_cpu(i)
355 {
356 if (p_r_files[i].pde != NULL &&
357 p_r_files[i].p.dentry == dentry)
358 break;
359 }
360
361 if (i != num_possible_cpus())
362 {
363 pde = p_r_files[i].pde;
364 proc_remove (pde);
365 path_put (& p_r_files[i].p);
366 p_r_files[i].pde = NULL;
367 }
368
369 dbug_trans(1, "remove-buf dentry=%08lx pde=%08lx i=%u",
370 (unsigned long) dentry, (unsigned long) pde, i);
371 return 0;
372 }
373
374
375 static struct dentry *
376 __stp_procfs_relay_create_buf_file_callback(const char *filename,
377 struct dentry *parent,
378 #ifdef STAPCONF_RELAY_UMODE_T
379 umode_t mode,
380 #else
381 int mode,
382 #endif
383 struct rchan_buf *buf,
384 int *is_global)
385 {
386 int rc = 0;
387 struct dentry* de = NULL;
388 char fullpath[sizeof("/proc/systemtap") + STP_MODULE_NAME_LEN + sizeof("/traceNNNNN") + 42];
389 struct proc_dir_entry *pde;
390 unsigned i = 0;
391 struct inode* in;
392
393 if (is_global)
394 *is_global = 0;
395
396 if (parent != _stp_procfs_module_dir_path.dentry)
397 goto out;
398
399 pde = proc_create (filename, 0400,
400 _stp_procfs_module_dir,
401 & relay_procfs_operations);
402 if (pde == NULL)
403 goto out;
404
405 proc_set_user(pde, KUIDT_INIT(_stp_uid), KGIDT_INIT(_stp_gid));
406
407 rc = snprintf(fullpath, sizeof(fullpath), "/proc/systemtap/%s/%s",
408 THIS_MODULE->name, filename);
409
410 // find spot to plop this
411
412 /* NB We cannot use the for_each_online_cpu() here since online
413 * CPUs may get changed on-the-fly through the CPU hotplug feature
414 * of the kernel.
415 */
416 for_each_possible_cpu(i)
417 {
418 if (p_r_files[i].pde == NULL)
419 break;
420 }
421 if (i == num_possible_cpus())
422 goto out1;
423
424 rc = kern_path (fullpath, 0, &p_r_files[i].p);
425 if (rc)
426 goto out1;
427 p_r_files[i].pde = pde;
428 de = p_r_files[i].p.dentry;
429
430 // fill in the relayfs i_private
431 in = de->d_inode;
432 in->i_private = buf;
433
434 // success!
435 goto out;
436
437 out1:
438 proc_remove (pde);
439
440 out:
441 dbug_trans(1, "create-buf name=%s parent=%08lx -> i=%u rc=%d de=%08lx",
442 filename, (unsigned long) parent,
443 i, rc, (unsigned long) de);
444 return de;
445 }