Skip to content

Commit a137401

Browse files
committed
Merge branch 'bpf-bpftool-mount-tracefs'
Quentin Monnet says: ==================== This series focus on mounting (or not mounting) tracefs with bpftool. First patch makes bpftool attempt to mount tracefs if tracefs is not found when running "bpftool prog tracelog". Second patch adds an option to bpftool to prevent it from attempting to mount any file system (tracefs or bpffs), in case this behaviour is undesirable for some users. ==================== Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2 parents 0d7410e + 3322130 commit a137401

File tree

7 files changed

+66
-14
lines changed

7 files changed

+66
-14
lines changed

tools/bpf/bpftool/Documentation/bpftool-map.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ OPTIONS
128128
-f, --bpffs
129129
Show file names of pinned maps.
130130

131+
-n, --nomount
132+
Do not automatically attempt to mount any virtual file system
133+
(such as tracefs or BPF virtual file system) when necessary.
134+
131135
EXAMPLES
132136
========
133137
**# bpftool map show**

tools/bpf/bpftool/Documentation/bpftool-prog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ OPTIONS
161161
-m, --mapcompat
162162
Allow loading maps with unknown map definitions.
163163

164+
-n, --nomount
165+
Do not automatically attempt to mount any virtual file system
166+
(such as tracefs or BPF virtual file system) when necessary.
167+
164168
EXAMPLES
165169
========
166170
**# bpftool prog show**

tools/bpf/bpftool/Documentation/bpftool.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ OPTIONS
6060
-m, --mapcompat
6161
Allow loading maps with unknown map definitions.
6262

63+
-n, --nomount
64+
Do not automatically attempt to mount any virtual file system
65+
(such as tracefs or BPF virtual file system) when necessary.
66+
6367

6468
SEE ALSO
6569
========

tools/bpf/bpftool/common.c

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ void set_max_rlimit(void)
7676
setrlimit(RLIMIT_MEMLOCK, &rinf);
7777
}
7878

79-
static int mnt_bpffs(const char *target, char *buff, size_t bufflen)
79+
static int
80+
mnt_fs(const char *target, const char *type, char *buff, size_t bufflen)
8081
{
8182
bool bind_done = false;
8283

@@ -98,15 +99,29 @@ static int mnt_bpffs(const char *target, char *buff, size_t bufflen)
9899
bind_done = true;
99100
}
100101

101-
if (mount("bpf", target, "bpf", 0, "mode=0700")) {
102-
snprintf(buff, bufflen, "mount -t bpf bpf %s failed: %s",
103-
target, strerror(errno));
102+
if (mount(type, target, type, 0, "mode=0700")) {
103+
snprintf(buff, bufflen, "mount -t %s %s %s failed: %s",
104+
type, type, target, strerror(errno));
104105
return -1;
105106
}
106107

107108
return 0;
108109
}
109110

111+
int mount_tracefs(const char *target)
112+
{
113+
char err_str[ERR_MAX_LEN];
114+
int err;
115+
116+
err = mnt_fs(target, "tracefs", err_str, ERR_MAX_LEN);
117+
if (err) {
118+
err_str[ERR_MAX_LEN - 1] = '\0';
119+
p_err("can't mount tracefs: %s", err_str);
120+
}
121+
122+
return err;
123+
}
124+
110125
int open_obj_pinned(char *path, bool quiet)
111126
{
112127
int fd;
@@ -162,7 +177,13 @@ int mount_bpffs_for_pin(const char *name)
162177
/* nothing to do if already mounted */
163178
goto out_free;
164179

165-
err = mnt_bpffs(dir, err_str, ERR_MAX_LEN);
180+
if (block_mount) {
181+
p_err("no BPF file system found, not mounting it due to --nomount option");
182+
err = -1;
183+
goto out_free;
184+
}
185+
186+
err = mnt_fs(dir, "bpf", err_str, ERR_MAX_LEN);
166187
if (err) {
167188
err_str[ERR_MAX_LEN - 1] = '\0';
168189
p_err("can't mount BPF file system to pin the object (%s): %s",

tools/bpf/bpftool/main.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ json_writer_t *json_wtr;
2424
bool pretty_output;
2525
bool json_output;
2626
bool show_pinned;
27+
bool block_mount;
2728
int bpf_flags;
2829
struct pinned_obj_table prog_table;
2930
struct pinned_obj_table map_table;
@@ -313,6 +314,7 @@ int main(int argc, char **argv)
313314
{ "version", no_argument, NULL, 'V' },
314315
{ "bpffs", no_argument, NULL, 'f' },
315316
{ "mapcompat", no_argument, NULL, 'm' },
317+
{ "nomount", no_argument, NULL, 'n' },
316318
{ 0 }
317319
};
318320
int opt, ret;
@@ -321,13 +323,14 @@ int main(int argc, char **argv)
321323
pretty_output = false;
322324
json_output = false;
323325
show_pinned = false;
326+
block_mount = false;
324327
bin_name = argv[0];
325328

326329
hash_init(prog_table.table);
327330
hash_init(map_table.table);
328331

329332
opterr = 0;
330-
while ((opt = getopt_long(argc, argv, "Vhpjfm",
333+
while ((opt = getopt_long(argc, argv, "Vhpjfmn",
331334
options, NULL)) >= 0) {
332335
switch (opt) {
333336
case 'V':
@@ -354,6 +357,9 @@ int main(int argc, char **argv)
354357
case 'm':
355358
bpf_flags = MAPS_RELAX_COMPAT;
356359
break;
360+
case 'n':
361+
block_mount = true;
362+
break;
357363
default:
358364
p_err("unrecognized option '%s'", argv[optind - 1]);
359365
if (json_output)

tools/bpf/bpftool/main.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
#define HELP_SPEC_PROGRAM \
4545
"PROG := { id PROG_ID | pinned FILE | tag PROG_TAG }"
4646
#define HELP_SPEC_OPTIONS \
47-
"OPTIONS := { {-j|--json} [{-p|--pretty}] | {-f|--bpffs} | {-m|--mapcompat}"
47+
"OPTIONS := { {-j|--json} [{-p|--pretty}] | {-f|--bpffs} |\n" \
48+
"\t {-m|--mapcompat} | {-n|--nomount} }"
4849
#define HELP_SPEC_MAP \
4950
"MAP := { id MAP_ID | pinned FILE }"
5051

@@ -85,6 +86,7 @@ extern const char *bin_name;
8586
extern json_writer_t *json_wtr;
8687
extern bool json_output;
8788
extern bool show_pinned;
89+
extern bool block_mount;
8890
extern int bpf_flags;
8991
extern struct pinned_obj_table prog_table;
9092
extern struct pinned_obj_table map_table;
@@ -98,6 +100,8 @@ void usage(void) __noreturn;
98100

99101
void set_max_rlimit(void);
100102

103+
int mount_tracefs(const char *target);
104+
101105
struct pinned_obj_table {
102106
DECLARE_HASHTABLE(table, 16);
103107
};

tools/bpf/bpftool/tracelog.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ find_tracefs_mnt_single(unsigned long magic, char *mnt, const char *mntpt)
5454
return true;
5555
}
5656

57-
static bool find_tracefs_pipe(char *mnt)
57+
static bool get_tracefs_pipe(char *mnt)
5858
{
5959
static const char * const known_mnts[] = {
6060
"/sys/kernel/debug/tracing",
@@ -88,7 +88,20 @@ static bool find_tracefs_pipe(char *mnt)
8888
fclose(fp);
8989

9090
/* The string from fscanf() might be truncated, check mnt is valid */
91-
if (!found || validate_tracefs_mnt(mnt, TRACEFS_MAGIC))
91+
if (found && validate_tracefs_mnt(mnt, TRACEFS_MAGIC))
92+
goto exit_found;
93+
94+
if (block_mount)
95+
return false;
96+
97+
p_info("could not find tracefs, attempting to mount it now");
98+
/* Most of the time, tracefs is automatically mounted by debugfs at
99+
* /sys/kernel/debug/tracing when we try to access it. If we could not
100+
* find it, it is likely that debugfs is not mounted. Let's give one
101+
* attempt at mounting just tracefs at /sys/kernel/tracing.
102+
*/
103+
strcpy(mnt, known_mnts[1]);
104+
if (mount_tracefs(mnt))
92105
return false;
93106

94107
exit_found:
@@ -115,17 +128,13 @@ int do_tracelog(int argc, char **argv)
115128
.sa_handler = exit_tracelog
116129
};
117130
char trace_pipe[PATH_MAX];
118-
bool found_trace_pipe;
119131
size_t buff_len = 0;
120132

121133
if (json_output)
122134
jsonw_start_array(json_wtr);
123135

124-
found_trace_pipe = find_tracefs_pipe(trace_pipe);
125-
if (!found_trace_pipe) {
126-
p_err("could not find trace pipe, tracefs not mounted?");
136+
if (!get_tracefs_pipe(trace_pipe))
127137
return -1;
128-
}
129138

130139
trace_pipe_fd = fopen(trace_pipe, "r");
131140
if (!trace_pipe_fd) {

0 commit comments

Comments
 (0)