From nobody Fri Dec 19 19:36:25 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 From nobody Fri Dec 19 19:36:25 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 29D453148B5 for ; Thu, 4 Dec 2025 02:50:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764816632; cv=none; b=Wx0kGJX6pzp2EDoVS9zE6dXW/DbhUOZyeJbGcLN5T90XHbr3wUGR3Zu2+YIKvkIx9CkB7A+s50JerZyPFyTS2zwY11MCQPcyuN8MiwNTCS2K6BmVxHpQuDVSz53APSzb3lQDUQy1eu7wLb7CIJ0Bta/lVIToIeipywzbeso8OIw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764816632; c=relaxed/simple; bh=i9rWPJ1BRPlZegR1PINTMP8i5dYmXfU+6qSQof50Jxc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lYYsIy09mK5L8hh6Ni4mAEY+G9tscrLhmfUUb+r30kXcdfdilmOEiOSONLnIb2Bhah+DmDqcq1DHKJkZ7C1E56EnoiE93euOffH4W7lGf6GmQP5jt9ASUYxV4mstAk5eT9+s4Djdi35bcTnfZUFOFbjD1WE1jmKVO5Y3M+GlAmA= 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=fuQ6s5Hn; arc=none smtp.client-ip=209.85.216.74 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="fuQ6s5Hn" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-343daf0f488so437063a91.1 for ; Wed, 03 Dec 2025 18:50:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764816622; x=1765421422; 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=qSyTnP2FbGdIREPxSFsXIq8s93x8aTcDdJSLn+2cqSQ=; b=fuQ6s5HnszUxh/kS8dnXcT8QoiqQ5LWe05lQ7bln9fw7ujHtTjFUcBlK2OTVFIzF3f 32gNiQScCBnk2K76LNxGta/Jsqurg8YXc5CcTBWTYDnraTWb8ISEmfACNgQFXvB3Pww8 H7nkMD39BcavVrTyVxlGLies42dZM1nCvukAtpEPq6nAdsvrSo9tNzKTkocZ5zvS3UW2 ti5194GwkzglfBGNSjP0mFeo5RpsrVS6ls/ba+iHxcpM9B+ExyHMgi/fqseUi2Z87S/N HVBaMVbK2HRWZQM28tiqYpdIKgR9W/jSZ7W1baiD2lUmbOHPISW5EbKTtqJF7QJ65vDt s6vg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764816622; x=1765421422; 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=qSyTnP2FbGdIREPxSFsXIq8s93x8aTcDdJSLn+2cqSQ=; b=cvwduidyDsEwd+nxnCanUYISRh3L3+69C4Tlc/sPPeucS62ewehUBaGtwyaQIPWR08 wgLRPelCjlcRzJOC2HOuUeW3noMlBmOJD8hbB22rViIjvD4QSYRugJWrGu8c4B8Um+ET WhDlIb4tmVMjqvcaZNc4FoZDz+De9AF9VH7stNb8kJlWRrIAuT3bhEHu7lsRvxQue4j1 ekGpFN5HTDinr2FU1B4gMFEuaqM6ArHh4YWrDfNE7oP5VbTdVHGQEtMqUBr0uML+Ku0F /j0kz8QmG/Ji5MLbxoUQWUeceEI4j7Lr2yZWKr9jiHXrsAmJesBMehZJRnklPEC7VVDQ v4Gw== X-Forwarded-Encrypted: i=1; AJvYcCU7BQbc73VpsctYgnU6JV+TeCBYChTmvc9GFb9viOuCng6IvVPVafdDCtqI4AlSUDxHoNAUjw6OArMg568=@vger.kernel.org X-Gm-Message-State: AOJu0YzvCJgFS45JLM5CymPlp+/NATtE+Nw/ybFJyCJm9zdE2o/J6YGO tpLUH/PI+VPgGj9ocUXQ+27zejGA23OLM3JDhcoJazhXQsLW0LQSSZTqqhG+OCo72Ogps/6AXxD 0r/1TFg5wvHeNmQ== X-Google-Smtp-Source: AGHT+IF+RwUg4NL/WO7wzUqMdZLA75lB4Kko0R/cM/dJoFvWb5ppd/z3qezKD0KnEXcQmgmOSSRWiV2vCaVfcQ== X-Received: from pgdi15.prod.google.com ([2002:a05:6a02:51ef:b0:bc8:68ae:e506]) (user=wusamuel job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:2588:b0:35c:f727:8a89 with SMTP id adf61e73a8af0-36403876ee8mr1666667637.41.1764816621600; Wed, 03 Dec 2025 18:50:21 -0800 (PST) Date: Wed, 3 Dec 2025 18:49:59 -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-3-wusamuel@google.com> Subject: [PATCH v1 2/4] bpf: Open coded BPF for wakeup_sources 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 open coded BPF iterators for wakeup_sources, which opens up more options for BPF programs that need to traverse through wakeup_sources. Signed-off-by: Samuel Wu --- kernel/bpf/helpers.c | 3 +++ kernel/bpf/wakeup_source_iter.c | 34 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index db72b96f9c8c..a5f867de6bd6 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -4518,6 +4518,9 @@ BTF_ID_FLAGS(func, bpf_iter_dmabuf_new, KF_ITER_NEW |= KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_iter_dmabuf_next, KF_ITER_NEXT | KF_RET_NULL | KF_S= LEEPABLE) BTF_ID_FLAGS(func, bpf_iter_dmabuf_destroy, KF_ITER_DESTROY | KF_SLEEPABLE) #endif +BTF_ID_FLAGS(func, bpf_iter_wakeup_source_new, KF_ITER_NEW | KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_iter_wakeup_source_next, KF_ITER_NEXT | KF_RET_NULL= | KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_iter_wakeup_source_destroy, KF_ITER_DESTROY | KF_SL= EEPABLE) BTF_ID_FLAGS(func, __bpf_trap) BTF_ID_FLAGS(func, bpf_strcmp); BTF_ID_FLAGS(func, bpf_strcasecmp); diff --git a/kernel/bpf/wakeup_source_iter.c b/kernel/bpf/wakeup_source_ite= r.c index b8719f47428e..e2c0dcbfd02d 100644 --- a/kernel/bpf/wakeup_source_iter.c +++ b/kernel/bpf/wakeup_source_iter.c @@ -90,6 +90,40 @@ static struct bpf_iter_reg bpf_wakeup_source_reg_info = =3D { .seq_info =3D &wakeup_source_iter_seq_info, }; =20 +struct bpf_iter_wakeup_source { + struct wakeup_source *ws; + int srcuidx; +}; + +__bpf_kfunc_start_defs(); + +__bpf_kfunc int bpf_iter_wakeup_source_new(struct bpf_iter_wakeup_source *= it) +{ + it->srcuidx =3D wakeup_sources_read_lock(); + it->ws =3D wakeup_sources_walk_start(); + + return 0; +} + +__bpf_kfunc struct wakeup_source *bpf_iter_wakeup_source_next(struct bpf_i= ter_wakeup_source *it) +{ + struct wakeup_source *prev =3D it->ws; + + if (!prev) + return NULL; + + it->ws =3D wakeup_sources_walk_next(it->ws); + + return prev; +} + +__bpf_kfunc void bpf_iter_wakeup_source_destroy(struct bpf_iter_wakeup_sou= rce *it) +{ + wakeup_sources_read_unlock(it->srcuidx); +} + +__bpf_kfunc_end_defs(); + 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) --=20 2.52.0.177.g9f829587af-goog From nobody Fri Dec 19 19:36:25 2025 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (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 445D7313527 for ; Thu, 4 Dec 2025 02:50:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764816641; cv=none; b=Z/usCiXE6NIkL8VZfuWQZXvpIFuZu/Io/rs+nJjG+qSky/T5LYaysBdTqrWKuk6aSXid+3BAxNKbhd8Djk0O/zzMU+4QSUhw4H5ebkyKj8ZYqo0472x/4/9rwOW5jIXfAYvRuCxWvR/h0u7vYj4bcLz8LEeSdBhwZaM/vbhY7gY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764816641; c=relaxed/simple; bh=Sxb2CNnwB5yz3Nvjw4v4DZy/DWUkxcLvLfxMRg2Odl0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OtD+TOEfLehUhU9ho2cK0i/+kJXu8VBYPaANn/wK7p/gE+gy4ZWD0cOI/hbaDNdZkxcPLxeesuGK7jY+rH9ZM4wPyqkxO16xh8p8YF7SsHEhLhY8cRoKSH4TBKElOKSXKTiw1mNabwQxPYr2QubWYHPjqrbxDyaMhpOFW+iRwnc= 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=jubUj/ax; arc=none smtp.client-ip=209.85.215.201 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="jubUj/ax" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b993eb2701bso415492a12.0 for ; Wed, 03 Dec 2025 18:50:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764816626; x=1765421426; 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=l+aQqCsw+NRuorfpf97PO+5v01PI5CSW8XqAEaIZiTg=; b=jubUj/axkNXpyvKN3Rkl8MIG339rmQmUt/bMdilIPPlZ+hxSeU6ZkCei3heXa5Nqe7 xwvzRTyiEV99CAdYRbOY2VwbLixFNPMSaCgO2L2v5LrwA/NH79kavKAWoSDcFTr9uCsV /+9hlCayGPQNtj9amI09Yfef0z9R1gWpgAgKyHu5HkboRNAWVSi3SMkhlbUPf1ahNzY2 tZPRgsb4N9OmY70g668CM0drHzf4eR9nq9fJsIGXwT+jfW71GcedOERcZGGpWOW/SFfE kPT/IzvKwva27GFxw42AL6ABWg0NTV4wuTxUo897ZWiIjqlOHirQ9gIuGiM6Ck8+yl1F //ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764816626; x=1765421426; 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=l+aQqCsw+NRuorfpf97PO+5v01PI5CSW8XqAEaIZiTg=; b=XdCUIZWeGoSopwiWTLmC6hjvwEEcAaVzvbWBl/ADejJ2TftdbSOgfcdjMAJAhcd3V6 gzjKzdE35+pU08dwgyjsskRpRSuSa750qzNEy63ewjdrsrujThtWXVlBuugiTsATFn7l OsyJOhqRe7XuKO4Dic5DGbIHmiIRoSfJG3obPfU/Vdy7t5YOmAqTiKPOAllssABPVtIl FFuEkjUXqoHmMmwr0D4kT3jjp41tqjZLffhbrFC8LDYMPgEsWqZvqSVOK4+4sLE5T0O8 91gXCL0uk9dK/kBtxHPvxU1394nYUQ7Eqak9vpk0CAWzepPQP6mFiOsuFO5gR/bkNrLp w+kw== X-Forwarded-Encrypted: i=1; AJvYcCUnnYVG3svZ8yD8VuYFHP0Oh5464s76EwAnDb4BEGlLQZKjaHO+PrP+Wj/yZ1V16NUpaL5ot/JRJPVQYMs=@vger.kernel.org X-Gm-Message-State: AOJu0YyvvAzTQ1r6bQ7fvI4cIVCyX5oHULRShwYz3KAfE0r9NvZcjvGY ZBkVyhs0j1J9kLVMON0gYtDpns1ju/TRMab7bJq00nOi2E8gdXyoVsktUjKC4obtilNzS6N2E5i vd5bJch77MWDU8g== X-Google-Smtp-Source: AGHT+IGVVV3e4uaKvdPuVO3KdHUpfuc4ovAm0F/av9uVP3ephWBMsJRols3ojDvenOv6Fn8IlvZue3yTqL3Asw== X-Received: from dlyy13.prod.google.com ([2002:a05:7022:68d:b0:11b:a3a4:c8b9]) (user=wusamuel job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:fa3:b0:119:e56b:98b8 with SMTP id a92af1059eb24-11df649e57cmr1046177c88.31.1764816626176; Wed, 03 Dec 2025 18:50:26 -0800 (PST) Date: Wed, 3 Dec 2025 18:50:00 -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-4-wusamuel@google.com> Subject: [PATCH v1 3/4] selftests/bpf: Add tests for wakeup_sources 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" Sets up the framework to test wakeup_sources iterators using BPF, and adds a few basic tests. Adds several helper functions that for grabbing and releasing a wakelock, abstracting out key functions to setup a framework for testing wakeup_sources. Additionally, adds 3 tests: 1. check_active_count: Checks that stats related to active_count are properly set after several lock/unlock cycles 2. check_sleep_times: Checks that time accounting related to sleep are properly calculated 3. check_no_infinite_reads: Checks that the iterator traversal returns NULL at the end Signed-off-by: Samuel Wu --- tools/testing/selftests/bpf/config | 1 + .../bpf/prog_tests/wakeup_source_iter.c | 281 ++++++++++++++++++ .../selftests/bpf/progs/wakeup_source_iter.c | 70 +++++ 3 files changed, 352 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/wakeup_source_it= er.c create mode 100644 tools/testing/selftests/bpf/progs/wakeup_source_iter.c diff --git a/tools/testing/selftests/bpf/config b/tools/testing/selftests/b= pf/config index 558839e3c185..c12c5e04b81f 100644 --- a/tools/testing/selftests/bpf/config +++ b/tools/testing/selftests/bpf/config @@ -111,6 +111,7 @@ CONFIG_IP6_NF_IPTABLES=3Dy CONFIG_IP6_NF_FILTER=3Dy CONFIG_NF_NAT=3Dy CONFIG_PACKET=3Dy +CONFIG_PM_WAKELOCKS=3Dy CONFIG_RC_CORE=3Dy CONFIG_SAMPLES=3Dy CONFIG_SAMPLE_LIVEPATCH=3Dm diff --git a/tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c b/= tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c new file mode 100644 index 000000000000..5cea4d4458f3 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c @@ -0,0 +1,281 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Google LLC */ + +#include +#include +#include "wakeup_source_iter.skel.h" + +#include +#include +#include +#include +#include +#include + + +/* Sleep for 10ms to ensure active time is > 0 after converting ns to ms*/ +#define TEST_SLEEP_US 10000 +#define TEST_SLEEP_MS (TEST_SLEEP_US / 1000) +#define WAKEUP_SOURCE_NAME_LEN 32 + +static const char test_ws_name[] =3D "bpf_selftest_ws"; +static bool test_ws_created; + +/* + * Creates a new wakeup source by writing to /sys/power/wake_lock. + * This lock persists until explicitly unlocked. + */ +static int lock_ws(const char *name) +{ + int fd; + ssize_t bytes; + + fd =3D open("/sys/power/wake_lock", O_WRONLY); + if (!ASSERT_OK_FD(fd, "open /sys/power/wake_lock")) + return -1; + + bytes =3D write(fd, name, strlen(name)); + close(fd); + if (!ASSERT_EQ(bytes, strlen(name), "write to wake_lock")) + return -1; + + return 0; +} + +/* + * Destroys the ws by writing the same name to /sys/power/wake_unlock. + */ +static void unlock_ws(const char *name) +{ + int fd; + + fd =3D open("/sys/power/wake_unlock", O_WRONLY); + if (!ASSERT_OK_FD(fd, "open /sys/power/wake_unlock")) + goto cleanup; + + write(fd, name, strlen(name)); + +cleanup: + if (fd) + close(fd); +} + +/* + * Setups for testing ws iterators. Will run once prior to suite of tests. + */ +static int setup_test_ws(void) +{ + if (lock_ws(test_ws_name)) + return -1; + test_ws_created =3D true; + + return 0; +} + +/* + * Tears down and cleanups testing ws iterators. WIll run once after the s= uite + * of tests. + */ +static void teardown_test_ws(void) +{ + if (!test_ws_created) + return; + unlock_ws(test_ws_name); + test_ws_created =3D false; +} + +struct WakeupSourceInfo { + char name[WAKEUP_SOURCE_NAME_LEN]; + unsigned long active_count; + long active_time_ms; + unsigned long event_count; + unsigned long expire_count; + long last_change_ms; + long max_time_ms; + long prevent_sleep_time_ms; + long total_time_ms; + unsigned long wakeup_count; +}; + +/* + * Reads and parses one wakeup_source record from the iterator file. + * A record is a single space-delimited line. + * Returns true on success, false on EOF. Asserts internally on errors. + */ +static bool read_ws_info(FILE *iter_file, struct WakeupSourceInfo *ws_info, + char **line) +{ + size_t linesize; + int items; + + if (getline(line, &linesize, iter_file) =3D=3D -1) + return false; + + (*line)[strcspn(*line, "\n")] =3D 0; + + items =3D sscanf(*line, "%s %lu %ld %lu %lu %ld %ld %ld %ld %lu", + ws_info->name, &ws_info->active_count, + &ws_info->active_time_ms, &ws_info->event_count, + &ws_info->expire_count, &ws_info->last_change_ms, + &ws_info->max_time_ms, &ws_info->prevent_sleep_time_ms, + &ws_info->total_time_ms, &ws_info->wakeup_count); + + if (!ASSERT_EQ(items, 10, "read wakeup source info")) + return false; + + if (!ASSERT_LT(strlen(ws_info->name), WAKEUP_SOURCE_NAME_LEN, + "name length")) + return false; + + return true; +} + +static int get_ws_iter_stream(struct wakeup_source_iter *skel, int *iter_f= d, + FILE **iter_file) +{ + *iter_fd =3D bpf_iter_create( + bpf_link__fd(skel->links.wakeup_source_collector)); + if (!ASSERT_OK_FD(*iter_fd, "iter_create")) + return -1; + + *iter_file =3D fdopen(*iter_fd, "r"); + if (!ASSERT_OK_PTR(*iter_file, "fdopen")) + return -1; + + return 0; +} + +static void subtest_ws_iter_check_active_count(struct wakeup_source_iter *= skel) +{ + static const char subtest_ws_name[] =3D "bpf_selftest_ws_active_count"; + const int lock_unlock_cycles =3D 5; + struct WakeupSourceInfo ws_info; + char *line =3D NULL; + bool found_ws =3D false; + FILE *iter_file =3D NULL; + int iter_fd =3D -1; + int i; + + for (i =3D 0; i < lock_unlock_cycles; i++) { + if (!ASSERT_OK(lock_ws(subtest_ws_name), "lock_ws")) + goto cleanup; + unlock_ws(subtest_ws_name); + } + + if (!get_ws_iter_stream(skel, &iter_fd, &iter_file)) + goto cleanup; + + while (read_ws_info(iter_file, &ws_info, &line)) { + if (strcmp(ws_info.name, subtest_ws_name) =3D=3D 0) { + found_ws =3D true; + ASSERT_EQ(ws_info.active_count, lock_unlock_cycles, + "active_count check"); + ASSERT_EQ(ws_info.wakeup_count, lock_unlock_cycles, + "wakeup_count check"); + break; + } + } + + ASSERT_TRUE(found_ws, "found active_count test ws"); + + free(line); +cleanup: + if (iter_file) + fclose(iter_file); + else if (iter_fd >=3D 0) + close(iter_fd); +} + +static void subtest_ws_iter_check_sleep_times(struct wakeup_source_iter *s= kel) +{ + bool found_test_ws =3D false; + struct WakeupSourceInfo ws_info; + char *line =3D NULL; + FILE *iter_file; + int iter_fd; + + if (!get_ws_iter_stream(skel, &iter_fd, &iter_file)) + goto cleanup; + + while (read_ws_info(iter_file, &ws_info, &line)) { + if (strcmp(ws_info.name, test_ws_name) =3D=3D 0) { + found_test_ws =3D true; + ASSERT_GT(ws_info.last_change_ms, 0, + "Expected non-zero last change"); + ASSERT_GE(ws_info.active_time_ms, TEST_SLEEP_MS, + "Expected active time >=3D TEST_SLEEP_MS"); + ASSERT_GE(ws_info.max_time_ms, TEST_SLEEP_MS, + "Expected max time >=3D TEST_SLEEP_MS"); + ASSERT_GE(ws_info.total_time_ms, TEST_SLEEP_MS, + "Expected total time >=3D TEST_SLEEP_MS"); + break; + } + } + + ASSERT_TRUE(found_test_ws, "found_test_ws"); + + free(line); +cleanup: + if (iter_file) + fclose(iter_file); + else if (iter_fd >=3D 0) + close(iter_fd); +} + +static void subtest_ws_iter_check_no_infinite_reads( + struct wakeup_source_iter *skel) +{ + int iter_fd; + char buf[256]; + + iter_fd =3D bpf_iter_create(bpf_link__fd(skel->links.wakeup_source_collec= tor)); + if (!ASSERT_OK_FD(iter_fd, "iter_create")) + return; + + while (read(iter_fd, buf, sizeof(buf)) > 0) + ; + + /* Final read should return 0 */ + ASSERT_EQ(read(iter_fd, buf, sizeof(buf)), 0, "read"); + + close(iter_fd); +} + +void test_wakeup_source_iter(void) +{ + struct wakeup_source_iter *skel =3D NULL; + + if (geteuid() !=3D 0) { + fprintf(stderr, + "Skipping wakeup_source_iter test, requires root\n"); + test__skip(); + return; + } + + skel =3D wakeup_source_iter__open_and_load(); + if (!ASSERT_OK_PTR(skel, "wakeup_source_iter__open_and_load")) + return; + + if (!ASSERT_OK(setup_test_ws(), "setup_test_ws")) + goto destroy; + + if (!ASSERT_OK(wakeup_source_iter__attach(skel), "skel_attach")) + goto destroy; + + /* + * Sleep on O(ms) to ensure that time stats' resolution isn't lost when + * converting from ns to ms + */ + usleep(TEST_SLEEP_US); + + if (test__start_subtest("active_count")) + subtest_ws_iter_check_active_count(skel); + if (test__start_subtest("sleep_times")) + subtest_ws_iter_check_sleep_times(skel); + if (test__start_subtest("no_infinite_reads")) + subtest_ws_iter_check_no_infinite_reads(skel); + +destroy: + teardown_test_ws(); + wakeup_source_iter__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/wakeup_source_iter.c b/tools= /testing/selftests/bpf/progs/wakeup_source_iter.c new file mode 100644 index 000000000000..8c1470f06740 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/wakeup_source_iter.c @@ -0,0 +1,70 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Google LLC */ +#include +#include +#include + +#define NSEC_PER_MS 1000000UL +#define WAKEUP_SOURCE_NAME_LEN 32 + +char _license[] SEC("license") =3D "GPL"; + +SEC("iter/wakeup_source") +int wakeup_source_collector(struct bpf_iter__wakeup_source *ctx) +{ + const struct wakeup_source *ws =3D ctx->wakeup_source; + struct seq_file *seq =3D ctx->meta->seq; + char name[WAKEUP_SOURCE_NAME_LEN] =3D {'\0'}; + const char *pname; + bool active, autosleep_enable; + u64 active_count, event_count, expire_count, wakeup_count; + s64 active_time, curr_time, last_change_time, max_time, + prevent_sleep_time, start_prevent_time, total_time; + + if (!ws) + return 0; + + active =3D BPF_CORE_READ_BITFIELD_PROBED(ws, active); + autosleep_enable =3D BPF_CORE_READ_BITFIELD_PROBED(ws, autosleep_enabled); + if (bpf_core_read(&pname, sizeof(pname), &ws->name) || + bpf_probe_read_kernel_str(name, sizeof(name), pname) < 0 || + bpf_core_read(&active_count, sizeof(active_count), &ws->active_count)= || + bpf_core_read(&event_count, sizeof(event_count), &ws->event_count) || + bpf_core_read(&expire_count, sizeof(expire_count), &ws->expire_count)= || + bpf_core_read(&last_change_time, sizeof(last_change_time), &ws->last_= time) || + bpf_core_read(&max_time, sizeof(max_time), &ws->max_time) || + bpf_core_read( + &prevent_sleep_time, sizeof(prevent_sleep_time), &ws->prevent_sleep_time= ) || + bpf_core_read( + &start_prevent_time, sizeof(start_prevent_time), &ws->start_prevent_time= ) || + bpf_core_read(&total_time, sizeof(total_time), &ws->total_time) || + bpf_core_read(&wakeup_count, sizeof(wakeup_count), &ws->wakeup_count)) + return 0; + + + curr_time =3D bpf_ktime_get_ns(); + active_time =3D 0; + if (active) { + active_time =3D curr_time - last_change_time; + total_time +=3D active_time; + if (active_time > max_time) + max_time =3D active_time; + if (autosleep_enable) + prevent_sleep_time +=3D curr_time - start_prevent_time; + + } + + BPF_SEQ_PRINTF(seq, + "%s %lu %ld %lu %lu %ld %ld %ld %ld %lu\n", + name, + active_count, + active_time / NSEC_PER_MS, + event_count, + expire_count, + last_change_time / NSEC_PER_MS, + max_time / NSEC_PER_MS, + prevent_sleep_time / NSEC_PER_MS, + total_time / NSEC_PER_MS, + wakeup_count); + return 0; +} --=20 2.52.0.177.g9f829587af-goog From nobody Fri Dec 19 19:36:25 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 E7E6A3148B8 for ; Thu, 4 Dec 2025 02:50:33 +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=1764816638; cv=none; b=FftqEyaPIdqogIh5xmbi2o3DaRiz3OvFrivjOrMBXJljlWkleIMjqP/C2n0znHYglHYN7YYVDxTWwMeu0SgjlsI4oAusyBNl8RABBMphhKP8FsINfdOPvme/Jhc1Tl/tMCOYhfaXQxAGXj5PTURGxiApOiTWCKWfsDwCYGGaqlA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764816638; c=relaxed/simple; bh=QCDqMS2iL6NJvwlOUAHOfCZ1wJXr07eE1QzpvwzI2kI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WKkb23Ty5dg0Mx5orVrcxVK3WePqp40OuWQrhmDom20tieJqIQxEoAwwkwbRtHnWWcycu1Dz8F+NxxnVFrUw3vMVbm4CbQw7OesWimks4u81Y7oM9YZYdPF0OFvqxMsFcA51RJbcG1kq9/nS0Et1gmXYqDrerKi9PP71reNAml0= 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=bnSJ4n+B; 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="bnSJ4n+B" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-bb27b422cf2so665993a12.1 for ; Wed, 03 Dec 2025 18:50:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764816631; x=1765421431; 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=3ojdYWQIWL/SNwbxa/IEirZwm3RCurubO9Kbb4a3348=; b=bnSJ4n+Buhvxweu8ey2pvCqDrfyzoP2+Oa8sg9E8tg0pYUxmjs1dXp71W4jLbmbv8F DeoOBwK7UM7eQq7K69/dzzD59LEkb7RUmtKrmnBS4FG9jXvXoqX29JR6ik7bFsIRu73Q fYQg1X65bBN5WWWMXteBqFRF4lpd301wpABf+iilN2MiuRKrZxP7ILrFKt04QkREL5lk XytzKL+Lg0n9/PLIYOAdZUkbPnOOytGfEph1gedwCNAL5+qkOc+s5z3ry4PZ2PjMaoUm o8k8V4JZOfjv5eRndEmKT0FZ/qeSjcK5oNXB2J3JIRM54Jwk0owufM4tTwmsjOwqNAUC anuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764816631; x=1765421431; 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=3ojdYWQIWL/SNwbxa/IEirZwm3RCurubO9Kbb4a3348=; b=RsBaFNur3faGKHuJjIP4mVXlqCcKvGYbFmUutpBtP0rhDoGMssodCHQe9+DwwgdAUc k2Uqr20Gx9xJZHDyQl2bItObKkeHGk23XT/g2bNv7CvVbAfvGpfOG0kJyPwar0umzHSm 4zT3AX184CTQrr1gfPZd8Z66BxCoAfcA01twTb2xtNYWUK8NrQd+NoNn9sdRC6z3REnE pDQkCV2luJS5ePmB+hb+WVpYO01L8wd3L7871AzRXs2C9KXlJqvJJW6LFRgb4Sz54BzX 7clRULTsC1IZlsA9X8Ryc4vpqYrOFCnj+HVjHDg1FDXGtrhLrodiBAThElzsahpI/qqP LJrg== X-Forwarded-Encrypted: i=1; AJvYcCVb9mdIsc7U+DVRiagCJr7BRoEAmzzsdRYWH7GrHsvFJp7VmCPsMEmzITSRlLW5QETMLnpV5P1Z2OIlvtw=@vger.kernel.org X-Gm-Message-State: AOJu0YzBJVGHPSB0GdnZZLhgVdvHzYn4XUjcgYMKBhsYTA/vh+T12DYT srH5/1VOFvjPK0PPqWiCZPeOgeckb30zrN9qD0voykWyFRcuvLen/az2zVe+dcTxEZpRQyXGeXG jOL/1DCo5TljkPA== X-Google-Smtp-Source: AGHT+IE1IL3Y10G3B/sbvEiw46evUWtOH5VTTCB2roL1OD61dgKPHCl58pJNgzJKYfsG+Vq6sx889nyqCyW1Bg== X-Received: from dycsd11.prod.google.com ([2002:a05:693c:310b:b0:2a4:5ebc:ca00]) (user=wusamuel job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:640d:b0:2a4:3593:9689 with SMTP id 5a478bee46e88-2ab92d39d41mr3811313eec.6.1764816631033; Wed, 03 Dec 2025 18:50:31 -0800 (PST) Date: Wed, 3 Dec 2025 18:50:01 -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-5-wusamuel@google.com> Subject: [PATCH v1 4/4] selftests/bpf: Open coded BPF wakeup_sources test 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" This commit introduces a new selftest for the BPF wakeup_source iterator to verify the functionality of open-coded iteration. The test adds: - A new BPF map `test_ws_hash` to track iterated wakeup source names. - A BPF program `iter_ws_for_each` that iterates over wakeup sources and updates the `test_ws_hash` map with the names of found sources. - A new subtest `subtest_ws_iter_check_open_coded` to trigger the BPF program and assert that the expected wakeup sources are marked in the map. Signed-off-by: Samuel Wu --- .../testing/selftests/bpf/bpf_experimental.h | 5 ++ .../bpf/prog_tests/wakeup_source_iter.c | 42 +++++++++++++++++ .../selftests/bpf/progs/wakeup_source_iter.c | 47 +++++++++++++++++++ 3 files changed, 94 insertions(+) diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing= /selftests/bpf/bpf_experimental.h index 2cd9165c7348..e532999b91ca 100644 --- a/tools/testing/selftests/bpf/bpf_experimental.h +++ b/tools/testing/selftests/bpf/bpf_experimental.h @@ -598,6 +598,11 @@ extern void bpf_iter_dmabuf_destroy(struct bpf_iter_dm= abuf *it) __weak __ksym; =20 extern int bpf_cgroup_read_xattr(struct cgroup *cgroup, const char *name__= str, struct bpf_dynptr *value_p) __weak __ksym; +struct bpf_iter_wakeup_source; +extern int bpf_iter_wakeup_source_new(struct bpf_iter_wakeup_source *it) _= _weak __ksym; +extern struct wakeup_source *bpf_iter_wakeup_source_next( + struct bpf_iter_wakeup_source *it) __weak __ksym; +extern void bpf_iter_wakeup_source_destroy(struct bpf_iter_wakeup_source *= it) __weak __ksym; =20 #define PREEMPT_BITS 8 #define SOFTIRQ_BITS 8 diff --git a/tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c b/= tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c index 5cea4d4458f3..b2eaba38cc68 100644 --- a/tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c +++ b/tools/testing/selftests/bpf/prog_tests/wakeup_source_iter.c @@ -241,9 +241,37 @@ static void subtest_ws_iter_check_no_infinite_reads( close(iter_fd); } =20 +static void subtest_ws_iter_check_open_coded(struct wakeup_source_iter *sk= el, + int map_fd) +{ + LIBBPF_OPTS(bpf_test_run_opts, topts); + char key[WAKEUP_SOURCE_NAME_LEN] =3D {0}; + int err, fd; + bool found =3D false; + + fd =3D bpf_program__fd(skel->progs.iter_ws_for_each); + + err =3D bpf_prog_test_run_opts(fd, &topts); + if (!ASSERT_OK(err, "test_run_opts err")) + return; + if (!ASSERT_OK(topts.retval, "test_run_opts retval")) + return; + + strncpy(key, test_ws_name, WAKEUP_SOURCE_NAME_LEN - 1); + + if (!ASSERT_OK(bpf_map_lookup_elem(map_fd, key, &found), + "lookup test_ws_name")) + return; + + ASSERT_TRUE(found, "found test ws via bpf_for_each"); +} + void test_wakeup_source_iter(void) { struct wakeup_source_iter *skel =3D NULL; + int map_fd; + const bool found_val =3D false; + char key[WAKEUP_SOURCE_NAME_LEN] =3D {0}; =20 if (geteuid() !=3D 0) { fprintf(stderr, @@ -256,6 +284,17 @@ void test_wakeup_source_iter(void) if (!ASSERT_OK_PTR(skel, "wakeup_source_iter__open_and_load")) return; =20 + map_fd =3D bpf_map__fd(skel->maps.test_ws_hash); + if (!ASSERT_OK_FD(map_fd, "map_fd")) + goto destroy_skel; + + /* Copy test name to key buffer, ensuring it's zero-padded */ + strncpy(key, test_ws_name, WAKEUP_SOURCE_NAME_LEN - 1); + + if (!ASSERT_OK(bpf_map_update_elem(map_fd, key, &found_val, BPF_ANY), + "insert test_ws_name")) + goto destroy_skel; + if (!ASSERT_OK(setup_test_ws(), "setup_test_ws")) goto destroy; =20 @@ -274,8 +313,11 @@ void test_wakeup_source_iter(void) subtest_ws_iter_check_sleep_times(skel); if (test__start_subtest("no_infinite_reads")) subtest_ws_iter_check_no_infinite_reads(skel); + if (test__start_subtest("open_coded")) + subtest_ws_iter_check_open_coded(skel, map_fd); =20 destroy: teardown_test_ws(); +destroy_skel: wakeup_source_iter__destroy(skel); } diff --git a/tools/testing/selftests/bpf/progs/wakeup_source_iter.c b/tools= /testing/selftests/bpf/progs/wakeup_source_iter.c index 8c1470f06740..7812e773aa0c 100644 --- a/tools/testing/selftests/bpf/progs/wakeup_source_iter.c +++ b/tools/testing/selftests/bpf/progs/wakeup_source_iter.c @@ -9,6 +9,13 @@ =20 char _license[] SEC("license") =3D "GPL"; =20 +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(key_size, WAKEUP_SOURCE_NAME_LEN); + __type(value, bool); + __uint(max_entries, 5); +} test_ws_hash SEC(".maps"); + SEC("iter/wakeup_source") int wakeup_source_collector(struct bpf_iter__wakeup_source *ctx) { @@ -68,3 +75,43 @@ int wakeup_source_collector(struct bpf_iter__wakeup_sour= ce *ctx) wakeup_count); return 0; } + +SEC("syscall") +int iter_ws_for_each(const void *ctx) +{ + struct wakeup_source *ws; + + bpf_for_each(wakeup_source, ws) { + char name[WAKEUP_SOURCE_NAME_LEN]; + const char *pname; + bool *found; + long len; + int i; + + if (bpf_core_read(&pname, sizeof(pname), &ws->name)) + return 1; + + if (!pname) + continue; + + len =3D bpf_probe_read_kernel_str(name, sizeof(name), pname); + if (len < 0) + return 1; + + /* + * Clear the remainder of the buffer to ensure a stable key for + * the map lookup. + */ + bpf_for(i, len, WAKEUP_SOURCE_NAME_LEN) + name[i] =3D 0; + + found =3D bpf_map_lookup_elem(&test_ws_hash, name); + if (found) { + bool t =3D true; + + bpf_map_update_elem(&test_ws_hash, name, &t, BPF_EXIST); + } + } + + return 0; +} --=20 2.52.0.177.g9f829587af-goog