Back to home page

LXR

 
 

    


0001 /* Copyright (c) 2016 Facebook
0002  *
0003  * This program is free software; you can redistribute it and/or
0004  * modify it under the terms of version 2 of the GNU General Public
0005  * License as published by the Free Software Foundation.
0006  */
0007 #include <linux/bpf.h>
0008 #include <linux/jhash.h>
0009 #include <linux/filter.h>
0010 #include <linux/stacktrace.h>
0011 #include <linux/perf_event.h>
0012 #include "percpu_freelist.h"
0013 
0014 struct stack_map_bucket {
0015     struct pcpu_freelist_node fnode;
0016     u32 hash;
0017     u32 nr;
0018     u64 ip[];
0019 };
0020 
0021 struct bpf_stack_map {
0022     struct bpf_map map;
0023     void *elems;
0024     struct pcpu_freelist freelist;
0025     u32 n_buckets;
0026     struct stack_map_bucket *buckets[];
0027 };
0028 
0029 static int prealloc_elems_and_freelist(struct bpf_stack_map *smap)
0030 {
0031     u32 elem_size = sizeof(struct stack_map_bucket) + smap->map.value_size;
0032     int err;
0033 
0034     smap->elems = bpf_map_area_alloc(elem_size * smap->map.max_entries);
0035     if (!smap->elems)
0036         return -ENOMEM;
0037 
0038     err = pcpu_freelist_init(&smap->freelist);
0039     if (err)
0040         goto free_elems;
0041 
0042     pcpu_freelist_populate(&smap->freelist, smap->elems, elem_size,
0043                    smap->map.max_entries);
0044     return 0;
0045 
0046 free_elems:
0047     bpf_map_area_free(smap->elems);
0048     return err;
0049 }
0050 
0051 /* Called from syscall */
0052 static struct bpf_map *stack_map_alloc(union bpf_attr *attr)
0053 {
0054     u32 value_size = attr->value_size;
0055     struct bpf_stack_map *smap;
0056     u64 cost, n_buckets;
0057     int err;
0058 
0059     if (!capable(CAP_SYS_ADMIN))
0060         return ERR_PTR(-EPERM);
0061 
0062     if (attr->map_flags)
0063         return ERR_PTR(-EINVAL);
0064 
0065     /* check sanity of attributes */
0066     if (attr->max_entries == 0 || attr->key_size != 4 ||
0067         value_size < 8 || value_size % 8 ||
0068         value_size / 8 > sysctl_perf_event_max_stack)
0069         return ERR_PTR(-EINVAL);
0070 
0071     /* hash table size must be power of 2 */
0072     n_buckets = roundup_pow_of_two(attr->max_entries);
0073 
0074     cost = n_buckets * sizeof(struct stack_map_bucket *) + sizeof(*smap);
0075     if (cost >= U32_MAX - PAGE_SIZE)
0076         return ERR_PTR(-E2BIG);
0077 
0078     smap = bpf_map_area_alloc(cost);
0079     if (!smap)
0080         return ERR_PTR(-ENOMEM);
0081 
0082     err = -E2BIG;
0083     cost += n_buckets * (value_size + sizeof(struct stack_map_bucket));
0084     if (cost >= U32_MAX - PAGE_SIZE)
0085         goto free_smap;
0086 
0087     smap->map.map_type = attr->map_type;
0088     smap->map.key_size = attr->key_size;
0089     smap->map.value_size = value_size;
0090     smap->map.max_entries = attr->max_entries;
0091     smap->n_buckets = n_buckets;
0092     smap->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
0093 
0094     err = bpf_map_precharge_memlock(smap->map.pages);
0095     if (err)
0096         goto free_smap;
0097 
0098     err = get_callchain_buffers(sysctl_perf_event_max_stack);
0099     if (err)
0100         goto free_smap;
0101 
0102     err = prealloc_elems_and_freelist(smap);
0103     if (err)
0104         goto put_buffers;
0105 
0106     return &smap->map;
0107 
0108 put_buffers:
0109     put_callchain_buffers();
0110 free_smap:
0111     bpf_map_area_free(smap);
0112     return ERR_PTR(err);
0113 }
0114 
0115 BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, struct bpf_map *, map,
0116        u64, flags)
0117 {
0118     struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
0119     struct perf_callchain_entry *trace;
0120     struct stack_map_bucket *bucket, *new_bucket, *old_bucket;
0121     u32 max_depth = map->value_size / 8;
0122     /* stack_map_alloc() checks that max_depth <= sysctl_perf_event_max_stack */
0123     u32 init_nr = sysctl_perf_event_max_stack - max_depth;
0124     u32 skip = flags & BPF_F_SKIP_FIELD_MASK;
0125     u32 hash, id, trace_nr, trace_len;
0126     bool user = flags & BPF_F_USER_STACK;
0127     bool kernel = !user;
0128     u64 *ips;
0129 
0130     if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
0131                    BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID)))
0132         return -EINVAL;
0133 
0134     trace = get_perf_callchain(regs, init_nr, kernel, user,
0135                    sysctl_perf_event_max_stack, false, false);
0136 
0137     if (unlikely(!trace))
0138         /* couldn't fetch the stack trace */
0139         return -EFAULT;
0140 
0141     /* get_perf_callchain() guarantees that trace->nr >= init_nr
0142      * and trace-nr <= sysctl_perf_event_max_stack, so trace_nr <= max_depth
0143      */
0144     trace_nr = trace->nr - init_nr;
0145 
0146     if (trace_nr <= skip)
0147         /* skipping more than usable stack trace */
0148         return -EFAULT;
0149 
0150     trace_nr -= skip;
0151     trace_len = trace_nr * sizeof(u64);
0152     ips = trace->ip + skip + init_nr;
0153     hash = jhash2((u32 *)ips, trace_len / sizeof(u32), 0);
0154     id = hash & (smap->n_buckets - 1);
0155     bucket = READ_ONCE(smap->buckets[id]);
0156 
0157     if (bucket && bucket->hash == hash) {
0158         if (flags & BPF_F_FAST_STACK_CMP)
0159             return id;
0160         if (bucket->nr == trace_nr &&
0161             memcmp(bucket->ip, ips, trace_len) == 0)
0162             return id;
0163     }
0164 
0165     /* this call stack is not in the map, try to add it */
0166     if (bucket && !(flags & BPF_F_REUSE_STACKID))
0167         return -EEXIST;
0168 
0169     new_bucket = (struct stack_map_bucket *)
0170         pcpu_freelist_pop(&smap->freelist);
0171     if (unlikely(!new_bucket))
0172         return -ENOMEM;
0173 
0174     memcpy(new_bucket->ip, ips, trace_len);
0175     new_bucket->hash = hash;
0176     new_bucket->nr = trace_nr;
0177 
0178     old_bucket = xchg(&smap->buckets[id], new_bucket);
0179     if (old_bucket)
0180         pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
0181     return id;
0182 }
0183 
0184 const struct bpf_func_proto bpf_get_stackid_proto = {
0185     .func       = bpf_get_stackid,
0186     .gpl_only   = true,
0187     .ret_type   = RET_INTEGER,
0188     .arg1_type  = ARG_PTR_TO_CTX,
0189     .arg2_type  = ARG_CONST_MAP_PTR,
0190     .arg3_type  = ARG_ANYTHING,
0191 };
0192 
0193 /* Called from eBPF program */
0194 static void *stack_map_lookup_elem(struct bpf_map *map, void *key)
0195 {
0196     return NULL;
0197 }
0198 
0199 /* Called from syscall */
0200 int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
0201 {
0202     struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
0203     struct stack_map_bucket *bucket, *old_bucket;
0204     u32 id = *(u32 *)key, trace_len;
0205 
0206     if (unlikely(id >= smap->n_buckets))
0207         return -ENOENT;
0208 
0209     bucket = xchg(&smap->buckets[id], NULL);
0210     if (!bucket)
0211         return -ENOENT;
0212 
0213     trace_len = bucket->nr * sizeof(u64);
0214     memcpy(value, bucket->ip, trace_len);
0215     memset(value + trace_len, 0, map->value_size - trace_len);
0216 
0217     old_bucket = xchg(&smap->buckets[id], bucket);
0218     if (old_bucket)
0219         pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
0220     return 0;
0221 }
0222 
0223 static int stack_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
0224 {
0225     return -EINVAL;
0226 }
0227 
0228 static int stack_map_update_elem(struct bpf_map *map, void *key, void *value,
0229                  u64 map_flags)
0230 {
0231     return -EINVAL;
0232 }
0233 
0234 /* Called from syscall or from eBPF program */
0235 static int stack_map_delete_elem(struct bpf_map *map, void *key)
0236 {
0237     struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
0238     struct stack_map_bucket *old_bucket;
0239     u32 id = *(u32 *)key;
0240 
0241     if (unlikely(id >= smap->n_buckets))
0242         return -E2BIG;
0243 
0244     old_bucket = xchg(&smap->buckets[id], NULL);
0245     if (old_bucket) {
0246         pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
0247         return 0;
0248     } else {
0249         return -ENOENT;
0250     }
0251 }
0252 
0253 /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
0254 static void stack_map_free(struct bpf_map *map)
0255 {
0256     struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
0257 
0258     /* wait for bpf programs to complete before freeing stack map */
0259     synchronize_rcu();
0260 
0261     bpf_map_area_free(smap->elems);
0262     pcpu_freelist_destroy(&smap->freelist);
0263     bpf_map_area_free(smap);
0264     put_callchain_buffers();
0265 }
0266 
0267 static const struct bpf_map_ops stack_map_ops = {
0268     .map_alloc = stack_map_alloc,
0269     .map_free = stack_map_free,
0270     .map_get_next_key = stack_map_get_next_key,
0271     .map_lookup_elem = stack_map_lookup_elem,
0272     .map_update_elem = stack_map_update_elem,
0273     .map_delete_elem = stack_map_delete_elem,
0274 };
0275 
0276 static struct bpf_map_type_list stack_map_type __read_mostly = {
0277     .ops = &stack_map_ops,
0278     .type = BPF_MAP_TYPE_STACK_TRACE,
0279 };
0280 
0281 static int __init register_stack_map(void)
0282 {
0283     bpf_register_map_type(&stack_map_type);
0284     return 0;
0285 }
0286 late_initcall(register_stack_map);