From nobody Fri Dec 19 21:17:24 2025 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 09DBF314B60 for ; Thu, 4 Dec 2025 02:50:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764816628; cv=none; b=odNxvUgNsGSZehpGUPDBi/yIw1BQ1AcPbpRUJGYOEQVLuVtQQ5poA8Gfz7u0gq4c7aWX9o1DnHkkolVQiUdkDc+2ZNJSRWb4vJV456l38Nyplh7vbJAvUctHGWiudklpfXWzfhm9zx1vDatCMcD8znP/jiQKpiw4ZoCNtXMVM20= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764816628; c=relaxed/simple; bh=68DTt/GuOSLKGluBcJnffa2coBM4bFiaelurtvz3SlM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HjvJZZYGD3olW5aArvNAErXZm5J86p2iktpRQKsdoy1gnmfi9zs75g6pzuRYnad06EZwVvOzjrnHPTZW9inDiMg4X9l3+RJzrlO9GKLbkxENDvvn87LVGn0o+uqmUNWXg3s1c4p0Mp/yy98n1Y/JK81Nn1rkgzkvSDtries56fg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--wusamuel.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=AzLEcSQH; arc=none smtp.client-ip=209.85.215.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--wusamuel.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="AzLEcSQH" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b609c0f6522so691749a12.3 for ; Wed, 03 Dec 2025 18:50:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764816617; x=1765421417; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=5GxvGZcQ0kz+qaUDRrIQhWZRD2UKUVtV4j+gmBLCpxM=; b=AzLEcSQH0Hv+q+qVuL8p6Lek307BO1Yy8gEiqRmm2R/BMn30rJfqw4AyzSMWoXso1V Zwny+oAOlr/kkWiNYM8SYrFtNlC8NWtEomHdvhLF6Z3s2K9f1T82/xmu621LyfppwjRo nl1pt4k64yHkpsOzCsZSL0u+pQwpT9TRqBLy/rWykCa0muhdyEFPPl1U/2IJ5YX977Bw W2Ss9Vb1wA2LnxKM/j0WZapN4T7IczKtWHm7iEvHs7ZMNrqBAgHwLvLzDYut5VT4/l7I E7kwG/EKcj1WLE47xOWW1ZDyb5heAjUPJxucB7ngyayDMx2ilQ7v7eRU/qJXxBo/YhF1 qzsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764816617; x=1765421417; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=5GxvGZcQ0kz+qaUDRrIQhWZRD2UKUVtV4j+gmBLCpxM=; b=NrKLrGoZmjMMfyNmsN2Ii+Yqi/+/JLQvBTKeMD/xwX4kS/PnyD0lTgpgNJa0zZ8X6j bZWROgpHt9aHvKstnibudM6bSLWLDMLtugLEv24K30TTypZaqNrISj9TFG05pAar2vA+ YHjGx2TmAwDNSBOd2lvH8fcRXa42TlN4LI8ae47ttX8Zeo/50Asb+Bkdt5zZ5tErnww2 p58VCSejkKEAOqOsOGNs1Ov5wg43NX5xAOl07RC65cVztuChkpEbMJtOgIg8yNietVIY +fzV6foh1Zj6qNk0gtnuIWn3y28pvaPP4Q12p5VNSBVuE0rlAdPfd2Pw8dplyVpHyPAu zD3w== X-Forwarded-Encrypted: i=1; AJvYcCXlbMMevIpl/tq07tbkuog3ROulGuZyk4UuuDwc6JX5iOje+ln3Was25zCTrA9sv02el9AJ45csdwUn9qk=@vger.kernel.org X-Gm-Message-State: AOJu0YzxcaWvxVjvH10wKB3AEBqAptib2fZRK5Y4Gk1WVug73plyT9Dw X+fK6MpeIHLIy+Lv2t2kGdDi7SCLAsVYMxCS8uFD4gVsI55MYVZoN4nuZo8RjwI5k+ZyJ9znqm1 iGgywUs7+tC11UA== X-Google-Smtp-Source: AGHT+IE2CnqpFT/04i/szjW02kaHfA3Zxl8mMBN7I5h+3K1cCj1sU8buNLgR9vr2JBGK5UyjM8V8NxSOFXaAtA== X-Received: from dydd3.prod.google.com ([2002:a05:693c:61c3:b0:2a4:6ae0:3502]) (user=wusamuel job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:cc9c:b0:2a4:3644:4be3 with SMTP id 5a478bee46e88-2ab92ee527fmr2994505eec.27.1764816617047; Wed, 03 Dec 2025 18:50:17 -0800 (PST) Date: Wed, 3 Dec 2025 18:49:58 -0800 In-Reply-To: <20251204025003.3162056-1-wusamuel@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251204025003.3162056-1-wusamuel@google.com> X-Mailer: git-send-email 2.52.0.177.g9f829587af-goog Message-ID: <20251204025003.3162056-2-wusamuel@google.com> Subject: [PATCH v1 1/4] bpf: Add wakeup_source iterator From: Samuel Wu To: 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 Cc: rafael.j.wysocki@intel.com, Samuel Wu , kernel-team@android.com, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a BPF iterator for traversing through wakeup_sources. Setup iterators to traverse through a SRCUs of wakeup_sources. This is a more elegant and efficient traversal than going through the options today, such as at /sys/class/wakeup, or through debugfs. Signed-off-by: Samuel Wu --- kernel/bpf/Makefile | 1 + kernel/bpf/wakeup_source_iter.c | 103 ++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 kernel/bpf/wakeup_source_iter.c diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile index 232cbc97434d..6a479982469a 100644 --- a/kernel/bpf/Makefile +++ b/kernel/bpf/Makefile @@ -56,6 +56,7 @@ obj-$(CONFIG_BPF_SYSCALL) +=3D kmem_cache_iter.o ifeq ($(CONFIG_DMA_SHARED_BUFFER),y) obj-$(CONFIG_BPF_SYSCALL) +=3D dmabuf_iter.o endif +obj-$(CONFIG_BPF_SYSCALL) +=3D wakeup_source_iter.o =20 CFLAGS_REMOVE_percpu_freelist.o =3D $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_bpf_lru_list.o =3D $(CC_FLAGS_FTRACE) diff --git a/kernel/bpf/wakeup_source_iter.c b/kernel/bpf/wakeup_source_ite= r.c new file mode 100644 index 000000000000..b8719f47428e --- /dev/null +++ b/kernel/bpf/wakeup_source_iter.c @@ -0,0 +1,103 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2025 Google LLC */ +#include +#include +#include +#include +#include + +struct bpf_iter__wakeup_source { + __bpf_md_ptr(struct bpf_iter_meta *, meta); + __bpf_md_ptr(struct wakeup_source *, wakeup_source); +}; + +static void *wakeup_source_iter_seq_start(struct seq_file *seq, loff_t *po= s) +{ + int *srcuidx =3D seq->private; + struct wakeup_source *ws; + loff_t i; + + *srcuidx =3D wakeup_sources_read_lock(); + + ws =3D wakeup_sources_walk_start(); + for (i =3D 0; ws && i < *pos; i++) + ws =3D wakeup_sources_walk_next(ws); + + return ws; +} + +static void *wakeup_source_iter_seq_next(struct seq_file *seq, void *v, lo= ff_t *pos) +{ + struct wakeup_source *ws =3D v; + + ++*pos; + + return wakeup_sources_walk_next(ws); +} + +static void wakeup_source_iter_seq_stop(struct seq_file *seq, void *v) +{ + int *srcuidx =3D seq->private; + + if (*srcuidx >=3D 0) + wakeup_sources_read_unlock(*srcuidx); + *srcuidx =3D -1; +} + +static int __wakeup_source_seq_show(struct seq_file *seq, void *v, bool in= _stop) +{ + struct bpf_iter_meta meta =3D { + .seq =3D seq, + }; + struct bpf_iter__wakeup_source ctx =3D { + .meta =3D &meta, + .wakeup_source =3D v, + }; + struct bpf_prog *prog =3D bpf_iter_get_info(&meta, in_stop); + + if (prog) + return bpf_iter_run_prog(prog, &ctx); + + return 0; +} + +static int wakeup_source_iter_seq_show(struct seq_file *seq, void *v) +{ + return __wakeup_source_seq_show(seq, v, false); +} + +static const struct seq_operations wakeup_source_iter_seq_ops =3D { + .start =3D wakeup_source_iter_seq_start, + .next =3D wakeup_source_iter_seq_next, + .stop =3D wakeup_source_iter_seq_stop, + .show =3D wakeup_source_iter_seq_show, +}; + +static const struct bpf_iter_seq_info wakeup_source_iter_seq_info =3D { + .seq_ops =3D &wakeup_source_iter_seq_ops, + .seq_priv_size =3D sizeof(int), +}; + +static struct bpf_iter_reg bpf_wakeup_source_reg_info =3D { + .target =3D "wakeup_source", + .ctx_arg_info_size =3D 1, + .ctx_arg_info =3D { + { + offsetof(struct bpf_iter__wakeup_source, wakeup_source), + PTR_TO_BTF_ID_OR_NULL + }, + }, + .seq_info =3D &wakeup_source_iter_seq_info, +}; + +DEFINE_BPF_ITER_FUNC(wakeup_source, struct bpf_iter_meta *meta, + struct wakeup_source *wakeup_source) +BTF_ID_LIST_SINGLE(bpf_wakeup_source_btf_id, struct, wakeup_source) + +static int __init wakeup_source_iter_init(void) +{ + bpf_wakeup_source_reg_info.ctx_arg_info[0].btf_id =3D bpf_wakeup_source_b= tf_id[0]; + return bpf_iter_reg_target(&bpf_wakeup_source_reg_info); +} + +late_initcall(wakeup_source_iter_init); --=20 2.52.0.177.g9f829587af-goog