|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* Copyright (c) 2024 Samsung */ |
| 3 | +#include "vmlinux.h" |
| 4 | +#include <bpf/bpf_helpers.h> |
| 5 | +#include <bpf/bpf_tracing.h> |
| 6 | +#include <bpf/bpf_core_read.h> |
| 7 | +#include "blkalgn.h" |
| 8 | +#include "bits.bpf.h" |
| 9 | +#include "core_fixes.bpf.h" |
| 10 | + |
| 11 | +const volatile bool filter_dev = false; |
| 12 | +const volatile __u32 targ_dev = 0; |
| 13 | +const volatile bool filter_ops = false; |
| 14 | +const volatile __u32 targ_ops = 0; |
| 15 | + |
| 16 | +extern __u32 LINUX_KERNEL_VERSION __kconfig; |
| 17 | + |
| 18 | +char LICENSE[] SEC("license") = "Dual BSD/GPL"; |
| 19 | + |
| 20 | +struct { |
| 21 | + __uint(type, BPF_MAP_TYPE_RINGBUF); |
| 22 | + __uint(max_entries, 2097152); |
| 23 | +} rb SEC(".maps"); |
| 24 | + |
| 25 | +struct { |
| 26 | + __uint(type, BPF_MAP_TYPE_HASH); |
| 27 | + __uint(max_entries, 10240); |
| 28 | + __type(key, struct hkey); |
| 29 | + __type(value, struct hval); |
| 30 | +} halgn_map SEC(".maps"); |
| 31 | + |
| 32 | +struct { |
| 33 | + __uint(type, BPF_MAP_TYPE_HASH); |
| 34 | + __uint(max_entries, 10240); |
| 35 | + __type(key, struct hkey); |
| 36 | + __type(value, struct hval); |
| 37 | +} hgran_map SEC(".maps"); |
| 38 | + |
| 39 | +static int __always_inline trace_rq_issue(struct request *rq) |
| 40 | +{ |
| 41 | + struct event *e; |
| 42 | + u32 dev; |
| 43 | + |
| 44 | + struct gendisk *disk = get_disk(rq); |
| 45 | + |
| 46 | + dev = disk ? MKDEV(BPF_CORE_READ(disk, major), |
| 47 | + BPF_CORE_READ(disk, first_minor)) : |
| 48 | + 0; |
| 49 | + |
| 50 | + if (filter_dev && targ_dev != dev) |
| 51 | + return 0; |
| 52 | + |
| 53 | + if (filter_ops && targ_ops != (rq->cmd_flags & 0xff)) |
| 54 | + return 0; |
| 55 | + |
| 56 | + e = bpf_ringbuf_reserve(&rb, sizeof(*e), 0); |
| 57 | + if (!e) |
| 58 | + return 0; |
| 59 | + |
| 60 | + bpf_get_current_comm(&e->comm, sizeof(e->comm)); |
| 61 | + bpf_probe_read_kernel(&e->disk, sizeof(e->disk), |
| 62 | + rq->q->disk->disk_name); |
| 63 | + |
| 64 | + e->flags = rq->cmd_flags; |
| 65 | + e->lbs = rq->q->limits.logical_block_size; |
| 66 | + e->len = rq->__data_len; |
| 67 | + e->sector = rq->__sector; |
| 68 | + |
| 69 | + bpf_ringbuf_submit(e, 0); |
| 70 | + |
| 71 | + return 0; |
| 72 | +} |
| 73 | + |
| 74 | +SEC("tp_btf/block_rq_issue") |
| 75 | +int BPF_PROG(block_rq_issue) |
| 76 | +{ |
| 77 | + /* |
| 78 | + * Commit a54895fa (v5.11-rc1) changed tracepoint argument list |
| 79 | + * from TP_PROTO(struct request_queue *q, struct request *rq) |
| 80 | + * to TP_PROTO(struct request *rq) |
| 81 | + */ |
| 82 | + if (LINUX_KERNEL_VERSION >= KERNEL_VERSION(5, 11, 0)) |
| 83 | + return trace_rq_issue((void *)ctx[0]); |
| 84 | + else |
| 85 | + return trace_rq_issue((void *)ctx[1]); |
| 86 | +} |
0 commit comments