186a35af6SYonghong Song // SPDX-License-Identifier: GPL-2.0
286a35af6SYonghong Song
386a35af6SYonghong Song #include <linux/ptrace.h>
486a35af6SYonghong Song #include <stddef.h>
586a35af6SYonghong Song #include <linux/bpf.h>
686a35af6SYonghong Song #include <bpf/bpf_helpers.h>
786a35af6SYonghong Song #include <bpf/bpf_tracing.h>
8*3c2611baSAlexei Starovoitov #include "bpf_misc.h"
986a35af6SYonghong Song
1086a35af6SYonghong Song char _license[] SEC("license") = "GPL";
1186a35af6SYonghong Song
1286a35af6SYonghong Song /* typically virtio scsi has max SGs of 6 */
1386a35af6SYonghong Song #define VIRTIO_MAX_SGS 6
1486a35af6SYonghong Song
1586a35af6SYonghong Song /* Verifier will fail with SG_MAX = 128. The failure can be
1686a35af6SYonghong Song * workarounded with a smaller SG_MAX, e.g. 10.
1786a35af6SYonghong Song */
1886a35af6SYonghong Song #define WORKAROUND
1986a35af6SYonghong Song #ifdef WORKAROUND
2086a35af6SYonghong Song #define SG_MAX 10
2186a35af6SYonghong Song #else
2286a35af6SYonghong Song /* typically virtio blk has max SEG of 128 */
2386a35af6SYonghong Song #define SG_MAX 128
2486a35af6SYonghong Song #endif
2586a35af6SYonghong Song
2686a35af6SYonghong Song #define SG_CHAIN 0x01UL
2786a35af6SYonghong Song #define SG_END 0x02UL
2886a35af6SYonghong Song
2986a35af6SYonghong Song struct scatterlist {
3086a35af6SYonghong Song unsigned long page_link;
3186a35af6SYonghong Song unsigned int offset;
3286a35af6SYonghong Song unsigned int length;
3386a35af6SYonghong Song };
3486a35af6SYonghong Song
3586a35af6SYonghong Song #define sg_is_chain(sg) ((sg)->page_link & SG_CHAIN)
3686a35af6SYonghong Song #define sg_is_last(sg) ((sg)->page_link & SG_END)
3786a35af6SYonghong Song #define sg_chain_ptr(sg) \
3886a35af6SYonghong Song ((struct scatterlist *) ((sg)->page_link & ~(SG_CHAIN | SG_END)))
3986a35af6SYonghong Song
__sg_next(struct scatterlist * sgp)4086a35af6SYonghong Song static inline struct scatterlist *__sg_next(struct scatterlist *sgp)
4186a35af6SYonghong Song {
4286a35af6SYonghong Song struct scatterlist sg;
4386a35af6SYonghong Song
4486a35af6SYonghong Song bpf_probe_read_kernel(&sg, sizeof(sg), sgp);
4586a35af6SYonghong Song if (sg_is_last(&sg))
4686a35af6SYonghong Song return NULL;
4786a35af6SYonghong Song
4886a35af6SYonghong Song sgp++;
4986a35af6SYonghong Song
5086a35af6SYonghong Song bpf_probe_read_kernel(&sg, sizeof(sg), sgp);
5186a35af6SYonghong Song if (sg_is_chain(&sg))
5286a35af6SYonghong Song sgp = sg_chain_ptr(&sg);
5386a35af6SYonghong Song
5486a35af6SYonghong Song return sgp;
5586a35af6SYonghong Song }
5686a35af6SYonghong Song
get_sgp(struct scatterlist ** sgs,int i)5786a35af6SYonghong Song static inline struct scatterlist *get_sgp(struct scatterlist **sgs, int i)
5886a35af6SYonghong Song {
5986a35af6SYonghong Song struct scatterlist *sgp;
6086a35af6SYonghong Song
6186a35af6SYonghong Song bpf_probe_read_kernel(&sgp, sizeof(sgp), sgs + i);
6286a35af6SYonghong Song return sgp;
6386a35af6SYonghong Song }
6486a35af6SYonghong Song
6586a35af6SYonghong Song int config = 0;
6686a35af6SYonghong Song int result = 0;
6786a35af6SYonghong Song
6886a35af6SYonghong Song SEC("kprobe/virtqueue_add_sgs")
BPF_KPROBE(trace_virtqueue_add_sgs,void * unused,struct scatterlist ** sgs,unsigned int out_sgs,unsigned int in_sgs)6911d39cfeSAndrii Nakryiko int BPF_KPROBE(trace_virtqueue_add_sgs, void *unused, struct scatterlist **sgs,
7086a35af6SYonghong Song unsigned int out_sgs, unsigned int in_sgs)
7186a35af6SYonghong Song {
7286a35af6SYonghong Song struct scatterlist *sgp = NULL;
7386a35af6SYonghong Song __u64 length1 = 0, length2 = 0;
7486a35af6SYonghong Song unsigned int i, n, len;
7586a35af6SYonghong Song
7686a35af6SYonghong Song if (config != 0)
7786a35af6SYonghong Song return 0;
7886a35af6SYonghong Song
7986a35af6SYonghong Song for (i = 0; (i < VIRTIO_MAX_SGS) && (i < out_sgs); i++) {
80*3c2611baSAlexei Starovoitov __sink(out_sgs);
8186a35af6SYonghong Song for (n = 0, sgp = get_sgp(sgs, i); sgp && (n < SG_MAX);
8286a35af6SYonghong Song sgp = __sg_next(sgp)) {
8386a35af6SYonghong Song bpf_probe_read_kernel(&len, sizeof(len), &sgp->length);
8486a35af6SYonghong Song length1 += len;
8586a35af6SYonghong Song n++;
8686a35af6SYonghong Song }
8786a35af6SYonghong Song }
8886a35af6SYonghong Song
8986a35af6SYonghong Song for (i = 0; (i < VIRTIO_MAX_SGS) && (i < in_sgs); i++) {
90*3c2611baSAlexei Starovoitov __sink(in_sgs);
9186a35af6SYonghong Song for (n = 0, sgp = get_sgp(sgs, i); sgp && (n < SG_MAX);
9286a35af6SYonghong Song sgp = __sg_next(sgp)) {
9386a35af6SYonghong Song bpf_probe_read_kernel(&len, sizeof(len), &sgp->length);
9486a35af6SYonghong Song length2 += len;
9586a35af6SYonghong Song n++;
9686a35af6SYonghong Song }
9786a35af6SYonghong Song }
9886a35af6SYonghong Song
9986a35af6SYonghong Song config = 1;
10086a35af6SYonghong Song result = length2 - length1;
10186a35af6SYonghong Song return 0;
10286a35af6SYonghong Song }
103