1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/ptrace.h>
6 #include <bpf/bpf_helpers.h>
7 #include <bpf/bpf_tracing.h>
10 char _license[] SEC("license") = "GPL";
12 /* typically virtio scsi has max SGs of 6 */
13 #define VIRTIO_MAX_SGS 6
15 /* Verifier will fail with SG_MAX = 128. The failure can be
16 * workarounded with a smaller SG_MAX, e.g. 10.
22 /* typically virtio blk has max SEG of 128 */
26 #define SG_CHAIN 0x01UL
30 unsigned long page_link;
35 #define sg_is_chain(sg) ((sg)->page_link & SG_CHAIN)
36 #define sg_is_last(sg) ((sg)->page_link & SG_END)
37 #define sg_chain_ptr(sg) \
38 ((struct scatterlist *) ((sg)->page_link & ~(SG_CHAIN | SG_END)))
40 static inline struct scatterlist *__sg_next(struct scatterlist *sgp)
42 struct scatterlist sg;
44 bpf_probe_read_kernel(&sg, sizeof(sg), sgp);
50 bpf_probe_read_kernel(&sg, sizeof(sg), sgp);
52 sgp = sg_chain_ptr(&sg);
57 static inline struct scatterlist *get_sgp(struct scatterlist **sgs, int i)
59 struct scatterlist *sgp;
61 bpf_probe_read_kernel(&sgp, sizeof(sgp), sgs + i);
68 SEC("kprobe/virtqueue_add_sgs")
69 int BPF_KPROBE(trace_virtqueue_add_sgs, void *unused, struct scatterlist **sgs,
70 unsigned int out_sgs, unsigned int in_sgs)
72 struct scatterlist *sgp = NULL;
73 __u64 length1 = 0, length2 = 0;
74 unsigned int i, n, len;
79 for (i = 0; (i < VIRTIO_MAX_SGS) && (i < out_sgs); i++) {
81 for (n = 0, sgp = get_sgp(sgs, i); sgp && (n < SG_MAX);
82 sgp = __sg_next(sgp)) {
83 bpf_probe_read_kernel(&len, sizeof(len), &sgp->length);
89 for (i = 0; (i < VIRTIO_MAX_SGS) && (i < in_sgs); i++) {
91 for (n = 0, sgp = get_sgp(sgs, i); sgp && (n < SG_MAX);
92 sgp = __sg_next(sgp)) {
93 bpf_probe_read_kernel(&len, sizeof(len), &sgp->length);
100 result = length2 - length1;