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

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/bpf/progs/task_ls_recursion.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) 2021 Facebook */
  3 
  4 #include "vmlinux.h"
  5 #include <bpf/bpf_helpers.h>
  6 #include <bpf/bpf_tracing.h>
  7 
  8 #ifndef EBUSY
  9 #define EBUSY 16
 10 #endif
 11 
 12 char _license[] SEC("license") = "GPL";
 13 int nr_del_errs = 0;
 14 int test_pid = 0;
 15 
 16 struct {
 17         __uint(type, BPF_MAP_TYPE_TASK_STORAGE);
 18         __uint(map_flags, BPF_F_NO_PREALLOC);
 19         __type(key, int);
 20         __type(value, long);
 21 } map_a SEC(".maps");
 22 
 23 struct {
 24         __uint(type, BPF_MAP_TYPE_TASK_STORAGE);
 25         __uint(map_flags, BPF_F_NO_PREALLOC);
 26         __type(key, int);
 27         __type(value, long);
 28 } map_b SEC(".maps");
 29 
 30 SEC("fentry/bpf_local_storage_update")
 31 int BPF_PROG(on_update)
 32 {
 33         struct task_struct *task = bpf_get_current_task_btf();
 34         long *ptr;
 35 
 36         if (!test_pid || task->pid != test_pid)
 37                 return 0;
 38 
 39         ptr = bpf_task_storage_get(&map_a, task, 0,
 40                                    BPF_LOCAL_STORAGE_GET_F_CREATE);
 41         /* ptr will not be NULL when it is called from
 42          * the bpf_task_storage_get(&map_b,...F_CREATE) in
 43          * the BPF_PROG(on_enter) below.  It is because
 44          * the value can be found in map_a and the kernel
 45          * does not need to acquire any spin_lock.
 46          */
 47         if (ptr) {
 48                 int err;
 49 
 50                 *ptr += 1;
 51                 err = bpf_task_storage_delete(&map_a, task);
 52                 if (err == -EBUSY)
 53                         nr_del_errs++;
 54         }
 55 
 56         /* This will still fail because map_b is empty and
 57          * this BPF_PROG(on_update) has failed to acquire
 58          * the percpu busy lock => meaning potential
 59          * deadlock is detected and it will fail to create
 60          * new storage.
 61          */
 62         ptr = bpf_task_storage_get(&map_b, task, 0,
 63                                    BPF_LOCAL_STORAGE_GET_F_CREATE);
 64         if (ptr)
 65                 *ptr += 1;
 66 
 67         return 0;
 68 }
 69 
 70 SEC("tp_btf/sys_enter")
 71 int BPF_PROG(on_enter, struct pt_regs *regs, long id)
 72 {
 73         struct task_struct *task;
 74         long *ptr;
 75 
 76         task = bpf_get_current_task_btf();
 77         if (!test_pid || task->pid != test_pid)
 78                 return 0;
 79 
 80         ptr = bpf_task_storage_get(&map_a, task, 0,
 81                                    BPF_LOCAL_STORAGE_GET_F_CREATE);
 82         if (ptr && !*ptr)
 83                 *ptr = 200;
 84 
 85         ptr = bpf_task_storage_get(&map_b, task, 0,
 86                                    BPF_LOCAL_STORAGE_GET_F_CREATE);
 87         if (ptr && !*ptr)
 88                 *ptr = 100;
 89         return 0;
 90 }
 91 

~ [ 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