Skip to content

Commit

Permalink
Merge pull request iovisor#4088 from chenhengqi/add-riscv-support-for…
Browse files Browse the repository at this point in the history
…-libbpf-tools

Add RISC-V support for libbpf tools
  • Loading branch information
davemarchevsky committed Jul 12, 2022
2 parents 4ce27d4 + a7fb5d6 commit 74ba1d1
Show file tree
Hide file tree
Showing 11 changed files with 211,111 additions and 97,986 deletions.
2 changes: 1 addition & 1 deletion libbpf-tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ CFLAGS := -g -O2 -Wall
BPFCFLAGS := -g -O2 -Wall
INSTALL ?= install
prefix ?= /usr/local
ARCH := $(shell uname -m | sed 's/x86_64/x86/' | sed 's/aarch64/arm64/' | sed 's/ppc64le/powerpc/' | sed 's/mips.*/mips/')
ARCH := $(shell uname -m | sed 's/x86_64/x86/' | sed 's/aarch64/arm64/' | sed 's/ppc64le/powerpc/' | sed 's/mips.*/mips/' | sed 's/riscv64/riscv/')
BTFHUB_ARCHIVE ?= $(abspath btfhub-archive)

ifeq ($(wildcard $(ARCH)/),)
Expand Down
21 changes: 3 additions & 18 deletions libbpf-tools/biolatency.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <bpf/bpf_tracing.h>
#include "biolatency.h"
#include "bits.bpf.h"
#include "core_fixes.bpf.h"

#define MAX_ENTRIES 10240

Expand All @@ -19,10 +20,6 @@ const volatile bool targ_ms = false;
const volatile bool filter_dev = false;
const volatile __u32 targ_dev = 0;

struct request_queue___x {
struct gendisk *disk;
} __attribute__((preserve_access_index));

struct {
__uint(type, BPF_MAP_TYPE_CGROUP_ARRAY);
__type(key, u32);
Expand Down Expand Up @@ -55,15 +52,9 @@ int trace_rq_start(struct request *rq, int issue)
u64 ts = bpf_ktime_get_ns();

if (filter_dev) {
struct request_queue___x *q = (void *)BPF_CORE_READ(rq, q);
struct gendisk *disk;
struct gendisk *disk = get_disk(rq);
u32 dev;

if (bpf_core_field_exists(q->disk))
disk = BPF_CORE_READ(q, disk);
else
disk = BPF_CORE_READ(rq, rq_disk);

dev = disk ? MKDEV(BPF_CORE_READ(disk, major),
BPF_CORE_READ(disk, first_minor)) : 0;
if (targ_dev != dev)
Expand Down Expand Up @@ -127,13 +118,7 @@ int BPF_PROG(block_rq_complete, struct request *rq, int error,
goto cleanup;

if (targ_per_disk) {
struct request_queue___x *q = (void *)BPF_CORE_READ(rq, q);
struct gendisk *disk;

if (bpf_core_field_exists(q->disk))
disk = BPF_CORE_READ(q, disk);
else
disk = BPF_CORE_READ(rq, rq_disk);
struct gendisk *disk = get_disk(rq);

hkey.dev = disk ? MKDEV(BPF_CORE_READ(disk, major),
BPF_CORE_READ(disk, first_minor)) : 0;
Expand Down
2 changes: 1 addition & 1 deletion libbpf-tools/biopattern.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ int handle__block_rq_complete(void *args)
nr_sector = BPF_CORE_READ(ctx, nr_sector);
dev = BPF_CORE_READ(ctx, dev);
} else {
struct trace_event_raw_block_rq_complete *ctx = args;
struct trace_event_raw_block_rq_complete___x *ctx = args;
sector = BPF_CORE_READ(ctx, sector);
nr_sector = BPF_CORE_READ(ctx, nr_sector);
dev = BPF_CORE_READ(ctx, dev);
Expand Down
13 changes: 2 additions & 11 deletions libbpf-tools/biosnoop.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <bpf/bpf_core_read.h>
#include <bpf/bpf_tracing.h>
#include "biosnoop.h"
#include "core_fixes.bpf.h"

#define MAX_ENTRIES 10240

Expand All @@ -15,10 +16,6 @@ const volatile __u32 targ_dev = 0;

extern __u32 LINUX_KERNEL_VERSION __kconfig;

struct request_queue___x {
struct gendisk *disk;
} __attribute__((preserve_access_index));

struct {
__uint(type, BPF_MAP_TYPE_CGROUP_ARRAY);
__type(key, u32);
Expand Down Expand Up @@ -95,13 +92,7 @@ int trace_rq_start(struct request *rq, bool insert)

stagep = bpf_map_lookup_elem(&start, &rq);
if (!stagep) {
struct request_queue___x *q = (void *)BPF_CORE_READ(rq, q);
struct gendisk *disk;

if (bpf_core_field_exists(q->disk))
disk = BPF_CORE_READ(q, disk);
else
disk = BPF_CORE_READ(rq, rq_disk);
struct gendisk *disk = get_disk(rq);

stage.dev = disk ? MKDEV(BPF_CORE_READ(disk, major),
BPF_CORE_READ(disk, first_minor)) : 0;
Expand Down
13 changes: 2 additions & 11 deletions libbpf-tools/biostacks.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@
#include "biostacks.h"
#include "bits.bpf.h"
#include "maps.bpf.h"
#include "core_fixes.bpf.h"

#define MAX_ENTRIES 10240

const volatile bool targ_ms = false;
const volatile bool filter_dev = false;
const volatile __u32 targ_dev = -1;

struct request_queue___x {
struct gendisk *disk;
} __attribute__((preserve_access_index));

struct internal_rqinfo {
u64 start_ts;
struct rqinfo rqinfo;
Expand All @@ -43,15 +40,9 @@ static __always_inline
int trace_start(void *ctx, struct request *rq, bool merge_bio)
{
struct internal_rqinfo *i_rqinfop = NULL, i_rqinfo = {};
struct request_queue___x *q = (void *)BPF_CORE_READ(rq, q);
struct gendisk *disk;
struct gendisk *disk = get_disk(rq);
u32 dev;

if (bpf_core_field_exists(q->disk))
disk = BPF_CORE_READ(q, disk);
else
disk = BPF_CORE_READ(rq, rq_disk);

dev = disk ? MKDEV(BPF_CORE_READ(disk, major),
BPF_CORE_READ(disk, first_minor)) : 0;
if (filter_dev && targ_dev != dev)
Expand Down
13 changes: 2 additions & 11 deletions libbpf-tools/bitesize.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@
#include <bpf/bpf_core_read.h>
#include "bitesize.h"
#include "bits.bpf.h"
#include "core_fixes.bpf.h"

const volatile char targ_comm[TASK_COMM_LEN] = {};
const volatile bool filter_dev = false;
const volatile __u32 targ_dev = 0;

extern __u32 LINUX_KERNEL_VERSION __kconfig;

struct request_queue___x {
struct gendisk *disk;
} __attribute__((preserve_access_index));

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 10240);
Expand Down Expand Up @@ -44,15 +41,9 @@ static int trace_rq_issue(struct request *rq)
u64 slot;

if (filter_dev) {
struct request_queue___x *q = (void *)BPF_CORE_READ(rq, q);
struct gendisk *disk;
struct gendisk *disk = get_disk(rq);
u32 dev;

if (bpf_core_field_exists(q->disk))
disk = BPF_CORE_READ(q, disk);
else
disk = BPF_CORE_READ(rq, rq_disk);

dev = disk ? MKDEV(BPF_CORE_READ(disk, major),
BPF_CORE_READ(disk, first_minor)) : 0;
if (targ_dev != dev)
Expand Down
46 changes: 44 additions & 2 deletions libbpf-tools/core_fixes.bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
* see:
* https://github.com/torvalds/linux/commit/2f064a59a1
*/
struct task_struct___o {
volatile long int state;
} __attribute__((preserve_access_index));

struct task_struct___x {
unsigned int __state;
} __attribute__((preserve_access_index));
Expand All @@ -23,7 +27,7 @@ static __always_inline __s64 get_task_state(void *task)

if (bpf_core_field_exists(t->__state))
return BPF_CORE_READ(t, __state);
return BPF_CORE_READ((struct task_struct *)task, state);
return BPF_CORE_READ((struct task_struct___o *)task, state);
}

/**
Expand All @@ -32,6 +36,10 @@ static __always_inline __s64 get_task_state(void *task)
* see:
* https://github.com/torvalds/linux/commit/309dca309fc3
*/
struct bio___o {
struct gendisk *bi_disk;
} __attribute__((preserve_access_index));

struct bio___x {
struct block_device *bi_bdev;
} __attribute__((preserve_access_index));
Expand All @@ -42,7 +50,7 @@ static __always_inline struct gendisk *get_gendisk(void *bio)

if (bpf_core_field_exists(b->bi_bdev))
return BPF_CORE_READ(b, bi_bdev, bd_disk);
return BPF_CORE_READ((struct bio *)bio, bi_disk);
return BPF_CORE_READ((struct bio___o *)bio, bi_disk);
}

/**
Expand All @@ -54,6 +62,12 @@ static __always_inline struct gendisk *get_gendisk(void *bio)
* see:
* https://github.com/torvalds/linux/commit/d5869fdc189f
*/
struct trace_event_raw_block_rq_complete___x {
dev_t dev;
sector_t sector;
unsigned int nr_sector;
} __attribute__((preserve_access_index));

struct trace_event_raw_block_rq_completion___x {
dev_t dev;
sector_t sector;
Expand All @@ -67,4 +81,32 @@ static __always_inline bool has_block_rq_completion()
return false;
}

/**
* commit d152c682f03c ("block: add an explicit ->disk backpointer to the
* request_queue") and commit f3fa33acca9f ("block: remove the ->rq_disk
* field in struct request") make some changes to `struct request` and
* `struct request_queue`. Now, to get the `struct gendisk *` field in a CO-RE
* way, we need both `struct request` and `struct request_queue`.
* see:
* https://github.com/torvalds/linux/commit/d152c682f03c
* https://github.com/torvalds/linux/commit/f3fa33acca9f
*/
struct request_queue___x {
struct gendisk *disk;
} __attribute__((preserve_access_index));

struct request___x {
struct request_queue___x *q;
struct gendisk *rq_disk;
} __attribute__((preserve_access_index));

static __always_inline struct gendisk *get_disk(void *request)
{
struct request___x *r = request;

if (bpf_core_field_exists(r->rq_disk))
return BPF_CORE_READ(r, rq_disk);
return BPF_CORE_READ(r, q, disk);
}

#endif /* __CORE_FIXES_BPF_H */
1 change: 1 addition & 0 deletions libbpf-tools/riscv/vmlinux.h
Loading

0 comments on commit 74ba1d1

Please sign in to comment.