From nobody Mon Apr 6 09:10:51 2026 Received: from mail-dy1-f201.google.com (mail-dy1-f201.google.com [74.125.82.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 D20CD3CD8BB for ; Fri, 20 Mar 2026 16:01:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774022480; cv=none; b=Oa4gXU3dKRcXkQtszvabJ384TubaFOgjhk95BBcUGmwAH2Gmh4jnQBOHAnfG0/QVhIXtNnX+AeJ6bzRkEw1jB+ETlfMK8fkumpoE+k4gQ1iKxd0kmV9/rFXIq5x4kNAOpXeTez27QnEYFXscWvXyBlZdUbq/rPRA5zsemV4Ym+g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774022480; c=relaxed/simple; bh=npCuQr8NSn+KqZ3wXFU+9qx8xDptk1bFwIoSkHgUiRI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Lk9FHOTUkYuDDGOys1hOInGxg3dxg4KRsrQm0HQGzcOr/YOf5uKetFeechSAxRqIWhKqqKABKuww7scV/XPuZX425rJye+JrKcCYICEynassOxa8Fmg8dM/uUCACwXX70rOTb5r6rcZvQnRH/ZqJtN6FCREr8CHRWdpLhakHPio= 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=DDxQN51j; arc=none smtp.client-ip=74.125.82.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="DDxQN51j" Received: by mail-dy1-f201.google.com with SMTP id 5a478bee46e88-2c0c85c38acso1121929eec.1 for ; Fri, 20 Mar 2026 09:01:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1774022478; x=1774627278; 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=SyOziUDRkFpvVzGnSUQxGo0qW7loZJVELoAjOyVArzc=; b=DDxQN51jfDCyHl+YeFzhP68f+C/58v+OWKgUxmyeyza9uIecSVt203qZL4PTU45/0W F8Kt7fJPtgfT+D8ERRDJmDPEGQ/yLeQfHJw72cTDzVqOIN6HbaH9q0utPGJvW4zRAe8g SFnUSC9NIdHnliS6kO8IqSbb8eY6owWE+02n5zMitdfhaDeVMvhLTfTdQ79Qq5ix40Qs xVZXe7LVIMR9aM4Co+mlMrSnQU1wn8gBqf2XkVH7H1O+2bm6tJZYo0Ws1slaxdu6846U YnzKKrOh4HUEv8E9S9c1TmFWV2ZL8jBc/YwTSybcQt6urlGa5+ZAdo2y19s6rUbFmR6W me3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774022478; x=1774627278; 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=SyOziUDRkFpvVzGnSUQxGo0qW7loZJVELoAjOyVArzc=; b=CDRwM/irL9IIT40kC5Gv4r/Gp3qQ5RReS9wyrSroN2BGmIBIZK7yQmUp+d7CZjal/H UdggCSj0e51nBfq1A72cSCJmziwNu63nZ7hCxSxARnsEjPa4hPq61PDIHFAAbxbav326 6NZRWI/kv+epfZvJ9w31DvZnPEFoW8bM0DURkbiKc1zFcHdpE4ox9ogHQEgmk5bhLN9t 8inXKf/BpDtkC/i2zjhZH2tnUQ6LTcGuGWMKN0WGjliacsllz2THNUEozOk/gS7Vpr5N 0Vz9wx1NveUF2lNn+RU8ML+M7iFac4qCMlfARjqgfohS38HGLlulhgWnHj+GBa1CrOx7 qFNw== X-Forwarded-Encrypted: i=1; AJvYcCVM0omy4dLuB7PF6HS70AOHGyBFWzxB9TNBT7HY/MYRxcOtRQSZxYwUZt1iQ2uNnc977VMlipulWreEh7g=@vger.kernel.org X-Gm-Message-State: AOJu0Yzeqcu/rhdTdAzenhil6YuwGJwQWlgjPSZTRL0hipzXNPdEQUXn HRkmXL2H27Bpve6pbvv6j9kLbUs/J8Rr3kI+cdndbvvkeHCGnDc3gtmcFKw2K+cK9sphV4u3kge UVCAQUHTfmVpFdw== X-Received: from dycpg9.prod.google.com ([2002:a05:7300:dd09:b0:2c0:f659:16d0]) (user=wusamuel job=prod-delivery.src-stubby-dispatcher) by 2002:a05:693c:2c13:b0:2c1:23d:c732 with SMTP id 5a478bee46e88-2c1096265c1mr2153248eec.14.1774022477845; Fri, 20 Mar 2026 09:01:17 -0700 (PDT) Date: Fri, 20 Mar 2026 09:00:53 -0700 In-Reply-To: <20260320160055.4114055-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: <20260320160055.4114055-1-wusamuel@google.com> X-Mailer: git-send-email 2.53.0.959.g497ff81fa9-goog Message-ID: <20260320160055.4114055-2-wusamuel@google.com> Subject: [PATCH v1 1/2] PM: wakeup: Add kfuncs to lock/unlock wakeup_sources From: Samuel Wu To: "Rafael J. Wysocki" , Len Brown , Pavel Machek , Greg Kroah-Hartman , Danilo Krummrich Cc: andrii@kernel.org, memxor@gmail.com, bpf@vger.kernel.org, Samuel Wu , kernel-team@android.com, linux-pm@vger.kernel.org, driver-core@lists.linux.dev, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add kfuncs to lock/unlock for safe traversal of wakeup sources. Currently, a traversal of wakeup sources require going through /sys/class/wakeup/* or /d/wakeup_sources/*. The repeated syscalls to query sysfs is inefficient, as there can be hundreds of wakeup_sources, with each wakeup source also having multiple attributes. debugfs is unstable and insecure. Adding kfuncs to lock/unlock wakeup sources allows BPF program to safely traverse the wakeup sources list. A new structure, bpf_ws_lock, acts as an opaque wrapper for the SRCU index. The head address of wakeup_sources can be safely resolved through BPF helper functions or variable attributes. Doing the traversal in BPF is significantly more performant, and has an output in a format that the user specifies; this solves all the drawbacks of current interfaces. Signed-off-by: Samuel Wu --- drivers/base/power/wakeup.c | 63 +++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/drivers/base/power/wakeup.c b/drivers/base/power/wakeup.c index b8e48a023bf0..7fc12ce125bc 100644 --- a/drivers/base/power/wakeup.c +++ b/drivers/base/power/wakeup.c @@ -1168,11 +1168,70 @@ static const struct file_operations wakeup_sources_= stats_fops =3D { .release =3D seq_release_private, }; =20 -static int __init wakeup_sources_debugfs_init(void) +#ifdef CONFIG_BPF_SYSCALL +#include + +struct bpf_ws_lock { }; + +__bpf_kfunc_start_defs(); + +/** + * bpf_wakeup_sources_read_lock - Acquire the SRCU lock for wakeup sources + * + * The underlying SRCU lock returns an integer index. However, the BPF ver= ifier + * requires a pointer (PTR_TO_BTF_ID) to strictly track the state of acqui= red + * resources using KF_ACQUIRE and KF_RELEASE semantics. We use an opaque + * structure pointer (struct bpf_ws_lock *) to satisfy the verifier while + * safely encoding the integer index within the pointer address itself. + * + * Return: An opaque pointer encoding the SRCU lock index + 1 (to avoid NU= LL). + */ +__bpf_kfunc struct bpf_ws_lock *bpf_wakeup_sources_read_lock(void) +{ + return (struct bpf_ws_lock *)(long)(wakeup_sources_read_lock() + 1); +} + +/** + * bpf_wakeup_sources_read_unlock - Release the SRCU lock for wakeup sourc= es + * @lock: The opaque pointer returned by bpf_wakeup_sources_read_lock() + * + * The BPF verifier guarantees that @lock is a valid, unreleased pointer f= rom + * the acquire function. We decode the pointer back into the integer SRCU = index + * by subtracting 1 and release the lock. + */ +__bpf_kfunc void bpf_wakeup_sources_read_unlock(struct bpf_ws_lock *lock) +{ + wakeup_sources_read_unlock((int)(long)lock - 1); +} + +__bpf_kfunc_end_defs(); + +BTF_KFUNCS_START(wakeup_source_kfunc_ids) +BTF_ID_FLAGS(func, bpf_wakeup_sources_read_lock, KF_ACQUIRE) +BTF_ID_FLAGS(func, bpf_wakeup_sources_read_unlock, KF_RELEASE) +BTF_KFUNCS_END(wakeup_source_kfunc_ids) + +static const struct btf_kfunc_id_set wakeup_source_kfunc_set =3D { + .owner =3D THIS_MODULE, + .set =3D &wakeup_source_kfunc_ids, +}; + +static void __init wakeup_sources_bpf_init(void) +{ + if (register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &wakeup_source_kfunc= _set)) + pm_pr_dbg("Wakeup: failed to register BTF kfuncs\n"); +} +#else +static inline void wakeup_sources_bpf_init(void) {} +#endif /* CONFIG_BPF_SYSCALL */ + +static int __init wakeup_sources_init(void) { debugfs_create_file("wakeup_sources", 0444, NULL, NULL, &wakeup_sources_stats_fops); + wakeup_sources_bpf_init(); + return 0; } =20 -postcore_initcall(wakeup_sources_debugfs_init); +postcore_initcall(wakeup_sources_init); --=20 2.53.0.959.g497ff81fa9-goog From nobody Mon Apr 6 09:10:51 2026 Received: from mail-dy1-f202.google.com (mail-dy1-f202.google.com [74.125.82.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 A9DFA374172 for ; Fri, 20 Mar 2026 16:01:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774022483; cv=none; b=NLm9vpIrbLMJiAxZiLODuzzA7VCsdzTZ9xtpcVK4B8IvXx0d+9j3ffBUJYht1NkaHJy1Dz7Rbdur+Q6D5lIccLyHHRZ10KPbdFnmUeM/Vjn90ZIEQ7zHSh7KV2n2+LBdS1cNmBg6YoOqc5kuPJZGk5gRGZRQbVMUQCE/6f3DVxI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774022483; c=relaxed/simple; bh=qvip7xD8lU2nv93aISHdb0Yy+GWL3ZEGT+t4OwzRpps=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WSu4rnpzMoYySI3EcMhorLZEJoUq/dr9DPnEc2AEgmP/3XFZXCRVc3Lyg2Vm9nxA3deBJmF+iTGPc6J31JACTYLVsLKgpSE8tlJpZyLB58/LxY3xrAfdk7Su74ncDLeb+a/C+wLMFjiTaY5oWTAldEDT9lTzRpR4w3+0wDlksiE= 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=lZ1j+rnf; arc=none smtp.client-ip=74.125.82.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="lZ1j+rnf" Received: by mail-dy1-f202.google.com with SMTP id 5a478bee46e88-2c0ec884bffso640379eec.1 for ; Fri, 20 Mar 2026 09:01:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1774022482; x=1774627282; 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=+yNIjEjBGn9q2o52lVHe2be8XDbIZIbZnOMsnfNZO68=; b=lZ1j+rnffCaCdFSXunUhynIfkw65gVCTJulV8QuGHVxz08TdqEkdvjLwE2lgHJzh0M IDbKzNYT9vMiis+ftSXvghE02S6VNWZIvKEgAHFYbNa5QBGa14E4ThTKEsKO9Axcx49r ovsn+eAjsfXCCjwNkIE1hrhW0zAqX/XubM3GAiK5xZG9Kn39wCK9Zfmr2n1lolJ+2cSZ 61ARa+ZogUrWd17+tuJbNfd86ldHSeBCgTTw7B8x6NOk4p19YUE6yCBzSlseSRSoOV4e ebUVmcopEkQOArtR6aW2nTX597jVx3nefT6yB7/dM2i3BB4+N6CBT49rNigNx1DCIu4Y SMTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774022482; x=1774627282; 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=+yNIjEjBGn9q2o52lVHe2be8XDbIZIbZnOMsnfNZO68=; b=CkfNQ4e6OnDYMjdtkLqBL+IzhpMQ3uOVUpelUUxKVvEiFLB5B0uXl+dqu2eHBG0ujz bn4ToA5oPxEJM+nq0IrT4RqeybLV+pbXPY8gv4MQ25J/pNK2SNWay+KL2ounH0Zlzh30 7uR/gs/pXxdTNWhUg+70aVs3BNoGb4enSqsmIWT9x72Jn//kvTx7QMKRjEGQAbLYUlMw MIELsvUeBz3tH8XTz9CQobvqGxyWs80jip0p7QbQAOyQ5nPJErGezwMVkgzDMVYvx99W tGZ2/xupR72FaJ76sbQ/rwZf/ELGgOfFNn7d7FB15P70FwAYZRoInuD/WAH9tpPTp+9w 9Lsw== X-Forwarded-Encrypted: i=1; AJvYcCVs/UHaUraFgwwntqM6epPsNqeeZjqdG/3jYy4yfNKgSdAVo2Se7dSPEx78t9AscK+oSz1vEQqBhwmRrR0=@vger.kernel.org X-Gm-Message-State: AOJu0Yy1F1gpozWxW4vvyMQa9Nprg8y/CBfokmijHacC/HwF3A/u7w4T hGsLehcFrcsfGz3509i7v/DwQsbwuo3k3+PMGnJApdZT7wv2g5PQMOb+HFwiY9HBoNEavO4+y6Y blDKM7ZVSw/a6Ew== X-Received: from dybfx32.prod.google.com ([2002:a05:7300:c3a0:b0:2c0:fdbd:f0ce]) (user=wusamuel job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:eab:b0:2c1:564:712a with SMTP id 5a478bee46e88-2c1097b7ab2mr1482908eec.26.1774022481485; Fri, 20 Mar 2026 09:01:21 -0700 (PDT) Date: Fri, 20 Mar 2026 09:00:54 -0700 In-Reply-To: <20260320160055.4114055-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: <20260320160055.4114055-1-wusamuel@google.com> X-Mailer: git-send-email 2.53.0.959.g497ff81fa9-goog Message-ID: <20260320160055.4114055-3-wusamuel@google.com> Subject: [PATCH v1 2/2] PM: Add config flag to gate sysfs wakeup_sources From: Samuel Wu To: "Rafael J. Wysocki" , Pavel Machek , Len Brown , Greg Kroah-Hartman , Danilo Krummrich Cc: andrii@kernel.org, memxor@gmail.com, bpf@vger.kernel.org, Samuel Wu , kernel-team@android.com, linux-pm@vger.kernel.org, driver-core@lists.linux.dev, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a config flag that gates the creation of /sys/class/wakeup_sources/*. This has the benefit of eliminating the work needed to create the nodes and corresponding attributes; and between kernfs, dentry, and malloc, there are some memory savings depending on the number of wakeup sources. Signed-off-by: Samuel Wu --- drivers/base/power/Makefile | 3 ++- drivers/base/power/power.h | 14 ++++++++++++++ kernel/power/Kconfig | 13 +++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/drivers/base/power/Makefile b/drivers/base/power/Makefile index 2989e42d0161..5933dadc0dd9 100644 --- a/drivers/base/power/Makefile +++ b/drivers/base/power/Makefile @@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_PM) +=3D sysfs.o generic_ops.o common.o qos.o runtime.o wakei= rq.o -obj-$(CONFIG_PM_SLEEP) +=3D main.o wakeup.o wakeup_stats.o +obj-$(CONFIG_PM_SLEEP) +=3D main.o wakeup.o +obj-$(CONFIG_PM_WAKEUP_STATS_SYSFS) +=3D wakeup_stats.o obj-$(CONFIG_PM_TRACE_RTC) +=3D trace.o obj-$(CONFIG_HAVE_CLK) +=3D clock_ops.o obj-$(CONFIG_PM_QOS_KUNIT_TEST) +=3D qos-test.o diff --git a/drivers/base/power/power.h b/drivers/base/power/power.h index 922ed457db19..364ca5512b4f 100644 --- a/drivers/base/power/power.h +++ b/drivers/base/power/power.h @@ -125,11 +125,25 @@ static inline bool device_pm_initialized(struct devic= e *dev) } =20 /* drivers/base/power/wakeup_stats.c */ +#ifdef CONFIG_PM_WAKEUP_STATS_SYSFS extern int wakeup_source_sysfs_add(struct device *parent, struct wakeup_source *ws); extern void wakeup_source_sysfs_remove(struct wakeup_source *ws); =20 extern int pm_wakeup_source_sysfs_add(struct device *parent); +#else /* !CONFIG_PM_WAKEUP_STATS_SYSFS */ +static inline int wakeup_source_sysfs_add(struct device *parent, + struct wakeup_source *ws) +{ + return 0; +} +static inline void wakeup_source_sysfs_remove(struct wakeup_source *ws) {} + +static inline int pm_wakeup_source_sysfs_add(struct device *parent) +{ + return 0; +} +#endif /* !CONFIG_PM_WAKEUP_STATS_SYSFS */ =20 #else /* !CONFIG_PM_SLEEP */ =20 diff --git a/kernel/power/Kconfig b/kernel/power/Kconfig index 05337f437cca..6945083ab053 100644 --- a/kernel/power/Kconfig +++ b/kernel/power/Kconfig @@ -202,6 +202,19 @@ config PM_WAKELOCKS_GC depends on PM_WAKELOCKS default y =20 +config PM_WAKEUP_STATS_SYSFS + bool "Sysfs wakeup statistics" + depends on PM_SLEEP + default y + help + Enable this for wakeup statistics in sysfs under /sys/class/wakeup/ + + Disabling this option eliminates the work of creating the wakeup + sources and each of their attributes in sysfs. Depending on the + number of wakeup sources, this can also have a non-negligible memory + impact. Regardless of this config option's value, wakeup statistics + are still available via debugfs and BPF. + config PM_QOS_CPU_SYSTEM_WAKEUP bool "User space interface for CPU system wakeup QoS" depends on CPU_IDLE --=20 2.53.0.959.g497ff81fa9-goog