Skip to content

blkalgn: add support #5128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docker/build/Dockerfile.fedora
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ RUN dnf -y install \
python3-devel \
libstdc++ \
libstdc++-devel \
systemtap-sdt-devel
systemtap-sdt-devel \
json-c-devel

RUN dnf -y install \
python3 \
Expand Down
20 changes: 16 additions & 4 deletions libbpf-tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ $(call allow-override,CC,$(CROSS_COMPILE)cc)
$(call allow-override,LD,$(CROSS_COMPILE)ld)

.PHONY: all
all: $(APPS) $(APP_ALIASES)
all: $(APPS) $(APP_ALIASES) blkalgn

ifeq ($(V),1)
Q =
Expand Down Expand Up @@ -161,7 +161,7 @@ endif
.PHONY: clean
clean:
$(call msg,CLEAN)
$(Q)rm -rf $(OUTPUT) $(APPS) $(APP_ALIASES)
$(Q)rm -rf $(OUTPUT) $(APPS) $(APP_ALIASES) blkalgn

$(LIBBLAZESYM_SRC)::
$(Q)cd blazesym && cargo build --release --features=cheader
Expand All @@ -186,12 +186,18 @@ $(APPS): %: $(OUTPUT)/%.o $(COMMON_OBJ) $(LIBBPF_OBJ) | $(OUTPUT)
$(call msg,BINARY,$@)
$(Q)$(CC) $(CFLAGS) $^ $(LDFLAGS) -lelf -lz -o $@

blkalgn: %: $(OUTPUT)/%.o $(COMMON_OBJ) $(LIBBPF_OBJ) | $(OUTPUT)
$(call msg,BINARY,$@)
$(Q)$(CC) $(CFLAGS) $^ $(LDFLAGS) -lelf -lz -ljson-c -lm -o $@

ifeq ($(USE_BLAZESYM),1)
$(patsubst %,$(OUTPUT)/%.o,$(BZ_APPS)): $(OUTPUT)/blazesym.h
endif

$(patsubst %,$(OUTPUT)/%.o,$(APPS)): %.o: %.skel.h

$(patsubst %,$(OUTPUT)/%.o,blkalgn): %.o: %.skel.h

$(OUTPUT)/%.o: %.c $(wildcard %.h) $(LIBBPF_OBJ) | $(OUTPUT)
$(call msg,CC,$@)
$(Q)$(CC) $(CFLAGS) $(INCLUDES) -c $(filter %.c,$^) -o $@
Expand All @@ -200,6 +206,10 @@ $(OUTPUT)/%.skel.h: $(OUTPUT)/%.bpf.o | $(OUTPUT) $(BPFTOOL)
$(call msg,GEN-SKEL,$@)
$(Q)$(BPFTOOL) gen skeleton $< > $@

$(OUTPUT)/blkalgn.skel.h: $(OUTPUT)/blkalgn.bpf.o | $(OUTPUT) $(BPFTOOL)
$(call msg,GEN-SKEL,$@)
$(Q)$(BPFTOOL) gen skeleton $< > $@

$(OUTPUT)/softirqs.bpf.o: BPFCFLAGS = $(BPFCFLAGS_softirqs)

$(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard %.h) $(ARCH)/vmlinux.h | $(OUTPUT)
Expand Down Expand Up @@ -238,11 +248,13 @@ $(SIGSNOOP_ALIAS): sigsnoop
$(call msg,SYMLINK,$@)
$(Q)ln -f -s $(APP_PREFIX)$^ $@

install: $(APPS) $(APP_ALIASES)
install: $(APPS) $(APP_ALIASES) install-blkalgn
$(call msg, INSTALL libbpf-tools)
$(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(bindir)
$(Q)$(foreach app,$(APPS),$(INSTALL) $(app) $(DESTDIR)$(bindir)/$(APP_PREFIX)$(app);)
$(Q)$(foreach alias,$(APP_ALIASES),cp -a $(alias) $(DESTDIR)$(bindir)/$(APP_PREFIX)$(alias);)

install-blkalgn: blkalgn
$(Q)$(INSTALL) blkalgn $(DESTDIR)$(bindir)/$(APP_PREFIX)blkalgn

.PHONY: force
force:
Expand Down
120 changes: 120 additions & 0 deletions libbpf-tools/blkalgn.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2024 Samsung */
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#include "blkalgn.h"
#include "bits.bpf.h"
#include "core_fixes.bpf.h"

const volatile bool filter_dev = false;
const volatile __u32 targ_dev = 0;
const volatile bool filter_ops = false;
const volatile __u32 targ_ops = 0;
const volatile bool filter_len = false;
const volatile __u32 targ_len = 0;
const volatile bool filter_comm = false;
const volatile char targ_comm[TASK_COMM_LEN] = {};
const volatile bool capture_stack = false;

extern __u32 LINUX_KERNEL_VERSION __kconfig;

char LICENSE[] SEC("license") = "Dual BSD/GPL";

struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 2097152);
} rb SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 10240);
__type(key, struct hkey);
__type(value, struct hval);
} halgn_map SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 10240);
__type(key, struct hkey);
__type(value, struct hval);
} hgran_map SEC(".maps");

static __always_inline bool comm_allowed(const char *comm)
{
int i;

for (i = 0; i < TASK_COMM_LEN && targ_comm[i] != '\0'; i++) {
if (comm[i] != targ_comm[i])
return false;
}
return true;
}

static int __always_inline trace_rq_issue(void *ctx, struct request *rq)
{
struct event *e;
u32 dev;
char comm[TASK_COMM_LEN];

struct gendisk *disk = get_disk(rq);

dev = disk ? MKDEV(BPF_CORE_READ(disk, major),
BPF_CORE_READ(disk, first_minor)) :
0;

if (filter_dev && targ_dev != dev)
return 0;

if (filter_ops && targ_ops != (rq->cmd_flags & 0xff))
return 0;

if (filter_len && targ_len != (rq->__data_len))
return 0;

if (filter_comm) {
bpf_get_current_comm(&comm, sizeof(comm));
if (!comm_allowed(comm))
return 0;
}

e = bpf_ringbuf_reserve(&rb, sizeof(*e), 0);
if (!e)
return 0;

bpf_get_current_comm(&e->comm, sizeof(e->comm));
e->pid = bpf_get_current_pid_tgid();
bpf_probe_read_kernel(&e->disk, sizeof(e->disk),
rq->q->disk->disk_name);

if (capture_stack) {
e->kstack_sz =
bpf_get_stack(ctx, e->kstack, sizeof(e->kstack), 0);
e->ustack_sz = bpf_get_stack(ctx, e->ustack, sizeof(e->ustack),
BPF_F_USER_STACK);
}

e->flags = rq->cmd_flags;
e->lbs = rq->q->limits.logical_block_size;
e->len = rq->__data_len;
e->sector = rq->__sector;

bpf_ringbuf_submit(e, 0);

return 0;
}

SEC("tp_btf/block_rq_issue")
int BPF_PROG(block_rq_issue)
{
/*
* Commit a54895fa (v5.11-rc1) changed tracepoint argument list
* from TP_PROTO(struct request_queue *q, struct request *rq)
* to TP_PROTO(struct request *rq)
*/
if (LINUX_KERNEL_VERSION >= KERNEL_VERSION(5, 11, 0))
return trace_rq_issue(ctx, (void *)ctx[0]);
else
return trace_rq_issue(ctx, (void *)ctx[1]);
}
Loading