From nobody Tue Dec 2 02:44:09 2025 Received: from out-187.mta1.migadu.com (out-187.mta1.migadu.com [95.215.58.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DCF8A25A322 for ; Wed, 19 Nov 2025 01:35:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.187 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763516118; cv=none; b=e2sTa2+vEcjp9UTdb6xIjUQD3R+gr+UjoiLCAPvgnnKuw9g/tzG7PzPovYs5+HNbvdmRaUcR+1LNRlDDfftcZJt8mOmKZ3DkBmPrL6Vq+jE9WP4RB49Tf57QzzvBR1Es+zsDTzJx8Me4Z1UPpvoN8TTUtpN+6XV81BlKGM5/wSw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763516118; c=relaxed/simple; bh=H4Zz7btA0ifGQ9ra42hl+F8evJJdL9ebClQBIpnOkAU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DpHH0+lRjYpW+hay/wjDHM/SXzZMfORGhNtvYTVQmEdsH0qx0/Kqvq9kKlo3GJkV9jSPkPQcA6XB6IyPqT4pv+Xf4sq9dWMIoVuiPq+zfkA8ivqJCo98IYsPyHgqDYr08DEOkeLsBfKmXttHLpgeKbDNFK73DnJeA10pm0Yqu/0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=l0cWAGhd; arc=none smtp.client-ip=95.215.58.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="l0cWAGhd" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1763516115; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=D8dIlCkeLmHRvfs6fUIYZ7yvGZoAG1TWr2WBwtR6cnw=; b=l0cWAGhdWRSC8NfGk4eXSNApKMWN/6jjT/zXj8aYwETERhPKAqw9nf91LKUUVCqiLM+fkK mo4lHL3jkthGa0LDFfBd+OXsWrDEQ5bR89bznO6SIPCzUQ0onQosC6w5/0FCn1sjQRk8+Q WrAPWxSBABrvy+tLp1sJdnXMjrEnfmM= From: Hui Zhu To: Andrew Morton , Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Peter Zijlstra , Miguel Ojeda , Nathan Chancellor , Kees Cook , Tejun Heo , Jeff Xu , mkoutny@suse.com, Jan Hendrik Farr , Christian Brauner , Randy Dunlap , Brian Gerst , Masahiro Yamada , linux-kernel@vger.kernel.org, linux-mm@kvack.org, cgroups@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: Hui Zhu , Geliang Tang Subject: [RFC PATCH 3/3] samples/bpf: add example memcg eBPF program Date: Wed, 19 Nov 2025 09:34:08 +0800 Message-ID: In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" From: Hui Zhu Add a sample eBPF program demonstrating the new memory controller eBPF support. This example serves as both a reference implementation and a validation tool for the memcg eBPF functionality. The sample includes: - memcg_printk.bpf.c: An eBPF program that attaches to the try_charge_memcg hook and prints detailed information about memory charging events, including: * Memory cgroup name * GFP flags and page count * Reclamation options * Affected memory cgroup (when applicable) - memcg_printk.c: A userspace loader program that: * Loads the eBPF object file * Finds and attaches the memcg_ops struct ops * Keeps the program attached until interrupted * Provides proper error handling and cleanup Usage: $ ./samples/bpf/memcg_printk This will attach the eBPF program to the memcg charging path. Output can be viewed via kernel trace events (e.g., trace_printk logs). The program demonstrates: - Accessing memory cgroup context fields - Using bpf_printk for debugging and monitoring - Proper struct ops registration via libbpf - Integration with the kernel's BPF infrastructure Signed-off-by: Geliang Tang Signed-off-by: Hui Zhu --- MAINTAINERS | 2 + samples/bpf/Makefile | 2 + samples/bpf/memcg_printk.bpf.c | 30 +++++++++++++ samples/bpf/memcg_printk.c | 82 ++++++++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 samples/bpf/memcg_printk.bpf.c create mode 100644 samples/bpf/memcg_printk.c diff --git a/MAINTAINERS b/MAINTAINERS index dc3aa53d2346..c8f32f7dad3f 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -6356,6 +6356,8 @@ F: mm/memcontrol_bpf.h F: mm/page_counter.c F: mm/swap_cgroup.c F: samples/cgroup/* +F: samples/memcg_printk.bpf.c +F: samples/memcg_printk.c F: tools/testing/selftests/bpf/*/memcg_ops.c F: tools/testing/selftests/cgroup/memcg_protection.m F: tools/testing/selftests/cgroup/test_hugetlb_memcg.c diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile index 95a4fa1f1e44..d50e958fc8d5 100644 --- a/samples/bpf/Makefile +++ b/samples/bpf/Makefile @@ -37,6 +37,7 @@ tprogs-y +=3D xdp_fwd tprogs-y +=3D task_fd_query tprogs-y +=3D ibumad tprogs-y +=3D hbm +tprogs-y +=3D memcg_printk =20 # Libbpf dependencies LIBBPF_SRC =3D $(TOOLS_PATH)/lib/bpf @@ -122,6 +123,7 @@ always-y +=3D task_fd_query_kern.o always-y +=3D ibumad_kern.o always-y +=3D hbm_out_kern.o always-y +=3D hbm_edt_kern.o +always-y +=3D memcg_printk.bpf.o =20 COMMON_CFLAGS =3D $(TPROGS_USER_CFLAGS) TPROGS_LDFLAGS =3D $(TPROGS_USER_LDFLAGS) diff --git a/samples/bpf/memcg_printk.bpf.c b/samples/bpf/memcg_printk.bpf.c new file mode 100644 index 000000000000..66c87bf4cbcb --- /dev/null +++ b/samples/bpf/memcg_printk.bpf.c @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" + +#include +#include + +SEC("struct_ops/try_charge_memcg") +int BPF_PROG(handle_try_charge_memcg, struct try_charge_memcg *tcm) +{ + bpf_printk( + "memcg %s gfp_mask 0x%x nr_pages %lu reclaim_options 0x%lx\n", + tcm->memcg->css.cgroup->kn->name, + tcm->gfp_mask, + tcm->nr_pages, + tcm->reclaim_options); + if (!tcm->charge_done) + bpf_printk("memcg %s mem_over_limit %s\n", + tcm->memcg->css.cgroup->kn->name, + tcm->mem_over_limit->css.cgroup->kn->name); + + return 0; +} + +SEC(".struct_ops") +struct memcg_ops mcg_ops =3D { + .try_charge_memcg =3D (void *)handle_try_charge_memcg, +}; + +char _license[] SEC("license") =3D "GPL"; diff --git a/samples/bpf/memcg_printk.c b/samples/bpf/memcg_printk.c new file mode 100644 index 000000000000..a2c5be2415ea --- /dev/null +++ b/samples/bpf/memcg_printk.c @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: GPL-2.0-only +#define _GNU_SOURCE + +#include +#include +#include + +static bool exiting; + +static void sig_handler(int sig) +{ + exiting =3D true; +} + +static int libbpf_print_fn(enum libbpf_print_level level, const char *form= at, va_list args) +{ + return vfprintf(stderr, format, args); +} + +int main(int argc, char **argv) +{ + struct bpf_object *obj =3D NULL; + struct bpf_link *link =3D NULL; + struct bpf_map *map; + char filename[256]; + int err; + + exiting =3D false; + + signal(SIGINT, sig_handler); + signal(SIGTERM, sig_handler); + + libbpf_set_print(libbpf_print_fn); + + snprintf(filename, sizeof(filename), "%s.bpf.o", argv[0]); + obj =3D bpf_object__open_file(filename, NULL); + err =3D libbpf_get_error(obj); + if (err) { + fprintf(stderr, "Failed to open BPF object file: %d\n", + err); + obj =3D NULL; + goto cleanup; + } + + err =3D bpf_object__load(obj); + if (err) { + fprintf(stderr, "Failed to loading BPF object file: %d\n", + err); + goto cleanup; + } + + map =3D bpf_object__find_map_by_name(obj, "mcg_ops"); + if (!map) { + fprintf(stderr, "Failed to find struct_ops map 'mcg_ops'\n"); + err =3D -ENOENT; + goto cleanup; + } + + link =3D bpf_map__attach_struct_ops(map); + err =3D libbpf_get_error(link); + if (err) { + fprintf(stderr, "Failed to attach struct ops: %d\n", + err); + link =3D NULL; + goto cleanup; + } + + printf("Press Ctrl+C to exit...\n"); + + while (!exiting) + sleep(1); + + printf("Bye!\n"); + +cleanup: + if (link) + bpf_link__destroy(link); + if (obj) + bpf_object__close(obj); + + return err; +} --=20 2.43.0