|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */ |
| 3 | + |
| 4 | +#include "vmlinux.h" |
| 5 | +#include <bpf/bpf_helpers.h> |
| 6 | + |
| 7 | +char _license[] SEC("license") = "GPL"; |
| 8 | + |
| 9 | +#define make_task_local_excl_map(name, num) \ |
| 10 | +struct { \ |
| 11 | + __uint(type, BPF_MAP_TYPE_TASK_STORAGE); \ |
| 12 | + __uint(map_flags, BPF_F_NO_PREALLOC); \ |
| 13 | + __type(key, int); \ |
| 14 | + __type(value, __u32); \ |
| 15 | + __uint(map_extra, BPF_LOCAL_STORAGE_FORCE_CACHE); \ |
| 16 | +} name ## num SEC(".maps"); |
| 17 | + |
| 18 | +#define make_task_local_map(name, num) \ |
| 19 | +struct { \ |
| 20 | + __uint(type, BPF_MAP_TYPE_TASK_STORAGE); \ |
| 21 | + __uint(map_flags, BPF_F_NO_PREALLOC); \ |
| 22 | + __type(key, int); \ |
| 23 | + __type(value, __u32); \ |
| 24 | +} name ## num SEC(".maps"); |
| 25 | + |
| 26 | +#define task_storage_get_excl(map, num) \ |
| 27 | +({ \ |
| 28 | + bpf_task_storage_get(&map ## num, task, 0, BPF_LOCAL_STORAGE_GET_F_CREATE); \ |
| 29 | + bpf_probe_read_kernel(&out__cache_smaps[num], \ |
| 30 | + sizeof(void *), \ |
| 31 | + &task->bpf_storage->cache[num]->smap); \ |
| 32 | + out__declared_smaps[num] = &map ## num; \ |
| 33 | +}) |
| 34 | + |
| 35 | +/* must match define in bpf_local_storage.h */ |
| 36 | +#define BPF_LOCAL_STORAGE_CACHE_SIZE 16 |
| 37 | + |
| 38 | +/* Try adding BPF_LOCAL_STORAGE_CACHE_SIZE task_storage maps w/ exclusive |
| 39 | + * cache slot |
| 40 | + */ |
| 41 | +make_task_local_excl_map(task_storage_map, 0); |
| 42 | +make_task_local_excl_map(task_storage_map, 1); |
| 43 | +make_task_local_excl_map(task_storage_map, 2); |
| 44 | +make_task_local_excl_map(task_storage_map, 3); |
| 45 | +make_task_local_excl_map(task_storage_map, 4); |
| 46 | +make_task_local_excl_map(task_storage_map, 5); |
| 47 | +make_task_local_excl_map(task_storage_map, 6); |
| 48 | +make_task_local_excl_map(task_storage_map, 7); |
| 49 | +make_task_local_excl_map(task_storage_map, 8); |
| 50 | +make_task_local_excl_map(task_storage_map, 9); |
| 51 | +make_task_local_excl_map(task_storage_map, 10); |
| 52 | +make_task_local_excl_map(task_storage_map, 11); |
| 53 | +make_task_local_excl_map(task_storage_map, 12); |
| 54 | +make_task_local_excl_map(task_storage_map, 13); |
| 55 | +make_task_local_excl_map(task_storage_map, 14); |
| 56 | +make_task_local_excl_map(task_storage_map, 15); |
| 57 | + |
| 58 | +make_task_local_map(task_storage_map, 16); |
| 59 | + |
| 60 | +extern const void task_cache __ksym; |
| 61 | +__u64 __BPF_LOCAL_STORAGE_CACHE_SIZE = BPF_LOCAL_STORAGE_CACHE_SIZE; |
| 62 | +__u64 out__cache_bitmap = -1; |
| 63 | +void *out__cache_smaps[BPF_LOCAL_STORAGE_CACHE_SIZE] = { (void *)-1 }; |
| 64 | +void *out__declared_smaps[BPF_LOCAL_STORAGE_CACHE_SIZE] = { (void *)-1 }; |
| 65 | + |
| 66 | +SEC("raw_tp/sys_enter") |
| 67 | +int handler(const void *ctx) |
| 68 | +{ |
| 69 | + struct task_struct *task = bpf_get_current_task_btf(); |
| 70 | + __u32 *ptr; |
| 71 | + |
| 72 | + bpf_probe_read_kernel(&out__cache_bitmap, sizeof(out__cache_bitmap), |
| 73 | + &task_cache + |
| 74 | + offsetof(struct bpf_local_storage_cache, idx_exclusive)); |
| 75 | + |
| 76 | + /* Get all BPF_LOCAL_STORAGE_CACHE_SIZE exclusive-cache maps into cache, |
| 77 | + * and one that shouldn't be cached |
| 78 | + */ |
| 79 | + task_storage_get_excl(task_storage_map, 0); |
| 80 | + task_storage_get_excl(task_storage_map, 1); |
| 81 | + task_storage_get_excl(task_storage_map, 2); |
| 82 | + task_storage_get_excl(task_storage_map, 3); |
| 83 | + task_storage_get_excl(task_storage_map, 4); |
| 84 | + task_storage_get_excl(task_storage_map, 5); |
| 85 | + task_storage_get_excl(task_storage_map, 6); |
| 86 | + task_storage_get_excl(task_storage_map, 7); |
| 87 | + task_storage_get_excl(task_storage_map, 8); |
| 88 | + task_storage_get_excl(task_storage_map, 9); |
| 89 | + task_storage_get_excl(task_storage_map, 10); |
| 90 | + task_storage_get_excl(task_storage_map, 11); |
| 91 | + task_storage_get_excl(task_storage_map, 12); |
| 92 | + task_storage_get_excl(task_storage_map, 13); |
| 93 | + task_storage_get_excl(task_storage_map, 14); |
| 94 | + task_storage_get_excl(task_storage_map, 15); |
| 95 | + |
| 96 | + bpf_task_storage_get(&task_storage_map16, task, 0, |
| 97 | + BPF_LOCAL_STORAGE_GET_F_CREATE); |
| 98 | + |
| 99 | + return 0; |
| 100 | +} |
0 commit comments