~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/bpf/progs/bpf_iter_task_stack.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 /* Copyright (c) 2020 Facebook */
  3 #include "bpf_iter.h"
  4 #include <bpf/bpf_helpers.h>
  5 
  6 char _license[] SEC("license") = "GPL";
  7 
  8 #define MAX_STACK_TRACE_DEPTH   64
  9 unsigned long entries[MAX_STACK_TRACE_DEPTH] = {};
 10 #define SIZE_OF_ULONG (sizeof(unsigned long))
 11 
 12 SEC("iter/task")
 13 int dump_task_stack(struct bpf_iter__task *ctx)
 14 {
 15         struct seq_file *seq = ctx->meta->seq;
 16         struct task_struct *task = ctx->task;
 17         long i, retlen;
 18 
 19         if (task == (void *)0)
 20                 return 0;
 21 
 22         retlen = bpf_get_task_stack(task, entries,
 23                                     MAX_STACK_TRACE_DEPTH * SIZE_OF_ULONG, 0);
 24         if (retlen < 0)
 25                 return 0;
 26 
 27         BPF_SEQ_PRINTF(seq, "pid: %8u num_entries: %8u\n", task->pid,
 28                        retlen / SIZE_OF_ULONG);
 29         for (i = 0; i < MAX_STACK_TRACE_DEPTH; i++) {
 30                 if (retlen > i * SIZE_OF_ULONG)
 31                         BPF_SEQ_PRINTF(seq, "[<0>] %pB\n", (void *)entries[i]);
 32         }
 33         BPF_SEQ_PRINTF(seq, "\n");
 34 
 35         return 0;
 36 }
 37 
 38 int num_user_stacks = 0;
 39 
 40 SEC("iter/task")
 41 int get_task_user_stacks(struct bpf_iter__task *ctx)
 42 {
 43         struct seq_file *seq = ctx->meta->seq;
 44         struct task_struct *task = ctx->task;
 45         uint64_t buf_sz = 0;
 46         int64_t res;
 47 
 48         if (task == (void *)0)
 49                 return 0;
 50 
 51         res = bpf_get_task_stack(task, entries,
 52                         MAX_STACK_TRACE_DEPTH * SIZE_OF_ULONG, BPF_F_USER_STACK);
 53         if (res <= 0)
 54                 return 0;
 55 
 56         /* Only one task, the current one, should succeed */
 57         ++num_user_stacks;
 58 
 59         buf_sz += res;
 60 
 61         /* If the verifier doesn't refine bpf_get_task_stack res, and instead
 62          * assumes res is entirely unknown, this program will fail to load as
 63          * the verifier will believe that max buf_sz value allows reading
 64          * past the end of entries in bpf_seq_write call
 65          */
 66         bpf_seq_write(seq, &entries, buf_sz);
 67         return 0;
 68 }
 69 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php