From nobody Fri Dec 5 18:28:39 2025 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4dNKdN13kbz6JFmJ for ; Fri, 05 Dec 2025 18:28:40 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R12" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4dNKdM6fwJz3Sng for ; Fri, 05 Dec 2025 18:28:39 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1764959319; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=DkWlvLl61AJgfUgwjYAdLmncFzmWJzBLNhFAnEKt3Qs=; b=Yy9FxcE2DX8ZH1+FsS41nHzyvVH43RRqz+TkPfOjAiCJwIxAn4Ho2pkUF7+JttCb6RxJIb GpjEMetFbyGMUezm1ZsWWlIlkKFp0QHHslujrea8eEjYodZDzmLHtePVeMLYMI8ASglxP2 zCJt7WqftCSM05pVLU632IVZr2abJpe7tpqnjQiDntWeASsy4D4tA4HTrOKuDRMzkHJeqg k4DhMHL4CZVks3VXaWNQg+RWNDIgPaMd4nxRVflxTAIvSGqH4pi6nejgDKwvhd8Uu2fuF0 XPFgWV5VCUkEXPcIuMkqt7/3wgpu93ryAHdAFlkrIP65yqDi4biIbOW9S2i6Lw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1764959319; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=DkWlvLl61AJgfUgwjYAdLmncFzmWJzBLNhFAnEKt3Qs=; b=AH3evhow5mbLgVAw9ssylKLC3JaQnhSn+wo3vGK7qu3DE10wxywRN9kO5hpSM8lrIx7O5P nqjMWEeumw0iDT7moX/2bYr2sNiQt89jT6K+3Kpytf03TEBNM2ywbkZE/IN/PBvOX7a0qH jSZ54tggbb9fzpaOjIzGY2vxsyY17bkXK8sg7gG1IJQtCIc+uLgW/q4qFPHaXRq8yjx1sq ppDtIXwqzW521T6w3lIU4iuwEDoopB+jBQDbTYpbiziZjdiO2voP9nVKrEP+673WuRtWLb 3XBtA6bx4NVhdcmmiGjrTImVkMN8TMPBQXQMMO9iyYDBBb8W4/b0cQM2O0ukXg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1764959319; a=rsa-sha256; cv=none; b=XltSMvloN0sJAHVdgYw6vVsA/d9nGyP5Wjx4EiGz2LU7NnLaBTn+YFoHIzWckca4ogBkTd 8F9yuYg7otHLwNg6Aea3EkHoqhr41SIafTJ6J52r11Qz+uWtDlALFGj/ff9voa96NlTwwV X2nZN6FWiW4TgmKoGnj70RwaAypa/UjxnC3z6TE4CgiNNaIfuSL2Cv+6INgVn8bevnnWVt Tyix6QPl/kqNuCRjAXS68YtGXp1FCbcGTxhrdqro0wmImP5rqtBk2aUmftWSOkrzuAjIut v787AW6MM2ihK3onpJ+5XQqyxgP8cOwV8kTbzevTIpALVo56DLuUetCKQGYzLg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4dNKdM68HFz62b for ; Fri, 05 Dec 2025 18:28:39 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id f09a by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Fri, 05 Dec 2025 18:28:39 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Dag-Erling=?utf-8?Q? Sm=C3=B8rg?=rav Subject: git: c0aeb34d5044 - stable/14 - bus: Add device_has_children predicate List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-branches@freebsd.org Sender: owner-dev-commits-src-branches@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: c0aeb34d5044f2549572bd3e49468e7df6079d28 Auto-Submitted: auto-generated Date: Fri, 05 Dec 2025 18:28:39 +0000 Message-Id: <69332457.f09a.6482bd0b@gitrepo.freebsd.org> The branch stable/14 has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=c0aeb34d5044f2549572bd3e49468e7df6079d28 commit c0aeb34d5044f2549572bd3e49468e7df6079d28 Author: Dag-Erling Smørgrav AuthorDate: 2025-11-28 09:14:31 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2025-12-05 10:58:57 +0000 bus: Add device_has_children predicate Add a device_has_children() function which can be used to check if a device has children without allocating a list of them which we aren't going to use, or even counting them. Also modify device_get_children() so it can be used to query the count without allocating a list. MFC after: 1 week Sponsored by: Klara, Inc. Sponsored by: NetApp, Inc. Reviewed by: imp, markj Differential Revision: https://reviews.freebsd.org/D53918 (cherry picked from commit 330d4437f2100fc05e576763f4f6a7198080663a) --- share/man/man9/Makefile | 1 + share/man/man9/device_get_children.9 | 47 ++++++++++++++++++++++++++++++------ sys/kern/subr_bus.c | 18 ++++++++++++++ sys/sys/bus.h | 1 + 4 files changed, 59 insertions(+), 8 deletions(-) diff --git a/share/man/man9/Makefile b/share/man/man9/Makefile index 148470e0934f..a0af571a4e9b 100644 --- a/share/man/man9/Makefile +++ b/share/man/man9/Makefile @@ -1031,6 +1031,7 @@ MLINKS+=devfs_set_cdevpriv.9 devfs_clear_cdevpriv.9 \ MLINKS+=device_add_child.9 device_add_child_ordered.9 MLINKS+=device_enable.9 device_disable.9 \ device_enable.9 device_is_enabled.9 +MLINKS+=device_get_children.9 device_has_children.9 MLINKS+=device_get_ivars.9 device_set_ivars.9 MLINKS+=device_get_name.9 device_get_nameunit.9 MLINKS+=device_get_state.9 device_busy.9 \ diff --git a/share/man/man9/device_get_children.9 b/share/man/man9/device_get_children.9 index f9c36971e9b5..45f8f468531b 100644 --- a/share/man/man9/device_get_children.9 +++ b/share/man/man9/device_get_children.9 @@ -1,6 +1,7 @@ .\" -*- nroff -*- .\" .\" Copyright (c) 1998 Doug Rabson +.\" Copyright (c) 2025 Dag-Erling Smørgrav .\" .\" All rights reserved. .\" @@ -26,21 +27,27 @@ .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .\" -.Dd August 23, 2008 +.Dd November 28, 2025 .Dt DEVICE_GET_CHILDREN 9 .Os .Sh NAME -.Nm device_get_children -.Nd get a list of devices connected to a device +.Nm device_get_children , +.Nm device_has_children +.Nd examine devices connected to a device .Sh SYNOPSIS .In sys/param.h .In sys/bus.h .Ft int .Fn device_get_children "device_t dev" "device_t **devlistp" "int *devcountp" +.Ft bool +.Fn device_has_children "device_t dev" .Sh DESCRIPTION -Retrieve a list of all device instances currently connected to -.Pa dev -and return the list in +The +.Nm device_get_children +function retrieves a list of all device instances currently connected +to +.Fa dev . +It returns the list in .Fa *devlistp and the count in .Fa *devcountp . @@ -50,11 +57,35 @@ The memory allocated for the list should be freed using and .Fa devcountp are not changed when an error is returned. +.Pp +As a special case, if +.Fa devlistp +is null, no memory is allocated but the count is still returned in +.Fa *devcountp . +.Pp +The +.Nm device_has_children +function returns +.Dv true +if +.Fa dev +has at least one child and +.Dv false +if it has none. .Sh RETURN VALUES -Zero is returned on success, otherwise an appropriate error is returned. +The +.Nm device_get_children +function returns zero on success and an appropriate error otherwise. +The +.Nm device_has_children +function returns true if the specified device has at least one child +and false otherwise. .Sh SEE ALSO .Xr devclass 9 , .Xr device 9 .Sh AUTHORS +.An -nosplit This manual page was written by -.An Doug Rabson . +.An Doug Rabson Aq Mt dfr@FreeBSD.org +and +.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org . diff --git a/sys/kern/subr_bus.c b/sys/kern/subr_bus.c index 28d37ad8d450..ce7209e5f852 100644 --- a/sys/kern/subr_bus.c +++ b/sys/kern/subr_bus.c @@ -1855,6 +1855,10 @@ device_get_children(device_t dev, device_t **devlistp, int *devcountp) TAILQ_FOREACH(child, &dev->children, link) { count++; } + if (devlistp == NULL) { + *devcountp = count; + return (0); + } if (count == 0) { *devlistp = NULL; *devcountp = 0; @@ -1877,6 +1881,20 @@ device_get_children(device_t dev, device_t **devlistp, int *devcountp) return (0); } +/** + * @brief Check if a device has children + * + * @param dev the device to examine + * + * @rerval true the device has at least one child + * @retval false the device has no children + */ +bool +device_has_children(device_t dev) +{ + return (!TAILQ_EMPTY(&dev->children)); +} + /** * @brief Return the current driver for the device or @c NULL if there * is no driver currently attached diff --git a/sys/sys/bus.h b/sys/sys/bus.h index 91a54b93a1db..c6ed86dc2545 100644 --- a/sys/sys/bus.h +++ b/sys/sys/bus.h @@ -684,6 +684,7 @@ device_state_t device_get_state(device_t dev); int device_get_unit(device_t dev); struct sysctl_ctx_list *device_get_sysctl_ctx(device_t dev); struct sysctl_oid *device_get_sysctl_tree(device_t dev); +bool device_has_children(device_t dev); int device_has_quiet_children(device_t dev); int device_is_alive(device_t dev); /* did probe succeed? */ int device_is_attached(device_t dev); /* did attach succeed? */